Skip to content
atari_NCR5380.c 93.7 KiB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
     * Insert the cmd into the issue queue. Note that REQUEST SENSE 
     * commands are added to the head of the queue since any command will
     * clear the contingent allegiance condition that exists and the 
     * sense data is only guaranteed to be valid while the condition exists.
     */

    local_irq_save(flags);
    /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA.
     * Otherwise a running NCR5380_main may steal the lock.
     * Lock before actually inserting due to fairness reasons explained in
     * atari_scsi.c. If we insert first, then it's impossible for this driver
     * to release the lock.
     * Stop timer for this command while waiting for the lock, or timeouts
     * may happen (and they really do), and it's no good if the command doesn't
     * appear in any of the queues.
     * ++roman: Just disabling the NCR interrupt isn't sufficient here,
     * because also a timer int can trigger an abort or reset, which would
     * alter queues and touch the lock.
     */
    if (!IS_A_TT()) {
	oldto = atari_scsi_update_timeout(cmd, 0);
Linus Torvalds's avatar
Linus Torvalds committed
	falcon_get_lock();
	atari_scsi_update_timeout(cmd, oldto);
Linus Torvalds's avatar
Linus Torvalds committed
    }
    if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) {
	LIST(cmd, hostdata->issue_queue);
	NEXT(cmd) = hostdata->issue_queue;
	hostdata->issue_queue = cmd;
    } else {
	for (tmp = (Scsi_Cmnd *)hostdata->issue_queue;
	     NEXT(tmp); tmp = NEXT(tmp))
	    ;
	LIST(cmd, tmp);
	NEXT(tmp) = cmd;
    }
    local_irq_restore(flags);

    QU_PRINTK("scsi%d: command added to %s of queue\n", H_NO(cmd),
	      (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail");

    /* If queue_command() is called from an interrupt (real one or bottom
     * half), we let queue_main() do the job of taking care about main. If it
     * is already running, this is a no-op, else main will be queued.
     *
     * If we're not in an interrupt, we can call NCR5380_main()
     * unconditionally, because it cannot be already running.
     */
    if (in_interrupt() || ((flags >> 8) & 7) >= 6)
	queue_main();
    else
	NCR5380_main(NULL);
    return 0;
}

/*
 * Function : NCR5380_main (void) 
 *
 * Purpose : NCR5380_main is a coroutine that runs as long as more work can 
 *	be done on the NCR5380 host adapters in a system.  Both 
 *	NCR5380_queue_command() and NCR5380_intr() will try to start it 
 *	in case it is not running.
 * 
 * NOTE : NCR5380_main exits with interrupts *disabled*, the caller should 
 *  reenable them.  This prevents reentrancy and kernel stack overflow.
 */ 	
    
static void NCR5380_main (void *bl)
{
    Scsi_Cmnd *tmp, *prev;
    struct Scsi_Host *instance = first_instance;
    struct NCR5380_hostdata *hostdata = HOSTDATA(instance);
    int done;
    unsigned long flags;
    
    /*
     * We run (with interrupts disabled) until we're sure that none of 
     * the host adapters have anything that can be done, at which point 
     * we set main_running to 0 and exit.
     *
     * Interrupts are enabled before doing various other internal 
     * instructions, after we've decided that we need to run through
     * the loop again.
     *
     * this should prevent any race conditions.
     * 
     * ++roman: Just disabling the NCR interrupt isn't sufficient here,
     * because also a timer int can trigger an abort or reset, which can
     * alter queues and touch the Falcon lock.
     */

    /* Tell int handlers main() is now already executing.  Note that
       no races are possible here. If an int comes in before
       'main_running' is set here, and queues/executes main via the
       task queue, it doesn't do any harm, just this instance of main
       won't find any work left to do. */
    if (main_running)
    	return;
    main_running = 1;

    local_save_flags(flags);
    do {
	local_irq_disable(); /* Freeze request queues */
	done = 1;
	
	if (!hostdata->connected) {
	    MAIN_PRINTK( "scsi%d: not connected\n", HOSTNO );
	    /*
	     * Search through the issue_queue for a command destined
	     * for a target that's not busy.
	     */
#if (NDEBUG & NDEBUG_LISTS)
	    for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL;
		 tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp))
		;
		/*printk("%p  ", tmp);*/
	    if ((tmp == prev) && tmp) printk(" LOOP\n");/* else printk("\n");*/
#endif
	    for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, 
		 prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp) ) {

#if (NDEBUG & NDEBUG_LISTS)
		if (prev != tmp)
		    printk("MAIN tmp=%p   target=%d   busy=%d lun=%d\n",
			   tmp, tmp->device->id, hostdata->busy[tmp->device->id],
			   tmp->device->lun);
#endif
		/*  When we find one, remove it from the issue queue. */
		/* ++guenther: possible race with Falcon locking */
		if (
#ifdef SUPPORT_TAGS
		    !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE)
#else
		    !(hostdata->busy[tmp->device->id] & (1 << tmp->device->lun))
#endif
		    ) {
		    /* ++guenther: just to be sure, this must be atomic */
		    local_irq_disable();
		    if (prev) {
		        REMOVE(prev, NEXT(prev), tmp, NEXT(tmp));
			NEXT(prev) = NEXT(tmp);
		    } else {
		        REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp));
			hostdata->issue_queue = NEXT(tmp);
		    }
		    NEXT(tmp) = NULL;
		    falcon_dont_release++;
		    
		    /* reenable interrupts after finding one */
		    local_irq_restore(flags);
		    
		    /* 
		     * Attempt to establish an I_T_L nexus here. 
		     * On success, instance->hostdata->connected is set.
		     * On failure, we must add the command back to the
		     *   issue queue so we can keep trying.	
		     */
		    MAIN_PRINTK("scsi%d: main(): command for target %d "
				"lun %d removed from issue_queue\n",
				HOSTNO, tmp->device->id, tmp->device->lun);
		    /* 
		     * REQUEST SENSE commands are issued without tagged
		     * queueing, even on SCSI-II devices because the 
		     * contingent allegiance condition exists for the 
		     * entire unit.
		     */
		    /* ++roman: ...and the standard also requires that
		     * REQUEST SENSE command are untagged.
		     */
		    
