include upstream ip1000a driver version 2.09f
[linux-2.4.git] / drivers / message / i2o / i2o_scsi.c
1 /* 
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
5  *  later version.
6  *
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.
11  *
12  *  Complications for I2O scsi
13  *
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
18  *              setup time.
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.
22  *      
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.
25  *
26  *      Fixes:
27  *              Steve Ralston   :       Scatter gather now works
28  *
29  *      To Do
30  *              64bit cleanups
31  *              Fix the resource management problems.
32  */
33
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>
44 #include <linux/prefetch.h>
45 #include <asm/dma.h>
46 #include <asm/system.h>
47 #include <asm/io.h>
48 #include <asm/atomic.h>
49 #include <linux/blk.h>
50 #include <linux/version.h>
51 #include <linux/i2o.h>
52 #include "../../scsi/scsi.h"
53 #include "../../scsi/hosts.h"
54 #include "../../scsi/sd.h"
55 #include "i2o_scsi.h"
56
57 #define VERSION_STRING        "Version 0.0.1"
58
59 #define dprintk(x)
60
61 #define MAXHOSTS 32
62
63 struct i2o_scsi_host
64 {
65         struct i2o_controller *controller;
66         s16 task[16][8];                /* Allow 16 devices for now */
67         unsigned long tagclock[16][8];  /* Tag clock for queueing */
68         s16 bus_task;           /* The adapter TID */
69 };
70
71 static int scsi_context;
72 static int lun_done;
73 static int i2o_scsi_hosts;
74
75 static u32 *retry[32];
76 static struct i2o_controller *retry_ctrl[32];
77 static struct timer_list retry_timer;
78 static int retry_ct = 0;
79
80 static atomic_t queue_depth;
81
82 /*
83  *      SG Chain buffer support...
84  */
85
86 #define SG_MAX_FRAGS            64
87
88 /*
89  *      FIXME: we should allocate one of these per bus we find as we
90  *      locate them not in a lump at boot.
91  */
92  
93 typedef struct _chain_buf
94 {
95         u32 sg_flags_cnt[SG_MAX_FRAGS];
96         u32 sg_buf[SG_MAX_FRAGS];
97 } chain_buf;
98
99 #define SG_CHAIN_BUF_SZ sizeof(chain_buf)
100
101 #define SG_MAX_BUFS             (i2o_num_controllers * I2O_SCSI_CAN_QUEUE)
102 #define SG_CHAIN_POOL_SZ        (SG_MAX_BUFS * SG_CHAIN_BUF_SZ)
103
104 static int max_sg_len = 0;
105 static chain_buf *sg_chain_pool = NULL;
106 static int sg_chain_tag = 0;
107 static int sg_max_frags = SG_MAX_FRAGS;
108
109 /*
110  *      Retry congested frames. This actually needs pushing down into
111  *      i2o core. We should only bother the OSM with this when we can't
112  *      queue and retry the frame. Or perhaps we should call the OSM
113  *      and its default handler should be this in the core, and this
114  *      call a 2nd "I give up" handler in the OSM ?
115  */
116  
117 static void i2o_retry_run(unsigned long f)
118 {
119         int i;
120         unsigned long flags;
121         
122         spin_lock_irqsave(&io_request_lock, flags);
123         for(i=0;i<retry_ct;i++)
124                 i2o_post_message(retry_ctrl[i], virt_to_bus(retry[i]));
125         retry_ct=0;
126         
127         spin_unlock_irqrestore(&io_request_lock, flags);
128 }
129
130 static void flush_pending(void)
131 {
132         int i;
133         unsigned long flags;
134         
135         spin_lock_irqsave(&io_request_lock, flags);
136
137         for(i=0;i<retry_ct;i++)
138         {
139                 retry[i][0]&=~0xFFFFFF;
140                 retry[i][0]|=I2O_CMD_UTIL_NOP<<24;
141                 i2o_post_message(retry_ctrl[i],virt_to_bus(retry[i]));
142         }
143         retry_ct=0;
144         
145         spin_unlock_irqrestore(&io_request_lock, flags);
146 }
147
148 static void i2o_scsi_reply(struct i2o_handler *h, struct i2o_controller *c, struct i2o_message *msg)
149 {
150         Scsi_Cmnd *current_command;
151         u32 *m = (u32 *)msg;
152         u8 as,ds,st;
153
154         spin_lock_prefetch(&io_request_lock);           
155
156         if(m[0] & (1<<13))
157         {
158                 printk("IOP fail.\n");
159                 printk("From %d To %d Cmd %d.\n",
160                         (m[1]>>12)&0xFFF,
161                         m[1]&0xFFF,
162                         m[1]>>24);
163                 printk("Failure Code %d.\n", m[4]>>24);
164                 if(m[4]&(1<<16))
165                         printk("Format error.\n");
166                 if(m[4]&(1<<17))
167                         printk("Path error.\n");
168                 if(m[4]&(1<<18))
169                         printk("Path State.\n");
170                 if(m[4]&(1<<18))
171                         printk("Congestion.\n");
172                 
173                 m=(u32 *)bus_to_virt(m[7]);
174                 printk("Failing message is %p.\n", m);
175                 
176                 if((m[4]&(1<<18)) && retry_ct < 32)
177                 {
178                         retry_ctrl[retry_ct]=c;
179                         retry[retry_ct]=m;
180                         if(!retry_ct++)
181                         {
182                                 retry_timer.expires=jiffies+1;
183                                 add_timer(&retry_timer);
184                         }
185                 }
186                 else
187                 {
188                         /* Create a scsi error for this */
189                         current_command = (Scsi_Cmnd *)m[3];
190                         printk("Aborted %ld\n", current_command->serial_number);
191
192                         spin_lock_irq(&io_request_lock);                        
193                         current_command->result = DID_ERROR << 16;
194                         current_command->scsi_done(current_command);
195                         spin_unlock_irq(&io_request_lock);                      
196                         
197                         /* Now flush the message by making it a NOP */
198                         m[0]&=0x00FFFFFF;
199                         m[0]|=(I2O_CMD_UTIL_NOP)<<24;
200                         i2o_post_message(c,virt_to_bus(m));
201                 }
202                 return;
203         }
204         
205         prefetchw(&queue_depth);
206                 
207         
208         /*
209          *      Low byte is device status, next is adapter status,
210          *      (then one byte reserved), then request status.
211          */
212         ds=(u8)le32_to_cpu(m[4]); 
213         as=(u8)le32_to_cpu(m[4]>>8);
214         st=(u8)le32_to_cpu(m[4]>>24);
215         
216         dprintk(("i2o got a scsi reply %08X: ", m[0]));
217         dprintk(("m[2]=%08X: ", m[2]));
218         dprintk(("m[4]=%08X\n", m[4]));
219
220         if(m[2]&0x80000000)
221         {
222                 if(m[2]&0x40000000)
223                 {
224                         dprintk(("Event.\n"));
225                         lun_done=1;
226                         return;
227                 }
228                 printk(KERN_ERR "i2o_scsi: bus reset reply.\n");
229                 return;
230         }
231
232         /*
233          *      FIXME: 64bit breakage
234          */
235         current_command = (Scsi_Cmnd *)m[3];
236         
237         /*
238          *      Is this a control request coming back - eg an abort ?
239          */
240          
241         if(current_command==NULL)
242         {
243                 if(st)
244                         dprintk(("SCSI abort: %08X", m[4]));
245                 dprintk(("SCSI abort completed.\n"));
246                 return;
247         }
248         
249         dprintk(("Completed %ld\n", current_command->serial_number));
250         
251         atomic_dec(&queue_depth);
252         
253         if(st == 0x06)
254         {
255                 if(le32_to_cpu(m[5]) < current_command->underflow)
256                 {
257                         int i;
258                         printk(KERN_ERR "SCSI: underflow 0x%08X 0x%08X\n",
259                                 le32_to_cpu(m[5]), current_command->underflow);
260                         printk("Cmd: ");
261                         for(i=0;i<15;i++)
262                                 printk("%02X ", current_command->cmnd[i]);
263                         printk(".\n");
264                 }
265                 else st=0;
266         }
267         
268         if(st)
269         {
270                 /* An error has occurred */
271
272                 dprintk((KERN_DEBUG "SCSI error %08X", m[4]));
273                         
274                 if (as == 0x0E) 
275                         /* SCSI Reset */
276                         current_command->result = DID_RESET << 16;
277                 else if (as == 0x0F)
278                         current_command->result = DID_PARITY << 16;
279                 else
280                         current_command->result = DID_ERROR << 16;
281         }
282         else
283                 /*
284                  *      It worked maybe ?
285                  */             
286                 current_command->result = DID_OK << 16 | ds;
287         spin_lock(&io_request_lock);
288         current_command->scsi_done(current_command);
289         spin_unlock(&io_request_lock);
290         return;
291 }
292
293 struct i2o_handler i2o_scsi_handler=
294 {
295         i2o_scsi_reply,
296         NULL,
297         NULL,
298         NULL,
299         "I2O SCSI OSM",
300         0,
301         I2O_CLASS_SCSI_PERIPHERAL
302 };
303
304 static int i2o_find_lun(struct i2o_controller *c, struct i2o_device *d, int *target, int *lun)
305 {
306         u8 reply[8];
307         
308         if(i2o_query_scalar(c, d->lct_data.tid, 0, 3, reply, 4)<0)
309                 return -1;
310                 
311         *target=reply[0];
312         
313         if(i2o_query_scalar(c, d->lct_data.tid, 0, 4, reply, 8)<0)
314                 return -1;
315
316         *lun=reply[1];
317
318         dprintk(("SCSI (%d,%d)\n", *target, *lun));
319         return 0;
320 }
321
322 static void i2o_scsi_init(struct i2o_controller *c, struct i2o_device *d, struct Scsi_Host *shpnt)
323 {
324         struct i2o_device *unit;
325         struct i2o_scsi_host *h =(struct i2o_scsi_host *)shpnt->hostdata;
326         int lun;
327         int target;
328         
329         h->controller=c;
330         h->bus_task=d->lct_data.tid;
331         
332         for(target=0;target<16;target++)
333                 for(lun=0;lun<8;lun++)
334                         h->task[target][lun] = -1;
335                         
336         for(unit=c->devices;unit!=NULL;unit=unit->next)
337         {
338                 dprintk(("Class %03X, parent %d, want %d.\n",
339                         unit->lct_data.class_id, unit->lct_data.parent_tid, d->lct_data.tid));
340                         
341                 /* Only look at scsi and fc devices */
342                 if (    (unit->lct_data.class_id != I2O_CLASS_SCSI_PERIPHERAL)
343                      && (unit->lct_data.class_id != I2O_CLASS_FIBRE_CHANNEL_PERIPHERAL)
344                    )
345                         continue;
346
347                 /* On our bus ? */
348                 dprintk(("Found a disk (%d).\n", unit->lct_data.tid));
349                 if ((unit->lct_data.parent_tid == d->lct_data.tid)
350                      || (unit->lct_data.parent_tid == d->lct_data.parent_tid)
351                    )
352                 {
353                         u16 limit;
354                         dprintk(("Its ours.\n"));
355                         if(i2o_find_lun(c, unit, &target, &lun)==-1)
356                         {
357                                 printk(KERN_ERR "i2o_scsi: Unable to get lun for tid %d.\n", unit->lct_data.tid);
358                                 continue;
359                         }
360                         dprintk(("Found disk %d %d.\n", target, lun));
361                         h->task[target][lun]=unit->lct_data.tid;
362                         h->tagclock[target][lun]=jiffies;
363
364                         /* Get the max fragments/request */
365                         i2o_query_scalar(c, d->lct_data.tid, 0xF103, 3, &limit, 2);
366                         
367                         /* sanity */
368                         if ( limit == 0 )
369                         {
370                                 printk(KERN_WARNING "i2o_scsi: Ignoring unreasonable SG limit of 0 from IOP!\n");
371                                 limit = 1;
372                         }
373                         
374                         shpnt->sg_tablesize = limit;
375
376                         dprintk(("i2o_scsi: set scatter-gather to %d.\n", 
377                                 shpnt->sg_tablesize));
378                 }
379         }               
380 }
381
382 static int i2o_scsi_detect(Scsi_Host_Template * tpnt)
383 {
384         unsigned long flags;
385         struct Scsi_Host *shpnt = NULL;
386         int i;
387         int count;
388
389         printk("i2o_scsi.c: %s\n", VERSION_STRING);
390
391         if(i2o_install_handler(&i2o_scsi_handler)<0)
392         {
393                 printk(KERN_ERR "i2o_scsi: Unable to install OSM handler.\n");
394                 return 0;
395         }
396         scsi_context = i2o_scsi_handler.context;
397         
398         if((sg_chain_pool = kmalloc(SG_CHAIN_POOL_SZ, GFP_KERNEL)) == NULL)
399         {
400                 printk("i2o_scsi: Unable to alloc %d byte SG chain buffer pool.\n", SG_CHAIN_POOL_SZ);
401                 printk("i2o_scsi: SG chaining DISABLED!\n");
402                 sg_max_frags = 11;
403         }
404         else
405         {
406                 printk("  chain_pool: %d bytes @ %p\n", SG_CHAIN_POOL_SZ, sg_chain_pool);
407                 printk("  (%d byte buffers X %d can_queue X %d i2o controllers)\n",
408                                 SG_CHAIN_BUF_SZ, I2O_SCSI_CAN_QUEUE, i2o_num_controllers);
409                 sg_max_frags = SG_MAX_FRAGS;    // 64
410         }
411         
412         init_timer(&retry_timer);
413         retry_timer.data = 0UL;
414         retry_timer.function = i2o_retry_run;
415         
416 //      printk("SCSI OSM at %d.\n", scsi_context);
417
418         for (count = 0, i = 0; i < MAX_I2O_CONTROLLERS; i++)
419         {
420                 struct i2o_controller *c=i2o_find_controller(i);
421                 struct i2o_device *d;
422                 /*
423                  *      This controller doesn't exist.
424                  */
425                 
426                 if(c==NULL)
427                         continue;
428                         
429                 /*
430                  *      Fixme - we need some altered device locking. This
431                  *      is racing with device addition in theory. Easy to fix.
432                  */
433                 
434                 for(d=c->devices;d!=NULL;d=d->next)
435                 {
436                         /*
437                          *      bus_adapter, SCSI (obsolete), or FibreChannel busses only
438                          */
439                         if(    (d->lct_data.class_id!=I2O_CLASS_BUS_ADAPTER_PORT)       // bus_adapter
440 //                          && (d->lct_data.class_id!=I2O_CLASS_FIBRE_CHANNEL_PORT)     // FC_PORT
441                           )
442                                 continue;
443                 
444                         shpnt = scsi_register(tpnt, sizeof(struct i2o_scsi_host));
445                         if(shpnt==NULL)
446                                 continue;
447                         save_flags(flags);
448                         cli();
449                         shpnt->unique_id = (u32)d;
450                         shpnt->io_port = 0;
451                         shpnt->n_io_port = 0;
452                         shpnt->irq = 0;
453                         shpnt->this_id = /* Good question */15;
454                         restore_flags(flags);
455                         i2o_scsi_init(c, d, shpnt);
456                         count++;
457                 }
458         }
459         i2o_scsi_hosts = count;
460         
461         if(count==0)
462         {
463                 if(sg_chain_pool!=NULL)
464                 {
465                         kfree(sg_chain_pool);
466                         sg_chain_pool = NULL;
467                 }
468                 flush_pending();
469                 del_timer(&retry_timer);
470                 i2o_remove_handler(&i2o_scsi_handler);
471         }
472         
473         return count;
474 }
475
476 static int i2o_scsi_release(struct Scsi_Host *host)
477 {
478         if(--i2o_scsi_hosts==0)
479         {
480                 if(sg_chain_pool!=NULL)
481                 {
482                         kfree(sg_chain_pool);
483                         sg_chain_pool = NULL;
484                 }
485                 flush_pending();
486                 del_timer(&retry_timer);
487                 i2o_remove_handler(&i2o_scsi_handler);
488         }
489         return 0;
490 }
491
492
493 static const char *i2o_scsi_info(struct Scsi_Host *SChost)
494 {
495         struct i2o_scsi_host *hostdata;
496         hostdata = (struct i2o_scsi_host *)SChost->hostdata;
497         return(&hostdata->controller->name[0]);
498 }
499
500 static int i2o_scsi_queuecommand(Scsi_Cmnd * SCpnt, void (*done) (Scsi_Cmnd *))
501 {
502         int i;
503         int tid;
504         struct i2o_controller *c;
505         Scsi_Cmnd *current_command;
506         struct Scsi_Host *host;
507         struct i2o_scsi_host *hostdata;
508         u32 *msg, *mptr;
509         u32 m;
510         u32 *lenptr;
511         int direction;
512         int scsidir;
513         u32 len;
514         u32 reqlen;
515         u32 tag;
516         
517         static int max_qd = 1;
518         
519         /*
520          *      Do the incoming paperwork
521          */
522          
523         host = SCpnt->host;
524         hostdata = (struct i2o_scsi_host *)host->hostdata;
525          
526         c = hostdata->controller;
527         prefetch(c);
528         prefetchw(&queue_depth);
529
530         SCpnt->scsi_done = done;
531         
532         if(SCpnt->target > 15)
533         {
534                 printk(KERN_ERR "i2o_scsi: Wild target %d.\n", SCpnt->target);
535                 return -1;
536         }
537         
538         tid = hostdata->task[SCpnt->target][SCpnt->lun];
539         
540         dprintk(("qcmd: Tid = %d\n", tid));
541         
542         current_command = SCpnt;                /* set current command                */
543         current_command->scsi_done = done;      /* set ptr to done function           */
544
545         /* We don't have such a device. Pretend we did the command 
546            and that selection timed out */
547         
548         if(tid == -1)
549         {
550                 SCpnt->result = DID_NO_CONNECT << 16;
551                 done(SCpnt);
552                 return 0;
553         }
554         
555         dprintk(("Real scsi messages.\n"));
556
557         
558         /*
559          *      Obtain an I2O message. Right now we _have_ to obtain one
560          *      until the scsi layer stuff is cleaned up.
561          */     
562          
563         do
564         {
565                 mb();
566                 m = le32_to_cpu(I2O_POST_READ32(c));
567         }
568         while(m==0xFFFFFFFF);
569
570         msg = (u32 *)(c->mem_offset + m);
571         
572         /*
573          *      Put together a scsi execscb message
574          */
575         
576         len = SCpnt->request_bufflen;
577         direction = 0x00000000;                 // SGL IN  (osm<--iop)
578         
579         if(SCpnt->sc_data_direction == SCSI_DATA_NONE)
580                 scsidir = 0x00000000;                   // DATA NO XFER
581         else if(SCpnt->sc_data_direction == SCSI_DATA_WRITE)
582         {
583                 direction=0x04000000;   // SGL OUT  (osm-->iop)
584                 scsidir  =0x80000000;   // DATA OUT (iop-->dev)
585         }
586         else if(SCpnt->sc_data_direction == SCSI_DATA_READ)
587         {
588                 scsidir  =0x40000000;   // DATA IN  (iop<--dev)
589         }
590         else
591         {
592                 /* Unknown - kill the command */
593                 SCpnt->result = DID_NO_CONNECT << 16;
594                 done(SCpnt);
595                 return 0;
596         }
597         
598         i2o_raw_writel(I2O_CMD_SCSI_EXEC<<24|HOST_TID<<12|tid, &msg[1]);
599         i2o_raw_writel(scsi_context, &msg[2]);  /* So the I2O layer passes to us */
600         /* Sorry 64bit folks. FIXME */
601         i2o_raw_writel((u32)SCpnt, &msg[3]);    /* We want the SCSI control block back */
602
603         /* LSI_920_PCI_QUIRK
604          *
605          *      Intermittant observations of msg frame word data corruption
606          *      observed on msg[4] after:
607          *        WRITE, READ-MODIFY-WRITE
608          *      operations.  19990606 -sralston
609          *
610          *      (Hence we build this word via tag. Its good practice anyway
611          *       we don't want fetches over PCI needlessly)
612          */
613
614         tag=0;
615         
616         /*
617          *      Attach tags to the devices
618          */     
619         if(SCpnt->device->tagged_supported)
620         {
621                 /*
622                  *      Some drives are too stupid to handle fairness issues
623                  *      with tagged queueing. We throw in the odd ordered
624                  *      tag to stop them starving themselves.
625                  */
626                 if((jiffies - hostdata->tagclock[SCpnt->target][SCpnt->lun]) > (5*HZ))
627                 {
628                         tag=0x01800000;         /* ORDERED! */
629                         hostdata->tagclock[SCpnt->target][SCpnt->lun]=jiffies;
630                 }
631                 else
632                 {
633                         /* Hmmm...  I always see value of 0 here,
634                          *  of which {HEAD_OF, ORDERED, SIMPLE} are NOT!  -sralston
635                          */
636                         if(SCpnt->tag == HEAD_OF_QUEUE_TAG)
637                                 tag=0x01000000;
638                         else if(SCpnt->tag == ORDERED_QUEUE_TAG)
639                                 tag=0x01800000;
640                 }
641         }
642
643         /* Direction, disconnect ok, tag, CDBLen */
644         i2o_raw_writel(scsidir|0x20000000|SCpnt->cmd_len|tag, &msg[4]);
645
646         mptr=msg+5;
647
648         /* 
649          *      Write SCSI command into the message - always 16 byte block 
650          */
651          
652         memcpy_toio(mptr, SCpnt->cmnd, 16);
653         mptr+=4;
654         lenptr=mptr++;          /* Remember me - fill in when we know */
655         
656         reqlen = 12;            // SINGLE SGE
657         
658         /*
659          *      Now fill in the SGList and command 
660          *
661          *      FIXME: we need to set the sglist limits according to the 
662          *      message size of the I2O controller. We might only have room
663          *      for 6 or so worst case
664          *
665          *      FIXME: pci dma mapping
666          */
667         
668         if(SCpnt->use_sg)
669         {
670                 struct scatterlist *sg = (struct scatterlist *)SCpnt->request_buffer;
671                 int chain = 0;
672                 
673                 len = 0;
674
675                 if((sg_max_frags > 11) && (SCpnt->use_sg > 11))
676                 {
677                         chain = 1;
678                         /*
679                          *      Need to chain!
680                          */
681                         i2o_raw_writel(direction|0xB0000000|(SCpnt->use_sg*2*4), mptr++);
682                         i2o_raw_writel(virt_to_bus(sg_chain_pool + sg_chain_tag), mptr);
683                         mptr = (u32*)(sg_chain_pool + sg_chain_tag);
684                         if (SCpnt->use_sg > max_sg_len)
685                         {
686                                 max_sg_len = SCpnt->use_sg;
687                                 printk("i2o_scsi: Chain SG! SCpnt=%p, SG_FragCnt=%d, SG_idx=%d\n",
688                                         SCpnt, SCpnt->use_sg, sg_chain_tag);
689                         }
690                         if ( ++sg_chain_tag == SG_MAX_BUFS )
691                                 sg_chain_tag = 0;
692                         for(i = 0 ; i < SCpnt->use_sg; i++)
693                         {
694                                 *mptr++=direction|0x10000000|sg->length;
695                                 len+=sg->length;
696                                 *mptr++=virt_to_bus(sg->address);
697                                 sg++;
698                         }
699                         mptr[-2]=direction|0xD0000000|(sg-1)->length;
700                 }
701                 else
702                 {               
703                         for(i = 0 ; i < SCpnt->use_sg; i++)
704                         {
705                                 i2o_raw_writel(direction|0x10000000|sg->length, mptr++);
706                                 len+=sg->length;
707                                 i2o_raw_writel(virt_to_bus(sg->address), mptr++);
708                                 sg++;
709                         }
710
711                         /* Make this an end of list. Again evade the 920 bug and
712                            unwanted PCI read traffic */
713                 
714                         i2o_raw_writel(direction|0xD0000000|(sg-1)->length, &mptr[-2]);
715                 }
716                 
717                 if(!chain)
718                         reqlen = mptr - msg;
719                 
720                 i2o_raw_writel(len, lenptr);
721                 
722                 if(len != SCpnt->underflow)
723                         printk("Cmd len %08X Cmd underflow %08X\n",
724                                 len, SCpnt->underflow);
725         }
726         else
727         {
728                 dprintk(("non sg for %p, %d\n", SCpnt->request_buffer,
729                                 SCpnt->request_bufflen));
730                 i2o_raw_writel(len = SCpnt->request_bufflen, lenptr);
731                 if(len == 0)
732                 {
733                         reqlen = 9;
734                 }
735                 else
736                 {
737                         i2o_raw_writel(0xD0000000|direction|SCpnt->request_bufflen, mptr++);
738                         i2o_raw_writel(virt_to_bus(SCpnt->request_buffer), mptr++);
739                 }
740         }
741         
742         /*
743          *      Stick the headers on 
744          */
745
746         i2o_raw_writel(reqlen<<16 | SGL_OFFSET_10, msg);
747         
748         /* Queue the message */
749         i2o_post_message(c,m);
750         
751         atomic_inc(&queue_depth);
752         
753         if(atomic_read(&queue_depth)> max_qd)
754         {
755                 max_qd=atomic_read(&queue_depth);
756                 printk("Queue depth now %d.\n", max_qd);
757         }
758         
759         mb();
760         dprintk(("Issued %ld\n", current_command->serial_number));
761         
762         return 0;
763 }
764
765 static void internal_done(Scsi_Cmnd * SCpnt)
766 {
767         SCpnt->SCp.Status++;
768 }
769
770 static int i2o_scsi_command(Scsi_Cmnd * SCpnt)
771 {
772         i2o_scsi_queuecommand(SCpnt, internal_done);
773         SCpnt->SCp.Status = 0;
774         while (!SCpnt->SCp.Status)
775                 barrier();
776         return SCpnt->result;
777 }
778
779 static int i2o_scsi_abort(Scsi_Cmnd * SCpnt)
780 {
781         struct i2o_controller *c;
782         struct Scsi_Host *host;
783         struct i2o_scsi_host *hostdata;
784         unsigned long msg;
785         u32 m;
786         int tid;
787         
788         printk("i2o_scsi: Aborting command block.\n");
789         
790         host = SCpnt->host;
791         hostdata = (struct i2o_scsi_host *)host->hostdata;
792         tid = hostdata->task[SCpnt->target][SCpnt->lun];
793         if(tid==-1)
794         {
795                 printk(KERN_ERR "impossible command to abort.\n");
796                 return SCSI_ABORT_NOT_RUNNING;
797         }
798         c = hostdata->controller;
799         
800         /*
801          *      Obtain an I2O message. Right now we _have_ to obtain one
802          *      until the scsi layer stuff is cleaned up.
803          */     
804          
805         do
806         {
807                 mb();
808                 m = le32_to_cpu(I2O_POST_READ32(c));
809         }
810         while(m==0xFFFFFFFF);
811         msg = c->mem_offset + m;
812         
813         i2o_raw_writel(FIVE_WORD_MSG_SIZE, msg);
814         i2o_raw_writel(I2O_CMD_SCSI_ABORT<<24|HOST_TID<<12|tid, msg+4);
815         i2o_raw_writel(scsi_context, msg+8);
816         i2o_raw_writel(0, msg+12);      /* Not needed for an abort */
817         i2o_raw_writel((u32)SCpnt, msg+16);     /* FIXME 32bitism */
818         wmb();
819         i2o_post_message(c,m);
820         wmb();
821         return SCSI_ABORT_PENDING;
822 }
823
824 static int i2o_scsi_reset(Scsi_Cmnd * SCpnt, unsigned int reset_flags)
825 {
826         int tid;
827         struct i2o_controller *c;
828         struct Scsi_Host *host;
829         struct i2o_scsi_host *hostdata;
830         u32 m;
831         unsigned long msg;
832
833         /*
834          *      Find the TID for the bus
835          */
836
837         printk("i2o_scsi: Attempting to reset the bus.\n");
838         
839         host = SCpnt->host;
840         hostdata = (struct i2o_scsi_host *)host->hostdata;
841         tid = hostdata->bus_task;
842         c = hostdata->controller;
843
844         /*
845          *      Now send a SCSI reset request. Any remaining commands
846          *      will be aborted by the IOP. We need to catch the reply
847          *      possibly ?
848          */
849          
850         m = le32_to_cpu(I2O_POST_READ32(c));
851         
852         /*
853          *      No free messages, try again next time - no big deal
854          */
855          
856         if(m == 0xFFFFFFFF)
857                 return SCSI_RESET_PUNT;
858         
859         msg = c->mem_offset + m;
860         i2o_raw_writel(FOUR_WORD_MSG_SIZE|SGL_OFFSET_0, msg);
861         i2o_raw_writel(I2O_CMD_SCSI_BUSRESET<<24|HOST_TID<<12|tid, msg+4);
862         i2o_raw_writel(scsi_context|0x80000000, msg+8);
863         /* We use the top bit to split controller and unit transactions */
864         /* Now store unit,tid so we can tie the completion back to a specific device */
865         i2o_raw_writel(c->unit << 16 | tid, msg+12);
866         wmb();
867         i2o_post_message(c,m);
868         return SCSI_RESET_PENDING;
869 }
870
871 /*
872  *      This is anyones guess quite frankly.
873  */
874  
875 static int i2o_scsi_bios_param(Disk * disk, kdev_t dev, int *ip)
876 {
877         int size;
878
879         size = disk->capacity;
880         ip[0] = 64;             /* heads                        */
881         ip[1] = 32;             /* sectors                      */
882         if ((ip[2] = size >> 11) > 1024) {      /* cylinders, test for big disk */
883                 ip[0] = 255;    /* heads                        */
884                 ip[1] = 63;     /* sectors                      */
885                 ip[2] = size / (255 * 63);      /* cylinders                    */
886         }
887         return 0;
888 }
889
890 MODULE_AUTHOR("Red Hat Software");
891 MODULE_LICENSE("GPL");
892
893
894 static Scsi_Host_Template driver_template = I2OSCSI;
895
896 #include "../../scsi/scsi_module.c"