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