Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * atari_scsi.c -- Device dependent functions for the Atari generic SCSI port |
| 3 | * |
| 4 | * Copyright 1994 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de> |
| 5 | * |
| 6 | * Loosely based on the work of Robert De Vries' team and added: |
| 7 | * - working real DMA |
| 8 | * - Falcon support (untested yet!) ++bjoern fixed and now it works |
| 9 | * - lots of extensions and bug fixes. |
| 10 | * |
| 11 | * This file is subject to the terms and conditions of the GNU General Public |
| 12 | * License. See the file COPYING in the main directory of this archive |
| 13 | * for more details. |
| 14 | * |
| 15 | */ |
| 16 | |
Finn Thain | ded155b | 2016-03-23 21:10:30 +1100 | [diff] [blame] | 17 | /* |
| 18 | * Notes for Falcon SCSI DMA |
| 19 | * |
| 20 | * The 5380 device is one of several that all share the DMA chip. Hence |
| 21 | * "locking" and "unlocking" access to this chip is required. |
| 22 | * |
| 23 | * Two possible schemes for ST DMA acquisition by atari_scsi are: |
| 24 | * 1) The lock is taken for each command separately (i.e. can_queue == 1). |
| 25 | * 2) The lock is taken when the first command arrives and released |
| 26 | * when the last command is finished (i.e. can_queue > 1). |
| 27 | * |
| 28 | * The first alternative limits SCSI bus utilization, since interleaving |
| 29 | * commands is not possible. The second gives better performance but is |
| 30 | * unfair to other drivers needing to use the ST DMA chip. In order to |
| 31 | * allow the IDE and floppy drivers equal access to the ST DMA chip |
| 32 | * the default is can_queue == 1. |
| 33 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | #include <linux/types.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | #include <linux/blkdev.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/interrupt.h> |
| 39 | #include <linux/init.h> |
| 40 | #include <linux/nvram.h> |
| 41 | #include <linux/bitops.h> |
Arnd Bergmann | eff9cf8 | 2014-03-01 20:51:03 +1300 | [diff] [blame] | 42 | #include <linux/wait.h> |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 43 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | #include <asm/setup.h> |
| 46 | #include <asm/atarihw.h> |
| 47 | #include <asm/atariints.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | #include <asm/atari_stdma.h> |
| 49 | #include <asm/atari_stram.h> |
| 50 | #include <asm/io.h> |
| 51 | |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 52 | #include <scsi/scsi_host.h> |
| 53 | |
Finn Thain | e63449c | 2016-03-23 21:10:13 +1100 | [diff] [blame] | 54 | #define DMA_MIN_SIZE 32 |
| 55 | |
Finn Thain | 4e70520 | 2014-11-12 16:12:10 +1100 | [diff] [blame] | 56 | /* Definitions for the core NCR5380 driver. */ |
| 57 | |
Finn Thain | 4e70520 | 2014-11-12 16:12:10 +1100 | [diff] [blame] | 58 | #define NCR5380_implementation_fields /* none */ |
| 59 | |
| 60 | #define NCR5380_read(reg) atari_scsi_reg_read(reg) |
| 61 | #define NCR5380_write(reg, value) atari_scsi_reg_write(reg, value) |
| 62 | |
| 63 | #define NCR5380_queue_command atari_scsi_queue_command |
| 64 | #define NCR5380_abort atari_scsi_abort |
Finn Thain | 4e70520 | 2014-11-12 16:12:10 +1100 | [diff] [blame] | 65 | #define NCR5380_info atari_scsi_info |
| 66 | |
Finn Thain | 52d3e56 | 2016-03-23 21:10:20 +1100 | [diff] [blame] | 67 | #define NCR5380_dma_recv_setup(instance, data, count) \ |
Finn Thain | 4e70520 | 2014-11-12 16:12:10 +1100 | [diff] [blame] | 68 | atari_scsi_dma_setup(instance, data, count, 0) |
Finn Thain | 52d3e56 | 2016-03-23 21:10:20 +1100 | [diff] [blame] | 69 | #define NCR5380_dma_send_setup(instance, data, count) \ |
Finn Thain | 4e70520 | 2014-11-12 16:12:10 +1100 | [diff] [blame] | 70 | atari_scsi_dma_setup(instance, data, count, 1) |
| 71 | #define NCR5380_dma_residual(instance) \ |
| 72 | atari_scsi_dma_residual(instance) |
| 73 | #define NCR5380_dma_xfer_len(instance, cmd, phase) \ |
| 74 | atari_dma_xfer_len(cmd->SCp.this_residual, cmd, !((phase) & SR_IO)) |
| 75 | |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 76 | #define NCR5380_acquire_dma_irq(instance) falcon_get_lock(instance) |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 77 | #define NCR5380_release_dma_irq(instance) falcon_release_lock() |
| 78 | |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 79 | #include "NCR5380.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Finn Thain | 4e70520 | 2014-11-12 16:12:10 +1100 | [diff] [blame] | 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | #define IS_A_TT() ATARIHW_PRESENT(TT_SCSI) |
| 83 | |
| 84 | #define SCSI_DMA_WRITE_P(elt,val) \ |
| 85 | do { \ |
| 86 | unsigned long v = val; \ |
| 87 | tt_scsi_dma.elt##_lo = v & 0xff; \ |
| 88 | v >>= 8; \ |
| 89 | tt_scsi_dma.elt##_lmd = v & 0xff; \ |
| 90 | v >>= 8; \ |
| 91 | tt_scsi_dma.elt##_hmd = v & 0xff; \ |
| 92 | v >>= 8; \ |
| 93 | tt_scsi_dma.elt##_hi = v & 0xff; \ |
| 94 | } while(0) |
| 95 | |
| 96 | #define SCSI_DMA_READ_P(elt) \ |
| 97 | (((((((unsigned long)tt_scsi_dma.elt##_hi << 8) | \ |
| 98 | (unsigned long)tt_scsi_dma.elt##_hmd) << 8) | \ |
| 99 | (unsigned long)tt_scsi_dma.elt##_lmd) << 8) | \ |
| 100 | (unsigned long)tt_scsi_dma.elt##_lo) |
| 101 | |
| 102 | |
| 103 | static inline void SCSI_DMA_SETADR(unsigned long adr) |
| 104 | { |
| 105 | st_dma.dma_lo = (unsigned char)adr; |
| 106 | MFPDELAY(); |
| 107 | adr >>= 8; |
| 108 | st_dma.dma_md = (unsigned char)adr; |
| 109 | MFPDELAY(); |
| 110 | adr >>= 8; |
| 111 | st_dma.dma_hi = (unsigned char)adr; |
| 112 | MFPDELAY(); |
| 113 | } |
| 114 | |
| 115 | static inline unsigned long SCSI_DMA_GETADR(void) |
| 116 | { |
| 117 | unsigned long adr; |
| 118 | adr = st_dma.dma_lo; |
| 119 | MFPDELAY(); |
| 120 | adr |= (st_dma.dma_md & 0xff) << 8; |
| 121 | MFPDELAY(); |
| 122 | adr |= (st_dma.dma_hi & 0xff) << 16; |
| 123 | MFPDELAY(); |
| 124 | return adr; |
| 125 | } |
| 126 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 127 | static void atari_scsi_fetch_restbytes(void); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 129 | static unsigned char (*atari_scsi_reg_read)(unsigned char reg); |
| 130 | static void (*atari_scsi_reg_write)(unsigned char reg, unsigned char value); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | static unsigned long atari_dma_residual, atari_dma_startaddr; |
| 133 | static short atari_dma_active; |
| 134 | /* pointer to the dribble buffer */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 135 | static char *atari_dma_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 136 | /* precalculated physical address of the dribble buffer */ |
| 137 | static unsigned long atari_dma_phys_buffer; |
| 138 | /* != 0 tells the Falcon int handler to copy data from the dribble buffer */ |
| 139 | static char *atari_dma_orig_addr; |
| 140 | /* size of the dribble buffer; 4k seems enough, since the Falcon cannot use |
| 141 | * scatter-gather anyway, so most transfers are 1024 byte only. In the rare |
| 142 | * cases where requests to physical contiguous buffers have been merged, this |
| 143 | * request is <= 4k (one page). So I don't think we have to split transfers |
| 144 | * just due to this buffer size... |
| 145 | */ |
| 146 | #define STRAM_BUFFER_SIZE (4096) |
| 147 | /* mask for address bits that can't be used with the ST-DMA */ |
| 148 | static unsigned long atari_dma_stram_mask; |
| 149 | #define STRAM_ADDR(a) (((a) & atari_dma_stram_mask) == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
| 151 | static int setup_can_queue = -1; |
Rusty Russell | 8d3b33f | 2006-03-25 03:07:05 -0800 | [diff] [blame] | 152 | module_param(setup_can_queue, int, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | static int setup_cmd_per_lun = -1; |
Rusty Russell | 8d3b33f | 2006-03-25 03:07:05 -0800 | [diff] [blame] | 154 | module_param(setup_cmd_per_lun, int, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | static int setup_sg_tablesize = -1; |
Rusty Russell | 8d3b33f | 2006-03-25 03:07:05 -0800 | [diff] [blame] | 156 | module_param(setup_sg_tablesize, int, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | static int setup_hostid = -1; |
Rusty Russell | 8d3b33f | 2006-03-25 03:07:05 -0800 | [diff] [blame] | 158 | module_param(setup_hostid, int, 0); |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 159 | static int setup_toshiba_delay = -1; |
| 160 | module_param(setup_toshiba_delay, int, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
| 162 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 163 | static int scsi_dma_is_ignored_buserr(unsigned char dma_stat) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | { |
| 165 | int i; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 166 | unsigned long addr = SCSI_DMA_READ_P(dma_addr), end_addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
| 168 | if (dma_stat & 0x01) { |
| 169 | |
| 170 | /* A bus error happens when DMA-ing from the last page of a |
| 171 | * physical memory chunk (DMA prefetch!), but that doesn't hurt. |
| 172 | * Check for this case: |
| 173 | */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 174 | |
| 175 | for (i = 0; i < m68k_num_memory; ++i) { |
| 176 | end_addr = m68k_memory[i].addr + m68k_memory[i].size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | if (end_addr <= addr && addr <= end_addr + 4) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 178 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | } |
| 180 | } |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 181 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | |
| 185 | #if 0 |
| 186 | /* Dead code... wasn't called anyway :-) and causes some trouble, because at |
| 187 | * end-of-DMA, both SCSI ints are triggered simultaneously, so the NCR int has |
| 188 | * to clear the DMA int pending bit before it allows other level 6 interrupts. |
| 189 | */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 190 | static void scsi_dma_buserr(int irq, void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 192 | unsigned char dma_stat = tt_scsi_dma.dma_ctrl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
| 194 | /* Don't do anything if a NCR interrupt is pending. Probably it's just |
| 195 | * masked... */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 196 | if (atari_irq_pending(IRQ_TT_MFP_SCSI)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | return; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | printk("Bad SCSI DMA interrupt! dma_addr=0x%08lx dma_stat=%02x dma_cnt=%08lx\n", |
| 200 | SCSI_DMA_READ_P(dma_addr), dma_stat, SCSI_DMA_READ_P(dma_cnt)); |
| 201 | if (dma_stat & 0x80) { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 202 | if (!scsi_dma_is_ignored_buserr(dma_stat)) |
| 203 | printk("SCSI DMA bus error -- bad DMA programming!\n"); |
| 204 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | /* Under normal circumstances we never should get to this point, |
| 206 | * since both interrupts are triggered simultaneously and the 5380 |
| 207 | * int has higher priority. When this irq is handled, that DMA |
| 208 | * interrupt is cleared. So a warning message is printed here. |
| 209 | */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 210 | printk("SCSI DMA intr ?? -- this shouldn't happen!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | #endif |
| 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | |
Finn Thain | cd46140 | 2016-01-03 16:06:06 +1100 | [diff] [blame] | 216 | static irqreturn_t scsi_tt_intr(int irq, void *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 217 | { |
Finn Thain | cd46140 | 2016-01-03 16:06:06 +1100 | [diff] [blame] | 218 | struct Scsi_Host *instance = dev; |
| 219 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | int dma_stat; |
| 221 | |
| 222 | dma_stat = tt_scsi_dma.dma_ctrl; |
| 223 | |
Finn Thain | cd46140 | 2016-01-03 16:06:06 +1100 | [diff] [blame] | 224 | dsprintk(NDEBUG_INTR, instance, "NCR5380 interrupt, DMA status = %02x\n", |
| 225 | dma_stat & 0xff); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | |
| 227 | /* Look if it was the DMA that has interrupted: First possibility |
| 228 | * is that a bus error occurred... |
| 229 | */ |
| 230 | if (dma_stat & 0x80) { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 231 | if (!scsi_dma_is_ignored_buserr(dma_stat)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | printk(KERN_ERR "SCSI DMA caused bus error near 0x%08lx\n", |
| 233 | SCSI_DMA_READ_P(dma_addr)); |
| 234 | printk(KERN_CRIT "SCSI DMA bus error -- bad DMA programming!"); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | /* If the DMA is active but not finished, we have the case |
| 239 | * that some other 5380 interrupt occurred within the DMA transfer. |
| 240 | * This means we have residual bytes, if the desired end address |
| 241 | * is not yet reached. Maybe we have to fetch some bytes from the |
| 242 | * rest data register, too. The residual must be calculated from |
| 243 | * the address pointer, not the counter register, because only the |
| 244 | * addr reg counts bytes not yet written and pending in the rest |
| 245 | * data reg! |
| 246 | */ |
| 247 | if ((dma_stat & 0x02) && !(dma_stat & 0x40)) { |
Finn Thain | cd46140 | 2016-01-03 16:06:06 +1100 | [diff] [blame] | 248 | atari_dma_residual = hostdata->dma_len - |
| 249 | (SCSI_DMA_READ_P(dma_addr) - atari_dma_startaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 251 | dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 252 | atari_dma_residual); |
| 253 | |
| 254 | if ((signed int)atari_dma_residual < 0) |
| 255 | atari_dma_residual = 0; |
| 256 | if ((dma_stat & 1) == 0) { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 257 | /* |
| 258 | * After read operations, we maybe have to |
| 259 | * transport some rest bytes |
| 260 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | atari_scsi_fetch_restbytes(); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 262 | } else { |
| 263 | /* |
| 264 | * There seems to be a nasty bug in some SCSI-DMA/NCR |
| 265 | * combinations: If a target disconnects while a write |
| 266 | * operation is going on, the address register of the |
| 267 | * DMA may be a few bytes farer than it actually read. |
| 268 | * This is probably due to DMA prefetching and a delay |
| 269 | * between DMA and NCR. Experiments showed that the |
| 270 | * dma_addr is 9 bytes to high, but this could vary. |
| 271 | * The problem is, that the residual is thus calculated |
| 272 | * wrong and the next transfer will start behind where |
| 273 | * it should. So we round up the residual to the next |
| 274 | * multiple of a sector size, if it isn't already a |
| 275 | * multiple and the originally expected transfer size |
| 276 | * was. The latter condition is there to ensure that |
| 277 | * the correction is taken only for "real" data |
| 278 | * transfers and not for, e.g., the parameters of some |
| 279 | * other command. These shouldn't disconnect anyway. |
| 280 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | if (atari_dma_residual & 0x1ff) { |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 282 | dprintk(NDEBUG_DMA, "SCSI DMA: DMA bug corrected, " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | "difference %ld bytes\n", |
| 284 | 512 - (atari_dma_residual & 0x1ff)); |
| 285 | atari_dma_residual = (atari_dma_residual + 511) & ~0x1ff; |
| 286 | } |
| 287 | } |
| 288 | tt_scsi_dma.dma_ctrl = 0; |
| 289 | } |
| 290 | |
| 291 | /* If the DMA is finished, fetch the rest bytes and turn it off */ |
| 292 | if (dma_stat & 0x40) { |
| 293 | atari_dma_residual = 0; |
| 294 | if ((dma_stat & 1) == 0) |
| 295 | atari_scsi_fetch_restbytes(); |
| 296 | tt_scsi_dma.dma_ctrl = 0; |
| 297 | } |
| 298 | |
Finn Thain | cd46140 | 2016-01-03 16:06:06 +1100 | [diff] [blame] | 299 | NCR5380_intr(irq, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | return IRQ_HANDLED; |
| 302 | } |
| 303 | |
| 304 | |
Finn Thain | cd46140 | 2016-01-03 16:06:06 +1100 | [diff] [blame] | 305 | static irqreturn_t scsi_falcon_intr(int irq, void *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | { |
Finn Thain | cd46140 | 2016-01-03 16:06:06 +1100 | [diff] [blame] | 307 | struct Scsi_Host *instance = dev; |
| 308 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | int dma_stat; |
| 310 | |
| 311 | /* Turn off DMA and select sector counter register before |
| 312 | * accessing the status register (Atari recommendation!) |
| 313 | */ |
| 314 | st_dma.dma_mode_status = 0x90; |
| 315 | dma_stat = st_dma.dma_mode_status; |
| 316 | |
| 317 | /* Bit 0 indicates some error in the DMA process... don't know |
| 318 | * what happened exactly (no further docu). |
| 319 | */ |
| 320 | if (!(dma_stat & 0x01)) { |
| 321 | /* DMA error */ |
| 322 | printk(KERN_CRIT "SCSI DMA error near 0x%08lx!\n", SCSI_DMA_GETADR()); |
| 323 | } |
| 324 | |
| 325 | /* If the DMA was active, but now bit 1 is not clear, it is some |
| 326 | * other 5380 interrupt that finishes the DMA transfer. We have to |
| 327 | * calculate the number of residual bytes and give a warning if |
| 328 | * bytes are stuck in the ST-DMA fifo (there's no way to reach them!) |
| 329 | */ |
| 330 | if (atari_dma_active && (dma_stat & 0x02)) { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 331 | unsigned long transferred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
| 333 | transferred = SCSI_DMA_GETADR() - atari_dma_startaddr; |
| 334 | /* The ST-DMA address is incremented in 2-byte steps, but the |
| 335 | * data are written only in 16-byte chunks. If the number of |
| 336 | * transferred bytes is not divisible by 16, the remainder is |
| 337 | * lost somewhere in outer space. |
| 338 | */ |
| 339 | if (transferred & 15) |
| 340 | printk(KERN_ERR "SCSI DMA error: %ld bytes lost in " |
| 341 | "ST-DMA fifo\n", transferred & 15); |
| 342 | |
Finn Thain | cd46140 | 2016-01-03 16:06:06 +1100 | [diff] [blame] | 343 | atari_dma_residual = hostdata->dma_len - transferred; |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 344 | dprintk(NDEBUG_DMA, "SCSI DMA: There are %ld residual bytes.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | atari_dma_residual); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 346 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | atari_dma_residual = 0; |
| 348 | atari_dma_active = 0; |
| 349 | |
| 350 | if (atari_dma_orig_addr) { |
| 351 | /* If the dribble buffer was used on a read operation, copy the DMA-ed |
| 352 | * data to the original destination address. |
| 353 | */ |
| 354 | memcpy(atari_dma_orig_addr, phys_to_virt(atari_dma_startaddr), |
Finn Thain | cd46140 | 2016-01-03 16:06:06 +1100 | [diff] [blame] | 355 | hostdata->dma_len - atari_dma_residual); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | atari_dma_orig_addr = NULL; |
| 357 | } |
| 358 | |
Finn Thain | cd46140 | 2016-01-03 16:06:06 +1100 | [diff] [blame] | 359 | NCR5380_intr(irq, dev); |
| 360 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | return IRQ_HANDLED; |
| 362 | } |
| 363 | |
| 364 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 365 | static void atari_scsi_fetch_restbytes(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | { |
| 367 | int nr; |
| 368 | char *src, *dst; |
| 369 | unsigned long phys_dst; |
| 370 | |
| 371 | /* fetch rest bytes in the DMA register */ |
| 372 | phys_dst = SCSI_DMA_READ_P(dma_addr); |
| 373 | nr = phys_dst & 3; |
| 374 | if (nr) { |
| 375 | /* there are 'nr' bytes left for the last long address |
| 376 | before the DMA pointer */ |
| 377 | phys_dst ^= nr; |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 378 | dprintk(NDEBUG_DMA, "SCSI DMA: there are %d rest bytes for phys addr 0x%08lx", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | nr, phys_dst); |
| 380 | /* The content of the DMA pointer is a physical address! */ |
| 381 | dst = phys_to_virt(phys_dst); |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 382 | dprintk(NDEBUG_DMA, " = virt addr %p\n", dst); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | for (src = (char *)&tt_scsi_dma.dma_restdata; nr != 0; --nr) |
| 384 | *dst++ = *src++; |
| 385 | } |
| 386 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | |
| 388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | /* This function releases the lock on the DMA chip if there is no |
Finn Thain | 16b29e7 | 2014-11-12 16:12:08 +1100 | [diff] [blame] | 390 | * connected command and the disconnected queue is empty. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | */ |
| 392 | |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 393 | static void falcon_release_lock(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 395 | if (IS_A_TT()) |
| 396 | return; |
| 397 | |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 398 | if (stdma_is_locked_by(scsi_falcon_intr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 399 | stdma_release(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /* This function manages the locking of the ST-DMA. |
| 403 | * If the DMA isn't locked already for SCSI, it tries to lock it by |
| 404 | * calling stdma_lock(). But if the DMA is locked by the SCSI code and |
| 405 | * there are other drivers waiting for the chip, we do not issue the |
Finn Thain | 16b29e7 | 2014-11-12 16:12:08 +1100 | [diff] [blame] | 406 | * command immediately but tell the SCSI mid-layer to defer. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | */ |
| 408 | |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 409 | static int falcon_get_lock(struct Scsi_Host *instance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 411 | if (IS_A_TT()) |
Finn Thain | 16b29e7 | 2014-11-12 16:12:08 +1100 | [diff] [blame] | 412 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | |
Finn Thain | ded155b | 2016-03-23 21:10:30 +1100 | [diff] [blame] | 414 | if (stdma_is_locked_by(scsi_falcon_intr) && |
| 415 | instance->hostt->can_queue > 1) |
| 416 | return 1; |
| 417 | |
Finn Thain | 16b29e7 | 2014-11-12 16:12:08 +1100 | [diff] [blame] | 418 | if (in_interrupt()) |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 419 | return stdma_try_lock(scsi_falcon_intr, instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 421 | stdma_lock(scsi_falcon_intr, instance); |
Finn Thain | 16b29e7 | 2014-11-12 16:12:08 +1100 | [diff] [blame] | 422 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | } |
| 424 | |
Geert Uytterhoeven | 7b54e43 | 2012-03-18 11:54:40 +0100 | [diff] [blame] | 425 | #ifndef MODULE |
| 426 | static int __init atari_scsi_setup(char *str) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | { |
| 428 | /* Format of atascsi parameter is: |
| 429 | * atascsi=<can_queue>,<cmd_per_lun>,<sg_tablesize>,<hostid>,<use_tags> |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 430 | * Defaults depend on TT or Falcon, determined at run time. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | * Negative values mean don't change. |
| 432 | */ |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 433 | int ints[8]; |
Geert Uytterhoeven | 7b54e43 | 2012-03-18 11:54:40 +0100 | [diff] [blame] | 434 | |
| 435 | get_options(str, ARRAY_SIZE(ints), ints); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 436 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | if (ints[0] < 1) { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 438 | printk("atari_scsi_setup: no arguments!\n"); |
Geert Uytterhoeven | 7b54e43 | 2012-03-18 11:54:40 +0100 | [diff] [blame] | 439 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | } |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 441 | if (ints[0] >= 1) |
| 442 | setup_can_queue = ints[1]; |
| 443 | if (ints[0] >= 2) |
| 444 | setup_cmd_per_lun = ints[2]; |
| 445 | if (ints[0] >= 3) |
| 446 | setup_sg_tablesize = ints[3]; |
| 447 | if (ints[0] >= 4) |
| 448 | setup_hostid = ints[4]; |
Finn Thain | c4ec6f9 | 2016-03-23 21:10:22 +1100 | [diff] [blame] | 449 | /* ints[5] (use_tagged_queuing) is ignored */ |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 450 | /* ints[6] (use_pdma) is ignored */ |
| 451 | if (ints[0] >= 7) |
| 452 | setup_toshiba_delay = ints[7]; |
Geert Uytterhoeven | 7b54e43 | 2012-03-18 11:54:40 +0100 | [diff] [blame] | 453 | |
| 454 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | } |
| 456 | |
Geert Uytterhoeven | 7b54e43 | 2012-03-18 11:54:40 +0100 | [diff] [blame] | 457 | __setup("atascsi=", atari_scsi_setup); |
| 458 | #endif /* !MODULE */ |
| 459 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 460 | |
Geert Uytterhoeven | 107b5d5 | 2011-06-12 20:48:33 +0200 | [diff] [blame] | 461 | static unsigned long atari_scsi_dma_setup(struct Scsi_Host *instance, |
| 462 | void *data, unsigned long count, |
| 463 | int dir) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 465 | unsigned long addr = virt_to_phys(data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 467 | dprintk(NDEBUG_DMA, "scsi%d: setting up dma, data = %p, phys = %lx, count = %ld, " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | "dir = %d\n", instance->host_no, data, addr, count, dir); |
| 469 | |
| 470 | if (!IS_A_TT() && !STRAM_ADDR(addr)) { |
| 471 | /* If we have a non-DMAable address on a Falcon, use the dribble |
| 472 | * buffer; 'orig_addr' != 0 in the read case tells the interrupt |
| 473 | * handler to copy data from the dribble buffer to the originally |
| 474 | * wanted address. |
| 475 | */ |
| 476 | if (dir) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 477 | memcpy(atari_dma_buffer, data, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | else |
| 479 | atari_dma_orig_addr = data; |
| 480 | addr = atari_dma_phys_buffer; |
| 481 | } |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | atari_dma_startaddr = addr; /* Needed for calculating residual later. */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 484 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | /* Cache cleanup stuff: On writes, push any dirty cache out before sending |
| 486 | * it to the peripheral. (Must be done before DMA setup, since at least |
| 487 | * the ST-DMA begins to fill internal buffers right after setup. For |
| 488 | * reads, invalidate any cache, may be altered after DMA without CPU |
| 489 | * knowledge. |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 490 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | * ++roman: For the Medusa, there's no need at all for that cache stuff, |
| 492 | * because the hardware does bus snooping (fine!). |
| 493 | */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 494 | dma_cache_maintenance(addr, count, dir); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | if (IS_A_TT()) { |
| 497 | tt_scsi_dma.dma_ctrl = dir; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 498 | SCSI_DMA_WRITE_P(dma_addr, addr); |
| 499 | SCSI_DMA_WRITE_P(dma_cnt, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | tt_scsi_dma.dma_ctrl = dir | 2; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 501 | } else { /* ! IS_A_TT */ |
| 502 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | /* set address */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 504 | SCSI_DMA_SETADR(addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | |
| 506 | /* toggle direction bit to clear FIFO and set DMA direction */ |
| 507 | dir <<= 8; |
| 508 | st_dma.dma_mode_status = 0x90 | dir; |
| 509 | st_dma.dma_mode_status = 0x90 | (dir ^ 0x100); |
| 510 | st_dma.dma_mode_status = 0x90 | dir; |
| 511 | udelay(40); |
| 512 | /* On writes, round up the transfer length to the next multiple of 512 |
| 513 | * (see also comment at atari_dma_xfer_len()). */ |
| 514 | st_dma.fdc_acces_seccount = (count + (dir ? 511 : 0)) >> 9; |
| 515 | udelay(40); |
| 516 | st_dma.dma_mode_status = 0x10 | dir; |
| 517 | udelay(40); |
| 518 | /* need not restore value of dir, only boolean value is tested */ |
| 519 | atari_dma_active = 1; |
| 520 | } |
| 521 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 522 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 526 | static long atari_scsi_dma_residual(struct Scsi_Host *instance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 528 | return atari_dma_residual; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | |
| 532 | #define CMD_SURELY_BLOCK_MODE 0 |
| 533 | #define CMD_SURELY_BYTE_MODE 1 |
| 534 | #define CMD_MODE_UNKNOWN 2 |
| 535 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 536 | static int falcon_classify_cmd(struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | { |
| 538 | unsigned char opcode = cmd->cmnd[0]; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | if (opcode == READ_DEFECT_DATA || opcode == READ_LONG || |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 541 | opcode == READ_BUFFER) |
| 542 | return CMD_SURELY_BYTE_MODE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | else if (opcode == READ_6 || opcode == READ_10 || |
| 544 | opcode == 0xa8 /* READ_12 */ || opcode == READ_REVERSE || |
| 545 | opcode == RECOVER_BUFFERED_DATA) { |
| 546 | /* In case of a sequential-access target (tape), special care is |
| 547 | * needed here: The transfer is block-mode only if the 'fixed' bit is |
| 548 | * set! */ |
| 549 | if (cmd->device->type == TYPE_TAPE && !(cmd->cmnd[1] & 1)) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 550 | return CMD_SURELY_BYTE_MODE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | else |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 552 | return CMD_SURELY_BLOCK_MODE; |
| 553 | } else |
| 554 | return CMD_MODE_UNKNOWN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | |
| 558 | /* This function calculates the number of bytes that can be transferred via |
| 559 | * DMA. On the TT, this is arbitrary, but on the Falcon we have to use the |
| 560 | * ST-DMA chip. There are only multiples of 512 bytes possible and max. |
| 561 | * 255*512 bytes :-( This means also, that defining READ_OVERRUNS is not |
| 562 | * possible on the Falcon, since that would require to program the DMA for |
| 563 | * n*512 - atari_read_overrun bytes. But it seems that the Falcon doesn't have |
| 564 | * the overrun problem, so this question is academic :-) |
| 565 | */ |
| 566 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 567 | static unsigned long atari_dma_xfer_len(unsigned long wanted_len, |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 568 | struct scsi_cmnd *cmd, int write_flag) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | { |
| 570 | unsigned long possible_len, limit; |
Adrian Bunk | 29c8a24 | 2008-10-13 21:58:59 +0200 | [diff] [blame] | 571 | |
Finn Thain | e63449c | 2016-03-23 21:10:13 +1100 | [diff] [blame] | 572 | if (wanted_len < DMA_MIN_SIZE) |
| 573 | return 0; |
| 574 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | if (IS_A_TT()) |
| 576 | /* TT SCSI DMA can transfer arbitrary #bytes */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 577 | return wanted_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | |
| 579 | /* ST DMA chip is stupid -- only multiples of 512 bytes! (and max. |
| 580 | * 255*512 bytes, but this should be enough) |
| 581 | * |
| 582 | * ++roman: Aaargl! Another Falcon-SCSI problem... There are some commands |
| 583 | * that return a number of bytes which cannot be known beforehand. In this |
| 584 | * case, the given transfer length is an "allocation length". Now it |
| 585 | * can happen that this allocation length is a multiple of 512 bytes and |
| 586 | * the DMA is used. But if not n*512 bytes really arrive, some input data |
| 587 | * will be lost in the ST-DMA's FIFO :-( Thus, we have to distinguish |
| 588 | * between commands that do block transfers and those that do byte |
| 589 | * transfers. But this isn't easy... there are lots of vendor specific |
| 590 | * commands, and the user can issue any command via the |
| 591 | * SCSI_IOCTL_SEND_COMMAND. |
| 592 | * |
| 593 | * The solution: We classify SCSI commands in 1) surely block-mode cmd.s, |
| 594 | * 2) surely byte-mode cmd.s and 3) cmd.s with unknown mode. In case 1) |
| 595 | * and 3), the thing to do is obvious: allow any number of blocks via DMA |
| 596 | * or none. In case 2), we apply some heuristic: Byte mode is assumed if |
| 597 | * the transfer (allocation) length is < 1024, hoping that no cmd. not |
| 598 | * explicitly known as byte mode have such big allocation lengths... |
| 599 | * BTW, all the discussion above applies only to reads. DMA writes are |
| 600 | * unproblematic anyways, since the targets aborts the transfer after |
| 601 | * receiving a sufficient number of bytes. |
| 602 | * |
| 603 | * Another point: If the transfer is from/to an non-ST-RAM address, we |
| 604 | * use the dribble buffer and thus can do only STRAM_BUFFER_SIZE bytes. |
| 605 | */ |
| 606 | |
| 607 | if (write_flag) { |
| 608 | /* Write operation can always use the DMA, but the transfer size must |
| 609 | * be rounded up to the next multiple of 512 (atari_dma_setup() does |
| 610 | * this). |
| 611 | */ |
| 612 | possible_len = wanted_len; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 613 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | /* Read operations: if the wanted transfer length is not a multiple of |
| 615 | * 512, we cannot use DMA, since the ST-DMA cannot split transfers |
| 616 | * (no interrupt on DMA finished!) |
| 617 | */ |
| 618 | if (wanted_len & 0x1ff) |
| 619 | possible_len = 0; |
| 620 | else { |
| 621 | /* Now classify the command (see above) and decide whether it is |
| 622 | * allowed to do DMA at all */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 623 | switch (falcon_classify_cmd(cmd)) { |
| 624 | case CMD_SURELY_BLOCK_MODE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | possible_len = wanted_len; |
| 626 | break; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 627 | case CMD_SURELY_BYTE_MODE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | possible_len = 0; /* DMA prohibited */ |
| 629 | break; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 630 | case CMD_MODE_UNKNOWN: |
| 631 | default: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 632 | /* For unknown commands assume block transfers if the transfer |
| 633 | * size/allocation length is >= 1024 */ |
| 634 | possible_len = (wanted_len < 1024) ? 0 : wanted_len; |
| 635 | break; |
| 636 | } |
| 637 | } |
| 638 | } |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 639 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | /* Last step: apply the hard limit on DMA transfers */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 641 | limit = (atari_dma_buffer && !STRAM_ADDR(virt_to_phys(cmd->SCp.ptr))) ? |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 642 | STRAM_BUFFER_SIZE : 255*512; |
| 643 | if (possible_len > limit) |
| 644 | possible_len = limit; |
| 645 | |
| 646 | if (possible_len != wanted_len) |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 647 | dprintk(NDEBUG_DMA, "Sorry, must cut DMA transfer size to %ld bytes " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | "instead of %ld\n", possible_len, wanted_len); |
| 649 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 650 | return possible_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 654 | /* NCR5380 register access functions |
| 655 | * |
| 656 | * There are separate functions for TT and Falcon, because the access |
| 657 | * methods are quite different. The calling macros NCR5380_read and |
| 658 | * NCR5380_write call these functions via function pointers. |
| 659 | */ |
| 660 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 661 | static unsigned char atari_scsi_tt_reg_read(unsigned char reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 663 | return tt_scsi_regp[reg * 2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | } |
| 665 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 666 | static void atari_scsi_tt_reg_write(unsigned char reg, unsigned char value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | { |
| 668 | tt_scsi_regp[reg * 2] = value; |
| 669 | } |
| 670 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 671 | static unsigned char atari_scsi_falcon_reg_read(unsigned char reg) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 672 | { |
| 673 | dma_wd.dma_mode_status= (u_short)(0x88 + reg); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 674 | return (u_char)dma_wd.fdc_acces_seccount; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 677 | static void atari_scsi_falcon_reg_write(unsigned char reg, unsigned char value) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 678 | { |
| 679 | dma_wd.dma_mode_status = (u_short)(0x88 + reg); |
| 680 | dma_wd.fdc_acces_seccount = (u_short)value; |
| 681 | } |
| 682 | |
| 683 | |
Finn Thain | 52d3e56 | 2016-03-23 21:10:20 +1100 | [diff] [blame] | 684 | #include "NCR5380.c" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | |
Finn Thain | 4d3d2a5 | 2014-11-12 16:11:52 +1100 | [diff] [blame] | 686 | static int atari_scsi_bus_reset(struct scsi_cmnd *cmd) |
| 687 | { |
| 688 | int rv; |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 689 | unsigned long flags; |
Finn Thain | 4d3d2a5 | 2014-11-12 16:11:52 +1100 | [diff] [blame] | 690 | |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 691 | local_irq_save(flags); |
| 692 | |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 693 | /* Abort a maybe active DMA transfer */ |
Finn Thain | 4d3d2a5 | 2014-11-12 16:11:52 +1100 | [diff] [blame] | 694 | if (IS_A_TT()) { |
Finn Thain | 4d3d2a5 | 2014-11-12 16:11:52 +1100 | [diff] [blame] | 695 | tt_scsi_dma.dma_ctrl = 0; |
Finn Thain | 4d3d2a5 | 2014-11-12 16:11:52 +1100 | [diff] [blame] | 696 | } else { |
Finn Thain | 4d3d2a5 | 2014-11-12 16:11:52 +1100 | [diff] [blame] | 697 | st_dma.dma_mode_status = 0x90; |
| 698 | atari_dma_active = 0; |
| 699 | atari_dma_orig_addr = NULL; |
Finn Thain | 4d3d2a5 | 2014-11-12 16:11:52 +1100 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | rv = NCR5380_bus_reset(cmd); |
| 703 | |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 704 | /* The 5380 raises its IRQ line while _RST is active but the ST DMA |
| 705 | * "lock" has been released so this interrupt may end up handled by |
| 706 | * floppy or IDE driver (if one of them holds the lock). The NCR5380 |
| 707 | * interrupt flag has been cleared already. |
| 708 | */ |
Finn Thain | 4d3d2a5 | 2014-11-12 16:11:52 +1100 | [diff] [blame] | 709 | |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 710 | local_irq_restore(flags); |
Finn Thain | 4d3d2a5 | 2014-11-12 16:11:52 +1100 | [diff] [blame] | 711 | |
| 712 | return rv; |
| 713 | } |
| 714 | |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 715 | #define DRV_MODULE_NAME "atari_scsi" |
| 716 | #define PFX DRV_MODULE_NAME ": " |
| 717 | |
| 718 | static struct scsi_host_template atari_scsi_template = { |
| 719 | .module = THIS_MODULE, |
| 720 | .proc_name = DRV_MODULE_NAME, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | .name = "Atari native SCSI", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | .info = atari_scsi_info, |
| 723 | .queuecommand = atari_scsi_queue_command, |
| 724 | .eh_abort_handler = atari_scsi_abort, |
| 725 | .eh_bus_reset_handler = atari_scsi_bus_reset, |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 726 | .this_id = 7, |
Finn Thain | a5217a8 | 2016-03-23 21:10:29 +1100 | [diff] [blame] | 727 | .cmd_per_lun = 2, |
Finn Thain | aa2e2cb | 2016-01-03 16:05:48 +1100 | [diff] [blame] | 728 | .use_clustering = DISABLE_CLUSTERING, |
Finn Thain | 32b26a1 | 2016-01-03 16:05:58 +1100 | [diff] [blame] | 729 | .cmd_size = NCR5380_CMD_SIZE, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | }; |
| 731 | |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 732 | static int __init atari_scsi_probe(struct platform_device *pdev) |
| 733 | { |
| 734 | struct Scsi_Host *instance; |
| 735 | int error; |
| 736 | struct resource *irq; |
Finn Thain | ef1081c | 2014-11-12 16:12:14 +1100 | [diff] [blame] | 737 | int host_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 739 | irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0); |
| 740 | if (!irq) |
| 741 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 742 | |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 743 | if (ATARIHW_PRESENT(TT_SCSI)) { |
| 744 | atari_scsi_reg_read = atari_scsi_tt_reg_read; |
| 745 | atari_scsi_reg_write = atari_scsi_tt_reg_write; |
| 746 | } else { |
| 747 | atari_scsi_reg_read = atari_scsi_falcon_reg_read; |
| 748 | atari_scsi_reg_write = atari_scsi_falcon_reg_write; |
| 749 | } |
| 750 | |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 751 | if (ATARIHW_PRESENT(TT_SCSI)) { |
| 752 | atari_scsi_template.can_queue = 16; |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 753 | atari_scsi_template.sg_tablesize = SG_ALL; |
| 754 | } else { |
Finn Thain | ded155b | 2016-03-23 21:10:30 +1100 | [diff] [blame] | 755 | atari_scsi_template.can_queue = 1; |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 756 | atari_scsi_template.sg_tablesize = SG_NONE; |
| 757 | } |
| 758 | |
| 759 | if (setup_can_queue > 0) |
| 760 | atari_scsi_template.can_queue = setup_can_queue; |
| 761 | |
| 762 | if (setup_cmd_per_lun > 0) |
| 763 | atari_scsi_template.cmd_per_lun = setup_cmd_per_lun; |
| 764 | |
| 765 | /* Leave sg_tablesize at 0 on a Falcon! */ |
| 766 | if (ATARIHW_PRESENT(TT_SCSI) && setup_sg_tablesize >= 0) |
| 767 | atari_scsi_template.sg_tablesize = setup_sg_tablesize; |
| 768 | |
| 769 | if (setup_hostid >= 0) { |
| 770 | atari_scsi_template.this_id = setup_hostid & 7; |
| 771 | } else { |
| 772 | /* Test if a host id is set in the NVRam */ |
| 773 | if (ATARIHW_PRESENT(TT_CLK) && nvram_check_checksum()) { |
Finn Thain | 6225a16 | 2016-01-03 16:05:02 +1100 | [diff] [blame] | 774 | unsigned char b = nvram_read_byte(16); |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 775 | |
| 776 | /* Arbitration enabled? (for TOS) |
| 777 | * If yes, use configured host ID |
| 778 | */ |
| 779 | if (b & 0x80) |
| 780 | atari_scsi_template.this_id = b & 7; |
| 781 | } |
| 782 | } |
| 783 | |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 784 | /* If running on a Falcon and if there's TT-Ram (i.e., more than one |
| 785 | * memory block, since there's always ST-Ram in a Falcon), then |
| 786 | * allocate a STRAM_BUFFER_SIZE byte dribble buffer for transfers |
| 787 | * from/to alternative Ram. |
| 788 | */ |
| 789 | if (ATARIHW_PRESENT(ST_SCSI) && !ATARIHW_PRESENT(EXTD_DMA) && |
| 790 | m68k_num_memory > 1) { |
| 791 | atari_dma_buffer = atari_stram_alloc(STRAM_BUFFER_SIZE, "SCSI"); |
| 792 | if (!atari_dma_buffer) { |
| 793 | pr_err(PFX "can't allocate ST-RAM double buffer\n"); |
| 794 | return -ENOMEM; |
| 795 | } |
| 796 | atari_dma_phys_buffer = atari_stram_to_phys(atari_dma_buffer); |
| 797 | atari_dma_orig_addr = 0; |
| 798 | } |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 799 | |
| 800 | instance = scsi_host_alloc(&atari_scsi_template, |
| 801 | sizeof(struct NCR5380_hostdata)); |
| 802 | if (!instance) { |
| 803 | error = -ENOMEM; |
| 804 | goto fail_alloc; |
| 805 | } |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 806 | |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 807 | instance->irq = irq->start; |
| 808 | |
Finn Thain | ef1081c | 2014-11-12 16:12:14 +1100 | [diff] [blame] | 809 | host_flags |= IS_A_TT() ? 0 : FLAG_LATE_DMA_SETUP; |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 810 | host_flags |= setup_toshiba_delay > 0 ? FLAG_TOSHIBA_DELAY : 0; |
Finn Thain | ca513fc | 2014-11-12 16:12:19 +1100 | [diff] [blame] | 811 | |
Finn Thain | 0ad0eff | 2016-01-03 16:05:21 +1100 | [diff] [blame] | 812 | error = NCR5380_init(instance, host_flags); |
| 813 | if (error) |
| 814 | goto fail_init; |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 815 | |
| 816 | if (IS_A_TT()) { |
| 817 | error = request_irq(instance->irq, scsi_tt_intr, 0, |
| 818 | "NCR5380", instance); |
| 819 | if (error) { |
| 820 | pr_err(PFX "request irq %d failed, aborting\n", |
| 821 | instance->irq); |
| 822 | goto fail_irq; |
| 823 | } |
| 824 | tt_mfp.active_edge |= 0x80; /* SCSI int on L->H */ |
Finn Thain | e4dec68 | 2016-03-23 21:10:12 +1100 | [diff] [blame] | 825 | |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 826 | tt_scsi_dma.dma_ctrl = 0; |
| 827 | atari_dma_residual = 0; |
| 828 | |
| 829 | /* While the read overruns (described by Drew Eckhardt in |
| 830 | * NCR5380.c) never happened on TTs, they do in fact on the |
| 831 | * Medusa (This was the cause why SCSI didn't work right for |
| 832 | * so long there.) Since handling the overruns slows down |
| 833 | * a bit, I turned the #ifdef's into a runtime condition. |
| 834 | * |
| 835 | * In principle it should be sufficient to do max. 1 byte with |
| 836 | * PIO, but there is another problem on the Medusa with the DMA |
Finn Thain | ef1081c | 2014-11-12 16:12:14 +1100 | [diff] [blame] | 837 | * rest data register. So read_overruns is currently set |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 838 | * to 4 to avoid having transfers that aren't a multiple of 4. |
| 839 | * If the rest data bug is fixed, this can be lowered to 1. |
| 840 | */ |
Finn Thain | ef1081c | 2014-11-12 16:12:14 +1100 | [diff] [blame] | 841 | if (MACH_IS_MEDUSA) { |
| 842 | struct NCR5380_hostdata *hostdata = |
| 843 | shost_priv(instance); |
| 844 | |
| 845 | hostdata->read_overruns = 4; |
| 846 | } |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 847 | } else { |
| 848 | /* Nothing to do for the interrupt: the ST-DMA is initialized |
| 849 | * already. |
| 850 | */ |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 851 | atari_dma_residual = 0; |
| 852 | atari_dma_active = 0; |
| 853 | atari_dma_stram_mask = (ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000 |
| 854 | : 0xff000000); |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 855 | } |
| 856 | |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 857 | NCR5380_maybe_reset_bus(instance); |
| 858 | |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 859 | error = scsi_add_host(instance, NULL); |
| 860 | if (error) |
| 861 | goto fail_host; |
| 862 | |
| 863 | platform_set_drvdata(pdev, instance); |
| 864 | |
| 865 | scsi_scan_host(instance); |
| 866 | return 0; |
| 867 | |
| 868 | fail_host: |
| 869 | if (IS_A_TT()) |
| 870 | free_irq(instance->irq, instance); |
| 871 | fail_irq: |
| 872 | NCR5380_exit(instance); |
Finn Thain | 0ad0eff | 2016-01-03 16:05:21 +1100 | [diff] [blame] | 873 | fail_init: |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 874 | scsi_host_put(instance); |
| 875 | fail_alloc: |
| 876 | if (atari_dma_buffer) |
| 877 | atari_stram_free(atari_dma_buffer); |
| 878 | return error; |
| 879 | } |
| 880 | |
| 881 | static int __exit atari_scsi_remove(struct platform_device *pdev) |
| 882 | { |
| 883 | struct Scsi_Host *instance = platform_get_drvdata(pdev); |
| 884 | |
| 885 | scsi_remove_host(instance); |
| 886 | if (IS_A_TT()) |
| 887 | free_irq(instance->irq, instance); |
| 888 | NCR5380_exit(instance); |
| 889 | scsi_host_put(instance); |
| 890 | if (atari_dma_buffer) |
| 891 | atari_stram_free(atari_dma_buffer); |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | static struct platform_driver atari_scsi_driver = { |
| 896 | .remove = __exit_p(atari_scsi_remove), |
| 897 | .driver = { |
| 898 | .name = DRV_MODULE_NAME, |
Finn Thain | 3ff228a | 2014-11-12 16:12:09 +1100 | [diff] [blame] | 899 | }, |
| 900 | }; |
| 901 | |
| 902 | module_platform_driver_probe(atari_scsi_driver, atari_scsi_probe); |
| 903 | |
| 904 | MODULE_ALIAS("platform:" DRV_MODULE_NAME); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | MODULE_LICENSE("GPL"); |