#ifdef SUPPORT_TAGS
		    cmd_get_tag( tmp, tmp->cmnd[0] != REQUEST_SENSE );
#endif
		    if (!NCR5380_select(instance, tmp, 
			    (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : 
			    TAG_NEXT)) {
			falcon_dont_release--;
			/* release if target did not response! */
			falcon_release_lock_if_possible( hostdata );
			break;
		    } else {
			local_irq_disable();
			LIST(tmp, hostdata->issue_queue);
			NEXT(tmp) = hostdata->issue_queue;
			hostdata->issue_queue = tmp;
#ifdef SUPPORT_TAGS
			cmd_free_tag( tmp );
#endif
			falcon_dont_release--;
			local_irq_restore(flags);
			MAIN_PRINTK("scsi%d: main(): select() failed, "
				    "returned to issue_queue\n", HOSTNO);
			if (hostdata->connected)
			    break;
		    }
		} /* if target/lun/target queue is not busy */
	    } /* for issue_queue */
	} /* if (!hostdata->connected) */
		
	if (hostdata->connected 
#ifdef REAL_DMA
	    && !hostdata->dma_len
#endif
	    ) {
	    local_irq_restore(flags);
	    MAIN_PRINTK("scsi%d: main: performing information transfer\n",
			HOSTNO);
	    NCR5380_information_transfer(instance);
	    MAIN_PRINTK("scsi%d: main: done set false\n", HOSTNO);
	    done = 0;
	}
    } while (!done);

    /* Better allow ints _after_ 'main_running' has been cleared, else
       an interrupt could believe we'll pick up the work it left for
       us, but we won't see it anymore here... */
    main_running = 0;
    local_irq_restore(flags);
}


#ifdef REAL_DMA
/*
 * Function : void NCR5380_dma_complete (struct Scsi_Host *instance)
 *
 * Purpose : Called by interrupt handler when DMA finishes or a phase
 *	mismatch occurs (which would finish the DMA transfer).  
 *
 * Inputs : instance - this instance of the NCR5380.
 *
 */

