Merge git://git.infradead.org/battery-2.6
[linux-2.6] / drivers / message / i2o / i2o_block.c
1 /*
2  *      Block OSM
3  *
4  *      Copyright (C) 1999-2002 Red Hat Software
5  *
6  *      Written by Alan Cox, Building Number Three Ltd
7  *
8  *      This program is free software; you can redistribute it and/or modify it
9  *      under the terms of the GNU General Public License as published by the
10  *      Free Software Foundation; either version 2 of the License, or (at your
11  *      option) any later version.
12  *
13  *      This program is distributed in the hope that it will be useful, but
14  *      WITHOUT ANY WARRANTY; without even the implied warranty of
15  *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *      General Public License for more details.
17  *
18  *      For the purpose of avoiding doubt the preferred form of the work
19  *      for making modifications shall be a standards compliant form such
20  *      gzipped tar and not one requiring a proprietary or patent encumbered
21  *      tool to unpack.
22  *
23  *      Fixes/additions:
24  *              Steve Ralston:
25  *                      Multiple device handling error fixes,
26  *                      Added a queue depth.
27  *              Alan Cox:
28  *                      FC920 has an rmw bug. Dont or in the end marker.
29  *                      Removed queue walk, fixed for 64bitness.
30  *                      Rewrote much of the code over time
31  *                      Added indirect block lists
32  *                      Handle 64K limits on many controllers
33  *                      Don't use indirects on the Promise (breaks)
34  *                      Heavily chop down the queue depths
35  *              Deepak Saxena:
36  *                      Independent queues per IOP
37  *                      Support for dynamic device creation/deletion
38  *                      Code cleanup
39  *                      Support for larger I/Os through merge* functions
40  *                      (taken from DAC960 driver)
41  *              Boji T Kannanthanam:
42  *                      Set the I2O Block devices to be detected in increasing
43  *                      order of TIDs during boot.
44  *                      Search and set the I2O block device that we boot off
45  *                      from as the first device to be claimed (as /dev/i2o/hda)
46  *                      Properly attach/detach I2O gendisk structure from the
47  *                      system gendisk list. The I2O block devices now appear in
48  *                      /proc/partitions.
49  *              Markus Lidel <Markus.Lidel@shadowconnect.com>:
50  *                      Minor bugfixes for 2.6.
51  */
52
53 #include <linux/module.h>
54 #include <linux/i2o.h>
55
56 #include <linux/mempool.h>
57
58 #include <linux/genhd.h>
59 #include <linux/blkdev.h>
60 #include <linux/hdreg.h>
61
62 #include <scsi/scsi.h>
63
64 #include "i2o_block.h"
65
66 #define OSM_NAME        "block-osm"
67 #define OSM_VERSION     "1.325"
68 #define OSM_DESCRIPTION "I2O Block Device OSM"
69
70 static struct i2o_driver i2o_block_driver;
71
72 /* global Block OSM request mempool */
73 static struct i2o_block_mempool i2o_blk_req_pool;
74
75 /* Block OSM class handling definition */
76 static struct i2o_class_id i2o_block_class_id[] = {
77         {I2O_CLASS_RANDOM_BLOCK_STORAGE},
78         {I2O_CLASS_END}
79 };
80
81 /**
82  *      i2o_block_device_free - free the memory of the I2O Block device
83  *      @dev: I2O Block device, which should be cleaned up
84  *
85  *      Frees the request queue, gendisk and the i2o_block_device structure.
86  */
87 static void i2o_block_device_free(struct i2o_block_device *dev)
88 {
89         blk_cleanup_queue(dev->gd->queue);
90
91         put_disk(dev->gd);
92
93         kfree(dev);
94 };
95
96 /**
97  *      i2o_block_remove - remove the I2O Block device from the system again
98  *      @dev: I2O Block device which should be removed
99  *
100  *      Remove gendisk from system and free all allocated memory.
101  *
102  *      Always returns 0.
103  */
104 static int i2o_block_remove(struct device *dev)
105 {
106         struct i2o_device *i2o_dev = to_i2o_device(dev);
107         struct i2o_block_device *i2o_blk_dev = dev_get_drvdata(dev);
108
109         osm_info("device removed (TID: %03x): %s\n", i2o_dev->lct_data.tid,
110                  i2o_blk_dev->gd->disk_name);
111
112         i2o_event_register(i2o_dev, &i2o_block_driver, 0, 0);
113
114         del_gendisk(i2o_blk_dev->gd);
115
116         dev_set_drvdata(dev, NULL);
117
118         i2o_device_claim_release(i2o_dev);
119
120         i2o_block_device_free(i2o_blk_dev);
121
122         return 0;
123 };
124
125 /**
126  *      i2o_block_device flush - Flush all dirty data of I2O device dev
127  *      @dev: I2O device which should be flushed
128  *
129  *      Flushes all dirty data on device dev.
130  *
131  *      Returns 0 on success or negative error code on failure.
132  */
133 static int i2o_block_device_flush(struct i2o_device *dev)
134 {
135         struct i2o_message *msg;
136
137         msg = i2o_msg_get_wait(dev->iop, I2O_TIMEOUT_MESSAGE_GET);
138         if (IS_ERR(msg))
139                 return PTR_ERR(msg);
140
141         msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
142         msg->u.head[1] =
143             cpu_to_le32(I2O_CMD_BLOCK_CFLUSH << 24 | HOST_TID << 12 | dev->
144                         lct_data.tid);
145         msg->body[0] = cpu_to_le32(60 << 16);
146         osm_debug("Flushing...\n");
147
148         return i2o_msg_post_wait(dev->iop, msg, 60);
149 };
150
151 /**
152  *      i2o_block_device_mount - Mount (load) the media of device dev
153  *      @dev: I2O device which should receive the mount request
154  *      @media_id: Media Identifier
155  *
156  *      Load a media into drive. Identifier should be set to -1, because the
157  *      spec does not support any other value.
158  *
159  *      Returns 0 on success or negative error code on failure.
160  */
161 static int i2o_block_device_mount(struct i2o_device *dev, u32 media_id)
162 {
163         struct i2o_message *msg;
164
165         msg = i2o_msg_get_wait(dev->iop, I2O_TIMEOUT_MESSAGE_GET);
166         if (IS_ERR(msg))
167                 return PTR_ERR(msg);
168
169         msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
170         msg->u.head[1] =
171             cpu_to_le32(I2O_CMD_BLOCK_MMOUNT << 24 | HOST_TID << 12 | dev->
172                         lct_data.tid);
173         msg->body[0] = cpu_to_le32(-1);
174         msg->body[1] = cpu_to_le32(0x00000000);
175         osm_debug("Mounting...\n");
176
177         return i2o_msg_post_wait(dev->iop, msg, 2);
178 };
179
180 /**
181  *      i2o_block_device_lock - Locks the media of device dev
182  *      @dev: I2O device which should receive the lock request
183  *      @media_id: Media Identifier
184  *
185  *      Lock media of device dev to prevent removal. The media identifier
186  *      should be set to -1, because the spec does not support any other value.
187  *
188  *      Returns 0 on success or negative error code on failure.
189  */
190 static int i2o_block_device_lock(struct i2o_device *dev, u32 media_id)
191 {
192         struct i2o_message *msg;
193
194         msg = i2o_msg_get_wait(dev->iop, I2O_TIMEOUT_MESSAGE_GET);
195         if (IS_ERR(msg))
196                 return PTR_ERR(msg);
197
198         msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
199         msg->u.head[1] =
200             cpu_to_le32(I2O_CMD_BLOCK_MLOCK << 24 | HOST_TID << 12 | dev->
201                         lct_data.tid);
202         msg->body[0] = cpu_to_le32(-1);
203         osm_debug("Locking...\n");
204
205         return i2o_msg_post_wait(dev->iop, msg, 2);
206 };
207
208 /**
209  *      i2o_block_device_unlock - Unlocks the media of device dev
210  *      @dev: I2O device which should receive the unlocked request
211  *      @media_id: Media Identifier
212  *
213  *      Unlocks the media in device dev. The media identifier should be set to
214  *      -1, because the spec does not support any other value.
215  *
216  *      Returns 0 on success or negative error code on failure.
217  */
218 static int i2o_block_device_unlock(struct i2o_device *dev, u32 media_id)
219 {
220         struct i2o_message *msg;
221
222         msg = i2o_msg_get_wait(dev->iop, I2O_TIMEOUT_MESSAGE_GET);
223         if (IS_ERR(msg))
224                 return PTR_ERR(msg);
225
226         msg->u.head[0] = cpu_to_le32(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0);
227         msg->u.head[1] =
228             cpu_to_le32(I2O_CMD_BLOCK_MUNLOCK << 24 | HOST_TID << 12 | dev->
229                         lct_data.tid);
230         msg->body[0] = cpu_to_le32(media_id);
231         osm_debug("Unlocking...\n");
232
233         return i2o_msg_post_wait(dev->iop, msg, 2);
234 };
235
236 /**
237  *      i2o_block_device_power - Power management for device dev
238  *      @dev: I2O device which should receive the power management request
239  *      @op: Operation to send
240  *
241  *      Send a power management request to the device dev.
242  *
243  *      Returns 0 on success or negative error code on failure.
244  */
245 static int i2o_block_device_power(struct i2o_block_device *dev, u8 op)
246 {
247         struct i2o_device *i2o_dev = dev->i2o_dev;
248         struct i2o_controller *c = i2o_dev->iop;
249         struct i2o_message *msg;
250         int rc;
251
252         msg = i2o_msg_get_wait(c, I2O_TIMEOUT_MESSAGE_GET);
253         if (IS_ERR(msg))
254                 return PTR_ERR(msg);
255
256         msg->u.head[0] = cpu_to_le32(FOUR_WORD_MSG_SIZE | SGL_OFFSET_0);
257         msg->u.head[1] =
258             cpu_to_le32(I2O_CMD_BLOCK_POWER << 24 | HOST_TID << 12 | i2o_dev->
259                         lct_data.tid);
260         msg->body[0] = cpu_to_le32(op << 24);
261         osm_debug("Power...\n");
262
263         rc = i2o_msg_post_wait(c, msg, 60);
264         if (!rc)
265                 dev->power = op;
266
267         return rc;
268 };
269
270 /**
271  *      i2o_block_request_alloc - Allocate an I2O block request struct
272  *
273  *      Allocates an I2O block request struct and initialize the list.
274  *
275  *      Returns a i2o_block_request pointer on success or negative error code
276  *      on failure.
277  */
278 static inline struct i2o_block_request *i2o_block_request_alloc(void)
279 {
280         struct i2o_block_request *ireq;
281
282         ireq = mempool_alloc(i2o_blk_req_pool.pool, GFP_ATOMIC);
283         if (!ireq)
284                 return ERR_PTR(-ENOMEM);
285
286         INIT_LIST_HEAD(&ireq->queue);
287
288         return ireq;
289 };
290
291 /**
292  *      i2o_block_request_free - Frees a I2O block request
293  *      @ireq: I2O block request which should be freed
294  *
295  *      Frees the allocated memory (give it back to the request mempool).
296  */
297 static inline void i2o_block_request_free(struct i2o_block_request *ireq)
298 {
299         mempool_free(ireq, i2o_blk_req_pool.pool);
300 };
301
302 /**
303  *      i2o_block_sglist_alloc - Allocate the SG list and map it
304  *      @c: I2O controller to which the request belongs
305  *      @ireq: I2O block request
306  *      @mptr: message body pointer
307  *
308  *      Builds the SG list and map it to be accessable by the controller.
309  *
310  *      Returns 0 on failure or 1 on success.
311  */
312 static inline int i2o_block_sglist_alloc(struct i2o_controller *c,
313                                          struct i2o_block_request *ireq,
314                                          u32 ** mptr)
315 {
316         int nents;
317         enum dma_data_direction direction;
318
319         ireq->dev = &c->pdev->dev;
320         nents = blk_rq_map_sg(ireq->req->q, ireq->req, ireq->sg_table);
321
322         if (rq_data_dir(ireq->req) == READ)
323                 direction = PCI_DMA_FROMDEVICE;
324         else
325                 direction = PCI_DMA_TODEVICE;
326
327         ireq->sg_nents = nents;
328
329         return i2o_dma_map_sg(c, ireq->sg_table, nents, direction, mptr);
330 };
331
332 /**
333  *      i2o_block_sglist_free - Frees the SG list
334  *      @ireq: I2O block request from which the SG should be freed
335  *
336  *      Frees the SG list from the I2O block request.
337  */
338 static inline void i2o_block_sglist_free(struct i2o_block_request *ireq)
339 {
340         enum dma_data_direction direction;
341
342         if (rq_data_dir(ireq->req) == READ)
343                 direction = PCI_DMA_FROMDEVICE;
344         else
345                 direction = PCI_DMA_TODEVICE;
346
347         dma_unmap_sg(ireq->dev, ireq->sg_table, ireq->sg_nents, direction);
348 };
349
350 /**
351  *      i2o_block_prep_req_fn - Allocates I2O block device specific struct
352  *      @q: request queue for the request
353  *      @req: the request to prepare
354  *
355  *      Allocate the necessary i2o_block_request struct and connect it to
356  *      the request. This is needed that we not loose the SG list later on.
357  *
358  *      Returns BLKPREP_OK on success or BLKPREP_DEFER on failure.
359  */
360 static int i2o_block_prep_req_fn(struct request_queue *q, struct request *req)
361 {
362         struct i2o_block_device *i2o_blk_dev = q->queuedata;
363         struct i2o_block_request *ireq;
364
365         if (unlikely(!i2o_blk_dev)) {
366                 osm_err("block device already removed\n");
367                 return BLKPREP_KILL;
368         }
369
370         /* connect the i2o_block_request to the request */
371         if (!req->special) {
372                 ireq = i2o_block_request_alloc();
373                 if (unlikely(IS_ERR(ireq))) {
374                         osm_debug("unable to allocate i2o_block_request!\n");
375                         return BLKPREP_DEFER;
376                 }
377
378                 ireq->i2o_blk_dev = i2o_blk_dev;
379                 req->special = ireq;
380                 ireq->req = req;
381         }
382         /* do not come back here */
383         req->cmd_flags |= REQ_DONTPREP;
384
385         return BLKPREP_OK;
386 };
387
388 /**
389  *      i2o_block_delayed_request_fn - delayed request queue function
390  *      @work: the delayed request with the queue to start
391  *
392  *      If the request queue is stopped for a disk, and there is no open
393  *      request, a new event is created, which calls this function to start
394  *      the queue after I2O_BLOCK_REQUEST_TIME. Otherwise the queue will never
395  *      be started again.
396  */
397 static void i2o_block_delayed_request_fn(struct work_struct *work)
398 {
399         struct i2o_block_delayed_request *dreq =
400                 container_of(work, struct i2o_block_delayed_request,
401                              work.work);
402         struct request_queue *q = dreq->queue;
403         unsigned long flags;
404
405         spin_lock_irqsave(q->queue_lock, flags);
406         blk_start_queue(q);
407         spin_unlock_irqrestore(q->queue_lock, flags);
408         kfree(dreq);
409 };
410
411 /**
412  *      i2o_block_end_request - Post-processing of completed commands
413  *      @req: request which should be completed
414  *      @uptodate: 1 for success, 0 for I/O error, < 0 for specific error
415  *      @nr_bytes: number of bytes to complete
416  *
417  *      Mark the request as complete. The lock must not be held when entering.
418  *
419  */
420 static void i2o_block_end_request(struct request *req, int uptodate,
421                                   int nr_bytes)
422 {
423         struct i2o_block_request *ireq = req->special;
424         struct i2o_block_device *dev = ireq->i2o_blk_dev;
425         struct request_queue *q = req->q;
426         unsigned long flags;
427
428         if (end_that_request_chunk(req, uptodate, nr_bytes)) {
429                 int leftover = (req->hard_nr_sectors << KERNEL_SECTOR_SHIFT);
430
431                 if (blk_pc_request(req))
432                         leftover = req->data_len;
433
434                 if (end_io_error(uptodate))
435                         end_that_request_chunk(req, 0, leftover);
436         }
437
438         add_disk_randomness(req->rq_disk);
439
440         spin_lock_irqsave(q->queue_lock, flags);
441
442         end_that_request_last(req, uptodate);
443
444         if (likely(dev)) {
445                 dev->open_queue_depth--;
446                 list_del(&ireq->queue);
447         }
448
449         blk_start_queue(q);
450
451         spin_unlock_irqrestore(q->queue_lock, flags);
452
453         i2o_block_sglist_free(ireq);
454         i2o_block_request_free(ireq);
455 };
456
457 /**
458  *      i2o_block_reply - Block OSM reply handler.
459  *      @c: I2O controller from which the message arrives
460  *      @m: message id of reply
461  *      @msg: the actual I2O message reply
462  *
463  *      This function gets all the message replies.
464  *
465  */
466 static int i2o_block_reply(struct i2o_controller *c, u32 m,
467                            struct i2o_message *msg)
468 {
469         struct request *req;
470         int uptodate = 1;
471
472         req = i2o_cntxt_list_get(c, le32_to_cpu(msg->u.s.tcntxt));
473         if (unlikely(!req)) {
474                 osm_err("NULL reply received!\n");
475                 return -1;
476         }
477
478         /*
479          *      Lets see what is cooking. We stuffed the
480          *      request in the context.
481          */
482
483         if ((le32_to_cpu(msg->body[0]) >> 24) != 0) {
484                 u32 status = le32_to_cpu(msg->body[0]);
485                 /*
486                  *      Device not ready means two things. One is that the
487                  *      the thing went offline (but not a removal media)
488                  *
489                  *      The second is that you have a SuperTrak 100 and the
490                  *      firmware got constipated. Unlike standard i2o card
491                  *      setups the supertrak returns an error rather than
492                  *      blocking for the timeout in these cases.
493                  *
494                  *      Don't stick a supertrak100 into cache aggressive modes
495                  */
496
497                 osm_err("TID %03x error status: 0x%02x, detailed status: "
498                         "0x%04x\n", (le32_to_cpu(msg->u.head[1]) >> 12 & 0xfff),
499                         status >> 24, status & 0xffff);
500
501                 req->errors++;
502
503                 uptodate = 0;
504         }
505
506         i2o_block_end_request(req, uptodate, le32_to_cpu(msg->body[1]));
507
508         return 1;
509 };
510
511 static void i2o_block_event(struct work_struct *work)
512 {
513         struct i2o_event *evt = container_of(work, struct i2o_event, work);
514         osm_debug("event received\n");
515         kfree(evt);
516 };
517
518 /*
519  *      SCSI-CAM for ioctl geometry mapping
520  *      Duplicated with SCSI - this should be moved into somewhere common
521  *      perhaps genhd ?
522  *
523  * LBA -> CHS mapping table taken from:
524  *
525  * "Incorporating the I2O Architecture into BIOS for Intel Architecture
526  *  Platforms"
527  *
528  * This is an I2O document that is only available to I2O members,
529  * not developers.
530  *
531  * From my understanding, this is how all the I2O cards do this
532  *
533  * Disk Size      | Sectors | Heads | Cylinders
534  * ---------------+---------+-------+-------------------
535  * 1 < X <= 528M  | 63      | 16    | X/(63 * 16 * 512)
536  * 528M < X <= 1G | 63      | 32    | X/(63 * 32 * 512)
537  * 1 < X <528M    | 63      | 16    | X/(63 * 16 * 512)
538  * 1 < X <528M    | 63      | 16    | X/(63 * 16 * 512)
539  *
540  */
541 #define BLOCK_SIZE_528M         1081344
542 #define BLOCK_SIZE_1G           2097152
543 #define BLOCK_SIZE_21G          4403200
544 #define BLOCK_SIZE_42G          8806400
545 #define BLOCK_SIZE_84G          17612800
546
547 static void i2o_block_biosparam(unsigned long capacity, unsigned short *cyls,
548                                 unsigned char *hds, unsigned char *secs)
549 {
550         unsigned long heads, sectors, cylinders;
551
552         sectors = 63L;          /* Maximize sectors per track */
553         if (capacity <= BLOCK_SIZE_528M)
554                 heads = 16;
555         else if (capacity <= BLOCK_SIZE_1G)
556                 heads = 32;
557         else if (capacity <= BLOCK_SIZE_21G)
558                 heads = 64;
559         else if (capacity <= BLOCK_SIZE_42G)
560                 heads = 128;
561         else
562                 heads = 255;
563
564         cylinders = (unsigned long)capacity / (heads * sectors);
565
566         *cyls = (unsigned short)cylinders;      /* Stuff return values */
567         *secs = (unsigned char)sectors;
568         *hds = (unsigned char)heads;
569 }
570
571 /**
572  *      i2o_block_open - Open the block device
573  *      @inode: inode for block device being opened
574  *      @file: file to open
575  *
576  *      Power up the device, mount and lock the media. This function is called,
577  *      if the block device is opened for access.
578  *
579  *      Returns 0 on success or negative error code on failure.
580  */
581 static int i2o_block_open(struct inode *inode, struct file *file)
582 {
583         struct i2o_block_device *dev = inode->i_bdev->bd_disk->private_data;
584
585         if (!dev->i2o_dev)
586                 return -ENODEV;
587
588         if (dev->power > 0x1f)
589                 i2o_block_device_power(dev, 0x02);
590
591         i2o_block_device_mount(dev->i2o_dev, -1);
592
593         i2o_block_device_lock(dev->i2o_dev, -1);
594
595         osm_debug("Ready.\n");
596
597         return 0;
598 };
599
600 /**
601  *      i2o_block_release - Release the I2O block device
602  *      @inode: inode for block device being released
603  *      @file: file to close
604  *
605  *      Unlock and unmount the media, and power down the device. Gets called if
606  *      the block device is closed.
607  *
608  *      Returns 0 on success or negative error code on failure.
609  */
610 static int i2o_block_release(struct inode *inode, struct file *file)
611 {
612         struct gendisk *disk = inode->i_bdev->bd_disk;
613         struct i2o_block_device *dev = disk->private_data;
614         u8 operation;
615
616         /*
617          * This is to deail with the case of an application
618          * opening a device and then the device dissapears while
619          * it's in use, and then the application tries to release
620          * it.  ex: Unmounting a deleted RAID volume at reboot.
621          * If we send messages, it will just cause FAILs since
622          * the TID no longer exists.
623          */
624         if (!dev->i2o_dev)
625                 return 0;
626
627         i2o_block_device_flush(dev->i2o_dev);
628
629         i2o_block_device_unlock(dev->i2o_dev, -1);
630
631         if (dev->flags & (1 << 3 | 1 << 4))     /* Removable */
632                 operation = 0x21;
633         else
634                 operation = 0x24;
635
636         i2o_block_device_power(dev, operation);
637
638         return 0;
639 }
640
641 static int i2o_block_getgeo(struct block_device *bdev, struct hd_geometry *geo)
642 {
643         i2o_block_biosparam(get_capacity(bdev->bd_disk),
644                             &geo->cylinders, &geo->heads, &geo->sectors);
645         return 0;
646 }
647
648 /**
649  *      i2o_block_ioctl - Issue device specific ioctl calls.
650  *      @inode: inode for block device ioctl
651  *      @file: file for ioctl
652  *      @cmd: ioctl command
653  *      @arg: arg
654  *
655  *      Handles ioctl request for the block device.
656  *
657  *      Return 0 on success or negative error on failure.
658  */
659 static int i2o_block_ioctl(struct inode *inode, struct file *file,
660                            unsigned int cmd, unsigned long arg)
661 {
662         struct gendisk *disk = inode->i_bdev->bd_disk;
663         struct i2o_block_device *dev = disk->private_data;
664
665         /* Anyone capable of this syscall can do *real bad* things */
666
667         if (!capable(CAP_SYS_ADMIN))
668                 return -EPERM;
669
670         switch (cmd) {
671         case BLKI2OGRSTRAT:
672                 return put_user(dev->rcache, (int __user *)arg);
673         case BLKI2OGWSTRAT:
674                 return put_user(dev->wcache, (int __user *)arg);
675         case BLKI2OSRSTRAT:
676                 if (arg < 0 || arg > CACHE_SMARTFETCH)
677                         return -EINVAL;
678                 dev->rcache = arg;
679                 break;
680         case BLKI2OSWSTRAT:
681                 if (arg != 0
682                     && (arg < CACHE_WRITETHROUGH || arg > CACHE_SMARTBACK))
683                         return -EINVAL;
684                 dev->wcache = arg;
685                 break;
686         }
687         return -ENOTTY;
688 };
689
690 /**
691  *      i2o_block_media_changed - Have we seen a media change?
692  *      @disk: gendisk which should be verified
693  *
694  *      Verifies if the media has changed.
695  *
696  *      Returns 1 if the media was changed or 0 otherwise.
697  */
698 static int i2o_block_media_changed(struct gendisk *disk)
699 {
700         struct i2o_block_device *p = disk->private_data;
701
702         if (p->media_change_flag) {
703                 p->media_change_flag = 0;
704                 return 1;
705         }
706         return 0;
707 }
708
709 /**
710  *      i2o_block_transfer - Transfer a request to/from the I2O controller
711  *      @req: the request which should be transfered
712  *
713  *      This function converts the request into a I2O message. The necessary
714  *      DMA buffers are allocated and after everything is setup post the message
715  *      to the I2O controller. No cleanup is done by this function. It is done
716  *      on the interrupt side when the reply arrives.
717  *
718  *      Return 0 on success or negative error code on failure.
719  */
720 static int i2o_block_transfer(struct request *req)
721 {
722         struct i2o_block_device *dev = req->rq_disk->private_data;
723         struct i2o_controller *c;
724         u32 tid = dev->i2o_dev->lct_data.tid;
725         struct i2o_message *msg;
726         u32 *mptr;
727         struct i2o_block_request *ireq = req->special;
728         u32 tcntxt;
729         u32 sgl_offset = SGL_OFFSET_8;
730         u32 ctl_flags = 0x00000000;
731         int rc;
732         u32 cmd;
733
734         if (unlikely(!dev->i2o_dev)) {
735                 osm_err("transfer to removed drive\n");
736                 rc = -ENODEV;
737                 goto exit;
738         }
739
740         c = dev->i2o_dev->iop;
741
742         msg = i2o_msg_get(c);
743         if (IS_ERR(msg)) {
744                 rc = PTR_ERR(msg);
745                 goto exit;
746         }
747
748         tcntxt = i2o_cntxt_list_add(c, req);
749         if (!tcntxt) {
750                 rc = -ENOMEM;
751                 goto nop_msg;
752         }
753
754         msg->u.s.icntxt = cpu_to_le32(i2o_block_driver.context);
755         msg->u.s.tcntxt = cpu_to_le32(tcntxt);
756
757         mptr = &msg->body[0];
758
759         if (rq_data_dir(req) == READ) {
760                 cmd = I2O_CMD_BLOCK_READ << 24;
761
762                 switch (dev->rcache) {
763                 case CACHE_PREFETCH:
764                         ctl_flags = 0x201F0008;
765                         break;
766
767                 case CACHE_SMARTFETCH:
768                         if (req->nr_sectors > 16)
769                                 ctl_flags = 0x201F0008;
770                         else
771                                 ctl_flags = 0x001F0000;
772                         break;
773
774                 default:
775                         break;
776                 }
777         } else {
778                 cmd = I2O_CMD_BLOCK_WRITE << 24;
779
780                 switch (dev->wcache) {
781                 case CACHE_WRITETHROUGH:
782                         ctl_flags = 0x001F0008;
783                         break;
784                 case CACHE_WRITEBACK:
785                         ctl_flags = 0x001F0010;
786                         break;
787                 case CACHE_SMARTBACK:
788                         if (req->nr_sectors > 16)
789                                 ctl_flags = 0x001F0004;
790                         else
791                                 ctl_flags = 0x001F0010;
792                         break;
793                 case CACHE_SMARTTHROUGH:
794                         if (req->nr_sectors > 16)
795                                 ctl_flags = 0x001F0004;
796                         else
797                                 ctl_flags = 0x001F0010;
798                 default:
799                         break;
800                 }
801         }
802
803 #ifdef CONFIG_I2O_EXT_ADAPTEC
804         if (c->adaptec) {
805                 u8 cmd[10];
806                 u32 scsi_flags;
807                 u16 hwsec = queue_hardsect_size(req->q) >> KERNEL_SECTOR_SHIFT;
808
809                 memset(cmd, 0, 10);
810
811                 sgl_offset = SGL_OFFSET_12;
812
813                 msg->u.head[1] =
814                     cpu_to_le32(I2O_CMD_PRIVATE << 24 | HOST_TID << 12 | tid);
815
816                 *mptr++ = cpu_to_le32(I2O_VENDOR_DPT << 16 | I2O_CMD_SCSI_EXEC);
817                 *mptr++ = cpu_to_le32(tid);
818
819                 /*
820                  * ENABLE_DISCONNECT
821                  * SIMPLE_TAG
822                  * RETURN_SENSE_DATA_IN_REPLY_MESSAGE_FRAME
823                  */
824                 if (rq_data_dir(req) == READ) {
825                         cmd[0] = READ_10;
826                         scsi_flags = 0x60a0000a;
827                 } else {
828                         cmd[0] = WRITE_10;
829                         scsi_flags = 0xa0a0000a;
830                 }
831
832                 *mptr++ = cpu_to_le32(scsi_flags);
833
834                 *((u32 *) & cmd[2]) = cpu_to_be32(req->sector * hwsec);
835                 *((u16 *) & cmd[7]) = cpu_to_be16(req->nr_sectors * hwsec);
836
837                 memcpy(mptr, cmd, 10);
838                 mptr += 4;
839                 *mptr++ = cpu_to_le32(req->nr_sectors << KERNEL_SECTOR_SHIFT);
840         } else
841 #endif
842         {
843                 msg->u.head[1] = cpu_to_le32(cmd | HOST_TID << 12 | tid);
844                 *mptr++ = cpu_to_le32(ctl_flags);
845                 *mptr++ = cpu_to_le32(req->nr_sectors << KERNEL_SECTOR_SHIFT);
846                 *mptr++ =
847                     cpu_to_le32((u32) (req->sector << KERNEL_SECTOR_SHIFT));
848                 *mptr++ =
849                     cpu_to_le32(req->sector >> (32 - KERNEL_SECTOR_SHIFT));
850         }
851
852         if (!i2o_block_sglist_alloc(c, ireq, &mptr)) {
853                 rc = -ENOMEM;
854                 goto context_remove;
855         }
856
857         msg->u.head[0] =
858             cpu_to_le32(I2O_MESSAGE_SIZE(mptr - &msg->u.head[0]) | sgl_offset);
859
860         list_add_tail(&ireq->queue, &dev->open_queue);
861         dev->open_queue_depth++;
862
863         i2o_msg_post(c, msg);
864
865         return 0;
866
867       context_remove:
868         i2o_cntxt_list_remove(c, req);
869
870       nop_msg:
871         i2o_msg_nop(c, msg);
872
873       exit:
874         return rc;
875 };
876
877 /**
878  *      i2o_block_request_fn - request queue handling function
879  *      @q: request queue from which the request could be fetched
880  *
881  *      Takes the next request from the queue, transfers it and if no error
882  *      occurs dequeue it from the queue. On arrival of the reply the message
883  *      will be processed further. If an error occurs requeue the request.
884  */
885 static void i2o_block_request_fn(struct request_queue *q)
886 {
887         struct request *req;
888
889         while (!blk_queue_plugged(q)) {
890                 req = elv_next_request(q);
891                 if (!req)
892                         break;
893
894                 if (blk_fs_request(req)) {
895                         struct i2o_block_delayed_request *dreq;
896                         struct i2o_block_request *ireq = req->special;
897                         unsigned int queue_depth;
898
899                         queue_depth = ireq->i2o_blk_dev->open_queue_depth;
900
901                         if (queue_depth < I2O_BLOCK_MAX_OPEN_REQUESTS) {
902                                 if (!i2o_block_transfer(req)) {
903                                         blkdev_dequeue_request(req);
904                                         continue;
905                                 } else
906                                         osm_info("transfer error\n");
907                         }
908
909                         if (queue_depth)
910                                 break;
911
912                         /* stop the queue and retry later */
913                         dreq = kmalloc(sizeof(*dreq), GFP_ATOMIC);
914                         if (!dreq)
915                                 continue;
916
917                         dreq->queue = q;
918                         INIT_DELAYED_WORK(&dreq->work,
919                                           i2o_block_delayed_request_fn);
920
921                         if (!queue_delayed_work(i2o_block_driver.event_queue,
922                                                 &dreq->work,
923                                                 I2O_BLOCK_RETRY_TIME))
924                                 kfree(dreq);
925                         else {
926                                 blk_stop_queue(q);
927                                 break;
928                         }
929                 } else
930                         end_request(req, 0);
931         }
932 };
933
934 /* I2O Block device operations definition */
935 static struct block_device_operations i2o_block_fops = {
936         .owner = THIS_MODULE,
937         .open = i2o_block_open,
938         .release = i2o_block_release,
939         .ioctl = i2o_block_ioctl,
940         .getgeo = i2o_block_getgeo,
941         .media_changed = i2o_block_media_changed
942 };
943
944 /**
945  *      i2o_block_device_alloc - Allocate memory for a I2O Block device
946  *
947  *      Allocate memory for the i2o_block_device struct, gendisk and request
948  *      queue and initialize them as far as no additional information is needed.
949  *
950  *      Returns a pointer to the allocated I2O Block device on succes or a
951  *      negative error code on failure.
952  */
953 static struct i2o_block_device *i2o_block_device_alloc(void)
954 {
955         struct i2o_block_device *dev;
956         struct gendisk *gd;
957         struct request_queue *queue;
958         int rc;
959
960         dev = kzalloc(sizeof(*dev), GFP_KERNEL);
961         if (!dev) {
962                 osm_err("Insufficient memory to allocate I2O Block disk.\n");
963                 rc = -ENOMEM;
964                 goto exit;
965         }
966
967         INIT_LIST_HEAD(&dev->open_queue);
968         spin_lock_init(&dev->lock);
969         dev->rcache = CACHE_PREFETCH;
970         dev->wcache = CACHE_WRITEBACK;
971
972         /* allocate a gendisk with 16 partitions */
973         gd = alloc_disk(16);
974         if (!gd) {
975                 osm_err("Insufficient memory to allocate gendisk.\n");
976                 rc = -ENOMEM;
977                 goto cleanup_dev;
978         }
979
980         /* initialize the request queue */
981         queue = blk_init_queue(i2o_block_request_fn, &dev->lock);
982         if (!queue) {
983                 osm_err("Insufficient memory to allocate request queue.\n");
984                 rc = -ENOMEM;
985                 goto cleanup_queue;
986         }
987
988         blk_queue_prep_rq(queue, i2o_block_prep_req_fn);
989
990         gd->major = I2O_MAJOR;
991         gd->queue = queue;
992         gd->fops = &i2o_block_fops;
993         gd->private_data = dev;
994
995         dev->gd = gd;
996
997         return dev;
998
999       cleanup_queue:
1000         put_disk(gd);
1001
1002       cleanup_dev:
1003         kfree(dev);
1004
1005       exit:
1006         return ERR_PTR(rc);
1007 };
1008
1009 /**
1010  *      i2o_block_probe - verify if dev is a I2O Block device and install it
1011  *      @dev: device to verify if it is a I2O Block device
1012  *
1013  *      We only verify if the user_tid of the device is 0xfff and then install
1014  *      the device. Otherwise it is used by some other device (e. g. RAID).
1015  *
1016  *      Returns 0 on success or negative error code on failure.
1017  */
1018 static int i2o_block_probe(struct device *dev)
1019 {
1020         struct i2o_device *i2o_dev = to_i2o_device(dev);
1021         struct i2o_controller *c = i2o_dev->iop;
1022         struct i2o_block_device *i2o_blk_dev;
1023         struct gendisk *gd;
1024         struct request_queue *queue;
1025         static int unit = 0;
1026         int rc;
1027         u64 size;
1028         u32 blocksize;
1029         u16 body_size = 4;
1030         u16 power;
1031         unsigned short max_sectors;
1032
1033 #ifdef CONFIG_I2O_EXT_ADAPTEC
1034         if (c->adaptec)
1035                 body_size = 8;
1036 #endif
1037
1038         if (c->limit_sectors)
1039                 max_sectors = I2O_MAX_SECTORS_LIMITED;
1040         else
1041                 max_sectors = I2O_MAX_SECTORS;
1042
1043         /* skip devices which are used by IOP */
1044         if (i2o_dev->lct_data.user_tid != 0xfff) {
1045                 osm_debug("skipping used device %03x\n", i2o_dev->lct_data.tid);
1046                 return -ENODEV;
1047         }
1048
1049         if (i2o_device_claim(i2o_dev)) {
1050                 osm_warn("Unable to claim device. Installation aborted\n");
1051                 rc = -EFAULT;
1052                 goto exit;
1053         }
1054
1055         i2o_blk_dev = i2o_block_device_alloc();
1056         if (IS_ERR(i2o_blk_dev)) {
1057                 osm_err("could not alloc a new I2O block device");
1058                 rc = PTR_ERR(i2o_blk_dev);
1059                 goto claim_release;
1060         }
1061
1062         i2o_blk_dev->i2o_dev = i2o_dev;
1063         dev_set_drvdata(dev, i2o_blk_dev);
1064
1065         /* setup gendisk */
1066         gd = i2o_blk_dev->gd;
1067         gd->first_minor = unit << 4;
1068         sprintf(gd->disk_name, "i2o/hd%c", 'a' + unit);
1069         gd->driverfs_dev = &i2o_dev->device;
1070
1071         /* setup request queue */
1072         queue = gd->queue;
1073         queue->queuedata = i2o_blk_dev;
1074
1075         blk_queue_max_phys_segments(queue, I2O_MAX_PHYS_SEGMENTS);
1076         blk_queue_max_sectors(queue, max_sectors);
1077         blk_queue_max_hw_segments(queue, i2o_sg_tablesize(c, body_size));
1078
1079         osm_debug("max sectors = %d\n", queue->max_phys_segments);
1080         osm_debug("phys segments = %d\n", queue->max_sectors);
1081         osm_debug("max hw segments = %d\n", queue->max_hw_segments);
1082
1083         /*
1084          *      Ask for the current media data. If that isn't supported
1085          *      then we ask for the device capacity data
1086          */
1087         if (!i2o_parm_field_get(i2o_dev, 0x0004, 1, &blocksize, 4) ||
1088             !i2o_parm_field_get(i2o_dev, 0x0000, 3, &blocksize, 4)) {
1089                 blk_queue_hardsect_size(queue, le32_to_cpu(blocksize));
1090         } else
1091                 osm_warn("unable to get blocksize of %s\n", gd->disk_name);
1092
1093         if (!i2o_parm_field_get(i2o_dev, 0x0004, 0, &size, 8) ||
1094             !i2o_parm_field_get(i2o_dev, 0x0000, 4, &size, 8)) {
1095                 set_capacity(gd, le64_to_cpu(size) >> KERNEL_SECTOR_SHIFT);
1096         } else
1097                 osm_warn("could not get size of %s\n", gd->disk_name);
1098
1099         if (!i2o_parm_field_get(i2o_dev, 0x0000, 2, &power, 2))
1100                 i2o_blk_dev->power = power;
1101
1102         i2o_event_register(i2o_dev, &i2o_block_driver, 0, 0xffffffff);
1103
1104         add_disk(gd);
1105
1106         unit++;
1107
1108         osm_info("device added (TID: %03x): %s\n", i2o_dev->lct_data.tid,
1109                  i2o_blk_dev->gd->disk_name);
1110
1111         return 0;
1112
1113       claim_release:
1114         i2o_device_claim_release(i2o_dev);
1115
1116       exit:
1117         return rc;
1118 };
1119
1120 /* Block OSM driver struct */
1121 static struct i2o_driver i2o_block_driver = {
1122         .name = OSM_NAME,
1123         .event = i2o_block_event,
1124         .reply = i2o_block_reply,
1125         .classes = i2o_block_class_id,
1126         .driver = {
1127                    .probe = i2o_block_probe,
1128                    .remove = i2o_block_remove,
1129                    },
1130 };
1131
1132 /**
1133  *      i2o_block_init - Block OSM initialization function
1134  *
1135  *      Allocate the slab and mempool for request structs, registers i2o_block
1136  *      block device and finally register the Block OSM in the I2O core.
1137  *
1138  *      Returns 0 on success or negative error code on failure.
1139  */
1140 static int __init i2o_block_init(void)
1141 {
1142         int rc;
1143         int size;
1144
1145         printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
1146
1147         /* Allocate request mempool and slab */
1148         size = sizeof(struct i2o_block_request);
1149         i2o_blk_req_pool.slab = kmem_cache_create("i2o_block_req", size, 0,
1150                                                   SLAB_HWCACHE_ALIGN, NULL);
1151         if (!i2o_blk_req_pool.slab) {
1152                 osm_err("can't init request slab\n");
1153                 rc = -ENOMEM;
1154                 goto exit;
1155         }
1156
1157         i2o_blk_req_pool.pool =
1158                 mempool_create_slab_pool(I2O_BLOCK_REQ_MEMPOOL_SIZE,
1159                                          i2o_blk_req_pool.slab);
1160         if (!i2o_blk_req_pool.pool) {
1161                 osm_err("can't init request mempool\n");
1162                 rc = -ENOMEM;
1163                 goto free_slab;
1164         }
1165
1166         /* Register the block device interfaces */
1167         rc = register_blkdev(I2O_MAJOR, "i2o_block");
1168         if (rc) {
1169                 osm_err("unable to register block device\n");
1170                 goto free_mempool;
1171         }
1172 #ifdef MODULE
1173         osm_info("registered device at major %d\n", I2O_MAJOR);
1174 #endif
1175
1176         /* Register Block OSM into I2O core */
1177         rc = i2o_driver_register(&i2o_block_driver);
1178         if (rc) {
1179                 osm_err("Could not register Block driver\n");
1180                 goto unregister_blkdev;
1181         }
1182
1183         return 0;
1184
1185       unregister_blkdev:
1186         unregister_blkdev(I2O_MAJOR, "i2o_block");
1187
1188       free_mempool:
1189         mempool_destroy(i2o_blk_req_pool.pool);
1190
1191       free_slab:
1192         kmem_cache_destroy(i2o_blk_req_pool.slab);
1193
1194       exit:
1195         return rc;
1196 };
1197
1198 /**
1199  *      i2o_block_exit - Block OSM exit function
1200  *
1201  *      Unregisters Block OSM from I2O core, unregisters i2o_block block device
1202  *      and frees the mempool and slab.
1203  */
1204 static void __exit i2o_block_exit(void)
1205 {
1206         /* Unregister I2O Block OSM from I2O core */
1207         i2o_driver_unregister(&i2o_block_driver);
1208
1209         /* Unregister block device */
1210         unregister_blkdev(I2O_MAJOR, "i2o_block");
1211
1212         /* Free request mempool and slab */
1213         mempool_destroy(i2o_blk_req_pool.pool);
1214         kmem_cache_destroy(i2o_blk_req_pool.slab);
1215 };
1216
1217 MODULE_AUTHOR("Red Hat");
1218 MODULE_LICENSE("GPL");
1219 MODULE_DESCRIPTION(OSM_DESCRIPTION);
1220 MODULE_VERSION(OSM_VERSION);
1221
1222 module_init(i2o_block_init);
1223 module_exit(i2o_block_exit);