| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* ------------------------------------------------------------ | 
|  | 2 | * rpa_vscsi.c | 
|  | 3 | * (C) Copyright IBM Corporation 1994, 2003 | 
|  | 4 | * Authors: Colin DeVilbiss (devilbis@us.ibm.com) | 
|  | 5 | *          Santiago Leon (santil@us.ibm.com) | 
|  | 6 | * | 
|  | 7 | * This program is free software; you can redistribute it and/or modify | 
|  | 8 | * it under the terms of the GNU General Public License as published by | 
|  | 9 | * the Free Software Foundation; either version 2 of the License, or | 
|  | 10 | * (at your option) any later version. | 
|  | 11 | * | 
|  | 12 | * This program is distributed in the hope that it will be useful, | 
|  | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 15 | * GNU General Public License for more details. | 
|  | 16 | * | 
|  | 17 | * You should have received a copy of the GNU General Public License | 
|  | 18 | * along with this program; if not, write to the Free Software | 
|  | 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 | 
|  | 20 | * USA | 
|  | 21 | * | 
|  | 22 | * ------------------------------------------------------------ | 
|  | 23 | * RPA-specific functions of the SCSI host adapter for Virtual I/O devices | 
|  | 24 | * | 
|  | 25 | * This driver allows the Linux SCSI peripheral drivers to directly | 
|  | 26 | * access devices in the hosting partition, either on an iSeries | 
|  | 27 | * hypervisor system or a converged hypervisor system. | 
|  | 28 | */ | 
|  | 29 |  | 
|  | 30 | #include <asm/vio.h> | 
| Stephen Rothwell | 71d276d | 2005-08-17 16:41:44 +1000 | [diff] [blame] | 31 | #include <asm/prom.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <asm/iommu.h> | 
|  | 33 | #include <asm/hvcall.h> | 
|  | 34 | #include <linux/dma-mapping.h> | 
|  | 35 | #include <linux/interrupt.h> | 
|  | 36 | #include "ibmvscsi.h" | 
| Linda Xie | b4687ca | 2005-06-27 17:01:48 -0500 | [diff] [blame] | 37 |  | 
|  | 38 | static char partition_name[97] = "UNKNOWN"; | 
|  | 39 | static unsigned int partition_number = -1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 |  | 
|  | 41 | /* ------------------------------------------------------------ | 
|  | 42 | * Routines for managing the command/response queue | 
|  | 43 | */ | 
|  | 44 | /** | 
|  | 45 | * ibmvscsi_handle_event: - Interrupt handler for crq events | 
|  | 46 | * @irq:	number of irq to handle, not used | 
|  | 47 | * @dev_instance: ibmvscsi_host_data of host that received interrupt | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | * | 
|  | 49 | * Disables interrupts and schedules srp_task | 
|  | 50 | * Always returns IRQ_HANDLED | 
|  | 51 | */ | 
| David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 52 | static irqreturn_t ibmvscsi_handle_event(int irq, void *dev_instance) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | { | 
|  | 54 | struct ibmvscsi_host_data *hostdata = | 
|  | 55 | (struct ibmvscsi_host_data *)dev_instance; | 
|  | 56 | vio_disable_interrupts(to_vio_dev(hostdata->dev)); | 
|  | 57 | tasklet_schedule(&hostdata->srp_task); | 
|  | 58 | return IRQ_HANDLED; | 
|  | 59 | } | 
|  | 60 |  | 
|  | 61 | /** | 
|  | 62 | * release_crq_queue: - Deallocates data and unregisters CRQ | 
|  | 63 | * @queue:	crq_queue to initialize and register | 
|  | 64 | * @host_data:	ibmvscsi_host_data of host | 
|  | 65 | * | 
|  | 66 | * Frees irq, deallocates a page for messages, unmaps dma, and unregisters | 
|  | 67 | * the crq with the hypervisor. | 
|  | 68 | */ | 
|  | 69 | void ibmvscsi_release_crq_queue(struct crq_queue *queue, | 
|  | 70 | struct ibmvscsi_host_data *hostdata, | 
|  | 71 | int max_requests) | 
|  | 72 | { | 
|  | 73 | long rc; | 
|  | 74 | struct vio_dev *vdev = to_vio_dev(hostdata->dev); | 
|  | 75 | free_irq(vdev->irq, (void *)hostdata); | 
|  | 76 | tasklet_kill(&hostdata->srp_task); | 
|  | 77 | do { | 
|  | 78 | rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); | 
| Segher Boessenkool | 706c8c9 | 2006-03-30 14:49:40 +0200 | [diff] [blame] | 79 | } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc))); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | dma_unmap_single(hostdata->dev, | 
|  | 81 | queue->msg_token, | 
|  | 82 | queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL); | 
|  | 83 | free_page((unsigned long)queue->msgs); | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | /** | 
|  | 87 | * crq_queue_next_crq: - Returns the next entry in message queue | 
|  | 88 | * @queue:	crq_queue to use | 
|  | 89 | * | 
|  | 90 | * Returns pointer to next entry in queue, or NULL if there are no new | 
|  | 91 | * entried in the CRQ. | 
|  | 92 | */ | 
|  | 93 | static struct viosrp_crq *crq_queue_next_crq(struct crq_queue *queue) | 
|  | 94 | { | 
|  | 95 | struct viosrp_crq *crq; | 
|  | 96 | unsigned long flags; | 
|  | 97 |  | 
|  | 98 | spin_lock_irqsave(&queue->lock, flags); | 
|  | 99 | crq = &queue->msgs[queue->cur]; | 
|  | 100 | if (crq->valid & 0x80) { | 
|  | 101 | if (++queue->cur == queue->size) | 
|  | 102 | queue->cur = 0; | 
|  | 103 | } else | 
|  | 104 | crq = NULL; | 
|  | 105 | spin_unlock_irqrestore(&queue->lock, flags); | 
|  | 106 |  | 
|  | 107 | return crq; | 
|  | 108 | } | 
|  | 109 |  | 
|  | 110 | /** | 
|  | 111 | * ibmvscsi_send_crq: - Send a CRQ | 
|  | 112 | * @hostdata:	the adapter | 
|  | 113 | * @word1:	the first 64 bits of the data | 
|  | 114 | * @word2:	the second 64 bits of the data | 
|  | 115 | */ | 
|  | 116 | int ibmvscsi_send_crq(struct ibmvscsi_host_data *hostdata, u64 word1, u64 word2) | 
|  | 117 | { | 
|  | 118 | struct vio_dev *vdev = to_vio_dev(hostdata->dev); | 
|  | 119 |  | 
|  | 120 | return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2); | 
|  | 121 | } | 
|  | 122 |  | 
|  | 123 | /** | 
|  | 124 | * ibmvscsi_task: - Process srps asynchronously | 
|  | 125 | * @data:	ibmvscsi_host_data of host | 
|  | 126 | */ | 
|  | 127 | static void ibmvscsi_task(void *data) | 
|  | 128 | { | 
|  | 129 | struct ibmvscsi_host_data *hostdata = (struct ibmvscsi_host_data *)data; | 
|  | 130 | struct vio_dev *vdev = to_vio_dev(hostdata->dev); | 
|  | 131 | struct viosrp_crq *crq; | 
|  | 132 | int done = 0; | 
|  | 133 |  | 
|  | 134 | while (!done) { | 
|  | 135 | /* Pull all the valid messages off the CRQ */ | 
|  | 136 | while ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) { | 
|  | 137 | ibmvscsi_handle_crq(crq, hostdata); | 
|  | 138 | crq->valid = 0x00; | 
|  | 139 | } | 
|  | 140 |  | 
|  | 141 | vio_enable_interrupts(vdev); | 
|  | 142 | if ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) { | 
|  | 143 | vio_disable_interrupts(vdev); | 
|  | 144 | ibmvscsi_handle_crq(crq, hostdata); | 
|  | 145 | crq->valid = 0x00; | 
|  | 146 | } else { | 
|  | 147 | done = 1; | 
|  | 148 | } | 
|  | 149 | } | 
|  | 150 | } | 
|  | 151 |  | 
| Linda Xie | b4687ca | 2005-06-27 17:01:48 -0500 | [diff] [blame] | 152 | static void gather_partition_info(void) | 
|  | 153 | { | 
|  | 154 | struct device_node *rootdn; | 
|  | 155 |  | 
| Jeremy Kerr | 294ef16 | 2006-07-12 15:40:51 +1000 | [diff] [blame] | 156 | const char *ppartition_name; | 
|  | 157 | const unsigned int *p_number_ptr; | 
| Linda Xie | b4687ca | 2005-06-27 17:01:48 -0500 | [diff] [blame] | 158 |  | 
|  | 159 | /* Retrieve information about this partition */ | 
| Stephen Rothwell | 8c8dc32 | 2007-04-24 13:50:55 +1000 | [diff] [blame] | 160 | rootdn = of_find_node_by_path("/"); | 
| Linda Xie | b4687ca | 2005-06-27 17:01:48 -0500 | [diff] [blame] | 161 | if (!rootdn) { | 
|  | 162 | return; | 
|  | 163 | } | 
|  | 164 |  | 
| Stephen Rothwell | 40cd3a4 | 2007-05-01 13:54:02 +1000 | [diff] [blame] | 165 | ppartition_name = of_get_property(rootdn, "ibm,partition-name", NULL); | 
| Linda Xie | b4687ca | 2005-06-27 17:01:48 -0500 | [diff] [blame] | 166 | if (ppartition_name) | 
|  | 167 | strncpy(partition_name, ppartition_name, | 
|  | 168 | sizeof(partition_name)); | 
| Stephen Rothwell | 40cd3a4 | 2007-05-01 13:54:02 +1000 | [diff] [blame] | 169 | p_number_ptr = of_get_property(rootdn, "ibm,partition-no", NULL); | 
| Linda Xie | b4687ca | 2005-06-27 17:01:48 -0500 | [diff] [blame] | 170 | if (p_number_ptr) | 
|  | 171 | partition_number = *p_number_ptr; | 
| Stephen Rothwell | 8c8dc32 | 2007-04-24 13:50:55 +1000 | [diff] [blame] | 172 | of_node_put(rootdn); | 
| Linda Xie | b4687ca | 2005-06-27 17:01:48 -0500 | [diff] [blame] | 173 | } | 
|  | 174 |  | 
|  | 175 | static void set_adapter_info(struct ibmvscsi_host_data *hostdata) | 
|  | 176 | { | 
|  | 177 | memset(&hostdata->madapter_info, 0x00, | 
|  | 178 | sizeof(hostdata->madapter_info)); | 
|  | 179 |  | 
|  | 180 | printk(KERN_INFO "rpa_vscsi: SPR_VERSION: %s\n", SRP_VERSION); | 
|  | 181 | strcpy(hostdata->madapter_info.srp_version, SRP_VERSION); | 
|  | 182 |  | 
|  | 183 | strncpy(hostdata->madapter_info.partition_name, partition_name, | 
|  | 184 | sizeof(hostdata->madapter_info.partition_name)); | 
|  | 185 |  | 
|  | 186 | hostdata->madapter_info.partition_number = partition_number; | 
|  | 187 |  | 
|  | 188 | hostdata->madapter_info.mad_version = 1; | 
|  | 189 | hostdata->madapter_info.os_type = 2; | 
|  | 190 | } | 
|  | 191 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | /** | 
|  | 193 | * initialize_crq_queue: - Initializes and registers CRQ with hypervisor | 
|  | 194 | * @queue:	crq_queue to initialize and register | 
|  | 195 | * @hostdata:	ibmvscsi_host_data of host | 
|  | 196 | * | 
|  | 197 | * Allocates a page for messages, maps it for dma, and registers | 
|  | 198 | * the crq with the hypervisor. | 
|  | 199 | * Returns zero on success. | 
|  | 200 | */ | 
|  | 201 | int ibmvscsi_init_crq_queue(struct crq_queue *queue, | 
|  | 202 | struct ibmvscsi_host_data *hostdata, | 
|  | 203 | int max_requests) | 
|  | 204 | { | 
|  | 205 | int rc; | 
| Dave C Boutcher | cefbda2 | 2006-06-12 21:22:51 -0500 | [diff] [blame] | 206 | int retrc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | struct vio_dev *vdev = to_vio_dev(hostdata->dev); | 
|  | 208 |  | 
|  | 209 | queue->msgs = (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL); | 
|  | 210 |  | 
|  | 211 | if (!queue->msgs) | 
|  | 212 | goto malloc_failed; | 
|  | 213 | queue->size = PAGE_SIZE / sizeof(*queue->msgs); | 
|  | 214 |  | 
|  | 215 | queue->msg_token = dma_map_single(hostdata->dev, queue->msgs, | 
|  | 216 | queue->size * sizeof(*queue->msgs), | 
|  | 217 | DMA_BIDIRECTIONAL); | 
|  | 218 |  | 
|  | 219 | if (dma_mapping_error(queue->msg_token)) | 
|  | 220 | goto map_failed; | 
|  | 221 |  | 
| Linda Xie | b4687ca | 2005-06-27 17:01:48 -0500 | [diff] [blame] | 222 | gather_partition_info(); | 
|  | 223 | set_adapter_info(hostdata); | 
|  | 224 |  | 
| Dave C Boutcher | cefbda2 | 2006-06-12 21:22:51 -0500 | [diff] [blame] | 225 | retrc = rc = plpar_hcall_norets(H_REG_CRQ, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | vdev->unit_address, | 
|  | 227 | queue->msg_token, PAGE_SIZE); | 
| Segher Boessenkool | 706c8c9 | 2006-03-30 14:49:40 +0200 | [diff] [blame] | 228 | if (rc == H_RESOURCE) | 
| Dave C Boutcher | bb58596 | 2005-11-15 09:53:00 -0600 | [diff] [blame] | 229 | /* maybe kexecing and resource is busy. try a reset */ | 
|  | 230 | rc = ibmvscsi_reset_crq_queue(queue, | 
|  | 231 | hostdata); | 
|  | 232 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | if (rc == 2) { | 
|  | 234 | /* Adapter is good, but other end is not ready */ | 
|  | 235 | printk(KERN_WARNING "ibmvscsi: Partner adapter not ready\n"); | 
| Dave C Boutcher | ae0fda0 | 2006-07-06 22:08:49 -0500 | [diff] [blame] | 236 | retrc = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | } else if (rc != 0) { | 
|  | 238 | printk(KERN_WARNING "ibmvscsi: Error %d opening adapter\n", rc); | 
|  | 239 | goto reg_crq_failed; | 
|  | 240 | } | 
|  | 241 |  | 
|  | 242 | if (request_irq(vdev->irq, | 
|  | 243 | ibmvscsi_handle_event, | 
|  | 244 | 0, "ibmvscsi", (void *)hostdata) != 0) { | 
|  | 245 | printk(KERN_ERR "ibmvscsi: couldn't register irq 0x%x\n", | 
|  | 246 | vdev->irq); | 
|  | 247 | goto req_irq_failed; | 
|  | 248 | } | 
|  | 249 |  | 
|  | 250 | rc = vio_enable_interrupts(vdev); | 
|  | 251 | if (rc != 0) { | 
|  | 252 | printk(KERN_ERR "ibmvscsi:  Error %d enabling interrupts!!!\n", | 
|  | 253 | rc); | 
|  | 254 | goto req_irq_failed; | 
|  | 255 | } | 
|  | 256 |  | 
|  | 257 | queue->cur = 0; | 
|  | 258 | spin_lock_init(&queue->lock); | 
|  | 259 |  | 
|  | 260 | tasklet_init(&hostdata->srp_task, (void *)ibmvscsi_task, | 
|  | 261 | (unsigned long)hostdata); | 
|  | 262 |  | 
| Dave C Boutcher | cefbda2 | 2006-06-12 21:22:51 -0500 | [diff] [blame] | 263 | return retrc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 |  | 
|  | 265 | req_irq_failed: | 
|  | 266 | do { | 
|  | 267 | rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); | 
| Segher Boessenkool | 706c8c9 | 2006-03-30 14:49:40 +0200 | [diff] [blame] | 268 | } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc))); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | reg_crq_failed: | 
|  | 270 | dma_unmap_single(hostdata->dev, | 
|  | 271 | queue->msg_token, | 
|  | 272 | queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL); | 
|  | 273 | map_failed: | 
|  | 274 | free_page((unsigned long)queue->msgs); | 
|  | 275 | malloc_failed: | 
|  | 276 | return -1; | 
|  | 277 | } | 
|  | 278 |  | 
|  | 279 | /** | 
| Dave C Boutcher | 2b541f8 | 2006-01-19 13:34:44 -0600 | [diff] [blame] | 280 | * reenable_crq_queue: - reenables a crq after | 
|  | 281 | * @queue:	crq_queue to initialize and register | 
|  | 282 | * @hostdata:	ibmvscsi_host_data of host | 
|  | 283 | * | 
|  | 284 | */ | 
|  | 285 | int ibmvscsi_reenable_crq_queue(struct crq_queue *queue, | 
|  | 286 | struct ibmvscsi_host_data *hostdata) | 
|  | 287 | { | 
|  | 288 | int rc; | 
|  | 289 | struct vio_dev *vdev = to_vio_dev(hostdata->dev); | 
|  | 290 |  | 
|  | 291 | /* Re-enable the CRQ */ | 
|  | 292 | do { | 
|  | 293 | rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address); | 
| Segher Boessenkool | 706c8c9 | 2006-03-30 14:49:40 +0200 | [diff] [blame] | 294 | } while ((rc == H_IN_PROGRESS) || (rc == H_BUSY) || (H_IS_LONG_BUSY(rc))); | 
| Dave C Boutcher | 2b541f8 | 2006-01-19 13:34:44 -0600 | [diff] [blame] | 295 |  | 
|  | 296 | if (rc) | 
|  | 297 | printk(KERN_ERR "ibmvscsi: Error %d enabling adapter\n", rc); | 
|  | 298 | return rc; | 
|  | 299 | } | 
|  | 300 |  | 
|  | 301 | /** | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | * reset_crq_queue: - resets a crq after a failure | 
|  | 303 | * @queue:	crq_queue to initialize and register | 
|  | 304 | * @hostdata:	ibmvscsi_host_data of host | 
|  | 305 | * | 
|  | 306 | */ | 
| Dave C Boutcher | bb58596 | 2005-11-15 09:53:00 -0600 | [diff] [blame] | 307 | int ibmvscsi_reset_crq_queue(struct crq_queue *queue, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | struct ibmvscsi_host_data *hostdata) | 
|  | 309 | { | 
|  | 310 | int rc; | 
|  | 311 | struct vio_dev *vdev = to_vio_dev(hostdata->dev); | 
|  | 312 |  | 
|  | 313 | /* Close the CRQ */ | 
|  | 314 | do { | 
|  | 315 | rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); | 
| Segher Boessenkool | 706c8c9 | 2006-03-30 14:49:40 +0200 | [diff] [blame] | 316 | } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc))); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 |  | 
|  | 318 | /* Clean out the queue */ | 
|  | 319 | memset(queue->msgs, 0x00, PAGE_SIZE); | 
|  | 320 | queue->cur = 0; | 
|  | 321 |  | 
| Linda Xie | b4687ca | 2005-06-27 17:01:48 -0500 | [diff] [blame] | 322 | set_adapter_info(hostdata); | 
|  | 323 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | /* And re-open it again */ | 
|  | 325 | rc = plpar_hcall_norets(H_REG_CRQ, | 
|  | 326 | vdev->unit_address, | 
|  | 327 | queue->msg_token, PAGE_SIZE); | 
|  | 328 | if (rc == 2) { | 
|  | 329 | /* Adapter is good, but other end is not ready */ | 
|  | 330 | printk(KERN_WARNING "ibmvscsi: Partner adapter not ready\n"); | 
|  | 331 | } else if (rc != 0) { | 
|  | 332 | printk(KERN_WARNING | 
|  | 333 | "ibmvscsi: couldn't register crq--rc 0x%x\n", rc); | 
|  | 334 | } | 
| Dave C Boutcher | bb58596 | 2005-11-15 09:53:00 -0600 | [diff] [blame] | 335 | return rc; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | } |