Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * atari_scsi.c -- Device dependent functions for the Atari generic SCSI port
0003  *
0004  * Copyright 1994 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
0005  *
0006  *   Loosely based on the work of Robert De Vries' team and added:
0007  *    - working real DMA
0008  *    - Falcon support (untested yet!)   ++bjoern fixed and now it works
0009  *    - lots of extensions and bug fixes.
0010  *
0011  * This file is subject to the terms and conditions of the GNU General Public
0012  * License.  See the file COPYING in the main directory of this archive
0013  * for more details.
0014  *
0015  */
0016 
0017 /*
0018  * Notes for Falcon SCSI DMA
0019  *
0020  * The 5380 device is one of several that all share the DMA chip. Hence
0021  * "locking" and "unlocking" access to this chip is required.
0022  *
0023  * Two possible schemes for ST DMA acquisition by atari_scsi are:
0024  * 1) The lock is taken for each command separately (i.e. can_queue == 1).
0025  * 2) The lock is taken when the first command arrives and released
0026  * when the last command is finished (i.e. can_queue > 1).
0027  *
0028  * The first alternative limits SCSI bus utilization, since interleaving
0029  * commands is not possible. The second gives better performance but is
0030  * unfair to other drivers needing to use the ST DMA chip. In order to
0031  * allow the IDE and floppy drivers equal access to the ST DMA chip
0032  * the default is can_queue == 1.
0033  */
0034 
0035 #include <linux/module.h>
0036 #include <linux/types.h>
0037 #include <linux/blkdev.h>
0038 #include <linux/interrupt.h>
0039 #include <linux/init.h>
0040 #include <linux/nvram.h>
0041 #include <linux/bitops.h>
0042 #include <linux/wait.h>
0043 #include <linux/platform_device.h>
0044 
0045 #include <asm/setup.h>
0046 #include <asm/atarihw.h>
0047 #include <asm/atariints.h>
0048 #include <asm/atari_stdma.h>
0049 #include <asm/atari_stram.h>
0050 #include <asm/io.h>
0051 
0052 #include <scsi/scsi_host.h>
0053 
0054 #define DMA_MIN_SIZE                    32
0055 
0056 /* Definitions for the core NCR5380 driver. */
0057 
0058 #define NCR5380_implementation_fields   /* none */
0059 
0060 static u8 (*atari_scsi_reg_read)(unsigned int);
0061 static void (*atari_scsi_reg_write)(unsigned int, u8);
0062 
0063 #define NCR5380_read(reg)               atari_scsi_reg_read(reg)
0064 #define NCR5380_write(reg, value)       atari_scsi_reg_write(reg, value)
0065 
0066 #define NCR5380_queue_command           atari_scsi_queue_command
0067 #define NCR5380_abort                   atari_scsi_abort
0068 #define NCR5380_info                    atari_scsi_info
0069 
0070 #define NCR5380_dma_xfer_len            atari_scsi_dma_xfer_len
0071 #define NCR5380_dma_recv_setup          atari_scsi_dma_recv_setup
0072 #define NCR5380_dma_send_setup          atari_scsi_dma_send_setup
0073 #define NCR5380_dma_residual            atari_scsi_dma_residual
0074 
0075 #define NCR5380_acquire_dma_irq(instance)      falcon_get_lock(instance)
0076 #define NCR5380_release_dma_irq(instance)      falcon_release_lock()
0077 
0078 #include "NCR5380.h"
0079 
0080 
0081 #define IS_A_TT()   ATARIHW_PRESENT(TT_SCSI)
0082 
0083 #define SCSI_DMA_WRITE_P(elt,val)               \
0084     do {                            \
0085         unsigned long v = val;              \
0086         tt_scsi_dma.elt##_lo = v & 0xff;        \
0087         v >>= 8;                    \
0088         tt_scsi_dma.elt##_lmd = v & 0xff;       \
0089         v >>= 8;                    \
0090         tt_scsi_dma.elt##_hmd = v & 0xff;       \
0091         v >>= 8;                    \
0092         tt_scsi_dma.elt##_hi = v & 0xff;        \
0093     } while(0)
0094 
0095 #define SCSI_DMA_READ_P(elt)                    \
0096     (((((((unsigned long)tt_scsi_dma.elt##_hi << 8) |   \
0097          (unsigned long)tt_scsi_dma.elt##_hmd) << 8) |  \
0098        (unsigned long)tt_scsi_dma.elt##_lmd) << 8) |    \
0099      (unsigned long)tt_scsi_dma.elt##_lo)
0100 
0101 
0102 static inline void SCSI_DMA_SETADR(unsigned long adr)
0103 {
0104     st_dma.dma_lo = (unsigned char)adr;
0105     MFPDELAY();
0106     adr >>= 8;
0107     st_dma.dma_md = (unsigned char)adr;
0108     MFPDELAY();
0109     adr >>= 8;
0110     st_dma.dma_hi = (unsigned char)adr;
0111     MFPDELAY();
0112 }
0113 
0114 static inline unsigned long SCSI_DMA_GETADR(void)
0115 {
0116     unsigned long adr;
0117     adr = st_dma.dma_lo;
0118     MFPDELAY();
0119     adr |= (st_dma.dma_md & 0xff) << 8;
0120     MFPDELAY();
0121     adr |= (st_dma.dma_hi & 0xff) << 16;
0122     MFPDELAY();
0123     return adr;
0124 }
0125 
0126 static void atari_scsi_fetch_restbytes(void);
0127 
0128 static unsigned long    atari_dma_residual, atari_dma_startaddr;
0129 static short        atari_dma_active;
0130 /* pointer to the dribble buffer */
0131 static char     *atari_dma_buffer;
0132 /* precalculated physical address of the dribble buffer */
0133 static unsigned long    atari_dma_phys_buffer;
0134 /* != 0 tells the Falcon int handler to copy data from the dribble buffer */
0135 static char     *atari_dma_orig_addr;
0136 /* size of the dribble buffer; 4k seems enough, since the Falcon cannot use
0137  * scatter-gather anyway, so most transfers are 1024 byte only. In the rare
0138  * cases where requests to physical contiguous buffers have been merged, this
0139  * request is <= 4k (one page). So I don't think we have to split transfers
0140  * just due to this buffer size...
0141  */
0142 #define STRAM_BUFFER_SIZE   (4096)
0143 /* mask for address bits that can't be used with the ST-DMA */
0144 static unsigned long    atari_dma_stram_mask;
0145 #define STRAM_ADDR(a)   (((a) & atari_dma_stram_mask) == 0)
0146 
0147 static int setup_can_queue = -1;
0148 module_param(setup_can_queue, int, 0);
0149 static int setup_cmd_per_lun = -1;
0150 module_param(setup_cmd_per_lun, int, 0);
0151 static int setup_sg_tablesize = -1;
0152 module_param(setup_sg_tablesize, int, 0);
0153 static int setup_hostid = -1;
0154 module_param(setup_hostid, int, 0);
0155 static int setup_toshiba_delay = -1;
0156 module_param(setup_toshiba_delay, int, 0);
0157 
0158 
0159 static int scsi_dma_is_ignored_buserr(unsigned char dma_stat)
0160 {
0161     int i;
0162     unsigned long addr = SCSI_DMA_READ_P(dma_addr), end_addr;
0163 
0164     if (dma_stat & 0x01) {
0165 
0166         /* A bus error happens when DMA-ing from the last page of a
0167          * physical memory chunk (DMA prefetch!), but that doesn't hurt.
0168          * Check for this case:
0169          */
0170 
0171         for (i = 0; i < m68k_num_memory; ++i) {
0172             end_addr = m68k_memory[i].addr + m68k_memory[i].size;
0173             if (end_addr <= addr && addr <= end_addr + 4)
0174                 return 1;
0175         }
0176     }
0177     return 0;
0178 }
0179 
0180 
0181 static irqreturn_t scsi_tt_intr(int irq, void *dev)
0182 {
0183     struct Scsi_Host *instance = dev;
0184     struct NCR5380_hostdata *hostdata = shost_priv(instance);
0185     int dma_stat;
0186 
0187     dma_stat = tt_scsi_dma.dma_ctrl;
0188 
0189     dsprintk(NDEBUG_INTR, instance, "NCR5380 interrupt, DMA status = %02x\n",
0190              dma_stat & 0xff);
0191 
0192     /* Look if it was the DMA that has interrupted: First possibility
0193      * is that a bus error occurred...
0194      */
0195     if (dma_stat & 0x80) {
0196         if (!scsi_dma_is_ignored_buserr(dma_stat)) {
0197             printk(KERN_ERR "SCSI DMA caused bus error near 0x%08lx\n",
0198                    SCSI_DMA_READ_P(dma_addr));
0199             printk(KERN_CRIT "SCSI DMA bus error -- bad DMA programming!");
0200         }
0201     }
0202 
0203     /* If the DMA is active but not finished, we have the case
0204      * that some other 5380 interrupt occurred within the DMA transfer.
0205      * This means we have residual bytes, if the desired end address
0206      * is not yet reached. Maybe we have to fetch some bytes from the
0207      * rest data register, too. The residual must be calculated from
0208      * the address pointer, not the counter register, because only the
0209      * addr reg counts bytes not yet written and pending in the rest
0210      * data reg!
0211      */
0212     if ((dma_stat & 0x02) && !(dma_stat & 0x40)) {
0213         atari_dma_residual = hostdata->dma_len -
0214             (SCSI_DMA_READ_P(dma_addr) - atari_dma_startaddr);
0215 
0216         dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n",
0217                atari_dma_residual);
0218 
0219         if ((signed int)atari_dma_residual < 0)
0220             atari_dma_residual = 0;
0221         if ((dma_stat & 1) == 0) {
0222             /*
0223              * After read operations, we maybe have to
0224              * transport some rest bytes
0225              */
0226             atari_scsi_fetch_restbytes();
0227         } else {
0228             /*
0229              * There seems to be a nasty bug in some SCSI-DMA/NCR
0230              * combinations: If a target disconnects while a write
0231              * operation is going on, the address register of the
0232              * DMA may be a few bytes farer than it actually read.
0233              * This is probably due to DMA prefetching and a delay
0234              * between DMA and NCR.  Experiments showed that the
0235              * dma_addr is 9 bytes to high, but this could vary.
0236              * The problem is, that the residual is thus calculated
0237              * wrong and the next transfer will start behind where
0238              * it should.  So we round up the residual to the next
0239              * multiple of a sector size, if it isn't already a
0240              * multiple and the originally expected transfer size
0241              * was.  The latter condition is there to ensure that
0242              * the correction is taken only for "real" data
0243              * transfers and not for, e.g., the parameters of some
0244              * other command.  These shouldn't disconnect anyway.
0245              */
0246             if (atari_dma_residual & 0x1ff) {
0247                 dprintk(NDEBUG_DMA, "SCSI DMA: DMA bug corrected, "
0248                        "difference %ld bytes\n",
0249                        512 - (atari_dma_residual & 0x1ff));
0250                 atari_dma_residual = (atari_dma_residual + 511) & ~0x1ff;
0251             }
0252         }
0253         tt_scsi_dma.dma_ctrl = 0;
0254     }
0255 
0256     /* If the DMA is finished, fetch the rest bytes and turn it off */
0257     if (dma_stat & 0x40) {
0258         atari_dma_residual = 0;
0259         if ((dma_stat & 1) == 0)
0260             atari_scsi_fetch_restbytes();
0261         tt_scsi_dma.dma_ctrl = 0;
0262     }
0263 
0264     NCR5380_intr(irq, dev);
0265 
0266     return IRQ_HANDLED;
0267 }
0268 
0269 
0270 static irqreturn_t scsi_falcon_intr(int irq, void *dev)
0271 {
0272     struct Scsi_Host *instance = dev;
0273     struct NCR5380_hostdata *hostdata = shost_priv(instance);
0274     int dma_stat;
0275 
0276     /* Turn off DMA and select sector counter register before
0277      * accessing the status register (Atari recommendation!)
0278      */
0279     st_dma.dma_mode_status = 0x90;
0280     dma_stat = st_dma.dma_mode_status;
0281 
0282     /* Bit 0 indicates some error in the DMA process... don't know
0283      * what happened exactly (no further docu).
0284      */
0285     if (!(dma_stat & 0x01)) {
0286         /* DMA error */
0287         printk(KERN_CRIT "SCSI DMA error near 0x%08lx!\n", SCSI_DMA_GETADR());
0288     }
0289 
0290     /* If the DMA was active, but now bit 1 is not clear, it is some
0291      * other 5380 interrupt that finishes the DMA transfer. We have to
0292      * calculate the number of residual bytes and give a warning if
0293      * bytes are stuck in the ST-DMA fifo (there's no way to reach them!)
0294      */
0295     if (atari_dma_active && (dma_stat & 0x02)) {
0296         unsigned long transferred;
0297 
0298         transferred = SCSI_DMA_GETADR() - atari_dma_startaddr;
0299         /* The ST-DMA address is incremented in 2-byte steps, but the
0300          * data are written only in 16-byte chunks. If the number of
0301          * transferred bytes is not divisible by 16, the remainder is
0302          * lost somewhere in outer space.
0303          */
0304         if (transferred & 15)
0305             printk(KERN_ERR "SCSI DMA error: %ld bytes lost in "
0306                    "ST-DMA fifo\n", transferred & 15);
0307 
0308         atari_dma_residual = hostdata->dma_len - transferred;
0309         dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n",
0310                atari_dma_residual);
0311     } else
0312         atari_dma_residual = 0;
0313     atari_dma_active = 0;
0314 
0315     if (atari_dma_orig_addr) {
0316         /* If the dribble buffer was used on a read operation, copy the DMA-ed
0317          * data to the original destination address.
0318          */
0319         memcpy(atari_dma_orig_addr, phys_to_virt(atari_dma_startaddr),
0320                hostdata->dma_len - atari_dma_residual);
0321         atari_dma_orig_addr = NULL;
0322     }
0323 
0324     NCR5380_intr(irq, dev);
0325 
0326     return IRQ_HANDLED;
0327 }
0328 
0329 
0330 static void atari_scsi_fetch_restbytes(void)
0331 {
0332     int nr;
0333     char *src, *dst;
0334     unsigned long phys_dst;
0335 
0336     /* fetch rest bytes in the DMA register */
0337     phys_dst = SCSI_DMA_READ_P(dma_addr);
0338     nr = phys_dst & 3;
0339     if (nr) {
0340         /* there are 'nr' bytes left for the last long address
0341            before the DMA pointer */
0342         phys_dst ^= nr;
0343         dprintk(NDEBUG_DMA, "SCSI DMA: there are %d rest bytes for phys addr 0x%08lx",
0344                nr, phys_dst);
0345         /* The content of the DMA pointer is a physical address!  */
0346         dst = phys_to_virt(phys_dst);
0347         dprintk(NDEBUG_DMA, " = virt addr %p\n", dst);
0348         for (src = (char *)&tt_scsi_dma.dma_restdata; nr != 0; --nr)
0349             *dst++ = *src++;
0350     }
0351 }
0352 
0353 
0354 /* This function releases the lock on the DMA chip if there is no
0355  * connected command and the disconnected queue is empty.
0356  */
0357 
0358 static void falcon_release_lock(void)
0359 {
0360     if (IS_A_TT())
0361         return;
0362 
0363     if (stdma_is_locked_by(scsi_falcon_intr))
0364         stdma_release();
0365 }
0366 
0367 /* This function manages the locking of the ST-DMA.
0368  * If the DMA isn't locked already for SCSI, it tries to lock it by
0369  * calling stdma_lock(). But if the DMA is locked by the SCSI code and
0370  * there are other drivers waiting for the chip, we do not issue the
0371  * command immediately but tell the SCSI mid-layer to defer.
0372  */
0373 
0374 static int falcon_get_lock(struct Scsi_Host *instance)
0375 {
0376     if (IS_A_TT())
0377         return 1;
0378 
0379     if (stdma_is_locked_by(scsi_falcon_intr))
0380         return 1;
0381 
0382     /* stdma_lock() may sleep which means it can't be used here */
0383     return stdma_try_lock(scsi_falcon_intr, instance);
0384 }
0385 
0386 #ifndef MODULE
0387 static int __init atari_scsi_setup(char *str)
0388 {
0389     /* Format of atascsi parameter is:
0390      *   atascsi=<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags>
0391      * Defaults depend on TT or Falcon, determined at run time.
0392      * Negative values mean don't change.
0393      */
0394     int ints[8];
0395 
0396     get_options(str, ARRAY_SIZE(ints), ints);
0397 
0398     if (ints[0] < 1) {
0399         printk("atari_scsi_setup: no arguments!\n");
0400         return 0;
0401     }
0402     if (ints[0] >= 1)
0403         setup_can_queue = ints[1];
0404     if (ints[0] >= 2)
0405         setup_cmd_per_lun = ints[2];
0406     if (ints[0] >= 3)
0407         setup_sg_tablesize = ints[3];
0408     if (ints[0] >= 4)
0409         setup_hostid = ints[4];
0410     /* ints[5] (use_tagged_queuing) is ignored */
0411     /* ints[6] (use_pdma) is ignored */
0412     if (ints[0] >= 7)
0413         setup_toshiba_delay = ints[7];
0414 
0415     return 1;
0416 }
0417 
0418 __setup("atascsi=", atari_scsi_setup);
0419 #endif /* !MODULE */
0420 
0421 static unsigned long atari_scsi_dma_setup(struct NCR5380_hostdata *hostdata,
0422                       void *data, unsigned long count,
0423                       int dir)
0424 {
0425     unsigned long addr = virt_to_phys(data);
0426 
0427     dprintk(NDEBUG_DMA, "scsi%d: setting up dma, data = %p, phys = %lx, count = %ld, dir = %d\n",
0428             hostdata->host->host_no, data, addr, count, dir);
0429 
0430     if (!IS_A_TT() && !STRAM_ADDR(addr)) {
0431         /* If we have a non-DMAable address on a Falcon, use the dribble
0432          * buffer; 'orig_addr' != 0 in the read case tells the interrupt
0433          * handler to copy data from the dribble buffer to the originally
0434          * wanted address.
0435          */
0436         if (dir)
0437             memcpy(atari_dma_buffer, data, count);
0438         else
0439             atari_dma_orig_addr = data;
0440         addr = atari_dma_phys_buffer;
0441     }
0442 
0443     atari_dma_startaddr = addr; /* Needed for calculating residual later. */
0444 
0445     /* Cache cleanup stuff: On writes, push any dirty cache out before sending
0446      * it to the peripheral. (Must be done before DMA setup, since at least
0447      * the ST-DMA begins to fill internal buffers right after setup. For
0448      * reads, invalidate any cache, may be altered after DMA without CPU
0449      * knowledge.
0450      *
0451      * ++roman: For the Medusa, there's no need at all for that cache stuff,
0452      * because the hardware does bus snooping (fine!).
0453      */
0454     dma_cache_maintenance(addr, count, dir);
0455 
0456     if (IS_A_TT()) {
0457         tt_scsi_dma.dma_ctrl = dir;
0458         SCSI_DMA_WRITE_P(dma_addr, addr);
0459         SCSI_DMA_WRITE_P(dma_cnt, count);
0460         tt_scsi_dma.dma_ctrl = dir | 2;
0461     } else { /* ! IS_A_TT */
0462 
0463         /* set address */
0464         SCSI_DMA_SETADR(addr);
0465 
0466         /* toggle direction bit to clear FIFO and set DMA direction */
0467         dir <<= 8;
0468         st_dma.dma_mode_status = 0x90 | dir;
0469         st_dma.dma_mode_status = 0x90 | (dir ^ 0x100);
0470         st_dma.dma_mode_status = 0x90 | dir;
0471         udelay(40);
0472         /* On writes, round up the transfer length to the next multiple of 512
0473          * (see also comment at atari_dma_xfer_len()). */
0474         st_dma.fdc_acces_seccount = (count + (dir ? 511 : 0)) >> 9;
0475         udelay(40);
0476         st_dma.dma_mode_status = 0x10 | dir;
0477         udelay(40);
0478         /* need not restore value of dir, only boolean value is tested */
0479         atari_dma_active = 1;
0480     }
0481 
0482     return count;
0483 }
0484 
0485 static inline int atari_scsi_dma_recv_setup(struct NCR5380_hostdata *hostdata,
0486                                             unsigned char *data, int count)
0487 {
0488     return atari_scsi_dma_setup(hostdata, data, count, 0);
0489 }
0490 
0491 static inline int atari_scsi_dma_send_setup(struct NCR5380_hostdata *hostdata,
0492                                             unsigned char *data, int count)
0493 {
0494     return atari_scsi_dma_setup(hostdata, data, count, 1);
0495 }
0496 
0497 static int atari_scsi_dma_residual(struct NCR5380_hostdata *hostdata)
0498 {
0499     return atari_dma_residual;
0500 }
0501 
0502 
0503 #define CMD_SURELY_BLOCK_MODE   0
0504 #define CMD_SURELY_BYTE_MODE    1
0505 #define CMD_MODE_UNKNOWN        2
0506 
0507 static int falcon_classify_cmd(struct scsi_cmnd *cmd)
0508 {
0509     unsigned char opcode = cmd->cmnd[0];
0510 
0511     if (opcode == READ_DEFECT_DATA || opcode == READ_LONG ||
0512         opcode == READ_BUFFER)
0513         return CMD_SURELY_BYTE_MODE;
0514     else if (opcode == READ_6 || opcode == READ_10 ||
0515          opcode == 0xa8 /* READ_12 */ || opcode == READ_REVERSE ||
0516          opcode == RECOVER_BUFFERED_DATA) {
0517         /* In case of a sequential-access target (tape), special care is
0518          * needed here: The transfer is block-mode only if the 'fixed' bit is
0519          * set! */
0520         if (cmd->device->type == TYPE_TAPE && !(cmd->cmnd[1] & 1))
0521             return CMD_SURELY_BYTE_MODE;
0522         else
0523             return CMD_SURELY_BLOCK_MODE;
0524     } else
0525         return CMD_MODE_UNKNOWN;
0526 }
0527 
0528 
0529 /* This function calculates the number of bytes that can be transferred via
0530  * DMA. On the TT, this is arbitrary, but on the Falcon we have to use the
0531  * ST-DMA chip. There are only multiples of 512 bytes possible and max.
0532  * 255*512 bytes :-( This means also, that defining READ_OVERRUNS is not
0533  * possible on the Falcon, since that would require to program the DMA for
0534  * n*512 - atari_read_overrun bytes. But it seems that the Falcon doesn't have
0535  * the overrun problem, so this question is academic :-)
0536  */
0537 
0538 static int atari_scsi_dma_xfer_len(struct NCR5380_hostdata *hostdata,
0539                                    struct scsi_cmnd *cmd)
0540 {
0541     int wanted_len = NCR5380_to_ncmd(cmd)->this_residual;
0542     int possible_len, limit;
0543 
0544     if (wanted_len < DMA_MIN_SIZE)
0545         return 0;
0546 
0547     if (IS_A_TT())
0548         /* TT SCSI DMA can transfer arbitrary #bytes */
0549         return wanted_len;
0550 
0551     /* ST DMA chip is stupid -- only multiples of 512 bytes! (and max.
0552      * 255*512 bytes, but this should be enough)
0553      *
0554      * ++roman: Aaargl! Another Falcon-SCSI problem... There are some commands
0555      * that return a number of bytes which cannot be known beforehand. In this
0556      * case, the given transfer length is an "allocation length". Now it
0557      * can happen that this allocation length is a multiple of 512 bytes and
0558      * the DMA is used. But if not n*512 bytes really arrive, some input data
0559      * will be lost in the ST-DMA's FIFO :-( Thus, we have to distinguish
0560      * between commands that do block transfers and those that do byte
0561      * transfers. But this isn't easy... there are lots of vendor specific
0562      * commands, and the user can issue any command via the
0563      * SCSI_IOCTL_SEND_COMMAND.
0564      *
0565      * The solution: We classify SCSI commands in 1) surely block-mode cmd.s,
0566      * 2) surely byte-mode cmd.s and 3) cmd.s with unknown mode. In case 1)
0567      * and 3), the thing to do is obvious: allow any number of blocks via DMA
0568      * or none. In case 2), we apply some heuristic: Byte mode is assumed if
0569      * the transfer (allocation) length is < 1024, hoping that no cmd. not
0570      * explicitly known as byte mode have such big allocation lengths...
0571      * BTW, all the discussion above applies only to reads. DMA writes are
0572      * unproblematic anyways, since the targets aborts the transfer after
0573      * receiving a sufficient number of bytes.
0574      *
0575      * Another point: If the transfer is from/to an non-ST-RAM address, we
0576      * use the dribble buffer and thus can do only STRAM_BUFFER_SIZE bytes.
0577      */
0578 
0579     if (cmd->sc_data_direction == DMA_TO_DEVICE) {
0580         /* Write operation can always use the DMA, but the transfer size must
0581          * be rounded up to the next multiple of 512 (atari_dma_setup() does
0582          * this).
0583          */
0584         possible_len = wanted_len;
0585     } else {
0586         /* Read operations: if the wanted transfer length is not a multiple of
0587          * 512, we cannot use DMA, since the ST-DMA cannot split transfers
0588          * (no interrupt on DMA finished!)
0589          */
0590         if (wanted_len & 0x1ff)
0591             possible_len = 0;
0592         else {
0593             /* Now classify the command (see above) and decide whether it is
0594              * allowed to do DMA at all */
0595             switch (falcon_classify_cmd(cmd)) {
0596             case CMD_SURELY_BLOCK_MODE:
0597                 possible_len = wanted_len;
0598                 break;
0599             case CMD_SURELY_BYTE_MODE:
0600                 possible_len = 0; /* DMA prohibited */
0601                 break;
0602             case CMD_MODE_UNKNOWN:
0603             default:
0604                 /* For unknown commands assume block transfers if the transfer
0605                  * size/allocation length is >= 1024 */
0606                 possible_len = (wanted_len < 1024) ? 0 : wanted_len;
0607                 break;
0608             }
0609         }
0610     }
0611 
0612     /* Last step: apply the hard limit on DMA transfers */
0613     limit = (atari_dma_buffer && !STRAM_ADDR(virt_to_phys(NCR5380_to_ncmd(cmd)->ptr))) ?
0614             STRAM_BUFFER_SIZE : 255*512;
0615     if (possible_len > limit)
0616         possible_len = limit;
0617 
0618     if (possible_len != wanted_len)
0619         dprintk(NDEBUG_DMA, "DMA transfer now %d bytes instead of %d\n",
0620                 possible_len, wanted_len);
0621 
0622     return possible_len;
0623 }
0624 
0625 
0626 /* NCR5380 register access functions
0627  *
0628  * There are separate functions for TT and Falcon, because the access
0629  * methods are quite different. The calling macros NCR5380_read and
0630  * NCR5380_write call these functions via function pointers.
0631  */
0632 
0633 static u8 atari_scsi_tt_reg_read(unsigned int reg)
0634 {
0635     return tt_scsi_regp[reg * 2];
0636 }
0637 
0638 static void atari_scsi_tt_reg_write(unsigned int reg, u8 value)
0639 {
0640     tt_scsi_regp[reg * 2] = value;
0641 }
0642 
0643 static u8 atari_scsi_falcon_reg_read(unsigned int reg)
0644 {
0645     unsigned long flags;
0646     u8 result;
0647 
0648     reg += 0x88;
0649     local_irq_save(flags);
0650     dma_wd.dma_mode_status = (u_short)reg;
0651     result = (u8)dma_wd.fdc_acces_seccount;
0652     local_irq_restore(flags);
0653     return result;
0654 }
0655 
0656 static void atari_scsi_falcon_reg_write(unsigned int reg, u8 value)
0657 {
0658     unsigned long flags;
0659 
0660     reg += 0x88;
0661     local_irq_save(flags);
0662     dma_wd.dma_mode_status = (u_short)reg;
0663     dma_wd.fdc_acces_seccount = (u_short)value;
0664     local_irq_restore(flags);
0665 }
0666 
0667 
0668 #include "NCR5380.c"
0669 
0670 static int atari_scsi_host_reset(struct scsi_cmnd *cmd)
0671 {
0672     int rv;
0673     unsigned long flags;
0674 
0675     local_irq_save(flags);
0676 
0677     /* Abort a maybe active DMA transfer */
0678     if (IS_A_TT()) {
0679         tt_scsi_dma.dma_ctrl = 0;
0680     } else {
0681         if (stdma_is_locked_by(scsi_falcon_intr))
0682             st_dma.dma_mode_status = 0x90;
0683         atari_dma_active = 0;
0684         atari_dma_orig_addr = NULL;
0685     }
0686 
0687     rv = NCR5380_host_reset(cmd);
0688 
0689     /* The 5380 raises its IRQ line while _RST is active but the ST DMA
0690      * "lock" has been released so this interrupt may end up handled by
0691      * floppy or IDE driver (if one of them holds the lock). The NCR5380
0692      * interrupt flag has been cleared already.
0693      */
0694 
0695     local_irq_restore(flags);
0696 
0697     return rv;
0698 }
0699 
0700 #define DRV_MODULE_NAME         "atari_scsi"
0701 #define PFX                     DRV_MODULE_NAME ": "
0702 
0703 static struct scsi_host_template atari_scsi_template = {
0704     .module         = THIS_MODULE,
0705     .proc_name      = DRV_MODULE_NAME,
0706     .name           = "Atari native SCSI",
0707     .info           = atari_scsi_info,
0708     .queuecommand       = atari_scsi_queue_command,
0709     .eh_abort_handler   = atari_scsi_abort,
0710     .eh_host_reset_handler  = atari_scsi_host_reset,
0711     .this_id        = 7,
0712     .cmd_per_lun        = 2,
0713     .dma_boundary       = PAGE_SIZE - 1,
0714     .cmd_size       = sizeof(struct NCR5380_cmd),
0715 };
0716 
0717 static int __init atari_scsi_probe(struct platform_device *pdev)
0718 {
0719     struct Scsi_Host *instance;
0720     int error;
0721     struct resource *irq;
0722     int host_flags = 0;
0723 
0724     irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
0725     if (!irq)
0726         return -ENODEV;
0727 
0728     if (ATARIHW_PRESENT(TT_SCSI)) {
0729         atari_scsi_reg_read  = atari_scsi_tt_reg_read;
0730         atari_scsi_reg_write = atari_scsi_tt_reg_write;
0731     } else {
0732         atari_scsi_reg_read  = atari_scsi_falcon_reg_read;
0733         atari_scsi_reg_write = atari_scsi_falcon_reg_write;
0734     }
0735 
0736     if (ATARIHW_PRESENT(TT_SCSI)) {
0737         atari_scsi_template.can_queue    = 16;
0738         atari_scsi_template.sg_tablesize = SG_ALL;
0739     } else {
0740         atari_scsi_template.can_queue    = 1;
0741         atari_scsi_template.sg_tablesize = 1;
0742     }
0743 
0744     if (setup_can_queue > 0)
0745         atari_scsi_template.can_queue = setup_can_queue;
0746 
0747     if (setup_cmd_per_lun > 0)
0748         atari_scsi_template.cmd_per_lun = setup_cmd_per_lun;
0749 
0750     /* Don't increase sg_tablesize on Falcon! */
0751     if (ATARIHW_PRESENT(TT_SCSI) && setup_sg_tablesize > 0)
0752         atari_scsi_template.sg_tablesize = setup_sg_tablesize;
0753 
0754     if (setup_hostid >= 0) {
0755         atari_scsi_template.this_id = setup_hostid & 7;
0756     } else if (IS_REACHABLE(CONFIG_NVRAM)) {
0757         /* Test if a host id is set in the NVRam */
0758         if (ATARIHW_PRESENT(TT_CLK)) {
0759             unsigned char b;
0760             loff_t offset = 16;
0761             ssize_t count = nvram_read(&b, 1, &offset);
0762 
0763             /* Arbitration enabled? (for TOS)
0764              * If yes, use configured host ID
0765              */
0766             if ((count == 1) && (b & 0x80))
0767                 atari_scsi_template.this_id = b & 7;
0768         }
0769     }
0770 
0771     /* If running on a Falcon and if there's TT-Ram (i.e., more than one
0772      * memory block, since there's always ST-Ram in a Falcon), then
0773      * allocate a STRAM_BUFFER_SIZE byte dribble buffer for transfers
0774      * from/to alternative Ram.
0775      */
0776     if (ATARIHW_PRESENT(ST_SCSI) && !ATARIHW_PRESENT(EXTD_DMA) &&
0777         m68k_realnum_memory > 1) {
0778         atari_dma_buffer = atari_stram_alloc(STRAM_BUFFER_SIZE, "SCSI");
0779         if (!atari_dma_buffer) {
0780             pr_err(PFX "can't allocate ST-RAM double buffer\n");
0781             return -ENOMEM;
0782         }
0783         atari_dma_phys_buffer = atari_stram_to_phys(atari_dma_buffer);
0784         atari_dma_orig_addr = NULL;
0785     }
0786 
0787     instance = scsi_host_alloc(&atari_scsi_template,
0788                                sizeof(struct NCR5380_hostdata));
0789     if (!instance) {
0790         error = -ENOMEM;
0791         goto fail_alloc;
0792     }
0793 
0794     instance->irq = irq->start;
0795 
0796     host_flags |= IS_A_TT() ? 0 : FLAG_LATE_DMA_SETUP;
0797     host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0;
0798 
0799     error = NCR5380_init(instance, host_flags);
0800     if (error)
0801         goto fail_init;
0802 
0803     if (IS_A_TT()) {
0804         error = request_irq(instance->irq, scsi_tt_intr, 0,
0805                             "NCR5380", instance);
0806         if (error) {
0807             pr_err(PFX "request irq %d failed, aborting\n",
0808                    instance->irq);
0809             goto fail_irq;
0810         }
0811         tt_mfp.active_edge |= 0x80; /* SCSI int on L->H */
0812 
0813         tt_scsi_dma.dma_ctrl = 0;
0814         atari_dma_residual = 0;
0815 
0816         /* While the read overruns (described by Drew Eckhardt in
0817          * NCR5380.c) never happened on TTs, they do in fact on the
0818          * Medusa (This was the cause why SCSI didn't work right for
0819          * so long there.) Since handling the overruns slows down
0820          * a bit, I turned the #ifdef's into a runtime condition.
0821          *
0822          * In principle it should be sufficient to do max. 1 byte with
0823          * PIO, but there is another problem on the Medusa with the DMA
0824          * rest data register. So read_overruns is currently set
0825          * to 4 to avoid having transfers that aren't a multiple of 4.
0826          * If the rest data bug is fixed, this can be lowered to 1.
0827          */
0828         if (MACH_IS_MEDUSA) {
0829             struct NCR5380_hostdata *hostdata =
0830                 shost_priv(instance);
0831 
0832             hostdata->read_overruns = 4;
0833         }
0834     } else {
0835         /* Nothing to do for the interrupt: the ST-DMA is initialized
0836          * already.
0837          */
0838         atari_dma_residual = 0;
0839         atari_dma_active = 0;
0840         atari_dma_stram_mask = (ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000
0841                     : 0xff000000);
0842     }
0843 
0844     NCR5380_maybe_reset_bus(instance);
0845 
0846     error = scsi_add_host(instance, NULL);
0847     if (error)
0848         goto fail_host;
0849 
0850     platform_set_drvdata(pdev, instance);
0851 
0852     scsi_scan_host(instance);
0853     return 0;
0854 
0855 fail_host:
0856     if (IS_A_TT())
0857         free_irq(instance->irq, instance);
0858 fail_irq:
0859     NCR5380_exit(instance);
0860 fail_init:
0861     scsi_host_put(instance);
0862 fail_alloc:
0863     if (atari_dma_buffer)
0864         atari_stram_free(atari_dma_buffer);
0865     return error;
0866 }
0867 
0868 static int __exit atari_scsi_remove(struct platform_device *pdev)
0869 {
0870     struct Scsi_Host *instance = platform_get_drvdata(pdev);
0871 
0872     scsi_remove_host(instance);
0873     if (IS_A_TT())
0874         free_irq(instance->irq, instance);
0875     NCR5380_exit(instance);
0876     scsi_host_put(instance);
0877     if (atari_dma_buffer)
0878         atari_stram_free(atari_dma_buffer);
0879     return 0;
0880 }
0881 
0882 static struct platform_driver atari_scsi_driver = {
0883     .remove = __exit_p(atari_scsi_remove),
0884     .driver = {
0885         .name   = DRV_MODULE_NAME,
0886     },
0887 };
0888 
0889 module_platform_driver_probe(atari_scsi_driver, atari_scsi_probe);
0890 
0891 MODULE_ALIAS("platform:" DRV_MODULE_NAME);
0892 MODULE_LICENSE("GPL");