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