]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
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> | |
71d276d7 | 31 | #include <asm/prom.h> |
1da177e4 LT |
32 | #include <asm/iommu.h> |
33 | #include <asm/hvcall.h> | |
34 | #include <linux/dma-mapping.h> | |
5a0e3ad6 | 35 | #include <linux/gfp.h> |
1da177e4 LT |
36 | #include <linux/interrupt.h> |
37 | #include "ibmvscsi.h" | |
b4687ca4 LX |
38 | |
39 | static char partition_name[97] = "UNKNOWN"; | |
40 | static unsigned int partition_number = -1; | |
1da177e4 LT |
41 | |
42 | /* ------------------------------------------------------------ | |
43 | * Routines for managing the command/response queue | |
44 | */ | |
45 | /** | |
d3849d51 | 46 | * rpavscsi_handle_event: - Interrupt handler for crq events |
1da177e4 LT |
47 | * @irq: number of irq to handle, not used |
48 | * @dev_instance: ibmvscsi_host_data of host that received interrupt | |
1da177e4 LT |
49 | * |
50 | * Disables interrupts and schedules srp_task | |
51 | * Always returns IRQ_HANDLED | |
52 | */ | |
d3849d51 | 53 | static irqreturn_t rpavscsi_handle_event(int irq, void *dev_instance) |
1da177e4 LT |
54 | { |
55 | struct ibmvscsi_host_data *hostdata = | |
56 | (struct ibmvscsi_host_data *)dev_instance; | |
57 | vio_disable_interrupts(to_vio_dev(hostdata->dev)); | |
58 | tasklet_schedule(&hostdata->srp_task); | |
59 | return IRQ_HANDLED; | |
60 | } | |
61 | ||
62 | /** | |
63 | * release_crq_queue: - Deallocates data and unregisters CRQ | |
64 | * @queue: crq_queue to initialize and register | |
65 | * @host_data: ibmvscsi_host_data of host | |
66 | * | |
67 | * Frees irq, deallocates a page for messages, unmaps dma, and unregisters | |
68 | * the crq with the hypervisor. | |
69 | */ | |
d3849d51 DW |
70 | static void rpavscsi_release_crq_queue(struct crq_queue *queue, |
71 | struct ibmvscsi_host_data *hostdata, | |
72 | int max_requests) | |
1da177e4 LT |
73 | { |
74 | long rc; | |
75 | struct vio_dev *vdev = to_vio_dev(hostdata->dev); | |
76 | free_irq(vdev->irq, (void *)hostdata); | |
77 | tasklet_kill(&hostdata->srp_task); | |
78 | do { | |
79 | rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); | |
706c8c93 | 80 | } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc))); |
1da177e4 LT |
81 | dma_unmap_single(hostdata->dev, |
82 | queue->msg_token, | |
83 | queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL); | |
84 | free_page((unsigned long)queue->msgs); | |
85 | } | |
86 | ||
87 | /** | |
88 | * crq_queue_next_crq: - Returns the next entry in message queue | |
89 | * @queue: crq_queue to use | |
90 | * | |
91 | * Returns pointer to next entry in queue, or NULL if there are no new | |
92 | * entried in the CRQ. | |
93 | */ | |
94 | static struct viosrp_crq *crq_queue_next_crq(struct crq_queue *queue) | |
95 | { | |
96 | struct viosrp_crq *crq; | |
97 | unsigned long flags; | |
98 | ||
99 | spin_lock_irqsave(&queue->lock, flags); | |
100 | crq = &queue->msgs[queue->cur]; | |
101 | if (crq->valid & 0x80) { | |
102 | if (++queue->cur == queue->size) | |
103 | queue->cur = 0; | |
104 | } else | |
105 | crq = NULL; | |
106 | spin_unlock_irqrestore(&queue->lock, flags); | |
107 | ||
108 | return crq; | |
109 | } | |
110 | ||
111 | /** | |
d3849d51 | 112 | * rpavscsi_send_crq: - Send a CRQ |
1da177e4 LT |
113 | * @hostdata: the adapter |
114 | * @word1: the first 64 bits of the data | |
115 | * @word2: the second 64 bits of the data | |
116 | */ | |
d3849d51 DW |
117 | static int rpavscsi_send_crq(struct ibmvscsi_host_data *hostdata, |
118 | u64 word1, u64 word2) | |
1da177e4 LT |
119 | { |
120 | struct vio_dev *vdev = to_vio_dev(hostdata->dev); | |
121 | ||
122 | return plpar_hcall_norets(H_SEND_CRQ, vdev->unit_address, word1, word2); | |
123 | } | |
124 | ||
125 | /** | |
d3849d51 | 126 | * rpavscsi_task: - Process srps asynchronously |
1da177e4 LT |
127 | * @data: ibmvscsi_host_data of host |
128 | */ | |
d3849d51 | 129 | static void rpavscsi_task(void *data) |
1da177e4 LT |
130 | { |
131 | struct ibmvscsi_host_data *hostdata = (struct ibmvscsi_host_data *)data; | |
132 | struct vio_dev *vdev = to_vio_dev(hostdata->dev); | |
133 | struct viosrp_crq *crq; | |
134 | int done = 0; | |
135 | ||
136 | while (!done) { | |
137 | /* Pull all the valid messages off the CRQ */ | |
138 | while ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) { | |
139 | ibmvscsi_handle_crq(crq, hostdata); | |
140 | crq->valid = 0x00; | |
141 | } | |
142 | ||
143 | vio_enable_interrupts(vdev); | |
144 | if ((crq = crq_queue_next_crq(&hostdata->queue)) != NULL) { | |
145 | vio_disable_interrupts(vdev); | |
146 | ibmvscsi_handle_crq(crq, hostdata); | |
147 | crq->valid = 0x00; | |
148 | } else { | |
149 | done = 1; | |
150 | } | |
151 | } | |
152 | } | |
153 | ||
b4687ca4 LX |
154 | static void gather_partition_info(void) |
155 | { | |
156 | struct device_node *rootdn; | |
157 | ||
294ef16a JK |
158 | const char *ppartition_name; |
159 | const unsigned int *p_number_ptr; | |
b4687ca4 LX |
160 | |
161 | /* Retrieve information about this partition */ | |
8c8dc322 | 162 | rootdn = of_find_node_by_path("/"); |
b4687ca4 LX |
163 | if (!rootdn) { |
164 | return; | |
165 | } | |
166 | ||
40cd3a45 | 167 | ppartition_name = of_get_property(rootdn, "ibm,partition-name", NULL); |
b4687ca4 LX |
168 | if (ppartition_name) |
169 | strncpy(partition_name, ppartition_name, | |
170 | sizeof(partition_name)); | |
40cd3a45 | 171 | p_number_ptr = of_get_property(rootdn, "ibm,partition-no", NULL); |
b4687ca4 LX |
172 | if (p_number_ptr) |
173 | partition_number = *p_number_ptr; | |
8c8dc322 | 174 | of_node_put(rootdn); |
b4687ca4 LX |
175 | } |
176 | ||
177 | static void set_adapter_info(struct ibmvscsi_host_data *hostdata) | |
178 | { | |
179 | memset(&hostdata->madapter_info, 0x00, | |
180 | sizeof(hostdata->madapter_info)); | |
181 | ||
6c0a60ec | 182 | dev_info(hostdata->dev, "SRP_VERSION: %s\n", SRP_VERSION); |
b4687ca4 LX |
183 | strcpy(hostdata->madapter_info.srp_version, SRP_VERSION); |
184 | ||
185 | strncpy(hostdata->madapter_info.partition_name, partition_name, | |
186 | sizeof(hostdata->madapter_info.partition_name)); | |
187 | ||
188 | hostdata->madapter_info.partition_number = partition_number; | |
189 | ||
190 | hostdata->madapter_info.mad_version = 1; | |
191 | hostdata->madapter_info.os_type = 2; | |
192 | } | |
193 | ||
d3849d51 DW |
194 | /** |
195 | * reset_crq_queue: - resets a crq after a failure | |
196 | * @queue: crq_queue to initialize and register | |
197 | * @hostdata: ibmvscsi_host_data of host | |
198 | * | |
199 | */ | |
200 | static int rpavscsi_reset_crq_queue(struct crq_queue *queue, | |
201 | struct ibmvscsi_host_data *hostdata) | |
202 | { | |
203 | int rc; | |
204 | struct vio_dev *vdev = to_vio_dev(hostdata->dev); | |
205 | ||
206 | /* Close the CRQ */ | |
207 | do { | |
208 | rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); | |
209 | } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc))); | |
210 | ||
211 | /* Clean out the queue */ | |
212 | memset(queue->msgs, 0x00, PAGE_SIZE); | |
213 | queue->cur = 0; | |
214 | ||
215 | set_adapter_info(hostdata); | |
216 | ||
217 | /* And re-open it again */ | |
218 | rc = plpar_hcall_norets(H_REG_CRQ, | |
219 | vdev->unit_address, | |
220 | queue->msg_token, PAGE_SIZE); | |
221 | if (rc == 2) { | |
222 | /* Adapter is good, but other end is not ready */ | |
223 | dev_warn(hostdata->dev, "Partner adapter not ready\n"); | |
224 | } else if (rc != 0) { | |
225 | dev_warn(hostdata->dev, "couldn't register crq--rc 0x%x\n", rc); | |
226 | } | |
227 | return rc; | |
228 | } | |
229 | ||
1da177e4 LT |
230 | /** |
231 | * initialize_crq_queue: - Initializes and registers CRQ with hypervisor | |
232 | * @queue: crq_queue to initialize and register | |
233 | * @hostdata: ibmvscsi_host_data of host | |
234 | * | |
235 | * Allocates a page for messages, maps it for dma, and registers | |
236 | * the crq with the hypervisor. | |
237 | * Returns zero on success. | |
238 | */ | |
d3849d51 DW |
239 | static int rpavscsi_init_crq_queue(struct crq_queue *queue, |
240 | struct ibmvscsi_host_data *hostdata, | |
241 | int max_requests) | |
1da177e4 LT |
242 | { |
243 | int rc; | |
cefbda2d | 244 | int retrc; |
1da177e4 LT |
245 | struct vio_dev *vdev = to_vio_dev(hostdata->dev); |
246 | ||
247 | queue->msgs = (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL); | |
248 | ||
249 | if (!queue->msgs) | |
250 | goto malloc_failed; | |
251 | queue->size = PAGE_SIZE / sizeof(*queue->msgs); | |
252 | ||
253 | queue->msg_token = dma_map_single(hostdata->dev, queue->msgs, | |
254 | queue->size * sizeof(*queue->msgs), | |
255 | DMA_BIDIRECTIONAL); | |
256 | ||
8d8bb39b | 257 | if (dma_mapping_error(hostdata->dev, queue->msg_token)) |
1da177e4 LT |
258 | goto map_failed; |
259 | ||
b4687ca4 LX |
260 | gather_partition_info(); |
261 | set_adapter_info(hostdata); | |
262 | ||
cefbda2d | 263 | retrc = rc = plpar_hcall_norets(H_REG_CRQ, |
1da177e4 LT |
264 | vdev->unit_address, |
265 | queue->msg_token, PAGE_SIZE); | |
706c8c93 | 266 | if (rc == H_RESOURCE) |
bb58596f | 267 | /* maybe kexecing and resource is busy. try a reset */ |
d3849d51 | 268 | rc = rpavscsi_reset_crq_queue(queue, |
bb58596f DB |
269 | hostdata); |
270 | ||
1da177e4 LT |
271 | if (rc == 2) { |
272 | /* Adapter is good, but other end is not ready */ | |
6c0a60ec | 273 | dev_warn(hostdata->dev, "Partner adapter not ready\n"); |
ae0fda0c | 274 | retrc = 0; |
1da177e4 | 275 | } else if (rc != 0) { |
6c0a60ec | 276 | dev_warn(hostdata->dev, "Error %d opening adapter\n", rc); |
1da177e4 LT |
277 | goto reg_crq_failed; |
278 | } | |
279 | ||
280 | if (request_irq(vdev->irq, | |
d3849d51 | 281 | rpavscsi_handle_event, |
1da177e4 | 282 | 0, "ibmvscsi", (void *)hostdata) != 0) { |
6c0a60ec BK |
283 | dev_err(hostdata->dev, "couldn't register irq 0x%x\n", |
284 | vdev->irq); | |
1da177e4 LT |
285 | goto req_irq_failed; |
286 | } | |
287 | ||
288 | rc = vio_enable_interrupts(vdev); | |
289 | if (rc != 0) { | |
6c0a60ec | 290 | dev_err(hostdata->dev, "Error %d enabling interrupts!!!\n", rc); |
1da177e4 LT |
291 | goto req_irq_failed; |
292 | } | |
293 | ||
294 | queue->cur = 0; | |
295 | spin_lock_init(&queue->lock); | |
296 | ||
d3849d51 | 297 | tasklet_init(&hostdata->srp_task, (void *)rpavscsi_task, |
1da177e4 LT |
298 | (unsigned long)hostdata); |
299 | ||
cefbda2d | 300 | return retrc; |
1da177e4 LT |
301 | |
302 | req_irq_failed: | |
303 | do { | |
304 | rc = plpar_hcall_norets(H_FREE_CRQ, vdev->unit_address); | |
706c8c93 | 305 | } while ((rc == H_BUSY) || (H_IS_LONG_BUSY(rc))); |
1da177e4 LT |
306 | reg_crq_failed: |
307 | dma_unmap_single(hostdata->dev, | |
308 | queue->msg_token, | |
309 | queue->size * sizeof(*queue->msgs), DMA_BIDIRECTIONAL); | |
310 | map_failed: | |
311 | free_page((unsigned long)queue->msgs); | |
312 | malloc_failed: | |
313 | return -1; | |
314 | } | |
315 | ||
2b541f8f DB |
316 | /** |
317 | * reenable_crq_queue: - reenables a crq after | |
318 | * @queue: crq_queue to initialize and register | |
319 | * @hostdata: ibmvscsi_host_data of host | |
320 | * | |
321 | */ | |
d3849d51 DW |
322 | static int rpavscsi_reenable_crq_queue(struct crq_queue *queue, |
323 | struct ibmvscsi_host_data *hostdata) | |
2b541f8f DB |
324 | { |
325 | int rc; | |
326 | struct vio_dev *vdev = to_vio_dev(hostdata->dev); | |
327 | ||
328 | /* Re-enable the CRQ */ | |
329 | do { | |
330 | rc = plpar_hcall_norets(H_ENABLE_CRQ, vdev->unit_address); | |
706c8c93 | 331 | } while ((rc == H_IN_PROGRESS) || (rc == H_BUSY) || (H_IS_LONG_BUSY(rc))); |
2b541f8f DB |
332 | |
333 | if (rc) | |
6c0a60ec | 334 | dev_err(hostdata->dev, "Error %d enabling adapter\n", rc); |
2b541f8f DB |
335 | return rc; |
336 | } | |
337 | ||
64355b92 BK |
338 | /** |
339 | * rpavscsi_resume: - resume after suspend | |
340 | * @hostdata: ibmvscsi_host_data of host | |
341 | * | |
342 | */ | |
343 | static int rpavscsi_resume(struct ibmvscsi_host_data *hostdata) | |
344 | { | |
345 | vio_disable_interrupts(to_vio_dev(hostdata->dev)); | |
346 | tasklet_schedule(&hostdata->srp_task); | |
347 | return 0; | |
348 | } | |
349 | ||
d3849d51 DW |
350 | struct ibmvscsi_ops rpavscsi_ops = { |
351 | .init_crq_queue = rpavscsi_init_crq_queue, | |
352 | .release_crq_queue = rpavscsi_release_crq_queue, | |
353 | .reset_crq_queue = rpavscsi_reset_crq_queue, | |
354 | .reenable_crq_queue = rpavscsi_reenable_crq_queue, | |
355 | .send_crq = rpavscsi_send_crq, | |
64355b92 | 356 | .resume = rpavscsi_resume, |
d3849d51 | 357 | }; |