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