ncr5380.c revision 1.7 1 1.7 leo /* $NetBSD: ncr5380.c,v 1.7 1995/09/05 07:02:21 leo Exp $ */
2 1.1 leo
3 1.1 leo /*
4 1.1 leo * Copyright (c) 1995 Leo Weppelman.
5 1.1 leo * All rights reserved.
6 1.1 leo *
7 1.1 leo * Redistribution and use in source and binary forms, with or without
8 1.1 leo * modification, are permitted provided that the following conditions
9 1.1 leo * are met:
10 1.1 leo * 1. Redistributions of source code must retain the above copyright
11 1.1 leo * notice, this list of conditions and the following disclaimer.
12 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 leo * notice, this list of conditions and the following disclaimer in the
14 1.1 leo * documentation and/or other materials provided with the distribution.
15 1.1 leo * 3. All advertising materials mentioning features or use of this software
16 1.1 leo * must display the following acknowledgement:
17 1.1 leo * This product includes software developed by Leo Weppelman.
18 1.1 leo * 4. The name of the author may not be used to endorse or promote products
19 1.1 leo * derived from this software without specific prior written permission
20 1.1 leo *
21 1.1 leo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 1.1 leo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.1 leo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 1.1 leo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 1.1 leo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 1.1 leo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 1.1 leo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 1.1 leo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 1.1 leo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 1.1 leo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 1.1 leo */
32 1.1 leo
33 1.1 leo
34 1.1 leo #ifdef DBG_NOSTATIC
35 1.1 leo # define static
36 1.1 leo #endif
37 1.1 leo #ifdef DBG_SEL
38 1.1 leo # define DBG_SELPRINT(a,b) printf(a,b)
39 1.1 leo #else
40 1.1 leo # define DBG_SELPRINT(a,b)
41 1.1 leo #endif
42 1.1 leo #ifdef DBG_PIO
43 1.1 leo # define DBG_PIOPRINT(a,b,c) printf(a,b,c)
44 1.1 leo #else
45 1.1 leo # define DBG_PIOPRINT(a,b,c)
46 1.1 leo #endif
47 1.1 leo #ifdef DBG_INF
48 1.1 leo # define DBG_INFPRINT(a,b,c) a(b,c)
49 1.1 leo #else
50 1.1 leo # define DBG_INFPRINT(a,b,c)
51 1.1 leo #endif
52 1.1 leo #ifdef DBG_PID
53 1.4 leo static char *last_hit = NULL, *olast_hit = NULL;
54 1.4 leo # define PID(a) olast_hit = last_hit; last_hit = a;
55 1.1 leo #else
56 1.1 leo # define PID(a)
57 1.1 leo #endif
58 1.1 leo
59 1.6 leo /*
60 1.6 leo * Bit mask of targets you want debugging to be shown
61 1.6 leo */
62 1.6 leo u_char dbg_target_mask = 0x7f;
63 1.1 leo
64 1.1 leo /*
65 1.1 leo * Set bit for target when parity checking must be disabled.
66 1.1 leo * My (LWP) Maxtor 7245S seems to generate parity errors on about 50%
67 1.1 leo * of all transfers while the data is correct!?
68 1.1 leo */
69 1.4 leo u_char ncr5380_no_parchk = 0xff;
70 1.1 leo
71 1.1 leo /*
72 1.1 leo * This is the default sense-command we send.
73 1.1 leo */
74 1.1 leo static u_char sense_cmd[] = {
75 1.6 leo REQUEST_SENSE, 0, 0, 0, sizeof(struct scsi_sense_data), 0
76 1.1 leo };
77 1.1 leo
78 1.1 leo /*
79 1.1 leo * True if the main co-routine is running
80 1.1 leo */
81 1.1 leo static volatile int main_running = 0;
82 1.1 leo
83 1.1 leo /*
84 1.1 leo * Mask of targets selected
85 1.1 leo */
86 1.4 leo static u_char busy;
87 1.1 leo
88 1.5 mycroft static void ncr5380_minphys(struct buf *bp);
89 1.1 leo static int ncr5380_scsi_cmd(struct scsi_xfer *xs);
90 1.1 leo static int ncr5380_show_scsi_cmd(struct scsi_xfer *xs);
91 1.1 leo
92 1.1 leo struct scsi_adapter ncr5380_switch = {
93 1.1 leo ncr5380_scsi_cmd, /* scsi_cmd() */
94 1.1 leo ncr5380_minphys, /* scsi_minphys() */
95 1.1 leo 0, /* open_target_lu() */
96 1.1 leo 0 /* close_target_lu() */
97 1.1 leo };
98 1.1 leo
99 1.1 leo struct scsi_device ncr5380_dev = {
100 1.1 leo NULL, /* use default error handler */
101 1.1 leo NULL, /* do not have a start functio */
102 1.1 leo NULL, /* have no async handler */
103 1.1 leo NULL /* Use default done routine */
104 1.1 leo };
105 1.1 leo
106 1.1 leo
107 1.1 leo static SC_REQ req_queue[NREQ];
108 1.1 leo static SC_REQ *free_head = NULL; /* Free request structures */
109 1.4 leo
110 1.1 leo
111 1.1 leo /*
112 1.1 leo * Inline functions:
113 1.1 leo */
114 1.1 leo
115 1.1 leo /*
116 1.1 leo * Determine the size of a SCSI command.
117 1.1 leo */
118 1.1 leo extern __inline__ int command_size(opcode)
119 1.1 leo u_char opcode;
120 1.1 leo {
121 1.4 leo switch ((opcode >> 4) & 0xf) {
122 1.1 leo case 0:
123 1.1 leo case 1:
124 1.4 leo return (6);
125 1.1 leo case 2:
126 1.1 leo case 3:
127 1.4 leo return (10);
128 1.1 leo }
129 1.4 leo return (12);
130 1.1 leo }
131 1.1 leo
132 1.1 leo
133 1.1 leo /*
134 1.1 leo * Wait for request-line to become active. When it doesn't return 0.
135 1.1 leo * Otherwise return != 0.
136 1.1 leo * The timeouts in the 'wait_req_*' functions are arbitrary and rather
137 1.1 leo * large. In 99% of the invocations nearly no timeout is needed but in
138 1.1 leo * some cases (especially when using my tapedrive, a Tandberg 3600) the
139 1.1 leo * device is busy internally and the first SCSI-phase will be delayed.
140 1.1 leo */
141 1.1 leo extern __inline__ int wait_req_true(void)
142 1.1 leo {
143 1.1 leo int timeout = 25000;
144 1.1 leo
145 1.4 leo while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
146 1.1 leo delay(1);
147 1.4 leo return (GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ);
148 1.1 leo }
149 1.1 leo
150 1.1 leo /*
151 1.1 leo * Wait for request-line to become inactive. When it doesn't return 0.
152 1.1 leo * Otherwise return != 0.
153 1.1 leo */
154 1.1 leo extern __inline__ int wait_req_false(void)
155 1.1 leo {
156 1.1 leo int timeout = 25000;
157 1.1 leo
158 1.4 leo while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ) && --timeout)
159 1.1 leo delay(1);
160 1.4 leo return (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_REQ));
161 1.1 leo }
162 1.1 leo
163 1.1 leo extern __inline__ void finish_req(SC_REQ *reqp)
164 1.1 leo {
165 1.1 leo int sps;
166 1.1 leo struct scsi_xfer *xs = reqp->xs;
167 1.1 leo
168 1.4 leo #ifdef REAL_DMA
169 1.4 leo /*
170 1.4 leo * If we bounced, free the bounce buffer
171 1.4 leo */
172 1.4 leo if (reqp->dr_flag & DRIVER_BOUNCING)
173 1.4 leo free_bounceb(reqp->bounceb);
174 1.4 leo #endif /* REAL_DMA */
175 1.6 leo #ifdef DBG_REQ
176 1.6 leo if (dbg_target_mask & (1 << reqp->targ_id))
177 1.6 leo show_request(reqp, "DONE");
178 1.6 leo #endif
179 1.6 leo #ifdef DBG_ERR_RET
180 1.6 leo if (reqp->xs->error != 0)
181 1.6 leo show_request(reqp, "ERR_RET");
182 1.6 leo #endif
183 1.4 leo
184 1.1 leo /*
185 1.1 leo * Return request to free-q
186 1.1 leo */
187 1.1 leo sps = splbio();
188 1.1 leo reqp->next = free_head;
189 1.1 leo free_head = reqp;
190 1.1 leo splx(sps);
191 1.1 leo
192 1.1 leo xs->flags |= ITSDONE;
193 1.1 leo scsi_done(xs);
194 1.1 leo }
195 1.1 leo
196 1.1 leo /*
197 1.1 leo * Auto config stuff....
198 1.1 leo */
199 1.4 leo int ncr_cprint __P((void *auxp, char *));
200 1.1 leo void ncr_attach __P((struct device *, struct device *, void *));
201 1.1 leo int ncr_match __P((struct device *, struct cfdata *, void *));
202 1.1 leo
203 1.4 leo /*
204 1.4 leo * Tricks to make driver-name configurable
205 1.4 leo */
206 1.4 leo #define CFNAME(n) __CONCAT(n,cd)
207 1.4 leo #define CFSTRING(n) __STRING(n)
208 1.4 leo
209 1.4 leo struct cfdriver CFNAME(DRNAME) = {
210 1.4 leo NULL, CFSTRING(DRNAME), (cfmatch_t)ncr_match, ncr_attach,
211 1.1 leo DV_DULL, sizeof(struct ncr_softc), NULL, 0 };
212 1.1 leo
213 1.1 leo int
214 1.1 leo ncr_match(pdp, cdp, auxp)
215 1.1 leo struct device *pdp;
216 1.1 leo struct cfdata *cdp;
217 1.1 leo void *auxp;
218 1.1 leo {
219 1.4 leo return (machine_match(pdp, cdp, auxp, &CFNAME(DRNAME)));
220 1.1 leo }
221 1.1 leo
222 1.1 leo void
223 1.1 leo ncr_attach(pdp, dp, auxp)
224 1.1 leo struct device *pdp, *dp;
225 1.1 leo void *auxp;
226 1.1 leo {
227 1.1 leo struct ncr_softc *sc;
228 1.4 leo int i;
229 1.1 leo
230 1.1 leo sc = (struct ncr_softc *)dp;
231 1.1 leo
232 1.1 leo sc->sc_link.adapter_softc = sc;
233 1.1 leo sc->sc_link.adapter_target = 7;
234 1.1 leo sc->sc_link.adapter = &ncr5380_switch;
235 1.1 leo sc->sc_link.device = &ncr5380_dev;
236 1.1 leo sc->sc_link.openings = NREQ - 1;
237 1.1 leo
238 1.1 leo /*
239 1.4 leo * Initialize machine-type specific things...
240 1.1 leo */
241 1.4 leo scsi_mach_init(sc);
242 1.4 leo printf("\n");
243 1.1 leo
244 1.1 leo /*
245 1.1 leo * Initialize request queue freelist.
246 1.1 leo */
247 1.4 leo for (i = 0; i < NREQ; i++) {
248 1.1 leo req_queue[i].next = free_head;
249 1.1 leo free_head = &req_queue[i];
250 1.1 leo }
251 1.1 leo
252 1.1 leo /*
253 1.1 leo * Initialize the host adapter
254 1.1 leo */
255 1.1 leo scsi_idisable();
256 1.4 leo ENABLE_NCR5380(sc);
257 1.4 leo SET_5380_REG(NCR5380_ICOM, 0);
258 1.4 leo SET_5380_REG(NCR5380_MODE, IMODE_BASE);
259 1.4 leo SET_5380_REG(NCR5380_TCOM, 0);
260 1.4 leo SET_5380_REG(NCR5380_IDSTAT, 0);
261 1.4 leo scsi_ienable();
262 1.1 leo
263 1.1 leo /*
264 1.1 leo * attach all scsi units on us
265 1.1 leo */
266 1.4 leo config_found(dp, &sc->sc_link, ncr_cprint);
267 1.1 leo }
268 1.1 leo
269 1.1 leo /*
270 1.1 leo * print diag if name is NULL else just extra
271 1.1 leo */
272 1.1 leo int
273 1.4 leo ncr_cprint(auxp, name)
274 1.1 leo void *auxp;
275 1.1 leo char *name;
276 1.1 leo {
277 1.4 leo if (name == NULL)
278 1.4 leo return (UNCONF);
279 1.4 leo return (QUIET);
280 1.1 leo }
281 1.1 leo /*
282 1.1 leo * End of auto config stuff....
283 1.1 leo */
284 1.1 leo
285 1.1 leo /*
286 1.1 leo * Carry out a request from the high level driver.
287 1.1 leo */
288 1.1 leo static int
289 1.1 leo ncr5380_scsi_cmd(struct scsi_xfer *xs)
290 1.1 leo {
291 1.1 leo int sps;
292 1.1 leo SC_REQ *reqp;
293 1.1 leo int flags = xs->flags;
294 1.1 leo
295 1.1 leo /*
296 1.1 leo * We do not queue RESET commands
297 1.1 leo */
298 1.4 leo if (flags & SCSI_RESET) {
299 1.4 leo scsi_reset(xs->sc_link->adapter_softc);
300 1.4 leo return (COMPLETE);
301 1.1 leo }
302 1.1 leo
303 1.1 leo /*
304 1.1 leo * Get a request block
305 1.1 leo */
306 1.1 leo sps = splbio();
307 1.4 leo if ((reqp = free_head) == 0) {
308 1.1 leo splx(sps);
309 1.4 leo return (TRY_AGAIN_LATER);
310 1.1 leo }
311 1.1 leo free_head = reqp->next;
312 1.1 leo reqp->next = NULL;
313 1.1 leo splx(sps);
314 1.1 leo
315 1.1 leo /*
316 1.1 leo * Initialize our private fields
317 1.1 leo */
318 1.4 leo reqp->dr_flag = (flags & SCSI_POLL) ? DRIVER_NOINT : 0;
319 1.1 leo reqp->phase = NR_PHASE;
320 1.1 leo reqp->msgout = MSG_NOOP;
321 1.1 leo reqp->status = SCSGOOD;
322 1.1 leo reqp->link = NULL;
323 1.1 leo reqp->xs = xs;
324 1.1 leo reqp->targ_id = xs->sc_link->target;
325 1.4 leo reqp->targ_lun = xs->sc_link->lun;
326 1.1 leo reqp->xdata_ptr = (u_char*)xs->data;
327 1.1 leo reqp->xdata_len = xs->datalen;
328 1.1 leo memcpy(&reqp->xcmd, xs->cmd, sizeof(struct scsi_generic));
329 1.4 leo reqp->xcmd.bytes[0] |= reqp->targ_lun << 5;
330 1.1 leo
331 1.1 leo /*
332 1.4 leo * Sanity check on flags...
333 1.4 leo */
334 1.4 leo if (flags & ITSDONE) {
335 1.4 leo ncr_tprint(reqp, "scsi_cmd: command already done.....\n");
336 1.4 leo xs->flags &= ~ITSDONE;
337 1.4 leo }
338 1.4 leo if (!(flags & INUSE)) {
339 1.4 leo ncr_tprint(reqp, "scsi_cmd: command not in use.....\n");
340 1.4 leo xs->flags |= ~INUSE;
341 1.4 leo }
342 1.4 leo
343 1.4 leo #ifdef REAL_DMA
344 1.4 leo /*
345 1.1 leo * Check if DMA can be used on this request
346 1.1 leo */
347 1.4 leo if (scsi_dmaok(reqp))
348 1.1 leo reqp->dr_flag |= DRIVER_DMAOK;
349 1.4 leo #endif /* REAL_DMA */
350 1.1 leo
351 1.1 leo /*
352 1.1 leo * Insert the command into the issue queue. Note that 'REQUEST SENSE'
353 1.1 leo * commands are inserted at the head of the queue since any command
354 1.1 leo * will clear the existing contingent allegience condition and the sense
355 1.1 leo * data is only valid while the condition exists.
356 1.1 leo * When possible, link the command to a previous command to the same
357 1.1 leo * target. This is not very sensible when AUTO_SENSE is not defined!
358 1.1 leo * Interrupts are disabled while we are fiddling with the issue-queue.
359 1.1 leo */
360 1.1 leo sps = splbio();
361 1.4 leo if ((issue_q == NULL) || (reqp->xcmd.opcode == REQUEST_SENSE)) {
362 1.1 leo reqp->next = issue_q;
363 1.1 leo issue_q = reqp;
364 1.1 leo }
365 1.1 leo else {
366 1.1 leo SC_REQ *tmp, *link;
367 1.1 leo
368 1.1 leo tmp = issue_q;
369 1.1 leo link = NULL;
370 1.1 leo do {
371 1.4 leo if (!link && (tmp->targ_id == reqp->targ_id) && !tmp->link)
372 1.1 leo link = tmp;
373 1.4 leo } while (tmp->next && (tmp = tmp->next));
374 1.1 leo tmp->next = reqp;
375 1.1 leo #ifdef AUTO_SENSE
376 1.4 leo if (link) {
377 1.1 leo link->link = reqp;
378 1.1 leo link->xcmd.bytes[link->xs->cmdlen-1] |= 1;
379 1.1 leo }
380 1.1 leo #endif
381 1.1 leo }
382 1.1 leo splx(sps);
383 1.1 leo
384 1.1 leo #ifdef DBG_REQ
385 1.6 leo if (dbg_target_mask & (1 << reqp->targ_id))
386 1.6 leo show_request(reqp, (reqp->xcmd.opcode == REQUEST_SENSE) ?
387 1.6 leo "HEAD":"TAIL");
388 1.1 leo #endif
389 1.1 leo
390 1.4 leo run_main(xs->sc_link->adapter_softc);
391 1.1 leo
392 1.4 leo if (xs->flags & SCSI_POLL)
393 1.4 leo return (COMPLETE); /* We're booting */
394 1.4 leo return (SUCCESSFULLY_QUEUED);
395 1.1 leo }
396 1.1 leo
397 1.1 leo #define MIN_PHYS 65536 /*BARF!!!!*/
398 1.5 mycroft static void
399 1.1 leo ncr5380_minphys(struct buf *bp)
400 1.1 leo {
401 1.6 leo if (bp->b_bcount > MIN_PHYS)
402 1.1 leo bp->b_bcount = MIN_PHYS;
403 1.5 mycroft minphys(bp);
404 1.1 leo }
405 1.1 leo #undef MIN_PHYS
406 1.1 leo
407 1.1 leo static int
408 1.1 leo ncr5380_show_scsi_cmd(struct scsi_xfer *xs)
409 1.1 leo {
410 1.1 leo u_char *b = (u_char *) xs->cmd;
411 1.1 leo int i = 0;
412 1.1 leo
413 1.4 leo if (!(xs->flags & SCSI_RESET)) {
414 1.4 leo printf("(%d:%d:%d,0x%x)-", xs->sc_link->scsibus,
415 1.1 leo xs->sc_link->target, xs->sc_link->lun, xs->sc_link->flags);
416 1.4 leo while (i < xs->cmdlen) {
417 1.4 leo if (i)
418 1.1 leo printf(",");
419 1.1 leo printf("%x",b[i++]);
420 1.1 leo }
421 1.1 leo printf("-\n");
422 1.1 leo }
423 1.1 leo else {
424 1.4 leo printf("(%d:%d:%d)-RESET-\n",
425 1.1 leo xs->sc_link->scsibus,xs->sc_link->target, xs->sc_link->lun);
426 1.1 leo }
427 1.1 leo }
428 1.1 leo
429 1.1 leo /*
430 1.1 leo * The body of the driver.
431 1.1 leo */
432 1.1 leo static void
433 1.4 leo scsi_main(sc)
434 1.4 leo struct ncr_softc *sc;
435 1.1 leo {
436 1.1 leo SC_REQ *req, *prev;
437 1.1 leo int itype;
438 1.1 leo int sps;
439 1.1 leo
440 1.1 leo /*
441 1.1 leo * While running in the driver SCSI-interrupts are disabled.
442 1.1 leo */
443 1.1 leo scsi_idisable();
444 1.4 leo ENABLE_NCR5380(sc);
445 1.1 leo
446 1.4 leo PID("scsi_main1");
447 1.4 leo for (;;) {
448 1.1 leo sps = splbio();
449 1.4 leo if (!connected) {
450 1.4 leo /*
451 1.4 leo * Check if it is fair keep any exclusive access to DMA
452 1.4 leo * claimed. If not, stop queueing new jobs so the discon_q
453 1.4 leo * will be eventually drained and DMA can be given up.
454 1.4 leo */
455 1.4 leo if (!fair_to_keep_dma())
456 1.4 leo goto main_exit;
457 1.1 leo
458 1.1 leo /*
459 1.1 leo * Search through the issue-queue for a command
460 1.1 leo * destined for a target that isn't busy.
461 1.1 leo */
462 1.1 leo prev = NULL;
463 1.4 leo for (req=issue_q; req != NULL; prev = req, req = req->next) {
464 1.4 leo if (!(busy & (1 << req->targ_id))) {
465 1.1 leo /*
466 1.1 leo * Found one, remove it from the issue queue
467 1.1 leo */
468 1.4 leo if (prev == NULL)
469 1.1 leo issue_q = req->next;
470 1.1 leo else prev->next = req->next;
471 1.1 leo req->next = NULL;
472 1.1 leo break;
473 1.1 leo }
474 1.1 leo }
475 1.1 leo
476 1.1 leo /*
477 1.1 leo * When a request has just ended, we get here before an other
478 1.1 leo * device detects that the bus is free and that it can
479 1.1 leo * reconnect. The problem is that when this happens, we always
480 1.1 leo * baffle the device because our (initiator) id is higher. This
481 1.1 leo * can cause a sort of starvation on slow devices. So we check
482 1.1 leo * for a pending reselection here.
483 1.1 leo * Note that 'connected' will be non-null if the reselection
484 1.1 leo * succeeds.
485 1.1 leo */
486 1.4 leo if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
487 1.1 leo == (SC_S_SEL|SC_S_IO)){
488 1.4 leo if (req != NULL) {
489 1.1 leo req->next = issue_q;
490 1.1 leo issue_q = req;
491 1.1 leo }
492 1.1 leo splx(sps);
493 1.1 leo
494 1.4 leo reselect(sc);
495 1.4 leo scsi_clr_ipend();
496 1.1 leo goto connected;
497 1.1 leo }
498 1.1 leo
499 1.1 leo /*
500 1.1 leo * The host is not connected and there is no request
501 1.1 leo * pending, exit.
502 1.1 leo */
503 1.4 leo if (req == NULL) {
504 1.4 leo PID("scsi_main2");
505 1.1 leo goto main_exit;
506 1.1 leo }
507 1.1 leo
508 1.1 leo /*
509 1.1 leo * Re-enable interrupts before handling the request.
510 1.1 leo */
511 1.1 leo splx(sps);
512 1.1 leo
513 1.1 leo #ifdef DBG_REQ
514 1.6 leo if (dbg_target_mask & (1 << req->targ_id))
515 1.6 leo show_request(req, "TARGET");
516 1.1 leo #endif
517 1.1 leo /*
518 1.1 leo * We found a request. Try to connect to the target. If the
519 1.1 leo * initiator fails arbitration, the command is put back in the
520 1.1 leo * issue queue.
521 1.1 leo */
522 1.4 leo if (scsi_select(req, 0)) {
523 1.1 leo sps = splbio();
524 1.6 leo req->next = issue_q;
525 1.1 leo issue_q = req;
526 1.1 leo splx(sps);
527 1.1 leo #ifdef DBG_REQ
528 1.6 leo if (dbg_target_mask & (1 << req->targ_id))
529 1.6 leo ncr_tprint(req, "Select failed\n");
530 1.1 leo #endif
531 1.1 leo }
532 1.1 leo }
533 1.1 leo else splx(sps);
534 1.1 leo connected:
535 1.4 leo if (connected) {
536 1.1 leo /*
537 1.1 leo * If the host is currently connected but a 'real-dma' transfer
538 1.1 leo * is in progress, the 'end-of-dma' interrupt restarts main.
539 1.1 leo * So quit.
540 1.1 leo */
541 1.1 leo sps = splbio();
542 1.4 leo if (connected && (connected->dr_flag & DRIVER_IN_DMA)) {
543 1.4 leo PID("scsi_main3");
544 1.1 leo goto main_exit;
545 1.1 leo }
546 1.1 leo splx(sps);
547 1.1 leo
548 1.1 leo /*
549 1.1 leo * Let the target guide us through the bus-phases
550 1.1 leo */
551 1.4 leo while (information_transfer() == -1)
552 1.1 leo ;
553 1.1 leo }
554 1.1 leo }
555 1.1 leo /* NEVER TO REACH HERE */
556 1.4 leo panic("ncr5380-SCSI: not designed to come here");
557 1.1 leo
558 1.1 leo main_exit:
559 1.1 leo /*
560 1.1 leo * We enter here with interrupts disabled. We are about to exit main
561 1.1 leo * so interrupts should be re-enabled. Because interrupts are edge
562 1.1 leo * triggered, we could already have missed the interrupt. Therefore
563 1.1 leo * we check the IRQ-line here and re-enter when we really missed a
564 1.1 leo * valid interrupt.
565 1.1 leo */
566 1.4 leo PID("scsi_main4");
567 1.1 leo scsi_ienable();
568 1.4 leo SET_5380_REG(NCR5380_IDSTAT, SC_HOST_ID);
569 1.4 leo if (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
570 1.4 leo if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
571 1.1 leo scsi_idisable();
572 1.1 leo splx(sps);
573 1.1 leo
574 1.4 leo if (itype == INTR_RESEL)
575 1.4 leo reselect(sc);
576 1.4 leo #ifdef REAL_DMA
577 1.4 leo else dma_ready();
578 1.4 leo #else
579 1.4 leo else panic("Got DMA interrupt without DMA");
580 1.4 leo #endif
581 1.4 leo scsi_clr_ipend();
582 1.1 leo goto connected;
583 1.1 leo }
584 1.1 leo }
585 1.4 leo reconsider_dma();
586 1.4 leo
587 1.1 leo main_running = 0;
588 1.1 leo splx(sps);
589 1.4 leo PID("scsi_main5");
590 1.1 leo }
591 1.1 leo
592 1.4 leo #ifdef REAL_DMA
593 1.1 leo /*
594 1.1 leo * The SCSI-DMA interrupt.
595 1.1 leo * This interrupt can only be triggered when running in non-polled DMA
596 1.1 leo * mode. When DMA is not active, it will be silently ignored, it is usually
597 1.1 leo * to late because the EOP interrupt of the controller happens just a tiny
598 1.1 leo * bit earlier. It might become usefull when scatter/gather is implemented,
599 1.1 leo * because in that case only part of the DATAIN/DATAOUT transfer is taken
600 1.1 leo * out of a single buffer.
601 1.1 leo */
602 1.4 leo static void
603 1.4 leo ncr_dma_intr(sc)
604 1.4 leo struct ncr_softc *sc;
605 1.1 leo {
606 1.1 leo SC_REQ *reqp;
607 1.1 leo int dma_done;
608 1.1 leo
609 1.4 leo PID("ncr_dma_intr");
610 1.4 leo if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
611 1.1 leo scsi_idisable();
612 1.4 leo if (!(dma_done = dma_ready())) {
613 1.1 leo transfer_dma(reqp, reqp->phase, 0);
614 1.1 leo return;
615 1.1 leo }
616 1.4 leo run_main(sc);
617 1.1 leo }
618 1.1 leo }
619 1.4 leo #endif /* REAL_DMA */
620 1.1 leo
621 1.1 leo /*
622 1.1 leo * The SCSI-controller interrupt. This interrupt occurs on reselections and
623 1.4 leo * at the end of non-polled DMA-interrupts. It is assumed to be called from
624 1.4 leo * the machine-dependent hardware interrupt.
625 1.1 leo */
626 1.4 leo static void
627 1.4 leo ncr_ctrl_intr(sc)
628 1.4 leo struct ncr_softc *sc;
629 1.1 leo {
630 1.1 leo int itype;
631 1.1 leo int dma_done;
632 1.1 leo
633 1.4 leo while (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
634 1.1 leo scsi_idisable();
635 1.4 leo if ((itype = check_intr(sc)) != INTR_SPURIOUS) {
636 1.4 leo if (itype == INTR_RESEL)
637 1.4 leo reselect(sc);
638 1.1 leo else {
639 1.4 leo #ifdef REAL_DMA
640 1.4 leo if (!(dma_done = dma_ready())) {
641 1.1 leo transfer_dma(connected, connected->phase, 0);
642 1.1 leo return;
643 1.1 leo }
644 1.4 leo #else
645 1.4 leo panic("Got DMA interrupt without DMA\n");
646 1.4 leo #endif
647 1.1 leo }
648 1.4 leo scsi_clr_ipend();
649 1.1 leo }
650 1.4 leo run_main(sc);
651 1.1 leo return;
652 1.1 leo }
653 1.4 leo PID("ncr_ctrl_intr1");
654 1.1 leo }
655 1.1 leo
656 1.1 leo /*
657 1.1 leo * Initiate a connection path between the host and the target. The function
658 1.1 leo * first goes into arbitration for the SCSI-bus. When this succeeds, the target
659 1.1 leo * is selected and an 'IDENTIFY' message is send.
660 1.1 leo * Returns -1 when the arbitration failed. Otherwise 0 is returned. When
661 1.1 leo * the target does not respond (to either selection or 'MESSAGE OUT') the
662 1.1 leo * 'done' function is executed.
663 1.1 leo * The result code given by the driver can be influenced by setting 'code'
664 1.1 leo * to a non-zero value. This is the case when 'select' is called by abort.
665 1.1 leo */
666 1.1 leo static int
667 1.1 leo scsi_select(reqp, code)
668 1.1 leo SC_REQ *reqp;
669 1.1 leo {
670 1.4 leo u_long timeout;
671 1.4 leo u_char tmp[1];
672 1.4 leo u_char phase;
673 1.4 leo u_long cnt;
674 1.4 leo int sps;
675 1.4 leo struct ncr_softc *sc;
676 1.1 leo
677 1.4 leo sc = reqp->xs->sc_link->adapter_softc;
678 1.1 leo DBG_SELPRINT ("Starting arbitration\n", 0);
679 1.4 leo PID("scsi_select1");
680 1.1 leo
681 1.1 leo sps = splbio();
682 1.1 leo
683 1.1 leo /*
684 1.1 leo * Prevent a race condition here. If a reslection interrupt occurred
685 1.1 leo * between the decision to pick a new request and the call to select,
686 1.1 leo * we abort the selection.
687 1.1 leo * Interrupts are lowered when the 5380 is setup to arbitrate for the
688 1.1 leo * bus.
689 1.1 leo */
690 1.4 leo if (connected || (GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
691 1.1 leo splx(sps);
692 1.4 leo PID("scsi_select2");
693 1.4 leo return (-1);
694 1.1 leo }
695 1.1 leo
696 1.1 leo /*
697 1.1 leo * Set phase bits to 0, otherwise the 5380 won't drive the bus during
698 1.1 leo * selection.
699 1.1 leo */
700 1.4 leo SET_5380_REG(NCR5380_TCOM, 0);
701 1.4 leo SET_5380_REG(NCR5380_ICOM, 0);
702 1.1 leo
703 1.1 leo /*
704 1.1 leo * Arbitrate for the bus.
705 1.1 leo */
706 1.4 leo SET_5380_REG(NCR5380_DATA, SC_HOST_ID);
707 1.4 leo SET_5380_REG(NCR5380_MODE, SC_ARBIT);
708 1.1 leo
709 1.1 leo splx(sps);
710 1.1 leo
711 1.1 leo cnt = 10000;
712 1.4 leo while (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP) && --cnt)
713 1.1 leo delay(1);
714 1.1 leo
715 1.4 leo if (!(GET_5380_REG(NCR5380_ICOM) & SC_AIP)) {
716 1.4 leo SET_5380_REG(NCR5380_MODE, IMODE_BASE);
717 1.1 leo delay(1);
718 1.4 leo PID("scsi_select3");
719 1.1 leo
720 1.4 leo if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) {
721 1.1 leo /*
722 1.1 leo * Damn, we have a connected target that we don't know
723 1.1 leo * of. Some targets seem to respond to a selection
724 1.1 leo * AFTER the selection-timeout. Try to get the target
725 1.1 leo * into the Message-out phase so we can send an ABORT
726 1.1 leo * message. We try to avoid resetting the SCSI-bus!
727 1.1 leo */
728 1.4 leo if (!reach_msg_out(sc, sizeof(struct scsi_generic))) {
729 1.1 leo u_long len = 1;
730 1.1 leo u_char phase = PH_MSGOUT;
731 1.1 leo u_char msg = MSG_ABORT;
732 1.1 leo
733 1.1 leo transfer_pio(&phase, &msg, &len);
734 1.1 leo }
735 1.4 leo else if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)
736 1.4 leo scsi_reset(sc);
737 1.1 leo }
738 1.4 leo PID("scsi_select4");
739 1.4 leo return (-1);
740 1.1 leo }
741 1.1 leo
742 1.1 leo /* The arbitration delay is 2.2 usecs */
743 1.1 leo delay(3);
744 1.1 leo
745 1.1 leo /*
746 1.1 leo * Check the result of the arbitration. If we failed, return -1.
747 1.1 leo */
748 1.4 leo if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
749 1.1 leo /*
750 1.1 leo * The spec requires that we should read the data register to
751 1.1 leo * check for higher id's and check the SC_LA again.
752 1.1 leo */
753 1.4 leo tmp[0] = GET_5380_REG(NCR5380_DATA);
754 1.4 leo if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
755 1.4 leo SET_5380_REG(NCR5380_MODE, IMODE_BASE);
756 1.4 leo SET_5380_REG(NCR5380_ICOM, 0);
757 1.1 leo DBG_SELPRINT ("Arbitration lost,deassert SC_ARBIT\n",0);
758 1.4 leo PID("scsi_select5");
759 1.4 leo return (-1);
760 1.1 leo }
761 1.1 leo }
762 1.4 leo SET_5380_REG(NCR5380_ICOM, SC_A_SEL | SC_A_BSY);
763 1.4 leo if (GET_5380_REG(NCR5380_ICOM) & SC_LA) {
764 1.4 leo SET_5380_REG(NCR5380_MODE, IMODE_BASE);
765 1.4 leo SET_5380_REG(NCR5380_ICOM, 0);
766 1.1 leo DBG_SELPRINT ("Arbitration lost, deassert SC_A_SEL\n", 0);
767 1.4 leo PID("scsi_select6");
768 1.4 leo return (-1);
769 1.1 leo }
770 1.1 leo /* Bus settle delay + Bus clear delay = 1.2 usecs */
771 1.1 leo delay(2);
772 1.1 leo DBG_SELPRINT ("Arbitration complete\n", 0);
773 1.1 leo
774 1.1 leo /*
775 1.1 leo * Now that we won the arbitration, start the selection.
776 1.1 leo */
777 1.4 leo SET_5380_REG(NCR5380_DATA, SC_HOST_ID | (1 << reqp->targ_id));
778 1.1 leo
779 1.1 leo /*
780 1.1 leo * Raise ATN while SEL is true before BSY goes false from arbitration,
781 1.1 leo * since this is the only way to guarantee that we'll get a MESSAGE OUT
782 1.1 leo * phase immediately after the selection.
783 1.1 leo */
784 1.4 leo SET_5380_REG(NCR5380_ICOM, SC_A_BSY | SC_A_SEL | SC_A_ATN | SC_ADTB);
785 1.4 leo SET_5380_REG(NCR5380_MODE, IMODE_BASE);
786 1.1 leo
787 1.1 leo /*
788 1.1 leo * Turn off reselection interrupts
789 1.1 leo */
790 1.4 leo SET_5380_REG(NCR5380_IDSTAT, 0);
791 1.1 leo
792 1.1 leo /*
793 1.1 leo * Reset BSY. The delay following it, surpresses a glitch in the
794 1.1 leo * 5380 which causes us to see our own BSY signal instead of that of
795 1.1 leo * the target.
796 1.1 leo */
797 1.4 leo SET_5380_REG(NCR5380_ICOM, SC_A_SEL | SC_A_ATN | SC_ADTB);
798 1.1 leo delay(1);
799 1.1 leo
800 1.1 leo /*
801 1.1 leo * Wait for the target to react, the specs call for a timeout of
802 1.1 leo * 250 ms.
803 1.1 leo */
804 1.4 leo cnt = 25000 /* 250 */;
805 1.4 leo while (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) && --cnt)
806 1.4 leo delay(10);
807 1.1 leo
808 1.4 leo if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
809 1.1 leo /*
810 1.1 leo * There is no reaction from the target, start the selection
811 1.1 leo * timeout procedure. We release the databus but keep SEL
812 1.1 leo * asserted. After that we wait a 'selection abort time' (200
813 1.1 leo * usecs) and 2 deskew delays (90 ns) and check BSY again.
814 1.1 leo * When BSY is asserted, we assume the selection succeeded,
815 1.1 leo * otherwise we release the bus.
816 1.1 leo */
817 1.4 leo SET_5380_REG(NCR5380_ICOM, SC_A_SEL | SC_A_ATN);
818 1.1 leo delay(201);
819 1.4 leo if (!(GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY)) {
820 1.4 leo SET_5380_REG(NCR5380_ICOM, 0);
821 1.1 leo reqp->xs->error = code ? code : XS_SELTIMEOUT;
822 1.1 leo DBG_SELPRINT ("Target %d not responding to sel\n",
823 1.1 leo reqp->targ_id);
824 1.1 leo finish_req(reqp);
825 1.4 leo PID("scsi_select7");
826 1.4 leo return (0);
827 1.1 leo }
828 1.1 leo }
829 1.4 leo SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
830 1.1 leo
831 1.1 leo DBG_SELPRINT ("Target %d responding to select.\n", reqp->targ_id);
832 1.1 leo
833 1.1 leo /*
834 1.1 leo * The SCSI-interrupts are disabled while a request is being handled.
835 1.1 leo */
836 1.1 leo scsi_idisable();
837 1.1 leo
838 1.1 leo /*
839 1.1 leo * Since we followed the SCSI-spec and raised ATN while SEL was true
840 1.1 leo * but before BSY was false during the selection, a 'MESSAGE OUT'
841 1.1 leo * phase should follow. Here we send an 'IDENTIFY' message.
842 1.1 leo * Allow disconnect only when interrups are allowed.
843 1.1 leo */
844 1.4 leo tmp[0] = MSG_IDENTIFY(reqp->targ_lun,
845 1.4 leo (reqp->dr_flag & DRIVER_NOINT) ? 0 : 1);
846 1.1 leo cnt = 1;
847 1.1 leo phase = PH_MSGOUT;
848 1.4 leo if (transfer_pio(&phase, tmp, &cnt) || cnt) {
849 1.1 leo DBG_SELPRINT ("Target %d: failed to send identify\n",
850 1.1 leo reqp->targ_id);
851 1.1 leo /*
852 1.1 leo * Try to disconnect from the target. We cannot leave it just
853 1.1 leo * hanging here.
854 1.1 leo */
855 1.4 leo if (!reach_msg_out(sc, sizeof(struct scsi_generic))) {
856 1.1 leo u_long len = 1;
857 1.1 leo u_char phase = PH_MSGOUT;
858 1.1 leo u_char msg = MSG_ABORT;
859 1.1 leo
860 1.1 leo transfer_pio(&phase, &msg, &len);
861 1.1 leo }
862 1.4 leo else scsi_reset(sc);
863 1.1 leo
864 1.4 leo SET_5380_REG(NCR5380_ICOM, 0);
865 1.1 leo reqp->xs->error = code ? code : XS_DRIVER_STUFFUP;
866 1.1 leo finish_req(reqp);
867 1.4 leo PID("scsi_select8");
868 1.4 leo return (0);
869 1.1 leo }
870 1.1 leo reqp->phase = PH_MSGOUT;
871 1.1 leo
872 1.1 leo #ifdef notyet /* LWP: Do we need timeouts in the driver? */
873 1.1 leo /*
874 1.1 leo * Command is connected, start timer ticking.
875 1.1 leo */
876 1.1 leo ccb_p->xtimeout = ccb_p->timeout + Lbolt;
877 1.1 leo #endif
878 1.1 leo
879 1.1 leo connected = reqp;
880 1.1 leo busy |= 1 << reqp->targ_id;
881 1.4 leo PID("scsi_select9");
882 1.4 leo return (0);
883 1.1 leo }
884 1.1 leo
885 1.1 leo /*
886 1.1 leo * Return codes:
887 1.1 leo * -1: quit main, trigger on interrupt
888 1.1 leo * 0: keep on running main.
889 1.1 leo */
890 1.1 leo static int
891 1.1 leo information_transfer()
892 1.1 leo {
893 1.1 leo SC_REQ *reqp = connected;
894 1.1 leo u_char tmp, phase;
895 1.1 leo u_long len;
896 1.1 leo
897 1.4 leo PID("info_transf1");
898 1.1 leo /*
899 1.1 leo * Clear pending interrupts from 5380-chip.
900 1.1 leo */
901 1.4 leo scsi_clr_ipend();
902 1.1 leo
903 1.1 leo /*
904 1.1 leo * We only have a valid SCSI-phase when REQ is asserted. Something
905 1.1 leo * is deadly wrong when BSY has dropped.
906 1.1 leo */
907 1.4 leo tmp = GET_5380_REG(NCR5380_IDSTAT);
908 1.1 leo
909 1.4 leo if (!(tmp & SC_S_BSY)) {
910 1.1 leo busy &= ~(1 << reqp->targ_id);
911 1.1 leo connected = NULL;
912 1.1 leo reqp->xs->error = XS_BUSY;
913 1.1 leo finish_req(reqp);
914 1.4 leo PID("info_transf2");
915 1.4 leo return (0);
916 1.1 leo }
917 1.1 leo
918 1.4 leo if (tmp & SC_S_REQ) {
919 1.1 leo phase = (tmp >> 2) & 7;
920 1.4 leo if (phase != reqp->phase) {
921 1.1 leo reqp->phase = phase;
922 1.1 leo DBG_INFPRINT(show_phase, reqp, phase);
923 1.1 leo }
924 1.1 leo }
925 1.4 leo else return (-1);
926 1.1 leo
927 1.4 leo switch (phase) {
928 1.1 leo case PH_DATAOUT:
929 1.1 leo
930 1.1 leo #ifdef DBG_NOWRITE
931 1.4 leo ncr_tprint(reqp, "NOWRITE set -- write attempt aborted.");
932 1.1 leo reqp->msgout = MSG_ABORT;
933 1.4 leo SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
934 1.4 leo return (-1);
935 1.1 leo #endif /* DBG_NOWRITE */
936 1.4 leo /*
937 1.4 leo * If this is the first write using DMA, fill
938 1.4 leo * the bounce buffer.
939 1.4 leo */
940 1.4 leo if (reqp->xdata_ptr == reqp->xs->data) { /* XXX */
941 1.4 leo if (reqp->dr_flag & DRIVER_BOUNCING)
942 1.4 leo bcopy(reqp->xdata_ptr, reqp->bounceb, reqp->xdata_len);
943 1.4 leo }
944 1.1 leo
945 1.1 leo case PH_DATAIN:
946 1.1 leo #ifdef REAL_DMA
947 1.4 leo if (reqp->dr_flag & DRIVER_DMAOK) {
948 1.1 leo int poll = REAL_DMA_POLL|(reqp->dr_flag & DRIVER_NOINT);
949 1.1 leo transfer_dma(reqp, phase, poll);
950 1.4 leo if (!poll)
951 1.4 leo return (0);
952 1.1 leo }
953 1.1 leo else
954 1.1 leo #endif
955 1.1 leo {
956 1.4 leo PID("info_transf3");
957 1.1 leo len = reqp->xdata_len;
958 1.4 leo #ifdef USE_PDMA
959 1.7 leo if (transfer_pdma(&phase, reqp->xdata_ptr, &len) == 0)
960 1.7 leo return (0);
961 1.4 leo #else
962 1.1 leo transfer_pio(&phase, reqp->xdata_ptr, &len);
963 1.4 leo #endif
964 1.1 leo reqp->xdata_ptr += reqp->xdata_len - len;
965 1.1 leo reqp->xdata_len = len;
966 1.1 leo }
967 1.4 leo return (-1);
968 1.1 leo case PH_MSGIN:
969 1.1 leo /*
970 1.1 leo * We only expect single byte messages here.
971 1.1 leo */
972 1.1 leo len = 1;
973 1.1 leo transfer_pio(&phase, &tmp, &len);
974 1.1 leo reqp->message = tmp;
975 1.4 leo return (handle_message(reqp, tmp));
976 1.1 leo case PH_MSGOUT:
977 1.1 leo len = 1;
978 1.1 leo transfer_pio(&phase, &reqp->msgout, &len);
979 1.4 leo if (reqp->msgout == MSG_ABORT) {
980 1.1 leo busy &= ~(1 << reqp->targ_id);
981 1.1 leo connected = NULL;
982 1.1 leo reqp->xs->error = XS_DRIVER_STUFFUP;
983 1.1 leo finish_req(reqp);
984 1.4 leo PID("info_transf4");
985 1.4 leo return (0);
986 1.1 leo }
987 1.1 leo reqp->msgout = MSG_NOOP;
988 1.4 leo return (-1);
989 1.1 leo case PH_CMD :
990 1.1 leo len = command_size(reqp->xcmd.opcode);
991 1.1 leo transfer_pio(&phase, (u_char *)&reqp->xcmd, &len);
992 1.4 leo PID("info_transf5");
993 1.4 leo return (-1);
994 1.1 leo case PH_STATUS:
995 1.1 leo len = 1;
996 1.1 leo transfer_pio(&phase, &tmp, &len);
997 1.1 leo reqp->status = tmp;
998 1.4 leo PID("info_transf6");
999 1.4 leo return (-1);
1000 1.1 leo default :
1001 1.4 leo ncr_tprint(reqp, "Unknown phase\n");
1002 1.1 leo }
1003 1.4 leo PID("info_transf7");
1004 1.4 leo return (-1);
1005 1.1 leo }
1006 1.1 leo
1007 1.1 leo /*
1008 1.1 leo * Handle the message 'msg' send to us by the target.
1009 1.1 leo * Return values:
1010 1.1 leo * 0 : The current command has completed.
1011 1.1 leo * -1 : Get on to the next phase.
1012 1.1 leo */
1013 1.1 leo static int
1014 1.1 leo handle_message(reqp, msg)
1015 1.1 leo SC_REQ *reqp;
1016 1.1 leo u_int msg;
1017 1.1 leo {
1018 1.1 leo int sps;
1019 1.1 leo
1020 1.4 leo PID("hmessage1");
1021 1.4 leo switch (msg) {
1022 1.1 leo /*
1023 1.1 leo * Linking lets us reduce the time required to get
1024 1.1 leo * the next command to the device, skipping the arbitration
1025 1.1 leo * and selection time. In the current implementation,
1026 1.1 leo * we merely have to start the next command pointed
1027 1.1 leo * to by 'next_link'.
1028 1.1 leo */
1029 1.1 leo case MSG_LINK_CMD_COMPLETE:
1030 1.1 leo case MSG_LINK_CMD_COMPLETEF:
1031 1.4 leo if (reqp->link == NULL) {
1032 1.4 leo ncr_tprint(reqp, "No link for linked command");
1033 1.1 leo reqp->msgout = MSG_ABORT;
1034 1.4 leo SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1035 1.4 leo PID("hmessage2");
1036 1.4 leo return (-1);
1037 1.1 leo }
1038 1.1 leo reqp->xs->error = 0;
1039 1.1 leo
1040 1.1 leo #ifdef AUTO_SENSE
1041 1.4 leo if (check_autosense(reqp, 0) == -1)
1042 1.4 leo return (-1);
1043 1.1 leo #endif /* AUTO_SENSE */
1044 1.1 leo
1045 1.1 leo #ifdef DBG_REQ
1046 1.6 leo if (dbg_target_mask & (1 << reqp->targ_id))
1047 1.6 leo show_request(reqp->link, "LINK");
1048 1.1 leo #endif
1049 1.1 leo connected = reqp->link;
1050 1.1 leo finish_req(reqp);
1051 1.4 leo PID("hmessage3");
1052 1.4 leo return (-1);
1053 1.1 leo case MSG_ABORT:
1054 1.1 leo case MSG_CMDCOMPLETE:
1055 1.6 leo connected = NULL;
1056 1.1 leo busy &= ~(1 << reqp->targ_id);
1057 1.6 leo if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1058 1.1 leo reqp->xs->resid = reqp->xdata_len;
1059 1.1 leo reqp->xs->error = 0;
1060 1.6 leo }
1061 1.1 leo
1062 1.1 leo #ifdef AUTO_SENSE
1063 1.4 leo if (check_autosense(reqp, 0) == -1) {
1064 1.4 leo PID("hmessage4");
1065 1.4 leo return (0);
1066 1.1 leo }
1067 1.1 leo #endif /* AUTO_SENSE */
1068 1.1 leo
1069 1.1 leo finish_req(reqp);
1070 1.4 leo PID("hmessage5");
1071 1.4 leo return (0);
1072 1.1 leo case MSG_MESSAGE_REJECT:
1073 1.4 leo PID("hmessage6");
1074 1.4 leo return (-1);
1075 1.1 leo case MSG_DISCONNECT:
1076 1.1 leo #ifdef DBG_REQ
1077 1.6 leo if (dbg_target_mask & (1 << reqp->targ_id))
1078 1.6 leo show_request(reqp, "DISCON");
1079 1.1 leo #endif
1080 1.1 leo sps = splbio();
1081 1.1 leo connected = NULL;
1082 1.1 leo reqp->next = discon_q;
1083 1.1 leo discon_q = reqp;
1084 1.1 leo splx(sps);
1085 1.4 leo PID("hmessage7");
1086 1.4 leo return (0);
1087 1.1 leo case MSG_SAVEDATAPOINTER:
1088 1.1 leo case MSG_RESTOREPOINTERS:
1089 1.1 leo /*
1090 1.1 leo * We save pointers implicitely at disconnect.
1091 1.1 leo * So we can ignore these messages.
1092 1.1 leo */
1093 1.4 leo PID("hmessage8");
1094 1.4 leo return (-1);
1095 1.1 leo default:
1096 1.4 leo ncr_tprint(reqp, "Unkown message %x\n", msg);
1097 1.4 leo return (-1);
1098 1.1 leo }
1099 1.4 leo PID("hmessage9");
1100 1.4 leo return (-1);
1101 1.1 leo }
1102 1.1 leo
1103 1.1 leo /*
1104 1.1 leo * Handle reselection. If a valid reconnection occurs, connected
1105 1.1 leo * points at the reconnected command. The command is removed from the
1106 1.1 leo * disconnected queue.
1107 1.1 leo */
1108 1.1 leo static void
1109 1.4 leo reselect(sc)
1110 1.4 leo struct ncr_softc *sc;
1111 1.1 leo {
1112 1.1 leo u_char phase;
1113 1.1 leo u_long len;
1114 1.1 leo u_char msg;
1115 1.1 leo u_char target_mask;
1116 1.1 leo int abort = 0;
1117 1.1 leo SC_REQ *tmp, *prev;
1118 1.1 leo
1119 1.4 leo PID("reselect1");
1120 1.4 leo target_mask = GET_5380_REG(NCR5380_DATA) & ~SC_HOST_ID;
1121 1.1 leo
1122 1.1 leo /*
1123 1.1 leo * At this point, we have detected that our SCSI-id is on the bus,
1124 1.1 leo * SEL is true and BSY was false for at least one bus settle
1125 1.1 leo * delay (400 ns.).
1126 1.1 leo * We must assert BSY ourselves, until the target drops the SEL signal.
1127 1.7 leo * This should happen within 2 deskew delays (2 * 45ns.) We wait too
1128 1.7 leo * long for this, but it's better to wait for slow targets than to
1129 1.7 leo * reset the bus too early...
1130 1.1 leo */
1131 1.4 leo SET_5380_REG(NCR5380_ICOM, SC_A_BSY);
1132 1.7 leo len = 2;
1133 1.7 leo while ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) && (len > 0)) {
1134 1.7 leo delay(1);
1135 1.7 leo len--;
1136 1.7 leo }
1137 1.7 leo if (GET_5380_REG(NCR5380_IDSTAT) & SC_S_SEL) {
1138 1.7 leo /* Damn SEL isn't dropping */
1139 1.7 leo scsi_reset(sc);
1140 1.7 leo return;
1141 1.7 leo }
1142 1.7 leo
1143 1.4 leo SET_5380_REG(NCR5380_ICOM, 0);
1144 1.1 leo
1145 1.1 leo /*
1146 1.1 leo * Get the expected identify message.
1147 1.1 leo */
1148 1.1 leo phase = PH_MSGIN;
1149 1.1 leo len = 1;
1150 1.1 leo transfer_pio(&phase, &msg, &len);
1151 1.4 leo if (len || !MSG_ISIDENTIFY(msg)) {
1152 1.4 leo ncr_aprint(sc, "Expecting IDENTIFY, got 0x%x\n", msg);
1153 1.1 leo abort = 1;
1154 1.1 leo }
1155 1.1 leo else {
1156 1.1 leo /*
1157 1.1 leo * Find the command reconnecting
1158 1.1 leo */
1159 1.4 leo for (tmp = discon_q, prev = NULL; tmp; prev = tmp, tmp = tmp->next){
1160 1.4 leo if (target_mask == (1 << tmp->targ_id)) {
1161 1.4 leo if (prev)
1162 1.1 leo prev->next = tmp->next;
1163 1.1 leo else discon_q = tmp->next;
1164 1.1 leo tmp->next = NULL;
1165 1.1 leo break;
1166 1.1 leo }
1167 1.1 leo }
1168 1.4 leo if (tmp == NULL) {
1169 1.4 leo ncr_aprint(sc, "No disconnected job for targetmask %x\n",
1170 1.1 leo target_mask);
1171 1.4 leo abort = 1;
1172 1.1 leo }
1173 1.1 leo }
1174 1.4 leo if (abort) {
1175 1.1 leo msg = MSG_ABORT;
1176 1.1 leo len = 1;
1177 1.1 leo phase = PH_MSGOUT;
1178 1.1 leo
1179 1.4 leo SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1180 1.1 leo transfer_pio(&phase, &msg, &len);
1181 1.1 leo }
1182 1.1 leo else {
1183 1.1 leo connected = tmp;
1184 1.1 leo #ifdef DBG_REQ
1185 1.6 leo if (dbg_target_mask & (1 << tmp->targ_id))
1186 1.6 leo show_request(tmp, "RECON");
1187 1.1 leo #endif
1188 1.1 leo }
1189 1.4 leo PID("reselect2");
1190 1.1 leo }
1191 1.1 leo
1192 1.1 leo /*
1193 1.4 leo * Transfer data in a given phase using programmed I/O.
1194 1.1 leo * Returns -1 when a different phase is entered without transferring the
1195 1.1 leo * maximum number of bytes, 0 if all bytes or exit is in the same
1196 1.1 leo * phase.
1197 1.1 leo */
1198 1.1 leo static int
1199 1.1 leo transfer_pio(phase, data, len)
1200 1.1 leo u_char *phase;
1201 1.1 leo u_char *data;
1202 1.1 leo u_long *len;
1203 1.1 leo {
1204 1.1 leo u_int cnt = *len;
1205 1.1 leo u_char ph = *phase;
1206 1.1 leo u_char tmp;
1207 1.1 leo
1208 1.1 leo DBG_PIOPRINT ("SCSI: transfer_pio start: phase: %d, len: %d\n", ph,cnt);
1209 1.4 leo PID("tpio1");
1210 1.4 leo SET_5380_REG(NCR5380_TCOM, ph);
1211 1.1 leo do {
1212 1.4 leo if (!wait_req_true()) {
1213 1.1 leo DBG_PIOPRINT ("SCSI: transfer_pio: missing REQ\n", 0, 0);
1214 1.1 leo break;
1215 1.1 leo }
1216 1.4 leo if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != ph) {
1217 1.1 leo DBG_PIOPRINT ("SCSI: transfer_pio: phase mismatch\n", 0, 0);
1218 1.1 leo break;
1219 1.1 leo }
1220 1.4 leo if (PH_IN(ph)) {
1221 1.4 leo *data++ = GET_5380_REG(NCR5380_DATA);
1222 1.4 leo SET_5380_REG(NCR5380_ICOM, SC_A_ACK);
1223 1.1 leo }
1224 1.1 leo else {
1225 1.4 leo SET_5380_REG(NCR5380_DATA, *data++);
1226 1.1 leo
1227 1.1 leo /*
1228 1.1 leo * The SCSI-standard suggests that in the 'MESSAGE OUT' phase,
1229 1.1 leo * the initiator should drop ATN on the last byte of the
1230 1.1 leo * message phase after REQ has been asserted for the handshake
1231 1.1 leo * but before the initiator raises ACK.
1232 1.1 leo */
1233 1.4 leo if (!( (ph == PH_MSGOUT) && (cnt > 1) )) {
1234 1.4 leo SET_5380_REG(NCR5380_ICOM, SC_ADTB);
1235 1.4 leo SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ACK);
1236 1.1 leo }
1237 1.1 leo else {
1238 1.4 leo SET_5380_REG(NCR5380_ICOM, SC_ADTB | SC_A_ATN);
1239 1.4 leo SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ATN|SC_A_ACK);
1240 1.1 leo }
1241 1.1 leo }
1242 1.4 leo if (!wait_req_false()) {
1243 1.1 leo DBG_PIOPRINT ("SCSI: transfer_pio - REQ not dropping\n", 0, 0);
1244 1.1 leo break;
1245 1.1 leo }
1246 1.1 leo
1247 1.4 leo if (!( (ph == PH_MSGOUT) && (cnt > 1) ))
1248 1.4 leo SET_5380_REG(NCR5380_ICOM, 0);
1249 1.4 leo else SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1250 1.4 leo } while (--cnt);
1251 1.1 leo
1252 1.4 leo if ((tmp = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1253 1.1 leo *phase = (tmp >> 2) & 7;
1254 1.1 leo else *phase = NR_PHASE;
1255 1.1 leo *len = cnt;
1256 1.1 leo DBG_PIOPRINT ("SCSI: transfer_pio done: phase: %d, len: %d\n",
1257 1.1 leo *phase, cnt);
1258 1.4 leo PID("tpio2");
1259 1.4 leo if (!cnt || (*phase == ph))
1260 1.4 leo return (0);
1261 1.4 leo return (-1);
1262 1.1 leo }
1263 1.1 leo
1264 1.4 leo #ifdef REAL_DMA
1265 1.1 leo /*
1266 1.1 leo * Start a DMA-transfer on the device using the current pointers.
1267 1.4 leo * If 'poll' is true, the function busy-waits until DMA has completed.
1268 1.1 leo */
1269 1.1 leo static void
1270 1.1 leo transfer_dma(reqp, phase, poll)
1271 1.1 leo SC_REQ *reqp;
1272 1.1 leo u_int phase;
1273 1.1 leo int poll;
1274 1.1 leo {
1275 1.4 leo int dma_done;
1276 1.1 leo u_char mbase = 0;
1277 1.1 leo int sps;
1278 1.1 leo
1279 1.1 leo again:
1280 1.4 leo PID("tdma1");
1281 1.4 leo
1282 1.1 leo /*
1283 1.1 leo * We should be in phase, otherwise we are not allowed to
1284 1.1 leo * drive the bus.
1285 1.1 leo */
1286 1.4 leo SET_5380_REG(NCR5380_TCOM, phase);
1287 1.1 leo
1288 1.1 leo /*
1289 1.1 leo * Defer interrupts until DMA is fully running.
1290 1.1 leo */
1291 1.1 leo sps = splbio();
1292 1.1 leo
1293 1.1 leo /*
1294 1.1 leo * Clear pending interrupts and parity errors.
1295 1.1 leo */
1296 1.4 leo scsi_clr_ipend();
1297 1.1 leo
1298 1.4 leo if (!poll) {
1299 1.1 leo /*
1300 1.1 leo * Enable SCSI interrupts and set IN_DMA flag, set 'mbase'
1301 1.1 leo * to the interrupts we want enabled.
1302 1.1 leo */
1303 1.1 leo scsi_ienable();
1304 1.1 leo reqp->dr_flag |= DRIVER_IN_DMA;
1305 1.1 leo mbase = SC_E_EOPI | SC_MON_BSY;
1306 1.1 leo }
1307 1.4 leo else scsi_idisable();
1308 1.4 leo mbase |= IMODE_BASE | SC_M_DMA;
1309 1.4 leo scsi_dma_setup(reqp, phase, mbase);
1310 1.1 leo
1311 1.1 leo splx(sps);
1312 1.1 leo
1313 1.4 leo if (poll) {
1314 1.1 leo /*
1315 1.4 leo * On polled-dma transfers, we wait here until the
1316 1.4 leo * 'end-of-dma' condition occurs.
1317 1.4 leo */
1318 1.4 leo poll_edma(reqp);
1319 1.4 leo if (!(dma_done = dma_ready()))
1320 1.1 leo goto again;
1321 1.1 leo }
1322 1.4 leo PID("tdma2");
1323 1.1 leo }
1324 1.1 leo
1325 1.1 leo /*
1326 1.1 leo * Check results of a DMA data-transfer.
1327 1.1 leo */
1328 1.1 leo static int
1329 1.4 leo dma_ready()
1330 1.1 leo {
1331 1.1 leo SC_REQ *reqp = connected;
1332 1.4 leo int dmstat, is_edma;
1333 1.4 leo long bytes_left, bytes_done;
1334 1.1 leo
1335 1.4 leo is_edma = get_dma_result(reqp, &bytes_left);
1336 1.4 leo dmstat = GET_5380_REG(NCR5380_DMSTAT);
1337 1.1 leo
1338 1.1 leo /*
1339 1.1 leo * Check if the call is sensible and not caused by any spurious
1340 1.1 leo * interrupt.
1341 1.1 leo */
1342 1.4 leo if (!is_edma && !(dmstat & (SC_END_DMA|SC_BSY_ERR))
1343 1.4 leo && (dmstat & SC_PHS_MTCH) ) {
1344 1.4 leo ncr_tprint(reqp, "dma_ready: spurious call"
1345 1.4 leo "(dm:%x,last_hit: %s)\n",
1346 1.4 leo #ifdef DBG_PID
1347 1.4 leo dmstat, last_hit);
1348 1.4 leo #else
1349 1.4 leo dmstat, "unknown");
1350 1.4 leo #endif
1351 1.4 leo return (0);
1352 1.1 leo }
1353 1.1 leo
1354 1.1 leo /*
1355 1.4 leo * Clear all (pending) interrupts.
1356 1.1 leo */
1357 1.4 leo scsi_clr_ipend();
1358 1.1 leo
1359 1.1 leo /*
1360 1.1 leo * Update various transfer-pointers/lengths
1361 1.1 leo */
1362 1.1 leo bytes_done = reqp->dm_cur->dm_count - bytes_left;
1363 1.1 leo
1364 1.4 leo if ((reqp->dr_flag & DRIVER_BOUNCING) && (PH_IN(reqp->phase))) {
1365 1.4 leo /*
1366 1.4 leo * Copy the bytes read until now from the bounce buffer
1367 1.4 leo * to the 'real' destination. Flush the data-cache
1368 1.4 leo * before copying.
1369 1.4 leo */
1370 1.4 leo PCIA();
1371 1.4 leo bcopy(reqp->bouncerp, reqp->xdata_ptr, bytes_done);
1372 1.4 leo reqp->bouncerp += bytes_done;
1373 1.4 leo }
1374 1.4 leo
1375 1.1 leo reqp->xdata_ptr = &reqp->xdata_ptr[bytes_done]; /* XXX */
1376 1.1 leo reqp->xdata_len -= bytes_done; /* XXX */
1377 1.4 leo if ((reqp->dm_cur->dm_count -= bytes_done) == 0)
1378 1.1 leo reqp->dm_cur++;
1379 1.1 leo else reqp->dm_cur->dm_addr += bytes_done;
1380 1.1 leo
1381 1.4 leo if (PH_IN(reqp->phase) && (dmstat & SC_PAR_ERR)) {
1382 1.4 leo if (!(ncr5380_no_parchk & (1 << reqp->targ_id)))
1383 1.1 leo /* XXX: Should be parity error ???? */
1384 1.1 leo reqp->xs->error = XS_DRIVER_STUFFUP;
1385 1.1 leo }
1386 1.1 leo
1387 1.1 leo /*
1388 1.1 leo * DMA mode should always be reset even when we will continue with the
1389 1.1 leo * next chain.
1390 1.1 leo */
1391 1.4 leo SET_5380_REG(NCR5380_MODE, GET_5380_REG(NCR5380_MODE) & ~SC_M_DMA);
1392 1.4 leo
1393 1.4 leo
1394 1.4 leo if ((dmstat & SC_BSY_ERR) || !(dmstat & SC_PHS_MTCH)
1395 1.4 leo || (reqp->dm_cur > reqp->dm_last) || (reqp->xs->error)) {
1396 1.1 leo
1397 1.1 leo /*
1398 1.1 leo * Tell interrupt functions DMA mode has ended.
1399 1.1 leo */
1400 1.1 leo reqp->dr_flag &= ~DRIVER_IN_DMA;
1401 1.1 leo
1402 1.1 leo /*
1403 1.4 leo * Clear mode and icom
1404 1.1 leo */
1405 1.4 leo SET_5380_REG(NCR5380_MODE, IMODE_BASE);
1406 1.4 leo SET_5380_REG(NCR5380_ICOM, 0);
1407 1.1 leo
1408 1.4 leo if (dmstat & SC_BSY_ERR) {
1409 1.4 leo if (!reqp->xs->error)
1410 1.1 leo reqp->xs->error = XS_BUSY;
1411 1.1 leo finish_req(reqp);
1412 1.4 leo PID("dma_ready1");
1413 1.4 leo return (1);
1414 1.1 leo }
1415 1.1 leo
1416 1.4 leo if (reqp->xs->error != 0) {
1417 1.4 leo ncr_tprint(reqp, "dma-ready: code = %d\n", reqp->xs->error); /* LWP */
1418 1.1 leo reqp->msgout = MSG_ABORT;
1419 1.4 leo SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1420 1.1 leo }
1421 1.4 leo PID("dma_ready2");
1422 1.4 leo return (1);
1423 1.1 leo }
1424 1.4 leo return (0);
1425 1.1 leo }
1426 1.4 leo #endif /* REAL_DMA */
1427 1.1 leo
1428 1.1 leo static int
1429 1.1 leo check_autosense(reqp, linked)
1430 1.1 leo SC_REQ *reqp;
1431 1.1 leo int linked;
1432 1.1 leo {
1433 1.1 leo int sps;
1434 1.1 leo
1435 1.1 leo /*
1436 1.1 leo * If we not executing an auto-sense and the status code
1437 1.1 leo * is request-sense, we automatically issue a request
1438 1.1 leo * sense command.
1439 1.1 leo */
1440 1.4 leo PID("cautos1");
1441 1.4 leo if (!(reqp->dr_flag & DRIVER_AUTOSEN)) {
1442 1.4 leo if (reqp->status == SCSCHKC) {
1443 1.1 leo memcpy(&reqp->xcmd, sense_cmd, sizeof(sense_cmd));
1444 1.1 leo reqp->xdata_ptr = (u_char *)&reqp->xs->sense;
1445 1.1 leo reqp->xdata_len = sizeof(reqp->xs->sense);
1446 1.1 leo reqp->dr_flag |= DRIVER_AUTOSEN;
1447 1.1 leo reqp->dr_flag &= ~DRIVER_DMAOK;
1448 1.4 leo if (!linked) {
1449 1.1 leo sps = splbio();
1450 1.1 leo reqp->next = issue_q;
1451 1.1 leo issue_q = reqp;
1452 1.1 leo splx(sps);
1453 1.1 leo }
1454 1.1 leo else reqp->xcmd.bytes[4] |= 1;
1455 1.1 leo
1456 1.1 leo #ifdef DBG_REQ
1457 1.6 leo bzero(reqp->xdata_ptr, reqp->xdata_len);
1458 1.6 leo if (dbg_target_mask & (1 << reqp->targ_id))
1459 1.6 leo show_request(reqp, "AUTO-SENSE");
1460 1.1 leo #endif
1461 1.4 leo PID("cautos2");
1462 1.4 leo return (-1);
1463 1.1 leo }
1464 1.1 leo }
1465 1.1 leo else {
1466 1.1 leo /*
1467 1.1 leo * An auto-sense has finished
1468 1.1 leo */
1469 1.4 leo if ((reqp->status & SCSMASK) != SCSGOOD)
1470 1.1 leo reqp->xs->error = XS_DRIVER_STUFFUP; /* SC_E_AUTOSEN; */
1471 1.1 leo else reqp->xs->error = XS_SENSE;
1472 1.1 leo reqp->status = SCSCHKC;
1473 1.1 leo }
1474 1.4 leo PID("cautos3");
1475 1.4 leo return (0);
1476 1.1 leo }
1477 1.1 leo
1478 1.1 leo static int
1479 1.4 leo reach_msg_out(sc, len)
1480 1.4 leo struct ncr_softc *sc;
1481 1.4 leo u_long len;
1482 1.1 leo {
1483 1.1 leo u_char phase;
1484 1.1 leo u_char data;
1485 1.1 leo
1486 1.4 leo ncr_aprint(sc, "Trying to reach Message-out phase\n");
1487 1.4 leo if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ)
1488 1.1 leo phase = (phase >> 2) & 7;
1489 1.4 leo else return (-1);
1490 1.4 leo ncr_aprint(sc, "Trying to reach Message-out phase, now: %d\n", phase);
1491 1.4 leo if (phase == PH_MSGOUT)
1492 1.4 leo return (0);
1493 1.1 leo
1494 1.4 leo SET_5380_REG(NCR5380_TCOM, phase);
1495 1.1 leo
1496 1.1 leo do {
1497 1.4 leo if (!wait_req_true())
1498 1.1 leo break;
1499 1.4 leo if (((GET_5380_REG(NCR5380_IDSTAT) >> 2) & 7) != phase)
1500 1.1 leo break;
1501 1.4 leo if (PH_IN(phase)) {
1502 1.4 leo data = GET_5380_REG(NCR5380_DATA);
1503 1.4 leo SET_5380_REG(NCR5380_ICOM, SC_A_ACK | SC_A_ATN);
1504 1.1 leo }
1505 1.1 leo else {
1506 1.4 leo SET_5380_REG(NCR5380_DATA, 0);
1507 1.4 leo SET_5380_REG(NCR5380_ICOM, SC_ADTB|SC_A_ACK|SC_A_ATN);
1508 1.1 leo }
1509 1.4 leo if (!wait_req_false())
1510 1.1 leo break;
1511 1.4 leo SET_5380_REG(NCR5380_ICOM, SC_A_ATN);
1512 1.4 leo } while (--len);
1513 1.1 leo
1514 1.4 leo if ((phase = GET_5380_REG(NCR5380_IDSTAT)) & SC_S_REQ) {
1515 1.1 leo phase = (phase >> 2) & 7;
1516 1.4 leo if (phase == PH_MSGOUT) {
1517 1.4 leo ncr_aprint(sc, "Message-out phase reached.\n");
1518 1.4 leo return (0);
1519 1.1 leo }
1520 1.1 leo }
1521 1.4 leo return (-1);
1522 1.1 leo }
1523 1.1 leo
1524 1.1 leo static void
1525 1.4 leo scsi_reset(sc)
1526 1.4 leo struct ncr_softc *sc;
1527 1.1 leo {
1528 1.1 leo SC_REQ *tmp, *next;
1529 1.4 leo int sps;
1530 1.1 leo
1531 1.4 leo ncr_aprint(sc, "Resetting SCSI-bus\n");
1532 1.1 leo
1533 1.4 leo PID("scsi_reset1");
1534 1.1 leo sps = splbio();
1535 1.4 leo SET_5380_REG(NCR5380_ICOM, SC_A_RST);
1536 1.1 leo delay(1);
1537 1.4 leo SET_5380_REG(NCR5380_ICOM, 0);
1538 1.1 leo
1539 1.1 leo /*
1540 1.1 leo * None of the jobs in the discon_q will ever be reconnected,
1541 1.1 leo * notify this to the higher level code.
1542 1.1 leo */
1543 1.4 leo for (tmp = discon_q; tmp ;) {
1544 1.1 leo next = tmp->next;
1545 1.1 leo tmp->next = NULL;
1546 1.1 leo tmp->xs->error = XS_TIMEOUT;
1547 1.1 leo busy &= ~(1 << tmp->targ_id);
1548 1.1 leo finish_req(tmp);
1549 1.1 leo tmp = next;
1550 1.1 leo }
1551 1.1 leo discon_q = NULL;
1552 1.1 leo
1553 1.1 leo /*
1554 1.1 leo * The current job will never finish either.
1555 1.1 leo * The problem is that we can't finish the job because an instance
1556 1.1 leo * of main is running on it. Our best guess is that the job is currently
1557 1.1 leo * doing REAL-DMA. In that case 'dma_ready()' should correctly finish
1558 1.1 leo * the job because it detects BSY-loss.
1559 1.1 leo */
1560 1.4 leo if (tmp = connected) {
1561 1.4 leo if (tmp->dr_flag & DRIVER_IN_DMA) {
1562 1.1 leo tmp->xs->error = XS_DRIVER_STUFFUP;
1563 1.4 leo #ifdef REAL_DMA
1564 1.4 leo dma_ready();
1565 1.4 leo #endif
1566 1.1 leo }
1567 1.1 leo }
1568 1.1 leo splx(sps);
1569 1.4 leo PID("scsi_reset2");
1570 1.1 leo }
1571 1.1 leo
1572 1.1 leo /*
1573 1.1 leo * Check validity of the IRQ set by the 5380. If the interrupt is valid,
1574 1.1 leo * the appropriate action is carried out (reselection or DMA ready) and
1575 1.1 leo * INTR_RESEL or INTR_DMA is returned. Otherwise a console notice is written
1576 1.1 leo * and INTR_SPURIOUS is returned.
1577 1.1 leo */
1578 1.1 leo static int
1579 1.4 leo check_intr(sc)
1580 1.4 leo struct ncr_softc *sc;
1581 1.1 leo {
1582 1.1 leo SC_REQ *reqp;
1583 1.1 leo
1584 1.4 leo if ((GET_5380_REG(NCR5380_IDSTAT) & (SC_S_SEL|SC_S_IO))
1585 1.4 leo ==(SC_S_SEL|SC_S_IO))
1586 1.4 leo return (INTR_RESEL);
1587 1.1 leo else {
1588 1.4 leo if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)){
1589 1.1 leo reqp->dr_flag &= ~DRIVER_IN_DMA;
1590 1.4 leo return (INTR_DMA);
1591 1.1 leo }
1592 1.1 leo }
1593 1.4 leo scsi_clr_ipend();
1594 1.1 leo printf("-->");
1595 1.1 leo scsi_show();
1596 1.4 leo ncr_aprint(sc, "Spurious interrupt.\n");
1597 1.4 leo return (INTR_SPURIOUS);
1598 1.1 leo }
1599 1.1 leo
1600 1.4 leo #ifdef REAL_DMA
1601 1.1 leo /*
1602 1.1 leo * Check if DMA can be used for this request. This function also builds
1603 1.1 leo * the dma-chain.
1604 1.1 leo */
1605 1.1 leo static int
1606 1.1 leo scsi_dmaok(reqp)
1607 1.1 leo SC_REQ *reqp;
1608 1.1 leo {
1609 1.1 leo u_long phy_buf;
1610 1.1 leo u_long phy_len;
1611 1.1 leo void *req_addr;
1612 1.1 leo u_long req_len;
1613 1.1 leo struct dma_chain *dm;
1614 1.1 leo
1615 1.1 leo /*
1616 1.1 leo * Initialize locals and requests' DMA-chain.
1617 1.1 leo */
1618 1.4 leo req_len = reqp->xdata_len;
1619 1.4 leo req_addr = (void*)reqp->xdata_ptr;
1620 1.4 leo dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1621 1.4 leo dm->dm_count = dm->dm_addr = 0;
1622 1.4 leo reqp->dr_flag &= ~DRIVER_BOUNCING;
1623 1.1 leo
1624 1.1 leo /*
1625 1.1 leo * Do not accept zero length DMA.
1626 1.1 leo */
1627 1.4 leo if (req_len == 0)
1628 1.4 leo return (0);
1629 1.1 leo
1630 1.1 leo /*
1631 1.1 leo * LWP: I think that this restriction is not strictly nessecary.
1632 1.1 leo */
1633 1.4 leo if ((req_len & 0x1) || ((u_int)req_addr & 0x3))
1634 1.4 leo return (0);
1635 1.1 leo
1636 1.1 leo /*
1637 1.1 leo * Build the DMA-chain.
1638 1.1 leo */
1639 1.1 leo dm->dm_addr = phy_buf = kvtop(req_addr);
1640 1.4 leo while (req_len) {
1641 1.4 leo if (req_len < (phy_len = NBPG - ((u_long)req_addr & PGOFSET)))
1642 1.1 leo phy_len = req_len;
1643 1.1 leo
1644 1.1 leo req_addr += phy_len;
1645 1.1 leo req_len -= phy_len;
1646 1.1 leo dm->dm_count += phy_len;
1647 1.1 leo
1648 1.4 leo if (req_len) {
1649 1.4 leo u_long tmp = kvtop(req_addr);
1650 1.1 leo
1651 1.4 leo if ((phy_buf + phy_len) != tmp) {
1652 1.4 leo if (wrong_dma_range(reqp, dm)) {
1653 1.4 leo if (reqp->dr_flag & DRIVER_BOUNCING)
1654 1.4 leo goto bounceit;
1655 1.4 leo return (0);
1656 1.4 leo }
1657 1.1 leo
1658 1.4 leo if (++dm >= &reqp->dm_chain[MAXDMAIO]) {
1659 1.4 leo ncr_tprint(reqp,"dmaok: DMA chain too long!\n");
1660 1.4 leo return (0);
1661 1.1 leo }
1662 1.1 leo dm->dm_count = 0;
1663 1.1 leo dm->dm_addr = tmp;
1664 1.1 leo }
1665 1.1 leo phy_buf = tmp;
1666 1.1 leo }
1667 1.1 leo }
1668 1.4 leo if (wrong_dma_range(reqp, dm)) {
1669 1.4 leo if (reqp->dr_flag & DRIVER_BOUNCING)
1670 1.4 leo goto bounceit;
1671 1.4 leo return (0);
1672 1.4 leo }
1673 1.1 leo reqp->dm_last = dm;
1674 1.4 leo return (1);
1675 1.4 leo
1676 1.4 leo bounceit:
1677 1.4 leo if ((reqp->bounceb = alloc_bounceb(reqp->xdata_len)) == NULL) {
1678 1.4 leo /*
1679 1.4 leo * If we can't get a bounce buffer, forget DMA
1680 1.4 leo */
1681 1.4 leo reqp->dr_flag &= ~DRIVER_BOUNCING;
1682 1.4 leo return(0);
1683 1.4 leo }
1684 1.4 leo /*
1685 1.4 leo * Initialize a single DMA-range containing the bounced request
1686 1.4 leo */
1687 1.4 leo dm = reqp->dm_cur = reqp->dm_last = reqp->dm_chain;
1688 1.4 leo dm->dm_addr = kvtop(reqp->bounceb);
1689 1.4 leo dm->dm_count = reqp->xdata_len;
1690 1.4 leo reqp->bouncerp = reqp->bounceb;
1691 1.4 leo
1692 1.4 leo return (1);
1693 1.1 leo }
1694 1.4 leo #endif /* REAL_DMA */
1695 1.1 leo
1696 1.1 leo static void
1697 1.4 leo run_main(sc)
1698 1.4 leo struct ncr_softc *sc;
1699 1.1 leo {
1700 1.1 leo int sps = splbio();
1701 1.1 leo
1702 1.4 leo if (!main_running) {
1703 1.4 leo /*
1704 1.4 leo * If shared resources are required, claim them
1705 1.4 leo * before entering 'scsi_main'. If we can't get them
1706 1.4 leo * now, assume 'run_main' will be called when the resource
1707 1.4 leo * becomes available.
1708 1.4 leo */
1709 1.4 leo if (!claimed_dma()) {
1710 1.4 leo splx(sps);
1711 1.4 leo return;
1712 1.4 leo }
1713 1.1 leo main_running = 1;
1714 1.1 leo splx(sps);
1715 1.4 leo scsi_main(sc);
1716 1.1 leo }
1717 1.1 leo else splx(sps);
1718 1.1 leo }
1719 1.1 leo
1720 1.4 leo /*
1721 1.4 leo * Prefix message with full target info.
1722 1.4 leo */
1723 1.4 leo static void
1724 1.4 leo ncr_tprint(SC_REQ *reqp, char *fmt, ...)
1725 1.4 leo {
1726 1.4 leo va_list ap;
1727 1.4 leo
1728 1.4 leo va_start(ap, fmt);
1729 1.4 leo sc_print_addr(reqp->xs->sc_link);
1730 1.4 leo printf("%r", fmt, ap);
1731 1.4 leo va_end(ap);
1732 1.4 leo }
1733 1.4 leo
1734 1.4 leo /*
1735 1.4 leo * Prefix message with adapter info.
1736 1.4 leo */
1737 1.4 leo static void
1738 1.4 leo ncr_aprint(struct ncr_softc *sc, char *fmt, ...)
1739 1.4 leo {
1740 1.4 leo va_list ap;
1741 1.4 leo
1742 1.4 leo va_start(ap, fmt);
1743 1.4 leo printf("%s : %r", sc->sc_dev.dv_xname, fmt, ap);
1744 1.4 leo va_end(ap);
1745 1.4 leo }
1746 1.1 leo /****************************************************************************
1747 1.1 leo * Start Debugging Functions *
1748 1.1 leo ****************************************************************************/
1749 1.1 leo static void
1750 1.1 leo show_data_sense(xs)
1751 1.1 leo struct scsi_xfer *xs;
1752 1.1 leo {
1753 1.6 leo u_char *p1, *p2;
1754 1.1 leo int i;
1755 1.1 leo int sz;
1756 1.1 leo
1757 1.6 leo p1 = (u_char *) xs->cmd;
1758 1.6 leo p2 = (u_char *)&xs->sense;
1759 1.6 leo if(*p2 == 0)
1760 1.6 leo return; /* No(n)sense */
1761 1.6 leo printf("cmd[%d,%d]: ", xs->cmdlen, sz = command_size(*p1));
1762 1.4 leo for (i = 0; i < sz; i++)
1763 1.6 leo printf("%x ", p1[i]);
1764 1.1 leo printf("\nsense: ");
1765 1.4 leo for (i = 0; i < sizeof(xs->sense); i++)
1766 1.6 leo printf("%x ", p2[i]);
1767 1.1 leo printf("\n");
1768 1.1 leo }
1769 1.1 leo
1770 1.1 leo static void
1771 1.1 leo show_request(reqp, qtxt)
1772 1.1 leo SC_REQ *reqp;
1773 1.1 leo char *qtxt;
1774 1.1 leo {
1775 1.6 leo printf("REQ-%s: %d %x[%d] cmd[0]=%x S=%x M=%x R=%x resid=%d %s\n",
1776 1.6 leo qtxt, reqp->targ_id, reqp->xdata_ptr, reqp->xdata_len,
1777 1.1 leo reqp->xcmd.opcode, reqp->status, reqp->message,
1778 1.6 leo reqp->xs->error, reqp->xs->resid, reqp->link ? "L":"");
1779 1.4 leo if (reqp->status == SCSCHKC)
1780 1.1 leo show_data_sense(reqp->xs);
1781 1.1 leo }
1782 1.1 leo
1783 1.1 leo scsi_show()
1784 1.1 leo {
1785 1.1 leo SC_REQ *tmp;
1786 1.1 leo int sps = splhigh();
1787 1.1 leo
1788 1.1 leo #ifndef DBG_PID
1789 1.1 leo #define last_hit ""
1790 1.4 leo #define olast_hit ""
1791 1.1 leo #endif
1792 1.1 leo
1793 1.4 leo for (tmp = issue_q; tmp; tmp = tmp->next)
1794 1.1 leo show_request(tmp, "ISSUED");
1795 1.4 leo for (tmp = discon_q; tmp; tmp = tmp->next)
1796 1.1 leo show_request(tmp, "DISCONNECTED");
1797 1.4 leo if (connected)
1798 1.1 leo show_request(connected, "CONNECTED");
1799 1.4 leo printf("idstat: %x, dmstat: %x\n", GET_5380_REG(NCR5380_IDSTAT),
1800 1.4 leo GET_5380_REG(NCR5380_DMSTAT));
1801 1.1 leo /* show_signals(); */
1802 1.4 leo if (connected)
1803 1.1 leo printf("phase = %d, ", connected->phase);
1804 1.4 leo printf("busy:%x, last_hit:%s, olast_hit:%s spl:%04x\n", busy,
1805 1.4 leo last_hit, olast_hit, sps);
1806 1.1 leo
1807 1.1 leo splx(sps);
1808 1.1 leo }
1809