]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * NCR 5380 generic driver routines. These should make it *trivial* | |
3 | * to implement 5380 SCSI drivers under Linux with a non-trantor | |
4 | * architecture. | |
5 | * | |
6 | * Note that these routines also work with NR53c400 family chips. | |
7 | * | |
8 | * Copyright 1993, Drew Eckhardt | |
9 | * Visionary Computing | |
10 | * (Unix and Linux consulting and custom programming) | |
11 | * drew@colorado.edu | |
12 | * +1 (303) 666-5836 | |
13 | * | |
14 | * DISTRIBUTION RELEASE 6. | |
15 | * | |
16 | * For more information, please consult | |
17 | * | |
18 | * NCR 5380 Family | |
19 | * SCSI Protocol Controller | |
20 | * Databook | |
21 | * | |
22 | * NCR Microelectronics | |
23 | * 1635 Aeroplaza Drive | |
24 | * Colorado Springs, CO 80916 | |
25 | * 1+ (719) 578-3400 | |
26 | * 1+ (800) 334-5454 | |
27 | */ | |
28 | ||
29 | /* | |
1da177e4 | 30 | * Revision 1.10 1998/9/2 Alan Cox |
fa195afe | 31 | * (alan@lxorguk.ukuu.org.uk) |
1da177e4 LT |
32 | * Fixed up the timer lockups reported so far. Things still suck. Looking |
33 | * forward to 2.3 and per device request queues. Then it'll be possible to | |
34 | * SMP thread this beast and improve life no end. | |
35 | ||
36 | * Revision 1.9 1997/7/27 Ronald van Cuijlenborg | |
37 | * (ronald.van.cuijlenborg@tip.nl or nutty@dds.nl) | |
38 | * (hopefully) fixed and enhanced USLEEP | |
39 | * added support for DTC3181E card (for Mustek scanner) | |
40 | * | |
41 | ||
42 | * Revision 1.8 Ingmar Baumgart | |
43 | * (ingmar@gonzo.schwaben.de) | |
44 | * added support for NCR53C400a card | |
45 | * | |
46 | ||
47 | * Revision 1.7 1996/3/2 Ray Van Tassle (rayvt@comm.mot.com) | |
48 | * added proc_info | |
49 | * added support needed for DTC 3180/3280 | |
50 | * fixed a couple of bugs | |
51 | * | |
52 | ||
53 | * Revision 1.5 1994/01/19 09:14:57 drew | |
54 | * Fixed udelay() hack that was being used on DATAOUT phases | |
55 | * instead of a proper wait for the final handshake. | |
56 | * | |
57 | * Revision 1.4 1994/01/19 06:44:25 drew | |
58 | * *** empty log message *** | |
59 | * | |
60 | * Revision 1.3 1994/01/19 05:24:40 drew | |
61 | * Added support for TCR LAST_BYTE_SENT bit. | |
62 | * | |
63 | * Revision 1.2 1994/01/15 06:14:11 drew | |
64 | * REAL DMA support, bug fixes. | |
65 | * | |
66 | * Revision 1.1 1994/01/15 06:00:54 drew | |
67 | * Initial revision | |
68 | * | |
69 | */ | |
70 | ||
71 | /* | |
72 | * Further development / testing that should be done : | |
73 | * 1. Cleanup the NCR5380_transfer_dma function and DMA operation complete | |
74 | * code so that everything does the same thing that's done at the | |
75 | * end of a pseudo-DMA read operation. | |
76 | * | |
77 | * 2. Fix REAL_DMA (interrupt driven, polled works fine) - | |
78 | * basically, transfer size needs to be reduced by one | |
79 | * and the last byte read as is done with PSEUDO_DMA. | |
80 | * | |
81 | * 4. Test SCSI-II tagged queueing (I have no devices which support | |
82 | * tagged queueing) | |
83 | * | |
84 | * 5. Test linked command handling code after Eric is ready with | |
85 | * the high level code. | |
86 | */ | |
db9dff36 | 87 | #include <scsi/scsi_dbg.h> |
1abfd370 | 88 | #include <scsi/scsi_transport_spi.h> |
1da177e4 | 89 | |
185a7a1c AV |
90 | #ifndef NDEBUG |
91 | #define NDEBUG 0 | |
92 | #endif | |
f566a576 | 93 | #ifndef NDEBUG_ABORT |
185a7a1c AV |
94 | #define NDEBUG_ABORT 0 |
95 | #endif | |
96 | ||
1da177e4 LT |
97 | #if (NDEBUG & NDEBUG_LISTS) |
98 | #define LIST(x,y) {printk("LINE:%d Adding %p to %p\n", __LINE__, (void*)(x), (void*)(y)); if ((x)==(y)) udelay(5); } | |
99 | #define REMOVE(w,x,y,z) {printk("LINE:%d Removing: %p->%p %p->%p \n", __LINE__, (void*)(w), (void*)(x), (void*)(y), (void*)(z)); if ((x)==(y)) udelay(5); } | |
100 | #else | |
101 | #define LIST(x,y) | |
102 | #define REMOVE(w,x,y,z) | |
103 | #endif | |
104 | ||
105 | #ifndef notyet | |
106 | #undef LINKED | |
107 | #undef REAL_DMA | |
108 | #endif | |
109 | ||
110 | #ifdef REAL_DMA_POLL | |
111 | #undef READ_OVERRUNS | |
112 | #define READ_OVERRUNS | |
113 | #endif | |
114 | ||
115 | #ifdef BOARD_REQUIRES_NO_DELAY | |
116 | #define io_recovery_delay(x) | |
117 | #else | |
118 | #define io_recovery_delay(x) udelay(x) | |
119 | #endif | |
120 | ||
121 | /* | |
122 | * Design | |
123 | * | |
124 | * This is a generic 5380 driver. To use it on a different platform, | |
125 | * one simply writes appropriate system specific macros (ie, data | |
126 | * transfer - some PC's will use the I/O bus, 68K's must use | |
127 | * memory mapped) and drops this file in their 'C' wrapper. | |
128 | * | |
129 | * (Note from hch: unfortunately it was not enough for the different | |
130 | * m68k folks and instead of improving this driver they copied it | |
131 | * and hacked it up for their needs. As a consequence they lost | |
132 | * most updates to this driver. Maybe someone will fix all these | |
133 | * drivers to use a common core one day..) | |
134 | * | |
135 | * As far as command queueing, two queues are maintained for | |
136 | * each 5380 in the system - commands that haven't been issued yet, | |
137 | * and commands that are currently executing. This means that an | |
138 | * unlimited number of commands may be queued, letting | |
139 | * more commands propagate from the higher driver levels giving higher | |
140 | * throughput. Note that both I_T_L and I_T_L_Q nexuses are supported, | |
141 | * allowing multiple commands to propagate all the way to a SCSI-II device | |
142 | * while a command is already executing. | |
143 | * | |
144 | * | |
145 | * Issues specific to the NCR5380 : | |
146 | * | |
147 | * When used in a PIO or pseudo-dma mode, the NCR5380 is a braindead | |
148 | * piece of hardware that requires you to sit in a loop polling for | |
149 | * the REQ signal as long as you are connected. Some devices are | |
150 | * brain dead (ie, many TEXEL CD ROM drives) and won't disconnect | |
151 | * while doing long seek operations. | |
152 | * | |
153 | * The workaround for this is to keep track of devices that have | |
154 | * disconnected. If the device hasn't disconnected, for commands that | |
155 | * should disconnect, we do something like | |
156 | * | |
157 | * while (!REQ is asserted) { sleep for N usecs; poll for M usecs } | |
158 | * | |
159 | * Some tweaking of N and M needs to be done. An algorithm based | |
160 | * on "time to data" would give the best results as long as short time | |
161 | * to datas (ie, on the same track) were considered, however these | |
162 | * broken devices are the exception rather than the rule and I'd rather | |
163 | * spend my time optimizing for the normal case. | |
164 | * | |
165 | * Architecture : | |
166 | * | |
167 | * At the heart of the design is a coroutine, NCR5380_main, | |
168 | * which is started from a workqueue for each NCR5380 host in the | |
169 | * system. It attempts to establish I_T_L or I_T_L_Q nexuses by | |
170 | * removing the commands from the issue queue and calling | |
171 | * NCR5380_select() if a nexus is not established. | |
172 | * | |
173 | * Once a nexus is established, the NCR5380_information_transfer() | |
174 | * phase goes through the various phases as instructed by the target. | |
175 | * if the target goes into MSG IN and sends a DISCONNECT message, | |
176 | * the command structure is placed into the per instance disconnected | |
177 | * queue, and NCR5380_main tries to find more work. If the target is | |
178 | * idle for too long, the system will try to sleep. | |
179 | * | |
180 | * If a command has disconnected, eventually an interrupt will trigger, | |
181 | * calling NCR5380_intr() which will in turn call NCR5380_reselect | |
182 | * to reestablish a nexus. This will run main if necessary. | |
183 | * | |
184 | * On command termination, the done function will be called as | |
185 | * appropriate. | |
186 | * | |
187 | * SCSI pointers are maintained in the SCp field of SCSI command | |
188 | * structures, being initialized after the command is connected | |
189 | * in NCR5380_select, and set as appropriate in NCR5380_information_transfer. | |
190 | * Note that in violation of the standard, an implicit SAVE POINTERS operation | |
191 | * is done, since some BROKEN disks fail to issue an explicit SAVE POINTERS. | |
192 | */ | |
193 | ||
194 | /* | |
195 | * Using this file : | |
196 | * This file a skeleton Linux SCSI driver for the NCR 5380 series | |
197 | * of chips. To use it, you write an architecture specific functions | |
198 | * and macros and include this file in your driver. | |
199 | * | |
200 | * These macros control options : | |
201 | * AUTOPROBE_IRQ - if defined, the NCR5380_probe_irq() function will be | |
202 | * defined. | |
203 | * | |
204 | * AUTOSENSE - if defined, REQUEST SENSE will be performed automatically | |
205 | * for commands that return with a CHECK CONDITION status. | |
206 | * | |
207 | * DIFFERENTIAL - if defined, NCR53c81 chips will use external differential | |
208 | * transceivers. | |
209 | * | |
210 | * DONT_USE_INTR - if defined, never use interrupts, even if we probe or | |
211 | * override-configure an IRQ. | |
212 | * | |
213 | * LIMIT_TRANSFERSIZE - if defined, limit the pseudo-dma transfers to 512 | |
214 | * bytes at a time. Since interrupts are disabled by default during | |
215 | * these transfers, we might need this to give reasonable interrupt | |
216 | * service time if the transfer size gets too large. | |
217 | * | |
218 | * LINKED - if defined, linked commands are supported. | |
219 | * | |
220 | * PSEUDO_DMA - if defined, PSEUDO DMA is used during the data transfer phases. | |
221 | * | |
222 | * REAL_DMA - if defined, REAL DMA is used during the data transfer phases. | |
223 | * | |
224 | * REAL_DMA_POLL - if defined, REAL DMA is used but the driver doesn't | |
225 | * rely on phase mismatch and EOP interrupts to determine end | |
226 | * of phase. | |
227 | * | |
228 | * UNSAFE - leave interrupts enabled during pseudo-DMA transfers. You | |
229 | * only really want to use this if you're having a problem with | |
230 | * dropped characters during high speed communications, and even | |
231 | * then, you're going to be better off twiddling with transfersize | |
232 | * in the high level code. | |
233 | * | |
234 | * Defaults for these will be provided although the user may want to adjust | |
235 | * these to allocate CPU resources to the SCSI driver or "real" code. | |
236 | * | |
237 | * USLEEP_SLEEP - amount of time, in jiffies, to sleep | |
238 | * | |
239 | * USLEEP_POLL - amount of time, in jiffies, to poll | |
240 | * | |
241 | * These macros MUST be defined : | |
242 | * NCR5380_local_declare() - declare any local variables needed for your | |
243 | * transfer routines. | |
244 | * | |
245 | * NCR5380_setup(instance) - initialize any local variables needed from a given | |
246 | * instance of the host adapter for NCR5380_{read,write,pread,pwrite} | |
247 | * | |
248 | * NCR5380_read(register) - read from the specified register | |
249 | * | |
250 | * NCR5380_write(register, value) - write to the specific register | |
251 | * | |
252 | * NCR5380_implementation_fields - additional fields needed for this | |
253 | * specific implementation of the NCR5380 | |
254 | * | |
255 | * Either real DMA *or* pseudo DMA may be implemented | |
256 | * REAL functions : | |
257 | * NCR5380_REAL_DMA should be defined if real DMA is to be used. | |
258 | * Note that the DMA setup functions should return the number of bytes | |
259 | * that they were able to program the controller for. | |
260 | * | |
261 | * Also note that generic i386/PC versions of these macros are | |
262 | * available as NCR5380_i386_dma_write_setup, | |
263 | * NCR5380_i386_dma_read_setup, and NCR5380_i386_dma_residual. | |
264 | * | |
265 | * NCR5380_dma_write_setup(instance, src, count) - initialize | |
266 | * NCR5380_dma_read_setup(instance, dst, count) - initialize | |
267 | * NCR5380_dma_residual(instance); - residual count | |
268 | * | |
269 | * PSEUDO functions : | |
270 | * NCR5380_pwrite(instance, src, count) | |
271 | * NCR5380_pread(instance, dst, count); | |
272 | * | |
273 | * The generic driver is initialized by calling NCR5380_init(instance), | |
274 | * after setting the appropriate host specific fields and ID. If the | |
275 | * driver wishes to autoprobe for an IRQ line, the NCR5380_probe_irq(instance, | |
276 | * possible) function may be used. | |
277 | */ | |
278 | ||
279 | static int do_abort(struct Scsi_Host *host); | |
280 | static void do_reset(struct Scsi_Host *host); | |
281 | ||
282 | /* | |
283 | * initialize_SCp - init the scsi pointer field | |
284 | * @cmd: command block to set up | |
285 | * | |
286 | * Set up the internal fields in the SCSI command. | |
287 | */ | |
288 | ||
289 | static __inline__ void initialize_SCp(Scsi_Cmnd * cmd) | |
290 | { | |
291 | /* | |
292 | * Initialize the Scsi Pointer field so that all of the commands in the | |
293 | * various queues are valid. | |
294 | */ | |
295 | ||
9e0fe44d BH |
296 | if (scsi_bufflen(cmd)) { |
297 | cmd->SCp.buffer = scsi_sglist(cmd); | |
298 | cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1; | |
45711f1a | 299 | cmd->SCp.ptr = sg_virt(cmd->SCp.buffer); |
1da177e4 LT |
300 | cmd->SCp.this_residual = cmd->SCp.buffer->length; |
301 | } else { | |
302 | cmd->SCp.buffer = NULL; | |
303 | cmd->SCp.buffers_residual = 0; | |
9e0fe44d BH |
304 | cmd->SCp.ptr = NULL; |
305 | cmd->SCp.this_residual = 0; | |
1da177e4 LT |
306 | } |
307 | } | |
308 | ||
309 | /** | |
310 | * NCR5380_poll_politely - wait for NCR5380 status bits | |
311 | * @instance: controller to poll | |
312 | * @reg: 5380 register to poll | |
313 | * @bit: Bitmask to check | |
314 | * @val: Value required to exit | |
315 | * | |
316 | * Polls the NCR5380 in a reasonably efficient manner waiting for | |
317 | * an event to occur, after a short quick poll we begin giving the | |
318 | * CPU back in non IRQ contexts | |
319 | * | |
320 | * Returns the value of the register or a negative error code. | |
321 | */ | |
322 | ||
323 | static int NCR5380_poll_politely(struct Scsi_Host *instance, int reg, int bit, int val, int t) | |
324 | { | |
325 | NCR5380_local_declare(); | |
326 | int n = 500; /* At about 8uS a cycle for the cpu access */ | |
327 | unsigned long end = jiffies + t; | |
328 | int r; | |
329 | ||
330 | NCR5380_setup(instance); | |
331 | ||
332 | while( n-- > 0) | |
333 | { | |
334 | r = NCR5380_read(reg); | |
335 | if((r & bit) == val) | |
336 | return 0; | |
337 | cpu_relax(); | |
338 | } | |
339 | ||
340 | /* t time yet ? */ | |
341 | while(time_before(jiffies, end)) | |
342 | { | |
343 | r = NCR5380_read(reg); | |
344 | if((r & bit) == val) | |
345 | return 0; | |
346 | if(!in_interrupt()) | |
730a646d | 347 | cond_resched(); |
1da177e4 LT |
348 | else |
349 | cpu_relax(); | |
350 | } | |
351 | return -ETIMEDOUT; | |
352 | } | |
353 | ||
354 | static struct { | |
355 | unsigned char value; | |
356 | const char *name; | |
702809ce | 357 | } phases[] __maybe_unused = { |
1da177e4 LT |
358 | {PHASE_DATAOUT, "DATAOUT"}, |
359 | {PHASE_DATAIN, "DATAIN"}, | |
360 | {PHASE_CMDOUT, "CMDOUT"}, | |
361 | {PHASE_STATIN, "STATIN"}, | |
362 | {PHASE_MSGOUT, "MSGOUT"}, | |
363 | {PHASE_MSGIN, "MSGIN"}, | |
364 | {PHASE_UNKNOWN, "UNKNOWN"} | |
365 | }; | |
366 | ||
185a7a1c | 367 | #if NDEBUG |
1da177e4 LT |
368 | static struct { |
369 | unsigned char mask; | |
370 | const char *name; | |
371 | } signals[] = { | |
372 | {SR_DBP, "PARITY"}, | |
373 | {SR_RST, "RST"}, | |
374 | {SR_BSY, "BSY"}, | |
375 | {SR_REQ, "REQ"}, | |
376 | {SR_MSG, "MSG"}, | |
377 | {SR_CD, "CD"}, | |
378 | {SR_IO, "IO"}, | |
379 | {SR_SEL, "SEL"}, | |
380 | {0, NULL} | |
381 | }, | |
382 | basrs[] = { | |
383 | {BASR_ATN, "ATN"}, | |
384 | {BASR_ACK, "ACK"}, | |
385 | {0, NULL} | |
386 | }, | |
387 | icrs[] = { | |
388 | {ICR_ASSERT_RST, "ASSERT RST"}, | |
389 | {ICR_ASSERT_ACK, "ASSERT ACK"}, | |
390 | {ICR_ASSERT_BSY, "ASSERT BSY"}, | |
391 | {ICR_ASSERT_SEL, "ASSERT SEL"}, | |
392 | {ICR_ASSERT_ATN, "ASSERT ATN"}, | |
393 | {ICR_ASSERT_DATA, "ASSERT DATA"}, | |
394 | {0, NULL} | |
395 | }, | |
396 | mrs[] = { | |
397 | {MR_BLOCK_DMA_MODE, "MODE BLOCK DMA"}, | |
398 | {MR_TARGET, "MODE TARGET"}, | |
399 | {MR_ENABLE_PAR_CHECK, "MODE PARITY CHECK"}, | |
400 | {MR_ENABLE_PAR_INTR, "MODE PARITY INTR"}, | |
401 | {MR_MONITOR_BSY, "MODE MONITOR BSY"}, | |
402 | {MR_DMA_MODE, "MODE DMA"}, | |
403 | {MR_ARBITRATE, "MODE ARBITRATION"}, | |
404 | {0, NULL} | |
405 | }; | |
406 | ||
407 | /** | |
408 | * NCR5380_print - print scsi bus signals | |
409 | * @instance: adapter state to dump | |
410 | * | |
411 | * Print the SCSI bus signals for debugging purposes | |
412 | * | |
413 | * Locks: caller holds hostdata lock (not essential) | |
414 | */ | |
415 | ||
416 | static void NCR5380_print(struct Scsi_Host *instance) | |
417 | { | |
418 | NCR5380_local_declare(); | |
419 | unsigned char status, data, basr, mr, icr, i; | |
420 | NCR5380_setup(instance); | |
421 | ||
422 | data = NCR5380_read(CURRENT_SCSI_DATA_REG); | |
423 | status = NCR5380_read(STATUS_REG); | |
424 | mr = NCR5380_read(MODE_REG); | |
425 | icr = NCR5380_read(INITIATOR_COMMAND_REG); | |
426 | basr = NCR5380_read(BUS_AND_STATUS_REG); | |
427 | ||
428 | printk("STATUS_REG: %02x ", status); | |
429 | for (i = 0; signals[i].mask; ++i) | |
430 | if (status & signals[i].mask) | |
431 | printk(",%s", signals[i].name); | |
432 | printk("\nBASR: %02x ", basr); | |
433 | for (i = 0; basrs[i].mask; ++i) | |
434 | if (basr & basrs[i].mask) | |
435 | printk(",%s", basrs[i].name); | |
436 | printk("\nICR: %02x ", icr); | |
437 | for (i = 0; icrs[i].mask; ++i) | |
438 | if (icr & icrs[i].mask) | |
439 | printk(",%s", icrs[i].name); | |
440 | printk("\nMODE: %02x ", mr); | |
441 | for (i = 0; mrs[i].mask; ++i) | |
442 | if (mr & mrs[i].mask) | |
443 | printk(",%s", mrs[i].name); | |
444 | printk("\n"); | |
445 | } | |
446 | ||
447 | ||
448 | /* | |
449 | * NCR5380_print_phase - show SCSI phase | |
450 | * @instance: adapter to dump | |
451 | * | |
452 | * Print the current SCSI phase for debugging purposes | |
453 | * | |
454 | * Locks: none | |
455 | */ | |
456 | ||
457 | static void NCR5380_print_phase(struct Scsi_Host *instance) | |
458 | { | |
459 | NCR5380_local_declare(); | |
460 | unsigned char status; | |
461 | int i; | |
462 | NCR5380_setup(instance); | |
463 | ||
464 | status = NCR5380_read(STATUS_REG); | |
465 | if (!(status & SR_REQ)) | |
466 | printk("scsi%d : REQ not asserted, phase unknown.\n", instance->host_no); | |
467 | else { | |
468 | for (i = 0; (phases[i].value != PHASE_UNKNOWN) && (phases[i].value != (status & PHASE_MASK)); ++i); | |
469 | printk("scsi%d : phase %s\n", instance->host_no, phases[i].name); | |
470 | } | |
471 | } | |
472 | #endif | |
473 | ||
474 | /* | |
475 | * These need tweaking, and would probably work best as per-device | |
476 | * flags initialized differently for disk, tape, cd, etc devices. | |
477 | * People with broken devices are free to experiment as to what gives | |
478 | * the best results for them. | |
479 | * | |
480 | * USLEEP_SLEEP should be a minimum seek time. | |
481 | * | |
482 | * USLEEP_POLL should be a maximum rotational latency. | |
483 | */ | |
484 | #ifndef USLEEP_SLEEP | |
485 | /* 20 ms (reasonable hard disk speed) */ | |
486 | #define USLEEP_SLEEP (20*HZ/1000) | |
487 | #endif | |
488 | /* 300 RPM (floppy speed) */ | |
489 | #ifndef USLEEP_POLL | |
490 | #define USLEEP_POLL (200*HZ/1000) | |
491 | #endif | |
492 | #ifndef USLEEP_WAITLONG | |
493 | /* RvC: (reasonable time to wait on select error) */ | |
494 | #define USLEEP_WAITLONG USLEEP_SLEEP | |
495 | #endif | |
496 | ||
497 | /* | |
498 | * Function : int should_disconnect (unsigned char cmd) | |
499 | * | |
d6e05edc | 500 | * Purpose : decide whether a command would normally disconnect or |
1da177e4 LT |
501 | * not, since if it won't disconnect we should go to sleep. |
502 | * | |
503 | * Input : cmd - opcode of SCSI command | |
504 | * | |
505 | * Returns : DISCONNECT_LONG if we should disconnect for a really long | |
506 | * time (ie always, sleep, look for REQ active, sleep), | |
507 | * DISCONNECT_TIME_TO_DATA if we would only disconnect for a normal | |
508 | * time-to-data delay, DISCONNECT_NONE if this command would return | |
509 | * immediately. | |
510 | * | |
511 | * Future sleep algorithms based on time to data can exploit | |
512 | * something like this so they can differentiate between "normal" | |
513 | * (ie, read, write, seek) and unusual commands (ie, * format). | |
514 | * | |
515 | * Note : We don't deal with commands that handle an immediate disconnect, | |
516 | * | |
517 | */ | |
518 | ||
519 | static int should_disconnect(unsigned char cmd) | |
520 | { | |
521 | switch (cmd) { | |
522 | case READ_6: | |
523 | case WRITE_6: | |
524 | case SEEK_6: | |
525 | case READ_10: | |
526 | case WRITE_10: | |
527 | case SEEK_10: | |
528 | return DISCONNECT_TIME_TO_DATA; | |
529 | case FORMAT_UNIT: | |
530 | case SEARCH_HIGH: | |
531 | case SEARCH_LOW: | |
532 | case SEARCH_EQUAL: | |
533 | return DISCONNECT_LONG; | |
534 | default: | |
535 | return DISCONNECT_NONE; | |
536 | } | |
537 | } | |
538 | ||
539 | static void NCR5380_set_timer(struct NCR5380_hostdata *hostdata, unsigned long timeout) | |
540 | { | |
541 | hostdata->time_expires = jiffies + timeout; | |
542 | schedule_delayed_work(&hostdata->coroutine, timeout); | |
543 | } | |
544 | ||
545 | ||
546 | static int probe_irq __initdata = 0; | |
547 | ||
548 | /** | |
549 | * probe_intr - helper for IRQ autoprobe | |
550 | * @irq: interrupt number | |
551 | * @dev_id: unused | |
552 | * @regs: unused | |
553 | * | |
554 | * Set a flag to indicate the IRQ in question was received. This is | |
555 | * used by the IRQ probe code. | |
556 | */ | |
557 | ||
7d12e780 | 558 | static irqreturn_t __init probe_intr(int irq, void *dev_id) |
1da177e4 LT |
559 | { |
560 | probe_irq = irq; | |
561 | return IRQ_HANDLED; | |
562 | } | |
563 | ||
564 | /** | |
565 | * NCR5380_probe_irq - find the IRQ of an NCR5380 | |
566 | * @instance: NCR5380 controller | |
567 | * @possible: bitmask of ISA IRQ lines | |
568 | * | |
569 | * Autoprobe for the IRQ line used by the NCR5380 by triggering an IRQ | |
570 | * and then looking to see what interrupt actually turned up. | |
571 | * | |
572 | * Locks: none, irqs must be enabled on entry | |
573 | */ | |
574 | ||
702809ce AM |
575 | static int __init __maybe_unused NCR5380_probe_irq(struct Scsi_Host *instance, |
576 | int possible) | |
1da177e4 LT |
577 | { |
578 | NCR5380_local_declare(); | |
579 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata; | |
580 | unsigned long timeout; | |
581 | int trying_irqs, i, mask; | |
582 | NCR5380_setup(instance); | |
583 | ||
584 | for (trying_irqs = i = 0, mask = 1; i < 16; ++i, mask <<= 1) | |
4909cc2b | 585 | if ((mask & possible) && (request_irq(i, &probe_intr, 0, "NCR-probe", NULL) == 0)) |
1da177e4 LT |
586 | trying_irqs |= mask; |
587 | ||
588 | timeout = jiffies + (250 * HZ / 1000); | |
589 | probe_irq = SCSI_IRQ_NONE; | |
590 | ||
591 | /* | |
592 | * A interrupt is triggered whenever BSY = false, SEL = true | |
593 | * and a bit set in the SELECT_ENABLE_REG is asserted on the | |
594 | * SCSI bus. | |
595 | * | |
596 | * Note that the bus is only driven when the phase control signals | |
597 | * (I/O, C/D, and MSG) match those in the TCR, so we must reset that | |
598 | * to zero. | |
599 | */ | |
600 | ||
601 | NCR5380_write(TARGET_COMMAND_REG, 0); | |
602 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); | |
603 | NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask); | |
604 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_SEL); | |
605 | ||
606 | while (probe_irq == SCSI_IRQ_NONE && time_before(jiffies, timeout)) | |
a9a3047d | 607 | schedule_timeout_uninterruptible(1); |
1da177e4 LT |
608 | |
609 | NCR5380_write(SELECT_ENABLE_REG, 0); | |
610 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
611 | ||
612 | for (i = 0, mask = 1; i < 16; ++i, mask <<= 1) | |
613 | if (trying_irqs & mask) | |
614 | free_irq(i, NULL); | |
615 | ||
616 | return probe_irq; | |
617 | } | |
618 | ||
619 | /** | |
620 | * NCR58380_print_options - show options | |
621 | * @instance: unused for now | |
622 | * | |
623 | * Called by probe code indicating the NCR5380 driver options that | |
624 | * were selected. At some point this will switch to runtime options | |
625 | * read from the adapter in question | |
626 | * | |
627 | * Locks: none | |
628 | */ | |
629 | ||
702809ce AM |
630 | static void __init __maybe_unused |
631 | NCR5380_print_options(struct Scsi_Host *instance) | |
1da177e4 LT |
632 | { |
633 | printk(" generic options" | |
634 | #ifdef AUTOPROBE_IRQ | |
635 | " AUTOPROBE_IRQ" | |
636 | #endif | |
637 | #ifdef AUTOSENSE | |
638 | " AUTOSENSE" | |
639 | #endif | |
640 | #ifdef DIFFERENTIAL | |
641 | " DIFFERENTIAL" | |
642 | #endif | |
643 | #ifdef REAL_DMA | |
644 | " REAL DMA" | |
645 | #endif | |
646 | #ifdef REAL_DMA_POLL | |
647 | " REAL DMA POLL" | |
648 | #endif | |
649 | #ifdef PARITY | |
650 | " PARITY" | |
651 | #endif | |
652 | #ifdef PSEUDO_DMA | |
653 | " PSEUDO DMA" | |
654 | #endif | |
655 | #ifdef UNSAFE | |
656 | " UNSAFE " | |
657 | #endif | |
658 | ); | |
659 | printk(" USLEEP, USLEEP_POLL=%d USLEEP_SLEEP=%d", USLEEP_POLL, USLEEP_SLEEP); | |
660 | printk(" generic release=%d", NCR5380_PUBLIC_RELEASE); | |
661 | if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400) { | |
662 | printk(" ncr53c400 release=%d", NCR53C400_PUBLIC_RELEASE); | |
663 | } | |
664 | } | |
665 | ||
666 | /** | |
667 | * NCR5380_print_status - dump controller info | |
668 | * @instance: controller to dump | |
669 | * | |
670 | * Print commands in the various queues, called from NCR5380_abort | |
671 | * and NCR5380_debug to aid debugging. | |
672 | * | |
673 | * Locks: called functions disable irqs | |
674 | */ | |
675 | ||
676 | static void NCR5380_print_status(struct Scsi_Host *instance) | |
677 | { | |
678 | NCR5380_dprint(NDEBUG_ANY, instance); | |
679 | NCR5380_dprint_phase(NDEBUG_ANY, instance); | |
680 | } | |
681 | ||
682 | /******************************************/ | |
683 | /* | |
684 | * /proc/scsi/[dtc pas16 t128 generic]/[0-ASC_NUM_BOARD_SUPPORTED] | |
685 | * | |
686 | * *buffer: I/O buffer | |
687 | * **start: if inout == FALSE pointer into buffer where user read should start | |
688 | * offset: current offset | |
689 | * length: length of buffer | |
690 | * hostno: Scsi_Host host_no | |
691 | * inout: TRUE - user is writing; FALSE - user is reading | |
692 | * | |
693 | * Return the number of bytes read from or written | |
694 | */ | |
695 | ||
dd7ab71b AV |
696 | static int __maybe_unused NCR5380_write_info(struct Scsi_Host *instance, |
697 | char *buffer, int length) | |
698 | { | |
699 | #ifdef DTC_PUBLIC_RELEASE | |
700 | dtc_wmaxi = dtc_maxi = 0; | |
701 | #endif | |
702 | #ifdef PAS16_PUBLIC_RELEASE | |
703 | pas_wmaxi = pas_maxi = 0; | |
704 | #endif | |
705 | return (-ENOSYS); /* Currently this is a no-op */ | |
706 | } | |
707 | ||
1da177e4 | 708 | #undef SPRINTF |
dd7ab71b | 709 | #define SPRINTF(args...) seq_printf(m, ## args) |
1da177e4 | 710 | static |
dd7ab71b | 711 | void lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, struct seq_file *m); |
1da177e4 | 712 | static |
dd7ab71b | 713 | void lprint_command(unsigned char *cmd, struct seq_file *m); |
1da177e4 | 714 | static |
dd7ab71b | 715 | void lprint_opcode(int opcode, struct seq_file *m); |
1da177e4 | 716 | |
dd7ab71b AV |
717 | static int __maybe_unused NCR5380_show_info(struct seq_file *m, |
718 | struct Scsi_Host *instance) | |
1da177e4 | 719 | { |
1da177e4 LT |
720 | struct NCR5380_hostdata *hostdata; |
721 | Scsi_Cmnd *ptr; | |
722 | ||
723 | hostdata = (struct NCR5380_hostdata *) instance->hostdata; | |
724 | ||
1da177e4 LT |
725 | SPRINTF("NCR5380 core release=%d. ", NCR5380_PUBLIC_RELEASE); |
726 | if (((struct NCR5380_hostdata *) instance->hostdata)->flags & FLAG_NCR53C400) | |
727 | SPRINTF("ncr53c400 release=%d. ", NCR53C400_PUBLIC_RELEASE); | |
728 | #ifdef DTC_PUBLIC_RELEASE | |
729 | SPRINTF("DTC 3180/3280 release %d", DTC_PUBLIC_RELEASE); | |
730 | #endif | |
731 | #ifdef T128_PUBLIC_RELEASE | |
732 | SPRINTF("T128 release %d", T128_PUBLIC_RELEASE); | |
733 | #endif | |
734 | #ifdef GENERIC_NCR5380_PUBLIC_RELEASE | |
735 | SPRINTF("Generic5380 release %d", GENERIC_NCR5380_PUBLIC_RELEASE); | |
736 | #endif | |
737 | #ifdef PAS16_PUBLIC_RELEASE | |
738 | SPRINTF("PAS16 release=%d", PAS16_PUBLIC_RELEASE); | |
739 | #endif | |
740 | ||
741 | SPRINTF("\nBase Addr: 0x%05lX ", (long) instance->base); | |
742 | SPRINTF("io_port: %04x ", (int) instance->io_port); | |
743 | if (instance->irq == SCSI_IRQ_NONE) | |
744 | SPRINTF("IRQ: None.\n"); | |
745 | else | |
746 | SPRINTF("IRQ: %d.\n", instance->irq); | |
747 | ||
748 | #ifdef DTC_PUBLIC_RELEASE | |
749 | SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n", dtc_wmaxi, dtc_maxi); | |
750 | #endif | |
751 | #ifdef PAS16_PUBLIC_RELEASE | |
752 | SPRINTF("Highwater I/O busy_spin_counts -- write: %d read: %d\n", pas_wmaxi, pas_maxi); | |
753 | #endif | |
754 | spin_lock_irq(instance->host_lock); | |
755 | if (!hostdata->connected) | |
756 | SPRINTF("scsi%d: no currently connected command\n", instance->host_no); | |
757 | else | |
dd7ab71b | 758 | lprint_Scsi_Cmnd((Scsi_Cmnd *) hostdata->connected, m); |
1da177e4 LT |
759 | SPRINTF("scsi%d: issue_queue\n", instance->host_no); |
760 | for (ptr = (Scsi_Cmnd *) hostdata->issue_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble) | |
dd7ab71b | 761 | lprint_Scsi_Cmnd(ptr, m); |
1da177e4 LT |
762 | |
763 | SPRINTF("scsi%d: disconnected_queue\n", instance->host_no); | |
764 | for (ptr = (Scsi_Cmnd *) hostdata->disconnected_queue; ptr; ptr = (Scsi_Cmnd *) ptr->host_scribble) | |
dd7ab71b | 765 | lprint_Scsi_Cmnd(ptr, m); |
1da177e4 | 766 | spin_unlock_irq(instance->host_lock); |
dd7ab71b | 767 | return 0; |
1da177e4 LT |
768 | } |
769 | ||
dd7ab71b | 770 | static void lprint_Scsi_Cmnd(Scsi_Cmnd * cmd, struct seq_file *m) |
1da177e4 LT |
771 | { |
772 | SPRINTF("scsi%d : destination target %d, lun %d\n", cmd->device->host->host_no, cmd->device->id, cmd->device->lun); | |
773 | SPRINTF(" command = "); | |
dd7ab71b | 774 | lprint_command(cmd->cmnd, m); |
1da177e4 LT |
775 | } |
776 | ||
dd7ab71b | 777 | static void lprint_command(unsigned char *command, struct seq_file *m) |
1da177e4 LT |
778 | { |
779 | int i, s; | |
dd7ab71b | 780 | lprint_opcode(command[0], m); |
1da177e4 LT |
781 | for (i = 1, s = COMMAND_SIZE(command[0]); i < s; ++i) |
782 | SPRINTF("%02x ", command[i]); | |
783 | SPRINTF("\n"); | |
1da177e4 LT |
784 | } |
785 | ||
dd7ab71b | 786 | static void lprint_opcode(int opcode, struct seq_file *m) |
1da177e4 LT |
787 | { |
788 | SPRINTF("%2d (0x%02x)", opcode, opcode); | |
1da177e4 LT |
789 | } |
790 | ||
791 | ||
792 | /** | |
793 | * NCR5380_init - initialise an NCR5380 | |
794 | * @instance: adapter to configure | |
795 | * @flags: control flags | |
796 | * | |
797 | * Initializes *instance and corresponding 5380 chip, | |
798 | * with flags OR'd into the initial flags value. | |
799 | * | |
800 | * Notes : I assume that the host, hostno, and id bits have been | |
801 | * set correctly. I don't care about the irq and other fields. | |
802 | * | |
803 | * Returns 0 for success | |
804 | * | |
805 | * Locks: interrupts must be enabled when we are called | |
806 | */ | |
807 | ||
6f039790 | 808 | static int NCR5380_init(struct Scsi_Host *instance, int flags) |
1da177e4 LT |
809 | { |
810 | NCR5380_local_declare(); | |
811 | int i, pass; | |
812 | unsigned long timeout; | |
813 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata; | |
814 | ||
815 | if(in_interrupt()) | |
816 | printk(KERN_ERR "NCR5380_init called with interrupts off!\n"); | |
817 | /* | |
818 | * On NCR53C400 boards, NCR5380 registers are mapped 8 past | |
819 | * the base address. | |
820 | */ | |
821 | ||
822 | #ifdef NCR53C400 | |
823 | if (flags & FLAG_NCR53C400) | |
824 | instance->NCR5380_instance_name += NCR53C400_address_adjust; | |
825 | #endif | |
826 | ||
827 | NCR5380_setup(instance); | |
828 | ||
829 | hostdata->aborted = 0; | |
830 | hostdata->id_mask = 1 << instance->this_id; | |
831 | for (i = hostdata->id_mask; i <= 0x80; i <<= 1) | |
832 | if (i > hostdata->id_mask) | |
833 | hostdata->id_higher_mask |= i; | |
834 | for (i = 0; i < 8; ++i) | |
835 | hostdata->busy[i] = 0; | |
836 | #ifdef REAL_DMA | |
837 | hostdata->dmalen = 0; | |
838 | #endif | |
839 | hostdata->targets_present = 0; | |
840 | hostdata->connected = NULL; | |
841 | hostdata->issue_queue = NULL; | |
842 | hostdata->disconnected_queue = NULL; | |
843 | ||
c4028958 | 844 | INIT_DELAYED_WORK(&hostdata->coroutine, NCR5380_main); |
1da177e4 LT |
845 | |
846 | #ifdef NCR5380_STATS | |
847 | for (i = 0; i < 8; ++i) { | |
848 | hostdata->time_read[i] = 0; | |
849 | hostdata->time_write[i] = 0; | |
850 | hostdata->bytes_read[i] = 0; | |
851 | hostdata->bytes_write[i] = 0; | |
852 | } | |
853 | hostdata->timebase = 0; | |
854 | hostdata->pendingw = 0; | |
855 | hostdata->pendingr = 0; | |
856 | #endif | |
857 | ||
858 | /* The CHECK code seems to break the 53C400. Will check it later maybe */ | |
859 | if (flags & FLAG_NCR53C400) | |
860 | hostdata->flags = FLAG_HAS_LAST_BYTE_SENT | flags; | |
861 | else | |
862 | hostdata->flags = FLAG_CHECK_LAST_BYTE_SENT | flags; | |
863 | ||
864 | hostdata->host = instance; | |
865 | hostdata->time_expires = 0; | |
866 | ||
867 | #ifndef AUTOSENSE | |
868 | if ((instance->cmd_per_lun > 1) || instance->can_queue > 1) | |
869 | printk(KERN_WARNING "scsi%d : WARNING : support for multiple outstanding commands enabled\n" " without AUTOSENSE option, contingent allegiance conditions may\n" | |
870 | " be incorrectly cleared.\n", instance->host_no); | |
871 | #endif /* def AUTOSENSE */ | |
872 | ||
873 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
874 | NCR5380_write(MODE_REG, MR_BASE); | |
875 | NCR5380_write(TARGET_COMMAND_REG, 0); | |
876 | NCR5380_write(SELECT_ENABLE_REG, 0); | |
877 | ||
878 | #ifdef NCR53C400 | |
879 | if (hostdata->flags & FLAG_NCR53C400) { | |
880 | NCR5380_write(C400_CONTROL_STATUS_REG, CSR_BASE); | |
881 | } | |
882 | #endif | |
883 | ||
884 | /* | |
885 | * Detect and correct bus wedge problems. | |
886 | * | |
887 | * If the system crashed, it may have crashed in a state | |
888 | * where a SCSI command was still executing, and the | |
889 | * SCSI bus is not in a BUS FREE STATE. | |
890 | * | |
891 | * If this is the case, we'll try to abort the currently | |
892 | * established nexus which we know nothing about, and that | |
893 | * failing, do a hard reset of the SCSI bus | |
894 | */ | |
895 | ||
896 | for (pass = 1; (NCR5380_read(STATUS_REG) & SR_BSY) && pass <= 6; ++pass) { | |
897 | switch (pass) { | |
898 | case 1: | |
899 | case 3: | |
900 | case 5: | |
901 | printk(KERN_INFO "scsi%d: SCSI bus busy, waiting up to five seconds\n", instance->host_no); | |
902 | timeout = jiffies + 5 * HZ; | |
903 | NCR5380_poll_politely(instance, STATUS_REG, SR_BSY, 0, 5*HZ); | |
904 | break; | |
905 | case 2: | |
906 | printk(KERN_WARNING "scsi%d: bus busy, attempting abort\n", instance->host_no); | |
907 | do_abort(instance); | |
908 | break; | |
909 | case 4: | |
910 | printk(KERN_WARNING "scsi%d: bus busy, attempting reset\n", instance->host_no); | |
911 | do_reset(instance); | |
912 | break; | |
913 | case 6: | |
914 | printk(KERN_ERR "scsi%d: bus locked solid or invalid override\n", instance->host_no); | |
915 | return -ENXIO; | |
916 | } | |
917 | } | |
918 | return 0; | |
919 | } | |
920 | ||
921 | /** | |
922 | * NCR5380_exit - remove an NCR5380 | |
923 | * @instance: adapter to remove | |
924 | */ | |
925 | ||
a43cf0f3 | 926 | static void NCR5380_exit(struct Scsi_Host *instance) |
1da177e4 LT |
927 | { |
928 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata; | |
929 | ||
a684b8da | 930 | cancel_delayed_work_sync(&hostdata->coroutine); |
1da177e4 LT |
931 | } |
932 | ||
933 | /** | |
934 | * NCR5380_queue_command - queue a command | |
935 | * @cmd: SCSI command | |
936 | * @done: completion handler | |
937 | * | |
938 | * cmd is added to the per instance issue_queue, with minor | |
939 | * twiddling done to the host specific fields of cmd. If the | |
940 | * main coroutine is not running, it is restarted. | |
941 | * | |
942 | * Locks: host lock taken by caller | |
943 | */ | |
944 | ||
f281233d | 945 | static int NCR5380_queue_command_lck(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *)) |
1da177e4 LT |
946 | { |
947 | struct Scsi_Host *instance = cmd->device->host; | |
948 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata; | |
949 | Scsi_Cmnd *tmp; | |
950 | ||
951 | #if (NDEBUG & NDEBUG_NO_WRITE) | |
952 | switch (cmd->cmnd[0]) { | |
953 | case WRITE_6: | |
954 | case WRITE_10: | |
955 | printk("scsi%d : WRITE attempted with NO_WRITE debugging flag set\n", instance->host_no); | |
956 | cmd->result = (DID_ERROR << 16); | |
957 | done(cmd); | |
958 | return 0; | |
959 | } | |
960 | #endif /* (NDEBUG & NDEBUG_NO_WRITE) */ | |
961 | ||
962 | #ifdef NCR5380_STATS | |
963 | switch (cmd->cmnd[0]) { | |
964 | case WRITE: | |
965 | case WRITE_6: | |
966 | case WRITE_10: | |
967 | hostdata->time_write[cmd->device->id] -= (jiffies - hostdata->timebase); | |
9e0fe44d | 968 | hostdata->bytes_write[cmd->device->id] += scsi_bufflen(cmd); |
1da177e4 LT |
969 | hostdata->pendingw++; |
970 | break; | |
971 | case READ: | |
972 | case READ_6: | |
973 | case READ_10: | |
974 | hostdata->time_read[cmd->device->id] -= (jiffies - hostdata->timebase); | |
9e0fe44d | 975 | hostdata->bytes_read[cmd->device->id] += scsi_bufflen(cmd); |
1da177e4 LT |
976 | hostdata->pendingr++; |
977 | break; | |
978 | } | |
979 | #endif | |
980 | ||
981 | /* | |
982 | * We use the host_scribble field as a pointer to the next command | |
983 | * in a queue | |
984 | */ | |
985 | ||
986 | cmd->host_scribble = NULL; | |
987 | cmd->scsi_done = done; | |
988 | cmd->result = 0; | |
989 | ||
990 | /* | |
991 | * Insert the cmd into the issue queue. Note that REQUEST SENSE | |
992 | * commands are added to the head of the queue since any command will | |
993 | * clear the contingent allegiance condition that exists and the | |
994 | * sense data is only guaranteed to be valid while the condition exists. | |
995 | */ | |
996 | ||
997 | if (!(hostdata->issue_queue) || (cmd->cmnd[0] == REQUEST_SENSE)) { | |
998 | LIST(cmd, hostdata->issue_queue); | |
999 | cmd->host_scribble = (unsigned char *) hostdata->issue_queue; | |
1000 | hostdata->issue_queue = cmd; | |
1001 | } else { | |
1002 | for (tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp->host_scribble; tmp = (Scsi_Cmnd *) tmp->host_scribble); | |
1003 | LIST(cmd, tmp); | |
1004 | tmp->host_scribble = (unsigned char *) cmd; | |
1005 | } | |
1006 | dprintk(NDEBUG_QUEUES, ("scsi%d : command added to %s of queue\n", instance->host_no, (cmd->cmnd[0] == REQUEST_SENSE) ? "head" : "tail")); | |
1007 | ||
1008 | /* Run the coroutine if it isn't already running. */ | |
1009 | /* Kick off command processing */ | |
c4028958 | 1010 | schedule_delayed_work(&hostdata->coroutine, 0); |
1da177e4 LT |
1011 | return 0; |
1012 | } | |
1013 | ||
f281233d | 1014 | static DEF_SCSI_QCMD(NCR5380_queue_command) |
1da177e4 LT |
1015 | |
1016 | /** | |
1017 | * NCR5380_main - NCR state machines | |
1018 | * | |
1019 | * NCR5380_main is a coroutine that runs as long as more work can | |
1020 | * be done on the NCR5380 host adapters in a system. Both | |
1021 | * NCR5380_queue_command() and NCR5380_intr() will try to start it | |
1022 | * in case it is not running. | |
1023 | * | |
1024 | * Locks: called as its own thread with no locks held. Takes the | |
1025 | * host lock and called routines may take the isa dma lock. | |
1026 | */ | |
1027 | ||
c4028958 | 1028 | static void NCR5380_main(struct work_struct *work) |
1da177e4 | 1029 | { |
c4028958 DH |
1030 | struct NCR5380_hostdata *hostdata = |
1031 | container_of(work, struct NCR5380_hostdata, coroutine.work); | |
1da177e4 LT |
1032 | struct Scsi_Host *instance = hostdata->host; |
1033 | Scsi_Cmnd *tmp, *prev; | |
1034 | int done; | |
1035 | ||
1036 | spin_lock_irq(instance->host_lock); | |
1037 | do { | |
1038 | /* Lock held here */ | |
1039 | done = 1; | |
1040 | if (!hostdata->connected && !hostdata->selecting) { | |
1041 | dprintk(NDEBUG_MAIN, ("scsi%d : not connected\n", instance->host_no)); | |
1042 | /* | |
1043 | * Search through the issue_queue for a command destined | |
1044 | * for a target that's not busy. | |
1045 | */ | |
1046 | for (tmp = (Scsi_Cmnd *) hostdata->issue_queue, prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble) | |
1047 | { | |
1048 | if (prev != tmp) | |
4dde8f7d | 1049 | dprintk(NDEBUG_LISTS, ("MAIN tmp=%p target=%d busy=%d lun=%d\n", tmp, tmp->device->id, hostdata->busy[tmp->device->id], tmp->device->lun)); |
1da177e4 LT |
1050 | /* When we find one, remove it from the issue queue. */ |
1051 | if (!(hostdata->busy[tmp->device->id] & (1 << tmp->device->lun))) { | |
1052 | if (prev) { | |
1053 | REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble); | |
1054 | prev->host_scribble = tmp->host_scribble; | |
1055 | } else { | |
1056 | REMOVE(-1, hostdata->issue_queue, tmp, tmp->host_scribble); | |
1057 | hostdata->issue_queue = (Scsi_Cmnd *) tmp->host_scribble; | |
1058 | } | |
1059 | tmp->host_scribble = NULL; | |
1060 | ||
1061 | /* | |
1062 | * Attempt to establish an I_T_L nexus here. | |
1063 | * On success, instance->hostdata->connected is set. | |
1064 | * On failure, we must add the command back to the | |
1065 | * issue queue so we can keep trying. | |
1066 | */ | |
4dde8f7d | 1067 | dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, ("scsi%d : main() : command for target %d lun %d removed from issue_queue\n", instance->host_no, tmp->device->id, tmp->device->lun)); |
1da177e4 LT |
1068 | |
1069 | /* | |
1070 | * A successful selection is defined as one that | |
1071 | * leaves us with the command connected and | |
1072 | * in hostdata->connected, OR has terminated the | |
1073 | * command. | |
1074 | * | |
1075 | * With successful commands, we fall through | |
1076 | * and see if we can do an information transfer, | |
1077 | * with failures we will restart. | |
1078 | */ | |
1079 | hostdata->selecting = NULL; | |
1080 | /* RvC: have to preset this to indicate a new command is being performed */ | |
1081 | ||
1082 | if (!NCR5380_select(instance, tmp, | |
1083 | /* | |
1084 | * REQUEST SENSE commands are issued without tagged | |
1085 | * queueing, even on SCSI-II devices because the | |
1086 | * contingent allegiance condition exists for the | |
1087 | * entire unit. | |
1088 | */ | |
1089 | (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : TAG_NEXT)) { | |
1090 | break; | |
1091 | } else { | |
1092 | LIST(tmp, hostdata->issue_queue); | |
1093 | tmp->host_scribble = (unsigned char *) hostdata->issue_queue; | |
1094 | hostdata->issue_queue = tmp; | |
1095 | done = 0; | |
1096 | dprintk(NDEBUG_MAIN|NDEBUG_QUEUES, ("scsi%d : main(): select() failed, returned to issue_queue\n", instance->host_no)); | |
1097 | } | |
1098 | /* lock held here still */ | |
1099 | } /* if target/lun is not busy */ | |
1100 | } /* for */ | |
1101 | /* exited locked */ | |
1102 | } /* if (!hostdata->connected) */ | |
1103 | if (hostdata->selecting) { | |
1104 | tmp = (Scsi_Cmnd *) hostdata->selecting; | |
1105 | /* Selection will drop and retake the lock */ | |
1106 | if (!NCR5380_select(instance, tmp, (tmp->cmnd[0] == REQUEST_SENSE) ? TAG_NONE : TAG_NEXT)) { | |
1107 | /* Ok ?? */ | |
1108 | } else { | |
1109 | /* RvC: device failed, so we wait a long time | |
1110 | this is needed for Mustek scanners, that | |
1111 | do not respond to commands immediately | |
1112 | after a scan */ | |
1113 | printk(KERN_DEBUG "scsi%d: device %d did not respond in time\n", instance->host_no, tmp->device->id); | |
1114 | LIST(tmp, hostdata->issue_queue); | |
1115 | tmp->host_scribble = (unsigned char *) hostdata->issue_queue; | |
1116 | hostdata->issue_queue = tmp; | |
1117 | NCR5380_set_timer(hostdata, USLEEP_WAITLONG); | |
1118 | } | |
1119 | } /* if hostdata->selecting */ | |
1120 | if (hostdata->connected | |
1121 | #ifdef REAL_DMA | |
1122 | && !hostdata->dmalen | |
1123 | #endif | |
1124 | && (!hostdata->time_expires || time_before_eq(hostdata->time_expires, jiffies)) | |
1125 | ) { | |
1126 | dprintk(NDEBUG_MAIN, ("scsi%d : main() : performing information transfer\n", instance->host_no)); | |
1127 | NCR5380_information_transfer(instance); | |
1128 | dprintk(NDEBUG_MAIN, ("scsi%d : main() : done set false\n", instance->host_no)); | |
1129 | done = 0; | |
1130 | } else | |
1131 | break; | |
1132 | } while (!done); | |
1133 | ||
1134 | spin_unlock_irq(instance->host_lock); | |
1135 | } | |
1136 | ||
1137 | #ifndef DONT_USE_INTR | |
1138 | ||
1139 | /** | |
1140 | * NCR5380_intr - generic NCR5380 irq handler | |
1141 | * @irq: interrupt number | |
1142 | * @dev_id: device info | |
1da177e4 LT |
1143 | * |
1144 | * Handle interrupts, reestablishing I_T_L or I_T_L_Q nexuses | |
1145 | * from the disconnected queue, and restarting NCR5380_main() | |
1146 | * as required. | |
1147 | * | |
1148 | * Locks: takes the needed instance locks | |
1149 | */ | |
1150 | ||
baa9aac6 | 1151 | static irqreturn_t NCR5380_intr(int dummy, void *dev_id) |
1da177e4 LT |
1152 | { |
1153 | NCR5380_local_declare(); | |
baa9aac6 | 1154 | struct Scsi_Host *instance = dev_id; |
1da177e4 LT |
1155 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata; |
1156 | int done; | |
1157 | unsigned char basr; | |
1158 | unsigned long flags; | |
1159 | ||
baa9aac6 JG |
1160 | dprintk(NDEBUG_INTR, ("scsi : NCR5380 irq %d triggered\n", |
1161 | instance->irq)); | |
1da177e4 LT |
1162 | |
1163 | do { | |
1164 | done = 1; | |
1165 | spin_lock_irqsave(instance->host_lock, flags); | |
1166 | /* Look for pending interrupts */ | |
1167 | NCR5380_setup(instance); | |
1168 | basr = NCR5380_read(BUS_AND_STATUS_REG); | |
1169 | /* XXX dispatch to appropriate routine if found and done=0 */ | |
1170 | if (basr & BASR_IRQ) { | |
1171 | NCR5380_dprint(NDEBUG_INTR, instance); | |
1172 | if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) { | |
1173 | done = 0; | |
1174 | dprintk(NDEBUG_INTR, ("scsi%d : SEL interrupt\n", instance->host_no)); | |
1175 | NCR5380_reselect(instance); | |
1176 | (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG); | |
1177 | } else if (basr & BASR_PARITY_ERROR) { | |
1178 | dprintk(NDEBUG_INTR, ("scsi%d : PARITY interrupt\n", instance->host_no)); | |
1179 | (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG); | |
1180 | } else if ((NCR5380_read(STATUS_REG) & SR_RST) == SR_RST) { | |
1181 | dprintk(NDEBUG_INTR, ("scsi%d : RESET interrupt\n", instance->host_no)); | |
1182 | (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG); | |
1183 | } else { | |
1184 | #if defined(REAL_DMA) | |
1185 | /* | |
1186 | * We should only get PHASE MISMATCH and EOP interrupts | |
1187 | * if we have DMA enabled, so do a sanity check based on | |
1188 | * the current setting of the MODE register. | |
1189 | */ | |
1190 | ||
1191 | if ((NCR5380_read(MODE_REG) & MR_DMA) && ((basr & BASR_END_DMA_TRANSFER) || !(basr & BASR_PHASE_MATCH))) { | |
25985edc | 1192 | int transferred; |
1da177e4 LT |
1193 | |
1194 | if (!hostdata->connected) | |
1195 | panic("scsi%d : received end of DMA interrupt with no connected cmd\n", instance->hostno); | |
1196 | ||
25985edc | 1197 | transferred = (hostdata->dmalen - NCR5380_dma_residual(instance)); |
1da177e4 LT |
1198 | hostdata->connected->SCp.this_residual -= transferred; |
1199 | hostdata->connected->SCp.ptr += transferred; | |
1200 | hostdata->dmalen = 0; | |
1201 | ||
1202 | (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG); | |
1203 | ||
1204 | /* FIXME: we need to poll briefly then defer a workqueue task ! */ | |
1205 | NCR5380_poll_politely(hostdata, BUS_AND_STATUS_REG, BASR_ACK, 0, 2*HZ); | |
1206 | ||
1207 | NCR5380_write(MODE_REG, MR_BASE); | |
1208 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
1209 | } | |
1210 | #else | |
1211 | dprintk(NDEBUG_INTR, ("scsi : unknown interrupt, BASR 0x%X, MR 0x%X, SR 0x%x\n", basr, NCR5380_read(MODE_REG), NCR5380_read(STATUS_REG))); | |
1212 | (void) NCR5380_read(RESET_PARITY_INTERRUPT_REG); | |
1213 | #endif | |
1214 | } | |
1215 | } /* if BASR_IRQ */ | |
1216 | spin_unlock_irqrestore(instance->host_lock, flags); | |
1217 | if(!done) | |
c4028958 | 1218 | schedule_delayed_work(&hostdata->coroutine, 0); |
1da177e4 LT |
1219 | } while (!done); |
1220 | return IRQ_HANDLED; | |
1221 | } | |
1222 | ||
1223 | #endif | |
1224 | ||
1225 | /** | |
1226 | * collect_stats - collect stats on a scsi command | |
1227 | * @hostdata: adapter | |
1228 | * @cmd: command being issued | |
1229 | * | |
1230 | * Update the statistical data by parsing the command in question | |
1231 | */ | |
1232 | ||
1233 | static void collect_stats(struct NCR5380_hostdata *hostdata, Scsi_Cmnd * cmd) | |
1234 | { | |
1235 | #ifdef NCR5380_STATS | |
1236 | switch (cmd->cmnd[0]) { | |
1237 | case WRITE: | |
1238 | case WRITE_6: | |
1239 | case WRITE_10: | |
422c0d61 | 1240 | hostdata->time_write[scmd_id(cmd)] += (jiffies - hostdata->timebase); |
1da177e4 LT |
1241 | hostdata->pendingw--; |
1242 | break; | |
1243 | case READ: | |
1244 | case READ_6: | |
1245 | case READ_10: | |
422c0d61 | 1246 | hostdata->time_read[scmd_id(cmd)] += (jiffies - hostdata->timebase); |
1da177e4 LT |
1247 | hostdata->pendingr--; |
1248 | break; | |
1249 | } | |
1250 | #endif | |
1251 | } | |
1252 | ||
1253 | ||
1254 | /* | |
1255 | * Function : int NCR5380_select (struct Scsi_Host *instance, Scsi_Cmnd *cmd, | |
1256 | * int tag); | |
1257 | * | |
1258 | * Purpose : establishes I_T_L or I_T_L_Q nexus for new or existing command, | |
1259 | * including ARBITRATION, SELECTION, and initial message out for | |
1260 | * IDENTIFY and queue messages. | |
1261 | * | |
1262 | * Inputs : instance - instantiation of the 5380 driver on which this | |
1263 | * target lives, cmd - SCSI command to execute, tag - set to TAG_NEXT for | |
1264 | * new tag, TAG_NONE for untagged queueing, otherwise set to the tag for | |
1265 | * the command that is presently connected. | |
1266 | * | |
1267 | * Returns : -1 if selection could not execute for some reason, | |
1268 | * 0 if selection succeeded or failed because the target | |
1269 | * did not respond. | |
1270 | * | |
1271 | * Side effects : | |
1272 | * If bus busy, arbitration failed, etc, NCR5380_select() will exit | |
1273 | * with registers as they should have been on entry - ie | |
1274 | * SELECT_ENABLE will be set appropriately, the NCR5380 | |
1275 | * will cease to drive any SCSI bus signals. | |
1276 | * | |
1277 | * If successful : I_T_L or I_T_L_Q nexus will be established, | |
1278 | * instance->connected will be set to cmd. | |
1279 | * SELECT interrupt will be disabled. | |
1280 | * | |
1281 | * If failed (no target) : cmd->scsi_done() will be called, and the | |
1282 | * cmd->result host byte set to DID_BAD_TARGET. | |
1283 | * | |
1284 | * Locks: caller holds hostdata lock in IRQ mode | |
1285 | */ | |
1286 | ||
1287 | static int NCR5380_select(struct Scsi_Host *instance, Scsi_Cmnd * cmd, int tag) | |
1288 | { | |
1289 | NCR5380_local_declare(); | |
1290 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata; | |
1291 | unsigned char tmp[3], phase; | |
1292 | unsigned char *data; | |
1293 | int len; | |
1294 | unsigned long timeout; | |
1295 | unsigned char value; | |
1296 | int err; | |
1297 | NCR5380_setup(instance); | |
1298 | ||
1299 | if (hostdata->selecting) | |
1300 | goto part2; | |
1301 | ||
1302 | hostdata->restart_select = 0; | |
1303 | ||
1304 | NCR5380_dprint(NDEBUG_ARBITRATION, instance); | |
1305 | dprintk(NDEBUG_ARBITRATION, ("scsi%d : starting arbitration, id = %d\n", instance->host_no, instance->this_id)); | |
1306 | ||
1307 | /* | |
1308 | * Set the phase bits to 0, otherwise the NCR5380 won't drive the | |
1309 | * data bus during SELECTION. | |
1310 | */ | |
1311 | ||
1312 | NCR5380_write(TARGET_COMMAND_REG, 0); | |
1313 | ||
1314 | /* | |
1315 | * Start arbitration. | |
1316 | */ | |
1317 | ||
1318 | NCR5380_write(OUTPUT_DATA_REG, hostdata->id_mask); | |
1319 | NCR5380_write(MODE_REG, MR_ARBITRATE); | |
1320 | ||
1321 | ||
1322 | /* We can be relaxed here, interrupts are on, we are | |
1323 | in workqueue context, the birds are singing in the trees */ | |
1324 | spin_unlock_irq(instance->host_lock); | |
1325 | err = NCR5380_poll_politely(instance, INITIATOR_COMMAND_REG, ICR_ARBITRATION_PROGRESS, ICR_ARBITRATION_PROGRESS, 5*HZ); | |
1326 | spin_lock_irq(instance->host_lock); | |
1327 | if (err < 0) { | |
1328 | printk(KERN_DEBUG "scsi: arbitration timeout at %d\n", __LINE__); | |
1329 | NCR5380_write(MODE_REG, MR_BASE); | |
1330 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); | |
1331 | goto failed; | |
1332 | } | |
1333 | ||
1334 | dprintk(NDEBUG_ARBITRATION, ("scsi%d : arbitration complete\n", instance->host_no)); | |
1335 | ||
1336 | /* | |
1337 | * The arbitration delay is 2.2us, but this is a minimum and there is | |
1338 | * no maximum so we can safely sleep for ceil(2.2) usecs to accommodate | |
1339 | * the integral nature of udelay(). | |
1340 | * | |
1341 | */ | |
1342 | ||
1343 | udelay(3); | |
1344 | ||
1345 | /* Check for lost arbitration */ | |
1346 | if ((NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST) || (NCR5380_read(CURRENT_SCSI_DATA_REG) & hostdata->id_higher_mask) || (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) { | |
1347 | NCR5380_write(MODE_REG, MR_BASE); | |
1348 | dprintk(NDEBUG_ARBITRATION, ("scsi%d : lost arbitration, deasserting MR_ARBITRATE\n", instance->host_no)); | |
1349 | goto failed; | |
1350 | } | |
1351 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_SEL); | |
1352 | ||
1353 | if (!(hostdata->flags & FLAG_DTC3181E) && | |
1354 | /* RvC: DTC3181E has some trouble with this | |
1355 | * so we simply removed it. Seems to work with | |
1356 | * only Mustek scanner attached | |
1357 | */ | |
1358 | (NCR5380_read(INITIATOR_COMMAND_REG) & ICR_ARBITRATION_LOST)) { | |
1359 | NCR5380_write(MODE_REG, MR_BASE); | |
1360 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
1361 | dprintk(NDEBUG_ARBITRATION, ("scsi%d : lost arbitration, deasserting ICR_ASSERT_SEL\n", instance->host_no)); | |
1362 | goto failed; | |
1363 | } | |
1364 | /* | |
1365 | * Again, bus clear + bus settle time is 1.2us, however, this is | |
1366 | * a minimum so we'll udelay ceil(1.2) | |
1367 | */ | |
1368 | ||
1369 | udelay(2); | |
1370 | ||
1371 | dprintk(NDEBUG_ARBITRATION, ("scsi%d : won arbitration\n", instance->host_no)); | |
1372 | ||
1373 | /* | |
1374 | * Now that we have won arbitration, start Selection process, asserting | |
1375 | * the host and target ID's on the SCSI bus. | |
1376 | */ | |
1377 | ||
422c0d61 | 1378 | NCR5380_write(OUTPUT_DATA_REG, (hostdata->id_mask | (1 << scmd_id(cmd)))); |
1da177e4 LT |
1379 | |
1380 | /* | |
1381 | * Raise ATN while SEL is true before BSY goes false from arbitration, | |
1382 | * since this is the only way to guarantee that we'll get a MESSAGE OUT | |
1383 | * phase immediately after selection. | |
1384 | */ | |
1385 | ||
1386 | NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_BSY | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL)); | |
1387 | NCR5380_write(MODE_REG, MR_BASE); | |
1388 | ||
1389 | /* | |
1390 | * Reselect interrupts must be turned off prior to the dropping of BSY, | |
1391 | * otherwise we will trigger an interrupt. | |
1392 | */ | |
1393 | NCR5380_write(SELECT_ENABLE_REG, 0); | |
1394 | ||
1395 | /* | |
1396 | * The initiator shall then wait at least two deskew delays and release | |
1397 | * the BSY signal. | |
1398 | */ | |
1399 | udelay(1); /* wingel -- wait two bus deskew delay >2*45ns */ | |
1400 | ||
1401 | /* Reset BSY */ | |
1402 | NCR5380_write(INITIATOR_COMMAND_REG, (ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_SEL)); | |
1403 | ||
1404 | /* | |
1405 | * Something weird happens when we cease to drive BSY - looks | |
1406 | * like the board/chip is letting us do another read before the | |
1407 | * appropriate propagation delay has expired, and we're confusing | |
1408 | * a BSY signal from ourselves as the target's response to SELECTION. | |
1409 | * | |
1410 | * A small delay (the 'C++' frontend breaks the pipeline with an | |
1411 | * unnecessary jump, making it work on my 386-33/Trantor T128, the | |
1412 | * tighter 'C' code breaks and requires this) solves the problem - | |
1413 | * the 1 us delay is arbitrary, and only used because this delay will | |
1414 | * be the same on other platforms and since it works here, it should | |
1415 | * work there. | |
1416 | * | |
1417 | * wingel suggests that this could be due to failing to wait | |
1418 | * one deskew delay. | |
1419 | */ | |
1420 | ||
1421 | udelay(1); | |
1422 | ||
422c0d61 | 1423 | dprintk(NDEBUG_SELECTION, ("scsi%d : selecting target %d\n", instance->host_no, scmd_id(cmd))); |
1da177e4 LT |
1424 | |
1425 | /* | |
1426 | * The SCSI specification calls for a 250 ms timeout for the actual | |
1427 | * selection. | |
1428 | */ | |
1429 | ||
1430 | timeout = jiffies + (250 * HZ / 1000); | |
1431 | ||
1432 | /* | |
1433 | * XXX very interesting - we're seeing a bounce where the BSY we | |
1434 | * asserted is being reflected / still asserted (propagation delay?) | |
1435 | * and it's detecting as true. Sigh. | |
1436 | */ | |
1437 | ||
1438 | hostdata->select_time = 0; /* we count the clock ticks at which we polled */ | |
1439 | hostdata->selecting = cmd; | |
1440 | ||
1441 | part2: | |
1442 | /* RvC: here we enter after a sleeping period, or immediately after | |
1443 | execution of part 1 | |
1444 | we poll only once ech clock tick */ | |
1445 | value = NCR5380_read(STATUS_REG) & (SR_BSY | SR_IO); | |
1446 | ||
1447 | if (!value && (hostdata->select_time < HZ/4)) { | |
1448 | /* RvC: we still must wait for a device response */ | |
1449 | hostdata->select_time++; /* after 25 ticks the device has failed */ | |
1450 | NCR5380_set_timer(hostdata, 1); | |
1451 | return 0; /* RvC: we return here with hostdata->selecting set, | |
1452 | to go to sleep */ | |
1453 | } | |
1454 | ||
1455 | hostdata->selecting = NULL;/* clear this pointer, because we passed the | |
1456 | waiting period */ | |
1457 | if ((NCR5380_read(STATUS_REG) & (SR_SEL | SR_IO)) == (SR_SEL | SR_IO)) { | |
1458 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
1459 | NCR5380_reselect(instance); | |
1460 | printk("scsi%d : reselection after won arbitration?\n", instance->host_no); | |
1461 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); | |
1462 | return -1; | |
1463 | } | |
1464 | /* | |
1465 | * No less than two deskew delays after the initiator detects the | |
1466 | * BSY signal is true, it shall release the SEL signal and may | |
1467 | * change the DATA BUS. -wingel | |
1468 | */ | |
1469 | ||
1470 | udelay(1); | |
1471 | ||
1472 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); | |
1473 | ||
1474 | if (!(NCR5380_read(STATUS_REG) & SR_BSY)) { | |
1475 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
422c0d61 | 1476 | if (hostdata->targets_present & (1 << scmd_id(cmd))) { |
1da177e4 LT |
1477 | printk(KERN_DEBUG "scsi%d : weirdness\n", instance->host_no); |
1478 | if (hostdata->restart_select) | |
1479 | printk(KERN_DEBUG "\trestart select\n"); | |
1480 | NCR5380_dprint(NDEBUG_SELECTION, instance); | |
1481 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); | |
1482 | return -1; | |
1483 | } | |
1484 | cmd->result = DID_BAD_TARGET << 16; | |
1485 | collect_stats(hostdata, cmd); | |
1486 | cmd->scsi_done(cmd); | |
1487 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); | |
1488 | dprintk(NDEBUG_SELECTION, ("scsi%d : target did not respond within 250ms\n", instance->host_no)); | |
1489 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); | |
1490 | return 0; | |
1491 | } | |
422c0d61 | 1492 | hostdata->targets_present |= (1 << scmd_id(cmd)); |
1da177e4 LT |
1493 | |
1494 | /* | |
1495 | * Since we followed the SCSI spec, and raised ATN while SEL | |
1496 | * was true but before BSY was false during selection, the information | |
1497 | * transfer phase should be a MESSAGE OUT phase so that we can send the | |
1498 | * IDENTIFY message. | |
1499 | * | |
1500 | * If SCSI-II tagged queuing is enabled, we also send a SIMPLE_QUEUE_TAG | |
1501 | * message (2 bytes) with a tag ID that we increment with every command | |
1502 | * until it wraps back to 0. | |
1503 | * | |
1504 | * XXX - it turns out that there are some broken SCSI-II devices, | |
1505 | * which claim to support tagged queuing but fail when more than | |
1506 | * some number of commands are issued at once. | |
1507 | */ | |
1508 | ||
1509 | /* Wait for start of REQ/ACK handshake */ | |
1510 | ||
1511 | spin_unlock_irq(instance->host_lock); | |
1512 | err = NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, HZ); | |
1513 | spin_lock_irq(instance->host_lock); | |
1514 | ||
1515 | if(err) { | |
1516 | printk(KERN_ERR "scsi%d: timeout at NCR5380.c:%d\n", instance->host_no, __LINE__); | |
1517 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); | |
1518 | goto failed; | |
1519 | } | |
1520 | ||
1521 | dprintk(NDEBUG_SELECTION, ("scsi%d : target %d selected, going into MESSAGE OUT phase.\n", instance->host_no, cmd->device->id)); | |
1522 | tmp[0] = IDENTIFY(((instance->irq == SCSI_IRQ_NONE) ? 0 : 1), cmd->device->lun); | |
1523 | ||
1524 | len = 1; | |
1525 | cmd->tag = 0; | |
1526 | ||
1527 | /* Send message(s) */ | |
1528 | data = tmp; | |
1529 | phase = PHASE_MSGOUT; | |
1530 | NCR5380_transfer_pio(instance, &phase, &len, &data); | |
1531 | dprintk(NDEBUG_SELECTION, ("scsi%d : nexus established.\n", instance->host_no)); | |
1532 | /* XXX need to handle errors here */ | |
1533 | hostdata->connected = cmd; | |
1534 | hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun); | |
1535 | ||
28424d3a | 1536 | initialize_SCp(cmd); |
1da177e4 LT |
1537 | |
1538 | return 0; | |
1539 | ||
1540 | /* Selection failed */ | |
1541 | failed: | |
1542 | return -1; | |
1543 | ||
1544 | } | |
1545 | ||
1546 | /* | |
1547 | * Function : int NCR5380_transfer_pio (struct Scsi_Host *instance, | |
1548 | * unsigned char *phase, int *count, unsigned char **data) | |
1549 | * | |
1550 | * Purpose : transfers data in given phase using polled I/O | |
1551 | * | |
1552 | * Inputs : instance - instance of driver, *phase - pointer to | |
1553 | * what phase is expected, *count - pointer to number of | |
1554 | * bytes to transfer, **data - pointer to data pointer. | |
1555 | * | |
1556 | * Returns : -1 when different phase is entered without transferring | |
25985edc | 1557 | * maximum number of bytes, 0 if all bytes or transferred or exit |
1da177e4 LT |
1558 | * is in same phase. |
1559 | * | |
1560 | * Also, *phase, *count, *data are modified in place. | |
1561 | * | |
1562 | * XXX Note : handling for bus free may be useful. | |
1563 | */ | |
1564 | ||
1565 | /* | |
1566 | * Note : this code is not as quick as it could be, however it | |
1567 | * IS 100% reliable, and for the actual data transfer where speed | |
1568 | * counts, we will always do a pseudo DMA or DMA transfer. | |
1569 | */ | |
1570 | ||
1571 | static int NCR5380_transfer_pio(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) { | |
1572 | NCR5380_local_declare(); | |
1573 | unsigned char p = *phase, tmp; | |
1574 | int c = *count; | |
1575 | unsigned char *d = *data; | |
1576 | /* | |
1577 | * RvC: some administrative data to process polling time | |
1578 | */ | |
1579 | int break_allowed = 0; | |
1580 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata; | |
1581 | NCR5380_setup(instance); | |
1582 | ||
1583 | if (!(p & SR_IO)) | |
1584 | dprintk(NDEBUG_PIO, ("scsi%d : pio write %d bytes\n", instance->host_no, c)); | |
1585 | else | |
1586 | dprintk(NDEBUG_PIO, ("scsi%d : pio read %d bytes\n", instance->host_no, c)); | |
1587 | ||
1588 | /* | |
1589 | * The NCR5380 chip will only drive the SCSI bus when the | |
1590 | * phase specified in the appropriate bits of the TARGET COMMAND | |
1591 | * REGISTER match the STATUS REGISTER | |
1592 | */ | |
1593 | ||
1594 | NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p)); | |
1595 | ||
1596 | /* RvC: don't know if this is necessary, but other SCSI I/O is short | |
1597 | * so breaks are not necessary there | |
1598 | */ | |
1599 | if ((p == PHASE_DATAIN) || (p == PHASE_DATAOUT)) { | |
1600 | break_allowed = 1; | |
1601 | } | |
1602 | do { | |
1603 | /* | |
1604 | * Wait for assertion of REQ, after which the phase bits will be | |
1605 | * valid | |
1606 | */ | |
1607 | ||
1608 | /* RvC: we simply poll once, after that we stop temporarily | |
1609 | * and let the device buffer fill up | |
1610 | * if breaking is not allowed, we keep polling as long as needed | |
1611 | */ | |
1612 | ||
1613 | /* FIXME */ | |
1614 | while (!((tmp = NCR5380_read(STATUS_REG)) & SR_REQ) && !break_allowed); | |
1615 | if (!(tmp & SR_REQ)) { | |
1616 | /* timeout condition */ | |
1617 | NCR5380_set_timer(hostdata, USLEEP_SLEEP); | |
1618 | break; | |
1619 | } | |
1620 | ||
1621 | dprintk(NDEBUG_HANDSHAKE, ("scsi%d : REQ detected\n", instance->host_no)); | |
1622 | ||
1623 | /* Check for phase mismatch */ | |
1624 | if ((tmp & PHASE_MASK) != p) { | |
1625 | dprintk(NDEBUG_HANDSHAKE, ("scsi%d : phase mismatch\n", instance->host_no)); | |
1626 | NCR5380_dprint_phase(NDEBUG_HANDSHAKE, instance); | |
1627 | break; | |
1628 | } | |
1629 | /* Do actual transfer from SCSI bus to / from memory */ | |
1630 | if (!(p & SR_IO)) | |
1631 | NCR5380_write(OUTPUT_DATA_REG, *d); | |
1632 | else | |
1633 | *d = NCR5380_read(CURRENT_SCSI_DATA_REG); | |
1634 | ||
1635 | ++d; | |
1636 | ||
1637 | /* | |
1638 | * The SCSI standard suggests that in MSGOUT phase, the initiator | |
1639 | * should drop ATN on the last byte of the message phase | |
1640 | * after REQ has been asserted for the handshake but before | |
1641 | * the initiator raises ACK. | |
1642 | */ | |
1643 | ||
1644 | if (!(p & SR_IO)) { | |
1645 | if (!((p & SR_MSG) && c > 1)) { | |
1646 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA); | |
1647 | NCR5380_dprint(NDEBUG_PIO, instance); | |
1648 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ACK); | |
1649 | } else { | |
1650 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN); | |
1651 | NCR5380_dprint(NDEBUG_PIO, instance); | |
1652 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA | ICR_ASSERT_ATN | ICR_ASSERT_ACK); | |
1653 | } | |
1654 | } else { | |
1655 | NCR5380_dprint(NDEBUG_PIO, instance); | |
1656 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ACK); | |
1657 | } | |
1658 | ||
1659 | /* FIXME - if this fails bus reset ?? */ | |
1660 | NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, 0, 5*HZ); | |
1661 | dprintk(NDEBUG_HANDSHAKE, ("scsi%d : req false, handshake complete\n", instance->host_no)); | |
1662 | ||
1663 | /* | |
1664 | * We have several special cases to consider during REQ/ACK handshaking : | |
1665 | * 1. We were in MSGOUT phase, and we are on the last byte of the | |
1666 | * message. ATN must be dropped as ACK is dropped. | |
1667 | * | |
1668 | * 2. We are in a MSGIN phase, and we are on the last byte of the | |
1669 | * message. We must exit with ACK asserted, so that the calling | |
1670 | * code may raise ATN before dropping ACK to reject the message. | |
1671 | * | |
1672 | * 3. ACK and ATN are clear and the target may proceed as normal. | |
1673 | */ | |
1674 | if (!(p == PHASE_MSGIN && c == 1)) { | |
1675 | if (p == PHASE_MSGOUT && c > 1) | |
1676 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); | |
1677 | else | |
1678 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
1679 | } | |
1680 | } while (--c); | |
1681 | ||
1682 | dprintk(NDEBUG_PIO, ("scsi%d : residual %d\n", instance->host_no, c)); | |
1683 | ||
1684 | *count = c; | |
1685 | *data = d; | |
1686 | tmp = NCR5380_read(STATUS_REG); | |
1687 | if (tmp & SR_REQ) | |
1688 | *phase = tmp & PHASE_MASK; | |
1689 | else | |
1690 | *phase = PHASE_UNKNOWN; | |
1691 | ||
1692 | if (!c || (*phase == p)) | |
1693 | return 0; | |
1694 | else | |
1695 | return -1; | |
1696 | } | |
1697 | ||
1698 | /** | |
1699 | * do_reset - issue a reset command | |
1700 | * @host: adapter to reset | |
1701 | * | |
1702 | * Issue a reset sequence to the NCR5380 and try and get the bus | |
1703 | * back into sane shape. | |
1704 | * | |
1705 | * Locks: caller holds queue lock | |
1706 | */ | |
1707 | ||
1708 | static void do_reset(struct Scsi_Host *host) { | |
1709 | NCR5380_local_declare(); | |
1710 | NCR5380_setup(host); | |
1711 | ||
1712 | NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(NCR5380_read(STATUS_REG) & PHASE_MASK)); | |
1713 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST); | |
1714 | udelay(25); | |
1715 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
1716 | } | |
1717 | ||
1718 | /* | |
1719 | * Function : do_abort (Scsi_Host *host) | |
1720 | * | |
1721 | * Purpose : abort the currently established nexus. Should only be | |
1722 | * called from a routine which can drop into a | |
1723 | * | |
1724 | * Returns : 0 on success, -1 on failure. | |
1725 | * | |
1726 | * Locks: queue lock held by caller | |
1727 | * FIXME: sort this out and get new_eh running | |
1728 | */ | |
1729 | ||
1730 | static int do_abort(struct Scsi_Host *host) { | |
1731 | NCR5380_local_declare(); | |
1732 | unsigned char *msgptr, phase, tmp; | |
1733 | int len; | |
1734 | int rc; | |
1735 | NCR5380_setup(host); | |
1736 | ||
1737 | ||
1738 | /* Request message out phase */ | |
1739 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); | |
1740 | ||
1741 | /* | |
1742 | * Wait for the target to indicate a valid phase by asserting | |
1743 | * REQ. Once this happens, we'll have either a MSGOUT phase | |
1744 | * and can immediately send the ABORT message, or we'll have some | |
1745 | * other phase and will have to source/sink data. | |
1746 | * | |
1747 | * We really don't care what value was on the bus or what value | |
1748 | * the target sees, so we just handshake. | |
1749 | */ | |
1750 | ||
1751 | rc = NCR5380_poll_politely(host, STATUS_REG, SR_REQ, SR_REQ, 60 * HZ); | |
1752 | ||
1753 | if(rc < 0) | |
1754 | return -1; | |
1755 | ||
1756 | tmp = (unsigned char)rc; | |
1757 | ||
1758 | NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp)); | |
1759 | ||
1760 | if ((tmp & PHASE_MASK) != PHASE_MSGOUT) { | |
1761 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK); | |
1762 | rc = NCR5380_poll_politely(host, STATUS_REG, SR_REQ, 0, 3*HZ); | |
1763 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); | |
1764 | if(rc == -1) | |
1765 | return -1; | |
1766 | } | |
1767 | tmp = ABORT; | |
1768 | msgptr = &tmp; | |
1769 | len = 1; | |
1770 | phase = PHASE_MSGOUT; | |
1771 | NCR5380_transfer_pio(host, &phase, &len, &msgptr); | |
1772 | ||
1773 | /* | |
1774 | * If we got here, and the command completed successfully, | |
1775 | * we're about to go into bus free state. | |
1776 | */ | |
1777 | ||
1778 | return len ? -1 : 0; | |
1779 | } | |
1780 | ||
1781 | #if defined(REAL_DMA) || defined(PSEUDO_DMA) || defined (REAL_DMA_POLL) | |
1782 | /* | |
1783 | * Function : int NCR5380_transfer_dma (struct Scsi_Host *instance, | |
1784 | * unsigned char *phase, int *count, unsigned char **data) | |
1785 | * | |
1786 | * Purpose : transfers data in given phase using either real | |
1787 | * or pseudo DMA. | |
1788 | * | |
1789 | * Inputs : instance - instance of driver, *phase - pointer to | |
1790 | * what phase is expected, *count - pointer to number of | |
1791 | * bytes to transfer, **data - pointer to data pointer. | |
1792 | * | |
1793 | * Returns : -1 when different phase is entered without transferring | |
25985edc | 1794 | * maximum number of bytes, 0 if all bytes or transferred or exit |
1da177e4 LT |
1795 | * is in same phase. |
1796 | * | |
1797 | * Also, *phase, *count, *data are modified in place. | |
1798 | * | |
1799 | * Locks: io_request lock held by caller | |
1800 | */ | |
1801 | ||
1802 | ||
1803 | static int NCR5380_transfer_dma(struct Scsi_Host *instance, unsigned char *phase, int *count, unsigned char **data) { | |
1804 | NCR5380_local_declare(); | |
1805 | register int c = *count; | |
1806 | register unsigned char p = *phase; | |
1807 | register unsigned char *d = *data; | |
1808 | unsigned char tmp; | |
1809 | int foo; | |
1810 | #if defined(REAL_DMA_POLL) | |
1811 | int cnt, toPIO; | |
1812 | unsigned char saved_data = 0, overrun = 0, residue; | |
1813 | #endif | |
1814 | ||
1815 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata; | |
1816 | ||
1817 | NCR5380_setup(instance); | |
1818 | ||
1819 | if ((tmp = (NCR5380_read(STATUS_REG) & PHASE_MASK)) != p) { | |
1820 | *phase = tmp; | |
1821 | return -1; | |
1822 | } | |
1823 | #if defined(REAL_DMA) || defined(REAL_DMA_POLL) | |
1824 | #ifdef READ_OVERRUNS | |
1825 | if (p & SR_IO) { | |
1826 | c -= 2; | |
1827 | } | |
1828 | #endif | |
1829 | dprintk(NDEBUG_DMA, ("scsi%d : initializing DMA channel %d for %s, %d bytes %s %0x\n", instance->host_no, instance->dma_channel, (p & SR_IO) ? "reading" : "writing", c, (p & SR_IO) ? "to" : "from", (unsigned) d)); | |
1830 | hostdata->dma_len = (p & SR_IO) ? NCR5380_dma_read_setup(instance, d, c) : NCR5380_dma_write_setup(instance, d, c); | |
1831 | #endif | |
1832 | ||
1833 | NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(p)); | |
1834 | ||
1835 | #ifdef REAL_DMA | |
1836 | NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY); | |
1837 | #elif defined(REAL_DMA_POLL) | |
1838 | NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE); | |
1839 | #else | |
1840 | /* | |
1841 | * Note : on my sample board, watch-dog timeouts occurred when interrupts | |
1842 | * were not disabled for the duration of a single DMA transfer, from | |
1843 | * before the setting of DMA mode to after transfer of the last byte. | |
1844 | */ | |
1845 | ||
1846 | #if defined(PSEUDO_DMA) && defined(UNSAFE) | |
1847 | spin_unlock_irq(instance->host_lock); | |
1848 | #endif | |
1849 | /* KLL May need eop and parity in 53c400 */ | |
1850 | if (hostdata->flags & FLAG_NCR53C400) | |
085267ac RK |
1851 | NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE | |
1852 | MR_ENABLE_PAR_CHECK | MR_ENABLE_PAR_INTR | | |
1853 | MR_ENABLE_EOP_INTR | MR_MONITOR_BSY); | |
1da177e4 LT |
1854 | else |
1855 | NCR5380_write(MODE_REG, MR_BASE | MR_DMA_MODE); | |
1856 | #endif /* def REAL_DMA */ | |
1857 | ||
1858 | dprintk(NDEBUG_DMA, ("scsi%d : mode reg = 0x%X\n", instance->host_no, NCR5380_read(MODE_REG))); | |
1859 | ||
1860 | /* | |
1861 | * On the PAS16 at least I/O recovery delays are not needed here. | |
1862 | * Everyone else seems to want them. | |
1863 | */ | |
1864 | ||
1865 | if (p & SR_IO) { | |
1866 | io_recovery_delay(1); | |
1867 | NCR5380_write(START_DMA_INITIATOR_RECEIVE_REG, 0); | |
1868 | } else { | |
1869 | io_recovery_delay(1); | |
1870 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_DATA); | |
1871 | io_recovery_delay(1); | |
1872 | NCR5380_write(START_DMA_SEND_REG, 0); | |
1873 | io_recovery_delay(1); | |
1874 | } | |
1875 | ||
1876 | #if defined(REAL_DMA_POLL) | |
1877 | do { | |
1878 | tmp = NCR5380_read(BUS_AND_STATUS_REG); | |
1879 | } while ((tmp & BASR_PHASE_MATCH) && !(tmp & (BASR_BUSY_ERROR | BASR_END_DMA_TRANSFER))); | |
1880 | ||
1881 | /* | |
1882 | At this point, either we've completed DMA, or we have a phase mismatch, | |
1883 | or we've unexpectedly lost BUSY (which is a real error). | |
1884 | ||
1885 | For write DMAs, we want to wait until the last byte has been | |
1886 | transferred out over the bus before we turn off DMA mode. Alas, there | |
1887 | seems to be no terribly good way of doing this on a 5380 under all | |
1888 | conditions. For non-scatter-gather operations, we can wait until REQ | |
1889 | and ACK both go false, or until a phase mismatch occurs. Gather-writes | |
1890 | are nastier, since the device will be expecting more data than we | |
1891 | are prepared to send it, and REQ will remain asserted. On a 53C8[01] we | |
1892 | could test LAST BIT SENT to assure transfer (I imagine this is precisely | |
1893 | why this signal was added to the newer chips) but on the older 538[01] | |
1894 | this signal does not exist. The workaround for this lack is a watchdog; | |
1895 | we bail out of the wait-loop after a modest amount of wait-time if | |
1896 | the usual exit conditions are not met. Not a terribly clean or | |
1897 | correct solution :-% | |
1898 | ||
1899 | Reads are equally tricky due to a nasty characteristic of the NCR5380. | |
1900 | If the chip is in DMA mode for an READ, it will respond to a target's | |
1901 | REQ by latching the SCSI data into the INPUT DATA register and asserting | |
1902 | ACK, even if it has _already_ been notified by the DMA controller that | |
1903 | the current DMA transfer has completed! If the NCR5380 is then taken | |
1904 | out of DMA mode, this already-acknowledged byte is lost. | |
1905 | ||
1906 | This is not a problem for "one DMA transfer per command" reads, because | |
1907 | the situation will never arise... either all of the data is DMA'ed | |
1908 | properly, or the target switches to MESSAGE IN phase to signal a | |
1909 | disconnection (either operation bringing the DMA to a clean halt). | |
1910 | However, in order to handle scatter-reads, we must work around the | |
1911 | problem. The chosen fix is to DMA N-2 bytes, then check for the | |
1912 | condition before taking the NCR5380 out of DMA mode. One or two extra | |
1913 | bytes are transferred via PIO as necessary to fill out the original | |
1914 | request. | |
1915 | */ | |
1916 | ||
1917 | if (p & SR_IO) { | |
1918 | #ifdef READ_OVERRUNS | |
1919 | udelay(10); | |
1920 | if (((NCR5380_read(BUS_AND_STATUS_REG) & (BASR_PHASE_MATCH | BASR_ACK)) == (BASR_PHASE_MATCH | BASR_ACK))) { | |
1921 | saved_data = NCR5380_read(INPUT_DATA_REGISTER); | |
1922 | overrun = 1; | |
1923 | } | |
1924 | #endif | |
1925 | } else { | |
1926 | int limit = 100; | |
1927 | while (((tmp = NCR5380_read(BUS_AND_STATUS_REG)) & BASR_ACK) || (NCR5380_read(STATUS_REG) & SR_REQ)) { | |
1928 | if (!(tmp & BASR_PHASE_MATCH)) | |
1929 | break; | |
1930 | if (--limit < 0) | |
1931 | break; | |
1932 | } | |
1933 | } | |
1934 | ||
1935 | dprintk(NDEBUG_DMA, ("scsi%d : polled DMA transfer complete, basr 0x%X, sr 0x%X\n", instance->host_no, tmp, NCR5380_read(STATUS_REG))); | |
1936 | ||
1937 | NCR5380_write(MODE_REG, MR_BASE); | |
1938 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
1939 | ||
1940 | residue = NCR5380_dma_residual(instance); | |
1941 | c -= residue; | |
1942 | *count -= c; | |
1943 | *data += c; | |
1944 | *phase = NCR5380_read(STATUS_REG) & PHASE_MASK; | |
1945 | ||
1946 | #ifdef READ_OVERRUNS | |
1947 | if (*phase == p && (p & SR_IO) && residue == 0) { | |
1948 | if (overrun) { | |
1949 | dprintk(NDEBUG_DMA, ("Got an input overrun, using saved byte\n")); | |
1950 | **data = saved_data; | |
1951 | *data += 1; | |
1952 | *count -= 1; | |
1953 | cnt = toPIO = 1; | |
1954 | } else { | |
1955 | printk("No overrun??\n"); | |
1956 | cnt = toPIO = 2; | |
1957 | } | |
1958 | dprintk(NDEBUG_DMA, ("Doing %d-byte PIO to 0x%X\n", cnt, *data)); | |
1959 | NCR5380_transfer_pio(instance, phase, &cnt, data); | |
1960 | *count -= toPIO - cnt; | |
1961 | } | |
1962 | #endif | |
1963 | ||
1964 | dprintk(NDEBUG_DMA, ("Return with data ptr = 0x%X, count %d, last 0x%X, next 0x%X\n", *data, *count, *(*data + *count - 1), *(*data + *count))); | |
1965 | return 0; | |
1966 | ||
1967 | #elif defined(REAL_DMA) | |
1968 | return 0; | |
1969 | #else /* defined(REAL_DMA_POLL) */ | |
1970 | if (p & SR_IO) { | |
1971 | #ifdef DMA_WORKS_RIGHT | |
1972 | foo = NCR5380_pread(instance, d, c); | |
1973 | #else | |
1974 | int diff = 1; | |
1975 | if (hostdata->flags & FLAG_NCR53C400) { | |
1976 | diff = 0; | |
1977 | } | |
1978 | if (!(foo = NCR5380_pread(instance, d, c - diff))) { | |
1979 | /* | |
1980 | * We can't disable DMA mode after successfully transferring | |
1981 | * what we plan to be the last byte, since that would open up | |
1982 | * a race condition where if the target asserted REQ before | |
1983 | * we got the DMA mode reset, the NCR5380 would have latched | |
1984 | * an additional byte into the INPUT DATA register and we'd | |
1985 | * have dropped it. | |
1986 | * | |
1987 | * The workaround was to transfer one fewer bytes than we | |
1988 | * intended to with the pseudo-DMA read function, wait for | |
1989 | * the chip to latch the last byte, read it, and then disable | |
1990 | * pseudo-DMA mode. | |
1991 | * | |
1992 | * After REQ is asserted, the NCR5380 asserts DRQ and ACK. | |
1993 | * REQ is deasserted when ACK is asserted, and not reasserted | |
1994 | * until ACK goes false. Since the NCR5380 won't lower ACK | |
1995 | * until DACK is asserted, which won't happen unless we twiddle | |
1996 | * the DMA port or we take the NCR5380 out of DMA mode, we | |
1997 | * can guarantee that we won't handshake another extra | |
1998 | * byte. | |
1999 | */ | |
2000 | ||
2001 | if (!(hostdata->flags & FLAG_NCR53C400)) { | |
2002 | while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ)); | |
2003 | /* Wait for clean handshake */ | |
2004 | while (NCR5380_read(STATUS_REG) & SR_REQ); | |
2005 | d[c - 1] = NCR5380_read(INPUT_DATA_REG); | |
2006 | } | |
2007 | } | |
2008 | #endif | |
2009 | } else { | |
2010 | #ifdef DMA_WORKS_RIGHT | |
2011 | foo = NCR5380_pwrite(instance, d, c); | |
2012 | #else | |
2013 | int timeout; | |
2014 | dprintk(NDEBUG_C400_PWRITE, ("About to pwrite %d bytes\n", c)); | |
2015 | if (!(foo = NCR5380_pwrite(instance, d, c))) { | |
2016 | /* | |
2017 | * Wait for the last byte to be sent. If REQ is being asserted for | |
2018 | * the byte we're interested, we'll ACK it and it will go false. | |
2019 | */ | |
2020 | if (!(hostdata->flags & FLAG_HAS_LAST_BYTE_SENT)) { | |
2021 | timeout = 20000; | |
2022 | while (!(NCR5380_read(BUS_AND_STATUS_REG) & BASR_DRQ) && (NCR5380_read(BUS_AND_STATUS_REG) & BASR_PHASE_MATCH)); | |
2023 | ||
2024 | if (!timeout) | |
2025 | dprintk(NDEBUG_LAST_BYTE_SENT, ("scsi%d : timed out on last byte\n", instance->host_no)); | |
2026 | ||
2027 | if (hostdata->flags & FLAG_CHECK_LAST_BYTE_SENT) { | |
2028 | hostdata->flags &= ~FLAG_CHECK_LAST_BYTE_SENT; | |
2029 | if (NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT) { | |
2030 | hostdata->flags |= FLAG_HAS_LAST_BYTE_SENT; | |
4dde8f7d | 2031 | dprintk(NDEBUG_LAST_BYTE_SENT, ("scsi%d : last byte sent works\n", instance->host_no)); |
1da177e4 LT |
2032 | } |
2033 | } | |
2034 | } else { | |
2035 | dprintk(NDEBUG_C400_PWRITE, ("Waiting for LASTBYTE\n")); | |
2036 | while (!(NCR5380_read(TARGET_COMMAND_REG) & TCR_LAST_BYTE_SENT)); | |
2037 | dprintk(NDEBUG_C400_PWRITE, ("Got LASTBYTE\n")); | |
2038 | } | |
2039 | } | |
2040 | #endif | |
2041 | } | |
2042 | NCR5380_write(MODE_REG, MR_BASE); | |
2043 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
2044 | ||
2045 | if ((!(p & SR_IO)) && (hostdata->flags & FLAG_NCR53C400)) { | |
2046 | dprintk(NDEBUG_C400_PWRITE, ("53C400w: Checking for IRQ\n")); | |
2047 | if (NCR5380_read(BUS_AND_STATUS_REG) & BASR_IRQ) { | |
2048 | dprintk(NDEBUG_C400_PWRITE, ("53C400w: got it, reading reset interrupt reg\n")); | |
2049 | NCR5380_read(RESET_PARITY_INTERRUPT_REG); | |
2050 | } else { | |
2051 | printk("53C400w: IRQ NOT THERE!\n"); | |
2052 | } | |
2053 | } | |
2054 | *data = d + c; | |
2055 | *count = 0; | |
2056 | *phase = NCR5380_read(STATUS_REG) & PHASE_MASK; | |
2057 | #if defined(PSEUDO_DMA) && defined(UNSAFE) | |
2058 | spin_lock_irq(instance->host_lock); | |
2059 | #endif /* defined(REAL_DMA_POLL) */ | |
2060 | return foo; | |
2061 | #endif /* def REAL_DMA */ | |
2062 | } | |
2063 | #endif /* defined(REAL_DMA) | defined(PSEUDO_DMA) */ | |
2064 | ||
2065 | /* | |
2066 | * Function : NCR5380_information_transfer (struct Scsi_Host *instance) | |
2067 | * | |
2068 | * Purpose : run through the various SCSI phases and do as the target | |
2069 | * directs us to. Operates on the currently connected command, | |
2070 | * instance->connected. | |
2071 | * | |
2072 | * Inputs : instance, instance for which we are doing commands | |
2073 | * | |
2074 | * Side effects : SCSI things happen, the disconnected queue will be | |
2075 | * modified if a command disconnects, *instance->connected will | |
2076 | * change. | |
2077 | * | |
2078 | * XXX Note : we need to watch for bus free or a reset condition here | |
2079 | * to recover from an unexpected bus free condition. | |
2080 | * | |
2081 | * Locks: io_request_lock held by caller in IRQ mode | |
2082 | */ | |
2083 | ||
2084 | static void NCR5380_information_transfer(struct Scsi_Host *instance) { | |
2085 | NCR5380_local_declare(); | |
2086 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *)instance->hostdata; | |
2087 | unsigned char msgout = NOP; | |
2088 | int sink = 0; | |
2089 | int len; | |
2090 | #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) | |
2091 | int transfersize; | |
2092 | #endif | |
2093 | unsigned char *data; | |
2094 | unsigned char phase, tmp, extended_msg[10], old_phase = 0xff; | |
2095 | Scsi_Cmnd *cmd = (Scsi_Cmnd *) hostdata->connected; | |
2096 | /* RvC: we need to set the end of the polling time */ | |
2097 | unsigned long poll_time = jiffies + USLEEP_POLL; | |
2098 | ||
2099 | NCR5380_setup(instance); | |
2100 | ||
2101 | while (1) { | |
2102 | tmp = NCR5380_read(STATUS_REG); | |
2103 | /* We only have a valid SCSI phase when REQ is asserted */ | |
2104 | if (tmp & SR_REQ) { | |
2105 | phase = (tmp & PHASE_MASK); | |
2106 | if (phase != old_phase) { | |
2107 | old_phase = phase; | |
2108 | NCR5380_dprint_phase(NDEBUG_INFORMATION, instance); | |
2109 | } | |
2110 | if (sink && (phase != PHASE_MSGOUT)) { | |
2111 | NCR5380_write(TARGET_COMMAND_REG, PHASE_SR_TO_TCR(tmp)); | |
2112 | ||
2113 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN | ICR_ASSERT_ACK); | |
2114 | while (NCR5380_read(STATUS_REG) & SR_REQ); | |
2115 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); | |
2116 | sink = 0; | |
2117 | continue; | |
2118 | } | |
2119 | switch (phase) { | |
2120 | case PHASE_DATAIN: | |
2121 | case PHASE_DATAOUT: | |
2122 | #if (NDEBUG & NDEBUG_NO_DATAOUT) | |
2123 | printk("scsi%d : NDEBUG_NO_DATAOUT set, attempted DATAOUT aborted\n", instance->host_no); | |
2124 | sink = 1; | |
2125 | do_abort(instance); | |
2126 | cmd->result = DID_ERROR << 16; | |
cce99c69 | 2127 | cmd->scsi_done(cmd); |
1da177e4 LT |
2128 | return; |
2129 | #endif | |
2130 | /* | |
2131 | * If there is no room left in the current buffer in the | |
2132 | * scatter-gather list, move onto the next one. | |
2133 | */ | |
2134 | ||
2135 | if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) { | |
2136 | ++cmd->SCp.buffer; | |
2137 | --cmd->SCp.buffers_residual; | |
2138 | cmd->SCp.this_residual = cmd->SCp.buffer->length; | |
45711f1a | 2139 | cmd->SCp.ptr = sg_virt(cmd->SCp.buffer); |
1da177e4 LT |
2140 | dprintk(NDEBUG_INFORMATION, ("scsi%d : %d bytes and %d buffers left\n", instance->host_no, cmd->SCp.this_residual, cmd->SCp.buffers_residual)); |
2141 | } | |
2142 | /* | |
2143 | * The preferred transfer method is going to be | |
2144 | * PSEUDO-DMA for systems that are strictly PIO, | |
2145 | * since we can let the hardware do the handshaking. | |
2146 | * | |
2147 | * For this to work, we need to know the transfersize | |
2148 | * ahead of time, since the pseudo-DMA code will sit | |
2149 | * in an unconditional loop. | |
2150 | */ | |
2151 | ||
2152 | #if defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) | |
2153 | /* KLL | |
2154 | * PSEUDO_DMA is defined here. If this is the g_NCR5380 | |
2155 | * driver then it will always be defined, so the | |
2156 | * FLAG_NO_PSEUDO_DMA is used to inhibit PDMA in the base | |
2157 | * NCR5380 case. I think this is a fairly clean solution. | |
2158 | * We supplement these 2 if's with the flag. | |
2159 | */ | |
2160 | #ifdef NCR5380_dma_xfer_len | |
2161 | if (!cmd->device->borken && !(hostdata->flags & FLAG_NO_PSEUDO_DMA) && (transfersize = NCR5380_dma_xfer_len(instance, cmd)) != 0) { | |
2162 | #else | |
2163 | transfersize = cmd->transfersize; | |
2164 | ||
2165 | #ifdef LIMIT_TRANSFERSIZE /* If we have problems with interrupt service */ | |
2166 | if (transfersize > 512) | |
2167 | transfersize = 512; | |
2168 | #endif /* LIMIT_TRANSFERSIZE */ | |
2169 | ||
2170 | if (!cmd->device->borken && transfersize && !(hostdata->flags & FLAG_NO_PSEUDO_DMA) && cmd->SCp.this_residual && !(cmd->SCp.this_residual % transfersize)) { | |
2171 | /* Limit transfers to 32K, for xx400 & xx406 | |
2172 | * pseudoDMA that transfers in 128 bytes blocks. */ | |
2173 | if (transfersize > 32 * 1024) | |
2174 | transfersize = 32 * 1024; | |
2175 | #endif | |
2176 | len = transfersize; | |
2177 | if (NCR5380_transfer_dma(instance, &phase, &len, (unsigned char **) &cmd->SCp.ptr)) { | |
2178 | /* | |
2179 | * If the watchdog timer fires, all future accesses to this | |
2180 | * device will use the polled-IO. | |
2181 | */ | |
017560fc JG |
2182 | scmd_printk(KERN_INFO, cmd, |
2183 | "switching to slow handshake\n"); | |
1da177e4 LT |
2184 | cmd->device->borken = 1; |
2185 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); | |
2186 | sink = 1; | |
2187 | do_abort(instance); | |
2188 | cmd->result = DID_ERROR << 16; | |
cce99c69 | 2189 | cmd->scsi_done(cmd); |
1da177e4 LT |
2190 | /* XXX - need to source or sink data here, as appropriate */ |
2191 | } else | |
2192 | cmd->SCp.this_residual -= transfersize - len; | |
2193 | } else | |
2194 | #endif /* defined(PSEUDO_DMA) || defined(REAL_DMA_POLL) */ | |
2195 | NCR5380_transfer_pio(instance, &phase, (int *) &cmd->SCp.this_residual, (unsigned char **) | |
2196 | &cmd->SCp.ptr); | |
2197 | break; | |
2198 | case PHASE_MSGIN: | |
2199 | len = 1; | |
2200 | data = &tmp; | |
2201 | NCR5380_transfer_pio(instance, &phase, &len, &data); | |
2202 | cmd->SCp.Message = tmp; | |
2203 | ||
2204 | switch (tmp) { | |
2205 | /* | |
2206 | * Linking lets us reduce the time required to get the | |
2207 | * next command out to the device, hopefully this will | |
2208 | * mean we don't waste another revolution due to the delays | |
2209 | * required by ARBITRATION and another SELECTION. | |
2210 | * | |
2211 | * In the current implementation proposal, low level drivers | |
2212 | * merely have to start the next command, pointed to by | |
2213 | * next_link, done() is called as with unlinked commands. | |
2214 | */ | |
2215 | #ifdef LINKED | |
2216 | case LINKED_CMD_COMPLETE: | |
2217 | case LINKED_FLG_CMD_COMPLETE: | |
2218 | /* Accept message by clearing ACK */ | |
2219 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
2220 | dprintk(NDEBUG_LINKED, ("scsi%d : target %d lun %d linked command complete.\n", instance->host_no, cmd->device->id, cmd->device->lun)); | |
2221 | /* | |
2222 | * Sanity check : A linked command should only terminate with | |
2223 | * one of these messages if there are more linked commands | |
2224 | * available. | |
2225 | */ | |
2226 | if (!cmd->next_link) { | |
2227 | printk("scsi%d : target %d lun %d linked command complete, no next_link\n" instance->host_no, cmd->device->id, cmd->device->lun); | |
2228 | sink = 1; | |
2229 | do_abort(instance); | |
2230 | return; | |
2231 | } | |
2232 | initialize_SCp(cmd->next_link); | |
2233 | /* The next command is still part of this process */ | |
2234 | cmd->next_link->tag = cmd->tag; | |
2235 | cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8); | |
2236 | dprintk(NDEBUG_LINKED, ("scsi%d : target %d lun %d linked request done, calling scsi_done().\n", instance->host_no, cmd->device->id, cmd->device->lun)); | |
2237 | collect_stats(hostdata, cmd); | |
2238 | cmd->scsi_done(cmd); | |
2239 | cmd = hostdata->connected; | |
2240 | break; | |
2241 | #endif /* def LINKED */ | |
2242 | case ABORT: | |
2243 | case COMMAND_COMPLETE: | |
2244 | /* Accept message by clearing ACK */ | |
2245 | sink = 1; | |
2246 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
2247 | hostdata->connected = NULL; | |
2248 | dprintk(NDEBUG_QUEUES, ("scsi%d : command for target %d, lun %d completed\n", instance->host_no, cmd->device->id, cmd->device->lun)); | |
2249 | hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun); | |
2250 | ||
2251 | /* | |
2252 | * I'm not sure what the correct thing to do here is : | |
2253 | * | |
2254 | * If the command that just executed is NOT a request | |
2255 | * sense, the obvious thing to do is to set the result | |
2256 | * code to the values of the stored parameters. | |
2257 | * | |
2258 | * If it was a REQUEST SENSE command, we need some way | |
2259 | * to differentiate between the failure code of the original | |
2260 | * and the failure code of the REQUEST sense - the obvious | |
2261 | * case is success, where we fall through and leave the result | |
2262 | * code unchanged. | |
2263 | * | |
2264 | * The non-obvious place is where the REQUEST SENSE failed | |
2265 | */ | |
2266 | ||
2267 | if (cmd->cmnd[0] != REQUEST_SENSE) | |
2268 | cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8); | |
2269 | else if (status_byte(cmd->SCp.Status) != GOOD) | |
2270 | cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16); | |
2271 | ||
2272 | #ifdef AUTOSENSE | |
28424d3a BH |
2273 | if ((cmd->cmnd[0] == REQUEST_SENSE) && |
2274 | hostdata->ses.cmd_len) { | |
2275 | scsi_eh_restore_cmnd(cmd, &hostdata->ses); | |
2276 | hostdata->ses.cmd_len = 0 ; | |
2277 | } | |
2278 | ||
1da177e4 | 2279 | if ((cmd->cmnd[0] != REQUEST_SENSE) && (status_byte(cmd->SCp.Status) == CHECK_CONDITION)) { |
28424d3a BH |
2280 | scsi_eh_prep_cmnd(cmd, &hostdata->ses, NULL, 0, ~0); |
2281 | ||
1da177e4 | 2282 | dprintk(NDEBUG_AUTOSENSE, ("scsi%d : performing request sense\n", instance->host_no)); |
1da177e4 LT |
2283 | |
2284 | LIST(cmd, hostdata->issue_queue); | |
2285 | cmd->host_scribble = (unsigned char *) | |
2286 | hostdata->issue_queue; | |
2287 | hostdata->issue_queue = (Scsi_Cmnd *) cmd; | |
2288 | dprintk(NDEBUG_QUEUES, ("scsi%d : REQUEST SENSE added to head of issue queue\n", instance->host_no)); | |
2289 | } else | |
2290 | #endif /* def AUTOSENSE */ | |
2291 | { | |
2292 | collect_stats(hostdata, cmd); | |
2293 | cmd->scsi_done(cmd); | |
2294 | } | |
2295 | ||
2296 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); | |
2297 | /* | |
2298 | * Restore phase bits to 0 so an interrupted selection, | |
2299 | * arbitration can resume. | |
2300 | */ | |
2301 | NCR5380_write(TARGET_COMMAND_REG, 0); | |
2302 | ||
2303 | while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected) | |
2304 | barrier(); | |
2305 | return; | |
2306 | case MESSAGE_REJECT: | |
2307 | /* Accept message by clearing ACK */ | |
2308 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
2309 | switch (hostdata->last_message) { | |
2310 | case HEAD_OF_QUEUE_TAG: | |
2311 | case ORDERED_QUEUE_TAG: | |
2312 | case SIMPLE_QUEUE_TAG: | |
2313 | cmd->device->simple_tags = 0; | |
2314 | hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun); | |
2315 | break; | |
2316 | default: | |
2317 | break; | |
2318 | } | |
2319 | case DISCONNECT:{ | |
2320 | /* Accept message by clearing ACK */ | |
2321 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
2322 | cmd->device->disconnect = 1; | |
2323 | LIST(cmd, hostdata->disconnected_queue); | |
2324 | cmd->host_scribble = (unsigned char *) | |
2325 | hostdata->disconnected_queue; | |
2326 | hostdata->connected = NULL; | |
2327 | hostdata->disconnected_queue = cmd; | |
2328 | dprintk(NDEBUG_QUEUES, ("scsi%d : command for target %d lun %d was moved from connected to" " the disconnected_queue\n", instance->host_no, cmd->device->id, cmd->device->lun)); | |
2329 | /* | |
2330 | * Restore phase bits to 0 so an interrupted selection, | |
2331 | * arbitration can resume. | |
2332 | */ | |
2333 | NCR5380_write(TARGET_COMMAND_REG, 0); | |
2334 | ||
2335 | /* Enable reselect interrupts */ | |
2336 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); | |
2337 | /* Wait for bus free to avoid nasty timeouts - FIXME timeout !*/ | |
2338 | /* NCR538_poll_politely(instance, STATUS_REG, SR_BSY, 0, 30 * HZ); */ | |
2339 | while ((NCR5380_read(STATUS_REG) & SR_BSY) && !hostdata->connected) | |
2340 | barrier(); | |
2341 | return; | |
2342 | } | |
2343 | /* | |
2344 | * The SCSI data pointer is *IMPLICITLY* saved on a disconnect | |
2345 | * operation, in violation of the SCSI spec so we can safely | |
2346 | * ignore SAVE/RESTORE pointers calls. | |
2347 | * | |
2348 | * Unfortunately, some disks violate the SCSI spec and | |
2349 | * don't issue the required SAVE_POINTERS message before | |
2350 | * disconnecting, and we have to break spec to remain | |
2351 | * compatible. | |
2352 | */ | |
2353 | case SAVE_POINTERS: | |
2354 | case RESTORE_POINTERS: | |
2355 | /* Accept message by clearing ACK */ | |
2356 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
2357 | break; | |
2358 | case EXTENDED_MESSAGE: | |
2359 | /* | |
2360 | * Extended messages are sent in the following format : | |
2361 | * Byte | |
2362 | * 0 EXTENDED_MESSAGE == 1 | |
2363 | * 1 length (includes one byte for code, doesn't | |
2364 | * include first two bytes) | |
2365 | * 2 code | |
2366 | * 3..length+1 arguments | |
2367 | * | |
2368 | * Start the extended message buffer with the EXTENDED_MESSAGE | |
1abfd370 | 2369 | * byte, since spi_print_msg() wants the whole thing. |
1da177e4 LT |
2370 | */ |
2371 | extended_msg[0] = EXTENDED_MESSAGE; | |
2372 | /* Accept first byte by clearing ACK */ | |
2373 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
2374 | dprintk(NDEBUG_EXTENDED, ("scsi%d : receiving extended message\n", instance->host_no)); | |
2375 | ||
2376 | len = 2; | |
2377 | data = extended_msg + 1; | |
2378 | phase = PHASE_MSGIN; | |
2379 | NCR5380_transfer_pio(instance, &phase, &len, &data); | |
2380 | ||
2381 | dprintk(NDEBUG_EXTENDED, ("scsi%d : length=%d, code=0x%02x\n", instance->host_no, (int) extended_msg[1], (int) extended_msg[2])); | |
2382 | ||
2383 | if (!len && extended_msg[1] <= (sizeof(extended_msg) - 1)) { | |
2384 | /* Accept third byte by clearing ACK */ | |
2385 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
2386 | len = extended_msg[1] - 1; | |
2387 | data = extended_msg + 3; | |
2388 | phase = PHASE_MSGIN; | |
2389 | ||
2390 | NCR5380_transfer_pio(instance, &phase, &len, &data); | |
2391 | dprintk(NDEBUG_EXTENDED, ("scsi%d : message received, residual %d\n", instance->host_no, len)); | |
2392 | ||
2393 | switch (extended_msg[2]) { | |
2394 | case EXTENDED_SDTR: | |
2395 | case EXTENDED_WDTR: | |
2396 | case EXTENDED_MODIFY_DATA_POINTER: | |
2397 | case EXTENDED_EXTENDED_IDENTIFY: | |
2398 | tmp = 0; | |
2399 | } | |
2400 | } else if (len) { | |
2401 | printk("scsi%d: error receiving extended message\n", instance->host_no); | |
2402 | tmp = 0; | |
2403 | } else { | |
2404 | printk("scsi%d: extended message code %02x length %d is too long\n", instance->host_no, extended_msg[2], extended_msg[1]); | |
2405 | tmp = 0; | |
2406 | } | |
2407 | /* Fall through to reject message */ | |
2408 | ||
2409 | /* | |
2410 | * If we get something weird that we aren't expecting, | |
2411 | * reject it. | |
2412 | */ | |
2413 | default: | |
2414 | if (!tmp) { | |
2415 | printk("scsi%d: rejecting message ", instance->host_no); | |
1abfd370 | 2416 | spi_print_msg(extended_msg); |
1da177e4 LT |
2417 | printk("\n"); |
2418 | } else if (tmp != EXTENDED_MESSAGE) | |
017560fc JG |
2419 | scmd_printk(KERN_INFO, cmd, |
2420 | "rejecting unknown message %02x\n",tmp); | |
1da177e4 | 2421 | else |
017560fc JG |
2422 | scmd_printk(KERN_INFO, cmd, |
2423 | "rejecting unknown extended message code %02x, length %d\n", extended_msg[1], extended_msg[0]); | |
1da177e4 LT |
2424 | |
2425 | msgout = MESSAGE_REJECT; | |
2426 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_ATN); | |
2427 | break; | |
2428 | } /* switch (tmp) */ | |
2429 | break; | |
2430 | case PHASE_MSGOUT: | |
2431 | len = 1; | |
2432 | data = &msgout; | |
2433 | hostdata->last_message = msgout; | |
2434 | NCR5380_transfer_pio(instance, &phase, &len, &data); | |
2435 | if (msgout == ABORT) { | |
2436 | hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun); | |
2437 | hostdata->connected = NULL; | |
2438 | cmd->result = DID_ERROR << 16; | |
2439 | collect_stats(hostdata, cmd); | |
2440 | cmd->scsi_done(cmd); | |
2441 | NCR5380_write(SELECT_ENABLE_REG, hostdata->id_mask); | |
2442 | return; | |
2443 | } | |
2444 | msgout = NOP; | |
2445 | break; | |
2446 | case PHASE_CMDOUT: | |
2447 | len = cmd->cmd_len; | |
2448 | data = cmd->cmnd; | |
2449 | /* | |
2450 | * XXX for performance reasons, on machines with a | |
2451 | * PSEUDO-DMA architecture we should probably | |
2452 | * use the dma transfer function. | |
2453 | */ | |
2454 | NCR5380_transfer_pio(instance, &phase, &len, &data); | |
2455 | if (!cmd->device->disconnect && should_disconnect(cmd->cmnd[0])) { | |
2456 | NCR5380_set_timer(hostdata, USLEEP_SLEEP); | |
4dde8f7d | 2457 | dprintk(NDEBUG_USLEEP, ("scsi%d : issued command, sleeping until %lu\n", instance->host_no, hostdata->time_expires)); |
1da177e4 LT |
2458 | return; |
2459 | } | |
2460 | break; | |
2461 | case PHASE_STATIN: | |
2462 | len = 1; | |
2463 | data = &tmp; | |
2464 | NCR5380_transfer_pio(instance, &phase, &len, &data); | |
2465 | cmd->SCp.Status = tmp; | |
2466 | break; | |
2467 | default: | |
2468 | printk("scsi%d : unknown phase\n", instance->host_no); | |
4dde8f7d | 2469 | NCR5380_dprint(NDEBUG_ANY, instance); |
1da177e4 LT |
2470 | } /* switch(phase) */ |
2471 | } /* if (tmp * SR_REQ) */ | |
2472 | else { | |
2473 | /* RvC: go to sleep if polling time expired | |
2474 | */ | |
2475 | if (!cmd->device->disconnect && time_after_eq(jiffies, poll_time)) { | |
2476 | NCR5380_set_timer(hostdata, USLEEP_SLEEP); | |
4dde8f7d | 2477 | dprintk(NDEBUG_USLEEP, ("scsi%d : poll timed out, sleeping until %lu\n", instance->host_no, hostdata->time_expires)); |
1da177e4 LT |
2478 | return; |
2479 | } | |
2480 | } | |
2481 | } /* while (1) */ | |
2482 | } | |
2483 | ||
2484 | /* | |
2485 | * Function : void NCR5380_reselect (struct Scsi_Host *instance) | |
2486 | * | |
2487 | * Purpose : does reselection, initializing the instance->connected | |
2488 | * field to point to the Scsi_Cmnd for which the I_T_L or I_T_L_Q | |
2489 | * nexus has been reestablished, | |
2490 | * | |
2491 | * Inputs : instance - this instance of the NCR5380. | |
2492 | * | |
2493 | * Locks: io_request_lock held by caller if IRQ driven | |
2494 | */ | |
2495 | ||
2496 | static void NCR5380_reselect(struct Scsi_Host *instance) { | |
2497 | NCR5380_local_declare(); | |
2498 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) | |
2499 | instance->hostdata; | |
2500 | unsigned char target_mask; | |
2501 | unsigned char lun, phase; | |
2502 | int len; | |
2503 | unsigned char msg[3]; | |
2504 | unsigned char *data; | |
2505 | Scsi_Cmnd *tmp = NULL, *prev; | |
2506 | int abort = 0; | |
2507 | NCR5380_setup(instance); | |
2508 | ||
2509 | /* | |
2510 | * Disable arbitration, etc. since the host adapter obviously | |
2511 | * lost, and tell an interrupted NCR5380_select() to restart. | |
2512 | */ | |
2513 | ||
2514 | NCR5380_write(MODE_REG, MR_BASE); | |
2515 | hostdata->restart_select = 1; | |
2516 | ||
2517 | target_mask = NCR5380_read(CURRENT_SCSI_DATA_REG) & ~(hostdata->id_mask); | |
2518 | dprintk(NDEBUG_SELECTION, ("scsi%d : reselect\n", instance->host_no)); | |
2519 | ||
2520 | /* | |
2521 | * At this point, we have detected that our SCSI ID is on the bus, | |
2522 | * SEL is true and BSY was false for at least one bus settle delay | |
2523 | * (400 ns). | |
2524 | * | |
2525 | * We must assert BSY ourselves, until the target drops the SEL | |
2526 | * signal. | |
2527 | */ | |
2528 | ||
2529 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_BSY); | |
2530 | ||
2531 | /* FIXME: timeout too long, must fail to workqueue */ | |
2532 | if(NCR5380_poll_politely(instance, STATUS_REG, SR_SEL, 0, 2*HZ)<0) | |
2533 | abort = 1; | |
2534 | ||
2535 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
2536 | ||
2537 | /* | |
2538 | * Wait for target to go into MSGIN. | |
2539 | * FIXME: timeout needed and fail to work queeu | |
2540 | */ | |
2541 | ||
2542 | if(NCR5380_poll_politely(instance, STATUS_REG, SR_REQ, SR_REQ, 2*HZ)) | |
2543 | abort = 1; | |
2544 | ||
2545 | len = 1; | |
2546 | data = msg; | |
2547 | phase = PHASE_MSGIN; | |
2548 | NCR5380_transfer_pio(instance, &phase, &len, &data); | |
2549 | ||
2550 | if (!(msg[0] & 0x80)) { | |
2551 | printk(KERN_ERR "scsi%d : expecting IDENTIFY message, got ", instance->host_no); | |
1abfd370 | 2552 | spi_print_msg(msg); |
1da177e4 LT |
2553 | abort = 1; |
2554 | } else { | |
2555 | /* Accept message by clearing ACK */ | |
2556 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
2557 | lun = (msg[0] & 0x07); | |
2558 | ||
2559 | /* | |
2560 | * We need to add code for SCSI-II to track which devices have | |
2561 | * I_T_L_Q nexuses established, and which have simple I_T_L | |
2562 | * nexuses so we can chose to do additional data transfer. | |
2563 | */ | |
2564 | ||
2565 | /* | |
2566 | * Find the command corresponding to the I_T_L or I_T_L_Q nexus we | |
2567 | * just reestablished, and remove it from the disconnected queue. | |
2568 | */ | |
2569 | ||
2570 | ||
2571 | for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue, prev = NULL; tmp; prev = tmp, tmp = (Scsi_Cmnd *) tmp->host_scribble) | |
2572 | if ((target_mask == (1 << tmp->device->id)) && (lun == tmp->device->lun) | |
2573 | ) { | |
2574 | if (prev) { | |
2575 | REMOVE(prev, prev->host_scribble, tmp, tmp->host_scribble); | |
2576 | prev->host_scribble = tmp->host_scribble; | |
2577 | } else { | |
2578 | REMOVE(-1, hostdata->disconnected_queue, tmp, tmp->host_scribble); | |
2579 | hostdata->disconnected_queue = (Scsi_Cmnd *) tmp->host_scribble; | |
2580 | } | |
2581 | tmp->host_scribble = NULL; | |
2582 | break; | |
2583 | } | |
2584 | if (!tmp) { | |
2585 | printk(KERN_ERR "scsi%d : warning : target bitmask %02x lun %d not in disconnect_queue.\n", instance->host_no, target_mask, lun); | |
2586 | /* | |
2587 | * Since we have an established nexus that we can't do anything with, | |
2588 | * we must abort it. | |
2589 | */ | |
2590 | abort = 1; | |
2591 | } | |
2592 | } | |
2593 | ||
2594 | if (abort) { | |
2595 | do_abort(instance); | |
2596 | } else { | |
2597 | hostdata->connected = tmp; | |
4dde8f7d | 2598 | dprintk(NDEBUG_RESELECTION, ("scsi%d : nexus established, target = %d, lun = %d, tag = %d\n", instance->host_no, tmp->device->id, tmp->device->lun, tmp->tag)); |
1da177e4 LT |
2599 | } |
2600 | } | |
2601 | ||
2602 | /* | |
2603 | * Function : void NCR5380_dma_complete (struct Scsi_Host *instance) | |
2604 | * | |
2605 | * Purpose : called by interrupt handler when DMA finishes or a phase | |
2606 | * mismatch occurs (which would finish the DMA transfer). | |
2607 | * | |
2608 | * Inputs : instance - this instance of the NCR5380. | |
2609 | * | |
2610 | * Returns : pointer to the Scsi_Cmnd structure for which the I_T_L | |
2611 | * nexus has been reestablished, on failure NULL is returned. | |
2612 | */ | |
2613 | ||
2614 | #ifdef REAL_DMA | |
2615 | static void NCR5380_dma_complete(NCR5380_instance * instance) { | |
2616 | NCR5380_local_declare(); | |
f8343685 | 2617 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata; |
1da177e4 LT |
2618 | int transferred; |
2619 | NCR5380_setup(instance); | |
2620 | ||
2621 | /* | |
2622 | * XXX this might not be right. | |
2623 | * | |
2624 | * Wait for final byte to transfer, ie wait for ACK to go false. | |
2625 | * | |
2626 | * We should use the Last Byte Sent bit, unfortunately this is | |
2627 | * not available on the 5380/5381 (only the various CMOS chips) | |
2628 | * | |
2629 | * FIXME: timeout, and need to handle long timeout/irq case | |
2630 | */ | |
2631 | ||
2632 | NCR5380_poll_politely(instance, BUS_AND_STATUS_REG, BASR_ACK, 0, 5*HZ); | |
2633 | ||
2634 | NCR5380_write(MODE_REG, MR_BASE); | |
2635 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_BASE); | |
2636 | ||
2637 | /* | |
2638 | * The only places we should see a phase mismatch and have to send | |
2639 | * data from the same set of pointers will be the data transfer | |
2640 | * phases. So, residual, requested length are only important here. | |
2641 | */ | |
2642 | ||
2643 | if (!(hostdata->connected->SCp.phase & SR_CD)) { | |
2644 | transferred = instance->dmalen - NCR5380_dma_residual(); | |
2645 | hostdata->connected->SCp.this_residual -= transferred; | |
2646 | hostdata->connected->SCp.ptr += transferred; | |
2647 | } | |
2648 | } | |
2649 | #endif /* def REAL_DMA */ | |
2650 | ||
2651 | /* | |
2652 | * Function : int NCR5380_abort (Scsi_Cmnd *cmd) | |
2653 | * | |
2654 | * Purpose : abort a command | |
2655 | * | |
2656 | * Inputs : cmd - the Scsi_Cmnd to abort, code - code to set the | |
2657 | * host byte of the result field to, if zero DID_ABORTED is | |
2658 | * used. | |
2659 | * | |
2660 | * Returns : 0 - success, -1 on failure. | |
2661 | * | |
2662 | * XXX - there is no way to abort the command that is currently | |
2663 | * connected, you have to wait for it to complete. If this is | |
2664 | * a problem, we could implement longjmp() / setjmp(), setjmp() | |
2665 | * called where the loop started in NCR5380_main(). | |
2666 | * | |
2667 | * Locks: host lock taken by caller | |
2668 | */ | |
2669 | ||
2670 | static int NCR5380_abort(Scsi_Cmnd * cmd) { | |
2671 | NCR5380_local_declare(); | |
2672 | struct Scsi_Host *instance = cmd->device->host; | |
2673 | struct NCR5380_hostdata *hostdata = (struct NCR5380_hostdata *) instance->hostdata; | |
2674 | Scsi_Cmnd *tmp, **prev; | |
2675 | ||
2676 | printk(KERN_WARNING "scsi%d : aborting command\n", instance->host_no); | |
db9dff36 | 2677 | scsi_print_command(cmd); |
1da177e4 LT |
2678 | |
2679 | NCR5380_print_status(instance); | |
2680 | ||
2681 | NCR5380_setup(instance); | |
2682 | ||
2683 | dprintk(NDEBUG_ABORT, ("scsi%d : abort called\n", instance->host_no)); | |
2684 | dprintk(NDEBUG_ABORT, (" basr 0x%X, sr 0x%X\n", NCR5380_read(BUS_AND_STATUS_REG), NCR5380_read(STATUS_REG))); | |
2685 | ||
2686 | #if 0 | |
2687 | /* | |
2688 | * Case 1 : If the command is the currently executing command, | |
2689 | * we'll set the aborted flag and return control so that | |
2690 | * information transfer routine can exit cleanly. | |
2691 | */ | |
2692 | ||
2693 | if (hostdata->connected == cmd) { | |
2694 | dprintk(NDEBUG_ABORT, ("scsi%d : aborting connected command\n", instance->host_no)); | |
2695 | hostdata->aborted = 1; | |
2696 | /* | |
2697 | * We should perform BSY checking, and make sure we haven't slipped | |
2698 | * into BUS FREE. | |
2699 | */ | |
2700 | ||
2701 | NCR5380_write(INITIATOR_COMMAND_REG, ICR_ASSERT_ATN); | |
2702 | /* | |
2703 | * Since we can't change phases until we've completed the current | |
2704 | * handshake, we have to source or sink a byte of data if the current | |
2705 | * phase is not MSGOUT. | |
2706 | */ | |
2707 | ||
2708 | /* | |
2709 | * Return control to the executing NCR drive so we can clear the | |
2710 | * aborted flag and get back into our main loop. | |
2711 | */ | |
2712 | ||
2713 | return 0; | |
2714 | } | |
2715 | #endif | |
2716 | ||
2717 | /* | |
2718 | * Case 2 : If the command hasn't been issued yet, we simply remove it | |
2719 | * from the issue queue. | |
2720 | */ | |
2721 | ||
2722 | dprintk(NDEBUG_ABORT, ("scsi%d : abort going into loop.\n", instance->host_no)); | |
2723 | for (prev = (Scsi_Cmnd **) & (hostdata->issue_queue), tmp = (Scsi_Cmnd *) hostdata->issue_queue; tmp; prev = (Scsi_Cmnd **) & (tmp->host_scribble), tmp = (Scsi_Cmnd *) tmp->host_scribble) | |
2724 | if (cmd == tmp) { | |
2725 | REMOVE(5, *prev, tmp, tmp->host_scribble); | |
2726 | (*prev) = (Scsi_Cmnd *) tmp->host_scribble; | |
2727 | tmp->host_scribble = NULL; | |
2728 | tmp->result = DID_ABORT << 16; | |
2729 | dprintk(NDEBUG_ABORT, ("scsi%d : abort removed command from issue queue.\n", instance->host_no)); | |
cce99c69 | 2730 | tmp->scsi_done(tmp); |
1da177e4 LT |
2731 | return SUCCESS; |
2732 | } | |
2733 | #if (NDEBUG & NDEBUG_ABORT) | |
2734 | /* KLL */ | |
2735 | else if (prev == tmp) | |
2736 | printk(KERN_ERR "scsi%d : LOOP\n", instance->host_no); | |
2737 | #endif | |
2738 | ||
2739 | /* | |
2740 | * Case 3 : If any commands are connected, we're going to fail the abort | |
2741 | * and let the high level SCSI driver retry at a later time or | |
2742 | * issue a reset. | |
2743 | * | |
2744 | * Timeouts, and therefore aborted commands, will be highly unlikely | |
2745 | * and handling them cleanly in this situation would make the common | |
2746 | * case of noresets less efficient, and would pollute our code. So, | |
2747 | * we fail. | |
2748 | */ | |
2749 | ||
2750 | if (hostdata->connected) { | |
2751 | dprintk(NDEBUG_ABORT, ("scsi%d : abort failed, command connected.\n", instance->host_no)); | |
2752 | return FAILED; | |
2753 | } | |
2754 | /* | |
2755 | * Case 4: If the command is currently disconnected from the bus, and | |
2756 | * there are no connected commands, we reconnect the I_T_L or | |
2757 | * I_T_L_Q nexus associated with it, go into message out, and send | |
2758 | * an abort message. | |
2759 | * | |
2760 | * This case is especially ugly. In order to reestablish the nexus, we | |
2761 | * need to call NCR5380_select(). The easiest way to implement this | |
2762 | * function was to abort if the bus was busy, and let the interrupt | |
2763 | * handler triggered on the SEL for reselect take care of lost arbitrations | |
2764 | * where necessary, meaning interrupts need to be enabled. | |
2765 | * | |
2766 | * When interrupts are enabled, the queues may change - so we | |
2767 | * can't remove it from the disconnected queue before selecting it | |
2768 | * because that could cause a failure in hashing the nexus if that | |
2769 | * device reselected. | |
2770 | * | |
2771 | * Since the queues may change, we can't use the pointers from when we | |
2772 | * first locate it. | |
2773 | * | |
2774 | * So, we must first locate the command, and if NCR5380_select() | |
2775 | * succeeds, then issue the abort, relocate the command and remove | |
2776 | * it from the disconnected queue. | |
2777 | */ | |
2778 | ||
2779 | for (tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp; tmp = (Scsi_Cmnd *) tmp->host_scribble) | |
2780 | if (cmd == tmp) { | |
2781 | dprintk(NDEBUG_ABORT, ("scsi%d : aborting disconnected command.\n", instance->host_no)); | |
2782 | ||
2783 | if (NCR5380_select(instance, cmd, (int) cmd->tag)) | |
2784 | return FAILED; | |
2785 | dprintk(NDEBUG_ABORT, ("scsi%d : nexus reestablished.\n", instance->host_no)); | |
2786 | ||
2787 | do_abort(instance); | |
2788 | ||
2789 | for (prev = (Scsi_Cmnd **) & (hostdata->disconnected_queue), tmp = (Scsi_Cmnd *) hostdata->disconnected_queue; tmp; prev = (Scsi_Cmnd **) & (tmp->host_scribble), tmp = (Scsi_Cmnd *) tmp->host_scribble) | |
2790 | if (cmd == tmp) { | |
2791 | REMOVE(5, *prev, tmp, tmp->host_scribble); | |
2792 | *prev = (Scsi_Cmnd *) tmp->host_scribble; | |
2793 | tmp->host_scribble = NULL; | |
2794 | tmp->result = DID_ABORT << 16; | |
cce99c69 | 2795 | tmp->scsi_done(tmp); |
1da177e4 LT |
2796 | return SUCCESS; |
2797 | } | |
2798 | } | |
2799 | /* | |
2800 | * Case 5 : If we reached this point, the command was not found in any of | |
2801 | * the queues. | |
2802 | * | |
2803 | * We probably reached this point because of an unlikely race condition | |
2804 | * between the command completing successfully and the abortion code, | |
2805 | * so we won't panic, but we will notify the user in case something really | |
2806 | * broke. | |
2807 | */ | |
2808 | printk(KERN_WARNING "scsi%d : warning : SCSI command probably completed successfully\n" | |
2809 | " before abortion\n", instance->host_no); | |
2810 | return FAILED; | |
2811 | } | |
2812 | ||
2813 | ||
2814 | /* | |
2815 | * Function : int NCR5380_bus_reset (Scsi_Cmnd *cmd) | |
2816 | * | |
2817 | * Purpose : reset the SCSI bus. | |
2818 | * | |
2819 | * Returns : SUCCESS | |
2820 | * | |
2821 | * Locks: host lock taken by caller | |
2822 | */ | |
2823 | ||
68b3aa7c JG |
2824 | static int NCR5380_bus_reset(Scsi_Cmnd * cmd) |
2825 | { | |
2826 | struct Scsi_Host *instance = cmd->device->host; | |
2827 | ||
1da177e4 | 2828 | NCR5380_local_declare(); |
68b3aa7c JG |
2829 | NCR5380_setup(instance); |
2830 | NCR5380_print_status(instance); | |
2831 | ||
2832 | spin_lock_irq(instance->host_lock); | |
2833 | do_reset(instance); | |
2834 | spin_unlock_irq(instance->host_lock); | |
1da177e4 | 2835 | |
1da177e4 LT |
2836 | return SUCCESS; |
2837 | } |