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