/[debburn]/nonameyet/trunk/libscg/scsi-openserver.c
ViewVC logotype

Contents of /nonameyet/trunk/libscg/scsi-openserver.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (show annotations) (download)
Fri Aug 18 17:34:13 2006 UTC (6 years, 9 months ago) by blade
File MIME type: text/plain
File size: 25846 byte(s)
Imported the resorted version a08
1 /* @(#)scsi-openserver.c 1.31 04/01/15 Copyright 1998 J. Schilling, Santa Cruz Operation */
2 #ifndef lint
3 static char __sccsid[] =
4 "@(#)scsi-openserver.c 1.31 04/01/15 Copyright 1998 J. Schilling, Santa Cruz Operation";
5 #endif
6 /*
7 * Interface for the SCO SCSI implementation.
8 *
9 * Warning: you may change this source, but if you do that
10 * you need to change the _scg_version and _scg_auth* string below.
11 * You may not return "schily" for an SCG_AUTHOR request anymore.
12 * Choose your name instead of "schily" and make clear that the version
13 * string is related to a modified source.
14 *
15 * Copyright (c) 1998 J. Schilling, Santa Cruz Operation
16 */
17 /*
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License version 2
20 * as published by the Free Software Foundation.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License along with
28 * this program; see the file COPYING. If not, write to the Free Software
29 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 */
31
32 #undef sense
33
34 #include <sys/scsicmd.h>
35
36 /*
37 * Warning: you may change this source, but if you do that
38 * you need to change the _scg_version and _scg_auth* string below.
39 * You may not return "schily" for an SCG_AUTHOR request anymore.
40 * Choose your name instead of "schily" and make clear that the version
41 * string is related to a modified source.
42 */
43 LOCAL char _scg_trans_version[] = "scsi-openserver.c-1.31"; /* The version for this transport*/
44
45 #define MAX_SCG 16 /* Max # of cdrom devices */
46 #define MAX_TGT 16 /* Not really needed */
47 #define MAX_LUN 8 /* Not really needed */
48
49 #define MAX_DMA (64*1024)
50
51 #define MAXPATH 256 /* max length of devicepath */
52 #define MAXLINE 80 /* max length of input line */
53 #define MAXSCSI 99 /* max number of mscsi lines */
54 #define MAXDRVN 10 /* max length of drivername */
55
56 #define DEV_DIR "/tmp"
57 #define DEV_NAME "scg.s%1dt%1dl%1d"
58
59 /*
60 * ---------------------------------------------------------------------
61 * We will only deal with cdroms by default! Only if you set a specific
62 * environment variable, we will scan "all" devices !
63 * Set LIBSCG_SCAN_ALL to any value to enable access to all your SCSI
64 * devices.
65 *
66 * The upcoming support for USB will be for USB 1.1, so as this is not
67 * tested yet, we will currently ignore drives connect to the USB stack
68 * (usbha controller) regardless of having set LIBSCG_SCAN_ALL or not!
69 */
70
71 #define DEV_ROOT "/dev/dsk/0s0"
72
73 #define DEV_SDSK "/dev/rdsk/%ds0"
74 #define DEV_SROM "/dev/rcd%d"
75 #define DEV_STP "/dev/xStp%d"
76 #define DEV_SFLP "/dev/rdsk/fp%dh"
77
78 #define SCAN_DEV "%s%s%d%d%d%d"
79
80 #define SCSI_CFG "/etc/sconf -r" /* no. of configured devices */
81 #define SCSI_DEV "/etc/sconf -g %d" /* read line 'n' of mscsi tbl */
82
83 #define DRV_ATAPI "wd" /* SCO OpenServer IDE driver */
84 #define DRV_USB "usbha" /* SCO OpenServer USB driver */
85 #define DRV_NOHA "noha" /* IDE/ATAPI device configured, */
86 /* but missing ! */
87
88
89 #define T_DISK "Sdsk" /* SCO OpenServer SCSI disk */
90 #define T_CDROM "Srom" /* SCO OpenServer SCSI cdrom */
91 #define T_TAPE "Stp" /* SCO OpenServer SCSI tape */
92 #define T_FLOPPY "Sflp" /* SCO OpenServer SCSI floppy */
93
94
95 /*
96 * ---------------------------------------------------------------------
97 * Environment variables to control certain functionality
98 */
99
100 #define SCAN_ALL "LIBSCG_SCAN_ALL" /* enable access for all devices */
101 #define SCSI_USER_CMD "LIBSCG_SCSIUSERCMD" /* use old SCSIUSERCMD ioctl() */
102 #define DMA_OVERRIDE "LIBSCG_MAX_DMA" /* override MAX_DMA value */
103 #define ENABLE_USB "LIBSCG_ENABLE_USB" /* enable access of USB devices */
104
105 LOCAL int scan_all = 0; /* don't scan all devices by default */
106 LOCAL int scsiusercmd = 0; /* use SCSIUSERCMD2 ioctl by default */
107 LOCAL int enable_usb = 0; /* don't scan USB devices by default */
108 LOCAL long max_dma = MAX_DMA; /* use MAX_DMA DMA buffer by default */
109
110
111 /*
112 * ---------------------------------------------------------------------
113 * There are two scsi passthrough ioctl() on SCO OpenServer 5.0.[45],
114 * while there is only one available on SCO OpenServer 5.0.[02].
115 *
116 * The SCSIUSERCMD ioctl is available on all OpenServer 5
117 *
118 * The SCSIUSERCMD2 ioctl which executes the usercmd and reads the sense
119 * in one go, is only available from 5.0.4 onwards.
120 *
121 * By default we will use the SCSIUSERCMD2 ioctl(), in order to execute
122 * the SCSIUSERCMD ioctl() instead set the environment variable
123 * LIBSCG_SCSIUSERCMD to any value. Using the olderSCSIUSERCMD ioctl() will
124 * if the SCSI commands returns a CHECK CONDITION status, run a seperate
125 * REQUEST_SENSE command immediately. But we need to remember that in a
126 * multi-tasking environment, there might be other code which has accessed
127 * the device in between these two steps and therefore the sense code
128 * is no longer valid !!!
129 *
130 * NOTE: There are problems with the usage of AHA 154X controllers
131 * and SCSIUSERCMD2 such as nonsense (weird) output on cdrecord -scanbus
132 *
133 */
134
135
136 typedef struct scg2sdi {
137
138 int valid;
139 int open;
140 int atapi;
141 int fd;
142 int lmscsi;
143
144 } scg2sdi_t;
145
146 LOCAL scg2sdi_t sdidevs [MAX_SCG][MAX_TGT][MAX_LUN];
147
148 typedef struct amscsi {
149 char typ[MAXDRVN];
150 char drv[MAXDRVN];
151 int hba;
152 int bus;
153 int scg;
154 int tgt;
155 int lun;
156 char dev[MAXPATH];
157
158 } amscsi_t;
159
160 struct scg_local {
161 short scgfiles[MAX_SCG][MAX_TGT][MAX_LUN];
162 };
163 #define scglocal(p) ((struct scg_local *)((p)->local))
164
165 LOCAL int sort_mscsi __PR((const void *l1, const void *l2));
166 LOCAL int openserver_init __PR((SCSI *scgp));
167 LOCAL void cp_scg2sco __PR((struct scsicmd2 *sco, struct scg_cmd *scg));
168 LOCAL void cp_sco2scg __PR((struct scsicmd2 *sco, struct scg_cmd *scg));
169
170 /*
171 * -------------------------------------------------------------------------
172 * SCO OpenServer does not have a generic scsi device driver, which can
173 * be used to access any configured scsi device. But we can use the "Sxxx"
174 * scsi peripherial drivers passthrough ioctl() (SCSIUSERCMD / SCSIUSERCMD2)
175 * to send scsi user comands to any target device controlled by the
176 * corresponding target driver.
177 *
178 * This passthrough implementation for libscg currently allows to
179 * handle the following devices classes:
180 *
181 * 1. DISK handled by Sdsk
182 * 2. CD-ROM handled by Srom
183 * 3. TAPES handled by Stp
184 * 4. FLOPPY handled by Sflp
185 *
186 * NOTE: The libscg OpenServer passthrough routines have changed with
187 * cdrecord-1.8 to enable the -scanbus option. Therefore the
188 * addressing scheme is now the same as used on many other platforms
189 * like Solaris, Linux etc.
190 *
191 * ===============================================================
192 * RUN 'cdrecord -scanbus' TO SEE THE DEVICE ADDRESSES YOU CAN USE
193 * ===============================================================
194 *
195 */
196
197 /*
198 * Return version information for the low level SCSI transport code.
199 * This has been introduced to make it easier to trace down problems
200 * in applications.
201 */
202 LOCAL char *
203 scgo_version(scgp, what)
204 SCSI *scgp;
205 int what;
206 {
207 if (scgp != (SCSI *)0) {
208 switch (what) {
209
210 case SCG_VERSION:
211 return (_scg_trans_version);
212 /*
213 * If you changed this source, you are not allowed to
214 * return "schily" for the SCG_AUTHOR request.
215 */
216 case SCG_AUTHOR:
217 return (_scg_auth_schily);
218 case SCG_SCCS_ID:
219 return (__sccsid);
220 }
221 }
222 return ((char *)0);
223 }
224
225 LOCAL int
226 scgo_help(scgp, f)
227 SCSI *scgp;
228 FILE *f;
229 {
230 __scg_help(f, "SCSIUSERCMD/SCSIUSERCMD2", "Generic SCSI",
231 "", "bus,target,lun", "1,2,0", TRUE, FALSE);
232 return (0);
233 }
234
235 /*
236 * ---------------------------------------------------------------
237 * This routine sorts the amscsi_t lines on the following columns
238 * in ascending order:
239 *
240 * 1. drv - driver name
241 * 2. bus - scsibus per controller
242 * 3. tgt - target id of device
243 * 4. lun - lun of the device
244 *
245 */
246
247
248 LOCAL int
249 sort_mscsi(l1, l2)
250 const void *l1;
251 const void *l2;
252 {
253 amscsi_t *t1 = (amscsi_t *) l1;
254 amscsi_t *t2 = (amscsi_t *) l2;
255
256 if (strcmp(t1->drv, t2->drv) == 0) {
257 if (t1->bus < t2->bus) {
258 return (-1);
259
260 } else if (t1->bus > t2->bus) {
261 return (1);
262
263 } else if (t1->tgt < t2->tgt) {
264 return (-1);
265
266 } else if (t1->tgt > t2->tgt) {
267 return (1);
268
269 } else if (t1->lun < t2->lun) {
270 return (-1);
271
272 } else if (t1->lun > t2->lun) {
273 return (1);
274 } else {
275 return (0);
276 }
277 } else {
278 return (strcmp(t1->drv, t2->drv));
279 }
280 }
281
282 /*
283 * ---------------------------------------------------------------
284 * This routine is introduced to find all scsi devices which are
285 * currently configured into the kernel. This is done by reading
286 * the dynamic kernel mscsi tables and parse the resulting lines.
287 * As the output of 'sconf' is not directly usable the information
288 * found is to be sorted and re-arranged to be used with the libscg
289 * routines.
290 *
291 * NOTE: One problem is currently still not solved ! If you don't
292 * have a media in your CD-ROM/CD-Writer we are not able to
293 * do an open() and therefore will set the drive to be not
294 * available (valid=0).
295 *
296 * This will for example cause cdrecord to not list the drive
297 * in the -scanbus output.
298 *
299 */
300
301 LOCAL int
302 openserver_init(scgp)
303 SCSI *scgp;
304 {
305 FILE *cmd;
306 int nscg = -1, lhba = -1, lbus = -1;
307 int nSrom = -1, nSdsk = -1, nStp = -1, nSflp = -1;
308 int atapi, fd, nopen = 0;
309 int pos = 0, len = 0, nlm = 0;
310 int s = 0, t = 0, l = 0;
311 int ide_rootdisk = 0;
312 long dma_override = 0;
313 int mscsi;
314 char sconf[MAXLINE];
315 char lines[MAXLINE];
316 char drvid[MAXDRVN];
317 amscsi_t cmtbl[MAXSCSI];
318 char dname[MAXPATH];
319 char **evsave;
320 extern char **environ;
321
322
323 for (s = 0; s < MAX_SCG; s++) {
324 for (t = 0; t < MAX_TGT; t++) {
325 for (l = 0; l < MAX_LUN; l++) {
326 sdidevs[s][t][l].valid = 0;
327 sdidevs[s][t][l].open = -1;
328 sdidevs[s][t][l].atapi = -1;
329 sdidevs[s][t][l].fd = -1;
330 sdidevs[s][t][l].lmscsi = -1;
331 }
332 }
333 }
334
335 /* Check whether we want to use the older SCSIUSERCMD ioctl() */
336
337 if (getenv(SCSI_USER_CMD) != NULL) {
338 scsiusercmd = 1;
339 }
340
341 /*
342 * Check whether we want to scan all devices
343 */
344 if (getenv(SCAN_ALL) != NULL) {
345 scan_all = 1;
346 }
347
348 /*
349 * Check whether we want to use USB devices
350 */
351 if (getenv(ENABLE_USB) != NULL) {
352 enable_usb = 1;
353 }
354
355 /*
356 * Check whether we want to override the MAX_DMA value
357 */
358 if (getenv(DMA_OVERRIDE) != NULL) {
359 dma_override = atol(getenv(DMA_OVERRIDE));
360 if ((dma_override >= 1) && (dma_override <= (256)))
361 max_dma = dma_override * 1024;
362 }
363
364
365 /* read sconf -r and get number of kernel mscsi lines ! */
366
367 evsave = environ;
368 environ = 0;
369 if ((cmd = popen(SCSI_CFG, "r")) == NULL) {
370 if (scgp->errstr)
371 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
372 "Error popen() for \"%s\"",
373 SCSI_CFG);
374 environ = evsave;
375 return (-1);
376 }
377 environ = evsave;
378
379 if (fgets(lines, MAXLINE, cmd) == NULL) {
380 errno = EIO;
381 if (scgp->errstr)
382 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
383 "Error reading popen() for \"%s\"",
384 SCSI_CFG);
385 return (-1);
386 } else
387 nlm = atoi(lines);
388
389 if (scgp->debug > 0) {
390 js_fprintf((FILE *)scgp->errfile, "-------------------- \n");
391 js_fprintf((FILE *)scgp->errfile, "mscsi lines = %d\n", nlm);
392 js_fprintf((FILE *)scgp->errfile, "-------------------- \n");
393 }
394
395 if (pclose(cmd) == -1) {
396 if (scgp->errstr)
397 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
398 "Error pclose() for \"%s\"",
399 SCSI_CFG);
400 return (-1);
401 }
402
403 for (l = 0; l < nlm; l++) {
404
405 /* initialize cmtbl entry */
406
407 cmtbl[l].hba = -1;
408 cmtbl[l].bus = -1;
409 cmtbl[l].tgt = -1;
410 cmtbl[l].lun = -1;
411 cmtbl[l].scg = -1;
412
413 memset(cmtbl[l].typ, '\0', MAXDRVN);
414 memset(cmtbl[l].drv, '\0', MAXDRVN);
415 memset(cmtbl[l].dev, '\0', MAXDRVN);
416
417 /* read sconf -g 'n' and get line of kernel mscsi table! */
418 /* the order the lines will be received in will determine */
419 /* the device name we can use to open the device */
420
421 js_snprintf(sconf, sizeof (sconf),
422 SCSI_DEV, l + 1); /* enumeration starts with 1 */
423
424 evsave = environ;
425 environ = 0;
426 if ((cmd = popen(sconf, "r")) == NULL) {
427 if (scgp->errstr)
428 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
429 "Error popen() for \"%s\"",
430 sconf);
431 environ = evsave;
432 return (-1);
433 }
434 environ = evsave;
435
436 if (fgets(lines, MAXLINE, cmd) == NULL)
437 break;
438
439 if (pclose(cmd) == -1) {
440 if (scgp->errstr)
441 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
442 "Error pclose() for \"%s\"",
443 sconf);
444 return (-1);
445 }
446
447 sscanf(lines, SCAN_DEV, cmtbl[l].typ,
448 cmtbl[l].drv,
449 &cmtbl[l].hba,
450 &cmtbl[l].bus,
451 &cmtbl[l].tgt,
452 &cmtbl[l].lun);
453
454 if (strstr(cmtbl[l].typ, T_DISK) != NULL) {
455 js_snprintf(cmtbl[l].dev, sizeof (cmtbl[l].dev),
456 DEV_SDSK, ++nSdsk);
457 }
458
459 if (strstr(cmtbl[l].typ, T_CDROM) != NULL) {
460 js_snprintf(cmtbl[l].dev, sizeof (cmtbl[l].dev),
461 DEV_SROM, ++nSrom);
462 }
463
464 if (strstr(cmtbl[l].typ, T_TAPE) != NULL) {
465 js_snprintf(cmtbl[l].dev, sizeof (cmtbl[l].dev),
466 DEV_STP, ++nStp);
467 }
468
469 if (strstr(cmtbl[l].typ, T_FLOPPY) != NULL) {
470 js_snprintf(cmtbl[l].dev, sizeof (cmtbl[l].dev),
471 DEV_SFLP, ++nSflp);
472 }
473
474 if (scgp->debug > 0) {
475 js_fprintf((FILE *)scgp->errfile,
476 "%-4s = %5s(%d,%d,%d,%d) -> %s\n",
477 cmtbl[l].typ,
478 cmtbl[l].drv,
479 cmtbl[l].hba,
480 cmtbl[l].bus,
481 cmtbl[l].tgt,
482 cmtbl[l].lun,
483 cmtbl[l].dev);
484 }
485
486 }
487
488 if (scgp->debug > 0) {
489 js_fprintf((FILE *)scgp->errfile, "-------------------- \n");
490 js_fprintf((FILE *)scgp->errfile, "%2d DISK \n", nSdsk + 1);
491 js_fprintf((FILE *)scgp->errfile, "%2d CD-ROM\n", nSrom + 1);
492 js_fprintf((FILE *)scgp->errfile, "%2d TAPE \n", nStp + 1);
493 js_fprintf((FILE *)scgp->errfile, "%2d FLOPPY\n", nSflp + 1);
494 js_fprintf((FILE *)scgp->errfile, "-------------------- \n");
495 }
496
497 /* ok, now let's sort this array of scsi devices */
498
499 qsort((void *) cmtbl, nlm, sizeof (amscsi_t), sort_mscsi);
500
501 if (scgp->debug > 0) {
502 for (l = 0; l < nlm; l++)
503 js_fprintf((FILE *)scgp->errfile,
504 "%-4s = %5s(%d,%d,%d,%d) -> %s\n",
505 cmtbl[l].typ,
506 cmtbl[l].drv,
507 cmtbl[l].hba,
508 cmtbl[l].bus,
509 cmtbl[l].tgt,
510 cmtbl[l].lun,
511 cmtbl[l].dev);
512 js_fprintf((FILE *)scgp->errfile, "-------------------- \n");
513 }
514
515 /* find root disk controller to make it scg 0 */
516
517 /*
518 * if we have disk(s) found in the mscsi table, we still
519 * don't know if the rootdisk is among these, there can
520 * be a IDE rootdisk as well, but it's not listed in
521 * the mscsi table.
522 */
523
524 t = 0;
525 if (nSdsk > 0) {
526 for (l = 0; l < nlm; l++)
527 if (strcmp(cmtbl[l].dev, DEV_ROOT) == 0)
528 t = l;
529 } else {
530
531 /*
532 * we haven't found a disk in mscsi, so we definitely
533 * have an IDE disk on a wd adapter as IDE disks are
534 * not listed as SCSI disks in the kernel mscsi table
535 */
536 ide_rootdisk = 1;
537
538 }
539
540 if (!(ide_rootdisk) && (scgp->debug > 0)) {
541 js_fprintf((FILE *)scgp->errfile,
542 "root = %5s(%d,%d,%d,%d) -> %s\n",
543 cmtbl[t].drv,
544 cmtbl[t].hba,
545 cmtbl[t].bus,
546 cmtbl[t].tgt,
547 cmtbl[t].lun,
548 cmtbl[t].dev);
549 js_fprintf((FILE *)scgp->errfile, "-------------------- \n");
550 }
551
552 /* calculate scg from drv, hba and bus */
553
554 strcpy(drvid, "");
555
556 for (l = 0, s = t; l < nlm; l++, s = (t + l) % nlm) {
557
558 if (strcmp(drvid, cmtbl[s].drv) != 0) {
559 strcpy(drvid, cmtbl[s].drv);
560 lhba = cmtbl[s].hba;
561 lbus = cmtbl[s].bus;
562 cmtbl[s].scg = ++nscg;
563
564 } else if (cmtbl[s].hba != lhba) {
565 lhba = cmtbl[s].hba;
566 lbus = cmtbl[s].bus;
567 cmtbl[s].scg = ++nscg;
568
569 } else if (cmtbl[s].bus != lbus) {
570 lbus = cmtbl[s].bus;
571 cmtbl[s].scg = ++nscg;
572 } else {
573 cmtbl[s].scg = nscg;
574 }
575 sdidevs[cmtbl[s].scg][cmtbl[s].tgt][cmtbl[s].lun].open = 0;
576
577 /* check whether we want to open all devices or it's a CDROM */
578
579 if ((scan_all) || (strcmp(cmtbl[s].typ, T_CDROM) == 0))
580 sdidevs[cmtbl[s].scg][cmtbl[s].tgt][cmtbl[s].lun].valid = 1;
581
582 /* check whether we have an IDE/ATAPI device */
583
584 if (strcmp(cmtbl[s].drv, DRV_ATAPI) == 0)
585 sdidevs[cmtbl[s].scg][cmtbl[s].tgt][cmtbl[s].lun].atapi = 1;
586
587 /* don't open a USB device if enable_usb is not set */
588
589 if (strcmp(cmtbl[s].drv, DRV_USB) == 0)
590 sdidevs[cmtbl[s].scg][cmtbl[s].tgt][cmtbl[s].lun].valid = enable_usb;
591
592 /* don't open a IDE/ATAPI device which is missing but configured */
593
594 if (strcmp(cmtbl[s].drv, DRV_NOHA) == 0)
595 sdidevs[cmtbl[s].scg][cmtbl[s].tgt][cmtbl[s].lun].valid = 0;
596
597
598 sdidevs[cmtbl[s].scg][cmtbl[s].tgt][cmtbl[s].lun].lmscsi = s;
599
600 }
601
602
603 /* open all yet valid device nodes */
604
605 for (s = 0; s < MAX_SCG; s++) {
606 for (t = 0; t < MAX_TGT; t++) {
607 for (l = 0; l < MAX_LUN; l++) {
608
609 if (sdidevs[s][t][l].valid == 0)
610 continue;
611
612 /* Open pass-through device node */
613
614 mscsi = sdidevs[s][t][l].lmscsi;
615
616 strcpy(dname, cmtbl[mscsi].dev);
617
618 /*
619 * ------------------------------------------------------------------
620 * NOTE: If we can't open the device, we will set the device invalid!
621 * ------------------------------------------------------------------
622 */
623 errno = 0;
624 if ((fd = open(dname, (O_RDONLY | O_NONBLOCK))) < 0) {
625 sdidevs[s][t][l].valid = 0;
626 if (scgp->debug > 0) {
627 js_fprintf((FILE *)scgp->errfile,
628 "%5s(%d,%d,%d,%d) -> %s open() failed: errno = %d (%s)\n",
629 cmtbl[mscsi].drv,
630 cmtbl[mscsi].hba,
631 cmtbl[mscsi].bus,
632 cmtbl[mscsi].tgt,
633 cmtbl[mscsi].lun,
634 cmtbl[mscsi].dev,
635 errno,
636 strerror(errno));
637 }
638 continue;
639 }
640
641 if (scgp->debug > 0) {
642 js_fprintf((FILE *)scgp->errfile,
643 "%d,%d,%d => %5s(%d,%d,%d,%d) -> %d : %s \n",
644 s, t, l,
645 cmtbl[mscsi].drv,
646 cmtbl[mscsi].hba,
647 cmtbl[mscsi].bus,
648 cmtbl[mscsi].tgt,
649 cmtbl[mscsi].lun,
650 cmtbl[mscsi].scg,
651 cmtbl[mscsi].dev);
652 }
653
654 sdidevs[s][t][l].fd = fd;
655 sdidevs[s][t][l].open = 1;
656 nopen++;
657 scglocal(scgp)->scgfiles[s][t][l] = (short) fd;
658 }
659 }
660 }
661
662 if (scgp->debug > 0) {
663 js_fprintf((FILE *)scgp->errfile, "-------------------- \n");
664 js_fprintf((FILE *)scgp->errfile, "nopen = %d devices \n", nopen);
665 js_fprintf((FILE *)scgp->errfile, "-------------------- \n");
666 }
667
668 return (nopen);
669 }
670
671
672 LOCAL int
673 scgo_open(scgp, device)
674 SCSI *scgp;
675 char *device;
676 {
677 int busno = scg_scsibus(scgp);
678 int tgt = scg_target(scgp);
679 int tlun = scg_lun(scgp);
680 int f, b, t, l;
681 int nopen = 0;
682 char devname[64];
683
684 if (busno >= MAX_SCG || tgt >= MAX_TGT || tlun >= MAX_LUN) {
685 errno = EINVAL;
686 if (scgp->errstr)
687 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
688 "Illegal value for busno, target or lun '%d,%d,%d'",
689 busno, tgt, tlun);
690 return (-1);
691 }
692
693 if (scgp->local == NULL) {
694 scgp->local = malloc(sizeof (struct scg_local));
695 if (scgp->local == NULL)
696 return (0);
697
698 for (b = 0; b < MAX_SCG; b++) {
699 for (t = 0; t < MAX_TGT; t++) {
700 for (l = 0; l < MAX_LUN; l++)
701 scglocal(scgp)->scgfiles[b][t][l] = (short)-1;
702 }
703 }
704 }
705
706 if (*device != '\0') { /* we don't allow old dev usage */
707 errno = EINVAL;
708 if (scgp->errstr)
709 js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
710 "Open by 'devname' no longer supported on this OS");
711 return (-1);
712 }
713
714 return (openserver_init(scgp));
715
716 }
717
718 LOCAL int
719 scgo_close(scgp)
720 SCSI *scgp;
721 {
722 register int f;
723 register int b;
724 register int t;
725 register int l;
726
727 if (scgp->local == NULL)
728 return (-1);
729
730 for (b = 0; b < MAX_SCG; b++) {
731 for (t = 0; t < MAX_TGT; t++) {
732 for (l = 0; l < MAX_LUN; l++) {
733
734 f = scglocal(scgp)->scgfiles[b][t][l];
735 if (f >= 0)
736 close(f);
737
738 sdidevs[b][t][l].fd = -1;
739 sdidevs[b][t][l].open = 0;
740 sdidevs[b][t][l].valid = 0;
741
742 scglocal(scgp)->scgfiles[b][t][l] = (short)-1;
743 }
744 }
745 }
746 return (0);
747 }
748
749 LOCAL long
750 scgo_maxdma(scgp, amt)
751 SCSI *scgp;
752 long amt;
753 {
754 return (max_dma);
755 }
756
757
758 LOCAL void *
759 scgo_getbuf(scgp, amt)
760 SCSI *scgp;
761 long amt;
762 {
763 if (scgp->debug > 0) {
764 js_fprintf((FILE *)scgp->errfile,
765 "scgo_getbuf: %ld bytes\n", amt);
766 }
767 scgp->bufbase = valloc((size_t)(amt));
768
769 return (scgp->bufbase);
770 }
771
772 LOCAL void
773 scgo_freebuf(scgp)
774 SCSI *scgp;
775 {
776 if (scgp->bufbase)
777 free(scgp->bufbase);
778 scgp->bufbase = NULL;
779 }
780
781 LOCAL BOOL
782 scgo_havebus(scgp, busno)
783 SCSI *scgp;
784 int busno;
785 {
786 register int t;
787 register int l;
788
789 if (busno < 0 || busno >= MAX_SCG)
790 return (FALSE);
791
792 if (scgp->local == NULL)
793 return (FALSE);
794
795 for (t = 0; t < MAX_TGT; t++) {
796 for (l = 0; l < MAX_LUN; l++)
797 if (scglocal(scgp)->scgfiles[busno][t][l] >= 0)
798 return (TRUE);
799 }
800 return (FALSE);
801 }
802
803 LOCAL int
804 scgo_fileno(scgp, busno, tgt, tlun)
805 SCSI *scgp;
806 int busno;
807 int tgt;
808 int tlun;
809 {
810 if (busno < 0 || busno >= MAX_SCG ||
811 tgt < 0 || tgt >= MAX_TGT ||
812 tlun < 0 || tlun >= MAX_LUN)
813 return (-1);
814
815 if (scgp->local == NULL)
816 return (-1);
817
818 return ((int)scglocal(scgp)->scgfiles[busno][tgt][tlun]);
819 }
820
821 LOCAL int
822 scgo_initiator_id(scgp)
823 SCSI *scgp;
824 {
825 return (-1);
826
827 /*
828 * We don't know the initiator ID yet, but we can if we parse the
829 * output of the command 'cat /dev/string/cfg | grep "%adapter"'
830 *
831 * Sample line:
832 *
833 * %adapter 0xE800-0xE8FF 11 - type=alad ha=0 bus=0 id=7 fts=sto
834 *
835 * This tells us that the alad controller 0 has an id of 7 !
836 * The parsing should be done in openserver_init().
837 *
838 */
839 }
840
841 LOCAL int
842 scgo_isatapi(scgp)
843 SCSI *scgp;
844 {
845 return (sdidevs[scg_scsibus(scgp)][scg_target(scgp)][scg_lun(scgp)].atapi);
846 }
847
848 LOCAL int
849 scgo_reset(scgp, what)
850 SCSI *scgp;
851 int what;
852 {
853 errno = EINVAL;
854 return (-1); /* no scsi reset available */
855 }
856
857 LOCAL void
858 cp_scg2sco(sco, scg)
859 struct scsicmd2 *sco;
860 struct scg_cmd *scg;
861 {
862 sco->cmd.data_ptr = (char *) scg->addr;
863 sco->cmd.data_len = scg->size;
864 sco->cmd.cdb_len = scg->cdb_len;
865
866 sco->sense_len = scg->sense_len;
867 sco->sense_ptr = scg->u_sense.cmd_sense;
868
869 if (!(scg->flags & SCG_RECV_DATA) && (scg->size > 0))
870 sco->cmd.is_write = 1;
871
872 if (scg->cdb_len == SC_G0_CDBLEN)
873 memcpy(sco->cmd.cdb, &scg->cdb.g0_cdb, scg->cdb_len);
874
875 if (scg->cdb_len == SC_G1_CDBLEN)
876 memcpy(sco->cmd.cdb, &scg->cdb.g1_cdb, scg->cdb_len);
877
878 if (scg->cdb_len == SC_G5_CDBLEN)
879 memcpy(sco->cmd.cdb, &scg->cdb.g5_cdb, scg->cdb_len);
880 }
881
882
883 LOCAL void
884 cp_sco2scg(sco, scg)
885 struct scsicmd2 *sco;
886 struct scg_cmd *scg;
887 {
888 scg->size = sco->cmd.data_len;
889
890 memset(&scg->scb, 0, sizeof (scg->scb));
891
892 if (sco->sense_len > SCG_MAX_SENSE)
893 scg->sense_count = SCG_MAX_SENSE;
894 else
895 scg->sense_count = sco->sense_len;
896
897 scg->resid = 0;
898
899 scg->u_scb.cmd_scb[0] = sco->cmd.target_sts;
900
901 }
902
903
904 LOCAL int
905 scgo_send(scgp)
906 SCSI *scgp;
907 {
908 struct scg_cmd *sp = scgp->scmd;
909 struct scsicmd2 scsi_cmd;
910 int i;
911 Uchar sense_buf[SCG_MAX_SENSE];
912
913 if (scgp->fd < 0) {
914 sp->error = SCG_FATAL;
915 return (0);
916 }
917
918 memset(&scsi_cmd, 0, sizeof (scsi_cmd));
919 memset(sense_buf, 0, sizeof (sense_buf));
920 scsi_cmd.sense_ptr = sense_buf;
921 scsi_cmd.sense_len = sizeof (sense_buf);
922 cp_scg2sco(&scsi_cmd, sp);
923
924 errno = 0;
925 sp->ux_errno = 0;
926 sp->error = SCG_NO_ERROR;
927 for (;;) {
928 int ioctlStatus;
929 struct scsicmd s_cmd;
930
931 if (scsiusercmd) { /* Use SCSIUSERCMD ioctl() */
932 if (scgp->debug > 1) {
933 js_fprintf((FILE *)scgp->errfile, "calling SCSIUSERCMD ioctl()\n");
934 }
935
936 if ((ioctlStatus = ioctl(scgp->fd, SCSIUSERCMD, &(scsi_cmd.cmd))) < 0) {
937 if (scgp->debug > 1) {
938 js_fprintf((FILE *)scgp->errfile, "returning from SCSIUSERCMD ioctl()\n");
939 }
940 if (errno == EINTR)
941 continue;
942
943 cp_sco2scg(&scsi_cmd, sp);
944 sp->ux_errno = errno;
945 if (errno == 0)
946 sp->ux_errno = EIO;
947 sp->error = SCG_RETRYABLE;
948
949 return (0);
950 }
951
952 if (scgp->debug > 1) {
953 js_fprintf((FILE *)scgp->errfile, "returning from SCSIUSERCMD ioctl()\n");
954 }
955 cp_sco2scg(&scsi_cmd, sp);
956 sp->ux_errno = errno;
957
958 if (scsi_cmd.cmd.target_sts & 0x02) { /* Check Condition & get Sense */
959
960 if (sp->sense_len > SCG_MAX_SENSE)
961 sp->sense_len = SCG_MAX_SENSE;
962
963 memset((caddr_t)&s_cmd, 0, sizeof (s_cmd));
964
965 s_cmd.data_ptr = (caddr_t) sp->u_sense.cmd_sense;
966 s_cmd.data_len = sp->sense_len;
967 s_cmd.is_write = 0;
968 s_cmd.cdb[0] = SC_REQUEST_SENSE;
969
970 while (((ioctlStatus = ioctl(scgp->fd, SCSIUSERCMD, &s_cmd)) < 0) &&
971 (errno == EINTR))
972 ;
973
974 sp->sense_count = sp->sense_len;
975 sp->ux_errno = errno;
976
977 if (errno == 0)
978 sp->ux_errno = EIO;
979 sp->error = SCG_NO_ERROR;
980 }
981
982 if (scgp->debug > 0) {
983 if (errno != 0)
984 js_fprintf((FILE *)scgp->errfile, "ux_errno: %d (%s) \n", sp->ux_errno, strerror(sp->ux_errno));
985 if (sp->u_scb.cmd_scb[0] != 0)
986 js_fprintf((FILE *)scgp->errfile, "tgt_stat: %d \n", sp->u_scb.cmd_scb[0]);
987 }
988 break;
989
990 } else { /* Use SCSIUSERCMD2 ioctl() */
991 if (scgp->debug > 1) {
992 js_fprintf((FILE *)scgp->errfile, "calling SCSIUSERCMD2 ioctl()\n");
993 }
994
995 if ((ioctlStatus = ioctl(scgp->fd, SCSIUSERCMD2, &scsi_cmd)) < 0) {
996 if (scgp->debug > 1) {
997 js_fprintf((FILE *)scgp->errfile, "returning from SCSIUSERCMD2 ioctl()\n");
998 }
999 if (errno == EINTR)
1000 continue;
1001
1002 cp_sco2scg(&scsi_cmd, sp);
1003 sp->ux_errno = errno;
1004 if (errno == 0)
1005 sp->ux_errno = EIO;
1006 sp->error = SCG_RETRYABLE;
1007
1008 return (0);
1009 }
1010 if (scgp->debug > 1) {
1011 js_fprintf((FILE *)scgp->errfile, "returning from SCSIUSERCMD2 ioctl()\n");
1012 }
1013
1014 cp_sco2scg(&scsi_cmd, sp);
1015 sp->ux_errno = errno;
1016
1017 if (scsi_cmd.cmd.target_sts & 0x02) { /* Check Condition */
1018 if (errno == 0)
1019 sp->ux_errno = EIO;
1020 sp->error = SCG_NO_ERROR;
1021 }
1022
1023 if (scgp->debug > 0) {
1024 if (errno != 0)
1025 js_fprintf((FILE *)scgp->errfile, "ux_errno: %d (%s) \n", sp->ux_errno, strerror(sp->ux_errno));
1026 if (sp->u_scb.cmd_scb[0] != 0)
1027 js_fprintf((FILE *)scgp->errfile, "tgt_stat: %d \n", sp->u_scb.cmd_scb[0]);
1028 }
1029 break;
1030
1031 }
1032 }
1033
1034 return (0);
1035 }
1036
1037 #define sense u_sense.Sense

  ViewVC Help
Powered by ViewVC 1.1.5