static void NCR5380_dma_complete( struct Scsi_Host *instance )
{
    SETUP_HOSTDATA(instance);
    int           transfered, saved_data = 0, overrun = 0, cnt, toPIO;
    unsigned char **data, p;
    volatile int  *count;

    if (!hostdata->connected) {
	printk(KERN_WARNING "scsi%d: received end of DMA interrupt with "
	       "no connected cmd\n", HOSTNO);
	return;
    }
    
    if (atari_read_overruns) {
	p = hostdata->connected->SCp.phase;
	if (p & SR_IO) {
	    udelay(10);
	    if ((((NCR5380_read(BUS_AND_STATUS_REG)) &
		  (BASR_PHASE_MATCH|BASR_ACK)) ==
		 (BASR_PHASE_MATCH|BASR_ACK))) {
		saved_data = NCR5380_read(INPUT_DATA_REG);
		overrun = 1;
		DMA_PRINTK("scsi%d: read overrun handled\n", HOSTNO);
	    }
	}
    }

    DMA_PRINTK("scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n",
	       HOSTNO, NCR5380_read(BUS_AND_STATUS_REG),
	       NCR5380_read(STATUS_REG));

    (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
    NCR5380_write(MODE_REG, MR_BASE);
    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);

    transfered = hostdata->dma_len - NCR5380_dma_residual(instance);
    hostdata->dma_len = 0;

    data = (unsigned char **) &(hostdata->connected->SCp.ptr);
    count = &(hostdata->connected->SCp.this_residual);
    *data += transfered;
    *count -= transfered;

    if (atari_read_overruns) {
	if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) {
	    cnt = toPIO = atari_read_overruns;
	    if (overrun) {
		DMA_PRINTK("Got an input overrun, using saved byte\n");
		*(*data)++ = saved_data;
		(*count)--;
		cnt--;
		toPIO--;
	    }
	    DMA_PRINTK("Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data);
	    NCR5380_transfer_pio(instance, &p, &cnt, data);
	    *count -= toPIO - cnt;
	}
    }
}
#endif /* REAL_DMA */


/*
 * Function : void NCR5380_intr (int irq)
 * 
 * Purpose : handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses
 *	from the disconnected queue, and restarting NCR5380_main() 
 *	as required.
 *
 * Inputs : int irq, irq that caused this interrupt.
 *
 */

static irqreturn_t NCR5380_intr (int irq, void *dev_id)
Linus Torvalds's avatar
Linus Torvalds committed
{
    struct Scsi_Host *instance = first_instance;
    int done = 1, handled = 0;
    unsigned char basr;

    INT_PRINTK("scsi%d: NCR5380 irq triggered\n", HOSTNO);

    /* Look for pending interrupts */
    basr = NCR5380_read(BUS_AND_STATUS_REG);
    INT_PRINTK("scsi%d: BASR=%02x\n", HOSTNO, basr);
    /* dispatch to appropriate routine if found and done=0 */
    if (basr & BASR_IRQ) {
	NCR_PRINT(NDEBUG_INTR);
	if ((NCR5380_read(STATUS_REG) & (SR_SEL|SR_IO)) == (SR_SEL|SR_IO)) {
	    done = 0;
	    ENABLE_IRQ();
	    INT_PRINTK("scsi%d: SEL interrupt\n", HOSTNO);
	    NCR5380_reselect(instance);
	    (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
	}
	else if (basr & BASR_PARITY_ERROR) {
	    INT_PRINTK("scsi%d: PARITY interrupt\n", HOSTNO);
	    (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
	}
	else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) {
	    INT_PRINTK("scsi%d: RESET interrupt\n", HOSTNO);
	    (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG);
	}
	else {
	    /*  
	     * The rest of the interrupt conditions can occur only during a
	     * DMA transfer
	     */

#if defined(REAL_DMA)
	    /*
	     * We should only get PHASE MISMATCH and EOP interrupts if we have
	     * DMA enabled, so do a sanity check based on the current setting
	     * of the MODE register.
	     */

	    if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) &&
		((basr & BASR_END_DMA_TRANSFER) || 
		 !(basr & BASR_PHASE_MATCH))) {
		    
		INT_PRINTK("scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO);
		NCR5380_dma_complete( instance );
		done = 0;
		ENABLE_IRQ();
	    } else
#endif /* REAL_DMA */
	    {
/* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */
		if (basr & BASR_PHASE_MATCH)
		    printk(KERN_NOTICE "scsi%d: unknown interrupt, "
			   "BASR 0x%x, MR 0x%x, SR 0x%x\n",
			   HOSTNO, basr, NCR5380_read(MODE_REG),
			   NCR5380_read(STATUS_REG));
		(void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
	    }
	} /* if !(SELECTION || PARITY) */
	handled = 1;
    } /* BASR & IRQ */
    else {
	printk(KERN_NOTICE "scsi%d: interrupt without IRQ bit set in BASR, "
	       "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO, basr,
	       NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG));
	(void) NCR5380_read(RESET_PARITY_INTERRUPT_REG);
    }
    
    if (!done) {
	INT_PRINTK("scsi%d: in int routine, calling main\n", HOSTNO);
	/* Put a call to NCR5380_main() on the queue... */
	queue_main();
    }
    return IRQ_RETVAL(handled);
}

