sun3_NCR5380.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:92k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

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