atari_NCR5380.c
上传用户:lgb322
上传日期:2013-02-24
资源大小:30529k
文件大小:92k
源码类别:

嵌入式Linux

开发平台:

Unix_Linux

  1. /* 
  2.  * NCR 5380 generic driver routines.  These should make it *trivial*
  3.  *  to implement 5380 SCSI drivers under Linux with a non-trantor
  4.  * architecture.
  5.  *
  6.  * Note that these routines also work with NR53c400 family chips.
  7.  *
  8.  * Copyright 1993, Drew Eckhardt
  9.  * Visionary Computing 
  10.  * (Unix and Linux consulting and custom programming)
  11.  *  drew@colorado.edu
  12.  * +1 (303) 666-5836
  13.  *
  14.  * DISTRIBUTION RELEASE 6. 
  15.  *
  16.  * For more information, please consult 
  17.  *
  18.  * NCR 5380 Family
  19.  * SCSI Protocol Controller
  20.  * Databook
  21.  *
  22.  * NCR Microelectronics
  23.  * 1635 Aeroplaza Drive
  24.  * Colorado Springs, CO 80916
  25.  * 1+ (719) 578-3400
  26.  * 1+ (800) 334-5454
  27.  */
  28. /*
  29.  * ++roman: To port the 5380 driver to the Atari, I had to do some changes in
  30.  * this file, too:
  31.  *
  32.  *  - Some of the debug statements were incorrect (undefined variables and the
  33.  *    like). I fixed that.
  34.  *
  35.  *  - In information_transfer(), I think a #ifdef was wrong. Looking at the
  36.  *    possible DMA transfer size should also happen for REAL_DMA. I added this
  37.  *    in the #if statement.
  38.  *
  39.  *  - When using real DMA, information_transfer() should return in a DATAOUT
  40.  *    phase after starting the DMA. It has nothing more to do.
  41.  *
  42.  *  - The interrupt service routine should run main after end of DMA, too (not
  43.  *    only after RESELECTION interrupts). Additionally, it should _not_ test
  44.  *    for more interrupts after running main, since a DMA process may have
  45.  *    been started and interrupts are turned on now. The new int could happen
  46.  *    inside the execution of NCR5380_intr(), leading to recursive
  47.  *    calls.
  48.  *
  49.  *  - I've added a function merge_contiguous_buffers() that tries to
  50.  *    merge scatter-gather buffers that are located at contiguous
  51.  *    physical addresses and can be processed with the same DMA setup.
  52.  *    Since most scatter-gather operations work on a page (4K) of
  53.  *    4 buffers (1K), in more than 90% of all cases three interrupts and
  54.  *    DMA setup actions are saved.
  55.  *
  56.  * - I've deleted all the stuff for AUTOPROBE_IRQ, REAL_DMA_POLL, PSEUDO_DMA
  57.  *    and USLEEP, because these were messing up readability and will never be
  58.  *    needed for Atari SCSI.
  59.  * 
  60.  * - I've revised the NCR5380_main() calling scheme (relax the 'main_running'
  61.  *   stuff), and 'main' is executed in a bottom half if awoken by an
  62.  *   interrupt.
  63.  *
  64.  * - The code was quite cluttered up by "#if (NDEBUG & NDEBUG_*) printk..."
  65.  *   constructs. In my eyes, this made the source rather unreadable, so I
  66.  *   finally replaced that by the *_PRINTK() macros.
  67.  *
  68.  */
  69. /*
  70.  * Further development / testing that should be done : 
  71.  * 1.  Test linked command handling code after Eric is ready with 
  72.  *     the high level code.
  73.  */
  74. #if (NDEBUG & NDEBUG_LISTS)
  75. #define LIST(x,y) 
  76.   { printk("LINE:%d   Adding %p to %pn", __LINE__, (void*)(x), (void*)(y)); 
  77.     if ((x)==(y)) udelay(5); }
  78. #define REMOVE(w,x,y,z) 
  79.   { printk("LINE:%d   Removing: %p->%p  %p->%p n", __LINE__, 
  80.    (void*)(w), (void*)(x), (void*)(y), (void*)(z)); 
  81.     if ((x)==(y)) udelay(5); }
  82. #else
  83. #define LIST(x,y)
  84. #define REMOVE(w,x,y,z)
  85. #endif
  86. #ifndef notyet
  87. #undef LINKED
  88. #endif
  89. /*
  90.  * Design
  91.  * Issues :
  92.  *
  93.  * The other Linux SCSI drivers were written when Linux was Intel PC-only,
  94.  * and specifically for each board rather than each chip.  This makes their
  95.  * adaptation to platforms like the Mac (Some of which use NCR5380's)
  96.  * more difficult than it has to be.
  97.  *
  98.  * Also, many of the SCSI drivers were written before the command queuing
  99.  * routines were implemented, meaning their implementations of queued 
  100.  * commands were hacked on rather than designed in from the start.
  101.  *
  102.  * When I designed the Linux SCSI drivers I figured that 
  103.  * while having two different SCSI boards in a system might be useful
  104.  * for debugging things, two of the same type wouldn't be used.
  105.  * Well, I was wrong and a number of users have mailed me about running
  106.  * multiple high-performance SCSI boards in a server.
  107.  *
  108.  * Finally, when I get questions from users, I have no idea what 
  109.  * revision of my driver they are running.
  110.  *
  111.  * This driver attempts to address these problems :
  112.  * This is a generic 5380 driver.  To use it on a different platform, 
  113.  * one simply writes appropriate system specific macros (ie, data
  114.  * transfer - some PC's will use the I/O bus, 68K's must use 
  115.  * memory mapped) and drops this file in their 'C' wrapper.
  116.  *
  117.  * As far as command queueing, two queues are maintained for 
  118.  * each 5380 in the system - commands that haven't been issued yet,
  119.  * and commands that are currently executing.  This means that an 
  120.  * unlimited number of commands may be queued, letting 
  121.  * more commands propagate from the higher driver levels giving higher 
  122.  * throughput.  Note that both I_T_L and I_T_L_Q nexuses are supported, 
  123.  * allowing multiple commands to propagate all the way to a SCSI-II device 
  124.  * while a command is already executing.
  125.  *
  126.  * To solve the multiple-boards-in-the-same-system problem, 
  127.  * there is a separate instance structure for each instance
  128.  * of a 5380 in the system.  So, multiple NCR5380 drivers will
  129.  * be able to coexist with appropriate changes to the high level
  130.  * SCSI code.  
  131.  *
  132.  * A NCR5380_PUBLIC_REVISION macro is provided, with the release
  133.  * number (updated for each public release) printed by the 
  134.  * NCR5380_print_options command, which should be called from the 
  135.  * wrapper detect function, so that I know what release of the driver
  136.  * users are using.
  137.  *
  138.  * Issues specific to the NCR5380 : 
  139.  *
  140.  * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead 
  141.  * piece of hardware that requires you to sit in a loop polling for 
  142.  * the REQ signal as long as you are connected.  Some devices are 
  143.  * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect 
  144.  * while doing long seek operations.
  145.  * 
  146.  * The workaround for this is to keep track of devices that have
  147.  * disconnected.  If the device hasn't disconnected, for commands that
  148.  * should disconnect, we do something like 
  149.  *
  150.  * while (!REQ is asserted) { sleep for N usecs; poll for M usecs }
  151.  * 
  152.  * Some tweaking of N and M needs to be done.  An algorithm based 
  153.  * on "time to data" would give the best results as long as short time
  154.  * to datas (ie, on the same track) were considered, however these 
  155.  * broken devices are the exception rather than the rule and I'd rather
  156.  * spend my time optimizing for the normal case.
  157.  *
  158.  * Architecture :
  159.  *
  160.  * At the heart of the design is a coroutine, NCR5380_main,
  161.  * which is started when not running by the interrupt handler,
  162.  * timer, and queue command function.  It attempts to establish
  163.  * I_T_L or I_T_L_Q nexuses by removing the commands from the 
  164.  * issue queue and calling NCR5380_select() if a nexus 
  165.  * is not established. 
  166.  *
  167.  * Once a nexus is established, the NCR5380_information_transfer()
  168.  * phase goes through the various phases as instructed by the target.
  169.  * if the target goes into MSG IN and sends a DISCONNECT message,
  170.  * the command structure is placed into the per instance disconnected
  171.  * queue, and NCR5380_main tries to find more work.  If USLEEP
  172.  * was defined, and the target is idle for too long, the system
  173.  * will try to sleep.
  174.  *
  175.  * If a command has disconnected, eventually an interrupt will trigger,
  176.  * calling NCR5380_intr()  which will in turn call NCR5380_reselect
  177.  * to reestablish a nexus.  This will run main if necessary.
  178.  *
  179.  * On command termination, the done function will be called as 
  180.  * appropriate.
  181.  *
  182.  * SCSI pointers are maintained in the SCp field of SCSI command 
  183.  * structures, being initialized after the command is connected
  184.  * in NCR5380_select, and set as appropriate in NCR5380_information_transfer.
  185.  * Note that in violation of the standard, an implicit SAVE POINTERS operation
  186.  * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS.
  187.  */
  188. /*
  189.  * Using this file :
  190.  * This file a skeleton Linux SCSI driver for the NCR 5380 series
  191.  * of chips.  To use it, you write an architecture specific functions 
  192.  * and macros and include this file in your driver.
  193.  *
  194.  * These macros control options : 
  195.  * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically
  196.  * for commands that return with a CHECK CONDITION status. 
  197.  *
  198.  * LINKED - if defined, linked commands are supported.
  199.  *
  200.  * REAL_DMA - if defined, REAL DMA is used during the data transfer phases.
  201.  *
  202.  * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible
  203.  *
  204.  * These macros MUST be defined :
  205.  * 
  206.  * NCR5380_read(register)  - read from the specified register
  207.  *
  208.  * NCR5380_write(register, value) - write to the specific register 
  209.  *
  210.  * Either real DMA *or* pseudo DMA may be implemented
  211.  * REAL functions : 
  212.  * NCR5380_REAL_DMA should be defined if real DMA is to be used.
  213.  * Note that the DMA setup functions should return the number of bytes 
  214.  * that they were able to program the controller for.
  215.  *
  216.  * Also note that generic i386/PC versions of these macros are 
  217.  * available as NCR5380_i386_dma_write_setup,
  218.  * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual.
  219.  *
  220.  * NCR5380_dma_write_setup(instance, src, count) - initialize
  221.  * NCR5380_dma_read_setup(instance, dst, count) - initialize
  222.  * NCR5380_dma_residual(instance); - residual count
  223.  *
  224.  * PSEUDO functions :
  225.  * NCR5380_pwrite(instance, src, count)
  226.  * NCR5380_pread(instance, dst, count);
  227.  *
  228.  * If nothing specific to this implementation needs doing (ie, with external
  229.  * hardware), you must also define 
  230.  *  
  231.  * NCR5380_queue_command
  232.  * NCR5380_reset
  233.  * NCR5380_abort
  234.  * NCR5380_proc_info
  235.  *
  236.  * to be the global entry points into the specific driver, ie 
  237.  * #define NCR5380_queue_command t128_queue_command.
  238.  *
  239.  * If this is not done, the routines will be defined as static functions
  240.  * with the NCR5380* names and the user must provide a globally
  241.  * accessible wrapper function.
  242.  *
  243.  * The generic driver is initialized by calling NCR5380_init(instance),
  244.  * after setting the appropriate host specific fields and ID.  If the 
  245.  * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance,
  246.  * possible) function may be used.  Before the specific driver initialization
  247.  * code finishes, NCR5380_print_options should be called.
  248.  */
  249. static struct Scsi_Host *first_instance = NULL;
  250. static Scsi_Host_Template *the_template = NULL;
  251. /* Macros ease life... :-) */
  252. #define SETUP_HOSTDATA(in)
  253.     struct NCR5380_hostdata *hostdata =
  254. (struct NCR5380_hostdata *)(in)->hostdata
  255. #define HOSTDATA(in) ((struct NCR5380_hostdata *)(in)->hostdata)
  256. #define NEXT(cmd) ((Scsi_Cmnd *)((cmd)->host_scribble))
  257. #define NEXTADDR(cmd) ((Scsi_Cmnd **)&((cmd)->host_scribble))
  258. #define HOSTNO instance->host_no
  259. #define H_NO(cmd) (cmd)->host->host_no
  260. #ifdef SUPPORT_TAGS
  261. /*
  262.  * Functions for handling tagged queuing
  263.  * =====================================
  264.  *
  265.  * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes:
  266.  *
  267.  * Using consecutive numbers for the tags is no good idea in my eyes. There
  268.  * could be wrong re-usings if the counter (8 bit!) wraps and some early
  269.  * command has been preempted for a long time. My solution: a bitfield for
  270.  * remembering used tags.
  271.  *
  272.  * There's also the problem that each target has a certain queue size, but we
  273.  * cannot know it in advance :-( We just see a QUEUE_FULL status being
  274.  * returned. So, in this case, the driver internal queue size assumption is
  275.  * reduced to the number of active tags if QUEUE_FULL is returned by the
  276.  * target. The command is returned to the mid-level, but with status changed
  277.  * to BUSY, since --as I've seen-- the mid-level can't handle QUEUE_FULL
  278.  * correctly.
  279.  *
  280.  * We're also not allowed running tagged commands as long as an untagged
  281.  * command is active. And REQUEST SENSE commands after a contingent allegiance
  282.  * condition _must_ be untagged. To keep track whether an untagged command has
  283.  * been issued, the host->busy array is still employed, as it is without
  284.  * support for tagged queuing.
  285.  *
  286.  * One could suspect that there are possible race conditions between
  287.  * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the
  288.  * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(),
  289.  * which already guaranteed to be running at most once. It is also the only
  290.  * place where tags/LUNs are allocated. So no other allocation can slip
  291.  * between that pair, there could only happen a reselection, which can free a
  292.  * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes
  293.  * important: the tag bit must be cleared before 'nr_allocated' is decreased.
  294.  */
  295. /* -1 for TAG_NONE is not possible with unsigned char cmd->tag */
  296. #undef TAG_NONE
  297. #define TAG_NONE 0xff
  298. /* For the m68k, the number of bits in 'allocated' must be a multiple of 32! */
  299. #if (MAX_TAGS % 32) != 0
  300. #error "MAX_TAGS must be a multiple of 32!"
  301. #endif
  302. typedef struct {
  303.     char allocated[MAX_TAGS/8];
  304.     int nr_allocated;
  305.     int queue_size;
  306. } TAG_ALLOC;
  307. static TAG_ALLOC TagAlloc[8][8]; /* 8 targets and 8 LUNs */
  308. static void __init init_tags( void )
  309. {
  310.     int target, lun;
  311.     TAG_ALLOC *ta;
  312.     
  313.     if (!setup_use_tagged_queuing)
  314. return;
  315.     
  316.     for( target = 0; target < 8; ++target ) {
  317. for( lun = 0; lun < 8; ++lun ) {
  318.     ta = &TagAlloc[target][lun];
  319.     memset( &ta->allocated, 0, MAX_TAGS/8 );
  320.     ta->nr_allocated = 0;
  321.     /* At the beginning, assume the maximum queue size we could
  322.      * support (MAX_TAGS). This value will be decreased if the target
  323.      * returns QUEUE_FULL status.
  324.      */
  325.     ta->queue_size = MAX_TAGS;
  326. }
  327.     }
  328. }
  329. /* Check if we can issue a command to this LUN: First see if the LUN is marked
  330.  * busy by an untagged command. If the command should use tagged queuing, also
  331.  * check that there is a free tag and the target's queue won't overflow. This
  332.  * function should be called with interrupts disabled to avoid race
  333.  * conditions.
  334.  */ 
  335. static int is_lun_busy( Scsi_Cmnd *cmd, int should_be_tagged )
  336. {
  337.     SETUP_HOSTDATA(cmd->host);
  338.     if (hostdata->busy[cmd->target] & (1 << cmd->lun))
  339. return( 1 );
  340.     if (!should_be_tagged ||
  341. !setup_use_tagged_queuing || !cmd->device->tagged_supported)
  342. return( 0 );
  343.     if (TagAlloc[cmd->target][cmd->lun].nr_allocated >=
  344. TagAlloc[cmd->target][cmd->lun].queue_size ) {
  345. TAG_PRINTK( "scsi%d: target %d lun %d: no free tagsn",
  346.     H_NO(cmd), cmd->target, cmd->lun );
  347. return( 1 );
  348.     }
  349.     return( 0 );
  350. }
  351. /* Allocate a tag for a command (there are no checks anymore, check_lun_busy()
  352.  * must be called before!), or reserve the LUN in 'busy' if the command is
  353.  * untagged.
  354.  */
  355. static void cmd_get_tag( Scsi_Cmnd *cmd, int should_be_tagged )
  356. {
  357.     SETUP_HOSTDATA(cmd->host);
  358.     /* If we or the target don't support tagged queuing, allocate the LUN for
  359.      * an untagged command.
  360.      */
  361.     if (!should_be_tagged ||
  362. !setup_use_tagged_queuing || !cmd->device->tagged_supported) {
  363. cmd->tag = TAG_NONE;
  364. hostdata->busy[cmd->target] |= (1 << cmd->lun);
  365. TAG_PRINTK( "scsi%d: target %d lun %d now allocated by untagged "
  366.     "commandn", H_NO(cmd), cmd->target, cmd->lun );
  367.     }
  368.     else {
  369. TAG_ALLOC *ta = &TagAlloc[cmd->target][cmd->lun];
  370. cmd->tag = find_first_zero_bit( &ta->allocated, MAX_TAGS );
  371. set_bit( cmd->tag, &ta->allocated );
  372. ta->nr_allocated++;
  373. TAG_PRINTK( "scsi%d: using tag %d for target %d lun %d "
  374.     "(now %d tags in use)n",
  375.     H_NO(cmd), cmd->tag, cmd->target, cmd->lun,
  376.     ta->nr_allocated );
  377.     }
  378. }
  379. /* Mark the tag of command 'cmd' as free, or in case of an untagged command,
  380.  * unlock the LUN.
  381.  */
  382. static void cmd_free_tag( Scsi_Cmnd *cmd )
  383. {
  384.     SETUP_HOSTDATA(cmd->host);
  385.     if (cmd->tag == TAG_NONE) {
  386. hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
  387. TAG_PRINTK( "scsi%d: target %d lun %d untagged cmd finishedn",
  388.     H_NO(cmd), cmd->target, cmd->lun );
  389.     }
  390.     else if (cmd->tag >= MAX_TAGS) {
  391. printk(KERN_NOTICE "scsi%d: trying to free bad tag %d!n",
  392. H_NO(cmd), cmd->tag );
  393.     }
  394.     else {
  395. TAG_ALLOC *ta = &TagAlloc[cmd->target][cmd->lun];
  396. clear_bit( cmd->tag, &ta->allocated );
  397. ta->nr_allocated--;
  398. TAG_PRINTK( "scsi%d: freed tag %d for target %d lun %dn",
  399.     H_NO(cmd), cmd->tag, cmd->target, cmd->lun );
  400.     }
  401. }
  402. static void free_all_tags( void )
  403. {
  404.     int target, lun;
  405.     TAG_ALLOC *ta;
  406.     if (!setup_use_tagged_queuing)
  407. return;
  408.     
  409.     for( target = 0; target < 8; ++target ) {
  410. for( lun = 0; lun < 8; ++lun ) {
  411.     ta = &TagAlloc[target][lun];
  412.     memset( &ta->allocated, 0, MAX_TAGS/8 );
  413.     ta->nr_allocated = 0;
  414. }
  415.     }
  416. }
  417. #endif /* SUPPORT_TAGS */
  418. /*
  419.  * Function: void merge_contiguous_buffers( Scsi_Cmnd *cmd )
  420.  *
  421.  * Purpose: Try to merge several scatter-gather requests into one DMA
  422.  *    transfer. This is possible if the scatter buffers lie on
  423.  *    physical contiguous addresses.
  424.  *
  425.  * Parameters: Scsi_Cmnd *cmd
  426.  *    The command to work on. The first scatter buffer's data are
  427.  *    assumed to be already transfered into ptr/this_residual.
  428.  */
  429. static void merge_contiguous_buffers( Scsi_Cmnd *cmd )
  430. {
  431.     unsigned long endaddr;
  432. #if (NDEBUG & NDEBUG_MERGING)
  433.     unsigned long oldlen = cmd->SCp.this_residual;
  434.     int   cnt = 1;
  435. #endif
  436.     for (endaddr = virt_to_phys(cmd->SCp.ptr + cmd->SCp.this_residual - 1) + 1;
  437.  cmd->SCp.buffers_residual &&
  438.  virt_to_phys(cmd->SCp.buffer[1].address) == endaddr; ) {
  439. MER_PRINTK("VTOP(%p) == %08lx -> mergingn",
  440.    cmd->SCp.buffer[1].address, endaddr);
  441. #if (NDEBUG & NDEBUG_MERGING)
  442. ++cnt;
  443. #endif
  444. ++cmd->SCp.buffer;
  445. --cmd->SCp.buffers_residual;
  446. cmd->SCp.this_residual += cmd->SCp.buffer->length;
  447. endaddr += cmd->SCp.buffer->length;
  448.     }
  449. #if (NDEBUG & NDEBUG_MERGING)
  450.     if (oldlen != cmd->SCp.this_residual)
  451. MER_PRINTK("merged %d buffers from %p, new length %08xn",
  452.    cnt, cmd->SCp.ptr, cmd->SCp.this_residual);
  453. #endif
  454. }
  455. /*
  456.  * Function : void initialize_SCp(Scsi_Cmnd *cmd)
  457.  *
  458.  * Purpose : initialize the saved data pointers for cmd to point to the 
  459.  * start of the buffer.
  460.  *
  461.  * Inputs : cmd - Scsi_Cmnd structure to have pointers reset.
  462.  */
  463. static __inline__ void initialize_SCp(Scsi_Cmnd *cmd)
  464. {
  465.     /* 
  466.      * Initialize the Scsi Pointer field so that all of the commands in the 
  467.      * various queues are valid.
  468.      */
  469.     if (cmd->use_sg) {
  470. cmd->SCp.buffer = (struct scatterlist *) cmd->buffer;
  471. cmd->SCp.buffers_residual = cmd->use_sg - 1;
  472. cmd->SCp.ptr = (char *) cmd->SCp.buffer->address;
  473. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  474. /* ++roman: Try to merge some scatter-buffers if they are at
  475.  * contiguous physical addresses.
  476.  */
  477. merge_contiguous_buffers( cmd );
  478.     } else {
  479. cmd->SCp.buffer = NULL;
  480. cmd->SCp.buffers_residual = 0;
  481. cmd->SCp.ptr = (char *) cmd->request_buffer;
  482. cmd->SCp.this_residual = cmd->request_bufflen;
  483.     }
  484. }
  485. #include <linux/config.h>
  486. #include <linux/delay.h>
  487. #if NDEBUG
  488. static struct {
  489.     unsigned char mask;
  490.     const char * name;} 
  491. signals[] = {{ SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" }, 
  492.     { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD,  "CD" }, { SR_IO, "IO" }, 
  493.     { SR_SEL, "SEL" }, {0, NULL}}, 
  494. basrs[] = {{BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL}},
  495. icrs[] = {{ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"},
  496.     {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"}, 
  497.     {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"}, 
  498.     {0, NULL}},
  499. mrs[] = {{MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"}, 
  500.     {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR, 
  501.     "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"},
  502.     {MR_MONITOR_BSY, "MODE MONITOR BSY"},
  503.     {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"}, 
  504.     {0, NULL}};
  505. /*
  506.  * Function : void NCR5380_print(struct Scsi_Host *instance)
  507.  *
  508.  * Purpose : print the SCSI bus signals for debugging purposes
  509.  *
  510.  * Input : instance - which NCR5380
  511.  */
  512. static void NCR5380_print(struct Scsi_Host *instance) {
  513.     unsigned char status, data, basr, mr, icr, i;
  514.     unsigned long flags;
  515.     save_flags(flags);
  516.     cli();
  517.     data = NCR5380_read(CURRENT_SCSI_DATA_REG);
  518.     status = NCR5380_read(STATUS_REG);
  519.     mr = NCR5380_read(MODE_REG);
  520.     icr = NCR5380_read(INITIATOR_COMMAND_REG);
  521.     basr = NCR5380_read(BUS_AND_STATUS_REG);
  522.     restore_flags(flags);
  523.     printk("STATUS_REG: %02x ", status);
  524.     for (i = 0; signals[i].mask ; ++i) 
  525. if (status & signals[i].mask)
  526.     printk(",%s", signals[i].name);
  527.     printk("nBASR: %02x ", basr);
  528.     for (i = 0; basrs[i].mask ; ++i) 
  529. if (basr & basrs[i].mask)
  530.     printk(",%s", basrs[i].name);
  531.     printk("nICR: %02x ", icr);
  532.     for (i = 0; icrs[i].mask; ++i) 
  533. if (icr & icrs[i].mask)
  534.     printk(",%s", icrs[i].name);
  535.     printk("nMODE: %02x ", mr);
  536.     for (i = 0; mrs[i].mask; ++i) 
  537. if (mr & mrs[i].mask)
  538.     printk(",%s", mrs[i].name);
  539.     printk("n");
  540. }
  541. static struct {
  542.     unsigned char value;
  543.     const char *name;
  544. } phases[] = {
  545.     {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"},
  546.     {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"},
  547.     {PHASE_UNKNOWN, "UNKNOWN"}};
  548. /* 
  549.  * Function : void NCR5380_print_phase(struct Scsi_Host *instance)
  550.  *
  551.  * Purpose : print the current SCSI phase for debugging purposes
  552.  *
  553.  * Input : instance - which NCR5380
  554.  */
  555. static void NCR5380_print_phase(struct Scsi_Host *instance)
  556. {
  557.     unsigned char status;
  558.     int i;
  559.     status = NCR5380_read(STATUS_REG);
  560.     if (!(status & SR_REQ)) 
  561. printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.n", HOSTNO);
  562.     else {
  563. for (i = 0; (phases[i].value != PHASE_UNKNOWN) && 
  564.     (phases[i].value != (status & PHASE_MASK)); ++i); 
  565. printk(KERN_DEBUG "scsi%d: phase %sn", HOSTNO, phases[i].name);
  566.     }
  567. }
  568. #else /* !NDEBUG */
  569. /* dummies... */
  570. __inline__ void NCR5380_print(struct Scsi_Host *instance) { };
  571. __inline__ void NCR5380_print_phase(struct Scsi_Host *instance) { };
  572. #endif
  573. /*
  574.  * ++roman: New scheme of calling NCR5380_main()
  575.  * 
  576.  * If we're not in an interrupt, we can call our main directly, it cannot be
  577.  * already running. Else, we queue it on a task queue, if not 'main_running'
  578.  * tells us that a lower level is already executing it. This way,
  579.  * 'main_running' needs not be protected in a special way.
  580.  *
  581.  * queue_main() is a utility function for putting our main onto the task
  582.  * queue, if main_running is false. It should be called only from a
  583.  * interrupt or bottom half.
  584.  */
  585. #include <linux/tqueue.h>
  586. #include <linux/interrupt.h>
  587. static volatile int main_running = 0;
  588. static struct tq_struct NCR5380_tqueue = {
  589.     NULL, /* next */
  590.     0, /* sync */
  591.     (void (*)(void*))NCR5380_main,  /* routine, must have (void *) arg... */
  592.     NULL /* data */
  593. };
  594. static __inline__ void queue_main(void)
  595. {
  596.     if (!main_running) {
  597. /* If in interrupt and NCR5380_main() not already running,
  598.    queue it on the 'immediate' task queue, to be processed
  599.    immediately after the current interrupt processing has
  600.    finished. */
  601. queue_task(&NCR5380_tqueue, &tq_immediate);
  602. mark_bh(IMMEDIATE_BH);
  603.     }
  604.     /* else: nothing to do: the running NCR5380_main() will pick up
  605.        any newly queued command. */
  606. }
  607. static inline void NCR5380_all_init (void)
  608. {
  609.     static int done = 0;
  610.     if (!done) {
  611. INI_PRINTK("scsi : NCR5380_all_init()n");
  612. done = 1;
  613.     }
  614. }
  615.  
  616. /*
  617.  * Function : void NCR58380_print_options (struct Scsi_Host *instance)
  618.  *
  619.  * Purpose : called by probe code indicating the NCR5380 driver
  620.  *      options that were selected.
  621.  *
  622.  * Inputs : instance, pointer to this instance.  Unused.
  623.  */
  624. static void __init NCR5380_print_options (struct Scsi_Host *instance)
  625. {
  626.     printk(" generic options"
  627. #ifdef AUTOSENSE 
  628.     " AUTOSENSE"
  629. #endif
  630. #ifdef REAL_DMA
  631.     " REAL DMA"
  632. #endif
  633. #ifdef PARITY
  634.     " PARITY"
  635. #endif
  636. #ifdef SUPPORT_TAGS
  637.     " SCSI-2 TAGGED QUEUING"
  638. #endif
  639.     );
  640.     printk(" generic release=%d", NCR5380_PUBLIC_RELEASE);
  641. }
  642. /*
  643.  * Function : void NCR5380_print_status (struct Scsi_Host *instance)
  644.  *
  645.  * Purpose : print commands in the various queues, called from
  646.  * NCR5380_abort and NCR5380_debug to aid debugging.
  647.  *
  648.  * Inputs : instance, pointer to this instance.  
  649.  */
  650. static void NCR5380_print_status (struct Scsi_Host *instance)
  651. {
  652.     char *pr_bfr;
  653.     char *start;
  654.     int len;
  655.     NCR_PRINT(NDEBUG_ANY);
  656.     NCR_PRINT_PHASE(NDEBUG_ANY);
  657.     pr_bfr = (char *) __get_free_page(GFP_ATOMIC);
  658.     if (!pr_bfr) {
  659. printk("NCR5380_print_status: no memory for print buffern");
  660. return;
  661.     }
  662.     len = NCR5380_proc_info(pr_bfr, &start, 0, PAGE_SIZE, HOSTNO, 0);
  663.     pr_bfr[len] = 0;
  664.     printk("n%sn", pr_bfr);
  665.     free_page((unsigned long) pr_bfr);
  666. }
  667. /******************************************/
  668. /*
  669.  * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED]
  670.  *
  671.  * *buffer: I/O buffer
  672.  * **start: if inout == FALSE pointer into buffer where user read should start
  673.  * offset: current offset
  674.  * length: length of buffer
  675.  * hostno: Scsi_Host host_no
  676.  * inout: TRUE - user is writing; FALSE - user is reading
  677.  *
  678.  * Return the number of bytes read from or written
  679. */
  680. #undef SPRINTF
  681. #define SPRINTF(fmt,args...) 
  682.   do { if (pos + strlen(fmt) + 20 /* slop */ < buffer + length) 
  683.  pos += sprintf(pos, fmt , ## args); } while(0)
  684. static
  685. char *lprint_Scsi_Cmnd (Scsi_Cmnd *cmd, char *pos, char *buffer, int length);
  686. #ifndef NCR5380_proc_info
  687. static
  688. #endif
  689. int NCR5380_proc_info (char *buffer, char **start, off_t offset,
  690.        int length, int hostno, int inout)
  691. {
  692.     char *pos = buffer;
  693.     struct Scsi_Host *instance;
  694.     struct NCR5380_hostdata *hostdata;
  695.     Scsi_Cmnd *ptr;
  696.     unsigned long flags;
  697.     off_t begin = 0;
  698. #define check_offset()
  699.     do {
  700. if (pos - buffer < offset - begin) {
  701.     begin += pos - buffer;
  702.     pos = buffer;
  703. }
  704.     } while (0)
  705.     for (instance = first_instance; instance && HOSTNO != hostno;
  706.  instance = instance->next)
  707. ;
  708.     if (!instance)
  709. return(-ESRCH);
  710.     hostdata = (struct NCR5380_hostdata *)instance->hostdata;
  711.     if (inout) { /* Has data been written to the file ? */
  712. return(-ENOSYS);  /* Currently this is a no-op */
  713.     }
  714.     SPRINTF("NCR5380 core release=%d.n", NCR5380_PUBLIC_RELEASE);
  715.     check_offset();
  716.     save_flags(flags);
  717.     cli();
  718.     SPRINTF("NCR5380: coroutine is%s running.n", main_running ? "" : "n't");
  719.     check_offset();
  720.     if (!hostdata->connected)
  721. SPRINTF("scsi%d: no currently connected commandn", HOSTNO);
  722.     else
  723. pos = lprint_Scsi_Cmnd ((Scsi_Cmnd *) hostdata->connected,
  724. pos, buffer, length);
  725.     SPRINTF("scsi%d: issue_queuen", HOSTNO);
  726.     check_offset();
  727.     for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr; ptr = NEXT(ptr)) {
  728. pos = lprint_Scsi_Cmnd (ptr, pos, buffer, length);
  729. check_offset();
  730.     }
  731.     SPRINTF("scsi%d: disconnected_queuen", HOSTNO);
  732.     check_offset();
  733.     for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr;
  734.  ptr = NEXT(ptr)) {
  735. pos = lprint_Scsi_Cmnd (ptr, pos, buffer, length);
  736. check_offset();
  737.     }
  738.     restore_flags(flags);
  739.     *start = buffer + (offset - begin);
  740.     if (pos - buffer < offset - begin)
  741. return 0;
  742.     else if (pos - buffer - (offset - begin) < length)
  743. return pos - buffer - (offset - begin);
  744.     return length;
  745. }
  746. static char *
  747. lprint_Scsi_Cmnd (Scsi_Cmnd *cmd, char *pos, char *buffer, int length)
  748. {
  749.     int i, s;
  750.     unsigned char *command;
  751.     SPRINTF("scsi%d: destination target %d, lun %dn",
  752.     H_NO(cmd), cmd->target, cmd->lun);
  753.     SPRINTF("        command = ");
  754.     command = cmd->cmnd;
  755.     SPRINTF("%2d (0x%02x)", command[0], command[0]);
  756.     for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i)
  757. SPRINTF(" %02x", command[i]);
  758.     SPRINTF("n");
  759.     return pos;
  760. }
  761. /* 
  762.  * Function : void NCR5380_init (struct Scsi_Host *instance)
  763.  *
  764.  * Purpose : initializes *instance and corresponding 5380 chip.
  765.  *
  766.  * Inputs : instance - instantiation of the 5380 driver.  
  767.  *
  768.  * Notes : I assume that the host, hostno, and id bits have been
  769.  *  set correctly.  I don't care about the irq and other fields. 
  770.  * 
  771.  */
  772. static void __init NCR5380_init (struct Scsi_Host *instance, int flags)
  773. {
  774.     int i;
  775.     SETUP_HOSTDATA(instance);
  776.     NCR5380_all_init();
  777.     hostdata->aborted = 0;
  778.     hostdata->id_mask = 1 << instance->this_id;
  779.     hostdata->id_higher_mask = 0;
  780.     for (i = hostdata->id_mask; i <= 0x80; i <<= 1)
  781. if (i > hostdata->id_mask)
  782.     hostdata->id_higher_mask |= i;
  783.     for (i = 0; i < 8; ++i)
  784. hostdata->busy[i] = 0;
  785. #ifdef SUPPORT_TAGS
  786.     init_tags();
  787. #endif
  788. #if defined (REAL_DMA)
  789.     hostdata->dma_len = 0;
  790. #endif
  791.     hostdata->targets_present = 0;
  792.     hostdata->connected = NULL;
  793.     hostdata->issue_queue = NULL;
  794.     hostdata->disconnected_queue = NULL;
  795.     hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT;
  796.     if (!the_template) {
  797. the_template = instance->hostt;
  798. first_instance = instance;
  799.     }
  800. #ifndef AUTOSENSE
  801.     if ((instance->cmd_per_lun > 1) || (instance->can_queue > 1))
  802.  printk("scsi%d: WARNING : support for multiple outstanding commands enabledn"
  803.         "        without AUTOSENSE option, contingent allegiance conditions mayn"
  804.         "        be incorrectly cleared.n", HOSTNO);
  805. #endif /* def AUTOSENSE */
  806.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  807.     NCR5380_write(MODE_REG, MR_BASE);
  808.     NCR5380_write(TARGET_COMMAND_REG, 0);
  809.     NCR5380_write(SELECT_ENABLE_REG, 0);
  810. }
  811. /* 
  812.  * Function : int NCR5380_queue_command (Scsi_Cmnd *cmd, 
  813.  * void (*done)(Scsi_Cmnd *)) 
  814.  *
  815.  * Purpose :  enqueues a SCSI command
  816.  *
  817.  * Inputs : cmd - SCSI command, done - function called on completion, with
  818.  * a pointer to the command descriptor.
  819.  * 
  820.  * Returns : 0
  821.  *
  822.  * Side effects : 
  823.  *      cmd is added to the per instance issue_queue, with minor 
  824.  * twiddling done to the host specific fields of cmd.  If the 
  825.  * main coroutine is not running, it is restarted.
  826.  *
  827.  */
  828. /* Only make static if a wrapper function is used */
  829. #ifndef NCR5380_queue_command
  830. static
  831. #endif
  832. int NCR5380_queue_command (Scsi_Cmnd *cmd, void (*done)(Scsi_Cmnd *))
  833. {
  834.     SETUP_HOSTDATA(cmd->host);
  835.     Scsi_Cmnd *tmp;
  836.     int oldto;
  837.     unsigned long flags;
  838.     extern int update_timeout(Scsi_Cmnd * SCset, int timeout);
  839. #if (NDEBUG & NDEBUG_NO_WRITE)
  840.     switch (cmd->cmnd[0]) {
  841.     case WRITE_6:
  842.     case WRITE_10:
  843. printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag setn",
  844.        H_NO(cmd));
  845. cmd->result = (DID_ERROR << 16);
  846. done(cmd);
  847. return 0;
  848.     }
  849. #endif /* (NDEBUG & NDEBUG_NO_WRITE) */
  850. #ifdef NCR5380_STATS
  851. # if 0
  852.     if (!hostdata->connected && !hostdata->issue_queue &&
  853. !hostdata->disconnected_queue) {
  854. hostdata->timebase = jiffies;
  855.     }
  856. # endif
  857. # ifdef NCR5380_STAT_LIMIT
  858.     if (cmd->request_bufflen > NCR5380_STAT_LIMIT)
  859. # endif
  860. switch (cmd->cmnd[0])
  861. {
  862.     case WRITE:
  863.     case WRITE_6:
  864.     case WRITE_10:
  865. hostdata->time_write[cmd->target] -= (jiffies - hostdata->timebase);
  866. hostdata->bytes_write[cmd->target] += cmd->request_bufflen;
  867. hostdata->pendingw++;
  868. break;
  869.     case READ:
  870.     case READ_6:
  871.     case READ_10:
  872. hostdata->time_read[cmd->target] -= (jiffies - hostdata->timebase);
  873. hostdata->bytes_read[cmd->target] += cmd->request_bufflen;
  874. hostdata->pendingr++;
  875. break;
  876. }
  877. #endif
  878.     /* 
  879.      * We use the host_scribble field as a pointer to the next command  
  880.      * in a queue 
  881.      */
  882.     NEXT(cmd) = NULL;
  883.     cmd->scsi_done = done;
  884.     cmd->result = 0;
  885.     /* 
  886.      * Insert the cmd into the issue queue. Note that REQUEST SENSE 
  887.      * commands are added to the head of the queue since any command will
  888.      * clear the contingent allegiance condition that exists and the 
  889.      * sense data is only guaranteed to be valid while the condition exists.
  890.      */
  891.     save_flags(flags);
  892.     cli();
  893.     /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
  894.      * Otherwise a running NCR5380_main may steal the lock.
  895.      * Lock before actually inserting due to fairness reasons explained in
  896.      * atari_scsi.c. If we insert first, then it's impossible for this driver
  897.      * to release the lock.
  898.      * Stop timer for this command while waiting for the lock, or timeouts
  899.      * may happen (and they really do), and it's no good if the command doesn't
  900.      * appear in any of the queues.
  901.      * ++roman: Just disabling the NCR interrupt isn't sufficient here,
  902.      * because also a timer int can trigger an abort or reset, which would
  903.      * alter queues and touch the lock.
  904.      */
  905.     if (!IS_A_TT()) {
  906. oldto = update_timeout(cmd, 0);
  907. falcon_get_lock();
  908. update_timeout(cmd, oldto);
  909.     }
  910.     if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
  911. LIST(cmd, hostdata->issue_queue);
  912. NEXT(cmd) = hostdata->issue_queue;
  913. hostdata->issue_queue = cmd;
  914.     } else {
  915. for (tmp = (Scsi_Cmnd *)hostdata->issue_queue;
  916.      NEXT(tmp); tmp = NEXT(tmp))
  917.     ;
  918. LIST(cmd, tmp);
  919. NEXT(tmp) = cmd;
  920.     }
  921.     restore_flags(flags);
  922.     QU_PRINTK("scsi%d: command added to %s of queuen", H_NO(cmd),
  923.       (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");
  924.     /* If queue_command() is called from an interrupt (real one or bottom
  925.      * half), we let queue_main() do the job of taking care about main. If it
  926.      * is already running, this is a no-op, else main will be queued.
  927.      *
  928.      * If we're not in an interrupt, we can call NCR5380_main()
  929.      * unconditionally, because it cannot be already running.
  930.      */
  931.     if (in_interrupt() || ((flags >> 8) & 7) >= 6)
  932. queue_main();
  933.     else
  934. NCR5380_main();
  935.     return 0;
  936. }
  937. /*
  938.  * Function : NCR5380_main (void) 
  939.  *
  940.  * Purpose : NCR5380_main is a coroutine that runs as long as more work can 
  941.  * be done on the NCR5380 host adapters in a system.  Both 
  942.  * NCR5380_queue_command() and NCR5380_intr() will try to start it 
  943.  * in case it is not running.
  944.  * 
  945.  * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should 
  946.  *  reenable them.  This prevents reentrancy and kernel stack overflow.
  947.  */ 
  948.     
  949. static void NCR5380_main (void)
  950. {
  951.     Scsi_Cmnd *tmp, *prev;
  952.     struct Scsi_Host *instance = first_instance;
  953.     struct NCR5380_hostdata *hostdata = HOSTDATA(instance);
  954.     int done;
  955.     unsigned long flags;
  956.     
  957.     /*
  958.      * We run (with interrupts disabled) until we're sure that none of 
  959.      * the host adapters have anything that can be done, at which point 
  960.      * we set main_running to 0 and exit.
  961.      *
  962.      * Interrupts are enabled before doing various other internal 
  963.      * instructions, after we've decided that we need to run through
  964.      * the loop again.
  965.      *
  966.      * this should prevent any race conditions.
  967.      * 
  968.      * ++roman: Just disabling the NCR interrupt isn't sufficient here,
  969.      * because also a timer int can trigger an abort or reset, which can
  970.      * alter queues and touch the Falcon lock.
  971.      */
  972.     /* Tell int handlers main() is now already executing.  Note that
  973.        no races are possible here. If an int comes in before
  974.        'main_running' is set here, and queues/executes main via the
  975.        task queue, it doesn't do any harm, just this instance of main
  976.        won't find any work left to do. */
  977.     if (main_running)
  978.      return;
  979.     main_running = 1;
  980.     save_flags(flags);
  981.     do {
  982. cli(); /* Freeze request queues */
  983. done = 1;
  984. if (!hostdata->connected) {
  985.     MAIN_PRINTK( "scsi%d: not connectedn", HOSTNO );
  986.     /*
  987.      * Search through the issue_queue for a command destined
  988.      * for a target that's not busy.
  989.      */
  990. #if (NDEBUG & NDEBUG_LISTS)
  991.     for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL;
  992.  tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
  993. ;
  994. /*printk("%p  ", tmp);*/
  995.     if ((tmp == prev) && tmp) printk(" LOOPn");/* else printk("n");*/
  996. #endif
  997.     for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, 
  998.  prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp) ) {
  999. #if (NDEBUG & NDEBUG_LISTS)
  1000. if (prev != tmp)
  1001.     printk("MAIN tmp=%p   target=%d   busy=%d lun=%dn",
  1002.    tmp, tmp->target, hostdata->busy[tmp->target],
  1003.    tmp->lun);
  1004. #endif
  1005. /*  When we find one, remove it from the issue queue. */
  1006. /* ++guenther: possible race with Falcon locking */
  1007. if (
  1008. #ifdef SUPPORT_TAGS
  1009.     !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
  1010. #else
  1011.     !(hostdata->busy[tmp->target] & (1 << tmp->lun))
  1012. #endif
  1013.     ) {
  1014.     cli(); /* ++guenther: just to be sure, this must be atomic */
  1015.     if (prev) {
  1016.         REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
  1017. NEXT(prev) = NEXT(tmp);
  1018.     } else {
  1019.         REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
  1020. hostdata->issue_queue = NEXT(tmp);
  1021.     }
  1022.     NEXT(tmp) = NULL;
  1023.     falcon_dont_release++;
  1024.     
  1025.     /* reenable interrupts after finding one */
  1026.     restore_flags(flags);
  1027.     
  1028.     /* 
  1029.      * Attempt to establish an I_T_L nexus here. 
  1030.      * On success, instance->hostdata->connected is set.
  1031.      * On failure, we must add the command back to the
  1032.      *   issue queue so we can keep trying.
  1033.      */
  1034.     MAIN_PRINTK("scsi%d: main(): command for target %d "
  1035. "lun %d removed from issue_queuen",
  1036. HOSTNO, tmp->target, tmp->lun);
  1037.     /* 
  1038.      * REQUEST SENSE commands are issued without tagged
  1039.      * queueing, even on SCSI-II devices because the 
  1040.      * contingent allegiance condition exists for the 
  1041.      * entire unit.
  1042.      */
  1043.     /* ++roman: ...and the standard also requires that
  1044.      * REQUEST SENSE command are untagged.
  1045.      */
  1046.     
  1047. #ifdef SUPPORT_TAGS
  1048.     cmd_get_tag( tmp, tmp->cmnd[0] != REQUEST_SENSE );
  1049. #endif
  1050.     if (!NCR5380_select(instance, tmp, 
  1051.     (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : 
  1052.     TAG_NEXT)) {
  1053. falcon_dont_release--;
  1054. /* release if target did not response! */
  1055. falcon_release_lock_if_possible( hostdata );
  1056. break;
  1057.     } else {
  1058. cli();
  1059. LIST(tmp, hostdata->issue_queue);
  1060. NEXT(tmp) = hostdata->issue_queue;
  1061. hostdata->issue_queue = tmp;
  1062. #ifdef SUPPORT_TAGS
  1063. cmd_free_tag( tmp );
  1064. #endif
  1065. falcon_dont_release--;
  1066. restore_flags(flags);
  1067. MAIN_PRINTK("scsi%d: main(): select() failed, "
  1068.     "returned to issue_queuen", HOSTNO);
  1069. if (hostdata->connected)
  1070.     break;
  1071.     }
  1072. } /* if target/lun/target queue is not busy */
  1073.     } /* for issue_queue */
  1074. } /* if (!hostdata->connected) */
  1075. if (hostdata->connected 
  1076. #ifdef REAL_DMA
  1077.     && !hostdata->dma_len
  1078. #endif
  1079.     ) {
  1080.     restore_flags(flags);
  1081.     MAIN_PRINTK("scsi%d: main: performing information transfern",
  1082. HOSTNO);
  1083.     NCR5380_information_transfer(instance);
  1084.     MAIN_PRINTK("scsi%d: main: done set falsen", HOSTNO);
  1085.     done = 0;
  1086. }
  1087.     } while (!done);
  1088.     /* Better allow ints _after_ 'main_running' has been cleared, else
  1089.        an interrupt could believe we'll pick up the work it left for
  1090.        us, but we won't see it anymore here... */
  1091.     main_running = 0;
  1092.     restore_flags(flags);
  1093. }
  1094. #ifdef REAL_DMA
  1095. /*
  1096.  * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
  1097.  *
  1098.  * Purpose : Called by interrupt handler when DMA finishes or a phase
  1099.  * mismatch occurs (which would finish the DMA transfer).  
  1100.  *
  1101.  * Inputs : instance - this instance of the NCR5380.
  1102.  *
  1103.  */
  1104. static void NCR5380_dma_complete( struct Scsi_Host *instance )
  1105. {
  1106.     SETUP_HOSTDATA(instance);
  1107.     int           transfered, saved_data = 0, overrun = 0, cnt, toPIO;
  1108.     unsigned char **data, p;
  1109.     volatile int  *count;
  1110.     if (!hostdata->connected) {
  1111. printk(KERN_WARNING "scsi%d: received end of DMA interrupt with "
  1112.        "no connected cmdn", HOSTNO);
  1113. return;
  1114.     }
  1115.     
  1116.     if (atari_read_overruns) {
  1117. p = hostdata->connected->SCp.phase;
  1118. if (p & SR_IO) {
  1119.     udelay(10);
  1120.     if ((((NCR5380_read(BUS_AND_STATUS_REG)) &
  1121.   (BASR_PHASE_MATCH|BASR_ACK)) ==
  1122.  (BASR_PHASE_MATCH|BASR_ACK))) {
  1123. saved_data = NCR5380_read(INPUT_DATA_REG);
  1124. overrun = 1;
  1125. DMA_PRINTK("scsi%d: read overrun handledn", HOSTNO);
  1126.     }
  1127. }
  1128.     }
  1129.     DMA_PRINTK("scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%Xn",
  1130.        HOSTNO, NCR5380_read(BUS_AND_STATUS_REG),
  1131.        NCR5380_read(STATUS_REG));
  1132.     (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1133.     NCR5380_write(MODE_REG, MR_BASE);
  1134.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1135.     transfered = hostdata->dma_len - NCR5380_dma_residual(instance);
  1136.     hostdata->dma_len = 0;
  1137.     data = (unsigned char **) &(hostdata->connected->SCp.ptr);
  1138.     count = &(hostdata->connected->SCp.this_residual);
  1139.     *data += transfered;
  1140.     *count -= transfered;
  1141.     if (atari_read_overruns) {
  1142. if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
  1143.     cnt = toPIO = atari_read_overruns;
  1144.     if (overrun) {
  1145. DMA_PRINTK("Got an input overrun, using saved byten");
  1146. *(*data)++ = saved_data;
  1147. (*count)--;
  1148. cnt--;
  1149. toPIO--;
  1150.     }
  1151.     DMA_PRINTK("Doing %d-byte PIO to 0x%08lxn", cnt, (long)*data);
  1152.     NCR5380_transfer_pio(instance, &p, &cnt, data);
  1153.     *count -= toPIO - cnt;
  1154. }
  1155.     }
  1156. }
  1157. #endif /* REAL_DMA */
  1158. /*
  1159.  * Function : void NCR5380_intr (int irq)
  1160.  * 
  1161.  * Purpose : handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
  1162.  * from the disconnected queue, and restarting NCR5380_main() 
  1163.  * as required.
  1164.  *
  1165.  * Inputs : int irq, irq that caused this interrupt.
  1166.  *
  1167.  */
  1168. static void NCR5380_intr (int irq, void *dev_id, struct pt_regs *regs)
  1169. {
  1170.     struct Scsi_Host *instance = first_instance;
  1171.     int done = 1;
  1172.     unsigned char basr;
  1173.     INT_PRINTK("scsi%d: NCR5380 irq triggeredn", HOSTNO);
  1174.     /* Look for pending interrupts */
  1175.     basr = NCR5380_read(BUS_AND_STATUS_REG);
  1176.     INT_PRINTK("scsi%d: BASR=%02xn", HOSTNO, basr);
  1177.     /* dispatch to appropriate routine if found and done=0 */
  1178.     if (basr & BASR_IRQ) {
  1179. NCR_PRINT(NDEBUG_INTR);
  1180. if ((NCR5380_read(STATUS_REG) & (SR_SEL|SR_IO)) == (SR_SEL|SR_IO)) {
  1181.     done = 0;
  1182.     ENABLE_IRQ();
  1183.     INT_PRINTK("scsi%d: SEL interruptn", HOSTNO);
  1184.     NCR5380_reselect(instance);
  1185.     (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1186. }
  1187. else if (basr & BASR_PARITY_ERROR) {
  1188.     INT_PRINTK("scsi%d: PARITY interruptn", HOSTNO);
  1189.     (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1190. }
  1191. else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
  1192.     INT_PRINTK("scsi%d: RESET interruptn", HOSTNO);
  1193.     (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1194. }
  1195. else {
  1196.     /*  
  1197.      * The rest of the interrupt conditions can occur only during a
  1198.      * DMA transfer
  1199.      */
  1200. #if defined(REAL_DMA)
  1201.     /*
  1202.      * We should only get PHASE MISMATCH and EOP interrupts if we have
  1203.      * DMA enabled, so do a sanity check based on the current setting
  1204.      * of the MODE register.
  1205.      */
  1206.     if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) &&
  1207. ((basr & BASR_END_DMA_TRANSFER) || 
  1208.  !(basr & BASR_PHASE_MATCH))) {
  1209.     
  1210. INT_PRINTK("scsi%d: PHASE MISM or EOP interruptn", HOSTNO);
  1211. NCR5380_dma_complete( instance );
  1212. done = 0;
  1213. ENABLE_IRQ();
  1214.     } else
  1215. #endif /* REAL_DMA */
  1216.     {
  1217. /* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
  1218. if (basr & BASR_PHASE_MATCH)
  1219.     printk(KERN_NOTICE "scsi%d: unknown interrupt, "
  1220.    "BASR 0x%x, MR 0x%x, SR 0x%xn",
  1221.    HOSTNO, basr, NCR5380_read(MODE_REG),
  1222.    NCR5380_read(STATUS_REG));
  1223. (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1224.     }
  1225. } /* if !(SELECTION || PARITY) */
  1226.     } /* BASR & IRQ */
  1227.     else {
  1228. printk(KERN_NOTICE "scsi%d: interrupt without IRQ bit set in BASR, "
  1229.        "BASR 0x%X, MR 0x%X, SR 0x%xn", HOSTNO, basr,
  1230.        NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
  1231. (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
  1232.     }
  1233.     
  1234.     if (!done) {
  1235. INT_PRINTK("scsi%d: in int routine, calling mainn", HOSTNO);
  1236. /* Put a call to NCR5380_main() on the queue... */
  1237. queue_main();
  1238.     }
  1239. }
  1240. #ifdef NCR5380_STATS
  1241. static void collect_stats(struct NCR5380_hostdata* hostdata, Scsi_Cmnd* cmd)
  1242. {
  1243. # ifdef NCR5380_STAT_LIMIT
  1244.     if (cmd->request_bufflen > NCR5380_STAT_LIMIT)
  1245. # endif
  1246. switch (cmd->cmnd[0])
  1247. {
  1248.     case WRITE:
  1249.     case WRITE_6:
  1250.     case WRITE_10:
  1251. hostdata->time_write[cmd->target] += (jiffies - hostdata->timebase);
  1252. /*hostdata->bytes_write[cmd->target] += cmd->request_bufflen;*/
  1253. hostdata->pendingw--;
  1254. break;
  1255.     case READ:
  1256.     case READ_6:
  1257.     case READ_10:
  1258. hostdata->time_read[cmd->target] += (jiffies - hostdata->timebase);
  1259. /*hostdata->bytes_read[cmd->target] += cmd->request_bufflen;*/
  1260. hostdata->pendingr--;
  1261. break;
  1262. }
  1263. }
  1264. #endif
  1265. /* 
  1266.  * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd, 
  1267.  * int tag);
  1268.  *
  1269.  * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
  1270.  * including ARBITRATION, SELECTION, and initial message out for 
  1271.  * IDENTIFY and queue messages. 
  1272.  *
  1273.  * Inputs : instance - instantiation of the 5380 driver on which this 
  1274.  *  target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for 
  1275.  * new tag, TAG_NONE for untagged queueing, otherwise set to the tag for 
  1276.  * the command that is presently connected.
  1277.  * 
  1278.  * Returns : -1 if selection could not execute for some reason,
  1279.  * 0 if selection succeeded or failed because the target 
  1280.  *  did not respond.
  1281.  *
  1282.  * Side effects : 
  1283.  *  If bus busy, arbitration failed, etc, NCR5380_select() will exit 
  1284.  * with registers as they should have been on entry - ie
  1285.  * SELECT_ENABLE will be set appropriately, the NCR5380
  1286.  * will cease to drive any SCSI bus signals.
  1287.  *
  1288.  * If successful : I_T_L or I_T_L_Q nexus will be established, 
  1289.  * instance->connected will be set to cmd.  
  1290.  *  SELECT interrupt will be disabled.
  1291.  *
  1292.  * If failed (no target) : cmd->scsi_done() will be called, and the 
  1293.  * cmd->result host byte set to DID_BAD_TARGET.
  1294.  */
  1295. static int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd, int tag)
  1296. {
  1297.     SETUP_HOSTDATA(instance);
  1298.     unsigned char tmp[3], phase;
  1299.     unsigned char *data;
  1300.     int len;
  1301.     unsigned long timeout;
  1302.     unsigned long flags;
  1303.     hostdata->restart_select = 0;
  1304.     NCR_PRINT(NDEBUG_ARBITRATION);
  1305.     ARB_PRINTK("scsi%d: starting arbitration, id = %dn", HOSTNO,
  1306.        instance->this_id);
  1307.     /* 
  1308.      * Set the phase bits to 0, otherwise the NCR5380 won't drive the 
  1309.      * data bus during SELECTION.
  1310.      */
  1311.     save_flags(flags);
  1312.     cli();
  1313.     if (hostdata->connected) {
  1314. restore_flags(flags);
  1315. return -1;
  1316.     }
  1317.     NCR5380_write(TARGET_COMMAND_REG, 0);
  1318.     /* 
  1319.      * Start arbitration.
  1320.      */
  1321.     
  1322.     NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
  1323.     NCR5380_write(MODE_REG, MR_ARBITRATE);
  1324.     restore_flags(flags);
  1325.     /* Wait for arbitration logic to complete */
  1326. #if NCR_TIMEOUT
  1327.     {
  1328.       unsigned long timeout = jiffies + 2*NCR_TIMEOUT;
  1329.       while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
  1330.    && time_before(jiffies, timeout) && !hostdata->connected)
  1331. ;
  1332.       if (time_after_eq(jiffies, timeout))
  1333.       {
  1334. printk("scsi : arbitration timeout at %dn", __LINE__);
  1335. NCR5380_write(MODE_REG, MR_BASE);
  1336. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1337. return -1;
  1338.       }
  1339.     }
  1340. #else /* NCR_TIMEOUT */
  1341.     while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
  1342.  && !hostdata->connected);
  1343. #endif
  1344.     ARB_PRINTK("scsi%d: arbitration completen", HOSTNO);
  1345.     if (hostdata->connected) {
  1346. NCR5380_write(MODE_REG, MR_BASE); 
  1347. return -1;
  1348.     }
  1349.     /* 
  1350.      * The arbitration delay is 2.2us, but this is a minimum and there is 
  1351.      * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
  1352.      * the integral nature of udelay().
  1353.      *
  1354.      */
  1355.     udelay(3);
  1356.     /* Check for lost arbitration */
  1357.     if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
  1358. (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
  1359. (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
  1360. hostdata->connected) {
  1361. NCR5380_write(MODE_REG, MR_BASE); 
  1362. ARB_PRINTK("scsi%d: lost arbitration, deasserting MR_ARBITRATEn",
  1363.    HOSTNO);
  1364. return -1;
  1365.     }
  1366.      /* after/during arbitration, BSY should be asserted.
  1367. IBM DPES-31080 Version S31Q works now */
  1368.      /* Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) */
  1369.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL |
  1370.  ICR_ASSERT_BSY ) ;
  1371.     
  1372.     if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
  1373. hostdata->connected) {
  1374. NCR5380_write(MODE_REG, MR_BASE);
  1375. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1376. ARB_PRINTK("scsi%d: lost arbitration, deasserting ICR_ASSERT_SELn",
  1377.    HOSTNO);
  1378. return -1;
  1379.     }
  1380.     /* 
  1381.      * Again, bus clear + bus settle time is 1.2us, however, this is 
  1382.      * a minimum so we'll udelay ceil(1.2)
  1383.      */
  1384. #ifdef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
  1385.     /* ++roman: But some targets (see above :-) seem to need a bit more... */
  1386.     udelay(15);
  1387. #else
  1388.     udelay(2);
  1389. #endif
  1390.     
  1391.     if (hostdata->connected) {
  1392. NCR5380_write(MODE_REG, MR_BASE);
  1393. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1394. return -1;
  1395.     }
  1396.     ARB_PRINTK("scsi%d: won arbitrationn", HOSTNO);
  1397.     /* 
  1398.      * Now that we have won arbitration, start Selection process, asserting 
  1399.      * the host and target ID's on the SCSI bus.
  1400.      */
  1401.     NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->target)));
  1402.     /* 
  1403.      * Raise ATN while SEL is true before BSY goes false from arbitration,
  1404.      * since this is the only way to guarantee that we'll get a MESSAGE OUT
  1405.      * phase immediately after selection.
  1406.      */
  1407.     NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | 
  1408. ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
  1409.     NCR5380_write(MODE_REG, MR_BASE);
  1410.     /* 
  1411.      * Reselect interrupts must be turned off prior to the dropping of BSY,
  1412.      * otherwise we will trigger an interrupt.
  1413.      */
  1414.     if (hostdata->connected) {
  1415. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1416. return -1;
  1417.     }
  1418.     NCR5380_write(SELECT_ENABLE_REG, 0);
  1419.     /*
  1420.      * The initiator shall then wait at least two deskew delays and release 
  1421.      * the BSY signal.
  1422.      */
  1423.     udelay(1);        /* wingel -- wait two bus deskew delay >2*45ns */
  1424.     /* Reset BSY */
  1425.     NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | 
  1426. ICR_ASSERT_ATN | ICR_ASSERT_SEL));
  1427.     /* 
  1428.      * Something weird happens when we cease to drive BSY - looks
  1429.      * like the board/chip is letting us do another read before the 
  1430.      * appropriate propagation delay has expired, and we're confusing
  1431.      * a BSY signal from ourselves as the target's response to SELECTION.
  1432.      *
  1433.      * A small delay (the 'C++' frontend breaks the pipeline with an
  1434.      * unnecessary jump, making it work on my 386-33/Trantor T128, the
  1435.      * tighter 'C' code breaks and requires this) solves the problem - 
  1436.      * the 1 us delay is arbitrary, and only used because this delay will 
  1437.      * be the same on other platforms and since it works here, it should 
  1438.      * work there.
  1439.      *
  1440.      * wingel suggests that this could be due to failing to wait
  1441.      * one deskew delay.
  1442.      */
  1443.     udelay(1);
  1444.     SEL_PRINTK("scsi%d: selecting target %dn", HOSTNO, cmd->target);
  1445.     /* 
  1446.      * The SCSI specification calls for a 250 ms timeout for the actual 
  1447.      * selection.
  1448.      */
  1449.     timeout = jiffies + 25; 
  1450.     /* 
  1451.      * XXX very interesting - we're seeing a bounce where the BSY we 
  1452.      * asserted is being reflected / still asserted (propagation delay?)
  1453.      * and it's detecting as true.  Sigh.
  1454.      */
  1455. #if 0
  1456.     /* ++roman: If a target conformed to the SCSI standard, it wouldn't assert
  1457.      * IO while SEL is true. But again, there are some disks out the in the
  1458.      * world that do that nevertheless. (Somebody claimed that this announces
  1459.      * reselection capability of the target.) So we better skip that test and
  1460.      * only wait for BSY... (Famous german words: Der Kl黦ere gibt nach :-)
  1461.      */
  1462.     while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) & 
  1463. (SR_BSY | SR_IO)));
  1464.     if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == 
  1465.     (SR_SEL | SR_IO)) {
  1466.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1467.     NCR5380_reselect(instance);
  1468.     printk (KERN_ERR "scsi%d: reselection after won arbitration?n",
  1469.     HOSTNO);
  1470.     NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1471.     return -1;
  1472.     }
  1473. #else
  1474.     while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) & SR_BSY));
  1475. #endif
  1476.     /* 
  1477.      * No less than two deskew delays after the initiator detects the 
  1478.      * BSY signal is true, it shall release the SEL signal and may 
  1479.      * change the DATA BUS.                                     -wingel
  1480.      */
  1481.     udelay(1);
  1482.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1483.     if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
  1484. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1485. if (hostdata->targets_present & (1 << cmd->target)) {
  1486.     printk(KERN_ERR "scsi%d: weirdnessn", HOSTNO);
  1487.     if (hostdata->restart_select)
  1488. printk(KERN_NOTICE "trestart selectn");
  1489.     NCR_PRINT(NDEBUG_ANY);
  1490.     NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1491.     return -1;
  1492. }
  1493. cmd->result = DID_BAD_TARGET << 16;
  1494. #ifdef NCR5380_STATS
  1495. collect_stats(hostdata, cmd);
  1496. #endif
  1497. #ifdef SUPPORT_TAGS
  1498. cmd_free_tag( cmd );
  1499. #endif
  1500. cmd->scsi_done(cmd);
  1501. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1502. SEL_PRINTK("scsi%d: target did not respond within 250msn", HOSTNO);
  1503. NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1504. return 0;
  1505.     } 
  1506.     hostdata->targets_present |= (1 << cmd->target);
  1507.     /*
  1508.      * Since we followed the SCSI spec, and raised ATN while SEL 
  1509.      * was true but before BSY was false during selection, the information
  1510.      * transfer phase should be a MESSAGE OUT phase so that we can send the
  1511.      * IDENTIFY message.
  1512.      * 
  1513.      * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
  1514.      * message (2 bytes) with a tag ID that we increment with every command
  1515.      * until it wraps back to 0.
  1516.      *
  1517.      * XXX - it turns out that there are some broken SCSI-II devices,
  1518.      *      which claim to support tagged queuing but fail when more than
  1519.      *      some number of commands are issued at once.
  1520.      */
  1521.     /* Wait for start of REQ/ACK handshake */
  1522.     while (!(NCR5380_read(STATUS_REG) & SR_REQ));
  1523.     SEL_PRINTK("scsi%d: target %d selected, going into MESSAGE OUT phase.n",
  1524.        HOSTNO, cmd->target);
  1525.     tmp[0] = IDENTIFY(1, cmd->lun);
  1526. #ifdef SUPPORT_TAGS
  1527.     if (cmd->tag != TAG_NONE) {
  1528. tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
  1529. tmp[2] = cmd->tag;
  1530. len = 3;
  1531.     } else 
  1532. len = 1;
  1533. #else
  1534.     len = 1;
  1535.     cmd->tag=0;
  1536. #endif /* SUPPORT_TAGS */
  1537.     /* Send message(s) */
  1538.     data = tmp;
  1539.     phase = PHASE_MSGOUT;
  1540.     NCR5380_transfer_pio(instance, &phase, &len, &data);
  1541.     SEL_PRINTK("scsi%d: nexus established.n", HOSTNO);
  1542.     /* XXX need to handle errors here */
  1543.     hostdata->connected = cmd;
  1544. #ifndef SUPPORT_TAGS
  1545.     hostdata->busy[cmd->target] |= (1 << cmd->lun);
  1546. #endif    
  1547.     initialize_SCp(cmd);
  1548.     return 0;
  1549. }
  1550. /* 
  1551.  * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance, 
  1552.  *      unsigned char *phase, int *count, unsigned char **data)
  1553.  *
  1554.  * Purpose : transfers data in given phase using polled I/O
  1555.  *
  1556.  * Inputs : instance - instance of driver, *phase - pointer to 
  1557.  * what phase is expected, *count - pointer to number of 
  1558.  * bytes to transfer, **data - pointer to data pointer.
  1559.  * 
  1560.  * Returns : -1 when different phase is entered without transferring
  1561.  * maximum number of bytes, 0 if all bytes are transfered or exit
  1562.  * is in same phase.
  1563.  *
  1564.  *  Also, *phase, *count, *data are modified in place.
  1565.  *
  1566.  * XXX Note : handling for bus free may be useful.
  1567.  */
  1568. /*
  1569.  * Note : this code is not as quick as it could be, however it 
  1570.  * IS 100% reliable, and for the actual data transfer where speed
  1571.  * counts, we will always do a pseudo DMA or DMA transfer.
  1572.  */
  1573. static int NCR5380_transfer_pio( struct Scsi_Host *instance, 
  1574.  unsigned char *phase, int *count,
  1575.  unsigned char **data)
  1576. {
  1577.     register unsigned char p = *phase, tmp;
  1578.     register int c = *count;
  1579.     register unsigned char *d = *data;
  1580.     /* 
  1581.      * The NCR5380 chip will only drive the SCSI bus when the 
  1582.      * phase specified in the appropriate bits of the TARGET COMMAND
  1583.      * REGISTER match the STATUS REGISTER
  1584.      */
  1585.     NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
  1586.     do {
  1587. /* 
  1588.  * Wait for assertion of REQ, after which the phase bits will be 
  1589.  * valid 
  1590.  */
  1591. while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ));
  1592. HSH_PRINTK("scsi%d: REQ detectedn", HOSTNO);
  1593. /* Check for phase mismatch */
  1594. if ((tmp & PHASE_MASK) != p) {
  1595.     PIO_PRINTK("scsi%d: phase mismatchn", HOSTNO);
  1596.     NCR_PRINT_PHASE(NDEBUG_PIO);
  1597.     break;
  1598. }
  1599. /* Do actual transfer from SCSI bus to / from memory */
  1600. if (!(p & SR_IO)) 
  1601.     NCR5380_write(OUTPUT_DATA_REG, *d);
  1602. else 
  1603.     *d = NCR5380_read(CURRENT_SCSI_DATA_REG);
  1604. ++d;
  1605. /* 
  1606.  * The SCSI standard suggests that in MSGOUT phase, the initiator
  1607.  * should drop ATN on the last byte of the message phase
  1608.  * after REQ has been asserted for the handshake but before
  1609.  * the initiator raises ACK.
  1610.  */
  1611. if (!(p & SR_IO)) {
  1612.     if (!((p & SR_MSG) && c > 1)) {
  1613. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
  1614.     ICR_ASSERT_DATA);
  1615. NCR_PRINT(NDEBUG_PIO);
  1616. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
  1617. ICR_ASSERT_DATA | ICR_ASSERT_ACK);
  1618.     } else {
  1619. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
  1620.     ICR_ASSERT_DATA | ICR_ASSERT_ATN);
  1621. NCR_PRINT(NDEBUG_PIO);
  1622. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
  1623.     ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
  1624.     }
  1625. } else {
  1626.     NCR_PRINT(NDEBUG_PIO);
  1627.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
  1628. }
  1629. while (NCR5380_read(STATUS_REG) & SR_REQ);
  1630. HSH_PRINTK("scsi%d: req false, handshake completen", HOSTNO);
  1631. /*
  1632.  * We have several special cases to consider during REQ/ACK handshaking : 
  1633.  * 1.  We were in MSGOUT phase, and we are on the last byte of the 
  1634.  * message.  ATN must be dropped as ACK is dropped.
  1635.  *
  1636.  * 2.  We are in a MSGIN phase, and we are on the last byte of the  
  1637.  * message.  We must exit with ACK asserted, so that the calling
  1638.  * code may raise ATN before dropping ACK to reject the message.
  1639.  *
  1640.  * 3.  ACK and ATN are clear and the target may proceed as normal.
  1641.  */
  1642. if (!(p == PHASE_MSGIN && c == 1)) {  
  1643.     if (p == PHASE_MSGOUT && c > 1)
  1644. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1645.     else
  1646. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1647.     } while (--c);
  1648.     PIO_PRINTK("scsi%d: residual %dn", HOSTNO, c);
  1649.     *count = c;
  1650.     *data = d;
  1651.     tmp = NCR5380_read(STATUS_REG);
  1652.     /* The phase read from the bus is valid if either REQ is (already)
  1653.      * asserted or if ACK hasn't been released yet. The latter is the case if
  1654.      * we're in MSGIN and all wanted bytes have been received. */
  1655.     if ((tmp & SR_REQ) || (p == PHASE_MSGIN && c == 0))
  1656. *phase = tmp & PHASE_MASK;
  1657.     else 
  1658. *phase = PHASE_UNKNOWN;
  1659.     if (!c || (*phase == p))
  1660. return 0;
  1661.     else 
  1662. return -1;
  1663. }
  1664. /*
  1665.  * Function : do_abort (Scsi_Host *host)
  1666.  * 
  1667.  * Purpose : abort the currently established nexus.  Should only be 
  1668.  *  called from a routine which can drop into a 
  1669.  * 
  1670.  * Returns : 0 on success, -1 on failure.
  1671.  */
  1672. static int do_abort (struct Scsi_Host *host) 
  1673. {
  1674.     unsigned char tmp, *msgptr, phase;
  1675.     int len;
  1676.     /* Request message out phase */
  1677.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1678.     /* 
  1679.      * Wait for the target to indicate a valid phase by asserting 
  1680.      * REQ.  Once this happens, we'll have either a MSGOUT phase 
  1681.      * and can immediately send the ABORT message, or we'll have some 
  1682.      * other phase and will have to source/sink data.
  1683.      * 
  1684.      * We really don't care what value was on the bus or what value
  1685.      * the target sees, so we just handshake.
  1686.      */
  1687.     
  1688.     while (!(tmp = NCR5380_read(STATUS_REG)) & SR_REQ);
  1689.     NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
  1690.     if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
  1691. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | 
  1692.       ICR_ASSERT_ACK);
  1693. while (NCR5380_read(STATUS_REG) & SR_REQ);
  1694. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
  1695.     }
  1696.    
  1697.     tmp = ABORT;
  1698.     msgptr = &tmp;
  1699.     len = 1;
  1700.     phase = PHASE_MSGOUT;
  1701.     NCR5380_transfer_pio (host, &phase, &len, &msgptr);
  1702.     /*
  1703.      * If we got here, and the command completed successfully,
  1704.      * we're about to go into bus free state.
  1705.      */
  1706.     return len ? -1 : 0;
  1707. }
  1708. #if defined(REAL_DMA)
  1709. /* 
  1710.  * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance, 
  1711.  *      unsigned char *phase, int *count, unsigned char **data)
  1712.  *
  1713.  * Purpose : transfers data in given phase using either real
  1714.  * or pseudo DMA.
  1715.  *
  1716.  * Inputs : instance - instance of driver, *phase - pointer to 
  1717.  * what phase is expected, *count - pointer to number of 
  1718.  * bytes to transfer, **data - pointer to data pointer.
  1719.  * 
  1720.  * Returns : -1 when different phase is entered without transferring
  1721.  * maximum number of bytes, 0 if all bytes or transfered or exit
  1722.  * is in same phase.
  1723.  *
  1724.  *  Also, *phase, *count, *data are modified in place.
  1725.  *
  1726.  */
  1727. static int NCR5380_transfer_dma( struct Scsi_Host *instance, 
  1728.  unsigned char *phase, int *count,
  1729.  unsigned char **data)
  1730. {
  1731.     SETUP_HOSTDATA(instance);
  1732.     register int c = *count;
  1733.     register unsigned char p = *phase;
  1734.     register unsigned char *d = *data;
  1735.     unsigned char tmp;
  1736.     unsigned long flags;
  1737.     if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
  1738.         *phase = tmp;
  1739.         return -1;
  1740.     }
  1741.     if (atari_read_overruns && (p & SR_IO)) {
  1742. c -= atari_read_overruns;
  1743.     }
  1744.     DMA_PRINTK("scsi%d: initializing DMA for %s, %d bytes %s %pn",
  1745.        HOSTNO, (p & SR_IO) ? "reading" : "writing",
  1746.        c, (p & SR_IO) ? "to" : "from", d);
  1747.     NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));
  1748. #ifdef REAL_DMA
  1749.     NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
  1750. #endif /* def REAL_DMA  */
  1751.     if (IS_A_TT()) {
  1752. /* On the Medusa, it is a must to initialize the DMA before
  1753.  * starting the NCR. This is also the cleaner way for the TT.
  1754.  */
  1755. save_flags(flags);
  1756. cli();
  1757. hostdata->dma_len = (p & SR_IO) ?
  1758.     NCR5380_dma_read_setup(instance, d, c) : 
  1759.     NCR5380_dma_write_setup(instance, d, c);
  1760. restore_flags(flags);
  1761.     }
  1762.     
  1763.     if (p & SR_IO)
  1764. NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
  1765.     else {
  1766. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
  1767. NCR5380_write(START_DMA_SEND_REG, 0);
  1768.     }
  1769.     if (!IS_A_TT()) {
  1770. /* On the Falcon, the DMA setup must be done after the last */
  1771. /* NCR access, else the DMA setup gets trashed!
  1772.  */
  1773. save_flags(flags);
  1774. cli();
  1775. hostdata->dma_len = (p & SR_IO) ?
  1776.     NCR5380_dma_read_setup(instance, d, c) : 
  1777.     NCR5380_dma_write_setup(instance, d, c);
  1778. restore_flags(flags);
  1779.     }
  1780.     return 0;
  1781. }
  1782. #endif /* defined(REAL_DMA) */
  1783. /*
  1784.  * Function : NCR5380_information_transfer (struct Scsi_Host *instance)
  1785.  *
  1786.  * Purpose : run through the various SCSI phases and do as the target 
  1787.  *  directs us to.  Operates on the currently connected command, 
  1788.  * instance->connected.
  1789.  *
  1790.  * Inputs : instance, instance for which we are doing commands
  1791.  *
  1792.  * Side effects : SCSI things happen, the disconnected queue will be 
  1793.  * modified if a command disconnects, *instance->connected will
  1794.  * change.
  1795.  *
  1796.  * XXX Note : we need to watch for bus free or a reset condition here 
  1797.  *  to recover from an unexpected bus free condition.
  1798.  */
  1799.  
  1800. static void NCR5380_information_transfer (struct Scsi_Host *instance)
  1801. {
  1802.     SETUP_HOSTDATA(instance);
  1803.     unsigned long flags;
  1804.     unsigned char msgout = NOP;
  1805.     int sink = 0;
  1806.     int len;
  1807. #if defined(REAL_DMA)
  1808.     int transfersize;
  1809. #endif
  1810.     unsigned char *data;
  1811.     unsigned char phase, tmp, extended_msg[10], old_phase=0xff;
  1812.     Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected;
  1813.     while (1) {
  1814. tmp = NCR5380_read(STATUS_REG);
  1815. /* We only have a valid SCSI phase when REQ is asserted */
  1816. if (tmp & SR_REQ) {
  1817.     phase = (tmp & PHASE_MASK); 
  1818.     if (phase != old_phase) {
  1819. old_phase = phase;
  1820. NCR_PRINT_PHASE(NDEBUG_INFORMATION);
  1821.     }
  1822.     
  1823.     if (sink && (phase != PHASE_MSGOUT)) {
  1824. NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));
  1825. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | 
  1826.     ICR_ASSERT_ACK);
  1827. while (NCR5380_read(STATUS_REG) & SR_REQ);
  1828. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
  1829.     ICR_ASSERT_ATN);
  1830. sink = 0;
  1831. continue;
  1832.     }
  1833.     switch (phase) {
  1834.     case PHASE_DATAOUT:
  1835. #if (NDEBUG & NDEBUG_NO_DATAOUT)
  1836. printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT "
  1837.        "abortedn", HOSTNO);
  1838. sink = 1;
  1839. do_abort(instance);
  1840. cmd->result = DID_ERROR  << 16;
  1841. cmd->done(cmd);
  1842. return;
  1843. #endif
  1844.     case PHASE_DATAIN:
  1845. /* 
  1846.  * If there is no room left in the current buffer in the
  1847.  * scatter-gather list, move onto the next one.
  1848.  */
  1849. if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
  1850.     ++cmd->SCp.buffer;
  1851.     --cmd->SCp.buffers_residual;
  1852.     cmd->SCp.this_residual = cmd->SCp.buffer->length;
  1853.     cmd->SCp.ptr = cmd->SCp.buffer->address;
  1854.     /* ++roman: Try to merge some scatter-buffers if
  1855.      * they are at contiguous physical addresses.
  1856.      */
  1857.     merge_contiguous_buffers( cmd );
  1858.     INF_PRINTK("scsi%d: %d bytes and %d buffers leftn",
  1859.        HOSTNO, cmd->SCp.this_residual,
  1860.        cmd->SCp.buffers_residual);
  1861. }
  1862. /*
  1863.  * The preferred transfer method is going to be 
  1864.  * PSEUDO-DMA for systems that are strictly PIO,
  1865.  * since we can let the hardware do the handshaking.
  1866.  *
  1867.  * For this to work, we need to know the transfersize
  1868.  * ahead of time, since the pseudo-DMA code will sit
  1869.  * in an unconditional loop.
  1870.  */
  1871. /* ++roman: I suggest, this should be
  1872.  *   #if def(REAL_DMA)
  1873.  * instead of leaving REAL_DMA out.
  1874.  */
  1875. #if defined(REAL_DMA)
  1876. if (!cmd->device->borken &&
  1877.     (transfersize = NCR5380_dma_xfer_len(instance,cmd,phase)) > 31) {
  1878.     len = transfersize;
  1879.     cmd->SCp.phase = phase;
  1880.     if (NCR5380_transfer_dma(instance, &phase,
  1881. &len, (unsigned char **) &cmd->SCp.ptr)) {
  1882. /*
  1883.  * If the watchdog timer fires, all future
  1884.  * accesses to this device will use the
  1885.  * polled-IO. */ 
  1886. printk(KERN_NOTICE "scsi%d: switching target %d "
  1887.        "lun %d to slow handshaken", HOSTNO,
  1888.        cmd->target, cmd->lun);
  1889. cmd->device->borken = 1;
  1890. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
  1891.     ICR_ASSERT_ATN);
  1892. sink = 1;
  1893. do_abort(instance);
  1894. cmd->result = DID_ERROR  << 16;
  1895. cmd->done(cmd);
  1896. /* XXX - need to source or sink data here, as appropriate */
  1897.     } else {
  1898. #ifdef REAL_DMA
  1899. /* ++roman: When using real DMA,
  1900.  * information_transfer() should return after
  1901.  * starting DMA since it has nothing more to
  1902.  * do.
  1903.  */
  1904. return;
  1905. #else
  1906. cmd->SCp.this_residual -= transfersize - len;
  1907. #endif
  1908.     }
  1909. } else
  1910. #endif /* defined(REAL_DMA) */
  1911.   NCR5380_transfer_pio(instance, &phase, 
  1912.     (int *) &cmd->SCp.this_residual, (unsigned char **)
  1913.     &cmd->SCp.ptr);
  1914. break;
  1915.     case PHASE_MSGIN:
  1916. len = 1;
  1917. data = &tmp;
  1918. NCR5380_write(SELECT_ENABLE_REG, 0);  /* disable reselects */
  1919. NCR5380_transfer_pio(instance, &phase, &len, &data);
  1920. cmd->SCp.Message = tmp;
  1921. switch (tmp) {
  1922. /*
  1923.  * Linking lets us reduce the time required to get the 
  1924.  * next command out to the device, hopefully this will
  1925.  * mean we don't waste another revolution due to the delays
  1926.  * required by ARBITRATION and another SELECTION.
  1927.  *
  1928.  * In the current implementation proposal, low level drivers
  1929.  * merely have to start the next command, pointed to by 
  1930.  * next_link, done() is called as with unlinked commands.
  1931.  */
  1932. #ifdef LINKED
  1933. case LINKED_CMD_COMPLETE:
  1934. case LINKED_FLG_CMD_COMPLETE:
  1935.     /* Accept message by clearing ACK */
  1936.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1937.     
  1938.     LNK_PRINTK("scsi%d: target %d lun %d linked command "
  1939.        "complete.n", HOSTNO, cmd->target, cmd->lun);
  1940.     /* Enable reselect interrupts */
  1941.     NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  1942.     /*
  1943.      * Sanity check : A linked command should only terminate
  1944.      * with one of these messages if there are more linked
  1945.      * commands available.
  1946.      */
  1947.     if (!cmd->next_link) {
  1948.  printk(KERN_NOTICE "scsi%d: target %d lun %d "
  1949. "linked command complete, no next_linkn",
  1950. HOSTNO, cmd->target, cmd->lun);
  1951.     sink = 1;
  1952.     do_abort (instance);
  1953.     return;
  1954.     }
  1955.     initialize_SCp(cmd->next_link);
  1956.     /* The next command is still part of this process; copy it
  1957.      * and don't free it! */
  1958.     cmd->next_link->tag = cmd->tag;
  1959.     cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8); 
  1960.     LNK_PRINTK("scsi%d: target %d lun %d linked request "
  1961.        "done, calling scsi_done().n",
  1962.        HOSTNO, cmd->target, cmd->lun);
  1963. #ifdef NCR5380_STATS
  1964.     collect_stats(hostdata, cmd);
  1965. #endif
  1966.     cmd->scsi_done(cmd);
  1967.     cmd = hostdata->connected;
  1968.     break;
  1969. #endif /* def LINKED */
  1970. case ABORT:
  1971. case COMMAND_COMPLETE: 
  1972.     /* Accept message by clearing ACK */
  1973.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  1974.     /* ++guenther: possible race with Falcon locking */
  1975.     falcon_dont_release++;
  1976.     hostdata->connected = NULL;
  1977.     QU_PRINTK("scsi%d: command for target %d, lun %d "
  1978.       "completedn", HOSTNO, cmd->target, cmd->lun);
  1979. #ifdef SUPPORT_TAGS
  1980.     cmd_free_tag( cmd );
  1981.     if (status_byte(cmd->SCp.Status) == QUEUE_FULL) {
  1982. /* Turn a QUEUE FULL status into BUSY, I think the
  1983.  * mid level cannot handle QUEUE FULL :-( (The
  1984.  * command is retried after BUSY). Also update our
  1985.  * queue size to the number of currently issued
  1986.  * commands now.
  1987.  */
  1988. /* ++Andreas: the mid level code knows about
  1989.    QUEUE_FULL now. */
  1990. TAG_ALLOC *ta = &TagAlloc[cmd->target][cmd->lun];
  1991. TAG_PRINTK("scsi%d: target %d lun %d returned "
  1992.    "QUEUE_FULL after %d commandsn",
  1993.    HOSTNO, cmd->target, cmd->lun,
  1994.    ta->nr_allocated);
  1995. if (ta->queue_size > ta->nr_allocated)
  1996.     ta->nr_allocated = ta->queue_size;
  1997.     }
  1998. #else
  1999.     hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
  2000. #endif
  2001.     /* Enable reselect interrupts */
  2002.     NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2003.     /* 
  2004.      * I'm not sure what the correct thing to do here is : 
  2005.      * 
  2006.      * If the command that just executed is NOT a request 
  2007.      * sense, the obvious thing to do is to set the result
  2008.      * code to the values of the stored parameters.
  2009.      * 
  2010.      * If it was a REQUEST SENSE command, we need some way to
  2011.      * differentiate between the failure code of the original
  2012.      * and the failure code of the REQUEST sense - the obvious
  2013.      * case is success, where we fall through and leave the
  2014.      * result code unchanged.
  2015.      * 
  2016.      * The non-obvious place is where the REQUEST SENSE failed
  2017.      */
  2018.     if (cmd->cmnd[0] != REQUEST_SENSE) 
  2019. cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8); 
  2020.     else if (status_byte(cmd->SCp.Status) != GOOD)
  2021. cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
  2022.     
  2023. #ifdef AUTOSENSE
  2024.     if ((cmd->cmnd[0] != REQUEST_SENSE) && 
  2025. (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) {
  2026. ASEN_PRINTK("scsi%d: performing request sensen",
  2027.     HOSTNO);
  2028. cmd->cmnd[0] = REQUEST_SENSE;
  2029. cmd->cmnd[1] &= 0xe0;
  2030. cmd->cmnd[2] = 0;
  2031. cmd->cmnd[3] = 0;
  2032. cmd->cmnd[4] = sizeof(cmd->sense_buffer);
  2033. cmd->cmnd[5] = 0;
  2034. cmd->cmd_len = COMMAND_SIZE(cmd->cmnd[0]);
  2035. cmd->use_sg = 0;
  2036. /* this is initialized from initialize_SCp 
  2037. cmd->SCp.buffer = NULL;
  2038. cmd->SCp.buffers_residual = 0;
  2039. */
  2040. cmd->request_buffer = (char *) cmd->sense_buffer;
  2041. cmd->request_bufflen = sizeof(cmd->sense_buffer);
  2042. save_flags(flags);
  2043. cli();
  2044. LIST(cmd,hostdata->issue_queue);
  2045. NEXT(cmd) = hostdata->issue_queue;
  2046.         hostdata->issue_queue = (Scsi_Cmnd *) cmd;
  2047.         restore_flags(flags);
  2048. QU_PRINTK("scsi%d: REQUEST SENSE added to head of "
  2049.   "issue queuen", H_NO(cmd));
  2050.    } else
  2051. #endif /* def AUTOSENSE */
  2052.    {
  2053. #ifdef NCR5380_STATS
  2054.        collect_stats(hostdata, cmd);
  2055. #endif
  2056.        cmd->scsi_done(cmd);
  2057.     }
  2058.     NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2059.     /* 
  2060.      * Restore phase bits to 0 so an interrupted selection, 
  2061.      * arbitration can resume.
  2062.      */
  2063.     NCR5380_write(TARGET_COMMAND_REG, 0);
  2064.     
  2065.     while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
  2066. barrier();
  2067.     falcon_dont_release--;
  2068.     /* ++roman: For Falcon SCSI, release the lock on the
  2069.      * ST-DMA here if no other commands are waiting on the
  2070.      * disconnected queue.
  2071.      */
  2072.     falcon_release_lock_if_possible( hostdata );
  2073.     return;
  2074. case MESSAGE_REJECT:
  2075.     /* Accept message by clearing ACK */
  2076.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2077.     /* Enable reselect interrupts */
  2078.     NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2079.     switch (hostdata->last_message) {
  2080.     case HEAD_OF_QUEUE_TAG:
  2081.     case ORDERED_QUEUE_TAG:
  2082.     case SIMPLE_QUEUE_TAG:
  2083. /* The target obviously doesn't support tagged
  2084.  * queuing, even though it announced this ability in
  2085.  * its INQUIRY data ?!? (maybe only this LUN?) Ok,
  2086.  * clear 'tagged_supported' and lock the LUN, since
  2087.  * the command is treated as untagged further on.
  2088.  */
  2089. cmd->device->tagged_supported = 0;
  2090. hostdata->busy[cmd->target] |= (1 << cmd->lun);
  2091. cmd->tag = TAG_NONE;
  2092. TAG_PRINTK("scsi%d: target %d lun %d rejected "
  2093.    "QUEUE_TAG message; tagged queuing "
  2094.    "disabledn",
  2095.    HOSTNO, cmd->target, cmd->lun);
  2096. break;
  2097.     }
  2098.     break;
  2099. case DISCONNECT:
  2100.     /* Accept message by clearing ACK */
  2101.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2102.     save_flags(flags);
  2103.     cli();
  2104.     cmd->device->disconnect = 1;
  2105.     LIST(cmd,hostdata->disconnected_queue);
  2106.     NEXT(cmd) = hostdata->disconnected_queue;
  2107.     hostdata->connected = NULL;
  2108.     hostdata->disconnected_queue = cmd;
  2109.     restore_flags(flags);
  2110.     QU_PRINTK("scsi%d: command for target %d lun %d was "
  2111.       "moved from connected to the "
  2112.       "disconnected_queuen", HOSTNO, 
  2113.       cmd->target, cmd->lun);
  2114.     /* 
  2115.      * Restore phase bits to 0 so an interrupted selection, 
  2116.      * arbitration can resume.
  2117.      */
  2118.     NCR5380_write(TARGET_COMMAND_REG, 0);
  2119.     /* Enable reselect interrupts */
  2120.     NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2121.     /* Wait for bus free to avoid nasty timeouts */
  2122.     while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected)
  2123.      barrier();
  2124.     return;
  2125. /* 
  2126.  * The SCSI data pointer is *IMPLICITLY* saved on a disconnect
  2127.  * operation, in violation of the SCSI spec so we can safely 
  2128.  * ignore SAVE/RESTORE pointers calls.
  2129.  *
  2130.  * Unfortunately, some disks violate the SCSI spec and 
  2131.  * don't issue the required SAVE_POINTERS message before
  2132.  * disconnecting, and we have to break spec to remain 
  2133.  * compatible.
  2134.  */
  2135. case SAVE_POINTERS:
  2136. case RESTORE_POINTERS:
  2137.     /* Accept message by clearing ACK */
  2138.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2139.     /* Enable reselect interrupts */
  2140.     NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2141.     break;
  2142. case EXTENDED_MESSAGE:
  2143. /* 
  2144.  * Extended messages are sent in the following format :
  2145.  * Byte 
  2146.  * 0 EXTENDED_MESSAGE == 1
  2147.  * 1 length (includes one byte for code, doesn't 
  2148.  * include first two bytes)
  2149.  * 2  code
  2150.  * 3..length+1 arguments
  2151.  *
  2152.  * Start the extended message buffer with the EXTENDED_MESSAGE
  2153.  * byte, since print_msg() wants the whole thing.  
  2154.  */
  2155.     extended_msg[0] = EXTENDED_MESSAGE;
  2156.     /* Accept first byte by clearing ACK */
  2157.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2158.     EXT_PRINTK("scsi%d: receiving extended messagen", HOSTNO);
  2159.     len = 2;
  2160.     data = extended_msg + 1;
  2161.     phase = PHASE_MSGIN;
  2162.     NCR5380_transfer_pio(instance, &phase, &len, &data);
  2163.     EXT_PRINTK("scsi%d: length=%d, code=0x%02xn", HOSTNO,
  2164.        (int)extended_msg[1], (int)extended_msg[2]);
  2165.     if (!len && extended_msg[1] <= 
  2166. (sizeof (extended_msg) - 1)) {
  2167. /* Accept third byte by clearing ACK */
  2168. NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2169. len = extended_msg[1] - 1;
  2170. data = extended_msg + 3;
  2171. phase = PHASE_MSGIN;
  2172. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2173. EXT_PRINTK("scsi%d: message received, residual %dn",
  2174.    HOSTNO, len);
  2175. switch (extended_msg[2]) {
  2176. case EXTENDED_SDTR:
  2177. case EXTENDED_WDTR:
  2178. case EXTENDED_MODIFY_DATA_POINTER:
  2179. case EXTENDED_EXTENDED_IDENTIFY:
  2180.     tmp = 0;
  2181. }
  2182.     } else if (len) {
  2183. printk(KERN_NOTICE "scsi%d: error receiving "
  2184.        "extended messagen", HOSTNO);
  2185. tmp = 0;
  2186.     } else {
  2187. printk(KERN_NOTICE "scsi%d: extended message "
  2188.        "code %02x length %d is too longn",
  2189.        HOSTNO, extended_msg[2], extended_msg[1]);
  2190. tmp = 0;
  2191.     }
  2192. /* Fall through to reject message */
  2193. /* 
  2194.     * If we get something weird that we aren't expecting, 
  2195.    * reject it.
  2196.  */
  2197. default:
  2198.     if (!tmp) {
  2199. printk(KERN_DEBUG "scsi%d: rejecting message ", HOSTNO);
  2200. print_msg (extended_msg);
  2201. printk("n");
  2202.     } else if (tmp != EXTENDED_MESSAGE)
  2203. printk(KERN_DEBUG "scsi%d: rejecting unknown "
  2204.        "message %02x from target %d, lun %dn",
  2205.        HOSTNO, tmp, cmd->target, cmd->lun);
  2206.     else
  2207. printk(KERN_DEBUG "scsi%d: rejecting unknown "
  2208.        "extended message "
  2209.        "code %02x, length %d from target %d, lun %dn",
  2210.        HOSTNO, extended_msg[1], extended_msg[0],
  2211.        cmd->target, cmd->lun);
  2212.    
  2213.     msgout = MESSAGE_REJECT;
  2214.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
  2215. ICR_ASSERT_ATN);
  2216.     break;
  2217. } /* switch (tmp) */
  2218. break;
  2219.     case PHASE_MSGOUT:
  2220. len = 1;
  2221. data = &msgout;
  2222. hostdata->last_message = msgout;
  2223. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2224. if (msgout == ABORT) {
  2225. #ifdef SUPPORT_TAGS
  2226.     cmd_free_tag( cmd );
  2227. #else
  2228.     hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
  2229. #endif
  2230.     hostdata->connected = NULL;
  2231.     cmd->result = DID_ERROR << 16;
  2232. #ifdef NCR5380_STATS
  2233.     collect_stats(hostdata, cmd);
  2234. #endif
  2235.     cmd->scsi_done(cmd);
  2236.     NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
  2237.     falcon_release_lock_if_possible( hostdata );
  2238.     return;
  2239. }
  2240. msgout = NOP;
  2241. break;
  2242.     case PHASE_CMDOUT:
  2243. len = cmd->cmd_len;
  2244. data = cmd->cmnd;
  2245. /* 
  2246.  * XXX for performance reasons, on machines with a 
  2247.  * PSEUDO-DMA architecture we should probably 
  2248.  * use the dma transfer function.  
  2249.  */
  2250. NCR5380_transfer_pio(instance, &phase, &len, 
  2251.     &data);
  2252. break;
  2253.     case PHASE_STATIN:
  2254. len = 1;
  2255. data = &tmp;
  2256. NCR5380_transfer_pio(instance, &phase, &len, &data);
  2257. cmd->SCp.Status = tmp;
  2258. break;
  2259.     default:
  2260. printk("scsi%d: unknown phasen", HOSTNO);
  2261. NCR_PRINT(NDEBUG_ANY);
  2262.     } /* switch(phase) */
  2263. } /* if (tmp * SR_REQ) */ 
  2264.     } /* while (1) */
  2265. }
  2266. /*
  2267.  * Function : void NCR5380_reselect (struct Scsi_Host *instance)
  2268.  *
  2269.  * Purpose : does reselection, initializing the instance->connected 
  2270.  * field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q 
  2271.  * nexus has been reestablished,
  2272.  *
  2273.  * Inputs : instance - this instance of the NCR5380.
  2274.  *
  2275.  */
  2276. static void NCR5380_reselect (struct Scsi_Host *instance)
  2277. {
  2278.     SETUP_HOSTDATA(instance);
  2279.     unsigned char target_mask;
  2280.     unsigned char lun, phase;
  2281.     int len;
  2282. #ifdef SUPPORT_TAGS
  2283.     unsigned char tag;
  2284. #endif
  2285.     unsigned char msg[3];
  2286.     unsigned char *data;
  2287.     Scsi_Cmnd *tmp = NULL, *prev;
  2288. /*    unsigned long flags; */
  2289.     /*
  2290.      * Disable arbitration, etc. since the host adapter obviously
  2291.      * lost, and tell an interrupted NCR5380_select() to restart.
  2292.      */
  2293.     NCR5380_write(MODE_REG, MR_BASE);
  2294.     hostdata->restart_select = 1;
  2295.     target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask);
  2296.     RSL_PRINTK("scsi%d: reselectn", HOSTNO);
  2297.     /* 
  2298.      * At this point, we have detected that our SCSI ID is on the bus,
  2299.      * SEL is true and BSY was false for at least one bus settle delay
  2300.      * (400 ns).
  2301.      *
  2302.      * We must assert BSY ourselves, until the target drops the SEL
  2303.      * signal.
  2304.      */
  2305.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY);
  2306.     
  2307.     while (NCR5380_read(STATUS_REG) & SR_SEL);
  2308.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2309.     /*
  2310.      * Wait for target to go into MSGIN.
  2311.      */
  2312.     while (!(NCR5380_read(STATUS_REG) & SR_REQ));
  2313.     len = 1;
  2314.     data = msg;
  2315.     phase = PHASE_MSGIN;
  2316.     NCR5380_transfer_pio(instance, &phase, &len, &data);
  2317.     if (!msg[0] & 0x80) {
  2318. printk(KERN_DEBUG "scsi%d: expecting IDENTIFY message, got ", HOSTNO);
  2319. print_msg(msg);
  2320. do_abort(instance);
  2321. return;
  2322.     }
  2323.     lun = (msg[0] & 0x07);
  2324. #ifdef SUPPORT_TAGS
  2325.     /* If the phase is still MSGIN, the target wants to send some more
  2326.      * messages. In case it supports tagged queuing, this is probably a
  2327.      * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus.
  2328.      */
  2329.     tag = TAG_NONE;
  2330.     if (phase == PHASE_MSGIN && setup_use_tagged_queuing) {
  2331. /* Accept previous IDENTIFY message by clearing ACK */
  2332. NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
  2333. len = 2;
  2334. data = msg+1;
  2335. if (!NCR5380_transfer_pio(instance, &phase, &len, &data) &&
  2336.     msg[1] == SIMPLE_QUEUE_TAG)
  2337.     tag = msg[2];
  2338. TAG_PRINTK("scsi%d: target mask %02x, lun %d sent tag %d at "
  2339.    "reselectionn", HOSTNO, target_mask, lun, tag);
  2340.     }
  2341. #endif
  2342.     
  2343.     /* 
  2344.      * Find the command corresponding to the I_T_L or I_T_L_Q  nexus we 
  2345.      * just reestablished, and remove it from the disconnected queue.
  2346.      */
  2347.     for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL; 
  2348.  tmp; prev = tmp, tmp = NEXT(tmp) ) {
  2349. if ((target_mask == (1 << tmp->target)) && (lun == tmp->lun)
  2350. #ifdef SUPPORT_TAGS
  2351.     && (tag == tmp->tag) 
  2352. #endif
  2353.     ) {
  2354.     /* ++guenther: prevent race with falcon_release_lock */
  2355.     falcon_dont_release++;
  2356.     if (prev) {
  2357. REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
  2358. NEXT(prev) = NEXT(tmp);
  2359.     } else {
  2360. REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp));
  2361. hostdata->disconnected_queue = NEXT(tmp);
  2362.     }
  2363.     NEXT(tmp) = NULL;
  2364.     break;
  2365. }
  2366.     }
  2367.     
  2368.     if (!tmp) {
  2369. printk(KERN_WARNING "scsi%d: warning: target bitmask %02x lun %d "
  2370. #ifdef SUPPORT_TAGS
  2371. "tag %d "
  2372. #endif
  2373. "not in disconnected_queue.n",
  2374. HOSTNO, target_mask, lun
  2375. #ifdef SUPPORT_TAGS
  2376. , tag
  2377. #endif
  2378. );
  2379. /* 
  2380.  * Since we have an established nexus that we can't do anything
  2381.  * with, we must abort it.  
  2382.  */
  2383. do_abort(instance);
  2384. return;
  2385.     }
  2386.     /* Accept message by clearing ACK */
  2387.     NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
  2388.     hostdata->connected = tmp;
  2389.     RSL_PRINTK("scsi%d: nexus established, target = %d, lun = %d, tag = %dn",
  2390.        HOSTNO, tmp->target, tmp->lun, tmp->tag);
  2391.     falcon_dont_release--;
  2392. }
  2393. /*
  2394.  * Function : int NCR5380_abort (Scsi_Cmnd *cmd)
  2395.  *
  2396.  * Purpose : abort a command
  2397.  *
  2398.  * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the 
  2399.  *  host byte of the result field to, if zero DID_ABORTED is 
  2400.  * used.
  2401.  *
  2402.  * Returns : 0 - success, -1 on failure.
  2403.  *
  2404.  * XXX - there is no way to abort the command that is currently 
  2405.  *   connected, you have to wait for it to complete.  If this is 
  2406.  *  a problem, we could implement longjmp() / setjmp(), setjmp()
  2407.  *   called where the loop started in NCR5380_main().
  2408.  */
  2409. #ifndef NCR5380_abort
  2410. static
  2411. #endif
  2412. int NCR5380_abort (Scsi_Cmnd *cmd)
  2413. {
  2414.     struct Scsi_Host *instance = cmd->host;
  2415.     SETUP_HOSTDATA(instance);
  2416.     Scsi_Cmnd *tmp, **prev;
  2417.     unsigned long flags;
  2418.     printk(KERN_NOTICE "scsi%d: aborting commandn", HOSTNO);
  2419.     print_Scsi_Cmnd (cmd);
  2420.     NCR5380_print_status (instance);
  2421.     save_flags(flags);
  2422.     cli();
  2423.     
  2424.     if (!IS_A_TT() && !falcon_got_lock)
  2425. printk(KERN_ERR "scsi%d: !!BINGO!! Falcon has no lock in NCR5380_abortn",
  2426.        HOSTNO);
  2427.     ABRT_PRINTK("scsi%d: abort called basr 0x%02x, sr 0x%02xn", HOSTNO,
  2428. NCR5380_read(BUS_AND_STATUS_REG),
  2429. NCR5380_read(STATUS_REG));
  2430. #if 1
  2431. /* 
  2432.  * Case 1 : If the command is the currently executing command, 
  2433.  * we'll set the aborted flag and return control so that 
  2434.  * information transfer routine can exit cleanly.
  2435.  */
  2436.     if (hostdata->connected == cmd) {
  2437. ABRT_PRINTK("scsi%d: aborting connected commandn", HOSTNO);
  2438. /*
  2439.  * We should perform BSY checking, and make sure we haven't slipped
  2440.  * into BUS FREE.
  2441.  */
  2442. /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */
  2443. /* 
  2444.  * Since we can't change phases until we've completed the current 
  2445.  * handshake, we have to source or sink a byte of data if the current
  2446.  * phase is not MSGOUT.
  2447.  */
  2448. /* 
  2449.  * Return control to the executing NCR drive so we can clear the
  2450.  * aborted flag and get back into our main loop.
  2451.  */ 
  2452. if (do_abort(instance) == 0) {
  2453.   hostdata->aborted = 1;
  2454.   hostdata->connected = NULL;
  2455.   cmd->result = DID_ABORT << 16;
  2456. #ifdef SUPPORT_TAGS
  2457.   cmd_free_tag( cmd );
  2458. #else
  2459.   hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
  2460. #endif
  2461.   restore_flags(flags);
  2462.   cmd->scsi_done(cmd);
  2463.   falcon_release_lock_if_possible( hostdata );
  2464.   return SCSI_ABORT_SUCCESS;
  2465. } else {
  2466. /*   restore_flags(flags); */
  2467.   printk("scsi%d: abort of connected command failed!n", HOSTNO);
  2468.   return SCSI_ABORT_ERROR;
  2469.    }
  2470. #endif
  2471. /* 
  2472.  * Case 2 : If the command hasn't been issued yet, we simply remove it 
  2473.  *      from the issue queue.
  2474.  */
  2475.     for (prev = (Scsi_Cmnd **) &(hostdata->issue_queue), 
  2476. tmp = (Scsi_Cmnd *) hostdata->issue_queue;
  2477. tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp) )
  2478. if (cmd == tmp) {
  2479.     REMOVE(5, *prev, tmp, NEXT(tmp));
  2480.     (*prev) = NEXT(tmp);
  2481.     NEXT(tmp) = NULL;
  2482.     tmp->result = DID_ABORT << 16;
  2483.     restore_flags(flags);
  2484.     ABRT_PRINTK("scsi%d: abort removed command from issue queue.n",
  2485. HOSTNO);
  2486.     /* Tagged queuing note: no tag to free here, hasn't been assigned
  2487.      * yet... */
  2488.     tmp->scsi_done(tmp);
  2489.     falcon_release_lock_if_possible( hostdata );
  2490.     return SCSI_ABORT_SUCCESS;
  2491. }
  2492. /* 
  2493.  * Case 3 : If any commands are connected, we're going to fail the abort
  2494.  *     and let the high level SCSI driver retry at a later time or 
  2495.  *     issue a reset.
  2496.  *
  2497.  *     Timeouts, and therefore aborted commands, will be highly unlikely
  2498.  *          and handling them cleanly in this situation would make the common
  2499.  *     case of noresets less efficient, and would pollute our code.  So,
  2500.  *     we fail.
  2501.  */
  2502.     if (hostdata->connected) {
  2503. restore_flags(flags);
  2504. ABRT_PRINTK("scsi%d: abort failed, command connected.n", HOSTNO);
  2505.         return SCSI_ABORT_SNOOZE;
  2506.     }
  2507. /*
  2508.  * Case 4: If the command is currently disconnected from the bus, and 
  2509.  *  there are no connected commands, we reconnect the I_T_L or 
  2510.  * I_T_L_Q nexus associated with it, go into message out, and send 
  2511.  *      an abort message.
  2512.  *
  2513.  * This case is especially ugly. In order to reestablish the nexus, we
  2514.  * need to call NCR5380_select().  The easiest way to implement this 
  2515.  * function was to abort if the bus was busy, and let the interrupt
  2516.  * handler triggered on the SEL for reselect take care of lost arbitrations
  2517.  * where necessary, meaning interrupts need to be enabled.
  2518.  *
  2519.  * When interrupts are enabled, the queues may change - so we 
  2520.  * can't remove it from the disconnected queue before selecting it
  2521.  * because that could cause a failure in hashing the nexus if that 
  2522.  * device reselected.
  2523.  * 
  2524.  * Since the queues may change, we can't use the pointers from when we
  2525.  * first locate it.
  2526.  *
  2527.  * So, we must first locate the command, and if NCR5380_select()
  2528.  * succeeds, then issue the abort, relocate the command and remove
  2529.  * it from the disconnected queue.
  2530.  */
  2531.     for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp;
  2532.  tmp = NEXT(tmp)) 
  2533.         if (cmd == tmp) {
  2534.             restore_flags(flags);
  2535.     ABRT_PRINTK("scsi%d: aborting disconnected command.n", HOSTNO);
  2536.   
  2537.             if (NCR5380_select (instance, cmd, (int) cmd->tag)) 
  2538. return SCSI_ABORT_BUSY;
  2539.     ABRT_PRINTK("scsi%d: nexus reestablished.n", HOSTNO);
  2540.     do_abort (instance);
  2541.     save_flags(flags);
  2542.     cli();
  2543.     for (prev = (Scsi_Cmnd **) &(hostdata->disconnected_queue), 
  2544. tmp = (Scsi_Cmnd *) hostdata->disconnected_queue;
  2545. tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp) )
  2546.     if (cmd == tmp) {
  2547.     REMOVE(5, *prev, tmp, NEXT(tmp));
  2548.     *prev = NEXT(tmp);
  2549.     NEXT(tmp) = NULL;
  2550.     tmp->result = DID_ABORT << 16;
  2551.     /* We must unlock the tag/LUN immediately here, since the
  2552.      * target goes to BUS FREE and doesn't send us another
  2553.      * message (COMMAND_COMPLETE or the like)
  2554.      */
  2555. #ifdef SUPPORT_TAGS
  2556.     cmd_free_tag( tmp );
  2557. #else
  2558.     hostdata->busy[cmd->target] &= ~(1 << cmd->lun);
  2559. #endif
  2560.     restore_flags(flags);
  2561.     tmp->scsi_done(tmp);
  2562.     falcon_release_lock_if_possible( hostdata );
  2563.     return SCSI_ABORT_SUCCESS;
  2564. }
  2565. }
  2566. /*
  2567.  * Case 5 : If we reached this point, the command was not found in any of 
  2568.  *     the queues.
  2569.  *
  2570.  * We probably reached this point because of an unlikely race condition
  2571.  * between the command completing successfully and the abortion code,
  2572.  * so we won't panic, but we will notify the user in case something really
  2573.  * broke.
  2574.  */
  2575.     restore_flags(flags);
  2576.     printk(KERN_INFO "scsi%d: warning : SCSI command probably completed successfullyn"
  2577.            KERN_INFO "        before abortionn", HOSTNO); 
  2578. /* Maybe it is sufficient just to release the ST-DMA lock... (if
  2579.  * possible at all) At least, we should check if the lock could be
  2580.  * released after the abort, in case it is kept due to some bug.
  2581.  */
  2582.     falcon_release_lock_if_possible( hostdata );
  2583.     return SCSI_ABORT_NOT_RUNNING;
  2584. }
  2585. /* 
  2586.  * Function : int NCR5380_reset (Scsi_Cmnd *cmd, unsigned int reset_flags)
  2587.  * 
  2588.  * Purpose : reset the SCSI bus.
  2589.  *
  2590.  * Returns : SCSI_RESET_WAKEUP
  2591.  *
  2592.  */ 
  2593. static int NCR5380_reset( Scsi_Cmnd *cmd, unsigned int reset_flags)
  2594. {
  2595.     SETUP_HOSTDATA(cmd->host);
  2596.     int           i;
  2597.     unsigned long flags;
  2598. #if 1
  2599.     Scsi_Cmnd *connected, *disconnected_queue;
  2600. #endif
  2601.     if (!IS_A_TT() && !falcon_got_lock)
  2602. printk(KERN_ERR "scsi%d: !!BINGO!! Falcon has no lock in NCR5380_resetn",
  2603.        H_NO(cmd) );
  2604.     NCR5380_print_status (cmd->host);
  2605.     /* get in phase */
  2606.     NCR5380_write( TARGET_COMMAND_REG,
  2607.    PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
  2608.     /* assert RST */
  2609.     NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
  2610.     udelay (40);
  2611.     /* reset NCR registers */
  2612.     NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
  2613.     NCR5380_write( MODE_REG, MR_BASE );
  2614.     NCR5380_write( TARGET_COMMAND_REG, 0 );
  2615.     NCR5380_write( SELECT_ENABLE_REG, 0 );
  2616.     /* ++roman: reset interrupt condition! otherwise no interrupts don't get
  2617.      * through anymore ... */
  2618.     (void)NCR5380_read( RESET_PARITY_INTERRUPT_REG );
  2619. #if 1 /* XXX Should now be done by midlevel code, but it's broken XXX */
  2620.       /* XXX see below                                            XXX */
  2621.     /* MSch: old-style reset: actually abort all command processing here */
  2622.     /* After the reset, there are no more connected or disconnected commands
  2623.      * and no busy units; to avoid problems with re-inserting the commands
  2624.      * into the issue_queue (via scsi_done()), the aborted commands are
  2625.      * remembered in local variables first.
  2626.      */
  2627.     save_flags(flags);
  2628.     cli();
  2629.     connected = (Scsi_Cmnd *)hostdata->connected;
  2630.     hostdata->connected = NULL;
  2631.     disconnected_queue = (Scsi_Cmnd *)hostdata->disconnected_queue;
  2632.     hostdata->disconnected_queue = NULL;
  2633. #ifdef SUPPORT_TAGS
  2634.     free_all_tags();
  2635. #endif
  2636.     for( i = 0; i < 8; ++i )
  2637. hostdata->busy[i] = 0;
  2638. #ifdef REAL_DMA
  2639.     hostdata->dma_len = 0;
  2640. #endif
  2641.     restore_flags(flags);
  2642.     /* In order to tell the mid-level code which commands were aborted, 
  2643.      * set the command status to DID_RESET and call scsi_done() !!!
  2644.      * This ultimately aborts processing of these commands in the mid-level.
  2645.      */
  2646.     if ((cmd = connected)) {
  2647. ABRT_PRINTK("scsi%d: reset aborted a connected commandn", H_NO(cmd));
  2648. cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
  2649. cmd->scsi_done( cmd );
  2650.     }
  2651.     for (i = 0; (cmd = disconnected_queue); ++i) {
  2652. disconnected_queue = NEXT(cmd);
  2653. NEXT(cmd) = NULL;
  2654. cmd->result = (cmd->result & 0xffff) | (DID_RESET << 16);
  2655. cmd->scsi_done( cmd );
  2656.     }
  2657.     if (i > 0)
  2658. ABRT_PRINTK("scsi: reset aborted %d disconnected command(s)n", i);
  2659. /* The Falcon lock should be released after a reset...
  2660.  */
  2661. /* ++guenther: moved to atari_scsi_reset(), to prevent a race between
  2662.  * unlocking and enabling dma interrupt.
  2663.  */
  2664. /*    falcon_release_lock_if_possible( hostdata );*/
  2665.     /* since all commands have been explicitly terminated, we need to tell
  2666.      * the midlevel code that the reset was SUCCESSFUL, and there is no 
  2667.      * need to 'wake up' the commands by a request_sense
  2668.      */
  2669.     return SCSI_RESET_SUCCESS | SCSI_RESET_BUS_RESET;
  2670. #else /* 1 */
  2671.     /* MSch: new-style reset handling: let the mid-level do what it can */
  2672.     /* ++guenther: MID-LEVEL IS STILL BROKEN.
  2673.      * Mid-level is supposed to requeue all commands that were active on the
  2674.      * various low-level queues. In fact it does this, but that's not enough
  2675.      * because all these commands are subject to timeout. And if a timeout
  2676.      * happens for any removed command, *_abort() is called but all queues
  2677.      * are now empty. Abort then gives up the falcon lock, which is fatal,
  2678.      * since the mid-level will queue more commands and must have the lock
  2679.      * (it's all happening inside timer interrupt handler!!).
  2680.      * Even worse, abort will return NOT_RUNNING for all those commands not
  2681.      * on any queue, so they won't be retried ...
  2682.      *
  2683.      * Conclusion: either scsi.c disables timeout for all resetted commands
  2684.      * immediately, or we loose!  As of linux-2.0.20 it doesn't.
  2685.      */
  2686.     /* After the reset, there are no more connected or disconnected commands
  2687.      * and no busy units; so clear the low-level status here to avoid 
  2688.      * conflicts when the mid-level code tries to wake up the affected 
  2689.      * commands!
  2690.      */
  2691.     if (hostdata->issue_queue)
  2692. ABRT_PRINTK("scsi%d: reset aborted issued command(s)n", H_NO(cmd));
  2693.     if (hostdata->connected) 
  2694. ABRT_PRINTK("scsi%d: reset aborted a connected commandn", H_NO(cmd));
  2695.     if (hostdata->disconnected_queue)
  2696. ABRT_PRINTK("scsi%d: reset aborted disconnected command(s)n", H_NO(cmd));
  2697.     save_flags(flags);
  2698.     cli();
  2699.     hostdata->issue_queue = NULL;
  2700.     hostdata->connected = NULL;
  2701.     hostdata->disconnected_queue = NULL;
  2702. #ifdef SUPPORT_TAGS
  2703.     free_all_tags();
  2704. #endif
  2705.     for( i = 0; i < 8; ++i )
  2706. hostdata->busy[i] = 0;
  2707. #ifdef REAL_DMA
  2708.     hostdata->dma_len = 0;
  2709. #endif
  2710.     restore_flags(flags);
  2711.     /* we did no complete reset of all commands, so a wakeup is required */
  2712.     return SCSI_RESET_WAKEUP | SCSI_RESET_BUS_RESET;
  2713. #endif /* 1 */
  2714. }
  2715. /* Local Variables: */
  2716. /* tab-width: 8     */
  2717. /* End:             */