#ifdef NCR5380_STATS
static void collect_stats(struct NCR5380_hostdata* hostdata, Scsi_Cmnd* cmd)
{
# ifdef NCR5380_STAT_LIMIT
    if (cmd->request_bufflen > NCR5380_STAT_LIMIT)
# endif
	switch (cmd->cmnd[0])
	{
	    case WRITE:
	    case WRITE_6:
	    case WRITE_10:
		hostdata->time_write[cmd->device->id] += (jiffies - hostdata->timebase);
		/*hostdata->bytes_write[cmd->device->id] += cmd->request_bufflen;*/
		hostdata->pendingw--;
		break;
	    case READ:
	    case READ_6:
	    case READ_10:
		hostdata->time_read[cmd->device->id] += (jiffies - hostdata->timebase);
		/*hostdata->bytes_read[cmd->device->id] += cmd->request_bufflen;*/
		hostdata->pendingr--;
		break;
	}
}
#endif

/* 
 * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd, 
 *	int tag);
 *
 * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command,
 *	including ARBITRATION, SELECTION, and initial message out for 
 *	IDENTIFY and queue messages. 
 *
 * Inputs : instance - instantiation of the 5380 driver on which this 
 * 	target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for 
 *	new tag, TAG_NONE for untagged queueing, otherwise set to the tag for 
 *	the command that is presently connected.
 * 
 * Returns : -1 if selection could not execute for some reason,
 *	0 if selection succeeded or failed because the target 
 * 	did not respond.
 *
 * Side effects : 
 * 	If bus busy, arbitration failed, etc, NCR5380_select() will exit 
 *		with registers as they should have been on entry - ie
 *		SELECT_ENABLE will be set appropriately, the NCR5380
 *		will cease to drive any SCSI bus signals.
 *
 *	If successful : I_T_L or I_T_L_Q nexus will be established, 
 *		instance->connected will be set to cmd.  
 * 		SELECT interrupt will be disabled.
 *
 *	If failed (no target) : cmd->scsi_done() will be called, and the 
 *		cmd->result host byte set to DID_BAD_TARGET.
 */

static int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd, int tag)
{
    SETUP_HOSTDATA(instance);
    unsigned char tmp[3], phase;
    unsigned char *data;
    int len;
    unsigned long timeout;
    unsigned long flags;

    hostdata->restart_select = 0;
    NCR_PRINT(NDEBUG_ARBITRATION);
    ARB_PRINTK("scsi%d: starting arbitration, id = %d\n", HOSTNO,
	       instance->this_id);

    /* 
     * Set the phase bits to 0, otherwise the NCR5380 won't drive the 
     * data bus during SELECTION.
     */

    local_irq_save(flags);
    if (hostdata->connected) {
	local_irq_restore(flags);
	return -1;
    }
    NCR5380_write(TARGET_COMMAND_REG, 0);


    /* 
     * Start arbitration.
     */
    
    NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask);
    NCR5380_write(MODE_REG, MR_ARBITRATE);

    local_irq_restore(flags);

    /* Wait for arbitration logic to complete */
