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> |
| 31 | #include <asm/iommu.h> |
| 32 | #include <asm/hvcall.h> |
| 33 | #include <linux/dma-mapping.h> |
| 34 | #include <linux/interrupt.h> |
| 35 | #include "ibmvscsi.h" |
| 36 | |
| 37 | /* ------------------------------------------------------------ |
| 38 | * Routines for managing the command/response queue |
| 39 | */ |
| 40 | /** |
| 41 | * ibmvscsi_handle_event: - Interrupt handler for crq events |
| 42 | * @irq: number of irq to handle, not used |
| 43 | * @dev_instance: ibmvscsi_host_data of host that received interrupt |
| 44 | * @regs: pt_regs with registers |
| 45 | * |
| 46 | * Disables interrupts and schedules srp_task |
| 47 | * Always returns IRQ_HANDLED |
| 48 | */ |
| 49 | static irqreturn_t ibmvscsi_handle_event(int irq, |
| 50 | void *dev_instance, |
| 51 | struct pt_regs *regs) |
| 52 | { |
| 53 | struct ibmvscsi_host_data *hostdata = |
| 54 | (struct ibmvscsi_host_data *)dev_instance; |
| 55 | vio_disable_interrupts(to_vio_dev(hostdata->dev)); |
| 56 | tasklet_schedule(&hostdata->srp_task); |
| 57 | return IRQ_HANDLED; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * release_crq_queue: - Deallocates data and unregisters CRQ |
| 62 | * @queue: crq_queue to initialize and register |
| 63 | * @host_data: ibmvscsi_host_data of host |
| 64 | * |
| 65 | * Frees irq, deallocates a page for messages, unmaps dma, and unregisters |
| 66 | * the crq with the hypervisor. |
| 67 | */ |
| 68 | void ibmvscsi_release_crq_queue(struct crq_queue *queue, |
| 69 | struct ibmvscsi_host_data *hostdata, |
| 70 | int max_requests) |
| 71 | { |
| 72 | long rc; |
| 73 | struct vio_dev *vdev = to_vio_dev(hostdata->dev); |
| 74 | free_irq(vdev->irq, (void *)hostdata); |
| 75 | tasklet_kill(&hostdata->srp_task); |
| 76 | do { |
| 77 | rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); |
| 78 | } while ((rc == H_Busy) || (H_isLongBusy(rc))); |
| 79 | dma_unmap_single(hostdata->dev, |
| 80 | queue->msg_token, |
| 81 | queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL); |
| 82 | free_page((unsigned long)queue->msgs); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * crq_queue_next_crq: - Returns the next entry in message queue |
| 87 | * @queue: crq_queue to use |
| 88 | * |
| 89 | * Returns pointer to next entry in queue, or NULL if there are no new |
| 90 | * entried in the CRQ. |
| 91 | */ |
| 92 | static struct viosrp_crq *crq_queue_next_crq(struct crq_queue *queue) |
| 93 | { |
| 94 | struct viosrp_crq *crq; |
| 95 | unsigned long flags; |
| 96 | |
| 97 | spin_lock_irqsave(&queue->lock, flags); |
| 98 | crq = &queue->msgs[queue->cur]; |
| 99 | if (crq->valid & 0x80) { |
| 100 | if (++queue->cur == queue->size) |
| 101 | queue->cur = 0; |
| 102 | } else |
| 103 | crq = NULL; |
| 104 | spin_unlock_irqrestore(&queue->lock, flags); |
| 105 | |
| 106 | return crq; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * ibmvscsi_send_crq: - Send a CRQ |
| 111 | * @hostdata: the adapter |
| 112 | * @word1: the first 64 bits of the data |
| 113 | * @word2: the second 64 bits of the data |
| 114 | */ |
| 115 | int ibmvscsi_send_crq(struct ibmvscsi_host_data *hostdata, u64 word1, u64 word2) |
| 116 | { |
| 117 | struct vio_dev *vdev = to_vio_dev(hostdata->dev); |
| 118 | |
| 119 | return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * ibmvscsi_task: - Process srps asynchronously |
| 124 | * @data: ibmvscsi_host_data of host |
| 125 | */ |
| 126 | static void ibmvscsi_task(void *data) |
| 127 | { |
| 128 | struct ibmvscsi_host_data *hostdata = (struct ibmvscsi_host_data *)data; |
| 129 | struct vio_dev *vdev = to_vio_dev(hostdata->dev); |
| 130 | struct viosrp_crq *crq; |
| 131 | int done = 0; |
| 132 | |
| 133 | while (!done) { |
| 134 | /* Pull all the valid messages off the CRQ */ |
| 135 | while ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) { |
| 136 | ibmvscsi_handle_crq(crq, hostdata); |
| 137 | crq->valid = 0x00; |
| 138 | } |
| 139 | |
| 140 | vio_enable_interrupts(vdev); |
| 141 | if ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) { |
| 142 | vio_disable_interrupts(vdev); |
| 143 | ibmvscsi_handle_crq(crq, hostdata); |
| 144 | crq->valid = 0x00; |
| 145 | } else { |
| 146 | done = 1; |
| 147 | } |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * initialize_crq_queue: - Initializes and registers CRQ with hypervisor |
| 153 | * @queue: crq_queue to initialize and register |
| 154 | * @hostdata: ibmvscsi_host_data of host |
| 155 | * |
| 156 | * Allocates a page for messages, maps it for dma, and registers |
| 157 | * the crq with the hypervisor. |
| 158 | * Returns zero on success. |
| 159 | */ |
| 160 | int ibmvscsi_init_crq_queue(struct crq_queue *queue, |
| 161 | struct ibmvscsi_host_data *hostdata, |
| 162 | int max_requests) |
| 163 | { |
| 164 | int rc; |
| 165 | struct vio_dev *vdev = to_vio_dev(hostdata->dev); |
| 166 | |
| 167 | queue->msgs = (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL); |
| 168 | |
| 169 | if (!queue->msgs) |
| 170 | goto malloc_failed; |
| 171 | queue->size = PAGE_SIZE / sizeof(*queue->msgs); |
| 172 | |
| 173 | queue->msg_token = dma_map_single(hostdata->dev, queue->msgs, |
| 174 | queue->size * sizeof(*queue->msgs), |
| 175 | DMA_BIDIRECTIONAL); |
| 176 | |
| 177 | if (dma_mapping_error(queue->msg_token)) |
| 178 | goto map_failed; |
| 179 | |
| 180 | rc = plpar_hcall_norets(H_REG_CRQ, |
| 181 | vdev->unit_address, |
| 182 | queue->msg_token, PAGE_SIZE); |
| 183 | if (rc == 2) { |
| 184 | /* Adapter is good, but other end is not ready */ |
| 185 | printk(KERN_WARNING "ibmvscsi: Partner adapter not ready\n"); |
| 186 | } else if (rc != 0) { |
| 187 | printk(KERN_WARNING "ibmvscsi: Error %d opening adapter\n", rc); |
| 188 | goto reg_crq_failed; |
| 189 | } |
| 190 | |
| 191 | if (request_irq(vdev->irq, |
| 192 | ibmvscsi_handle_event, |
| 193 | 0, "ibmvscsi", (void *)hostdata) != 0) { |
| 194 | printk(KERN_ERR "ibmvscsi: couldn't register irq 0x%x\n", |
| 195 | vdev->irq); |
| 196 | goto req_irq_failed; |
| 197 | } |
| 198 | |
| 199 | rc = vio_enable_interrupts(vdev); |
| 200 | if (rc != 0) { |
| 201 | printk(KERN_ERR "ibmvscsi: Error %d enabling interrupts!!!\n", |
| 202 | rc); |
| 203 | goto req_irq_failed; |
| 204 | } |
| 205 | |
| 206 | queue->cur = 0; |
| 207 | spin_lock_init(&queue->lock); |
| 208 | |
| 209 | tasklet_init(&hostdata->srp_task, (void *)ibmvscsi_task, |
| 210 | (unsigned long)hostdata); |
| 211 | |
| 212 | return 0; |
| 213 | |
| 214 | req_irq_failed: |
| 215 | do { |
| 216 | rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); |
| 217 | } while ((rc == H_Busy) || (H_isLongBusy(rc))); |
| 218 | reg_crq_failed: |
| 219 | dma_unmap_single(hostdata->dev, |
| 220 | queue->msg_token, |
| 221 | queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL); |
| 222 | map_failed: |
| 223 | free_page((unsigned long)queue->msgs); |
| 224 | malloc_failed: |
| 225 | return -1; |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * reset_crq_queue: - resets a crq after a failure |
| 230 | * @queue: crq_queue to initialize and register |
| 231 | * @hostdata: ibmvscsi_host_data of host |
| 232 | * |
| 233 | */ |
| 234 | void ibmvscsi_reset_crq_queue(struct crq_queue *queue, |
| 235 | struct ibmvscsi_host_data *hostdata) |
| 236 | { |
| 237 | int rc; |
| 238 | struct vio_dev *vdev = to_vio_dev(hostdata->dev); |
| 239 | |
| 240 | /* Close the CRQ */ |
| 241 | do { |
| 242 | rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); |
| 243 | } while ((rc == H_Busy) || (H_isLongBusy(rc))); |
| 244 | |
| 245 | /* Clean out the queue */ |
| 246 | memset(queue->msgs, 0x00, PAGE_SIZE); |
| 247 | queue->cur = 0; |
| 248 | |
| 249 | /* And re-open it again */ |
| 250 | rc = plpar_hcall_norets(H_REG_CRQ, |
| 251 | vdev->unit_address, |
| 252 | queue->msg_token, PAGE_SIZE); |
| 253 | if (rc == 2) { |
| 254 | /* Adapter is good, but other end is not ready */ |
| 255 | printk(KERN_WARNING "ibmvscsi: Partner adapter not ready\n"); |
| 256 | } else if (rc != 0) { |
| 257 | printk(KERN_WARNING |
| 258 | "ibmvscsi: couldn't register crq--rc 0x%x\n", rc); |
| 259 | } |
| 260 | } |