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 %s\n", i2o_blk_dev
->gd
->disk_name
);
109 i2o_event_register(i2o_dev
, &i2o_block_driver
, 0, 0);
111 del_gendisk(i2o_blk_dev
->gd
);
113 dev_set_drvdata(dev
, NULL
);
115 i2o_device_claim_release(i2o_dev
);
117 i2o_block_device_free(i2o_blk_dev
);
123 * i2o_block_device flush - Flush all dirty data of I2O device dev
124 * @dev: I2O device which should be flushed
126 * Flushes all dirty data on device dev.
128 * Returns 0 on success or negative error code on failure.
130 static int i2o_block_device_flush(struct i2o_device
*dev
)
132 struct i2o_message __iomem
*msg
;
135 m
= i2o_msg_get_wait(dev
->iop
, &msg
, I2O_TIMEOUT_MESSAGE_GET
);
136 if (m
== I2O_QUEUE_EMPTY
)
139 writel(FIVE_WORD_MSG_SIZE
| SGL_OFFSET_0
, &msg
->u
.head
[0]);
140 writel(I2O_CMD_BLOCK_CFLUSH
<< 24 | HOST_TID
<< 12 | dev
->lct_data
.tid
,
142 writel(60 << 16, &msg
->body
[0]);
143 osm_debug("Flushing...\n");
145 return i2o_msg_post_wait(dev
->iop
, m
, 60);
149 * i2o_block_device_mount - Mount (load) the media of device dev
150 * @dev: I2O device which should receive the mount request
151 * @media_id: Media Identifier
153 * Load a media into drive. Identifier should be set to -1, because the
154 * spec does not support any other value.
156 * Returns 0 on success or negative error code on failure.
158 static int i2o_block_device_mount(struct i2o_device
*dev
, u32 media_id
)
160 struct i2o_message __iomem
*msg
;
163 m
= i2o_msg_get_wait(dev
->iop
, &msg
, I2O_TIMEOUT_MESSAGE_GET
);
164 if (m
== I2O_QUEUE_EMPTY
)
167 writel(FIVE_WORD_MSG_SIZE
| SGL_OFFSET_0
, &msg
->u
.head
[0]);
168 writel(I2O_CMD_BLOCK_MMOUNT
<< 24 | HOST_TID
<< 12 | dev
->lct_data
.tid
,
170 writel(-1, &msg
->body
[0]);
171 writel(0, &msg
->body
[1]);
172 osm_debug("Mounting...\n");
174 return i2o_msg_post_wait(dev
->iop
, m
, 2);
178 * i2o_block_device_lock - Locks the media of device dev
179 * @dev: I2O device which should receive the lock request
180 * @media_id: Media Identifier
182 * Lock media of device dev to prevent removal. The media identifier
183 * should be set to -1, because the spec does not support any other value.
185 * Returns 0 on success or negative error code on failure.
187 static int i2o_block_device_lock(struct i2o_device
*dev
, u32 media_id
)
189 struct i2o_message __iomem
*msg
;
192 m
= i2o_msg_get_wait(dev
->iop
, &msg
, I2O_TIMEOUT_MESSAGE_GET
);
193 if (m
== I2O_QUEUE_EMPTY
)
196 writel(FIVE_WORD_MSG_SIZE
| SGL_OFFSET_0
, &msg
->u
.head
[0]);
197 writel(I2O_CMD_BLOCK_MLOCK
<< 24 | HOST_TID
<< 12 | dev
->lct_data
.tid
,
199 writel(-1, &msg
->body
[0]);
200 osm_debug("Locking...\n");
202 return i2o_msg_post_wait(dev
->iop
, m
, 2);
206 * i2o_block_device_unlock - Unlocks the media of device dev
207 * @dev: I2O device which should receive the unlocked request
208 * @media_id: Media Identifier
210 * Unlocks the media in device dev. The media identifier should be set to
211 * -1, because the spec does not support any other value.
213 * Returns 0 on success or negative error code on failure.
215 static int i2o_block_device_unlock(struct i2o_device
*dev
, u32 media_id
)
217 struct i2o_message __iomem
*msg
;
220 m
= i2o_msg_get_wait(dev
->iop
, &msg
, I2O_TIMEOUT_MESSAGE_GET
);
221 if (m
== I2O_QUEUE_EMPTY
)
224 writel(FIVE_WORD_MSG_SIZE
| SGL_OFFSET_0
, &msg
->u
.head
[0]);
225 writel(I2O_CMD_BLOCK_MUNLOCK
<< 24 | HOST_TID
<< 12 | dev
->lct_data
.tid
,
227 writel(media_id
, &msg
->body
[0]);
228 osm_debug("Unlocking...\n");
230 return i2o_msg_post_wait(dev
->iop
, m
, 2);
234 * i2o_block_device_power - Power management for device dev
235 * @dev: I2O device which should receive the power management request
236 * @operation: Operation which should be send
238 * Send a power management request to the device dev.
240 * Returns 0 on success or negative error code on failure.
242 static int i2o_block_device_power(struct i2o_block_device
*dev
, u8 op
)
244 struct i2o_device
*i2o_dev
= dev
->i2o_dev
;
245 struct i2o_controller
*c
= i2o_dev
->iop
;
246 struct i2o_message __iomem
*msg
;
250 m
= i2o_msg_get_wait(c
, &msg
, I2O_TIMEOUT_MESSAGE_GET
);
251 if (m
== I2O_QUEUE_EMPTY
)
254 writel(FOUR_WORD_MSG_SIZE
| SGL_OFFSET_0
, &msg
->u
.head
[0]);
255 writel(I2O_CMD_BLOCK_POWER
<< 24 | HOST_TID
<< 12 | i2o_dev
->lct_data
.
256 tid
, &msg
->u
.head
[1]);
257 writel(op
<< 24, &msg
->body
[0]);
258 osm_debug("Power...\n");
260 rc
= i2o_msg_post_wait(c
, m
, 60);
268 * i2o_block_request_alloc - Allocate an I2O block request struct
270 * Allocates an I2O block request struct and initialize the list.
272 * Returns a i2o_block_request pointer on success or negative error code
275 static inline struct i2o_block_request
*i2o_block_request_alloc(void)
277 struct i2o_block_request
*ireq
;
279 ireq
= mempool_alloc(i2o_blk_req_pool
.pool
, GFP_ATOMIC
);
281 return ERR_PTR(-ENOMEM
);
283 INIT_LIST_HEAD(&ireq
->queue
);
289 * i2o_block_request_free - Frees a I2O block request
290 * @ireq: I2O block request which should be freed
292 * Fres the allocated memory (give it back to the request mempool).
294 static inline void i2o_block_request_free(struct i2o_block_request
*ireq
)
296 mempool_free(ireq
, i2o_blk_req_pool
.pool
);
300 * i2o_block_sglist_alloc - Allocate the SG list and map it
301 * @ireq: I2O block request
303 * Builds the SG list and map it into to be accessable by the controller.
305 * Returns the number of elements in the SG list or 0 on failure.
307 static inline int i2o_block_sglist_alloc(struct i2o_block_request
*ireq
)
309 struct device
*dev
= &ireq
->i2o_blk_dev
->i2o_dev
->iop
->pdev
->dev
;
312 nents
= blk_rq_map_sg(ireq
->req
->q
, ireq
->req
, ireq
->sg_table
);
314 if (rq_data_dir(ireq
->req
) == READ
)
315 ireq
->sg_dma_direction
= PCI_DMA_FROMDEVICE
;
317 ireq
->sg_dma_direction
= PCI_DMA_TODEVICE
;
319 ireq
->sg_nents
= dma_map_sg(dev
, ireq
->sg_table
, nents
,
320 ireq
->sg_dma_direction
);
322 return ireq
->sg_nents
;
326 * i2o_block_sglist_free - Frees the SG list
327 * @ireq: I2O block request from which the SG should be freed
329 * Frees the SG list from the I2O block request.
331 static inline void i2o_block_sglist_free(struct i2o_block_request
*ireq
)
333 struct device
*dev
= &ireq
->i2o_blk_dev
->i2o_dev
->iop
->pdev
->dev
;
335 dma_unmap_sg(dev
, ireq
->sg_table
, ireq
->sg_nents
,
336 ireq
->sg_dma_direction
);
340 * i2o_block_prep_req_fn - Allocates I2O block device specific struct
341 * @q: request queue for the request
342 * @req: the request to prepare
344 * Allocate the necessary i2o_block_request struct and connect it to
345 * the request. This is needed that we not loose the SG list later on.
347 * Returns BLKPREP_OK on success or BLKPREP_DEFER on failure.
349 static int i2o_block_prep_req_fn(struct request_queue
*q
, struct request
*req
)
351 struct i2o_block_device
*i2o_blk_dev
= q
->queuedata
;
352 struct i2o_block_request
*ireq
;
354 /* request is already processed by us, so return */
355 if (req
->flags
& REQ_SPECIAL
) {
356 osm_debug("REQ_SPECIAL already set!\n");
357 req
->flags
|= REQ_DONTPREP
;
361 /* connect the i2o_block_request to the request */
363 ireq
= i2o_block_request_alloc();
364 if (unlikely(IS_ERR(ireq
))) {
365 osm_debug("unable to allocate i2o_block_request!\n");
366 return BLKPREP_DEFER
;
369 ireq
->i2o_blk_dev
= i2o_blk_dev
;
375 /* do not come back here */
376 req
->flags
|= REQ_DONTPREP
| REQ_SPECIAL
;
382 * i2o_block_delayed_request_fn - delayed request queue function
383 * delayed_request: the delayed request with the queue to start
385 * If the request queue is stopped for a disk, and there is no open
386 * request, a new event is created, which calls this function to start
387 * the queue after I2O_BLOCK_REQUEST_TIME. Otherwise the queue will never
390 static void i2o_block_delayed_request_fn(void *delayed_request
)
392 struct i2o_block_delayed_request
*dreq
= delayed_request
;
393 struct request_queue
*q
= dreq
->queue
;
396 spin_lock_irqsave(q
->queue_lock
, flags
);
398 spin_unlock_irqrestore(q
->queue_lock
, flags
);
403 * i2o_block_reply - Block OSM reply handler.
404 * @c: I2O controller from which the message arrives
405 * @m: message id of reply
406 * qmsg: the actuall I2O message reply
408 * This function gets all the message replies.
411 static int i2o_block_reply(struct i2o_controller
*c
, u32 m
,
412 struct i2o_message
*msg
)
414 struct i2o_block_request
*ireq
;
416 struct i2o_block_device
*dev
;
417 struct request_queue
*q
;
422 if (unlikely(le32_to_cpu(msg
->u
.head
[0]) & (1 << 13))) {
423 struct i2o_message
*pmsg
;
427 * FAILed message from controller
428 * We increment the error count and abort it
430 * In theory this will never happen. The I2O block class
431 * specification states that block devices never return
432 * FAILs but instead use the REQ status field...but
433 * better be on the safe side since no one really follows
434 * the spec to the book :)
436 pm
= le32_to_cpu(msg
->body
[3]);
437 pmsg
= i2o_msg_in_to_virt(c
, pm
);
439 req
= i2o_cntxt_list_get(c
, le32_to_cpu(pmsg
->u
.s
.tcntxt
));
440 if (unlikely(!req
)) {
441 osm_err("NULL reply received!\n");
446 dev
= ireq
->i2o_blk_dev
;
451 spin_lock_irqsave(q
->queue_lock
, flags
);
453 while (end_that_request_chunk(req
, !req
->errors
,
454 le32_to_cpu(pmsg
->body
[1]))) ;
455 end_that_request_last(req
);
457 dev
->open_queue_depth
--;
458 list_del(&ireq
->queue
);
461 spin_unlock_irqrestore(q
->queue_lock
, flags
);
463 /* Now flush the message by making it a NOP */
469 req
= i2o_cntxt_list_get(c
, le32_to_cpu(msg
->u
.s
.tcntxt
));
470 if (unlikely(!req
)) {
471 osm_err("NULL reply received!\n");
476 dev
= ireq
->i2o_blk_dev
;
479 if (unlikely(!dev
->i2o_dev
)) {
481 * This is HACK, but Intel Integrated RAID allows user
482 * to delete a volume that is claimed, locked, and in use
483 * by the OS. We have to check for a reply from a
484 * non-existent device and flag it as an error or the system
488 osm_warn("Data transfer to deleted device!\n");
489 spin_lock_irqsave(q
->queue_lock
, flags
);
490 while (end_that_request_chunk
491 (req
, !req
->errors
, le32_to_cpu(msg
->body
[1]))) ;
492 end_that_request_last(req
);
494 dev
->open_queue_depth
--;
495 list_del(&ireq
->queue
);
498 spin_unlock_irqrestore(q
->queue_lock
, flags
);
503 * Lets see what is cooking. We stuffed the
504 * request in the context.
507 st
= le32_to_cpu(msg
->body
[0]) >> 24;
511 char *bsa_errors
[] = {
514 "Failure communicating to device",
516 "Device is not ready",
518 "Media is locked by another user",
520 "Failure communicating to device",
521 "Device bus failure",
522 "Device is locked by another user",
523 "Device is write protected",
525 "Volume has changed, waiting for acknowledgement"
528 err
= le32_to_cpu(msg
->body
[0]) & 0xffff;
531 * Device not ready means two things. One is that the
532 * the thing went offline (but not a removal media)
534 * The second is that you have a SuperTrak 100 and the
535 * firmware got constipated. Unlike standard i2o card
536 * setups the supertrak returns an error rather than
537 * blocking for the timeout in these cases.
539 * Don't stick a supertrak100 into cache aggressive modes
542 osm_err("block-osm: /dev/%s error: %s", dev
->gd
->disk_name
,
543 bsa_errors
[le32_to_cpu(msg
->body
[0]) & 0xffff]);
544 if (le32_to_cpu(msg
->body
[0]) & 0x00ff0000)
545 printk(KERN_ERR
" - DDM attempted %d retries",
546 (le32_to_cpu(msg
->body
[0]) >> 16) & 0x00ff);
547 printk(KERN_ERR
".\n");
552 if (!end_that_request_chunk
553 (req
, !req
->errors
, le32_to_cpu(msg
->body
[1]))) {
554 add_disk_randomness(req
->rq_disk
);
555 spin_lock_irqsave(q
->queue_lock
, flags
);
557 end_that_request_last(req
);
559 dev
->open_queue_depth
--;
560 list_del(&ireq
->queue
);
563 spin_unlock_irqrestore(q
->queue_lock
, flags
);
565 i2o_block_sglist_free(ireq
);
566 i2o_block_request_free(ireq
);
568 osm_err("still remaining chunks\n");
573 static void i2o_block_event(struct i2o_event
*evt
)
575 osm_info("block-osm: event received\n");
579 * SCSI-CAM for ioctl geometry mapping
580 * Duplicated with SCSI - this should be moved into somewhere common
583 * LBA -> CHS mapping table taken from:
585 * "Incorporating the I2O Architecture into BIOS for Intel Architecture
588 * This is an I2O document that is only available to I2O members,
591 * From my understanding, this is how all the I2O cards do this
593 * Disk Size | Sectors | Heads | Cylinders
594 * ---------------+---------+-------+-------------------
595 * 1 < X <= 528M | 63 | 16 | X/(63 * 16 * 512)
596 * 528M < X <= 1G | 63 | 32 | X/(63 * 32 * 512)
597 * 1 < X <528M | 63 | 16 | X/(63 * 16 * 512)
598 * 1 < X <528M | 63 | 16 | X/(63 * 16 * 512)
601 #define BLOCK_SIZE_528M 1081344
602 #define BLOCK_SIZE_1G 2097152
603 #define BLOCK_SIZE_21G 4403200
604 #define BLOCK_SIZE_42G 8806400
605 #define BLOCK_SIZE_84G 17612800
607 static void i2o_block_biosparam(unsigned long capacity
, unsigned short *cyls
,
608 unsigned char *hds
, unsigned char *secs
)
610 unsigned long heads
, sectors
, cylinders
;
612 sectors
= 63L; /* Maximize sectors per track */
613 if (capacity
<= BLOCK_SIZE_528M
)
615 else if (capacity
<= BLOCK_SIZE_1G
)
617 else if (capacity
<= BLOCK_SIZE_21G
)
619 else if (capacity
<= BLOCK_SIZE_42G
)
624 cylinders
= (unsigned long)capacity
/ (heads
* sectors
);
626 *cyls
= (unsigned short)cylinders
; /* Stuff return values */
627 *secs
= (unsigned char)sectors
;
628 *hds
= (unsigned char)heads
;
632 * i2o_block_open - Open the block device
634 * Power up the device, mount and lock the media. This function is called,
635 * if the block device is opened for access.
637 * Returns 0 on success or negative error code on failure.
639 static int i2o_block_open(struct inode
*inode
, struct file
*file
)
641 struct i2o_block_device
*dev
= inode
->i_bdev
->bd_disk
->private_data
;
646 if (dev
->power
> 0x1f)
647 i2o_block_device_power(dev
, 0x02);
649 i2o_block_device_mount(dev
->i2o_dev
, -1);
651 i2o_block_device_lock(dev
->i2o_dev
, -1);
653 osm_debug("Ready.\n");
659 * i2o_block_release - Release the I2O block device
661 * Unlock and unmount the media, and power down the device. Gets called if
662 * the block device is closed.
664 * Returns 0 on success or negative error code on failure.
666 static int i2o_block_release(struct inode
*inode
, struct file
*file
)
668 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
669 struct i2o_block_device
*dev
= disk
->private_data
;
673 * This is to deail with the case of an application
674 * opening a device and then the device dissapears while
675 * it's in use, and then the application tries to release
676 * it. ex: Unmounting a deleted RAID volume at reboot.
677 * If we send messages, it will just cause FAILs since
678 * the TID no longer exists.
683 i2o_block_device_flush(dev
->i2o_dev
);
685 i2o_block_device_unlock(dev
->i2o_dev
, -1);
687 if (dev
->flags
& (1 << 3 | 1 << 4)) /* Removable */
692 i2o_block_device_power(dev
, operation
);
698 * i2o_block_ioctl - Issue device specific ioctl calls.
699 * @cmd: ioctl command
702 * Handles ioctl request for the block device.
704 * Return 0 on success or negative error on failure.
706 static int i2o_block_ioctl(struct inode
*inode
, struct file
*file
,
707 unsigned int cmd
, unsigned long arg
)
709 struct gendisk
*disk
= inode
->i_bdev
->bd_disk
;
710 struct i2o_block_device
*dev
= disk
->private_data
;
711 void __user
*argp
= (void __user
*)arg
;
713 /* Anyone capable of this syscall can do *real bad* things */
715 if (!capable(CAP_SYS_ADMIN
))
721 struct hd_geometry g
;
722 i2o_block_biosparam(get_capacity(disk
),
723 &g
.cylinders
, &g
.heads
, &g
.sectors
);
724 g
.start
= get_start_sect(inode
->i_bdev
);
725 return copy_to_user(argp
, &g
, sizeof(g
)) ? -EFAULT
: 0;
729 return put_user(dev
->rcache
, (int __user
*)arg
);
731 return put_user(dev
->wcache
, (int __user
*)arg
);
733 if (arg
< 0 || arg
> CACHE_SMARTFETCH
)
739 && (arg
< CACHE_WRITETHROUGH
|| arg
> CACHE_SMARTBACK
))
748 * i2o_block_media_changed - Have we seen a media change?
749 * @disk: gendisk which should be verified
751 * Verifies if the media has changed.
753 * Returns 1 if the media was changed or 0 otherwise.
755 static int i2o_block_media_changed(struct gendisk
*disk
)
757 struct i2o_block_device
*p
= disk
->private_data
;
759 if (p
->media_change_flag
) {
760 p
->media_change_flag
= 0;
767 * i2o_block_transfer - Transfer a request to/from the I2O controller
768 * @req: the request which should be transfered
770 * This function converts the request into a I2O message. The necessary
771 * DMA buffers are allocated and after everything is setup post the message
772 * to the I2O controller. No cleanup is done by this function. It is done
773 * on the interrupt side when the reply arrives.
775 * Return 0 on success or negative error code on failure.
777 static int i2o_block_transfer(struct request
*req
)
779 struct i2o_block_device
*dev
= req
->rq_disk
->private_data
;
780 struct i2o_controller
*c
= dev
->i2o_dev
->iop
;
781 int tid
= dev
->i2o_dev
->lct_data
.tid
;
782 struct i2o_message __iomem
*msg
;
784 struct i2o_block_request
*ireq
= req
->special
;
785 struct scatterlist
*sg
;
793 m
= i2o_msg_get(c
, &msg
);
794 if (m
== I2O_QUEUE_EMPTY
) {
799 tcntxt
= i2o_cntxt_list_add(c
, req
);
805 if ((sgnum
= i2o_block_sglist_alloc(ireq
)) <= 0) {
810 /* Build the message based on the request. */
811 writel(i2o_block_driver
.context
, &msg
->u
.s
.icntxt
);
812 writel(tcntxt
, &msg
->u
.s
.tcntxt
);
813 writel(req
->nr_sectors
<< 9, &msg
->body
[1]);
815 writel((((u64
) req
->sector
) << 9) & 0xffffffff, &msg
->body
[2]);
816 writel(req
->sector
>> 23, &msg
->body
[3]);
818 mptr
= &msg
->body
[4];
822 if (rq_data_dir(req
) == READ
) {
823 writel(I2O_CMD_BLOCK_READ
<< 24 | HOST_TID
<< 12 | tid
,
825 sg_flags
= 0x10000000;
826 switch (dev
->rcache
) {
828 writel(0, &msg
->body
[0]);
831 writel(0x201F0008, &msg
->body
[0]);
833 case CACHE_SMARTFETCH
:
834 if (req
->nr_sectors
> 16)
835 writel(0x201F0008, &msg
->body
[0]);
837 writel(0x001F0000, &msg
->body
[0]);
841 writel(I2O_CMD_BLOCK_WRITE
<< 24 | HOST_TID
<< 12 | tid
,
843 sg_flags
= 0x14000000;
844 switch (dev
->wcache
) {
846 writel(0, &msg
->body
[0]);
848 case CACHE_WRITETHROUGH
:
849 writel(0x001F0008, &msg
->body
[0]);
851 case CACHE_WRITEBACK
:
852 writel(0x001F0010, &msg
->body
[0]);
854 case CACHE_SMARTBACK
:
855 if (req
->nr_sectors
> 16)
856 writel(0x001F0004, &msg
->body
[0]);
858 writel(0x001F0010, &msg
->body
[0]);
860 case CACHE_SMARTTHROUGH
:
861 if (req
->nr_sectors
> 16)
862 writel(0x001F0004, &msg
->body
[0]);
864 writel(0x001F0010, &msg
->body
[0]);
868 for (i
= sgnum
; i
> 0; i
--) {
870 sg_flags
|= 0x80000000;
871 writel(sg_flags
| sg_dma_len(sg
), mptr
);
872 writel(sg_dma_address(sg
), mptr
+ 4);
877 writel(I2O_MESSAGE_SIZE
878 (((unsigned long)mptr
-
879 (unsigned long)&msg
->u
.head
[0]) >> 2) | SGL_OFFSET_8
,
882 list_add_tail(&ireq
->queue
, &dev
->open_queue
);
883 dev
->open_queue_depth
++;
890 i2o_cntxt_list_remove(c
, req
);
900 * i2o_block_request_fn - request queue handling function
901 * q: request queue from which the request could be fetched
903 * Takes the next request from the queue, transfers it and if no error
904 * occurs dequeue it from the queue. On arrival of the reply the message
905 * will be processed further. If an error occurs requeue the request.
907 static void i2o_block_request_fn(struct request_queue
*q
)
911 while (!blk_queue_plugged(q
)) {
912 req
= elv_next_request(q
);
916 if (blk_fs_request(req
)) {
917 struct i2o_block_delayed_request
*dreq
;
918 struct i2o_block_request
*ireq
= req
->special
;
919 unsigned int queue_depth
;
921 queue_depth
= ireq
->i2o_blk_dev
->open_queue_depth
;
923 if (queue_depth
< I2O_BLOCK_MAX_OPEN_REQUESTS
)
924 if (!i2o_block_transfer(req
)) {
925 blkdev_dequeue_request(req
);
932 /* stop the queue and retry later */
933 dreq
= kmalloc(sizeof(*dreq
), GFP_ATOMIC
);
938 INIT_WORK(&dreq
->work
, i2o_block_delayed_request_fn
,
941 osm_info("transfer error\n");
942 if (!queue_delayed_work(i2o_block_driver
.event_queue
,
944 I2O_BLOCK_RETRY_TIME
))
955 /* I2O Block device operations definition */
956 static struct block_device_operations i2o_block_fops
= {
957 .owner
= THIS_MODULE
,
958 .open
= i2o_block_open
,
959 .release
= i2o_block_release
,
960 .ioctl
= i2o_block_ioctl
,
961 .media_changed
= i2o_block_media_changed
965 * i2o_block_device_alloc - Allocate memory for a I2O Block device
967 * Allocate memory for the i2o_block_device struct, gendisk and request
968 * queue and initialize them as far as no additional information is needed.
970 * Returns a pointer to the allocated I2O Block device on succes or a
971 * negative error code on failure.
973 static struct i2o_block_device
*i2o_block_device_alloc(void)
975 struct i2o_block_device
*dev
;
977 struct request_queue
*queue
;
980 dev
= kmalloc(sizeof(*dev
), GFP_KERNEL
);
982 osm_err("Insufficient memory to allocate I2O Block disk.\n");
986 memset(dev
, 0, sizeof(*dev
));
988 INIT_LIST_HEAD(&dev
->open_queue
);
989 spin_lock_init(&dev
->lock
);
990 dev
->rcache
= CACHE_PREFETCH
;
991 dev
->wcache
= CACHE_WRITEBACK
;
993 /* allocate a gendisk with 16 partitions */
996 osm_err("Insufficient memory to allocate gendisk.\n");
1001 /* initialize the request queue */
1002 queue
= blk_init_queue(i2o_block_request_fn
, &dev
->lock
);
1004 osm_err("Insufficient memory to allocate request queue.\n");
1009 blk_queue_prep_rq(queue
, i2o_block_prep_req_fn
);
1011 gd
->major
= I2O_MAJOR
;
1013 gd
->fops
= &i2o_block_fops
;
1014 gd
->private_data
= dev
;
1031 * i2o_block_probe - verify if dev is a I2O Block device and install it
1032 * @dev: device to verify if it is a I2O Block device
1034 * We only verify if the user_tid of the device is 0xfff and then install
1035 * the device. Otherwise it is used by some other device (e. g. RAID).
1037 * Returns 0 on success or negative error code on failure.
1039 static int i2o_block_probe(struct device
*dev
)
1041 struct i2o_device
*i2o_dev
= to_i2o_device(dev
);
1042 struct i2o_block_device
*i2o_blk_dev
;
1043 struct i2o_controller
*c
= i2o_dev
->iop
;
1045 struct request_queue
*queue
;
1046 static int unit
= 0;
1054 /* skip devices which are used by IOP */
1055 if (i2o_dev
->lct_data
.user_tid
!= 0xfff) {
1056 osm_debug("skipping used device %03x\n", i2o_dev
->lct_data
.tid
);
1060 osm_info("New device detected (TID: %03x)\n", i2o_dev
->lct_data
.tid
);
1062 if (i2o_device_claim(i2o_dev
)) {
1063 osm_warn("Unable to claim device. Installation aborted\n");
1068 i2o_blk_dev
= i2o_block_device_alloc();
1069 if (IS_ERR(i2o_blk_dev
)) {
1070 osm_err("could not alloc a new I2O block device");
1071 rc
= PTR_ERR(i2o_blk_dev
);
1075 i2o_blk_dev
->i2o_dev
= i2o_dev
;
1076 dev_set_drvdata(dev
, i2o_blk_dev
);
1079 gd
= i2o_blk_dev
->gd
;
1080 gd
->first_minor
= unit
<< 4;
1081 sprintf(gd
->disk_name
, "i2o/hd%c", 'a' + unit
);
1082 sprintf(gd
->devfs_name
, "i2o/hd%c", 'a' + unit
);
1083 gd
->driverfs_dev
= &i2o_dev
->device
;
1085 /* setup request queue */
1087 queue
->queuedata
= i2o_blk_dev
;
1089 blk_queue_max_phys_segments(queue
, I2O_MAX_SEGMENTS
);
1090 blk_queue_max_sectors(queue
, I2O_MAX_SECTORS
);
1095 i2o_status_block
*sb
;
1097 sb
= c
->status_block
.virt
;
1099 segments
= (sb
->inbound_frame_size
-
1100 sizeof(struct i2o_message
) / 4 - 4) / 2;
1103 blk_queue_max_hw_segments(queue
, segments
);
1105 osm_debug("max sectors = %d\n", I2O_MAX_SECTORS
);
1106 osm_debug("phys segments = %d\n", I2O_MAX_SEGMENTS
);
1107 osm_debug("hw segments = %d\n", segments
);
1110 * Ask for the current media data. If that isn't supported
1111 * then we ask for the device capacity data
1113 if (i2o_parm_field_get(i2o_dev
, 0x0004, 1, &blocksize
, 4) != 0
1114 || i2o_parm_field_get(i2o_dev
, 0x0004, 0, &size
, 8) != 0) {
1115 i2o_parm_field_get(i2o_dev
, 0x0000, 3, &blocksize
, 4);
1116 i2o_parm_field_get(i2o_dev
, 0x0000, 4, &size
, 8);
1118 osm_debug("blocksize = %d\n", blocksize
);
1120 if (i2o_parm_field_get(i2o_dev
, 0x0000, 2, &power
, 2))
1122 i2o_parm_field_get(i2o_dev
, 0x0000, 5, &flags
, 4);
1123 i2o_parm_field_get(i2o_dev
, 0x0000, 6, &status
, 4);
1125 set_capacity(gd
, size
>> 9);
1127 i2o_event_register(i2o_dev
, &i2o_block_driver
, 0, 0xffffffff);
1136 i2o_device_claim_release(i2o_dev
);
1142 /* Block OSM driver struct */
1143 static struct i2o_driver i2o_block_driver
= {
1145 .event
= i2o_block_event
,
1146 .reply
= i2o_block_reply
,
1147 .classes
= i2o_block_class_id
,
1149 .probe
= i2o_block_probe
,
1150 .remove
= i2o_block_remove
,
1155 * i2o_block_init - Block OSM initialization function
1157 * Allocate the slab and mempool for request structs, registers i2o_block
1158 * block device and finally register the Block OSM in the I2O core.
1160 * Returns 0 on success or negative error code on failure.
1162 static int __init
i2o_block_init(void)
1167 printk(KERN_INFO OSM_DESCRIPTION
" v" OSM_VERSION
"\n");
1169 /* Allocate request mempool and slab */
1170 size
= sizeof(struct i2o_block_request
);
1171 i2o_blk_req_pool
.slab
= kmem_cache_create("i2o_block_req", size
, 0,
1172 SLAB_HWCACHE_ALIGN
, NULL
,
1174 if (!i2o_blk_req_pool
.slab
) {
1175 osm_err("can't init request slab\n");
1180 i2o_blk_req_pool
.pool
= mempool_create(I2O_REQ_MEMPOOL_SIZE
,
1183 i2o_blk_req_pool
.slab
);
1184 if (!i2o_blk_req_pool
.pool
) {
1185 osm_err("can't init request mempool\n");
1190 /* Register the block device interfaces */
1191 rc
= register_blkdev(I2O_MAJOR
, "i2o_block");
1193 osm_err("unable to register block device\n");
1197 osm_info("registered device at major %d\n", I2O_MAJOR
);
1200 /* Register Block OSM into I2O core */
1201 rc
= i2o_driver_register(&i2o_block_driver
);
1203 osm_err("Could not register Block driver\n");
1204 goto unregister_blkdev
;
1210 unregister_blkdev(I2O_MAJOR
, "i2o_block");
1213 mempool_destroy(i2o_blk_req_pool
.pool
);
1216 kmem_cache_destroy(i2o_blk_req_pool
.slab
);
1223 * i2o_block_exit - Block OSM exit function
1225 * Unregisters Block OSM from I2O core, unregisters i2o_block block device
1226 * and frees the mempool and slab.
1228 static void __exit
i2o_block_exit(void)
1230 /* Unregister I2O Block OSM from I2O core */
1231 i2o_driver_unregister(&i2o_block_driver
);
1233 /* Unregister block device */
1234 unregister_blkdev(I2O_MAJOR
, "i2o_block");
1236 /* Free request mempool and slab */
1237 mempool_destroy(i2o_blk_req_pool
.pool
);
1238 kmem_cache_destroy(i2o_blk_req_pool
.slab
);
1241 MODULE_AUTHOR("Red Hat");
1242 MODULE_LICENSE("GPL");
1243 MODULE_DESCRIPTION(OSM_DESCRIPTION
);
1244 MODULE_VERSION(OSM_VERSION
);
1246 module_init(i2o_block_init
);
1247 module_exit(i2o_block_exit
);