#if defined(NCR_TIMEOUT)
Linus Torvalds's avatar
Linus Torvalds committed
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000
    {
      unsigned long timeout = jiffies + 2*NCR_TIMEOUT;

      while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
	   && time_before(jiffies, timeout) && !hostdata->connected)
	;
      if (time_after_eq(jiffies, timeout))
      {
	printk("scsi : arbitration timeout at %d\n", __LINE__);
	NCR5380_write(MODE_REG, MR_BASE);
	NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
	return -1;
      }
    }
#else /* NCR_TIMEOUT */
    while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS)
	 && !hostdata->connected);
#endif

    ARB_PRINTK("scsi%d: arbitration complete\n", HOSTNO);

    if (hostdata->connected) {
	NCR5380_write(MODE_REG, MR_BASE); 
	return -1;
    }
    /* 
     * The arbitration delay is 2.2us, but this is a minimum and there is 
     * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate
     * the integral nature of udelay().
     *
     */

    udelay(3);

    /* Check for lost arbitration */
    if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
	(NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) ||
	(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
	hostdata->connected) {
	NCR5380_write(MODE_REG, MR_BASE); 
	ARB_PRINTK("scsi%d: lost arbitration, deasserting MR_ARBITRATE\n",
		   HOSTNO);
	return -1;
    }

     /* after/during arbitration, BSY should be asserted.
	IBM DPES-31080 Version S31Q works now */
     /* Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) */
    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL |
					 ICR_ASSERT_BSY ) ;
    
    if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) ||
	hostdata->connected) {
	NCR5380_write(MODE_REG, MR_BASE);
	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
	ARB_PRINTK("scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n",
		   HOSTNO);
	return -1;
    }

    /* 
     * Again, bus clear + bus settle time is 1.2us, however, this is 
     * a minimum so we'll udelay ceil(1.2)
     */

#ifdef CONFIG_ATARI_SCSI_TOSHIBA_DELAY
    /* ++roman: But some targets (see above :-) seem to need a bit more... */
    udelay(15);
#else
    udelay(2);
#endif
    
    if (hostdata->connected) {
	NCR5380_write(MODE_REG, MR_BASE);
	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
	return -1;
    }

    ARB_PRINTK("scsi%d: won arbitration\n", HOSTNO);

    /* 
     * Now that we have won arbitration, start Selection process, asserting 
     * the host and target ID's on the SCSI bus.
     */

    NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id)));

    /* 
     * Raise ATN while SEL is true before BSY goes false from arbitration,
     * since this is the only way to guarantee that we'll get a MESSAGE OUT
     * phase immediately after selection.
     */

    NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | 
	ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL ));
    NCR5380_write(MODE_REG, MR_BASE);

    /* 
     * Reselect interrupts must be turned off prior to the dropping of BSY,
     * otherwise we will trigger an interrupt.
     */

    if (hostdata->connected) {
	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
	return -1;
    }

    NCR5380_write(SELECT_ENABLE_REG, 0);

    /*
     * The initiator shall then wait at least two deskew delays and release 
     * the BSY signal.
     */
    udelay(1);        /* wingel -- wait two bus deskew delay >2*45ns */

    /* Reset BSY */
    NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | 
	ICR_ASSERT_ATN | ICR_ASSERT_SEL));

    /* 
     * Something weird happens when we cease to drive BSY - looks
     * like the board/chip is letting us do another read before the 
     * appropriate propagation delay has expired, and we're confusing
     * a BSY signal from ourselves as the target's response to SELECTION.
     *
     * A small delay (the 'C++' frontend breaks the pipeline with an
     * unnecessary jump, making it work on my 386-33/Trantor T128, the
     * tighter 'C' code breaks and requires this) solves the problem - 
     * the 1 us delay is arbitrary, and only used because this delay will 
     * be the same on other platforms and since it works here, it should 
     * work there.
     *
     * wingel suggests that this could be due to failing to wait
     * one deskew delay.
     */

    udelay(1);

    SEL_PRINTK("scsi%d: selecting target %d\n", HOSTNO, cmd->device->id);

    /* 
     * The SCSI specification calls for a 250 ms timeout for the actual 
     * selection.
     */

    timeout = jiffies + 25; 

    /* 
     * XXX very interesting - we're seeing a bounce where the BSY we 
     * asserted is being reflected / still asserted (propagation delay?)
     * and it's detecting as true.  Sigh.
     */

