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