Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * NCR 5380 generic driver routines. These should make it *trivial* |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 3 | * to implement 5380 SCSI drivers under Linux with a non-trantor |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * architecture. |
| 5 | * |
| 6 | * Note that these routines also work with NR53c400 family chips. |
| 7 | * |
| 8 | * Copyright 1993, Drew Eckhardt |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 9 | * Visionary Computing |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * (Unix and Linux consulting and custom programming) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 11 | * drew@colorado.edu |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * +1 (303) 666-5836 |
| 13 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 14 | * For more information, please consult |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | /* |
| 28 | * ++roman: To port the 5380 driver to the Atari, I had to do some changes in |
| 29 | * this file, too: |
| 30 | * |
| 31 | * - Some of the debug statements were incorrect (undefined variables and the |
| 32 | * like). I fixed that. |
| 33 | * |
| 34 | * - In information_transfer(), I think a #ifdef was wrong. Looking at the |
| 35 | * possible DMA transfer size should also happen for REAL_DMA. I added this |
| 36 | * in the #if statement. |
| 37 | * |
| 38 | * - When using real DMA, information_transfer() should return in a DATAOUT |
| 39 | * phase after starting the DMA. It has nothing more to do. |
| 40 | * |
| 41 | * - The interrupt service routine should run main after end of DMA, too (not |
| 42 | * only after RESELECTION interrupts). Additionally, it should _not_ test |
| 43 | * for more interrupts after running main, since a DMA process may have |
| 44 | * been started and interrupts are turned on now. The new int could happen |
| 45 | * inside the execution of NCR5380_intr(), leading to recursive |
| 46 | * calls. |
| 47 | * |
| 48 | * - I've added a function merge_contiguous_buffers() that tries to |
| 49 | * merge scatter-gather buffers that are located at contiguous |
| 50 | * physical addresses and can be processed with the same DMA setup. |
| 51 | * Since most scatter-gather operations work on a page (4K) of |
| 52 | * 4 buffers (1K), in more than 90% of all cases three interrupts and |
| 53 | * DMA setup actions are saved. |
| 54 | * |
| 55 | * - I've deleted all the stuff for AUTOPROBE_IRQ, REAL_DMA_POLL, PSEUDO_DMA |
| 56 | * and USLEEP, because these were messing up readability and will never be |
| 57 | * needed for Atari SCSI. |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 58 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | * - I've revised the NCR5380_main() calling scheme (relax the 'main_running' |
| 60 | * stuff), and 'main' is executed in a bottom half if awoken by an |
| 61 | * interrupt. |
| 62 | * |
| 63 | * - The code was quite cluttered up by "#if (NDEBUG & NDEBUG_*) printk..." |
| 64 | * constructs. In my eyes, this made the source rather unreadable, so I |
| 65 | * finally replaced that by the *_PRINTK() macros. |
| 66 | * |
| 67 | */ |
| 68 | |
| 69 | /* |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 70 | * Further development / testing that should be done : |
| 71 | * 1. Test linked command handling code after Eric is ready with |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | * the high level code. |
| 73 | */ |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 74 | |
| 75 | /* Adapted for the sun3 by Sam Creasey. */ |
| 76 | |
| db9dff3 | 2005-04-03 14:53:59 -0500 | [diff] [blame] | 77 | #include <scsi/scsi_dbg.h> |
Matthew Wilcox | 1abfd37 | 2005-12-15 16:22:01 -0500 | [diff] [blame] | 78 | #include <scsi/scsi_transport_spi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
| 80 | #if (NDEBUG & NDEBUG_LISTS) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 81 | #define LIST(x, y) \ |
| 82 | do { \ |
| 83 | printk("LINE:%d Adding %p to %p\n", \ |
| 84 | __LINE__, (void*)(x), (void*)(y)); \ |
| 85 | if ((x) == (y)) \ |
| 86 | udelay(5); \ |
| 87 | } while (0) |
| 88 | #define REMOVE(w, x, y, z) \ |
| 89 | do { \ |
| 90 | printk("LINE:%d Removing: %p->%p %p->%p \n", \ |
| 91 | __LINE__, (void*)(w), (void*)(x), \ |
| 92 | (void*)(y), (void*)(z)); \ |
| 93 | if ((x) == (y)) \ |
| 94 | udelay(5); \ |
| 95 | } while (0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | #else |
| 97 | #define LIST(x,y) |
| 98 | #define REMOVE(w,x,y,z) |
| 99 | #endif |
| 100 | |
| 101 | #ifndef notyet |
| 102 | #undef LINKED |
| 103 | #endif |
| 104 | |
| 105 | /* |
| 106 | * Design |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 108 | * This is a generic 5380 driver. To use it on a different platform, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | * one simply writes appropriate system specific macros (ie, data |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 110 | * transfer - some PC's will use the I/O bus, 68K's must use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | * memory mapped) and drops this file in their 'C' wrapper. |
| 112 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 113 | * As far as command queueing, two queues are maintained for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | * each 5380 in the system - commands that haven't been issued yet, |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 115 | * and commands that are currently executing. This means that an |
| 116 | * unlimited number of commands may be queued, letting |
| 117 | * more commands propagate from the higher driver levels giving higher |
| 118 | * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported, |
| 119 | * allowing multiple commands to propagate all the way to a SCSI-II device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | * while a command is already executing. |
| 121 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 123 | * Issues specific to the NCR5380 : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 125 | * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead |
| 126 | * piece of hardware that requires you to sit in a loop polling for |
| 127 | * the REQ signal as long as you are connected. Some devices are |
| 128 | * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | * while doing long seek operations. |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 130 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 131 | * The workaround for this is to keep track of devices that have |
| 132 | * disconnected. If the device hasn't disconnected, for commands that |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 133 | * should disconnect, we do something like |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | * |
| 135 | * while (!REQ is asserted) { sleep for N usecs; poll for M usecs } |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 136 | * |
| 137 | * Some tweaking of N and M needs to be done. An algorithm based |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | * on "time to data" would give the best results as long as short time |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 139 | * to datas (ie, on the same track) were considered, however these |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | * broken devices are the exception rather than the rule and I'd rather |
| 141 | * spend my time optimizing for the normal case. |
| 142 | * |
| 143 | * Architecture : |
| 144 | * |
| 145 | * At the heart of the design is a coroutine, NCR5380_main, |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 146 | * which is started from a workqueue for each NCR5380 host in the |
| 147 | * system. It attempts to establish I_T_L or I_T_L_Q nexuses by |
| 148 | * removing the commands from the issue queue and calling |
| 149 | * NCR5380_select() if a nexus is not established. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | * |
| 151 | * Once a nexus is established, the NCR5380_information_transfer() |
| 152 | * phase goes through the various phases as instructed by the target. |
| 153 | * if the target goes into MSG IN and sends a DISCONNECT message, |
| 154 | * the command structure is placed into the per instance disconnected |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 155 | * queue, and NCR5380_main tries to find more work. If the target is |
| 156 | * idle for too long, the system will try to sleep. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | * |
| 158 | * If a command has disconnected, eventually an interrupt will trigger, |
| 159 | * calling NCR5380_intr() which will in turn call NCR5380_reselect |
| 160 | * to reestablish a nexus. This will run main if necessary. |
| 161 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 162 | * On command termination, the done function will be called as |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | * appropriate. |
| 164 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 165 | * SCSI pointers are maintained in the SCp field of SCSI command |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | * structures, being initialized after the command is connected |
| 167 | * in NCR5380_select, and set as appropriate in NCR5380_information_transfer. |
| 168 | * Note that in violation of the standard, an implicit SAVE POINTERS operation |
| 169 | * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS. |
| 170 | */ |
| 171 | |
| 172 | /* |
| 173 | * Using this file : |
| 174 | * This file a skeleton Linux SCSI driver for the NCR 5380 series |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 175 | * of chips. To use it, you write an architecture specific functions |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | * and macros and include this file in your driver. |
| 177 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 178 | * These macros control options : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 180 | * for commands that return with a CHECK CONDITION status. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | * |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 182 | * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential |
| 183 | * transceivers. |
| 184 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | * LINKED - if defined, linked commands are supported. |
| 186 | * |
| 187 | * REAL_DMA - if defined, REAL DMA is used during the data transfer phases. |
| 188 | * |
| 189 | * SUPPORT_TAGS - if defined, SCSI-2 tagged queuing is used where possible |
| 190 | * |
| 191 | * These macros MUST be defined : |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 192 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | * NCR5380_read(register) - read from the specified register |
| 194 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 195 | * NCR5380_write(register, value) - write to the specific register |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | * |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 197 | * NCR5380_implementation_fields - additional fields needed for this |
| 198 | * specific implementation of the NCR5380 |
| 199 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | * Either real DMA *or* pseudo DMA may be implemented |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 201 | * REAL functions : |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | * NCR5380_REAL_DMA should be defined if real DMA is to be used. |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 203 | * Note that the DMA setup functions should return the number of bytes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | * that they were able to program the controller for. |
| 205 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 206 | * Also note that generic i386/PC versions of these macros are |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 207 | * available as NCR5380_i386_dma_write_setup, |
| 208 | * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual. |
| 209 | * |
| 210 | * NCR5380_dma_write_setup(instance, src, count) - initialize |
| 211 | * NCR5380_dma_read_setup(instance, dst, count) - initialize |
| 212 | * NCR5380_dma_residual(instance); - residual count |
| 213 | * |
| 214 | * PSEUDO functions : |
| 215 | * NCR5380_pwrite(instance, src, count) |
| 216 | * NCR5380_pread(instance, dst, count); |
| 217 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | * The generic driver is initialized by calling NCR5380_init(instance), |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 219 | * after setting the appropriate host specific fields and ID. If the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance, |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 221 | * possible) function may be used. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | */ |
| 223 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 224 | /* Macros ease life... :-) */ |
| 225 | #define SETUP_HOSTDATA(in) \ |
| 226 | struct NCR5380_hostdata *hostdata = \ |
| 227 | (struct NCR5380_hostdata *)(in)->hostdata |
| 228 | #define HOSTDATA(in) ((struct NCR5380_hostdata *)(in)->hostdata) |
| 229 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 230 | #define NEXT(cmd) ((struct scsi_cmnd *)(cmd)->host_scribble) |
Roman Zippel | 3130d90 | 2007-05-01 22:32:37 +0200 | [diff] [blame] | 231 | #define SET_NEXT(cmd,next) ((cmd)->host_scribble = (void *)(next)) |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 232 | #define NEXTADDR(cmd) ((struct scsi_cmnd **)&(cmd)->host_scribble) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | |
| 234 | #define HOSTNO instance->host_no |
| 235 | #define H_NO(cmd) (cmd)->device->host->host_no |
| 236 | |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 237 | static int do_abort(struct Scsi_Host *); |
| 238 | static void do_reset(struct Scsi_Host *); |
| 239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | #ifdef SUPPORT_TAGS |
| 241 | |
| 242 | /* |
| 243 | * Functions for handling tagged queuing |
| 244 | * ===================================== |
| 245 | * |
| 246 | * ++roman (01/96): Now I've implemented SCSI-2 tagged queuing. Some notes: |
| 247 | * |
| 248 | * Using consecutive numbers for the tags is no good idea in my eyes. There |
| 249 | * could be wrong re-usings if the counter (8 bit!) wraps and some early |
| 250 | * command has been preempted for a long time. My solution: a bitfield for |
| 251 | * remembering used tags. |
| 252 | * |
| 253 | * There's also the problem that each target has a certain queue size, but we |
| 254 | * cannot know it in advance :-( We just see a QUEUE_FULL status being |
| 255 | * returned. So, in this case, the driver internal queue size assumption is |
| 256 | * reduced to the number of active tags if QUEUE_FULL is returned by the |
| 257 | * target. The command is returned to the mid-level, but with status changed |
| 258 | * to BUSY, since --as I've seen-- the mid-level can't handle QUEUE_FULL |
| 259 | * correctly. |
| 260 | * |
| 261 | * We're also not allowed running tagged commands as long as an untagged |
| 262 | * command is active. And REQUEST SENSE commands after a contingent allegiance |
| 263 | * condition _must_ be untagged. To keep track whether an untagged command has |
| 264 | * been issued, the host->busy array is still employed, as it is without |
| 265 | * support for tagged queuing. |
| 266 | * |
| 267 | * One could suspect that there are possible race conditions between |
| 268 | * is_lun_busy(), cmd_get_tag() and cmd_free_tag(). But I think this isn't the |
| 269 | * case: is_lun_busy() and cmd_get_tag() are both called from NCR5380_main(), |
| 270 | * which already guaranteed to be running at most once. It is also the only |
| 271 | * place where tags/LUNs are allocated. So no other allocation can slip |
| 272 | * between that pair, there could only happen a reselection, which can free a |
| 273 | * tag, but that doesn't hurt. Only the sequence in cmd_free_tag() becomes |
| 274 | * important: the tag bit must be cleared before 'nr_allocated' is decreased. |
| 275 | */ |
| 276 | |
Finn Thain | ca513fc | 2014-11-12 16:12:19 +1100 | [diff] [blame] | 277 | static void __init init_tags(struct NCR5380_hostdata *hostdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 279 | int target, lun; |
Finn Thain | 61d739a | 2014-11-12 16:12:20 +1100 | [diff] [blame] | 280 | struct tag_alloc *ta; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 281 | |
Finn Thain | ca513fc | 2014-11-12 16:12:19 +1100 | [diff] [blame] | 282 | if (!(hostdata->flags & FLAG_TAGGED_QUEUING)) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 283 | return; |
| 284 | |
| 285 | for (target = 0; target < 8; ++target) { |
| 286 | for (lun = 0; lun < 8; ++lun) { |
Finn Thain | 61d739a | 2014-11-12 16:12:20 +1100 | [diff] [blame] | 287 | ta = &hostdata->TagAlloc[target][lun]; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 288 | bitmap_zero(ta->allocated, MAX_TAGS); |
| 289 | ta->nr_allocated = 0; |
| 290 | /* At the beginning, assume the maximum queue size we could |
| 291 | * support (MAX_TAGS). This value will be decreased if the target |
| 292 | * returns QUEUE_FULL status. |
| 293 | */ |
| 294 | ta->queue_size = MAX_TAGS; |
| 295 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | |
| 300 | /* Check if we can issue a command to this LUN: First see if the LUN is marked |
| 301 | * busy by an untagged command. If the command should use tagged queuing, also |
| 302 | * check that there is a free tag and the target's queue won't overflow. This |
| 303 | * function should be called with interrupts disabled to avoid race |
| 304 | * conditions. |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 305 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 307 | static int is_lun_busy(struct scsi_cmnd *cmd, int should_be_tagged) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | { |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 309 | u8 lun = cmd->device->lun; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 310 | SETUP_HOSTDATA(cmd->device->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 312 | if (hostdata->busy[cmd->device->id] & (1 << lun)) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 313 | return 1; |
| 314 | if (!should_be_tagged || |
Finn Thain | ca513fc | 2014-11-12 16:12:19 +1100 | [diff] [blame] | 315 | !(hostdata->flags & FLAG_TAGGED_QUEUING) || |
| 316 | !cmd->device->tagged_supported) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 317 | return 0; |
Finn Thain | 61d739a | 2014-11-12 16:12:20 +1100 | [diff] [blame] | 318 | if (hostdata->TagAlloc[scmd_id(cmd)][lun].nr_allocated >= |
| 319 | hostdata->TagAlloc[scmd_id(cmd)][lun].queue_size) { |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 320 | dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d: no free tags\n", |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 321 | H_NO(cmd), cmd->device->id, lun); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 322 | return 1; |
| 323 | } |
| 324 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | |
| 328 | /* Allocate a tag for a command (there are no checks anymore, check_lun_busy() |
| 329 | * must be called before!), or reserve the LUN in 'busy' if the command is |
| 330 | * untagged. |
| 331 | */ |
| 332 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 333 | static void cmd_get_tag(struct scsi_cmnd *cmd, int should_be_tagged) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | { |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 335 | u8 lun = cmd->device->lun; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 336 | SETUP_HOSTDATA(cmd->device->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 337 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 338 | /* If we or the target don't support tagged queuing, allocate the LUN for |
| 339 | * an untagged command. |
| 340 | */ |
| 341 | if (!should_be_tagged || |
Finn Thain | ca513fc | 2014-11-12 16:12:19 +1100 | [diff] [blame] | 342 | !(hostdata->flags & FLAG_TAGGED_QUEUING) || |
| 343 | !cmd->device->tagged_supported) { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 344 | cmd->tag = TAG_NONE; |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 345 | hostdata->busy[cmd->device->id] |= (1 << lun); |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 346 | dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d now allocated by untagged " |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 347 | "command\n", H_NO(cmd), cmd->device->id, lun); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 348 | } else { |
Finn Thain | 61d739a | 2014-11-12 16:12:20 +1100 | [diff] [blame] | 349 | struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 351 | cmd->tag = find_first_zero_bit(ta->allocated, MAX_TAGS); |
| 352 | set_bit(cmd->tag, ta->allocated); |
| 353 | ta->nr_allocated++; |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 354 | dprintk(NDEBUG_TAGS, "scsi%d: using tag %d for target %d lun %d " |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 355 | "(now %d tags in use)\n", |
| 356 | H_NO(cmd), cmd->tag, cmd->device->id, |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 357 | lun, ta->nr_allocated); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 358 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | |
| 362 | /* Mark the tag of command 'cmd' as free, or in case of an untagged command, |
| 363 | * unlock the LUN. |
| 364 | */ |
| 365 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 366 | static void cmd_free_tag(struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | { |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 368 | u8 lun = cmd->device->lun; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 369 | SETUP_HOSTDATA(cmd->device->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 371 | if (cmd->tag == TAG_NONE) { |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 372 | hostdata->busy[cmd->device->id] &= ~(1 << lun); |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 373 | dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %d untagged cmd finished\n", |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 374 | H_NO(cmd), cmd->device->id, lun); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 375 | } else if (cmd->tag >= MAX_TAGS) { |
| 376 | printk(KERN_NOTICE "scsi%d: trying to free bad tag %d!\n", |
| 377 | H_NO(cmd), cmd->tag); |
| 378 | } else { |
Finn Thain | 61d739a | 2014-11-12 16:12:20 +1100 | [diff] [blame] | 379 | struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][lun]; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 380 | clear_bit(cmd->tag, ta->allocated); |
| 381 | ta->nr_allocated--; |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 382 | dprintk(NDEBUG_TAGS, "scsi%d: freed tag %d for target %d lun %d\n", |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 383 | H_NO(cmd), cmd->tag, cmd->device->id, lun); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 384 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | |
Finn Thain | ca513fc | 2014-11-12 16:12:19 +1100 | [diff] [blame] | 388 | static void free_all_tags(struct NCR5380_hostdata *hostdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 390 | int target, lun; |
Finn Thain | 61d739a | 2014-11-12 16:12:20 +1100 | [diff] [blame] | 391 | struct tag_alloc *ta; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
Finn Thain | ca513fc | 2014-11-12 16:12:19 +1100 | [diff] [blame] | 393 | if (!(hostdata->flags & FLAG_TAGGED_QUEUING)) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 394 | return; |
| 395 | |
| 396 | for (target = 0; target < 8; ++target) { |
| 397 | for (lun = 0; lun < 8; ++lun) { |
Finn Thain | 61d739a | 2014-11-12 16:12:20 +1100 | [diff] [blame] | 398 | ta = &hostdata->TagAlloc[target][lun]; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 399 | bitmap_zero(ta->allocated, MAX_TAGS); |
| 400 | ta->nr_allocated = 0; |
| 401 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | #endif /* SUPPORT_TAGS */ |
| 406 | |
| 407 | |
| 408 | /* |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 409 | * Function: void merge_contiguous_buffers( struct scsi_cmnd *cmd ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | * |
| 411 | * Purpose: Try to merge several scatter-gather requests into one DMA |
| 412 | * transfer. This is possible if the scatter buffers lie on |
| 413 | * physical contiguous addresses. |
| 414 | * |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 415 | * Parameters: struct scsi_cmnd *cmd |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 416 | * The command to work on. The first scatter buffer's data are |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 417 | * assumed to be already transferred into ptr/this_residual. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | */ |
| 419 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 420 | static void merge_contiguous_buffers(struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | { |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 422 | #if !defined(CONFIG_SUN3) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 423 | unsigned long endaddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | #if (NDEBUG & NDEBUG_MERGING) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 425 | unsigned long oldlen = cmd->SCp.this_residual; |
| 426 | int cnt = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | #endif |
| 428 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 429 | for (endaddr = virt_to_phys(cmd->SCp.ptr + cmd->SCp.this_residual - 1) + 1; |
| 430 | cmd->SCp.buffers_residual && |
Geert Uytterhoeven | 5a1cb47 | 2007-10-24 08:55:40 +0200 | [diff] [blame] | 431 | virt_to_phys(sg_virt(&cmd->SCp.buffer[1])) == endaddr;) { |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 432 | dprintk(NDEBUG_MERGING, "VTOP(%p) == %08lx -> merging\n", |
Geert Uytterhoeven | 5a1cb47 | 2007-10-24 08:55:40 +0200 | [diff] [blame] | 433 | page_address(sg_page(&cmd->SCp.buffer[1])), endaddr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | #if (NDEBUG & NDEBUG_MERGING) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 435 | ++cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | #endif |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 437 | ++cmd->SCp.buffer; |
| 438 | --cmd->SCp.buffers_residual; |
| 439 | cmd->SCp.this_residual += cmd->SCp.buffer->length; |
| 440 | endaddr += cmd->SCp.buffer->length; |
| 441 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | #if (NDEBUG & NDEBUG_MERGING) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 443 | if (oldlen != cmd->SCp.this_residual) |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 444 | dprintk(NDEBUG_MERGING, "merged %d buffers from %p, new length %08x\n", |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 445 | cnt, cmd->SCp.ptr, cmd->SCp.this_residual); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | #endif |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 447 | #endif /* !defined(CONFIG_SUN3) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | } |
| 449 | |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 450 | /** |
| 451 | * initialize_SCp - init the scsi pointer field |
| 452 | * @cmd: command block to set up |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | * |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 454 | * Set up the internal fields in the SCSI command. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | */ |
| 456 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 457 | static inline void initialize_SCp(struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 459 | /* |
| 460 | * Initialize the Scsi Pointer field so that all of the commands in the |
| 461 | * various queues are valid. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 463 | |
Boaz Harrosh | 9e0fe44 | 2007-11-05 11:23:35 +0200 | [diff] [blame] | 464 | if (scsi_bufflen(cmd)) { |
| 465 | cmd->SCp.buffer = scsi_sglist(cmd); |
| 466 | cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1; |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 467 | cmd->SCp.ptr = sg_virt(cmd->SCp.buffer); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 468 | cmd->SCp.this_residual = cmd->SCp.buffer->length; |
| 469 | /* ++roman: Try to merge some scatter-buffers if they are at |
| 470 | * contiguous physical addresses. |
| 471 | */ |
| 472 | merge_contiguous_buffers(cmd); |
| 473 | } else { |
| 474 | cmd->SCp.buffer = NULL; |
| 475 | cmd->SCp.buffers_residual = 0; |
Boaz Harrosh | 9e0fe44 | 2007-11-05 11:23:35 +0200 | [diff] [blame] | 476 | cmd->SCp.ptr = NULL; |
| 477 | cmd->SCp.this_residual = 0; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 478 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } |
| 480 | |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 481 | /** |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 482 | * NCR5380_poll_politely - wait for chip register value |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 483 | * @instance: controller to poll |
| 484 | * @reg: 5380 register to poll |
| 485 | * @bit: Bitmask to check |
| 486 | * @val: Value required to exit |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 487 | * @wait: Time-out in jiffies |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 488 | * |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 489 | * Polls the chip in a reasonably efficient manner waiting for an |
| 490 | * event to occur. After a short quick poll we begin to yield the CPU |
| 491 | * (if possible). In irq contexts the time-out is arbitrarily limited. |
| 492 | * Callers may hold locks as long as they are held in irq mode. |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 493 | * |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 494 | * Returns 0 if event occurred otherwise -ETIMEDOUT. |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 495 | */ |
| 496 | |
| 497 | static int NCR5380_poll_politely(struct Scsi_Host *instance, |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 498 | int reg, int bit, int val, int wait) |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 499 | { |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 500 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 501 | unsigned long deadline = jiffies + wait; |
| 502 | unsigned long n; |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 503 | |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 504 | /* Busy-wait for up to 10 ms */ |
| 505 | n = min(10000U, jiffies_to_usecs(wait)); |
| 506 | n *= hostdata->accesses_per_ms; |
| 507 | n /= 1000; |
| 508 | do { |
| 509 | if ((NCR5380_read(reg) & bit) == val) |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 510 | return 0; |
| 511 | cpu_relax(); |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 512 | } while (n--); |
| 513 | |
| 514 | if (irqs_disabled() || in_interrupt()) |
| 515 | return -ETIMEDOUT; |
| 516 | |
| 517 | /* Repeatedly sleep for 1 ms until deadline */ |
| 518 | while (time_is_after_jiffies(deadline)) { |
| 519 | schedule_timeout_uninterruptible(1); |
| 520 | if ((NCR5380_read(reg) & bit) == val) |
| 521 | return 0; |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 522 | } |
| 523 | |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 524 | return -ETIMEDOUT; |
| 525 | } |
| 526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | #include <linux/delay.h> |
| 528 | |
| 529 | #if NDEBUG |
| 530 | static struct { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 531 | unsigned char mask; |
| 532 | const char *name; |
| 533 | } signals[] = { |
| 534 | { SR_DBP, "PARITY"}, { SR_RST, "RST" }, { SR_BSY, "BSY" }, |
| 535 | { SR_REQ, "REQ" }, { SR_MSG, "MSG" }, { SR_CD, "CD" }, { SR_IO, "IO" }, |
| 536 | { SR_SEL, "SEL" }, {0, NULL} |
| 537 | }, basrs[] = { |
| 538 | {BASR_ATN, "ATN"}, {BASR_ACK, "ACK"}, {0, NULL} |
| 539 | }, icrs[] = { |
| 540 | {ICR_ASSERT_RST, "ASSERT RST"},{ICR_ASSERT_ACK, "ASSERT ACK"}, |
| 541 | {ICR_ASSERT_BSY, "ASSERT BSY"}, {ICR_ASSERT_SEL, "ASSERT SEL"}, |
| 542 | {ICR_ASSERT_ATN, "ASSERT ATN"}, {ICR_ASSERT_DATA, "ASSERT DATA"}, |
| 543 | {0, NULL} |
| 544 | }, mrs[] = { |
| 545 | {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, {MR_TARGET, "MODE TARGET"}, |
| 546 | {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, {MR_ENABLE_PAR_INTR, |
| 547 | "MODE PARITY INTR"}, {MR_ENABLE_EOP_INTR,"MODE EOP INTR"}, |
| 548 | {MR_MONITOR_BSY, "MODE MONITOR BSY"}, |
| 549 | {MR_DMA_MODE, "MODE DMA"}, {MR_ARBITRATE, "MODE ARBITRATION"}, |
| 550 | {0, NULL} |
| 551 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 553 | /** |
| 554 | * NCR5380_print - print scsi bus signals |
| 555 | * @instance: adapter state to dump |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | * |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 557 | * Print the SCSI bus signals for debugging purposes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | */ |
| 559 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 560 | static void NCR5380_print(struct Scsi_Host *instance) |
| 561 | { |
| 562 | unsigned char status, data, basr, mr, icr, i; |
| 563 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 565 | local_irq_save(flags); |
| 566 | data = NCR5380_read(CURRENT_SCSI_DATA_REG); |
| 567 | status = NCR5380_read(STATUS_REG); |
| 568 | mr = NCR5380_read(MODE_REG); |
| 569 | icr = NCR5380_read(INITIATOR_COMMAND_REG); |
| 570 | basr = NCR5380_read(BUS_AND_STATUS_REG); |
| 571 | local_irq_restore(flags); |
| 572 | printk("STATUS_REG: %02x ", status); |
| 573 | for (i = 0; signals[i].mask; ++i) |
| 574 | if (status & signals[i].mask) |
| 575 | printk(",%s", signals[i].name); |
| 576 | printk("\nBASR: %02x ", basr); |
| 577 | for (i = 0; basrs[i].mask; ++i) |
| 578 | if (basr & basrs[i].mask) |
| 579 | printk(",%s", basrs[i].name); |
| 580 | printk("\nICR: %02x ", icr); |
| 581 | for (i = 0; icrs[i].mask; ++i) |
| 582 | if (icr & icrs[i].mask) |
| 583 | printk(",%s", icrs[i].name); |
| 584 | printk("\nMODE: %02x ", mr); |
| 585 | for (i = 0; mrs[i].mask; ++i) |
| 586 | if (mr & mrs[i].mask) |
| 587 | printk(",%s", mrs[i].name); |
| 588 | printk("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | } |
| 590 | |
| 591 | static struct { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 592 | unsigned char value; |
| 593 | const char *name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 594 | } phases[] = { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 595 | {PHASE_DATAOUT, "DATAOUT"}, {PHASE_DATAIN, "DATAIN"}, {PHASE_CMDOUT, "CMDOUT"}, |
| 596 | {PHASE_STATIN, "STATIN"}, {PHASE_MSGOUT, "MSGOUT"}, {PHASE_MSGIN, "MSGIN"}, |
| 597 | {PHASE_UNKNOWN, "UNKNOWN"} |
| 598 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 600 | /** |
| 601 | * NCR5380_print_phase - show SCSI phase |
| 602 | * @instance: adapter to dump |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | * |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 604 | * Print the current SCSI phase for debugging purposes |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | * |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 606 | * Locks: none |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | */ |
| 608 | |
| 609 | static void NCR5380_print_phase(struct Scsi_Host *instance) |
| 610 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 611 | unsigned char status; |
| 612 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 614 | status = NCR5380_read(STATUS_REG); |
| 615 | if (!(status & SR_REQ)) |
| 616 | printk(KERN_DEBUG "scsi%d: REQ not asserted, phase unknown.\n", HOSTNO); |
| 617 | else { |
| 618 | for (i = 0; (phases[i].value != PHASE_UNKNOWN) && |
| 619 | (phases[i].value != (status & PHASE_MASK)); ++i) |
| 620 | ; |
| 621 | printk(KERN_DEBUG "scsi%d: phase %s\n", HOSTNO, phases[i].name); |
| 622 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | } |
| 624 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | #endif |
| 626 | |
| 627 | /* |
| 628 | * ++roman: New scheme of calling NCR5380_main() |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 629 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | * If we're not in an interrupt, we can call our main directly, it cannot be |
| 631 | * already running. Else, we queue it on a task queue, if not 'main_running' |
| 632 | * tells us that a lower level is already executing it. This way, |
| 633 | * 'main_running' needs not be protected in a special way. |
| 634 | * |
| 635 | * queue_main() is a utility function for putting our main onto the task |
| 636 | * queue, if main_running is false. It should be called only from a |
| 637 | * interrupt or bottom half. |
| 638 | */ |
| 639 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 640 | #include <linux/gfp.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | #include <linux/workqueue.h> |
| 642 | #include <linux/interrupt.h> |
| 643 | |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 644 | static inline void queue_main(struct NCR5380_hostdata *hostdata) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | { |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 646 | if (!hostdata->main_running) { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 647 | /* If in interrupt and NCR5380_main() not already running, |
| 648 | queue it on the 'immediate' task queue, to be processed |
| 649 | immediately after the current interrupt processing has |
| 650 | finished. */ |
Finn Thain | 0ad0eff | 2016-01-03 16:05:21 +1100 | [diff] [blame] | 651 | queue_work(hostdata->work_q, &hostdata->main_task); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 652 | } |
| 653 | /* else: nothing to do: the running NCR5380_main() will pick up |
| 654 | any newly queued command. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 657 | /** |
| 658 | * NCR58380_info - report driver and host information |
| 659 | * @instance: relevant scsi host instance |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 660 | * |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 661 | * For use as the host template info() handler. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 662 | * |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 663 | * Locks: none |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | */ |
| 665 | |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 666 | static const char *NCR5380_info(struct Scsi_Host *instance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | { |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 668 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 669 | |
| 670 | return hostdata->info; |
| 671 | } |
| 672 | |
| 673 | static void prepare_info(struct Scsi_Host *instance) |
| 674 | { |
| 675 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 676 | |
| 677 | snprintf(hostdata->info, sizeof(hostdata->info), |
| 678 | "%s, io_port 0x%lx, n_io_port %d, " |
| 679 | "base 0x%lx, irq %d, " |
| 680 | "can_queue %d, cmd_per_lun %d, " |
| 681 | "sg_tablesize %d, this_id %d, " |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 682 | "flags { %s%s}, " |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 683 | "options { %s} ", |
| 684 | instance->hostt->name, instance->io_port, instance->n_io_port, |
| 685 | instance->base, instance->irq, |
| 686 | instance->can_queue, instance->cmd_per_lun, |
| 687 | instance->sg_tablesize, instance->this_id, |
Finn Thain | ca513fc | 2014-11-12 16:12:19 +1100 | [diff] [blame] | 688 | hostdata->flags & FLAG_TAGGED_QUEUING ? "TAGGED_QUEUING " : "", |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 689 | hostdata->flags & FLAG_TOSHIBA_DELAY ? "TOSHIBA_DELAY " : "", |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 690 | #ifdef DIFFERENTIAL |
| 691 | "DIFFERENTIAL " |
| 692 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | #ifdef REAL_DMA |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 694 | "REAL_DMA " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | #endif |
| 696 | #ifdef PARITY |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 697 | "PARITY " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | #endif |
| 699 | #ifdef SUPPORT_TAGS |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 700 | "SUPPORT_TAGS " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | #endif |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 702 | ""); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | } |
| 704 | |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 705 | /** |
| 706 | * NCR5380_print_status - dump controller info |
| 707 | * @instance: controller to dump |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | * |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 709 | * Print commands in the various queues, called from NCR5380_abort |
| 710 | * to aid debugging. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | */ |
| 712 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 713 | static void lprint_Scsi_Cmnd(struct scsi_cmnd *cmd) |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 714 | { |
| 715 | int i, s; |
| 716 | unsigned char *command; |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 717 | printk("scsi%d: destination target %d, lun %llu\n", |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 718 | H_NO(cmd), cmd->device->id, cmd->device->lun); |
| 719 | printk(KERN_CONT " command = "); |
| 720 | command = cmd->cmnd; |
| 721 | printk(KERN_CONT "%2d (0x%02x)", command[0], command[0]); |
| 722 | for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i) |
| 723 | printk(KERN_CONT " %02x", command[i]); |
| 724 | printk("\n"); |
| 725 | } |
| 726 | |
Finn Thain | 3be1b3e | 2016-01-03 16:05:12 +1100 | [diff] [blame] | 727 | static void __maybe_unused NCR5380_print_status(struct Scsi_Host *instance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 728 | { |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 729 | struct NCR5380_hostdata *hostdata; |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 730 | struct scsi_cmnd *ptr; |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 731 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
Finn Thain | 8ad3a59 | 2014-03-18 11:42:19 +1100 | [diff] [blame] | 733 | NCR5380_dprint(NDEBUG_ANY, instance); |
| 734 | NCR5380_dprint_phase(NDEBUG_ANY, instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 736 | hostdata = (struct NCR5380_hostdata *)instance->hostdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 738 | local_irq_save(flags); |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 739 | printk("NCR5380: coroutine is%s running.\n", |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 740 | hostdata->main_running ? "" : "n't"); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 741 | if (!hostdata->connected) |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 742 | printk("scsi%d: no currently connected command\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 743 | else |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 744 | lprint_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected); |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 745 | printk("scsi%d: issue_queue\n", HOSTNO); |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 746 | for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr)) |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 747 | lprint_Scsi_Cmnd(ptr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 748 | |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 749 | printk("scsi%d: disconnected_queue\n", HOSTNO); |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 750 | for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr; |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 751 | ptr = NEXT(ptr)) |
| 752 | lprint_Scsi_Cmnd(ptr); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 753 | |
| 754 | local_irq_restore(flags); |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 755 | printk("\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | } |
| 757 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 758 | static void show_Scsi_Cmnd(struct scsi_cmnd *cmd, struct seq_file *m) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 760 | int i, s; |
| 761 | unsigned char *command; |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 762 | seq_printf(m, "scsi%d: destination target %d, lun %llu\n", |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 763 | H_NO(cmd), cmd->device->id, cmd->device->lun); |
Rasmus Villemoes | 91c40f2 | 2014-12-03 00:10:52 +0100 | [diff] [blame] | 764 | seq_puts(m, " command = "); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 765 | command = cmd->cmnd; |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 766 | seq_printf(m, "%2d (0x%02x)", command[0], command[0]); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 767 | for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i) |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 768 | seq_printf(m, " %02x", command[i]); |
Rasmus Villemoes | f50332f | 2014-12-03 00:10:54 +0100 | [diff] [blame] | 769 | seq_putc(m, '\n'); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 770 | } |
| 771 | |
Finn Thain | 4d3d2a5 | 2014-11-12 16:11:52 +1100 | [diff] [blame] | 772 | static int __maybe_unused NCR5380_show_info(struct seq_file *m, |
| 773 | struct Scsi_Host *instance) |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 774 | { |
| 775 | struct NCR5380_hostdata *hostdata; |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 776 | struct scsi_cmnd *ptr; |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 777 | unsigned long flags; |
| 778 | |
| 779 | hostdata = (struct NCR5380_hostdata *)instance->hostdata; |
| 780 | |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 781 | local_irq_save(flags); |
| 782 | seq_printf(m, "NCR5380: coroutine is%s running.\n", |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 783 | hostdata->main_running ? "" : "n't"); |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 784 | if (!hostdata->connected) |
| 785 | seq_printf(m, "scsi%d: no currently connected command\n", HOSTNO); |
| 786 | else |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 787 | show_Scsi_Cmnd((struct scsi_cmnd *) hostdata->connected, m); |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 788 | seq_printf(m, "scsi%d: issue_queue\n", HOSTNO); |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 789 | for (ptr = (struct scsi_cmnd *)hostdata->issue_queue; ptr; ptr = NEXT(ptr)) |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 790 | show_Scsi_Cmnd(ptr, m); |
| 791 | |
| 792 | seq_printf(m, "scsi%d: disconnected_queue\n", HOSTNO); |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 793 | for (ptr = (struct scsi_cmnd *) hostdata->disconnected_queue; ptr; |
Al Viro | d89537e | 2013-03-31 13:24:44 -0400 | [diff] [blame] | 794 | ptr = NEXT(ptr)) |
| 795 | show_Scsi_Cmnd(ptr, m); |
| 796 | |
| 797 | local_irq_restore(flags); |
| 798 | return 0; |
| 799 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 801 | /** |
| 802 | * NCR5380_init - initialise an NCR5380 |
| 803 | * @instance: adapter to configure |
| 804 | * @flags: control flags |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 805 | * |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 806 | * Initializes *instance and corresponding 5380 chip, |
| 807 | * with flags OR'd into the initial flags value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 808 | * |
| 809 | * Notes : I assume that the host, hostno, and id bits have been |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 810 | * set correctly. I don't care about the irq and other fields. |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 811 | * |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 812 | * Returns 0 for success |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 813 | */ |
| 814 | |
Michael Schmitz | 95fde7a | 2009-01-18 03:22:15 +0100 | [diff] [blame] | 815 | static int __init NCR5380_init(struct Scsi_Host *instance, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 817 | int i; |
| 818 | SETUP_HOSTDATA(instance); |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 819 | unsigned long deadline; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 820 | |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 821 | hostdata->host = instance; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 822 | hostdata->id_mask = 1 << instance->this_id; |
| 823 | hostdata->id_higher_mask = 0; |
| 824 | for (i = hostdata->id_mask; i <= 0x80; i <<= 1) |
| 825 | if (i > hostdata->id_mask) |
| 826 | hostdata->id_higher_mask |= i; |
| 827 | for (i = 0; i < 8; ++i) |
| 828 | hostdata->busy[i] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 829 | #ifdef SUPPORT_TAGS |
Finn Thain | ca513fc | 2014-11-12 16:12:19 +1100 | [diff] [blame] | 830 | init_tags(hostdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | #endif |
| 832 | #if defined (REAL_DMA) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 833 | hostdata->dma_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 834 | #endif |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 835 | hostdata->connected = NULL; |
| 836 | hostdata->issue_queue = NULL; |
| 837 | hostdata->disconnected_queue = NULL; |
Finn Thain | ef1081c | 2014-11-12 16:12:14 +1100 | [diff] [blame] | 838 | hostdata->flags = flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 839 | |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 840 | INIT_WORK(&hostdata->main_task, NCR5380_main); |
Finn Thain | 0ad0eff | 2016-01-03 16:05:21 +1100 | [diff] [blame] | 841 | hostdata->work_q = alloc_workqueue("ncr5380_%d", |
| 842 | WQ_UNBOUND | WQ_MEM_RECLAIM, |
| 843 | 1, instance->host_no); |
| 844 | if (!hostdata->work_q) |
| 845 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | |
Finn Thain | 8c32513 | 2014-11-12 16:11:58 +1100 | [diff] [blame] | 847 | prepare_info(instance); |
| 848 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 849 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 850 | NCR5380_write(MODE_REG, MR_BASE); |
| 851 | NCR5380_write(TARGET_COMMAND_REG, 0); |
| 852 | NCR5380_write(SELECT_ENABLE_REG, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 853 | |
Finn Thain | 2f854b8 | 2016-01-03 16:05:22 +1100 | [diff] [blame] | 854 | /* Calibrate register polling loop */ |
| 855 | i = 0; |
| 856 | deadline = jiffies + 1; |
| 857 | do { |
| 858 | cpu_relax(); |
| 859 | } while (time_is_after_jiffies(deadline)); |
| 860 | deadline += msecs_to_jiffies(256); |
| 861 | do { |
| 862 | NCR5380_read(STATUS_REG); |
| 863 | ++i; |
| 864 | cpu_relax(); |
| 865 | } while (time_is_after_jiffies(deadline)); |
| 866 | hostdata->accesses_per_ms = i / 256; |
| 867 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 868 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 869 | } |
| 870 | |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 871 | /** |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 872 | * NCR5380_maybe_reset_bus - Detect and correct bus wedge problems. |
| 873 | * @instance: adapter to check |
| 874 | * |
| 875 | * If the system crashed, it may have crashed with a connected target and |
| 876 | * the SCSI bus busy. Check for BUS FREE phase. If not, try to abort the |
| 877 | * currently established nexus, which we know nothing about. Failing that |
| 878 | * do a bus reset. |
| 879 | * |
| 880 | * Note that a bus reset will cause the chip to assert IRQ. |
| 881 | * |
| 882 | * Returns 0 if successful, otherwise -ENXIO. |
| 883 | */ |
| 884 | |
| 885 | static int NCR5380_maybe_reset_bus(struct Scsi_Host *instance) |
| 886 | { |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 887 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 888 | int pass; |
| 889 | |
| 890 | for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) { |
| 891 | switch (pass) { |
| 892 | case 1: |
| 893 | case 3: |
| 894 | case 5: |
| 895 | shost_printk(KERN_ERR, instance, "SCSI bus busy, waiting up to five seconds\n"); |
| 896 | NCR5380_poll_politely(instance, |
| 897 | STATUS_REG, SR_BSY, 0, 5 * HZ); |
| 898 | break; |
| 899 | case 2: |
| 900 | shost_printk(KERN_ERR, instance, "bus busy, attempting abort\n"); |
| 901 | do_abort(instance); |
| 902 | break; |
| 903 | case 4: |
| 904 | shost_printk(KERN_ERR, instance, "bus busy, attempting reset\n"); |
| 905 | do_reset(instance); |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 906 | /* Wait after a reset; the SCSI standard calls for |
| 907 | * 250ms, we wait 500ms to be on the safe side. |
| 908 | * But some Toshiba CD-ROMs need ten times that. |
| 909 | */ |
| 910 | if (hostdata->flags & FLAG_TOSHIBA_DELAY) |
| 911 | msleep(2500); |
| 912 | else |
| 913 | msleep(500); |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 914 | break; |
| 915 | case 6: |
| 916 | shost_printk(KERN_ERR, instance, "bus locked solid\n"); |
| 917 | return -ENXIO; |
| 918 | } |
| 919 | } |
| 920 | return 0; |
| 921 | } |
| 922 | |
| 923 | /** |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 924 | * NCR5380_exit - remove an NCR5380 |
| 925 | * @instance: adapter to remove |
| 926 | * |
| 927 | * Assumes that no more work can be queued (e.g. by NCR5380_intr). |
| 928 | */ |
| 929 | |
Geert Uytterhoeven | 6323e4f | 2011-06-13 20:39:15 +0200 | [diff] [blame] | 930 | static void NCR5380_exit(struct Scsi_Host *instance) |
| 931 | { |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 932 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 933 | |
| 934 | cancel_work_sync(&hostdata->main_task); |
Finn Thain | 0ad0eff | 2016-01-03 16:05:21 +1100 | [diff] [blame] | 935 | destroy_workqueue(hostdata->work_q); |
Geert Uytterhoeven | 6323e4f | 2011-06-13 20:39:15 +0200 | [diff] [blame] | 936 | } |
| 937 | |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 938 | /** |
| 939 | * NCR5380_queue_command - queue a command |
| 940 | * @instance: the relevant SCSI adapter |
| 941 | * @cmd: SCSI command |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | * |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 943 | * cmd is added to the per instance issue_queue, with minor |
| 944 | * twiddling done to the host specific fields of cmd. If the |
| 945 | * main coroutine is not running, it is restarted. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | */ |
| 947 | |
Finn Thain | 16b29e7 | 2014-11-12 16:12:08 +1100 | [diff] [blame] | 948 | static int NCR5380_queue_command(struct Scsi_Host *instance, |
| 949 | struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 950 | { |
Finn Thain | 16b29e7 | 2014-11-12 16:12:08 +1100 | [diff] [blame] | 951 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 952 | struct scsi_cmnd *tmp; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 953 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 954 | |
| 955 | #if (NDEBUG & NDEBUG_NO_WRITE) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 956 | switch (cmd->cmnd[0]) { |
| 957 | case WRITE_6: |
| 958 | case WRITE_10: |
| 959 | printk(KERN_NOTICE "scsi%d: WRITE attempted with NO_WRITE debugging flag set\n", |
| 960 | H_NO(cmd)); |
| 961 | cmd->result = (DID_ERROR << 16); |
Finn Thain | 16b29e7 | 2014-11-12 16:12:08 +1100 | [diff] [blame] | 962 | cmd->scsi_done(cmd); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 963 | return 0; |
| 964 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | #endif /* (NDEBUG & NDEBUG_NO_WRITE) */ |
| 966 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 967 | /* |
| 968 | * We use the host_scribble field as a pointer to the next command |
| 969 | * in a queue |
| 970 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | |
Roman Zippel | 3130d90 | 2007-05-01 22:32:37 +0200 | [diff] [blame] | 972 | SET_NEXT(cmd, NULL); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 973 | cmd->result = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 975 | /* |
| 976 | * Insert the cmd into the issue queue. Note that REQUEST SENSE |
| 977 | * commands are added to the head of the queue since any command will |
| 978 | * clear the contingent allegiance condition that exists and the |
| 979 | * sense data is only guaranteed to be valid while the condition exists. |
| 980 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 982 | /* ++guenther: now that the issue queue is being set up, we can lock ST-DMA. |
| 983 | * Otherwise a running NCR5380_main may steal the lock. |
| 984 | * Lock before actually inserting due to fairness reasons explained in |
| 985 | * atari_scsi.c. If we insert first, then it's impossible for this driver |
| 986 | * to release the lock. |
| 987 | * Stop timer for this command while waiting for the lock, or timeouts |
| 988 | * may happen (and they really do), and it's no good if the command doesn't |
| 989 | * appear in any of the queues. |
| 990 | * ++roman: Just disabling the NCR interrupt isn't sufficient here, |
| 991 | * because also a timer int can trigger an abort or reset, which would |
| 992 | * alter queues and touch the lock. |
| 993 | */ |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 994 | if (!NCR5380_acquire_dma_irq(instance)) |
Finn Thain | 16b29e7 | 2014-11-12 16:12:08 +1100 | [diff] [blame] | 995 | return SCSI_MLQUEUE_HOST_BUSY; |
| 996 | |
| 997 | local_irq_save(flags); |
| 998 | |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 999 | /* |
| 1000 | * Insert the cmd into the issue queue. Note that REQUEST SENSE |
| 1001 | * commands are added to the head of the queue since any command will |
| 1002 | * clear the contingent allegiance condition that exists and the |
| 1003 | * sense data is only guaranteed to be valid while the condition exists. |
| 1004 | */ |
| 1005 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1006 | if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) { |
| 1007 | LIST(cmd, hostdata->issue_queue); |
Roman Zippel | 3130d90 | 2007-05-01 22:32:37 +0200 | [diff] [blame] | 1008 | SET_NEXT(cmd, hostdata->issue_queue); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1009 | hostdata->issue_queue = cmd; |
| 1010 | } else { |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 1011 | for (tmp = (struct scsi_cmnd *)hostdata->issue_queue; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1012 | NEXT(tmp); tmp = NEXT(tmp)) |
| 1013 | ; |
| 1014 | LIST(cmd, tmp); |
Roman Zippel | 3130d90 | 2007-05-01 22:32:37 +0200 | [diff] [blame] | 1015 | SET_NEXT(tmp, cmd); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1016 | } |
| 1017 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1018 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1019 | dprintk(NDEBUG_QUEUES, "scsi%d: command added to %s of queue\n", H_NO(cmd), |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1020 | (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1021 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1022 | /* If queue_command() is called from an interrupt (real one or bottom |
| 1023 | * half), we let queue_main() do the job of taking care about main. If it |
| 1024 | * is already running, this is a no-op, else main will be queued. |
| 1025 | * |
| 1026 | * If we're not in an interrupt, we can call NCR5380_main() |
| 1027 | * unconditionally, because it cannot be already running. |
| 1028 | */ |
Finn Thain | 16b29e7 | 2014-11-12 16:12:08 +1100 | [diff] [blame] | 1029 | if (in_interrupt() || irqs_disabled()) |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 1030 | queue_main(hostdata); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1031 | else |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 1032 | NCR5380_main(&hostdata->main_task); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1033 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | } |
| 1035 | |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 1036 | static inline void maybe_release_dma_irq(struct Scsi_Host *instance) |
| 1037 | { |
| 1038 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
| 1039 | |
| 1040 | /* Caller does the locking needed to set & test these data atomically */ |
| 1041 | if (!hostdata->disconnected_queue && |
| 1042 | !hostdata->issue_queue && |
| 1043 | !hostdata->connected && |
| 1044 | !hostdata->retain_dma_intr) |
| 1045 | NCR5380_release_dma_irq(instance); |
| 1046 | } |
| 1047 | |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 1048 | /** |
| 1049 | * NCR5380_main - NCR state machines |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1050 | * |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 1051 | * NCR5380_main is a coroutine that runs as long as more work can |
| 1052 | * be done on the NCR5380 host adapters in a system. Both |
| 1053 | * NCR5380_queue_command() and NCR5380_intr() will try to start it |
| 1054 | * in case it is not running. |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1055 | * |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 1056 | * Locks: called as its own thread with no locks held. |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1057 | */ |
| 1058 | |
Geert Uytterhoeven | b312b38 | 2007-05-01 22:32:48 +0200 | [diff] [blame] | 1059 | static void NCR5380_main(struct work_struct *work) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1060 | { |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 1061 | struct NCR5380_hostdata *hostdata = |
| 1062 | container_of(work, struct NCR5380_hostdata, main_task); |
| 1063 | struct Scsi_Host *instance = hostdata->host; |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 1064 | struct scsi_cmnd *tmp, *prev; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1065 | int done; |
| 1066 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1068 | /* |
| 1069 | * We run (with interrupts disabled) until we're sure that none of |
| 1070 | * the host adapters have anything that can be done, at which point |
| 1071 | * we set main_running to 0 and exit. |
| 1072 | * |
| 1073 | * Interrupts are enabled before doing various other internal |
| 1074 | * instructions, after we've decided that we need to run through |
| 1075 | * the loop again. |
| 1076 | * |
| 1077 | * this should prevent any race conditions. |
| 1078 | * |
| 1079 | * ++roman: Just disabling the NCR interrupt isn't sufficient here, |
| 1080 | * because also a timer int can trigger an abort or reset, which can |
| 1081 | * alter queues and touch the Falcon lock. |
| 1082 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1084 | /* Tell int handlers main() is now already executing. Note that |
| 1085 | no races are possible here. If an int comes in before |
| 1086 | 'main_running' is set here, and queues/executes main via the |
| 1087 | task queue, it doesn't do any harm, just this instance of main |
| 1088 | won't find any work left to do. */ |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 1089 | if (hostdata->main_running) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1090 | return; |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 1091 | hostdata->main_running = 1; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1092 | |
| 1093 | local_save_flags(flags); |
| 1094 | do { |
| 1095 | local_irq_disable(); /* Freeze request queues */ |
| 1096 | done = 1; |
| 1097 | |
| 1098 | if (!hostdata->connected) { |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1099 | dprintk(NDEBUG_MAIN, "scsi%d: not connected\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1100 | /* |
| 1101 | * Search through the issue_queue for a command destined |
| 1102 | * for a target that's not busy. |
| 1103 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | #if (NDEBUG & NDEBUG_LISTS) |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 1105 | for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, prev = NULL; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1106 | tmp && (tmp != prev); prev = tmp, tmp = NEXT(tmp)) |
| 1107 | ; |
| 1108 | /*printk("%p ", tmp);*/ |
| 1109 | if ((tmp == prev) && tmp) |
| 1110 | printk(" LOOP\n"); |
| 1111 | /* else printk("\n"); */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1112 | #endif |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 1113 | for (tmp = (struct scsi_cmnd *) hostdata->issue_queue, |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1114 | prev = NULL; tmp; prev = tmp, tmp = NEXT(tmp)) { |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 1115 | u8 lun = tmp->device->lun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1116 | |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 1117 | dprintk(NDEBUG_LISTS, |
| 1118 | "MAIN tmp=%p target=%d busy=%d lun=%d\n", |
| 1119 | tmp, scmd_id(tmp), hostdata->busy[scmd_id(tmp)], |
| 1120 | lun); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1121 | /* When we find one, remove it from the issue queue. */ |
| 1122 | /* ++guenther: possible race with Falcon locking */ |
| 1123 | if ( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | #ifdef SUPPORT_TAGS |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1125 | !is_lun_busy( tmp, tmp->cmnd[0] != REQUEST_SENSE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | #else |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 1127 | !(hostdata->busy[tmp->device->id] & (1 << lun)) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1128 | #endif |
| 1129 | ) { |
| 1130 | /* ++guenther: just to be sure, this must be atomic */ |
| 1131 | local_irq_disable(); |
| 1132 | if (prev) { |
| 1133 | REMOVE(prev, NEXT(prev), tmp, NEXT(tmp)); |
Roman Zippel | 3130d90 | 2007-05-01 22:32:37 +0200 | [diff] [blame] | 1134 | SET_NEXT(prev, NEXT(tmp)); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1135 | } else { |
| 1136 | REMOVE(-1, hostdata->issue_queue, tmp, NEXT(tmp)); |
| 1137 | hostdata->issue_queue = NEXT(tmp); |
| 1138 | } |
Roman Zippel | 3130d90 | 2007-05-01 22:32:37 +0200 | [diff] [blame] | 1139 | SET_NEXT(tmp, NULL); |
Finn Thain | ef1081c | 2014-11-12 16:12:14 +1100 | [diff] [blame] | 1140 | hostdata->retain_dma_intr++; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1141 | |
| 1142 | /* reenable interrupts after finding one */ |
| 1143 | local_irq_restore(flags); |
| 1144 | |
| 1145 | /* |
| 1146 | * Attempt to establish an I_T_L nexus here. |
| 1147 | * On success, instance->hostdata->connected is set. |
| 1148 | * On failure, we must add the command back to the |
| 1149 | * issue queue so we can keep trying. |
| 1150 | */ |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1151 | dprintk(NDEBUG_MAIN, "scsi%d: main(): command for target %d " |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1152 | "lun %d removed from issue_queue\n", |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 1153 | HOSTNO, tmp->device->id, lun); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1154 | /* |
| 1155 | * REQUEST SENSE commands are issued without tagged |
| 1156 | * queueing, even on SCSI-II devices because the |
| 1157 | * contingent allegiance condition exists for the |
| 1158 | * entire unit. |
| 1159 | */ |
| 1160 | /* ++roman: ...and the standard also requires that |
| 1161 | * REQUEST SENSE command are untagged. |
| 1162 | */ |
| 1163 | |
| 1164 | #ifdef SUPPORT_TAGS |
| 1165 | cmd_get_tag(tmp, tmp->cmnd[0] != REQUEST_SENSE); |
| 1166 | #endif |
Finn Thain | 76f13b9 | 2014-11-12 16:11:53 +1100 | [diff] [blame] | 1167 | if (!NCR5380_select(instance, tmp)) { |
Finn Thain | 1f1b0c7 | 2016-01-03 16:05:17 +1100 | [diff] [blame] | 1168 | /* OK or bad target */ |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 1169 | local_irq_disable(); |
Finn Thain | ef1081c | 2014-11-12 16:12:14 +1100 | [diff] [blame] | 1170 | hostdata->retain_dma_intr--; |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 1171 | maybe_release_dma_irq(instance); |
| 1172 | local_irq_restore(flags); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1173 | } else { |
Finn Thain | 1f1b0c7 | 2016-01-03 16:05:17 +1100 | [diff] [blame] | 1174 | /* Need to retry */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1175 | local_irq_disable(); |
| 1176 | LIST(tmp, hostdata->issue_queue); |
Roman Zippel | 3130d90 | 2007-05-01 22:32:37 +0200 | [diff] [blame] | 1177 | SET_NEXT(tmp, hostdata->issue_queue); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1178 | hostdata->issue_queue = tmp; |
| 1179 | #ifdef SUPPORT_TAGS |
| 1180 | cmd_free_tag(tmp); |
| 1181 | #endif |
Finn Thain | ef1081c | 2014-11-12 16:12:14 +1100 | [diff] [blame] | 1182 | hostdata->retain_dma_intr--; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1183 | local_irq_restore(flags); |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1184 | dprintk(NDEBUG_MAIN, "scsi%d: main(): select() failed, " |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1185 | "returned to issue_queue\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1186 | } |
Finn Thain | 1f1b0c7 | 2016-01-03 16:05:17 +1100 | [diff] [blame] | 1187 | if (hostdata->connected) |
| 1188 | break; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1189 | } /* if target/lun/target queue is not busy */ |
| 1190 | } /* for issue_queue */ |
| 1191 | } /* if (!hostdata->connected) */ |
| 1192 | |
| 1193 | if (hostdata->connected |
| 1194 | #ifdef REAL_DMA |
| 1195 | && !hostdata->dma_len |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | #endif |
| 1197 | ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1198 | local_irq_restore(flags); |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1199 | dprintk(NDEBUG_MAIN, "scsi%d: main: performing information transfer\n", |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1200 | HOSTNO); |
| 1201 | NCR5380_information_transfer(instance); |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1202 | dprintk(NDEBUG_MAIN, "scsi%d: main: done set false\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1203 | done = 0; |
| 1204 | } |
| 1205 | } while (!done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1206 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1207 | /* Better allow ints _after_ 'main_running' has been cleared, else |
| 1208 | an interrupt could believe we'll pick up the work it left for |
| 1209 | us, but we won't see it anymore here... */ |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 1210 | hostdata->main_running = 0; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1211 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | } |
| 1213 | |
| 1214 | |
| 1215 | #ifdef REAL_DMA |
| 1216 | /* |
| 1217 | * Function : void NCR5380_dma_complete (struct Scsi_Host *instance) |
| 1218 | * |
| 1219 | * Purpose : Called by interrupt handler when DMA finishes or a phase |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1220 | * mismatch occurs (which would finish the DMA transfer). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | * |
| 1222 | * Inputs : instance - this instance of the NCR5380. |
| 1223 | * |
| 1224 | */ |
| 1225 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1226 | static void NCR5380_dma_complete(struct Scsi_Host *instance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1228 | SETUP_HOSTDATA(instance); |
Finn Thain | 01bd908 | 2014-11-12 16:12:23 +1100 | [diff] [blame] | 1229 | int transferred; |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 1230 | unsigned char **data; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1231 | volatile int *count; |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 1232 | int saved_data = 0, overrun = 0; |
| 1233 | unsigned char p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1235 | if (!hostdata->connected) { |
| 1236 | printk(KERN_WARNING "scsi%d: received end of DMA interrupt with " |
| 1237 | "no connected cmd\n", HOSTNO); |
| 1238 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | |
Finn Thain | ef1081c | 2014-11-12 16:12:14 +1100 | [diff] [blame] | 1241 | if (hostdata->read_overruns) { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1242 | p = hostdata->connected->SCp.phase; |
| 1243 | if (p & SR_IO) { |
| 1244 | udelay(10); |
| 1245 | if ((NCR5380_read(BUS_AND_STATUS_REG) & |
| 1246 | (BASR_PHASE_MATCH|BASR_ACK)) == |
| 1247 | (BASR_PHASE_MATCH|BASR_ACK)) { |
| 1248 | saved_data = NCR5380_read(INPUT_DATA_REG); |
| 1249 | overrun = 1; |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1250 | dprintk(NDEBUG_DMA, "scsi%d: read overrun handled\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1251 | } |
| 1252 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1253 | } |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1254 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1255 | dprintk(NDEBUG_DMA, "scsi%d: real DMA transfer complete, basr 0x%X, sr 0x%X\n", |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1256 | HOSTNO, NCR5380_read(BUS_AND_STATUS_REG), |
| 1257 | NCR5380_read(STATUS_REG)); |
| 1258 | |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 1259 | #if defined(CONFIG_SUN3) |
| 1260 | if ((sun3scsi_dma_finish(rq_data_dir(hostdata->connected->request)))) { |
| 1261 | pr_err("scsi%d: overrun in UDC counter -- not prepared to deal with this!\n", |
| 1262 | instance->host_no); |
| 1263 | BUG(); |
| 1264 | } |
| 1265 | |
| 1266 | /* make sure we're not stuck in a data phase */ |
| 1267 | if ((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) == |
| 1268 | (BASR_PHASE_MATCH | BASR_ACK)) { |
| 1269 | pr_err("scsi%d: BASR %02x\n", instance->host_no, |
| 1270 | NCR5380_read(BUS_AND_STATUS_REG)); |
| 1271 | pr_err("scsi%d: bus stuck in data phase -- probably a single byte overrun!\n", |
| 1272 | instance->host_no); |
| 1273 | BUG(); |
| 1274 | } |
| 1275 | #endif |
| 1276 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1277 | (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG); |
| 1278 | NCR5380_write(MODE_REG, MR_BASE); |
| 1279 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1280 | |
Finn Thain | 01bd908 | 2014-11-12 16:12:23 +1100 | [diff] [blame] | 1281 | transferred = hostdata->dma_len - NCR5380_dma_residual(instance); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1282 | hostdata->dma_len = 0; |
| 1283 | |
| 1284 | data = (unsigned char **)&hostdata->connected->SCp.ptr; |
| 1285 | count = &hostdata->connected->SCp.this_residual; |
Finn Thain | 01bd908 | 2014-11-12 16:12:23 +1100 | [diff] [blame] | 1286 | *data += transferred; |
| 1287 | *count -= transferred; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1288 | |
Finn Thain | ef1081c | 2014-11-12 16:12:14 +1100 | [diff] [blame] | 1289 | if (hostdata->read_overruns) { |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 1290 | int cnt, toPIO; |
| 1291 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1292 | if ((NCR5380_read(STATUS_REG) & PHASE_MASK) == p && (p & SR_IO)) { |
Finn Thain | ef1081c | 2014-11-12 16:12:14 +1100 | [diff] [blame] | 1293 | cnt = toPIO = hostdata->read_overruns; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1294 | if (overrun) { |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1295 | dprintk(NDEBUG_DMA, "Got an input overrun, using saved byte\n"); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1296 | *(*data)++ = saved_data; |
| 1297 | (*count)--; |
| 1298 | cnt--; |
| 1299 | toPIO--; |
| 1300 | } |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1301 | dprintk(NDEBUG_DMA, "Doing %d-byte PIO to 0x%08lx\n", cnt, (long)*data); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1302 | NCR5380_transfer_pio(instance, &p, &cnt, data); |
| 1303 | *count -= toPIO - cnt; |
| 1304 | } |
| 1305 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | } |
| 1307 | #endif /* REAL_DMA */ |
| 1308 | |
| 1309 | |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 1310 | /** |
| 1311 | * NCR5380_intr - generic NCR5380 irq handler |
| 1312 | * @irq: interrupt number |
| 1313 | * @dev_id: device info |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1314 | * |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 1315 | * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses |
| 1316 | * from the disconnected queue, and restarting NCR5380_main() |
| 1317 | * as required. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | */ |
| 1319 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1320 | static irqreturn_t NCR5380_intr(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1321 | { |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 1322 | struct Scsi_Host *instance = dev_id; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1323 | int done = 1, handled = 0; |
| 1324 | unsigned char basr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1326 | dprintk(NDEBUG_INTR, "scsi%d: NCR5380 irq triggered\n", HOSTNO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1327 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1328 | /* Look for pending interrupts */ |
| 1329 | basr = NCR5380_read(BUS_AND_STATUS_REG); |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1330 | dprintk(NDEBUG_INTR, "scsi%d: BASR=%02x\n", HOSTNO, basr); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1331 | /* dispatch to appropriate routine if found and done=0 */ |
| 1332 | if (basr & BASR_IRQ) { |
Finn Thain | 8ad3a59 | 2014-03-18 11:42:19 +1100 | [diff] [blame] | 1333 | NCR5380_dprint(NDEBUG_INTR, instance); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1334 | if ((NCR5380_read(STATUS_REG) & (SR_SEL|SR_IO)) == (SR_SEL|SR_IO)) { |
| 1335 | done = 0; |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1336 | dprintk(NDEBUG_INTR, "scsi%d: SEL interrupt\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1337 | NCR5380_reselect(instance); |
| 1338 | (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG); |
| 1339 | } else if (basr & BASR_PARITY_ERROR) { |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1340 | dprintk(NDEBUG_INTR, "scsi%d: PARITY interrupt\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1341 | (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG); |
| 1342 | } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) { |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1343 | dprintk(NDEBUG_INTR, "scsi%d: RESET interrupt\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1344 | (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG); |
| 1345 | } else { |
| 1346 | /* |
| 1347 | * The rest of the interrupt conditions can occur only during a |
| 1348 | * DMA transfer |
| 1349 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | |
| 1351 | #if defined(REAL_DMA) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1352 | /* |
| 1353 | * We should only get PHASE MISMATCH and EOP interrupts if we have |
| 1354 | * DMA enabled, so do a sanity check based on the current setting |
| 1355 | * of the MODE register. |
| 1356 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1357 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1358 | if ((NCR5380_read(MODE_REG) & MR_DMA_MODE) && |
| 1359 | ((basr & BASR_END_DMA_TRANSFER) || |
| 1360 | !(basr & BASR_PHASE_MATCH))) { |
| 1361 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1362 | dprintk(NDEBUG_INTR, "scsi%d: PHASE MISM or EOP interrupt\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1363 | NCR5380_dma_complete( instance ); |
| 1364 | done = 0; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1365 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | #endif /* REAL_DMA */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1367 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1368 | /* MS: Ignore unknown phase mismatch interrupts (caused by EOP interrupt) */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1369 | if (basr & BASR_PHASE_MATCH) |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 1370 | dprintk(NDEBUG_INTR, "scsi%d: unknown interrupt, " |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1371 | "BASR 0x%x, MR 0x%x, SR 0x%x\n", |
| 1372 | HOSTNO, basr, NCR5380_read(MODE_REG), |
| 1373 | NCR5380_read(STATUS_REG)); |
| 1374 | (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG); |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 1375 | #ifdef SUN3_SCSI_VME |
| 1376 | dregs->csr |= CSR_DMA_ENABLE; |
| 1377 | #endif |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1378 | } |
| 1379 | } /* if !(SELECTION || PARITY) */ |
| 1380 | handled = 1; |
| 1381 | } /* BASR & IRQ */ else { |
| 1382 | printk(KERN_NOTICE "scsi%d: interrupt without IRQ bit set in BASR, " |
| 1383 | "BASR 0x%X, MR 0x%X, SR 0x%x\n", HOSTNO, basr, |
| 1384 | NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG)); |
| 1385 | (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG); |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 1386 | #ifdef SUN3_SCSI_VME |
| 1387 | dregs->csr |= CSR_DMA_ENABLE; |
| 1388 | #endif |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1389 | } |
| 1390 | |
| 1391 | if (!done) { |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1392 | dprintk(NDEBUG_INTR, "scsi%d: in int routine, calling main\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1393 | /* Put a call to NCR5380_main() on the queue... */ |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 1394 | queue_main(shost_priv(instance)); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1395 | } |
| 1396 | return IRQ_RETVAL(handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1397 | } |
| 1398 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1399 | /* |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 1400 | * Function : int NCR5380_select(struct Scsi_Host *instance, |
| 1401 | * struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | * |
| 1403 | * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command, |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1404 | * including ARBITRATION, SELECTION, and initial message out for |
| 1405 | * IDENTIFY and queue messages. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1406 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1407 | * Inputs : instance - instantiation of the 5380 driver on which this |
Finn Thain | 76f13b9 | 2014-11-12 16:11:53 +1100 | [diff] [blame] | 1408 | * target lives, cmd - SCSI command to execute. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | * |
Finn Thain | 6323876 | 2016-01-03 16:05:15 +1100 | [diff] [blame] | 1410 | * Returns : -1 if selection failed but should be retried. |
| 1411 | * 0 if selection failed and should not be retried. |
| 1412 | * 0 if selection succeeded completely (hostdata->connected == cmd). |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1413 | * |
| 1414 | * Side effects : |
| 1415 | * If bus busy, arbitration failed, etc, NCR5380_select() will exit |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | * with registers as they should have been on entry - ie |
| 1417 | * SELECT_ENABLE will be set appropriately, the NCR5380 |
| 1418 | * will cease to drive any SCSI bus signals. |
| 1419 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1420 | * If successful : I_T_L or I_T_L_Q nexus will be established, |
| 1421 | * instance->connected will be set to cmd. |
| 1422 | * SELECT interrupt will be disabled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1424 | * If failed (no target) : cmd->scsi_done() will be called, and the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | * cmd->result host byte set to DID_BAD_TARGET. |
| 1426 | */ |
| 1427 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 1428 | static int NCR5380_select(struct Scsi_Host *instance, struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1430 | SETUP_HOSTDATA(instance); |
| 1431 | unsigned char tmp[3], phase; |
| 1432 | unsigned char *data; |
| 1433 | int len; |
Finn Thain | ae753a3 | 2016-01-03 16:05:23 +1100 | [diff] [blame^] | 1434 | int err; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1435 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | |
Finn Thain | 8ad3a59 | 2014-03-18 11:42:19 +1100 | [diff] [blame] | 1437 | NCR5380_dprint(NDEBUG_ARBITRATION, instance); |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1438 | dprintk(NDEBUG_ARBITRATION, "scsi%d: starting arbitration, id = %d\n", HOSTNO, |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1439 | instance->this_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1441 | /* |
| 1442 | * Set the phase bits to 0, otherwise the NCR5380 won't drive the |
| 1443 | * data bus during SELECTION. |
| 1444 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1445 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1446 | local_irq_save(flags); |
| 1447 | if (hostdata->connected) { |
| 1448 | local_irq_restore(flags); |
| 1449 | return -1; |
| 1450 | } |
| 1451 | NCR5380_write(TARGET_COMMAND_REG, 0); |
| 1452 | |
| 1453 | /* |
| 1454 | * Start arbitration. |
| 1455 | */ |
| 1456 | |
| 1457 | NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask); |
| 1458 | NCR5380_write(MODE_REG, MR_ARBITRATE); |
| 1459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1460 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1462 | /* Wait for arbitration logic to complete */ |
Michael Schmitz | fb810d1 | 2007-05-01 22:32:35 +0200 | [diff] [blame] | 1463 | #if defined(NCR_TIMEOUT) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1464 | { |
| 1465 | unsigned long timeout = jiffies + 2*NCR_TIMEOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1467 | while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) && |
| 1468 | time_before(jiffies, timeout) && !hostdata->connected) |
| 1469 | ; |
| 1470 | if (time_after_eq(jiffies, timeout)) { |
| 1471 | printk("scsi : arbitration timeout at %d\n", __LINE__); |
| 1472 | NCR5380_write(MODE_REG, MR_BASE); |
| 1473 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
| 1474 | return -1; |
| 1475 | } |
| 1476 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | #else /* NCR_TIMEOUT */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1478 | while (!(NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_PROGRESS) && |
| 1479 | !hostdata->connected) |
| 1480 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | #endif |
| 1482 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1483 | dprintk(NDEBUG_ARBITRATION, "scsi%d: arbitration complete\n", HOSTNO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1485 | if (hostdata->connected) { |
| 1486 | NCR5380_write(MODE_REG, MR_BASE); |
| 1487 | return -1; |
| 1488 | } |
| 1489 | /* |
| 1490 | * The arbitration delay is 2.2us, but this is a minimum and there is |
| 1491 | * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate |
| 1492 | * the integral nature of udelay(). |
| 1493 | * |
| 1494 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1495 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1496 | udelay(3); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1497 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1498 | /* Check for lost arbitration */ |
| 1499 | if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) || |
| 1500 | (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) || |
| 1501 | (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) || |
| 1502 | hostdata->connected) { |
| 1503 | NCR5380_write(MODE_REG, MR_BASE); |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1504 | dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting MR_ARBITRATE\n", |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1505 | HOSTNO); |
| 1506 | return -1; |
| 1507 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | |
Finn Thain | cf13b08 | 2016-01-03 16:05:18 +1100 | [diff] [blame] | 1509 | /* After/during arbitration, BSY should be asserted. |
| 1510 | * IBM DPES-31080 Version S31Q works now |
| 1511 | * Tnx to Thomas_Roesch@m2.maus.de for finding this! (Roman) |
| 1512 | */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1513 | NCR5380_write(INITIATOR_COMMAND_REG, |
| 1514 | ICR_BASE | ICR_ASSERT_SEL | ICR_ASSERT_BSY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1516 | if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) || |
| 1517 | hostdata->connected) { |
| 1518 | NCR5380_write(MODE_REG, MR_BASE); |
| 1519 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1520 | dprintk(NDEBUG_ARBITRATION, "scsi%d: lost arbitration, deasserting ICR_ASSERT_SEL\n", |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1521 | HOSTNO); |
| 1522 | return -1; |
| 1523 | } |
| 1524 | |
| 1525 | /* |
| 1526 | * Again, bus clear + bus settle time is 1.2us, however, this is |
| 1527 | * a minimum so we'll udelay ceil(1.2) |
| 1528 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | |
Finn Thain | 9c3f0e2 | 2016-01-03 16:05:11 +1100 | [diff] [blame] | 1530 | if (hostdata->flags & FLAG_TOSHIBA_DELAY) |
| 1531 | udelay(15); |
| 1532 | else |
| 1533 | udelay(2); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1534 | |
| 1535 | if (hostdata->connected) { |
| 1536 | NCR5380_write(MODE_REG, MR_BASE); |
| 1537 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1538 | return -1; |
| 1539 | } |
| 1540 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1541 | dprintk(NDEBUG_ARBITRATION, "scsi%d: won arbitration\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1542 | |
| 1543 | /* |
| 1544 | * Now that we have won arbitration, start Selection process, asserting |
| 1545 | * the host and target ID's on the SCSI bus. |
| 1546 | */ |
| 1547 | |
| 1548 | NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << cmd->device->id))); |
| 1549 | |
| 1550 | /* |
| 1551 | * Raise ATN while SEL is true before BSY goes false from arbitration, |
| 1552 | * since this is the only way to guarantee that we'll get a MESSAGE OUT |
| 1553 | * phase immediately after selection. |
| 1554 | */ |
| 1555 | |
| 1556 | NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | |
| 1557 | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL )); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | NCR5380_write(MODE_REG, MR_BASE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1560 | /* |
| 1561 | * Reselect interrupts must be turned off prior to the dropping of BSY, |
| 1562 | * otherwise we will trigger an interrupt. |
| 1563 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1565 | if (hostdata->connected) { |
| 1566 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1567 | return -1; |
| 1568 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1570 | NCR5380_write(SELECT_ENABLE_REG, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1572 | /* |
| 1573 | * The initiator shall then wait at least two deskew delays and release |
| 1574 | * the BSY signal. |
| 1575 | */ |
| 1576 | udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1577 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1578 | /* Reset BSY */ |
| 1579 | NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | |
| 1580 | ICR_ASSERT_ATN | ICR_ASSERT_SEL)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1581 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1582 | /* |
| 1583 | * Something weird happens when we cease to drive BSY - looks |
| 1584 | * like the board/chip is letting us do another read before the |
| 1585 | * appropriate propagation delay has expired, and we're confusing |
| 1586 | * a BSY signal from ourselves as the target's response to SELECTION. |
| 1587 | * |
| 1588 | * A small delay (the 'C++' frontend breaks the pipeline with an |
| 1589 | * unnecessary jump, making it work on my 386-33/Trantor T128, the |
| 1590 | * tighter 'C' code breaks and requires this) solves the problem - |
| 1591 | * the 1 us delay is arbitrary, and only used because this delay will |
| 1592 | * be the same on other platforms and since it works here, it should |
| 1593 | * work there. |
| 1594 | * |
| 1595 | * wingel suggests that this could be due to failing to wait |
| 1596 | * one deskew delay. |
| 1597 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1599 | udelay(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1600 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1601 | dprintk(NDEBUG_SELECTION, "scsi%d: selecting target %d\n", HOSTNO, cmd->device->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1602 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1603 | /* |
| 1604 | * The SCSI specification calls for a 250 ms timeout for the actual |
| 1605 | * selection. |
| 1606 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | |
Finn Thain | ae753a3 | 2016-01-03 16:05:23 +1100 | [diff] [blame^] | 1608 | err = NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, SR_BSY, |
| 1609 | msecs_to_jiffies(250)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1610 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1611 | if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) { |
| 1612 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1613 | NCR5380_reselect(instance); |
| 1614 | printk(KERN_ERR "scsi%d: reselection after won arbitration?\n", |
| 1615 | HOSTNO); |
| 1616 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
| 1617 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | } |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1619 | |
Finn Thain | ae753a3 | 2016-01-03 16:05:23 +1100 | [diff] [blame^] | 1620 | if (err < 0) { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1621 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1622 | cmd->result = DID_BAD_TARGET << 16; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1623 | #ifdef SUPPORT_TAGS |
| 1624 | cmd_free_tag(cmd); |
| 1625 | #endif |
| 1626 | cmd->scsi_done(cmd); |
| 1627 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1628 | dprintk(NDEBUG_SELECTION, "scsi%d: target did not respond within 250ms\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1629 | return 0; |
| 1630 | } |
| 1631 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1632 | /* |
Finn Thain | ae753a3 | 2016-01-03 16:05:23 +1100 | [diff] [blame^] | 1633 | * No less than two deskew delays after the initiator detects the |
| 1634 | * BSY signal is true, it shall release the SEL signal and may |
| 1635 | * change the DATA BUS. -wingel |
| 1636 | */ |
| 1637 | |
| 1638 | udelay(1); |
| 1639 | |
| 1640 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); |
| 1641 | |
| 1642 | /* |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1643 | * Since we followed the SCSI spec, and raised ATN while SEL |
| 1644 | * was true but before BSY was false during selection, the information |
| 1645 | * transfer phase should be a MESSAGE OUT phase so that we can send the |
| 1646 | * IDENTIFY message. |
| 1647 | * |
| 1648 | * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG |
| 1649 | * message (2 bytes) with a tag ID that we increment with every command |
| 1650 | * until it wraps back to 0. |
| 1651 | * |
| 1652 | * XXX - it turns out that there are some broken SCSI-II devices, |
| 1653 | * which claim to support tagged queuing but fail when more than |
| 1654 | * some number of commands are issued at once. |
| 1655 | */ |
| 1656 | |
| 1657 | /* Wait for start of REQ/ACK handshake */ |
| 1658 | while (!(NCR5380_read(STATUS_REG) & SR_REQ)) |
| 1659 | ; |
| 1660 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1661 | dprintk(NDEBUG_SELECTION, "scsi%d: target %d selected, going into MESSAGE OUT phase.\n", |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1662 | HOSTNO, cmd->device->id); |
| 1663 | tmp[0] = IDENTIFY(1, cmd->device->lun); |
| 1664 | |
| 1665 | #ifdef SUPPORT_TAGS |
| 1666 | if (cmd->tag != TAG_NONE) { |
| 1667 | tmp[1] = hostdata->last_message = SIMPLE_QUEUE_TAG; |
| 1668 | tmp[2] = cmd->tag; |
| 1669 | len = 3; |
| 1670 | } else |
| 1671 | len = 1; |
| 1672 | #else |
| 1673 | len = 1; |
| 1674 | cmd->tag = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1675 | #endif /* SUPPORT_TAGS */ |
| 1676 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1677 | /* Send message(s) */ |
| 1678 | data = tmp; |
| 1679 | phase = PHASE_MSGOUT; |
| 1680 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1681 | dprintk(NDEBUG_SELECTION, "scsi%d: nexus established.\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1682 | /* XXX need to handle errors here */ |
| 1683 | hostdata->connected = cmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | #ifndef SUPPORT_TAGS |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1685 | hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun); |
| 1686 | #endif |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 1687 | #ifdef SUN3_SCSI_VME |
| 1688 | dregs->csr |= CSR_INTR; |
| 1689 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1691 | initialize_SCp(cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1692 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1693 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | } |
| 1695 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1696 | /* |
| 1697 | * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | * unsigned char *phase, int *count, unsigned char **data) |
| 1699 | * |
| 1700 | * Purpose : transfers data in given phase using polled I/O |
| 1701 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1702 | * Inputs : instance - instance of driver, *phase - pointer to |
| 1703 | * what phase is expected, *count - pointer to number of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1704 | * bytes to transfer, **data - pointer to data pointer. |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1705 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1706 | * Returns : -1 when different phase is entered without transferring |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1707 | * maximum number of bytes, 0 if all bytes are transferred or exit |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | * is in same phase. |
| 1709 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1710 | * Also, *phase, *count, *data are modified in place. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | * |
| 1712 | * XXX Note : handling for bus free may be useful. |
| 1713 | */ |
| 1714 | |
| 1715 | /* |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1716 | * Note : this code is not as quick as it could be, however it |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | * IS 100% reliable, and for the actual data transfer where speed |
| 1718 | * counts, we will always do a pseudo DMA or DMA transfer. |
| 1719 | */ |
| 1720 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1721 | static int NCR5380_transfer_pio(struct Scsi_Host *instance, |
| 1722 | unsigned char *phase, int *count, |
| 1723 | unsigned char **data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1725 | register unsigned char p = *phase, tmp; |
| 1726 | register int c = *count; |
| 1727 | register unsigned char *d = *data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1728 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1729 | /* |
| 1730 | * The NCR5380 chip will only drive the SCSI bus when the |
| 1731 | * phase specified in the appropriate bits of the TARGET COMMAND |
| 1732 | * REGISTER match the STATUS REGISTER |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1733 | */ |
| 1734 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1735 | NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1737 | do { |
| 1738 | /* |
| 1739 | * Wait for assertion of REQ, after which the phase bits will be |
| 1740 | * valid |
| 1741 | */ |
| 1742 | while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ)) |
| 1743 | ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1745 | dprintk(NDEBUG_HANDSHAKE, "scsi%d: REQ detected\n", HOSTNO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1746 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1747 | /* Check for phase mismatch */ |
| 1748 | if ((tmp & PHASE_MASK) != p) { |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1749 | dprintk(NDEBUG_PIO, "scsi%d: phase mismatch\n", HOSTNO); |
Finn Thain | 8ad3a59 | 2014-03-18 11:42:19 +1100 | [diff] [blame] | 1750 | NCR5380_dprint_phase(NDEBUG_PIO, instance); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1751 | break; |
| 1752 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1754 | /* Do actual transfer from SCSI bus to / from memory */ |
| 1755 | if (!(p & SR_IO)) |
| 1756 | NCR5380_write(OUTPUT_DATA_REG, *d); |
| 1757 | else |
| 1758 | *d = NCR5380_read(CURRENT_SCSI_DATA_REG); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1760 | ++d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1762 | /* |
| 1763 | * The SCSI standard suggests that in MSGOUT phase, the initiator |
| 1764 | * should drop ATN on the last byte of the message phase |
| 1765 | * after REQ has been asserted for the handshake but before |
| 1766 | * the initiator raises ACK. |
| 1767 | */ |
| 1768 | |
| 1769 | if (!(p & SR_IO)) { |
| 1770 | if (!((p & SR_MSG) && c > 1)) { |
| 1771 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA); |
Finn Thain | 8ad3a59 | 2014-03-18 11:42:19 +1100 | [diff] [blame] | 1772 | NCR5380_dprint(NDEBUG_PIO, instance); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1773 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | |
| 1774 | ICR_ASSERT_DATA | ICR_ASSERT_ACK); |
| 1775 | } else { |
| 1776 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | |
| 1777 | ICR_ASSERT_DATA | ICR_ASSERT_ATN); |
Finn Thain | 8ad3a59 | 2014-03-18 11:42:19 +1100 | [diff] [blame] | 1778 | NCR5380_dprint(NDEBUG_PIO, instance); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1779 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | |
| 1780 | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK); |
| 1781 | } |
| 1782 | } else { |
Finn Thain | 8ad3a59 | 2014-03-18 11:42:19 +1100 | [diff] [blame] | 1783 | NCR5380_dprint(NDEBUG_PIO, instance); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1784 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK); |
| 1785 | } |
| 1786 | |
| 1787 | while (NCR5380_read(STATUS_REG) & SR_REQ) |
| 1788 | ; |
| 1789 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1790 | dprintk(NDEBUG_HANDSHAKE, "scsi%d: req false, handshake complete\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1791 | |
| 1792 | /* |
| 1793 | * We have several special cases to consider during REQ/ACK handshaking : |
| 1794 | * 1. We were in MSGOUT phase, and we are on the last byte of the |
| 1795 | * message. ATN must be dropped as ACK is dropped. |
| 1796 | * |
| 1797 | * 2. We are in a MSGIN phase, and we are on the last byte of the |
| 1798 | * message. We must exit with ACK asserted, so that the calling |
| 1799 | * code may raise ATN before dropping ACK to reject the message. |
| 1800 | * |
| 1801 | * 3. ACK and ATN are clear and the target may proceed as normal. |
| 1802 | */ |
| 1803 | if (!(p == PHASE_MSGIN && c == 1)) { |
| 1804 | if (p == PHASE_MSGOUT && c > 1) |
| 1805 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); |
| 1806 | else |
| 1807 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1808 | } |
| 1809 | } while (--c); |
| 1810 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1811 | dprintk(NDEBUG_PIO, "scsi%d: residual %d\n", HOSTNO, c); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1812 | |
| 1813 | *count = c; |
| 1814 | *data = d; |
| 1815 | tmp = NCR5380_read(STATUS_REG); |
| 1816 | /* The phase read from the bus is valid if either REQ is (already) |
| 1817 | * asserted or if ACK hasn't been released yet. The latter is the case if |
| 1818 | * we're in MSGIN and all wanted bytes have been received. |
| 1819 | */ |
| 1820 | if ((tmp & SR_REQ) || (p == PHASE_MSGIN && c == 0)) |
| 1821 | *phase = tmp & PHASE_MASK; |
| 1822 | else |
| 1823 | *phase = PHASE_UNKNOWN; |
| 1824 | |
| 1825 | if (!c || (*phase == p)) |
| 1826 | return 0; |
| 1827 | else |
| 1828 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | } |
| 1830 | |
Finn Thain | 636b1ec | 2016-01-03 16:05:10 +1100 | [diff] [blame] | 1831 | /** |
| 1832 | * do_reset - issue a reset command |
| 1833 | * @instance: adapter to reset |
| 1834 | * |
| 1835 | * Issue a reset sequence to the NCR5380 and try and get the bus |
| 1836 | * back into sane shape. |
| 1837 | * |
| 1838 | * This clears the reset interrupt flag because there may be no handler for |
| 1839 | * it. When the driver is initialized, the NCR5380_intr() handler has not yet |
| 1840 | * been installed. And when in EH we may have released the ST DMA interrupt. |
| 1841 | */ |
| 1842 | |
| 1843 | static void do_reset(struct Scsi_Host *instance) |
| 1844 | { |
| 1845 | unsigned long flags; |
| 1846 | |
| 1847 | local_irq_save(flags); |
| 1848 | NCR5380_write(TARGET_COMMAND_REG, |
| 1849 | PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK)); |
| 1850 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST); |
| 1851 | udelay(50); |
| 1852 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 1853 | (void)NCR5380_read(RESET_PARITY_INTERRUPT_REG); |
| 1854 | local_irq_restore(flags); |
| 1855 | } |
| 1856 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1857 | /* |
| 1858 | * Function : do_abort (Scsi_Host *host) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1859 | * |
| 1860 | * Purpose : abort the currently established nexus. Should only be |
| 1861 | * called from a routine which can drop into a |
| 1862 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1863 | * Returns : 0 on success, -1 on failure. |
| 1864 | */ |
| 1865 | |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 1866 | static int do_abort(struct Scsi_Host *instance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1867 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1868 | unsigned char tmp, *msgptr, phase; |
| 1869 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1871 | /* Request message out phase */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1874 | /* |
| 1875 | * Wait for the target to indicate a valid phase by asserting |
| 1876 | * REQ. Once this happens, we'll have either a MSGOUT phase |
| 1877 | * and can immediately send the ABORT message, or we'll have some |
| 1878 | * other phase and will have to source/sink data. |
| 1879 | * |
| 1880 | * We really don't care what value was on the bus or what value |
| 1881 | * the target sees, so we just handshake. |
| 1882 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1883 | |
Roel Kluin | 3be38e7 | 2007-11-15 21:13:46 +0100 | [diff] [blame] | 1884 | while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ)) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1885 | ; |
| 1886 | |
| 1887 | NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp)); |
| 1888 | |
| 1889 | if ((tmp & PHASE_MASK) != PHASE_MSGOUT) { |
| 1890 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | |
| 1891 | ICR_ASSERT_ACK); |
| 1892 | while (NCR5380_read(STATUS_REG) & SR_REQ) |
| 1893 | ; |
| 1894 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); |
| 1895 | } |
| 1896 | |
| 1897 | tmp = ABORT; |
| 1898 | msgptr = &tmp; |
| 1899 | len = 1; |
| 1900 | phase = PHASE_MSGOUT; |
Finn Thain | a53a21e | 2014-11-12 16:12:21 +1100 | [diff] [blame] | 1901 | NCR5380_transfer_pio(instance, &phase, &len, &msgptr); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1902 | |
| 1903 | /* |
| 1904 | * If we got here, and the command completed successfully, |
| 1905 | * we're about to go into bus free state. |
| 1906 | */ |
| 1907 | |
| 1908 | return len ? -1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | } |
| 1910 | |
| 1911 | #if defined(REAL_DMA) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1912 | /* |
| 1913 | * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | * unsigned char *phase, int *count, unsigned char **data) |
| 1915 | * |
| 1916 | * Purpose : transfers data in given phase using either real |
| 1917 | * or pseudo DMA. |
| 1918 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1919 | * Inputs : instance - instance of driver, *phase - pointer to |
| 1920 | * what phase is expected, *count - pointer to number of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | * bytes to transfer, **data - pointer to data pointer. |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1922 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | * Returns : -1 when different phase is entered without transferring |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 1924 | * maximum number of bytes, 0 if all bytes or transferred or exit |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | * is in same phase. |
| 1926 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1927 | * Also, *phase, *count, *data are modified in place. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | * |
| 1929 | */ |
| 1930 | |
| 1931 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1932 | static int NCR5380_transfer_dma(struct Scsi_Host *instance, |
| 1933 | unsigned char *phase, int *count, |
| 1934 | unsigned char **data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1936 | SETUP_HOSTDATA(instance); |
| 1937 | register int c = *count; |
| 1938 | register unsigned char p = *phase; |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 1939 | unsigned long flags; |
| 1940 | |
| 1941 | #if defined(CONFIG_SUN3) |
| 1942 | /* sanity check */ |
| 1943 | if (!sun3_dma_setup_done) { |
| 1944 | pr_err("scsi%d: transfer_dma without setup!\n", |
| 1945 | instance->host_no); |
| 1946 | BUG(); |
| 1947 | } |
| 1948 | hostdata->dma_len = c; |
| 1949 | |
| 1950 | dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n", |
| 1951 | instance->host_no, (p & SR_IO) ? "reading" : "writing", |
| 1952 | c, (p & SR_IO) ? "to" : "from", *data); |
| 1953 | |
| 1954 | /* netbsd turns off ints here, why not be safe and do it too */ |
| 1955 | local_irq_save(flags); |
| 1956 | |
| 1957 | /* send start chain */ |
| 1958 | sun3scsi_dma_start(c, *data); |
| 1959 | |
| 1960 | if (p & SR_IO) { |
| 1961 | NCR5380_write(TARGET_COMMAND_REG, 1); |
| 1962 | NCR5380_read(RESET_PARITY_INTERRUPT_REG); |
| 1963 | NCR5380_write(INITIATOR_COMMAND_REG, 0); |
| 1964 | NCR5380_write(MODE_REG, |
| 1965 | (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR)); |
| 1966 | NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0); |
| 1967 | } else { |
| 1968 | NCR5380_write(TARGET_COMMAND_REG, 0); |
| 1969 | NCR5380_read(RESET_PARITY_INTERRUPT_REG); |
| 1970 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_DATA); |
| 1971 | NCR5380_write(MODE_REG, |
| 1972 | (NCR5380_read(MODE_REG) | MR_DMA_MODE | MR_ENABLE_EOP_INTR)); |
| 1973 | NCR5380_write(START_DMA_SEND_REG, 0); |
| 1974 | } |
| 1975 | |
| 1976 | #ifdef SUN3_SCSI_VME |
| 1977 | dregs->csr |= CSR_DMA_ENABLE; |
| 1978 | #endif |
| 1979 | |
| 1980 | local_irq_restore(flags); |
| 1981 | |
| 1982 | sun3_dma_active = 1; |
| 1983 | |
| 1984 | #else /* !defined(CONFIG_SUN3) */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1985 | register unsigned char *d = *data; |
| 1986 | unsigned char tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1988 | if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) { |
| 1989 | *phase = tmp; |
| 1990 | return -1; |
| 1991 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | |
Finn Thain | ef1081c | 2014-11-12 16:12:14 +1100 | [diff] [blame] | 1993 | if (hostdata->read_overruns && (p & SR_IO)) |
| 1994 | c -= hostdata->read_overruns; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 1996 | dprintk(NDEBUG_DMA, "scsi%d: initializing DMA for %s, %d bytes %s %p\n", |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 1997 | HOSTNO, (p & SR_IO) ? "reading" : "writing", |
| 1998 | c, (p & SR_IO) ? "to" : "from", d); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1999 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2000 | NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2001 | |
| 2002 | #ifdef REAL_DMA |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2003 | NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | #endif /* def REAL_DMA */ |
| 2005 | |
Finn Thain | ef1081c | 2014-11-12 16:12:14 +1100 | [diff] [blame] | 2006 | if (!(hostdata->flags & FLAG_LATE_DMA_SETUP)) { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2007 | /* On the Medusa, it is a must to initialize the DMA before |
| 2008 | * starting the NCR. This is also the cleaner way for the TT. |
| 2009 | */ |
| 2010 | local_irq_save(flags); |
| 2011 | hostdata->dma_len = (p & SR_IO) ? |
| 2012 | NCR5380_dma_read_setup(instance, d, c) : |
| 2013 | NCR5380_dma_write_setup(instance, d, c); |
| 2014 | local_irq_restore(flags); |
| 2015 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2016 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2017 | if (p & SR_IO) |
| 2018 | NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0); |
| 2019 | else { |
| 2020 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA); |
| 2021 | NCR5380_write(START_DMA_SEND_REG, 0); |
| 2022 | } |
| 2023 | |
Finn Thain | ef1081c | 2014-11-12 16:12:14 +1100 | [diff] [blame] | 2024 | if (hostdata->flags & FLAG_LATE_DMA_SETUP) { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2025 | /* On the Falcon, the DMA setup must be done after the last */ |
| 2026 | /* NCR access, else the DMA setup gets trashed! |
| 2027 | */ |
| 2028 | local_irq_save(flags); |
| 2029 | hostdata->dma_len = (p & SR_IO) ? |
| 2030 | NCR5380_dma_read_setup(instance, d, c) : |
| 2031 | NCR5380_dma_write_setup(instance, d, c); |
| 2032 | local_irq_restore(flags); |
| 2033 | } |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 2034 | #endif /* !defined(CONFIG_SUN3) */ |
| 2035 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2036 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | } |
| 2038 | #endif /* defined(REAL_DMA) */ |
| 2039 | |
| 2040 | /* |
| 2041 | * Function : NCR5380_information_transfer (struct Scsi_Host *instance) |
| 2042 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2043 | * Purpose : run through the various SCSI phases and do as the target |
| 2044 | * directs us to. Operates on the currently connected command, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2045 | * instance->connected. |
| 2046 | * |
| 2047 | * Inputs : instance, instance for which we are doing commands |
| 2048 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2049 | * Side effects : SCSI things happen, the disconnected queue will be |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | * modified if a command disconnects, *instance->connected will |
| 2051 | * change. |
| 2052 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2053 | * XXX Note : we need to watch for bus free or a reset condition here |
| 2054 | * to recover from an unexpected bus free condition. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2056 | |
| 2057 | static void NCR5380_information_transfer(struct Scsi_Host *instance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2059 | SETUP_HOSTDATA(instance); |
| 2060 | unsigned long flags; |
| 2061 | unsigned char msgout = NOP; |
| 2062 | int sink = 0; |
| 2063 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | #if defined(REAL_DMA) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2065 | int transfersize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | #endif |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2067 | unsigned char *data; |
| 2068 | unsigned char phase, tmp, extended_msg[10], old_phase = 0xff; |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2069 | struct scsi_cmnd *cmd = (struct scsi_cmnd *) hostdata->connected; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 2071 | #ifdef SUN3_SCSI_VME |
| 2072 | dregs->csr |= CSR_INTR; |
| 2073 | #endif |
| 2074 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2075 | while (1) { |
| 2076 | tmp = NCR5380_read(STATUS_REG); |
| 2077 | /* We only have a valid SCSI phase when REQ is asserted */ |
| 2078 | if (tmp & SR_REQ) { |
| 2079 | phase = (tmp & PHASE_MASK); |
| 2080 | if (phase != old_phase) { |
| 2081 | old_phase = phase; |
Finn Thain | 8ad3a59 | 2014-03-18 11:42:19 +1100 | [diff] [blame] | 2082 | NCR5380_dprint_phase(NDEBUG_INFORMATION, instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | } |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 2084 | #if defined(CONFIG_SUN3) |
| 2085 | if (phase == PHASE_CMDOUT) { |
| 2086 | #if defined(REAL_DMA) |
| 2087 | void *d; |
| 2088 | unsigned long count; |
| 2089 | |
| 2090 | if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) { |
| 2091 | count = cmd->SCp.buffer->length; |
| 2092 | d = sg_virt(cmd->SCp.buffer); |
| 2093 | } else { |
| 2094 | count = cmd->SCp.this_residual; |
| 2095 | d = cmd->SCp.ptr; |
| 2096 | } |
| 2097 | /* this command setup for dma yet? */ |
| 2098 | if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != cmd)) { |
| 2099 | if (cmd->request->cmd_type == REQ_TYPE_FS) { |
| 2100 | sun3scsi_dma_setup(d, count, |
| 2101 | rq_data_dir(cmd->request)); |
| 2102 | sun3_dma_setup_done = cmd; |
| 2103 | } |
| 2104 | } |
| 2105 | #endif |
| 2106 | #ifdef SUN3_SCSI_VME |
| 2107 | dregs->csr |= CSR_INTR; |
| 2108 | #endif |
| 2109 | } |
| 2110 | #endif /* CONFIG_SUN3 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2112 | if (sink && (phase != PHASE_MSGOUT)) { |
| 2113 | NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2115 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | |
| 2116 | ICR_ASSERT_ACK); |
| 2117 | while (NCR5380_read(STATUS_REG) & SR_REQ) |
| 2118 | ; |
| 2119 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | |
| 2120 | ICR_ASSERT_ATN); |
| 2121 | sink = 0; |
| 2122 | continue; |
| 2123 | } |
| 2124 | |
| 2125 | switch (phase) { |
| 2126 | case PHASE_DATAOUT: |
| 2127 | #if (NDEBUG & NDEBUG_NO_DATAOUT) |
| 2128 | printk("scsi%d: NDEBUG_NO_DATAOUT set, attempted DATAOUT " |
| 2129 | "aborted\n", HOSTNO); |
| 2130 | sink = 1; |
| 2131 | do_abort(instance); |
| 2132 | cmd->result = DID_ERROR << 16; |
Matthew Wilcox | cce99c6 | 2007-09-25 12:42:01 -0400 | [diff] [blame] | 2133 | cmd->scsi_done(cmd); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2134 | return; |
| 2135 | #endif |
| 2136 | case PHASE_DATAIN: |
| 2137 | /* |
| 2138 | * If there is no room left in the current buffer in the |
| 2139 | * scatter-gather list, move onto the next one. |
| 2140 | */ |
| 2141 | |
| 2142 | if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) { |
| 2143 | ++cmd->SCp.buffer; |
| 2144 | --cmd->SCp.buffers_residual; |
| 2145 | cmd->SCp.this_residual = cmd->SCp.buffer->length; |
Jens Axboe | 45711f1 | 2007-10-22 21:19:53 +0200 | [diff] [blame] | 2146 | cmd->SCp.ptr = sg_virt(cmd->SCp.buffer); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2147 | /* ++roman: Try to merge some scatter-buffers if |
| 2148 | * they are at contiguous physical addresses. |
| 2149 | */ |
| 2150 | merge_contiguous_buffers(cmd); |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 2151 | dprintk(NDEBUG_INFORMATION, "scsi%d: %d bytes and %d buffers left\n", |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2152 | HOSTNO, cmd->SCp.this_residual, |
| 2153 | cmd->SCp.buffers_residual); |
| 2154 | } |
| 2155 | |
| 2156 | /* |
| 2157 | * The preferred transfer method is going to be |
| 2158 | * PSEUDO-DMA for systems that are strictly PIO, |
| 2159 | * since we can let the hardware do the handshaking. |
| 2160 | * |
| 2161 | * For this to work, we need to know the transfersize |
| 2162 | * ahead of time, since the pseudo-DMA code will sit |
| 2163 | * in an unconditional loop. |
| 2164 | */ |
| 2165 | |
| 2166 | /* ++roman: I suggest, this should be |
| 2167 | * #if def(REAL_DMA) |
| 2168 | * instead of leaving REAL_DMA out. |
| 2169 | */ |
| 2170 | |
| 2171 | #if defined(REAL_DMA) |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 2172 | if ( |
| 2173 | #if !defined(CONFIG_SUN3) |
| 2174 | !cmd->device->borken && |
| 2175 | #endif |
| 2176 | (transfersize = NCR5380_dma_xfer_len(instance, cmd, phase)) >= DMA_MIN_SIZE) { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2177 | len = transfersize; |
| 2178 | cmd->SCp.phase = phase; |
| 2179 | if (NCR5380_transfer_dma(instance, &phase, |
| 2180 | &len, (unsigned char **)&cmd->SCp.ptr)) { |
| 2181 | /* |
| 2182 | * If the watchdog timer fires, all future |
| 2183 | * accesses to this device will use the |
| 2184 | * polled-IO. */ |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 2185 | scmd_printk(KERN_INFO, cmd, |
| 2186 | "switching to slow handshake\n"); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2187 | cmd->device->borken = 1; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2188 | sink = 1; |
| 2189 | do_abort(instance); |
| 2190 | cmd->result = DID_ERROR << 16; |
Matthew Wilcox | cce99c6 | 2007-09-25 12:42:01 -0400 | [diff] [blame] | 2191 | cmd->scsi_done(cmd); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2192 | /* XXX - need to source or sink data here, as appropriate */ |
| 2193 | } else { |
| 2194 | #ifdef REAL_DMA |
| 2195 | /* ++roman: When using real DMA, |
| 2196 | * information_transfer() should return after |
| 2197 | * starting DMA since it has nothing more to |
| 2198 | * do. |
| 2199 | */ |
| 2200 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | #else |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2202 | cmd->SCp.this_residual -= transfersize - len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | #endif |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2204 | } |
| 2205 | } else |
| 2206 | #endif /* defined(REAL_DMA) */ |
| 2207 | NCR5380_transfer_pio(instance, &phase, |
| 2208 | (int *)&cmd->SCp.this_residual, |
| 2209 | (unsigned char **)&cmd->SCp.ptr); |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 2210 | #if defined(CONFIG_SUN3) && defined(REAL_DMA) |
| 2211 | /* if we had intended to dma that command clear it */ |
| 2212 | if (sun3_dma_setup_done == cmd) |
| 2213 | sun3_dma_setup_done = NULL; |
| 2214 | #endif |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2215 | break; |
| 2216 | case PHASE_MSGIN: |
| 2217 | len = 1; |
| 2218 | data = &tmp; |
| 2219 | NCR5380_write(SELECT_ENABLE_REG, 0); /* disable reselects */ |
| 2220 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
| 2221 | cmd->SCp.Message = tmp; |
| 2222 | |
| 2223 | switch (tmp) { |
| 2224 | /* |
| 2225 | * Linking lets us reduce the time required to get the |
| 2226 | * next command out to the device, hopefully this will |
| 2227 | * mean we don't waste another revolution due to the delays |
| 2228 | * required by ARBITRATION and another SELECTION. |
| 2229 | * |
| 2230 | * In the current implementation proposal, low level drivers |
| 2231 | * merely have to start the next command, pointed to by |
| 2232 | * next_link, done() is called as with unlinked commands. |
| 2233 | */ |
| 2234 | #ifdef LINKED |
| 2235 | case LINKED_CMD_COMPLETE: |
| 2236 | case LINKED_FLG_CMD_COMPLETE: |
| 2237 | /* Accept message by clearing ACK */ |
| 2238 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 2239 | |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 2240 | dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked command " |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2241 | "complete.\n", HOSTNO, cmd->device->id, cmd->device->lun); |
| 2242 | |
| 2243 | /* Enable reselect interrupts */ |
| 2244 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
| 2245 | /* |
| 2246 | * Sanity check : A linked command should only terminate |
| 2247 | * with one of these messages if there are more linked |
| 2248 | * commands available. |
| 2249 | */ |
| 2250 | |
| 2251 | if (!cmd->next_link) { |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 2252 | printk(KERN_NOTICE "scsi%d: target %d lun %llu " |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2253 | "linked command complete, no next_link\n", |
| 2254 | HOSTNO, cmd->device->id, cmd->device->lun); |
| 2255 | sink = 1; |
| 2256 | do_abort(instance); |
| 2257 | return; |
| 2258 | } |
| 2259 | |
| 2260 | initialize_SCp(cmd->next_link); |
| 2261 | /* The next command is still part of this process; copy it |
| 2262 | * and don't free it! */ |
| 2263 | cmd->next_link->tag = cmd->tag; |
| 2264 | cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8); |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 2265 | dprintk(NDEBUG_LINKED, "scsi%d: target %d lun %llu linked request " |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2266 | "done, calling scsi_done().\n", |
| 2267 | HOSTNO, cmd->device->id, cmd->device->lun); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2268 | cmd->scsi_done(cmd); |
| 2269 | cmd = hostdata->connected; |
| 2270 | break; |
| 2271 | #endif /* def LINKED */ |
| 2272 | case ABORT: |
| 2273 | case COMMAND_COMPLETE: |
| 2274 | /* Accept message by clearing ACK */ |
| 2275 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 2276 | dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d, lun %llu " |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2277 | "completed\n", HOSTNO, cmd->device->id, cmd->device->lun); |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 2278 | |
| 2279 | local_irq_save(flags); |
| 2280 | hostdata->retain_dma_intr++; |
| 2281 | hostdata->connected = NULL; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2282 | #ifdef SUPPORT_TAGS |
| 2283 | cmd_free_tag(cmd); |
| 2284 | if (status_byte(cmd->SCp.Status) == QUEUE_FULL) { |
| 2285 | /* Turn a QUEUE FULL status into BUSY, I think the |
| 2286 | * mid level cannot handle QUEUE FULL :-( (The |
| 2287 | * command is retried after BUSY). Also update our |
| 2288 | * queue size to the number of currently issued |
| 2289 | * commands now. |
| 2290 | */ |
| 2291 | /* ++Andreas: the mid level code knows about |
| 2292 | QUEUE_FULL now. */ |
Finn Thain | 61d739a | 2014-11-12 16:12:20 +1100 | [diff] [blame] | 2293 | struct tag_alloc *ta = &hostdata->TagAlloc[scmd_id(cmd)][cmd->device->lun]; |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 2294 | dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu returned " |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2295 | "QUEUE_FULL after %d commands\n", |
| 2296 | HOSTNO, cmd->device->id, cmd->device->lun, |
| 2297 | ta->nr_allocated); |
| 2298 | if (ta->queue_size > ta->nr_allocated) |
| 2299 | ta->nr_allocated = ta->queue_size; |
| 2300 | } |
| 2301 | #else |
| 2302 | hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun); |
| 2303 | #endif |
| 2304 | /* Enable reselect interrupts */ |
| 2305 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
| 2306 | |
| 2307 | /* |
| 2308 | * I'm not sure what the correct thing to do here is : |
| 2309 | * |
| 2310 | * If the command that just executed is NOT a request |
| 2311 | * sense, the obvious thing to do is to set the result |
| 2312 | * code to the values of the stored parameters. |
| 2313 | * |
| 2314 | * If it was a REQUEST SENSE command, we need some way to |
| 2315 | * differentiate between the failure code of the original |
| 2316 | * and the failure code of the REQUEST sense - the obvious |
| 2317 | * case is success, where we fall through and leave the |
| 2318 | * result code unchanged. |
| 2319 | * |
| 2320 | * The non-obvious place is where the REQUEST SENSE failed |
| 2321 | */ |
| 2322 | |
| 2323 | if (cmd->cmnd[0] != REQUEST_SENSE) |
| 2324 | cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8); |
| 2325 | else if (status_byte(cmd->SCp.Status) != GOOD) |
| 2326 | cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16); |
| 2327 | |
Boaz Harrosh | 28424d3 | 2007-09-10 22:37:45 +0300 | [diff] [blame] | 2328 | if ((cmd->cmnd[0] == REQUEST_SENSE) && |
| 2329 | hostdata->ses.cmd_len) { |
| 2330 | scsi_eh_restore_cmnd(cmd, &hostdata->ses); |
| 2331 | hostdata->ses.cmd_len = 0 ; |
| 2332 | } |
| 2333 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2334 | if ((cmd->cmnd[0] != REQUEST_SENSE) && |
| 2335 | (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) { |
Boaz Harrosh | 28424d3 | 2007-09-10 22:37:45 +0300 | [diff] [blame] | 2336 | scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2337 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 2338 | dprintk(NDEBUG_AUTOSENSE, "scsi%d: performing request sense\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2339 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2340 | LIST(cmd,hostdata->issue_queue); |
Roman Zippel | 3130d90 | 2007-05-01 22:32:37 +0200 | [diff] [blame] | 2341 | SET_NEXT(cmd, hostdata->issue_queue); |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2342 | hostdata->issue_queue = (struct scsi_cmnd *) cmd; |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 2343 | dprintk(NDEBUG_QUEUES, "scsi%d: REQUEST SENSE added to head of " |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2344 | "issue queue\n", H_NO(cmd)); |
Finn Thain | 997acab | 2014-11-12 16:11:54 +1100 | [diff] [blame] | 2345 | } else { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2346 | cmd->scsi_done(cmd); |
| 2347 | } |
| 2348 | |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 2349 | local_irq_restore(flags); |
| 2350 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2351 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
| 2352 | /* |
| 2353 | * Restore phase bits to 0 so an interrupted selection, |
| 2354 | * arbitration can resume. |
| 2355 | */ |
| 2356 | NCR5380_write(TARGET_COMMAND_REG, 0); |
| 2357 | |
| 2358 | while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected) |
| 2359 | barrier(); |
| 2360 | |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 2361 | local_irq_save(flags); |
Finn Thain | ef1081c | 2014-11-12 16:12:14 +1100 | [diff] [blame] | 2362 | hostdata->retain_dma_intr--; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2363 | /* ++roman: For Falcon SCSI, release the lock on the |
| 2364 | * ST-DMA here if no other commands are waiting on the |
| 2365 | * disconnected queue. |
| 2366 | */ |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 2367 | maybe_release_dma_irq(instance); |
| 2368 | local_irq_restore(flags); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2369 | return; |
| 2370 | case MESSAGE_REJECT: |
| 2371 | /* Accept message by clearing ACK */ |
| 2372 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 2373 | /* Enable reselect interrupts */ |
| 2374 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
| 2375 | switch (hostdata->last_message) { |
| 2376 | case HEAD_OF_QUEUE_TAG: |
| 2377 | case ORDERED_QUEUE_TAG: |
| 2378 | case SIMPLE_QUEUE_TAG: |
| 2379 | /* The target obviously doesn't support tagged |
| 2380 | * queuing, even though it announced this ability in |
| 2381 | * its INQUIRY data ?!? (maybe only this LUN?) Ok, |
| 2382 | * clear 'tagged_supported' and lock the LUN, since |
| 2383 | * the command is treated as untagged further on. |
| 2384 | */ |
| 2385 | cmd->device->tagged_supported = 0; |
| 2386 | hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun); |
| 2387 | cmd->tag = TAG_NONE; |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 2388 | dprintk(NDEBUG_TAGS, "scsi%d: target %d lun %llu rejected " |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2389 | "QUEUE_TAG message; tagged queuing " |
| 2390 | "disabled\n", |
| 2391 | HOSTNO, cmd->device->id, cmd->device->lun); |
| 2392 | break; |
| 2393 | } |
| 2394 | break; |
| 2395 | case DISCONNECT: |
| 2396 | /* Accept message by clearing ACK */ |
| 2397 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 2398 | local_irq_save(flags); |
| 2399 | cmd->device->disconnect = 1; |
| 2400 | LIST(cmd,hostdata->disconnected_queue); |
Roman Zippel | 3130d90 | 2007-05-01 22:32:37 +0200 | [diff] [blame] | 2401 | SET_NEXT(cmd, hostdata->disconnected_queue); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2402 | hostdata->connected = NULL; |
| 2403 | hostdata->disconnected_queue = cmd; |
| 2404 | local_irq_restore(flags); |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 2405 | dprintk(NDEBUG_QUEUES, "scsi%d: command for target %d lun %llu was " |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2406 | "moved from connected to the " |
| 2407 | "disconnected_queue\n", HOSTNO, |
| 2408 | cmd->device->id, cmd->device->lun); |
| 2409 | /* |
| 2410 | * Restore phase bits to 0 so an interrupted selection, |
| 2411 | * arbitration can resume. |
| 2412 | */ |
| 2413 | NCR5380_write(TARGET_COMMAND_REG, 0); |
| 2414 | |
| 2415 | /* Enable reselect interrupts */ |
| 2416 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
| 2417 | /* Wait for bus free to avoid nasty timeouts */ |
| 2418 | while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected) |
| 2419 | barrier(); |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 2420 | #ifdef SUN3_SCSI_VME |
| 2421 | dregs->csr |= CSR_DMA_ENABLE; |
| 2422 | #endif |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2423 | return; |
| 2424 | /* |
| 2425 | * The SCSI data pointer is *IMPLICITLY* saved on a disconnect |
| 2426 | * operation, in violation of the SCSI spec so we can safely |
| 2427 | * ignore SAVE/RESTORE pointers calls. |
| 2428 | * |
| 2429 | * Unfortunately, some disks violate the SCSI spec and |
| 2430 | * don't issue the required SAVE_POINTERS message before |
| 2431 | * disconnecting, and we have to break spec to remain |
| 2432 | * compatible. |
| 2433 | */ |
| 2434 | case SAVE_POINTERS: |
| 2435 | case RESTORE_POINTERS: |
| 2436 | /* Accept message by clearing ACK */ |
| 2437 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 2438 | /* Enable reselect interrupts */ |
| 2439 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
| 2440 | break; |
| 2441 | case EXTENDED_MESSAGE: |
| 2442 | /* |
| 2443 | * Extended messages are sent in the following format : |
| 2444 | * Byte |
| 2445 | * 0 EXTENDED_MESSAGE == 1 |
| 2446 | * 1 length (includes one byte for code, doesn't |
| 2447 | * include first two bytes) |
| 2448 | * 2 code |
| 2449 | * 3..length+1 arguments |
| 2450 | * |
| 2451 | * Start the extended message buffer with the EXTENDED_MESSAGE |
| 2452 | * byte, since spi_print_msg() wants the whole thing. |
| 2453 | */ |
| 2454 | extended_msg[0] = EXTENDED_MESSAGE; |
| 2455 | /* Accept first byte by clearing ACK */ |
| 2456 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 2457 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 2458 | dprintk(NDEBUG_EXTENDED, "scsi%d: receiving extended message\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2459 | |
| 2460 | len = 2; |
| 2461 | data = extended_msg + 1; |
| 2462 | phase = PHASE_MSGIN; |
| 2463 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 2464 | dprintk(NDEBUG_EXTENDED, "scsi%d: length=%d, code=0x%02x\n", HOSTNO, |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2465 | (int)extended_msg[1], (int)extended_msg[2]); |
| 2466 | |
| 2467 | if (!len && extended_msg[1] <= |
| 2468 | (sizeof(extended_msg) - 1)) { |
| 2469 | /* Accept third byte by clearing ACK */ |
| 2470 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 2471 | len = extended_msg[1] - 1; |
| 2472 | data = extended_msg + 3; |
| 2473 | phase = PHASE_MSGIN; |
| 2474 | |
| 2475 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 2476 | dprintk(NDEBUG_EXTENDED, "scsi%d: message received, residual %d\n", |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2477 | HOSTNO, len); |
| 2478 | |
| 2479 | switch (extended_msg[2]) { |
| 2480 | case EXTENDED_SDTR: |
| 2481 | case EXTENDED_WDTR: |
| 2482 | case EXTENDED_MODIFY_DATA_POINTER: |
| 2483 | case EXTENDED_EXTENDED_IDENTIFY: |
| 2484 | tmp = 0; |
| 2485 | } |
| 2486 | } else if (len) { |
| 2487 | printk(KERN_NOTICE "scsi%d: error receiving " |
| 2488 | "extended message\n", HOSTNO); |
| 2489 | tmp = 0; |
| 2490 | } else { |
| 2491 | printk(KERN_NOTICE "scsi%d: extended message " |
| 2492 | "code %02x length %d is too long\n", |
| 2493 | HOSTNO, extended_msg[2], extended_msg[1]); |
| 2494 | tmp = 0; |
| 2495 | } |
| 2496 | /* Fall through to reject message */ |
| 2497 | |
| 2498 | /* |
| 2499 | * If we get something weird that we aren't expecting, |
| 2500 | * reject it. |
| 2501 | */ |
| 2502 | default: |
| 2503 | if (!tmp) { |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 2504 | printk(KERN_INFO "scsi%d: rejecting message ", |
| 2505 | instance->host_no); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2506 | spi_print_msg(extended_msg); |
| 2507 | printk("\n"); |
| 2508 | } else if (tmp != EXTENDED_MESSAGE) |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 2509 | scmd_printk(KERN_INFO, cmd, |
| 2510 | "rejecting unknown message %02x\n", |
| 2511 | tmp); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2512 | else |
Finn Thain | ff50f9e | 2014-11-12 16:12:18 +1100 | [diff] [blame] | 2513 | scmd_printk(KERN_INFO, cmd, |
| 2514 | "rejecting unknown extended message code %02x, length %d\n", |
| 2515 | extended_msg[1], extended_msg[0]); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2516 | |
| 2517 | msgout = MESSAGE_REJECT; |
| 2518 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); |
| 2519 | break; |
| 2520 | } /* switch (tmp) */ |
| 2521 | break; |
| 2522 | case PHASE_MSGOUT: |
| 2523 | len = 1; |
| 2524 | data = &msgout; |
| 2525 | hostdata->last_message = msgout; |
| 2526 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
| 2527 | if (msgout == ABORT) { |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 2528 | local_irq_save(flags); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2529 | #ifdef SUPPORT_TAGS |
| 2530 | cmd_free_tag(cmd); |
| 2531 | #else |
| 2532 | hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun); |
| 2533 | #endif |
| 2534 | hostdata->connected = NULL; |
| 2535 | cmd->result = DID_ERROR << 16; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2536 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 2537 | maybe_release_dma_irq(instance); |
| 2538 | local_irq_restore(flags); |
| 2539 | cmd->scsi_done(cmd); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2540 | return; |
| 2541 | } |
| 2542 | msgout = NOP; |
| 2543 | break; |
| 2544 | case PHASE_CMDOUT: |
| 2545 | len = cmd->cmd_len; |
| 2546 | data = cmd->cmnd; |
| 2547 | /* |
| 2548 | * XXX for performance reasons, on machines with a |
| 2549 | * PSEUDO-DMA architecture we should probably |
| 2550 | * use the dma transfer function. |
| 2551 | */ |
| 2552 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
| 2553 | break; |
| 2554 | case PHASE_STATIN: |
| 2555 | len = 1; |
| 2556 | data = &tmp; |
| 2557 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
| 2558 | cmd->SCp.Status = tmp; |
| 2559 | break; |
| 2560 | default: |
| 2561 | printk("scsi%d: unknown phase\n", HOSTNO); |
Finn Thain | 8ad3a59 | 2014-03-18 11:42:19 +1100 | [diff] [blame] | 2562 | NCR5380_dprint(NDEBUG_ANY, instance); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2563 | } /* switch(phase) */ |
| 2564 | } /* if (tmp * SR_REQ) */ |
| 2565 | } /* while (1) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2566 | } |
| 2567 | |
| 2568 | /* |
| 2569 | * Function : void NCR5380_reselect (struct Scsi_Host *instance) |
| 2570 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2571 | * Purpose : does reselection, initializing the instance->connected |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2572 | * 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] | 2573 | * nexus has been reestablished, |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2574 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2575 | * Inputs : instance - this instance of the NCR5380. |
| 2576 | * |
| 2577 | */ |
| 2578 | |
| 2579 | |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 2580 | /* it might eventually prove necessary to do a dma setup on |
| 2581 | reselection, but it doesn't seem to be needed now -- sam */ |
| 2582 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2583 | static void NCR5380_reselect(struct Scsi_Host *instance) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2584 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2585 | SETUP_HOSTDATA(instance); |
| 2586 | unsigned char target_mask; |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 2587 | unsigned char lun; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | #ifdef SUPPORT_TAGS |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2589 | unsigned char tag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | #endif |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2591 | unsigned char msg[3]; |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 2592 | int __maybe_unused len; |
| 2593 | unsigned char __maybe_unused *data, __maybe_unused phase; |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2594 | struct scsi_cmnd *tmp = NULL, *prev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2596 | /* |
| 2597 | * Disable arbitration, etc. since the host adapter obviously |
| 2598 | * lost, and tell an interrupted NCR5380_select() to restart. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2599 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2600 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2601 | NCR5380_write(MODE_REG, MR_BASE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2603 | target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask); |
| 2604 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 2605 | dprintk(NDEBUG_RESELECTION, "scsi%d: reselect\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2606 | |
| 2607 | /* |
| 2608 | * At this point, we have detected that our SCSI ID is on the bus, |
| 2609 | * SEL is true and BSY was false for at least one bus settle delay |
| 2610 | * (400 ns). |
| 2611 | * |
| 2612 | * We must assert BSY ourselves, until the target drops the SEL |
| 2613 | * signal. |
| 2614 | */ |
| 2615 | |
| 2616 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY); |
| 2617 | |
| 2618 | while (NCR5380_read(STATUS_REG) & SR_SEL) |
| 2619 | ; |
| 2620 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 2621 | |
| 2622 | /* |
| 2623 | * Wait for target to go into MSGIN. |
| 2624 | */ |
| 2625 | |
| 2626 | while (!(NCR5380_read(STATUS_REG) & SR_REQ)) |
| 2627 | ; |
| 2628 | |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 2629 | #if defined(CONFIG_SUN3) && defined(REAL_DMA) |
| 2630 | /* acknowledge toggle to MSGIN */ |
| 2631 | NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(PHASE_MSGIN)); |
| 2632 | |
| 2633 | /* peek at the byte without really hitting the bus */ |
| 2634 | msg[0] = NCR5380_read(CURRENT_SCSI_DATA_REG); |
| 2635 | #else |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2636 | len = 1; |
| 2637 | data = msg; |
| 2638 | phase = PHASE_MSGIN; |
| 2639 | NCR5380_transfer_pio(instance, &phase, &len, &data); |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 2640 | #endif |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2641 | |
| 2642 | if (!(msg[0] & 0x80)) { |
| 2643 | printk(KERN_DEBUG "scsi%d: expecting IDENTIFY message, got ", HOSTNO); |
| 2644 | spi_print_msg(msg); |
| 2645 | do_abort(instance); |
| 2646 | return; |
| 2647 | } |
| 2648 | lun = (msg[0] & 0x07); |
| 2649 | |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 2650 | #if defined(SUPPORT_TAGS) && !defined(CONFIG_SUN3) |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2651 | /* If the phase is still MSGIN, the target wants to send some more |
| 2652 | * messages. In case it supports tagged queuing, this is probably a |
| 2653 | * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus. |
| 2654 | */ |
| 2655 | tag = TAG_NONE; |
Finn Thain | ca513fc | 2014-11-12 16:12:19 +1100 | [diff] [blame] | 2656 | if (phase == PHASE_MSGIN && (hostdata->flags & FLAG_TAGGED_QUEUING)) { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2657 | /* Accept previous IDENTIFY message by clearing ACK */ |
| 2658 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 2659 | len = 2; |
| 2660 | data = msg + 1; |
| 2661 | if (!NCR5380_transfer_pio(instance, &phase, &len, &data) && |
| 2662 | msg[1] == SIMPLE_QUEUE_TAG) |
| 2663 | tag = msg[2]; |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 2664 | dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at " |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2665 | "reselection\n", HOSTNO, target_mask, lun, tag); |
| 2666 | } |
| 2667 | #endif |
| 2668 | |
| 2669 | /* |
| 2670 | * Find the command corresponding to the I_T_L or I_T_L_Q nexus we |
| 2671 | * just reestablished, and remove it from the disconnected queue. |
| 2672 | */ |
| 2673 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2674 | for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue, prev = NULL; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2675 | tmp; prev = tmp, tmp = NEXT(tmp)) { |
| 2676 | if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun) |
| 2677 | #ifdef SUPPORT_TAGS |
| 2678 | && (tag == tmp->tag) |
| 2679 | #endif |
| 2680 | ) { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2681 | if (prev) { |
| 2682 | REMOVE(prev, NEXT(prev), tmp, NEXT(tmp)); |
Roman Zippel | 3130d90 | 2007-05-01 22:32:37 +0200 | [diff] [blame] | 2683 | SET_NEXT(prev, NEXT(tmp)); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2684 | } else { |
| 2685 | REMOVE(-1, hostdata->disconnected_queue, tmp, NEXT(tmp)); |
| 2686 | hostdata->disconnected_queue = NEXT(tmp); |
| 2687 | } |
Roman Zippel | 3130d90 | 2007-05-01 22:32:37 +0200 | [diff] [blame] | 2688 | SET_NEXT(tmp, NULL); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2689 | break; |
| 2690 | } |
| 2691 | } |
| 2692 | |
| 2693 | if (!tmp) { |
| 2694 | printk(KERN_WARNING "scsi%d: warning: target bitmask %02x lun %d " |
| 2695 | #ifdef SUPPORT_TAGS |
| 2696 | "tag %d " |
| 2697 | #endif |
| 2698 | "not in disconnected_queue.\n", |
| 2699 | HOSTNO, target_mask, lun |
| 2700 | #ifdef SUPPORT_TAGS |
| 2701 | , tag |
| 2702 | #endif |
| 2703 | ); |
| 2704 | /* |
| 2705 | * Since we have an established nexus that we can't do anything |
| 2706 | * with, we must abort it. |
| 2707 | */ |
| 2708 | do_abort(instance); |
| 2709 | return; |
| 2710 | } |
| 2711 | |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 2712 | #if defined(CONFIG_SUN3) && defined(REAL_DMA) |
| 2713 | /* engage dma setup for the command we just saw */ |
| 2714 | { |
| 2715 | void *d; |
| 2716 | unsigned long count; |
| 2717 | |
| 2718 | if (!tmp->SCp.this_residual && tmp->SCp.buffers_residual) { |
| 2719 | count = tmp->SCp.buffer->length; |
| 2720 | d = sg_virt(tmp->SCp.buffer); |
| 2721 | } else { |
| 2722 | count = tmp->SCp.this_residual; |
| 2723 | d = tmp->SCp.ptr; |
| 2724 | } |
| 2725 | /* setup this command for dma if not already */ |
| 2726 | if ((count >= DMA_MIN_SIZE) && (sun3_dma_setup_done != tmp)) { |
| 2727 | sun3scsi_dma_setup(d, count, rq_data_dir(tmp->request)); |
| 2728 | sun3_dma_setup_done = tmp; |
| 2729 | } |
| 2730 | } |
| 2731 | |
| 2732 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK); |
| 2733 | #endif |
| 2734 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2735 | /* Accept message by clearing ACK */ |
| 2736 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 2737 | |
Finn Thain | e3f463b | 2014-11-12 16:12:16 +1100 | [diff] [blame] | 2738 | #if defined(SUPPORT_TAGS) && defined(CONFIG_SUN3) |
| 2739 | /* If the phase is still MSGIN, the target wants to send some more |
| 2740 | * messages. In case it supports tagged queuing, this is probably a |
| 2741 | * SIMPLE_QUEUE_TAG for the I_T_L_Q nexus. |
| 2742 | */ |
| 2743 | tag = TAG_NONE; |
| 2744 | if (phase == PHASE_MSGIN && setup_use_tagged_queuing) { |
| 2745 | /* Accept previous IDENTIFY message by clearing ACK */ |
| 2746 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); |
| 2747 | len = 2; |
| 2748 | data = msg + 1; |
| 2749 | if (!NCR5380_transfer_pio(instance, &phase, &len, &data) && |
| 2750 | msg[1] == SIMPLE_QUEUE_TAG) |
| 2751 | tag = msg[2]; |
| 2752 | dprintk(NDEBUG_TAGS, "scsi%d: target mask %02x, lun %d sent tag %d at reselection\n" |
| 2753 | HOSTNO, target_mask, lun, tag); |
| 2754 | } |
| 2755 | #endif |
| 2756 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2757 | hostdata->connected = tmp; |
Hannes Reinecke | 9cb78c1 | 2014-06-25 15:27:36 +0200 | [diff] [blame] | 2758 | dprintk(NDEBUG_RESELECTION, "scsi%d: nexus established, target = %d, lun = %llu, tag = %d\n", |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2759 | HOSTNO, tmp->device->id, tmp->device->lun, tmp->tag); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | |
| 2763 | /* |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2764 | * Function : int NCR5380_abort (struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | * |
| 2766 | * Purpose : abort a command |
| 2767 | * |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2768 | * Inputs : cmd - the scsi_cmnd to abort, code - code to set the |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2769 | * host byte of the result field to, if zero DID_ABORTED is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2770 | * used. |
| 2771 | * |
Hannes Reinecke | b6c92b7 | 2014-10-30 09:44:36 +0100 | [diff] [blame] | 2772 | * Returns : SUCCESS - success, FAILED on failure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | * |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2774 | * XXX - there is no way to abort the command that is currently |
| 2775 | * connected, you have to wait for it to complete. If this is |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2776 | * a problem, we could implement longjmp() / setjmp(), setjmp() |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2777 | * called where the loop started in NCR5380_main(). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | */ |
| 2779 | |
| 2780 | static |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2781 | int NCR5380_abort(struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2782 | { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2783 | struct Scsi_Host *instance = cmd->device->host; |
| 2784 | SETUP_HOSTDATA(instance); |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2785 | struct scsi_cmnd *tmp, **prev; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2786 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2787 | |
Hannes Reinecke | 1fa6b5f | 2014-10-24 14:26:58 +0200 | [diff] [blame] | 2788 | scmd_printk(KERN_NOTICE, cmd, "aborting command\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2790 | NCR5380_print_status(instance); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2792 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 2794 | dprintk(NDEBUG_ABORT, "scsi%d: abort called basr 0x%02x, sr 0x%02x\n", HOSTNO, |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2795 | NCR5380_read(BUS_AND_STATUS_REG), |
| 2796 | NCR5380_read(STATUS_REG)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2797 | |
| 2798 | #if 1 |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2799 | /* |
| 2800 | * Case 1 : If the command is the currently executing command, |
| 2801 | * we'll set the aborted flag and return control so that |
| 2802 | * information transfer routine can exit cleanly. |
| 2803 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2805 | if (hostdata->connected == cmd) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 2807 | dprintk(NDEBUG_ABORT, "scsi%d: aborting connected command\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2808 | /* |
| 2809 | * We should perform BSY checking, and make sure we haven't slipped |
| 2810 | * into BUS FREE. |
| 2811 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2813 | /* NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); */ |
| 2814 | /* |
| 2815 | * Since we can't change phases until we've completed the current |
| 2816 | * handshake, we have to source or sink a byte of data if the current |
| 2817 | * phase is not MSGOUT. |
| 2818 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2819 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2820 | /* |
| 2821 | * Return control to the executing NCR drive so we can clear the |
| 2822 | * aborted flag and get back into our main loop. |
| 2823 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2825 | if (do_abort(instance) == 0) { |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2826 | hostdata->connected = NULL; |
| 2827 | cmd->result = DID_ABORT << 16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2828 | #ifdef SUPPORT_TAGS |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2829 | cmd_free_tag(cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | #else |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2831 | hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | #endif |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 2833 | maybe_release_dma_irq(instance); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2834 | local_irq_restore(flags); |
| 2835 | cmd->scsi_done(cmd); |
Michael Schmitz | 2b0f834 | 2014-05-02 20:43:01 +1200 | [diff] [blame] | 2836 | return SUCCESS; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2837 | } else { |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 2838 | local_irq_restore(flags); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2839 | printk("scsi%d: abort of connected command failed!\n", HOSTNO); |
Michael Schmitz | 2b0f834 | 2014-05-02 20:43:01 +1200 | [diff] [blame] | 2840 | return FAILED; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2841 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2843 | #endif |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2844 | |
| 2845 | /* |
| 2846 | * Case 2 : If the command hasn't been issued yet, we simply remove it |
| 2847 | * from the issue queue. |
| 2848 | */ |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2849 | for (prev = (struct scsi_cmnd **)&(hostdata->issue_queue), |
| 2850 | tmp = (struct scsi_cmnd *)hostdata->issue_queue; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2851 | tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) { |
| 2852 | if (cmd == tmp) { |
| 2853 | REMOVE(5, *prev, tmp, NEXT(tmp)); |
| 2854 | (*prev) = NEXT(tmp); |
Roman Zippel | 3130d90 | 2007-05-01 22:32:37 +0200 | [diff] [blame] | 2855 | SET_NEXT(tmp, NULL); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2856 | tmp->result = DID_ABORT << 16; |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 2857 | maybe_release_dma_irq(instance); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2858 | local_irq_restore(flags); |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 2859 | dprintk(NDEBUG_ABORT, "scsi%d: abort removed command from issue queue.\n", |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2860 | HOSTNO); |
| 2861 | /* Tagged queuing note: no tag to free here, hasn't been assigned |
| 2862 | * yet... */ |
| 2863 | tmp->scsi_done(tmp); |
Michael Schmitz | 2b0f834 | 2014-05-02 20:43:01 +1200 | [diff] [blame] | 2864 | return SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2865 | } |
| 2866 | } |
| 2867 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2868 | /* |
| 2869 | * Case 3 : If any commands are connected, we're going to fail the abort |
| 2870 | * and let the high level SCSI driver retry at a later time or |
| 2871 | * issue a reset. |
| 2872 | * |
| 2873 | * Timeouts, and therefore aborted commands, will be highly unlikely |
| 2874 | * and handling them cleanly in this situation would make the common |
| 2875 | * case of noresets less efficient, and would pollute our code. So, |
| 2876 | * we fail. |
| 2877 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2878 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2879 | if (hostdata->connected) { |
| 2880 | local_irq_restore(flags); |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 2881 | dprintk(NDEBUG_ABORT, "scsi%d: abort failed, command connected.\n", HOSTNO); |
Michael Schmitz | 2b0f834 | 2014-05-02 20:43:01 +1200 | [diff] [blame] | 2882 | return FAILED; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2883 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2884 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2885 | /* |
| 2886 | * Case 4: If the command is currently disconnected from the bus, and |
| 2887 | * there are no connected commands, we reconnect the I_T_L or |
| 2888 | * I_T_L_Q nexus associated with it, go into message out, and send |
| 2889 | * an abort message. |
| 2890 | * |
| 2891 | * This case is especially ugly. In order to reestablish the nexus, we |
| 2892 | * need to call NCR5380_select(). The easiest way to implement this |
| 2893 | * function was to abort if the bus was busy, and let the interrupt |
| 2894 | * handler triggered on the SEL for reselect take care of lost arbitrations |
| 2895 | * where necessary, meaning interrupts need to be enabled. |
| 2896 | * |
| 2897 | * When interrupts are enabled, the queues may change - so we |
| 2898 | * can't remove it from the disconnected queue before selecting it |
| 2899 | * because that could cause a failure in hashing the nexus if that |
| 2900 | * device reselected. |
| 2901 | * |
| 2902 | * Since the queues may change, we can't use the pointers from when we |
| 2903 | * first locate it. |
| 2904 | * |
| 2905 | * So, we must first locate the command, and if NCR5380_select() |
| 2906 | * succeeds, then issue the abort, relocate the command and remove |
| 2907 | * it from the disconnected queue. |
| 2908 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2909 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2910 | for (tmp = (struct scsi_cmnd *) hostdata->disconnected_queue; tmp; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2911 | tmp = NEXT(tmp)) { |
| 2912 | if (cmd == tmp) { |
| 2913 | local_irq_restore(flags); |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 2914 | dprintk(NDEBUG_ABORT, "scsi%d: aborting disconnected command.\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2915 | |
Finn Thain | 76f13b9 | 2014-11-12 16:11:53 +1100 | [diff] [blame] | 2916 | if (NCR5380_select(instance, cmd)) |
Michael Schmitz | 2b0f834 | 2014-05-02 20:43:01 +1200 | [diff] [blame] | 2917 | return FAILED; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2918 | |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 2919 | dprintk(NDEBUG_ABORT, "scsi%d: nexus reestablished.\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2920 | |
| 2921 | do_abort(instance); |
| 2922 | |
| 2923 | local_irq_save(flags); |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2924 | for (prev = (struct scsi_cmnd **)&(hostdata->disconnected_queue), |
| 2925 | tmp = (struct scsi_cmnd *)hostdata->disconnected_queue; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2926 | tmp; prev = NEXTADDR(tmp), tmp = NEXT(tmp)) { |
| 2927 | if (cmd == tmp) { |
| 2928 | REMOVE(5, *prev, tmp, NEXT(tmp)); |
| 2929 | *prev = NEXT(tmp); |
Roman Zippel | 3130d90 | 2007-05-01 22:32:37 +0200 | [diff] [blame] | 2930 | SET_NEXT(tmp, NULL); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2931 | tmp->result = DID_ABORT << 16; |
| 2932 | /* We must unlock the tag/LUN immediately here, since the |
| 2933 | * target goes to BUS FREE and doesn't send us another |
| 2934 | * message (COMMAND_COMPLETE or the like) |
| 2935 | */ |
| 2936 | #ifdef SUPPORT_TAGS |
| 2937 | cmd_free_tag(tmp); |
| 2938 | #else |
| 2939 | hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun); |
| 2940 | #endif |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 2941 | maybe_release_dma_irq(instance); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2942 | local_irq_restore(flags); |
| 2943 | tmp->scsi_done(tmp); |
Michael Schmitz | 2b0f834 | 2014-05-02 20:43:01 +1200 | [diff] [blame] | 2944 | return SUCCESS; |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2945 | } |
| 2946 | } |
| 2947 | } |
| 2948 | } |
| 2949 | |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 2950 | /* Maybe it is sufficient just to release the ST-DMA lock... (if |
| 2951 | * possible at all) At least, we should check if the lock could be |
| 2952 | * released after the abort, in case it is kept due to some bug. |
| 2953 | */ |
| 2954 | maybe_release_dma_irq(instance); |
| 2955 | local_irq_restore(flags); |
| 2956 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2957 | /* |
| 2958 | * Case 5 : If we reached this point, the command was not found in any of |
| 2959 | * the queues. |
| 2960 | * |
| 2961 | * We probably reached this point because of an unlikely race condition |
| 2962 | * between the command completing successfully and the abortion code, |
| 2963 | * so we won't panic, but we will notify the user in case something really |
| 2964 | * broke. |
| 2965 | */ |
| 2966 | |
Joe Perches | ad361c9 | 2009-07-06 13:05:40 -0700 | [diff] [blame] | 2967 | printk(KERN_INFO "scsi%d: warning : SCSI command probably completed successfully before abortion\n", HOSTNO); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2968 | |
Michael Schmitz | 2b0f834 | 2014-05-02 20:43:01 +1200 | [diff] [blame] | 2969 | return FAILED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | } |
| 2971 | |
| 2972 | |
Finn Thain | 3be1b3e | 2016-01-03 16:05:12 +1100 | [diff] [blame] | 2973 | /** |
| 2974 | * NCR5380_bus_reset - reset the SCSI bus |
| 2975 | * @cmd: SCSI command undergoing EH |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2976 | * |
Finn Thain | 3be1b3e | 2016-01-03 16:05:12 +1100 | [diff] [blame] | 2977 | * Returns SUCCESS |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2978 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2979 | |
Finn Thain | 710ddd0 | 2014-11-12 16:12:02 +1100 | [diff] [blame] | 2980 | static int NCR5380_bus_reset(struct scsi_cmnd *cmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2981 | { |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 2982 | struct Scsi_Host *instance = cmd->device->host; |
| 2983 | struct NCR5380_hostdata *hostdata = shost_priv(instance); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2984 | int i; |
| 2985 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2986 | |
Finn Thain | 3be1b3e | 2016-01-03 16:05:12 +1100 | [diff] [blame] | 2987 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | |
Finn Thain | 3be1b3e | 2016-01-03 16:05:12 +1100 | [diff] [blame] | 2989 | #if (NDEBUG & NDEBUG_ANY) |
| 2990 | scmd_printk(KERN_INFO, cmd, "performing bus reset\n"); |
| 2991 | NCR5380_print_status(instance); |
| 2992 | #endif |
| 2993 | |
| 2994 | do_reset(instance); |
| 2995 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2996 | /* reset NCR registers */ |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 2997 | NCR5380_write(MODE_REG, MR_BASE); |
| 2998 | NCR5380_write(TARGET_COMMAND_REG, 0); |
| 2999 | NCR5380_write(SELECT_ENABLE_REG, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3000 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 3001 | /* After the reset, there are no more connected or disconnected commands |
| 3002 | * and no busy units; so clear the low-level status here to avoid |
| 3003 | * conflicts when the mid-level code tries to wake up the affected |
| 3004 | * commands! |
| 3005 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 3007 | if (hostdata->issue_queue) |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 3008 | dprintk(NDEBUG_ABORT, "scsi%d: reset aborted issued command(s)\n", H_NO(cmd)); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 3009 | if (hostdata->connected) |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 3010 | dprintk(NDEBUG_ABORT, "scsi%d: reset aborted a connected command\n", H_NO(cmd)); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 3011 | if (hostdata->disconnected_queue) |
Finn Thain | d65e634 | 2014-03-18 11:42:20 +1100 | [diff] [blame] | 3012 | dprintk(NDEBUG_ABORT, "scsi%d: reset aborted disconnected command(s)\n", H_NO(cmd)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3013 | |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 3014 | hostdata->issue_queue = NULL; |
| 3015 | hostdata->connected = NULL; |
| 3016 | hostdata->disconnected_queue = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3017 | #ifdef SUPPORT_TAGS |
Finn Thain | ca513fc | 2014-11-12 16:12:19 +1100 | [diff] [blame] | 3018 | free_all_tags(hostdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3019 | #endif |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 3020 | for (i = 0; i < 8; ++i) |
| 3021 | hostdata->busy[i] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3022 | #ifdef REAL_DMA |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 3023 | hostdata->dma_len = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3024 | #endif |
Finn Thain | e3c3da6 | 2014-11-12 16:12:15 +1100 | [diff] [blame] | 3025 | |
| 3026 | maybe_release_dma_irq(instance); |
Roman Zippel | c28bda2 | 2007-05-01 22:32:36 +0200 | [diff] [blame] | 3027 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3028 | |
Michael Schmitz | 2b0f834 | 2014-05-02 20:43:01 +1200 | [diff] [blame] | 3029 | return SUCCESS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3030 | } |