2 * This file has been modified for the cdrkit suite.
4 * The behaviour and appearence of the program code below can differ to a major
5 * extent from the version distributed by the original author(s).
7 * For details, see Changelog file distributed with the cdrkit package. If you
8 * received this file from another source then ask the distributing person for
9 * a log of modifications.
13 /* @(#)scsi-qnx.c 1.3 04/01/15 Copyright 1998-2003 J. Schilling */
15 * Interface for QNX (Neutrino generic SCSI implementation).
16 * First version adopted from the OSF-1 version by
17 * Kevin Chiles <kchiles@qnx.com>
19 * Warning: you may change this source, but if you do that
20 * you need to change the _usal_version and _usal_auth* string below.
21 * You may not return "schily" for an SCG_AUTHOR request anymore.
22 * Choose your name instead of "schily" and make clear that the version
23 * string is related to a modified source.
25 * Copyright (c) 1998-2003 J. Schilling
28 * This program is free software; you can redistribute it and/or modify
29 * it under the terms of the GNU General Public License version 2
30 * as published by the Free Software Foundation.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License along with
38 * this program; see the file COPYING. If not, write to the Free Software
39 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
43 #include <sys/types.h>
44 #include <sys/dcmd_cam.h>
45 #include <sys/cam_device.h>
48 * Warning: you may change this source, but if you do that
49 * you need to change the _usal_version and _usal_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.
54 static char _usal_trans_version[] = "scsi-qnx.c-1.3"; /* The version for this transport*/
56 #define MAX_SCG 16 /* Max # of SCSI controllers */
64 #define usallocal(p) ((struct usal_local *)((p)->local))
65 #define QNX_CAM_MAX_DMA (32*1024)
67 #ifndef AUTO_SENSE_LEN
68 # define AUTO_SENSE_LEN 32 /* SCG_MAX_SENSE */
72 * Return version information for the low level SCSI transport code.
73 * This has been introduced to make it easier to trace down problems
77 usalo_version(SCSI *usalp, int what)
79 if (usalp != (SCSI *)0) {
83 return (_usal_trans_version);
85 * If you changed this source, you are not allowed to
86 * return "schily" for the SCG_AUTHOR request.
89 return ("Initial Version adopted from OSF-1 by QNX-people");
90 return (_usal_auth_cdrkit);
99 usalo_help(SCSI *usalp, FILE *f)
101 __usal_help(f, "CAM", "Generic transport independent SCSI (Common Access Method)",
102 "", "bus,target,lun", "1,2,0", TRUE, FALSE);
107 usalo_open(SCSI *usalp, char *device)
111 if (device == NULL || *device == '\0') {
114 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
115 "'devname' must be specified on this OS");
119 if (usalp->local == NULL) {
120 usalp->local = malloc(sizeof (struct usal_local));
121 if (usalp->local == NULL)
123 usallocal(usalp)->fd = -1;
126 if (usallocal(usalp)->fd != -1) /* multiple open? */
129 if ((usallocal(usalp)->fd = open(device, O_RDONLY, 0)) < 0) {
131 snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
132 "Cannot open '%s'", device);
136 usal_settarget(usalp, 0, 0, 0);
142 usalo_close(SCSI *usalp)
144 if (usalp->local == NULL)
147 if (usallocal(usalp)->fd >= 0)
148 close(usallocal(usalp)->fd);
149 usallocal(usalp)->fd = -1;
154 usalo_maxdma(SCSI *usalp, long amt)
156 long maxdma = QNX_CAM_MAX_DMA;
162 usalo_getbuf(SCSI *usalp, long amt)
166 if (usalp->debug > 0) {
167 fprintf((FILE *)usalp->errfile, "usalo_getbuf: %ld bytes\n", amt);
170 if ((addr = mmap(NULL, amt, PROT_READ | PROT_WRITE | PROT_NOCACHE,
171 MAP_ANON | MAP_PHYS | MAP_NOX64K, NOFD, 0)) == MAP_FAILED) {
175 usalp->bufbase = addr;
180 usalo_freebuf(SCSI *usalp)
183 munmap(usalp->bufbase, QNX_CAM_MAX_DMA);
184 usalp->bufbase = NULL;
188 usalo_havebus(SCSI *usalp, int busno)
195 usalo_fileno(SCSI *usalp, int busno, int tgt, int tlun)
197 if (usalp->local == NULL)
200 return ((busno < 0 || busno >= MAX_SCG) ? -1 : usallocal(usalp)->fd);
204 usalo_initiator_id(SCSI *usalp)
210 usalo_isatapi(SCSI *usalp)
214 if (devctl(usalp->fd, DCMD_CAM_DEVINFO, &cinfo, sizeof (cinfo), NULL) != EOK) {
215 return (TRUE); /* default to ATAPI */
217 return ((cinfo.flags & DEV_ATAPI) ? TRUE : FALSE);
221 usalo_reset(SCSI *usalp, int what)
228 usalo_send(SCSI *usalp)
239 sp->error = SCG_FATAL;
243 memset(&cpt, 0, sizeof (cpt));
247 sp->error = SCG_NO_ERROR;
248 cpt.cam_timeout = sp->timeout;
249 cpt.cam_cdb_len = sp->cdb_len;
250 memcpy(cpt.cam_cdb, sp->cdb.cmd_cdb, sp->cdb_len);
252 if (sp->sense_len != -1) {
253 cpt.cam_sense_len = sp->sense_len;
254 cpt.cam_sense_ptr = sizeof (cpt); /* XXX Offset from start of struct to data ??? */
257 cpt.cam_flags |= CAM_DIS_AUTOSENSE;
260 if (cpt.cam_dxfer_len = sp->size) {
262 cpt.cam_data_ptr = (paddr_t)sizeof (cpt) + cpt.cam_sense_len;
263 if (sp->flags & SCG_RECV_DATA) {
264 cpt.cam_flags |= CAM_DIR_IN;
266 cpt.cam_flags |= CAM_DIR_OUT;
269 cpt.cam_flags |= CAM_DIR_NONE;
272 SETIOV(&iov[0], &cpt, sizeof (cpt));
273 SETIOV(&iov[1], sp->u_sense.cmd_sense, cpt.cam_sense_len);
274 SETIOV(&iov[2], sp->addr, sp->size);
275 if (devctlv(usallocal(usalp)->fd, DCMD_CAM_PASS_THRU, icnt, icnt, iov, iov, NULL)) {
276 sp->ux_errno = geterrno();
277 sp->error = SCG_FATAL;
278 if (usalp->debug > 0) {
279 errmsg("cam_io failed\n");
284 sp->resid = cpt.cam_resid;
285 sp->u_scb.cmd_scb[0] = cpt.cam_scsi_status;
287 switch (cpt.cam_status & CAM_STATUS_MASK) {
291 case CAM_SEL_TIMEOUT:
292 sp->error = SCG_FATAL;
296 case CAM_CMD_TIMEOUT:
297 sp->error = SCG_TIMEOUT;
302 sp->error = SCG_RETRYABLE;
307 if (cpt.cam_status & CAM_AUTOSNS_VALID) {
308 sp->sense_count = min(cpt.cam_sense_len - cpt.cam_sense_resid,
310 sp->sense_count = min(sp->sense_count, sp->sense_len);
311 if (sp->sense_len < 0)