ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / block / viodasd.c
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>
6  *           Stephen Rothwell <sfr@au1.ibm.com>
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
44 #include <asm/uaccess.h>
45 #include <asm/iSeries/HvTypes.h>
46 #include <asm/iSeries/HvLpEvent.h>
47 #include <asm/iSeries/HvLpConfig.h>
48 #include <asm/iSeries/vio.h>
49
50 MODULE_DESCRIPTION("iSeries Virtual DASD");
51 MODULE_AUTHOR("Dave Boutcher");
52 MODULE_LICENSE("GPL");
53
54 /*
55  * We only support 7 partitions per physical disk....so with minor
56  * numbers 0-255 we get a maximum of 32 disks.
57  */
58 #define VIOD_GENHD_NAME         "iseries/vd"
59 #define VIOD_GENHD_DEVFS_NAME   "iseries/disc"
60
61 #define VIOD_VERS               "1.64"
62
63 #define VIOD_KERN_WARNING       KERN_WARNING "viod: "
64 #define VIOD_KERN_INFO          KERN_INFO "viod: "
65
66 enum {
67         PARTITION_SHIFT = 3,
68         MAX_DISKNO = 32,
69         MAX_DISK_NAME = sizeof(((struct gendisk *)0)->disk_name)
70 };
71
72 static spinlock_t       viodasd_spinlock = SPIN_LOCK_UNLOCKED;
73
74 #define VIOMAXREQ               16
75 #define VIOMAXBLOCKDMA          12
76
77 #define DEVICE_NO(cell) ((struct viodasd_device *)(cell) - &viodasd_devices[0])
78
79 struct open_data {
80         u64     disk_size;
81         u16     max_disk;
82         u16     cylinders;
83         u16     tracks;
84         u16     sectors;
85         u16     bytes_per_sector;
86 };
87
88 struct rw_data {
89         u64     offset;
90         struct {
91                 u32     token;
92                 u32     reserved;
93                 u64     len;
94         } dma_info[VIOMAXBLOCKDMA];
95 };
96
97 struct vioblocklpevent {
98         struct HvLpEvent        event;
99         u32                     reserved;
100         u16                     version;
101         u16                     sub_result;
102         u16                     disk;
103         u16                     flags;
104         union {
105                 struct open_data        open_data;
106                 struct rw_data          rw_data;
107                 u64                     changed;
108         } u;
109 };
110
111 #define vioblockflags_ro   0x0001
112
113 enum vioblocksubtype {
114         vioblockopen = 0x0001,
115         vioblockclose = 0x0002,
116         vioblockread = 0x0003,
117         vioblockwrite = 0x0004,
118         vioblockflush = 0x0005,
119         vioblockcheck = 0x0007
120 };
121
122 struct viodasd_waitevent {
123         struct completion       com;
124         int                     rc;
125         u16                     sub_result;
126         int                     max_disk;       /* open */
127 };
128
129 static const struct vio_error_entry viodasd_err_table[] = {
130         { 0x0201, EINVAL, "Invalid Range" },
131         { 0x0202, EINVAL, "Invalid Token" },
132         { 0x0203, EIO, "DMA Error" },
133         { 0x0204, EIO, "Use Error" },
134         { 0x0205, EIO, "Release Error" },
135         { 0x0206, EINVAL, "Invalid Disk" },
136         { 0x0207, EBUSY, "Cant Lock" },
137         { 0x0208, EIO, "Already Locked" },
138         { 0x0209, EIO, "Already Unlocked" },
139         { 0x020A, EIO, "Invalid Arg" },
140         { 0x020B, EIO, "Bad IFS File" },
141         { 0x020C, EROFS, "Read Only Device" },
142         { 0x02FF, EIO, "Internal Error" },
143         { 0x0000, 0, NULL },
144 };
145
146 /*
147  * Figure out the biggest I/O request (in sectors) we can accept
148  */
149 #define VIODASD_MAXSECTORS (4096 / 512 * VIOMAXBLOCKDMA)
150
151 /*
152  * Number of disk I/O requests we've sent to OS/400
153  */
154 static int num_req_outstanding;
155
156 /*
157  * This is our internal structure for keeping track of disk devices
158  */
159 struct viodasd_device {
160         u16             cylinders;
161         u16             tracks;
162         u16             sectors;
163         u16             bytes_per_sector;
164         u64             size;
165         int             read_only;
166         spinlock_t      q_lock;
167         struct gendisk  *disk;
168 } viodasd_devices[MAX_DISKNO];
169
170 /*
171  * External open entry point.
172  */
173 static int viodasd_open(struct inode *ino, struct file *fil)
174 {
175         struct viodasd_device *d = ino->i_bdev->bd_disk->private_data;
176         HvLpEvent_Rc hvrc;
177         struct viodasd_waitevent we;
178
179         init_completion(&we.com);
180
181         /* Send the open event to OS/400 */
182         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
183                         HvLpEvent_Type_VirtualIo,
184                         viomajorsubtype_blockio | vioblockopen,
185                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
186                         viopath_sourceinst(viopath_hostLp),
187                         viopath_targetinst(viopath_hostLp),
188                         (u64)(unsigned long)&we, VIOVERSION << 16,
189                         ((u64)DEVICE_NO(d) << 48) /* | ((u64)flags << 32) */,
190                         0, 0, 0);
191         if (hvrc != 0) {
192                 printk(VIOD_KERN_WARNING "HV open failed %d\n", (int)hvrc);
193                 return -EIO;
194         }
195
196         wait_for_completion(&we.com);
197
198         /* Check the return code */
199         if (we.rc != 0) {
200                 const struct vio_error_entry *err =
201                         vio_lookup_rc(viodasd_err_table, we.sub_result);
202
203                 printk(VIOD_KERN_WARNING
204                                 "bad rc opening disk: %d:0x%04x (%s)\n",
205                                 (int)we.rc, we.sub_result, err->msg);
206                 return -EIO;
207         }
208
209         return 0;
210 }
211
212 /*
213  * External release entry point.
214  */
215 static int viodasd_release(struct inode *ino, struct file *fil)
216 {
217         struct viodasd_device *d = ino->i_bdev->bd_disk->private_data;
218         HvLpEvent_Rc hvrc;
219
220         /* Send the event to OS/400.  We DON'T expect a response */
221         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
222                         HvLpEvent_Type_VirtualIo,
223                         viomajorsubtype_blockio | vioblockclose,
224                         HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
225                         viopath_sourceinst(viopath_hostLp),
226                         viopath_targetinst(viopath_hostLp),
227                         0, VIOVERSION << 16,
228                         ((u64)DEVICE_NO(d) << 48) /* | ((u64)flags << 32) */,
229                         0, 0, 0);
230         if (hvrc != 0)
231                 printk(VIOD_KERN_WARNING "HV close call failed %d\n",
232                                 (int)hvrc);
233         return 0;
234 }
235
236
237 /* External ioctl entry point.
238  */
239 static int viodasd_ioctl(struct inode *ino, struct file *fil,
240                          unsigned int cmd, unsigned long arg)
241 {
242         int err;
243         unsigned char sectors;
244         unsigned char heads;
245         unsigned short cylinders;
246         struct hd_geometry *geo;
247         struct gendisk *gendisk;
248         struct viodasd_device *d;
249
250         switch (cmd) {
251         case HDIO_GETGEO:
252                 geo = (struct hd_geometry *)arg;
253                 if (geo == NULL)
254                         return -EINVAL;
255                 err = verify_area(VERIFY_WRITE, geo, sizeof(*geo));
256                 if (err)
257                         return err;
258                 gendisk = ino->i_bdev->bd_disk;
259                 d = gendisk->private_data;
260                 sectors = d->sectors;
261                 if (sectors == 0)
262                         sectors = 32;
263                 heads = d->tracks;
264                 if (heads == 0)
265                         heads = 64;
266                 cylinders = d->cylinders;
267                 if (cylinders == 0)
268                         cylinders = get_capacity(gendisk) / (sectors * heads);
269                 if (__put_user(sectors, &geo->sectors) ||
270                     __put_user(heads, &geo->heads) ||
271                     __put_user(cylinders, &geo->cylinders) ||
272                     __put_user(get_start_sect(ino->i_bdev), &geo->start))
273                         return -EFAULT;
274                 return 0;
275         }
276
277         return -EINVAL;
278 }
279
280 /*
281  * Our file operations table
282  */
283 static struct block_device_operations viodasd_fops = {
284         .owner = THIS_MODULE,
285         .open = viodasd_open,
286         .release = viodasd_release,
287         .ioctl = viodasd_ioctl,
288 };
289
290 /*
291  * End a request
292  */
293 static void viodasd_end_request(struct request *req, int uptodate,
294                 int num_sectors)
295 {
296         if (end_that_request_first(req, uptodate, num_sectors))
297                 return;
298         add_disk_randomness(req->rq_disk);
299         end_that_request_last(req);
300 }
301
302 /*
303  * Send an actual I/O request to OS/400
304  */
305 static int send_request(struct request *req)
306 {
307         u64 start;
308         int direction;
309         int nsg;
310         u16 viocmd;
311         HvLpEvent_Rc hvrc;
312         struct vioblocklpevent *bevent;
313         struct scatterlist sg[VIOMAXBLOCKDMA];
314         int sgindex;
315         int statindex;
316         struct viodasd_device *d;
317         unsigned long flags;
318
319         start = (u64)req->sector << 9;
320
321         if (rq_data_dir(req) == READ) {
322                 direction = DMA_FROM_DEVICE;
323                 viocmd = viomajorsubtype_blockio | vioblockread;
324                 statindex = 0;
325         } else {
326                 direction = DMA_TO_DEVICE;
327                 viocmd = viomajorsubtype_blockio | vioblockwrite;
328                 statindex = 1;
329         }
330
331         d = req->rq_disk->private_data;
332
333         /* Now build the scatter-gather list */
334         nsg = blk_rq_map_sg(req->q, req, sg);
335         nsg = dma_map_sg(iSeries_vio_dev, sg, nsg, direction);
336
337         spin_lock_irqsave(&viodasd_spinlock, flags);
338         num_req_outstanding++;
339
340         /* This optimization handles a single DMA block */
341         if (nsg == 1)
342                 hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
343                                 HvLpEvent_Type_VirtualIo, viocmd,
344                                 HvLpEvent_AckInd_DoAck,
345                                 HvLpEvent_AckType_ImmediateAck,
346                                 viopath_sourceinst(viopath_hostLp),
347                                 viopath_targetinst(viopath_hostLp),
348                                 (u64)(unsigned long)req, VIOVERSION << 16,
349                                 ((u64)DEVICE_NO(d) << 48), start,
350                                 ((u64)sg_dma_address(&sg[0])) << 32,
351                                 sg_dma_len(&sg[0]));
352         else {
353                 bevent = (struct vioblocklpevent *)
354                         vio_get_event_buffer(viomajorsubtype_blockio);
355                 if (bevent == NULL) {
356                         printk(VIOD_KERN_WARNING
357                                "error allocating disk event buffer\n");
358                         goto error_ret;
359                 }
360
361                 /*
362                  * Now build up the actual request.  Note that we store
363                  * the pointer to the request in the correlation
364                  * token so we can match the response up later
365                  */
366                 memset(bevent, 0, sizeof(struct vioblocklpevent));
367                 bevent->event.xFlags.xValid = 1;
368                 bevent->event.xFlags.xFunction = HvLpEvent_Function_Int;
369                 bevent->event.xFlags.xAckInd = HvLpEvent_AckInd_DoAck;
370                 bevent->event.xFlags.xAckType = HvLpEvent_AckType_ImmediateAck;
371                 bevent->event.xType = HvLpEvent_Type_VirtualIo;
372                 bevent->event.xSubtype = viocmd;
373                 bevent->event.xSourceLp = HvLpConfig_getLpIndex();
374                 bevent->event.xTargetLp = viopath_hostLp;
375                 bevent->event.xSizeMinus1 =
376                         offsetof(struct vioblocklpevent, u.rw_data.dma_info) +
377                         (sizeof(bevent->u.rw_data.dma_info[0]) * nsg) - 1;
378                 bevent->event.xSourceInstanceId =
379                         viopath_sourceinst(viopath_hostLp);
380                 bevent->event.xTargetInstanceId =
381                         viopath_targetinst(viopath_hostLp);
382                 bevent->event.xCorrelationToken = (u64)req;
383                 bevent->version = VIOVERSION;
384                 bevent->disk = DEVICE_NO(d);
385                 bevent->u.rw_data.offset = start;
386
387                 /*
388                  * Copy just the dma information from the sg list
389                  * into the request
390                  */
391                 for (sgindex = 0; sgindex < nsg; sgindex++) {
392                         bevent->u.rw_data.dma_info[sgindex].token =
393                                 sg_dma_address(&sg[sgindex]);
394                         bevent->u.rw_data.dma_info[sgindex].len =
395                                 sg_dma_len(&sg[sgindex]);
396                 }
397
398                 /* Send the request */
399                 hvrc = HvCallEvent_signalLpEvent(&bevent->event);
400                 vio_free_event_buffer(viomajorsubtype_blockio, bevent);
401         }
402
403         if (hvrc != HvLpEvent_Rc_Good) {
404                 printk(VIOD_KERN_WARNING
405                        "error sending disk event to OS/400 (rc %d)\n",
406                        (int)hvrc);
407                 goto error_ret;
408         }
409         spin_unlock_irqrestore(&viodasd_spinlock, flags);
410         return 0;
411
412 error_ret:
413         num_req_outstanding--;
414         spin_unlock_irqrestore(&viodasd_spinlock, flags);
415         dma_unmap_sg(iSeries_vio_dev, sg, nsg, direction);
416         return -1;
417 }
418
419 /*
420  * This is the external request processing routine
421  */
422 static void do_viodasd_request(request_queue_t *q)
423 {
424         struct request *req;
425
426         /*
427          * If we already have the maximum number of requests
428          * outstanding to OS/400 just bail out. We'll come
429          * back later.
430          */
431         while (num_req_outstanding < VIOMAXREQ) {
432                 req = elv_next_request(q);
433                 if (req == NULL)
434                         return;
435                 /* dequeue the current request from the queue */
436                 blkdev_dequeue_request(req);
437                 /* check that request contains a valid command */
438                 if (!blk_fs_request(req)) {
439                         viodasd_end_request(req, 0, req->hard_nr_sectors);
440                         continue;
441                 }
442                 /* Try sending the request */
443                 if (send_request(req) != 0)
444                         viodasd_end_request(req, 0, req->hard_nr_sectors);
445         }
446 }
447
448 /*
449  * Probe a single disk and fill in the viodasd_device structure
450  * for it.
451  */
452 static void probe_disk(struct viodasd_device *d)
453 {
454         HvLpEvent_Rc hvrc;
455         struct viodasd_waitevent we;
456         int dev_no = DEVICE_NO(d);
457         struct gendisk *g;
458         struct request_queue *q;
459
460         init_completion(&we.com);
461
462         /* Send the open event to OS/400 */
463         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
464                         HvLpEvent_Type_VirtualIo,
465                         viomajorsubtype_blockio | vioblockopen,
466                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
467                         viopath_sourceinst(viopath_hostLp),
468                         viopath_targetinst(viopath_hostLp),
469                         (u64)(unsigned long)&we, VIOVERSION << 16,
470                         ((u64)dev_no << 48) | ((u64)vioblockflags_ro << 32),
471                         0, 0, 0);
472         if (hvrc != 0) {
473                 printk(VIOD_KERN_WARNING "bad rc on HV open %d\n", (int)hvrc);
474                 return;
475         }
476
477         wait_for_completion(&we.com);
478
479         if (we.rc != 0)
480                 return;
481         if (we.max_disk > (MAX_DISKNO - 1)) {
482                 static int warned;
483
484                 if (warned == 0) {
485                         warned++;
486                         printk(VIOD_KERN_INFO
487                                 "Only examining the first %d "
488                                 "of %d disks connected\n",
489                                 MAX_DISKNO, we.max_disk + 1);
490                 }
491         }
492
493         /* Send the close event to OS/400.  We DON'T expect a response */
494         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
495                         HvLpEvent_Type_VirtualIo,
496                         viomajorsubtype_blockio | vioblockclose,
497                         HvLpEvent_AckInd_NoAck, HvLpEvent_AckType_ImmediateAck,
498                         viopath_sourceinst(viopath_hostLp),
499                         viopath_targetinst(viopath_hostLp),
500                         0, VIOVERSION << 16,
501                         ((u64)dev_no << 48) | ((u64)vioblockflags_ro << 32),
502                         0, 0, 0);
503         if (hvrc != 0) {
504                 printk(VIOD_KERN_WARNING
505                        "bad rc sending event to OS/400 %d\n", (int)hvrc);
506                 return;
507         }
508         printk(VIOD_KERN_INFO "disk %d: %lu sectors (%lu MB) "
509                         "CHS=%d/%d/%d sector size %d\n",
510                         dev_no, (unsigned long)(d->size >> 9),
511                         (unsigned long)(d->size >> 20),
512                         (int)d->cylinders, (int)d->tracks,
513                         (int)d->sectors, (int)d->bytes_per_sector);
514         /* create the request queue for the disk */
515         spin_lock_init(&d->q_lock);
516         q = blk_init_queue(do_viodasd_request, &d->q_lock);
517         if (q == NULL) {
518                 printk(VIOD_KERN_WARNING "cannot allocate queue for disk %d\n",
519                                 dev_no);
520                 return;
521         }
522         g = alloc_disk(1 << PARTITION_SHIFT);
523         if (g == NULL) {
524                 printk(VIOD_KERN_WARNING
525                                 "cannot allocate disk structure for disk %d\n",
526                                 dev_no);
527                 blk_cleanup_queue(q);
528                 return;
529         }
530
531         d->disk = g;
532         blk_queue_max_hw_segments(q, VIOMAXBLOCKDMA);
533         blk_queue_max_phys_segments(q, VIOMAXBLOCKDMA);
534         blk_queue_max_sectors(q, VIODASD_MAXSECTORS);
535         g->major = VIODASD_MAJOR;
536         g->first_minor = dev_no << PARTITION_SHIFT;
537         if (dev_no >= 26)
538                 snprintf(g->disk_name, sizeof(g->disk_name),
539                                 VIOD_GENHD_NAME "%c%c",
540                                 'a' + (dev_no / 26) - 1, 'a' + (dev_no % 26));
541         else
542                 snprintf(g->disk_name, sizeof(g->disk_name),
543                                 VIOD_GENHD_NAME "%c", 'a' + (dev_no % 26));
544         snprintf(g->devfs_name, sizeof(g->devfs_name),
545                         "%s%d", VIOD_GENHD_DEVFS_NAME, dev_no);
546         g->fops = &viodasd_fops;
547         g->queue = q;
548         g->private_data = d;
549         set_capacity(g, d->size >> 9);
550
551         /* register us in the global list */
552         add_disk(g);
553 }
554
555 /* returns the total number of scatterlist elements converted */
556 static int block_event_to_scatterlist(const struct vioblocklpevent *bevent,
557                 struct scatterlist *sg, int *total_len)
558 {
559         int i, numsg;
560         const struct rw_data *rw_data = &bevent->u.rw_data;
561         static const int offset =
562                 offsetof(struct vioblocklpevent, u.rw_data.dma_info);
563         static const int element_size = sizeof(rw_data->dma_info[0]);
564
565         numsg = ((bevent->event.xSizeMinus1 + 1) - offset) / element_size;
566         if (numsg > VIOMAXBLOCKDMA)
567                 numsg = VIOMAXBLOCKDMA;
568
569         *total_len = 0;
570         memset(sg, 0, sizeof(sg[0]) * VIOMAXBLOCKDMA);
571
572         for (i = 0; (i < numsg) && (rw_data->dma_info[i].len > 0); ++i) {
573                 sg_dma_address(&sg[i]) = rw_data->dma_info[i].token;
574                 sg_dma_len(&sg[i]) = rw_data->dma_info[i].len;
575                 *total_len += rw_data->dma_info[i].len;
576         }
577         return i;
578 }
579
580 /*
581  * Restart all queues, starting with the one _after_ the disk given,
582  * thus reducing the chance of starvation of higher numbered disks.
583  */
584 static void viodasd_restart_all_queues_starting_from(int first_index)
585 {
586         int i;
587
588         for (i = first_index + 1; i < MAX_DISKNO; ++i)
589                 if (viodasd_devices[i].disk)
590                         blk_run_queue(viodasd_devices[i].disk->queue);
591         for (i = 0; i <= first_index; ++i)
592                 if (viodasd_devices[i].disk)
593                         blk_run_queue(viodasd_devices[i].disk->queue);
594 }
595
596 /*
597  * For read and write requests, decrement the number of outstanding requests,
598  * Free the DMA buffers we allocated.
599  */
600 static int viodasd_handle_read_write(struct vioblocklpevent *bevent)
601 {
602         int num_sg, num_sect, pci_direction, total_len;
603         struct request *req;
604         struct scatterlist sg[VIOMAXBLOCKDMA];
605         struct HvLpEvent *event = &bevent->event;
606         unsigned long irq_flags;
607         int device_no;
608         int error;
609         spinlock_t *qlock;
610
611         num_sg = block_event_to_scatterlist(bevent, sg, &total_len);
612         num_sect = total_len >> 9;
613         if (event->xSubtype == (viomajorsubtype_blockio | vioblockread))
614                 pci_direction = DMA_FROM_DEVICE;
615         else
616                 pci_direction = DMA_TO_DEVICE;
617         dma_unmap_sg(iSeries_vio_dev, sg, num_sg, pci_direction);
618
619         /*
620          * Since this is running in interrupt mode, we need to make sure
621          * we're not stepping on any global I/O operations
622          */
623         spin_lock_irqsave(&viodasd_spinlock, irq_flags);
624         num_req_outstanding--;
625         spin_unlock_irqrestore(&viodasd_spinlock, irq_flags);
626
627         req = (struct request *)bevent->event.xCorrelationToken;
628         device_no = DEVICE_NO(req->rq_disk->private_data);
629
630         error = event->xRc != HvLpEvent_Rc_Good;
631         if (error) {
632                 const struct vio_error_entry *err;
633                 err = vio_lookup_rc(viodasd_err_table, bevent->sub_result);
634                 printk(VIOD_KERN_WARNING "read/write error %d:0x%04x (%s)\n",
635                                 event->xRc, bevent->sub_result, err->msg);
636                 num_sect = req->hard_nr_sectors;
637         }
638         qlock = req->q->queue_lock;
639         spin_lock_irqsave(qlock, irq_flags);
640         viodasd_end_request(req, !error, num_sect);
641         spin_unlock_irqrestore(qlock, irq_flags);
642
643         /* Finally, try to get more requests off of this device's queue */
644         viodasd_restart_all_queues_starting_from(device_no);
645
646         return 0;
647 }
648
649 /* This routine handles incoming block LP events */
650 static void handle_block_event(struct HvLpEvent *event)
651 {
652         struct vioblocklpevent *bevent = (struct vioblocklpevent *)event;
653         struct viodasd_waitevent *pwe;
654
655         if (event == NULL)
656                 /* Notification that a partition went away! */
657                 return;
658         /* First, we should NEVER get an int here...only acks */
659         if (event->xFlags.xFunction == HvLpEvent_Function_Int) {
660                 printk(VIOD_KERN_WARNING
661                        "Yikes! got an int in viodasd event handler!\n");
662                 if (event->xFlags.xAckInd == HvLpEvent_AckInd_DoAck) {
663                         event->xRc = HvLpEvent_Rc_InvalidSubtype;
664                         HvCallEvent_ackLpEvent(event);
665                 }
666         }
667
668         switch (event->xSubtype & VIOMINOR_SUBTYPE_MASK) {
669         case vioblockopen:
670                 /*
671                  * Handle a response to an open request.  We get all the
672                  * disk information in the response, so update it.  The
673                  * correlation token contains a pointer to a waitevent
674                  * structure that has a completion in it.  update the
675                  * return code in the waitevent structure and post the
676                  * completion to wake up the guy who sent the request
677                  */
678                 pwe = (struct viodasd_waitevent *)event->xCorrelationToken;
679                 pwe->rc = event->xRc;
680                 pwe->sub_result = bevent->sub_result;
681                 if (event->xRc == HvLpEvent_Rc_Good) {
682                         const struct open_data *data = &bevent->u.open_data;
683                         struct viodasd_device *device =
684                                 &viodasd_devices[bevent->disk];
685                         device->read_only =
686                                 bevent->flags & vioblockflags_ro;
687                         device->size = data->disk_size;
688                         device->cylinders = data->cylinders;
689                         device->tracks = data->tracks;
690                         device->sectors = data->sectors;
691                         device->bytes_per_sector = data->bytes_per_sector;
692                         pwe->max_disk = data->max_disk;
693                 }
694                 complete(&pwe->com);
695                 break;
696         case vioblockclose:
697                 break;
698         case vioblockread:
699         case vioblockwrite:
700                 viodasd_handle_read_write(bevent);
701                 break;
702
703         default:
704                 printk(VIOD_KERN_WARNING "invalid subtype!");
705                 if (event->xFlags.xAckInd == HvLpEvent_AckInd_DoAck) {
706                         event->xRc = HvLpEvent_Rc_InvalidSubtype;
707                         HvCallEvent_ackLpEvent(event);
708                 }
709         }
710 }
711
712 /*
713  * Initialize the whole device driver.  Handle module and non-module
714  * versions
715  */
716 static int __init viodasd_init(void)
717 {
718         int i;
719
720         /* Try to open to our host lp */
721         if (viopath_hostLp == HvLpIndexInvalid)
722                 vio_set_hostlp();
723
724         if (viopath_hostLp == HvLpIndexInvalid) {
725                 printk(VIOD_KERN_WARNING "invalid hosting partition\n");
726                 return -EIO;
727         }
728
729         printk(VIOD_KERN_INFO "vers " VIOD_VERS ", hosting partition %d\n",
730                         viopath_hostLp);
731
732         /* register the block device */
733         if (register_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME)) {
734                 printk(VIOD_KERN_WARNING
735                                 "Unable to get major number %d for %s\n",
736                                 VIODASD_MAJOR, VIOD_GENHD_NAME);
737                 return -EIO;
738         }
739         /* Actually open the path to the hosting partition */
740         if (viopath_open(viopath_hostLp, viomajorsubtype_blockio,
741                                 VIOMAXREQ + 2)) {
742                 printk(VIOD_KERN_WARNING
743                        "error opening path to host partition %d\n",
744                        viopath_hostLp);
745                 unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
746                 return -EIO;
747         }
748
749         /* Initialize our request handler */
750         vio_setHandler(viomajorsubtype_blockio, handle_block_event);
751
752         for (i = 0; i < MAX_DISKNO; i++)
753                 probe_disk(&viodasd_devices[i]);
754
755         return 0;
756 }
757 module_init(viodasd_init);
758
759 void viodasd_exit(void)
760 {
761         int i;
762         struct viodasd_device *d;
763
764         for (i = 0; i < MAX_DISKNO; i++) {
765                 d = &viodasd_devices[i];
766                 if (d->disk) {
767                         del_gendisk(d->disk);
768                         put_disk(d->disk);
769                         blk_cleanup_queue(d->disk->queue);
770                         d->disk = NULL;
771                 }
772         }
773         vio_clearHandler(viomajorsubtype_blockio);
774         unregister_blkdev(VIODASD_MAJOR, VIOD_GENHD_NAME);
775         viopath_close(viopath_hostLp, viomajorsubtype_blockio, VIOMAXREQ + 2);
776 }
777
778 module_exit(viodasd_exit);