#if 0
    /* ++roman: If a target conformed to the SCSI standard, it wouldn't assert
     * IO while SEL is true. But again, there are some disks out the in the
     * world that do that nevertheless. (Somebody claimed that this announces
     * reselection capability of the target.) So we better skip that test and
     * only wait for BSY... (Famous german words: Der Klügere gibt nach :-)
     */

    while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) & 
	(SR_BSY | SR_IO)));

    if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == 
	    (SR_SEL | SR_IO)) {
	    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
	    NCR5380_reselect(instance);
	    printk (KERN_ERR "scsi%d: reselection after won arbitration?\n",
		    HOSTNO);
	    NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
	    return -1;
    }
#else
    while (time_before(jiffies, timeout) && !(NCR5380_read(STATUS_REG) & SR_BSY));
#endif

    /* 
     * No less than two deskew delays after the initiator detects the 
     * BSY signal is true, it shall release the SEL signal and may 
     * change the DATA BUS.                                     -wingel
     */

    udelay(1);

    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);

    if (!(NCR5380_read(STATUS_REG) & SR_BSY)) {
	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
	if (hostdata->targets_present & (1 << cmd->device->id)) {
	    printk(KERN_ERR "scsi%d: weirdness\n", HOSTNO);
	    if (hostdata->restart_select)
		printk(KERN_NOTICE "\trestart select\n");
	    NCR_PRINT(NDEBUG_ANY);
	    NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
	    return -1;
	}
	cmd->result = DID_BAD_TARGET << 16;
#ifdef NCR5380_STATS
	collect_stats(hostdata, cmd);
#endif
#ifdef SUPPORT_TAGS
	cmd_free_tag( cmd );
#endif
	cmd->scsi_done(cmd);
	NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
	SEL_PRINTK("scsi%d: target did not respond within 250ms\n", HOSTNO);
	NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask);
	return 0;
    } 

    hostdata->targets_present |= (1 << cmd->device->id);

    /*
     * Since we followed the SCSI spec, and raised ATN while SEL 
     * was true but before BSY was false during selection, the information
     * transfer phase should be a MESSAGE OUT phase so that we can send the
     * IDENTIFY message.
     * 
     * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG
     * message (2 bytes) with a tag ID that we increment with every command
     * until it wraps back to 0.
     *
     * XXX - it turns out that there are some broken SCSI-II devices,
     *	     which claim to support tagged queuing but fail when more than
     *	     some number of commands are issued at once.
     */

    /* Wait for start of REQ/ACK handshake */
    while (!(NCR5380_read(STATUS_REG) & SR_REQ));

    SEL_PRINTK("scsi%d: target %d selected, going into MESSAGE OUT phase.\n",
	       HOSTNO, cmd->device->id);
    tmp[0] = IDENTIFY(1, cmd->device->lun);

#ifdef SUPPORT_TAGS
    if (cmd->tag != TAG_NONE) {
	tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG;
	tmp[2] = cmd->tag;
	len = 3;
    } else 
	len = 1;
#else
    len = 1;
    cmd->tag=0;
#endif /* SUPPORT_TAGS */

    /* Send message(s) */
    data = tmp;
    phase = PHASE_MSGOUT;
    NCR5380_transfer_pio(instance, &phase, &len, &data);
    SEL_PRINTK("scsi%d: nexus established.\n", HOSTNO);
    /* XXX need to handle errors here */
    hostdata->connected = cmd;
