1 /* @(#)scsi-sgi.c 1.38 09/11/28 Copyright 1997 J. Schilling */
2 #ifndef lint
3 static char __sccsid[] =
4 "@(#)scsi-sgi.c 1.38 09/11/28 Copyright 1997 J. Schilling";
5 #endif
6 /*
7 * Interface for the SGI generic SCSI implementation.
8 *
9 * First Hacky implementation
10 * (needed libds, did not support bus scanning and had no error checking)
11 * from "Frank van Beek" <frank@neogeo.nl>
12 *
13 * Actual implementation supports all scg features.
14 *
15 * Warning: you may change this source, but if you do that
16 * you need to change the _scg_version and _scg_auth* string below.
17 * You may not return "schily" for an SCG_AUTHOR request anymore.
18 * Choose your name instead of "schily" and make clear that the version
19 * string is related to a modified source.
20 *
21 * Copyright (c) 1997 J. Schilling
22 */
23 /*
24 * The contents of this file are subject to the terms of the
25 * Common Development and Distribution License, Version 1.0 only
26 * (the "License"). You may not use this file except in compliance
27 * with the License.
28 *
29 * See the file CDDL.Schily.txt in this distribution for details.
30 * A copy of the CDDL is also available via the Internet at
31 * http://www.opensource.org/licenses/cddl1.txt
32 *
33 * The following exceptions apply:
34 * CDDL �3.6 needs to be replaced by: "You may create a Larger Work by
35 * combining Covered Software with other code if all other code is governed by
36 * the terms of a license that is OSI approved (see www.opensource.org) and
37 * you may distribute the Larger Work as a single product. In such a case,
38 * You must make sure the requirements of this License are fulfilled for
39 * the Covered Software."
40 *
41 * When distributing Covered Code, include this CDDL HEADER in each
42 * file and include the License file CDDL.Schily.txt from this distribution.
43 */
44
45 #include <dslib.h>
46
47 /*
48 * Warning: you may change this source, but if you do that
49 * you need to change the _scg_version and _scg_auth* string below.
50 * You may not return "schily" for an SCG_AUTHOR request anymore.
51 * Choose your name instead of "schily" and make clear that the version
52 * string is related to a modified source.
53 */
54 LOCAL char _scg_trans_version[] = "scsi-sgi.c-1.38"; /* The version for this transport*/
55
56 #ifdef USE_DSLIB
57
58 struct dsreq *dsp = 0;
59 #define MAX_SCG 1 /* Max # of SCSI controllers */
60
61 #else
62
63 #define MAX_SCG 16 /* Max # of SCSI controllers */
64 #define MAX_TGT 16
65 #define MAX_LUN 8
66
67 struct scg_local {
68 short scgfiles[MAX_SCG][MAX_TGT][MAX_LUN];
69 };
70 #define scglocal(p) ((struct scg_local *)((p)->local))
71
72 #endif
73
74 #define MAX_DMA_SGI (256*1024) /* Check if this is not too big */
75
76
77 #ifndef USE_DSLIB
78 LOCAL int scg_sendreq __PR((SCSI *scgp, struct scg_cmd *sp, struct dsreq *dsp));
79 #endif
80
81
82 /*
83 * Return version information for the low level SCSI transport code.
84 * This has been introduced to make it easier to trace down problems
85 * in applications.
86 */
87 LOCAL char *
scgo_version(scgp,what)88 scgo_version(scgp, what)
89 SCSI *scgp;
90 int what;
91 {
92 if (scgp != (SCSI *)0) {
93 switch (what) {
94
95 case SCG_VERSION:
96 return (_scg_trans_version);
97 /*
98 * If you changed this source, you are not allowed to
99 * return "schily" for the SCG_AUTHOR request.
100 */
101 case SCG_AUTHOR:
102 return (_scg_auth_schily);
103 case SCG_SCCS_ID:
104 return (__sccsid);
105 }
106 }
107 return ((char *)0);
108 }
109
110 LOCAL int
scgo_help(scgp,f)111 scgo_help(scgp, f)
112 SCSI *scgp;
113 FILE *f;
114 {
115 __scg_help(f, "DS", "Generic SCSI",
116 "", "bus,target,lun", "1,2,0", TRUE, FALSE);
117 return (0);
118 }
119
120 LOCAL int
scgo_open(scgp,device)121 scgo_open(scgp, device)
122 SCSI *scgp;
123 char *device;
124 {
125 int busno = scg_scsibus(scgp);
126 int tgt = scg_target(scgp);
127 int tlun = scg_lun(scgp);
128 register int f;
129 register int b;
130 register int t;
131 register int l;
132 register int nopen = 0;
133 char devname[64];
134
135 if (busno >= MAX_SCG || tgt >= MAX_TGT || tlun >= MAX_LUN) {
136 errno = EINVAL;
137 if (scgp->errstr)
138 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
139 "Illegal value for busno, target or lun '%d,%d,%d'",
140 busno, tgt, tlun);
141 return (-1);
142 }
143
144 if ((device != NULL && *device != '\0') || (busno == -2 && tgt == -2)) {
145 errno = EINVAL;
146 if (scgp->errstr)
147 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
148 "Open by 'devname' not supported on this OS");
149 return (-1);
150 }
151
152 if (scgp->local == NULL) {
153 scgp->local = malloc(sizeof (struct scg_local));
154 if (scgp->local == NULL)
155 return (0);
156
157 for (b = 0; b < MAX_SCG; b++) {
158 for (t = 0; t < MAX_TGT; t++) {
159 for (l = 0; l < MAX_LUN; l++)
160 scglocal(scgp)->scgfiles[b][t][l] = (short)-1;
161 }
162 }
163 }
164
165 if (busno >= 0 && tgt >= 0 && tlun >= 0) {
166
167 js_snprintf(devname, sizeof (devname),
168 "/dev/scsi/sc%dd%dl%d", busno, tgt, tlun);
169 #ifdef USE_DSLIB
170 dsp = dsopen(devname, O_RDWR);
171 if (dsp == 0)
172 return (-1);
173 #else
174 f = open(devname, O_RDWR);
175 if (f < 0) {
176 if (scgp->errstr)
177 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
178 "Cannot open '%s'",
179 devname);
180 return (-1);
181 }
182 scglocal(scgp)->scgfiles[busno][tgt][tlun] = f;
183 #endif
184 return (1);
185 } else {
186 #ifdef USE_DSLIB
187 return (-1);
188 #else
189 for (b = 0; b < MAX_SCG; b++) {
190 for (t = 0; t < MAX_TGT; t++) {
191 /* for (l = 0; l < MAX_LUN; l++) {*/
192 for (l = 0; l < 1; l++) {
193 js_snprintf(devname, sizeof (devname),
194 "/dev/scsi/sc%dd%dl%d", b, t, l);
195 f = open(devname, O_RDWR);
196 if (f >= 0) {
197 scglocal(scgp)->scgfiles[b][t][l] = (short)f;
198 nopen++;
199 }
200 }
201 }
202 }
203 #endif
204 }
205 return (nopen);
206 }
207
208 LOCAL int
scgo_close(scgp)209 scgo_close(scgp)
210 SCSI *scgp;
211 {
212 #ifndef USE_DSLIB
213 register int f;
214 register int b;
215 register int t;
216 register int l;
217
218 if (scgp->local == NULL)
219 return (-1);
220
221 for (b = 0; b < MAX_SCG; b++) {
222 for (t = 0; t < MAX_TGT; t++) {
223 for (l = 0; l < MAX_LUN; l++) {
224 f = scglocal(scgp)->scgfiles[b][t][l];
225 if (f >= 0)
226 close(f);
227 scglocal(scgp)->scgfiles[b][t][l] = (short)-1;
228 }
229 }
230 }
231 #else
232 dsclose(dsp);
233 #endif
234 return (0);
235 }
236
237 LOCAL long
scgo_maxdma(scgp,amt)238 scgo_maxdma(scgp, amt)
239 SCSI *scgp;
240 long amt;
241 {
242 return (MAX_DMA_SGI);
243 }
244
245 LOCAL void *
scgo_getbuf(scgp,amt)246 scgo_getbuf(scgp, amt)
247 SCSI *scgp;
248 long amt;
249 {
250 if (scgp->debug > 0) {
251 js_fprintf((FILE *)scgp->errfile,
252 "scgo_getbuf: %ld bytes\n", amt);
253 }
254 scgp->bufbase = valloc((size_t)(amt));
255 return (scgp->bufbase);
256 }
257
258 LOCAL void
scgo_freebuf(scgp)259 scgo_freebuf(scgp)
260 SCSI *scgp;
261 {
262 if (scgp->bufbase)
263 free(scgp->bufbase);
264 scgp->bufbase = NULL;
265 }
266
267 LOCAL int
scgo_numbus(scgp)268 scgo_numbus(scgp)
269 SCSI *scgp;
270 {
271 return (MAX_SCG);
272 }
273
274 LOCAL BOOL
scgo_havebus(scgp,busno)275 scgo_havebus(scgp, busno)
276 SCSI *scgp;
277 int busno;
278 {
279 register int t;
280 register int l;
281
282 if (busno < 0 || busno >= MAX_SCG)
283 return (FALSE);
284
285 if (scgp->local == NULL)
286 return (FALSE);
287
288 for (t = 0; t < MAX_TGT; t++) {
289 for (l = 0; l < MAX_LUN; l++)
290 if (scglocal(scgp)->scgfiles[busno][t][l] >= 0)
291 return (TRUE);
292 }
293 return (FALSE);
294 }
295
296 LOCAL int
scgo_fileno(scgp,busno,tgt,tlun)297 scgo_fileno(scgp, busno, tgt, tlun)
298 SCSI *scgp;
299 int busno;
300 int tgt;
301 int tlun;
302 {
303 #ifdef USE_DSLIB
304 if (dsp == NULL)
305 return (-1);
306 return (getfd(dsp));
307 #else
308 if (busno < 0 || busno >= MAX_SCG ||
309 tgt < 0 || tgt >= MAX_TGT ||
310 tlun < 0 || tlun >= MAX_LUN)
311 return (-1);
312
313 if (scgp->local == NULL)
314 return (-1);
315
316 return ((int)scglocal(scgp)->scgfiles[busno][tgt][tlun]);
317 #endif
318 }
319
320 LOCAL int
scgo_initiator_id(scgp)321 scgo_initiator_id(scgp)
322 SCSI *scgp;
323 {
324 return (-1);
325 }
326
327 LOCAL int
scgo_isatapi(scgp)328 scgo_isatapi(scgp)
329 SCSI *scgp;
330 {
331 return (FALSE);
332 }
333
334 LOCAL int
scgo_reset(scgp,what)335 scgo_reset(scgp, what)
336 SCSI *scgp;
337 int what;
338 {
339 /*
340 * Do we have a SCSI reset on SGI?
341 */
342 #ifdef DS_RESET
343 if (what == SCG_RESET_NOP)
344 return (0);
345 if (what != SCG_RESET_BUS) {
346 errno = EINVAL;
347 return (-1);
348 }
349 /*
350 * XXX Does this reset TGT or BUS ???
351 */
352 return (ioctl(scgp->fd, DS_RESET, 0));
353 #else
354 return (-1);
355 #endif
356 }
357
358 #ifndef USE_DSLIB
359 LOCAL int
scg_sendreq(scgp,sp,dsp)360 scg_sendreq(scgp, sp, dsp)
361 SCSI *scgp;
362 struct scg_cmd *sp;
363 struct dsreq *dsp;
364 {
365 int ret;
366 int retries = 4;
367 Uchar status;
368
369 /* if ((sp->flags & SCG_CMD_RETRY) == 0)*/
370 /* retries = 0;*/
371
372 while (--retries > 0) {
373 ret = ioctl(scgp->fd, DS_ENTER, dsp);
374 if (ret < 0) {
375 RET(dsp) = DSRT_DEVSCSI;
376 return (-1);
377 }
378 /*
379 * SGI implementattion botch!!!
380 * If a target does not select the bus,
381 * the return code is DSRT_TIMEOUT
382 */
383 if (RET(dsp) == DSRT_TIMEOUT) {
384 struct timeval to;
385
386 to.tv_sec = TIME(dsp)/1000;
387 to.tv_usec = TIME(dsp)%1000;
388 __scg_times(scgp);
389
390 if (sp->cdb.g0_cdb.cmd == SC_TEST_UNIT_READY &&
391 (scgp->cmdstop->tv_sec < to.tv_sec ||
392 (scgp->cmdstop->tv_sec == to.tv_sec &&
393 scgp->cmdstop->tv_usec < to.tv_usec))) {
394
395 RET(dsp) = DSRT_NOSEL;
396 return (-1);
397 }
398 }
399 if (RET(dsp) == DSRT_NOSEL)
400 continue; /* retry noselect 3X */
401
402 status = STATUS(dsp);
403 switch (status) {
404
405 case 0x08: /* BUSY */
406 case 0x18: /* RESERV CONFLICT */
407 if (retries > 0)
408 sleep(2);
409 continue;
410 case 0x00: /* GOOD */
411 case 0x02: /* CHECK CONDITION */
412 case 0x10: /* INTERM/GOOD */
413 default:
414 return (status);
415 }
416 }
417 return (-1); /* failed retry limit */
418 }
419 #endif
420
421 LOCAL int
scgo_send(scgp)422 scgo_send(scgp)
423 SCSI *scgp;
424 {
425 struct scg_cmd *sp = scgp->scmd;
426 int ret;
427 int flags;
428 #ifndef USE_DSLIB
429 struct dsreq ds;
430 struct dsreq *dsp = &ds;
431
432 dsp->ds_iovbuf = 0;
433 dsp->ds_iovlen = 0;
434 #endif
435
436 if (scgp->fd < 0) {
437 sp->error = SCG_FATAL;
438 return (0);
439 }
440
441 flags = DSRQ_SENSE;
442 if (sp->flags & SCG_RECV_DATA)
443 flags |= DSRQ_READ;
444 else if (sp->size > 0)
445 flags |= DSRQ_WRITE;
446
447 dsp->ds_flags = flags;
448 dsp->ds_link = 0;
449 dsp->ds_synch = 0;
450 dsp->ds_ret = 0;
451
452 DATABUF(dsp) = sp->addr;
453 DATALEN(dsp) = sp->size;
454 CMDBUF(dsp) = (void *) &sp->cdb;
455 CMDLEN(dsp) = sp->cdb_len;
456 SENSEBUF(dsp) = (caddr_t)sp->u_sense.cmd_sense;
457 SENSELEN(dsp) = sizeof (sp->u_sense.cmd_sense);
458 TIME(dsp) = (sp->timeout * 1000) + 100;
459
460 errno = 0;
461 sp->ux_errno = 0;
462 sp->sense_count = 0;
463
464 #ifdef USE_DSLIB
465 ret = doscsireq(scgp->fd, dsp);
466 #else
467 ret = scg_sendreq(scgp, sp, dsp);
468 #endif
469
470 if (RET(dsp) != DSRT_DEVSCSI)
471 ret = 0;
472
473 if (RET(dsp)) {
474 if (RET(dsp) == DSRT_SHORT) {
475 sp->resid = DATALEN(dsp)- DATASENT(dsp);
476 } else if (errno) {
477 sp->ux_errno = errno;
478 } else {
479 sp->ux_errno = EIO;
480 }
481
482 sp->u_scb.cmd_scb[0] = STATUS(dsp);
483
484 sp->sense_count = SENSESENT(dsp);
485 if (sp->sense_count > SCG_MAX_SENSE)
486 sp->sense_count = SCG_MAX_SENSE;
487
488 }
489 switch (RET(dsp)) {
490
491 default:
492 sp->error = SCG_RETRYABLE; break;
493
494 case 0: /* OK */
495 case DSRT_SHORT: /* not implemented */
496 sp->error = SCG_NO_ERROR; break;
497
498 case DSRT_UNIMPL: /* not implemented */
499 case DSRT_REVCODE: /* software obsolete must recompile */
500 case DSRT_NOSEL:
501 sp->u_scb.cmd_scb[0] = 0;
502 sp->error = SCG_FATAL; break;
503
504 case DSRT_TIMEOUT:
505 sp->u_scb.cmd_scb[0] = 0;
506 sp->error = SCG_TIMEOUT; break;
507 }
508 return (ret);
509 }
510