ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / scsi / arm / fas216.c
1 /*
2  *  linux/drivers/acorn/scsi/fas216.c
3  *
4  *  Copyright (C) 1997-2003 Russell King
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  *
10  * Based on information in qlogicfas.c by Tom Zerucha, Michael Griffith, and
11  * other sources, including:
12  *   the AMD Am53CF94 data sheet
13  *   the AMD Am53C94 data sheet
14  *
15  * This is a generic driver.  To use it, have a look at cumana_2.c.  You
16  * should define your own structure that overlays FAS216_Info, eg:
17  * struct my_host_data {
18  *    FAS216_Info info;
19  *    ... my host specific data ...
20  * };
21  *
22  * Changelog:
23  *  30-08-1997  RMK     Created
24  *  14-09-1997  RMK     Started disconnect support
25  *  08-02-1998  RMK     Corrected real DMA support
26  *  15-02-1998  RMK     Started sync xfer support
27  *  06-04-1998  RMK     Tightened conditions for printing incomplete
28  *                      transfers
29  *  02-05-1998  RMK     Added extra checks in fas216_reset
30  *  24-05-1998  RMK     Fixed synchronous transfers with period >= 200ns
31  *  27-06-1998  RMK     Changed asm/delay.h to linux/delay.h
32  *  26-08-1998  RMK     Improved message support wrt MESSAGE_REJECT
33  *  02-04-2000  RMK     Converted to use the new error handling, and
34  *                      automatically request sense data upon check
35  *                      condition status from targets.
36  */
37 #include <linux/module.h>
38 #include <linux/blkdev.h>
39 #include <linux/kernel.h>
40 #include <linux/string.h>
41 #include <linux/ioport.h>
42 #include <linux/sched.h>
43 #include <linux/proc_fs.h>
44 #include <linux/delay.h>
45 #include <linux/bitops.h>
46 #include <linux/init.h>
47 #include <linux/interrupt.h>
48
49 #include <asm/dma.h>
50 #include <asm/io.h>
51 #include <asm/irq.h>
52 #include <asm/ecard.h>
53
54 #include "../scsi.h"
55 #include "../hosts.h"
56 #include "fas216.h"
57 #include "scsi.h"
58
59 /* NOTE: SCSI2 Synchronous transfers *require* DMA according to
60  *  the data sheet.  This restriction is crazy, especially when
61  *  you only want to send 16 bytes!  What were the guys who
62  *  designed this chip on at that time?  Did they read the SCSI2
63  *  spec at all?  The following sections are taken from the SCSI2
64  *  standard (s2r10) concerning this:
65  *
66  * > IMPLEMENTORS NOTES:
67  * >   (1)  Re-negotiation at every selection is not recommended, since a
68  * >   significant performance impact is likely.
69  *
70  * >  The implied synchronous agreement shall remain in effect until a BUS DEVICE
71  * >  RESET message is received, until a hard reset condition occurs, or until one
72  * >  of the two SCSI devices elects to modify the agreement.  The default data
73  * >  transfer mode is asynchronous data transfer mode.  The default data transfer
74  * >  mode is entered at power on, after a BUS DEVICE RESET message, or after a hard
75  * >  reset condition.
76  *
77  *  In total, this means that once you have elected to use synchronous
78  *  transfers, you must always use DMA.
79  *
80  *  I was thinking that this was a good chip until I found this restriction ;(
81  */
82 #define SCSI2_SYNC
83 #undef  SCSI2_TAG
84
85 #undef DEBUG_CONNECT
86 #undef DEBUG_MESSAGES
87
88 #undef CHECK_STRUCTURE
89
90 #define LOG_CONNECT             (1 << 0)
91 #define LOG_BUSSERVICE          (1 << 1)
92 #define LOG_FUNCTIONDONE        (1 << 2)
93 #define LOG_MESSAGES            (1 << 3)
94 #define LOG_BUFFER              (1 << 4)
95 #define LOG_ERROR               (1 << 8)
96
97 static int level_mask = LOG_ERROR;
98
99 MODULE_PARM(level_mask, "i");
100
101 static int __init fas216_log_setup(char *str)
102 {
103         char *s;
104
105         level_mask = 0;
106
107         while ((s = strsep(&str, ",")) != NULL) {
108                 switch (s[0]) {
109                 case 'a':
110                         if (strcmp(s, "all") == 0)
111                                 level_mask |= -1;
112                         break;
113                 case 'b':
114                         if (strncmp(s, "bus", 3) == 0)
115                                 level_mask |= LOG_BUSSERVICE;
116                         if (strncmp(s, "buf", 3) == 0)
117                                 level_mask |= LOG_BUFFER;
118                         break;
119                 case 'c':
120                         level_mask |= LOG_CONNECT;
121                         break;
122                 case 'e':
123                         level_mask |= LOG_ERROR;
124                         break;
125                 case 'm':
126                         level_mask |= LOG_MESSAGES;
127                         break;
128                 case 'n':
129                         if (strcmp(s, "none") == 0)
130                                 level_mask = 0;
131                         break;
132                 case 's':
133                         level_mask |= LOG_FUNCTIONDONE;
134                         break;
135                 }
136         }
137         return 1;
138 }
139
140 __setup("fas216_logging=", fas216_log_setup);
141
142 static inline unsigned char fas216_readb(FAS216_Info *info, unsigned int reg)
143 {
144         unsigned int off = reg << info->scsi.io_shift;
145         if (info->scsi.io_base)
146                 return readb(info->scsi.io_base + off);
147         else
148                 return inb(info->scsi.io_port + off);
149 }
150
151 static inline void fas216_writeb(FAS216_Info *info, unsigned int reg, unsigned int val)
152 {
153         unsigned int off = reg << info->scsi.io_shift;
154         if (info->scsi.io_base)
155                 writeb(val, info->scsi.io_base + off);
156         else
157                 outb(val, info->scsi.io_port + off);
158 }
159
160 static void fas216_dumpstate(FAS216_Info *info)
161 {
162         unsigned char is, stat, inst;
163
164         is   = fas216_readb(info, REG_IS);
165         stat = fas216_readb(info, REG_STAT);
166         inst = fas216_readb(info, REG_INST);
167         
168         printk("FAS216: CTCL=%02X CTCM=%02X CMD=%02X STAT=%02X"
169                " INST=%02X IS=%02X CFIS=%02X",
170                 fas216_readb(info, REG_CTCL),
171                 fas216_readb(info, REG_CTCM),
172                 fas216_readb(info, REG_CMD),  stat, inst, is,
173                 fas216_readb(info, REG_CFIS));
174         printk(" CNTL1=%02X CNTL2=%02X CNTL3=%02X CTCH=%02X\n",
175                 fas216_readb(info, REG_CNTL1),
176                 fas216_readb(info, REG_CNTL2),
177                 fas216_readb(info, REG_CNTL3),
178                 fas216_readb(info, REG_CTCH));
179 }
180
181 static void print_SCp(Scsi_Pointer *SCp, const char *prefix, const char *suffix)
182 {
183         printk("%sptr %p this_residual 0x%x buffer %p buffers_residual 0x%x%s",
184                 prefix, SCp->ptr, SCp->this_residual, SCp->buffer,
185                 SCp->buffers_residual, suffix);
186 }
187
188 static void fas216_dumpinfo(FAS216_Info *info)
189 {
190         static int used = 0;
191         int i;
192
193         if (used++)
194                 return;
195
196         printk("FAS216_Info=\n");
197         printk("  { magic_start=%lX host=%p SCpnt=%p origSCpnt=%p\n",
198                 info->magic_start, info->host, info->SCpnt,
199                 info->origSCpnt);
200         printk("    scsi={ io_port=%X io_shift=%X irq=%X cfg={ %X %X %X %X }\n",
201                 info->scsi.io_port, info->scsi.io_shift, info->scsi.irq,
202                 info->scsi.cfg[0], info->scsi.cfg[1], info->scsi.cfg[2],
203                 info->scsi.cfg[3]);
204         printk("           type=%p phase=%X\n",
205                 info->scsi.type, info->scsi.phase);
206         print_SCp(&info->scsi.SCp, "           SCp={ ", " }\n");
207         printk("      msgs async_stp=%X disconnectable=%d aborting=%d }\n",
208                 info->scsi.async_stp,
209                 info->scsi.disconnectable, info->scsi.aborting);
210         printk("    stats={ queues=%X removes=%X fins=%X reads=%X writes=%X miscs=%X\n"
211                "            disconnects=%X aborts=%X bus_resets=%X host_resets=%X}\n",
212                 info->stats.queues, info->stats.removes, info->stats.fins,
213                 info->stats.reads, info->stats.writes, info->stats.miscs,
214                 info->stats.disconnects, info->stats.aborts, info->stats.bus_resets,
215                 info->stats.host_resets);
216         printk("    ifcfg={ clockrate=%X select_timeout=%X asyncperiod=%X sync_max_depth=%X }\n",
217                 info->ifcfg.clockrate, info->ifcfg.select_timeout,
218                 info->ifcfg.asyncperiod, info->ifcfg.sync_max_depth);
219         for (i = 0; i < 8; i++) {
220                 printk("    busyluns[%d]=%08lx dev[%d]={ disconnect_ok=%d stp=%X sof=%X sync_state=%X }\n",
221                         i, info->busyluns[i], i,
222                         info->device[i].disconnect_ok, info->device[i].stp,
223                         info->device[i].sof, info->device[i].sync_state);
224         }
225         printk("    dma={ transfer_type=%X setup=%p pseudo=%p stop=%p }\n",
226                 info->dma.transfer_type, info->dma.setup,
227                 info->dma.pseudo, info->dma.stop);
228         printk("    internal_done=%X magic_end=%lX }\n",
229                 info->internal_done, info->magic_end);
230 }
231
232 #ifdef CHECK_STRUCTURE
233 static void __fas216_checkmagic(FAS216_Info *info, const char *func)
234 {
235         int corruption = 0;
236         if (info->magic_start != MAGIC) {
237                 printk(KERN_CRIT "FAS216 Error: magic at start corrupted\n");
238                 corruption++;
239         }
240         if (info->magic_end != MAGIC) {
241                 printk(KERN_CRIT "FAS216 Error: magic at end corrupted\n");
242                 corruption++;
243         }
244         if (corruption) {
245                 fas216_dumpinfo(info);
246                 panic("scsi memory space corrupted in %s", func);
247         }
248 }
249 #define fas216_checkmagic(info) __fas216_checkmagic((info), __FUNCTION__)
250 #else
251 #define fas216_checkmagic(info)
252 #endif
253
254 static const char *fas216_bus_phase(int stat)
255 {
256         static const char *phases[] = {
257                 "DATA OUT", "DATA IN",
258                 "COMMAND", "STATUS",
259                 "MISC OUT", "MISC IN",
260                 "MESG OUT", "MESG IN"
261         };
262
263         return phases[stat & STAT_BUSMASK];
264 }
265
266 static const char *fas216_drv_phase(FAS216_Info *info)
267 {
268         static const char *phases[] = {
269                 [PHASE_IDLE]            = "idle",
270                 [PHASE_SELECTION]       = "selection",
271                 [PHASE_COMMAND]         = "command",
272                 [PHASE_DATAOUT]         = "data out",
273                 [PHASE_DATAIN]          = "data in",
274                 [PHASE_MSGIN]           = "message in",
275                 [PHASE_MSGIN_DISCONNECT]= "disconnect",
276                 [PHASE_MSGOUT_EXPECT]   = "expect message out",
277                 [PHASE_MSGOUT]          = "message out",
278                 [PHASE_STATUS]          = "status",
279                 [PHASE_DONE]            = "done",
280         };
281
282         if (info->scsi.phase < ARRAY_SIZE(phases) &&
283             phases[info->scsi.phase])
284                 return phases[info->scsi.phase];
285         return "???";
286 }
287
288 static char fas216_target(FAS216_Info *info)
289 {
290         if (info->SCpnt)
291                 return '0' + info->SCpnt->device->id;
292         else
293                 return 'H';
294 }
295
296 static void
297 fas216_do_log(FAS216_Info *info, char target, char *fmt, va_list ap)
298 {
299         static char buf[1024];
300
301         vsnprintf(buf, sizeof(buf), fmt, ap);
302         printk("scsi%d.%c: %s", info->host->host_no, target, buf);
303 }
304
305 static void
306 fas216_log_command(FAS216_Info *info, int level, Scsi_Cmnd *SCpnt, char *fmt, ...)
307 {
308         va_list args;
309
310         if (level != 0 && !(level & level_mask))
311                 return;
312
313         va_start(args, fmt);
314         fas216_do_log(info, '0' + SCpnt->device->id, fmt, args);
315         va_end(args);
316
317         printk(" CDB: ");
318         print_command(SCpnt->cmnd);
319 }
320
321 static void
322 fas216_log_target(FAS216_Info *info, int level, int target, char *fmt, ...)
323 {
324         va_list args;
325
326         if (level != 0 && !(level & level_mask))
327                 return;
328
329         if (target < 0)
330                 target = 'H';
331         else
332                 target += '0';
333
334         va_start(args, fmt);
335         fas216_do_log(info, target, fmt, args);
336         va_end(args);
337
338         printk("\n");
339 }
340
341 static void fas216_log(FAS216_Info *info, int level, char *fmt, ...)
342 {
343         va_list args;
344
345         if (level != 0 && !(level & level_mask))
346                 return;
347
348         va_start(args, fmt);
349         fas216_do_log(info, fas216_target(info), fmt, args);
350         va_end(args);
351
352         printk("\n");
353 }
354
355 #define PH_SIZE 32
356
357 static struct { int stat, ssr, isr, ph; } ph_list[PH_SIZE];
358 static int ph_ptr;
359
360 static void add_debug_list(int stat, int ssr, int isr, int ph)
361 {
362         ph_list[ph_ptr].stat = stat;
363         ph_list[ph_ptr].ssr = ssr;
364         ph_list[ph_ptr].isr = isr;
365         ph_list[ph_ptr].ph = ph;
366
367         ph_ptr = (ph_ptr + 1) & (PH_SIZE-1);
368 }
369
370 static struct { int command; void *from; } cmd_list[8];
371 static int cmd_ptr;
372
373 static void fas216_cmd(FAS216_Info *info, unsigned int command)
374 {
375         cmd_list[cmd_ptr].command = command;
376         cmd_list[cmd_ptr].from = __builtin_return_address(0);
377
378         cmd_ptr = (cmd_ptr + 1) & 7;
379
380         fas216_writeb(info, REG_CMD, command);
381 }
382
383 static void print_debug_list(void)
384 {
385         int i;
386
387         i = ph_ptr;
388
389         printk(KERN_ERR "SCSI IRQ trail\n");
390         do {
391                 printk(" %02x:%02x:%02x:%1x",
392                         ph_list[i].stat, ph_list[i].ssr,
393                         ph_list[i].isr, ph_list[i].ph);
394                 i = (i + 1) & (PH_SIZE - 1);
395                 if (((i ^ ph_ptr) & 7) == 0)
396                         printk("\n");
397         } while (i != ph_ptr);
398         if ((i ^ ph_ptr) & 7)
399                 printk("\n");
400
401         i = cmd_ptr;
402         printk(KERN_ERR "FAS216 commands: ");
403         do {
404                 printk("%02x:%p ", cmd_list[i].command, cmd_list[i].from);
405                 i = (i + 1) & 7;
406         } while (i != cmd_ptr);
407         printk("\n");
408 }
409
410 static void fas216_done(FAS216_Info *info, unsigned int result);
411
412 /**
413  * fas216_get_last_msg - retrive last message from the list
414  * @info: interface to search
415  * @pos: current fifo position
416  *
417  * Retrieve a last message from the list, using position in fifo.
418  */
419 static inline unsigned short
420 fas216_get_last_msg(FAS216_Info *info, int pos)
421 {
422         unsigned short packed_msg = NOP;
423         struct message *msg;
424         int msgnr = 0;
425
426         while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
427                 if (pos >= msg->fifo)
428                         break;
429         }
430
431         if (msg) {
432                 if (msg->msg[0] == EXTENDED_MESSAGE)
433                         packed_msg = EXTENDED_MESSAGE | msg->msg[2] << 8;
434                 else
435                         packed_msg = msg->msg[0];
436         }
437
438         fas216_log(info, LOG_MESSAGES,
439                 "Message: %04x found at position %02x\n", packed_msg, pos);
440
441         return packed_msg;
442 }
443
444 /**
445  * fas216_syncperiod - calculate STP register value
446  * @info: state structure for interface connected to device
447  * @ns: period in ns (between subsequent bytes)
448  *
449  * Calculate value to be loaded into the STP register for a given period
450  * in ns. Returns a value suitable for REG_STP.
451  */
452 static int fas216_syncperiod(FAS216_Info *info, int ns)
453 {
454         int value = (info->ifcfg.clockrate * ns) / 1000;
455
456         fas216_checkmagic(info);
457
458         if (value < 4)
459                 value = 4;
460         else if (value > 35)
461                 value = 35;
462
463         return value & 31;
464 }
465
466 /**
467  * fas216_set_sync - setup FAS216 chip for specified transfer period.
468  * @info: state structure for interface connected to device
469  * @target: target
470  *
471  * Correctly setup FAS216 chip for specified transfer period.
472  * Notes   : we need to switch the chip out of FASTSCSI mode if we have
473  *           a transfer period >= 200ns - otherwise the chip will violate
474  *           the SCSI timings.
475  */
476 static void fas216_set_sync(FAS216_Info *info, int target)
477 {
478         unsigned int cntl3;
479
480         fas216_writeb(info, REG_SOF, info->device[target].sof);
481         fas216_writeb(info, REG_STP, info->device[target].stp);
482
483         cntl3 = info->scsi.cfg[2];
484         if (info->device[target].period >= (200 / 4))
485                 cntl3 = cntl3 & ~CNTL3_FASTSCSI;
486
487         fas216_writeb(info, REG_CNTL3, cntl3);
488 }
489
490 /* Synchronous transfer support
491  *
492  * Note: The SCSI II r10 spec says (5.6.12):
493  *
494  *  (2)  Due to historical problems with early host adapters that could
495  *  not accept an SDTR message, some targets may not initiate synchronous
496  *  negotiation after a power cycle as required by this standard.  Host
497  *  adapters that support synchronous mode may avoid the ensuing failure
498  *  modes when the target is independently power cycled by initiating a
499  *  synchronous negotiation on each REQUEST SENSE and INQUIRY command.
500  *  This approach increases the SCSI bus overhead and is not recommended
501  *  for new implementations.  The correct method is to respond to an
502  *  SDTR message with a MESSAGE REJECT message if the either the
503  *  initiator or target devices does not support synchronous transfers
504  *  or does not want to negotiate for synchronous transfers at the time.
505  *  Using the correct method assures compatibility with wide data
506  *  transfers and future enhancements.
507  *
508  * We will always initiate a synchronous transfer negotiation request on
509  * every INQUIRY or REQUEST SENSE message, unless the target itself has
510  * at some point performed a synchronous transfer negotiation request, or
511  * we have synchronous transfers disabled for this device.
512  */
513
514 /**
515  * fas216_handlesync - Handle a synchronous transfer message
516  * @info: state structure for interface
517  * @msg: message from target
518  *
519  * Handle a synchronous transfer message from the target
520  */
521 static void fas216_handlesync(FAS216_Info *info, char *msg)
522 {
523         struct fas216_device *dev = &info->device[info->SCpnt->device->id];
524         enum { sync, async, none, reject } res = none;
525
526 #ifdef SCSI2_SYNC
527         switch (msg[0]) {
528         case MESSAGE_REJECT:
529                 /* Synchronous transfer request failed.
530                  * Note: SCSI II r10:
531                  *
532                  *  SCSI devices that are capable of synchronous
533                  *  data transfers shall not respond to an SDTR
534                  *  message with a MESSAGE REJECT message.
535                  *
536                  * Hence, if we get this condition, we disable
537                  * negotiation for this device.
538                  */
539                 if (dev->sync_state == neg_inprogress) {
540                         dev->sync_state = neg_invalid;
541                         res = async;
542                 }
543                 break;
544
545         case EXTENDED_MESSAGE:
546                 switch (dev->sync_state) {
547                 /* We don't accept synchronous transfer requests.
548                  * Respond with a MESSAGE_REJECT to prevent a
549                  * synchronous transfer agreement from being reached.
550                  */
551                 case neg_invalid:
552                         res = reject;
553                         break;
554
555                 /* We were not negotiating a synchronous transfer,
556                  * but the device sent us a negotiation request.
557                  * Honour the request by sending back a SDTR
558                  * message containing our capability, limited by
559                  * the targets capability.
560                  */
561                 default:
562                         fas216_cmd(info, CMD_SETATN);
563                         if (msg[4] > info->ifcfg.sync_max_depth)
564                                 msg[4] = info->ifcfg.sync_max_depth;
565                         if (msg[3] < 1000 / info->ifcfg.clockrate)
566                                 msg[3] = 1000 / info->ifcfg.clockrate;
567
568                         msgqueue_flush(&info->scsi.msgs);
569                         msgqueue_addmsg(&info->scsi.msgs, 5,
570                                         EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
571                                         msg[3], msg[4]);
572                         info->scsi.phase = PHASE_MSGOUT_EXPECT;
573
574                         /* This is wrong.  The agreement is not in effect
575                          * until this message is accepted by the device
576                          */
577                         dev->sync_state = neg_targcomplete;
578                         res = sync;
579                         break;
580
581                 /* We initiated the synchronous transfer negotiation,
582                  * and have successfully received a response from the
583                  * target.  The synchronous transfer agreement has been
584                  * reached.  Note: if the values returned are out of our
585                  * bounds, we must reject the message.
586                  */
587                 case neg_inprogress:
588                         res = reject;
589                         if (msg[4] <= info->ifcfg.sync_max_depth &&
590                             msg[3] >= 1000 / info->ifcfg.clockrate) {
591                                 dev->sync_state = neg_complete;
592                                 res = sync;
593                         }
594                         break;
595                 }
596         }
597 #else
598         res = reject;
599 #endif
600
601         switch (res) {
602         case sync:
603                 dev->period = msg[3];
604                 dev->sof    = msg[4];
605                 dev->stp    = fas216_syncperiod(info, msg[3] * 4);
606                 fas216_set_sync(info, info->SCpnt->device->id);
607                 break;
608
609         case reject:
610                 fas216_cmd(info, CMD_SETATN);
611                 msgqueue_flush(&info->scsi.msgs);
612                 msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
613                 info->scsi.phase = PHASE_MSGOUT_EXPECT;
614
615         case async:
616                 dev->period = info->ifcfg.asyncperiod / 4;
617                 dev->sof    = 0;
618                 dev->stp    = info->scsi.async_stp;
619                 fas216_set_sync(info, info->SCpnt->device->id);
620                 break;
621
622         case none:
623                 break;
624         }
625 }
626
627 /**
628  * fas216_updateptrs - update data pointers after transfer suspended/paused
629  * @info: interface's local pointer to update
630  * @bytes_transferred: number of bytes transferred
631  *
632  * Update data pointers after transfer suspended/paused
633  */
634 static void fas216_updateptrs(FAS216_Info *info, int bytes_transferred)
635 {
636         Scsi_Pointer *SCp = &info->scsi.SCp;
637
638         fas216_checkmagic(info);
639
640         BUG_ON(bytes_transferred < 0);
641
642         info->SCpnt->request_bufflen -= bytes_transferred;
643
644         while (bytes_transferred != 0) {
645                 if (SCp->this_residual > bytes_transferred)
646                         break;
647                 /*
648                  * We have used up this buffer.  Move on to the
649                  * next buffer.
650                  */
651                 bytes_transferred -= SCp->this_residual;
652                 if (!next_SCp(SCp) && bytes_transferred) {
653                         printk(KERN_WARNING "scsi%d.%c: out of buffers\n",
654                                 info->host->host_no, '0' + info->SCpnt->device->id);
655                         return;
656                 }
657         }
658
659         SCp->this_residual -= bytes_transferred;
660         if (SCp->this_residual)
661                 SCp->ptr += bytes_transferred;
662         else
663                 SCp->ptr = NULL;
664 }
665
666 /**
667  * fas216_pio - transfer data off of/on to card using programmed IO
668  * @info: interface to transfer data to/from
669  * @direction: direction to transfer data (DMA_OUT/DMA_IN)
670  *
671  * Transfer data off of/on to card using programmed IO.
672  * Notes: this is incredibly slow.
673  */
674 static void fas216_pio(FAS216_Info *info, fasdmadir_t direction)
675 {
676         Scsi_Pointer *SCp = &info->scsi.SCp;
677
678         fas216_checkmagic(info);
679
680         if (direction == DMA_OUT)
681                 fas216_writeb(info, REG_FF, get_next_SCp_byte(SCp));
682         else
683                 put_next_SCp_byte(SCp, fas216_readb(info, REG_FF));
684
685         if (SCp->this_residual == 0)
686                 next_SCp(SCp);
687 }
688
689 static void fas216_set_stc(FAS216_Info *info, unsigned int length)
690 {
691         fas216_writeb(info, REG_STCL, length);
692         fas216_writeb(info, REG_STCM, length >> 8);
693         fas216_writeb(info, REG_STCH, length >> 16);
694 }
695
696 static unsigned int fas216_get_ctc(FAS216_Info *info)
697 {
698         return fas216_readb(info, REG_CTCL) +
699                (fas216_readb(info, REG_CTCM) << 8) +
700                (fas216_readb(info, REG_CTCH) << 16);
701 }
702
703 /**
704  * fas216_cleanuptransfer - clean up after a transfer has completed.
705  * @info: interface to clean up
706  *
707  * Update the data pointers according to the number of bytes transferred
708  * on the SCSI bus.
709  */
710 static void fas216_cleanuptransfer(FAS216_Info *info)
711 {
712         unsigned long total, residual, fifo;
713         fasdmatype_t dmatype = info->dma.transfer_type;
714
715         info->dma.transfer_type = fasdma_none;
716
717         /*
718          * PIO transfers do not need to be cleaned up.
719          */
720         if (dmatype == fasdma_pio || dmatype == fasdma_none)
721                 return;
722
723         if (dmatype == fasdma_real_all)
724                 total = info->SCpnt->request_bufflen;
725         else
726                 total = info->scsi.SCp.this_residual;
727
728         residual = fas216_get_ctc(info);
729
730         fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
731
732         fas216_log(info, LOG_BUFFER, "cleaning up from previous "
733                    "transfer: length 0x%06x, residual 0x%x, fifo %d",
734                    total, residual, fifo);
735
736         /*
737          * If we were performing Data-Out, the transfer counter
738          * counts down each time a byte is transferred by the
739          * host to the FIFO.  This means we must include the
740          * bytes left in the FIFO from the transfer counter.
741          */
742         if (info->scsi.phase == PHASE_DATAOUT)
743                 residual += fifo;
744
745         fas216_updateptrs(info, total - residual);
746 }
747
748 /**
749  * fas216_transfer - Perform a DMA/PIO transfer off of/on to card
750  * @info: interface from which device disconnected from
751  *
752  * Start a DMA/PIO transfer off of/on to card
753  */
754 static void fas216_transfer(FAS216_Info *info)
755 {
756         fasdmadir_t direction;
757         fasdmatype_t dmatype;
758
759         fas216_log(info, LOG_BUFFER,
760                    "starttransfer: buffer %p length 0x%06x reqlen 0x%06x",
761                    info->scsi.SCp.ptr, info->scsi.SCp.this_residual,
762                    info->SCpnt->request_bufflen);
763
764         if (!info->scsi.SCp.ptr) {
765                 fas216_log(info, LOG_ERROR, "null buffer passed to "
766                            "fas216_starttransfer");
767                 print_SCp(&info->scsi.SCp, "SCp: ", "\n");
768                 print_SCp(&info->SCpnt->SCp, "Cmnd SCp: ", "\n");
769                 return;
770         }
771
772         /*
773          * If we have a synchronous transfer agreement in effect, we must
774          * use DMA mode.  If we are using asynchronous transfers, we may
775          * use DMA mode or PIO mode.
776          */
777         if (info->device[info->SCpnt->device->id].sof)
778                 dmatype = fasdma_real_all;
779         else
780                 dmatype = fasdma_pio;
781
782         if (info->scsi.phase == PHASE_DATAOUT)
783                 direction = DMA_OUT;
784         else
785                 direction = DMA_IN;
786
787         if (info->dma.setup)
788                 dmatype = info->dma.setup(info->host, &info->scsi.SCp,
789                                           direction, dmatype);
790         info->dma.transfer_type = dmatype;
791
792         if (dmatype == fasdma_real_all)
793                 fas216_set_stc(info, info->SCpnt->request_bufflen);
794         else
795                 fas216_set_stc(info, info->scsi.SCp.this_residual);
796
797         switch (dmatype) {
798         case fasdma_pio:
799                 fas216_log(info, LOG_BUFFER, "PIO transfer");
800                 fas216_writeb(info, REG_SOF, 0);
801                 fas216_writeb(info, REG_STP, info->scsi.async_stp);
802                 fas216_cmd(info, CMD_TRANSFERINFO);
803                 fas216_pio(info, direction);
804                 break;
805
806         case fasdma_pseudo:
807                 fas216_log(info, LOG_BUFFER, "pseudo transfer");
808                 fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
809                 info->dma.pseudo(info->host, &info->scsi.SCp,
810                                  direction, info->SCpnt->transfersize);
811                 break;
812
813         case fasdma_real_block:
814                 fas216_log(info, LOG_BUFFER, "block dma transfer");
815                 fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
816                 break;
817
818         case fasdma_real_all:
819                 fas216_log(info, LOG_BUFFER, "total dma transfer");
820                 fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
821                 break;
822
823         default:
824                 fas216_log(info, LOG_BUFFER | LOG_ERROR,
825                            "invalid FAS216 DMA type");
826                 break;
827         }
828 }
829
830 /**
831  * fas216_stoptransfer - Stop a DMA transfer onto / off of the card
832  * @info: interface from which device disconnected from
833  *
834  * Called when we switch away from DATA IN or DATA OUT phases.
835  */
836 static void fas216_stoptransfer(FAS216_Info *info)
837 {
838         fas216_checkmagic(info);
839
840         if (info->dma.transfer_type == fasdma_real_all ||
841             info->dma.transfer_type == fasdma_real_block)
842                 info->dma.stop(info->host, &info->scsi.SCp);
843
844         fas216_cleanuptransfer(info);
845
846         if (info->scsi.phase == PHASE_DATAIN) {
847                 unsigned int fifo;
848
849                 /*
850                  * If we were performing Data-In, then the FIFO counter
851                  * contains the number of bytes not transferred via DMA
852                  * from the on-board FIFO.  Read them manually.
853                  */
854                 fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
855                 while (fifo && info->scsi.SCp.ptr) {
856                         *info->scsi.SCp.ptr = fas216_readb(info, REG_FF);
857                         fas216_updateptrs(info, 1);
858                         fifo--;
859                 }
860         } else {
861                 /*
862                  * After a Data-Out phase, there may be unsent
863                  * bytes left in the FIFO.  Flush them out.
864                  */
865                 fas216_cmd(info, CMD_FLUSHFIFO);
866         }
867 }
868
869 static void fas216_aborttransfer(FAS216_Info *info)
870 {
871         fas216_checkmagic(info);
872
873         if (info->dma.transfer_type == fasdma_real_all ||
874             info->dma.transfer_type == fasdma_real_block)
875                 info->dma.stop(info->host, &info->scsi.SCp);
876
877         info->dma.transfer_type = fasdma_none;
878         fas216_cmd(info, CMD_FLUSHFIFO);
879 }
880
881 static void fas216_kick(FAS216_Info *info);
882
883 /**
884  * fas216_disconnected_intr - handle device disconnection
885  * @info: interface from which device disconnected from
886  *
887  * Handle device disconnection
888  */
889 static void fas216_disconnect_intr(FAS216_Info *info)
890 {
891         unsigned long flags;
892
893         fas216_checkmagic(info);
894
895         fas216_log(info, LOG_CONNECT, "disconnect phase=%02x",
896                    info->scsi.phase);
897
898         msgqueue_flush(&info->scsi.msgs);
899
900         switch (info->scsi.phase) {
901         case PHASE_SELECTION:                   /* while selecting - no target          */
902         case PHASE_SELSTEPS:
903                 fas216_done(info, DID_NO_CONNECT);
904                 break;
905
906         case PHASE_MSGIN_DISCONNECT:            /* message in - disconnecting           */
907                 info->scsi.disconnectable = 1;
908                 info->scsi.phase = PHASE_IDLE;
909                 info->stats.disconnects += 1;
910                 spin_lock_irqsave(&info->host_lock, flags);
911                 if (info->scsi.phase == PHASE_IDLE)
912                         fas216_kick(info);
913                 spin_unlock_irqrestore(&info->host_lock, flags);
914                 break;
915
916         case PHASE_DONE:                        /* at end of command - complete         */
917                 fas216_done(info, DID_OK);
918                 break;
919
920         case PHASE_MSGOUT:                      /* message out - possible ABORT message */
921                 if (fas216_get_last_msg(info, info->scsi.msgin_fifo) == ABORT) {
922                         info->scsi.aborting = 0;
923                         fas216_done(info, DID_ABORT);
924                         break;
925                 }
926
927         default:                                /* huh?                                 */
928                 printk(KERN_ERR "scsi%d.%c: unexpected disconnect in phase %s\n",
929                         info->host->host_no, fas216_target(info), fas216_drv_phase(info));
930                 print_debug_list();
931                 fas216_stoptransfer(info);
932                 fas216_done(info, DID_ERROR);
933                 break;
934         }
935 }
936
937 /**
938  * fas216_reselected_intr - start reconnection of a device
939  * @info: interface which was reselected
940  *
941  * Start reconnection of a device
942  */
943 static void
944 fas216_reselected_intr(FAS216_Info *info)
945 {
946         unsigned int cfis, i;
947         unsigned char msg[4];
948         unsigned char target, lun, tag;
949
950         fas216_checkmagic(info);
951
952         WARN_ON(info->scsi.phase == PHASE_SELECTION ||
953                 info->scsi.phase == PHASE_SELSTEPS);
954
955         cfis = fas216_readb(info, REG_CFIS);
956
957         fas216_log(info, LOG_CONNECT, "reconnect phase=%02x cfis=%02x",
958                    info->scsi.phase, cfis);
959
960         cfis &= CFIS_CF;
961
962         if (cfis < 2 || cfis > 4) {
963                 printk(KERN_ERR "scsi%d.H: incorrect number of bytes after reselect\n",
964                         info->host->host_no);
965                 goto bad_message;
966         }
967
968         for (i = 0; i < cfis; i++)
969                 msg[i] = fas216_readb(info, REG_FF);
970
971         if (!(msg[0] & (1 << info->host->this_id)) ||
972             !(msg[1] & 0x80))
973                 goto initiator_error;
974
975         target = msg[0] & ~(1 << info->host->this_id);
976         target = ffs(target) - 1;
977         lun = msg[1] & 7;
978         tag = 0;
979
980         if (cfis >= 3) {
981                 if (msg[2] != SIMPLE_QUEUE_TAG)
982                         goto initiator_error;
983
984                 tag = msg[3];
985         }
986
987         /* set up for synchronous transfers */
988         fas216_writeb(info, REG_SDID, target);
989         fas216_set_sync(info, target);
990         msgqueue_flush(&info->scsi.msgs);
991
992         fas216_log(info, LOG_CONNECT, "Reconnected: target %1x lun %1x tag %02x",
993                    target, lun, tag);
994
995         if (info->scsi.disconnectable && info->SCpnt) {
996                 info->scsi.disconnectable = 0;
997                 if (info->SCpnt->device->id  == target &&
998                     info->SCpnt->device->lun == lun &&
999                     info->SCpnt->tag         == tag) {
1000                         fas216_log(info, LOG_CONNECT, "reconnected previously executing command");
1001                 } else {
1002                         queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
1003                         fas216_log(info, LOG_CONNECT, "had to move command to disconnected queue");
1004                         info->SCpnt = NULL;
1005                 }
1006         }
1007         if (!info->SCpnt) {
1008                 info->SCpnt = queue_remove_tgtluntag(&info->queues.disconnected,
1009                                         target, lun, tag);
1010                 fas216_log(info, LOG_CONNECT, "had to get command");
1011         }
1012
1013         if (info->SCpnt) {
1014                 /*
1015                  * Restore data pointer from SAVED data pointer
1016                  */
1017                 info->scsi.SCp = info->SCpnt->SCp;
1018
1019                 fas216_log(info, LOG_CONNECT, "data pointers: [%p, %X]",
1020                         info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
1021                 info->scsi.phase = PHASE_MSGIN;
1022         } else {
1023                 /*
1024                  * Our command structure not found - abort the
1025                  * command on the target.  Since we have no
1026                  * record of this command, we can't send
1027                  * an INITIATOR DETECTED ERROR message.
1028                  */
1029                 fas216_cmd(info, CMD_SETATN);
1030
1031 #if 0
1032                 if (tag)
1033                         msgqueue_addmsg(&info->scsi.msgs, 2, ABORT_TAG, tag);
1034                 else
1035 #endif
1036                         msgqueue_addmsg(&info->scsi.msgs, 1, ABORT);
1037                 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1038                 info->scsi.aborting = 1;
1039         }
1040
1041         fas216_cmd(info, CMD_MSGACCEPTED);
1042         return;
1043
1044  initiator_error:
1045         printk(KERN_ERR "scsi%d.H: error during reselection: bytes",
1046                 info->host->host_no);
1047         for (i = 0; i < cfis; i++)
1048                 printk(" %02x", msg[i]);
1049         printk("\n");
1050  bad_message:
1051         fas216_cmd(info, CMD_SETATN);
1052         msgqueue_flush(&info->scsi.msgs);
1053         msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
1054         info->scsi.phase = PHASE_MSGOUT_EXPECT;
1055         fas216_cmd(info, CMD_MSGACCEPTED);
1056 }
1057
1058 static void fas216_parse_message(FAS216_Info *info, unsigned char *message, int msglen)
1059 {
1060         int i;
1061
1062         switch (message[0]) {
1063         case COMMAND_COMPLETE:
1064                 if (msglen != 1)
1065                         goto unrecognised;
1066
1067                 printk(KERN_ERR "scsi%d.%c: command complete with no "
1068                         "status in MESSAGE_IN?\n",
1069                         info->host->host_no, fas216_target(info));
1070                 break;
1071
1072         case SAVE_POINTERS:
1073                 if (msglen != 1)
1074                         goto unrecognised;
1075
1076                 /*
1077                  * Save current data pointer to SAVED data pointer
1078                  * SCSI II standard says that we must not acknowledge
1079                  * this until we have really saved pointers.
1080                  * NOTE: we DO NOT save the command nor status pointers
1081                  * as required by the SCSI II standard.  These always
1082                  * point to the start of their respective areas.
1083                  */
1084                 info->SCpnt->SCp = info->scsi.SCp;
1085                 info->SCpnt->SCp.sent_command = 0;
1086                 fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
1087                         "save data pointers: [%p, %X]",
1088                         info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
1089                 break;
1090
1091         case RESTORE_POINTERS:
1092                 if (msglen != 1)
1093                         goto unrecognised;
1094
1095                 /*
1096                  * Restore current data pointer from SAVED data pointer
1097                  */
1098                 info->scsi.SCp = info->SCpnt->SCp;
1099                 fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
1100                         "restore data pointers: [%p, 0x%x]",
1101                         info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
1102                 break;
1103
1104         case DISCONNECT:
1105                 if (msglen != 1)
1106                         goto unrecognised;
1107
1108                 info->scsi.phase = PHASE_MSGIN_DISCONNECT;
1109                 break;
1110
1111         case MESSAGE_REJECT:
1112                 if (msglen != 1)
1113                         goto unrecognised;
1114
1115                 switch (fas216_get_last_msg(info, info->scsi.msgin_fifo)) {
1116                 case EXTENDED_MESSAGE | EXTENDED_SDTR << 8:
1117                         fas216_handlesync(info, message);
1118                         break;
1119
1120                 default:
1121                         fas216_log(info, 0, "reject, last message 0x%04x",
1122                                 fas216_get_last_msg(info, info->scsi.msgin_fifo));
1123                 }
1124                 break;
1125
1126         case NOP:
1127                 break;
1128
1129         case EXTENDED_MESSAGE:
1130                 if (msglen < 3)
1131                         goto unrecognised;
1132
1133                 switch (message[2]) {
1134                 case EXTENDED_SDTR:     /* Sync transfer negotiation request/reply */
1135                         fas216_handlesync(info, message);
1136                         break;
1137
1138                 default:
1139                         goto unrecognised;
1140                 }
1141                 break;
1142
1143         default:
1144                 goto unrecognised;
1145         }
1146         return;
1147
1148 unrecognised:
1149         fas216_log(info, 0, "unrecognised message, rejecting");
1150         printk("scsi%d.%c: message was", info->host->host_no, fas216_target(info));
1151         for (i = 0; i < msglen; i++)
1152                 printk("%s%02X", i & 31 ? " " : "\n  ", message[i]);
1153         printk("\n");
1154
1155         /*
1156          * Something strange seems to be happening here -
1157          * I can't use SETATN since the chip gives me an
1158          * invalid command interrupt when I do.  Weird.
1159          */
1160 fas216_cmd(info, CMD_NOP);
1161 fas216_dumpstate(info);
1162         fas216_cmd(info, CMD_SETATN);
1163         msgqueue_flush(&info->scsi.msgs);
1164         msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
1165         info->scsi.phase = PHASE_MSGOUT_EXPECT;
1166 fas216_dumpstate(info);
1167 }
1168
1169 static int fas216_wait_cmd(FAS216_Info *info, int cmd)
1170 {
1171         int tout;
1172         int stat;
1173
1174         fas216_cmd(info, cmd);
1175
1176         for (tout = 1000; tout; tout -= 1) {
1177                 stat = fas216_readb(info, REG_STAT);
1178                 if (stat & (STAT_INT|STAT_PARITYERROR))
1179                         break;
1180                 udelay(1);
1181         }
1182
1183         return stat;
1184 }
1185
1186 static int fas216_get_msg_byte(FAS216_Info *info)
1187 {
1188         unsigned int stat = fas216_wait_cmd(info, CMD_MSGACCEPTED);
1189
1190         if ((stat & STAT_INT) == 0)
1191                 goto timedout;
1192
1193         if ((stat & STAT_BUSMASK) != STAT_MESGIN)
1194                 goto unexpected_phase_change;
1195
1196         fas216_readb(info, REG_INST);
1197
1198         stat = fas216_wait_cmd(info, CMD_TRANSFERINFO);
1199
1200         if ((stat & STAT_INT) == 0)
1201                 goto timedout;
1202
1203         if (stat & STAT_PARITYERROR)
1204                 goto parity_error;
1205
1206         if ((stat & STAT_BUSMASK) != STAT_MESGIN)
1207                 goto unexpected_phase_change;
1208
1209         fas216_readb(info, REG_INST);
1210
1211         return fas216_readb(info, REG_FF);
1212
1213 timedout:
1214         fas216_log(info, LOG_ERROR, "timed out waiting for message byte");
1215         return -1;
1216
1217 unexpected_phase_change:
1218         fas216_log(info, LOG_ERROR, "unexpected phase change: status = %02x", stat);
1219         return -2;
1220
1221 parity_error:
1222         fas216_log(info, LOG_ERROR, "parity error during message in phase");
1223         return -3;
1224 }
1225
1226 /**
1227  * fas216_message - handle a function done interrupt from FAS216 chip
1228  * @info: interface which caused function done interrupt
1229  *
1230  * Handle a function done interrupt from FAS216 chip
1231  */
1232 static void fas216_message(FAS216_Info *info)
1233 {
1234         unsigned char *message = info->scsi.message;
1235         unsigned int msglen = 1;
1236         int msgbyte = 0;
1237
1238         fas216_checkmagic(info);
1239
1240         message[0] = fas216_readb(info, REG_FF);
1241
1242         if (message[0] == EXTENDED_MESSAGE) {
1243                 msgbyte = fas216_get_msg_byte(info);
1244
1245                 if (msgbyte >= 0) {
1246                         message[1] = msgbyte;
1247
1248                         for (msglen = 2; msglen < message[1] + 2; msglen++) {
1249                                 msgbyte = fas216_get_msg_byte(info);
1250
1251                                 if (msgbyte >= 0)
1252                                         message[msglen] = msgbyte;
1253                                 else
1254                                         break;
1255                         }
1256                 }
1257         }
1258
1259         if (msgbyte == -3)
1260                 goto parity_error;
1261
1262 #ifdef DEBUG_MESSAGES
1263         {
1264                 int i;
1265
1266                 printk("scsi%d.%c: message in: ",
1267                         info->host->host_no, fas216_target(info));
1268                 for (i = 0; i < msglen; i++)
1269                         printk("%02X ", message[i]);
1270                 printk("\n");
1271         }
1272 #endif
1273
1274         fas216_parse_message(info, message, msglen);
1275         fas216_cmd(info, CMD_MSGACCEPTED);
1276         return;
1277
1278 parity_error:
1279         fas216_cmd(info, CMD_SETATN);
1280         msgqueue_flush(&info->scsi.msgs);
1281         msgqueue_addmsg(&info->scsi.msgs, 1, MSG_PARITY_ERROR);
1282         info->scsi.phase = PHASE_MSGOUT_EXPECT;
1283         fas216_cmd(info, CMD_MSGACCEPTED);
1284         return;
1285 }
1286
1287 /**
1288  * fas216_send_command - send command after all message bytes have been sent
1289  * @info: interface which caused bus service
1290  *
1291  * Send a command to a target after all message bytes have been sent
1292  */
1293 static void fas216_send_command(FAS216_Info *info)
1294 {
1295         int i;
1296
1297         fas216_checkmagic(info);
1298
1299         fas216_cmd(info, CMD_NOP|CMD_WITHDMA);
1300         fas216_cmd(info, CMD_FLUSHFIFO);
1301
1302         /* load command */
1303         for (i = info->scsi.SCp.sent_command; i < info->SCpnt->cmd_len; i++)
1304                 fas216_writeb(info, REG_FF, info->SCpnt->cmnd[i]);
1305
1306         fas216_cmd(info, CMD_TRANSFERINFO);
1307
1308         info->scsi.phase = PHASE_COMMAND;
1309 }
1310
1311 /**
1312  * fas216_send_messageout - handle bus service to send a message
1313  * @info: interface which caused bus service
1314  *
1315  * Handle bus service to send a message.
1316  * Note: We do not allow the device to change the data direction!
1317  */
1318 static void fas216_send_messageout(FAS216_Info *info, int start)
1319 {
1320         unsigned int tot_msglen = msgqueue_msglength(&info->scsi.msgs);
1321
1322         fas216_checkmagic(info);
1323
1324         fas216_cmd(info, CMD_FLUSHFIFO);
1325
1326         if (tot_msglen) {
1327                 struct message *msg;
1328                 int msgnr = 0;
1329
1330                 while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
1331                         int i;
1332
1333                         for (i = start; i < msg->length; i++)
1334                                 fas216_writeb(info, REG_FF, msg->msg[i]);
1335
1336                         msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
1337                         start = 0;
1338                 }
1339         } else
1340                 fas216_writeb(info, REG_FF, NOP);
1341
1342         fas216_cmd(info, CMD_TRANSFERINFO);
1343
1344         info->scsi.phase = PHASE_MSGOUT;
1345 }
1346
1347 /**
1348  * fas216_busservice_intr - handle bus service interrupt from FAS216 chip
1349  * @info: interface which caused bus service interrupt
1350  * @stat: Status register contents
1351  * @is: SCSI Status register contents
1352  *
1353  * Handle a bus service interrupt from FAS216 chip
1354  */
1355 static void fas216_busservice_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
1356 {
1357         fas216_checkmagic(info);
1358
1359         fas216_log(info, LOG_BUSSERVICE,
1360                    "bus service: stat=%02x is=%02x phase=%02x",
1361                    stat, is, info->scsi.phase);
1362
1363         switch (info->scsi.phase) {
1364         case PHASE_SELECTION:
1365                 if ((is & IS_BITS) != IS_MSGBYTESENT)
1366                         goto bad_is;
1367                 break;
1368
1369         case PHASE_SELSTEPS:
1370                 switch (is & IS_BITS) {
1371                 case IS_SELARB:
1372                 case IS_MSGBYTESENT:
1373                         goto bad_is;
1374
1375                 case IS_NOTCOMMAND:
1376                 case IS_EARLYPHASE:
1377                         if ((stat & STAT_BUSMASK) == STAT_MESGIN)
1378                                 break;
1379                         goto bad_is;
1380
1381                 case IS_COMPLETE:
1382                         break;
1383                 }
1384
1385         default:
1386                 break;
1387         }
1388
1389         fas216_cmd(info, CMD_NOP);
1390
1391 #define STATE(st,ph) ((ph) << 3 | (st))
1392         /* This table describes the legal SCSI state transitions,
1393          * as described by the SCSI II spec.
1394          */
1395         switch (STATE(stat & STAT_BUSMASK, info->scsi.phase)) {
1396         case STATE(STAT_DATAIN, PHASE_SELSTEPS):/* Sel w/ steps -> Data In      */
1397         case STATE(STAT_DATAIN, PHASE_MSGOUT):  /* Message Out  -> Data In      */
1398         case STATE(STAT_DATAIN, PHASE_COMMAND): /* Command      -> Data In      */
1399         case STATE(STAT_DATAIN, PHASE_MSGIN):   /* Message In   -> Data In      */
1400                 info->scsi.phase = PHASE_DATAIN;
1401                 fas216_transfer(info);
1402                 return;
1403
1404         case STATE(STAT_DATAIN, PHASE_DATAIN):  /* Data In      -> Data In      */
1405         case STATE(STAT_DATAOUT, PHASE_DATAOUT):/* Data Out     -> Data Out     */
1406                 fas216_cleanuptransfer(info);
1407                 fas216_transfer(info);
1408                 return;
1409
1410         case STATE(STAT_DATAOUT, PHASE_SELSTEPS):/* Sel w/ steps-> Data Out     */
1411         case STATE(STAT_DATAOUT, PHASE_MSGOUT): /* Message Out  -> Data Out     */
1412         case STATE(STAT_DATAOUT, PHASE_COMMAND):/* Command      -> Data Out     */
1413         case STATE(STAT_DATAOUT, PHASE_MSGIN):  /* Message In   -> Data Out     */
1414                 fas216_cmd(info, CMD_FLUSHFIFO);
1415                 info->scsi.phase = PHASE_DATAOUT;
1416                 fas216_transfer(info);
1417                 return;
1418
1419         case STATE(STAT_STATUS, PHASE_DATAOUT): /* Data Out     -> Status       */
1420         case STATE(STAT_STATUS, PHASE_DATAIN):  /* Data In      -> Status       */
1421                 fas216_stoptransfer(info);
1422         case STATE(STAT_STATUS, PHASE_SELSTEPS):/* Sel w/ steps -> Status       */
1423         case STATE(STAT_STATUS, PHASE_MSGOUT):  /* Message Out  -> Status       */
1424         case STATE(STAT_STATUS, PHASE_COMMAND): /* Command      -> Status       */
1425         case STATE(STAT_STATUS, PHASE_MSGIN):   /* Message In   -> Status       */
1426                 fas216_cmd(info, CMD_INITCMDCOMPLETE);
1427                 info->scsi.phase = PHASE_STATUS;
1428                 return;
1429
1430         case STATE(STAT_MESGIN, PHASE_DATAOUT): /* Data Out     -> Message In   */
1431         case STATE(STAT_MESGIN, PHASE_DATAIN):  /* Data In      -> Message In   */
1432                 fas216_stoptransfer(info);
1433         case STATE(STAT_MESGIN, PHASE_COMMAND): /* Command      -> Message In   */
1434         case STATE(STAT_MESGIN, PHASE_SELSTEPS):/* Sel w/ steps -> Message In   */
1435         case STATE(STAT_MESGIN, PHASE_MSGOUT):  /* Message Out  -> Message In   */
1436                 info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
1437                 fas216_cmd(info, CMD_FLUSHFIFO);
1438                 fas216_cmd(info, CMD_TRANSFERINFO);
1439                 info->scsi.phase = PHASE_MSGIN;
1440                 return;
1441
1442         case STATE(STAT_MESGIN, PHASE_MSGIN):
1443                 info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
1444                 fas216_cmd(info, CMD_TRANSFERINFO);
1445                 return;
1446
1447         case STATE(STAT_COMMAND, PHASE_MSGOUT): /* Message Out  -> Command      */
1448         case STATE(STAT_COMMAND, PHASE_MSGIN):  /* Message In   -> Command      */
1449                 fas216_send_command(info);
1450                 info->scsi.phase = PHASE_COMMAND;
1451                 return;
1452
1453
1454         /*
1455          * Selection    -> Message Out
1456          */
1457         case STATE(STAT_MESGOUT, PHASE_SELECTION):
1458                 fas216_send_messageout(info, 1);
1459                 return;
1460
1461         /*
1462          * Message Out  -> Message Out
1463          */
1464         case STATE(STAT_MESGOUT, PHASE_SELSTEPS):
1465         case STATE(STAT_MESGOUT, PHASE_MSGOUT):
1466                 /*
1467                  * If we get another message out phase, this usually
1468                  * means some parity error occurred.  Resend complete
1469                  * set of messages.  If we have more than one byte to
1470                  * send, we need to assert ATN again.
1471                  */
1472                 if (info->device[info->SCpnt->device->id].parity_check) {
1473                         /*
1474                          * We were testing... good, the device
1475                          * supports parity checking.
1476                          */
1477                         info->device[info->SCpnt->device->id].parity_check = 0;
1478                         info->device[info->SCpnt->device->id].parity_enabled = 1;
1479                         fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
1480                 }
1481
1482                 if (msgqueue_msglength(&info->scsi.msgs) > 1)
1483                         fas216_cmd(info, CMD_SETATN);
1484                 /*FALLTHROUGH*/
1485
1486         /*
1487          * Any          -> Message Out
1488          */
1489         case STATE(STAT_MESGOUT, PHASE_MSGOUT_EXPECT):
1490                 fas216_send_messageout(info, 0);
1491                 return;
1492
1493         /* Error recovery rules.
1494          *   These either attempt to abort or retry the operation.
1495          * TODO: we need more of these
1496          */
1497         case STATE(STAT_COMMAND, PHASE_COMMAND):/* Command      -> Command      */
1498                 /* error - we've sent out all the command bytes
1499                  * we have.
1500                  * NOTE: we need SAVE DATA POINTERS/RESTORE DATA POINTERS
1501                  * to include the command bytes sent for this to work
1502                  * correctly.
1503                  */
1504                 printk(KERN_ERR "scsi%d.%c: "
1505                         "target trying to receive more command bytes\n",
1506                         info->host->host_no, fas216_target(info));
1507                 fas216_cmd(info, CMD_SETATN);
1508                 fas216_set_stc(info, 15);
1509                 fas216_cmd(info, CMD_PADBYTES | CMD_WITHDMA);
1510                 msgqueue_flush(&info->scsi.msgs);
1511                 msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
1512                 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1513                 return;
1514         }
1515
1516         if (info->scsi.phase == PHASE_MSGIN_DISCONNECT) {
1517                 printk(KERN_ERR "scsi%d.%c: disconnect message received, but bus service %s?\n",
1518                         info->host->host_no, fas216_target(info),
1519                         fas216_bus_phase(stat));
1520                 msgqueue_flush(&info->scsi.msgs);
1521                 fas216_cmd(info, CMD_SETATN);
1522                 msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
1523                 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1524                 info->scsi.aborting = 1;
1525                 fas216_cmd(info, CMD_TRANSFERINFO);
1526                 return;
1527         }
1528         printk(KERN_ERR "scsi%d.%c: bus phase %s after %s?\n",
1529                 info->host->host_no, fas216_target(info),
1530                 fas216_bus_phase(stat),
1531                 fas216_drv_phase(info));
1532         print_debug_list();
1533         return;
1534
1535 bad_is:
1536         fas216_log(info, 0, "bus service at step %d?", is & IS_BITS);
1537         fas216_dumpstate(info);
1538         print_debug_list();
1539
1540         fas216_done(info, DID_ERROR);
1541 }
1542
1543 /**
1544  * fas216_funcdone_intr - handle a function done interrupt from FAS216 chip
1545  * @info: interface which caused function done interrupt
1546  * @stat: Status register contents
1547  * @is: SCSI Status register contents
1548  *
1549  * Handle a function done interrupt from FAS216 chip
1550  */
1551 static void fas216_funcdone_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
1552 {
1553         unsigned int fifo_len = fas216_readb(info, REG_CFIS) & CFIS_CF;
1554
1555         fas216_checkmagic(info);
1556
1557         fas216_log(info, LOG_FUNCTIONDONE,
1558                    "function done: stat=%02x is=%02x phase=%02x",
1559                    stat, is, info->scsi.phase);
1560
1561         switch (info->scsi.phase) {
1562         case PHASE_STATUS:                      /* status phase - read status and msg   */
1563                 if (fifo_len != 2) {
1564                         fas216_log(info, 0, "odd number of bytes in FIFO: %d", fifo_len);
1565                 }
1566                 /*
1567                  * Read status then message byte.
1568                  */
1569                 info->scsi.SCp.Status = fas216_readb(info, REG_FF);
1570                 info->scsi.SCp.Message = fas216_readb(info, REG_FF);
1571                 info->scsi.phase = PHASE_DONE;
1572                 fas216_cmd(info, CMD_MSGACCEPTED);
1573                 break;
1574
1575         case PHASE_IDLE:
1576         case PHASE_SELECTION:
1577         case PHASE_SELSTEPS:
1578                 break;
1579
1580         case PHASE_MSGIN:                       /* message in phase                     */
1581                 if ((stat & STAT_BUSMASK) == STAT_MESGIN) {
1582                         info->scsi.msgin_fifo = fifo_len;
1583                         fas216_message(info);
1584                         break;
1585                 }
1586
1587         default:
1588                 fas216_log(info, 0, "internal phase %s for function done?"
1589                         "  What do I do with this?",
1590                         fas216_target(info), fas216_drv_phase(info));
1591         }
1592 }
1593
1594 static void fas216_bus_reset(FAS216_Info *info)
1595 {
1596         neg_t sync_state;
1597         int i;
1598
1599         msgqueue_flush(&info->scsi.msgs);
1600
1601         sync_state = neg_invalid;
1602
1603 #ifdef SCSI2_SYNC
1604         if (info->ifcfg.capabilities & (FASCAP_DMA|FASCAP_PSEUDODMA))
1605                 sync_state = neg_wait;
1606 #endif
1607
1608         info->scsi.phase = PHASE_IDLE;
1609         info->SCpnt = NULL; /* bug! */
1610         memset(&info->scsi.SCp, 0, sizeof(info->scsi.SCp));
1611
1612         for (i = 0; i < 8; i++) {
1613                 info->device[i].disconnect_ok   = info->ifcfg.disconnect_ok;
1614                 info->device[i].sync_state      = sync_state;
1615                 info->device[i].period          = info->ifcfg.asyncperiod / 4;
1616                 info->device[i].stp             = info->scsi.async_stp;
1617                 info->device[i].sof             = 0;
1618                 info->device[i].wide_xfer       = 0;
1619         }
1620
1621         info->rst_bus_status = 1;
1622         wake_up(&info->eh_wait);
1623 }
1624
1625 /**
1626  * fas216_intr - handle interrupts to progress a command
1627  * @info: interface to service
1628  *
1629  * Handle interrupts from the interface to progress a command
1630  */
1631 irqreturn_t fas216_intr(FAS216_Info *info)
1632 {
1633         unsigned char inst, is, stat;
1634         int handled = IRQ_NONE;
1635
1636         fas216_checkmagic(info);
1637
1638         stat = fas216_readb(info, REG_STAT);
1639         is = fas216_readb(info, REG_IS);
1640         inst = fas216_readb(info, REG_INST);
1641
1642         add_debug_list(stat, is, inst, info->scsi.phase);
1643
1644         if (stat & STAT_INT) {
1645                 if (inst & INST_BUSRESET) {
1646                         fas216_log(info, 0, "bus reset detected");
1647                         fas216_bus_reset(info);
1648                         scsi_report_bus_reset(info->host, 0);
1649                 } else if (inst & INST_ILLEGALCMD) {
1650                         fas216_log(info, LOG_ERROR, "illegal command given\n");
1651                         fas216_dumpstate(info);
1652                         print_debug_list();
1653                 } else if (inst & INST_DISCONNECT)
1654                         fas216_disconnect_intr(info);
1655                 else if (inst & INST_RESELECTED)        /* reselected                   */
1656                         fas216_reselected_intr(info);
1657                 else if (inst & INST_BUSSERVICE)        /* bus service request          */
1658                         fas216_busservice_intr(info, stat, is);
1659                 else if (inst & INST_FUNCDONE)          /* function done                */
1660                         fas216_funcdone_intr(info, stat, is);
1661                 else
1662                         fas216_log(info, 0, "unknown interrupt received:"
1663                                 " phase %s inst %02X is %02X stat %02X",
1664                                 fas216_drv_phase(info), inst, is, stat);
1665                 handled = IRQ_HANDLED;
1666         }
1667         return handled;
1668 }
1669
1670 static void __fas216_start_command(FAS216_Info *info, Scsi_Cmnd *SCpnt)
1671 {
1672         int tot_msglen;
1673
1674         /* following what the ESP driver says */
1675         fas216_set_stc(info, 0);
1676         fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
1677
1678         /* flush FIFO */
1679         fas216_cmd(info, CMD_FLUSHFIFO);
1680
1681         /* load bus-id and timeout */
1682         fas216_writeb(info, REG_SDID, BUSID(SCpnt->device->id));
1683         fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
1684
1685         /* synchronous transfers */
1686         fas216_set_sync(info, SCpnt->device->id);
1687
1688         tot_msglen = msgqueue_msglength(&info->scsi.msgs);
1689
1690 #ifdef DEBUG_MESSAGES
1691         {
1692                 struct message *msg;
1693                 int msgnr = 0, i;
1694
1695                 printk("scsi%d.%c: message out: ",
1696                         info->host->host_no, '0' + SCpnt->device->id);
1697                 while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
1698                         printk("{ ");
1699                         for (i = 0; i < msg->length; i++)
1700                                 printk("%02x ", msg->msg[i]);
1701                         printk("} ");
1702                 }
1703                 printk("\n");
1704         }
1705 #endif
1706
1707         if (tot_msglen == 1 || tot_msglen == 3) {
1708                 /*
1709                  * We have an easy message length to send...
1710                  */
1711                 struct message *msg;
1712                 int msgnr = 0, i;
1713
1714                 info->scsi.phase = PHASE_SELSTEPS;
1715
1716                 /* load message bytes */
1717                 while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
1718                         for (i = 0; i < msg->length; i++)
1719                                 fas216_writeb(info, REG_FF, msg->msg[i]);
1720                         msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
1721                 }
1722
1723                 /* load command */
1724                 for (i = 0; i < SCpnt->cmd_len; i++)
1725                         fas216_writeb(info, REG_FF, SCpnt->cmnd[i]);
1726
1727                 if (tot_msglen == 1)
1728                         fas216_cmd(info, CMD_SELECTATN);
1729                 else
1730                         fas216_cmd(info, CMD_SELECTATN3);
1731         } else {
1732                 /*
1733                  * We have an unusual number of message bytes to send.
1734                  *  Load first byte into fifo, and issue SELECT with ATN and
1735                  *  stop steps.
1736                  */
1737                 struct message *msg = msgqueue_getmsg(&info->scsi.msgs, 0);
1738
1739                 fas216_writeb(info, REG_FF, msg->msg[0]);
1740                 msg->fifo = 1;
1741
1742                 fas216_cmd(info, CMD_SELECTATNSTOP);
1743         }
1744 }
1745
1746 /*
1747  * Decide whether we need to perform a parity test on this device.
1748  * Can also be used to force parity error conditions during initial
1749  * information transfer phase (message out) for test purposes.
1750  */
1751 static int parity_test(FAS216_Info *info, int target)
1752 {
1753 #if 0
1754         if (target == 3) {
1755                 info->device[target].parity_check = 0;
1756                 return 1;
1757         }
1758 #endif
1759         return info->device[target].parity_check;
1760 }
1761
1762 static void fas216_start_command(FAS216_Info *info, Scsi_Cmnd *SCpnt)
1763 {
1764         int disconnect_ok;
1765
1766         /*
1767          * claim host busy
1768          */
1769         info->scsi.phase = PHASE_SELECTION;
1770         info->scsi.SCp = SCpnt->SCp;
1771         info->SCpnt = SCpnt;
1772         info->dma.transfer_type = fasdma_none;
1773
1774         if (parity_test(info, SCpnt->device->id))
1775                 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0] | CNTL1_PTE);
1776         else
1777                 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
1778
1779         /*
1780          * Don't allow request sense commands to disconnect.
1781          */
1782         disconnect_ok = SCpnt->cmnd[0] != REQUEST_SENSE &&
1783                         info->device[SCpnt->device->id].disconnect_ok;
1784
1785         /*
1786          * build outgoing message bytes
1787          */
1788         msgqueue_flush(&info->scsi.msgs);
1789         msgqueue_addmsg(&info->scsi.msgs, 1, IDENTIFY(disconnect_ok, SCpnt->device->lun));
1790
1791         /*
1792          * add tag message if required
1793          */
1794         if (SCpnt->tag)
1795                 msgqueue_addmsg(&info->scsi.msgs, 2, SIMPLE_QUEUE_TAG, SCpnt->tag);
1796
1797         do {
1798 #ifdef SCSI2_SYNC
1799                 if ((info->device[SCpnt->device->id].sync_state == neg_wait ||
1800                      info->device[SCpnt->device->id].sync_state == neg_complete) &&
1801                     (SCpnt->cmnd[0] == REQUEST_SENSE ||
1802                      SCpnt->cmnd[0] == INQUIRY)) {
1803                         info->device[SCpnt->device->id].sync_state = neg_inprogress;
1804                         msgqueue_addmsg(&info->scsi.msgs, 5,
1805                                         EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
1806                                         1000 / info->ifcfg.clockrate,
1807                                         info->ifcfg.sync_max_depth);
1808                         break;
1809                 }
1810 #endif
1811         } while (0);
1812
1813         __fas216_start_command(info, SCpnt);
1814 }
1815
1816 static void fas216_allocate_tag(FAS216_Info *info, Scsi_Cmnd *SCpnt)
1817 {
1818 #ifdef SCSI2_TAG
1819         /*
1820          * tagged queuing - allocate a new tag to this command
1821          */
1822         if (SCpnt->device->simple_tags && SCpnt->cmnd[0] != REQUEST_SENSE &&
1823             SCpnt->cmnd[0] != INQUIRY) {
1824             SCpnt->device->current_tag += 1;
1825                 if (SCpnt->device->current_tag == 0)
1826                     SCpnt->device->current_tag = 1;
1827                         SCpnt->tag = SCpnt->device->current_tag;
1828         } else
1829 #endif
1830                 set_bit(SCpnt->device->id * 8 + SCpnt->device->lun, info->busyluns);
1831
1832         info->stats.removes += 1;
1833         switch (SCpnt->cmnd[0]) {
1834         case WRITE_6:
1835         case WRITE_10:
1836         case WRITE_12:
1837                 info->stats.writes += 1;
1838                 break;
1839         case READ_6:
1840         case READ_10:
1841         case READ_12:
1842                 info->stats.reads += 1;
1843                 break;
1844         default:
1845                 info->stats.miscs += 1;
1846                 break;
1847         }
1848 }
1849
1850 static void fas216_do_bus_device_reset(FAS216_Info *info, Scsi_Cmnd *SCpnt)
1851 {
1852         struct message *msg;
1853
1854         /*
1855          * claim host busy
1856          */
1857         info->scsi.phase = PHASE_SELECTION;
1858         info->scsi.SCp = SCpnt->SCp;
1859         info->SCpnt = SCpnt;
1860         info->dma.transfer_type = fasdma_none;
1861
1862         fas216_log(info, LOG_ERROR, "sending bus device reset");
1863
1864         msgqueue_flush(&info->scsi.msgs);
1865         msgqueue_addmsg(&info->scsi.msgs, 1, BUS_DEVICE_RESET);
1866
1867         /* following what the ESP driver says */
1868         fas216_set_stc(info, 0);
1869         fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
1870
1871         /* flush FIFO */
1872         fas216_cmd(info, CMD_FLUSHFIFO);
1873
1874         /* load bus-id and timeout */
1875         fas216_writeb(info, REG_SDID, BUSID(SCpnt->device->id));
1876         fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
1877
1878         /* synchronous transfers */
1879         fas216_set_sync(info, SCpnt->device->id);
1880
1881         msg = msgqueue_getmsg(&info->scsi.msgs, 0);
1882
1883         fas216_writeb(info, REG_FF, BUS_DEVICE_RESET);
1884         msg->fifo = 1;
1885
1886         fas216_cmd(info, CMD_SELECTATNSTOP);
1887 }
1888
1889 /**
1890  * fas216_kick - kick a command to the interface
1891  * @info: our host interface to kick
1892  *
1893  * Kick a command to the interface, interface should be idle.
1894  * Notes: Interrupts are always disabled!
1895  */
1896 static void fas216_kick(FAS216_Info *info)
1897 {
1898         Scsi_Cmnd *SCpnt = NULL;
1899 #define TYPE_OTHER      0
1900 #define TYPE_RESET      1
1901 #define TYPE_QUEUE      2
1902         int where_from = TYPE_OTHER;
1903
1904         fas216_checkmagic(info);
1905
1906         /*
1907          * Obtain the next command to process.
1908          */
1909         do {
1910                 if (info->rstSCpnt) {
1911                         SCpnt = info->rstSCpnt;
1912                         /* don't remove it */
1913                         where_from = TYPE_RESET;
1914                         break;
1915                 }
1916
1917                 if (info->reqSCpnt) {
1918                         SCpnt = info->reqSCpnt;
1919                         info->reqSCpnt = NULL;
1920                         break;
1921                 }
1922
1923                 if (info->origSCpnt) {
1924                         SCpnt = info->origSCpnt;
1925                         info->origSCpnt = NULL;
1926                         break;
1927                 }
1928
1929                 /* retrieve next command */
1930                 if (!SCpnt) {
1931                         SCpnt = queue_remove_exclude(&info->queues.issue,
1932                                                      info->busyluns);
1933                         where_from = TYPE_QUEUE;
1934                         break;
1935                 }
1936         } while (0);
1937
1938         if (!SCpnt) {
1939                 /*
1940                  * no command pending, so enable reselection.
1941                  */
1942                 fas216_cmd(info, CMD_ENABLESEL);
1943                 return;
1944         }
1945
1946         /*
1947          * We're going to start a command, so disable reselection
1948          */
1949         fas216_cmd(info, CMD_DISABLESEL);
1950
1951         if (info->scsi.disconnectable && info->SCpnt) {
1952                 fas216_log(info, LOG_CONNECT,
1953                         "moved command for %d to disconnected queue",
1954                         info->SCpnt->device->id);
1955                 queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
1956                 info->scsi.disconnectable = 0;
1957                 info->SCpnt = NULL;
1958         }
1959
1960         fas216_log_command(info, LOG_CONNECT | LOG_MESSAGES, SCpnt,
1961                            "starting");
1962
1963         switch (where_from) {
1964         case TYPE_QUEUE:
1965                 fas216_allocate_tag(info, SCpnt);
1966         case TYPE_OTHER:
1967                 fas216_start_command(info, SCpnt);
1968                 break;
1969         case TYPE_RESET:
1970                 fas216_do_bus_device_reset(info, SCpnt);
1971                 break;
1972         }
1973
1974         fas216_log(info, LOG_CONNECT, "select: data pointers [%p, %X]",
1975                 info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
1976
1977         /*
1978          * should now get either DISCONNECT or
1979          * (FUNCTION DONE with BUS SERVICE) interrupt
1980          */
1981 }
1982
1983 /*
1984  * Clean up from issuing a BUS DEVICE RESET message to a device.
1985  */
1986 static void
1987 fas216_devicereset_done(FAS216_Info *info, Scsi_Cmnd *SCpnt, unsigned int result)
1988 {
1989         fas216_log(info, LOG_ERROR, "fas216 device reset complete");
1990
1991         info->rstSCpnt = NULL;
1992         info->rst_dev_status = 1;
1993         wake_up(&info->eh_wait);
1994 }
1995
1996 /**
1997  * fas216_rq_sns_done - Finish processing automatic request sense command
1998  * @info: interface that completed
1999  * @SCpnt: command that completed
2000  * @result: driver byte of result
2001  *
2002  * Finish processing automatic request sense command
2003  */
2004 static void
2005 fas216_rq_sns_done(FAS216_Info *info, Scsi_Cmnd *SCpnt, unsigned int result)
2006 {
2007         fas216_log_target(info, LOG_CONNECT, SCpnt->device->id,
2008                    "request sense complete, result=0x%04x%02x%02x",
2009                    result, SCpnt->SCp.Message, SCpnt->SCp.Status);
2010
2011         if (result != DID_OK || SCpnt->SCp.Status != GOOD)
2012                 /*
2013                  * Something went wrong.  Make sure that we don't
2014                  * have valid data in the sense buffer that could
2015                  * confuse the higher levels.
2016                  */
2017                 memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
2018 //printk("scsi%d.%c: sense buffer: ", info->host->host_no, '0' + SCpnt->device->id);
2019 //{ int i; for (i = 0; i < 32; i++) printk("%02x ", SCpnt->sense_buffer[i]); printk("\n"); }
2020         /*
2021          * Note that we don't set SCpnt->result, since that should
2022          * reflect the status of the command that we were asked by
2023          * the upper layers to process.  This would have been set
2024          * correctly by fas216_std_done.
2025          */
2026         SCpnt->scsi_done(SCpnt);
2027 }
2028
2029 /**
2030  * fas216_std_done - finish processing of standard command
2031  * @info: interface that completed
2032  * @SCpnt: command that completed
2033  * @result: driver byte of result
2034  *
2035  * Finish processing of standard command
2036  */
2037 static void
2038 fas216_std_done(FAS216_Info *info, Scsi_Cmnd *SCpnt, unsigned int result)
2039 {
2040         info->stats.fins += 1;
2041
2042         SCpnt->result = result << 16 | info->scsi.SCp.Message << 8 |
2043                         info->scsi.SCp.Status;
2044
2045         fas216_log_command(info, LOG_CONNECT, SCpnt,
2046                 "command complete, result=0x%08x", SCpnt->result);
2047
2048         /*
2049          * If the driver detected an error, we're all done.
2050          */
2051         if (host_byte(SCpnt->result) != DID_OK ||
2052             msg_byte(SCpnt->result) != COMMAND_COMPLETE)
2053                 goto done;
2054
2055         /*
2056          * If the command returned CHECK_CONDITION or COMMAND_TERMINATED
2057          * status, request the sense information.
2058          */
2059         if (status_byte(SCpnt->result) == CHECK_CONDITION ||
2060             status_byte(SCpnt->result) == COMMAND_TERMINATED)
2061                 goto request_sense;
2062
2063         /*
2064          * If the command did not complete with GOOD status,
2065          * we are all done here.
2066          */
2067         if (status_byte(SCpnt->result) != GOOD)
2068                 goto done;
2069
2070         /*
2071          * We have successfully completed a command.  Make sure that
2072          * we do not have any buffers left to transfer.  The world
2073          * is not perfect, and we seem to occasionally hit this.
2074          * It can be indicative of a buggy driver, target or the upper
2075          * levels of the SCSI code.
2076          */
2077         if (info->scsi.SCp.ptr) {
2078                 switch (SCpnt->cmnd[0]) {
2079                 case INQUIRY:
2080                 case START_STOP:
2081                 case MODE_SENSE:
2082                         break;
2083
2084                 default:
2085                         printk(KERN_ERR "scsi%d.%c: incomplete data transfer "
2086                                 "detected: res=%08X ptr=%p len=%X CDB: ",
2087                                 info->host->host_no, '0' + SCpnt->device->id,
2088                                 SCpnt->result, info->scsi.SCp.ptr,
2089                                 info->scsi.SCp.this_residual);
2090                         print_command(SCpnt->cmnd);
2091                         SCpnt->result &= ~(255 << 16);
2092                         SCpnt->result |= DID_BAD_TARGET << 16;
2093                         goto request_sense;
2094                 }
2095         }
2096
2097 done:
2098         if (SCpnt->scsi_done) {
2099                 SCpnt->scsi_done(SCpnt);
2100                 return;
2101         }
2102
2103         panic("scsi%d.H: null scsi_done function in fas216_done",
2104                 info->host->host_no);
2105
2106
2107 request_sense:
2108         if (SCpnt->cmnd[0] == REQUEST_SENSE)
2109                 goto done;
2110
2111         fas216_log_target(info, LOG_CONNECT, SCpnt->device->id,
2112                           "requesting sense");
2113         memset(SCpnt->cmnd, 0, sizeof (SCpnt->cmnd));
2114         SCpnt->cmnd[0] = REQUEST_SENSE;
2115         SCpnt->cmnd[1] = SCpnt->device->lun << 5;
2116         SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
2117         SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
2118         SCpnt->SCp.buffer = NULL;
2119         SCpnt->SCp.buffers_residual = 0;
2120         SCpnt->SCp.ptr = (char *)SCpnt->sense_buffer;
2121         SCpnt->SCp.this_residual = sizeof(SCpnt->sense_buffer);
2122         SCpnt->SCp.Message = 0;
2123         SCpnt->SCp.Status = 0;
2124         SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
2125         SCpnt->sc_data_direction = SCSI_DATA_READ;
2126         SCpnt->use_sg = 0;
2127         SCpnt->tag = 0;
2128         SCpnt->host_scribble = (void *)fas216_rq_sns_done;
2129
2130         /*
2131          * Place this command into the high priority "request
2132          * sense" slot.  This will be the very next command
2133          * executed, unless a target connects to us.
2134          */
2135         if (info->reqSCpnt)
2136                 printk(KERN_WARNING "scsi%d.%c: loosing request command\n",
2137                         info->host->host_no, '0' + SCpnt->device->id);
2138         info->reqSCpnt = SCpnt;
2139 }
2140
2141 /**
2142  * fas216_done - complete processing for current command
2143  * @info: interface that completed
2144  * @result: driver byte of result
2145  *
2146  * Complete processing for current command
2147  */
2148 static void fas216_done(FAS216_Info *info, unsigned int result)
2149 {
2150         void (*fn)(FAS216_Info *, Scsi_Cmnd *, unsigned int);
2151         Scsi_Cmnd *SCpnt;
2152         unsigned long flags;
2153
2154         fas216_checkmagic(info);
2155
2156         if (!info->SCpnt)
2157                 goto no_command;
2158
2159         SCpnt = info->SCpnt;
2160         info->SCpnt = NULL;
2161         info->scsi.phase = PHASE_IDLE;
2162
2163         if (info->scsi.aborting) {
2164                 fas216_log(info, 0, "uncaught abort - returning DID_ABORT");
2165                 result = DID_ABORT;
2166                 info->scsi.aborting = 0;
2167         }
2168
2169         /*
2170          * Sanity check the completion - if we have zero bytes left
2171          * to transfer, we should not have a valid pointer.
2172          */
2173         if (info->scsi.SCp.ptr && info->scsi.SCp.this_residual == 0) {
2174                 printk("scsi%d.%c: zero bytes left to transfer, but "
2175                        "buffer pointer still valid: ptr=%p len=%08x CDB: ",
2176                        info->host->host_no, '0' + SCpnt->device->id,
2177                        info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
2178                 info->scsi.SCp.ptr = NULL;
2179                 print_command(SCpnt->cmnd);
2180         }
2181
2182         /*
2183          * Clear down this command as completed.  If we need to request
2184          * the sense information, fas216_kick will re-assert the busy
2185          * status.
2186          */
2187         info->device[SCpnt->device->id].parity_check = 0;
2188         clear_bit(SCpnt->device->id * 8 + SCpnt->device->lun, info->busyluns);
2189
2190         fn = (void (*)(FAS216_Info *, Scsi_Cmnd *, unsigned int))SCpnt->host_scribble;
2191         fn(info, SCpnt, result);
2192
2193         if (info->scsi.irq != NO_IRQ) {
2194                 spin_lock_irqsave(&info->host_lock, flags);
2195                 if (info->scsi.phase == PHASE_IDLE)
2196                         fas216_kick(info);
2197                 spin_unlock_irqrestore(&info->host_lock, flags);
2198         }
2199         return;
2200
2201 no_command:
2202         panic("scsi%d.H: null command in fas216_done",
2203                 info->host->host_no);
2204 }
2205
2206 /**
2207  * fas216_queue_command - queue a command for adapter to process.
2208  * @SCpnt: Command to queue
2209  * @done: done function to call once command is complete
2210  *
2211  * Queue a command for adapter to process.
2212  * Returns: 0 on success, else error.
2213  * Notes: io_request_lock is held, interrupts are disabled.
2214  */
2215 int fas216_queue_command(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
2216 {
2217         FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2218         int result;
2219
2220         fas216_checkmagic(info);
2221
2222         fas216_log_command(info, LOG_CONNECT, SCpnt,
2223                            "received command (%p)", SCpnt);
2224
2225         SCpnt->scsi_done = done;
2226         SCpnt->host_scribble = (void *)fas216_std_done;
2227         SCpnt->result = 0;
2228
2229         init_SCp(SCpnt);
2230
2231         info->stats.queues += 1;
2232         SCpnt->tag = 0;
2233
2234         spin_lock(&info->host_lock);
2235
2236         /*
2237          * Add command into execute queue and let it complete under
2238          * whatever scheme we're using.
2239          */
2240         result = !queue_add_cmd_ordered(&info->queues.issue, SCpnt);
2241
2242         /*
2243          * If we successfully added the command,
2244          * kick the interface to get it moving.
2245          */
2246         if (result == 0 && info->scsi.phase == PHASE_IDLE)
2247                 fas216_kick(info);
2248         spin_unlock(&info->host_lock);
2249
2250         fas216_log_target(info, LOG_CONNECT, -1, "queue %s",
2251                 result ? "failure" : "success");
2252
2253         return result;
2254 }
2255
2256 /**
2257  * fas216_internal_done - trigger restart of a waiting thread in fas216_noqueue_command
2258  * @SCpnt: Command to wake
2259  *
2260  * Trigger restart of a waiting thread in fas216_command
2261  */
2262 static void fas216_internal_done(Scsi_Cmnd *SCpnt)
2263 {
2264         FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2265
2266         fas216_checkmagic(info);
2267
2268         info->internal_done = 1;
2269 }
2270
2271 /**
2272  * fas216_noqueue_command - process a command for the adapter.
2273  * @SCpnt: Command to queue
2274  *
2275  * Queue a command for adapter to process.
2276  * Returns: scsi result code.
2277  * Notes: io_request_lock is held, interrupts are disabled.
2278  */
2279 int fas216_noqueue_command(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
2280 {
2281         FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2282
2283         fas216_checkmagic(info);
2284
2285         /*
2286          * We should only be using this if we don't have an interrupt.
2287          * Provide some "incentive" to use the queueing code.
2288          */
2289         BUG_ON(info->scsi.irq != NO_IRQ);
2290
2291         info->internal_done = 0;
2292         fas216_queue_command(SCpnt, fas216_internal_done);
2293
2294         /*
2295          * This wastes time, since we can't return until the command is
2296          * complete. We can't sleep either since we may get re-entered!
2297          * However, we must re-enable interrupts, or else we'll be
2298          * waiting forever.
2299          */
2300         spin_unlock_irq(info->host->host_lock);
2301
2302         while (!info->internal_done) {
2303                 /*
2304                  * If we don't have an IRQ, then we must poll the card for
2305                  * it's interrupt, and use that to call this driver's
2306                  * interrupt routine.  That way, we keep the command
2307                  * progressing.  Maybe we can add some inteligence here
2308                  * and go to sleep if we know that the device is going
2309                  * to be some time (eg, disconnected).
2310                  */
2311                 if (fas216_readb(info, REG_STAT) & STAT_INT) {
2312                         spin_lock_irq(info->host->host_lock);
2313                         fas216_intr(info);
2314                         spin_unlock_irq(info->host->host_lock);
2315                 }
2316         }
2317
2318         spin_lock_irq(info->host->host_lock);
2319
2320         done(SCpnt);
2321
2322         return 0;
2323 }
2324
2325 /*
2326  * Error handler timeout function.  Indicate that we timed out,
2327  * and wake up any error handler process so it can continue.
2328  */
2329 static void fas216_eh_timer(unsigned long data)
2330 {
2331         FAS216_Info *info = (FAS216_Info *)data;
2332
2333         fas216_log(info, LOG_ERROR, "error handling timed out\n");
2334
2335         del_timer(&info->eh_timer);
2336
2337         if (info->rst_bus_status == 0)
2338                 info->rst_bus_status = -1;
2339         if (info->rst_dev_status == 0)
2340                 info->rst_dev_status = -1;
2341
2342         wake_up(&info->eh_wait);
2343 }
2344
2345 enum res_find {
2346         res_failed,             /* not found                    */
2347         res_success,            /* command on issue queue       */
2348         res_hw_abort            /* command on disconnected dev  */
2349 };
2350
2351 /**
2352  * fas216_do_abort - decide how to abort a command
2353  * @SCpnt: command to abort
2354  *
2355  * Decide how to abort a command.
2356  * Returns: abort status
2357  */
2358 static enum res_find fas216_find_command(FAS216_Info *info, Scsi_Cmnd *SCpnt)
2359 {
2360         enum res_find res = res_failed;
2361
2362         if (queue_remove_cmd(&info->queues.issue, SCpnt)) {
2363                 /*
2364                  * The command was on the issue queue, and has not been
2365                  * issued yet.  We can remove the command from the queue,
2366                  * and acknowledge the abort.  Neither the device nor the
2367                  * interface know about the command.
2368                  */
2369                 printk("on issue queue ");
2370
2371                 res = res_success;
2372         } else if (queue_remove_cmd(&info->queues.disconnected, SCpnt)) {
2373                 /*
2374                  * The command was on the disconnected queue.  We must
2375                  * reconnect with the device if possible, and send it
2376                  * an abort message.
2377                  */
2378                 printk("on disconnected queue ");
2379
2380                 res = res_hw_abort;
2381         } else if (info->SCpnt == SCpnt) {
2382                 printk("executing ");
2383
2384                 switch (info->scsi.phase) {
2385                 /*
2386                  * If the interface is idle, and the command is 'disconnectable',
2387                  * then it is the same as on the disconnected queue.
2388                  */
2389                 case PHASE_IDLE:
2390                         if (info->scsi.disconnectable) {
2391                                 info->scsi.disconnectable = 0;
2392                                 info->SCpnt = NULL;
2393                                 res = res_hw_abort;
2394                         }
2395                         break;
2396
2397                 default:
2398                         break;
2399                 }
2400         } else if (info->origSCpnt == SCpnt) {
2401                 /*
2402                  * The command will be executed next, but a command
2403                  * is currently using the interface.  This is similar to
2404                  * being on the issue queue, except the busylun bit has
2405                  * been set.
2406                  */
2407                 info->origSCpnt = NULL;
2408                 clear_bit(SCpnt->device->id * 8 + SCpnt->device->lun, info->busyluns);
2409                 printk("waiting for execution ");
2410                 res = res_success;
2411         } else
2412                 printk("unknown ");
2413
2414         return res;
2415 }
2416
2417 /**
2418  * fas216_eh_abort - abort this command
2419  * @SCpnt: command to abort
2420  *
2421  * Abort this command.
2422  * Returns: FAILED if unable to abort
2423  * Notes: io_request_lock is taken, and irqs are disabled
2424  */
2425 int fas216_eh_abort(Scsi_Cmnd *SCpnt)
2426 {
2427         FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2428         int result = FAILED;
2429
2430         fas216_checkmagic(info);
2431
2432         info->stats.aborts += 1;
2433
2434         printk(KERN_WARNING "scsi%d: abort command ", info->host->host_no);
2435         print_command(SCpnt->data_cmnd);
2436
2437         print_debug_list();
2438         fas216_dumpstate(info);
2439
2440         printk(KERN_WARNING "scsi%d: abort %p ", info->host->host_no, SCpnt);
2441
2442         switch (fas216_find_command(info, SCpnt)) {
2443         /*
2444          * We found the command, and cleared it out.  Either
2445          * the command is still known to be executing on the
2446          * target, or the busylun bit is not set.
2447          */
2448         case res_success:
2449                 printk("success\n");
2450                 result = SUCCESS;
2451                 break;
2452
2453         /*
2454          * We need to reconnect to the target and send it an
2455          * ABORT or ABORT_TAG message.  We can only do this
2456          * if the bus is free.
2457          */
2458         case res_hw_abort:
2459                 
2460
2461         /*
2462          * We are unable to abort the command for some reason.
2463          */
2464         default:
2465         case res_failed:
2466                 printk("failed\n");
2467                 break;
2468         }
2469
2470         return result;
2471 }
2472
2473 /**
2474  * fas216_eh_device_reset - Reset the device associated with this command
2475  * @SCpnt: command specifing device to reset
2476  *
2477  * Reset the device associated with this command.
2478  * Returns: FAILED if unable to reset.
2479  * Notes: We won't be re-entered, so we'll only have one device
2480  * reset on the go at one time.
2481  */
2482 int fas216_eh_device_reset(Scsi_Cmnd *SCpnt)
2483 {
2484         FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2485         unsigned long flags;
2486         int i, res = FAILED, target = SCpnt->device->id;
2487
2488         fas216_log(info, LOG_ERROR, "device reset for target %d", target);
2489
2490         spin_lock_irqsave(&info->host_lock, flags);
2491
2492         do {
2493                 /*
2494                  * If we are currently connected to a device, and
2495                  * it is the device we want to reset, there is
2496                  * nothing we can do here.  Chances are it is stuck,
2497                  * and we need a bus reset.
2498                  */
2499                 if (info->SCpnt && !info->scsi.disconnectable &&
2500                     info->SCpnt->device->id == SCpnt->device->id)
2501                         break;
2502
2503                 /*
2504                  * We're going to be resetting this device.  Remove
2505                  * all pending commands from the driver.  By doing
2506                  * so, we guarantee that we won't touch the command
2507                  * structures except to process the reset request.
2508                  */
2509                 queue_remove_all_target(&info->queues.issue, target);
2510                 queue_remove_all_target(&info->queues.disconnected, target);
2511                 if (info->origSCpnt && info->origSCpnt->device->id == target)
2512                         info->origSCpnt = NULL;
2513                 if (info->reqSCpnt && info->reqSCpnt->device->id == target)
2514                         info->reqSCpnt = NULL;
2515                 for (i = 0; i < 8; i++)
2516                         clear_bit(target * 8 + i, info->busyluns);
2517
2518                 /*
2519                  * Hijack this SCSI command structure to send
2520                  * a bus device reset message to this device.
2521                  */
2522                 SCpnt->host_scribble = (void *)fas216_devicereset_done;
2523
2524                 info->rst_dev_status = 0;
2525                 info->rstSCpnt = SCpnt;
2526
2527                 if (info->scsi.phase == PHASE_IDLE)
2528                         fas216_kick(info);
2529
2530                 mod_timer(&info->eh_timer, 30 * HZ);
2531                 spin_unlock_irqrestore(&info->host_lock, flags);
2532
2533                 /*
2534                  * Wait up to 30 seconds for the reset to complete.
2535                  */
2536                 wait_event(info->eh_wait, info->rst_dev_status);
2537
2538                 del_timer_sync(&info->eh_timer);
2539                 spin_lock_irqsave(&info->host_lock, flags);
2540                 info->rstSCpnt = NULL;
2541
2542                 if (info->rst_dev_status == 1)
2543                         res = SUCCESS;
2544         } while (0);
2545
2546         SCpnt->host_scribble = NULL;
2547         spin_unlock_irqrestore(&info->host_lock, flags);
2548
2549         fas216_log(info, LOG_ERROR, "device reset complete: %s\n",
2550                    res == SUCCESS ? "success" : "failed");
2551
2552         return res;
2553 }
2554
2555 /**
2556  * fas216_eh_bus_reset - Reset the bus associated with the command
2557  * @SCpnt: command specifing bus to reset
2558  *
2559  * Reset the bus associated with the command.
2560  * Returns: FAILED if unable to reset.
2561  * Notes: Further commands are blocked.
2562  */
2563 int fas216_eh_bus_reset(Scsi_Cmnd *SCpnt)
2564 {
2565         FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2566         unsigned long flags;
2567         Scsi_Device *SDpnt;
2568
2569         fas216_checkmagic(info);
2570         fas216_log(info, LOG_ERROR, "resetting bus");
2571
2572         info->stats.bus_resets += 1;
2573
2574         spin_lock_irqsave(&info->host_lock, flags);
2575
2576         /*
2577          * Stop all activity on this interface.
2578          */
2579         fas216_aborttransfer(info);
2580         fas216_writeb(info, REG_CNTL3, info->scsi.cfg[2]);
2581
2582         /*
2583          * Clear any pending interrupts.
2584          */
2585         while (fas216_readb(info, REG_STAT) & STAT_INT)
2586                 fas216_readb(info, REG_INST);
2587
2588         info->rst_bus_status = 0;
2589
2590         /*
2591          * For each attached hard-reset device, clear out
2592          * all command structures.  Leave the running
2593          * command in place.
2594          */
2595         shost_for_each_device(SDpnt, info->host) {
2596                 int i;
2597
2598                 if (SDpnt->soft_reset)
2599                         continue;
2600
2601                 queue_remove_all_target(&info->queues.issue, SDpnt->id);
2602                 queue_remove_all_target(&info->queues.disconnected, SDpnt->id);
2603                 if (info->origSCpnt && info->origSCpnt->device->id == SDpnt->id)
2604                         info->origSCpnt = NULL;
2605                 if (info->reqSCpnt && info->reqSCpnt->device->id == SDpnt->id)
2606                         info->reqSCpnt = NULL;
2607                 info->SCpnt = NULL;
2608
2609                 for (i = 0; i < 8; i++)
2610                         clear_bit(SDpnt->id * 8 + i, info->busyluns);
2611         }
2612
2613         info->scsi.phase = PHASE_IDLE;
2614
2615         /*
2616          * Reset the SCSI bus.  Device cleanup happens in
2617          * the interrupt handler.
2618          */
2619         fas216_cmd(info, CMD_RESETSCSI);
2620
2621         mod_timer(&info->eh_timer, jiffies + HZ);
2622         spin_unlock_irqrestore(&info->host_lock, flags);
2623
2624         /*
2625          * Wait one second for the interrupt.
2626          */
2627         wait_event(info->eh_wait, info->rst_bus_status);
2628         del_timer_sync(&info->eh_timer);
2629
2630         fas216_log(info, LOG_ERROR, "bus reset complete: %s\n",
2631                    info->rst_bus_status == 1 ? "success" : "failed");
2632
2633         return info->rst_bus_status == 1 ? SUCCESS : FAILED;
2634 }
2635
2636 /**
2637  * fas216_init_chip - Initialise FAS216 state after reset
2638  * @info: state structure for interface
2639  *
2640  * Initialise FAS216 state after reset
2641  */
2642 static void fas216_init_chip(FAS216_Info *info)
2643 {
2644         unsigned int clock = ((info->ifcfg.clockrate - 1) / 5 + 1) & 7;
2645         fas216_writeb(info, REG_CLKF, clock);
2646         fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
2647         fas216_writeb(info, REG_CNTL2, info->scsi.cfg[1]);
2648         fas216_writeb(info, REG_CNTL3, info->scsi.cfg[2]);
2649         fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
2650         fas216_writeb(info, REG_SOF, 0);
2651         fas216_writeb(info, REG_STP, info->scsi.async_stp);
2652         fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
2653 }
2654
2655 /**
2656  * fas216_eh_host_reset - Reset the host associated with this command
2657  * @SCpnt: command specifing host to reset
2658  *
2659  * Reset the host associated with this command.
2660  * Returns: FAILED if unable to reset.
2661  * Notes: io_request_lock is taken, and irqs are disabled
2662  */
2663 int fas216_eh_host_reset(Scsi_Cmnd *SCpnt)
2664 {
2665         FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2666
2667         fas216_checkmagic(info);
2668
2669         printk("scsi%d.%c: %s: resetting host\n",
2670                 info->host->host_no, '0' + SCpnt->device->id, __FUNCTION__);
2671
2672         /*
2673          * Reset the SCSI chip.
2674          */
2675         fas216_cmd(info, CMD_RESETCHIP);
2676
2677         /*
2678          * Ugly ugly ugly!
2679          * We need to release the host_lock and enable
2680          * IRQs if we sleep, but we must relock and disable
2681          * IRQs after the sleep.
2682          */
2683         spin_unlock_irq(info->host->host_lock);
2684         scsi_sleep(50 * HZ/100);
2685         spin_lock_irq(info->host->host_lock);
2686
2687         /*
2688          * Release the SCSI reset.
2689          */
2690         fas216_cmd(info, CMD_NOP);
2691
2692         fas216_init_chip(info);
2693
2694         return SUCCESS;
2695 }
2696
2697 #define TYPE_UNKNOWN    0
2698 #define TYPE_NCR53C90   1
2699 #define TYPE_NCR53C90A  2
2700 #define TYPE_NCR53C9x   3
2701 #define TYPE_Am53CF94   4
2702 #define TYPE_EmFAS216   5
2703 #define TYPE_QLFAS216   6
2704
2705 static char *chip_types[] = {
2706         "unknown",
2707         "NS NCR53C90",
2708         "NS NCR53C90A",
2709         "NS NCR53C9x",
2710         "AMD Am53CF94",
2711         "Emulex FAS216",
2712         "QLogic FAS216"
2713 };
2714
2715 static int fas216_detect_type(FAS216_Info *info)
2716 {
2717         int family, rev;
2718
2719         /*
2720          * Reset the chip.
2721          */
2722         fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2723         udelay(50);
2724         fas216_writeb(info, REG_CMD, CMD_NOP);
2725
2726         /*
2727          * Check to see if control reg 2 is present.
2728          */
2729         fas216_writeb(info, REG_CNTL3, 0);
2730         fas216_writeb(info, REG_CNTL2, CNTL2_S2FE);
2731
2732         /*
2733          * If we are unable to read back control reg 2
2734          * correctly, it is not present, and we have a
2735          * NCR53C90.
2736          */
2737         if ((fas216_readb(info, REG_CNTL2) & (~0xe0)) != CNTL2_S2FE)
2738                 return TYPE_NCR53C90;
2739
2740         /*
2741          * Now, check control register 3
2742          */
2743         fas216_writeb(info, REG_CNTL2, 0);
2744         fas216_writeb(info, REG_CNTL3, 0);
2745         fas216_writeb(info, REG_CNTL3, 5);
2746
2747         /*
2748          * If we are unable to read the register back
2749          * correctly, we have a NCR53C90A
2750          */
2751         if (fas216_readb(info, REG_CNTL3) != 5)
2752                 return TYPE_NCR53C90A;
2753
2754         /*
2755          * Now read the ID from the chip.
2756          */
2757         fas216_writeb(info, REG_CNTL3, 0);
2758
2759         fas216_writeb(info, REG_CNTL3, CNTL3_ADIDCHK);
2760         fas216_writeb(info, REG_CNTL3, 0);
2761
2762         fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2763         udelay(50);
2764         fas216_writeb(info, REG_CMD, CMD_WITHDMA | CMD_NOP);
2765
2766         fas216_writeb(info, REG_CNTL2, CNTL2_ENF);
2767         fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2768         udelay(50);
2769         fas216_writeb(info, REG_CMD, CMD_NOP);
2770
2771         rev     = fas216_readb(info, REG_ID);
2772         family  = rev >> 3;
2773         rev    &= 7;
2774
2775         switch (family) {
2776         case 0x01:
2777                 if (rev == 4)
2778                         return TYPE_Am53CF94;
2779                 break;
2780
2781         case 0x02:
2782                 switch (rev) {
2783                 case 2:
2784                         return TYPE_EmFAS216;
2785                 case 3:
2786                         return TYPE_QLFAS216;
2787                 }
2788                 break;
2789
2790         default:
2791                 break;
2792         }
2793         printk("family %x rev %x\n", family, rev);
2794         return TYPE_NCR53C9x;
2795 }
2796
2797 /**
2798  * fas216_reset_state - Initialise driver internal state
2799  * @info: state to initialise
2800  *
2801  * Initialise driver internal state
2802  */
2803 static void fas216_reset_state(FAS216_Info *info)
2804 {
2805         int i;
2806
2807         fas216_checkmagic(info);
2808
2809         fas216_bus_reset(info);
2810
2811         /*
2812          * Clear out all stale info in our state structure
2813          */
2814         memset(info->busyluns, 0, sizeof(info->busyluns));
2815         info->scsi.disconnectable = 0;
2816         info->scsi.aborting = 0;
2817
2818         for (i = 0; i < 8; i++) {
2819                 info->device[i].parity_enabled  = 0;
2820                 info->device[i].parity_check    = 1;
2821         }
2822
2823         /*
2824          * Drain all commands on disconnected queue
2825          */
2826         while (queue_remove(&info->queues.disconnected) != NULL);
2827
2828         /*
2829          * Remove executing commands.
2830          */
2831         info->SCpnt     = NULL;
2832         info->reqSCpnt  = NULL;
2833         info->rstSCpnt  = NULL;
2834         info->origSCpnt = NULL;
2835 }
2836
2837 /**
2838  * fas216_init - initialise FAS/NCR/AMD SCSI structures.
2839  * @host: a driver-specific filled-out structure
2840  *
2841  * Initialise FAS/NCR/AMD SCSI structures.
2842  * Returns: 0 on success
2843  */
2844 int fas216_init(struct Scsi_Host *host)
2845 {
2846         FAS216_Info *info = (FAS216_Info *)host->hostdata;
2847
2848         info->magic_start    = MAGIC;
2849         info->magic_end      = MAGIC;
2850         info->host           = host;
2851         info->scsi.cfg[0]    = host->this_id | CNTL1_PERE;
2852         info->scsi.cfg[1]    = CNTL2_ENF | CNTL2_S2FE;
2853         info->scsi.cfg[2]    = info->ifcfg.cntl3 |
2854                                CNTL3_ADIDCHK | CNTL3_QTAG | CNTL3_G2CB | CNTL3_LBTM;
2855         info->scsi.async_stp = fas216_syncperiod(info, info->ifcfg.asyncperiod);
2856
2857         info->rst_dev_status = -1;
2858         info->rst_bus_status = -1;
2859         init_waitqueue_head(&info->eh_wait);
2860         init_timer(&info->eh_timer);
2861         info->eh_timer.data  = (unsigned long)info;
2862         info->eh_timer.function = fas216_eh_timer;
2863         
2864         spin_lock_init(&info->host_lock);
2865
2866         memset(&info->stats, 0, sizeof(info->stats));
2867
2868         msgqueue_initialise(&info->scsi.msgs);
2869
2870         if (!queue_initialise(&info->queues.issue))
2871                 return -ENOMEM;
2872
2873         if (!queue_initialise(&info->queues.disconnected)) {
2874                 queue_free(&info->queues.issue);
2875                 return -ENOMEM;
2876         }
2877
2878         return 0;
2879 }
2880
2881 /**
2882  * fas216_add - initialise FAS/NCR/AMD SCSI ic.
2883  * @host: a driver-specific filled-out structure
2884  * @dev: parent device
2885  *
2886  * Initialise FAS/NCR/AMD SCSI ic.
2887  * Returns: 0 on success
2888  */
2889 int fas216_add(struct Scsi_Host *host, struct device *dev)
2890 {
2891         FAS216_Info *info = (FAS216_Info *)host->hostdata;
2892         int type, ret;
2893
2894         if (info->ifcfg.clockrate <= 10 || info->ifcfg.clockrate > 40) {
2895                 printk(KERN_CRIT "fas216: invalid clock rate %u MHz\n",
2896                         info->ifcfg.clockrate);
2897                 return -EINVAL;
2898         }
2899
2900         fas216_reset_state(info);
2901         type = fas216_detect_type(info);
2902         info->scsi.type = chip_types[type];
2903
2904         udelay(300);
2905
2906         /*
2907          * Initialise the chip correctly.
2908          */
2909         fas216_init_chip(info);
2910
2911         /*
2912          * Reset the SCSI bus.  We don't want to see
2913          * the resulting reset interrupt, so mask it
2914          * out.
2915          */
2916         fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0] | CNTL1_DISR);
2917         fas216_writeb(info, REG_CMD, CMD_RESETSCSI);
2918
2919         /*
2920          * scsi standard says wait 250ms
2921          */
2922         spin_unlock_irq(info->host->host_lock);
2923         scsi_sleep(100*HZ/100);
2924         spin_lock_irq(info->host->host_lock);
2925
2926         fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
2927         fas216_readb(info, REG_INST);
2928
2929         fas216_checkmagic(info);
2930
2931         ret = scsi_add_host(host, dev);
2932         if (ret)
2933                 fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2934         else
2935                 scsi_scan_host(host);
2936
2937         return ret;
2938 }
2939
2940 void fas216_remove(struct Scsi_Host *host)
2941 {
2942         FAS216_Info *info = (FAS216_Info *)host->hostdata;
2943
2944         fas216_checkmagic(info);
2945         scsi_remove_host(host);
2946
2947         fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2948         scsi_host_put(host);
2949 }
2950
2951 /**
2952  * fas216_release - release all resources for FAS/NCR/AMD SCSI ic.
2953  * @host: a driver-specific filled-out structure
2954  *
2955  * release all resources and put everything to bed for FAS/NCR/AMD SCSI ic.
2956  */
2957 void fas216_release(struct Scsi_Host *host)
2958 {
2959         FAS216_Info *info = (FAS216_Info *)host->hostdata;
2960
2961         queue_free(&info->queues.disconnected);
2962         queue_free(&info->queues.issue);
2963 }
2964
2965 int fas216_print_host(FAS216_Info *info, char *buffer)
2966 {
2967         return sprintf(buffer,
2968                         "\n"
2969                         "Chip    : %s\n"
2970                         " Address: 0x%08lx\n"
2971                         " IRQ    : %d\n"
2972                         " DMA    : %d\n",
2973                         info->scsi.type, info->host->io_port,
2974                         info->host->irq, info->host->dma_channel);
2975 }
2976
2977 int fas216_print_stats(FAS216_Info *info, char *buffer)
2978 {
2979         char *p = buffer;
2980
2981         p += sprintf(p, "\n"
2982                         "Command Statistics:\n"
2983                         " Queued     : %u\n"
2984                         " Issued     : %u\n"
2985                         " Completed  : %u\n"
2986                         " Reads      : %u\n"
2987                         " Writes     : %u\n"
2988                         " Others     : %u\n"
2989                         " Disconnects: %u\n"
2990                         " Aborts     : %u\n"
2991                         " Bus resets : %u\n"
2992                         " Host resets: %u\n",
2993                         info->stats.queues,      info->stats.removes,
2994                         info->stats.fins,        info->stats.reads,
2995                         info->stats.writes,      info->stats.miscs,
2996                         info->stats.disconnects, info->stats.aborts,
2997                         info->stats.bus_resets,  info->stats.host_resets);
2998
2999         return p - buffer;
3000 }
3001
3002 int fas216_print_devices(FAS216_Info *info, char *buffer)
3003 {
3004         struct fas216_device *dev;
3005         Scsi_Device *scd;
3006         char *p = buffer;
3007
3008         p += sprintf(p, "Device/Lun TaggedQ       Parity   Sync\n");
3009
3010         shost_for_each_device(scd, info->host) {
3011                 dev = &info->device[scd->id];
3012                 p += sprintf(p, "     %d/%d   ", scd->id, scd->lun);
3013                 if (scd->tagged_supported)
3014                         p += sprintf(p, "%3sabled(%3d) ",
3015                                      scd->simple_tags ? "en" : "dis",
3016                                      scd->current_tag);
3017                 else
3018                         p += sprintf(p, "unsupported   ");
3019
3020                 p += sprintf(p, "%3sabled ", dev->parity_enabled ? "en" : "dis");
3021
3022                 if (dev->sof)
3023                         p += sprintf(p, "offset %d, %d ns\n",
3024                                      dev->sof, dev->period * 4);
3025                 else
3026                         p += sprintf(p, "async\n");
3027         }
3028
3029         return p - buffer;
3030 }
3031
3032 EXPORT_SYMBOL(fas216_init);
3033 EXPORT_SYMBOL(fas216_add);
3034 EXPORT_SYMBOL(fas216_queue_command);
3035 EXPORT_SYMBOL(fas216_noqueue_command);
3036 EXPORT_SYMBOL(fas216_intr);
3037 EXPORT_SYMBOL(fas216_remove);
3038 EXPORT_SYMBOL(fas216_release);
3039 EXPORT_SYMBOL(fas216_eh_abort);
3040 EXPORT_SYMBOL(fas216_eh_device_reset);
3041 EXPORT_SYMBOL(fas216_eh_bus_reset);
3042 EXPORT_SYMBOL(fas216_eh_host_reset);
3043 EXPORT_SYMBOL(fas216_print_host);
3044 EXPORT_SYMBOL(fas216_print_stats);
3045 EXPORT_SYMBOL(fas216_print_devices);
3046
3047 MODULE_AUTHOR("Russell King");
3048 MODULE_DESCRIPTION("Generic FAS216/NCR53C9x driver core");
3049 MODULE_LICENSE("GPL");