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