]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* -*- linux-c -*- |
2 | * viodasd.c | |
3 | * Authors: Dave Boutcher <boutcher@us.ibm.com> | |
4 | * Ryan Arnold <ryanarn@us.ibm.com> | |
5 | * Colin Devilbiss <devilbis@us.ibm.com> | |
8962cadb | 6 | * Stephen Rothwell |
1da177e4 LT |
7 | * |
8 | * (C) Copyright 2000-2004 IBM Corporation | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or | |
11 | * modify it under the terms of the GNU General Public License as | |
12 | * published by the Free Software Foundation; either version 2 of the | |
13 | * License, or (at your option) any later version. | |
14 | * | |
15 | * This program is distributed in the hope that it will be useful, | |
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
18 | * GNU General Public License for more details. | |
19 | * | |
20 | * You should have received a copy of the GNU General Public License | |
21 | * along with this program; if not, write to the Free Software | |
22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
23 | * | |
24 | * This routine provides access to disk space (termed "DASD" in historical | |
25 | * IBM terms) owned and managed by an OS/400 partition running on the | |
26 | * same box as this Linux partition. | |
27 | * | |
28 | * All disk operations are performed by sending messages back and forth to | |
29 | * the OS/400 partition. | |
30 | */ | |
31 | #include <linux/major.h> | |
32 | #include <linux/fs.h> | |
33 | #include <linux/module.h> | |
34 | #include <linux/kernel.h> | |
35 | #include <linux/blkdev.h> | |
36 | #include <linux/genhd.h> | |
37 | #include <linux/hdreg.h> | |
38 | #include <linux/errno.h> | |
39 | #include <linux/init.h> | |
40 | #include <linux/string.h> | |
41 | #include <linux/dma-mapping.h> | |
42 | #include <linux/completion.h> | |
43 | #include <linux/device.h> | |
45711f1a | 44 | #include <linux/scatterlist.h> |
1da177e4 LT |
45 | |
46 | #include <asm/uaccess.h> | |
47 | #include <asm/vio.h> | |
1ec65d76 | 48 | #include <asm/iseries/hv_types.h> |
e45423ea | 49 | #include <asm/iseries/hv_lp_event.h> |
15b17189 | 50 | #include <asm/iseries/hv_lp_config.h> |
b4206778 | 51 | #include <asm/iseries/vio.h> |
fb8b5007 | 52 | #include <asm/firmware.h> |
1da177e4 LT |
53 | |
54 | MODULE_DESCRIPTION("iSeries Virtual DASD"); | |
55 | MODULE_AUTHOR("Dave Boutcher"); | |
56 | MODULE_LICENSE("GPL"); | |
57 | ||
58 | /* | |
59 | * We only support 7 partitions per physical disk....so with minor | |
60 | * numbers 0-255 we get a maximum of 32 disks. | |
61 | */ | |
62 | #define VIOD_GENHD_NAME "iseries/vd" | |
1da177e4 LT |
63 | |
64 | #define VIOD_VERS "1.64" | |
65 | ||
66 | #define VIOD_KERN_WARNING KERN_WARNING "viod: " | |
67 | #define VIOD_KERN_INFO KERN_INFO "viod: " | |
68 | ||
69 | enum { | |
70 | PARTITION_SHIFT = 3, | |
71 | MAX_DISKNO = HVMAXARCHITECTEDVIRTUALDISKS, | |
ea6728c1 | 72 | MAX_DISK_NAME = FIELD_SIZEOF(struct gendisk, disk_name) |
1da177e4 LT |
73 | }; |
74 | ||
75 | static DEFINE_SPINLOCK(viodasd_spinlock); | |
76 | ||
77 | #define VIOMAXREQ 16 | |
1da177e4 LT |
78 | |
79 | #define DEVICE_NO(cell) ((struct viodasd_device *)(cell) - &viodasd_devices[0]) | |
80 | ||
1da177e4 LT |
81 | struct viodasd_waitevent { |
82 | struct completion com; | |
83 | int rc; | |
84 | u16 sub_result; | |
85 | int max_disk; /* open */ | |
86 | }; | |
87 | ||
88 | static const struct vio_error_entry viodasd_err_table[] = { | |
89 | { 0x0201, EINVAL, "Invalid Range" }, | |
90 | { 0x0202, EINVAL, "Invalid Token" }, | |
91 | { 0x0203, EIO, "DMA Error" }, | |
92 | { 0x0204, EIO, "Use Error" }, | |
93 | { 0x0205, EIO, "Release Error" }, | |
94 | { 0x0206, EINVAL, "Invalid Disk" }, | |
95 | { 0x0207, EBUSY, "Cant Lock" }, | |
96 | { 0x0208, EIO, "Already Locked" }, | |
97 | { 0x0209, EIO, "Already Unlocked" }, | |
98 | { 0x020A, EIO, "Invalid Arg" }, | |
99 | { 0x020B, EIO, "Bad IFS File" }, | |
100 | { 0x020C, EROFS, "Read Only Device" }, | |
101 | { 0x02FF, EIO, "Internal Error" }, | |
102 | { 0x0000, 0, NULL }, | |
103 | }; | |
104 | ||
105 | /* | |
106 | * Figure out the biggest I/O request (in sectors) we can accept | |
107 | */ | |
108 | #define VIODASD_MAXSECTORS (4096 / 512 * VIOMAXBLOCKDMA) | |
109 | ||
110 | /* | |
111 | * Number of disk I/O requests we've sent to OS/400 | |
112 | */ | |
113 | static int num_req_outstanding; | |
114 | ||
115 | /* | |
116 | * This is our internal structure for keeping track of disk devices | |
117 | */ | |
118 | struct viodasd_device { | |
119 | u16 cylinders; | |
120 | u16 tracks; | |
121 | u16 sectors; | |
122 | u16 bytes_per_sector; | |
123 | u64 size; | |
124 | int read_only; | |
125 | spinlock_t q_lock; | |
126 | struct gendisk *disk; | |
127 | struct device *dev; | |
128 | } viodasd_devices[MAX_DISKNO]; | |
129 | ||
130 | /* | |
131 | * External open entry point. | |
132 | */ | |
f115a14a | 133 | static int viodasd_open(struct block_device *bdev, fmode_t mode) |
1da177e4 | 134 | { |
f115a14a | 135 | struct viodasd_device *d = bdev->bd_disk->private_data; |
1da177e4 LT |
136 | HvLpEvent_Rc hvrc; |
137 | struct viodasd_waitevent we; | |
138 | u16 flags = 0; | |
139 | ||
140 | if (d->read_only) { | |
f115a14a | 141 | if (mode & FMODE_WRITE) |
1da177e4 LT |
142 | return -EROFS; |
143 | flags = vioblockflags_ro; | |
144 | } | |
145 | ||
146 | init_completion(&we.com); | |
147 | ||
148 | /* Send the open event to OS/400 */ | |
149 | hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp, | |
150 | HvLpEvent_Type_VirtualIo, | |
151 | viomajorsubtype_blockio | vioblockopen, | |
152 | HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck, | |
153 | viopath_sourceinst(viopath_hostLp), | |
154 | viopath_targetinst(viopath_hostLp), | |
155 | (u64)(unsigned long)&we, VIOVERSION << 16, | |
156 | ((u64)DEVICE_NO(d) << 48) | ((u64)flags << 32), | |
157 | 0, 0, 0); | |
158 | if (hvrc != 0) { | |
159 | printk(VIOD_KERN_WARNING "HV open failed %d\n", (int)hvrc); | |
160 | return -EIO; | |
161 | } | |
162 | ||
163 | wait_for_completion(&we.com); | |
164 | ||
165 | /* Check the return code */ | |
166 | if (we.rc != 0) { | |
167 | const struct vio_error_entry *err = | |
168 | vio_lookup_rc(viodasd_err_table, we.sub_result); | |
169 | ||
170 | printk(VIOD_KERN_WARNING | |
171 | "bad rc opening disk: %d:0x%04x (%s)\n", | |
172 | (int)we.rc, we.sub_result, err->msg); | |
173 | return -EIO; | |
174 | } | |
175 | ||
176 | return 0; | |
177 | } | |
178 | ||
179 | /* | |
180 | * External release entry point. | |
181 | */ | |
f115a14a | 182 | static int viodasd_release(struct gendisk *disk, fmode_t mode) |
1da177e4 | 183 | { |
f115a14a | 184 | struct viodasd_device *d = disk->private_data; |
1da177e4 LT |
185 | HvLpEvent_Rc hvrc; |
186 | ||
187 | /* Send the event to OS/400. We DON'T expect a response */ | |
188 | hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp, | |
189 | HvLpEvent_Type_VirtualIo, | |
190 | viomajorsubtype_blockio | vioblockclose, | |
191 | HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck, | |
192 | viopath_sourceinst(viopath_hostLp), | |
193 | viopath_targetinst(viopath_hostLp), | |
194 | 0, VIOVERSION << 16, | |
195 | ((u64)DEVICE_NO(d) << 48) /* | ((u64)flags << 32) */, | |
196 | 0, 0, 0); | |
197 | if (hvrc != 0) | |
198 | printk(VIOD_KERN_WARNING "HV close call failed %d\n", | |
199 | (int)hvrc); | |
200 | return 0; | |
201 | } | |
202 | ||
203 | ||
204 | /* External ioctl entry point. | |
205 | */ | |
a885c8c4 | 206 | static int viodasd_getgeo(struct block_device *bdev, struct hd_geometry *geo) |
1da177e4 | 207 | { |
a885c8c4 CH |
208 | struct gendisk *disk = bdev->bd_disk; |
209 | struct viodasd_device *d = disk->private_data; | |
210 | ||
36a70030 | 211 | geo->sectors = d->sectors ? d->sectors : 32; |
a885c8c4 CH |
212 | geo->heads = d->tracks ? d->tracks : 64; |
213 | geo->cylinders = d->cylinders ? d->cylinders : | |
36a70030 | 214 | get_capacity(disk) / (geo->sectors * geo->heads); |
1da177e4 | 215 | |
a885c8c4 | 216 | return 0; |
1da177e4 LT |
217 | } |
218 | ||
219 | /* | |
220 | * Our file operations table | |
221 | */ | |
83d5cde4 | 222 | static const struct block_device_operations viodasd_fops = { |
1da177e4 | 223 | .owner = THIS_MODULE, |
f115a14a AV |
224 | .open = viodasd_open, |
225 | .release = viodasd_release, | |
a885c8c4 | 226 | .getgeo = viodasd_getgeo, |
1da177e4 LT |
227 | }; |
228 | ||
229 | /* | |
230 | * End a request | |
231 | */ | |
b2aec24e | 232 | static void viodasd_end_request(struct request *req, int error, |
1da177e4 LT |
233 | int num_sectors) |
234 | { | |
b2aec24e | 235 | __blk_end_request(req, error, num_sectors << 9); |
1da177e4 LT |
236 | } |
237 | ||
238 | /* | |
239 | * Send an actual I/O request to OS/400 | |
240 | */ | |
241 | static int send_request(struct request *req) | |
242 | { | |
243 | u64 start; | |
244 | int direction; | |
245 | int nsg; | |
246 | u16 viocmd; | |
247 | HvLpEvent_Rc hvrc; | |
248 | struct vioblocklpevent *bevent; | |
677f8c0d | 249 | struct HvLpEvent *hev; |
1da177e4 LT |
250 | struct scatterlist sg[VIOMAXBLOCKDMA]; |
251 | int sgindex; | |
1da177e4 LT |
252 | struct viodasd_device *d; |
253 | unsigned long flags; | |
254 | ||
83096ebf | 255 | start = (u64)blk_rq_pos(req) << 9; |
1da177e4 LT |
256 | |
257 | if (rq_data_dir(req) == READ) { | |
258 | direction = DMA_FROM_DEVICE; | |
259 | viocmd = viomajorsubtype_blockio | vioblockread; | |
1da177e4 LT |
260 | } else { |
261 | direction = DMA_TO_DEVICE; | |
262 | viocmd = viomajorsubtype_blockio | vioblockwrite; | |
1da177e4 LT |
263 | } |
264 | ||
265 | d = req->rq_disk->private_data; | |
266 | ||
267 | /* Now build the scatter-gather list */ | |
45711f1a | 268 | sg_init_table(sg, VIOMAXBLOCKDMA); |
1da177e4 LT |
269 | nsg = blk_rq_map_sg(req->q, req, sg); |
270 | nsg = dma_map_sg(d->dev, sg, nsg, direction); | |
271 | ||
272 | spin_lock_irqsave(&viodasd_spinlock, flags); | |
273 | num_req_outstanding++; | |
274 | ||
275 | /* This optimization handles a single DMA block */ | |
276 | if (nsg == 1) | |
277 | hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp, | |
278 | HvLpEvent_Type_VirtualIo, viocmd, | |
279 | HvLpEvent_AckInd_DoAck, | |
280 | HvLpEvent_AckType_ImmediateAck, | |
281 | viopath_sourceinst(viopath_hostLp), | |
282 | viopath_targetinst(viopath_hostLp), | |
283 | (u64)(unsigned long)req, VIOVERSION << 16, | |
284 | ((u64)DEVICE_NO(d) << 48), start, | |
285 | ((u64)sg_dma_address(&sg[0])) << 32, | |
286 | sg_dma_len(&sg[0])); | |
287 | else { | |
288 | bevent = (struct vioblocklpevent *) | |
289 | vio_get_event_buffer(viomajorsubtype_blockio); | |
290 | if (bevent == NULL) { | |
291 | printk(VIOD_KERN_WARNING | |
292 | "error allocating disk event buffer\n"); | |
293 | goto error_ret; | |
294 | } | |
295 | ||
296 | /* | |
297 | * Now build up the actual request. Note that we store | |
298 | * the pointer to the request in the correlation | |
299 | * token so we can match the response up later | |
300 | */ | |
301 | memset(bevent, 0, sizeof(struct vioblocklpevent)); | |
677f8c0d SR |
302 | hev = &bevent->event; |
303 | hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DO_ACK | | |
304 | HV_LP_EVENT_INT; | |
305 | hev->xType = HvLpEvent_Type_VirtualIo; | |
306 | hev->xSubtype = viocmd; | |
307 | hev->xSourceLp = HvLpConfig_getLpIndex(); | |
308 | hev->xTargetLp = viopath_hostLp; | |
309 | hev->xSizeMinus1 = | |
1da177e4 LT |
310 | offsetof(struct vioblocklpevent, u.rw_data.dma_info) + |
311 | (sizeof(bevent->u.rw_data.dma_info[0]) * nsg) - 1; | |
677f8c0d SR |
312 | hev->xSourceInstanceId = viopath_sourceinst(viopath_hostLp); |
313 | hev->xTargetInstanceId = viopath_targetinst(viopath_hostLp); | |
314 | hev->xCorrelationToken = (u64)req; | |
1da177e4 LT |
315 | bevent->version = VIOVERSION; |
316 | bevent->disk = DEVICE_NO(d); | |
317 | bevent->u.rw_data.offset = start; | |
318 | ||
319 | /* | |
320 | * Copy just the dma information from the sg list | |
321 | * into the request | |
322 | */ | |
323 | for (sgindex = 0; sgindex < nsg; sgindex++) { | |
324 | bevent->u.rw_data.dma_info[sgindex].token = | |
325 | sg_dma_address(&sg[sgindex]); | |
326 | bevent->u.rw_data.dma_info[sgindex].len = | |
327 | sg_dma_len(&sg[sgindex]); | |
328 | } | |
329 | ||
330 | /* Send the request */ | |
331 | hvrc = HvCallEvent_signalLpEvent(&bevent->event); | |
332 | vio_free_event_buffer(viomajorsubtype_blockio, bevent); | |
333 | } | |
334 | ||
335 | if (hvrc != HvLpEvent_Rc_Good) { | |
336 | printk(VIOD_KERN_WARNING | |
337 | "error sending disk event to OS/400 (rc %d)\n", | |
338 | (int)hvrc); | |
339 | goto error_ret; | |
340 | } | |
341 | spin_unlock_irqrestore(&viodasd_spinlock, flags); | |
342 | return 0; | |
343 | ||
344 | error_ret: | |
345 | num_req_outstanding--; | |
346 | spin_unlock_irqrestore(&viodasd_spinlock, flags); | |
347 | dma_unmap_sg(d->dev, sg, nsg, direction); | |
348 | return -1; | |
349 | } | |
350 | ||
351 | /* | |
352 | * This is the external request processing routine | |
353 | */ | |
165125e1 | 354 | static void do_viodasd_request(struct request_queue *q) |
1da177e4 LT |
355 | { |
356 | struct request *req; | |
357 | ||
358 | /* | |
359 | * If we already have the maximum number of requests | |
360 | * outstanding to OS/400 just bail out. We'll come | |
361 | * back later. | |
362 | */ | |
363 | while (num_req_outstanding < VIOMAXREQ) { | |
9934c8c0 | 364 | req = blk_fetch_request(q); |
1da177e4 LT |
365 | if (req == NULL) |
366 | return; | |
1da177e4 LT |
367 | /* check that request contains a valid command */ |
368 | if (!blk_fs_request(req)) { | |
5b93629b | 369 | viodasd_end_request(req, -EIO, blk_rq_sectors(req)); |
1da177e4 LT |
370 | continue; |
371 | } | |
372 | /* Try sending the request */ | |
373 | if (send_request(req) != 0) | |
5b93629b | 374 | viodasd_end_request(req, -EIO, blk_rq_sectors(req)); |
1da177e4 LT |
375 | } |
376 | } | |
377 | ||
378 | /* | |
379 | * Probe a single disk and fill in the viodasd_device structure | |
380 | * for it. | |
381 | */ | |
8251b4c4 | 382 | static int probe_disk(struct viodasd_device *d) |
1da177e4 LT |
383 | { |
384 | HvLpEvent_Rc hvrc; | |
385 | struct viodasd_waitevent we; | |
386 | int dev_no = DEVICE_NO(d); | |
387 | struct gendisk *g; | |
388 | struct request_queue *q; | |
389 | u16 flags = 0; | |
390 | ||
391 | retry: | |
392 | init_completion(&we.com); | |
393 | ||
394 | /* Send the open event to OS/400 */ | |
395 | hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp, | |
396 | HvLpEvent_Type_VirtualIo, | |
397 | viomajorsubtype_blockio | vioblockopen, | |
398 | HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck, | |
399 | viopath_sourceinst(viopath_hostLp), | |
400 | viopath_targetinst(viopath_hostLp), | |
401 | (u64)(unsigned long)&we, VIOVERSION << 16, | |
402 | ((u64)dev_no << 48) | ((u64)flags<< 32), | |
403 | 0, 0, 0); | |
404 | if (hvrc != 0) { | |
405 | printk(VIOD_KERN_WARNING "bad rc on HV open %d\n", (int)hvrc); | |
8251b4c4 | 406 | return 0; |
1da177e4 LT |
407 | } |
408 | ||
409 | wait_for_completion(&we.com); | |
410 | ||
411 | if (we.rc != 0) { | |
412 | if (flags != 0) | |
8251b4c4 | 413 | return 0; |
1da177e4 LT |
414 | /* try again with read only flag set */ |
415 | flags = vioblockflags_ro; | |
416 | goto retry; | |
417 | } | |
418 | if (we.max_disk > (MAX_DISKNO - 1)) { | |
49b3a3cb MS |
419 | printk_once(VIOD_KERN_INFO |
420 | "Only examining the first %d of %d disks connected\n", | |
421 | MAX_DISKNO, we.max_disk + 1); | |
1da177e4 LT |
422 | } |
423 | ||
424 | /* Send the close event to OS/400. We DON'T expect a response */ | |
425 | hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp, | |
426 | HvLpEvent_Type_VirtualIo, | |
427 | viomajorsubtype_blockio | vioblockclose, | |
428 | HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck, | |
429 | viopath_sourceinst(viopath_hostLp), | |
430 | viopath_targetinst(viopath_hostLp), | |
431 | 0, VIOVERSION << 16, | |
432 | ((u64)dev_no << 48) | ((u64)flags << 32), | |
433 | 0, 0, 0); | |
434 | if (hvrc != 0) { | |
435 | printk(VIOD_KERN_WARNING | |
436 | "bad rc sending event to OS/400 %d\n", (int)hvrc); | |
8251b4c4 | 437 | return 0; |
1da177e4 | 438 | } |
8251b4c4 SR |
439 | |
440 | if (d->dev == NULL) { | |
441 | /* this is when we reprobe for new disks */ | |
442 | if (vio_create_viodasd(dev_no) == NULL) { | |
443 | printk(VIOD_KERN_WARNING | |
444 | "cannot allocate virtual device for disk %d\n", | |
445 | dev_no); | |
446 | return 0; | |
447 | } | |
448 | /* | |
449 | * The vio_create_viodasd will have recursed into this | |
450 | * routine with d->dev set to the new vio device and | |
451 | * will finish the setup of the disk below. | |
452 | */ | |
453 | return 1; | |
454 | } | |
455 | ||
1da177e4 LT |
456 | /* create the request queue for the disk */ |
457 | spin_lock_init(&d->q_lock); | |
458 | q = blk_init_queue(do_viodasd_request, &d->q_lock); | |
459 | if (q == NULL) { | |
460 | printk(VIOD_KERN_WARNING "cannot allocate queue for disk %d\n", | |
461 | dev_no); | |
8251b4c4 | 462 | return 0; |
1da177e4 LT |
463 | } |
464 | g = alloc_disk(1 << PARTITION_SHIFT); | |
465 | if (g == NULL) { | |
466 | printk(VIOD_KERN_WARNING | |
467 | "cannot allocate disk structure for disk %d\n", | |
468 | dev_no); | |
469 | blk_cleanup_queue(q); | |
8251b4c4 | 470 | return 0; |
1da177e4 LT |
471 | } |
472 | ||
473 | d->disk = g; | |
8a78362c | 474 | blk_queue_max_segments(q, VIOMAXBLOCKDMA); |
086fa5ff | 475 | blk_queue_max_hw_sectors(q, VIODASD_MAXSECTORS); |
1da177e4 LT |
476 | g->major = VIODASD_MAJOR; |
477 | g->first_minor = dev_no << PARTITION_SHIFT; | |
478 | if (dev_no >= 26) | |
479 | snprintf(g->disk_name, sizeof(g->disk_name), | |
480 | VIOD_GENHD_NAME "%c%c", | |
481 | 'a' + (dev_no / 26) - 1, 'a' + (dev_no % 26)); | |
482 | else | |
483 | snprintf(g->disk_name, sizeof(g->disk_name), | |
484 | VIOD_GENHD_NAME "%c", 'a' + (dev_no % 26)); | |
1da177e4 LT |
485 | g->fops = &viodasd_fops; |
486 | g->queue = q; | |
487 | g->private_data = d; | |
488 | g->driverfs_dev = d->dev; | |
489 | set_capacity(g, d->size >> 9); | |
490 | ||
491 | printk(VIOD_KERN_INFO "disk %d: %lu sectors (%lu MB) " | |
492 | "CHS=%d/%d/%d sector size %d%s\n", | |
493 | dev_no, (unsigned long)(d->size >> 9), | |
494 | (unsigned long)(d->size >> 20), | |
495 | (int)d->cylinders, (int)d->tracks, | |
496 | (int)d->sectors, (int)d->bytes_per_sector, | |
497 | d->read_only ? " (RO)" : ""); | |
498 | ||
499 | /* register us in the global list */ | |
500 | add_disk(g); | |
8251b4c4 | 501 | return 1; |
1da177e4 LT |
502 | } |
503 | ||
504 | /* returns the total number of scatterlist elements converted */ | |
505 | static int block_event_to_scatterlist(const struct vioblocklpevent *bevent, | |
506 | struct scatterlist *sg, int *total_len) | |
507 | { | |
508 | int i, numsg; | |
509 | const struct rw_data *rw_data = &bevent->u.rw_data; | |
510 | static const int offset = | |
511 | offsetof(struct vioblocklpevent, u.rw_data.dma_info); | |
512 | static const int element_size = sizeof(rw_data->dma_info[0]); | |
513 | ||
514 | numsg = ((bevent->event.xSizeMinus1 + 1) - offset) / element_size; | |
515 | if (numsg > VIOMAXBLOCKDMA) | |
516 | numsg = VIOMAXBLOCKDMA; | |
517 | ||
518 | *total_len = 0; | |
25c0a7b8 | 519 | sg_init_table(sg, VIOMAXBLOCKDMA); |
1da177e4 LT |
520 | for (i = 0; (i < numsg) && (rw_data->dma_info[i].len > 0); ++i) { |
521 | sg_dma_address(&sg[i]) = rw_data->dma_info[i].token; | |
522 | sg_dma_len(&sg[i]) = rw_data->dma_info[i].len; | |
523 | *total_len += rw_data->dma_info[i].len; | |
524 | } | |
525 | return i; | |
526 | } | |
527 | ||
528 | /* | |
529 | * Restart all queues, starting with the one _after_ the disk given, | |
530 | * thus reducing the chance of starvation of higher numbered disks. | |
531 | */ | |
532 | static void viodasd_restart_all_queues_starting_from(int first_index) | |
533 | { | |
534 | int i; | |
535 | ||
536 | for (i = first_index + 1; i < MAX_DISKNO; ++i) | |
537 | if (viodasd_devices[i].disk) | |
538 | blk_run_queue(viodasd_devices[i].disk->queue); | |
539 | for (i = 0; i <= first_index; ++i) | |
540 | if (viodasd_devices[i].disk) | |
541 | blk_run_queue(viodasd_devices[i].disk->queue); | |
542 | } | |
543 | ||
544 | /* | |
545 | * For read and write requests, decrement the number of outstanding requests, | |
546 | * Free the DMA buffers we allocated. | |
547 | */ | |
548 | static int viodasd_handle_read_write(struct vioblocklpevent *bevent) | |
549 | { | |
550 | int num_sg, num_sect, pci_direction, total_len; | |
551 | struct request *req; | |
552 | struct scatterlist sg[VIOMAXBLOCKDMA]; | |
553 | struct HvLpEvent *event = &bevent->event; | |
554 | unsigned long irq_flags; | |
555 | struct viodasd_device *d; | |
556 | int error; | |
557 | spinlock_t *qlock; | |
558 | ||
559 | num_sg = block_event_to_scatterlist(bevent, sg, &total_len); | |
560 | num_sect = total_len >> 9; | |
561 | if (event->xSubtype == (viomajorsubtype_blockio | vioblockread)) | |
562 | pci_direction = DMA_FROM_DEVICE; | |
563 | else | |
564 | pci_direction = DMA_TO_DEVICE; | |
565 | req = (struct request *)bevent->event.xCorrelationToken; | |
566 | d = req->rq_disk->private_data; | |
567 | ||
568 | dma_unmap_sg(d->dev, sg, num_sg, pci_direction); | |
569 | ||
570 | /* | |
571 | * Since this is running in interrupt mode, we need to make sure | |
572 | * we're not stepping on any global I/O operations | |
573 | */ | |
574 | spin_lock_irqsave(&viodasd_spinlock, irq_flags); | |
575 | num_req_outstanding--; | |
576 | spin_unlock_irqrestore(&viodasd_spinlock, irq_flags); | |
577 | ||
b2aec24e | 578 | error = (event->xRc == HvLpEvent_Rc_Good) ? 0 : -EIO; |
1da177e4 LT |
579 | if (error) { |
580 | const struct vio_error_entry *err; | |
581 | err = vio_lookup_rc(viodasd_err_table, bevent->sub_result); | |
582 | printk(VIOD_KERN_WARNING "read/write error %d:0x%04x (%s)\n", | |
583 | event->xRc, bevent->sub_result, err->msg); | |
5b93629b | 584 | num_sect = blk_rq_sectors(req); |
1da177e4 LT |
585 | } |
586 | qlock = req->q->queue_lock; | |
587 | spin_lock_irqsave(qlock, irq_flags); | |
b2aec24e | 588 | viodasd_end_request(req, error, num_sect); |
1da177e4 LT |
589 | spin_unlock_irqrestore(qlock, irq_flags); |
590 | ||
591 | /* Finally, try to get more requests off of this device's queue */ | |
592 | viodasd_restart_all_queues_starting_from(DEVICE_NO(d)); | |
593 | ||
594 | return 0; | |
595 | } | |
596 | ||
597 | /* This routine handles incoming block LP events */ | |
598 | static void handle_block_event(struct HvLpEvent *event) | |
599 | { | |
600 | struct vioblocklpevent *bevent = (struct vioblocklpevent *)event; | |
601 | struct viodasd_waitevent *pwe; | |
602 | ||
603 | if (event == NULL) | |
604 | /* Notification that a partition went away! */ | |
605 | return; | |
606 | /* First, we should NEVER get an int here...only acks */ | |
677f8c0d | 607 | if (hvlpevent_is_int(event)) { |
1da177e4 LT |
608 | printk(VIOD_KERN_WARNING |
609 | "Yikes! got an int in viodasd event handler!\n"); | |
677f8c0d | 610 | if (hvlpevent_need_ack(event)) { |
1da177e4 LT |
611 | event->xRc = HvLpEvent_Rc_InvalidSubtype; |
612 | HvCallEvent_ackLpEvent(event); | |
613 | } | |
614 | } | |
615 | ||
616 | switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) { | |
617 | case vioblockopen: | |
618 | /* | |
619 | * Handle a response to an open request. We get all the | |
620 | * disk information in the response, so update it. The | |
621 | * correlation token contains a pointer to a waitevent | |
622 | * structure that has a completion in it. update the | |
623 | * return code in the waitevent structure and post the | |
624 | * completion to wake up the guy who sent the request | |
625 | */ | |
626 | pwe = (struct viodasd_waitevent *)event->xCorrelationToken; | |
627 | pwe->rc = event->xRc; | |
628 | pwe->sub_result = bevent->sub_result; | |
629 | if (event->xRc == HvLpEvent_Rc_Good) { | |
630 | const struct open_data *data = &bevent->u.open_data; | |
631 | struct viodasd_device *device = | |
632 | &viodasd_devices[bevent->disk]; | |
633 | device->read_only = | |
634 | bevent->flags & vioblockflags_ro; | |
635 | device->size = data->disk_size; | |
636 | device->cylinders = data->cylinders; | |
637 | device->tracks = data->tracks; | |
638 | device->sectors = data->sectors; | |
639 | device->bytes_per_sector = data->bytes_per_sector; | |
640 | pwe->max_disk = data->max_disk; | |
641 | } | |
642 | complete(&pwe->com); | |
643 | break; | |
644 | case vioblockclose: | |
645 | break; | |
646 | case vioblockread: | |
647 | case vioblockwrite: | |
648 | viodasd_handle_read_write(bevent); | |
649 | break; | |
650 | ||
651 | default: | |
652 | printk(VIOD_KERN_WARNING "invalid subtype!"); | |
677f8c0d | 653 | if (hvlpevent_need_ack(event)) { |
1da177e4 LT |
654 | event->xRc = HvLpEvent_Rc_InvalidSubtype; |
655 | HvCallEvent_ackLpEvent(event); | |
656 | } | |
657 | } | |
658 | } | |
659 | ||
660 | /* | |
661 | * Get the driver to reprobe for more disks. | |
662 | */ | |
663 | static ssize_t probe_disks(struct device_driver *drv, const char *buf, | |
664 | size_t count) | |
665 | { | |
666 | struct viodasd_device *d; | |
667 | ||
668 | for (d = viodasd_devices; d < &viodasd_devices[MAX_DISKNO]; d++) { | |
669 | if (d->disk == NULL) | |
670 | probe_disk(d); | |
671 | } | |
672 | return count; | |
673 | } | |
674 | static DRIVER_ATTR(probe, S_IWUSR, NULL, probe_disks); | |
675 | ||
676 | static int viodasd_probe(struct vio_dev *vdev, const struct vio_device_id *id) | |
677 | { | |
678 | struct viodasd_device *d = &viodasd_devices[vdev->unit_address]; | |
679 | ||
680 | d->dev = &vdev->dev; | |
8251b4c4 | 681 | if (!probe_disk(d)) |
1da177e4 LT |
682 | return -ENODEV; |
683 | return 0; | |
684 | } | |
685 | ||
686 | static int viodasd_remove(struct vio_dev *vdev) | |
687 | { | |
688 | struct viodasd_device *d; | |
689 | ||
690 | d = &viodasd_devices[vdev->unit_address]; | |
691 | if (d->disk) { | |
692 | del_gendisk(d->disk); | |
693 | blk_cleanup_queue(d->disk->queue); | |
694 | put_disk(d->disk); | |
695 | d->disk = NULL; | |
696 | } | |
697 | d->dev = NULL; | |
698 | return 0; | |
699 | } | |
700 | ||
701 | /** | |
702 | * viodasd_device_table: Used by vio.c to match devices that we | |
703 | * support. | |
704 | */ | |
705 | static struct vio_device_id viodasd_device_table[] __devinitdata = { | |
fbabeb60 | 706 | { "block", "IBM,iSeries-viodasd" }, |
fb120da6 | 707 | { "", "" } |
1da177e4 | 708 | }; |
1da177e4 | 709 | MODULE_DEVICE_TABLE(vio, viodasd_device_table); |
915124d8 | 710 | |
1da177e4 | 711 | static struct vio_driver viodasd_driver = { |
1da177e4 LT |
712 | .id_table = viodasd_device_table, |
713 | .probe = viodasd_probe, | |
6fdf5392 SR |
714 | .remove = viodasd_remove, |
715 | .driver = { | |
716 | .name = "viodasd", | |
915124d8 | 717 | .owner = THIS_MODULE, |
6fdf5392 | 718 | } |
1da177e4 LT |
719 | }; |
720 | ||
f9df68ec SR |
721 | static int need_delete_probe; |
722 | ||
1da177e4 LT |
723 | /* |
724 | * Initialize the whole device driver. Handle module and non-module | |
725 | * versions | |
726 | */ | |
727 | static int __init viodasd_init(void) | |
728 | { | |
729 | int rc; | |
730 | ||
fb8b5007 SR |
731 | if (!firmware_has_feature(FW_FEATURE_ISERIES)) { |
732 | rc = -ENODEV; | |
733 | goto early_fail; | |
734 | } | |
735 | ||
1da177e4 LT |
736 | /* Try to open to our host lp */ |
737 | if (viopath_hostLp == HvLpIndexInvalid) | |
738 | vio_set_hostlp(); | |
739 | ||
740 | if (viopath_hostLp == HvLpIndexInvalid) { | |
741 | printk(VIOD_KERN_WARNING "invalid hosting partition\n"); | |
f9df68ec SR |
742 | rc = -EIO; |
743 | goto early_fail; | |
1da177e4 LT |
744 | } |
745 | ||
746 | printk(VIOD_KERN_INFO "vers " VIOD_VERS ", hosting partition %d\n", | |
747 | viopath_hostLp); | |
748 | ||
749 | /* register the block device */ | |
f9df68ec SR |
750 | rc = register_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME); |
751 | if (rc) { | |
1da177e4 LT |
752 | printk(VIOD_KERN_WARNING |
753 | "Unable to get major number %d for %s\n", | |
754 | VIODASD_MAJOR, VIOD_GENHD_NAME); | |
f9df68ec | 755 | goto early_fail; |
1da177e4 LT |
756 | } |
757 | /* Actually open the path to the hosting partition */ | |
f9df68ec SR |
758 | rc = viopath_open(viopath_hostLp, viomajorsubtype_blockio, |
759 | VIOMAXREQ + 2); | |
760 | if (rc) { | |
1da177e4 LT |
761 | printk(VIOD_KERN_WARNING |
762 | "error opening path to host partition %d\n", | |
763 | viopath_hostLp); | |
f9df68ec | 764 | goto unregister_blk; |
1da177e4 LT |
765 | } |
766 | ||
767 | /* Initialize our request handler */ | |
768 | vio_setHandler(viomajorsubtype_blockio, handle_block_event); | |
769 | ||
770 | rc = vio_register_driver(&viodasd_driver); | |
f9df68ec SR |
771 | if (rc) { |
772 | printk(VIOD_KERN_WARNING "vio_register_driver failed\n"); | |
773 | goto unset_handler; | |
774 | } | |
775 | ||
776 | /* | |
777 | * If this call fails, it just means that we cannot dynamically | |
778 | * add virtual disks, but the driver will still work fine for | |
779 | * all existing disk, so ignore the failure. | |
780 | */ | |
781 | if (!driver_create_file(&viodasd_driver.driver, &driver_attr_probe)) | |
782 | need_delete_probe = 1; | |
783 | ||
784 | return 0; | |
785 | ||
786 | unset_handler: | |
787 | vio_clearHandler(viomajorsubtype_blockio); | |
788 | viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2); | |
789 | unregister_blk: | |
790 | unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME); | |
791 | early_fail: | |
1da177e4 LT |
792 | return rc; |
793 | } | |
794 | module_init(viodasd_init); | |
795 | ||
f9df68ec | 796 | void __exit viodasd_exit(void) |
1da177e4 | 797 | { |
f9df68ec SR |
798 | if (need_delete_probe) |
799 | driver_remove_file(&viodasd_driver.driver, &driver_attr_probe); | |
1da177e4 LT |
800 | vio_unregister_driver(&viodasd_driver); |
801 | vio_clearHandler(viomajorsubtype_blockio); | |
1da177e4 | 802 | viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2); |
f9df68ec | 803 | unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME); |
1da177e4 | 804 | } |
1da177e4 | 805 | module_exit(viodasd_exit); |