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