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