sc.c revision 1.18 1 1.18 tsutsui /* $NetBSD: sc.c,v 1.18 2021/06/15 17:16:16 tsutsui Exp $ */
2 1.1 tsutsui
3 1.1 tsutsui /*
4 1.1 tsutsui * Copyright (c) 1992 OMRON Corporation.
5 1.1 tsutsui *
6 1.1 tsutsui * This code is derived from software contributed to Berkeley by
7 1.1 tsutsui * OMRON Corporation.
8 1.1 tsutsui *
9 1.1 tsutsui * Redistribution and use in source and binary forms, with or without
10 1.1 tsutsui * modification, are permitted provided that the following conditions
11 1.1 tsutsui * are met:
12 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright
13 1.1 tsutsui * notice, this list of conditions and the following disclaimer.
14 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the
16 1.1 tsutsui * documentation and/or other materials provided with the distribution.
17 1.1 tsutsui * 3. All advertising materials mentioning features or use of this software
18 1.1 tsutsui * must display the following acknowledgement:
19 1.1 tsutsui * This product includes software developed by the University of
20 1.1 tsutsui * California, Berkeley and its contributors.
21 1.1 tsutsui * 4. Neither the name of the University nor the names of its contributors
22 1.1 tsutsui * may be used to endorse or promote products derived from this software
23 1.1 tsutsui * without specific prior written permission.
24 1.1 tsutsui *
25 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.1 tsutsui * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 tsutsui * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 tsutsui * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.1 tsutsui * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 tsutsui * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 tsutsui * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 tsutsui * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 tsutsui * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 tsutsui * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 tsutsui * SUCH DAMAGE.
36 1.1 tsutsui *
37 1.1 tsutsui * @(#)sc.c 8.1 (Berkeley) 6/10/93
38 1.1 tsutsui */
39 1.1 tsutsui /*
40 1.1 tsutsui * Copyright (c) 1992, 1993
41 1.1 tsutsui * The Regents of the University of California. All rights reserved.
42 1.1 tsutsui *
43 1.1 tsutsui * This code is derived from software contributed to Berkeley by
44 1.1 tsutsui * OMRON Corporation.
45 1.1 tsutsui *
46 1.1 tsutsui * Redistribution and use in source and binary forms, with or without
47 1.1 tsutsui * modification, are permitted provided that the following conditions
48 1.1 tsutsui * are met:
49 1.1 tsutsui * 1. Redistributions of source code must retain the above copyright
50 1.1 tsutsui * notice, this list of conditions and the following disclaimer.
51 1.1 tsutsui * 2. Redistributions in binary form must reproduce the above copyright
52 1.1 tsutsui * notice, this list of conditions and the following disclaimer in the
53 1.1 tsutsui * documentation and/or other materials provided with the distribution.
54 1.1 tsutsui * 3. Neither the name of the University nor the names of its contributors
55 1.1 tsutsui * may be used to endorse or promote products derived from this software
56 1.1 tsutsui * without specific prior written permission.
57 1.1 tsutsui *
58 1.1 tsutsui * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
59 1.1 tsutsui * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
60 1.1 tsutsui * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
61 1.1 tsutsui * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
62 1.1 tsutsui * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
63 1.1 tsutsui * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
64 1.1 tsutsui * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
65 1.1 tsutsui * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
66 1.1 tsutsui * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
67 1.1 tsutsui * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
68 1.1 tsutsui * SUCH DAMAGE.
69 1.1 tsutsui *
70 1.1 tsutsui * @(#)sc.c 8.1 (Berkeley) 6/10/93
71 1.1 tsutsui */
72 1.1 tsutsui
73 1.1 tsutsui /*
74 1.1 tsutsui * sc.c -- SCSI Protocole Controller (SPC) driver
75 1.1 tsutsui * remaked by A.Fujita, MAR-11-199
76 1.1 tsutsui */
77 1.1 tsutsui
78 1.1 tsutsui
79 1.3 tsutsui #define NSC 2
80 1.1 tsutsui
81 1.1 tsutsui #include <sys/param.h>
82 1.1 tsutsui #include <luna68k/stand/boot/samachdep.h>
83 1.1 tsutsui #include <luna68k/stand/boot/scsireg.h>
84 1.1 tsutsui #include <luna68k/stand/boot/scsivar.h>
85 1.1 tsutsui
86 1.1 tsutsui #define SCSI_ID 7
87 1.1 tsutsui
88 1.9 tsutsui static void screset(struct scsi_softc *);
89 1.9 tsutsui static void scprobe(struct scsi_softc *, uint, uint);
90 1.12 tsutsui static int issue_select(struct scsidevice *, uint8_t);
91 1.12 tsutsui static void ixfer_start(struct scsidevice *, int, uint8_t, int);
92 1.12 tsutsui static void ixfer_out(struct scsidevice *, int, uint8_t *);
93 1.12 tsutsui static void ixfer_in(struct scsidevice *, int, uint8_t *);
94 1.12 tsutsui static int scrun(int, int, uint8_t *, int, uint8_t *, int, volatile int *);
95 1.1 tsutsui static int scfinish(int);
96 1.9 tsutsui static void scabort(struct scsi_softc *);
97 1.1 tsutsui
98 1.1 tsutsui struct scsi_softc scsi_softc[NSC];
99 1.1 tsutsui
100 1.1 tsutsui /*
101 1.1 tsutsui * Initialize SPC & Data Structure
102 1.1 tsutsui */
103 1.1 tsutsui
104 1.1 tsutsui int
105 1.9 tsutsui scinit(int ctlr, void *addr)
106 1.1 tsutsui {
107 1.3 tsutsui struct scsi_softc *hs;
108 1.9 tsutsui uint id;
109 1.3 tsutsui
110 1.9 tsutsui if (ctlr < 0 || ctlr >= NSC)
111 1.3 tsutsui return 0;
112 1.3 tsutsui
113 1.9 tsutsui hs = &scsi_softc[ctlr];
114 1.9 tsutsui hs->sc_ctlr = ctlr;
115 1.9 tsutsui hs->sc_spc = addr;
116 1.1 tsutsui
117 1.1 tsutsui hs->sc_flags = 0;
118 1.1 tsutsui hs->sc_phase = BUS_FREE_PHASE;
119 1.1 tsutsui hs->sc_target = SCSI_ID;
120 1.1 tsutsui
121 1.1 tsutsui hs->sc_cdb = NULL;
122 1.1 tsutsui hs->sc_cdblen = 0;
123 1.1 tsutsui hs->sc_buf = NULL;
124 1.1 tsutsui hs->sc_len = 0;
125 1.1 tsutsui hs->sc_lock = NULL;
126 1.1 tsutsui
127 1.1 tsutsui hs->sc_stat = 0;
128 1.1 tsutsui hs->sc_msg[0] = 0;
129 1.1 tsutsui
130 1.9 tsutsui screset(hs);
131 1.9 tsutsui
132 1.9 tsutsui for (id = 0; id < 7; id++)
133 1.9 tsutsui scprobe(hs, id, 0);
134 1.9 tsutsui
135 1.10 tsutsui return 1;
136 1.1 tsutsui }
137 1.1 tsutsui
138 1.14 tsutsui static void
139 1.9 tsutsui screset(struct scsi_softc *hs)
140 1.1 tsutsui {
141 1.9 tsutsui struct scsidevice *hd = hs->sc_spc;
142 1.1 tsutsui
143 1.9 tsutsui printf("sc%d at 0x%08lx: ", hs->sc_ctlr, (u_long)hs->sc_spc);
144 1.1 tsutsui
145 1.1 tsutsui /*
146 1.1 tsutsui * Disable interrupts then reset the FUJI chip.
147 1.1 tsutsui */
148 1.1 tsutsui
149 1.1 tsutsui hd->scsi_sctl = SCTL_DISABLE | SCTL_CTRLRST;
150 1.1 tsutsui hd->scsi_scmd = 0;
151 1.1 tsutsui hd->scsi_pctl = 0;
152 1.1 tsutsui hd->scsi_temp = 0;
153 1.1 tsutsui hd->scsi_tch = 0;
154 1.1 tsutsui hd->scsi_tcm = 0;
155 1.1 tsutsui hd->scsi_tcl = 0;
156 1.1 tsutsui hd->scsi_ints = 0;
157 1.1 tsutsui
158 1.1 tsutsui /* We can use Asynchronous Transfer only */
159 1.1 tsutsui printf("async");
160 1.1 tsutsui
161 1.1 tsutsui /*
162 1.1 tsutsui * Configure MB89352 with its SCSI address, all
163 1.1 tsutsui * interrupts enabled & appropriate parity.
164 1.1 tsutsui */
165 1.1 tsutsui hd->scsi_bdid = SCSI_ID;
166 1.1 tsutsui hd->scsi_sctl = SCTL_DISABLE | SCTL_ABRT_ENAB|
167 1.1 tsutsui SCTL_PARITY_ENAB | SCTL_RESEL_ENAB |
168 1.1 tsutsui SCTL_INTR_ENAB;
169 1.1 tsutsui printf(", parity");
170 1.1 tsutsui
171 1.1 tsutsui DELAY(400);
172 1.1 tsutsui hd->scsi_sctl &= ~SCTL_DISABLE;
173 1.1 tsutsui
174 1.9 tsutsui printf(", ID %d\n", SCSI_ID);
175 1.9 tsutsui }
176 1.9 tsutsui
177 1.9 tsutsui bool
178 1.9 tsutsui scident(uint ctlr, uint target, uint lun, struct scsi_inquiry *inqout,
179 1.9 tsutsui uint32_t *capout)
180 1.9 tsutsui {
181 1.9 tsutsui struct scsi_inquiry inqbuf;
182 1.9 tsutsui struct scsi_generic_cdb inq = {
183 1.9 tsutsui 6,
184 1.9 tsutsui { CMD_INQUIRY, 0, 0, 0, sizeof(inqbuf), 0 }
185 1.9 tsutsui };
186 1.9 tsutsui uint32_t capbuf[2];
187 1.9 tsutsui struct scsi_generic_cdb cap = {
188 1.9 tsutsui 10,
189 1.9 tsutsui { CMD_READ_CAPACITY, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
190 1.9 tsutsui };
191 1.9 tsutsui int i;
192 1.9 tsutsui int tries = 10;
193 1.9 tsutsui
194 1.9 tsutsui /*
195 1.9 tsutsui * See if unit exists and is a disk then read block size & nblocks.
196 1.9 tsutsui */
197 1.9 tsutsui while ((i = scsi_test_unit_rdy(ctlr, target, lun)) != 0) {
198 1.9 tsutsui if (i < 0 || --tries < 0)
199 1.9 tsutsui return false;
200 1.9 tsutsui if (i == STS_CHECKCOND) {
201 1.12 tsutsui uint8_t sensebuf[8];
202 1.9 tsutsui struct scsi_xsense *sp = (struct scsi_xsense *)sensebuf;
203 1.9 tsutsui
204 1.9 tsutsui scsi_request_sense(ctlr, target, lun, sensebuf, 8);
205 1.9 tsutsui if (sp->class == 7 && sp->key == 6)
206 1.9 tsutsui /* drive doing an RTZ -- give it a while */
207 1.9 tsutsui DELAY(1000000);
208 1.9 tsutsui }
209 1.9 tsutsui DELAY(1000);
210 1.9 tsutsui }
211 1.12 tsutsui if (scsi_immed_command(ctlr, target, lun, &inq, (uint8_t *)&inqbuf,
212 1.9 tsutsui sizeof(inqbuf)) ||
213 1.12 tsutsui scsi_immed_command(ctlr, target, lun, &cap, (uint8_t *)&capbuf,
214 1.9 tsutsui sizeof(capbuf)))
215 1.9 tsutsui /* doesn't exist or not a CCS device */
216 1.9 tsutsui return false;
217 1.9 tsutsui
218 1.9 tsutsui switch (inqbuf.type) {
219 1.9 tsutsui case 0: /* disk */
220 1.9 tsutsui case 4: /* WORM */
221 1.9 tsutsui case 5: /* CD-ROM */
222 1.9 tsutsui case 7: /* Magneto-optical */
223 1.9 tsutsui break;
224 1.9 tsutsui default: /* not a disk */
225 1.9 tsutsui return false;
226 1.9 tsutsui }
227 1.9 tsutsui
228 1.9 tsutsui if (inqout != NULL)
229 1.9 tsutsui *inqout = inqbuf;
230 1.9 tsutsui if (capout != NULL) {
231 1.9 tsutsui /* assume big endian */
232 1.9 tsutsui capout[0] = capbuf[0];
233 1.9 tsutsui capout[1] = capbuf[1];
234 1.9 tsutsui }
235 1.9 tsutsui
236 1.9 tsutsui return true;
237 1.9 tsutsui }
238 1.9 tsutsui
239 1.9 tsutsui static void
240 1.9 tsutsui scprobe(struct scsi_softc *hs, uint target, uint lun)
241 1.9 tsutsui {
242 1.9 tsutsui struct scsi_inquiry inqbuf;
243 1.18 tsutsui uint32_t capbuf[2], blocks, blksize;
244 1.9 tsutsui char idstr[32];
245 1.9 tsutsui int i;
246 1.9 tsutsui
247 1.9 tsutsui if (!scident(hs->sc_ctlr, target, lun, &inqbuf, capbuf))
248 1.9 tsutsui return;
249 1.9 tsutsui
250 1.18 tsutsui /* CMD_READ_CAPACITY returns the last logical data block address. */
251 1.18 tsutsui blocks = capbuf[0] + 1;
252 1.18 tsutsui blksize = capbuf[1];
253 1.18 tsutsui
254 1.9 tsutsui memcpy(idstr, &inqbuf.vendor_id, 28);
255 1.9 tsutsui for (i = 27; i > 23; --i)
256 1.9 tsutsui if (idstr[i] != ' ')
257 1.9 tsutsui break;
258 1.9 tsutsui idstr[i + 1] = '\0';
259 1.9 tsutsui for (i = 23; i > 7; --i)
260 1.9 tsutsui if (idstr[i] != ' ')
261 1.9 tsutsui break;
262 1.9 tsutsui idstr[i + 1] = '\0';
263 1.9 tsutsui for (i = 7; i >= 0; --i)
264 1.9 tsutsui if (idstr[i] != ' ')
265 1.9 tsutsui break;
266 1.9 tsutsui idstr[i + 1] = '\0';
267 1.9 tsutsui
268 1.9 tsutsui printf(" ID %d: %s %s rev %s", target, idstr, &idstr[8], &idstr[24]);
269 1.18 tsutsui printf(", %d bytes/sect x %d sectors\n", blksize, blocks);
270 1.1 tsutsui }
271 1.1 tsutsui
272 1.1 tsutsui
273 1.1 tsutsui /*
274 1.1 tsutsui * SPC Arbitration/Selection routine
275 1.1 tsutsui */
276 1.1 tsutsui
277 1.14 tsutsui static int
278 1.12 tsutsui issue_select(struct scsidevice *hd, uint8_t target)
279 1.1 tsutsui {
280 1.10 tsutsui
281 1.1 tsutsui hd->scsi_pctl = 0;
282 1.1 tsutsui hd->scsi_temp = (1 << SCSI_ID) | (1 << target);
283 1.1 tsutsui
284 1.5 tsutsui /* select timeout is hardcoded to 250ms */
285 1.5 tsutsui hd->scsi_tch = 2;
286 1.5 tsutsui hd->scsi_tcm = 113;
287 1.5 tsutsui hd->scsi_tcl = 3;
288 1.1 tsutsui
289 1.1 tsutsui hd->scsi_scmd = SCMD_SELECT;
290 1.1 tsutsui
291 1.10 tsutsui return 1;
292 1.1 tsutsui }
293 1.1 tsutsui
294 1.1 tsutsui
295 1.1 tsutsui /*
296 1.1 tsutsui * SPC Manual Transfer routines
297 1.1 tsutsui */
298 1.1 tsutsui
299 1.1 tsutsui /* not yet */
300 1.1 tsutsui
301 1.1 tsutsui
302 1.1 tsutsui /*
303 1.1 tsutsui * SPC Program Transfer routines
304 1.1 tsutsui */
305 1.1 tsutsui
306 1.14 tsutsui static void
307 1.12 tsutsui ixfer_start(struct scsidevice *hd, int len, uint8_t phase, int wait)
308 1.1 tsutsui {
309 1.10 tsutsui
310 1.1 tsutsui hd->scsi_tch = ((len & 0xff0000) >> 16);
311 1.1 tsutsui hd->scsi_tcm = ((len & 0x00ff00) >> 8);
312 1.1 tsutsui hd->scsi_tcl = (len & 0x0000ff);
313 1.1 tsutsui hd->scsi_pctl = phase;
314 1.1 tsutsui hd->scsi_scmd = SCMD_XFR | SCMD_PROG_XFR;
315 1.1 tsutsui }
316 1.1 tsutsui
317 1.14 tsutsui static void
318 1.12 tsutsui ixfer_out(struct scsidevice *hd, int len, uint8_t *buf)
319 1.1 tsutsui {
320 1.10 tsutsui
321 1.10 tsutsui for (; len > 0; len--) {
322 1.1 tsutsui while (hd->scsi_ssts & SSTS_DREG_FULL) {
323 1.1 tsutsui DELAY(5);
324 1.1 tsutsui }
325 1.1 tsutsui hd->scsi_dreg = *buf++;
326 1.1 tsutsui }
327 1.1 tsutsui }
328 1.1 tsutsui
329 1.14 tsutsui static void
330 1.12 tsutsui ixfer_in(struct scsidevice *hd, int len, uint8_t *buf)
331 1.1 tsutsui {
332 1.10 tsutsui
333 1.1 tsutsui for (; len > 0; len--) {
334 1.1 tsutsui while (hd->scsi_ssts & SSTS_DREG_EMPTY) {
335 1.1 tsutsui DELAY(5);
336 1.1 tsutsui }
337 1.1 tsutsui *buf++ = hd->scsi_dreg;
338 1.1 tsutsui }
339 1.1 tsutsui }
340 1.1 tsutsui
341 1.1 tsutsui
342 1.1 tsutsui /*
343 1.1 tsutsui * SPC drive routines
344 1.1 tsutsui */
345 1.1 tsutsui
346 1.14 tsutsui static int
347 1.12 tsutsui scrun(int ctlr, int target, uint8_t *cdb, int cdblen, uint8_t *buf, int len,
348 1.1 tsutsui volatile int *lock)
349 1.1 tsutsui {
350 1.6 tsutsui struct scsi_softc *hs;
351 1.6 tsutsui struct scsidevice *hd;
352 1.6 tsutsui
353 1.6 tsutsui if (ctlr < 0 || ctlr >= NSC)
354 1.6 tsutsui return 0;
355 1.6 tsutsui
356 1.6 tsutsui hs = &scsi_softc[ctlr];
357 1.9 tsutsui hd = hs->sc_spc;
358 1.9 tsutsui if (hd == NULL)
359 1.9 tsutsui return 0;
360 1.1 tsutsui
361 1.13 tsutsui if ((hd->scsi_ssts & (SSTS_INITIATOR | SSTS_TARGET | SSTS_BUSY)) != 0)
362 1.10 tsutsui return 0;
363 1.1 tsutsui
364 1.1 tsutsui hs->sc_flags = 0;
365 1.1 tsutsui hs->sc_phase = ARB_SEL_PHASE;
366 1.9 tsutsui hs->sc_target = target;
367 1.1 tsutsui
368 1.1 tsutsui hs->sc_cdb = cdb;
369 1.1 tsutsui hs->sc_cdblen = cdblen;
370 1.1 tsutsui hs->sc_buf = buf;
371 1.1 tsutsui hs->sc_len = len;
372 1.1 tsutsui hs->sc_lock = lock;
373 1.1 tsutsui
374 1.1 tsutsui hs->sc_stat = 0;
375 1.1 tsutsui hs->sc_msg[0] = 0;
376 1.1 tsutsui
377 1.1 tsutsui *(hs->sc_lock) = SC_IN_PROGRESS;
378 1.1 tsutsui issue_select(hd, hs->sc_target);
379 1.1 tsutsui
380 1.10 tsutsui return 1;
381 1.1 tsutsui }
382 1.1 tsutsui
383 1.14 tsutsui static int
384 1.1 tsutsui scfinish(int ctlr)
385 1.1 tsutsui {
386 1.1 tsutsui struct scsi_softc *hs = &scsi_softc[ctlr];
387 1.1 tsutsui int status = hs->sc_stat;
388 1.1 tsutsui
389 1.1 tsutsui hs->sc_flags = 0;
390 1.1 tsutsui hs->sc_phase = BUS_FREE_PHASE;
391 1.1 tsutsui hs->sc_target = SCSI_ID;
392 1.1 tsutsui
393 1.1 tsutsui hs->sc_cdb = NULL;
394 1.1 tsutsui hs->sc_cdblen = 0;
395 1.1 tsutsui hs->sc_buf = NULL;
396 1.1 tsutsui hs->sc_len = 0;
397 1.1 tsutsui hs->sc_lock = NULL;
398 1.1 tsutsui
399 1.1 tsutsui hs->sc_stat = 0;
400 1.1 tsutsui hs->sc_msg[0] = 0;
401 1.1 tsutsui
402 1.10 tsutsui return status;
403 1.1 tsutsui }
404 1.1 tsutsui
405 1.14 tsutsui static void
406 1.9 tsutsui scabort(struct scsi_softc *hs)
407 1.1 tsutsui {
408 1.9 tsutsui struct scsidevice *hd = hs->sc_spc;
409 1.1 tsutsui int len;
410 1.1 tsutsui
411 1.1 tsutsui printf("sc%d: abort phase=0x%x, ssts=0x%x, ints=0x%x\n",
412 1.13 tsutsui hs->sc_ctlr, hd->scsi_psns, hd->scsi_ssts, hd->scsi_ints);
413 1.1 tsutsui
414 1.16 christos if (hd->scsi_ints != 0)
415 1.17 jakllsch /* write register value back to register */
416 1.16 christos hd->scsi_ints = hd->scsi_ints;
417 1.16 christos
418 1.1 tsutsui if (hd->scsi_psns == 0 || (hd->scsi_ssts & SSTS_INITIATOR) == 0)
419 1.1 tsutsui /* no longer connected to scsi target */
420 1.1 tsutsui return;
421 1.1 tsutsui
422 1.1 tsutsui /* get the number of bytes remaining in current xfer + fudge */
423 1.1 tsutsui len = (hd->scsi_tch << 16) | (hd->scsi_tcm << 8) | hd->scsi_tcl;
424 1.1 tsutsui
425 1.1 tsutsui /* for that many bus cycles, try to send an abort msg */
426 1.13 tsutsui for (len += 1024;
427 1.13 tsutsui ((hd->scsi_ssts & SSTS_INITIATOR)) != 0 && --len >= 0;) {
428 1.1 tsutsui hd->scsi_scmd = SCMD_SET_ATN;
429 1.1 tsutsui
430 1.1 tsutsui while ((hd->scsi_psns & PSNS_REQ) == 0) {
431 1.10 tsutsui if ((hd->scsi_ssts & SSTS_INITIATOR) == 0)
432 1.1 tsutsui goto out;
433 1.1 tsutsui DELAY(1);
434 1.1 tsutsui }
435 1.1 tsutsui
436 1.1 tsutsui if ((hd->scsi_psns & PHASE) == MESG_OUT_PHASE)
437 1.1 tsutsui hd->scsi_scmd = SCMD_RST_ATN;
438 1.1 tsutsui hd->scsi_pctl = hs->sc_phase = hd->scsi_psns & PHASE;
439 1.1 tsutsui
440 1.1 tsutsui if (hd->scsi_psns & PHASE_IO) {
441 1.1 tsutsui /* one of the input phases - read & discard a byte */
442 1.1 tsutsui hd->scsi_scmd = SCMD_SET_ACK;
443 1.13 tsutsui while ((hd->scsi_psns & PSNS_REQ) != 0)
444 1.1 tsutsui DELAY(1);
445 1.11 tsutsui (void)hd->scsi_temp;
446 1.1 tsutsui } else {
447 1.1 tsutsui /* one of the output phases - send an abort msg */
448 1.1 tsutsui hd->scsi_temp = MSG_ABORT;
449 1.1 tsutsui hd->scsi_scmd = SCMD_SET_ACK;
450 1.13 tsutsui while ((hd->scsi_psns & PSNS_REQ) != 0)
451 1.1 tsutsui DELAY(1);
452 1.1 tsutsui }
453 1.1 tsutsui
454 1.1 tsutsui hd->scsi_scmd = SCMD_RST_ACK;
455 1.1 tsutsui }
456 1.1 tsutsui out:
457 1.1 tsutsui /*
458 1.1 tsutsui * Either the abort was successful & the bus is disconnected or
459 1.1 tsutsui * the device didn't listen. If the latter, announce the problem.
460 1.1 tsutsui * Either way, reset the card & the SPC.
461 1.1 tsutsui */
462 1.1 tsutsui if (len < 0 && hs)
463 1.1 tsutsui printf("sc%d: abort failed. phase=0x%x, ssts=0x%x\n",
464 1.13 tsutsui hs->sc_ctlr, hd->scsi_psns, hd->scsi_ssts);
465 1.1 tsutsui }
466 1.1 tsutsui
467 1.1 tsutsui
468 1.1 tsutsui /*
469 1.1 tsutsui * SCSI Command Handler
470 1.1 tsutsui */
471 1.1 tsutsui
472 1.1 tsutsui int
473 1.9 tsutsui scsi_test_unit_rdy(int ctlr, int target, int lun)
474 1.1 tsutsui {
475 1.1 tsutsui static struct scsi_cdb6 cdb = { CMD_TEST_UNIT_READY };
476 1.1 tsutsui int status;
477 1.1 tsutsui volatile int lock;
478 1.1 tsutsui
479 1.1 tsutsui #ifdef DEBUG
480 1.9 tsutsui printf("scsi_test_unit_rdy( %d, %d, %d): Start\n", ctlr, target, lun);
481 1.1 tsutsui #endif
482 1.1 tsutsui
483 1.9 tsutsui cdb.lun = lun;
484 1.1 tsutsui
485 1.13 tsutsui if (scrun(ctlr, target, (void *)&cdb, 6, NULL, 0, &lock) == 0) {
486 1.1 tsutsui #ifdef DEBUG
487 1.1 tsutsui printf("scsi_test_unit_rdy: Command Transfer Failed.\n");
488 1.1 tsutsui #endif
489 1.10 tsutsui return -1;
490 1.1 tsutsui }
491 1.1 tsutsui
492 1.1 tsutsui while ((lock == SC_IN_PROGRESS) || (lock == SC_DISCONNECTED))
493 1.1 tsutsui DELAY(10);
494 1.1 tsutsui
495 1.1 tsutsui status = scfinish(ctlr);
496 1.1 tsutsui
497 1.1 tsutsui if (lock == SC_IO_COMPLETE) {
498 1.1 tsutsui #ifdef DEBUG
499 1.1 tsutsui printf("scsi_test_unit_rdy: Status -- 0x%x\n", status);
500 1.1 tsutsui #endif
501 1.10 tsutsui return status;
502 1.1 tsutsui } else {
503 1.10 tsutsui return lock;
504 1.1 tsutsui }
505 1.1 tsutsui }
506 1.1 tsutsui
507 1.1 tsutsui int
508 1.12 tsutsui scsi_request_sense(int ctlr, int target, int lun, uint8_t *buf,
509 1.12 tsutsui unsigned int len)
510 1.1 tsutsui {
511 1.1 tsutsui static struct scsi_cdb6 cdb = { CMD_REQUEST_SENSE };
512 1.1 tsutsui int status;
513 1.1 tsutsui volatile int lock;
514 1.1 tsutsui
515 1.1 tsutsui #ifdef DEBUG
516 1.1 tsutsui printf("scsi_request_sense: Start\n");
517 1.1 tsutsui #endif
518 1.1 tsutsui
519 1.7 tsutsui /* Request Sense */
520 1.7 tsutsui /* Additional Sens Length*/
521 1.7 tsutsui /* cdbAllocation Length */
522 1.7 tsutsui /* */
523 1.1 tsutsui
524 1.7 tsutsui /* Addtional Sens Field */
525 1.7 tsutsui /* len */
526 1.1 tsutsui
527 1.9 tsutsui cdb.lun = lun;
528 1.1 tsutsui cdb.len = len;
529 1.1 tsutsui
530 1.13 tsutsui if (scrun(ctlr, target, (void *)&cdb, 6, buf, len, &lock) == 0) {
531 1.1 tsutsui #ifdef DEBUG
532 1.1 tsutsui printf("scsi_request_sense: Command Transfer Failed.\n");
533 1.1 tsutsui #endif
534 1.10 tsutsui return -1;
535 1.1 tsutsui }
536 1.1 tsutsui
537 1.1 tsutsui while ((lock == SC_IN_PROGRESS) || (lock == SC_DISCONNECTED))
538 1.1 tsutsui DELAY(10);
539 1.1 tsutsui
540 1.1 tsutsui status = scfinish(ctlr);
541 1.1 tsutsui
542 1.1 tsutsui if (lock == SC_IO_COMPLETE) {
543 1.1 tsutsui #ifdef DEBUG
544 1.1 tsutsui printf("scsi_request_sense: Status -- 0x%x\n", status);
545 1.1 tsutsui #endif
546 1.10 tsutsui return status;
547 1.1 tsutsui } else {
548 1.10 tsutsui return lock;
549 1.1 tsutsui }
550 1.1 tsutsui }
551 1.1 tsutsui
552 1.1 tsutsui int
553 1.9 tsutsui scsi_immed_command(int ctlr, int target, int lun, struct scsi_generic_cdb *cdb,
554 1.12 tsutsui uint8_t *buf, unsigned int len)
555 1.1 tsutsui {
556 1.1 tsutsui int status;
557 1.1 tsutsui volatile int lock;
558 1.1 tsutsui
559 1.1 tsutsui #ifdef DEBUG
560 1.1 tsutsui printf("scsi_immed_command( %d, %d, %d, cdb(%d), buf, %d): Start\n",
561 1.13 tsutsui ctlr, target, lun, cdb->len, len);
562 1.1 tsutsui #endif
563 1.1 tsutsui
564 1.9 tsutsui cdb->cdb[1] |= lun << 5;
565 1.1 tsutsui
566 1.13 tsutsui if (scrun(ctlr, target, (void *)&cdb->cdb[0], cdb->len, buf, len,
567 1.13 tsutsui &lock) == 0) {
568 1.1 tsutsui #ifdef DEBUG
569 1.1 tsutsui printf("scsi_immed_command: Command Transfer Failed.\n");
570 1.1 tsutsui #endif
571 1.10 tsutsui return -1;
572 1.1 tsutsui }
573 1.1 tsutsui
574 1.1 tsutsui while ((lock == SC_IN_PROGRESS) || (lock == SC_DISCONNECTED))
575 1.1 tsutsui DELAY(10);
576 1.1 tsutsui
577 1.1 tsutsui status = scfinish(ctlr);
578 1.1 tsutsui
579 1.1 tsutsui if (lock == SC_IO_COMPLETE) {
580 1.1 tsutsui #ifdef DEBUG
581 1.1 tsutsui printf("scsi_immed_command: Status -- 0x%x\n", status);
582 1.1 tsutsui #endif
583 1.10 tsutsui return status;
584 1.1 tsutsui } else {
585 1.10 tsutsui return lock;
586 1.1 tsutsui }
587 1.1 tsutsui }
588 1.1 tsutsui
589 1.1 tsutsui int
590 1.9 tsutsui scsi_format_unit(int ctlr, int target, int lun)
591 1.1 tsutsui {
592 1.1 tsutsui static struct scsi_cdb6 cdb = { CMD_FORMAT_UNIT, 0, 0, 0, 0, 0 };
593 1.1 tsutsui int status;
594 1.1 tsutsui volatile int lock;
595 1.1 tsutsui #ifdef DEBUG
596 1.1 tsutsui int count = 0;
597 1.1 tsutsui #endif
598 1.1 tsutsui
599 1.1 tsutsui #ifdef DEBUG
600 1.9 tsutsui printf("scsi_format_unit( %d, %d, %d): Start\n", ctlr, target, lun);
601 1.1 tsutsui #endif
602 1.1 tsutsui
603 1.9 tsutsui cdb.lun = lun;
604 1.1 tsutsui
605 1.13 tsutsui if (scrun(ctlr, target, (void *)&cdb, 6, NULL, 0, &lock) == 0) {
606 1.1 tsutsui #ifdef DEBUG
607 1.1 tsutsui printf("scsi_format_unit: Command Transfer Failed.\n");
608 1.1 tsutsui #endif
609 1.10 tsutsui return -1;
610 1.1 tsutsui }
611 1.1 tsutsui
612 1.1 tsutsui while ((lock == SC_IN_PROGRESS) || (lock == SC_DISCONNECTED)) {
613 1.1 tsutsui DELAY(1000000);
614 1.1 tsutsui #ifdef DEBUG
615 1.1 tsutsui if ((++count % 60) == 0)
616 1.1 tsutsui printf("scsi_format_unit: %d\n", count / 60);
617 1.1 tsutsui #endif
618 1.1 tsutsui }
619 1.1 tsutsui
620 1.1 tsutsui status = scfinish(ctlr);
621 1.1 tsutsui
622 1.1 tsutsui if (lock == SC_IO_COMPLETE) {
623 1.1 tsutsui #ifdef DEBUG
624 1.1 tsutsui printf("scsi_format_unit: Status -- 0x%x\n", status);
625 1.1 tsutsui #endif
626 1.10 tsutsui return status;
627 1.1 tsutsui } else {
628 1.10 tsutsui return lock;
629 1.1 tsutsui }
630 1.1 tsutsui }
631 1.1 tsutsui
632 1.1 tsutsui
633 1.1 tsutsui /*
634 1.1 tsutsui * Interrupt Routine
635 1.1 tsutsui */
636 1.1 tsutsui
637 1.1 tsutsui int
638 1.1 tsutsui scintr(void)
639 1.1 tsutsui {
640 1.1 tsutsui struct scsi_softc *hs;
641 1.1 tsutsui struct scsidevice *hd;
642 1.12 tsutsui uint8_t ints, temp;
643 1.1 tsutsui int i;
644 1.12 tsutsui uint8_t *buf;
645 1.1 tsutsui int len;
646 1.1 tsutsui
647 1.1 tsutsui for (i = 0; i < NSC; i++) {
648 1.1 tsutsui hs = &scsi_softc[i];
649 1.9 tsutsui hd = hs->sc_spc;
650 1.1 tsutsui if ((ints = hd->scsi_ints) != 0)
651 1.1 tsutsui goto get_intr;
652 1.1 tsutsui }
653 1.1 tsutsui
654 1.1 tsutsui /* Unknown Interrupt occured */
655 1.1 tsutsui return -1;
656 1.1 tsutsui
657 1.1 tsutsui
658 1.1 tsutsui /*
659 1.1 tsutsui * Interrupt
660 1.1 tsutsui */
661 1.1 tsutsui
662 1.1 tsutsui get_intr:
663 1.1 tsutsui #ifdef DEBUG
664 1.1 tsutsui printf("scintr: INTS 0x%x, SSTS 0x%x, PCTL 0x%x, PSNS 0x%x 0x%x\n",
665 1.13 tsutsui ints, hd->scsi_ssts, hd->scsi_pctl, hd->scsi_psns, hs->sc_phase);
666 1.1 tsutsui #endif
667 1.1 tsutsui if (ints & INTS_RESEL) {
668 1.1 tsutsui if (hs->sc_phase == BUS_FREE_PHASE) {
669 1.1 tsutsui temp = hd->scsi_temp & ~(1 << SCSI_ID);
670 1.1 tsutsui for (i = 0; temp != 1; i++) {
671 1.1 tsutsui temp >>= 1;
672 1.1 tsutsui }
673 1.1 tsutsui hs->sc_target = i;
674 1.1 tsutsui *(hs->sc_lock) = SC_IN_PROGRESS;
675 1.4 tsutsui } else
676 1.1 tsutsui goto abort;
677 1.1 tsutsui } else if (ints & INTS_DISCON) {
678 1.10 tsutsui if ((hs->sc_msg[0] == MSG_CMD_COMPLETE) ||
679 1.10 tsutsui (hs->sc_msg[0] == MSG_DISCONNECT)) {
680 1.1 tsutsui hs->sc_phase = BUS_FREE_PHASE;
681 1.1 tsutsui hs->sc_target = SCSI_ID;
682 1.13 tsutsui if (hs->sc_msg[0] == MSG_CMD_COMPLETE) {
683 1.1 tsutsui /* SCSI IO complete */
684 1.1 tsutsui *(hs->sc_lock) = SC_IO_COMPLETE;
685 1.13 tsutsui } else {
686 1.13 tsutsui /* Disconnected from Target */
687 1.1 tsutsui *(hs->sc_lock) = SC_DISCONNECTED;
688 1.13 tsutsui }
689 1.1 tsutsui hd->scsi_ints = ints;
690 1.1 tsutsui return 0;
691 1.1 tsutsui } else
692 1.1 tsutsui goto abort;
693 1.1 tsutsui } else if (ints & INTS_CMD_DONE) {
694 1.1 tsutsui if (hs->sc_phase == BUS_FREE_PHASE)
695 1.1 tsutsui goto abort;
696 1.1 tsutsui else if (hs->sc_phase == MESG_IN_PHASE) {
697 1.1 tsutsui hd->scsi_scmd = SCMD_RST_ACK;
698 1.1 tsutsui hd->scsi_ints = ints;
699 1.1 tsutsui hs->sc_phase = hd->scsi_psns & PHASE;
700 1.1 tsutsui return 0;
701 1.1 tsutsui }
702 1.1 tsutsui if (hs->sc_flags & SC_SEL_TIMEOUT)
703 1.1 tsutsui hs->sc_flags &= ~SC_SEL_TIMEOUT;
704 1.1 tsutsui } else if (ints & INTS_SRV_REQ) {
705 1.1 tsutsui if (hs->sc_phase != MESG_IN_PHASE)
706 1.1 tsutsui goto abort;
707 1.1 tsutsui } else if (ints & INTS_TIMEOUT) {
708 1.1 tsutsui if (hs->sc_phase == ARB_SEL_PHASE) {
709 1.1 tsutsui if (hs->sc_flags & SC_SEL_TIMEOUT) {
710 1.1 tsutsui hs->sc_flags &= ~SC_SEL_TIMEOUT;
711 1.1 tsutsui hs->sc_phase = BUS_FREE_PHASE;
712 1.1 tsutsui hs->sc_target = SCSI_ID;
713 1.13 tsutsui /* Such SCSI Device is not connected. */
714 1.1 tsutsui *(hs->sc_lock) = SC_DEV_NOT_FOUND;
715 1.1 tsutsui hd->scsi_ints = ints;
716 1.1 tsutsui return 0;
717 1.1 tsutsui } else {
718 1.1 tsutsui /* wait more 250 usec */
719 1.1 tsutsui hs->sc_flags |= SC_SEL_TIMEOUT;
720 1.1 tsutsui hd->scsi_temp = 0;
721 1.1 tsutsui hd->scsi_tch = 0;
722 1.1 tsutsui hd->scsi_tcm = 0x06;
723 1.1 tsutsui hd->scsi_tcl = 0x40;
724 1.1 tsutsui hd->scsi_ints = ints;
725 1.1 tsutsui return 0;
726 1.1 tsutsui }
727 1.1 tsutsui } else
728 1.1 tsutsui goto abort;
729 1.1 tsutsui } else
730 1.1 tsutsui goto abort;
731 1.1 tsutsui
732 1.1 tsutsui hd->scsi_ints = ints;
733 1.1 tsutsui
734 1.1 tsutsui /*
735 1.1 tsutsui * Next SCSI Transfer
736 1.1 tsutsui */
737 1.1 tsutsui
738 1.1 tsutsui while ((hd->scsi_psns & PSNS_REQ) == 0) {
739 1.1 tsutsui DELAY(1);
740 1.1 tsutsui }
741 1.1 tsutsui
742 1.1 tsutsui hs->sc_phase = hd->scsi_psns & PHASE;
743 1.1 tsutsui
744 1.10 tsutsui if ((hs->sc_phase == DATA_OUT_PHASE) ||
745 1.10 tsutsui (hs->sc_phase == DATA_IN_PHASE)) {
746 1.1 tsutsui len = hs->sc_len;
747 1.1 tsutsui buf = hs->sc_buf;
748 1.1 tsutsui } else if (hs->sc_phase == CMD_PHASE) {
749 1.1 tsutsui len = hs->sc_cdblen;
750 1.1 tsutsui buf = hs->sc_cdb;
751 1.1 tsutsui } else if (hs->sc_phase == STATUS_PHASE) {
752 1.1 tsutsui len = 1;
753 1.1 tsutsui buf = &hs->sc_stat;
754 1.1 tsutsui } else {
755 1.1 tsutsui len = 1;
756 1.1 tsutsui buf = hs->sc_msg;
757 1.1 tsutsui }
758 1.1 tsutsui
759 1.1 tsutsui ixfer_start(hd, len, hs->sc_phase, 0);
760 1.1 tsutsui if (hs->sc_phase & PHASE_IO)
761 1.1 tsutsui ixfer_in(hd, len, buf);
762 1.1 tsutsui else
763 1.1 tsutsui ixfer_out(hd, len, buf);
764 1.1 tsutsui
765 1.1 tsutsui return 0;
766 1.1 tsutsui
767 1.1 tsutsui /*
768 1.1 tsutsui * SCSI Abort
769 1.1 tsutsui */
770 1.1 tsutsui abort:
771 1.1 tsutsui /* SCSI IO failed */
772 1.9 tsutsui scabort(hs);
773 1.1 tsutsui hd->scsi_ints = ints;
774 1.1 tsutsui *(hs->sc_lock) = SC_IO_FAILED;
775 1.1 tsutsui return -1;
776 1.1 tsutsui }
777