2 * linux/drivers/acorn/scsi/fas216.c
4 * Copyright (C) 1997-2003 Russell King
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.
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
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 {
19 * ... my host specific data ...
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
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.
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>
52 #include <asm/ecard.h>
55 #include <scsi/scsi_dbg.h>
56 #include <scsi/scsi_host.h>
60 /* NOTE: SCSI2 Synchronous transfers *require* DMA according to
61 * the data sheet. This restriction is crazy, especially when
62 * you only want to send 16 bytes! What were the guys who
63 * designed this chip on at that time? Did they read the SCSI2
64 * spec at all? The following sections are taken from the SCSI2
65 * standard (s2r10) concerning this:
67 * > IMPLEMENTORS NOTES:
68 * > (1) Re-negotiation at every selection is not recommended, since a
69 * > significant performance impact is likely.
71 * > The implied synchronous agreement shall remain in effect until a BUS DEVICE
72 * > RESET message is received, until a hard reset condition occurs, or until one
73 * > of the two SCSI devices elects to modify the agreement. The default data
74 * > transfer mode is asynchronous data transfer mode. The default data transfer
75 * > mode is entered at power on, after a BUS DEVICE RESET message, or after a hard
78 * In total, this means that once you have elected to use synchronous
79 * transfers, you must always use DMA.
81 * I was thinking that this was a good chip until I found this restriction ;(
89 #undef CHECK_STRUCTURE
91 #define LOG_CONNECT (1 << 0)
92 #define LOG_BUSSERVICE (1 << 1)
93 #define LOG_FUNCTIONDONE (1 << 2)
94 #define LOG_MESSAGES (1 << 3)
95 #define LOG_BUFFER (1 << 4)
96 #define LOG_ERROR (1 << 8)
98 static int level_mask = LOG_ERROR;
100 module_param(level_mask, int, 0644);
102 static int __init fas216_log_setup(char *str)
108 while ((s = strsep(&str, ",")) != NULL) {
111 if (strcmp(s, "all") == 0)
115 if (strncmp(s, "bus", 3) == 0)
116 level_mask |= LOG_BUSSERVICE;
117 if (strncmp(s, "buf", 3) == 0)
118 level_mask |= LOG_BUFFER;
121 level_mask |= LOG_CONNECT;
124 level_mask |= LOG_ERROR;
127 level_mask |= LOG_MESSAGES;
130 if (strcmp(s, "none") == 0)
134 level_mask |= LOG_FUNCTIONDONE;
141 __setup("fas216_logging=", fas216_log_setup);
143 static inline unsigned char fas216_readb(FAS216_Info *info, unsigned int reg)
145 unsigned int off = reg << info->scsi.io_shift;
146 return readb(info->scsi.io_base + off);
149 static inline void fas216_writeb(FAS216_Info *info, unsigned int reg, unsigned int val)
151 unsigned int off = reg << info->scsi.io_shift;
152 writeb(val, info->scsi.io_base + off);
155 static void fas216_dumpstate(FAS216_Info *info)
157 unsigned char is, stat, inst;
159 is = fas216_readb(info, REG_IS);
160 stat = fas216_readb(info, REG_STAT);
161 inst = fas216_readb(info, REG_INST);
163 printk("FAS216: CTCL=%02X CTCM=%02X CMD=%02X STAT=%02X"
164 " INST=%02X IS=%02X CFIS=%02X",
165 fas216_readb(info, REG_CTCL),
166 fas216_readb(info, REG_CTCM),
167 fas216_readb(info, REG_CMD), stat, inst, is,
168 fas216_readb(info, REG_CFIS));
169 printk(" CNTL1=%02X CNTL2=%02X CNTL3=%02X CTCH=%02X\n",
170 fas216_readb(info, REG_CNTL1),
171 fas216_readb(info, REG_CNTL2),
172 fas216_readb(info, REG_CNTL3),
173 fas216_readb(info, REG_CTCH));
176 static void print_SCp(struct scsi_pointer *SCp, const char *prefix, const char *suffix)
178 printk("%sptr %p this_residual 0x%x buffer %p buffers_residual 0x%x%s",
179 prefix, SCp->ptr, SCp->this_residual, SCp->buffer,
180 SCp->buffers_residual, suffix);
183 static void fas216_dumpinfo(FAS216_Info *info)
191 printk("FAS216_Info=\n");
192 printk(" { magic_start=%lX host=%p SCpnt=%p origSCpnt=%p\n",
193 info->magic_start, info->host, info->SCpnt,
195 printk(" scsi={ io_shift=%X irq=%X cfg={ %X %X %X %X }\n",
196 info->scsi.io_shift, info->scsi.irq,
197 info->scsi.cfg[0], info->scsi.cfg[1], info->scsi.cfg[2],
199 printk(" type=%p phase=%X\n",
200 info->scsi.type, info->scsi.phase);
201 print_SCp(&info->scsi.SCp, " SCp={ ", " }\n");
202 printk(" msgs async_stp=%X disconnectable=%d aborting=%d }\n",
203 info->scsi.async_stp,
204 info->scsi.disconnectable, info->scsi.aborting);
205 printk(" stats={ queues=%X removes=%X fins=%X reads=%X writes=%X miscs=%X\n"
206 " disconnects=%X aborts=%X bus_resets=%X host_resets=%X}\n",
207 info->stats.queues, info->stats.removes, info->stats.fins,
208 info->stats.reads, info->stats.writes, info->stats.miscs,
209 info->stats.disconnects, info->stats.aborts, info->stats.bus_resets,
210 info->stats.host_resets);
211 printk(" ifcfg={ clockrate=%X select_timeout=%X asyncperiod=%X sync_max_depth=%X }\n",
212 info->ifcfg.clockrate, info->ifcfg.select_timeout,
213 info->ifcfg.asyncperiod, info->ifcfg.sync_max_depth);
214 for (i = 0; i < 8; i++) {
215 printk(" busyluns[%d]=%08lx dev[%d]={ disconnect_ok=%d stp=%X sof=%X sync_state=%X }\n",
216 i, info->busyluns[i], i,
217 info->device[i].disconnect_ok, info->device[i].stp,
218 info->device[i].sof, info->device[i].sync_state);
220 printk(" dma={ transfer_type=%X setup=%p pseudo=%p stop=%p }\n",
221 info->dma.transfer_type, info->dma.setup,
222 info->dma.pseudo, info->dma.stop);
223 printk(" internal_done=%X magic_end=%lX }\n",
224 info->internal_done, info->magic_end);
227 #ifdef CHECK_STRUCTURE
228 static void __fas216_checkmagic(FAS216_Info *info, const char *func)
231 if (info->magic_start != MAGIC) {
232 printk(KERN_CRIT "FAS216 Error: magic at start corrupted\n");
235 if (info->magic_end != MAGIC) {
236 printk(KERN_CRIT "FAS216 Error: magic at end corrupted\n");
240 fas216_dumpinfo(info);
241 panic("scsi memory space corrupted in %s", func);
244 #define fas216_checkmagic(info) __fas216_checkmagic((info), __FUNCTION__)
246 #define fas216_checkmagic(info)
249 static const char *fas216_bus_phase(int stat)
251 static const char *phases[] = {
252 "DATA OUT", "DATA IN",
254 "MISC OUT", "MISC IN",
255 "MESG OUT", "MESG IN"
258 return phases[stat & STAT_BUSMASK];
261 static const char *fas216_drv_phase(FAS216_Info *info)
263 static const char *phases[] = {
264 [PHASE_IDLE] = "idle",
265 [PHASE_SELECTION] = "selection",
266 [PHASE_COMMAND] = "command",
267 [PHASE_DATAOUT] = "data out",
268 [PHASE_DATAIN] = "data in",
269 [PHASE_MSGIN] = "message in",
270 [PHASE_MSGIN_DISCONNECT]= "disconnect",
271 [PHASE_MSGOUT_EXPECT] = "expect message out",
272 [PHASE_MSGOUT] = "message out",
273 [PHASE_STATUS] = "status",
274 [PHASE_DONE] = "done",
277 if (info->scsi.phase < ARRAY_SIZE(phases) &&
278 phases[info->scsi.phase])
279 return phases[info->scsi.phase];
283 static char fas216_target(FAS216_Info *info)
286 return '0' + info->SCpnt->device->id;
292 fas216_do_log(FAS216_Info *info, char target, char *fmt, va_list ap)
294 static char buf[1024];
296 vsnprintf(buf, sizeof(buf), fmt, ap);
297 printk("scsi%d.%c: %s", info->host->host_no, target, buf);
301 fas216_log_command(FAS216_Info *info, int level, Scsi_Cmnd *SCpnt, char *fmt, ...)
305 if (level != 0 && !(level & level_mask))
309 fas216_do_log(info, '0' + SCpnt->device->id, fmt, args);
313 __scsi_print_command(SCpnt->cmnd);
317 fas216_log_target(FAS216_Info *info, int level, int target, char *fmt, ...)
321 if (level != 0 && !(level & level_mask))
330 fas216_do_log(info, target, fmt, args);
336 static void fas216_log(FAS216_Info *info, int level, char *fmt, ...)
340 if (level != 0 && !(level & level_mask))
344 fas216_do_log(info, fas216_target(info), fmt, args);
352 static struct { int stat, ssr, isr, ph; } ph_list[PH_SIZE];
355 static void add_debug_list(int stat, int ssr, int isr, int ph)
357 ph_list[ph_ptr].stat = stat;
358 ph_list[ph_ptr].ssr = ssr;
359 ph_list[ph_ptr].isr = isr;
360 ph_list[ph_ptr].ph = ph;
362 ph_ptr = (ph_ptr + 1) & (PH_SIZE-1);
365 static struct { int command; void *from; } cmd_list[8];
368 static void fas216_cmd(FAS216_Info *info, unsigned int command)
370 cmd_list[cmd_ptr].command = command;
371 cmd_list[cmd_ptr].from = __builtin_return_address(0);
373 cmd_ptr = (cmd_ptr + 1) & 7;
375 fas216_writeb(info, REG_CMD, command);
378 static void print_debug_list(void)
384 printk(KERN_ERR "SCSI IRQ trail\n");
386 printk(" %02x:%02x:%02x:%1x",
387 ph_list[i].stat, ph_list[i].ssr,
388 ph_list[i].isr, ph_list[i].ph);
389 i = (i + 1) & (PH_SIZE - 1);
390 if (((i ^ ph_ptr) & 7) == 0)
392 } while (i != ph_ptr);
393 if ((i ^ ph_ptr) & 7)
397 printk(KERN_ERR "FAS216 commands: ");
399 printk("%02x:%p ", cmd_list[i].command, cmd_list[i].from);
401 } while (i != cmd_ptr);
405 static void fas216_done(FAS216_Info *info, unsigned int result);
408 * fas216_get_last_msg - retrive last message from the list
409 * @info: interface to search
410 * @pos: current fifo position
412 * Retrieve a last message from the list, using position in fifo.
414 static inline unsigned short
415 fas216_get_last_msg(FAS216_Info *info, int pos)
417 unsigned short packed_msg = NOP;
421 while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
422 if (pos >= msg->fifo)
427 if (msg->msg[0] == EXTENDED_MESSAGE)
428 packed_msg = EXTENDED_MESSAGE | msg->msg[2] << 8;
430 packed_msg = msg->msg[0];
433 fas216_log(info, LOG_MESSAGES,
434 "Message: %04x found at position %02x\n", packed_msg, pos);
440 * fas216_syncperiod - calculate STP register value
441 * @info: state structure for interface connected to device
442 * @ns: period in ns (between subsequent bytes)
444 * Calculate value to be loaded into the STP register for a given period
445 * in ns. Returns a value suitable for REG_STP.
447 static int fas216_syncperiod(FAS216_Info *info, int ns)
449 int value = (info->ifcfg.clockrate * ns) / 1000;
451 fas216_checkmagic(info);
462 * fas216_set_sync - setup FAS216 chip for specified transfer period.
463 * @info: state structure for interface connected to device
466 * Correctly setup FAS216 chip for specified transfer period.
467 * Notes : we need to switch the chip out of FASTSCSI mode if we have
468 * a transfer period >= 200ns - otherwise the chip will violate
471 static void fas216_set_sync(FAS216_Info *info, int target)
475 fas216_writeb(info, REG_SOF, info->device[target].sof);
476 fas216_writeb(info, REG_STP, info->device[target].stp);
478 cntl3 = info->scsi.cfg[2];
479 if (info->device[target].period >= (200 / 4))
480 cntl3 = cntl3 & ~CNTL3_FASTSCSI;
482 fas216_writeb(info, REG_CNTL3, cntl3);
485 /* Synchronous transfer support
487 * Note: The SCSI II r10 spec says (5.6.12):
489 * (2) Due to historical problems with early host adapters that could
490 * not accept an SDTR message, some targets may not initiate synchronous
491 * negotiation after a power cycle as required by this standard. Host
492 * adapters that support synchronous mode may avoid the ensuing failure
493 * modes when the target is independently power cycled by initiating a
494 * synchronous negotiation on each REQUEST SENSE and INQUIRY command.
495 * This approach increases the SCSI bus overhead and is not recommended
496 * for new implementations. The correct method is to respond to an
497 * SDTR message with a MESSAGE REJECT message if the either the
498 * initiator or target devices does not support synchronous transfers
499 * or does not want to negotiate for synchronous transfers at the time.
500 * Using the correct method assures compatibility with wide data
501 * transfers and future enhancements.
503 * We will always initiate a synchronous transfer negotiation request on
504 * every INQUIRY or REQUEST SENSE message, unless the target itself has
505 * at some point performed a synchronous transfer negotiation request, or
506 * we have synchronous transfers disabled for this device.
510 * fas216_handlesync - Handle a synchronous transfer message
511 * @info: state structure for interface
512 * @msg: message from target
514 * Handle a synchronous transfer message from the target
516 static void fas216_handlesync(FAS216_Info *info, char *msg)
518 struct fas216_device *dev = &info->device[info->SCpnt->device->id];
519 enum { sync, async, none, reject } res = none;
524 /* Synchronous transfer request failed.
527 * SCSI devices that are capable of synchronous
528 * data transfers shall not respond to an SDTR
529 * message with a MESSAGE REJECT message.
531 * Hence, if we get this condition, we disable
532 * negotiation for this device.
534 if (dev->sync_state == neg_inprogress) {
535 dev->sync_state = neg_invalid;
540 case EXTENDED_MESSAGE:
541 switch (dev->sync_state) {
542 /* We don't accept synchronous transfer requests.
543 * Respond with a MESSAGE_REJECT to prevent a
544 * synchronous transfer agreement from being reached.
550 /* We were not negotiating a synchronous transfer,
551 * but the device sent us a negotiation request.
552 * Honour the request by sending back a SDTR
553 * message containing our capability, limited by
554 * the targets capability.
557 fas216_cmd(info, CMD_SETATN);
558 if (msg[4] > info->ifcfg.sync_max_depth)
559 msg[4] = info->ifcfg.sync_max_depth;
560 if (msg[3] < 1000 / info->ifcfg.clockrate)
561 msg[3] = 1000 / info->ifcfg.clockrate;
563 msgqueue_flush(&info->scsi.msgs);
564 msgqueue_addmsg(&info->scsi.msgs, 5,
565 EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
567 info->scsi.phase = PHASE_MSGOUT_EXPECT;
569 /* This is wrong. The agreement is not in effect
570 * until this message is accepted by the device
572 dev->sync_state = neg_targcomplete;
576 /* We initiated the synchronous transfer negotiation,
577 * and have successfully received a response from the
578 * target. The synchronous transfer agreement has been
579 * reached. Note: if the values returned are out of our
580 * bounds, we must reject the message.
584 if (msg[4] <= info->ifcfg.sync_max_depth &&
585 msg[3] >= 1000 / info->ifcfg.clockrate) {
586 dev->sync_state = neg_complete;
598 dev->period = msg[3];
600 dev->stp = fas216_syncperiod(info, msg[3] * 4);
601 fas216_set_sync(info, info->SCpnt->device->id);
605 fas216_cmd(info, CMD_SETATN);
606 msgqueue_flush(&info->scsi.msgs);
607 msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
608 info->scsi.phase = PHASE_MSGOUT_EXPECT;
611 dev->period = info->ifcfg.asyncperiod / 4;
613 dev->stp = info->scsi.async_stp;
614 fas216_set_sync(info, info->SCpnt->device->id);
623 * fas216_updateptrs - update data pointers after transfer suspended/paused
624 * @info: interface's local pointer to update
625 * @bytes_transferred: number of bytes transferred
627 * Update data pointers after transfer suspended/paused
629 static void fas216_updateptrs(FAS216_Info *info, int bytes_transferred)
631 struct scsi_pointer *SCp = &info->scsi.SCp;
633 fas216_checkmagic(info);
635 BUG_ON(bytes_transferred < 0);
637 info->SCpnt->request_bufflen -= bytes_transferred;
639 while (bytes_transferred != 0) {
640 if (SCp->this_residual > bytes_transferred)
643 * We have used up this buffer. Move on to the
646 bytes_transferred -= SCp->this_residual;
647 if (!next_SCp(SCp) && bytes_transferred) {
648 printk(KERN_WARNING "scsi%d.%c: out of buffers\n",
649 info->host->host_no, '0' + info->SCpnt->device->id);
654 SCp->this_residual -= bytes_transferred;
655 if (SCp->this_residual)
656 SCp->ptr += bytes_transferred;
662 * fas216_pio - transfer data off of/on to card using programmed IO
663 * @info: interface to transfer data to/from
664 * @direction: direction to transfer data (DMA_OUT/DMA_IN)
666 * Transfer data off of/on to card using programmed IO.
667 * Notes: this is incredibly slow.
669 static void fas216_pio(FAS216_Info *info, fasdmadir_t direction)
671 struct scsi_pointer *SCp = &info->scsi.SCp;
673 fas216_checkmagic(info);
675 if (direction == DMA_OUT)
676 fas216_writeb(info, REG_FF, get_next_SCp_byte(SCp));
678 put_next_SCp_byte(SCp, fas216_readb(info, REG_FF));
680 if (SCp->this_residual == 0)
684 static void fas216_set_stc(FAS216_Info *info, unsigned int length)
686 fas216_writeb(info, REG_STCL, length);
687 fas216_writeb(info, REG_STCM, length >> 8);
688 fas216_writeb(info, REG_STCH, length >> 16);
691 static unsigned int fas216_get_ctc(FAS216_Info *info)
693 return fas216_readb(info, REG_CTCL) +
694 (fas216_readb(info, REG_CTCM) << 8) +
695 (fas216_readb(info, REG_CTCH) << 16);
699 * fas216_cleanuptransfer - clean up after a transfer has completed.
700 * @info: interface to clean up
702 * Update the data pointers according to the number of bytes transferred
705 static void fas216_cleanuptransfer(FAS216_Info *info)
707 unsigned long total, residual, fifo;
708 fasdmatype_t dmatype = info->dma.transfer_type;
710 info->dma.transfer_type = fasdma_none;
713 * PIO transfers do not need to be cleaned up.
715 if (dmatype == fasdma_pio || dmatype == fasdma_none)
718 if (dmatype == fasdma_real_all)
719 total = info->SCpnt->request_bufflen;
721 total = info->scsi.SCp.this_residual;
723 residual = fas216_get_ctc(info);
725 fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
727 fas216_log(info, LOG_BUFFER, "cleaning up from previous "
728 "transfer: length 0x%06x, residual 0x%x, fifo %d",
729 total, residual, fifo);
732 * If we were performing Data-Out, the transfer counter
733 * counts down each time a byte is transferred by the
734 * host to the FIFO. This means we must include the
735 * bytes left in the FIFO from the transfer counter.
737 if (info->scsi.phase == PHASE_DATAOUT)
740 fas216_updateptrs(info, total - residual);
744 * fas216_transfer - Perform a DMA/PIO transfer off of/on to card
745 * @info: interface from which device disconnected from
747 * Start a DMA/PIO transfer off of/on to card
749 static void fas216_transfer(FAS216_Info *info)
751 fasdmadir_t direction;
752 fasdmatype_t dmatype;
754 fas216_log(info, LOG_BUFFER,
755 "starttransfer: buffer %p length 0x%06x reqlen 0x%06x",
756 info->scsi.SCp.ptr, info->scsi.SCp.this_residual,
757 info->SCpnt->request_bufflen);
759 if (!info->scsi.SCp.ptr) {
760 fas216_log(info, LOG_ERROR, "null buffer passed to "
761 "fas216_starttransfer");
762 print_SCp(&info->scsi.SCp, "SCp: ", "\n");
763 print_SCp(&info->SCpnt->SCp, "Cmnd SCp: ", "\n");
768 * If we have a synchronous transfer agreement in effect, we must
769 * use DMA mode. If we are using asynchronous transfers, we may
770 * use DMA mode or PIO mode.
772 if (info->device[info->SCpnt->device->id].sof)
773 dmatype = fasdma_real_all;
775 dmatype = fasdma_pio;
777 if (info->scsi.phase == PHASE_DATAOUT)
783 dmatype = info->dma.setup(info->host, &info->scsi.SCp,
785 info->dma.transfer_type = dmatype;
787 if (dmatype == fasdma_real_all)
788 fas216_set_stc(info, info->SCpnt->request_bufflen);
790 fas216_set_stc(info, info->scsi.SCp.this_residual);
794 fas216_log(info, LOG_BUFFER, "PIO transfer");
795 fas216_writeb(info, REG_SOF, 0);
796 fas216_writeb(info, REG_STP, info->scsi.async_stp);
797 fas216_cmd(info, CMD_TRANSFERINFO);
798 fas216_pio(info, direction);
802 fas216_log(info, LOG_BUFFER, "pseudo transfer");
803 fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
804 info->dma.pseudo(info->host, &info->scsi.SCp,
805 direction, info->SCpnt->transfersize);
808 case fasdma_real_block:
809 fas216_log(info, LOG_BUFFER, "block dma transfer");
810 fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
813 case fasdma_real_all:
814 fas216_log(info, LOG_BUFFER, "total dma transfer");
815 fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
819 fas216_log(info, LOG_BUFFER | LOG_ERROR,
820 "invalid FAS216 DMA type");
826 * fas216_stoptransfer - Stop a DMA transfer onto / off of the card
827 * @info: interface from which device disconnected from
829 * Called when we switch away from DATA IN or DATA OUT phases.
831 static void fas216_stoptransfer(FAS216_Info *info)
833 fas216_checkmagic(info);
835 if (info->dma.transfer_type == fasdma_real_all ||
836 info->dma.transfer_type == fasdma_real_block)
837 info->dma.stop(info->host, &info->scsi.SCp);
839 fas216_cleanuptransfer(info);
841 if (info->scsi.phase == PHASE_DATAIN) {
845 * If we were performing Data-In, then the FIFO counter
846 * contains the number of bytes not transferred via DMA
847 * from the on-board FIFO. Read them manually.
849 fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
850 while (fifo && info->scsi.SCp.ptr) {
851 *info->scsi.SCp.ptr = fas216_readb(info, REG_FF);
852 fas216_updateptrs(info, 1);
857 * After a Data-Out phase, there may be unsent
858 * bytes left in the FIFO. Flush them out.
860 fas216_cmd(info, CMD_FLUSHFIFO);
864 static void fas216_aborttransfer(FAS216_Info *info)
866 fas216_checkmagic(info);
868 if (info->dma.transfer_type == fasdma_real_all ||
869 info->dma.transfer_type == fasdma_real_block)
870 info->dma.stop(info->host, &info->scsi.SCp);
872 info->dma.transfer_type = fasdma_none;
873 fas216_cmd(info, CMD_FLUSHFIFO);
876 static void fas216_kick(FAS216_Info *info);
879 * fas216_disconnected_intr - handle device disconnection
880 * @info: interface from which device disconnected from
882 * Handle device disconnection
884 static void fas216_disconnect_intr(FAS216_Info *info)
888 fas216_checkmagic(info);
890 fas216_log(info, LOG_CONNECT, "disconnect phase=%02x",
893 msgqueue_flush(&info->scsi.msgs);
895 switch (info->scsi.phase) {
896 case PHASE_SELECTION: /* while selecting - no target */
898 fas216_done(info, DID_NO_CONNECT);
901 case PHASE_MSGIN_DISCONNECT: /* message in - disconnecting */
902 info->scsi.disconnectable = 1;
903 info->scsi.phase = PHASE_IDLE;
904 info->stats.disconnects += 1;
905 spin_lock_irqsave(&info->host_lock, flags);
906 if (info->scsi.phase == PHASE_IDLE)
908 spin_unlock_irqrestore(&info->host_lock, flags);
911 case PHASE_DONE: /* at end of command - complete */
912 fas216_done(info, DID_OK);
915 case PHASE_MSGOUT: /* message out - possible ABORT message */
916 if (fas216_get_last_msg(info, info->scsi.msgin_fifo) == ABORT) {
917 info->scsi.aborting = 0;
918 fas216_done(info, DID_ABORT);
923 printk(KERN_ERR "scsi%d.%c: unexpected disconnect in phase %s\n",
924 info->host->host_no, fas216_target(info), fas216_drv_phase(info));
926 fas216_stoptransfer(info);
927 fas216_done(info, DID_ERROR);
933 * fas216_reselected_intr - start reconnection of a device
934 * @info: interface which was reselected
936 * Start reconnection of a device
939 fas216_reselected_intr(FAS216_Info *info)
941 unsigned int cfis, i;
942 unsigned char msg[4];
943 unsigned char target, lun, tag;
945 fas216_checkmagic(info);
947 WARN_ON(info->scsi.phase == PHASE_SELECTION ||
948 info->scsi.phase == PHASE_SELSTEPS);
950 cfis = fas216_readb(info, REG_CFIS);
952 fas216_log(info, LOG_CONNECT, "reconnect phase=%02x cfis=%02x",
953 info->scsi.phase, cfis);
957 if (cfis < 2 || cfis > 4) {
958 printk(KERN_ERR "scsi%d.H: incorrect number of bytes after reselect\n",
959 info->host->host_no);
963 for (i = 0; i < cfis; i++)
964 msg[i] = fas216_readb(info, REG_FF);
966 if (!(msg[0] & (1 << info->host->this_id)) ||
968 goto initiator_error;
970 target = msg[0] & ~(1 << info->host->this_id);
971 target = ffs(target) - 1;
976 if (msg[2] != SIMPLE_QUEUE_TAG)
977 goto initiator_error;
982 /* set up for synchronous transfers */
983 fas216_writeb(info, REG_SDID, target);
984 fas216_set_sync(info, target);
985 msgqueue_flush(&info->scsi.msgs);
987 fas216_log(info, LOG_CONNECT, "Reconnected: target %1x lun %1x tag %02x",
990 if (info->scsi.disconnectable && info->SCpnt) {
991 info->scsi.disconnectable = 0;
992 if (info->SCpnt->device->id == target &&
993 info->SCpnt->device->lun == lun &&
994 info->SCpnt->tag == tag) {
995 fas216_log(info, LOG_CONNECT, "reconnected previously executing command");
997 queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
998 fas216_log(info, LOG_CONNECT, "had to move command to disconnected queue");
1003 info->SCpnt = queue_remove_tgtluntag(&info->queues.disconnected,
1005 fas216_log(info, LOG_CONNECT, "had to get command");
1010 * Restore data pointer from SAVED data pointer
1012 info->scsi.SCp = info->SCpnt->SCp;
1014 fas216_log(info, LOG_CONNECT, "data pointers: [%p, %X]",
1015 info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
1016 info->scsi.phase = PHASE_MSGIN;
1019 * Our command structure not found - abort the
1020 * command on the target. Since we have no
1021 * record of this command, we can't send
1022 * an INITIATOR DETECTED ERROR message.
1024 fas216_cmd(info, CMD_SETATN);
1028 msgqueue_addmsg(&info->scsi.msgs, 2, ABORT_TAG, tag);
1031 msgqueue_addmsg(&info->scsi.msgs, 1, ABORT);
1032 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1033 info->scsi.aborting = 1;
1036 fas216_cmd(info, CMD_MSGACCEPTED);
1040 printk(KERN_ERR "scsi%d.H: error during reselection: bytes",
1041 info->host->host_no);
1042 for (i = 0; i < cfis; i++)
1043 printk(" %02x", msg[i]);
1046 fas216_cmd(info, CMD_SETATN);
1047 msgqueue_flush(&info->scsi.msgs);
1048 msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
1049 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1050 fas216_cmd(info, CMD_MSGACCEPTED);
1053 static void fas216_parse_message(FAS216_Info *info, unsigned char *message, int msglen)
1057 switch (message[0]) {
1058 case COMMAND_COMPLETE:
1062 printk(KERN_ERR "scsi%d.%c: command complete with no "
1063 "status in MESSAGE_IN?\n",
1064 info->host->host_no, fas216_target(info));
1072 * Save current data pointer to SAVED data pointer
1073 * SCSI II standard says that we must not acknowledge
1074 * this until we have really saved pointers.
1075 * NOTE: we DO NOT save the command nor status pointers
1076 * as required by the SCSI II standard. These always
1077 * point to the start of their respective areas.
1079 info->SCpnt->SCp = info->scsi.SCp;
1080 info->SCpnt->SCp.sent_command = 0;
1081 fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
1082 "save data pointers: [%p, %X]",
1083 info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
1086 case RESTORE_POINTERS:
1091 * Restore current data pointer from SAVED data pointer
1093 info->scsi.SCp = info->SCpnt->SCp;
1094 fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
1095 "restore data pointers: [%p, 0x%x]",
1096 info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
1103 info->scsi.phase = PHASE_MSGIN_DISCONNECT;
1106 case MESSAGE_REJECT:
1110 switch (fas216_get_last_msg(info, info->scsi.msgin_fifo)) {
1111 case EXTENDED_MESSAGE | EXTENDED_SDTR << 8:
1112 fas216_handlesync(info, message);
1116 fas216_log(info, 0, "reject, last message 0x%04x",
1117 fas216_get_last_msg(info, info->scsi.msgin_fifo));
1124 case EXTENDED_MESSAGE:
1128 switch (message[2]) {
1129 case EXTENDED_SDTR: /* Sync transfer negotiation request/reply */
1130 fas216_handlesync(info, message);
1144 fas216_log(info, 0, "unrecognised message, rejecting");
1145 printk("scsi%d.%c: message was", info->host->host_no, fas216_target(info));
1146 for (i = 0; i < msglen; i++)
1147 printk("%s%02X", i & 31 ? " " : "\n ", message[i]);
1151 * Something strange seems to be happening here -
1152 * I can't use SETATN since the chip gives me an
1153 * invalid command interrupt when I do. Weird.
1155 fas216_cmd(info, CMD_NOP);
1156 fas216_dumpstate(info);
1157 fas216_cmd(info, CMD_SETATN);
1158 msgqueue_flush(&info->scsi.msgs);
1159 msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
1160 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1161 fas216_dumpstate(info);
1164 static int fas216_wait_cmd(FAS216_Info *info, int cmd)
1169 fas216_cmd(info, cmd);
1171 for (tout = 1000; tout; tout -= 1) {
1172 stat = fas216_readb(info, REG_STAT);
1173 if (stat & (STAT_INT|STAT_PARITYERROR))
1181 static int fas216_get_msg_byte(FAS216_Info *info)
1183 unsigned int stat = fas216_wait_cmd(info, CMD_MSGACCEPTED);
1185 if ((stat & STAT_INT) == 0)
1188 if ((stat & STAT_BUSMASK) != STAT_MESGIN)
1189 goto unexpected_phase_change;
1191 fas216_readb(info, REG_INST);
1193 stat = fas216_wait_cmd(info, CMD_TRANSFERINFO);
1195 if ((stat & STAT_INT) == 0)
1198 if (stat & STAT_PARITYERROR)
1201 if ((stat & STAT_BUSMASK) != STAT_MESGIN)
1202 goto unexpected_phase_change;
1204 fas216_readb(info, REG_INST);
1206 return fas216_readb(info, REG_FF);
1209 fas216_log(info, LOG_ERROR, "timed out waiting for message byte");
1212 unexpected_phase_change:
1213 fas216_log(info, LOG_ERROR, "unexpected phase change: status = %02x", stat);
1217 fas216_log(info, LOG_ERROR, "parity error during message in phase");
1222 * fas216_message - handle a function done interrupt from FAS216 chip
1223 * @info: interface which caused function done interrupt
1225 * Handle a function done interrupt from FAS216 chip
1227 static void fas216_message(FAS216_Info *info)
1229 unsigned char *message = info->scsi.message;
1230 unsigned int msglen = 1;
1233 fas216_checkmagic(info);
1235 message[0] = fas216_readb(info, REG_FF);
1237 if (message[0] == EXTENDED_MESSAGE) {
1238 msgbyte = fas216_get_msg_byte(info);
1241 message[1] = msgbyte;
1243 for (msglen = 2; msglen < message[1] + 2; msglen++) {
1244 msgbyte = fas216_get_msg_byte(info);
1247 message[msglen] = msgbyte;
1257 #ifdef DEBUG_MESSAGES
1261 printk("scsi%d.%c: message in: ",
1262 info->host->host_no, fas216_target(info));
1263 for (i = 0; i < msglen; i++)
1264 printk("%02X ", message[i]);
1269 fas216_parse_message(info, message, msglen);
1270 fas216_cmd(info, CMD_MSGACCEPTED);
1274 fas216_cmd(info, CMD_SETATN);
1275 msgqueue_flush(&info->scsi.msgs);
1276 msgqueue_addmsg(&info->scsi.msgs, 1, MSG_PARITY_ERROR);
1277 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1278 fas216_cmd(info, CMD_MSGACCEPTED);
1283 * fas216_send_command - send command after all message bytes have been sent
1284 * @info: interface which caused bus service
1286 * Send a command to a target after all message bytes have been sent
1288 static void fas216_send_command(FAS216_Info *info)
1292 fas216_checkmagic(info);
1294 fas216_cmd(info, CMD_NOP|CMD_WITHDMA);
1295 fas216_cmd(info, CMD_FLUSHFIFO);
1298 for (i = info->scsi.SCp.sent_command; i < info->SCpnt->cmd_len; i++)
1299 fas216_writeb(info, REG_FF, info->SCpnt->cmnd[i]);
1301 fas216_cmd(info, CMD_TRANSFERINFO);
1303 info->scsi.phase = PHASE_COMMAND;
1307 * fas216_send_messageout - handle bus service to send a message
1308 * @info: interface which caused bus service
1310 * Handle bus service to send a message.
1311 * Note: We do not allow the device to change the data direction!
1313 static void fas216_send_messageout(FAS216_Info *info, int start)
1315 unsigned int tot_msglen = msgqueue_msglength(&info->scsi.msgs);
1317 fas216_checkmagic(info);
1319 fas216_cmd(info, CMD_FLUSHFIFO);
1322 struct message *msg;
1325 while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
1328 for (i = start; i < msg->length; i++)
1329 fas216_writeb(info, REG_FF, msg->msg[i]);
1331 msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
1335 fas216_writeb(info, REG_FF, NOP);
1337 fas216_cmd(info, CMD_TRANSFERINFO);
1339 info->scsi.phase = PHASE_MSGOUT;
1343 * fas216_busservice_intr - handle bus service interrupt from FAS216 chip
1344 * @info: interface which caused bus service interrupt
1345 * @stat: Status register contents
1346 * @is: SCSI Status register contents
1348 * Handle a bus service interrupt from FAS216 chip
1350 static void fas216_busservice_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
1352 fas216_checkmagic(info);
1354 fas216_log(info, LOG_BUSSERVICE,
1355 "bus service: stat=%02x is=%02x phase=%02x",
1356 stat, is, info->scsi.phase);
1358 switch (info->scsi.phase) {
1359 case PHASE_SELECTION:
1360 if ((is & IS_BITS) != IS_MSGBYTESENT)
1364 case PHASE_SELSTEPS:
1365 switch (is & IS_BITS) {
1367 case IS_MSGBYTESENT:
1372 if ((stat & STAT_BUSMASK) == STAT_MESGIN)
1384 fas216_cmd(info, CMD_NOP);
1386 #define STATE(st,ph) ((ph) << 3 | (st))
1387 /* This table describes the legal SCSI state transitions,
1388 * as described by the SCSI II spec.
1390 switch (STATE(stat & STAT_BUSMASK, info->scsi.phase)) {
1391 case STATE(STAT_DATAIN, PHASE_SELSTEPS):/* Sel w/ steps -> Data In */
1392 case STATE(STAT_DATAIN, PHASE_MSGOUT): /* Message Out -> Data In */
1393 case STATE(STAT_DATAIN, PHASE_COMMAND): /* Command -> Data In */
1394 case STATE(STAT_DATAIN, PHASE_MSGIN): /* Message In -> Data In */
1395 info->scsi.phase = PHASE_DATAIN;
1396 fas216_transfer(info);
1399 case STATE(STAT_DATAIN, PHASE_DATAIN): /* Data In -> Data In */
1400 case STATE(STAT_DATAOUT, PHASE_DATAOUT):/* Data Out -> Data Out */
1401 fas216_cleanuptransfer(info);
1402 fas216_transfer(info);
1405 case STATE(STAT_DATAOUT, PHASE_SELSTEPS):/* Sel w/ steps-> Data Out */
1406 case STATE(STAT_DATAOUT, PHASE_MSGOUT): /* Message Out -> Data Out */
1407 case STATE(STAT_DATAOUT, PHASE_COMMAND):/* Command -> Data Out */
1408 case STATE(STAT_DATAOUT, PHASE_MSGIN): /* Message In -> Data Out */
1409 fas216_cmd(info, CMD_FLUSHFIFO);
1410 info->scsi.phase = PHASE_DATAOUT;
1411 fas216_transfer(info);
1414 case STATE(STAT_STATUS, PHASE_DATAOUT): /* Data Out -> Status */
1415 case STATE(STAT_STATUS, PHASE_DATAIN): /* Data In -> Status */
1416 fas216_stoptransfer(info);
1417 case STATE(STAT_STATUS, PHASE_SELSTEPS):/* Sel w/ steps -> Status */
1418 case STATE(STAT_STATUS, PHASE_MSGOUT): /* Message Out -> Status */
1419 case STATE(STAT_STATUS, PHASE_COMMAND): /* Command -> Status */
1420 case STATE(STAT_STATUS, PHASE_MSGIN): /* Message In -> Status */
1421 fas216_cmd(info, CMD_INITCMDCOMPLETE);
1422 info->scsi.phase = PHASE_STATUS;
1425 case STATE(STAT_MESGIN, PHASE_DATAOUT): /* Data Out -> Message In */
1426 case STATE(STAT_MESGIN, PHASE_DATAIN): /* Data In -> Message In */
1427 fas216_stoptransfer(info);
1428 case STATE(STAT_MESGIN, PHASE_COMMAND): /* Command -> Message In */
1429 case STATE(STAT_MESGIN, PHASE_SELSTEPS):/* Sel w/ steps -> Message In */
1430 case STATE(STAT_MESGIN, PHASE_MSGOUT): /* Message Out -> Message In */
1431 info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
1432 fas216_cmd(info, CMD_FLUSHFIFO);
1433 fas216_cmd(info, CMD_TRANSFERINFO);
1434 info->scsi.phase = PHASE_MSGIN;
1437 case STATE(STAT_MESGIN, PHASE_MSGIN):
1438 info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
1439 fas216_cmd(info, CMD_TRANSFERINFO);
1442 case STATE(STAT_COMMAND, PHASE_MSGOUT): /* Message Out -> Command */
1443 case STATE(STAT_COMMAND, PHASE_MSGIN): /* Message In -> Command */
1444 fas216_send_command(info);
1445 info->scsi.phase = PHASE_COMMAND;
1450 * Selection -> Message Out
1452 case STATE(STAT_MESGOUT, PHASE_SELECTION):
1453 fas216_send_messageout(info, 1);
1457 * Message Out -> Message Out
1459 case STATE(STAT_MESGOUT, PHASE_SELSTEPS):
1460 case STATE(STAT_MESGOUT, PHASE_MSGOUT):
1462 * If we get another message out phase, this usually
1463 * means some parity error occurred. Resend complete
1464 * set of messages. If we have more than one byte to
1465 * send, we need to assert ATN again.
1467 if (info->device[info->SCpnt->device->id].parity_check) {
1469 * We were testing... good, the device
1470 * supports parity checking.
1472 info->device[info->SCpnt->device->id].parity_check = 0;
1473 info->device[info->SCpnt->device->id].parity_enabled = 1;
1474 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
1477 if (msgqueue_msglength(&info->scsi.msgs) > 1)
1478 fas216_cmd(info, CMD_SETATN);
1482 * Any -> Message Out
1484 case STATE(STAT_MESGOUT, PHASE_MSGOUT_EXPECT):
1485 fas216_send_messageout(info, 0);
1488 /* Error recovery rules.
1489 * These either attempt to abort or retry the operation.
1490 * TODO: we need more of these
1492 case STATE(STAT_COMMAND, PHASE_COMMAND):/* Command -> Command */
1493 /* error - we've sent out all the command bytes
1495 * NOTE: we need SAVE DATA POINTERS/RESTORE DATA POINTERS
1496 * to include the command bytes sent for this to work
1499 printk(KERN_ERR "scsi%d.%c: "
1500 "target trying to receive more command bytes\n",
1501 info->host->host_no, fas216_target(info));
1502 fas216_cmd(info, CMD_SETATN);
1503 fas216_set_stc(info, 15);
1504 fas216_cmd(info, CMD_PADBYTES | CMD_WITHDMA);
1505 msgqueue_flush(&info->scsi.msgs);
1506 msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
1507 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1511 if (info->scsi.phase == PHASE_MSGIN_DISCONNECT) {
1512 printk(KERN_ERR "scsi%d.%c: disconnect message received, but bus service %s?\n",
1513 info->host->host_no, fas216_target(info),
1514 fas216_bus_phase(stat));
1515 msgqueue_flush(&info->scsi.msgs);
1516 fas216_cmd(info, CMD_SETATN);
1517 msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
1518 info->scsi.phase = PHASE_MSGOUT_EXPECT;
1519 info->scsi.aborting = 1;
1520 fas216_cmd(info, CMD_TRANSFERINFO);
1523 printk(KERN_ERR "scsi%d.%c: bus phase %s after %s?\n",
1524 info->host->host_no, fas216_target(info),
1525 fas216_bus_phase(stat),
1526 fas216_drv_phase(info));
1531 fas216_log(info, 0, "bus service at step %d?", is & IS_BITS);
1532 fas216_dumpstate(info);
1535 fas216_done(info, DID_ERROR);
1539 * fas216_funcdone_intr - handle a function done interrupt from FAS216 chip
1540 * @info: interface which caused function done interrupt
1541 * @stat: Status register contents
1542 * @is: SCSI Status register contents
1544 * Handle a function done interrupt from FAS216 chip
1546 static void fas216_funcdone_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
1548 unsigned int fifo_len = fas216_readb(info, REG_CFIS) & CFIS_CF;
1550 fas216_checkmagic(info);
1552 fas216_log(info, LOG_FUNCTIONDONE,
1553 "function done: stat=%02x is=%02x phase=%02x",
1554 stat, is, info->scsi.phase);
1556 switch (info->scsi.phase) {
1557 case PHASE_STATUS: /* status phase - read status and msg */
1558 if (fifo_len != 2) {
1559 fas216_log(info, 0, "odd number of bytes in FIFO: %d", fifo_len);
1562 * Read status then message byte.
1564 info->scsi.SCp.Status = fas216_readb(info, REG_FF);
1565 info->scsi.SCp.Message = fas216_readb(info, REG_FF);
1566 info->scsi.phase = PHASE_DONE;
1567 fas216_cmd(info, CMD_MSGACCEPTED);
1571 case PHASE_SELECTION:
1572 case PHASE_SELSTEPS:
1575 case PHASE_MSGIN: /* message in phase */
1576 if ((stat & STAT_BUSMASK) == STAT_MESGIN) {
1577 info->scsi.msgin_fifo = fifo_len;
1578 fas216_message(info);
1583 fas216_log(info, 0, "internal phase %s for function done?"
1584 " What do I do with this?",
1585 fas216_target(info), fas216_drv_phase(info));
1589 static void fas216_bus_reset(FAS216_Info *info)
1594 msgqueue_flush(&info->scsi.msgs);
1596 sync_state = neg_invalid;
1599 if (info->ifcfg.capabilities & (FASCAP_DMA|FASCAP_PSEUDODMA))
1600 sync_state = neg_wait;
1603 info->scsi.phase = PHASE_IDLE;
1604 info->SCpnt = NULL; /* bug! */
1605 memset(&info->scsi.SCp, 0, sizeof(info->scsi.SCp));
1607 for (i = 0; i < 8; i++) {
1608 info->device[i].disconnect_ok = info->ifcfg.disconnect_ok;
1609 info->device[i].sync_state = sync_state;
1610 info->device[i].period = info->ifcfg.asyncperiod / 4;
1611 info->device[i].stp = info->scsi.async_stp;
1612 info->device[i].sof = 0;
1613 info->device[i].wide_xfer = 0;
1616 info->rst_bus_status = 1;
1617 wake_up(&info->eh_wait);
1621 * fas216_intr - handle interrupts to progress a command
1622 * @info: interface to service
1624 * Handle interrupts from the interface to progress a command
1626 irqreturn_t fas216_intr(FAS216_Info *info)
1628 unsigned char inst, is, stat;
1629 int handled = IRQ_NONE;
1631 fas216_checkmagic(info);
1633 stat = fas216_readb(info, REG_STAT);
1634 is = fas216_readb(info, REG_IS);
1635 inst = fas216_readb(info, REG_INST);
1637 add_debug_list(stat, is, inst, info->scsi.phase);
1639 if (stat & STAT_INT) {
1640 if (inst & INST_BUSRESET) {
1641 fas216_log(info, 0, "bus reset detected");
1642 fas216_bus_reset(info);
1643 scsi_report_bus_reset(info->host, 0);
1644 } else if (inst & INST_ILLEGALCMD) {
1645 fas216_log(info, LOG_ERROR, "illegal command given\n");
1646 fas216_dumpstate(info);
1648 } else if (inst & INST_DISCONNECT)
1649 fas216_disconnect_intr(info);
1650 else if (inst & INST_RESELECTED) /* reselected */
1651 fas216_reselected_intr(info);
1652 else if (inst & INST_BUSSERVICE) /* bus service request */
1653 fas216_busservice_intr(info, stat, is);
1654 else if (inst & INST_FUNCDONE) /* function done */
1655 fas216_funcdone_intr(info, stat, is);
1657 fas216_log(info, 0, "unknown interrupt received:"
1658 " phase %s inst %02X is %02X stat %02X",
1659 fas216_drv_phase(info), inst, is, stat);
1660 handled = IRQ_HANDLED;
1665 static void __fas216_start_command(FAS216_Info *info, Scsi_Cmnd *SCpnt)
1669 /* following what the ESP driver says */
1670 fas216_set_stc(info, 0);
1671 fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
1674 fas216_cmd(info, CMD_FLUSHFIFO);
1676 /* load bus-id and timeout */
1677 fas216_writeb(info, REG_SDID, BUSID(SCpnt->device->id));
1678 fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
1680 /* synchronous transfers */
1681 fas216_set_sync(info, SCpnt->device->id);
1683 tot_msglen = msgqueue_msglength(&info->scsi.msgs);
1685 #ifdef DEBUG_MESSAGES
1687 struct message *msg;
1690 printk("scsi%d.%c: message out: ",
1691 info->host->host_no, '0' + SCpnt->device->id);
1692 while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
1694 for (i = 0; i < msg->length; i++)
1695 printk("%02x ", msg->msg[i]);
1702 if (tot_msglen == 1 || tot_msglen == 3) {
1704 * We have an easy message length to send...
1706 struct message *msg;
1709 info->scsi.phase = PHASE_SELSTEPS;
1711 /* load message bytes */
1712 while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
1713 for (i = 0; i < msg->length; i++)
1714 fas216_writeb(info, REG_FF, msg->msg[i]);
1715 msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
1719 for (i = 0; i < SCpnt->cmd_len; i++)
1720 fas216_writeb(info, REG_FF, SCpnt->cmnd[i]);
1722 if (tot_msglen == 1)
1723 fas216_cmd(info, CMD_SELECTATN);
1725 fas216_cmd(info, CMD_SELECTATN3);
1728 * We have an unusual number of message bytes to send.
1729 * Load first byte into fifo, and issue SELECT with ATN and
1732 struct message *msg = msgqueue_getmsg(&info->scsi.msgs, 0);
1734 fas216_writeb(info, REG_FF, msg->msg[0]);
1737 fas216_cmd(info, CMD_SELECTATNSTOP);
1742 * Decide whether we need to perform a parity test on this device.
1743 * Can also be used to force parity error conditions during initial
1744 * information transfer phase (message out) for test purposes.
1746 static int parity_test(FAS216_Info *info, int target)
1750 info->device[target].parity_check = 0;
1754 return info->device[target].parity_check;
1757 static void fas216_start_command(FAS216_Info *info, Scsi_Cmnd *SCpnt)
1764 info->scsi.phase = PHASE_SELECTION;
1765 info->scsi.SCp = SCpnt->SCp;
1766 info->SCpnt = SCpnt;
1767 info->dma.transfer_type = fasdma_none;
1769 if (parity_test(info, SCpnt->device->id))
1770 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0] | CNTL1_PTE);
1772 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
1775 * Don't allow request sense commands to disconnect.
1777 disconnect_ok = SCpnt->cmnd[0] != REQUEST_SENSE &&
1778 info->device[SCpnt->device->id].disconnect_ok;
1781 * build outgoing message bytes
1783 msgqueue_flush(&info->scsi.msgs);
1784 msgqueue_addmsg(&info->scsi.msgs, 1, IDENTIFY(disconnect_ok, SCpnt->device->lun));
1787 * add tag message if required
1790 msgqueue_addmsg(&info->scsi.msgs, 2, SIMPLE_QUEUE_TAG, SCpnt->tag);
1794 if ((info->device[SCpnt->device->id].sync_state == neg_wait ||
1795 info->device[SCpnt->device->id].sync_state == neg_complete) &&
1796 (SCpnt->cmnd[0] == REQUEST_SENSE ||
1797 SCpnt->cmnd[0] == INQUIRY)) {
1798 info->device[SCpnt->device->id].sync_state = neg_inprogress;
1799 msgqueue_addmsg(&info->scsi.msgs, 5,
1800 EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
1801 1000 / info->ifcfg.clockrate,
1802 info->ifcfg.sync_max_depth);
1808 __fas216_start_command(info, SCpnt);
1811 static void fas216_allocate_tag(FAS216_Info *info, Scsi_Cmnd *SCpnt)
1815 * tagged queuing - allocate a new tag to this command
1817 if (SCpnt->device->simple_tags && SCpnt->cmnd[0] != REQUEST_SENSE &&
1818 SCpnt->cmnd[0] != INQUIRY) {
1819 SCpnt->device->current_tag += 1;
1820 if (SCpnt->device->current_tag == 0)
1821 SCpnt->device->current_tag = 1;
1822 SCpnt->tag = SCpnt->device->current_tag;
1825 set_bit(SCpnt->device->id * 8 + SCpnt->device->lun, info->busyluns);
1827 info->stats.removes += 1;
1828 switch (SCpnt->cmnd[0]) {
1832 info->stats.writes += 1;
1837 info->stats.reads += 1;
1840 info->stats.miscs += 1;
1845 static void fas216_do_bus_device_reset(FAS216_Info *info, Scsi_Cmnd *SCpnt)
1847 struct message *msg;
1852 info->scsi.phase = PHASE_SELECTION;
1853 info->scsi.SCp = SCpnt->SCp;
1854 info->SCpnt = SCpnt;
1855 info->dma.transfer_type = fasdma_none;
1857 fas216_log(info, LOG_ERROR, "sending bus device reset");
1859 msgqueue_flush(&info->scsi.msgs);
1860 msgqueue_addmsg(&info->scsi.msgs, 1, BUS_DEVICE_RESET);
1862 /* following what the ESP driver says */
1863 fas216_set_stc(info, 0);
1864 fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
1867 fas216_cmd(info, CMD_FLUSHFIFO);
1869 /* load bus-id and timeout */
1870 fas216_writeb(info, REG_SDID, BUSID(SCpnt->device->id));
1871 fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
1873 /* synchronous transfers */
1874 fas216_set_sync(info, SCpnt->device->id);
1876 msg = msgqueue_getmsg(&info->scsi.msgs, 0);
1878 fas216_writeb(info, REG_FF, BUS_DEVICE_RESET);
1881 fas216_cmd(info, CMD_SELECTATNSTOP);
1885 * fas216_kick - kick a command to the interface
1886 * @info: our host interface to kick
1888 * Kick a command to the interface, interface should be idle.
1889 * Notes: Interrupts are always disabled!
1891 static void fas216_kick(FAS216_Info *info)
1893 Scsi_Cmnd *SCpnt = NULL;
1894 #define TYPE_OTHER 0
1895 #define TYPE_RESET 1
1896 #define TYPE_QUEUE 2
1897 int where_from = TYPE_OTHER;
1899 fas216_checkmagic(info);
1902 * Obtain the next command to process.
1905 if (info->rstSCpnt) {
1906 SCpnt = info->rstSCpnt;
1907 /* don't remove it */
1908 where_from = TYPE_RESET;
1912 if (info->reqSCpnt) {
1913 SCpnt = info->reqSCpnt;
1914 info->reqSCpnt = NULL;
1918 if (info->origSCpnt) {
1919 SCpnt = info->origSCpnt;
1920 info->origSCpnt = NULL;
1924 /* retrieve next command */
1926 SCpnt = queue_remove_exclude(&info->queues.issue,
1928 where_from = TYPE_QUEUE;
1935 * no command pending, so enable reselection.
1937 fas216_cmd(info, CMD_ENABLESEL);
1942 * We're going to start a command, so disable reselection
1944 fas216_cmd(info, CMD_DISABLESEL);
1946 if (info->scsi.disconnectable && info->SCpnt) {
1947 fas216_log(info, LOG_CONNECT,
1948 "moved command for %d to disconnected queue",
1949 info->SCpnt->device->id);
1950 queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
1951 info->scsi.disconnectable = 0;
1955 fas216_log_command(info, LOG_CONNECT | LOG_MESSAGES, SCpnt,
1958 switch (where_from) {
1960 fas216_allocate_tag(info, SCpnt);
1962 fas216_start_command(info, SCpnt);
1965 fas216_do_bus_device_reset(info, SCpnt);
1969 fas216_log(info, LOG_CONNECT, "select: data pointers [%p, %X]",
1970 info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
1973 * should now get either DISCONNECT or
1974 * (FUNCTION DONE with BUS SERVICE) interrupt
1979 * Clean up from issuing a BUS DEVICE RESET message to a device.
1982 fas216_devicereset_done(FAS216_Info *info, Scsi_Cmnd *SCpnt, unsigned int result)
1984 fas216_log(info, LOG_ERROR, "fas216 device reset complete");
1986 info->rstSCpnt = NULL;
1987 info->rst_dev_status = 1;
1988 wake_up(&info->eh_wait);
1992 * fas216_rq_sns_done - Finish processing automatic request sense command
1993 * @info: interface that completed
1994 * @SCpnt: command that completed
1995 * @result: driver byte of result
1997 * Finish processing automatic request sense command
2000 fas216_rq_sns_done(FAS216_Info *info, Scsi_Cmnd *SCpnt, unsigned int result)
2002 fas216_log_target(info, LOG_CONNECT, SCpnt->device->id,
2003 "request sense complete, result=0x%04x%02x%02x",
2004 result, SCpnt->SCp.Message, SCpnt->SCp.Status);
2006 if (result != DID_OK || SCpnt->SCp.Status != GOOD)
2008 * Something went wrong. Make sure that we don't
2009 * have valid data in the sense buffer that could
2010 * confuse the higher levels.
2012 memset(SCpnt->sense_buffer, 0, sizeof(SCpnt->sense_buffer));
2013 //printk("scsi%d.%c: sense buffer: ", info->host->host_no, '0' + SCpnt->device->id);
2014 //{ int i; for (i = 0; i < 32; i++) printk("%02x ", SCpnt->sense_buffer[i]); printk("\n"); }
2016 * Note that we don't set SCpnt->result, since that should
2017 * reflect the status of the command that we were asked by
2018 * the upper layers to process. This would have been set
2019 * correctly by fas216_std_done.
2021 SCpnt->scsi_done(SCpnt);
2025 * fas216_std_done - finish processing of standard command
2026 * @info: interface that completed
2027 * @SCpnt: command that completed
2028 * @result: driver byte of result
2030 * Finish processing of standard command
2033 fas216_std_done(FAS216_Info *info, Scsi_Cmnd *SCpnt, unsigned int result)
2035 info->stats.fins += 1;
2037 SCpnt->result = result << 16 | info->scsi.SCp.Message << 8 |
2038 info->scsi.SCp.Status;
2040 fas216_log_command(info, LOG_CONNECT, SCpnt,
2041 "command complete, result=0x%08x", SCpnt->result);
2044 * If the driver detected an error, we're all done.
2046 if (host_byte(SCpnt->result) != DID_OK ||
2047 msg_byte(SCpnt->result) != COMMAND_COMPLETE)
2051 * If the command returned CHECK_CONDITION or COMMAND_TERMINATED
2052 * status, request the sense information.
2054 if (status_byte(SCpnt->result) == CHECK_CONDITION ||
2055 status_byte(SCpnt->result) == COMMAND_TERMINATED)
2059 * If the command did not complete with GOOD status,
2060 * we are all done here.
2062 if (status_byte(SCpnt->result) != GOOD)
2066 * We have successfully completed a command. Make sure that
2067 * we do not have any buffers left to transfer. The world
2068 * is not perfect, and we seem to occasionally hit this.
2069 * It can be indicative of a buggy driver, target or the upper
2070 * levels of the SCSI code.
2072 if (info->scsi.SCp.ptr) {
2073 switch (SCpnt->cmnd[0]) {
2080 printk(KERN_ERR "scsi%d.%c: incomplete data transfer "
2081 "detected: res=%08X ptr=%p len=%X CDB: ",
2082 info->host->host_no, '0' + SCpnt->device->id,
2083 SCpnt->result, info->scsi.SCp.ptr,
2084 info->scsi.SCp.this_residual);
2085 __scsi_print_command(SCpnt->cmnd);
2086 SCpnt->result &= ~(255 << 16);
2087 SCpnt->result |= DID_BAD_TARGET << 16;
2093 if (SCpnt->scsi_done) {
2094 SCpnt->scsi_done(SCpnt);
2098 panic("scsi%d.H: null scsi_done function in fas216_done",
2099 info->host->host_no);
2103 if (SCpnt->cmnd[0] == REQUEST_SENSE)
2106 fas216_log_target(info, LOG_CONNECT, SCpnt->device->id,
2107 "requesting sense");
2108 memset(SCpnt->cmnd, 0, sizeof (SCpnt->cmnd));
2109 SCpnt->cmnd[0] = REQUEST_SENSE;
2110 SCpnt->cmnd[1] = SCpnt->device->lun << 5;
2111 SCpnt->cmnd[4] = sizeof(SCpnt->sense_buffer);
2112 SCpnt->cmd_len = COMMAND_SIZE(SCpnt->cmnd[0]);
2113 SCpnt->SCp.buffer = NULL;
2114 SCpnt->SCp.buffers_residual = 0;
2115 SCpnt->SCp.ptr = (char *)SCpnt->sense_buffer;
2116 SCpnt->SCp.this_residual = sizeof(SCpnt->sense_buffer);
2117 SCpnt->SCp.Message = 0;
2118 SCpnt->SCp.Status = 0;
2119 SCpnt->request_bufflen = sizeof(SCpnt->sense_buffer);
2120 SCpnt->sc_data_direction = DMA_FROM_DEVICE;
2123 SCpnt->host_scribble = (void *)fas216_rq_sns_done;
2126 * Place this command into the high priority "request
2127 * sense" slot. This will be the very next command
2128 * executed, unless a target connects to us.
2131 printk(KERN_WARNING "scsi%d.%c: loosing request command\n",
2132 info->host->host_no, '0' + SCpnt->device->id);
2133 info->reqSCpnt = SCpnt;
2137 * fas216_done - complete processing for current command
2138 * @info: interface that completed
2139 * @result: driver byte of result
2141 * Complete processing for current command
2143 static void fas216_done(FAS216_Info *info, unsigned int result)
2145 void (*fn)(FAS216_Info *, Scsi_Cmnd *, unsigned int);
2147 unsigned long flags;
2149 fas216_checkmagic(info);
2154 SCpnt = info->SCpnt;
2156 info->scsi.phase = PHASE_IDLE;
2158 if (info->scsi.aborting) {
2159 fas216_log(info, 0, "uncaught abort - returning DID_ABORT");
2161 info->scsi.aborting = 0;
2165 * Sanity check the completion - if we have zero bytes left
2166 * to transfer, we should not have a valid pointer.
2168 if (info->scsi.SCp.ptr && info->scsi.SCp.this_residual == 0) {
2169 printk("scsi%d.%c: zero bytes left to transfer, but "
2170 "buffer pointer still valid: ptr=%p len=%08x CDB: ",
2171 info->host->host_no, '0' + SCpnt->device->id,
2172 info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
2173 info->scsi.SCp.ptr = NULL;
2174 __scsi_print_command(SCpnt->cmnd);
2178 * Clear down this command as completed. If we need to request
2179 * the sense information, fas216_kick will re-assert the busy
2182 info->device[SCpnt->device->id].parity_check = 0;
2183 clear_bit(SCpnt->device->id * 8 + SCpnt->device->lun, info->busyluns);
2185 fn = (void (*)(FAS216_Info *, Scsi_Cmnd *, unsigned int))SCpnt->host_scribble;
2186 fn(info, SCpnt, result);
2188 if (info->scsi.irq != NO_IRQ) {
2189 spin_lock_irqsave(&info->host_lock, flags);
2190 if (info->scsi.phase == PHASE_IDLE)
2192 spin_unlock_irqrestore(&info->host_lock, flags);
2197 panic("scsi%d.H: null command in fas216_done",
2198 info->host->host_no);
2202 * fas216_queue_command - queue a command for adapter to process.
2203 * @SCpnt: Command to queue
2204 * @done: done function to call once command is complete
2206 * Queue a command for adapter to process.
2207 * Returns: 0 on success, else error.
2208 * Notes: io_request_lock is held, interrupts are disabled.
2210 int fas216_queue_command(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
2212 FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2215 fas216_checkmagic(info);
2217 fas216_log_command(info, LOG_CONNECT, SCpnt,
2218 "received command (%p)", SCpnt);
2220 SCpnt->scsi_done = done;
2221 SCpnt->host_scribble = (void *)fas216_std_done;
2226 info->stats.queues += 1;
2229 spin_lock(&info->host_lock);
2232 * Add command into execute queue and let it complete under
2233 * whatever scheme we're using.
2235 result = !queue_add_cmd_ordered(&info->queues.issue, SCpnt);
2238 * If we successfully added the command,
2239 * kick the interface to get it moving.
2241 if (result == 0 && info->scsi.phase == PHASE_IDLE)
2243 spin_unlock(&info->host_lock);
2245 fas216_log_target(info, LOG_CONNECT, -1, "queue %s",
2246 result ? "failure" : "success");
2252 * fas216_internal_done - trigger restart of a waiting thread in fas216_noqueue_command
2253 * @SCpnt: Command to wake
2255 * Trigger restart of a waiting thread in fas216_command
2257 static void fas216_internal_done(Scsi_Cmnd *SCpnt)
2259 FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2261 fas216_checkmagic(info);
2263 info->internal_done = 1;
2267 * fas216_noqueue_command - process a command for the adapter.
2268 * @SCpnt: Command to queue
2270 * Queue a command for adapter to process.
2271 * Returns: scsi result code.
2272 * Notes: io_request_lock is held, interrupts are disabled.
2274 int fas216_noqueue_command(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
2276 FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2278 fas216_checkmagic(info);
2281 * We should only be using this if we don't have an interrupt.
2282 * Provide some "incentive" to use the queueing code.
2284 BUG_ON(info->scsi.irq != NO_IRQ);
2286 info->internal_done = 0;
2287 fas216_queue_command(SCpnt, fas216_internal_done);
2290 * This wastes time, since we can't return until the command is
2291 * complete. We can't sleep either since we may get re-entered!
2292 * However, we must re-enable interrupts, or else we'll be
2295 spin_unlock_irq(info->host->host_lock);
2297 while (!info->internal_done) {
2299 * If we don't have an IRQ, then we must poll the card for
2300 * it's interrupt, and use that to call this driver's
2301 * interrupt routine. That way, we keep the command
2302 * progressing. Maybe we can add some inteligence here
2303 * and go to sleep if we know that the device is going
2304 * to be some time (eg, disconnected).
2306 if (fas216_readb(info, REG_STAT) & STAT_INT) {
2307 spin_lock_irq(info->host->host_lock);
2309 spin_unlock_irq(info->host->host_lock);
2313 spin_lock_irq(info->host->host_lock);
2321 * Error handler timeout function. Indicate that we timed out,
2322 * and wake up any error handler process so it can continue.
2324 static void fas216_eh_timer(unsigned long data)
2326 FAS216_Info *info = (FAS216_Info *)data;
2328 fas216_log(info, LOG_ERROR, "error handling timed out\n");
2330 del_timer(&info->eh_timer);
2332 if (info->rst_bus_status == 0)
2333 info->rst_bus_status = -1;
2334 if (info->rst_dev_status == 0)
2335 info->rst_dev_status = -1;
2337 wake_up(&info->eh_wait);
2341 res_failed, /* not found */
2342 res_success, /* command on issue queue */
2343 res_hw_abort /* command on disconnected dev */
2347 * fas216_do_abort - decide how to abort a command
2348 * @SCpnt: command to abort
2350 * Decide how to abort a command.
2351 * Returns: abort status
2353 static enum res_find fas216_find_command(FAS216_Info *info, Scsi_Cmnd *SCpnt)
2355 enum res_find res = res_failed;
2357 if (queue_remove_cmd(&info->queues.issue, SCpnt)) {
2359 * The command was on the issue queue, and has not been
2360 * issued yet. We can remove the command from the queue,
2361 * and acknowledge the abort. Neither the device nor the
2362 * interface know about the command.
2364 printk("on issue queue ");
2367 } else if (queue_remove_cmd(&info->queues.disconnected, SCpnt)) {
2369 * The command was on the disconnected queue. We must
2370 * reconnect with the device if possible, and send it
2373 printk("on disconnected queue ");
2376 } else if (info->SCpnt == SCpnt) {
2377 printk("executing ");
2379 switch (info->scsi.phase) {
2381 * If the interface is idle, and the command is 'disconnectable',
2382 * then it is the same as on the disconnected queue.
2385 if (info->scsi.disconnectable) {
2386 info->scsi.disconnectable = 0;
2395 } else if (info->origSCpnt == SCpnt) {
2397 * The command will be executed next, but a command
2398 * is currently using the interface. This is similar to
2399 * being on the issue queue, except the busylun bit has
2402 info->origSCpnt = NULL;
2403 clear_bit(SCpnt->device->id * 8 + SCpnt->device->lun, info->busyluns);
2404 printk("waiting for execution ");
2413 * fas216_eh_abort - abort this command
2414 * @SCpnt: command to abort
2416 * Abort this command.
2417 * Returns: FAILED if unable to abort
2418 * Notes: io_request_lock is taken, and irqs are disabled
2420 int fas216_eh_abort(Scsi_Cmnd *SCpnt)
2422 FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2423 int result = FAILED;
2425 fas216_checkmagic(info);
2427 info->stats.aborts += 1;
2429 printk(KERN_WARNING "scsi%d: abort command ", info->host->host_no);
2430 __scsi_print_command(SCpnt->data_cmnd);
2433 fas216_dumpstate(info);
2435 printk(KERN_WARNING "scsi%d: abort %p ", info->host->host_no, SCpnt);
2437 switch (fas216_find_command(info, SCpnt)) {
2439 * We found the command, and cleared it out. Either
2440 * the command is still known to be executing on the
2441 * target, or the busylun bit is not set.
2444 printk("success\n");
2449 * We need to reconnect to the target and send it an
2450 * ABORT or ABORT_TAG message. We can only do this
2451 * if the bus is free.
2457 * We are unable to abort the command for some reason.
2469 * fas216_eh_device_reset - Reset the device associated with this command
2470 * @SCpnt: command specifing device to reset
2472 * Reset the device associated with this command.
2473 * Returns: FAILED if unable to reset.
2474 * Notes: We won't be re-entered, so we'll only have one device
2475 * reset on the go at one time.
2477 int fas216_eh_device_reset(Scsi_Cmnd *SCpnt)
2479 FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2480 unsigned long flags;
2481 int i, res = FAILED, target = SCpnt->device->id;
2483 fas216_log(info, LOG_ERROR, "device reset for target %d", target);
2485 spin_lock_irqsave(&info->host_lock, flags);
2489 * If we are currently connected to a device, and
2490 * it is the device we want to reset, there is
2491 * nothing we can do here. Chances are it is stuck,
2492 * and we need a bus reset.
2494 if (info->SCpnt && !info->scsi.disconnectable &&
2495 info->SCpnt->device->id == SCpnt->device->id)
2499 * We're going to be resetting this device. Remove
2500 * all pending commands from the driver. By doing
2501 * so, we guarantee that we won't touch the command
2502 * structures except to process the reset request.
2504 queue_remove_all_target(&info->queues.issue, target);
2505 queue_remove_all_target(&info->queues.disconnected, target);
2506 if (info->origSCpnt && info->origSCpnt->device->id == target)
2507 info->origSCpnt = NULL;
2508 if (info->reqSCpnt && info->reqSCpnt->device->id == target)
2509 info->reqSCpnt = NULL;
2510 for (i = 0; i < 8; i++)
2511 clear_bit(target * 8 + i, info->busyluns);
2514 * Hijack this SCSI command structure to send
2515 * a bus device reset message to this device.
2517 SCpnt->host_scribble = (void *)fas216_devicereset_done;
2519 info->rst_dev_status = 0;
2520 info->rstSCpnt = SCpnt;
2522 if (info->scsi.phase == PHASE_IDLE)
2525 mod_timer(&info->eh_timer, 30 * HZ);
2526 spin_unlock_irqrestore(&info->host_lock, flags);
2529 * Wait up to 30 seconds for the reset to complete.
2531 wait_event(info->eh_wait, info->rst_dev_status);
2533 del_timer_sync(&info->eh_timer);
2534 spin_lock_irqsave(&info->host_lock, flags);
2535 info->rstSCpnt = NULL;
2537 if (info->rst_dev_status == 1)
2541 SCpnt->host_scribble = NULL;
2542 spin_unlock_irqrestore(&info->host_lock, flags);
2544 fas216_log(info, LOG_ERROR, "device reset complete: %s\n",
2545 res == SUCCESS ? "success" : "failed");
2551 * fas216_eh_bus_reset - Reset the bus associated with the command
2552 * @SCpnt: command specifing bus to reset
2554 * Reset the bus associated with the command.
2555 * Returns: FAILED if unable to reset.
2556 * Notes: Further commands are blocked.
2558 int fas216_eh_bus_reset(Scsi_Cmnd *SCpnt)
2560 FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2561 unsigned long flags;
2562 struct scsi_device *SDpnt;
2564 fas216_checkmagic(info);
2565 fas216_log(info, LOG_ERROR, "resetting bus");
2567 info->stats.bus_resets += 1;
2569 spin_lock_irqsave(&info->host_lock, flags);
2572 * Stop all activity on this interface.
2574 fas216_aborttransfer(info);
2575 fas216_writeb(info, REG_CNTL3, info->scsi.cfg[2]);
2578 * Clear any pending interrupts.
2580 while (fas216_readb(info, REG_STAT) & STAT_INT)
2581 fas216_readb(info, REG_INST);
2583 info->rst_bus_status = 0;
2586 * For each attached hard-reset device, clear out
2587 * all command structures. Leave the running
2590 shost_for_each_device(SDpnt, info->host) {
2593 if (SDpnt->soft_reset)
2596 queue_remove_all_target(&info->queues.issue, SDpnt->id);
2597 queue_remove_all_target(&info->queues.disconnected, SDpnt->id);
2598 if (info->origSCpnt && info->origSCpnt->device->id == SDpnt->id)
2599 info->origSCpnt = NULL;
2600 if (info->reqSCpnt && info->reqSCpnt->device->id == SDpnt->id)
2601 info->reqSCpnt = NULL;
2604 for (i = 0; i < 8; i++)
2605 clear_bit(SDpnt->id * 8 + i, info->busyluns);
2608 info->scsi.phase = PHASE_IDLE;
2611 * Reset the SCSI bus. Device cleanup happens in
2612 * the interrupt handler.
2614 fas216_cmd(info, CMD_RESETSCSI);
2616 mod_timer(&info->eh_timer, jiffies + HZ);
2617 spin_unlock_irqrestore(&info->host_lock, flags);
2620 * Wait one second for the interrupt.
2622 wait_event(info->eh_wait, info->rst_bus_status);
2623 del_timer_sync(&info->eh_timer);
2625 fas216_log(info, LOG_ERROR, "bus reset complete: %s\n",
2626 info->rst_bus_status == 1 ? "success" : "failed");
2628 return info->rst_bus_status == 1 ? SUCCESS : FAILED;
2632 * fas216_init_chip - Initialise FAS216 state after reset
2633 * @info: state structure for interface
2635 * Initialise FAS216 state after reset
2637 static void fas216_init_chip(FAS216_Info *info)
2639 unsigned int clock = ((info->ifcfg.clockrate - 1) / 5 + 1) & 7;
2640 fas216_writeb(info, REG_CLKF, clock);
2641 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
2642 fas216_writeb(info, REG_CNTL2, info->scsi.cfg[1]);
2643 fas216_writeb(info, REG_CNTL3, info->scsi.cfg[2]);
2644 fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
2645 fas216_writeb(info, REG_SOF, 0);
2646 fas216_writeb(info, REG_STP, info->scsi.async_stp);
2647 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
2651 * fas216_eh_host_reset - Reset the host associated with this command
2652 * @SCpnt: command specifing host to reset
2654 * Reset the host associated with this command.
2655 * Returns: FAILED if unable to reset.
2656 * Notes: io_request_lock is taken, and irqs are disabled
2658 int fas216_eh_host_reset(Scsi_Cmnd *SCpnt)
2660 FAS216_Info *info = (FAS216_Info *)SCpnt->device->host->hostdata;
2662 spin_lock_irq(info->host->host_lock);
2664 fas216_checkmagic(info);
2666 printk("scsi%d.%c: %s: resetting host\n",
2667 info->host->host_no, '0' + SCpnt->device->id, __FUNCTION__);
2670 * Reset the SCSI chip.
2672 fas216_cmd(info, CMD_RESETCHIP);
2676 * We need to release the host_lock and enable
2677 * IRQs if we sleep, but we must relock and disable
2678 * IRQs after the sleep.
2680 spin_unlock_irq(info->host->host_lock);
2681 msleep(50 * 1000/100);
2682 spin_lock_irq(info->host->host_lock);
2685 * Release the SCSI reset.
2687 fas216_cmd(info, CMD_NOP);
2689 fas216_init_chip(info);
2691 spin_unlock_irq(info->host->host_lock);
2695 #define TYPE_UNKNOWN 0
2696 #define TYPE_NCR53C90 1
2697 #define TYPE_NCR53C90A 2
2698 #define TYPE_NCR53C9x 3
2699 #define TYPE_Am53CF94 4
2700 #define TYPE_EmFAS216 5
2701 #define TYPE_QLFAS216 6
2703 static char *chip_types[] = {
2713 static int fas216_detect_type(FAS216_Info *info)
2720 fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2722 fas216_writeb(info, REG_CMD, CMD_NOP);
2725 * Check to see if control reg 2 is present.
2727 fas216_writeb(info, REG_CNTL3, 0);
2728 fas216_writeb(info, REG_CNTL2, CNTL2_S2FE);
2731 * If we are unable to read back control reg 2
2732 * correctly, it is not present, and we have a
2735 if ((fas216_readb(info, REG_CNTL2) & (~0xe0)) != CNTL2_S2FE)
2736 return TYPE_NCR53C90;
2739 * Now, check control register 3
2741 fas216_writeb(info, REG_CNTL2, 0);
2742 fas216_writeb(info, REG_CNTL3, 0);
2743 fas216_writeb(info, REG_CNTL3, 5);
2746 * If we are unable to read the register back
2747 * correctly, we have a NCR53C90A
2749 if (fas216_readb(info, REG_CNTL3) != 5)
2750 return TYPE_NCR53C90A;
2753 * Now read the ID from the chip.
2755 fas216_writeb(info, REG_CNTL3, 0);
2757 fas216_writeb(info, REG_CNTL3, CNTL3_ADIDCHK);
2758 fas216_writeb(info, REG_CNTL3, 0);
2760 fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2762 fas216_writeb(info, REG_CMD, CMD_WITHDMA | CMD_NOP);
2764 fas216_writeb(info, REG_CNTL2, CNTL2_ENF);
2765 fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2767 fas216_writeb(info, REG_CMD, CMD_NOP);
2769 rev = fas216_readb(info, REG_ID);
2776 return TYPE_Am53CF94;
2782 return TYPE_EmFAS216;
2784 return TYPE_QLFAS216;
2791 printk("family %x rev %x\n", family, rev);
2792 return TYPE_NCR53C9x;
2796 * fas216_reset_state - Initialise driver internal state
2797 * @info: state to initialise
2799 * Initialise driver internal state
2801 static void fas216_reset_state(FAS216_Info *info)
2805 fas216_checkmagic(info);
2807 fas216_bus_reset(info);
2810 * Clear out all stale info in our state structure
2812 memset(info->busyluns, 0, sizeof(info->busyluns));
2813 info->scsi.disconnectable = 0;
2814 info->scsi.aborting = 0;
2816 for (i = 0; i < 8; i++) {
2817 info->device[i].parity_enabled = 0;
2818 info->device[i].parity_check = 1;
2822 * Drain all commands on disconnected queue
2824 while (queue_remove(&info->queues.disconnected) != NULL);
2827 * Remove executing commands.
2830 info->reqSCpnt = NULL;
2831 info->rstSCpnt = NULL;
2832 info->origSCpnt = NULL;
2836 * fas216_init - initialise FAS/NCR/AMD SCSI structures.
2837 * @host: a driver-specific filled-out structure
2839 * Initialise FAS/NCR/AMD SCSI structures.
2840 * Returns: 0 on success
2842 int fas216_init(struct Scsi_Host *host)
2844 FAS216_Info *info = (FAS216_Info *)host->hostdata;
2846 info->magic_start = MAGIC;
2847 info->magic_end = MAGIC;
2849 info->scsi.cfg[0] = host->this_id | CNTL1_PERE;
2850 info->scsi.cfg[1] = CNTL2_ENF | CNTL2_S2FE;
2851 info->scsi.cfg[2] = info->ifcfg.cntl3 |
2852 CNTL3_ADIDCHK | CNTL3_QTAG | CNTL3_G2CB | CNTL3_LBTM;
2853 info->scsi.async_stp = fas216_syncperiod(info, info->ifcfg.asyncperiod);
2855 info->rst_dev_status = -1;
2856 info->rst_bus_status = -1;
2857 init_waitqueue_head(&info->eh_wait);
2858 init_timer(&info->eh_timer);
2859 info->eh_timer.data = (unsigned long)info;
2860 info->eh_timer.function = fas216_eh_timer;
2862 spin_lock_init(&info->host_lock);
2864 memset(&info->stats, 0, sizeof(info->stats));
2866 msgqueue_initialise(&info->scsi.msgs);
2868 if (!queue_initialise(&info->queues.issue))
2871 if (!queue_initialise(&info->queues.disconnected)) {
2872 queue_free(&info->queues.issue);
2880 * fas216_add - initialise FAS/NCR/AMD SCSI ic.
2881 * @host: a driver-specific filled-out structure
2882 * @dev: parent device
2884 * Initialise FAS/NCR/AMD SCSI ic.
2885 * Returns: 0 on success
2887 int fas216_add(struct Scsi_Host *host, struct device *dev)
2889 FAS216_Info *info = (FAS216_Info *)host->hostdata;
2892 if (info->ifcfg.clockrate <= 10 || info->ifcfg.clockrate > 40) {
2893 printk(KERN_CRIT "fas216: invalid clock rate %u MHz\n",
2894 info->ifcfg.clockrate);
2898 fas216_reset_state(info);
2899 type = fas216_detect_type(info);
2900 info->scsi.type = chip_types[type];
2905 * Initialise the chip correctly.
2907 fas216_init_chip(info);
2910 * Reset the SCSI bus. We don't want to see
2911 * the resulting reset interrupt, so mask it
2914 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0] | CNTL1_DISR);
2915 fas216_writeb(info, REG_CMD, CMD_RESETSCSI);
2918 * scsi standard says wait 250ms
2920 spin_unlock_irq(info->host->host_lock);
2921 msleep(100*1000/100);
2922 spin_lock_irq(info->host->host_lock);
2924 fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
2925 fas216_readb(info, REG_INST);
2927 fas216_checkmagic(info);
2929 ret = scsi_add_host(host, dev);
2931 fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2933 scsi_scan_host(host);
2938 void fas216_remove(struct Scsi_Host *host)
2940 FAS216_Info *info = (FAS216_Info *)host->hostdata;
2942 fas216_checkmagic(info);
2943 scsi_remove_host(host);
2945 fas216_writeb(info, REG_CMD, CMD_RESETCHIP);
2946 scsi_host_put(host);
2950 * fas216_release - release all resources for FAS/NCR/AMD SCSI ic.
2951 * @host: a driver-specific filled-out structure
2953 * release all resources and put everything to bed for FAS/NCR/AMD SCSI ic.
2955 void fas216_release(struct Scsi_Host *host)
2957 FAS216_Info *info = (FAS216_Info *)host->hostdata;
2959 queue_free(&info->queues.disconnected);
2960 queue_free(&info->queues.issue);
2963 int fas216_print_host(FAS216_Info *info, char *buffer)
2965 return sprintf(buffer,
2971 info->scsi.type, info->scsi.io_base,
2972 info->scsi.irq, info->scsi.dma);
2975 int fas216_print_stats(FAS216_Info *info, char *buffer)
2979 p += sprintf(p, "\n"
2980 "Command Statistics:\n"
2987 " Disconnects: %u\n"
2989 " Bus resets : %u\n"
2990 " Host resets: %u\n",
2991 info->stats.queues, info->stats.removes,
2992 info->stats.fins, info->stats.reads,
2993 info->stats.writes, info->stats.miscs,
2994 info->stats.disconnects, info->stats.aborts,
2995 info->stats.bus_resets, info->stats.host_resets);
3000 int fas216_print_devices(FAS216_Info *info, char *buffer)
3002 struct fas216_device *dev;
3003 struct scsi_device *scd;
3006 p += sprintf(p, "Device/Lun TaggedQ Parity Sync\n");
3008 shost_for_each_device(scd, info->host) {
3009 dev = &info->device[scd->id];
3010 p += sprintf(p, " %d/%d ", scd->id, scd->lun);
3011 if (scd->tagged_supported)
3012 p += sprintf(p, "%3sabled(%3d) ",
3013 scd->simple_tags ? "en" : "dis",
3016 p += sprintf(p, "unsupported ");
3018 p += sprintf(p, "%3sabled ", dev->parity_enabled ? "en" : "dis");
3021 p += sprintf(p, "offset %d, %d ns\n",
3022 dev->sof, dev->period * 4);
3024 p += sprintf(p, "async\n");
3030 EXPORT_SYMBOL(fas216_init);
3031 EXPORT_SYMBOL(fas216_add);
3032 EXPORT_SYMBOL(fas216_queue_command);
3033 EXPORT_SYMBOL(fas216_noqueue_command);
3034 EXPORT_SYMBOL(fas216_intr);
3035 EXPORT_SYMBOL(fas216_remove);
3036 EXPORT_SYMBOL(fas216_release);
3037 EXPORT_SYMBOL(fas216_eh_abort);
3038 EXPORT_SYMBOL(fas216_eh_device_reset);
3039 EXPORT_SYMBOL(fas216_eh_bus_reset);
3040 EXPORT_SYMBOL(fas216_eh_host_reset);
3041 EXPORT_SYMBOL(fas216_print_host);
3042 EXPORT_SYMBOL(fas216_print_stats);
3043 EXPORT_SYMBOL(fas216_print_devices);
3045 MODULE_AUTHOR("Russell King");
3046 MODULE_DESCRIPTION("Generic FAS216/NCR53C9x driver core");
3047 MODULE_LICENSE("GPL");