| 1 |
/* @(#)scsi-qnx.c 1.3 04/01/15 Copyright 1998-2003 J. Schilling */
|
| 2 |
#ifndef lint
|
| 3 |
static char __sccsid[] =
|
| 4 |
"@(#)scsi-qnx.c 1.3 04/01/15 Copyright 1998-2003 J. Schilling";
|
| 5 |
#endif
|
| 6 |
/*
|
| 7 |
* Interface for QNX (Neutrino generic SCSI implementation).
|
| 8 |
* First version adopted from the OSF-1 version by
|
| 9 |
* Kevin Chiles <kchiles@qnx.com>
|
| 10 |
*
|
| 11 |
* Warning: you may change this source, but if you do that
|
| 12 |
* you need to change the _scg_version and _scg_auth* string below.
|
| 13 |
* You may not return "schily" for an SCG_AUTHOR request anymore.
|
| 14 |
* Choose your name instead of "schily" and make clear that the version
|
| 15 |
* string is related to a modified source.
|
| 16 |
*
|
| 17 |
* Copyright (c) 1998-2003 J. Schilling
|
| 18 |
*/
|
| 19 |
/*
|
| 20 |
* This program is free software; you can redistribute it and/or modify
|
| 21 |
* it under the terms of the GNU General Public License version 2
|
| 22 |
* as published by the Free Software Foundation.
|
| 23 |
*
|
| 24 |
* This program is distributed in the hope that it will be useful,
|
| 25 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 26 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 27 |
* GNU General Public License for more details.
|
| 28 |
*
|
| 29 |
* You should have received a copy of the GNU General Public License along with
|
| 30 |
* this program; see the file COPYING. If not, write to the Free Software
|
| 31 |
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
| 32 |
*/
|
| 33 |
|
| 34 |
#include <sys/mman.h>
|
| 35 |
#include <sys/types.h>
|
| 36 |
#include <sys/dcmd_cam.h>
|
| 37 |
#include <sys/cam_device.h>
|
| 38 |
|
| 39 |
/*
|
| 40 |
* Warning: you may change this source, but if you do that
|
| 41 |
* you need to change the _scg_version and _scg_auth* string below.
|
| 42 |
* You may not return "schily" for an SCG_AUTHOR request anymore.
|
| 43 |
* Choose your name instead of "schily" and make clear that the version
|
| 44 |
* string is related to a modified source.
|
| 45 |
*/
|
| 46 |
LOCAL char _scg_trans_version[] = "scsi-qnx.c-1.3"; /* The version for this transport*/
|
| 47 |
|
| 48 |
#define MAX_SCG 16 /* Max # of SCSI controllers */
|
| 49 |
#define MAX_TGT 16
|
| 50 |
#define MAX_LUN 8
|
| 51 |
|
| 52 |
struct scg_local {
|
| 53 |
int fd;
|
| 54 |
};
|
| 55 |
|
| 56 |
#define scglocal(p) ((struct scg_local *)((p)->local))
|
| 57 |
#define QNX_CAM_MAX_DMA (32*1024)
|
| 58 |
|
| 59 |
#ifndef AUTO_SENSE_LEN
|
| 60 |
# define AUTO_SENSE_LEN 32 /* SCG_MAX_SENSE */
|
| 61 |
#endif
|
| 62 |
|
| 63 |
/*
|
| 64 |
* Return version information for the low level SCSI transport code.
|
| 65 |
* This has been introduced to make it easier to trace down problems
|
| 66 |
* in applications.
|
| 67 |
*/
|
| 68 |
LOCAL char *
|
| 69 |
scgo_version(scgp, what)
|
| 70 |
SCSI *scgp;
|
| 71 |
int what;
|
| 72 |
{
|
| 73 |
if (scgp != (SCSI *)0) {
|
| 74 |
switch (what) {
|
| 75 |
|
| 76 |
case SCG_VERSION:
|
| 77 |
return (_scg_trans_version);
|
| 78 |
/*
|
| 79 |
* If you changed this source, you are not allowed to
|
| 80 |
* return "schily" for the SCG_AUTHOR request.
|
| 81 |
*/
|
| 82 |
case SCG_AUTHOR:
|
| 83 |
return ("Initial Version adopted from OSF-1 by QNX-people");
|
| 84 |
return (_scg_auth_schily);
|
| 85 |
case SCG_SCCS_ID:
|
| 86 |
return (__sccsid);
|
| 87 |
}
|
| 88 |
}
|
| 89 |
return ((char *)0);
|
| 90 |
}
|
| 91 |
|
| 92 |
LOCAL int
|
| 93 |
scgo_help(scgp, f)
|
| 94 |
SCSI *scgp;
|
| 95 |
FILE *f;
|
| 96 |
{
|
| 97 |
__scg_help(f, "CAM", "Generic transport independent SCSI (Common Access Method)",
|
| 98 |
"", "bus,target,lun", "1,2,0", TRUE, FALSE);
|
| 99 |
return (0);
|
| 100 |
}
|
| 101 |
|
| 102 |
LOCAL int
|
| 103 |
scgo_open(scgp, device)
|
| 104 |
SCSI *scgp;
|
| 105 |
char *device;
|
| 106 |
{
|
| 107 |
int fd;
|
| 108 |
|
| 109 |
if (device == NULL || *device == '\0') {
|
| 110 |
errno = EINVAL;
|
| 111 |
if (scgp->errstr)
|
| 112 |
js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
|
| 113 |
"'devname' must be specified on this OS");
|
| 114 |
return (-1);
|
| 115 |
}
|
| 116 |
|
| 117 |
if (scgp->local == NULL) {
|
| 118 |
scgp->local = malloc(sizeof (struct scg_local));
|
| 119 |
if (scgp->local == NULL)
|
| 120 |
return (0);
|
| 121 |
scglocal(scgp)->fd = -1;
|
| 122 |
}
|
| 123 |
|
| 124 |
if (scglocal(scgp)->fd != -1) /* multiple open? */
|
| 125 |
return (1);
|
| 126 |
|
| 127 |
if ((scglocal(scgp)->fd = open(device, O_RDONLY, 0)) < 0) {
|
| 128 |
if (scgp->errstr)
|
| 129 |
js_snprintf(scgp->errstr, SCSI_ERRSTR_SIZE,
|
| 130 |
"Cannot open '%s'", device);
|
| 131 |
return (-1);
|
| 132 |
}
|
| 133 |
|
| 134 |
scg_settarget(scgp, 0, 0, 0);
|
| 135 |
|
| 136 |
return (1);
|
| 137 |
}
|
| 138 |
|
| 139 |
LOCAL int
|
| 140 |
scgo_close(scgp)
|
| 141 |
SCSI *scgp;
|
| 142 |
{
|
| 143 |
if (scgp->local == NULL)
|
| 144 |
return (-1);
|
| 145 |
|
| 146 |
if (scglocal(scgp)->fd >= 0)
|
| 147 |
close(scglocal(scgp)->fd);
|
| 148 |
scglocal(scgp)->fd = -1;
|
| 149 |
return (0);
|
| 150 |
}
|
| 151 |
|
| 152 |
LOCAL long
|
| 153 |
scgo_maxdma(scgp, amt)
|
| 154 |
SCSI *scgp;
|
| 155 |
long amt;
|
| 156 |
{
|
| 157 |
long maxdma = QNX_CAM_MAX_DMA;
|
| 158 |
|
| 159 |
return (maxdma);
|
| 160 |
}
|
| 161 |
|
| 162 |
LOCAL void *
|
| 163 |
scgo_getbuf(scgp, amt)
|
| 164 |
SCSI *scgp;
|
| 165 |
long amt;
|
| 166 |
{
|
| 167 |
void *addr;
|
| 168 |
|
| 169 |
if (scgp->debug > 0) {
|
| 170 |
js_fprintf((FILE *)scgp->errfile, "scgo_getbuf: %ld bytes\n", amt);
|
| 171 |
}
|
| 172 |
|
| 173 |
if ((addr = mmap(NULL, amt, PROT_READ | PROT_WRITE | PROT_NOCACHE,
|
| 174 |
MAP_ANON | MAP_PHYS | MAP_NOX64K, NOFD, 0)) == MAP_FAILED) {
|
| 175 |
return (NULL);
|
| 176 |
}
|
| 177 |
|
| 178 |
scgp->bufbase = addr;
|
| 179 |
return (addr);
|
| 180 |
}
|
| 181 |
|
| 182 |
LOCAL void
|
| 183 |
scgo_freebuf(scgp)
|
| 184 |
SCSI *scgp;
|
| 185 |
{
|
| 186 |
if (scgp->bufbase)
|
| 187 |
munmap(scgp->bufbase, QNX_CAM_MAX_DMA);
|
| 188 |
scgp->bufbase = NULL;
|
| 189 |
}
|
| 190 |
|
| 191 |
LOCAL BOOL
|
| 192 |
scgo_havebus(scgp, busno)
|
| 193 |
SCSI *scgp;
|
| 194 |
int busno;
|
| 195 |
{
|
| 196 |
return (FALSE);
|
| 197 |
}
|
| 198 |
|
| 199 |
|
| 200 |
LOCAL int
|
| 201 |
scgo_fileno(scgp, busno, tgt, tlun)
|
| 202 |
SCSI *scgp;
|
| 203 |
int busno;
|
| 204 |
int tgt;
|
| 205 |
int tlun;
|
| 206 |
{
|
| 207 |
if (scgp->local == NULL)
|
| 208 |
return (-1);
|
| 209 |
|
| 210 |
return ((busno < 0 || busno >= MAX_SCG) ? -1 : scglocal(scgp)->fd);
|
| 211 |
}
|
| 212 |
|
| 213 |
LOCAL int
|
| 214 |
scgo_initiator_id(scgp)
|
| 215 |
SCSI *scgp;
|
| 216 |
{
|
| 217 |
return (-1);
|
| 218 |
}
|
| 219 |
|
| 220 |
LOCAL int
|
| 221 |
scgo_isatapi(scgp)
|
| 222 |
SCSI *scgp;
|
| 223 |
{
|
| 224 |
cam_devinfo_t cinfo;
|
| 225 |
|
| 226 |
if (devctl(scgp->fd, DCMD_CAM_DEVINFO, &cinfo, sizeof (cinfo), NULL) != EOK) {
|
| 227 |
return (TRUE); /* default to ATAPI */
|
| 228 |
}
|
| 229 |
return ((cinfo.flags & DEV_ATAPI) ? TRUE : FALSE);
|
| 230 |
}
|
| 231 |
|
| 232 |
LOCAL int
|
| 233 |
scgo_reset(scgp, what)
|
| 234 |
SCSI *scgp;
|
| 235 |
int what;
|
| 236 |
{
|
| 237 |
errno = EINVAL;
|
| 238 |
return (-1);
|
| 239 |
}
|
| 240 |
|
| 241 |
LOCAL int
|
| 242 |
scgo_send(scgp)
|
| 243 |
SCSI *scgp;
|
| 244 |
{
|
| 245 |
int i;
|
| 246 |
struct scg_cmd *sp;
|
| 247 |
int icnt;
|
| 248 |
iov_t iov[3];
|
| 249 |
CAM_PASS_THRU cpt;
|
| 250 |
|
| 251 |
icnt = 1;
|
| 252 |
sp = scgp->scmd;
|
| 253 |
if (scgp->fd < 0) {
|
| 254 |
sp->error = SCG_FATAL;
|
| 255 |
return (0);
|
| 256 |
}
|
| 257 |
|
| 258 |
memset(&cpt, 0, sizeof (cpt));
|
| 259 |
|
| 260 |
sp->sense_count = 0;
|
| 261 |
sp->ux_errno = 0;
|
| 262 |
sp->error = SCG_NO_ERROR;
|
| 263 |
cpt.cam_timeout = sp->timeout;
|
| 264 |
cpt.cam_cdb_len = sp->cdb_len;
|
| 265 |
memcpy(cpt.cam_cdb, sp->cdb.cmd_cdb, sp->cdb_len);
|
| 266 |
|
| 267 |
if (sp->sense_len != -1) {
|
| 268 |
cpt.cam_sense_len = sp->sense_len;
|
| 269 |
cpt.cam_sense_ptr = sizeof (cpt); /* XXX Offset from start of struct to data ??? */
|
| 270 |
icnt++;
|
| 271 |
} else {
|
| 272 |
cpt.cam_flags |= CAM_DIS_AUTOSENSE;
|
| 273 |
}
|
| 274 |
|
| 275 |
if (cpt.cam_dxfer_len = sp->size) {
|
| 276 |
icnt++;
|
| 277 |
cpt.cam_data_ptr = (paddr_t)sizeof (cpt) + cpt.cam_sense_len;
|
| 278 |
if (sp->flags & SCG_RECV_DATA) {
|
| 279 |
cpt.cam_flags |= CAM_DIR_IN;
|
| 280 |
} else {
|
| 281 |
cpt.cam_flags |= CAM_DIR_OUT;
|
| 282 |
}
|
| 283 |
} else {
|
| 284 |
cpt.cam_flags |= CAM_DIR_NONE;
|
| 285 |
}
|
| 286 |
|
| 287 |
SETIOV(&iov[0], &cpt, sizeof (cpt));
|
| 288 |
SETIOV(&iov[1], sp->u_sense.cmd_sense, cpt.cam_sense_len);
|
| 289 |
SETIOV(&iov[2], sp->addr, sp->size);
|
| 290 |
if (devctlv(scglocal(scgp)->fd, DCMD_CAM_PASS_THRU, icnt, icnt, iov, iov, NULL)) {
|
| 291 |
sp->ux_errno = geterrno();
|
| 292 |
sp->error = SCG_FATAL;
|
| 293 |
if (scgp->debug > 0) {
|
| 294 |
errmsg("cam_io failed\n");
|
| 295 |
}
|
| 296 |
return (0);
|
| 297 |
}
|
| 298 |
|
| 299 |
sp->resid = cpt.cam_resid;
|
| 300 |
sp->u_scb.cmd_scb[0] = cpt.cam_scsi_status;
|
| 301 |
|
| 302 |
switch (cpt.cam_status & CAM_STATUS_MASK) {
|
| 303 |
case CAM_REQ_CMP:
|
| 304 |
break;
|
| 305 |
|
| 306 |
case CAM_SEL_TIMEOUT:
|
| 307 |
sp->error = SCG_FATAL;
|
| 308 |
sp->ux_errno = EIO;
|
| 309 |
break;
|
| 310 |
|
| 311 |
case CAM_CMD_TIMEOUT:
|
| 312 |
sp->error = SCG_TIMEOUT;
|
| 313 |
sp->ux_errno = EIO;
|
| 314 |
break;
|
| 315 |
|
| 316 |
default:
|
| 317 |
sp->error = SCG_RETRYABLE;
|
| 318 |
sp->ux_errno = EIO;
|
| 319 |
break;
|
| 320 |
}
|
| 321 |
|
| 322 |
if (cpt.cam_status & CAM_AUTOSNS_VALID) {
|
| 323 |
sp->sense_count = min(cpt.cam_sense_len - cpt.cam_sense_resid,
|
| 324 |
SCG_MAX_SENSE);
|
| 325 |
sp->sense_count = min(sp->sense_count, sp->sense_len);
|
| 326 |
if (sp->sense_len < 0)
|
| 327 |
sp->sense_count = 0;
|
| 328 |
}
|
| 329 |
|
| 330 |
return (0);
|
| 331 |
}
|