2 * This program is free software; you can redistribute it and/or modify it
3 * under the terms of the GNU General Public License as published by the
4 * Free Software Foundation; either version 2, or (at your option) any
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * For the avoidance of doubt the "preferred form" of this code is one which
13 * is in an open non patent encumbered format. Where cryptographic key signing
14 * forms part of the process of creating an executable the information
15 * including keys needed to generate an equivalently functional executable
16 * are deemed to be part of the source code.
18 * Complications for I2O scsi
20 * o Each (bus,lun) is a logical device in I2O. We keep a map
21 * table. We spoof failed selection for unmapped units
22 * o Request sense buffers can come back for free.
23 * o Scatter gather is a bit dynamic. We have to investigate at
25 * o Some of our resources are dynamically shared. The i2o core
26 * needs a message reservation protocol to avoid swap v net
27 * deadlocking. We need to back off queue requests.
29 * In general the firmware wants to help. Where its help isn't performance
30 * useful we just ignore the aid. Its not worth the code in truth.
34 * Scatter gather now works
35 * Markus Lidel <Markus.Lidel@shadowconnect.com>:
36 * Minor fixes for 2.6.
40 * Fix the resource management problems.
43 #include <linux/module.h>
44 #include <linux/kernel.h>
45 #include <linux/types.h>
46 #include <linux/string.h>
47 #include <linux/ioport.h>
48 #include <linux/jiffies.h>
49 #include <linux/interrupt.h>
50 #include <linux/timer.h>
51 #include <linux/delay.h>
52 #include <linux/proc_fs.h>
53 #include <linux/prefetch.h>
54 #include <linux/pci.h>
55 #include <linux/blkdev.h>
56 #include <linux/i2o.h>
59 #include <asm/system.h>
61 #include <asm/atomic.h>
63 #include <scsi/scsi.h>
64 #include <scsi/scsi_host.h>
65 #include <scsi/scsi_device.h>
66 #include <scsi/scsi_cmnd.h>
68 #define VERSION_STRING "Version 0.1.2"
70 static struct i2o_driver i2o_scsi_driver;
72 static int i2o_scsi_max_id = 16;
73 static int i2o_scsi_max_lun = 8;
75 struct i2o_scsi_host {
76 struct Scsi_Host *scsi_host; /* pointer to the SCSI host */
77 struct i2o_controller *iop; /* pointer to the I2O controller */
78 struct i2o_device *channel[0]; /* channel->i2o_dev mapping table */
81 static struct scsi_host_template i2o_scsi_host_template;
83 #define I2O_SCSI_CAN_QUEUE 4
85 /* SCSI OSM class handling definition */
86 static struct i2o_class_id i2o_scsi_class_id[] = {
87 {I2O_CLASS_SCSI_PERIPHERAL},
91 static struct i2o_scsi_host *i2o_scsi_host_alloc(struct i2o_controller *c)
93 struct i2o_scsi_host *i2o_shost;
94 struct i2o_device *i2o_dev;
95 struct Scsi_Host *scsi_host;
100 i2o_status_block *sb;
102 list_for_each_entry(i2o_dev, &c->devices, list)
103 if (i2o_dev->lct_data.class_id == I2O_CLASS_BUS_ADAPTER_PORT) {
104 if (i2o_parm_field_get(i2o_dev, 0x0000, 0, &type, 1) || (type == 1)) /* SCSI bus */
109 printk(KERN_WARNING "scsi-osm: no channels found on %s\n",
111 return ERR_PTR(-EFAULT);
114 size = max_channel * sizeof(struct i2o_device *)
115 + sizeof(struct i2o_scsi_host);
117 scsi_host = scsi_host_alloc(&i2o_scsi_host_template, size);
119 printk(KERN_WARNING "scsi-osm: Could not allocate SCSI host\n");
120 return ERR_PTR(-ENOMEM);
123 scsi_host->max_channel = max_channel - 1;
124 scsi_host->max_id = i2o_scsi_max_id;
125 scsi_host->max_lun = i2o_scsi_max_lun;
126 scsi_host->this_id = c->unit;
128 sb = c->status_block.virt;
130 scsi_host->sg_tablesize = (sb->inbound_frame_size -
131 sizeof(struct i2o_message) / 4 - 6) / 2;
133 i2o_shost = (struct i2o_scsi_host *)scsi_host->hostdata;
134 i2o_shost->scsi_host = scsi_host;
138 list_for_each_entry(i2o_dev, &c->devices, list)
139 if (i2o_dev->lct_data.class_id == I2O_CLASS_BUS_ADAPTER_PORT) {
140 if (i2o_parm_field_get(i2o_dev, 0x0000, 0, &type, 1) || (type == 1)) /* only SCSI bus */
141 i2o_shost->channel[i++] = i2o_dev;
143 if (i >= max_channel)
151 * i2o_scsi_get_host - Get an I2O SCSI host
152 * @c: I2O controller to for which to get the SCSI host
154 * If the I2O controller already exists as SCSI host, the SCSI host
155 * is returned, otherwise the I2O controller is added to the SCSI
158 * Returns pointer to the I2O SCSI host on success or NULL on failure.
160 static struct i2o_scsi_host *i2o_scsi_get_host(struct i2o_controller *c)
162 return c->driver_data[i2o_scsi_driver.context];
166 * i2o_scsi_remove - Remove I2O device from SCSI core
167 * @dev: device which should be removed
169 * Removes the I2O device from the SCSI core again.
171 * Returns 0 on success.
173 static int i2o_scsi_remove(struct device *dev)
175 struct i2o_device *i2o_dev = to_i2o_device(dev);
176 struct i2o_controller *c = i2o_dev->iop;
177 struct i2o_scsi_host *i2o_shost;
178 struct scsi_device *scsi_dev;
180 i2o_shost = i2o_scsi_get_host(c);
182 shost_for_each_device(scsi_dev, i2o_shost->scsi_host)
183 if (scsi_dev->hostdata == i2o_dev) {
184 scsi_remove_device(scsi_dev);
185 scsi_device_put(scsi_dev);
193 * i2o_scsi_probe - verify if dev is a I2O SCSI device and install it
194 * @dev: device to verify if it is a I2O SCSI device
196 * Retrieve channel, id and lun for I2O device. If everthing goes well
197 * register the I2O device as SCSI device on the I2O SCSI controller.
199 * Returns 0 on success or negative error code on failure.
201 static int i2o_scsi_probe(struct device *dev)
203 struct i2o_device *i2o_dev = to_i2o_device(dev);
204 struct i2o_controller *c = i2o_dev->iop;
205 struct i2o_scsi_host *i2o_shost;
206 struct Scsi_Host *scsi_host;
207 struct i2o_device *parent;
208 struct scsi_device *scsi_dev;
214 i2o_shost = i2o_scsi_get_host(c);
218 scsi_host = i2o_shost->scsi_host;
220 if (i2o_parm_field_get(i2o_dev, 0, 3, &id, 4) < 0)
223 if (id >= scsi_host->max_id) {
224 printk(KERN_WARNING "scsi-osm: SCSI device id (%d) >= max_id "
225 "of I2O host (%d)", id, scsi_host->max_id);
229 if (i2o_parm_field_get(i2o_dev, 0, 4, &lun, 8) < 0)
231 if (lun >= scsi_host->max_lun) {
232 printk(KERN_WARNING "scsi-osm: SCSI device id (%d) >= max_lun "
233 "of I2O host (%d)", (unsigned int)lun,
238 parent = i2o_iop_find_device(c, i2o_dev->lct_data.parent_tid);
240 printk(KERN_WARNING "scsi-osm: can not find parent of device "
241 "%03x\n", i2o_dev->lct_data.tid);
245 for (i = 0; i <= i2o_shost->scsi_host->max_channel; i++)
246 if (i2o_shost->channel[i] == parent)
250 printk(KERN_WARNING "scsi-osm: can not find channel of device "
251 "%03x\n", i2o_dev->lct_data.tid);
256 __scsi_add_device(i2o_shost->scsi_host, channel, id, lun, i2o_dev);
259 printk(KERN_WARNING "scsi-osm: can not add SCSI device "
260 "%03x\n", i2o_dev->lct_data.tid);
264 pr_debug("Added new SCSI device %03x (cannel: %d, id: %d, lun: %d)\n",
265 i2o_dev->lct_data.tid, channel, id, (unsigned int)lun);
270 static const char *i2o_scsi_info(struct Scsi_Host *SChost)
272 struct i2o_scsi_host *hostdata;
273 hostdata = (struct i2o_scsi_host *)SChost->hostdata;
274 return hostdata->iop->name;
278 * i2o_scsi_reply - SCSI OSM message reply handler
279 * @c: controller issuing the reply
280 * @m: message id for flushing
281 * @msg: the message from the controller
283 * Process reply messages (interrupts in normal scsi controller think).
284 * We can get a variety of messages to process. The normal path is
285 * scsi command completions. We must also deal with IOP failures,
286 * the reply to a bus reset and the reply to a LUN query.
288 * Returns 0 on success and if the reply should not be flushed or > 0
289 * on success and if the reply should be flushed. Returns negative error
290 * code on failure and if the reply should be flushed.
292 static int i2o_scsi_reply(struct i2o_controller *c, u32 m,
293 struct i2o_message *msg)
295 struct scsi_cmnd *cmd;
299 cmd = i2o_cntxt_list_get(c, le32_to_cpu(msg->u.s.tcntxt));
301 if (msg->u.head[0] & (1 << 13)) {
302 struct i2o_message __iomem *pmsg; /* preserved message */
306 pm = le32_to_cpu(msg->body[3]);
308 pmsg = i2o_msg_in_to_virt(c, pm);
310 printk(KERN_ERR "IOP fail.\n");
311 printk(KERN_ERR "From %d To %d Cmd %d.\n",
312 (msg->u.head[1] >> 12) & 0xFFF,
313 msg->u.head[1] & 0xFFF, msg->u.head[1] >> 24);
314 printk(KERN_ERR "Failure Code %d.\n", msg->body[0] >> 24);
315 if (msg->body[0] & (1 << 16))
316 printk(KERN_ERR "Format error.\n");
317 if (msg->body[0] & (1 << 17))
318 printk(KERN_ERR "Path error.\n");
319 if (msg->body[0] & (1 << 18))
320 printk(KERN_ERR "Path State.\n");
321 if (msg->body[0] & (1 << 18))
323 printk(KERN_ERR "Congestion.\n");
327 printk(KERN_DEBUG "Failing message is %p.\n", pmsg);
329 cmd = i2o_cntxt_list_get(c, readl(&pmsg->u.s.tcntxt));
333 cmd->result = err << 16;
336 /* Now flush the message by making it a NOP */
343 * Low byte is device status, next is adapter status,
344 * (then one byte reserved), then request status.
346 ds = (u8) le32_to_cpu(msg->body[0]);
347 as = (u8) (le32_to_cpu(msg->body[0]) >> 8);
348 st = (u8) (le32_to_cpu(msg->body[0]) >> 24);
351 * Is this a control request coming back - eg an abort ?
356 printk(KERN_WARNING "SCSI abort: %08X",
357 le32_to_cpu(msg->body[0]));
358 printk(KERN_INFO "SCSI abort completed.\n");
362 pr_debug("Completed %ld\n", cmd->serial_number);
366 /* An error has occurred */
370 count = le32_to_cpu(msg->body[1]);
371 if (count < cmd->underflow) {
373 printk(KERN_ERR "SCSI: underflow 0x%08X 0x%08X"
374 "\n", count, cmd->underflow);
375 printk(KERN_DEBUG "Cmd: ");
376 for (i = 0; i < 15; i++)
377 printk(KERN_DEBUG "%02X ",
379 printk(KERN_DEBUG ".\n");
380 cmd->result = (DID_ERROR << 16);
385 error = le32_to_cpu(msg->body[0]);
387 printk(KERN_ERR "scsi-osm: SCSI error %08x\n", error);
389 if ((error & 0xff) == 0x02 /*CHECK_CONDITION */ ) {
391 u32 len = sizeof(cmd->sense_buffer);
392 len = (len > 40) ? 40 : len;
393 // Copy over the sense data
394 memcpy(cmd->sense_buffer, (void *)&msg->body[3],
396 for (i = 0; i <= len; i++)
397 printk(KERN_INFO "%02x\n",
398 cmd->sense_buffer[i]);
399 if (cmd->sense_buffer[0] == 0x70
400 && cmd->sense_buffer[2] == DATA_PROTECT) {
401 /* This is to handle an array failed */
402 cmd->result = (DID_TIME_OUT << 16);
403 printk(KERN_WARNING "%s: SCSI Data "
404 "Protect-Device (%d,%d,%d) "
405 "hba_status=0x%x, dev_status="
406 "0x%x, cmd=0x%x\n", c->name,
407 (u32) cmd->device->channel,
408 (u32) cmd->device->id,
409 (u32) cmd->device->lun,
411 error & 0xff, cmd->cmnd[0]);
413 cmd->result = (DID_ERROR << 16);
421 cmd->result = DID_RESET << 16;
425 cmd->result = DID_PARITY << 16;
429 cmd->result = DID_ERROR << 16;
440 cmd->result = DID_OK << 16 | ds;
446 dma_unmap_sg(dev, (struct scatterlist *)cmd->buffer,
447 cmd->use_sg, cmd->sc_data_direction);
448 else if (cmd->request_bufflen)
449 dma_unmap_single(dev, (dma_addr_t) ((long)cmd->SCp.ptr),
450 cmd->request_bufflen, cmd->sc_data_direction);
456 * i2o_scsi_notify_controller_add - Retrieve notifications of added
458 * @c: the controller which was added
460 * If a I2O controller is added, we catch the notification to add a
461 * corresponding Scsi_Host.
463 static void i2o_scsi_notify_controller_add(struct i2o_controller *c)
465 struct i2o_scsi_host *i2o_shost;
468 i2o_shost = i2o_scsi_host_alloc(c);
469 if (IS_ERR(i2o_shost)) {
470 printk(KERN_ERR "scsi-osm: Could not initialize"
475 rc = scsi_add_host(i2o_shost->scsi_host, &c->device);
477 printk(KERN_ERR "scsi-osm: Could not add SCSI " "host\n");
478 scsi_host_put(i2o_shost->scsi_host);
482 c->driver_data[i2o_scsi_driver.context] = i2o_shost;
484 pr_debug("new I2O SCSI host added\n");
488 * i2o_scsi_notify_controller_remove - Retrieve notifications of removed
490 * @c: the controller which was removed
492 * If a I2O controller is removed, we catch the notification to remove the
493 * corresponding Scsi_Host.
495 static void i2o_scsi_notify_controller_remove(struct i2o_controller *c)
497 struct i2o_scsi_host *i2o_shost;
498 i2o_shost = i2o_scsi_get_host(c);
502 c->driver_data[i2o_scsi_driver.context] = NULL;
504 scsi_remove_host(i2o_shost->scsi_host);
505 scsi_host_put(i2o_shost->scsi_host);
506 pr_debug("I2O SCSI host removed\n");
509 /* SCSI OSM driver struct */
510 static struct i2o_driver i2o_scsi_driver = {
512 .reply = i2o_scsi_reply,
513 .classes = i2o_scsi_class_id,
514 .notify_controller_add = i2o_scsi_notify_controller_add,
515 .notify_controller_remove = i2o_scsi_notify_controller_remove,
517 .probe = i2o_scsi_probe,
518 .remove = i2o_scsi_remove,
523 * i2o_scsi_queuecommand - queue a SCSI command
524 * @SCpnt: scsi command pointer
525 * @done: callback for completion
527 * Issue a scsi command asynchronously. Return 0 on success or 1 if
528 * we hit an error (normally message queue congestion). The only
529 * minor complication here is that I2O deals with the device addressing
530 * so we have to map the bus/dev/lun back to an I2O handle as well
531 * as faking absent devices ourself.
533 * Locks: takes the controller lock on error path only
536 static int i2o_scsi_queuecommand(struct scsi_cmnd *SCpnt,
537 void (*done) (struct scsi_cmnd *))
539 struct i2o_controller *c;
540 struct Scsi_Host *host;
541 struct i2o_device *i2o_dev;
544 struct i2o_message __iomem *msg;
546 u32 scsi_flags, sg_flags;
553 * Do the incoming paperwork
556 i2o_dev = SCpnt->device->hostdata;
557 host = SCpnt->device->host;
561 SCpnt->scsi_done = done;
563 if (unlikely(!i2o_dev)) {
564 printk(KERN_WARNING "scsi-osm: no I2O device in request\n");
565 SCpnt->result = DID_NO_CONNECT << 16;
570 tid = i2o_dev->lct_data.tid;
572 pr_debug("qcmd: Tid = %03x\n", tid);
573 pr_debug("Real scsi messages.\n");
576 * Obtain an I2O message. If there are none free then
577 * throw it back to the scsi layer
580 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
581 if (m == I2O_QUEUE_EMPTY)
582 return SCSI_MLQUEUE_HOST_BUSY;
585 * Put together a scsi execscb message
588 len = SCpnt->request_bufflen;
590 switch (SCpnt->sc_data_direction) {
592 scsi_flags = 0x00000000; // DATA NO XFER
593 sg_flags = 0x00000000;
596 case PCI_DMA_TODEVICE:
597 scsi_flags = 0x80000000; // DATA OUT (iop-->dev)
598 sg_flags = 0x14000000;
601 case PCI_DMA_FROMDEVICE:
602 scsi_flags = 0x40000000; // DATA IN (iop<--dev)
603 sg_flags = 0x10000000;
607 /* Unknown - kill the command */
608 SCpnt->result = DID_NO_CONNECT << 16;
613 writel(I2O_CMD_SCSI_EXEC << 24 | HOST_TID << 12 | tid, &msg->u.head[1]);
614 writel(i2o_scsi_driver.context, &msg->u.s.icntxt);
616 /* We want the SCSI control block back */
617 writel(i2o_cntxt_list_add(c, SCpnt), &msg->u.s.tcntxt);
621 * Intermittant observations of msg frame word data corruption
622 * observed on msg[4] after:
623 * WRITE, READ-MODIFY-WRITE
624 * operations. 19990606 -sralston
626 * (Hence we build this word via tag. Its good practice anyway
627 * we don't want fetches over PCI needlessly)
630 /* Attach tags to the devices */
632 if(SCpnt->device->tagged_supported) {
633 if(SCpnt->tag == HEAD_OF_QUEUE_TAG)
634 scsi_flags |= 0x01000000;
635 else if(SCpnt->tag == ORDERED_QUEUE_TAG)
636 scsi_flags |= 0x01800000;
640 /* Direction, disconnect ok, tag, CDBLen */
641 writel(scsi_flags | 0x20200000 | SCpnt->cmd_len, &msg->body[0]);
643 mptr = &msg->body[1];
645 /* Write SCSI command into the message - always 16 byte block */
646 memcpy_toio(mptr, SCpnt->cmnd, 16);
648 lenptr = mptr++; /* Remember me - fill in when we know */
650 reqlen = 12; // SINGLE SGE
652 /* Now fill in the SGList and command */
654 struct scatterlist *sg;
657 sg = SCpnt->request_buffer;
660 sg_count = dma_map_sg(dev, sg, SCpnt->use_sg,
661 SCpnt->sc_data_direction);
663 if (unlikely(sg_count <= 0))
666 for (i = SCpnt->use_sg; i > 0; i--) {
668 sg_flags |= 0xC0000000;
669 writel(sg_flags | sg_dma_len(sg), mptr++);
670 writel(sg_dma_address(sg), mptr++);
671 len += sg_dma_len(sg);
675 reqlen = mptr - &msg->u.head[0];
678 len = SCpnt->request_bufflen;
685 dma_addr = dma_map_single(dev, SCpnt->request_buffer,
686 SCpnt->request_bufflen,
687 SCpnt->sc_data_direction);
691 SCpnt->SCp.ptr = (void *)(unsigned long)dma_addr;
692 sg_flags |= 0xC0000000;
693 writel(sg_flags | SCpnt->request_bufflen, mptr++);
694 writel(dma_addr, mptr++);
699 /* Stick the headers on */
700 writel(reqlen << 16 | SGL_OFFSET_10, &msg->u.head[0]);
702 /* Queue the message */
705 pr_debug("Issued %ld\n", SCpnt->serial_number);
711 * i2o_scsi_abort - abort a running command
712 * @SCpnt: command to abort
714 * Ask the I2O controller to abort a command. This is an asynchrnous
715 * process and our callback handler will see the command complete with an
716 * aborted message if it succeeds.
718 * Returns 0 if the command is successfully aborted or negative error code
721 static int i2o_scsi_abort(struct scsi_cmnd *SCpnt)
723 struct i2o_device *i2o_dev;
724 struct i2o_controller *c;
725 struct i2o_message __iomem *msg;
730 printk(KERN_WARNING "i2o_scsi: Aborting command block.\n");
732 i2o_dev = SCpnt->device->hostdata;
734 tid = i2o_dev->lct_data.tid;
736 m = i2o_msg_get_wait(c, &msg, I2O_TIMEOUT_MESSAGE_GET);
737 if (m == I2O_QUEUE_EMPTY)
738 return SCSI_MLQUEUE_HOST_BUSY;
740 writel(FIVE_WORD_MSG_SIZE | SGL_OFFSET_0, &msg->u.head[0]);
741 writel(I2O_CMD_SCSI_ABORT << 24 | HOST_TID << 12 | tid,
743 writel(i2o_cntxt_list_get_ptr(c, SCpnt), &msg->body[0]);
745 if (i2o_msg_post_wait(c, m, I2O_TIMEOUT_SCSI_SCB_ABORT))
752 * i2o_scsi_bios_param - Invent disk geometry
754 * @dev: block layer device
755 * @capacity: size in sectors
756 * @ip: geometry array
758 * This is anyones guess quite frankly. We use the same rules everyone
759 * else appears to and hope. It seems to work.
762 static int i2o_scsi_bios_param(struct scsi_device *sdev,
763 struct block_device *dev, sector_t capacity,
769 ip[0] = 64; /* heads */
770 ip[1] = 32; /* sectors */
771 if ((ip[2] = size >> 11) > 1024) { /* cylinders, test for big disk */
772 ip[0] = 255; /* heads */
773 ip[1] = 63; /* sectors */
774 ip[2] = size / (255 * 63); /* cylinders */
779 static struct scsi_host_template i2o_scsi_host_template = {
780 .proc_name = "SCSI-OSM",
781 .name = "I2O SCSI Peripheral OSM",
782 .info = i2o_scsi_info,
783 .queuecommand = i2o_scsi_queuecommand,
784 .eh_abort_handler = i2o_scsi_abort,
785 .bios_param = i2o_scsi_bios_param,
786 .can_queue = I2O_SCSI_CAN_QUEUE,
789 .use_clustering = ENABLE_CLUSTERING,
794 i2o_scsi_queuecommand(struct scsi_cmnd * cmd, void (*done) (struct scsi_cmnd *))
796 printk(KERN_INFO "queuecommand\n");
797 return SCSI_MLQUEUE_HOST_BUSY;
802 * i2o_scsi_init - SCSI OSM initialization function
804 * Register SCSI OSM into I2O core.
806 * Returns 0 on success or negative error code on failure.
808 static int __init i2o_scsi_init(void)
812 printk(KERN_INFO "I2O SCSI Peripheral OSM\n");
814 /* Register SCSI OSM into I2O core */
815 rc = i2o_driver_register(&i2o_scsi_driver);
817 printk(KERN_ERR "scsi-osm: Could not register SCSI driver\n");
825 * i2o_scsi_exit - SCSI OSM exit function
827 * Unregisters SCSI OSM from I2O core.
829 static void __exit i2o_scsi_exit(void)
831 /* Unregister I2O SCSI OSM from I2O core */
832 i2o_driver_unregister(&i2o_scsi_driver);
835 MODULE_AUTHOR("Red Hat Software");
836 MODULE_LICENSE("GPL");
838 module_init(i2o_scsi_init);
839 module_exit(i2o_scsi_exit);