#ifndef SUPPORT_TAGS
    hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
#endif    

    initialize_SCp(cmd);


    return 0;
}

/* 
 * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance, 
 *      unsigned char *phase, int *count, unsigned char **data)
 *
 * Purpose : transfers data in given phase using polled I/O
 *
 * Inputs : instance - instance of driver, *phase - pointer to 
 *	what phase is expected, *count - pointer to number of 
 *	bytes to transfer, **data - pointer to data pointer.
 * 
 * Returns : -1 when different phase is entered without transferring
 *	maximum number of bytes, 0 if all bytes are transfered or exit
 *	is in same phase.
 *
 * 	Also, *phase, *count, *data are modified in place.
 *
 * XXX Note : handling for bus free may be useful.
 */

/*
 * Note : this code is not as quick as it could be, however it 
 * IS 100% reliable, and for the actual data transfer where speed
 * counts, we will always do a pseudo DMA or DMA transfer.
 */

static int NCR5380_transfer_pio( struct Scsi_Host *instance, 
				 unsigned char *phase, int *count,
				 unsigned char **data)
{
    register unsigned char p = *phase, tmp;
    register int c = *count;
    register unsigned char *d = *data;

    /* 
     * The NCR5380 chip will only drive the SCSI bus when the 
     * phase specified in the appropriate bits of the TARGET COMMAND
     * REGISTER match the STATUS REGISTER
     */

    NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));

    do {
	/* 
	 * Wait for assertion of REQ, after which the phase bits will be 
	 * valid 
	 */
	while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ));

	HSH_PRINTK("scsi%d: REQ detected\n", HOSTNO);

	/* Check for phase mismatch */	
	if ((tmp & PHASE_MASK) != p) {
	    PIO_PRINTK("scsi%d: phase mismatch\n", HOSTNO);
	    NCR_PRINT_PHASE(NDEBUG_PIO);
	    break;
	}

	/* Do actual transfer from SCSI bus to / from memory */
	if (!(p & SR_IO)) 
	    NCR5380_write(OUTPUT_DATA_REG, *d);
	else 
	    *d = NCR5380_read(CURRENT_SCSI_DATA_REG);

	++d;

	/* 
	 * The SCSI standard suggests that in MSGOUT phase, the initiator
	 * should drop ATN on the last byte of the message phase
	 * after REQ has been asserted for the handshake but before
	 * the initiator raises ACK.
	 */

	if (!(p & SR_IO)) {
	    if (!((p & SR_MSG) && c > 1)) {
		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
		    ICR_ASSERT_DATA);
		NCR_PRINT(NDEBUG_PIO);
		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
			ICR_ASSERT_DATA | ICR_ASSERT_ACK);
	    } else {
		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE |
		    ICR_ASSERT_DATA | ICR_ASSERT_ATN);
		NCR_PRINT(NDEBUG_PIO);
		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | 
		    ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK);
	    }
	} else {
	    NCR_PRINT(NDEBUG_PIO);
	    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK);
	}

	while (NCR5380_read(STATUS_REG) & SR_REQ);

	HSH_PRINTK("scsi%d: req false, handshake complete\n", HOSTNO);

/*
 * We have several special cases to consider during REQ/ACK handshaking : 
 * 1.  We were in MSGOUT phase, and we are on the last byte of the 
 *	message.  ATN must be dropped as ACK is dropped.
 *
 * 2.  We are in a MSGIN phase, and we are on the last byte of the  
 *	message.  We must exit with ACK asserted, so that the calling
 *	code may raise ATN before dropping ACK to reject the message.
 *
 * 3.  ACK and ATN are clear and the target may proceed as normal.
 */
	if (!(p == PHASE_MSGIN && c == 1)) {  
	    if (p == PHASE_MSGOUT && c > 1)
		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
	    else
		NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE);
	} 
    } while (--c);

    PIO_PRINTK("scsi%d: residual %d\n", HOSTNO, c);

    *count = c;
    *data = d;
    tmp = NCR5380_read(STATUS_REG);
    /* The phase read from the bus is valid if either REQ is (already)
     * asserted or if ACK hasn't been released yet. The latter is the case if
     * we're in MSGIN and all wanted bytes have been received. */
    if ((tmp & SR_REQ) || (p == PHASE_MSGIN && c == 0))
	*phase = tmp & PHASE_MASK;
    else 
	*phase = PHASE_UNKNOWN;

    if (!c || (*phase == p))
	return 0;
    else 
	return -1;
}

