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