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