/*
 * Function : do_abort (Scsi_Host *host)
 * 
 * Purpose : abort the currently established nexus.  Should only be 
 * 	called from a routine which can drop into a 
 * 
 * Returns : 0 on success, -1 on failure.
 */

static int do_abort (struct Scsi_Host *host) 
{
    unsigned char tmp, *msgptr, phase;
    int len;

    /* Request message out phase */
    NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);

    /* 
     * Wait for the target to indicate a valid phase by asserting 
     * REQ.  Once this happens, we'll have either a MSGOUT phase 
     * and can immediately send the ABORT message, or we'll have some 
     * other phase and will have to source/sink data.
     * 
     * We really don't care what value was on the bus or what value
     * the target sees, so we just handshake.
     */
    
    while (!(tmp = NCR5380_read(STATUS_REG)) & SR_REQ);

    NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp));

    if ((tmp & PHASE_MASK) != PHASE_MSGOUT) {
	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | 
		      ICR_ASSERT_ACK);
	while (NCR5380_read(STATUS_REG) & SR_REQ);
	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN);
    }
   
    tmp = ABORT;
    msgptr = &tmp;
    len = 1;
    phase = PHASE_MSGOUT;
    NCR5380_transfer_pio (host, &phase, &len, &msgptr);

    /*
     * If we got here, and the command completed successfully,
     * we're about to go into bus free state.
     */

    return len ? -1 : 0;
}

#if defined(REAL_DMA)
/* 
 * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance, 
 *      unsigned char *phase, int *count, unsigned char **data)
 *
 * Purpose : transfers data in given phase using either real
 *	or pseudo DMA.
 *
 * Inputs : instance - instance of driver, *phase - pointer to 
 *	what phase is expected, *count - pointer to number of 
 *	bytes to transfer, **data - pointer to data pointer.
 * 
 * Returns : -1 when different phase is entered without transferring
 *	maximum number of bytes, 0 if all bytes or transfered or exit
 *	is in same phase.
 *
 * 	Also, *phase, *count, *data are modified in place.
 *
 */


static int NCR5380_transfer_dma( struct Scsi_Host *instance, 
				 unsigned char *phase, int *count,
				 unsigned char **data)
{
    SETUP_HOSTDATA(instance);
    register int c = *count;
    register unsigned char p = *phase;
    register unsigned char *d = *data;
    unsigned char tmp;
    unsigned long flags;

    if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) {
        *phase = tmp;
        return -1;
    }

    if (atari_read_overruns && (p & SR_IO)) {
	c -= atari_read_overruns;
    }

    DMA_PRINTK("scsi%d: initializing DMA for %s, %d bytes %s %p\n",
	       HOSTNO, (p & SR_IO) ? "reading" : "writing",
	       c, (p & SR_IO) ? "to" : "from", d);

    NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p));

#ifdef REAL_DMA
    NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY);
#endif /* def REAL_DMA  */

    if (IS_A_TT()) {
	/* On the Medusa, it is a must to initialize the DMA before
	 * starting the NCR. This is also the cleaner way for the TT.
	 */
	local_irq_save(flags);
	hostdata->dma_len = (p & SR_IO) ?
	    NCR5380_dma_read_setup(instance, d, c) : 
	    NCR5380_dma_write_setup(instance, d, c);
	local_irq_restore(flags);
    }
    
    if (p & SR_IO)
	NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0);
    else {
	NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA);
	NCR5380_write(START_DMA_SEND_REG, 0);
    }

    if (!IS_A_TT()) {
	/* On the Falcon, the DMA setup must be done after the last */
	/* NCR access, else the DMA setup gets trashed!
	 */