Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * NCR 5380 generic driver routines. These should make it *trivial* |
| 3 | * to implement 5380 SCSI drivers under Linux with a non-trantor |
| 4 | * architecture. |
| 5 | * |
| 6 | * Note that these routines also work with NR53c400 family chips. |
| 7 | * |
| 8 | * Copyright 1993, Drew Eckhardt |
| 9 | * Visionary Computing |
| 10 | * (Unix and Linux consulting and custom programming) |
| 11 | * drew@colorado.edu |
| 12 | * +1 (303) 666-5836 |
| 13 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | * For more information, please consult |
| 15 | * |
| 16 | * NCR 5380 Family |
| 17 | * SCSI Protocol Controller |
| 18 | * Databook |
| 19 | * |
| 20 | * NCR Microelectronics |
| 21 | * 1635 Aeroplaza Drive |
| 22 | * Colorado Springs, CO 80916 |
| 23 | * 1+ (719) 578-3400 |
| 24 | * 1+ (800) 334-5454 |
| 25 | */ |
| 26 | |
| 27 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | * Revision 1.10 1998/9/2 Alan Cox |
Alan Cox | fa195af | 2008-10-27 15:16:36 +0000 | [diff] [blame] | 29 | * (alan@lxorguk.ukuu.org.uk) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | * Fixed up the timer lockups reported so far. Things still suck. Looking |
| 31 | * forward to 2.3 and per device request queues. Then it'll be possible to |
| 32 | * SMP thread this beast and improve life no end. |
| 33 | |
| 34 | * Revision 1.9 1997/7/27 Ronald van Cuijlenborg |
| 35 | * (ronald.van.cuijlenborg@tip.nl or nutty@dds.nl) |
| 36 | * (hopefully) fixed and enhanced USLEEP |
| 37 | * added support for DTC3181E card (for Mustek scanner) |
| 38 | * |
| 39 | |
| 40 | * Revision 1.8 Ingmar Baumgart |
| 41 | * (ingmar@gonzo.schwaben.de) |
| 42 | * added support for NCR53C400a card |
| 43 | * |
| 44 | |
| 45 | * Revision 1.7 1996/3/2 Ray Van Tassle (rayvt@comm.mot.com) |
| 46 | * added proc_info |
| 47 | * added support needed for DTC 3180/3280 |
| 48 | * fixed a couple of bugs |
| 49 | * |
| 50 | |
| 51 | * Revision 1.5 1994/01/19 09:14:57 drew |
| 52 | * Fixed udelay() hack that was being used on DATAOUT phases |
| 53 | * instead of a proper wait for the final handshake. |
| 54 | * |
| 55 | * Revision 1.4 1994/01/19 06:44:25 drew |
| 56 | * *** empty log message *** |
| 57 | * |
| 58 | * Revision 1.3 1994/01/19 05:24:40 drew |
| 59 | * Added support for TCR LAST_BYTE_SENT bit. |
| 60 | * |
| 61 | * Revision 1.2 1994/01/15 06:14:11 drew |
| 62 | * REAL DMA support, bug fixes. |
| 63 | * |
| 64 | * Revision 1.1 1994/01/15 06:00:54 drew |
| 65 | * Initial revision |
| 66 | * |
| 67 | */ |
| 68 | |
| 69 | /* |
| 70 | * Further development / testing that should be done : |
| 71 | * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete |
| 72 | * code so that everything does the same thing that's done at the |
| 73 | * end of a pseudo-DMA read operation. |
| 74 | * |
| 75 | * 2. Fix REAL_DMA (interrupt driven, polled works fine) - |
| 76 | * basically, transfer size needs to be reduced by one |
| 77 | * and the last byte read as is done with PSEUDO_DMA. |
| 78 | * |
| 79 | * 4. Test SCSI-II tagged queueing (I have no devices which support |
| 80 | * tagged queueing) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | */ |
| 82 | |
| 83 | #if (NDEBUG & NDEBUG_LISTS) |
| 84 | #define LIST(x,y) {printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); } |
| 85 | #define REMOVE(w,x,y,z) {printk("LINE:%d Removing: %p->%p %p->%p \n", __LINE__, (void*)(w), (void*)(x), (void*)(y), (void*)(z)); if ((x)==(y)) udelay(5); } |
| 86 | #else |
| 87 | #define LIST(x,y) |
| 88 | #define REMOVE(w,x,y,z) |
| 89 | #endif |
| 90 | |
| 91 | #ifndef notyet |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | #undef REAL_DMA |
| 93 | #endif |
| 94 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | #ifdef BOARD_REQUIRES_NO_DELAY |
| 96 | #define io_recovery_delay(x) |
| 97 | #else |
| 98 | #define io_recovery_delay(x) udelay(x) |
| 99 | #endif |
| 100 | |
| 101 | /* |
| 102 | * Design |
| 103 | * |
| 104 | * This is a generic 5380 driver. To use it on a different platform, |
| 105 | * one simply writes appropriate system specific macros (ie, data |
| 106 | * transfer - some PC's will use the I/O bus, 68K's must use |
| 107 | * memory mapped) and drops this file in their 'C' wrapper. |
| 108 | * |
| 109 | * (Note from hch: unfortunately it was not enough for the different |
| 110 | * m68k folks and instead of improving this driver they copied it |
| 111 | * and hacked it up for their needs. As a consequence they lost |
| 112 | * most updates to this driver. Maybe someone will fix all these |
| 113 | * drivers to use a common core one day..) |
| 114 | * |
| 115 | * As far as command queueing, two queues are maintained for |
| 116 | * each 5380 in the system - commands that haven't been issued yet, |
| 117 | * and commands that are currently executing. This means that an |
| 118 | * unlimited number of commands may be queued, letting |
| 119 | * more commands propagate from the higher driver levels giving higher |
| 120 | * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported, |
| 121 | * allowing multiple commands to propagate all the way to a SCSI-II device |
| 122 | * while a command is already executing. |
| 123 | * |
| 124 | * |
| 125 | * Issues specific to the NCR5380 : |
| 126 | * |
| 127 | * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead |
| 128 | * piece of hardware that requires you to sit in a loop polling for |
| 129 | * the REQ signal as long as you are connected. Some devices are |
| 130 | * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect |
Finn Thain | 686f399 | 2016-01-03 16:05:26 +1100 | [diff] [blame] | 131 | * while doing long seek operations. [...] These |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | * broken devices are the exception rather than the rule and I'd rather |
| 133 | * spend my time optimizing for the normal case. |
| 134 | * |
| 135 | * Architecture : |
| 136 | * |
| 137 | * At the heart of the design is a coroutine, NCR5380_main, |
| 138 | * which is started from a workqueue for each NCR5380 host in the |
| 139 | * system. It attempts to establish I_T_L or I_T_L_Q nexuses by |
| 140 | * removing the commands from the issue queue and calling |
| 141 | * NCR5380_select() if a nexus is not established. |
| 142 | * |
| 143 | * Once a nexus is established, the NCR5380_information_transfer() |
| 144 | * phase goes through the various phases as instructed by the target. |
| 145 | * if the target goes into MSG IN and sends a DISCONNECT message, |
| 146 | * the command structure is placed into the per instance disconnected |
| 147 | * queue, and NCR5380_main tries to find more work. If the target is |
| 148 | * idle for too long, the system will try to sleep. |
| 149 | * |
| 150 | * If a command has disconnected, eventually an interrupt will trigger, |
| 151 | * calling NCR5380_intr() which will in turn call NCR5380_reselect |
| 152 | * to reestablish a nexus. This will run main if necessary. |
| 153 | * |
| 154 | * On command termination, the done function will be called as |
| 155 | * appropriate. |
| 156 | * |
| 157 | * SCSI pointers are maintained in the SCp field of SCSI command |
| 158 | * structures, being initialized after the command is connected |
| 159 | * in NCR5380_select, and set as appropriate in NCR5380_information_transfer. |
| 160 | * Note that in violation of the standard, an implicit SAVE POINTERS operation |
| 161 | * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS. |
| 162 | */ |
| 163 | |
| 164 | /* |
| 165 | * Using this file : |
| 166 | * This file a skeleton Linux SCSI driver for the NCR 5380 series |
| 167 | * of chips. To use it, you write an architecture specific functions |
| 168 | * and macros and include this file in your driver. |
| 169 | * |
| 170 | * These macros control options : |
| 171 | * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be |
| 172 | * defined. |
| 173 | * |
| 174 | * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically |
| 175 | * for commands that return with a CHECK CONDITION status. |
| 176 | * |
| 177 | * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential |
| 178 | * transceivers. |
| 179 | * |
| 180 | * DONT_USE_INTR - if defined, never use interrupts, even if we probe or |
| 181 | * override-configure an IRQ. |
| 182 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases. |
| 184 | * |
| 185 | * REAL_DMA - if defined, REAL DMA is used during the data transfer phases. |
| 186 | * |
| 187 | * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't |
| 188 | * rely on phase mismatch and EOP interrupts to determine end |
| 189 | * of phase. |
| 190 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | * Defaults for these will be provided although the user may want to adjust |
| 192 | * these to allocate CPU resources to the SCSI driver or "real" code. |
| 193 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | * These macros MUST be defined : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | * |
| 196 | * NCR5380_read(register) - read from the specified register |
| 197 | * |
| 198 | * NCR5380_write(register, value) - write to the specific register |
| 199 | * |
| 200 | * NCR5380_implementation_fields - additional fields needed for this |
| 201 | * specific implementation of the NCR5380 |
| 202 | * |
| 203 | * Either real DMA *or* pseudo DMA may be implemented |
| 204 | * REAL functions : |
| 205 | * NCR5380_REAL_DMA should be defined if real DMA is to be used. |
| 206 | * Note that the DMA setup functions should return the number of bytes |
| 207 | * that they were able to program the controller for. |
| 208 | * |
| 209 | * Also note that generic i386/PC versions of these macros are |
| 210 | * available as NCR5380_i386_dma_write_setup, |
| 211 | * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual. |
| 212 | * |
| 213 | * NCR5380_dma_write_setup(instance, src, count) - initialize |
| 214 | * NCR5380_dma_read_setup(instance, dst, count) - initialize |
| 215 | * NCR5380_dma_residual(instance); - residual count |
| 216 | * |
| 217 | * PSEUDO functions : |
| 218 | * NCR5380_pwrite(instance, src, count) |
| 219 | * NCR5380_pread(instance, dst, count); |
| 220 | * |
| 221 | * The generic driver is initialized by calling NCR5380_init(instance), |
| 222 | * after setting the appropriate host specific fields and ID. If the |
| 223 | * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance, |
| 224 | * possible) function may be used. |
| 225 | */ |
| 226 | |
Finn Thain | 54d8fe4 | 2016-01-03 16:05:06 +1100 | [diff] [blame] | 227 | static int do_abort(struct Scsi_Host *); |
| 228 | static void do_reset(struct Scsi_Host *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | /* |
| 231 | * initialize_SCp - init the scsi pointer field |
| 232 | * @cmd: command block to set up |
| 233 | * |
| 234 | * Set up the internal fields in the SCSI command. |
| 235 | */ |
| 236 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 237 | static inline void initialize_SCp(struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 238 | { |
| 239 | /* |
| 240 | * Initialize the Scsi Pointer field so that all of the commands in the |
| 241 | * various queues are valid. |
| 242 | */ |
| 243 | |
Boaz Harrosh | 9e0fe44 | 2007-11-05 11:23:35 +0200 | [diff] [blame] | 244 | if (scsi_bufflen(cmd)) { |
| 245 | cmd->SCp.buffer = scsi_sglist(cmd); |
| 246 | cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1; |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 247 | cmd->SCp.ptr = sg_virt(cmd->SCp.buffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 248 | cmd->SCp.this_residual = cmd->SCp.buffer->length; |
| 249 | } else { |
| 250 | cmd->SCp.buffer = NULL; |
| 251 | cmd->SCp.buffers_residual = 0; |
Boaz Harrosh | 9e0fe44 | 2007-11-05 11:23:35 +0200 | [diff] [blame] | 252 | cmd->SCp.ptr = NULL; |
| 253 | cmd->SCp.this_residual = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
| 257 | /** |
Finn Thain | b32ade1 | 2016-01-03 16:05:41 +1100 | [diff] [blame] | 258 | * NCR5380_poll_politely2 - wait for two chip register values |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 259 | * @instance: controller to poll |
Finn Thain | b32ade1 | 2016-01-03 16:05:41 +1100 | [diff] [blame] | 260 | * @reg1: 5380 register to poll |
| 261 | * @bit1: Bitmask to check |
| 262 | * @val1: Expected value |
| 263 | * @reg2: Second 5380 register to poll |
| 264 | * @bit2: Second bitmask to check |
| 265 | * @val2: Second expected value |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 266 | * @wait: Time-out in jiffies |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | * |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 268 | * Polls the chip in a reasonably efficient manner waiting for an |
| 269 | * event to occur. After a short quick poll we begin to yield the CPU |
| 270 | * (if possible). In irq contexts the time-out is arbitrarily limited. |
| 271 | * Callers may hold locks as long as they are held in irq mode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | * |
Finn Thain | b32ade1 | 2016-01-03 16:05:41 +1100 | [diff] [blame] | 273 | * Returns 0 if either or both event(s) occurred otherwise -ETIMEDOUT. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | |
Finn Thain | b32ade1 | 2016-01-03 16:05:41 +1100 | [diff] [blame] | 276 | static int NCR5380_poll_politely2(struct Scsi_Host *instance, |
| 277 | int reg1, int bit1, int val1, |
| 278 | int reg2, int bit2, int val2, int wait) |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 279 | { |
| 280 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 281 | unsigned long deadline = jiffies + wait; |
| 282 | unsigned long n; |
| 283 | |
| 284 | /* Busy-wait for up to 10 ms */ |
| 285 | n = min(10000U, jiffies_to_usecs(wait)); |
| 286 | n *= hostdata->accesses_per_ms; |
Finn Thain | b32ade1 | 2016-01-03 16:05:41 +1100 | [diff] [blame] | 287 | n /= 2000; |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 288 | do { |
Finn Thain | b32ade1 | 2016-01-03 16:05:41 +1100 | [diff] [blame] | 289 | if ((NCR5380_read(reg1) & bit1) == val1) |
| 290 | return 0; |
| 291 | if ((NCR5380_read(reg2) & bit2) == val2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | return 0; |
| 293 | cpu_relax(); |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 294 | } while (n--); |
| 295 | |
| 296 | if (irqs_disabled() || in_interrupt()) |
| 297 | return -ETIMEDOUT; |
| 298 | |
| 299 | /* Repeatedly sleep for 1 ms until deadline */ |
| 300 | while (time_is_after_jiffies(deadline)) { |
| 301 | schedule_timeout_uninterruptible(1); |
Finn Thain | b32ade1 | 2016-01-03 16:05:41 +1100 | [diff] [blame] | 302 | if ((NCR5380_read(reg1) & bit1) == val1) |
| 303 | return 0; |
| 304 | if ((NCR5380_read(reg2) & bit2) == val2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | } |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | return -ETIMEDOUT; |
| 309 | } |
| 310 | |
Finn Thain | b32ade1 | 2016-01-03 16:05:41 +1100 | [diff] [blame] | 311 | static inline int NCR5380_poll_politely(struct Scsi_Host *instance, |
| 312 | int reg, int bit, int val, int wait) |
| 313 | { |
| 314 | return NCR5380_poll_politely2(instance, reg, bit, val, |
| 315 | reg, bit, val, wait); |
| 316 | } |
| 317 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | static struct { |
| 319 | unsigned char value; |
| 320 | const char *name; |
Andrew Morton | 702809c | 2007-05-23 14:41:56 -0700 | [diff] [blame] | 321 | } phases[] __maybe_unused = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | {PHASE_DATAOUT, "DATAOUT"}, |
| 323 | {PHASE_DATAIN, "DATAIN"}, |
| 324 | {PHASE_CMDOUT, "CMDOUT"}, |
| 325 | {PHASE_STATIN, "STATIN"}, |
| 326 | {PHASE_MSGOUT, "MSGOUT"}, |
| 327 | {PHASE_MSGIN, "MSGIN"}, |
| 328 | {PHASE_UNKNOWN, "UNKNOWN"} |
| 329 | }; |
| 330 | |
viro@ZenIV.linux.org.uk | 185a7a1 | 2005-09-07 23:18:24 +0100 | [diff] [blame] | 331 | #if NDEBUG |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | static struct { |
| 333 | unsigned char mask; |
| 334 | const char *name; |
| 335 | } signals[] = { |
| 336 | {SR_DBP, "PARITY"}, |
| 337 | {SR_RST, "RST"}, |
| 338 | {SR_BSY, "BSY"}, |
| 339 | {SR_REQ, "REQ"}, |
| 340 | {SR_MSG, "MSG"}, |
| 341 | {SR_CD, "CD"}, |
| 342 | {SR_IO, "IO"}, |
| 343 | {SR_SEL, "SEL"}, |
| 344 | {0, NULL} |
| 345 | }, |
| 346 | basrs[] = { |
| 347 | {BASR_ATN, "ATN"}, |
| 348 | {BASR_ACK, "ACK"}, |
| 349 | {0, NULL} |
| 350 | }, |
| 351 | icrs[] = { |
| 352 | {ICR_ASSERT_RST, "ASSERT RST"}, |
| 353 | {ICR_ASSERT_ACK, "ASSERT ACK"}, |
| 354 | {ICR_ASSERT_BSY, "ASSERT BSY"}, |
| 355 | {ICR_ASSERT_SEL, "ASSERT SEL"}, |
| 356 | {ICR_ASSERT_ATN, "ASSERT ATN"}, |
| 357 | {ICR_ASSERT_DATA, "ASSERT DATA"}, |
| 358 | {0, NULL} |
| 359 | }, |
| 360 | mrs[] = { |
| 361 | {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, |
| 362 | {MR_TARGET, "MODE TARGET"}, |
| 363 | {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, |
| 364 | {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"}, |
| 365 | {MR_MONITOR_BSY, "MODE MONITOR BSY"}, |
| 366 | {MR_DMA_MODE, "MODE DMA"}, |
| 367 | {MR_ARBITRATE, "MODE ARBITRATION"}, |
| 368 | {0, NULL} |
| 369 | }; |
| 370 | |
| 371 | /** |
| 372 | * NCR5380_print - print scsi bus signals |
| 373 | * @instance: adapter state to dump |
| 374 | * |
| 375 | * Print the SCSI bus signals for debugging purposes |
| 376 | * |
| 377 | * Locks: caller holds hostdata lock (not essential) |
| 378 | */ |
| 379 | |
| 380 | static void NCR5380_print(struct Scsi_Host *instance) |
| 381 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | unsigned char status, data, basr, mr, icr, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | |
| 384 | data = NCR5380_read(CURRENT_SCSI_DATA_REG); |
| 385 | status = NCR5380_read(STATUS_REG); |
| 386 | mr = NCR5380_read(MODE_REG); |
| 387 | icr = NCR5380_read(INITIATOR_COMMAND_REG); |
| 388 | basr = NCR5380_read(BUS_AND_STATUS_REG); |
| 389 | |
| 390 | printk("STATUS_REG: %02x ", status); |
| 391 | for (i = 0; signals[i].mask; ++i) |
| 392 | if (status & signals[i].mask) |
| 393 | printk(",%s", signals[i].name); |
| 394 | printk("\nBASR: %02x ", basr); |
| 395 | for (i = 0; basrs[i].mask; ++i) |
| 396 | if (basr & basrs[i].mask) |
| 397 | printk(",%s", basrs[i].name); |
| 398 | printk("\nICR: %02x ", icr); |
| 399 | for (i = 0; icrs[i].mask; ++i) |
| 400 | if (icr & icrs[i].mask) |
| 401 | printk(",%s", icrs[i].name); |
| 402 | printk("\nMODE: %02x ", mr); |
| 403 | for (i = 0; mrs[i].mask; ++i) |
| 404 | if (mr & mrs[i].mask) |
| 405 | printk(",%s", mrs[i].name); |
| 406 | printk("\n"); |
| 407 | } |
| 408 | |
| 409 | |
| 410 | /* |
| 411 | * NCR5380_print_phase - show SCSI phase |
| 412 | * @instance: adapter to dump |
| 413 | * |
| 414 | * Print the current SCSI phase for debugging purposes |
| 415 | * |
| 416 | * Locks: none |
| 417 | */ |
| 418 | |
| 419 | static void NCR5380_print_phase(struct Scsi_Host *instance) |
| 420 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | unsigned char status; |
| 422 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | |
| 424 | status = NCR5380_read(STATUS_REG); |
| 425 | if (!(status & SR_REQ)) |
| 426 | printk("scsi%d : REQ not asserted, phase unknown.\n", instance->host_no); |
| 427 | else { |
| 428 | for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i); |
| 429 | printk("scsi%d : phase %s\n", instance->host_no, phases[i].name); |
| 430 | } |
| 431 | } |
| 432 | #endif |
| 433 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | |
Finn Thain | d5f7e65 | 2016-01-03 16:05:03 +1100 | [diff] [blame] | 435 | static int probe_irq __initdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
| 437 | /** |
| 438 | * probe_intr - helper for IRQ autoprobe |
| 439 | * @irq: interrupt number |
| 440 | * @dev_id: unused |
| 441 | * @regs: unused |
| 442 | * |
| 443 | * Set a flag to indicate the IRQ in question was received. This is |
| 444 | * used by the IRQ probe code. |
| 445 | */ |
| 446 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 447 | static irqreturn_t __init probe_intr(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | { |
| 449 | probe_irq = irq; |
| 450 | return IRQ_HANDLED; |
| 451 | } |
| 452 | |
| 453 | /** |
| 454 | * NCR5380_probe_irq - find the IRQ of an NCR5380 |
| 455 | * @instance: NCR5380 controller |
| 456 | * @possible: bitmask of ISA IRQ lines |
| 457 | * |
| 458 | * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ |
| 459 | * and then looking to see what interrupt actually turned up. |
| 460 | * |
| 461 | * Locks: none, irqs must be enabled on entry |
| 462 | */ |
| 463 | |
Andrew Morton | 702809c | 2007-05-23 14:41:56 -0700 | [diff] [blame] | 464 | static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance, |
| 465 | int possible) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata; |
| 468 | unsigned long timeout; |
| 469 | int trying_irqs, i, mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 470 | |
Finn Thain | 22f5f10 | 2014-11-12 16:11:56 +1100 | [diff] [blame] | 471 | for (trying_irqs = 0, i = 1, mask = 2; i < 16; ++i, mask <<= 1) |
Michael Opdenacker | 4909cc2 | 2014-03-05 06:09:41 +0100 | [diff] [blame] | 472 | if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | trying_irqs |= mask; |
| 474 | |
Nicholas Mc Guire | 4e5a800 | 2015-02-04 13:30:20 -0500 | [diff] [blame] | 475 | timeout = jiffies + msecs_to_jiffies(250); |
Finn Thain | 22f5f10 | 2014-11-12 16:11:56 +1100 | [diff] [blame] | 476 | probe_irq = NO_IRQ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
| 478 | /* |
| 479 | * A interrupt is triggered whenever BSY = false, SEL = true |
| 480 | * and a bit set in the SELECT_ENABLE_REG is asserted on the |
| 481 | * SCSI bus. |
| 482 | * |
| 483 | * Note that the bus is only driven when the phase control signals |
| 484 | * (I/O, C/D, and MSG) match those in the TCR, so we must reset that |
| 485 | * to zero. |
| 486 | */ |
| 487 | |
| 488 | NCR5380_write(TARGET_COMMAND_REG, 0); |
| 489 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
| 490 | NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask); |
| 491 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL); |
| 492 | |
Finn Thain | 22f5f10 | 2014-11-12 16:11:56 +1100 | [diff] [blame] | 493 | while (probe_irq == NO_IRQ && time_before(jiffies, timeout)) |
Nishanth Aravamudan | a9a3047 | 2005-11-07 01:01:20 -0800 | [diff] [blame] | 494 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | |
| 496 | NCR5380_write(SELECT_ENABLE_REG, 0); |
| 497 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 498 | |
Finn Thain | 22f5f10 | 2014-11-12 16:11:56 +1100 | [diff] [blame] | 499 | for (i = 1, mask = 2; i < 16; ++i, mask <<= 1) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | if (trying_irqs & mask) |
| 501 | free_irq(i, NULL); |
| 502 | |
| 503 | return probe_irq; |
| 504 | } |
| 505 | |
| 506 | /** |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 507 | * NCR58380_info - report driver and host information |
| 508 | * @instance: relevant scsi host instance |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | * |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 510 | * For use as the host template info() handler. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | * |
| 512 | * Locks: none |
| 513 | */ |
| 514 | |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 515 | static const char *NCR5380_info(struct Scsi_Host *instance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | { |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 517 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 518 | |
| 519 | return hostdata->info; |
| 520 | } |
| 521 | |
| 522 | static void prepare_info(struct Scsi_Host *instance) |
| 523 | { |
| 524 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 525 | |
| 526 | snprintf(hostdata->info, sizeof(hostdata->info), |
| 527 | "%s, io_port 0x%lx, n_io_port %d, " |
| 528 | "base 0x%lx, irq %d, " |
| 529 | "can_queue %d, cmd_per_lun %d, " |
| 530 | "sg_tablesize %d, this_id %d, " |
Finn Thain | be3f412 | 2016-01-03 16:05:50 +1100 | [diff] [blame^] | 531 | "flags { %s%s%s}, " |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 532 | "options { %s} ", |
| 533 | instance->hostt->name, instance->io_port, instance->n_io_port, |
| 534 | instance->base, instance->irq, |
| 535 | instance->can_queue, instance->cmd_per_lun, |
| 536 | instance->sg_tablesize, instance->this_id, |
Finn Thain | 55181be | 2016-01-03 16:05:42 +1100 | [diff] [blame] | 537 | hostdata->flags & FLAG_NO_DMA_FIXUP ? "NO_DMA_FIXUP " : "", |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 538 | hostdata->flags & FLAG_NO_PSEUDO_DMA ? "NO_PSEUDO_DMA " : "", |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 539 | hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | #ifdef AUTOPROBE_IRQ |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 541 | "AUTOPROBE_IRQ " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | #ifdef DIFFERENTIAL |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 544 | "DIFFERENTIAL " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | #endif |
| 546 | #ifdef REAL_DMA |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 547 | "REAL_DMA " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | #endif |
| 549 | #ifdef REAL_DMA_POLL |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 550 | "REAL_DMA_POLL " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | #endif |
| 552 | #ifdef PARITY |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 553 | "PARITY " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | #endif |
| 555 | #ifdef PSEUDO_DMA |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 556 | "PSEUDO_DMA " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 557 | #endif |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 558 | ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | /** |
| 562 | * NCR5380_print_status - dump controller info |
| 563 | * @instance: controller to dump |
| 564 | * |
| 565 | * Print commands in the various queues, called from NCR5380_abort |
| 566 | * and NCR5380_debug to aid debugging. |
| 567 | * |
| 568 | * Locks: called functions disable irqs |
| 569 | */ |
| 570 | |
Finn Thain | 3be1b3e | 2016-01-03 16:05:12 +1100 | [diff] [blame] | 571 | static void __maybe_unused NCR5380_print_status(struct Scsi_Host *instance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | { |
| 573 | NCR5380_dprint(NDEBUG_ANY, instance); |
| 574 | NCR5380_dprint_phase(NDEBUG_ANY, instance); |
| 575 | } |
| 576 | |
Finn Thain | a9c2dc4 | 2014-11-12 16:11:59 +1100 | [diff] [blame] | 577 | #ifdef PSEUDO_DMA |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 578 | /******************************************/ |
| 579 | /* |
| 580 | * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED] |
| 581 | * |
| 582 | * *buffer: I/O buffer |
| 583 | * **start: if inout == FALSE pointer into buffer where user read should start |
| 584 | * offset: current offset |
| 585 | * length: length of buffer |
| 586 | * hostno: Scsi_Host host_no |
| 587 | * inout: TRUE - user is writing; FALSE - user is reading |
| 588 | * |
| 589 | * Return the number of bytes read from or written |
| 590 | */ |
| 591 | |
Al Viro | dd7ab71 | 2013-03-31 01:15:54 -0400 | [diff] [blame] | 592 | static int __maybe_unused NCR5380_write_info(struct Scsi_Host *instance, |
| 593 | char *buffer, int length) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | { |
Finn Thain | a9c2dc4 | 2014-11-12 16:11:59 +1100 | [diff] [blame] | 595 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 596 | |
| 597 | hostdata->spin_max_r = 0; |
| 598 | hostdata->spin_max_w = 0; |
| 599 | return 0; |
Al Viro | dd7ab71 | 2013-03-31 01:15:54 -0400 | [diff] [blame] | 600 | } |
Finn Thain | a9c2dc4 | 2014-11-12 16:11:59 +1100 | [diff] [blame] | 601 | #endif |
Al Viro | dd7ab71 | 2013-03-31 01:15:54 -0400 | [diff] [blame] | 602 | |
Al Viro | dd7ab71 | 2013-03-31 01:15:54 -0400 | [diff] [blame] | 603 | static |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 604 | void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m); |
Al Viro | dd7ab71 | 2013-03-31 01:15:54 -0400 | [diff] [blame] | 605 | static |
| 606 | void lprint_command(unsigned char *cmd, struct seq_file *m); |
| 607 | static |
| 608 | void lprint_opcode(int opcode, struct seq_file *m); |
| 609 | |
| 610 | static int __maybe_unused NCR5380_show_info(struct seq_file *m, |
| 611 | struct Scsi_Host *instance) |
| 612 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | struct NCR5380_hostdata *hostdata; |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 614 | struct scsi_cmnd *ptr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | |
| 616 | hostdata = (struct NCR5380_hostdata *) instance->hostdata; |
| 617 | |
Finn Thain | a9c2dc4 | 2014-11-12 16:11:59 +1100 | [diff] [blame] | 618 | #ifdef PSEUDO_DMA |
Rasmus Villemoes | 0c3de38 | 2014-12-03 00:10:49 +0100 | [diff] [blame] | 619 | seq_printf(m, "Highwater I/O busy spin counts: write %d, read %d\n", |
Finn Thain | a9c2dc4 | 2014-11-12 16:11:59 +1100 | [diff] [blame] | 620 | hostdata->spin_max_w, hostdata->spin_max_r); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | #endif |
| 622 | spin_lock_irq(instance->host_lock); |
| 623 | if (!hostdata->connected) |
Rasmus Villemoes | 0c3de38 | 2014-12-03 00:10:49 +0100 | [diff] [blame] | 624 | seq_printf(m, "scsi%d: no currently connected command\n", instance->host_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | else |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 626 | lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m); |
Rasmus Villemoes | 0c3de38 | 2014-12-03 00:10:49 +0100 | [diff] [blame] | 627 | seq_printf(m, "scsi%d: issue_queue\n", instance->host_no); |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 628 | for (ptr = (struct scsi_cmnd *) hostdata->issue_queue; ptr; ptr = (struct scsi_cmnd *) ptr->host_scribble) |
Al Viro | dd7ab71 | 2013-03-31 01:15:54 -0400 | [diff] [blame] | 629 | lprint_Scsi_Cmnd(ptr, m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | |
Rasmus Villemoes | 0c3de38 | 2014-12-03 00:10:49 +0100 | [diff] [blame] | 631 | seq_printf(m, "scsi%d: disconnected_queue\n", instance->host_no); |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 632 | for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr; ptr = (struct scsi_cmnd *) ptr->host_scribble) |
Al Viro | dd7ab71 | 2013-03-31 01:15:54 -0400 | [diff] [blame] | 633 | lprint_Scsi_Cmnd(ptr, m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 634 | spin_unlock_irq(instance->host_lock); |
Al Viro | dd7ab71 | 2013-03-31 01:15:54 -0400 | [diff] [blame] | 635 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 636 | } |
| 637 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 638 | static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 639 | { |
Rasmus Villemoes | 0c3de38 | 2014-12-03 00:10:49 +0100 | [diff] [blame] | 640 | seq_printf(m, "scsi%d : destination target %d, lun %llu\n", cmd->device->host->host_no, cmd->device->id, cmd->device->lun); |
Rasmus Villemoes | 91c40f2 | 2014-12-03 00:10:52 +0100 | [diff] [blame] | 641 | seq_puts(m, " command = "); |
Al Viro | dd7ab71 | 2013-03-31 01:15:54 -0400 | [diff] [blame] | 642 | lprint_command(cmd->cmnd, m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Al Viro | dd7ab71 | 2013-03-31 01:15:54 -0400 | [diff] [blame] | 645 | static void lprint_command(unsigned char *command, struct seq_file *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | { |
| 647 | int i, s; |
Al Viro | dd7ab71 | 2013-03-31 01:15:54 -0400 | [diff] [blame] | 648 | lprint_opcode(command[0], m); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i) |
Rasmus Villemoes | 0c3de38 | 2014-12-03 00:10:49 +0100 | [diff] [blame] | 650 | seq_printf(m, "%02x ", command[i]); |
Rasmus Villemoes | f50332f | 2014-12-03 00:10:54 +0100 | [diff] [blame] | 651 | seq_putc(m, '\n'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Al Viro | dd7ab71 | 2013-03-31 01:15:54 -0400 | [diff] [blame] | 654 | static void lprint_opcode(int opcode, struct seq_file *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | { |
Rasmus Villemoes | 0c3de38 | 2014-12-03 00:10:49 +0100 | [diff] [blame] | 656 | seq_printf(m, "%2d (0x%02x)", opcode, opcode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | |
| 660 | /** |
| 661 | * NCR5380_init - initialise an NCR5380 |
| 662 | * @instance: adapter to configure |
| 663 | * @flags: control flags |
| 664 | * |
| 665 | * Initializes *instance and corresponding 5380 chip, |
| 666 | * with flags OR'd into the initial flags value. |
| 667 | * |
| 668 | * Notes : I assume that the host, hostno, and id bits have been |
| 669 | * set correctly. I don't care about the irq and other fields. |
| 670 | * |
| 671 | * Returns 0 for success |
| 672 | * |
| 673 | * Locks: interrupts must be enabled when we are called |
| 674 | */ |
| 675 | |
Greg Kroah-Hartman | 6f03979 | 2012-12-21 13:08:55 -0800 | [diff] [blame] | 676 | static int NCR5380_init(struct Scsi_Host *instance, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | { |
Finn Thain | b6488f9 | 2016-01-03 16:05:08 +1100 | [diff] [blame] | 678 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 679 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata; |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 680 | unsigned long deadline; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 681 | |
| 682 | if(in_interrupt()) |
| 683 | printk(KERN_ERR "NCR5380_init called with interrupts off!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | hostdata->id_mask = 1 << instance->this_id; |
| 686 | for (i = hostdata->id_mask; i <= 0x80; i <<= 1) |
| 687 | if (i > hostdata->id_mask) |
| 688 | hostdata->id_higher_mask |= i; |
| 689 | for (i = 0; i < 8; ++i) |
| 690 | hostdata->busy[i] = 0; |
| 691 | #ifdef REAL_DMA |
| 692 | hostdata->dmalen = 0; |
| 693 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | hostdata->connected = NULL; |
| 695 | hostdata->issue_queue = NULL; |
| 696 | hostdata->disconnected_queue = NULL; |
Finn Thain | 55181be | 2016-01-03 16:05:42 +1100 | [diff] [blame] | 697 | hostdata->flags = flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
Finn Thain | 8d8601a | 2016-01-03 16:05:37 +1100 | [diff] [blame] | 699 | INIT_WORK(&hostdata->main_task, NCR5380_main); |
Finn Thain | 0ad0eff | 2016-01-03 16:05:21 +1100 | [diff] [blame] | 700 | hostdata->work_q = alloc_workqueue("ncr5380_%d", |
| 701 | WQ_UNBOUND | WQ_MEM_RECLAIM, |
| 702 | 1, instance->host_no); |
| 703 | if (!hostdata->work_q) |
| 704 | return -ENOMEM; |
| 705 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | hostdata->host = instance; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 708 | prepare_info(instance); |
| 709 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 711 | NCR5380_write(MODE_REG, MR_BASE); |
| 712 | NCR5380_write(TARGET_COMMAND_REG, 0); |
| 713 | NCR5380_write(SELECT_ENABLE_REG, 0); |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 714 | |
| 715 | /* Calibrate register polling loop */ |
| 716 | i = 0; |
| 717 | deadline = jiffies + 1; |
| 718 | do { |
| 719 | cpu_relax(); |
| 720 | } while (time_is_after_jiffies(deadline)); |
| 721 | deadline += msecs_to_jiffies(256); |
| 722 | do { |
| 723 | NCR5380_read(STATUS_REG); |
| 724 | ++i; |
| 725 | cpu_relax(); |
| 726 | } while (time_is_after_jiffies(deadline)); |
| 727 | hostdata->accesses_per_ms = i / 256; |
| 728 | |
Finn Thain | b6488f9 | 2016-01-03 16:05:08 +1100 | [diff] [blame] | 729 | return 0; |
| 730 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | |
Finn Thain | b6488f9 | 2016-01-03 16:05:08 +1100 | [diff] [blame] | 732 | /** |
| 733 | * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems. |
| 734 | * @instance: adapter to check |
| 735 | * |
| 736 | * If the system crashed, it may have crashed with a connected target and |
| 737 | * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the |
| 738 | * currently established nexus, which we know nothing about. Failing that |
| 739 | * do a bus reset. |
| 740 | * |
| 741 | * Note that a bus reset will cause the chip to assert IRQ. |
| 742 | * |
| 743 | * Returns 0 if successful, otherwise -ENXIO. |
| 744 | */ |
| 745 | |
| 746 | static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance) |
| 747 | { |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 748 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Finn Thain | b6488f9 | 2016-01-03 16:05:08 +1100 | [diff] [blame] | 749 | int pass; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | |
| 751 | for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) { |
| 752 | switch (pass) { |
| 753 | case 1: |
| 754 | case 3: |
| 755 | case 5: |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 756 | shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n"); |
| 757 | NCR5380_poll_politely(instance, |
| 758 | STATUS_REG, SR_BSY, 0, 5 * HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | break; |
| 760 | case 2: |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 761 | shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | do_abort(instance); |
| 763 | break; |
| 764 | case 4: |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 765 | shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | do_reset(instance); |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 767 | /* Wait after a reset; the SCSI standard calls for |
| 768 | * 250ms, we wait 500ms to be on the safe side. |
| 769 | * But some Toshiba CD-ROMs need ten times that. |
| 770 | */ |
| 771 | if (hostdata->flags & FLAG_TOSHIBA_DELAY) |
| 772 | msleep(2500); |
| 773 | else |
| 774 | msleep(500); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 775 | break; |
| 776 | case 6: |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 777 | shost_printk(KERN_ERR, instance, "bus locked solid\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | return -ENXIO; |
| 779 | } |
| 780 | } |
| 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | /** |
| 785 | * NCR5380_exit - remove an NCR5380 |
| 786 | * @instance: adapter to remove |
| 787 | */ |
| 788 | |
Randy Dunlap | a43cf0f | 2008-01-22 21:39:33 -0800 | [diff] [blame] | 789 | static void NCR5380_exit(struct Scsi_Host *instance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 790 | { |
| 791 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata; |
| 792 | |
Finn Thain | 8d8601a | 2016-01-03 16:05:37 +1100 | [diff] [blame] | 793 | cancel_work_sync(&hostdata->main_task); |
Finn Thain | 0ad0eff | 2016-01-03 16:05:21 +1100 | [diff] [blame] | 794 | destroy_workqueue(hostdata->work_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | /** |
Finn Thain | 1bb4058 | 2016-01-03 16:05:29 +1100 | [diff] [blame] | 798 | * NCR5380_queue_command - queue a command |
| 799 | * @instance: the relevant SCSI adapter |
| 800 | * @cmd: SCSI command |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | * |
Finn Thain | 1bb4058 | 2016-01-03 16:05:29 +1100 | [diff] [blame] | 802 | * cmd is added to the per-instance issue queue, with minor |
| 803 | * twiddling done to the host specific fields of cmd. If the |
| 804 | * main coroutine is not running, it is restarted. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | */ |
| 806 | |
Finn Thain | 1bb4058 | 2016-01-03 16:05:29 +1100 | [diff] [blame] | 807 | static int NCR5380_queue_command(struct Scsi_Host *instance, |
| 808 | struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 809 | { |
Finn Thain | 1bb4058 | 2016-01-03 16:05:29 +1100 | [diff] [blame] | 810 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 811 | struct scsi_cmnd *tmp; |
Finn Thain | 1bb4058 | 2016-01-03 16:05:29 +1100 | [diff] [blame] | 812 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | |
| 814 | #if (NDEBUG & NDEBUG_NO_WRITE) |
| 815 | switch (cmd->cmnd[0]) { |
| 816 | case WRITE_6: |
| 817 | case WRITE_10: |
| 818 | printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n", instance->host_no); |
| 819 | cmd->result = (DID_ERROR << 16); |
Finn Thain | 1bb4058 | 2016-01-03 16:05:29 +1100 | [diff] [blame] | 820 | cmd->scsi_done(cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | return 0; |
| 822 | } |
| 823 | #endif /* (NDEBUG & NDEBUG_NO_WRITE) */ |
| 824 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | /* |
| 826 | * We use the host_scribble field as a pointer to the next command |
| 827 | * in a queue |
| 828 | */ |
| 829 | |
| 830 | cmd->host_scribble = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | cmd->result = 0; |
| 832 | |
Finn Thain | 1bb4058 | 2016-01-03 16:05:29 +1100 | [diff] [blame] | 833 | spin_lock_irqsave(instance->host_lock, flags); |
| 834 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | /* |
| 836 | * Insert the cmd into the issue queue. Note that REQUEST SENSE |
| 837 | * commands are added to the head of the queue since any command will |
| 838 | * clear the contingent allegiance condition that exists and the |
| 839 | * sense data is only guaranteed to be valid while the condition exists. |
| 840 | */ |
| 841 | |
| 842 | if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) { |
| 843 | LIST(cmd, hostdata->issue_queue); |
| 844 | cmd->host_scribble = (unsigned char *) hostdata->issue_queue; |
| 845 | hostdata->issue_queue = cmd; |
| 846 | } else { |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 847 | for (tmp = (struct scsi_cmnd *) hostdata->issue_queue; tmp->host_scribble; tmp = (struct scsi_cmnd *) tmp->host_scribble); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | LIST(cmd, tmp); |
| 849 | tmp->host_scribble = (unsigned char *) cmd; |
| 850 | } |
Finn Thain | 1bb4058 | 2016-01-03 16:05:29 +1100 | [diff] [blame] | 851 | spin_unlock_irqrestore(instance->host_lock, flags); |
| 852 | |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 853 | dprintk(NDEBUG_QUEUES, "scsi%d : command added to %s of queue\n", instance->host_no, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | /* Kick off command processing */ |
Finn Thain | 8d8601a | 2016-01-03 16:05:37 +1100 | [diff] [blame] | 856 | queue_work(hostdata->work_q, &hostdata->main_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 857 | return 0; |
| 858 | } |
| 859 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | /** |
| 861 | * NCR5380_main - NCR state machines |
| 862 | * |
| 863 | * NCR5380_main is a coroutine that runs as long as more work can |
| 864 | * be done on the NCR5380 host adapters in a system. Both |
| 865 | * NCR5380_queue_command() and NCR5380_intr() will try to start it |
| 866 | * in case it is not running. |
| 867 | * |
| 868 | * Locks: called as its own thread with no locks held. Takes the |
| 869 | * host lock and called routines may take the isa dma lock. |
| 870 | */ |
| 871 | |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 872 | static void NCR5380_main(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 873 | { |
David Howells | c402895 | 2006-11-22 14:57:56 +0000 | [diff] [blame] | 874 | struct NCR5380_hostdata *hostdata = |
Finn Thain | 8d8601a | 2016-01-03 16:05:37 +1100 | [diff] [blame] | 875 | container_of(work, struct NCR5380_hostdata, main_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 876 | struct Scsi_Host *instance = hostdata->host; |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 877 | struct scsi_cmnd *tmp, *prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 878 | int done; |
| 879 | |
| 880 | spin_lock_irq(instance->host_lock); |
| 881 | do { |
| 882 | /* Lock held here */ |
| 883 | done = 1; |
Finn Thain | ae753a3 | 2016-01-03 16:05:23 +1100 | [diff] [blame] | 884 | if (!hostdata->connected) { |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 885 | dprintk(NDEBUG_MAIN, "scsi%d : not connected\n", instance->host_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 886 | /* |
| 887 | * Search through the issue_queue for a command destined |
| 888 | * for a target that's not busy. |
| 889 | */ |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 890 | for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL; tmp; prev = tmp, tmp = (struct scsi_cmnd *) tmp->host_scribble) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 891 | { |
| 892 | if (prev != tmp) |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 893 | dprintk(NDEBUG_LISTS, "MAIN tmp=%p target=%d busy=%d lun=%llu\n", tmp, tmp->device->id, hostdata->busy[tmp->device->id], tmp->device->lun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 894 | /* When we find one, remove it from the issue queue. */ |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 895 | if (!(hostdata->busy[tmp->device->id] & |
| 896 | (1 << (u8)(tmp->device->lun & 0xff)))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | if (prev) { |
| 898 | REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble); |
| 899 | prev->host_scribble = tmp->host_scribble; |
| 900 | } else { |
| 901 | REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble); |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 902 | hostdata->issue_queue = (struct scsi_cmnd *) tmp->host_scribble; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 903 | } |
| 904 | tmp->host_scribble = NULL; |
| 905 | |
| 906 | /* |
| 907 | * Attempt to establish an I_T_L nexus here. |
| 908 | * On success, instance->hostdata->connected is set. |
| 909 | * On failure, we must add the command back to the |
| 910 | * issue queue so we can keep trying. |
| 911 | */ |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 912 | dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, "scsi%d : main() : command for target %d lun %llu removed from issue_queue\n", instance->host_no, tmp->device->id, tmp->device->lun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | |
Finn Thain | 76f13b9 | 2014-11-12 16:11:53 +1100 | [diff] [blame] | 914 | /* |
| 915 | * REQUEST SENSE commands are issued without tagged |
| 916 | * queueing, even on SCSI-II devices because the |
| 917 | * contingent allegiance condition exists for the |
| 918 | * entire unit. |
| 919 | */ |
| 920 | |
| 921 | if (!NCR5380_select(instance, tmp)) { |
Finn Thain | 1f1b0c7 | 2016-01-03 16:05:17 +1100 | [diff] [blame] | 922 | /* OK or bad target */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | } else { |
Finn Thain | 1f1b0c7 | 2016-01-03 16:05:17 +1100 | [diff] [blame] | 924 | /* Need to retry */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | LIST(tmp, hostdata->issue_queue); |
| 926 | tmp->host_scribble = (unsigned char *) hostdata->issue_queue; |
| 927 | hostdata->issue_queue = tmp; |
| 928 | done = 0; |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 929 | dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, "scsi%d : main(): select() failed, returned to issue_queue\n", instance->host_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 930 | } |
Finn Thain | ae753a3 | 2016-01-03 16:05:23 +1100 | [diff] [blame] | 931 | if (hostdata->connected) |
Finn Thain | 1f1b0c7 | 2016-01-03 16:05:17 +1100 | [diff] [blame] | 932 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 933 | /* lock held here still */ |
| 934 | } /* if target/lun is not busy */ |
| 935 | } /* for */ |
| 936 | /* exited locked */ |
| 937 | } /* if (!hostdata->connected) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | if (hostdata->connected |
| 939 | #ifdef REAL_DMA |
| 940 | && !hostdata->dmalen |
| 941 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | ) { |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 943 | dprintk(NDEBUG_MAIN, "scsi%d : main() : performing information transfer\n", instance->host_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | NCR5380_information_transfer(instance); |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 945 | dprintk(NDEBUG_MAIN, "scsi%d : main() : done set false\n", instance->host_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | done = 0; |
Finn Thain | 1d3db59 | 2016-01-03 16:05:24 +1100 | [diff] [blame] | 947 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | } while (!done); |
| 949 | |
| 950 | spin_unlock_irq(instance->host_lock); |
| 951 | } |
| 952 | |
| 953 | #ifndef DONT_USE_INTR |
| 954 | |
| 955 | /** |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 956 | * NCR5380_intr - generic NCR5380 irq handler |
| 957 | * @irq: interrupt number |
| 958 | * @dev_id: device info |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | * |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 960 | * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses |
| 961 | * from the disconnected queue, and restarting NCR5380_main() |
| 962 | * as required. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | * |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 964 | * The chip can assert IRQ in any of six different conditions. The IRQ flag |
| 965 | * is then cleared by reading the Reset Parity/Interrupt Register (RPIR). |
| 966 | * Three of these six conditions are latched in the Bus and Status Register: |
| 967 | * - End of DMA (cleared by ending DMA Mode) |
| 968 | * - Parity error (cleared by reading RPIR) |
| 969 | * - Loss of BSY (cleared by reading RPIR) |
| 970 | * Two conditions have flag bits that are not latched: |
| 971 | * - Bus phase mismatch (non-maskable in DMA Mode, cleared by ending DMA Mode) |
| 972 | * - Bus reset (non-maskable) |
| 973 | * The remaining condition has no flag bit at all: |
| 974 | * - Selection/reselection |
| 975 | * |
| 976 | * Hence, establishing the cause(s) of any interrupt is partly guesswork. |
| 977 | * In "The DP8490 and DP5380 Comparison Guide", National Semiconductor |
| 978 | * claimed that "the design of the [DP8490] interrupt logic ensures |
| 979 | * interrupts will not be lost (they can be on the DP5380)." |
| 980 | * The L5380/53C80 datasheet from LOGIC Devices has more details. |
| 981 | * |
| 982 | * Checking for bus reset by reading RST is futile because of interrupt |
| 983 | * latency, but a bus reset will reset chip logic. Checking for parity error |
| 984 | * is unnecessary because that interrupt is never enabled. A Loss of BSY |
| 985 | * condition will clear DMA Mode. We can tell when this occurs because the |
| 986 | * the Busy Monitor interrupt is enabled together with DMA Mode. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | */ |
| 988 | |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 989 | static irqreturn_t NCR5380_intr(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | { |
Jeff Garzik | baa9aac | 2007-12-13 16:14:14 -0800 | [diff] [blame] | 991 | struct Scsi_Host *instance = dev_id; |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 992 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 993 | int handled = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 994 | unsigned char basr; |
| 995 | unsigned long flags; |
| 996 | |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 997 | spin_lock_irqsave(instance->host_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 998 | |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 999 | basr = NCR5380_read(BUS_AND_STATUS_REG); |
| 1000 | if (basr & BASR_IRQ) { |
| 1001 | unsigned char mr = NCR5380_read(MODE_REG); |
| 1002 | unsigned char sr = NCR5380_read(STATUS_REG); |
| 1003 | |
| 1004 | dprintk(NDEBUG_INTR, "scsi%d: IRQ %d, BASR 0x%02x, SR 0x%02x, MR 0x%02x\n", |
| 1005 | instance->host_no, irq, basr, sr, mr); |
| 1006 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | #if defined(REAL_DMA) |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 1008 | if ((mr & MR_DMA_MODE) || (mr & MR_MONITOR_BSY)) { |
| 1009 | /* Probably End of DMA, Phase Mismatch or Loss of BSY. |
| 1010 | * We ack IRQ after clearing Mode Register. Workarounds |
| 1011 | * for End of DMA errata need to happen in DMA Mode. |
| 1012 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1013 | |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 1014 | dprintk(NDEBUG_INTR, "scsi%d: interrupt in DMA mode\n", intance->host_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 1016 | int transferred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 1018 | if (!hostdata->connected) |
| 1019 | panic("scsi%d : DMA interrupt with no connected cmd\n", |
| 1020 | instance->hostno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 1022 | transferred = hostdata->dmalen - NCR5380_dma_residual(instance); |
| 1023 | hostdata->connected->SCp.this_residual -= transferred; |
| 1024 | hostdata->connected->SCp.ptr += transferred; |
| 1025 | hostdata->dmalen = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1026 | |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 1027 | /* FIXME: we need to poll briefly then defer a workqueue task ! */ |
| 1028 | NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2 * HZ); |
| 1029 | |
| 1030 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1031 | NCR5380_write(MODE_REG, MR_BASE); |
| 1032 | NCR5380_read(RESET_PARITY_INTERRUPT_REG); |
| 1033 | } else |
| 1034 | #endif /* REAL_DMA */ |
| 1035 | if ((NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_mask) && |
| 1036 | (sr & (SR_SEL | SR_IO | SR_BSY | SR_RST)) == (SR_SEL | SR_IO)) { |
| 1037 | /* Probably reselected */ |
| 1038 | NCR5380_write(SELECT_ENABLE_REG, 0); |
| 1039 | NCR5380_read(RESET_PARITY_INTERRUPT_REG); |
| 1040 | |
| 1041 | dprintk(NDEBUG_INTR, "scsi%d: interrupt with SEL and IO\n", |
| 1042 | instance->host_no); |
| 1043 | |
| 1044 | if (!hostdata->connected) { |
| 1045 | NCR5380_reselect(instance); |
| 1046 | queue_work(hostdata->work_q, &hostdata->main_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | } |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 1048 | if (!hostdata->connected) |
| 1049 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
| 1050 | } else { |
| 1051 | /* Probably Bus Reset */ |
| 1052 | NCR5380_read(RESET_PARITY_INTERRUPT_REG); |
| 1053 | |
| 1054 | dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt\n", instance->host_no); |
| 1055 | } |
| 1056 | handled = 1; |
| 1057 | } else { |
| 1058 | shost_printk(KERN_NOTICE, instance, "interrupt without IRQ bit\n"); |
| 1059 | } |
| 1060 | |
| 1061 | spin_unlock_irqrestore(instance->host_lock, flags); |
| 1062 | |
| 1063 | return IRQ_RETVAL(handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
| 1066 | #endif |
| 1067 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1068 | /* |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 1069 | * Function : int NCR5380_select(struct Scsi_Host *instance, |
| 1070 | * struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | * |
| 1072 | * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command, |
| 1073 | * including ARBITRATION, SELECTION, and initial message out for |
| 1074 | * IDENTIFY and queue messages. |
| 1075 | * |
| 1076 | * Inputs : instance - instantiation of the 5380 driver on which this |
Finn Thain | 76f13b9 | 2014-11-12 16:11:53 +1100 | [diff] [blame] | 1077 | * target lives, cmd - SCSI command to execute. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1078 | * |
Finn Thain | 6323876 | 2016-01-03 16:05:15 +1100 | [diff] [blame] | 1079 | * Returns : -1 if selection failed but should be retried. |
| 1080 | * 0 if selection failed and should not be retried. |
| 1081 | * 0 if selection succeeded completely (hostdata->connected == cmd). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | * |
| 1083 | * Side effects : |
| 1084 | * If bus busy, arbitration failed, etc, NCR5380_select() will exit |
| 1085 | * with registers as they should have been on entry - ie |
| 1086 | * SELECT_ENABLE will be set appropriately, the NCR5380 |
| 1087 | * will cease to drive any SCSI bus signals. |
| 1088 | * |
| 1089 | * If successful : I_T_L or I_T_L_Q nexus will be established, |
| 1090 | * instance->connected will be set to cmd. |
| 1091 | * SELECT interrupt will be disabled. |
| 1092 | * |
| 1093 | * If failed (no target) : cmd->scsi_done() will be called, and the |
| 1094 | * cmd->result host byte set to DID_BAD_TARGET. |
| 1095 | * |
| 1096 | * Locks: caller holds hostdata lock in IRQ mode |
| 1097 | */ |
| 1098 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 1099 | static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata; |
| 1102 | unsigned char tmp[3], phase; |
| 1103 | unsigned char *data; |
| 1104 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | NCR5380_dprint(NDEBUG_ARBITRATION, instance); |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1108 | dprintk(NDEBUG_ARBITRATION, "scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1109 | |
| 1110 | /* |
| 1111 | * Set the phase bits to 0, otherwise the NCR5380 won't drive the |
| 1112 | * data bus during SELECTION. |
| 1113 | */ |
| 1114 | |
| 1115 | NCR5380_write(TARGET_COMMAND_REG, 0); |
| 1116 | |
| 1117 | /* |
| 1118 | * Start arbitration. |
| 1119 | */ |
| 1120 | |
| 1121 | NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask); |
| 1122 | NCR5380_write(MODE_REG, MR_ARBITRATE); |
| 1123 | |
Finn Thain | 55500d9 | 2016-01-03 16:05:35 +1100 | [diff] [blame] | 1124 | /* The chip now waits for BUS FREE phase. Then after the 800 ns |
| 1125 | * Bus Free Delay, arbitration will begin. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | */ |
| 1127 | |
Finn Thain | 55500d9 | 2016-01-03 16:05:35 +1100 | [diff] [blame] | 1128 | spin_unlock_irq(instance->host_lock); |
Finn Thain | b32ade1 | 2016-01-03 16:05:41 +1100 | [diff] [blame] | 1129 | err = NCR5380_poll_politely2(instance, MODE_REG, MR_ARBITRATE, 0, |
| 1130 | INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS, |
| 1131 | ICR_ARBITRATION_PROGRESS, HZ); |
| 1132 | spin_lock_irq(instance->host_lock); |
| 1133 | if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) { |
| 1134 | /* Reselection interrupt */ |
| 1135 | return -1; |
| 1136 | } |
| 1137 | if (err < 0) { |
| 1138 | NCR5380_write(MODE_REG, MR_BASE); |
| 1139 | shost_printk(KERN_ERR, instance, |
| 1140 | "select: arbitration timeout\n"); |
| 1141 | return -1; |
Finn Thain | 55500d9 | 2016-01-03 16:05:35 +1100 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | /* The SCSI-2 arbitration delay is 2.4 us */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | udelay(3); |
| 1146 | |
| 1147 | /* Check for lost arbitration */ |
| 1148 | 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)) { |
| 1149 | NCR5380_write(MODE_REG, MR_BASE); |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1150 | dprintk(NDEBUG_ARBITRATION, "scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no); |
Finn Thain | 6323876 | 2016-01-03 16:05:15 +1100 | [diff] [blame] | 1151 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | } |
Finn Thain | cf13b08 | 2016-01-03 16:05:18 +1100 | [diff] [blame] | 1153 | |
| 1154 | /* After/during arbitration, BSY should be asserted. |
| 1155 | * IBM DPES-31080 Version S31Q works now |
| 1156 | * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) |
| 1157 | */ |
| 1158 | NCR5380_write(INITIATOR_COMMAND_REG, |
| 1159 | ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | /* |
| 1162 | * Again, bus clear + bus settle time is 1.2us, however, this is |
| 1163 | * a minimum so we'll udelay ceil(1.2) |
| 1164 | */ |
| 1165 | |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 1166 | if (hostdata->flags & FLAG_TOSHIBA_DELAY) |
| 1167 | udelay(15); |
| 1168 | else |
| 1169 | udelay(2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1170 | |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 1171 | /* NCR5380_reselect() clears MODE_REG after a reselection interrupt */ |
| 1172 | if (!(NCR5380_read(MODE_REG) & MR_ARBITRATE)) |
| 1173 | return -1; |
| 1174 | |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1175 | dprintk(NDEBUG_ARBITRATION, "scsi%d : won arbitration\n", instance->host_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | |
| 1177 | /* |
| 1178 | * Now that we have won arbitration, start Selection process, asserting |
| 1179 | * the host and target ID's on the SCSI bus. |
| 1180 | */ |
| 1181 | |
Jeff Garzik | 422c0d6 | 2005-10-24 18:05:09 -0400 | [diff] [blame] | 1182 | NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd)))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | |
| 1184 | /* |
| 1185 | * Raise ATN while SEL is true before BSY goes false from arbitration, |
| 1186 | * since this is the only way to guarantee that we'll get a MESSAGE OUT |
| 1187 | * phase immediately after selection. |
| 1188 | */ |
| 1189 | |
| 1190 | NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL)); |
| 1191 | NCR5380_write(MODE_REG, MR_BASE); |
| 1192 | |
| 1193 | /* |
| 1194 | * Reselect interrupts must be turned off prior to the dropping of BSY, |
| 1195 | * otherwise we will trigger an interrupt. |
| 1196 | */ |
| 1197 | NCR5380_write(SELECT_ENABLE_REG, 0); |
| 1198 | |
| 1199 | /* |
| 1200 | * The initiator shall then wait at least two deskew delays and release |
| 1201 | * the BSY signal. |
| 1202 | */ |
| 1203 | udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */ |
| 1204 | |
| 1205 | /* Reset BSY */ |
| 1206 | NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL)); |
| 1207 | |
| 1208 | /* |
| 1209 | * Something weird happens when we cease to drive BSY - looks |
| 1210 | * like the board/chip is letting us do another read before the |
| 1211 | * appropriate propagation delay has expired, and we're confusing |
| 1212 | * a BSY signal from ourselves as the target's response to SELECTION. |
| 1213 | * |
| 1214 | * A small delay (the 'C++' frontend breaks the pipeline with an |
| 1215 | * unnecessary jump, making it work on my 386-33/Trantor T128, the |
| 1216 | * tighter 'C' code breaks and requires this) solves the problem - |
| 1217 | * the 1 us delay is arbitrary, and only used because this delay will |
| 1218 | * be the same on other platforms and since it works here, it should |
| 1219 | * work there. |
| 1220 | * |
| 1221 | * wingel suggests that this could be due to failing to wait |
| 1222 | * one deskew delay. |
| 1223 | */ |
| 1224 | |
| 1225 | udelay(1); |
| 1226 | |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1227 | dprintk(NDEBUG_SELECTION, "scsi%d : selecting target %d\n", instance->host_no, scmd_id(cmd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1228 | |
| 1229 | /* |
| 1230 | * The SCSI specification calls for a 250 ms timeout for the actual |
| 1231 | * selection. |
| 1232 | */ |
| 1233 | |
Finn Thain | ae753a3 | 2016-01-03 16:05:23 +1100 | [diff] [blame] | 1234 | err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY, |
| 1235 | msecs_to_jiffies(250)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1236 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1237 | if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) { |
| 1238 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1239 | NCR5380_reselect(instance); |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 1240 | if (!hostdata->connected) |
| 1241 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | printk("scsi%d : reselection after won arbitration?\n", instance->host_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | return -1; |
| 1244 | } |
Finn Thain | ae753a3 | 2016-01-03 16:05:23 +1100 | [diff] [blame] | 1245 | |
| 1246 | if (err < 0) { |
| 1247 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1248 | cmd->result = DID_BAD_TARGET << 16; |
| 1249 | cmd->scsi_done(cmd); |
| 1250 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
| 1251 | dprintk(NDEBUG_SELECTION, "scsi%d : target did not respond within 250ms\n", |
| 1252 | instance->host_no); |
| 1253 | return 0; |
| 1254 | } |
| 1255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | /* |
| 1257 | * No less than two deskew delays after the initiator detects the |
| 1258 | * BSY signal is true, it shall release the SEL signal and may |
| 1259 | * change the DATA BUS. -wingel |
| 1260 | */ |
| 1261 | |
| 1262 | udelay(1); |
| 1263 | |
| 1264 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); |
| 1265 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1266 | /* |
| 1267 | * Since we followed the SCSI spec, and raised ATN while SEL |
| 1268 | * was true but before BSY was false during selection, the information |
| 1269 | * transfer phase should be a MESSAGE OUT phase so that we can send the |
| 1270 | * IDENTIFY message. |
| 1271 | * |
| 1272 | * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG |
| 1273 | * message (2 bytes) with a tag ID that we increment with every command |
| 1274 | * until it wraps back to 0. |
| 1275 | * |
| 1276 | * XXX - it turns out that there are some broken SCSI-II devices, |
| 1277 | * which claim to support tagged queuing but fail when more than |
| 1278 | * some number of commands are issued at once. |
| 1279 | */ |
| 1280 | |
| 1281 | /* Wait for start of REQ/ACK handshake */ |
| 1282 | |
| 1283 | spin_unlock_irq(instance->host_lock); |
| 1284 | err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ); |
| 1285 | spin_lock_irq(instance->host_lock); |
Finn Thain | 1cc160e | 2016-01-03 16:05:32 +1100 | [diff] [blame] | 1286 | if (err < 0) { |
Finn Thain | 55500d9 | 2016-01-03 16:05:35 +1100 | [diff] [blame] | 1287 | shost_printk(KERN_ERR, instance, "select: REQ timeout\n"); |
| 1288 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
Finn Thain | 6323876 | 2016-01-03 16:05:15 +1100 | [diff] [blame] | 1290 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | } |
| 1292 | |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1293 | dprintk(NDEBUG_SELECTION, "scsi%d : target %d selected, going into MESSAGE OUT phase.\n", instance->host_no, cmd->device->id); |
Finn Thain | 22f5f10 | 2014-11-12 16:11:56 +1100 | [diff] [blame] | 1294 | tmp[0] = IDENTIFY(((instance->irq == NO_IRQ) ? 0 : 1), cmd->device->lun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | |
| 1296 | len = 1; |
| 1297 | cmd->tag = 0; |
| 1298 | |
| 1299 | /* Send message(s) */ |
| 1300 | data = tmp; |
| 1301 | phase = PHASE_MSGOUT; |
| 1302 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1303 | dprintk(NDEBUG_SELECTION, "scsi%d : nexus established.\n", instance->host_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | /* XXX need to handle errors here */ |
| 1305 | hostdata->connected = cmd; |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 1306 | hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | |
Boaz Harrosh | 28424d3 | 2007-09-10 22:37:45 +0300 | [diff] [blame] | 1308 | initialize_SCp(cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | |
| 1310 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | /* |
| 1314 | * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance, |
| 1315 | * unsigned char *phase, int *count, unsigned char **data) |
| 1316 | * |
| 1317 | * Purpose : transfers data in given phase using polled I/O |
| 1318 | * |
| 1319 | * Inputs : instance - instance of driver, *phase - pointer to |
| 1320 | * what phase is expected, *count - pointer to number of |
| 1321 | * bytes to transfer, **data - pointer to data pointer. |
| 1322 | * |
| 1323 | * Returns : -1 when different phase is entered without transferring |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1324 | * maximum number of bytes, 0 if all bytes or transferred or exit |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | * is in same phase. |
| 1326 | * |
| 1327 | * Also, *phase, *count, *data are modified in place. |
| 1328 | * |
| 1329 | * XXX Note : handling for bus free may be useful. |
| 1330 | */ |
| 1331 | |
| 1332 | /* |
| 1333 | * Note : this code is not as quick as it could be, however it |
| 1334 | * IS 100% reliable, and for the actual data transfer where speed |
| 1335 | * counts, we will always do a pseudo DMA or DMA transfer. |
| 1336 | */ |
| 1337 | |
| 1338 | static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | unsigned char p = *phase, tmp; |
| 1340 | int c = *count; |
| 1341 | unsigned char *d = *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | |
| 1343 | if (!(p & SR_IO)) |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1344 | dprintk(NDEBUG_PIO, "scsi%d : pio write %d bytes\n", instance->host_no, c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | else |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1346 | dprintk(NDEBUG_PIO, "scsi%d : pio read %d bytes\n", instance->host_no, c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | |
| 1348 | /* |
| 1349 | * The NCR5380 chip will only drive the SCSI bus when the |
| 1350 | * phase specified in the appropriate bits of the TARGET COMMAND |
| 1351 | * REGISTER match the STATUS REGISTER |
| 1352 | */ |
| 1353 | |
| 1354 | NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p)); |
| 1355 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | do { |
| 1357 | /* |
| 1358 | * Wait for assertion of REQ, after which the phase bits will be |
| 1359 | * valid |
| 1360 | */ |
| 1361 | |
Finn Thain | 686f399 | 2016-01-03 16:05:26 +1100 | [diff] [blame] | 1362 | if (NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1365 | dprintk(NDEBUG_HANDSHAKE, "scsi%d : REQ detected\n", instance->host_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | |
| 1367 | /* Check for phase mismatch */ |
Finn Thain | 686f399 | 2016-01-03 16:05:26 +1100 | [diff] [blame] | 1368 | if ((NCR5380_read(STATUS_REG) & PHASE_MASK) != p) { |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1369 | dprintk(NDEBUG_HANDSHAKE, "scsi%d : phase mismatch\n", instance->host_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance); |
| 1371 | break; |
| 1372 | } |
| 1373 | /* Do actual transfer from SCSI bus to / from memory */ |
| 1374 | if (!(p & SR_IO)) |
| 1375 | NCR5380_write(OUTPUT_DATA_REG, *d); |
| 1376 | else |
| 1377 | *d = NCR5380_read(CURRENT_SCSI_DATA_REG); |
| 1378 | |
| 1379 | ++d; |
| 1380 | |
| 1381 | /* |
| 1382 | * The SCSI standard suggests that in MSGOUT phase, the initiator |
| 1383 | * should drop ATN on the last byte of the message phase |
| 1384 | * after REQ has been asserted for the handshake but before |
| 1385 | * the initiator raises ACK. |
| 1386 | */ |
| 1387 | |
| 1388 | if (!(p & SR_IO)) { |
| 1389 | if (!((p & SR_MSG) && c > 1)) { |
| 1390 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA); |
| 1391 | NCR5380_dprint(NDEBUG_PIO, instance); |
| 1392 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK); |
| 1393 | } else { |
| 1394 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN); |
| 1395 | NCR5380_dprint(NDEBUG_PIO, instance); |
| 1396 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK); |
| 1397 | } |
| 1398 | } else { |
| 1399 | NCR5380_dprint(NDEBUG_PIO, instance); |
| 1400 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK); |
| 1401 | } |
| 1402 | |
Finn Thain | a2edc4a | 2016-01-03 16:05:27 +1100 | [diff] [blame] | 1403 | if (NCR5380_poll_politely(instance, |
| 1404 | STATUS_REG, SR_REQ, 0, 5 * HZ) < 0) |
| 1405 | break; |
| 1406 | |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1407 | dprintk(NDEBUG_HANDSHAKE, "scsi%d : req false, handshake complete\n", instance->host_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | |
| 1409 | /* |
| 1410 | * We have several special cases to consider during REQ/ACK handshaking : |
| 1411 | * 1. We were in MSGOUT phase, and we are on the last byte of the |
| 1412 | * message. ATN must be dropped as ACK is dropped. |
| 1413 | * |
| 1414 | * 2. We are in a MSGIN phase, and we are on the last byte of the |
| 1415 | * message. We must exit with ACK asserted, so that the calling |
| 1416 | * code may raise ATN before dropping ACK to reject the message. |
| 1417 | * |
| 1418 | * 3. ACK and ATN are clear and the target may proceed as normal. |
| 1419 | */ |
| 1420 | if (!(p == PHASE_MSGIN && c == 1)) { |
| 1421 | if (p == PHASE_MSGOUT && c > 1) |
| 1422 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); |
| 1423 | else |
| 1424 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1425 | } |
| 1426 | } while (--c); |
| 1427 | |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1428 | dprintk(NDEBUG_PIO, "scsi%d : residual %d\n", instance->host_no, c); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | |
| 1430 | *count = c; |
| 1431 | *data = d; |
| 1432 | tmp = NCR5380_read(STATUS_REG); |
Finn Thain | a2edc4a | 2016-01-03 16:05:27 +1100 | [diff] [blame] | 1433 | /* The phase read from the bus is valid if either REQ is (already) |
| 1434 | * asserted or if ACK hasn't been released yet. The latter applies if |
| 1435 | * we're in MSG IN, DATA IN or STATUS and all bytes have been received. |
| 1436 | */ |
| 1437 | if ((tmp & SR_REQ) || ((tmp & SR_IO) && c == 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | *phase = tmp & PHASE_MASK; |
| 1439 | else |
| 1440 | *phase = PHASE_UNKNOWN; |
| 1441 | |
| 1442 | if (!c || (*phase == p)) |
| 1443 | return 0; |
| 1444 | else |
| 1445 | return -1; |
| 1446 | } |
| 1447 | |
| 1448 | /** |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 1449 | * do_reset - issue a reset command |
| 1450 | * @instance: adapter to reset |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1451 | * |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 1452 | * Issue a reset sequence to the NCR5380 and try and get the bus |
| 1453 | * back into sane shape. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | * |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 1455 | * This clears the reset interrupt flag because there may be no handler for |
| 1456 | * it. When the driver is initialized, the NCR5380_intr() handler has not yet |
| 1457 | * been installed. And when in EH we may have released the ST DMA interrupt. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | */ |
| 1459 | |
Finn Thain | 54d8fe4 | 2016-01-03 16:05:06 +1100 | [diff] [blame] | 1460 | static void do_reset(struct Scsi_Host *instance) |
| 1461 | { |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 1462 | unsigned long flags; |
| 1463 | |
| 1464 | local_irq_save(flags); |
| 1465 | NCR5380_write(TARGET_COMMAND_REG, |
| 1466 | PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1467 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST); |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 1468 | udelay(50); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 1470 | (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG); |
| 1471 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
Finn Thain | 80d3eb6 | 2016-01-03 16:05:34 +1100 | [diff] [blame] | 1474 | /** |
| 1475 | * do_abort - abort the currently established nexus by going to |
| 1476 | * MESSAGE OUT phase and sending an ABORT message. |
| 1477 | * @instance: relevant scsi host instance |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | * |
Finn Thain | 80d3eb6 | 2016-01-03 16:05:34 +1100 | [diff] [blame] | 1479 | * Returns 0 on success, -1 on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | */ |
| 1481 | |
Finn Thain | 54d8fe4 | 2016-01-03 16:05:06 +1100 | [diff] [blame] | 1482 | static int do_abort(struct Scsi_Host *instance) |
| 1483 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | unsigned char *msgptr, phase, tmp; |
| 1485 | int len; |
| 1486 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | |
| 1488 | /* Request message out phase */ |
| 1489 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); |
| 1490 | |
| 1491 | /* |
| 1492 | * Wait for the target to indicate a valid phase by asserting |
| 1493 | * REQ. Once this happens, we'll have either a MSGOUT phase |
| 1494 | * and can immediately send the ABORT message, or we'll have some |
| 1495 | * other phase and will have to source/sink data. |
| 1496 | * |
| 1497 | * We really don't care what value was on the bus or what value |
| 1498 | * the target sees, so we just handshake. |
| 1499 | */ |
| 1500 | |
Finn Thain | 80d3eb6 | 2016-01-03 16:05:34 +1100 | [diff] [blame] | 1501 | rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 10 * HZ); |
Finn Thain | 1cc160e | 2016-01-03 16:05:32 +1100 | [diff] [blame] | 1502 | if (rc < 0) |
Finn Thain | 80d3eb6 | 2016-01-03 16:05:34 +1100 | [diff] [blame] | 1503 | goto timeout; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | |
Finn Thain | f35d347 | 2016-01-03 16:05:33 +1100 | [diff] [blame] | 1505 | tmp = NCR5380_read(STATUS_REG) & PHASE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | |
| 1507 | NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp)); |
| 1508 | |
Finn Thain | f35d347 | 2016-01-03 16:05:33 +1100 | [diff] [blame] | 1509 | if (tmp != PHASE_MSGOUT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK); |
Finn Thain | 54d8fe4 | 2016-01-03 16:05:06 +1100 | [diff] [blame] | 1511 | rc = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 3 * HZ); |
Finn Thain | 1cc160e | 2016-01-03 16:05:32 +1100 | [diff] [blame] | 1512 | if (rc < 0) |
Finn Thain | 80d3eb6 | 2016-01-03 16:05:34 +1100 | [diff] [blame] | 1513 | goto timeout; |
| 1514 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | } |
| 1516 | tmp = ABORT; |
| 1517 | msgptr = &tmp; |
| 1518 | len = 1; |
| 1519 | phase = PHASE_MSGOUT; |
Finn Thain | 54d8fe4 | 2016-01-03 16:05:06 +1100 | [diff] [blame] | 1520 | NCR5380_transfer_pio(instance, &phase, &len, &msgptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | |
| 1522 | /* |
| 1523 | * If we got here, and the command completed successfully, |
| 1524 | * we're about to go into bus free state. |
| 1525 | */ |
| 1526 | |
| 1527 | return len ? -1 : 0; |
Finn Thain | 80d3eb6 | 2016-01-03 16:05:34 +1100 | [diff] [blame] | 1528 | |
| 1529 | timeout: |
| 1530 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1531 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | #if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL) |
| 1535 | /* |
| 1536 | * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance, |
| 1537 | * unsigned char *phase, int *count, unsigned char **data) |
| 1538 | * |
| 1539 | * Purpose : transfers data in given phase using either real |
| 1540 | * or pseudo DMA. |
| 1541 | * |
| 1542 | * Inputs : instance - instance of driver, *phase - pointer to |
| 1543 | * what phase is expected, *count - pointer to number of |
| 1544 | * bytes to transfer, **data - pointer to data pointer. |
| 1545 | * |
| 1546 | * Returns : -1 when different phase is entered without transferring |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1547 | * maximum number of bytes, 0 if all bytes or transferred or exit |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | * is in same phase. |
| 1549 | * |
| 1550 | * Also, *phase, *count, *data are modified in place. |
| 1551 | * |
| 1552 | * Locks: io_request lock held by caller |
| 1553 | */ |
| 1554 | |
| 1555 | |
| 1556 | static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | register int c = *count; |
| 1558 | register unsigned char p = *phase; |
| 1559 | register unsigned char *d = *data; |
| 1560 | unsigned char tmp; |
| 1561 | int foo; |
| 1562 | #if defined(REAL_DMA_POLL) |
| 1563 | int cnt, toPIO; |
| 1564 | unsigned char saved_data = 0, overrun = 0, residue; |
| 1565 | #endif |
| 1566 | |
| 1567 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata; |
| 1568 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) { |
| 1570 | *phase = tmp; |
| 1571 | return -1; |
| 1572 | } |
| 1573 | #if defined(REAL_DMA) || defined(REAL_DMA_POLL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | if (p & SR_IO) { |
Finn Thain | 9db6024 | 2016-01-03 16:05:43 +1100 | [diff] [blame] | 1575 | if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS)) |
| 1576 | c -= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | } |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1578 | dprintk(NDEBUG_DMA, "scsi%d : initializing DMA channel %d for %s, %d bytes %s %0x\n", instance->host_no, instance->dma_channel, (p & SR_IO) ? "reading" : "writing", c, (p & SR_IO) ? "to" : "from", (unsigned) d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1579 | hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c); |
| 1580 | #endif |
| 1581 | |
| 1582 | NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p)); |
| 1583 | |
| 1584 | #ifdef REAL_DMA |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 1585 | NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY | |
| 1586 | MR_ENABLE_EOP_INTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | #elif defined(REAL_DMA_POLL) |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 1588 | NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | #else |
| 1590 | /* |
| 1591 | * Note : on my sample board, watch-dog timeouts occurred when interrupts |
| 1592 | * were not disabled for the duration of a single DMA transfer, from |
| 1593 | * before the setting of DMA mode to after transfer of the last byte. |
| 1594 | */ |
| 1595 | |
Finn Thain | 55181be | 2016-01-03 16:05:42 +1100 | [diff] [blame] | 1596 | if (hostdata->flags & FLAG_NO_DMA_FIXUP) |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 1597 | NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY | |
| 1598 | MR_ENABLE_EOP_INTR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | else |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 1600 | NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_MONITOR_BSY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1601 | #endif /* def REAL_DMA */ |
| 1602 | |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1603 | dprintk(NDEBUG_DMA, "scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1604 | |
| 1605 | /* |
| 1606 | * On the PAS16 at least I/O recovery delays are not needed here. |
| 1607 | * Everyone else seems to want them. |
| 1608 | */ |
| 1609 | |
| 1610 | if (p & SR_IO) { |
| 1611 | io_recovery_delay(1); |
| 1612 | NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0); |
| 1613 | } else { |
| 1614 | io_recovery_delay(1); |
| 1615 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA); |
| 1616 | io_recovery_delay(1); |
| 1617 | NCR5380_write(START_DMA_SEND_REG, 0); |
| 1618 | io_recovery_delay(1); |
| 1619 | } |
| 1620 | |
| 1621 | #if defined(REAL_DMA_POLL) |
| 1622 | do { |
| 1623 | tmp = NCR5380_read(BUS_AND_STATUS_REG); |
| 1624 | } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER))); |
| 1625 | |
| 1626 | /* |
| 1627 | At this point, either we've completed DMA, or we have a phase mismatch, |
| 1628 | or we've unexpectedly lost BUSY (which is a real error). |
| 1629 | |
| 1630 | For write DMAs, we want to wait until the last byte has been |
| 1631 | transferred out over the bus before we turn off DMA mode. Alas, there |
| 1632 | seems to be no terribly good way of doing this on a 5380 under all |
| 1633 | conditions. For non-scatter-gather operations, we can wait until REQ |
| 1634 | and ACK both go false, or until a phase mismatch occurs. Gather-writes |
| 1635 | are nastier, since the device will be expecting more data than we |
| 1636 | are prepared to send it, and REQ will remain asserted. On a 53C8[01] we |
| 1637 | could test LAST BIT SENT to assure transfer (I imagine this is precisely |
| 1638 | why this signal was added to the newer chips) but on the older 538[01] |
| 1639 | this signal does not exist. The workaround for this lack is a watchdog; |
| 1640 | we bail out of the wait-loop after a modest amount of wait-time if |
| 1641 | the usual exit conditions are not met. Not a terribly clean or |
| 1642 | correct solution :-% |
| 1643 | |
| 1644 | Reads are equally tricky due to a nasty characteristic of the NCR5380. |
| 1645 | If the chip is in DMA mode for an READ, it will respond to a target's |
| 1646 | REQ by latching the SCSI data into the INPUT DATA register and asserting |
| 1647 | ACK, even if it has _already_ been notified by the DMA controller that |
| 1648 | the current DMA transfer has completed! If the NCR5380 is then taken |
| 1649 | out of DMA mode, this already-acknowledged byte is lost. |
| 1650 | |
| 1651 | This is not a problem for "one DMA transfer per command" reads, because |
| 1652 | the situation will never arise... either all of the data is DMA'ed |
| 1653 | properly, or the target switches to MESSAGE IN phase to signal a |
| 1654 | disconnection (either operation bringing the DMA to a clean halt). |
| 1655 | However, in order to handle scatter-reads, we must work around the |
| 1656 | problem. The chosen fix is to DMA N-2 bytes, then check for the |
| 1657 | condition before taking the NCR5380 out of DMA mode. One or two extra |
| 1658 | bytes are transferred via PIO as necessary to fill out the original |
| 1659 | request. |
| 1660 | */ |
| 1661 | |
| 1662 | if (p & SR_IO) { |
Finn Thain | 9db6024 | 2016-01-03 16:05:43 +1100 | [diff] [blame] | 1663 | if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS)) { |
| 1664 | udelay(10); |
| 1665 | if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) == |
| 1666 | (BASR_PHASE_MATCH | BASR_ACK)) { |
| 1667 | saved_data = NCR5380_read(INPUT_DATA_REGISTER); |
| 1668 | overrun = 1; |
| 1669 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | } else { |
| 1672 | int limit = 100; |
| 1673 | while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) { |
| 1674 | if (!(tmp & BASR_PHASE_MATCH)) |
| 1675 | break; |
| 1676 | if (--limit < 0) |
| 1677 | break; |
| 1678 | } |
| 1679 | } |
| 1680 | |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1681 | dprintk(NDEBUG_DMA, "scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n", instance->host_no, tmp, NCR5380_read(STATUS_REG)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | |
| 1683 | NCR5380_write(MODE_REG, MR_BASE); |
| 1684 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1685 | |
| 1686 | residue = NCR5380_dma_residual(instance); |
| 1687 | c -= residue; |
| 1688 | *count -= c; |
| 1689 | *data += c; |
| 1690 | *phase = NCR5380_read(STATUS_REG) & PHASE_MASK; |
| 1691 | |
Finn Thain | 9db6024 | 2016-01-03 16:05:43 +1100 | [diff] [blame] | 1692 | if (!(hostdata->flags & FLAG_NO_DMA_FIXUPS) && |
| 1693 | *phase == p && (p & SR_IO) && residue == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | if (overrun) { |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1695 | dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | **data = saved_data; |
| 1697 | *data += 1; |
| 1698 | *count -= 1; |
| 1699 | cnt = toPIO = 1; |
| 1700 | } else { |
| 1701 | printk("No overrun??\n"); |
| 1702 | cnt = toPIO = 2; |
| 1703 | } |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1704 | dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%X\n", cnt, *data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | NCR5380_transfer_pio(instance, phase, &cnt, data); |
| 1706 | *count -= toPIO - cnt; |
| 1707 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1709 | dprintk(NDEBUG_DMA, "Return with data ptr = 0x%X, count %d, last 0x%X, next 0x%X\n", *data, *count, *(*data + *count - 1), *(*data + *count)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | return 0; |
| 1711 | |
| 1712 | #elif defined(REAL_DMA) |
| 1713 | return 0; |
| 1714 | #else /* defined(REAL_DMA_POLL) */ |
| 1715 | if (p & SR_IO) { |
Finn Thain | 55181be | 2016-01-03 16:05:42 +1100 | [diff] [blame] | 1716 | foo = NCR5380_pread(instance, d, |
| 1717 | hostdata->flags & FLAG_NO_DMA_FIXUP ? c : c - 1); |
| 1718 | if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | /* |
| 1720 | * We can't disable DMA mode after successfully transferring |
| 1721 | * what we plan to be the last byte, since that would open up |
| 1722 | * a race condition where if the target asserted REQ before |
| 1723 | * we got the DMA mode reset, the NCR5380 would have latched |
| 1724 | * an additional byte into the INPUT DATA register and we'd |
| 1725 | * have dropped it. |
| 1726 | * |
| 1727 | * The workaround was to transfer one fewer bytes than we |
| 1728 | * intended to with the pseudo-DMA read function, wait for |
| 1729 | * the chip to latch the last byte, read it, and then disable |
| 1730 | * pseudo-DMA mode. |
| 1731 | * |
| 1732 | * After REQ is asserted, the NCR5380 asserts DRQ and ACK. |
| 1733 | * REQ is deasserted when ACK is asserted, and not reasserted |
| 1734 | * until ACK goes false. Since the NCR5380 won't lower ACK |
| 1735 | * until DACK is asserted, which won't happen unless we twiddle |
| 1736 | * the DMA port or we take the NCR5380 out of DMA mode, we |
| 1737 | * can guarantee that we won't handshake another extra |
| 1738 | * byte. |
| 1739 | */ |
| 1740 | |
Finn Thain | 55181be | 2016-01-03 16:05:42 +1100 | [diff] [blame] | 1741 | if (NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, |
| 1742 | BASR_DRQ, BASR_DRQ, HZ) < 0) { |
| 1743 | foo = -1; |
| 1744 | shost_printk(KERN_ERR, instance, "PDMA read: DRQ timeout\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | } |
Finn Thain | 55181be | 2016-01-03 16:05:42 +1100 | [diff] [blame] | 1746 | if (NCR5380_poll_politely(instance, STATUS_REG, |
| 1747 | SR_REQ, 0, HZ) < 0) { |
| 1748 | foo = -1; |
| 1749 | shost_printk(KERN_ERR, instance, "PDMA read: !REQ timeout\n"); |
| 1750 | } |
| 1751 | d[c - 1] = NCR5380_read(INPUT_DATA_REG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1752 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | foo = NCR5380_pwrite(instance, d, c); |
Finn Thain | 55181be | 2016-01-03 16:05:42 +1100 | [diff] [blame] | 1755 | if (!foo && !(hostdata->flags & FLAG_NO_DMA_FIXUP)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | /* |
| 1757 | * Wait for the last byte to be sent. If REQ is being asserted for |
| 1758 | * the byte we're interested, we'll ACK it and it will go false. |
| 1759 | */ |
Finn Thain | 55181be | 2016-01-03 16:05:42 +1100 | [diff] [blame] | 1760 | if (NCR5380_poll_politely2(instance, |
| 1761 | BUS_AND_STATUS_REG, BASR_DRQ, BASR_DRQ, |
| 1762 | BUS_AND_STATUS_REG, BASR_PHASE_MATCH, 0, HZ) < 0) { |
| 1763 | foo = -1; |
| 1764 | shost_printk(KERN_ERR, instance, "PDMA write: DRQ and phase timeout\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1765 | } |
| 1766 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | } |
| 1768 | NCR5380_write(MODE_REG, MR_BASE); |
| 1769 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
Finn Thain | cd40082 | 2016-01-03 16:05:40 +1100 | [diff] [blame] | 1770 | NCR5380_read(RESET_PARITY_INTERRUPT_REG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1771 | *data = d + c; |
| 1772 | *count = 0; |
| 1773 | *phase = NCR5380_read(STATUS_REG) & PHASE_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1774 | return foo; |
| 1775 | #endif /* def REAL_DMA */ |
| 1776 | } |
| 1777 | #endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */ |
| 1778 | |
| 1779 | /* |
| 1780 | * Function : NCR5380_information_transfer (struct Scsi_Host *instance) |
| 1781 | * |
| 1782 | * Purpose : run through the various SCSI phases and do as the target |
| 1783 | * directs us to. Operates on the currently connected command, |
| 1784 | * instance->connected. |
| 1785 | * |
| 1786 | * Inputs : instance, instance for which we are doing commands |
| 1787 | * |
| 1788 | * Side effects : SCSI things happen, the disconnected queue will be |
| 1789 | * modified if a command disconnects, *instance->connected will |
| 1790 | * change. |
| 1791 | * |
| 1792 | * XXX Note : we need to watch for bus free or a reset condition here |
| 1793 | * to recover from an unexpected bus free condition. |
| 1794 | * |
| 1795 | * Locks: io_request_lock held by caller in IRQ mode |
| 1796 | */ |
| 1797 | |
| 1798 | static void NCR5380_information_transfer(struct Scsi_Host *instance) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)instance->hostdata; |
| 1800 | unsigned char msgout = NOP; |
| 1801 | int sink = 0; |
| 1802 | int len; |
| 1803 | #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) |
| 1804 | int transfersize; |
| 1805 | #endif |
| 1806 | unsigned char *data; |
| 1807 | unsigned char phase, tmp, extended_msg[10], old_phase = 0xff; |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 1808 | struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | while (1) { |
| 1811 | tmp = NCR5380_read(STATUS_REG); |
| 1812 | /* We only have a valid SCSI phase when REQ is asserted */ |
| 1813 | if (tmp & SR_REQ) { |
| 1814 | phase = (tmp & PHASE_MASK); |
| 1815 | if (phase != old_phase) { |
| 1816 | old_phase = phase; |
| 1817 | NCR5380_dprint_phase(NDEBUG_INFORMATION, instance); |
| 1818 | } |
| 1819 | if (sink && (phase != PHASE_MSGOUT)) { |
| 1820 | NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp)); |
| 1821 | |
| 1822 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK); |
| 1823 | while (NCR5380_read(STATUS_REG) & SR_REQ); |
| 1824 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); |
| 1825 | sink = 0; |
| 1826 | continue; |
| 1827 | } |
| 1828 | switch (phase) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | case PHASE_DATAOUT: |
| 1830 | #if (NDEBUG & NDEBUG_NO_DATAOUT) |
| 1831 | printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no); |
| 1832 | sink = 1; |
| 1833 | do_abort(instance); |
| 1834 | cmd->result = DID_ERROR << 16; |
Matthew Wilcox | cce99c6 | 2007-09-25 12:42:01 -0400 | [diff] [blame] | 1835 | cmd->scsi_done(cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | return; |
| 1837 | #endif |
Finn Thain | bf1a0c6 | 2016-01-03 16:05:47 +1100 | [diff] [blame] | 1838 | case PHASE_DATAIN: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | /* |
| 1840 | * If there is no room left in the current buffer in the |
| 1841 | * scatter-gather list, move onto the next one. |
| 1842 | */ |
| 1843 | |
| 1844 | if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) { |
| 1845 | ++cmd->SCp.buffer; |
| 1846 | --cmd->SCp.buffers_residual; |
| 1847 | cmd->SCp.this_residual = cmd->SCp.buffer->length; |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 1848 | cmd->SCp.ptr = sg_virt(cmd->SCp.buffer); |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1849 | dprintk(NDEBUG_INFORMATION, "scsi%d : %d bytes and %d buffers left\n", instance->host_no, cmd->SCp.this_residual, cmd->SCp.buffers_residual); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1850 | } |
| 1851 | /* |
| 1852 | * The preferred transfer method is going to be |
| 1853 | * PSEUDO-DMA for systems that are strictly PIO, |
| 1854 | * since we can let the hardware do the handshaking. |
| 1855 | * |
| 1856 | * For this to work, we need to know the transfersize |
| 1857 | * ahead of time, since the pseudo-DMA code will sit |
| 1858 | * in an unconditional loop. |
| 1859 | */ |
| 1860 | |
| 1861 | #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) |
Finn Thain | ff3d457 | 2016-01-03 16:05:25 +1100 | [diff] [blame] | 1862 | transfersize = 0; |
| 1863 | if (!cmd->device->borken && |
| 1864 | !(hostdata->flags & FLAG_NO_PSEUDO_DMA)) |
| 1865 | transfersize = NCR5380_dma_xfer_len(instance, cmd, phase); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1866 | |
Finn Thain | ff3d457 | 2016-01-03 16:05:25 +1100 | [diff] [blame] | 1867 | if (transfersize) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | len = transfersize; |
| 1869 | if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) { |
| 1870 | /* |
| 1871 | * If the watchdog timer fires, all future accesses to this |
| 1872 | * device will use the polled-IO. |
| 1873 | */ |
Jeff Garzik | 017560f | 2005-10-24 18:04:36 -0400 | [diff] [blame] | 1874 | scmd_printk(KERN_INFO, cmd, |
| 1875 | "switching to slow handshake\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | cmd->device->borken = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | sink = 1; |
| 1878 | do_abort(instance); |
| 1879 | cmd->result = DID_ERROR << 16; |
Matthew Wilcox | cce99c6 | 2007-09-25 12:42:01 -0400 | [diff] [blame] | 1880 | cmd->scsi_done(cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1881 | /* XXX - need to source or sink data here, as appropriate */ |
| 1882 | } else |
| 1883 | cmd->SCp.this_residual -= transfersize - len; |
| 1884 | } else |
| 1885 | #endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */ |
| 1886 | NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **) |
| 1887 | &cmd->SCp.ptr); |
| 1888 | break; |
| 1889 | case PHASE_MSGIN: |
| 1890 | len = 1; |
| 1891 | data = &tmp; |
| 1892 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
| 1893 | cmd->SCp.Message = tmp; |
| 1894 | |
| 1895 | switch (tmp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | case ABORT: |
| 1897 | case COMMAND_COMPLETE: |
| 1898 | /* Accept message by clearing ACK */ |
| 1899 | sink = 1; |
| 1900 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1901 | hostdata->connected = NULL; |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 1902 | dprintk(NDEBUG_QUEUES, "scsi%d : command for target %d, lun %llu completed\n", instance->host_no, cmd->device->id, cmd->device->lun); |
| 1903 | hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | |
| 1905 | /* |
| 1906 | * I'm not sure what the correct thing to do here is : |
| 1907 | * |
| 1908 | * If the command that just executed is NOT a request |
| 1909 | * sense, the obvious thing to do is to set the result |
| 1910 | * code to the values of the stored parameters. |
| 1911 | * |
| 1912 | * If it was a REQUEST SENSE command, we need some way |
| 1913 | * to differentiate between the failure code of the original |
| 1914 | * and the failure code of the REQUEST sense - the obvious |
| 1915 | * case is success, where we fall through and leave the result |
| 1916 | * code unchanged. |
| 1917 | * |
| 1918 | * The non-obvious place is where the REQUEST SENSE failed |
| 1919 | */ |
| 1920 | |
| 1921 | if (cmd->cmnd[0] != REQUEST_SENSE) |
| 1922 | cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8); |
| 1923 | else if (status_byte(cmd->SCp.Status) != GOOD) |
| 1924 | cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16); |
| 1925 | |
Boaz Harrosh | 28424d3 | 2007-09-10 22:37:45 +0300 | [diff] [blame] | 1926 | if ((cmd->cmnd[0] == REQUEST_SENSE) && |
| 1927 | hostdata->ses.cmd_len) { |
| 1928 | scsi_eh_restore_cmnd(cmd, &hostdata->ses); |
| 1929 | hostdata->ses.cmd_len = 0 ; |
| 1930 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1931 | |
Boaz Harrosh | 28424d3 | 2007-09-10 22:37:45 +0300 | [diff] [blame] | 1932 | if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) { |
| 1933 | scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0); |
| 1934 | |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1935 | dprintk(NDEBUG_AUTOSENSE, "scsi%d : performing request sense\n", instance->host_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | |
| 1937 | LIST(cmd, hostdata->issue_queue); |
| 1938 | cmd->host_scribble = (unsigned char *) |
| 1939 | hostdata->issue_queue; |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 1940 | hostdata->issue_queue = (struct scsi_cmnd *) cmd; |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 1941 | dprintk(NDEBUG_QUEUES, "scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no); |
Finn Thain | 997acab | 2014-11-12 16:11:54 +1100 | [diff] [blame] | 1942 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | cmd->scsi_done(cmd); |
| 1944 | } |
| 1945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | /* |
| 1947 | * Restore phase bits to 0 so an interrupted selection, |
| 1948 | * arbitration can resume. |
| 1949 | */ |
| 1950 | NCR5380_write(TARGET_COMMAND_REG, 0); |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 1951 | |
| 1952 | /* Enable reselect interrupts */ |
| 1953 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1954 | return; |
| 1955 | case MESSAGE_REJECT: |
| 1956 | /* Accept message by clearing ACK */ |
| 1957 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1958 | switch (hostdata->last_message) { |
| 1959 | case HEAD_OF_QUEUE_TAG: |
| 1960 | case ORDERED_QUEUE_TAG: |
| 1961 | case SIMPLE_QUEUE_TAG: |
| 1962 | cmd->device->simple_tags = 0; |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 1963 | hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | break; |
| 1965 | default: |
| 1966 | break; |
| 1967 | } |
Finn Thain | 340b961 | 2016-01-03 16:05:31 +1100 | [diff] [blame] | 1968 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | case DISCONNECT:{ |
| 1970 | /* Accept message by clearing ACK */ |
| 1971 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | LIST(cmd, hostdata->disconnected_queue); |
| 1973 | cmd->host_scribble = (unsigned char *) |
| 1974 | hostdata->disconnected_queue; |
| 1975 | hostdata->connected = NULL; |
| 1976 | hostdata->disconnected_queue = cmd; |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 1977 | dprintk(NDEBUG_QUEUES, "scsi%d : command for target %d lun %llu was moved from connected to" " the disconnected_queue\n", instance->host_no, cmd->device->id, cmd->device->lun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1978 | /* |
| 1979 | * Restore phase bits to 0 so an interrupted selection, |
| 1980 | * arbitration can resume. |
| 1981 | */ |
| 1982 | NCR5380_write(TARGET_COMMAND_REG, 0); |
| 1983 | |
| 1984 | /* Enable reselect interrupts */ |
| 1985 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | return; |
| 1987 | } |
| 1988 | /* |
| 1989 | * The SCSI data pointer is *IMPLICITLY* saved on a disconnect |
| 1990 | * operation, in violation of the SCSI spec so we can safely |
| 1991 | * ignore SAVE/RESTORE pointers calls. |
| 1992 | * |
| 1993 | * Unfortunately, some disks violate the SCSI spec and |
| 1994 | * don't issue the required SAVE_POINTERS message before |
| 1995 | * disconnecting, and we have to break spec to remain |
| 1996 | * compatible. |
| 1997 | */ |
| 1998 | case SAVE_POINTERS: |
| 1999 | case RESTORE_POINTERS: |
| 2000 | /* Accept message by clearing ACK */ |
| 2001 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 2002 | break; |
| 2003 | case EXTENDED_MESSAGE: |
| 2004 | /* |
| 2005 | * Extended messages are sent in the following format : |
| 2006 | * Byte |
| 2007 | * 0 EXTENDED_MESSAGE == 1 |
| 2008 | * 1 length (includes one byte for code, doesn't |
| 2009 | * include first two bytes) |
| 2010 | * 2 code |
| 2011 | * 3..length+1 arguments |
| 2012 | * |
| 2013 | * Start the extended message buffer with the EXTENDED_MESSAGE |
Matthew Wilcox | 1abfd37 | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 2014 | * byte, since spi_print_msg() wants the whole thing. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | */ |
| 2016 | extended_msg[0] = EXTENDED_MESSAGE; |
| 2017 | /* Accept first byte by clearing ACK */ |
| 2018 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 2019 | dprintk(NDEBUG_EXTENDED, "scsi%d : receiving extended message\n", instance->host_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2020 | |
| 2021 | len = 2; |
| 2022 | data = extended_msg + 1; |
| 2023 | phase = PHASE_MSGIN; |
| 2024 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
| 2025 | |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 2026 | dprintk(NDEBUG_EXTENDED, "scsi%d : length=%d, code=0x%02x\n", instance->host_no, (int) extended_msg[1], (int) extended_msg[2]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | |
Finn Thain | e0783ed | 2016-01-03 16:05:45 +1100 | [diff] [blame] | 2028 | if (!len && extended_msg[1] > 0 && |
| 2029 | extended_msg[1] <= sizeof(extended_msg) - 2) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2030 | /* Accept third byte by clearing ACK */ |
| 2031 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 2032 | len = extended_msg[1] - 1; |
| 2033 | data = extended_msg + 3; |
| 2034 | phase = PHASE_MSGIN; |
| 2035 | |
| 2036 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 2037 | dprintk(NDEBUG_EXTENDED, "scsi%d : message received, residual %d\n", instance->host_no, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | |
| 2039 | switch (extended_msg[2]) { |
| 2040 | case EXTENDED_SDTR: |
| 2041 | case EXTENDED_WDTR: |
| 2042 | case EXTENDED_MODIFY_DATA_POINTER: |
| 2043 | case EXTENDED_EXTENDED_IDENTIFY: |
| 2044 | tmp = 0; |
| 2045 | } |
| 2046 | } else if (len) { |
| 2047 | printk("scsi%d: error receiving extended message\n", instance->host_no); |
| 2048 | tmp = 0; |
| 2049 | } else { |
| 2050 | printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]); |
| 2051 | tmp = 0; |
| 2052 | } |
| 2053 | /* Fall through to reject message */ |
| 2054 | |
| 2055 | /* |
| 2056 | * If we get something weird that we aren't expecting, |
| 2057 | * reject it. |
| 2058 | */ |
| 2059 | default: |
| 2060 | if (!tmp) { |
| 2061 | printk("scsi%d: rejecting message ", instance->host_no); |
Matthew Wilcox | 1abfd37 | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 2062 | spi_print_msg(extended_msg); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | printk("\n"); |
| 2064 | } else if (tmp != EXTENDED_MESSAGE) |
Jeff Garzik | 017560f | 2005-10-24 18:04:36 -0400 | [diff] [blame] | 2065 | scmd_printk(KERN_INFO, cmd, |
| 2066 | "rejecting unknown message %02x\n",tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2067 | else |
Jeff Garzik | 017560f | 2005-10-24 18:04:36 -0400 | [diff] [blame] | 2068 | scmd_printk(KERN_INFO, cmd, |
| 2069 | "rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | |
| 2071 | msgout = MESSAGE_REJECT; |
| 2072 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); |
| 2073 | break; |
| 2074 | } /* switch (tmp) */ |
| 2075 | break; |
| 2076 | case PHASE_MSGOUT: |
| 2077 | len = 1; |
| 2078 | data = &msgout; |
| 2079 | hostdata->last_message = msgout; |
| 2080 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
| 2081 | if (msgout == ABORT) { |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 2082 | hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xFF)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | hostdata->connected = NULL; |
| 2084 | cmd->result = DID_ERROR << 16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | cmd->scsi_done(cmd); |
| 2086 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
| 2087 | return; |
| 2088 | } |
| 2089 | msgout = NOP; |
| 2090 | break; |
| 2091 | case PHASE_CMDOUT: |
| 2092 | len = cmd->cmd_len; |
| 2093 | data = cmd->cmnd; |
| 2094 | /* |
| 2095 | * XXX for performance reasons, on machines with a |
| 2096 | * PSEUDO-DMA architecture we should probably |
| 2097 | * use the dma transfer function. |
| 2098 | */ |
| 2099 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | break; |
| 2101 | case PHASE_STATIN: |
| 2102 | len = 1; |
| 2103 | data = &tmp; |
| 2104 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
| 2105 | cmd->SCp.Status = tmp; |
| 2106 | break; |
| 2107 | default: |
| 2108 | printk("scsi%d : unknown phase\n", instance->host_no); |
Finn Thain | 4dde8f7 | 2014-03-18 11:42:17 +1100 | [diff] [blame] | 2109 | NCR5380_dprint(NDEBUG_ANY, instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | } /* switch(phase) */ |
Finn Thain | 686f399 | 2016-01-03 16:05:26 +1100 | [diff] [blame] | 2111 | } else { |
| 2112 | spin_unlock_irq(instance->host_lock); |
| 2113 | NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ); |
| 2114 | spin_lock_irq(instance->host_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | } |
| 2116 | } /* while (1) */ |
| 2117 | } |
| 2118 | |
| 2119 | /* |
| 2120 | * Function : void NCR5380_reselect (struct Scsi_Host *instance) |
| 2121 | * |
| 2122 | * Purpose : does reselection, initializing the instance->connected |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2123 | * field to point to the scsi_cmnd for which the I_T_L or I_T_L_Q |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | * nexus has been reestablished, |
| 2125 | * |
| 2126 | * Inputs : instance - this instance of the NCR5380. |
| 2127 | * |
| 2128 | * Locks: io_request_lock held by caller if IRQ driven |
| 2129 | */ |
| 2130 | |
| 2131 | static void NCR5380_reselect(struct Scsi_Host *instance) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2132 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) |
| 2133 | instance->hostdata; |
| 2134 | unsigned char target_mask; |
| 2135 | unsigned char lun, phase; |
| 2136 | int len; |
| 2137 | unsigned char msg[3]; |
| 2138 | unsigned char *data; |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2139 | struct scsi_cmnd *tmp = NULL, *prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | |
| 2141 | /* |
| 2142 | * Disable arbitration, etc. since the host adapter obviously |
| 2143 | * lost, and tell an interrupted NCR5380_select() to restart. |
| 2144 | */ |
| 2145 | |
| 2146 | NCR5380_write(MODE_REG, MR_BASE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | |
| 2148 | target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask); |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2149 | dprintk(NDEBUG_RESELECTION, "scsi%d : reselect\n", instance->host_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | |
| 2151 | /* |
| 2152 | * At this point, we have detected that our SCSI ID is on the bus, |
| 2153 | * SEL is true and BSY was false for at least one bus settle delay |
| 2154 | * (400 ns). |
| 2155 | * |
| 2156 | * We must assert BSY ourselves, until the target drops the SEL |
| 2157 | * signal. |
| 2158 | */ |
| 2159 | |
| 2160 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY); |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2161 | if (NCR5380_poll_politely(instance, |
| 2162 | STATUS_REG, SR_SEL, 0, 2 * HZ) < 0) { |
| 2163 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 2164 | return; |
| 2165 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2166 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 2167 | |
| 2168 | /* |
| 2169 | * Wait for target to go into MSGIN. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | */ |
| 2171 | |
Finn Thain | 1cc160e | 2016-01-03 16:05:32 +1100 | [diff] [blame] | 2172 | if (NCR5380_poll_politely(instance, |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2173 | STATUS_REG, SR_REQ, SR_REQ, 2 * HZ) < 0) { |
| 2174 | do_abort(instance); |
| 2175 | return; |
| 2176 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | |
| 2178 | len = 1; |
| 2179 | data = msg; |
| 2180 | phase = PHASE_MSGIN; |
| 2181 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
| 2182 | |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2183 | if (len) { |
| 2184 | do_abort(instance); |
| 2185 | return; |
| 2186 | } |
| 2187 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | if (!(msg[0] & 0x80)) { |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2189 | shost_printk(KERN_ERR, instance, "expecting IDENTIFY message, got "); |
Matthew Wilcox | 1abfd37 | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 2190 | spi_print_msg(msg); |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2191 | printk("\n"); |
| 2192 | do_abort(instance); |
| 2193 | return; |
| 2194 | } |
| 2195 | lun = msg[0] & 0x07; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2197 | /* |
| 2198 | * We need to add code for SCSI-II to track which devices have |
| 2199 | * I_T_L_Q nexuses established, and which have simple I_T_L |
| 2200 | * nexuses so we can chose to do additional data transfer. |
| 2201 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2202 | |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2203 | /* |
| 2204 | * Find the command corresponding to the I_T_L or I_T_L_Q nexus we |
| 2205 | * just reestablished, and remove it from the disconnected queue. |
| 2206 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2207 | |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2208 | for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL; |
| 2209 | tmp; prev = tmp, tmp = (struct scsi_cmnd *) tmp->host_scribble) { |
| 2210 | if ((target_mask == (1 << tmp->device->id)) && (lun == (u8)tmp->device->lun)) { |
| 2211 | if (prev) { |
| 2212 | REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble); |
| 2213 | prev->host_scribble = tmp->host_scribble; |
| 2214 | } else { |
| 2215 | REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble); |
| 2216 | hostdata->disconnected_queue = |
| 2217 | (struct scsi_cmnd *) tmp->host_scribble; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2218 | } |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2219 | tmp->host_scribble = NULL; |
| 2220 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | } |
| 2222 | } |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2223 | if (!tmp) { |
| 2224 | shost_printk(KERN_ERR, instance, "target bitmask 0x%02x lun %d not in disconnected queue.\n", |
| 2225 | target_mask, lun); |
| 2226 | /* |
| 2227 | * Since we have an established nexus that we can't do anything with, |
| 2228 | * we must abort it. |
| 2229 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2230 | do_abort(instance); |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2231 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | } |
Finn Thain | 72064a7 | 2016-01-03 16:05:44 +1100 | [diff] [blame] | 2233 | |
| 2234 | /* Accept message by clearing ACK */ |
| 2235 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 2236 | |
| 2237 | hostdata->connected = tmp; |
| 2238 | dprintk(NDEBUG_RESELECTION, "scsi%d : nexus established, target = %d, lun = %llu, tag = %d\n", |
| 2239 | instance->host_no, tmp->device->id, tmp->device->lun, tmp->tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | } |
| 2241 | |
| 2242 | /* |
| 2243 | * Function : void NCR5380_dma_complete (struct Scsi_Host *instance) |
| 2244 | * |
| 2245 | * Purpose : called by interrupt handler when DMA finishes or a phase |
| 2246 | * mismatch occurs (which would finish the DMA transfer). |
| 2247 | * |
| 2248 | * Inputs : instance - this instance of the NCR5380. |
| 2249 | * |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2250 | * Returns : pointer to the scsi_cmnd structure for which the I_T_L |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | * nexus has been reestablished, on failure NULL is returned. |
| 2252 | */ |
| 2253 | |
| 2254 | #ifdef REAL_DMA |
| 2255 | static void NCR5380_dma_complete(NCR5380_instance * instance) { |
Yoann Padioleau | f834368 | 2007-06-01 00:46:36 -0700 | [diff] [blame] | 2256 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | int transferred; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | |
| 2259 | /* |
| 2260 | * XXX this might not be right. |
| 2261 | * |
| 2262 | * Wait for final byte to transfer, ie wait for ACK to go false. |
| 2263 | * |
| 2264 | * We should use the Last Byte Sent bit, unfortunately this is |
| 2265 | * not available on the 5380/5381 (only the various CMOS chips) |
| 2266 | * |
| 2267 | * FIXME: timeout, and need to handle long timeout/irq case |
| 2268 | */ |
| 2269 | |
| 2270 | NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ); |
| 2271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 2273 | |
| 2274 | /* |
| 2275 | * The only places we should see a phase mismatch and have to send |
| 2276 | * data from the same set of pointers will be the data transfer |
| 2277 | * phases. So, residual, requested length are only important here. |
| 2278 | */ |
| 2279 | |
| 2280 | if (!(hostdata->connected->SCp.phase & SR_CD)) { |
| 2281 | transferred = instance->dmalen - NCR5380_dma_residual(); |
| 2282 | hostdata->connected->SCp.this_residual -= transferred; |
| 2283 | hostdata->connected->SCp.ptr += transferred; |
| 2284 | } |
| 2285 | } |
| 2286 | #endif /* def REAL_DMA */ |
| 2287 | |
| 2288 | /* |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2289 | * Function : int NCR5380_abort (struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2290 | * |
| 2291 | * Purpose : abort a command |
| 2292 | * |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2293 | * Inputs : cmd - the scsi_cmnd to abort, code - code to set the |
Hannes Reinecke | b6c92b7 | 2014-10-30 09:44:36 +0100 | [diff] [blame] | 2294 | * host byte of the result field to, if zero DID_ABORTED is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2295 | * used. |
| 2296 | * |
Hannes Reinecke | b6c92b7 | 2014-10-30 09:44:36 +0100 | [diff] [blame] | 2297 | * Returns : SUCCESS - success, FAILED on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2298 | * |
Hannes Reinecke | b6c92b7 | 2014-10-30 09:44:36 +0100 | [diff] [blame] | 2299 | * XXX - there is no way to abort the command that is currently |
| 2300 | * connected, you have to wait for it to complete. If this is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2301 | * a problem, we could implement longjmp() / setjmp(), setjmp() |
| 2302 | * called where the loop started in NCR5380_main(). |
| 2303 | * |
| 2304 | * Locks: host lock taken by caller |
| 2305 | */ |
| 2306 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2307 | static int NCR5380_abort(struct scsi_cmnd *cmd) |
| 2308 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2309 | struct Scsi_Host *instance = cmd->device->host; |
| 2310 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata; |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2311 | struct scsi_cmnd *tmp, **prev; |
Hannes Reinecke | 1fa6b5f | 2014-10-24 14:26:58 +0200 | [diff] [blame] | 2312 | |
| 2313 | scmd_printk(KERN_WARNING, cmd, "aborting command\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2314 | |
Finn Thain | 9dafbd9 | 2016-01-03 16:05:28 +1100 | [diff] [blame] | 2315 | spin_lock_irq(instance->host_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2316 | NCR5380_print_status(instance); |
| 2317 | |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 2318 | dprintk(NDEBUG_ABORT, "scsi%d : abort called\n", instance->host_no); |
| 2319 | dprintk(NDEBUG_ABORT, " basr 0x%X, sr 0x%X\n", NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2320 | |
| 2321 | #if 0 |
| 2322 | /* |
| 2323 | * Case 1 : If the command is the currently executing command, |
| 2324 | * we'll set the aborted flag and return control so that |
| 2325 | * information transfer routine can exit cleanly. |
| 2326 | */ |
| 2327 | |
| 2328 | if (hostdata->connected == cmd) { |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 2329 | dprintk(NDEBUG_ABORT, "scsi%d : aborting connected command\n", instance->host_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | /* |
| 2331 | * We should perform BSY checking, and make sure we haven't slipped |
| 2332 | * into BUS FREE. |
| 2333 | */ |
| 2334 | |
| 2335 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); |
| 2336 | /* |
| 2337 | * Since we can't change phases until we've completed the current |
| 2338 | * handshake, we have to source or sink a byte of data if the current |
| 2339 | * phase is not MSGOUT. |
| 2340 | */ |
| 2341 | |
| 2342 | /* |
| 2343 | * Return control to the executing NCR drive so we can clear the |
| 2344 | * aborted flag and get back into our main loop. |
| 2345 | */ |
| 2346 | |
Hannes Reinecke | b6c92b7 | 2014-10-30 09:44:36 +0100 | [diff] [blame] | 2347 | return SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | } |
| 2349 | #endif |
| 2350 | |
| 2351 | /* |
| 2352 | * Case 2 : If the command hasn't been issued yet, we simply remove it |
| 2353 | * from the issue queue. |
| 2354 | */ |
| 2355 | |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 2356 | dprintk(NDEBUG_ABORT, "scsi%d : abort going into loop.\n", instance->host_no); |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2357 | for (prev = (struct scsi_cmnd **) &(hostdata->issue_queue), tmp = (struct scsi_cmnd *) hostdata->issue_queue; tmp; prev = (struct scsi_cmnd **) &(tmp->host_scribble), tmp = (struct scsi_cmnd *) tmp->host_scribble) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2358 | if (cmd == tmp) { |
| 2359 | REMOVE(5, *prev, tmp, tmp->host_scribble); |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2360 | (*prev) = (struct scsi_cmnd *) tmp->host_scribble; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | tmp->host_scribble = NULL; |
Finn Thain | 9dafbd9 | 2016-01-03 16:05:28 +1100 | [diff] [blame] | 2362 | spin_unlock_irq(instance->host_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2363 | tmp->result = DID_ABORT << 16; |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 2364 | dprintk(NDEBUG_ABORT, "scsi%d : abort removed command from issue queue.\n", instance->host_no); |
Matthew Wilcox | cce99c6 | 2007-09-25 12:42:01 -0400 | [diff] [blame] | 2365 | tmp->scsi_done(tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2366 | return SUCCESS; |
| 2367 | } |
| 2368 | #if (NDEBUG & NDEBUG_ABORT) |
| 2369 | /* KLL */ |
| 2370 | else if (prev == tmp) |
| 2371 | printk(KERN_ERR "scsi%d : LOOP\n", instance->host_no); |
| 2372 | #endif |
| 2373 | |
| 2374 | /* |
| 2375 | * Case 3 : If any commands are connected, we're going to fail the abort |
| 2376 | * and let the high level SCSI driver retry at a later time or |
| 2377 | * issue a reset. |
| 2378 | * |
| 2379 | * Timeouts, and therefore aborted commands, will be highly unlikely |
| 2380 | * and handling them cleanly in this situation would make the common |
| 2381 | * case of noresets less efficient, and would pollute our code. So, |
| 2382 | * we fail. |
| 2383 | */ |
| 2384 | |
| 2385 | if (hostdata->connected) { |
Finn Thain | 9dafbd9 | 2016-01-03 16:05:28 +1100 | [diff] [blame] | 2386 | spin_unlock_irq(instance->host_lock); |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 2387 | dprintk(NDEBUG_ABORT, "scsi%d : abort failed, command connected.\n", instance->host_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2388 | return FAILED; |
| 2389 | } |
| 2390 | /* |
| 2391 | * Case 4: If the command is currently disconnected from the bus, and |
| 2392 | * there are no connected commands, we reconnect the I_T_L or |
| 2393 | * I_T_L_Q nexus associated with it, go into message out, and send |
| 2394 | * an abort message. |
| 2395 | * |
| 2396 | * This case is especially ugly. In order to reestablish the nexus, we |
| 2397 | * need to call NCR5380_select(). The easiest way to implement this |
| 2398 | * function was to abort if the bus was busy, and let the interrupt |
| 2399 | * handler triggered on the SEL for reselect take care of lost arbitrations |
| 2400 | * where necessary, meaning interrupts need to be enabled. |
| 2401 | * |
| 2402 | * When interrupts are enabled, the queues may change - so we |
| 2403 | * can't remove it from the disconnected queue before selecting it |
| 2404 | * because that could cause a failure in hashing the nexus if that |
| 2405 | * device reselected. |
| 2406 | * |
| 2407 | * Since the queues may change, we can't use the pointers from when we |
| 2408 | * first locate it. |
| 2409 | * |
| 2410 | * So, we must first locate the command, and if NCR5380_select() |
| 2411 | * succeeds, then issue the abort, relocate the command and remove |
| 2412 | * it from the disconnected queue. |
| 2413 | */ |
| 2414 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2415 | for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp; tmp = (struct scsi_cmnd *) tmp->host_scribble) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | if (cmd == tmp) { |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 2417 | dprintk(NDEBUG_ABORT, "scsi%d : aborting disconnected command.\n", instance->host_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | |
Finn Thain | 9dafbd9 | 2016-01-03 16:05:28 +1100 | [diff] [blame] | 2419 | if (NCR5380_select(instance, cmd)) { |
| 2420 | spin_unlock_irq(instance->host_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2421 | return FAILED; |
Finn Thain | 9dafbd9 | 2016-01-03 16:05:28 +1100 | [diff] [blame] | 2422 | } |
Finn Thain | 52a6a1c | 2014-03-18 11:42:18 +1100 | [diff] [blame] | 2423 | dprintk(NDEBUG_ABORT, "scsi%d : nexus reestablished.\n", instance->host_no); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2424 | |
| 2425 | do_abort(instance); |
| 2426 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2427 | for (prev = (struct scsi_cmnd **) &(hostdata->disconnected_queue), tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp; prev = (struct scsi_cmnd **) &(tmp->host_scribble), tmp = (struct scsi_cmnd *) tmp->host_scribble) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | if (cmd == tmp) { |
| 2429 | REMOVE(5, *prev, tmp, tmp->host_scribble); |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2430 | *prev = (struct scsi_cmnd *) tmp->host_scribble; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2431 | tmp->host_scribble = NULL; |
Finn Thain | 9dafbd9 | 2016-01-03 16:05:28 +1100 | [diff] [blame] | 2432 | spin_unlock_irq(instance->host_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | tmp->result = DID_ABORT << 16; |
Matthew Wilcox | cce99c6 | 2007-09-25 12:42:01 -0400 | [diff] [blame] | 2434 | tmp->scsi_done(tmp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | return SUCCESS; |
| 2436 | } |
| 2437 | } |
| 2438 | /* |
| 2439 | * Case 5 : If we reached this point, the command was not found in any of |
| 2440 | * the queues. |
| 2441 | * |
| 2442 | * We probably reached this point because of an unlikely race condition |
| 2443 | * between the command completing successfully and the abortion code, |
| 2444 | * so we won't panic, but we will notify the user in case something really |
| 2445 | * broke. |
| 2446 | */ |
Finn Thain | 9dafbd9 | 2016-01-03 16:05:28 +1100 | [diff] [blame] | 2447 | spin_unlock_irq(instance->host_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | printk(KERN_WARNING "scsi%d : warning : SCSI command probably completed successfully\n" |
| 2449 | " before abortion\n", instance->host_no); |
| 2450 | return FAILED; |
| 2451 | } |
| 2452 | |
| 2453 | |
Finn Thain | 3be1b3e | 2016-01-03 16:05:12 +1100 | [diff] [blame] | 2454 | /** |
| 2455 | * NCR5380_bus_reset - reset the SCSI bus |
| 2456 | * @cmd: SCSI command undergoing EH |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | * |
Finn Thain | 3be1b3e | 2016-01-03 16:05:12 +1100 | [diff] [blame] | 2458 | * Returns SUCCESS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | */ |
| 2460 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2461 | static int NCR5380_bus_reset(struct scsi_cmnd *cmd) |
Jeff Garzik | 68b3aa7 | 2005-05-28 07:56:31 -0400 | [diff] [blame] | 2462 | { |
| 2463 | struct Scsi_Host *instance = cmd->device->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | |
Jeff Garzik | 68b3aa7 | 2005-05-28 07:56:31 -0400 | [diff] [blame] | 2465 | spin_lock_irq(instance->host_lock); |
Finn Thain | 3be1b3e | 2016-01-03 16:05:12 +1100 | [diff] [blame] | 2466 | |
| 2467 | #if (NDEBUG & NDEBUG_ANY) |
| 2468 | scmd_printk(KERN_INFO, cmd, "performing bus reset\n"); |
| 2469 | NCR5380_print_status(instance); |
| 2470 | #endif |
| 2471 | |
Jeff Garzik | 68b3aa7 | 2005-05-28 07:56:31 -0400 | [diff] [blame] | 2472 | do_reset(instance); |
Finn Thain | 3be1b3e | 2016-01-03 16:05:12 +1100 | [diff] [blame] | 2473 | |
Jeff Garzik | 68b3aa7 | 2005-05-28 07:56:31 -0400 | [diff] [blame] | 2474 | spin_unlock_irq(instance->host_lock); |
| 2475 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2476 | return SUCCESS; |
| 2477 | } |