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 * Complications for I2O scsi
14 * o Each (bus,lun) is a logical device in I2O. We keep a map
15 * table. We spoof failed selection for unmapped units
16 * o Request sense buffers can come back for free.
17 * o Scatter gather is a bit dynamic. We have to investigate at
19 * o Some of our resources are dynamically shared. The i2o core
20 * needs a message reservation protocol to avoid swap v net
21 * deadlocking. We need to back off queue requests.
23 * In general the firmware wants to help. Where its help isn't performance
24 * useful we just ignore the aid. Its not worth the code in truth.
27 * Steve Ralston : Scatter gather now works
31 * Fix the resource management problems.
34 #include <linux/module.h>
35 #include <linux/kernel.h>
36 #include <linux/types.h>
37 #include <linux/string.h>
38 #include <linux/ioport.h>
39 #include <linux/sched.h>
40 #include <linux/interrupt.h>
41 #include <linux/timer.h>
42 #include <linux/delay.h>
43 #include <linux/proc_fs.h>
45 #include <asm/system.h>
47 #include <asm/atomic.h>
48 #include <linux/blk.h>
49 #include <linux/version.h>
50 #include <linux/i2o.h>
51 #include "../scsi/scsi.h"
52 #include "../scsi/hosts.h"
53 #include "../scsi/sd.h"
56 #define VERSION_STRING "Version 0.0.1"
62 struct proc_dir_entry proc_scsi_i2o_scsi
= {
63 PROC_SCSI_I2O
, 8, "i2o_scsi", S_IFDIR
| S_IRUGO
| S_IXUGO
, 2
68 struct i2o_controller
*controller
;
69 s16 task
[16][8]; /* Allow 16 devices for now */
70 unsigned long tagclock
[16][8]; /* Tag clock for queueing */
71 s16 bus_task
; /* The adapter TID */
74 static int scsi_context
;
76 static int i2o_scsi_hosts
;
78 static u32
*retry
[32];
79 static struct i2o_controller
*retry_ctrl
[32];
80 static struct timer_list retry_timer
;
81 static int retry_ct
= 0;
83 static atomic_t queue_depth
;
86 * SG Chain buffer support...
89 #define SG_MAX_FRAGS 64
92 * FIXME: we should allocate one of these per bus we find as we
93 * locate them not in a lump at boot.
96 typedef struct _chain_buf
98 u32 sg_flags_cnt
[SG_MAX_FRAGS
];
99 u32 sg_buf
[SG_MAX_FRAGS
];
102 #define SG_CHAIN_BUF_SZ sizeof(chain_buf)
104 #define SG_MAX_BUFS (i2o_num_controllers * I2O_SCSI_CAN_QUEUE)
105 #define SG_CHAIN_POOL_SZ (SG_MAX_BUFS * SG_CHAIN_BUF_SZ)
107 static int max_sg_len
= 0;
108 static chain_buf
*sg_chain_pool
= NULL
;
109 static int sg_chain_tag
= 0;
110 static int sg_max_frags
= SG_MAX_FRAGS
;
113 * Retry congested frames. This actually needs pushing down into
114 * i2o core. We should only bother the OSM with this when we can't
115 * queue and retry the frame. Or perhaps we should call the OSM
116 * and its default handler should be this in the core, and this
117 * call a 2nd "I give up" handler in the OSM ?
120 static void i2o_retry_run(unsigned long f
)
128 for(i
=0;i
<retry_ct
;i
++)
129 i2o_post_message(retry_ctrl
[i
], virt_to_bus(retry
[i
]));
132 restore_flags(flags
);
135 static void flush_pending(void)
143 for(i
=0;i
<retry_ct
;i
++)
145 retry
[i
][0]&=~0xFFFFFF;
146 retry
[i
][0]|=I2O_CMD_UTIL_NOP
<<24;
147 i2o_post_message(retry_ctrl
[i
],virt_to_bus(retry
[i
]));
151 restore_flags(flags
);
154 static void i2o_scsi_reply(struct i2o_handler
*h
, struct i2o_controller
*c
, struct i2o_message
*msg
)
156 Scsi_Cmnd
*current_command
;
162 printk("IOP fail.\n");
163 printk("From %d To %d Cmd %d.\n",
167 printk("Failure Code %d.\n", m
[4]>>24);
169 printk("Format error.\n");
171 printk("Path error.\n");
173 printk("Path State.\n");
175 printk("Congestion.\n");
177 m
=(u32
*)bus_to_virt(m
[7]);
178 printk("Failing message is %p.\n", m
);
180 if((m
[4]&(1<<18)) && retry_ct
< 32)
182 retry_ctrl
[retry_ct
]=c
;
186 retry_timer
.expires
=jiffies
+1;
187 add_timer(&retry_timer
);
192 /* Create a scsi error for this */
193 current_command
= (Scsi_Cmnd
*)m
[3];
194 printk("Aborted %ld\n", current_command
->serial_number
);
196 spin_lock_irq(&io_request_lock
);
197 current_command
->result
= DID_ERROR
<< 16;
198 current_command
->scsi_done(current_command
);
199 spin_unlock_irq(&io_request_lock
);
201 /* Now flush the message by making it a NOP */
203 m
[0]|=(I2O_CMD_UTIL_NOP
)<<24;
204 i2o_post_message(c
,virt_to_bus(m
));
211 * Low byte is device status, next is adapter status,
212 * (then one byte reserved), then request status.
218 dprintk(("i2o got a scsi reply %08X: ", m
[0]));
219 dprintk(("m[2]=%08X: ", m
[2]));
220 dprintk(("m[4]=%08X\n", m
[4]));
226 dprintk(("Event.\n"));
230 printk(KERN_ERR
"i2o_scsi: bus reset reply.\n");
234 current_command
= (Scsi_Cmnd
*)m
[3];
237 * Is this a control request coming back - eg an abort ?
240 if(current_command
==NULL
)
243 dprintk(("SCSI abort: %08X", m
[4]));
244 dprintk(("SCSI abort completed.\n"));
248 dprintk(("Completed %ld\n", current_command
->serial_number
));
250 atomic_dec(&queue_depth
);
254 if(m
[5] < current_command
->underflow
)
257 printk(KERN_ERR
"SCSI: underflow 0x%08X 0x%08X\n",
258 m
[5], current_command
->underflow
);
261 printk("%02X ", current_command
->cmnd
[i
]);
269 /* An error has occured */
271 dprintk((KERN_DEBUG
"SCSI error %08X", m
[4]));
275 current_command
->result
= DID_RESET
<< 16;
277 current_command
->result
= DID_PARITY
<< 16;
279 current_command
->result
= DID_ERROR
<< 16;
285 current_command
->result
= DID_OK
<< 16 | ds
;
286 spin_lock(&io_request_lock
);
287 current_command
->scsi_done(current_command
);
288 spin_unlock(&io_request_lock
);
292 struct i2o_handler i2o_scsi_handler
=
297 I2O_CLASS_SCSI_PERIPHERAL
300 static int i2o_find_lun(struct i2o_controller
*c
, struct i2o_device
*d
, int *target
, int *lun
)
304 if(i2o_query_scalar(c
, d
->id
, scsi_context
|0x40000000,
305 0, 3, reply
, 4, &lun_done
)<0)
310 if(i2o_query_scalar(c
, d
->id
, scsi_context
|0x40000000,
311 0, 4, reply
, 8, &lun_done
)<0)
316 dprintk(("SCSI (%d,%d)\n", *target
, *lun
));
320 void i2o_scsi_init(struct i2o_controller
*c
, struct i2o_device
*d
, struct Scsi_Host
*shpnt
)
322 struct i2o_device
*unit
;
323 struct i2o_scsi_host
*h
=(struct i2o_scsi_host
*)shpnt
->hostdata
;
330 for(target
=0;target
<16;target
++)
331 for(lun
=0;lun
<8;lun
++)
332 h
->task
[target
][lun
] = -1;
334 for(unit
=c
->devices
;unit
!=NULL
;unit
=unit
->next
)
336 dprintk(("Class %03X, parent %d, want %d.\n",
337 unit
->class, unit
->parent
, d
->id
));
339 /* Only look at scsi and fc devices */
340 if ( (unit
->class != I2O_CLASS_SCSI_PERIPHERAL
)
341 && (unit
->class != I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL
)
346 dprintk(("Found a disk.\n"));
347 if ( (unit
->parent
== d
->id
)
348 || (unit
->parent
== d
->parent
)
352 dprintk(("Its ours.\n"));
353 if(i2o_find_lun(c
, unit
, &target
, &lun
)==-1)
355 printk(KERN_ERR
"i2o_scsi: Unable to get lun for tid %d.\n", d
->id
);
358 dprintk(("Found disk %d %d.\n", target
, lun
));
359 h
->task
[target
][lun
]=unit
->id
;
360 h
->tagclock
[target
][lun
]=jiffies
;
362 /* Get the max fragments/request */
363 i2o_query_scalar(c
, d
->id
, scsi_context
|0x40000000,
364 0xF103, 3, &limit
, 2, &lun_done
);
369 printk(KERN_WARNING
"i2o_scsi: Ignoring unreasonable SG limit of 0 from IOP!\n");
373 shpnt
->sg_tablesize
= limit
;
375 dprintk(("i2o_scsi: set scatter-gather to %d.\n",
376 shpnt
->sg_tablesize
));
381 int i2o_scsi_detect(Scsi_Host_Template
* tpnt
)
384 struct Scsi_Host
*shpnt
= NULL
;
388 printk("i2o_scsi.c: %s\n", VERSION_STRING
);
390 if(i2o_install_handler(&i2o_scsi_handler
)<0)
392 printk(KERN_ERR
"i2o_scsi: Unable to install OSM handler.\n");
395 scsi_context
= i2o_scsi_handler
.context
;
397 if((sg_chain_pool
= kmalloc(SG_CHAIN_POOL_SZ
, GFP_KERNEL
)) == NULL
)
399 printk("i2o_scsi: Unable to alloc %d byte SG chain buffer pool.\n", SG_CHAIN_POOL_SZ
);
400 printk("i2o_scsi: SG chaining DISABLED!\n");
405 printk(" chain_pool: %d bytes @ %p\n", SG_CHAIN_POOL_SZ
, sg_chain_pool
);
406 printk(" (%d byte buffers X %d can_queue X %d i2o controllers)\n",
407 SG_CHAIN_BUF_SZ
, I2O_SCSI_CAN_QUEUE
, i2o_num_controllers
);
408 sg_max_frags
= SG_MAX_FRAGS
; // 64
411 init_timer(&retry_timer
);
412 retry_timer
.data
= 0UL;
413 retry_timer
.function
= i2o_retry_run
;
415 // printk("SCSI OSM at %d.\n", scsi_context);
417 for (count
= 0, i
= 0; i
< MAX_I2O_CONTROLLERS
; i
++)
419 struct i2o_controller
*c
=i2o_find_controller(i
);
420 struct i2o_device
*d
;
422 * This controller doesn't exist.
429 * Fixme - we need some altered device locking. This
430 * is racing with device addition in theory. Easy to fix.
433 for(d
=c
->devices
;d
!=NULL
;d
=d
->next
)
436 * bus_adapter, SCSI (obsolete), or FibreChannel busses only
438 if( (d
->class!=I2O_CLASS_BUS_ADAPTER_PORT
) // bus_adapter
439 && (d
->class!=I2O_CLASS_FIBRE_CHANNEL_PORT
) // FC_PORT
443 shpnt
= scsi_register(tpnt
, sizeof(struct i2o_scsi_host
));
446 shpnt
->unique_id
= (u32
)d
;
448 shpnt
->n_io_port
= 0;
450 shpnt
->this_id
= /* Good question */15;
451 restore_flags(flags
);
452 i2o_scsi_init(c
, d
, shpnt
);
456 i2o_scsi_hosts
= count
;
460 if(sg_chain_pool
!=NULL
)
462 kfree(sg_chain_pool
);
463 sg_chain_pool
= NULL
;
466 del_timer(&retry_timer
);
467 i2o_remove_handler(&i2o_scsi_handler
);
473 int i2o_scsi_release(struct Scsi_Host
*host
)
475 if(--i2o_scsi_hosts
==0)
477 if(sg_chain_pool
!=NULL
)
479 kfree(sg_chain_pool
);
480 sg_chain_pool
= NULL
;
483 del_timer(&retry_timer
);
484 i2o_remove_handler(&i2o_scsi_handler
);
490 const char *i2o_scsi_info(struct Scsi_Host
*SChost
)
492 struct i2o_scsi_host
*hostdata
;
494 hostdata
= (struct i2o_scsi_host
*)SChost
->hostdata
;
496 return(&hostdata
->controller
->name
[0]);
501 * From the wd93 driver:
502 * Returns true if there will be a DATA_OUT phase with this command,
504 * (Thanks to Joerg Dorchain for the research and suggestion.)
507 static int is_dir_out(Scsi_Cmnd
*cmd
)
509 switch (cmd
->cmnd
[0])
511 case WRITE_6
: case WRITE_10
: case WRITE_12
:
512 case WRITE_LONG
: case WRITE_SAME
: case WRITE_BUFFER
:
513 case WRITE_VERIFY
: case WRITE_VERIFY_12
:
514 case COMPARE
: case COPY
: case COPY_VERIFY
:
515 case SEARCH_EQUAL
: case SEARCH_HIGH
: case SEARCH_LOW
:
516 case SEARCH_EQUAL_12
: case SEARCH_HIGH_12
: case SEARCH_LOW_12
:
517 case FORMAT_UNIT
: case REASSIGN_BLOCKS
: case RESERVE
:
518 case MODE_SELECT
: case MODE_SELECT_10
: case LOG_SELECT
:
519 case SEND_DIAGNOSTIC
: case CHANGE_DEFINITION
: case UPDATE_BLOCK
:
520 case SET_WINDOW
: case MEDIUM_SCAN
: case SEND_VOLUME_TAG
:
528 int i2o_scsi_queuecommand(Scsi_Cmnd
* SCpnt
, void (*done
) (Scsi_Cmnd
*))
532 struct i2o_controller
*c
;
533 Scsi_Cmnd
*current_command
;
534 struct Scsi_Host
*host
;
535 struct i2o_scsi_host
*hostdata
;
545 static int max_qd
= 1;
548 * Do the incoming paperwork
552 hostdata
= (struct i2o_scsi_host
*)host
->hostdata
;
553 SCpnt
->scsi_done
= done
;
555 if(SCpnt
->target
> 15)
557 printk(KERN_ERR
"i2o_scsi: Wild target %d.\n", SCpnt
->target
);
561 tid
= hostdata
->task
[SCpnt
->target
][SCpnt
->lun
];
563 dprintk(("qcmd: Tid = %d\n", tid
));
565 current_command
= SCpnt
; /* set current command */
566 current_command
->scsi_done
= done
; /* set ptr to done function */
568 /* We don't have such a device. Pretend we did the command
569 and that selection timed out */
573 SCpnt
->result
= DID_NO_CONNECT
<< 16;
578 dprintk(("Real scsi messages.\n"));
580 c
= hostdata
->controller
;
583 * Obtain an I2O message. Right now we _have_ to obtain one
584 * until the scsi layer stuff is cleaned up.
590 m
= I2O_POST_READ32(c
);
592 while(m
==0xFFFFFFFF);
593 msg
= bus_to_virt(c
->mem_offset
+ m
);
596 * Put together a scsi execscb message
599 len
= SCpnt
->request_bufflen
;
600 direction
= 0x00000000; // SGL IN (osm<--iop)
603 * The scsi layer should be handling this stuff
606 scsidir
= 0x00000000; // DATA NO XFER
609 if(is_dir_out(SCpnt
))
611 direction
=0x04000000; // SGL OUT (osm-->iop)
612 scsidir
=0x80000000; // DATA OUT (iop-->dev)
616 scsidir
=0x40000000; // DATA IN (iop<--dev)
620 msg
[1] = I2O_CMD_SCSI_EXEC
<<24|HOST_TID
<<12|tid
;
621 msg
[2] = scsi_context
; /* So the I2O layer passes to us */
622 /* Sorry 64bit folks. FIXME */
623 msg
[3] = (u32
)SCpnt
; /* We want the SCSI control block back */
627 * Intermittant observations of msg frame word data corruption
628 * observed on msg[4] after:
629 * WRITE, READ-MODIFY-WRITE
630 * operations. 19990606 -sralston
632 * (Hence we build this word via tag. Its good practice anyway
633 * we don't want fetches over PCI needlessly)
639 * Attach tags to the devices
641 if(SCpnt
->device
->tagged_supported
)
644 * Some drives are too stupid to handle fairness issues
645 * with tagged queueing. We throw in the odd ordered
646 * tag to stop them starving themselves.
648 if((jiffies
- hostdata
->tagclock
[SCpnt
->target
][SCpnt
->lun
]) > (5*HZ
))
650 tag
=0x01800000; /* ORDERED! */
651 hostdata
->tagclock
[SCpnt
->target
][SCpnt
->lun
]=jiffies
;
655 /* Hmmm... I always see value of 0 here,
656 * of which {HEAD_OF, ORDERED, SIMPLE} are NOT! -sralston
658 if(SCpnt
->tag
== HEAD_OF_QUEUE_TAG
)
660 else if(SCpnt
->tag
== ORDERED_QUEUE_TAG
)
665 /* Direction, disconnect ok, tag, CDBLen */
666 msg
[4] = scsidir
|0x20000000|SCpnt
->cmd_len
|tag
;
671 * Write SCSI command into the message - always 16 byte block
674 memcpy(mptr
, SCpnt
->cmnd
, 16);
676 lenptr
=mptr
++; /* Remember me - fill in when we know */
678 reqlen
= 12; // SINGLE SGE
680 * Now fill in the SGList and command
682 * FIXME: we need to set the sglist limits according to the
683 * message size of the I2O controller. We might only have room
684 * for 6 or so worst case
689 struct scatterlist
*sg
= (struct scatterlist
*)SCpnt
->request_buffer
;
692 if((sg_max_frags
> 11) && (SCpnt
->use_sg
> 11))
698 *mptr
++=direction
|0xB0000000|(SCpnt
->use_sg
*2*4);
699 *mptr
=virt_to_bus(sg_chain_pool
+ sg_chain_tag
);
700 mptr
= (u32
*)(sg_chain_pool
+ sg_chain_tag
);
701 if (SCpnt
->use_sg
> max_sg_len
)
703 max_sg_len
= SCpnt
->use_sg
;
704 printk("i2o_scsi: Chain SG! SCpnt=%p, SG_FragCnt=%d, SG_idx=%d\n",
705 SCpnt
, SCpnt
->use_sg
, sg_chain_tag
);
707 if ( ++sg_chain_tag
== SG_MAX_BUFS
)
713 for(i
= 0 ; i
< SCpnt
->use_sg
; i
++)
715 *mptr
++=direction
|0x10000000|sg
->length
;
717 *mptr
++=virt_to_bus(sg
->address
);
721 /* Make this an end of list. Again evade the 920 bug and
722 unwanted PCI read traffic */
724 mptr
[-2]=direction
|0xD0000000|(sg
-1)->length
;
730 if(len
!= SCpnt
->underflow
)
731 printk("Cmd len %08X Cmd underflow %08X\n",
732 len
, SCpnt
->underflow
);
736 dprintk(("non sg for %p, %d\n", SCpnt
->request_buffer
,
737 SCpnt
->request_bufflen
));
738 *lenptr
= len
= SCpnt
->request_bufflen
;
745 *mptr
++=0xD0000000|direction
|SCpnt
->request_bufflen
;
746 *mptr
++=virt_to_bus(SCpnt
->request_buffer
);
751 * Stick the headers on
754 msg
[0] = reqlen
<<16 | SGL_OFFSET_10
;
756 /* Queue the message */
757 i2o_post_message(c
,m
);
759 atomic_inc(&queue_depth
);
761 if(atomic_read(&queue_depth
)> max_qd
)
763 max_qd
=atomic_read(&queue_depth
);
764 printk("Queue depth now %d.\n", max_qd
);
768 dprintk(("Issued %ld\n", current_command
->serial_number
));
773 static void internal_done(Scsi_Cmnd
* SCpnt
)
778 int i2o_scsi_command(Scsi_Cmnd
* SCpnt
)
780 i2o_scsi_queuecommand(SCpnt
, internal_done
);
781 SCpnt
->SCp
.Status
= 0;
782 while (!SCpnt
->SCp
.Status
)
784 return SCpnt
->result
;
787 int i2o_scsi_abort(Scsi_Cmnd
* SCpnt
)
789 struct i2o_controller
*c
;
790 struct Scsi_Host
*host
;
791 struct i2o_scsi_host
*hostdata
;
796 printk("i2o_scsi: Aborting command block.\n");
799 hostdata
= (struct i2o_scsi_host
*)host
->hostdata
;
800 tid
= hostdata
->task
[SCpnt
->target
][SCpnt
->lun
];
803 printk(KERN_ERR
"impossible command to abort.\n");
804 return SCSI_ABORT_NOT_RUNNING
;
806 c
= hostdata
->controller
;
809 * Obtain an I2O message. Right now we _have_ to obtain one
810 * until the scsi layer stuff is cleaned up.
816 m
= I2O_POST_READ32(c
);
818 while(m
==0xFFFFFFFF);
819 msg
= bus_to_virt(c
->mem_offset
+ m
);
821 msg
[0] = FIVE_WORD_MSG_SIZE
;
822 msg
[1] = I2O_CMD_SCSI_ABORT
<<24|HOST_TID
<<12|tid
;
823 msg
[2] = scsi_context
;
824 msg
[3] = 0; /* Not needed for an abort */
827 i2o_post_message(c
,m
);
829 return SCSI_ABORT_PENDING
;
832 int i2o_scsi_reset(Scsi_Cmnd
* SCpnt
, unsigned int reset_flags
)
835 struct i2o_controller
*c
;
836 struct Scsi_Host
*host
;
837 struct i2o_scsi_host
*hostdata
;
842 * Find the TID for the bus
845 printk("i2o_scsi: Attempting to reset the bus.\n");
848 hostdata
= (struct i2o_scsi_host
*)host
->hostdata
;
849 tid
= hostdata
->bus_task
;
850 c
= hostdata
->controller
;
853 * Now send a SCSI reset request. Any remaining commands
854 * will be aborted by the IOP. We need to catch the reply
858 m
= I2O_POST_READ32(c
);
861 * No free messages, try again next time - no big deal
865 return SCSI_RESET_PUNT
;
867 msg
= bus_to_virt(c
->mem_offset
+ m
);
868 msg
[0] = FOUR_WORD_MSG_SIZE
|SGL_OFFSET_0
;
869 msg
[1] = I2O_CMD_SCSI_BUSRESET
<<24|HOST_TID
<<12|tid
;
870 msg
[2] = scsi_context
|0x80000000;
871 /* We use the top bit to split controller and unit transactions */
872 /* Now store unit,tid so we can tie the completion back to a specific device */
873 msg
[3] = c
->unit
<< 16 | tid
;
874 i2o_post_message(c
,m
);
875 return SCSI_RESET_PENDING
;
879 * This is anyones guess quite frankly.
882 int i2o_scsi_bios_param(Disk
* disk
, kdev_t dev
, int *ip
)
886 size
= disk
->capacity
;
887 ip
[0] = 64; /* heads */
888 ip
[1] = 32; /* sectors */
889 if ((ip
[2] = size
>> 11) > 1024) { /* cylinders, test for big disk */
890 ip
[0] = 255; /* heads */
891 ip
[1] = 63; /* sectors */
892 ip
[2] = size
/ (255 * 63); /* cylinders */
897 /* Loadable module support */
900 MODULE_AUTHOR("Red Hat Software");
902 Scsi_Host_Template driver_template
= I2OSCSI
;
904 #include "../scsi/scsi_module.c"