1 /* @(#)scsi-linux-sg.c 1.98 16/01/11 Copyright 1997-2016 J. Schilling */
2 #ifndef lint
3 static char __sccsid[] =
4 "@(#)scsi-linux-sg.c 1.98 16/01/11 Copyright 1997-2016 J. Schilling";
5 #endif
6 /*
7 * Interface for Linux generic SCSI implementation (sg).
8 *
9 * This is the interface for the broken Linux SCSI generic driver.
10 * This is a hack, that tries to emulate the functionality
11 * of the scg driver.
12 *
13 * Design flaws of the sg driver:
14 * - cannot see if SCSI command could not be send
15 * - cannot get SCSI status byte
16 * - cannot get real dma count of tranfer
17 * - cannot get number of bytes valid in auto sense data
18 * - to few data in auto sense (CCS/SCSI-2/SCSI-3 needs >= 18)
19 *
20 * This code contains support for the sg driver version 2 by
21 * H. Ei�feld & J. Schilling
22 * Although this enhanced version has been announced to Linus and Alan,
23 * there was no reaction at all.
24 *
25 * About half a year later there occured a version in the official
26 * Linux that was also called version 2. The interface of this version
27 * looks like a playground - the enhancements from this version are
28 * more or less useless for a portable real-world program.
29 *
30 * With Linux 2.4 the official version of the sg driver is called 3.x
31 * and seems to be usable again. The main problem now is the curious
32 * interface that is provided to raise the DMA limit from 32 kB to a
33 * more reasonable value. To do this in a reliable way, a lot of actions
34 * are required.
35 *
36 * Warning: you may change this source, but if you do that
37 * you need to change the _scg_version and _scg_auth* string below.
38 * You may not return "schily" for an SCG_AUTHOR request anymore.
39 * Choose your name instead of "schily" and make clear that the version
40 * string is related to a modified source.
41 *
42 * Copyright (c) 1997-2016 J. Schilling
43 */
44 /*
45 * The contents of this file are subject to the terms of the
46 * Common Development and Distribution License, Version 1.0 only
47 * (the "License"). You may not use this file except in compliance
48 * with the License.
49 *
50 * See the file CDDL.Schily.txt in this distribution for details.
51 * A copy of the CDDL is also available via the Internet at
52 * http://www.opensource.org/licenses/cddl1.txt
53 *
54 * The following exceptions apply:
55 * CDDL �3.6 needs to be replaced by: "You may create a Larger Work by
56 * combining Covered Software with other code if all other code is governed by
57 * the terms of a license that is OSI approved (see www.opensource.org) and
58 * you may distribute the Larger Work as a single product. In such a case,
59 * You must make sure the requirements of this License are fulfilled for
60 * the Covered Software."
61 *
62 * When distributing Covered Code, include this CDDL HEADER in each
63 * file and include the License file CDDL.Schily.txt from this distribution.
64 */
65
66 #include <linux/version.h>
67
68 #ifndef LINUX_VERSION_CODE /* Very old kernel? */
69 # define LINUX_VERSION_CODE 0
70 #endif
71
72 #if LINUX_VERSION_CODE >= 0x01031a /* <linux/scsi.h> introduced in 1.3.26 */
73 #if LINUX_VERSION_CODE >= 0x020000 /* <scsi/scsi.h> introduced somewhere. */
74 /* Need to fine tune the ifdef so we get the transition point right. */
75
76 #if defined(HAVE_BROKEN_SCSI_SCSI_H) || \
77 defined(HAVE_BROKEN_SRC_SCSI_SCSI_H)
78 /*
79 * Be very careful in case that the Linux Kernel maintainers
80 * unexpectedly fix the bugs in the Linux Lernel include files.
81 * Only introduce the attempt for a workaround in case the include
82 * files are broken anyway.
83 */
84 #define __KERNEL__
85 #include <asm/types.h>
86 #undef __KERNEL__
87 #endif
88 #include <scsi/scsi.h>
89 #else
90 #include <linux/scsi.h>
91 #endif
92 #else /* LINUX_VERSION_CODE == 0 Very old kernel? */
93 #define __KERNEL__ /* Some Linux Include files are inconsistent */
94 #include <linux/fs.h> /* From ancient versions, really needed? */
95 #undef __KERNEL__
96 #include "block/blk.h" /* From ancient versions, really needed? */
97 #include "scsi/scsi.h"
98 #endif
99 #include <schily/stat.h>
100 #include <schily/device.h>
101
102 #if defined(HAVE_BROKEN_SCSI_SG_H) || \
103 defined(HAVE_BROKEN_SRC_SCSI_SG_H)
104 /*
105 * Be very careful in case that the Linux Kernel maintainers
106 * unexpectedly fix the bugs in the Linux Lernel include files.
107 * Only introduce the attempt for a workaround in case the include
108 * files are broken anyway.
109 */
110 #define __user
111 #endif
112 #include "scsi/sg.h"
113 #if defined(HAVE_BROKEN_SCSI_SG_H) || \
114 defined(HAVE_BROKEN_SRC_SCSI_SG_H)
115 #undef __user
116 #endif
117
118 #undef sense /* conflict in struct cdrom_generic_command */
119 #include <linux/cdrom.h>
120
121 #if defined(CDROM_PACKET_SIZE) && defined(CDROM_SEND_PACKET)
122 #define USE_ATAPI
123 #endif
124
125 /*
126 * Warning: you may change this source, but if you do that
127 * you need to change the _scg_version and _scg_auth* string below.
128 * You may not return "schily" for an SCG_AUTHOR request anymore.
129 * Choose your name instead of "schily" and make clear that the version
130 * string is related to a modified source.
131 */
132 LOCAL char _scg_trans_version[] = "scsi-linux-sg.c-1.98"; /* The version for this transport*/
133
134 #ifndef SCSI_IOCTL_GET_BUS_NUMBER
135 #define SCSI_IOCTL_GET_BUS_NUMBER 0x5386
136 #endif
137
138 /*
139 * XXX There must be a better way than duplicating things from system include
140 * XXX files. This is stolen from /usr/src/linux/drivers/scsi/scsi.h
141 */
142 #ifndef DID_OK
143 #define DID_OK 0x00 /* NO error */
144 #define DID_NO_CONNECT 0x01 /* Couldn't connect before timeout period */
145 #define DID_BUS_BUSY 0x02 /* BUS stayed busy through time out period */
146 #define DID_TIME_OUT 0x03 /* TIMED OUT for other reason */
147 #define DID_BAD_TARGET 0x04 /* BAD target. */
148 #define DID_ABORT 0x05 /* Told to abort for some other reason */
149 #define DID_PARITY 0x06 /* Parity error */
150 #define DID_ERROR 0x07 /* Internal error */
151 #define DID_RESET 0x08 /* Reset by somebody. */
152 #define DID_BAD_INTR 0x09 /* Got an interrupt we weren't expecting. */
153 #endif
154
155 /*
156 * These indicate the error that occurred, and what is available.
157 */
158 #ifndef DRIVER_BUSY
159 #define DRIVER_BUSY 0x01
160 #define DRIVER_SOFT 0x02
161 #define DRIVER_MEDIA 0x03
162 #define DRIVER_ERROR 0x04
163
164 #define DRIVER_INVALID 0x05
165 #define DRIVER_TIMEOUT 0x06
166 #define DRIVER_HARD 0x07
167 #define DRIVER_SENSE 0x08
168 #endif
169
170 /*
171 * XXX Should add extra space in buscookies and scgfiles for a "PP bus"
172 * XXX and for two or more "ATAPI busses".
173 */
174 #define MAX_SCG (512-MAX_ATA) /* Max # of SCSI controllers */
175 #define MAX_ATA 13 /* Max # of ATA devices */
176 #define MAX_TGT 16
177 #define MAX_LUN 8
178
179 #if (MAX_SCG+MAX_ATA) >= 1000
180 error too many SCSI busses
181 #endif
182
183 #ifdef USE_ATAPI
184 /*
185 * # of virtual buses (schilly_host number)
186 */
187 #define MAX_ATAPI_HOSTS MAX_SCG
188 typedef struct {
189 Uchar typ:4;
190 Uchar bus:4;
191 Uchar host:8;
192 } ata_buscookies;
193 #endif
194
195 struct scg_local {
196 int scgfile; /* Used for SG_GET_BUFSIZE ioctl()*/
197 short scgfiles[MAX_SCG + MAX_ATA][MAX_TGT][MAX_LUN];
198 short buscookies[MAX_SCG + MAX_ATA];
199 int pgbus;
200 int pack_id; /* Should be a random number */
201 int drvers;
202 short isold;
203 short flags;
204 long xbufsize;
205 char *xbuf;
206 char *SCSIbuf;
207 #ifdef USE_ATAPI
208 ata_buscookies bc[MAX_ATAPI_HOSTS];
209 #endif
210 };
211 #define scglocal(p) ((struct scg_local *)((p)->local))
212
213 /*
214 * Flag definitions
215 */
216 #define LF_ATA 0x01 /* Using /dev/hd* ATA interface */
217
218 #ifdef SG_BIG_BUFF
219 #define MAX_DMA_LINUX SG_BIG_BUFF /* Defined in include/scsi/sg.h */
220 #else
221 #define MAX_DMA_LINUX (4*1024) /* Old Linux versions */
222 #endif
223
224 #ifndef SG_MAX_SENSE
225 # define SG_MAX_SENSE 16 /* Too small for CCS / SCSI-2 */
226 #endif /* But cannot be changed */
227
228 #if !defined(__i386) && !defined(i386) && !defined(mc68000)
229 #define MISALIGN
230 #endif
231 /*#define MISALIGN*/
232 /*#undef SG_GET_BUFSIZE*/
233
234 #if defined(USE_PG) && !defined(USE_PG_ONLY)
235 #include "scsi-linux-pg.c"
236 #endif
237 #ifdef USE_ATAPI
238 #include "scsi-linux-ata.c"
239 #endif
240
241 /*
242 * Work around some broken Linux distributions with defective include files.
243 */
244 #if !defined(HZ) && !defined(USER_HZ)
245 #define USER_HZ 100
246 #endif
247
248 #ifdef MISALIGN
249 LOCAL int sg_getint __PR((int *ip));
250 #endif
251 LOCAL int scgo_send __PR((SCSI *scgp));
252 #ifdef SG_IO
253 LOCAL int sg_rwsend __PR((SCSI *scgp));
254 #endif
255 LOCAL void sg_clearnblock __PR((int f));
256 LOCAL BOOL sg_setup __PR((SCSI *scgp, int f, int busno, int tgt, int tlun, int ataidx));
257 LOCAL void sg_initdev __PR((SCSI *scgp, int f));
258 LOCAL int sg_mapbus __PR((SCSI *scgp, int busno, int ino));
259 LOCAL BOOL sg_mapdev __PR((SCSI *scgp, int f, int *busp, int *tgtp, int *lunp,
260 int *chanp, int *inop, int ataidx));
261 #if defined(SG_SET_RESERVED_SIZE) && defined(SG_GET_RESERVED_SIZE)
262 LOCAL long sg_raisedma __PR((SCSI *scgp, long newmax));
263 LOCAL void sg_checkdma __PR((int f, int *valp));
264 #endif
265 LOCAL void sg_settimeout __PR((int f, int timeout));
266
267 /*
268 * Return version information for the low level SCSI transport code.
269 * This has been introduced to make it easier to trace down problems
270 * in applications.
271 */
272 LOCAL char *
scgo_version(scgp,what)273 scgo_version(scgp, what)
274 SCSI *scgp;
275 int what;
276 {
277 if (scgp != (SCSI *)0) {
278 #ifdef USE_PG
279 /*
280 * If we only have a Parallel port or only opened a handle
281 * for PP, just return PP version.
282 */
283 if (scglocal(scgp)->pgbus == 0 ||
284 (scg_scsibus(scgp) >= 0 &&
285 scg_scsibus(scgp) == scglocal(scgp)->pgbus))
286 return (pg_version(scgp, what));
287 #endif
288 switch (what) {
289
290 case SCG_VERSION:
291 return (_scg_trans_version);
292 /*
293 * If you changed this source, you are not allowed to
294 * return "schily" for the SCG_AUTHOR request.
295 */
296 case SCG_AUTHOR:
297 return (_scg_auth_schily);
298 case SCG_SCCS_ID:
299 return (__sccsid);
300 case SCG_KVERSION:
301 {
302 static char kv[16];
303 int n;
304
305 if (scglocal(scgp)->drvers >= 0) {
306 n = scglocal(scgp)->drvers;
307 js_snprintf(kv, sizeof (kv),
308 "%d.%d.%d",
309 n/10000, (n%10000)/100, n%100);
310
311 return (kv);
312 }
313 }
314 }
315 }
316 return ((char *)0);
317 }
318
319 LOCAL int
scgo_help(scgp,f)320 scgo_help(scgp, f)
321 SCSI *scgp;
322 FILE *f;
323 {
324 __scg_help(f, "sg", "Generic transport independent SCSI",
325 "", "bus,target,lun", "1,2,0", TRUE, FALSE);
326 #ifdef USE_PG
327 pg_help(scgp, f);
328 #endif
329 #ifdef USE_ATAPI
330 scgo_ahelp(scgp, f);
331 #endif
332 __scg_help(f, "ATA", "ATA Packet specific SCSI transport using sg interface",
333 "ATA:", "bus,target,lun", "1,2,0", TRUE, FALSE);
334 return (0);
335 }
336
337 LOCAL int
scgo_open(scgp,device)338 scgo_open(scgp, device)
339 SCSI *scgp;
340 char *device;
341 {
342 int busno = scg_scsibus(scgp);
343 int tgt = scg_target(scgp);
344 int tlun = scg_lun(scgp);
345 register int f;
346 register int i;
347 register int b;
348 register int t;
349 register int l;
350 register int nopen = 0;
351 int xopen = 0;
352 char devname[64];
353 BOOL use_ata = FALSE;
354
355 if ((busno >= MAX_SCG && busno < 1000) || busno >= (1000+MAX_ATA) ||
356 tgt >= MAX_TGT || tlun >= MAX_LUN) {
357 errno = EINVAL;
358 if (scgp->errstr)
359 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
360 "Illegal value for busno, target or lun '%d,%d,%d'",
361 busno, tgt, tlun);
362 return (-1);
363 }
364 if (busno >= 1000)
365 busno = busno - 1000 + MAX_SCG;
366
367 if (device != NULL && *device != '\0') {
368 #ifdef USE_ATAPI
369 if (strncmp(device, "ATAPI", 5) == 0) {
370 scgp->ops = &atapi_ops;
371 return (SCGO_OPEN(scgp, device));
372 }
373 #endif
374 if (strcmp(device, "ATA") == 0) {
375 /*
376 * Sending generic SCSI commands via /dev/hd* is a
377 * really bad idea when there also is a generic
378 * SCSI driver interface - it breaks the protocol
379 * layering model. A better idea would be to
380 * have a SCSI host bus adapter driver that sends
381 * the SCSI commands via the ATA hardware. This way,
382 * the layering model would be honored.
383 *
384 * People like Jens Axboe should finally fix the DMA
385 * bugs in the ide-scsi hostadaptor emulation module
386 * from Linux instead of publishing childish patches
387 * to the comment above.
388 */
389 use_ata = TRUE;
390 device = NULL;
391 if (scgp->overbose) {
392 /*
393 * I strongly encourage people who believe that
394 * they need to patch this message away to read
395 * the messages in the Solaris USCSI libscg
396 * layer instead of wetting their tissues while
397 * being unwilling to look besides their
398 * own belly button.
399 */
400 js_fprintf((FILE *)scgp->errfile,
401 "Warning: Using badly designed ATAPI via /dev/hd* interface.\n");
402 }
403 }
404 }
405
406 if (scgp->local == NULL) {
407 scgp->local = malloc(sizeof (struct scg_local));
408 if (scgp->local == NULL)
409 return (0);
410
411 scglocal(scgp)->scgfile = -1;
412 scglocal(scgp)->pgbus = -2;
413 scglocal(scgp)->SCSIbuf = (char *)-1;
414 scglocal(scgp)->pack_id = 5;
415 scglocal(scgp)->drvers = -1;
416 scglocal(scgp)->isold = -1;
417 scglocal(scgp)->flags = 0;
418 if (use_ata)
419 scglocal(scgp)->flags |= LF_ATA;
420 scglocal(scgp)->xbufsize = 0L;
421 scglocal(scgp)->xbuf = NULL;
422
423 for (b = 0; b < MAX_SCG+MAX_ATA; b++) {
424 scglocal(scgp)->buscookies[b] = (short)-1;
425 for (t = 0; t < MAX_TGT; t++) {
426 for (l = 0; l < MAX_LUN; l++)
427 scglocal(scgp)->scgfiles[b][t][l] = (short)-1;
428 }
429 }
430 }
431
432 if (use_ata)
433 goto scanopen;
434
435 if ((device != NULL && *device != '\0') || (busno == -2 && tgt == -2))
436 goto openbydev;
437
438 scanopen:
439 /*
440 * Note that it makes no sense to scan less than all /dev/hd* devices
441 * as even /dev/hda may be a device that talks SCSI (e.g. a ATAPI
442 * notebook disk or a CD/DVD writer). The CD/DVD writer case may
443 * look silly but there may be users that did boot from a SCSI hdd
444 * and connected 4 CD/DVD writers to both IDE cables in the PC.
445 */
446 #if LINUX_VERSION_CODE <= 0x020600
447 if (use_ata)
448 #endif
449 for (i = 0; i <= 25; i++) {
450 js_snprintf(devname, sizeof (devname), "/dev/hd%c", i+'a');
451 /* O_NONBLOCK is dangerous */
452 f = open(devname, O_RDWR | O_NONBLOCK);
453 if (f < 0) {
454 /*
455 * Set up error string but let us clear it later
456 * if at least one open succeeded.
457 */
458 if (scgp->errstr)
459 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
460 "Cannot open '/dev/hd*'");
461 #ifdef EROFS
462 if (errno == EROFS) /* should we rather open RO? */
463 continue;
464 #endif
465 if (errno != ENOENT && errno != ENXIO && errno != ENODEV) {
466 if (scgp->errstr)
467 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
468 "Cannot open '%s'", devname);
469 return (0);
470 }
471 } else {
472 int iparm;
473
474 if (ioctl(f, SG_GET_TIMEOUT, &iparm) < 0) {
475 if (scgp->errstr)
476 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
477 "SCSI unsupported with '/dev/hd*'");
478 close(f);
479 continue;
480 }
481 sg_clearnblock(f); /* Be very proper about this */
482 if (sg_setup(scgp, f, busno, tgt, tlun, i))
483 return (++nopen);
484 if (busno < 0 && tgt < 0 && tlun < 0)
485 nopen++;
486 }
487 }
488 if (nopen > 0 && scgp->errstr)
489 scgp->errstr[0] = '\0';
490
491 xopen = nopen;
492 if (!use_ata) for (i = 0; i < 32; i++) {
493 js_snprintf(devname, sizeof (devname), "/dev/sg%d", i);
494 /* O_NONBLOCK is dangerous */
495 f = open(devname, O_RDWR | O_NONBLOCK);
496 if (f < 0) {
497 /*
498 * Set up error string but let us clear it later
499 * if at least one open succeeded.
500 */
501 if (scgp->errstr)
502 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
503 "Cannot open '/dev/sg*'");
504 if (errno != ENOENT && errno != ENXIO && errno != ENODEV) {
505 if (scgp->errstr)
506 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
507 "Cannot open '%s'", devname);
508 return (0);
509 }
510 } else {
511 sg_clearnblock(f); /* Be very proper about this */
512 if (sg_setup(scgp, f, busno, tgt, tlun, -1))
513 return (++nopen);
514 if (busno < 0 && tgt < 0 && tlun < 0)
515 nopen++;
516 }
517 }
518 if (nopen > 0 && scgp->errstr)
519 scgp->errstr[0] = '\0';
520
521 if (!use_ata && nopen == xopen) for (i = 0; i <= 25; i++) {
522 js_snprintf(devname, sizeof (devname), "/dev/sg%c", i+'a');
523 /* O_NONBLOCK is dangerous */
524 f = open(devname, O_RDWR | O_NONBLOCK);
525 if (f < 0) {
526 /*
527 * Set up error string but let us clear it later
528 * if at least one open succeeded.
529 */
530 if (scgp->errstr)
531 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
532 "Cannot open '/dev/sg*'");
533 if (errno != ENOENT && errno != ENXIO && errno != ENODEV) {
534 if (scgp->errstr)
535 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
536 "Cannot open '%s'", devname);
537 return (0);
538 }
539 } else {
540 sg_clearnblock(f); /* Be very proper about this */
541 if (sg_setup(scgp, f, busno, tgt, tlun, -1))
542 return (++nopen);
543 if (busno < 0 && tgt < 0 && tlun < 0)
544 nopen++;
545 }
546 }
547 if (nopen > 0 && scgp->errstr)
548 scgp->errstr[0] = '\0';
549
550 openbydev:
551 if (device != NULL && *device != '\0') {
552 b = -1;
553 if (strlen(device) == 8 && strncmp(device, "/dev/hd", 7) == 0) {
554 b = device[7] - 'a';
555 if (b < 0 || b > 25)
556 b = -1;
557 }
558 if (scgp->overbose) {
559 /*
560 * Before you patch this away, are you sure that you
561 * know what you are going to to?
562 *
563 * Note that this is a warning that helps users from
564 * cdda2wav, mkisofs and other programs (that
565 * distinguish SCSI addresses from file names) from
566 * getting unexpected results.
567 */
568 js_fprintf((FILE *)scgp->errfile,
569 "Warning: Open by 'devname' is unintentional and not supported.\n");
570 }
571 /* O_NONBLOCK is dangerous */
572 f = open(device, O_RDWR | O_NONBLOCK);
573 /* if (f < 0 && errno == ENOENT)*/
574 /* goto openpg;*/
575
576 if (f < 0) {
577 /*
578 * The pg driver has the same rules to decide whether
579 * to use openbydev. If we cannot open the device, it
580 * makes no sense to try the /dev/pg* driver.
581 */
582 if (scgp->errstr)
583 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
584 "Cannot open '%s'",
585 device);
586 return (0);
587 }
588
589 sg_clearnblock(f); /* Be very proper about this */
590 if (!sg_mapdev(scgp, f, &busno, &tgt, &tlun, 0, 0, b)) {
591 close(f);
592 /*
593 * If sg_mapdev() failes, this may be /dev/pg* device.
594 */
595 goto openpg;
596 }
597
598 #ifdef OOO
599 if (scg_scsibus(scgp) < 0)
600 scg_scsibus(scgp) = busno;
601 if (scg_target(scgp) < 0)
602 scg_target(scgp) = tgt;
603 if (scg_lun(scgp) < 0)
604 scg_lun(scgp) = tlun;
605 #endif
606
607 scg_settarget(scgp, busno, tgt, tlun);
608 if (sg_setup(scgp, f, busno, tgt, tlun, b))
609 return (++nopen);
610 }
611 openpg:
612 #ifdef USE_PG
613 nopen += pg_open(scgp, device);
614 #endif
615 if (scgp->debug > 0) for (b = 0; b < MAX_SCG+MAX_ATA; b++) {
616 js_fprintf((FILE *)scgp->errfile,
617 "Bus: %d cookie: %X\n",
618 b, scglocal(scgp)->buscookies[b]);
619 for (t = 0; t < MAX_TGT; t++) {
620 for (l = 0; l < MAX_LUN; l++) {
621 if (scglocal(scgp)->scgfiles[b][t][l] != (short)-1) {
622 js_fprintf((FILE *)scgp->errfile,
623 "file (%d,%d,%d): %d\n",
624 b, t, l, scglocal(scgp)->scgfiles[b][t][l]);
625 }
626 }
627 }
628 }
629 return (nopen);
630 }
631
632 LOCAL int
scgo_close(scgp)633 scgo_close(scgp)
634 SCSI *scgp;
635 {
636 register int f;
637 register int b;
638 register int t;
639 register int l;
640
641 if (scgp->local == NULL)
642 return (-1);
643
644 for (b = 0; b < MAX_SCG+MAX_ATA; b++) {
645 if (b == scglocal(scgp)->pgbus)
646 continue;
647 scglocal(scgp)->buscookies[b] = (short)-1;
648 for (t = 0; t < MAX_TGT; t++) {
649 for (l = 0; l < MAX_LUN; l++) {
650 f = scglocal(scgp)->scgfiles[b][t][l];
651 if (f >= 0)
652 close(f);
653 scglocal(scgp)->scgfiles[b][t][l] = (short)-1;
654 }
655 }
656 }
657 if (scglocal(scgp)->xbuf != NULL) {
658 free(scglocal(scgp)->xbuf);
659 scglocal(scgp)->xbufsize = 0L;
660 scglocal(scgp)->xbuf = NULL;
661 }
662 #ifdef USE_PG
663 pg_close(scgp);
664 #endif
665 return (0);
666 }
667
668 /*
669 * The Linux kernel becomes more and more unmaintainable.
670 * Every year, a new incompatible SCSI transport interface is added.
671 * Each of them has it's own contradictory constraints.
672 * While you cannot have O_NONBLOCK set during operation, at least one
673 * of the drivers requires O_NONBLOCK to be set during open().
674 * This is used to clear O_NONBLOCK immediately after open() succeeded.
675 */
676 LOCAL void
sg_clearnblock(f)677 sg_clearnblock(f)
678 int f;
679 {
680 int n;
681
682 n = fcntl(f, F_GETFL);
683 n &= ~O_NONBLOCK;
684 fcntl(f, F_SETFL, n);
685 }
686
687 LOCAL BOOL
sg_setup(scgp,f,busno,tgt,tlun,ataidx)688 sg_setup(scgp, f, busno, tgt, tlun, ataidx)
689 SCSI *scgp;
690 int f;
691 int busno;
692 int tgt;
693 int tlun;
694 int ataidx;
695 {
696 int n;
697 int Chan;
698 int Ino;
699 int Bus;
700 int Target;
701 int Lun;
702 BOOL onetarget = FALSE;
703
704 #ifdef SG_GET_VERSION_NUM
705 if (scglocal(scgp)->drvers < 0) {
706 scglocal(scgp)->drvers = 0;
707 if (ioctl(f, SG_GET_VERSION_NUM, &n) >= 0) {
708 scglocal(scgp)->drvers = n;
709 if (scgp->overbose) {
710 js_fprintf((FILE *)scgp->errfile,
711 "Linux sg driver version: %d.%d.%d\n",
712 n/10000, (n%10000)/100, n%100);
713 }
714 }
715 }
716 #endif
717 if (scg_scsibus(scgp) >= 0 && scg_target(scgp) >= 0 && scg_lun(scgp) >= 0)
718 onetarget = TRUE;
719
720 sg_mapdev(scgp, f, &Bus, &Target, &Lun, &Chan, &Ino, ataidx);
721
722 /*
723 * For old kernels try to make the best guess.
724 */
725 Ino |= Chan << 8;
726 n = sg_mapbus(scgp, Bus, Ino);
727 if (Bus == -1) {
728 Bus = n;
729 if (scgp->debug > 0) {
730 js_fprintf((FILE *)scgp->errfile,
731 "SCSI Bus: %d (mapped from %d)\n", Bus, Ino);
732 }
733 }
734
735 if (Bus < 0 || Bus >= (MAX_SCG+MAX_ATA) ||
736 Target < 0 || Target >= MAX_TGT ||
737 Lun < 0 || Lun >= MAX_LUN) {
738 return (FALSE);
739 }
740
741 if (scglocal(scgp)->scgfiles[Bus][Target][Lun] == (short)-1)
742 scglocal(scgp)->scgfiles[Bus][Target][Lun] = (short)f;
743
744 if (onetarget) {
745 if (Bus == busno && Target == tgt && Lun == tlun) {
746 sg_initdev(scgp, f);
747 scglocal(scgp)->scgfile = f; /* remember file for ioctl's */
748 return (TRUE);
749 } else {
750 scglocal(scgp)->scgfiles[Bus][Target][Lun] = (short)-1;
751 close(f);
752 }
753 } else {
754 /*
755 * SCSI bus scanning may cause other generic SCSI activities to
756 * fail because we set the default timeout and clear command
757 * queues (in case of the old sg driver interface).
758 */
759 sg_initdev(scgp, f);
760 if (scglocal(scgp)->scgfile < 0)
761 scglocal(scgp)->scgfile = f; /* remember file for ioctl's */
762 }
763 return (FALSE);
764 }
765
766 LOCAL void
sg_initdev(scgp,f)767 sg_initdev(scgp, f)
768 SCSI *scgp;
769 int f;
770 {
771 struct sg_rep {
772 struct sg_header hd;
773 unsigned char rbuf[100];
774 } sg_rep;
775 int n;
776 int i;
777 struct stat sb;
778
779 sg_settimeout(f, scgp->deftimeout);
780
781 /*
782 * If it's a block device, don't read.... pre Linux-2.4 /dev/sg*
783 * definitely is a character device and we only need to clear the
784 * queue for old /dev/sg* versions. If somebody ever implements
785 * raw disk access for Linux, this test may fail.
786 */
787 if (fstat(f, &sb) >= 0 && S_ISBLK(sb.st_mode))
788 return;
789
790 /* Eat any unwanted garbage from prior use of this device */
791
792 n = fcntl(f, F_GETFL); /* Be very proper about this */
793 fcntl(f, F_SETFL, n|O_NONBLOCK);
794
795 fillbytes((caddr_t)&sg_rep, sizeof (struct sg_header), '\0');
796 sg_rep.hd.reply_len = sizeof (struct sg_header);
797
798 /*
799 * This is really ugly.
800 * We come here if 'f' is related to a raw device. If Linux
801 * will ever have raw devices for /dev/hd* we may get problems.
802 * As long as there is no clean way to find out whether the
803 * filedescriptor 'f' is related to an old /dev/sg* or to
804 * /dev/hd*, we must assume that we found an old /dev/sg* and
805 * clean it up. Unfortunately, reading from /dev/hd* will
806 * Access the medium.
807 */
808 for (i = 0; i < 1000; i++) { /* Read at least 32k from /dev/sg* */
809 int ret;
810
811 ret = read(f, &sg_rep, sizeof (sg_rep));
812 if (ret > 0)
813 continue;
814 if (ret == 0 || errno == EAGAIN || errno == EIO)
815 break;
816 if (ret < 0 && i > 10) /* Stop on repeated unknown error */
817 break;
818 }
819 fcntl(f, F_SETFL, n);
820 }
821
822 LOCAL int
sg_mapbus(scgp,busno,ino)823 sg_mapbus(scgp, busno, ino)
824 SCSI *scgp;
825 int busno;
826 int ino;
827 {
828 register int i;
829
830 if (busno >= 0 && busno < (MAX_SCG+MAX_ATA)) {
831 /*
832 * SCSI_IOCTL_GET_BUS_NUMBER worked.
833 * Now we have the problem that Linux does not properly number
834 * SCSI busses. The Bus number that Linux creates really is
835 * the controller (card) number. I case of multi SCSI bus
836 * cards we are lost.
837 */
838 if (scglocal(scgp)->buscookies[busno] == (short)-1) {
839 scglocal(scgp)->buscookies[busno] = ino;
840 return (busno);
841 }
842 if (scglocal(scgp)->buscookies[busno] != (short)ino)
843 errmsgno(EX_BAD, "Warning Linux Bus mapping botch.\n");
844 return (busno);
845
846 } else for (i = 0; i < (MAX_SCG+MAX_ATA); i++) {
847 if (scglocal(scgp)->buscookies[i] == (short)-1) {
848 scglocal(scgp)->buscookies[i] = ino;
849 return (i);
850 }
851
852 if (scglocal(scgp)->buscookies[i] == ino)
853 return (i);
854 }
855 return (0);
856 }
857
858 LOCAL BOOL
sg_mapdev(scgp,f,busp,tgtp,lunp,chanp,inop,ataidx)859 sg_mapdev(scgp, f, busp, tgtp, lunp, chanp, inop, ataidx)
860 SCSI *scgp;
861 int f;
862 int *busp;
863 int *tgtp;
864 int *lunp;
865 int *chanp;
866 int *inop;
867 int ataidx;
868 {
869 struct sg_id {
870 long l1; /* target | lun << 8 | channel << 16 | low_ino << 24 */
871 long l2; /* Unique id */
872 } sg_id;
873 int Chan;
874 int Ino;
875 int Bus;
876 int Target;
877 int Lun;
878
879 if (ataidx >= 0) {
880 /*
881 * The badly designed /dev/hd* interface maps everything
882 * to 0,0,0 so we need to do the mapping ourselves.
883 */
884 *busp = (ataidx / 2) + MAX_SCG;
885 *tgtp = ataidx % 2;
886 *lunp = 0;
887 if (chanp)
888 *chanp = 0;
889 if (inop)
890 *inop = 0;
891 return (TRUE);
892 }
893 if (ioctl(f, SCSI_IOCTL_GET_IDLUN, &sg_id))
894 return (FALSE);
895 if (scgp->debug > 0) {
896 js_fprintf((FILE *)scgp->errfile,
897 "l1: 0x%lX l2: 0x%lX\n", sg_id.l1, sg_id.l2);
898 }
899 if (ioctl(f, SCSI_IOCTL_GET_BUS_NUMBER, &Bus) < 0) {
900 Bus = -1;
901 }
902
903 Target = sg_id.l1 & 0xFF;
904 Lun = (sg_id.l1 >> 8) & 0xFF;
905 Chan = (sg_id.l1 >> 16) & 0xFF;
906 Ino = (sg_id.l1 >> 24) & 0xFF;
907 if (scgp->debug > 0) {
908 js_fprintf((FILE *)scgp->errfile,
909 "Bus: %d Target: %d Lun: %d Chan: %d Ino: %d\n",
910 Bus, Target, Lun, Chan, Ino);
911 }
912 *busp = Bus;
913 *tgtp = Target;
914 *lunp = Lun;
915 if (chanp)
916 *chanp = Chan;
917 if (inop)
918 *inop = Ino;
919 return (TRUE);
920 }
921
922 #if defined(SG_SET_RESERVED_SIZE) && defined(SG_GET_RESERVED_SIZE)
923 /*
924 * The way Linux does DMA resouce management is a bit curious.
925 * It totally deviates from all other OS and forces long ugly code.
926 * If we are opening all drivers for a SCSI bus scan operation, we need
927 * to set the limit for all open devices.
928 * This may use up all kernel memory ... so do the job carefully.
929 *
930 * A big problem is that SG_SET_RESERVED_SIZE does not return any hint
931 * on whether the request did fail. The only way to find if it worked
932 * is to use SG_GET_RESERVED_SIZE to read back the current values.
933 */
934 LOCAL long
sg_raisedma(scgp,newmax)935 sg_raisedma(scgp, newmax)
936 SCSI *scgp;
937 long newmax;
938 {
939 register int b;
940 register int t;
941 register int l;
942 register int f;
943 int val;
944 int old;
945
946 /*
947 * First try to raise the DMA limit to a moderate value that
948 * most likely does not use up all kernel memory.
949 */
950 val = 126*1024;
951
952 if (val > MAX_DMA_LINUX) {
953 for (b = 0; b < (MAX_SCG+MAX_ATA); b++) {
954 for (t = 0; t < MAX_TGT; t++) {
955 for (l = 0; l < MAX_LUN; l++) {
956 if ((f = SCGO_FILENO(scgp, b, t, l)) < 0)
957 continue;
958 old = 0;
959 if (ioctl(f, SG_GET_RESERVED_SIZE, &old) < 0)
960 continue;
961 if (val > old)
962 ioctl(f, SG_SET_RESERVED_SIZE, &val);
963 }
964 }
965 }
966 }
967
968 /*
969 * Now to raise the DMA limit to what we really need.
970 */
971 if (newmax > val) {
972 val = newmax;
973 for (b = 0; b < (MAX_SCG+MAX_ATA); b++) {
974 for (t = 0; t < MAX_TGT; t++) {
975 for (l = 0; l < MAX_LUN; l++) {
976 if ((f = SCGO_FILENO(scgp, b, t, l)) < 0)
977 continue;
978 old = 0;
979 if (ioctl(f, SG_GET_RESERVED_SIZE, &old) < 0)
980 continue;
981 if (val > old)
982 ioctl(f, SG_SET_RESERVED_SIZE, &val);
983 }
984 }
985 }
986 }
987
988 /*
989 * To make sure we did not fail (the ioctl does not report errors)
990 * we need to check the DMA limits. We return the smallest value.
991 */
992 for (b = 0; b < (MAX_SCG+MAX_ATA); b++) {
993 for (t = 0; t < MAX_TGT; t++) {
994 for (l = 0; l < MAX_LUN; l++) {
995 /*
996 * XXX Remove the SG_GET_MAX_TRANSFER_LENGTH after the
997 * XXX SG_GET_RESERVED_SIZE call returns the real max DMA.
998 */
999 #ifdef SG_GET_MAX_TRANSFER_LENGTH
1000 int v2;
1001 #endif
1002 if ((f = SCGO_FILENO(scgp, b, t, l)) < 0)
1003 continue;
1004 if (ioctl(f, SG_GET_RESERVED_SIZE, &val) < 0)
1005 continue;
1006 #ifdef SG_GET_MAX_TRANSFER_LENGTH
1007 if (ioctl(f, SG_GET_MAX_TRANSFER_LENGTH, &v2) >= 0) {
1008 if (v2 < val)
1009 val = v2;
1010 }
1011 #else
1012 sg_checkdma(f, &val); /* This does not work */
1013 #endif
1014 if (scgp->debug > 0) {
1015 js_fprintf((FILE *)scgp->errfile,
1016 "Target (%d,%d,%d): DMA max %d old max: %ld\n",
1017 b, t, l, val, newmax);
1018 }
1019 if (val < newmax)
1020 newmax = val;
1021 }
1022 }
1023 }
1024 return ((long)newmax);
1025 }
1026
1027 /*
1028 * This is extremely ugly code. It would be nice if Linux could
1029 * provide an ioctl to retrieve the max DMA size...
1030 * XXX Remove this function completely at the same time when
1031 * XXX SG_GET_MAX_TRANSFER_LENGTHis removed.
1032 */
1033 LOCAL void
sg_checkdma(f,valp)1034 sg_checkdma(f, valp)
1035 int f;
1036 int *valp;
1037 {
1038 #ifdef SG_GET_PACK_ID
1039 struct stat sb;
1040 int val;
1041 char nbuf[80];
1042 int fx;
1043
1044 if (ioctl(f, SG_GET_PACK_ID, &val) < 0) /* Only works with /dev/sg* */
1045 return;
1046 val = -1;
1047 if (fstat(f, &sb) >= 0) /* Try to get instance # */
1048 val = minor(sb.st_rdev);
1049 if (val < 0)
1050 return;
1051
1052 js_snprintf(nbuf, sizeof (nbuf),
1053 "/sys/class/scsi_generic/sg%d/device/block/queue/max_hw_sectors_kb",
1054 val);
1055 fx = open(nbuf, O_RDONLY | O_NDELAY);
1056 if (fx >= 0) {
1057 if (read(fx, nbuf, sizeof (nbuf)) > 0) {
1058 val = -1;
1059 astoi(nbuf, &val);
1060 if (val > 0) {
1061 val *= 1024;
1062 if (*valp > val)
1063 *valp = val;
1064 }
1065 }
1066 close(fx);
1067 }
1068 #endif /* SG_GET_PACK_ID */
1069 }
1070 #endif
1071
1072 LOCAL long
scgo_maxdma(scgp,amt)1073 scgo_maxdma(scgp, amt)
1074 SCSI *scgp;
1075 long amt;
1076 {
1077 long maxdma = MAX_DMA_LINUX;
1078
1079 #if defined(SG_SET_RESERVED_SIZE) && defined(SG_GET_RESERVED_SIZE)
1080 /*
1081 * Use the curious new kernel interface found on Linux >= 2.2.10
1082 * This interface first appeared in 2.2.6 but it was not working.
1083 */
1084 if (scglocal(scgp)->drvers >= 20134)
1085 maxdma = sg_raisedma(scgp, amt);
1086 #endif
1087 #ifdef SG_GET_BUFSIZE
1088 /*
1089 * We assume that all /dev/sg instances use the same
1090 * maximum buffer size.
1091 */
1092 maxdma = ioctl(scglocal(scgp)->scgfile, SG_GET_BUFSIZE, 0);
1093 #endif
1094 if (maxdma < 0) {
1095 #ifdef USE_PG
1096 /*
1097 * If we only have a Parallel port, just return PP maxdma.
1098 */
1099 if (scglocal(scgp)->pgbus == 0)
1100 return (pg_maxdma(scgp, amt));
1101 #endif
1102 if (scglocal(scgp)->scgfile >= 0)
1103 maxdma = MAX_DMA_LINUX;
1104 }
1105 #ifdef USE_PG
1106 if (scg_scsibus(scgp) == scglocal(scgp)->pgbus)
1107 return (pg_maxdma(scgp, amt));
1108 if ((scg_scsibus(scgp) < 0) && (pg_maxdma(scgp, amt) < maxdma))
1109 return (pg_maxdma(scgp, amt));
1110 #endif
1111 return (maxdma);
1112 }
1113
1114 LOCAL void *
scgo_getbuf(scgp,amt)1115 scgo_getbuf(scgp, amt)
1116 SCSI *scgp;
1117 long amt;
1118 {
1119 char *ret;
1120
1121 if (scgp->debug > 0) {
1122 js_fprintf((FILE *)scgp->errfile,
1123 "scgo_getbuf: %ld bytes\n", amt);
1124 }
1125 /*
1126 * For performance reason, we allocate pagesize()
1127 * bytes before the SCSI buffer to avoid
1128 * copying the whole buffer contents when
1129 * setting up the /dev/sg data structures.
1130 */
1131 ret = valloc((size_t)(amt+getpagesize()));
1132 if (ret == NULL)
1133 return (ret);
1134 scgp->bufbase = ret;
1135 ret += getpagesize();
1136 scglocal(scgp)->SCSIbuf = ret;
1137 return ((void *)ret);
1138 }
1139
1140 LOCAL void
scgo_freebuf(scgp)1141 scgo_freebuf(scgp)
1142 SCSI *scgp;
1143 {
1144 if (scgp->bufbase)
1145 free(scgp->bufbase);
1146 scgp->bufbase = NULL;
1147 }
1148
1149 LOCAL int
scgo_numbus(scgp)1150 scgo_numbus(scgp)
1151 SCSI *scgp;
1152 {
1153 return (1000+MAX_ATA);
1154 return (MAX_SCG+MAX_ATA);
1155 }
1156
1157 LOCAL BOOL
scgo_havebus(scgp,busno)1158 scgo_havebus(scgp, busno)
1159 SCSI *scgp;
1160 int busno;
1161 {
1162 register int t;
1163 register int l;
1164
1165 if (busno >= 1000) {
1166 if (busno >= (1000+MAX_ATA))
1167 return (FALSE);
1168 busno = busno - 1000 + MAX_SCG;
1169 } else if (busno < 0 || busno >= MAX_SCG)
1170 return (FALSE);
1171
1172 if (scgp->local == NULL)
1173 return (FALSE);
1174
1175 for (t = 0; t < MAX_TGT; t++) {
1176 for (l = 0; l < MAX_LUN; l++)
1177 if (scglocal(scgp)->scgfiles[busno][t][l] >= 0)
1178 return (TRUE);
1179 }
1180 return (FALSE);
1181 }
1182
1183 LOCAL int
scgo_fileno(scgp,busno,tgt,tlun)1184 scgo_fileno(scgp, busno, tgt, tlun)
1185 SCSI *scgp;
1186 int busno;
1187 int tgt;
1188 int tlun;
1189 {
1190 if (busno >= 1000)
1191 busno = busno - 1000 + MAX_SCG;
1192
1193 if (busno < 0 || busno >= (MAX_SCG+MAX_ATA) ||
1194 tgt < 0 || tgt >= MAX_TGT ||
1195 tlun < 0 || tlun >= MAX_LUN)
1196 return (-1);
1197
1198 if (scgp->local == NULL)
1199 return (-1);
1200
1201 return ((int)scglocal(scgp)->scgfiles[busno][tgt][tlun]);
1202 }
1203
1204 LOCAL int
scgo_initiator_id(scgp)1205 scgo_initiator_id(scgp)
1206 SCSI *scgp;
1207 {
1208 #ifdef USE_PG
1209 if (scg_scsibus(scgp) == scglocal(scgp)->pgbus)
1210 return (pg_initiator_id(scgp));
1211 #endif
1212 return (-1);
1213 }
1214
1215 LOCAL int
scgo_isatapi(scgp)1216 scgo_isatapi(scgp)
1217 SCSI *scgp;
1218 {
1219 #ifdef USE_PG
1220 if (scg_scsibus(scgp) == scglocal(scgp)->pgbus)
1221 return (pg_isatapi(scgp));
1222 #endif
1223
1224 /*
1225 * The /dev/hd* interface always returns TRUE for SG_EMULATED_HOST.
1226 * So this is completely useless.
1227 */
1228 if (scglocal(scgp)->flags & LF_ATA)
1229 return (-1);
1230
1231 #ifdef SG_EMULATED_HOST
1232 {
1233 int emulated = FALSE;
1234
1235 /*
1236 * XXX Should we use this at all?
1237 * XXX The badly designed /dev/hd* interface always
1238 * XXX returns TRUE, even when used with e.g. /dev/sr0.
1239 */
1240 if (ioctl(scgp->fd, SG_EMULATED_HOST, &emulated) >= 0)
1241 return (emulated != 0);
1242 }
1243 #endif
1244 return (-1);
1245 }
1246
1247 LOCAL int
scgo_reset(scgp,what)1248 scgo_reset(scgp, what)
1249 SCSI *scgp;
1250 int what;
1251 {
1252 #ifdef SG_SCSI_RESET
1253 int f = scgp->fd;
1254 int func = -1;
1255 #endif
1256 #ifdef USE_PG
1257 if (scg_scsibus(scgp) == scglocal(scgp)->pgbus)
1258 return (pg_reset(scgp, what));
1259 #endif
1260 /*
1261 * Do we have a SCSI reset in the Linux sg driver?
1262 */
1263 #ifdef SG_SCSI_RESET
1264 /*
1265 * Newer Linux sg driver seem to finally implement it...
1266 */
1267 #ifdef SG_SCSI_RESET_NOTHING
1268 func = SG_SCSI_RESET_NOTHING;
1269 if (ioctl(f, SG_SCSI_RESET, &func) >= 0) {
1270 if (what == SCG_RESET_NOP)
1271 return (0);
1272 #ifdef SG_SCSI_RESET_DEVICE
1273 if (what == SCG_RESET_TGT) {
1274 func = SG_SCSI_RESET_DEVICE;
1275 if (ioctl(f, SG_SCSI_RESET, &func) >= 0)
1276 return (0);
1277 }
1278 #endif
1279 #ifdef SG_SCSI_RESET_BUS
1280 if (what == SCG_RESET_BUS) {
1281 func = SG_SCSI_RESET_BUS;
1282 if (ioctl(f, SG_SCSI_RESET, &func) >= 0)
1283 return (0);
1284 }
1285 #endif
1286 }
1287 #endif
1288 #endif
1289 return (-1);
1290 }
1291
1292 LOCAL void
sg_settimeout(f,tmo)1293 sg_settimeout(f, tmo)
1294 int f;
1295 int tmo;
1296 {
1297 #ifdef USER_HZ
1298 tmo *= USER_HZ;
1299 if (tmo)
1300 tmo += USER_HZ/2;
1301 #else
1302 tmo *= HZ;
1303 if (tmo)
1304 tmo += HZ/2;
1305 #endif
1306
1307 if (ioctl(f, SG_SET_TIMEOUT, &tmo) < 0)
1308 comerr("Cannot set SG_SET_TIMEOUT.\n");
1309 }
1310
1311 /*
1312 * Get misaligned int.
1313 * Needed for all recent processors (sparc/ppc/alpha)
1314 * because the /dev/sg design forces us to do misaligned
1315 * reads of integers.
1316 */
1317 #ifdef MISALIGN
1318 LOCAL int
sg_getint(ip)1319 sg_getint(ip)
1320 int *ip;
1321 {
1322 int ret;
1323 register char *cp = (char *)ip;
1324 register char *tp = (char *)&ret;
1325 register int i;
1326
1327 for (i = sizeof (int); --i >= 0; )
1328 *tp++ = *cp++;
1329
1330 return (ret);
1331 }
1332 #define GETINT(a) sg_getint(&(a))
1333 #else
1334 #define GETINT(a) (a)
1335 #endif
1336
1337 #ifdef SG_IO
1338 LOCAL int
scgo_send(scgp)1339 scgo_send(scgp)
1340 SCSI *scgp;
1341 {
1342 struct scg_cmd *sp = scgp->scmd;
1343 int ret;
1344 sg_io_hdr_t sg_io;
1345 struct timeval to;
1346 static uid_t cureuid = 0; /* XXX Hack until we have uid management */
1347
1348 if (scgp->fd < 0) {
1349 sp->error = SCG_FATAL;
1350 sp->ux_errno = EIO;
1351 return (0);
1352 }
1353 if (scglocal(scgp)->isold > 0) {
1354 return (sg_rwsend(scgp));
1355 }
1356 fillbytes((caddr_t)&sg_io, sizeof (sg_io), '\0');
1357
1358 sg_io.interface_id = 'S';
1359
1360 if (sp->flags & SCG_RECV_DATA) {
1361 sg_io.dxfer_direction = SG_DXFER_FROM_DEV;
1362 } else if (sp->size > 0) {
1363 sg_io.dxfer_direction = SG_DXFER_TO_DEV;
1364 } else {
1365 sg_io.dxfer_direction = SG_DXFER_NONE;
1366 }
1367 sg_io.cmd_len = sp->cdb_len;
1368 if (sp->sense_len > SG_MAX_SENSE)
1369 sg_io.mx_sb_len = SG_MAX_SENSE;
1370 else
1371 sg_io.mx_sb_len = sp->sense_len;
1372 sg_io.dxfer_len = sp->size;
1373 sg_io.dxferp = sp->addr;
1374 sg_io.cmdp = sp->cdb.cmd_cdb;
1375 sg_io.sbp = sp->u_sense.cmd_sense;
1376 sg_io.timeout = sp->timeout*1000;
1377 sg_io.flags |= SG_FLAG_DIRECT_IO;
1378
1379 if (cureuid != 0)
1380 seteuid(0);
1381 again:
1382 errno = 0;
1383 ret = ioctl(scgp->fd, SG_IO, &sg_io);
1384 if (ret < 0 && geterrno() == EPERM) { /* XXX Hack until we have uid management */
1385 cureuid = geteuid();
1386 if (seteuid(0) >= 0)
1387 goto again;
1388 }
1389 if (cureuid != 0)
1390 seteuid(cureuid);
1391
1392 if (scgp->debug > 0) {
1393 js_fprintf((FILE *)scgp->errfile,
1394 "ioctl ret: %d\n", ret);
1395 }
1396
1397 if (ret < 0) {
1398 sp->ux_errno = geterrno();
1399 /*
1400 * Check if SCSI command could not be send at all.
1401 * Linux usually returns EINVAL for an unknown ioctl.
1402 * In case somebody from the Linux kernel team learns that the
1403 * corect errno would be ENOTTY, we check for this errno too.
1404 */
1405 if ((sp->ux_errno == ENOTTY || sp->ux_errno == EINVAL) &&
1406 scglocal(scgp)->isold < 0) {
1407 scglocal(scgp)->isold = 1;
1408 return (sg_rwsend(scgp));
1409 }
1410 if (sp->ux_errno == ENXIO || sp->ux_errno == EPERM ||
1411 sp->ux_errno == EINVAL || sp->ux_errno == EACCES) {
1412 return (-1);
1413 }
1414 }
1415
1416 sp->u_scb.cmd_scb[0] = sg_io.status;
1417 sp->sense_count = sg_io.sb_len_wr;
1418
1419 if (scgp->debug > 0) {
1420 js_fprintf((FILE *)scgp->errfile,
1421 "host_status: %02X driver_status: %02X\n",
1422 sg_io.host_status, sg_io.driver_status);
1423 }
1424
1425 switch (sg_io.host_status) {
1426
1427 case DID_OK:
1428 /*
1429 * If there is no DMA overrun and there is a
1430 * SCSI Status byte != 0 then the SCSI cdb transport
1431 * was OK and sp->error must be SCG_NO_ERROR.
1432 */
1433 if ((sg_io.driver_status & DRIVER_SENSE) != 0) {
1434 if (sp->ux_errno == 0)
1435 sp->ux_errno = EIO;
1436
1437 if (sp->u_sense.cmd_sense[0] != 0 &&
1438 sp->u_scb.cmd_scb[0] == 0) {
1439 /*
1440 * The Linux SCSI system up to 2.4.xx
1441 * trashes the status byte in the
1442 * kernel. This is true at least for
1443 * ide-scsi emulation. Until this gets
1444 * fixed, we need this hack.
1445 */
1446 sp->u_scb.cmd_scb[0] = ST_CHK_COND;
1447 if (sp->sense_count == 0)
1448 sp->sense_count = SG_MAX_SENSE;
1449
1450 if ((sp->u_sense.cmd_sense[2] == 0) &&
1451 (sp->u_sense.cmd_sense[12] == 0) &&
1452 (sp->u_sense.cmd_sense[13] == 0)) {
1453 /*
1454 * The Linux SCSI system will
1455 * send a request sense for
1456 * even a dma underrun error.
1457 * Clear CHECK CONDITION state
1458 * in case of No Sense.
1459 */
1460 sp->u_scb.cmd_scb[0] = 0;
1461 sp->u_sense.cmd_sense[0] = 0;
1462 sp->sense_count = 0;
1463 }
1464 }
1465 }
1466 break;
1467
1468 case DID_NO_CONNECT: /* Arbitration won, retry NO_CONNECT? */
1469 sp->error = SCG_RETRYABLE;
1470 break;
1471 case DID_BAD_TARGET:
1472 sp->error = SCG_FATAL;
1473 break;
1474
1475 case DID_TIME_OUT:
1476 __scg_times(scgp);
1477
1478 if (sp->timeout > 1 && scgp->cmdstop->tv_sec == 0) {
1479 sp->u_scb.cmd_scb[0] = 0;
1480 sp->error = SCG_FATAL; /* a selection timeout */
1481 } else {
1482 sp->error = SCG_TIMEOUT;
1483 }
1484 break;
1485
1486 default:
1487 to.tv_sec = sp->timeout;
1488 to.tv_usec = 500000;
1489 __scg_times(scgp);
1490
1491 if (scgp->cmdstop->tv_sec > to.tv_sec ||
1492 (scgp->cmdstop->tv_sec == to.tv_sec &&
1493 scgp->cmdstop->tv_usec > to.tv_usec)) {
1494
1495 sp->ux_errno = 0;
1496 sp->error = SCG_TIMEOUT; /* a timeout */
1497 } else {
1498 sp->error = SCG_RETRYABLE;
1499 if (!scgp->silent) {
1500 js_fprintf((FILE *)scgp->errfile,
1501 "Unknown sg_io.host_status %X\n",
1502 sg_io.host_status);
1503 }
1504 }
1505 break;
1506 }
1507 if (sp->error && sp->ux_errno == 0)
1508 sp->ux_errno = EIO;
1509
1510 sp->resid = sg_io.resid;
1511 return (0);
1512 }
1513 #else
1514 # define sg_rwsend scgo_send
1515 #endif
1516
1517 LOCAL int
sg_rwsend(scgp)1518 sg_rwsend(scgp)
1519 SCSI *scgp;
1520 {
1521 int f = scgp->fd;
1522 struct scg_cmd *sp = scgp->scmd;
1523 struct sg_rq *sgp;
1524 struct sg_rq *sgp2;
1525 int i;
1526 int pack_len;
1527 int reply_len;
1528 int amt = sp->cdb_len;
1529 struct sg_rq {
1530 struct sg_header hd;
1531 unsigned char buf[MAX_DMA_LINUX+SCG_MAX_CMD];
1532 } sg_rq;
1533 #ifdef SG_GET_BUFSIZE /* We may use a 'sg' version 2 driver */
1534 char driver_byte;
1535 char host_byte;
1536 char msg_byte;
1537 char status_byte;
1538 #endif
1539
1540 if (f < 0) {
1541 sp->error = SCG_FATAL;
1542 sp->ux_errno = EIO;
1543 return (0);
1544 }
1545 #ifdef USE_PG
1546 if (scg_scsibus(scgp) == scglocal(scgp)->pgbus)
1547 return (pg_send(scgp));
1548 #endif
1549 if (sp->timeout != scgp->deftimeout)
1550 sg_settimeout(f, sp->timeout);
1551
1552
1553 sgp2 = sgp = &sg_rq;
1554 if (sp->addr == scglocal(scgp)->SCSIbuf) {
1555 sgp = (struct sg_rq *)
1556 (scglocal(scgp)->SCSIbuf - (sizeof (struct sg_header) + amt));
1557 sgp2 = (struct sg_rq *)
1558 (scglocal(scgp)->SCSIbuf - (sizeof (struct sg_header)));
1559 } else {
1560 if (scgp->debug > 0) {
1561 js_fprintf((FILE *)scgp->errfile,
1562 "DMA addr: 0x%8.8lX size: %d - using copy buffer\n",
1563 (long)sp->addr, sp->size);
1564 }
1565 if (sp->size > (int)(sizeof (sg_rq.buf) - SCG_MAX_CMD)) {
1566
1567 if (scglocal(scgp)->xbuf == NULL) {
1568 scglocal(scgp)->xbufsize = scgp->maxbuf;
1569 scglocal(scgp)->xbuf =
1570 malloc(scglocal(scgp)->xbufsize +
1571 SCG_MAX_CMD +
1572 sizeof (struct sg_header));
1573 if (scgp->debug > 0) {
1574 js_fprintf((FILE *)scgp->errfile,
1575 "Allocated DMA copy buffer, addr: 0x%8.8lX size: %ld\n",
1576 (long)scglocal(scgp)->xbuf,
1577 scgp->maxbuf);
1578 }
1579 }
1580 if (scglocal(scgp)->xbuf == NULL ||
1581 sp->size > scglocal(scgp)->xbufsize) {
1582 errno = ENOMEM;
1583 return (-1);
1584 }
1585 sgp2 = sgp = (struct sg_rq *)scglocal(scgp)->xbuf;
1586 }
1587 }
1588
1589 /*
1590 * This is done to avoid misaligned access of sgp->some_int
1591 */
1592 pack_len = sizeof (struct sg_header) + amt;
1593 reply_len = sizeof (struct sg_header);
1594 if (sp->flags & SCG_RECV_DATA) {
1595 reply_len += sp->size;
1596 } else {
1597 pack_len += sp->size;
1598 }
1599
1600 #ifdef MISALIGN
1601 /*
1602 * sgp->some_int may be misaligned if (sp->addr == SCSIbuf)
1603 * This is no problem on Intel porocessors, however
1604 * all other processors don't like it.
1605 * sizeof (struct sg_header) + amt is usually not a multiple of
1606 * sizeof (int). For this reason, we fill in the values into sg_rq
1607 * which is always corectly aligned and then copy it to the real
1608 * location if this location differs from sg_rq.
1609 * Never read/write directly to sgp->some_int !!!!!
1610 */
1611 fillbytes((caddr_t)&sg_rq, sizeof (struct sg_header), '\0');
1612
1613 sg_rq.hd.pack_len = pack_len;
1614 sg_rq.hd.reply_len = reply_len;
1615 sg_rq.hd.pack_id = scglocal(scgp)->pack_id++;
1616 /* sg_rq.hd.result = 0; not needed because of fillbytes() */
1617
1618 if ((caddr_t)&sg_rq != (caddr_t)sgp)
1619 movebytes((caddr_t)&sg_rq, (caddr_t)sgp, sizeof (struct sg_header));
1620 #else
1621 fillbytes((caddr_t)sgp, sizeof (struct sg_header), '\0');
1622
1623 sgp->hd.pack_len = pack_len;
1624 sgp->hd.reply_len = reply_len;
1625 sgp->hd.pack_id = scglocal(scgp)->pack_id++;
1626 /* sgp->hd.result = 0; not needed because of fillbytes() */
1627 #endif
1628 if (amt == 12)
1629 sgp->hd.twelve_byte = 1;
1630
1631
1632 for (i = 0; i < amt; i++) {
1633 sgp->buf[i] = sp->cdb.cmd_cdb[i];
1634 }
1635 if (!(sp->flags & SCG_RECV_DATA)) {
1636 if ((void *)sp->addr != (void *)&sgp->buf[amt])
1637 movebytes(sp->addr, &sgp->buf[amt], sp->size);
1638 amt += sp->size;
1639 }
1640 #ifdef SG_GET_BUFSIZE
1641 sgp->hd.want_new = 1; /* Order new behaviour */
1642 sgp->hd.cdb_len = sp->cdb_len; /* Set CDB length */
1643 if (sp->sense_len > SG_MAX_SENSE)
1644 sgp->hd.sense_len = SG_MAX_SENSE;
1645 else
1646 sgp->hd.sense_len = sp->sense_len;
1647 #endif
1648 i = sizeof (struct sg_header) + amt;
1649 if ((amt = write(f, sgp, i)) < 0) { /* write */
1650 sg_settimeout(f, scgp->deftimeout);
1651 return (-1);
1652 } else if (amt != i) {
1653 errmsg("scgo_send(%s) wrote %d bytes (expected %d).\n",
1654 scgp->cmdname, amt, i);
1655 }
1656
1657 if (sp->addr == scglocal(scgp)->SCSIbuf) {
1658 movebytes(sgp, sgp2, sizeof (struct sg_header));
1659 sgp = sgp2;
1660 }
1661 sgp->hd.sense_buffer[0] = 0;
1662 if ((amt = read(f, sgp, reply_len)) < 0) { /* read */
1663 sg_settimeout(f, scgp->deftimeout);
1664 return (-1);
1665 }
1666
1667 if (sp->flags & SCG_RECV_DATA && ((void *)sgp->buf != (void *)sp->addr)) {
1668 movebytes(sgp->buf, sp->addr, sp->size);
1669 }
1670 sp->ux_errno = GETINT(sgp->hd.result); /* Unaligned read */
1671 sp->error = SCG_NO_ERROR;
1672
1673 #ifdef SG_GET_BUFSIZE
1674 if (sgp->hd.grant_new) {
1675 sp->sense_count = sgp->hd.sense_len;
1676 pack_len = GETINT(sgp->hd.sg_cmd_status); /* Unaligned read */
1677 driver_byte = (pack_len >> 24) & 0xFF;
1678 host_byte = (pack_len >> 16) & 0xFF;
1679 msg_byte = (pack_len >> 8) & 0xFF;
1680 status_byte = pack_len & 0xFF;
1681
1682 switch (host_byte) {
1683
1684 case DID_OK:
1685 if ((driver_byte & DRIVER_SENSE ||
1686 sgp->hd.sense_buffer[0] != 0) &&
1687 status_byte == 0) {
1688 /*
1689 * The Linux SCSI system up to 2.4.xx
1690 * trashes the status byte in the
1691 * kernel. This is true at least for
1692 * ide-scsi emulation. Until this gets
1693 * fixed, we need this hack.
1694 */
1695 status_byte = ST_CHK_COND;
1696 if (sgp->hd.sense_len == 0)
1697 sgp->hd.sense_len = SG_MAX_SENSE;
1698
1699 if ((sp->u_sense.cmd_sense[2] == 0) &&
1700 (sp->u_sense.cmd_sense[12] == 0) &&
1701 (sp->u_sense.cmd_sense[13] == 0)) {
1702 /*
1703 * The Linux SCSI system will
1704 * send a request sense for
1705 * even a dma underrun error.
1706 * Clear CHECK CONDITION state
1707 * in case of No Sense.
1708 */
1709 sp->u_scb.cmd_scb[0] = 0;
1710 sp->u_sense.cmd_sense[0] = 0;
1711 sp->sense_count = 0;
1712 }
1713 }
1714 break;
1715
1716 case DID_NO_CONNECT: /* Arbitration won, retry NO_CONNECT? */
1717 sp->error = SCG_RETRYABLE;
1718 break;
1719
1720 case DID_BAD_TARGET:
1721 sp->error = SCG_FATAL;
1722 break;
1723
1724 case DID_TIME_OUT:
1725 sp->error = SCG_TIMEOUT;
1726 break;
1727
1728 default:
1729 sp->error = SCG_RETRYABLE;
1730
1731 if ((driver_byte & DRIVER_SENSE ||
1732 sgp->hd.sense_buffer[0] != 0) &&
1733 status_byte == 0) {
1734 status_byte = ST_CHK_COND;
1735 sp->error = SCG_NO_ERROR;
1736 }
1737 if (status_byte != 0 && sgp->hd.sense_len == 0) {
1738 sgp->hd.sense_len = SG_MAX_SENSE;
1739 sp->error = SCG_NO_ERROR;
1740 }
1741 break;
1742
1743 }
1744 if ((host_byte != DID_OK || status_byte != 0) && sp->ux_errno == 0)
1745 sp->ux_errno = EIO;
1746 sp->u_scb.cmd_scb[0] = status_byte;
1747 if (status_byte & ST_CHK_COND) {
1748 sp->sense_count = sgp->hd.sense_len;
1749 movebytes(sgp->hd.sense_buffer, sp->u_sense.cmd_sense, sp->sense_count);
1750 }
1751 } else
1752 #endif
1753 {
1754 if (GETINT(sgp->hd.result) == EBUSY) { /* Unaligned read */
1755 struct timeval to;
1756
1757 to.tv_sec = sp->timeout;
1758 to.tv_usec = 500000;
1759 __scg_times(scgp);
1760
1761 if (sp->timeout > 1 && scgp->cmdstop->tv_sec == 0) {
1762 sp->u_scb.cmd_scb[0] = 0;
1763 sp->ux_errno = EIO;
1764 sp->error = SCG_FATAL; /* a selection timeout */
1765 } else if (scgp->cmdstop->tv_sec < to.tv_sec ||
1766 (scgp->cmdstop->tv_sec == to.tv_sec &&
1767 scgp->cmdstop->tv_usec < to.tv_usec)) {
1768
1769 sp->ux_errno = EIO;
1770 sp->error = SCG_TIMEOUT; /* a timeout */
1771 } else {
1772 sp->error = SCG_RETRYABLE; /* may be BUS_BUSY */
1773 }
1774 }
1775
1776 if (sp->flags & SCG_RECV_DATA)
1777 sp->resid = (sp->size + sizeof (struct sg_header)) - amt;
1778 else
1779 sp->resid = 0; /* sg version1 cannot return DMA resid count */
1780
1781 if (sgp->hd.sense_buffer[0] != 0) {
1782 sp->scb.chk = 1;
1783 sp->sense_count = SG_MAX_SENSE;
1784 movebytes(sgp->hd.sense_buffer, sp->u_sense.cmd_sense, sp->sense_count);
1785 if (sp->ux_errno == 0)
1786 sp->ux_errno = EIO;
1787 }
1788 }
1789
1790 if (scgp->verbose > 0 && scgp->debug > 0) {
1791 #ifdef SG_GET_BUFSIZE
1792 js_fprintf((FILE *)scgp->errfile,
1793 "status: 0x%08X pack_len: %d, reply_len: %d pack_id: %d result: %d wn: %d gn: %d cdb_len: %d sense_len: %d sense[0]: %02X\n",
1794 GETINT(sgp->hd.sg_cmd_status),
1795 GETINT(sgp->hd.pack_len),
1796 GETINT(sgp->hd.reply_len),
1797 GETINT(sgp->hd.pack_id),
1798 GETINT(sgp->hd.result),
1799 sgp->hd.want_new,
1800 sgp->hd.grant_new,
1801 sgp->hd.cdb_len,
1802 sgp->hd.sense_len,
1803 sgp->hd.sense_buffer[0]);
1804 #else
1805 js_fprintf((FILE *)scgp->errfile,
1806 "pack_len: %d, reply_len: %d pack_id: %d result: %d sense[0]: %02X\n",
1807 GETINT(sgp->hd.pack_len),
1808 GETINT(sgp->hd.reply_len),
1809 GETINT(sgp->hd.pack_id),
1810 GETINT(sgp->hd.result),
1811 sgp->hd.sense_buffer[0]);
1812 #endif
1813 #ifdef DEBUG
1814 js_fprintf((FILE *)scgp->errfile, "sense: ");
1815 for (i = 0; i < 16; i++)
1816 js_fprintf((FILE *)scgp->errfile, "%02X ", sgp->hd.sense_buffer[i]);
1817 js_fprintf((FILE *)scgp->errfile, "\n");
1818 #endif
1819 }
1820
1821 if (sp->timeout != scgp->deftimeout)
1822 sg_settimeout(f, scgp->deftimeout);
1823 return (0);
1824 }
1825