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