4 * Copyright (C) 1999-2002 Red Hat Software
6 * Written by Alan Cox, Building Number Three Ltd
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.
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.
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
25 * Multiple device handling error fixes,
26 * Added a queue depth.
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
36 * Independent queues per IOP
37 * Support for dynamic device creation/deletion
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
49 * Markus Lidel <Markus.Lidel@shadowconnect.com>:
50 * Minor bugfixes for 2.6.
53 #include <linux/module.h>
54 #include <linux/i2o.h>
56 #include <linux/mempool.h>
58 #include <linux/genhd.h>
59 #include <linux/blkdev.h>
60 #include <linux/hdreg.h>
62 #include "i2o_block.h"
64 #define OSM_NAME "block-osm"
65 #define OSM_VERSION "$Rev$"
66 #define OSM_DESCRIPTION "I2O Block Device OSM"
68 static struct i2o_driver i2o_block_driver;
70 /* global Block OSM request mempool */
71 static struct i2o_block_mempool i2o_blk_req_pool;
73 /* Block OSM class handling definition */
74 static struct i2o_class_id i2o_block_class_id[] = {
75 {I2O_CLASS_RANDOM_BLOCK_STORAGE},
80 * i2o_block_device_free - free the memory of the I2O Block device
81 * @dev: I2O Block device, which should be cleaned up
83 * Frees the request queue, gendisk and the i2o_block_device structure.
85 static void i2o_block_device_free(struct i2o_block_device *dev)
87 blk_cleanup_queue(dev->gd->queue);
95 * i2o_block_remove - remove the I2O Block device from the system again
96 * @dev: I2O Block device which should be removed
98 * Remove gendisk from system and free all allocated memory.
102 static int i2o_block_remove(struct device *dev)
104 struct i2o_device *i2o_dev = to_i2o_device(dev);
105 struct i2o_block_device *i2o_blk_dev = dev_get_drvdata(dev);
107 osm_info("device removed (TID: %03x): %s\n", i2o_dev->lct_data.tid,
108 i2o_blk_dev->gd->disk_name);
110 i2o_event_register(i2o_dev, &i2o_block_driver, 0, 0);
112 del_gendisk(i2o_blk_dev->gd);
114 dev_set_drvdata(dev, NULL);
116 i2o_device_claim_release(i2o_dev);
118 i2o_block_device_free(i2o_blk_dev);
124 * i2o_block_device flush - Flush all dirty data of I2O device dev
125 * @dev: I2O device which should be flushed
127 * Flushes all dirty data on device dev.
129 * Returns 0 on success or negative error code on failure.
131 static int i2o_block_device_flush(struct i2o_device *dev)
133 struct i2o_message __iomem *msg;
136 m = i2o_msg_get_wait(dev->iop, &msg, I2O_TIMEOUT_MESSAGE_GET);
137 if (m == I2O_QUEUE_EMPTY)
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,
143 writel(60 << 16, &msg->body[0]);
144 osm_debug("Flushing...\n");
146 return i2o_msg_post_wait(dev->iop, m, 60);
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
154 * Load a media into drive. Identifier should be set to -1, because the
155 * spec does not support any other value.
157 * Returns 0 on success or negative error code on failure.
159 static int i2o_block_device_mount(struct i2o_device *dev, u32 media_id)
161 struct i2o_message __iomem *msg;
164 m = i2o_msg_get_wait(dev->iop, &msg, I2O_TIMEOUT_MESSAGE_GET);
165 if (m == I2O_QUEUE_EMPTY)
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,
171 writel(-1, &msg->body[0]);
172 writel(0, &msg->body[1]);
173 osm_debug("Mounting...\n");
175 return i2o_msg_post_wait(dev->iop, m, 2);
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
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.
186 * Returns 0 on success or negative error code on failure.
188 static int i2o_block_device_lock(struct i2o_device *dev, u32 media_id)
190 struct i2o_message __iomem *msg;
193 m = i2o_msg_get_wait(dev->iop, &msg, I2O_TIMEOUT_MESSAGE_GET);
194 if (m == I2O_QUEUE_EMPTY)
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,
200 writel(-1, &msg->body[0]);
201 osm_debug("Locking...\n");
203 return i2o_msg_post_wait(dev->iop, m, 2);
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
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.
214 * Returns 0 on success or negative error code on failure.
216 static int i2o_block_device_unlock(struct i2o_device *dev, u32 media_id)
218 struct i2o_message __iomem *msg;
221 m = i2o_msg_get_wait(dev->iop, &msg, I2O_TIMEOUT_MESSAGE_GET);
222 if (m == I2O_QUEUE_EMPTY)
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,
228 writel(media_id, &msg->body[0]);
229 osm_debug("Unlocking...\n");
231 return i2o_msg_post_wait(dev->iop, m, 2);
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
239 * Send a power management request to the device dev.
241 * Returns 0 on success or negative error code on failure.
243 static int i2o_block_device_power(struct i2o_block_device *dev, u8 op)
245 struct i2o_device *i2o_dev = dev->i2o_dev;
246 struct i2o_controller *c = i2o_dev->iop;
247 struct i2o_message __iomem *msg;
251 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
252 if (m == I2O_QUEUE_EMPTY)
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");
261 rc = i2o_msg_post_wait(c, m, 60);
269 * i2o_block_request_alloc - Allocate an I2O block request struct
271 * Allocates an I2O block request struct and initialize the list.
273 * Returns a i2o_block_request pointer on success or negative error code
276 static inline struct i2o_block_request *i2o_block_request_alloc(void)
278 struct i2o_block_request *ireq;
280 ireq = mempool_alloc(i2o_blk_req_pool.pool, GFP_ATOMIC);
282 return ERR_PTR(-ENOMEM);
284 INIT_LIST_HEAD(&ireq->queue);
290 * i2o_block_request_free - Frees a I2O block request
291 * @ireq: I2O block request which should be freed
293 * Fres the allocated memory (give it back to the request mempool).
295 static inline void i2o_block_request_free(struct i2o_block_request *ireq)
297 mempool_free(ireq, i2o_blk_req_pool.pool);
301 * i2o_block_sglist_alloc - Allocate the SG list and map it
302 * @ireq: I2O block request
304 * Builds the SG list and map it into to be accessable by the controller.
306 * Returns the number of elements in the SG list or 0 on failure.
308 static inline int i2o_block_sglist_alloc(struct i2o_block_request *ireq)
310 struct device *dev = &ireq->i2o_blk_dev->i2o_dev->iop->pdev->dev;
313 nents = blk_rq_map_sg(ireq->req->q, ireq->req, ireq->sg_table);
315 if (rq_data_dir(ireq->req) == READ)
316 ireq->sg_dma_direction = PCI_DMA_FROMDEVICE;
318 ireq->sg_dma_direction = PCI_DMA_TODEVICE;
320 ireq->sg_nents = dma_map_sg(dev, ireq->sg_table, nents,
321 ireq->sg_dma_direction);
323 return ireq->sg_nents;
327 * i2o_block_sglist_free - Frees the SG list
328 * @ireq: I2O block request from which the SG should be freed
330 * Frees the SG list from the I2O block request.
332 static inline void i2o_block_sglist_free(struct i2o_block_request *ireq)
334 struct device *dev = &ireq->i2o_blk_dev->i2o_dev->iop->pdev->dev;
336 dma_unmap_sg(dev, ireq->sg_table, ireq->sg_nents,
337 ireq->sg_dma_direction);
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
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.
348 * Returns BLKPREP_OK on success or BLKPREP_DEFER on failure.
350 static int i2o_block_prep_req_fn(struct request_queue *q, struct request *req)
352 struct i2o_block_device *i2o_blk_dev = q->queuedata;
353 struct i2o_block_request *ireq;
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;
362 /* connect the i2o_block_request to the request */
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;
370 ireq->i2o_blk_dev = i2o_blk_dev;
376 /* do not come back here */
377 req->flags |= REQ_DONTPREP | REQ_SPECIAL;
383 * i2o_block_delayed_request_fn - delayed request queue function
384 * delayed_request: the delayed request with the queue to start
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
391 static void i2o_block_delayed_request_fn(void *delayed_request)
393 struct i2o_block_delayed_request *dreq = delayed_request;
394 struct request_queue *q = dreq->queue;
397 spin_lock_irqsave(q->queue_lock, flags);
399 spin_unlock_irqrestore(q->queue_lock, flags);
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
409 * Mark the request as complete. The lock must not be held when entering.
412 static void i2o_block_end_request(struct request *req, int uptodate,
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;
420 if (end_that_request_chunk(req, uptodate, nr_bytes)) {
421 int leftover = (req->hard_nr_sectors << 9);
423 if (blk_pc_request(req))
424 leftover = req->data_len;
426 if (end_io_error(uptodate))
427 end_that_request_chunk(req, 0, leftover);
430 add_disk_randomness(req->rq_disk);
432 spin_lock_irqsave(q->queue_lock, flags);
434 end_that_request_last(req);
435 dev->open_queue_depth--;
436 list_del(&ireq->queue);
440 spin_unlock_irqrestore(q->queue_lock, flags);
442 i2o_block_sglist_free(ireq);
443 i2o_block_request_free(ireq);
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
452 * This function gets all the message replies.
455 static int i2o_block_reply(struct i2o_controller *c, u32 m,
456 struct i2o_message *msg)
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");
468 * Lets see what is cooking. We stuffed the
469 * request in the context.
472 if ((le32_to_cpu(msg->body[0]) >> 24) != 0) {
473 u32 status = le32_to_cpu(msg->body[0]);
475 * Device not ready means two things. One is that the
476 * the thing went offline (but not a removal media)
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.
483 * Don't stick a supertrak100 into cache aggressive modes
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);
495 i2o_block_end_request(req, uptodate, le32_to_cpu(msg->body[1]));
500 static void i2o_block_event(struct i2o_event *evt)
502 osm_info("event received\n");
507 * SCSI-CAM for ioctl geometry mapping
508 * Duplicated with SCSI - this should be moved into somewhere common
511 * LBA -> CHS mapping table taken from:
513 * "Incorporating the I2O Architecture into BIOS for Intel Architecture
516 * This is an I2O document that is only available to I2O members,
519 * From my understanding, this is how all the I2O cards do this
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)
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
535 static void i2o_block_biosparam(unsigned long capacity, unsigned short *cyls,
536 unsigned char *hds, unsigned char *secs)
538 unsigned long heads, sectors, cylinders;
540 sectors = 63L; /* Maximize sectors per track */
541 if (capacity <= BLOCK_SIZE_528M)
543 else if (capacity <= BLOCK_SIZE_1G)
545 else if (capacity <= BLOCK_SIZE_21G)
547 else if (capacity <= BLOCK_SIZE_42G)
552 cylinders = (unsigned long)capacity / (heads * sectors);
554 *cyls = (unsigned short)cylinders; /* Stuff return values */
555 *secs = (unsigned char)sectors;
556 *hds = (unsigned char)heads;
560 * i2o_block_open - Open the block device
562 * Power up the device, mount and lock the media. This function is called,
563 * if the block device is opened for access.
565 * Returns 0 on success or negative error code on failure.
567 static int i2o_block_open(struct inode *inode, struct file *file)
569 struct i2o_block_device *dev = inode->i_bdev->bd_disk->private_data;
574 if (dev->power > 0x1f)
575 i2o_block_device_power(dev, 0x02);
577 i2o_block_device_mount(dev->i2o_dev, -1);
579 i2o_block_device_lock(dev->i2o_dev, -1);
581 osm_debug("Ready.\n");
587 * i2o_block_release - Release the I2O block device
589 * Unlock and unmount the media, and power down the device. Gets called if
590 * the block device is closed.
592 * Returns 0 on success or negative error code on failure.
594 static int i2o_block_release(struct inode *inode, struct file *file)
596 struct gendisk *disk = inode->i_bdev->bd_disk;
597 struct i2o_block_device *dev = disk->private_data;
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.
611 i2o_block_device_flush(dev->i2o_dev);
613 i2o_block_device_unlock(dev->i2o_dev, -1);
615 if (dev->flags & (1 << 3 | 1 << 4)) /* Removable */
620 i2o_block_device_power(dev, operation);
626 * i2o_block_ioctl - Issue device specific ioctl calls.
627 * @cmd: ioctl command
630 * Handles ioctl request for the block device.
632 * Return 0 on success or negative error on failure.
634 static int i2o_block_ioctl(struct inode *inode, struct file *file,
635 unsigned int cmd, unsigned long arg)
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;
641 /* Anyone capable of this syscall can do *real bad* things */
643 if (!capable(CAP_SYS_ADMIN))
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;
657 return put_user(dev->rcache, (int __user *)arg);
659 return put_user(dev->wcache, (int __user *)arg);
661 if (arg < 0 || arg > CACHE_SMARTFETCH)
667 && (arg < CACHE_WRITETHROUGH || arg > CACHE_SMARTBACK))
676 * i2o_block_media_changed - Have we seen a media change?
677 * @disk: gendisk which should be verified
679 * Verifies if the media has changed.
681 * Returns 1 if the media was changed or 0 otherwise.
683 static int i2o_block_media_changed(struct gendisk *disk)
685 struct i2o_block_device *p = disk->private_data;
687 if (p->media_change_flag) {
688 p->media_change_flag = 0;
695 * i2o_block_transfer - Transfer a request to/from the I2O controller
696 * @req: the request which should be transfered
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.
703 * Return 0 on success or negative error code on failure.
705 static int i2o_block_transfer(struct request *req)
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;
712 struct i2o_block_request *ireq = req->special;
713 struct scatterlist *sg;
721 m = i2o_msg_get(c, &msg);
722 if (m == I2O_QUEUE_EMPTY) {
727 tcntxt = i2o_cntxt_list_add(c, req);
733 if ((sgnum = i2o_block_sglist_alloc(ireq)) <= 0) {
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]);
743 writel((((u64) req->sector) << 9) & 0xffffffff, &msg->body[2]);
744 writel(req->sector >> 23, &msg->body[3]);
746 mptr = &msg->body[4];
750 if (rq_data_dir(req) == READ) {
751 writel(I2O_CMD_BLOCK_READ << 24 | HOST_TID << 12 | tid,
753 sg_flags = 0x10000000;
754 switch (dev->rcache) {
756 writel(0, &msg->body[0]);
759 writel(0x201F0008, &msg->body[0]);
761 case CACHE_SMARTFETCH:
762 if (req->nr_sectors > 16)
763 writel(0x201F0008, &msg->body[0]);
765 writel(0x001F0000, &msg->body[0]);
769 writel(I2O_CMD_BLOCK_WRITE << 24 | HOST_TID << 12 | tid,
771 sg_flags = 0x14000000;
772 switch (dev->wcache) {
774 writel(0, &msg->body[0]);
776 case CACHE_WRITETHROUGH:
777 writel(0x001F0008, &msg->body[0]);
779 case CACHE_WRITEBACK:
780 writel(0x001F0010, &msg->body[0]);
782 case CACHE_SMARTBACK:
783 if (req->nr_sectors > 16)
784 writel(0x001F0004, &msg->body[0]);
786 writel(0x001F0010, &msg->body[0]);
788 case CACHE_SMARTTHROUGH:
789 if (req->nr_sectors > 16)
790 writel(0x001F0004, &msg->body[0]);
792 writel(0x001F0010, &msg->body[0]);
796 for (i = sgnum; i > 0; i--) {
798 sg_flags |= 0x80000000;
799 writel(sg_flags | sg_dma_len(sg), mptr);
800 writel(sg_dma_address(sg), mptr + 4);
805 writel(I2O_MESSAGE_SIZE(mptr - &msg->u.head[0]) | SGL_OFFSET_8,
808 list_add_tail(&ireq->queue, &dev->open_queue);
809 dev->open_queue_depth++;
816 i2o_cntxt_list_remove(c, req);
826 * i2o_block_request_fn - request queue handling function
827 * q: request queue from which the request could be fetched
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.
833 static void i2o_block_request_fn(struct request_queue *q)
837 while (!blk_queue_plugged(q)) {
838 req = elv_next_request(q);
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;
847 queue_depth = ireq->i2o_blk_dev->open_queue_depth;
849 if (queue_depth < I2O_BLOCK_MAX_OPEN_REQUESTS)
850 if (!i2o_block_transfer(req)) {
851 blkdev_dequeue_request(req);
858 /* stop the queue and retry later */
859 dreq = kmalloc(sizeof(*dreq), GFP_ATOMIC);
864 INIT_WORK(&dreq->work, i2o_block_delayed_request_fn,
867 osm_info("transfer error\n");
868 if (!queue_delayed_work(i2o_block_driver.event_queue,
870 I2O_BLOCK_RETRY_TIME))
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
891 * i2o_block_device_alloc - Allocate memory for a I2O Block device
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.
896 * Returns a pointer to the allocated I2O Block device on succes or a
897 * negative error code on failure.
899 static struct i2o_block_device *i2o_block_device_alloc(void)
901 struct i2o_block_device *dev;
903 struct request_queue *queue;
906 dev = kmalloc(sizeof(*dev), GFP_KERNEL);
908 osm_err("Insufficient memory to allocate I2O Block disk.\n");
912 memset(dev, 0, sizeof(*dev));
914 INIT_LIST_HEAD(&dev->open_queue);
915 spin_lock_init(&dev->lock);
916 dev->rcache = CACHE_PREFETCH;
917 dev->wcache = CACHE_WRITEBACK;
919 /* allocate a gendisk with 16 partitions */
922 osm_err("Insufficient memory to allocate gendisk.\n");
927 /* initialize the request queue */
928 queue = blk_init_queue(i2o_block_request_fn, &dev->lock);
930 osm_err("Insufficient memory to allocate request queue.\n");
935 blk_queue_prep_rq(queue, i2o_block_prep_req_fn);
937 gd->major = I2O_MAJOR;
939 gd->fops = &i2o_block_fops;
940 gd->private_data = dev;
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
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).
963 * Returns 0 on success or negative error code on failure.
965 static int i2o_block_probe(struct device *dev)
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;
971 struct request_queue *queue;
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);
985 if (i2o_device_claim(i2o_dev)) {
986 osm_warn("Unable to claim device. Installation aborted\n");
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);
998 i2o_blk_dev->i2o_dev = i2o_dev;
999 dev_set_drvdata(dev, i2o_blk_dev);
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;
1008 /* setup request queue */
1010 queue->queuedata = i2o_blk_dev;
1012 blk_queue_max_phys_segments(queue, I2O_MAX_SEGMENTS);
1013 blk_queue_max_sectors(queue, I2O_MAX_SECTORS);
1018 i2o_status_block *sb;
1020 sb = c->status_block.virt;
1022 segments = (sb->inbound_frame_size -
1023 sizeof(struct i2o_message) / 4 - 4) / 2;
1026 blk_queue_max_hw_segments(queue, segments);
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);
1033 * Ask for the current media data. If that isn't supported
1034 * then we ask for the device capacity data
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);
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",
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);
1054 set_capacity(gd, size >> 9);
1056 i2o_event_register(i2o_dev, &i2o_block_driver, 0, 0xffffffff);
1062 osm_info("device added (TID: %03x): %s\n", i2o_dev->lct_data.tid,
1063 i2o_blk_dev->gd->disk_name);
1068 i2o_device_claim_release(i2o_dev);
1074 /* Block OSM driver struct */
1075 static struct i2o_driver i2o_block_driver = {
1077 .event = i2o_block_event,
1078 .reply = i2o_block_reply,
1079 .classes = i2o_block_class_id,
1081 .probe = i2o_block_probe,
1082 .remove = i2o_block_remove,
1087 * i2o_block_init - Block OSM initialization function
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.
1092 * Returns 0 on success or negative error code on failure.
1094 static int __init i2o_block_init(void)
1099 printk(KERN_INFO OSM_DESCRIPTION " v" OSM_VERSION "\n");
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,
1106 if (!i2o_blk_req_pool.slab) {
1107 osm_err("can't init request slab\n");
1112 i2o_blk_req_pool.pool = mempool_create(I2O_REQ_MEMPOOL_SIZE,
1115 i2o_blk_req_pool.slab);
1116 if (!i2o_blk_req_pool.pool) {
1117 osm_err("can't init request mempool\n");
1122 /* Register the block device interfaces */
1123 rc = register_blkdev(I2O_MAJOR, "i2o_block");
1125 osm_err("unable to register block device\n");
1129 osm_info("registered device at major %d\n", I2O_MAJOR);
1132 /* Register Block OSM into I2O core */
1133 rc = i2o_driver_register(&i2o_block_driver);
1135 osm_err("Could not register Block driver\n");
1136 goto unregister_blkdev;
1142 unregister_blkdev(I2O_MAJOR, "i2o_block");
1145 mempool_destroy(i2o_blk_req_pool.pool);
1148 kmem_cache_destroy(i2o_blk_req_pool.slab);
1155 * i2o_block_exit - Block OSM exit function
1157 * Unregisters Block OSM from I2O core, unregisters i2o_block block device
1158 * and frees the mempool and slab.
1160 static void __exit i2o_block_exit(void)
1162 /* Unregister I2O Block OSM from I2O core */
1163 i2o_driver_unregister(&i2o_block_driver);
1165 /* Unregister block device */
1166 unregister_blkdev(I2O_MAJOR, "i2o_block");
1168 /* Free request mempool and slab */
1169 mempool_destroy(i2o_blk_req_pool.pool);
1170 kmem_cache_destroy(i2o_blk_req_pool.slab);
1173 MODULE_AUTHOR("Red Hat");
1174 MODULE_LICENSE("GPL");
1175 MODULE_DESCRIPTION(OSM_DESCRIPTION);
1176 MODULE_VERSION(OSM_VERSION);
1178 module_init(i2o_block_init);
1179 module_exit(i2o_block_exit);