Imported Upstream version 1.1.11
[platform/upstream/cdrkit.git] / libusal / scsi-qnx.c
1 /*
2  * This file has been modified for the cdrkit suite.
3  *
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).
6  *
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.
10  *
11  */
12
13 /* @(#)scsi-qnx.c       1.3 04/01/15 Copyright 1998-2003 J. Schilling */
14 /*
15  *      Interface for QNX (Neutrino generic SCSI implementation).
16  *      First version adopted from the OSF-1 version by
17  *      Kevin Chiles <kchiles@qnx.com>
18  *
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.
24  *
25  *      Copyright (c) 1998-2003 J. Schilling
26  */
27 /*
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.
31  *
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.
36  *
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.
40  */
41
42 #include <sys/mman.h>
43 #include <sys/types.h>
44 #include <sys/dcmd_cam.h>
45 #include <sys/cam_device.h>
46
47 /*
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.
53  */
54 static  char    _usal_trans_version[] = "scsi-qnx.c-1.3";       /* The version for this transport*/
55
56 #define MAX_SCG         16      /* Max # of SCSI controllers */
57 #define MAX_TGT         16
58 #define MAX_LUN         8
59
60 struct usal_local {
61         int             fd;
62 };
63
64 #define usallocal(p)    ((struct usal_local *)((p)->local))
65 #define QNX_CAM_MAX_DMA (32*1024)
66
67 #ifndef AUTO_SENSE_LEN
68 #       define  AUTO_SENSE_LEN  32      /* SCG_MAX_SENSE */
69 #endif
70
71 /*
72  * Return version information for the low level SCSI transport code.
73  * This has been introduced to make it easier to trace down problems
74  * in applications.
75  */
76 static char *
77 usalo_version(SCSI *usalp, int what)
78 {
79         if (usalp != (SCSI *)0) {
80                 switch (what) {
81
82                 case SCG_VERSION:
83                         return (_usal_trans_version);
84                 /*
85                  * If you changed this source, you are not allowed to
86                  * return "schily" for the SCG_AUTHOR request.
87                  */
88                 case SCG_AUTHOR:
89                         return ("Initial Version adopted from OSF-1 by QNX-people");
90                         return (_usal_auth_cdrkit);
91                 case SCG_SCCS_ID:
92                         return (__sccsid);
93                 }
94         }
95         return ((char *)0);
96 }
97
98 static int
99 usalo_help(SCSI *usalp, FILE *f)
100 {
101         __usal_help(f, "CAM", "Generic transport independent SCSI (Common Access Method)",
102                 "", "bus,target,lun", "1,2,0", TRUE, FALSE);
103         return (0);
104 }
105
106 static int
107 usalo_open(SCSI *usalp, char *device)
108 {
109         int fd;
110
111         if (device == NULL || *device == '\0') {
112                 errno = EINVAL;
113                 if (usalp->errstr)
114                         snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
115                                 "'devname' must be specified on this OS");
116                 return (-1);
117         }
118
119         if (usalp->local == NULL) {
120                 usalp->local = malloc(sizeof (struct usal_local));
121                 if (usalp->local == NULL)
122                         return (0);
123                 usallocal(usalp)->fd = -1;
124         }
125
126         if (usallocal(usalp)->fd != -1) /* multiple open? */
127                 return (1);
128
129         if ((usallocal(usalp)->fd = open(device, O_RDONLY, 0)) < 0) {
130                 if (usalp->errstr)
131                         snprintf(usalp->errstr, SCSI_ERRSTR_SIZE,
132                                 "Cannot open '%s'", device);
133                 return (-1);
134         }
135
136         usal_settarget(usalp, 0, 0, 0);
137
138         return (1);
139 }
140
141 static int
142 usalo_close(SCSI *usalp)
143 {
144         if (usalp->local == NULL)
145                 return (-1);
146
147         if (usallocal(usalp)->fd >= 0)
148                 close(usallocal(usalp)->fd);
149         usallocal(usalp)->fd = -1;
150         return (0);
151 }
152
153 static long
154 usalo_maxdma(SCSI *usalp, long amt)
155 {
156         long maxdma = QNX_CAM_MAX_DMA;
157
158         return (maxdma);
159 }
160
161 static void *
162 usalo_getbuf(SCSI *usalp, long amt)
163 {
164         void    *addr;
165
166         if (usalp->debug > 0) {
167                 fprintf((FILE *)usalp->errfile, "usalo_getbuf: %ld bytes\n", amt);
168         }
169
170         if ((addr = mmap(NULL, amt, PROT_READ | PROT_WRITE | PROT_NOCACHE,
171                                                 MAP_ANON | MAP_PHYS | MAP_NOX64K, NOFD, 0)) == MAP_FAILED) {
172                 return (NULL);
173         }
174
175         usalp->bufbase = addr;
176         return (addr);
177 }
178
179 static void
180 usalo_freebuf(SCSI *usalp)
181 {
182         if (usalp->bufbase)
183                 munmap(usalp->bufbase, QNX_CAM_MAX_DMA);
184         usalp->bufbase = NULL;
185 }
186
187 static BOOL
188 usalo_havebus(SCSI *usalp, int busno)
189 {
190         return (FALSE);
191 }
192
193
194 static int
195 usalo_fileno(SCSI *usalp, int busno, int tgt, int tlun)
196 {
197         if (usalp->local == NULL)
198                 return (-1);
199
200         return ((busno < 0 || busno >= MAX_SCG) ? -1 : usallocal(usalp)->fd);
201 }
202
203 static int
204 usalo_initiator_id(SCSI *usalp)
205 {
206         return (-1);
207 }
208
209 static int
210 usalo_isatapi(SCSI *usalp)
211 {
212         cam_devinfo_t   cinfo;
213
214         if (devctl(usalp->fd, DCMD_CAM_DEVINFO, &cinfo, sizeof (cinfo), NULL) != EOK) {
215                 return (TRUE);          /* default to ATAPI */
216         }
217         return ((cinfo.flags & DEV_ATAPI) ? TRUE : FALSE);
218 }
219
220 static int
221 usalo_reset(SCSI *usalp, int what)
222 {
223         errno = EINVAL;
224         return (-1);
225 }
226
227 static int
228 usalo_send(SCSI *usalp)
229 {
230         int             i;
231         struct usal_cmd *sp;
232         int             icnt;
233         iov_t           iov[3];
234         CAM_PASS_THRU   cpt;
235
236         icnt    = 1;
237         sp      = usalp->scmd;
238         if (usalp->fd < 0) {
239                 sp->error = SCG_FATAL;
240                 return (0);
241         }
242
243         memset(&cpt, 0, sizeof (cpt));
244
245         sp->sense_count = 0;
246         sp->ux_errno    = 0;
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);
251
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 ??? */
255                 icnt++;
256         } else {
257                 cpt.cam_flags |= CAM_DIS_AUTOSENSE;
258         }
259
260         if (cpt.cam_dxfer_len = sp->size) {
261                 icnt++;
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;
265                 } else {
266                         cpt.cam_flags |= CAM_DIR_OUT;
267                 }
268         } else {
269                 cpt.cam_flags |= CAM_DIR_NONE;
270         }
271
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");
280                 }
281                 return (0);
282         }
283
284         sp->resid               = cpt.cam_resid;
285         sp->u_scb.cmd_scb[0]    = cpt.cam_scsi_status;
286
287         switch (cpt.cam_status & CAM_STATUS_MASK) {
288                 case CAM_REQ_CMP:
289                         break;
290
291                 case CAM_SEL_TIMEOUT:
292                         sp->error       = SCG_FATAL;
293                         sp->ux_errno    = EIO;
294                         break;
295
296                 case CAM_CMD_TIMEOUT:
297                         sp->error       = SCG_TIMEOUT;
298                         sp->ux_errno    = EIO;
299                         break;
300
301                 default:
302                         sp->error       = SCG_RETRYABLE;
303                         sp->ux_errno    = EIO;
304                         break;
305         }
306
307         if (cpt.cam_status & CAM_AUTOSNS_VALID) {
308                 sp->sense_count = min(cpt.cam_sense_len - cpt.cam_sense_resid,
309                                                         SCG_MAX_SENSE);
310                 sp->sense_count = min(sp->sense_count, sp->sense_len);
311                 if (sp->sense_len < 0)
312                         sp->sense_count = 0;
313         }
314
315         return (0);
316 }