Deleted Added
full compact
scsi_ch.c (164906) scsi_ch.c (168752)
1/*-
2 * Copyright (c) 1997 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28/*
29 * Derived from the NetBSD SCSI changer driver.
30 *
31 * $NetBSD: ch.c,v 1.32 1998/01/12 09:49:12 thorpej Exp $
32 *
33 */
34/*-
35 * Copyright (c) 1996, 1997 Jason R. Thorpe <thorpej@and.com>
36 * All rights reserved.
37 *
38 * Partially based on an autochanger driver written by Stefan Grefen
39 * and on an autochanger driver written by the Systems Programming Group
40 * at the University of Utah Computer Science Department.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgements:
52 * This product includes software developed by Jason R. Thorpe
53 * for And Communications, http://www.and.com/
54 * 4. The name of the author may not be used to endorse or promote products
55 * derived from this software without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
62 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
63 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
64 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
65 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * SUCH DAMAGE.
68 */
69
70#include <sys/cdefs.h>
1/*-
2 * Copyright (c) 1997 Justin T. Gibbs.
3 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification, immediately at the beginning of the file.
12 * 2. The name of the author may not be used to endorse or promote products
13 * derived from this software without specific prior written permission.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
19 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28/*
29 * Derived from the NetBSD SCSI changer driver.
30 *
31 * $NetBSD: ch.c,v 1.32 1998/01/12 09:49:12 thorpej Exp $
32 *
33 */
34/*-
35 * Copyright (c) 1996, 1997 Jason R. Thorpe <thorpej@and.com>
36 * All rights reserved.
37 *
38 * Partially based on an autochanger driver written by Stefan Grefen
39 * and on an autochanger driver written by the Systems Programming Group
40 * at the University of Utah Computer Science Department.
41 *
42 * Redistribution and use in source and binary forms, with or without
43 * modification, are permitted provided that the following conditions
44 * are met:
45 * 1. Redistributions of source code must retain the above copyright
46 * notice, this list of conditions and the following disclaimer.
47 * 2. Redistributions in binary form must reproduce the above copyright
48 * notice, this list of conditions and the following disclaimer in the
49 * documentation and/or other materials provided with the distribution.
50 * 3. All advertising materials mentioning features or use of this software
51 * must display the following acknowledgements:
52 * This product includes software developed by Jason R. Thorpe
53 * for And Communications, http://www.and.com/
54 * 4. The name of the author may not be used to endorse or promote products
55 * derived from this software without specific prior written permission.
56 *
57 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
58 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
59 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
60 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
61 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
62 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
63 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
64 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
65 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
66 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
67 * SUCH DAMAGE.
68 */
69
70#include <sys/cdefs.h>
71__FBSDID("$FreeBSD: head/sys/cam/scsi/scsi_ch.c 164906 2006-12-05 07:45:28Z mjacob $");
71__FBSDID("$FreeBSD: head/sys/cam/scsi/scsi_ch.c 168752 2007-04-15 08:49:19Z scottl $");
72
73#include <sys/param.h>
74#include <sys/queue.h>
75#include <sys/systm.h>
76#include <sys/kernel.h>
77#include <sys/types.h>
78#include <sys/malloc.h>
79#include <sys/fcntl.h>
80#include <sys/conf.h>
81#include <sys/chio.h>
82#include <sys/errno.h>
83#include <sys/devicestat.h>
84
85#include <cam/cam.h>
86#include <cam/cam_ccb.h>
87#include <cam/cam_periph.h>
88#include <cam/cam_xpt_periph.h>
89#include <cam/cam_debug.h>
90
91#include <cam/scsi/scsi_all.h>
92#include <cam/scsi/scsi_message.h>
93#include <cam/scsi/scsi_ch.h>
94
95/*
96 * Timeout definitions for various changer related commands. They may
97 * be too short for some devices (especially the timeout for INITIALIZE
98 * ELEMENT STATUS).
99 */
100
101static const u_int32_t CH_TIMEOUT_MODE_SENSE = 6000;
102static const u_int32_t CH_TIMEOUT_MOVE_MEDIUM = 100000;
103static const u_int32_t CH_TIMEOUT_EXCHANGE_MEDIUM = 100000;
104static const u_int32_t CH_TIMEOUT_POSITION_TO_ELEMENT = 100000;
105static const u_int32_t CH_TIMEOUT_READ_ELEMENT_STATUS = 10000;
106static const u_int32_t CH_TIMEOUT_SEND_VOLTAG = 10000;
107static const u_int32_t CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS = 500000;
108
109typedef enum {
110 CH_FLAG_INVALID = 0x001,
111 CH_FLAG_OPEN = 0x002
112} ch_flags;
113
114typedef enum {
115 CH_STATE_PROBE,
116 CH_STATE_NORMAL
117} ch_state;
118
119typedef enum {
120 CH_CCB_PROBE,
121 CH_CCB_WAITING
122} ch_ccb_types;
123
124typedef enum {
125 CH_Q_NONE = 0x00,
126 CH_Q_NO_DBD = 0x01
127} ch_quirks;
128
129#define ccb_state ppriv_field0
130#define ccb_bp ppriv_ptr1
131
132struct scsi_mode_sense_data {
133 struct scsi_mode_header_6 header;
134 struct scsi_mode_blk_desc blk_desc;
135 union {
136 struct page_element_address_assignment ea;
137 struct page_transport_geometry_parameters tg;
138 struct page_device_capabilities cap;
139 } pages;
140};
141
142struct ch_softc {
143 ch_flags flags;
144 ch_state state;
145 ch_quirks quirks;
146 union ccb saved_ccb;
147 struct devstat *device_stats;
148 struct cdev *dev;
149
150 int sc_picker; /* current picker */
151
152 /*
153 * The following information is obtained from the
154 * element address assignment page.
155 */
156 int sc_firsts[CHET_MAX + 1]; /* firsts */
157 int sc_counts[CHET_MAX + 1]; /* counts */
158
159 /*
160 * The following mask defines the legal combinations
161 * of elements for the MOVE MEDIUM command.
162 */
163 u_int8_t sc_movemask[CHET_MAX + 1];
164
165 /*
166 * As above, but for EXCHANGE MEDIUM.
167 */
168 u_int8_t sc_exchangemask[CHET_MAX + 1];
169
170 /*
171 * Quirks; see below. XXX KDM not implemented yet
172 */
173 int sc_settledelay; /* delay for settle */
174};
175
72
73#include <sys/param.h>
74#include <sys/queue.h>
75#include <sys/systm.h>
76#include <sys/kernel.h>
77#include <sys/types.h>
78#include <sys/malloc.h>
79#include <sys/fcntl.h>
80#include <sys/conf.h>
81#include <sys/chio.h>
82#include <sys/errno.h>
83#include <sys/devicestat.h>
84
85#include <cam/cam.h>
86#include <cam/cam_ccb.h>
87#include <cam/cam_periph.h>
88#include <cam/cam_xpt_periph.h>
89#include <cam/cam_debug.h>
90
91#include <cam/scsi/scsi_all.h>
92#include <cam/scsi/scsi_message.h>
93#include <cam/scsi/scsi_ch.h>
94
95/*
96 * Timeout definitions for various changer related commands. They may
97 * be too short for some devices (especially the timeout for INITIALIZE
98 * ELEMENT STATUS).
99 */
100
101static const u_int32_t CH_TIMEOUT_MODE_SENSE = 6000;
102static const u_int32_t CH_TIMEOUT_MOVE_MEDIUM = 100000;
103static const u_int32_t CH_TIMEOUT_EXCHANGE_MEDIUM = 100000;
104static const u_int32_t CH_TIMEOUT_POSITION_TO_ELEMENT = 100000;
105static const u_int32_t CH_TIMEOUT_READ_ELEMENT_STATUS = 10000;
106static const u_int32_t CH_TIMEOUT_SEND_VOLTAG = 10000;
107static const u_int32_t CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS = 500000;
108
109typedef enum {
110 CH_FLAG_INVALID = 0x001,
111 CH_FLAG_OPEN = 0x002
112} ch_flags;
113
114typedef enum {
115 CH_STATE_PROBE,
116 CH_STATE_NORMAL
117} ch_state;
118
119typedef enum {
120 CH_CCB_PROBE,
121 CH_CCB_WAITING
122} ch_ccb_types;
123
124typedef enum {
125 CH_Q_NONE = 0x00,
126 CH_Q_NO_DBD = 0x01
127} ch_quirks;
128
129#define ccb_state ppriv_field0
130#define ccb_bp ppriv_ptr1
131
132struct scsi_mode_sense_data {
133 struct scsi_mode_header_6 header;
134 struct scsi_mode_blk_desc blk_desc;
135 union {
136 struct page_element_address_assignment ea;
137 struct page_transport_geometry_parameters tg;
138 struct page_device_capabilities cap;
139 } pages;
140};
141
142struct ch_softc {
143 ch_flags flags;
144 ch_state state;
145 ch_quirks quirks;
146 union ccb saved_ccb;
147 struct devstat *device_stats;
148 struct cdev *dev;
149
150 int sc_picker; /* current picker */
151
152 /*
153 * The following information is obtained from the
154 * element address assignment page.
155 */
156 int sc_firsts[CHET_MAX + 1]; /* firsts */
157 int sc_counts[CHET_MAX + 1]; /* counts */
158
159 /*
160 * The following mask defines the legal combinations
161 * of elements for the MOVE MEDIUM command.
162 */
163 u_int8_t sc_movemask[CHET_MAX + 1];
164
165 /*
166 * As above, but for EXCHANGE MEDIUM.
167 */
168 u_int8_t sc_exchangemask[CHET_MAX + 1];
169
170 /*
171 * Quirks; see below. XXX KDM not implemented yet
172 */
173 int sc_settledelay; /* delay for settle */
174};
175
176#define CHUNIT(x) (minor((x)))
177
178static d_open_t chopen;
179static d_close_t chclose;
180static d_ioctl_t chioctl;
181static periph_init_t chinit;
182static periph_ctor_t chregister;
183static periph_oninv_t choninvalidate;
184static periph_dtor_t chcleanup;
185static periph_start_t chstart;
186static void chasync(void *callback_arg, u_int32_t code,
187 struct cam_path *path, void *arg);
188static void chdone(struct cam_periph *periph,
189 union ccb *done_ccb);
190static int cherror(union ccb *ccb, u_int32_t cam_flags,
191 u_int32_t sense_flags);
192static int chmove(struct cam_periph *periph,
193 struct changer_move *cm);
194static int chexchange(struct cam_periph *periph,
195 struct changer_exchange *ce);
196static int chposition(struct cam_periph *periph,
197 struct changer_position *cp);
198static int chgetelemstatus(struct cam_periph *periph,
199 struct changer_element_status_request *csr);
200static int chsetvoltag(struct cam_periph *periph,
201 struct changer_set_voltag_request *csvr);
202static int chielem(struct cam_periph *periph,
203 unsigned int timeout);
204static int chgetparams(struct cam_periph *periph);
205
206static struct periph_driver chdriver =
207{
208 chinit, "ch",
209 TAILQ_HEAD_INITIALIZER(chdriver.units), /* generation */ 0
210};
211
212PERIPHDRIVER_DECLARE(ch, chdriver);
213
214static struct cdevsw ch_cdevsw = {
215 .d_version = D_VERSION,
176static d_open_t chopen;
177static d_close_t chclose;
178static d_ioctl_t chioctl;
179static periph_init_t chinit;
180static periph_ctor_t chregister;
181static periph_oninv_t choninvalidate;
182static periph_dtor_t chcleanup;
183static periph_start_t chstart;
184static void chasync(void *callback_arg, u_int32_t code,
185 struct cam_path *path, void *arg);
186static void chdone(struct cam_periph *periph,
187 union ccb *done_ccb);
188static int cherror(union ccb *ccb, u_int32_t cam_flags,
189 u_int32_t sense_flags);
190static int chmove(struct cam_periph *periph,
191 struct changer_move *cm);
192static int chexchange(struct cam_periph *periph,
193 struct changer_exchange *ce);
194static int chposition(struct cam_periph *periph,
195 struct changer_position *cp);
196static int chgetelemstatus(struct cam_periph *periph,
197 struct changer_element_status_request *csr);
198static int chsetvoltag(struct cam_periph *periph,
199 struct changer_set_voltag_request *csvr);
200static int chielem(struct cam_periph *periph,
201 unsigned int timeout);
202static int chgetparams(struct cam_periph *periph);
203
204static struct periph_driver chdriver =
205{
206 chinit, "ch",
207 TAILQ_HEAD_INITIALIZER(chdriver.units), /* generation */ 0
208};
209
210PERIPHDRIVER_DECLARE(ch, chdriver);
211
212static struct cdevsw ch_cdevsw = {
213 .d_version = D_VERSION,
216 .d_flags = D_NEEDGIANT,
214 .d_flags = 0,
217 .d_open = chopen,
218 .d_close = chclose,
219 .d_ioctl = chioctl,
220 .d_name = "ch",
221};
222
223static void
224chinit(void)
225{
226 cam_status status;
227 struct cam_path *path;
228
229 /*
230 * Install a global async callback. This callback will
231 * receive async callbacks like "new device found".
232 */
233 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
234 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
235
236 if (status == CAM_REQ_CMP) {
237 struct ccb_setasync csa;
238
239 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
240 csa.ccb_h.func_code = XPT_SASYNC_CB;
241 csa.event_enable = AC_FOUND_DEVICE;
242 csa.callback = chasync;
243 csa.callback_arg = NULL;
244 xpt_action((union ccb *)&csa);
245 status = csa.ccb_h.status;
246 xpt_free_path(path);
247 }
248
249 if (status != CAM_REQ_CMP) {
250 printf("ch: Failed to attach master async callback "
251 "due to status 0x%x!\n", status);
252 }
253}
254
255static void
256choninvalidate(struct cam_periph *periph)
257{
258 struct ch_softc *softc;
259 struct ccb_setasync csa;
260
261 softc = (struct ch_softc *)periph->softc;
262
263 /*
264 * De-register any async callbacks.
265 */
266 xpt_setup_ccb(&csa.ccb_h, periph->path,
267 /* priority */ 5);
268 csa.ccb_h.func_code = XPT_SASYNC_CB;
269 csa.event_enable = 0;
270 csa.callback = chasync;
271 csa.callback_arg = periph;
272 xpt_action((union ccb *)&csa);
273
274 softc->flags |= CH_FLAG_INVALID;
275
276 xpt_print(periph->path, "lost device\n");
277
278}
279
280static void
281chcleanup(struct cam_periph *periph)
282{
283 struct ch_softc *softc;
284
285 softc = (struct ch_softc *)periph->softc;
286
287 devstat_remove_entry(softc->device_stats);
288 destroy_dev(softc->dev);
289 xpt_print(periph->path, "removing device entry\n");
290 free(softc, M_DEVBUF);
291}
292
293static void
294chasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
295{
296 struct cam_periph *periph;
297
298 periph = (struct cam_periph *)callback_arg;
299
300 switch(code) {
301 case AC_FOUND_DEVICE:
302 {
303 struct ccb_getdev *cgd;
304 cam_status status;
305
306 cgd = (struct ccb_getdev *)arg;
307 if (cgd == NULL)
308 break;
309
310 if (SID_TYPE(&cgd->inq_data)!= T_CHANGER)
311 break;
312
313 /*
314 * Allocate a peripheral instance for
315 * this device and start the probe
316 * process.
317 */
318 status = cam_periph_alloc(chregister, choninvalidate,
319 chcleanup, chstart, "ch",
320 CAM_PERIPH_BIO, cgd->ccb_h.path,
321 chasync, AC_FOUND_DEVICE, cgd);
322
323 if (status != CAM_REQ_CMP
324 && status != CAM_REQ_INPROG)
325 printf("chasync: Unable to probe new device "
326 "due to status 0x%x\n", status);
327
328 break;
329
330 }
331 default:
332 cam_periph_async(periph, code, path, arg);
333 break;
334 }
335}
336
337static cam_status
338chregister(struct cam_periph *periph, void *arg)
339{
340 struct ch_softc *softc;
341 struct ccb_setasync csa;
342 struct ccb_getdev *cgd;
343
344 cgd = (struct ccb_getdev *)arg;
345 if (periph == NULL) {
346 printf("chregister: periph was NULL!!\n");
347 return(CAM_REQ_CMP_ERR);
348 }
349
350 if (cgd == NULL) {
351 printf("chregister: no getdev CCB, can't register device\n");
352 return(CAM_REQ_CMP_ERR);
353 }
354
355 softc = (struct ch_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
356
357 if (softc == NULL) {
358 printf("chregister: Unable to probe new device. "
359 "Unable to allocate softc\n");
360 return(CAM_REQ_CMP_ERR);
361 }
362
363 bzero(softc, sizeof(*softc));
364 softc->state = CH_STATE_PROBE;
365 periph->softc = softc;
366 softc->quirks = CH_Q_NONE;
367
368 /*
369 * Changers don't have a blocksize, and obviously don't support
370 * tagged queueing.
371 */
372 softc->device_stats = devstat_new_entry("ch",
373 periph->unit_number, 0,
374 DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
375 SID_TYPE(&cgd->inq_data)| DEVSTAT_TYPE_IF_SCSI,
376 DEVSTAT_PRIORITY_OTHER);
377
378 /* Register the device */
215 .d_open = chopen,
216 .d_close = chclose,
217 .d_ioctl = chioctl,
218 .d_name = "ch",
219};
220
221static void
222chinit(void)
223{
224 cam_status status;
225 struct cam_path *path;
226
227 /*
228 * Install a global async callback. This callback will
229 * receive async callbacks like "new device found".
230 */
231 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
232 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
233
234 if (status == CAM_REQ_CMP) {
235 struct ccb_setasync csa;
236
237 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
238 csa.ccb_h.func_code = XPT_SASYNC_CB;
239 csa.event_enable = AC_FOUND_DEVICE;
240 csa.callback = chasync;
241 csa.callback_arg = NULL;
242 xpt_action((union ccb *)&csa);
243 status = csa.ccb_h.status;
244 xpt_free_path(path);
245 }
246
247 if (status != CAM_REQ_CMP) {
248 printf("ch: Failed to attach master async callback "
249 "due to status 0x%x!\n", status);
250 }
251}
252
253static void
254choninvalidate(struct cam_periph *periph)
255{
256 struct ch_softc *softc;
257 struct ccb_setasync csa;
258
259 softc = (struct ch_softc *)periph->softc;
260
261 /*
262 * De-register any async callbacks.
263 */
264 xpt_setup_ccb(&csa.ccb_h, periph->path,
265 /* priority */ 5);
266 csa.ccb_h.func_code = XPT_SASYNC_CB;
267 csa.event_enable = 0;
268 csa.callback = chasync;
269 csa.callback_arg = periph;
270 xpt_action((union ccb *)&csa);
271
272 softc->flags |= CH_FLAG_INVALID;
273
274 xpt_print(periph->path, "lost device\n");
275
276}
277
278static void
279chcleanup(struct cam_periph *periph)
280{
281 struct ch_softc *softc;
282
283 softc = (struct ch_softc *)periph->softc;
284
285 devstat_remove_entry(softc->device_stats);
286 destroy_dev(softc->dev);
287 xpt_print(periph->path, "removing device entry\n");
288 free(softc, M_DEVBUF);
289}
290
291static void
292chasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
293{
294 struct cam_periph *periph;
295
296 periph = (struct cam_periph *)callback_arg;
297
298 switch(code) {
299 case AC_FOUND_DEVICE:
300 {
301 struct ccb_getdev *cgd;
302 cam_status status;
303
304 cgd = (struct ccb_getdev *)arg;
305 if (cgd == NULL)
306 break;
307
308 if (SID_TYPE(&cgd->inq_data)!= T_CHANGER)
309 break;
310
311 /*
312 * Allocate a peripheral instance for
313 * this device and start the probe
314 * process.
315 */
316 status = cam_periph_alloc(chregister, choninvalidate,
317 chcleanup, chstart, "ch",
318 CAM_PERIPH_BIO, cgd->ccb_h.path,
319 chasync, AC_FOUND_DEVICE, cgd);
320
321 if (status != CAM_REQ_CMP
322 && status != CAM_REQ_INPROG)
323 printf("chasync: Unable to probe new device "
324 "due to status 0x%x\n", status);
325
326 break;
327
328 }
329 default:
330 cam_periph_async(periph, code, path, arg);
331 break;
332 }
333}
334
335static cam_status
336chregister(struct cam_periph *periph, void *arg)
337{
338 struct ch_softc *softc;
339 struct ccb_setasync csa;
340 struct ccb_getdev *cgd;
341
342 cgd = (struct ccb_getdev *)arg;
343 if (periph == NULL) {
344 printf("chregister: periph was NULL!!\n");
345 return(CAM_REQ_CMP_ERR);
346 }
347
348 if (cgd == NULL) {
349 printf("chregister: no getdev CCB, can't register device\n");
350 return(CAM_REQ_CMP_ERR);
351 }
352
353 softc = (struct ch_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
354
355 if (softc == NULL) {
356 printf("chregister: Unable to probe new device. "
357 "Unable to allocate softc\n");
358 return(CAM_REQ_CMP_ERR);
359 }
360
361 bzero(softc, sizeof(*softc));
362 softc->state = CH_STATE_PROBE;
363 periph->softc = softc;
364 softc->quirks = CH_Q_NONE;
365
366 /*
367 * Changers don't have a blocksize, and obviously don't support
368 * tagged queueing.
369 */
370 softc->device_stats = devstat_new_entry("ch",
371 periph->unit_number, 0,
372 DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
373 SID_TYPE(&cgd->inq_data)| DEVSTAT_TYPE_IF_SCSI,
374 DEVSTAT_PRIORITY_OTHER);
375
376 /* Register the device */
377 cam_periph_unlock(periph);
379 softc->dev = make_dev(&ch_cdevsw, periph->unit_number, UID_ROOT,
380 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
381 periph->unit_number);
378 softc->dev = make_dev(&ch_cdevsw, periph->unit_number, UID_ROOT,
379 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
380 periph->unit_number);
381 cam_periph_lock(periph);
382 softc->dev->si_drv1 = periph;
383
384 /*
385 * Add an async callback so that we get
386 * notified if this device goes away.
387 */
388 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
389 csa.ccb_h.func_code = XPT_SASYNC_CB;
390 csa.event_enable = AC_LOST_DEVICE;
391 csa.callback = chasync;
392 csa.callback_arg = periph;
393 xpt_action((union ccb *)&csa);
394
395 /*
382 softc->dev->si_drv1 = periph;
383
384 /*
385 * Add an async callback so that we get
386 * notified if this device goes away.
387 */
388 xpt_setup_ccb(&csa.ccb_h, periph->path, /* priority */ 5);
389 csa.ccb_h.func_code = XPT_SASYNC_CB;
390 csa.event_enable = AC_LOST_DEVICE;
391 csa.callback = chasync;
392 csa.callback_arg = periph;
393 xpt_action((union ccb *)&csa);
394
395 /*
396 * Lock this peripheral until we are setup.
396 * Lock this periph until we are setup.
397 * This first call can't block
398 */
397 * This first call can't block
398 */
399 (void)cam_periph_lock(periph, PRIBIO);
399 (void)cam_periph_hold(periph, PRIBIO);
400 xpt_schedule(periph, /*priority*/5);
401
402 return(CAM_REQ_CMP);
403}
404
405static int
406chopen(struct cdev *dev, int flags, int fmt, struct thread *td)
407{
408 struct cam_periph *periph;
409 struct ch_softc *softc;
410 int error;
400 xpt_schedule(periph, /*priority*/5);
401
402 return(CAM_REQ_CMP);
403}
404
405static int
406chopen(struct cdev *dev, int flags, int fmt, struct thread *td)
407{
408 struct cam_periph *periph;
409 struct ch_softc *softc;
410 int error;
411 int s;
412
413 periph = (struct cam_periph *)dev->si_drv1;
411
412 periph = (struct cam_periph *)dev->si_drv1;
414 if (periph == NULL)
415 return(ENXIO);
413 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
414 return (ENXIO);
416
417 softc = (struct ch_softc *)periph->softc;
418
415
416 softc = (struct ch_softc *)periph->softc;
417
419 s = splsoftcam();
418 cam_periph_lock(periph);
419
420 if (softc->flags & CH_FLAG_INVALID) {
420 if (softc->flags & CH_FLAG_INVALID) {
421 splx(s);
421 cam_periph_unlock(periph);
422 cam_periph_release(periph);
422 return(ENXIO);
423 }
424
423 return(ENXIO);
424 }
425
425 if ((error = cam_periph_lock(periph, PRIBIO | PCATCH)) != 0) {
426 splx(s);
426 if ((softc->flags & CH_FLAG_OPEN) == 0)
427 softc->flags |= CH_FLAG_OPEN;
428 else
429 cam_periph_release(periph);
430
431 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
432 cam_periph_unlock(periph);
433 cam_periph_release(periph);
427 return (error);
428 }
434 return (error);
435 }
429
430 splx(s);
431
436
432 if ((softc->flags & CH_FLAG_OPEN) == 0) {
433 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
434 return(ENXIO);
435 softc->flags |= CH_FLAG_OPEN;
436 }
437
438 /*
439 * Load information about this changer device into the softc.
440 */
441 if ((error = chgetparams(periph)) != 0) {
442 softc->flags &= ~CH_FLAG_OPEN;
443 cam_periph_unlock(periph);
444 cam_periph_release(periph);
445 return(error);
446 }
447
437 /*
438 * Load information about this changer device into the softc.
439 */
440 if ((error = chgetparams(periph)) != 0) {
441 softc->flags &= ~CH_FLAG_OPEN;
442 cam_periph_unlock(periph);
443 cam_periph_release(periph);
444 return(error);
445 }
446
447 cam_periph_unhold(periph);
448 cam_periph_unlock(periph);
449
450 return(error);
451}
452
453static int
454chclose(struct cdev *dev, int flag, int fmt, struct thread *td)
455{
456 struct cam_periph *periph;
457 struct ch_softc *softc;
458 int error;
459
460 error = 0;
461
462 periph = (struct cam_periph *)dev->si_drv1;
463 if (periph == NULL)
464 return(ENXIO);
465
466 softc = (struct ch_softc *)periph->softc;
467
448 cam_periph_unlock(periph);
449
450 return(error);
451}
452
453static int
454chclose(struct cdev *dev, int flag, int fmt, struct thread *td)
455{
456 struct cam_periph *periph;
457 struct ch_softc *softc;
458 int error;
459
460 error = 0;
461
462 periph = (struct cam_periph *)dev->si_drv1;
463 if (periph == NULL)
464 return(ENXIO);
465
466 softc = (struct ch_softc *)periph->softc;
467
468 if ((error = cam_periph_lock(periph, PRIBIO)) != 0)
469 return(error);
468 cam_periph_lock(periph);
470
471 softc->flags &= ~CH_FLAG_OPEN;
472
473 cam_periph_unlock(periph);
474 cam_periph_release(periph);
475
476 return(0);
477}
478
479static void
480chstart(struct cam_periph *periph, union ccb *start_ccb)
481{
482 struct ch_softc *softc;
469
470 softc->flags &= ~CH_FLAG_OPEN;
471
472 cam_periph_unlock(periph);
473 cam_periph_release(periph);
474
475 return(0);
476}
477
478static void
479chstart(struct cam_periph *periph, union ccb *start_ccb)
480{
481 struct ch_softc *softc;
483 int s;
484
485 softc = (struct ch_softc *)periph->softc;
486
487 switch (softc->state) {
488 case CH_STATE_NORMAL:
489 {
482
483 softc = (struct ch_softc *)periph->softc;
484
485 switch (softc->state) {
486 case CH_STATE_NORMAL:
487 {
490 s = splbio();
491 if (periph->immediate_priority <= periph->pinfo.priority){
492 start_ccb->ccb_h.ccb_state = CH_CCB_WAITING;
493
494 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
495 periph_links.sle);
496 periph->immediate_priority = CAM_PRIORITY_NONE;
488 if (periph->immediate_priority <= periph->pinfo.priority){
489 start_ccb->ccb_h.ccb_state = CH_CCB_WAITING;
490
491 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
492 periph_links.sle);
493 periph->immediate_priority = CAM_PRIORITY_NONE;
497 splx(s);
498 wakeup(&periph->ccb_list);
494 wakeup(&periph->ccb_list);
499 } else
500 splx(s);
495 }
501 break;
502 }
503 case CH_STATE_PROBE:
504 {
505 int mode_buffer_len;
506 void *mode_buffer;
507
508 /*
509 * Include the block descriptor when calculating the mode
510 * buffer length,
511 */
512 mode_buffer_len = sizeof(struct scsi_mode_header_6) +
513 sizeof(struct scsi_mode_blk_desc) +
514 sizeof(struct page_element_address_assignment);
515
516 mode_buffer = malloc(mode_buffer_len, M_TEMP, M_NOWAIT);
517
518 if (mode_buffer == NULL) {
519 printf("chstart: couldn't malloc mode sense data\n");
520 break;
521 }
522 bzero(mode_buffer, mode_buffer_len);
523
524 /*
525 * Get the element address assignment page.
526 */
527 scsi_mode_sense(&start_ccb->csio,
528 /* retries */ 1,
529 /* cbfcnp */ chdone,
530 /* tag_action */ MSG_SIMPLE_Q_TAG,
531 /* dbd */ (softc->quirks & CH_Q_NO_DBD) ?
532 FALSE : TRUE,
533 /* page_code */ SMS_PAGE_CTRL_CURRENT,
534 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
535 /* param_buf */ (u_int8_t *)mode_buffer,
536 /* param_len */ mode_buffer_len,
537 /* sense_len */ SSD_FULL_SIZE,
538 /* timeout */ CH_TIMEOUT_MODE_SENSE);
539
540 start_ccb->ccb_h.ccb_bp = NULL;
541 start_ccb->ccb_h.ccb_state = CH_CCB_PROBE;
542 xpt_action(start_ccb);
543 break;
544 }
545 }
546}
547
548static void
549chdone(struct cam_periph *periph, union ccb *done_ccb)
550{
551 struct ch_softc *softc;
552 struct ccb_scsiio *csio;
553
554 softc = (struct ch_softc *)periph->softc;
555 csio = &done_ccb->csio;
556
557 switch(done_ccb->ccb_h.ccb_state) {
558 case CH_CCB_PROBE:
559 {
560 struct scsi_mode_header_6 *mode_header;
561 struct page_element_address_assignment *ea;
562 char announce_buf[80];
563
564
565 mode_header = (struct scsi_mode_header_6 *)csio->data_ptr;
566
567 ea = (struct page_element_address_assignment *)
568 find_mode_page_6(mode_header);
569
570 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){
571
572 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
573 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
574 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
575 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
576 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
577 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
578 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
579 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
580 softc->sc_picker = softc->sc_firsts[CHET_MT];
581
582#define PLURAL(c) (c) == 1 ? "" : "s"
583 snprintf(announce_buf, sizeof(announce_buf),
584 "%d slot%s, %d drive%s, "
585 "%d picker%s, %d portal%s",
586 softc->sc_counts[CHET_ST],
587 PLURAL(softc->sc_counts[CHET_ST]),
588 softc->sc_counts[CHET_DT],
589 PLURAL(softc->sc_counts[CHET_DT]),
590 softc->sc_counts[CHET_MT],
591 PLURAL(softc->sc_counts[CHET_MT]),
592 softc->sc_counts[CHET_IE],
593 PLURAL(softc->sc_counts[CHET_IE]));
594#undef PLURAL
595 } else {
596 int error;
597
598 error = cherror(done_ccb, CAM_RETRY_SELTO,
599 SF_RETRY_UA | SF_NO_PRINT);
600 /*
601 * Retry any UNIT ATTENTION type errors. They
602 * are expected at boot.
603 */
604 if (error == ERESTART) {
605 /*
606 * A retry was scheuled, so
607 * just return.
608 */
609 return;
610 } else if (error != 0) {
611 int retry_scheduled;
612 struct scsi_mode_sense_6 *sms;
613
614 sms = (struct scsi_mode_sense_6 *)
615 done_ccb->csio.cdb_io.cdb_bytes;
616
617 /*
618 * Check to see if block descriptors were
619 * disabled. Some devices don't like that.
620 * We're taking advantage of the fact that
621 * the first few bytes of the 6 and 10 byte
622 * mode sense commands are the same. If
623 * block descriptors were disabled, enable
624 * them and re-send the command.
625 */
626 if (sms->byte2 & SMS_DBD) {
627 sms->byte2 &= ~SMS_DBD;
628 xpt_action(done_ccb);
629 softc->quirks |= CH_Q_NO_DBD;
630 retry_scheduled = 1;
631 } else
632 retry_scheduled = 0;
633
634 /* Don't wedge this device's queue */
635 cam_release_devq(done_ccb->ccb_h.path,
636 /*relsim_flags*/0,
637 /*reduction*/0,
638 /*timeout*/0,
639 /*getcount_only*/0);
640
641 if (retry_scheduled)
642 return;
643
644 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK)
645 == CAM_SCSI_STATUS_ERROR)
646 scsi_sense_print(&done_ccb->csio);
647 else {
648 xpt_print(periph->path,
649 "got CAM status %#x\n",
650 done_ccb->ccb_h.status);
651 }
652 xpt_print(periph->path, "fatal error, failed "
653 "to attach to device\n");
654
655 cam_periph_invalidate(periph);
656
657 announce_buf[0] = '\0';
658 }
659 }
660 if (announce_buf[0] != '\0')
661 xpt_announce_periph(periph, announce_buf);
662 softc->state = CH_STATE_NORMAL;
663 free(mode_header, M_TEMP);
664 /*
665 * Since our peripheral may be invalidated by an error
666 * above or an external event, we must release our CCB
667 * before releasing the probe lock on the peripheral.
668 * The peripheral will only go away once the last lock
669 * is removed, and we need it around for the CCB release
670 * operation.
671 */
672 xpt_release_ccb(done_ccb);
496 break;
497 }
498 case CH_STATE_PROBE:
499 {
500 int mode_buffer_len;
501 void *mode_buffer;
502
503 /*
504 * Include the block descriptor when calculating the mode
505 * buffer length,
506 */
507 mode_buffer_len = sizeof(struct scsi_mode_header_6) +
508 sizeof(struct scsi_mode_blk_desc) +
509 sizeof(struct page_element_address_assignment);
510
511 mode_buffer = malloc(mode_buffer_len, M_TEMP, M_NOWAIT);
512
513 if (mode_buffer == NULL) {
514 printf("chstart: couldn't malloc mode sense data\n");
515 break;
516 }
517 bzero(mode_buffer, mode_buffer_len);
518
519 /*
520 * Get the element address assignment page.
521 */
522 scsi_mode_sense(&start_ccb->csio,
523 /* retries */ 1,
524 /* cbfcnp */ chdone,
525 /* tag_action */ MSG_SIMPLE_Q_TAG,
526 /* dbd */ (softc->quirks & CH_Q_NO_DBD) ?
527 FALSE : TRUE,
528 /* page_code */ SMS_PAGE_CTRL_CURRENT,
529 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
530 /* param_buf */ (u_int8_t *)mode_buffer,
531 /* param_len */ mode_buffer_len,
532 /* sense_len */ SSD_FULL_SIZE,
533 /* timeout */ CH_TIMEOUT_MODE_SENSE);
534
535 start_ccb->ccb_h.ccb_bp = NULL;
536 start_ccb->ccb_h.ccb_state = CH_CCB_PROBE;
537 xpt_action(start_ccb);
538 break;
539 }
540 }
541}
542
543static void
544chdone(struct cam_periph *periph, union ccb *done_ccb)
545{
546 struct ch_softc *softc;
547 struct ccb_scsiio *csio;
548
549 softc = (struct ch_softc *)periph->softc;
550 csio = &done_ccb->csio;
551
552 switch(done_ccb->ccb_h.ccb_state) {
553 case CH_CCB_PROBE:
554 {
555 struct scsi_mode_header_6 *mode_header;
556 struct page_element_address_assignment *ea;
557 char announce_buf[80];
558
559
560 mode_header = (struct scsi_mode_header_6 *)csio->data_ptr;
561
562 ea = (struct page_element_address_assignment *)
563 find_mode_page_6(mode_header);
564
565 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){
566
567 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
568 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
569 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
570 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
571 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
572 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
573 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
574 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
575 softc->sc_picker = softc->sc_firsts[CHET_MT];
576
577#define PLURAL(c) (c) == 1 ? "" : "s"
578 snprintf(announce_buf, sizeof(announce_buf),
579 "%d slot%s, %d drive%s, "
580 "%d picker%s, %d portal%s",
581 softc->sc_counts[CHET_ST],
582 PLURAL(softc->sc_counts[CHET_ST]),
583 softc->sc_counts[CHET_DT],
584 PLURAL(softc->sc_counts[CHET_DT]),
585 softc->sc_counts[CHET_MT],
586 PLURAL(softc->sc_counts[CHET_MT]),
587 softc->sc_counts[CHET_IE],
588 PLURAL(softc->sc_counts[CHET_IE]));
589#undef PLURAL
590 } else {
591 int error;
592
593 error = cherror(done_ccb, CAM_RETRY_SELTO,
594 SF_RETRY_UA | SF_NO_PRINT);
595 /*
596 * Retry any UNIT ATTENTION type errors. They
597 * are expected at boot.
598 */
599 if (error == ERESTART) {
600 /*
601 * A retry was scheuled, so
602 * just return.
603 */
604 return;
605 } else if (error != 0) {
606 int retry_scheduled;
607 struct scsi_mode_sense_6 *sms;
608
609 sms = (struct scsi_mode_sense_6 *)
610 done_ccb->csio.cdb_io.cdb_bytes;
611
612 /*
613 * Check to see if block descriptors were
614 * disabled. Some devices don't like that.
615 * We're taking advantage of the fact that
616 * the first few bytes of the 6 and 10 byte
617 * mode sense commands are the same. If
618 * block descriptors were disabled, enable
619 * them and re-send the command.
620 */
621 if (sms->byte2 & SMS_DBD) {
622 sms->byte2 &= ~SMS_DBD;
623 xpt_action(done_ccb);
624 softc->quirks |= CH_Q_NO_DBD;
625 retry_scheduled = 1;
626 } else
627 retry_scheduled = 0;
628
629 /* Don't wedge this device's queue */
630 cam_release_devq(done_ccb->ccb_h.path,
631 /*relsim_flags*/0,
632 /*reduction*/0,
633 /*timeout*/0,
634 /*getcount_only*/0);
635
636 if (retry_scheduled)
637 return;
638
639 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK)
640 == CAM_SCSI_STATUS_ERROR)
641 scsi_sense_print(&done_ccb->csio);
642 else {
643 xpt_print(periph->path,
644 "got CAM status %#x\n",
645 done_ccb->ccb_h.status);
646 }
647 xpt_print(periph->path, "fatal error, failed "
648 "to attach to device\n");
649
650 cam_periph_invalidate(periph);
651
652 announce_buf[0] = '\0';
653 }
654 }
655 if (announce_buf[0] != '\0')
656 xpt_announce_periph(periph, announce_buf);
657 softc->state = CH_STATE_NORMAL;
658 free(mode_header, M_TEMP);
659 /*
660 * Since our peripheral may be invalidated by an error
661 * above or an external event, we must release our CCB
662 * before releasing the probe lock on the peripheral.
663 * The peripheral will only go away once the last lock
664 * is removed, and we need it around for the CCB release
665 * operation.
666 */
667 xpt_release_ccb(done_ccb);
673 cam_periph_unlock(periph);
668 cam_periph_unhold(periph);
674 return;
675 }
676 case CH_CCB_WAITING:
677 {
678 /* Caller will release the CCB */
679 wakeup(&done_ccb->ccb_h.cbfcnp);
680 return;
681 }
682 default:
683 break;
684 }
685 xpt_release_ccb(done_ccb);
686}
687
688static int
689cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
690{
691 struct ch_softc *softc;
692 struct cam_periph *periph;
693
694 periph = xpt_path_periph(ccb->ccb_h.path);
695 softc = (struct ch_softc *)periph->softc;
696
697 return (cam_periph_error(ccb, cam_flags, sense_flags,
698 &softc->saved_ccb));
699}
700
701static int
702chioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
703{
704 struct cam_periph *periph;
705 struct ch_softc *softc;
706 int error;
707
708 periph = (struct cam_periph *)dev->si_drv1;
709 if (periph == NULL)
710 return(ENXIO);
711
669 return;
670 }
671 case CH_CCB_WAITING:
672 {
673 /* Caller will release the CCB */
674 wakeup(&done_ccb->ccb_h.cbfcnp);
675 return;
676 }
677 default:
678 break;
679 }
680 xpt_release_ccb(done_ccb);
681}
682
683static int
684cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
685{
686 struct ch_softc *softc;
687 struct cam_periph *periph;
688
689 periph = xpt_path_periph(ccb->ccb_h.path);
690 softc = (struct ch_softc *)periph->softc;
691
692 return (cam_periph_error(ccb, cam_flags, sense_flags,
693 &softc->saved_ccb));
694}
695
696static int
697chioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
698{
699 struct cam_periph *periph;
700 struct ch_softc *softc;
701 int error;
702
703 periph = (struct cam_periph *)dev->si_drv1;
704 if (periph == NULL)
705 return(ENXIO);
706
707 cam_periph_lock(periph);
712 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n"));
713
714 softc = (struct ch_softc *)periph->softc;
715
716 error = 0;
717
718 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
719 ("trying to do ioctl %#lx\n", cmd));
720
721 /*
722 * If this command can change the device's state, we must
723 * have the device open for writing.
724 */
725 switch (cmd) {
726 case CHIOGPICKER:
727 case CHIOGPARAMS:
728 case CHIOGSTATUS:
729 break;
730
731 default:
708 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n"));
709
710 softc = (struct ch_softc *)periph->softc;
711
712 error = 0;
713
714 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
715 ("trying to do ioctl %#lx\n", cmd));
716
717 /*
718 * If this command can change the device's state, we must
719 * have the device open for writing.
720 */
721 switch (cmd) {
722 case CHIOGPICKER:
723 case CHIOGPARAMS:
724 case CHIOGSTATUS:
725 break;
726
727 default:
732 if ((flag & FWRITE) == 0)
728 if ((flag & FWRITE) == 0) {
729 cam_periph_unlock(periph);
733 return (EBADF);
730 return (EBADF);
731 }
734 }
735
736 switch (cmd) {
737 case CHIOMOVE:
738 error = chmove(periph, (struct changer_move *)addr);
739 break;
740
741 case CHIOEXCHANGE:
742 error = chexchange(periph, (struct changer_exchange *)addr);
743 break;
744
745 case CHIOPOSITION:
746 error = chposition(periph, (struct changer_position *)addr);
747 break;
748
749 case CHIOGPICKER:
750 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT];
751 break;
752
753 case CHIOSPICKER:
754 {
755 int new_picker = *(int *)addr;
756
732 }
733
734 switch (cmd) {
735 case CHIOMOVE:
736 error = chmove(periph, (struct changer_move *)addr);
737 break;
738
739 case CHIOEXCHANGE:
740 error = chexchange(periph, (struct changer_exchange *)addr);
741 break;
742
743 case CHIOPOSITION:
744 error = chposition(periph, (struct changer_position *)addr);
745 break;
746
747 case CHIOGPICKER:
748 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT];
749 break;
750
751 case CHIOSPICKER:
752 {
753 int new_picker = *(int *)addr;
754
757 if (new_picker > (softc->sc_counts[CHET_MT] - 1))
758 return (EINVAL);
755 if (new_picker > (softc->sc_counts[CHET_MT] - 1)) {
756 error = EINVAL;
757 break;
758 }
759 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker;
760 break;
761 }
762 case CHIOGPARAMS:
763 {
764 struct changer_params *cp = (struct changer_params *)addr;
765
766 cp->cp_npickers = softc->sc_counts[CHET_MT];
767 cp->cp_nslots = softc->sc_counts[CHET_ST];
768 cp->cp_nportals = softc->sc_counts[CHET_IE];
769 cp->cp_ndrives = softc->sc_counts[CHET_DT];
770 break;
771 }
772 case CHIOIELEM:
773 error = chielem(periph, *(unsigned int *)addr);
774 break;
775
776 case CHIOGSTATUS:
777 {
778 error = chgetelemstatus(periph,
779 (struct changer_element_status_request *) addr);
780 break;
781 }
782
783 case CHIOSETVOLTAG:
784 {
785 error = chsetvoltag(periph,
786 (struct changer_set_voltag_request *) addr);
787 break;
788 }
789
790 /* Implement prevent/allow? */
791
792 default:
793 error = cam_periph_ioctl(periph, cmd, addr, cherror);
794 break;
795 }
796
759 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker;
760 break;
761 }
762 case CHIOGPARAMS:
763 {
764 struct changer_params *cp = (struct changer_params *)addr;
765
766 cp->cp_npickers = softc->sc_counts[CHET_MT];
767 cp->cp_nslots = softc->sc_counts[CHET_ST];
768 cp->cp_nportals = softc->sc_counts[CHET_IE];
769 cp->cp_ndrives = softc->sc_counts[CHET_DT];
770 break;
771 }
772 case CHIOIELEM:
773 error = chielem(periph, *(unsigned int *)addr);
774 break;
775
776 case CHIOGSTATUS:
777 {
778 error = chgetelemstatus(periph,
779 (struct changer_element_status_request *) addr);
780 break;
781 }
782
783 case CHIOSETVOLTAG:
784 {
785 error = chsetvoltag(periph,
786 (struct changer_set_voltag_request *) addr);
787 break;
788 }
789
790 /* Implement prevent/allow? */
791
792 default:
793 error = cam_periph_ioctl(periph, cmd, addr, cherror);
794 break;
795 }
796
797 cam_periph_unlock(periph);
797 return (error);
798}
799
800static int
801chmove(struct cam_periph *periph, struct changer_move *cm)
802{
803 struct ch_softc *softc;
804 u_int16_t fromelem, toelem;
805 union ccb *ccb;
806 int error;
807
808 error = 0;
809 softc = (struct ch_softc *)periph->softc;
810
811 /*
812 * Check arguments.
813 */
814 if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
815 return (EINVAL);
816 if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) ||
817 (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1)))
818 return (ENODEV);
819
820 /*
821 * Check the request against the changer's capabilities.
822 */
823 if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
824 return (ENODEV);
825
826 /*
827 * Calculate the source and destination elements.
828 */
829 fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
830 toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
831
832 ccb = cam_periph_getccb(periph, /*priority*/ 1);
833
834 scsi_move_medium(&ccb->csio,
835 /* retries */ 1,
836 /* cbfcnp */ chdone,
837 /* tag_action */ MSG_SIMPLE_Q_TAG,
838 /* tea */ softc->sc_picker,
839 /* src */ fromelem,
840 /* dst */ toelem,
841 /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE,
842 /* sense_len */ SSD_FULL_SIZE,
843 /* timeout */ CH_TIMEOUT_MOVE_MEDIUM);
844
845 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
846 /*sense_flags*/ SF_RETRY_UA,
847 softc->device_stats);
848
849 xpt_release_ccb(ccb);
850
851 return(error);
852}
853
854static int
855chexchange(struct cam_periph *periph, struct changer_exchange *ce)
856{
857 struct ch_softc *softc;
858 u_int16_t src, dst1, dst2;
859 union ccb *ccb;
860 int error;
861
862 error = 0;
863 softc = (struct ch_softc *)periph->softc;
864 /*
865 * Check arguments.
866 */
867 if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
868 (ce->ce_sdsttype > CHET_DT))
869 return (EINVAL);
870 if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) ||
871 (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) ||
872 (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1)))
873 return (ENODEV);
874
875 /*
876 * Check the request against the changer's capabilities.
877 */
878 if (((softc->sc_exchangemask[ce->ce_srctype] &
879 (1 << ce->ce_fdsttype)) == 0) ||
880 ((softc->sc_exchangemask[ce->ce_fdsttype] &
881 (1 << ce->ce_sdsttype)) == 0))
882 return (ENODEV);
883
884 /*
885 * Calculate the source and destination elements.
886 */
887 src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
888 dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
889 dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
890
891 ccb = cam_periph_getccb(periph, /*priority*/ 1);
892
893 scsi_exchange_medium(&ccb->csio,
894 /* retries */ 1,
895 /* cbfcnp */ chdone,
896 /* tag_action */ MSG_SIMPLE_Q_TAG,
897 /* tea */ softc->sc_picker,
898 /* src */ src,
899 /* dst1 */ dst1,
900 /* dst2 */ dst2,
901 /* invert1 */ (ce->ce_flags & CE_INVERT1) ?
902 TRUE : FALSE,
903 /* invert2 */ (ce->ce_flags & CE_INVERT2) ?
904 TRUE : FALSE,
905 /* sense_len */ SSD_FULL_SIZE,
906 /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM);
907
908 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
909 /*sense_flags*/ SF_RETRY_UA,
910 softc->device_stats);
911
912 xpt_release_ccb(ccb);
913
914 return(error);
915}
916
917static int
918chposition(struct cam_periph *periph, struct changer_position *cp)
919{
920 struct ch_softc *softc;
921 u_int16_t dst;
922 union ccb *ccb;
923 int error;
924
925 error = 0;
926 softc = (struct ch_softc *)periph->softc;
927
928 /*
929 * Check arguments.
930 */
931 if (cp->cp_type > CHET_DT)
932 return (EINVAL);
933 if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1))
934 return (ENODEV);
935
936 /*
937 * Calculate the destination element.
938 */
939 dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit;
940
941 ccb = cam_periph_getccb(periph, /*priority*/ 1);
942
943 scsi_position_to_element(&ccb->csio,
944 /* retries */ 1,
945 /* cbfcnp */ chdone,
946 /* tag_action */ MSG_SIMPLE_Q_TAG,
947 /* tea */ softc->sc_picker,
948 /* dst */ dst,
949 /* invert */ (cp->cp_flags & CP_INVERT) ?
950 TRUE : FALSE,
951 /* sense_len */ SSD_FULL_SIZE,
952 /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT);
953
954 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
955 /*sense_flags*/ SF_RETRY_UA,
956 softc->device_stats);
957
958 xpt_release_ccb(ccb);
959
960 return(error);
961}
962
963/*
964 * Copy a volume tag to a volume_tag struct, converting SCSI byte order
965 * to host native byte order in the volume serial number. The volume
966 * label as returned by the changer is transferred to user mode as
967 * nul-terminated string. Volume labels are truncated at the first
968 * space, as suggested by SCSI-2.
969 */
970static void
971copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag)
972{
973 int i;
974 for (i=0; i<CH_VOLTAG_MAXLEN; i++) {
975 char c = voltag->vif[i];
976 if (c && c != ' ')
977 uvoltag->cv_volid[i] = c;
978 else
979 break;
980 }
981 uvoltag->cv_serial = scsi_2btoul(voltag->vsn);
982}
983
984/*
985 * Copy an an element status descriptor to a user-mode
986 * changer_element_status structure.
987 */
988
989static void
990copy_element_status(struct ch_softc *softc,
991 u_int16_t flags,
992 struct read_element_status_descriptor *desc,
993 struct changer_element_status *ces)
994{
995 u_int16_t eaddr = scsi_2btoul(desc->eaddr);
996 u_int16_t et;
997
998 ces->ces_int_addr = eaddr;
999 /* set up logical address in element status */
1000 for (et = CHET_MT; et <= CHET_DT; et++) {
1001 if ((softc->sc_firsts[et] <= eaddr)
1002 && ((softc->sc_firsts[et] + softc->sc_counts[et])
1003 > eaddr)) {
1004 ces->ces_addr = eaddr - softc->sc_firsts[et];
1005 ces->ces_type = et;
1006 break;
1007 }
1008 }
1009
1010 ces->ces_flags = desc->flags1;
1011
1012 ces->ces_sensecode = desc->sense_code;
1013 ces->ces_sensequal = desc->sense_qual;
1014
1015 if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
1016 ces->ces_flags |= CES_INVERT;
1017
1018 if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
1019
1020 eaddr = scsi_2btoul(desc->ssea);
1021
1022 /* convert source address to logical format */
1023 for (et = CHET_MT; et <= CHET_DT; et++) {
1024 if ((softc->sc_firsts[et] <= eaddr)
1025 && ((softc->sc_firsts[et] + softc->sc_counts[et])
1026 > eaddr)) {
1027 ces->ces_source_addr =
1028 eaddr - softc->sc_firsts[et];
1029 ces->ces_source_type = et;
1030 ces->ces_flags |= CES_SOURCE_VALID;
1031 break;
1032 }
1033 }
1034
1035 if (!(ces->ces_flags & CES_SOURCE_VALID))
1036 printf("ch: warning: could not map element source "
1037 "address %ud to a valid element type\n",
1038 eaddr);
1039 }
1040
1041
1042 if (flags & READ_ELEMENT_STATUS_PVOLTAG)
1043 copy_voltag(&(ces->ces_pvoltag), &(desc->pvoltag));
1044 if (flags & READ_ELEMENT_STATUS_AVOLTAG)
1045 copy_voltag(&(ces->ces_avoltag), &(desc->avoltag));
1046
1047 if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_IDVALID) {
1048 ces->ces_flags |= CES_SCSIID_VALID;
1049 ces->ces_scsi_id = desc->dt_scsi_addr;
1050 }
1051
1052 if (desc->dt_scsi_addr & READ_ELEMENT_STATUS_DT_LUVALID) {
1053 ces->ces_flags |= CES_LUN_VALID;
1054 ces->ces_scsi_lun =
1055 desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_LUNMASK;
1056 }
1057}
1058
1059static int
1060chgetelemstatus(struct cam_periph *periph,
1061 struct changer_element_status_request *cesr)
1062{
1063 struct read_element_status_header *st_hdr;
1064 struct read_element_status_page_header *pg_hdr;
1065 struct read_element_status_descriptor *desc;
1066 caddr_t data = NULL;
1067 size_t size, desclen;
1068 int avail, i, error = 0;
1069 struct changer_element_status *user_data = NULL;
1070 struct ch_softc *softc;
1071 union ccb *ccb;
1072 int chet = cesr->cesr_element_type;
1073 int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
1074
1075 softc = (struct ch_softc *)periph->softc;
1076
1077 /* perform argument checking */
1078
1079 /*
1080 * Perform a range check on the cesr_element_{base,count}
1081 * request argument fields.
1082 */
1083 if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0
1084 || (cesr->cesr_element_base + cesr->cesr_element_count)
1085 > softc->sc_counts[chet])
1086 return (EINVAL);
1087
1088 /*
1089 * Request one descriptor for the given element type. This
1090 * is used to determine the size of the descriptor so that
1091 * we can allocate enough storage for all of them. We assume
1092 * that the first one can fit into 1k.
1093 */
798 return (error);
799}
800
801static int
802chmove(struct cam_periph *periph, struct changer_move *cm)
803{
804 struct ch_softc *softc;
805 u_int16_t fromelem, toelem;
806 union ccb *ccb;
807 int error;
808
809 error = 0;
810 softc = (struct ch_softc *)periph->softc;
811
812 /*
813 * Check arguments.
814 */
815 if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
816 return (EINVAL);
817 if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) ||
818 (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1)))
819 return (ENODEV);
820
821 /*
822 * Check the request against the changer's capabilities.
823 */
824 if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
825 return (ENODEV);
826
827 /*
828 * Calculate the source and destination elements.
829 */
830 fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
831 toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
832
833 ccb = cam_periph_getccb(periph, /*priority*/ 1);
834
835 scsi_move_medium(&ccb->csio,
836 /* retries */ 1,
837 /* cbfcnp */ chdone,
838 /* tag_action */ MSG_SIMPLE_Q_TAG,
839 /* tea */ softc->sc_picker,
840 /* src */ fromelem,
841 /* dst */ toelem,
842 /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE,
843 /* sense_len */ SSD_FULL_SIZE,
844 /* timeout */ CH_TIMEOUT_MOVE_MEDIUM);
845
846 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
847 /*sense_flags*/ SF_RETRY_UA,
848 softc->device_stats);
849
850 xpt_release_ccb(ccb);
851
852 return(error);
853}
854
855static int
856chexchange(struct cam_periph *periph, struct changer_exchange *ce)
857{
858 struct ch_softc *softc;
859 u_int16_t src, dst1, dst2;
860 union ccb *ccb;
861 int error;
862
863 error = 0;
864 softc = (struct ch_softc *)periph->softc;
865 /*
866 * Check arguments.
867 */
868 if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
869 (ce->ce_sdsttype > CHET_DT))
870 return (EINVAL);
871 if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) ||
872 (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) ||
873 (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1)))
874 return (ENODEV);
875
876 /*
877 * Check the request against the changer's capabilities.
878 */
879 if (((softc->sc_exchangemask[ce->ce_srctype] &
880 (1 << ce->ce_fdsttype)) == 0) ||
881 ((softc->sc_exchangemask[ce->ce_fdsttype] &
882 (1 << ce->ce_sdsttype)) == 0))
883 return (ENODEV);
884
885 /*
886 * Calculate the source and destination elements.
887 */
888 src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
889 dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
890 dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
891
892 ccb = cam_periph_getccb(periph, /*priority*/ 1);
893
894 scsi_exchange_medium(&ccb->csio,
895 /* retries */ 1,
896 /* cbfcnp */ chdone,
897 /* tag_action */ MSG_SIMPLE_Q_TAG,
898 /* tea */ softc->sc_picker,
899 /* src */ src,
900 /* dst1 */ dst1,
901 /* dst2 */ dst2,
902 /* invert1 */ (ce->ce_flags & CE_INVERT1) ?
903 TRUE : FALSE,
904 /* invert2 */ (ce->ce_flags & CE_INVERT2) ?
905 TRUE : FALSE,
906 /* sense_len */ SSD_FULL_SIZE,
907 /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM);
908
909 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
910 /*sense_flags*/ SF_RETRY_UA,
911 softc->device_stats);
912
913 xpt_release_ccb(ccb);
914
915 return(error);
916}
917
918static int
919chposition(struct cam_periph *periph, struct changer_position *cp)
920{
921 struct ch_softc *softc;
922 u_int16_t dst;
923 union ccb *ccb;
924 int error;
925
926 error = 0;
927 softc = (struct ch_softc *)periph->softc;
928
929 /*
930 * Check arguments.
931 */
932 if (cp->cp_type > CHET_DT)
933 return (EINVAL);
934 if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1))
935 return (ENODEV);
936
937 /*
938 * Calculate the destination element.
939 */
940 dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit;
941
942 ccb = cam_periph_getccb(periph, /*priority*/ 1);
943
944 scsi_position_to_element(&ccb->csio,
945 /* retries */ 1,
946 /* cbfcnp */ chdone,
947 /* tag_action */ MSG_SIMPLE_Q_TAG,
948 /* tea */ softc->sc_picker,
949 /* dst */ dst,
950 /* invert */ (cp->cp_flags & CP_INVERT) ?
951 TRUE : FALSE,
952 /* sense_len */ SSD_FULL_SIZE,
953 /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT);
954
955 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
956 /*sense_flags*/ SF_RETRY_UA,
957 softc->device_stats);
958
959 xpt_release_ccb(ccb);
960
961 return(error);
962}
963
964/*
965 * Copy a volume tag to a volume_tag struct, converting SCSI byte order
966 * to host native byte order in the volume serial number. The volume
967 * label as returned by the changer is transferred to user mode as
968 * nul-terminated string. Volume labels are truncated at the first
969 * space, as suggested by SCSI-2.
970 */
971static void
972copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag)
973{
974 int i;
975 for (i=0; i<CH_VOLTAG_MAXLEN; i++) {
976 char c = voltag->vif[i];
977 if (c && c != ' ')
978 uvoltag->cv_volid[i] = c;
979 else
980 break;
981 }
982 uvoltag->cv_serial = scsi_2btoul(voltag->vsn);
983}
984
985/*
986 * Copy an an element status descriptor to a user-mode
987 * changer_element_status structure.
988 */
989
990static void
991copy_element_status(struct ch_softc *softc,
992 u_int16_t flags,
993 struct read_element_status_descriptor *desc,
994 struct changer_element_status *ces)
995{
996 u_int16_t eaddr = scsi_2btoul(desc->eaddr);
997 u_int16_t et;
998
999 ces->ces_int_addr = eaddr;
1000 /* set up logical address in element status */
1001 for (et = CHET_MT; et <= CHET_DT; et++) {
1002 if ((softc->sc_firsts[et] <= eaddr)
1003 && ((softc->sc_firsts[et] + softc->sc_counts[et])
1004 > eaddr)) {
1005 ces->ces_addr = eaddr - softc->sc_firsts[et];
1006 ces->ces_type = et;
1007 break;
1008 }
1009 }
1010
1011 ces->ces_flags = desc->flags1;
1012
1013 ces->ces_sensecode = desc->sense_code;
1014 ces->ces_sensequal = desc->sense_qual;
1015
1016 if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
1017 ces->ces_flags |= CES_INVERT;
1018
1019 if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
1020
1021 eaddr = scsi_2btoul(desc->ssea);
1022
1023 /* convert source address to logical format */
1024 for (et = CHET_MT; et <= CHET_DT; et++) {
1025 if ((softc->sc_firsts[et] <= eaddr)
1026 && ((softc->sc_firsts[et] + softc->sc_counts[et])
1027 > eaddr)) {
1028 ces->ces_source_addr =
1029 eaddr - softc->sc_firsts[et];
1030 ces->ces_source_type = et;
1031 ces->ces_flags |= CES_SOURCE_VALID;
1032 break;
1033 }
1034 }
1035
1036 if (!(ces->ces_flags & CES_SOURCE_VALID))
1037 printf("ch: warning: could not map element source "
1038 "address %ud to a valid element type\n",
1039 eaddr);
1040 }
1041
1042
1043 if (flags & READ_ELEMENT_STATUS_PVOLTAG)
1044 copy_voltag(&(ces->ces_pvoltag), &(desc->pvoltag));
1045 if (flags & READ_ELEMENT_STATUS_AVOLTAG)
1046 copy_voltag(&(ces->ces_avoltag), &(desc->avoltag));
1047
1048 if (desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_IDVALID) {
1049 ces->ces_flags |= CES_SCSIID_VALID;
1050 ces->ces_scsi_id = desc->dt_scsi_addr;
1051 }
1052
1053 if (desc->dt_scsi_addr & READ_ELEMENT_STATUS_DT_LUVALID) {
1054 ces->ces_flags |= CES_LUN_VALID;
1055 ces->ces_scsi_lun =
1056 desc->dt_scsi_flags & READ_ELEMENT_STATUS_DT_LUNMASK;
1057 }
1058}
1059
1060static int
1061chgetelemstatus(struct cam_periph *periph,
1062 struct changer_element_status_request *cesr)
1063{
1064 struct read_element_status_header *st_hdr;
1065 struct read_element_status_page_header *pg_hdr;
1066 struct read_element_status_descriptor *desc;
1067 caddr_t data = NULL;
1068 size_t size, desclen;
1069 int avail, i, error = 0;
1070 struct changer_element_status *user_data = NULL;
1071 struct ch_softc *softc;
1072 union ccb *ccb;
1073 int chet = cesr->cesr_element_type;
1074 int want_voltags = (cesr->cesr_flags & CESR_VOLTAGS) ? 1 : 0;
1075
1076 softc = (struct ch_softc *)periph->softc;
1077
1078 /* perform argument checking */
1079
1080 /*
1081 * Perform a range check on the cesr_element_{base,count}
1082 * request argument fields.
1083 */
1084 if ((softc->sc_counts[chet] - cesr->cesr_element_base) <= 0
1085 || (cesr->cesr_element_base + cesr->cesr_element_count)
1086 > softc->sc_counts[chet])
1087 return (EINVAL);
1088
1089 /*
1090 * Request one descriptor for the given element type. This
1091 * is used to determine the size of the descriptor so that
1092 * we can allocate enough storage for all of them. We assume
1093 * that the first one can fit into 1k.
1094 */
1095 cam_periph_unlock(periph);
1094 data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
1095
1096 data = (caddr_t)malloc(1024, M_DEVBUF, M_WAITOK);
1097
1098 cam_periph_lock(periph);
1096 ccb = cam_periph_getccb(periph, /*priority*/ 1);
1097
1098 scsi_read_element_status(&ccb->csio,
1099 /* retries */ 1,
1100 /* cbfcnp */ chdone,
1101 /* tag_action */ MSG_SIMPLE_Q_TAG,
1102 /* voltag */ want_voltags,
1103 /* sea */ softc->sc_firsts[chet],
1104 /* count */ 1,
1105 /* data_ptr */ data,
1106 /* dxfer_len */ 1024,
1107 /* sense_len */ SSD_FULL_SIZE,
1108 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1109
1110 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1111 /*sense_flags*/ SF_RETRY_UA,
1112 softc->device_stats);
1113
1114 if (error)
1115 goto done;
1099 ccb = cam_periph_getccb(periph, /*priority*/ 1);
1100
1101 scsi_read_element_status(&ccb->csio,
1102 /* retries */ 1,
1103 /* cbfcnp */ chdone,
1104 /* tag_action */ MSG_SIMPLE_Q_TAG,
1105 /* voltag */ want_voltags,
1106 /* sea */ softc->sc_firsts[chet],
1107 /* count */ 1,
1108 /* data_ptr */ data,
1109 /* dxfer_len */ 1024,
1110 /* sense_len */ SSD_FULL_SIZE,
1111 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1112
1113 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1114 /*sense_flags*/ SF_RETRY_UA,
1115 softc->device_stats);
1116
1117 if (error)
1118 goto done;
1119 cam_periph_unlock(periph);
1116
1117 st_hdr = (struct read_element_status_header *)data;
1118 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1119 sizeof(struct read_element_status_header));
1120 desclen = scsi_2btoul(pg_hdr->edl);
1121
1122 size = sizeof(struct read_element_status_header) +
1123 sizeof(struct read_element_status_page_header) +
1124 (desclen * cesr->cesr_element_count);
1125
1126 /*
1127 * Reallocate storage for descriptors and get them from the
1128 * device.
1129 */
1130 free(data, M_DEVBUF);
1131 data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
1132
1120
1121 st_hdr = (struct read_element_status_header *)data;
1122 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1123 sizeof(struct read_element_status_header));
1124 desclen = scsi_2btoul(pg_hdr->edl);
1125
1126 size = sizeof(struct read_element_status_header) +
1127 sizeof(struct read_element_status_page_header) +
1128 (desclen * cesr->cesr_element_count);
1129
1130 /*
1131 * Reallocate storage for descriptors and get them from the
1132 * device.
1133 */
1134 free(data, M_DEVBUF);
1135 data = (caddr_t)malloc(size, M_DEVBUF, M_WAITOK);
1136
1137 cam_periph_lock(periph);
1133 scsi_read_element_status(&ccb->csio,
1134 /* retries */ 1,
1135 /* cbfcnp */ chdone,
1136 /* tag_action */ MSG_SIMPLE_Q_TAG,
1137 /* voltag */ want_voltags,
1138 /* sea */ softc->sc_firsts[chet]
1139 + cesr->cesr_element_base,
1140 /* count */ cesr->cesr_element_count,
1141 /* data_ptr */ data,
1142 /* dxfer_len */ size,
1143 /* sense_len */ SSD_FULL_SIZE,
1144 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1145
1146 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1147 /*sense_flags*/ SF_RETRY_UA,
1148 softc->device_stats);
1149
1150 if (error)
1151 goto done;
1138 scsi_read_element_status(&ccb->csio,
1139 /* retries */ 1,
1140 /* cbfcnp */ chdone,
1141 /* tag_action */ MSG_SIMPLE_Q_TAG,
1142 /* voltag */ want_voltags,
1143 /* sea */ softc->sc_firsts[chet]
1144 + cesr->cesr_element_base,
1145 /* count */ cesr->cesr_element_count,
1146 /* data_ptr */ data,
1147 /* dxfer_len */ size,
1148 /* sense_len */ SSD_FULL_SIZE,
1149 /* timeout */ CH_TIMEOUT_READ_ELEMENT_STATUS);
1150
1151 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1152 /*sense_flags*/ SF_RETRY_UA,
1153 softc->device_stats);
1154
1155 if (error)
1156 goto done;
1157 cam_periph_unlock(periph);
1152
1153 /*
1154 * Fill in the user status array.
1155 */
1156 st_hdr = (struct read_element_status_header *)data;
1157 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1158 sizeof(struct read_element_status_header));
1159 avail = scsi_2btoul(st_hdr->count);
1160
1161 if (avail != cesr->cesr_element_count) {
1162 xpt_print(periph->path,
1163 "warning, READ ELEMENT STATUS avail != count\n");
1164 }
1165
1166 user_data = (struct changer_element_status *)
1167 malloc(avail * sizeof(struct changer_element_status),
1168 M_DEVBUF, M_WAITOK | M_ZERO);
1169
1170 desc = (struct read_element_status_descriptor *)((uintptr_t)data +
1171 sizeof(struct read_element_status_header) +
1172 sizeof(struct read_element_status_page_header));
1173 /*
1174 * Set up the individual element status structures
1175 */
1176 for (i = 0; i < avail; ++i) {
1177 struct changer_element_status *ces = &(user_data[i]);
1178
1179 copy_element_status(softc, pg_hdr->flags, desc, ces);
1180
1181 desc = (struct read_element_status_descriptor *)
1182 ((uintptr_t)desc + desclen);
1183 }
1184
1185 /* Copy element status structures out to userspace. */
1186 error = copyout(user_data,
1187 cesr->cesr_element_status,
1188 avail * sizeof(struct changer_element_status));
1158
1159 /*
1160 * Fill in the user status array.
1161 */
1162 st_hdr = (struct read_element_status_header *)data;
1163 pg_hdr = (struct read_element_status_page_header *)((uintptr_t)st_hdr +
1164 sizeof(struct read_element_status_header));
1165 avail = scsi_2btoul(st_hdr->count);
1166
1167 if (avail != cesr->cesr_element_count) {
1168 xpt_print(periph->path,
1169 "warning, READ ELEMENT STATUS avail != count\n");
1170 }
1171
1172 user_data = (struct changer_element_status *)
1173 malloc(avail * sizeof(struct changer_element_status),
1174 M_DEVBUF, M_WAITOK | M_ZERO);
1175
1176 desc = (struct read_element_status_descriptor *)((uintptr_t)data +
1177 sizeof(struct read_element_status_header) +
1178 sizeof(struct read_element_status_page_header));
1179 /*
1180 * Set up the individual element status structures
1181 */
1182 for (i = 0; i < avail; ++i) {
1183 struct changer_element_status *ces = &(user_data[i]);
1184
1185 copy_element_status(softc, pg_hdr->flags, desc, ces);
1186
1187 desc = (struct read_element_status_descriptor *)
1188 ((uintptr_t)desc + desclen);
1189 }
1190
1191 /* Copy element status structures out to userspace. */
1192 error = copyout(user_data,
1193 cesr->cesr_element_status,
1194 avail * sizeof(struct changer_element_status));
1195 cam_periph_lock(periph);
1189
1190 done:
1191 xpt_release_ccb(ccb);
1192
1193 if (data != NULL)
1194 free(data, M_DEVBUF);
1195 if (user_data != NULL)
1196 free(user_data, M_DEVBUF);
1197
1198 return (error);
1199}
1200
1201static int
1202chielem(struct cam_periph *periph,
1203 unsigned int timeout)
1204{
1205 union ccb *ccb;
1206 struct ch_softc *softc;
1207 int error;
1208
1209 if (!timeout) {
1210 timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS;
1211 } else {
1212 timeout *= 1000;
1213 }
1214
1215 error = 0;
1216 softc = (struct ch_softc *)periph->softc;
1217
1218 ccb = cam_periph_getccb(periph, /*priority*/ 1);
1219
1220 scsi_initialize_element_status(&ccb->csio,
1221 /* retries */ 1,
1222 /* cbfcnp */ chdone,
1223 /* tag_action */ MSG_SIMPLE_Q_TAG,
1224 /* sense_len */ SSD_FULL_SIZE,
1225 /* timeout */ timeout);
1226
1227 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1228 /*sense_flags*/ SF_RETRY_UA,
1229 softc->device_stats);
1230
1231 xpt_release_ccb(ccb);
1232
1233 return(error);
1234}
1235
1236static int
1237chsetvoltag(struct cam_periph *periph,
1238 struct changer_set_voltag_request *csvr)
1239{
1240 union ccb *ccb;
1241 struct ch_softc *softc;
1242 u_int16_t ea;
1243 u_int8_t sac;
1244 struct scsi_send_volume_tag_parameters ssvtp;
1245 int error;
1246 int i;
1247
1248 error = 0;
1249 softc = (struct ch_softc *)periph->softc;
1250
1251 bzero(&ssvtp, sizeof(ssvtp));
1252 for (i=0; i<sizeof(ssvtp.vitf); i++) {
1253 ssvtp.vitf[i] = ' ';
1254 }
1255
1256 /*
1257 * Check arguments.
1258 */
1259 if (csvr->csvr_type > CHET_DT)
1260 return EINVAL;
1261 if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1))
1262 return ENODEV;
1263
1264 ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr;
1265
1266 if (csvr->csvr_flags & CSVR_ALTERNATE) {
1267 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1268 case CSVR_MODE_SET:
1269 sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE;
1270 break;
1271 case CSVR_MODE_REPLACE:
1272 sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE;
1273 break;
1274 case CSVR_MODE_CLEAR:
1275 sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE;
1276 break;
1277 default:
1278 error = EINVAL;
1279 goto out;
1280 }
1281 } else {
1282 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1283 case CSVR_MODE_SET:
1284 sac = SEND_VOLUME_TAG_ASSERT_PRIMARY;
1285 break;
1286 case CSVR_MODE_REPLACE:
1287 sac = SEND_VOLUME_TAG_REPLACE_PRIMARY;
1288 break;
1289 case CSVR_MODE_CLEAR:
1290 sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY;
1291 break;
1292 default:
1293 error = EINVAL;
1294 goto out;
1295 }
1296 }
1297
1298 memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid,
1299 min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf)));
1300 scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn);
1301
1302 ccb = cam_periph_getccb(periph, /*priority*/ 1);
1303
1304 scsi_send_volume_tag(&ccb->csio,
1305 /* retries */ 1,
1306 /* cbfcnp */ chdone,
1307 /* tag_action */ MSG_SIMPLE_Q_TAG,
1308 /* element_address */ ea,
1309 /* send_action_code */ sac,
1310 /* parameters */ &ssvtp,
1311 /* sense_len */ SSD_FULL_SIZE,
1312 /* timeout */ CH_TIMEOUT_SEND_VOLTAG);
1313
1314 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1315 /*sense_flags*/ SF_RETRY_UA,
1316 softc->device_stats);
1317
1318 xpt_release_ccb(ccb);
1319
1320 out:
1321 return error;
1322}
1323
1324static int
1325chgetparams(struct cam_periph *periph)
1326{
1327 union ccb *ccb;
1328 struct ch_softc *softc;
1329 void *mode_buffer;
1330 int mode_buffer_len;
1331 struct page_element_address_assignment *ea;
1332 struct page_device_capabilities *cap;
1333 int error, from, dbd;
1334 u_int8_t *moves, *exchanges;
1335
1336 error = 0;
1337
1338 softc = (struct ch_softc *)periph->softc;
1339
1340 ccb = cam_periph_getccb(periph, /*priority*/ 1);
1341
1342 /*
1343 * The scsi_mode_sense_data structure is just a convenience
1344 * structure that allows us to easily calculate the worst-case
1345 * storage size of the mode sense buffer.
1346 */
1347 mode_buffer_len = sizeof(struct scsi_mode_sense_data);
1348
1349 mode_buffer = malloc(mode_buffer_len, M_TEMP, M_NOWAIT);
1350
1351 if (mode_buffer == NULL) {
1352 printf("chgetparams: couldn't malloc mode sense data\n");
1353 return(ENOSPC);
1354 }
1355
1356 bzero(mode_buffer, mode_buffer_len);
1357
1358 if (softc->quirks & CH_Q_NO_DBD)
1359 dbd = FALSE;
1360 else
1361 dbd = TRUE;
1362
1363 /*
1364 * Get the element address assignment page.
1365 */
1366 scsi_mode_sense(&ccb->csio,
1367 /* retries */ 1,
1368 /* cbfcnp */ chdone,
1369 /* tag_action */ MSG_SIMPLE_Q_TAG,
1370 /* dbd */ dbd,
1371 /* page_code */ SMS_PAGE_CTRL_CURRENT,
1372 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
1373 /* param_buf */ (u_int8_t *)mode_buffer,
1374 /* param_len */ mode_buffer_len,
1375 /* sense_len */ SSD_FULL_SIZE,
1376 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1377
1378 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1379 /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT,
1380 softc->device_stats);
1381
1382 if (error) {
1383 if (dbd) {
1384 struct scsi_mode_sense_6 *sms;
1385
1386 sms = (struct scsi_mode_sense_6 *)
1387 ccb->csio.cdb_io.cdb_bytes;
1388
1389 sms->byte2 &= ~SMS_DBD;
1390 error = cam_periph_runccb(ccb, cherror,
1391 /*cam_flags*/ CAM_RETRY_SELTO,
1392 /*sense_flags*/ SF_RETRY_UA,
1393 softc->device_stats);
1394 } else {
1395 /*
1396 * Since we disabled sense printing above, print
1397 * out the sense here since we got an error.
1398 */
1399 scsi_sense_print(&ccb->csio);
1400 }
1401
1402 if (error) {
1403 xpt_print(periph->path,
1404 "chgetparams: error getting element "
1405 "address page\n");
1406 xpt_release_ccb(ccb);
1407 free(mode_buffer, M_TEMP);
1408 return(error);
1409 }
1410 }
1411
1412 ea = (struct page_element_address_assignment *)
1413 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1414
1415 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
1416 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
1417 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
1418 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
1419 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
1420 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
1421 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
1422 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
1423
1424 bzero(mode_buffer, mode_buffer_len);
1425
1426 /*
1427 * Now get the device capabilities page.
1428 */
1429 scsi_mode_sense(&ccb->csio,
1430 /* retries */ 1,
1431 /* cbfcnp */ chdone,
1432 /* tag_action */ MSG_SIMPLE_Q_TAG,
1433 /* dbd */ dbd,
1434 /* page_code */ SMS_PAGE_CTRL_CURRENT,
1435 /* page */ CH_DEVICE_CAP_PAGE,
1436 /* param_buf */ (u_int8_t *)mode_buffer,
1437 /* param_len */ mode_buffer_len,
1438 /* sense_len */ SSD_FULL_SIZE,
1439 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1440
1441 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1442 /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
1443 softc->device_stats);
1444
1445 if (error) {
1446 if (dbd) {
1447 struct scsi_mode_sense_6 *sms;
1448
1449 sms = (struct scsi_mode_sense_6 *)
1450 ccb->csio.cdb_io.cdb_bytes;
1451
1452 sms->byte2 &= ~SMS_DBD;
1453 error = cam_periph_runccb(ccb, cherror,
1454 /*cam_flags*/ CAM_RETRY_SELTO,
1455 /*sense_flags*/ SF_RETRY_UA,
1456 softc->device_stats);
1457 } else {
1458 /*
1459 * Since we disabled sense printing above, print
1460 * out the sense here since we got an error.
1461 */
1462 scsi_sense_print(&ccb->csio);
1463 }
1464
1465 if (error) {
1466 xpt_print(periph->path,
1467 "chgetparams: error getting device "
1468 "capabilities page\n");
1469 xpt_release_ccb(ccb);
1470 free(mode_buffer, M_TEMP);
1471 return(error);
1472 }
1473 }
1474
1475 xpt_release_ccb(ccb);
1476
1477 cap = (struct page_device_capabilities *)
1478 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1479
1480 bzero(softc->sc_movemask, sizeof(softc->sc_movemask));
1481 bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask));
1482 moves = cap->move_from;
1483 exchanges = cap->exchange_with;
1484 for (from = CHET_MT; from <= CHET_MAX; ++from) {
1485 softc->sc_movemask[from] = moves[from];
1486 softc->sc_exchangemask[from] = exchanges[from];
1487 }
1488
1489 free(mode_buffer, M_TEMP);
1490
1491 return(error);
1492}
1493
1494void
1495scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries,
1496 void (*cbfcnp)(struct cam_periph *, union ccb *),
1497 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1498 u_int32_t dst, int invert, u_int8_t sense_len,
1499 u_int32_t timeout)
1500{
1501 struct scsi_move_medium *scsi_cmd;
1502
1503 scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes;
1504 bzero(scsi_cmd, sizeof(*scsi_cmd));
1505
1506 scsi_cmd->opcode = MOVE_MEDIUM;
1507
1508 scsi_ulto2b(tea, scsi_cmd->tea);
1509 scsi_ulto2b(src, scsi_cmd->src);
1510 scsi_ulto2b(dst, scsi_cmd->dst);
1511
1512 if (invert)
1513 scsi_cmd->invert |= MOVE_MEDIUM_INVERT;
1514
1515 cam_fill_csio(csio,
1516 retries,
1517 cbfcnp,
1518 /*flags*/ CAM_DIR_NONE,
1519 tag_action,
1520 /*data_ptr*/ NULL,
1521 /*dxfer_len*/ 0,
1522 sense_len,
1523 sizeof(*scsi_cmd),
1524 timeout);
1525}
1526
1527void
1528scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries,
1529 void (*cbfcnp)(struct cam_periph *, union ccb *),
1530 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1531 u_int32_t dst1, u_int32_t dst2, int invert1,
1532 int invert2, u_int8_t sense_len, u_int32_t timeout)
1533{
1534 struct scsi_exchange_medium *scsi_cmd;
1535
1536 scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes;
1537 bzero(scsi_cmd, sizeof(*scsi_cmd));
1538
1539 scsi_cmd->opcode = EXCHANGE_MEDIUM;
1540
1541 scsi_ulto2b(tea, scsi_cmd->tea);
1542 scsi_ulto2b(src, scsi_cmd->src);
1543 scsi_ulto2b(dst1, scsi_cmd->fdst);
1544 scsi_ulto2b(dst2, scsi_cmd->sdst);
1545
1546 if (invert1)
1547 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1;
1548
1549 if (invert2)
1550 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2;
1551
1552 cam_fill_csio(csio,
1553 retries,
1554 cbfcnp,
1555 /*flags*/ CAM_DIR_NONE,
1556 tag_action,
1557 /*data_ptr*/ NULL,
1558 /*dxfer_len*/ 0,
1559 sense_len,
1560 sizeof(*scsi_cmd),
1561 timeout);
1562}
1563
1564void
1565scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries,
1566 void (*cbfcnp)(struct cam_periph *, union ccb *),
1567 u_int8_t tag_action, u_int32_t tea, u_int32_t dst,
1568 int invert, u_int8_t sense_len, u_int32_t timeout)
1569{
1570 struct scsi_position_to_element *scsi_cmd;
1571
1572 scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes;
1573 bzero(scsi_cmd, sizeof(*scsi_cmd));
1574
1575 scsi_cmd->opcode = POSITION_TO_ELEMENT;
1576
1577 scsi_ulto2b(tea, scsi_cmd->tea);
1578 scsi_ulto2b(dst, scsi_cmd->dst);
1579
1580 if (invert)
1581 scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT;
1582
1583 cam_fill_csio(csio,
1584 retries,
1585 cbfcnp,
1586 /*flags*/ CAM_DIR_NONE,
1587 tag_action,
1588 /*data_ptr*/ NULL,
1589 /*dxfer_len*/ 0,
1590 sense_len,
1591 sizeof(*scsi_cmd),
1592 timeout);
1593}
1594
1595void
1596scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1597 void (*cbfcnp)(struct cam_periph *, union ccb *),
1598 u_int8_t tag_action, int voltag, u_int32_t sea,
1599 u_int32_t count, u_int8_t *data_ptr,
1600 u_int32_t dxfer_len, u_int8_t sense_len,
1601 u_int32_t timeout)
1602{
1603 struct scsi_read_element_status *scsi_cmd;
1604
1605 scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes;
1606 bzero(scsi_cmd, sizeof(*scsi_cmd));
1607
1608 scsi_cmd->opcode = READ_ELEMENT_STATUS;
1609
1610 scsi_ulto2b(sea, scsi_cmd->sea);
1611 scsi_ulto2b(count, scsi_cmd->count);
1612 scsi_ulto3b(dxfer_len, scsi_cmd->len);
1613
1614 if (voltag)
1615 scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG;
1616
1617 cam_fill_csio(csio,
1618 retries,
1619 cbfcnp,
1620 /*flags*/ CAM_DIR_IN,
1621 tag_action,
1622 data_ptr,
1623 dxfer_len,
1624 sense_len,
1625 sizeof(*scsi_cmd),
1626 timeout);
1627}
1628
1629void
1630scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1631 void (*cbfcnp)(struct cam_periph *, union ccb *),
1632 u_int8_t tag_action, u_int8_t sense_len,
1633 u_int32_t timeout)
1634{
1635 struct scsi_initialize_element_status *scsi_cmd;
1636
1637 scsi_cmd = (struct scsi_initialize_element_status *)
1638 &csio->cdb_io.cdb_bytes;
1639 bzero(scsi_cmd, sizeof(*scsi_cmd));
1640
1641 scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS;
1642
1643 cam_fill_csio(csio,
1644 retries,
1645 cbfcnp,
1646 /*flags*/ CAM_DIR_NONE,
1647 tag_action,
1648 /* data_ptr */ NULL,
1649 /* dxfer_len */ 0,
1650 sense_len,
1651 sizeof(*scsi_cmd),
1652 timeout);
1653}
1654
1655void
1656scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries,
1657 void (*cbfcnp)(struct cam_periph *, union ccb *),
1658 u_int8_t tag_action,
1659 u_int16_t element_address,
1660 u_int8_t send_action_code,
1661 struct scsi_send_volume_tag_parameters *parameters,
1662 u_int8_t sense_len, u_int32_t timeout)
1663{
1664 struct scsi_send_volume_tag *scsi_cmd;
1665
1666 scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes;
1667 bzero(scsi_cmd, sizeof(*scsi_cmd));
1668
1669 scsi_cmd->opcode = SEND_VOLUME_TAG;
1670 scsi_ulto2b(element_address, scsi_cmd->ea);
1671 scsi_cmd->sac = send_action_code;
1672 scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll);
1673
1674 cam_fill_csio(csio,
1675 retries,
1676 cbfcnp,
1677 /*flags*/ CAM_DIR_OUT,
1678 tag_action,
1679 /* data_ptr */ (u_int8_t *) parameters,
1680 sizeof(*parameters),
1681 sense_len,
1682 sizeof(*scsi_cmd),
1683 timeout);
1684}
1196
1197 done:
1198 xpt_release_ccb(ccb);
1199
1200 if (data != NULL)
1201 free(data, M_DEVBUF);
1202 if (user_data != NULL)
1203 free(user_data, M_DEVBUF);
1204
1205 return (error);
1206}
1207
1208static int
1209chielem(struct cam_periph *periph,
1210 unsigned int timeout)
1211{
1212 union ccb *ccb;
1213 struct ch_softc *softc;
1214 int error;
1215
1216 if (!timeout) {
1217 timeout = CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS;
1218 } else {
1219 timeout *= 1000;
1220 }
1221
1222 error = 0;
1223 softc = (struct ch_softc *)periph->softc;
1224
1225 ccb = cam_periph_getccb(periph, /*priority*/ 1);
1226
1227 scsi_initialize_element_status(&ccb->csio,
1228 /* retries */ 1,
1229 /* cbfcnp */ chdone,
1230 /* tag_action */ MSG_SIMPLE_Q_TAG,
1231 /* sense_len */ SSD_FULL_SIZE,
1232 /* timeout */ timeout);
1233
1234 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1235 /*sense_flags*/ SF_RETRY_UA,
1236 softc->device_stats);
1237
1238 xpt_release_ccb(ccb);
1239
1240 return(error);
1241}
1242
1243static int
1244chsetvoltag(struct cam_periph *periph,
1245 struct changer_set_voltag_request *csvr)
1246{
1247 union ccb *ccb;
1248 struct ch_softc *softc;
1249 u_int16_t ea;
1250 u_int8_t sac;
1251 struct scsi_send_volume_tag_parameters ssvtp;
1252 int error;
1253 int i;
1254
1255 error = 0;
1256 softc = (struct ch_softc *)periph->softc;
1257
1258 bzero(&ssvtp, sizeof(ssvtp));
1259 for (i=0; i<sizeof(ssvtp.vitf); i++) {
1260 ssvtp.vitf[i] = ' ';
1261 }
1262
1263 /*
1264 * Check arguments.
1265 */
1266 if (csvr->csvr_type > CHET_DT)
1267 return EINVAL;
1268 if (csvr->csvr_addr > (softc->sc_counts[csvr->csvr_type] - 1))
1269 return ENODEV;
1270
1271 ea = softc->sc_firsts[csvr->csvr_type] + csvr->csvr_addr;
1272
1273 if (csvr->csvr_flags & CSVR_ALTERNATE) {
1274 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1275 case CSVR_MODE_SET:
1276 sac = SEND_VOLUME_TAG_ASSERT_ALTERNATE;
1277 break;
1278 case CSVR_MODE_REPLACE:
1279 sac = SEND_VOLUME_TAG_REPLACE_ALTERNATE;
1280 break;
1281 case CSVR_MODE_CLEAR:
1282 sac = SEND_VOLUME_TAG_UNDEFINED_ALTERNATE;
1283 break;
1284 default:
1285 error = EINVAL;
1286 goto out;
1287 }
1288 } else {
1289 switch (csvr->csvr_flags & CSVR_MODE_MASK) {
1290 case CSVR_MODE_SET:
1291 sac = SEND_VOLUME_TAG_ASSERT_PRIMARY;
1292 break;
1293 case CSVR_MODE_REPLACE:
1294 sac = SEND_VOLUME_TAG_REPLACE_PRIMARY;
1295 break;
1296 case CSVR_MODE_CLEAR:
1297 sac = SEND_VOLUME_TAG_UNDEFINED_PRIMARY;
1298 break;
1299 default:
1300 error = EINVAL;
1301 goto out;
1302 }
1303 }
1304
1305 memcpy(ssvtp.vitf, csvr->csvr_voltag.cv_volid,
1306 min(strlen(csvr->csvr_voltag.cv_volid), sizeof(ssvtp.vitf)));
1307 scsi_ulto2b(csvr->csvr_voltag.cv_serial, ssvtp.minvsn);
1308
1309 ccb = cam_periph_getccb(periph, /*priority*/ 1);
1310
1311 scsi_send_volume_tag(&ccb->csio,
1312 /* retries */ 1,
1313 /* cbfcnp */ chdone,
1314 /* tag_action */ MSG_SIMPLE_Q_TAG,
1315 /* element_address */ ea,
1316 /* send_action_code */ sac,
1317 /* parameters */ &ssvtp,
1318 /* sense_len */ SSD_FULL_SIZE,
1319 /* timeout */ CH_TIMEOUT_SEND_VOLTAG);
1320
1321 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1322 /*sense_flags*/ SF_RETRY_UA,
1323 softc->device_stats);
1324
1325 xpt_release_ccb(ccb);
1326
1327 out:
1328 return error;
1329}
1330
1331static int
1332chgetparams(struct cam_periph *periph)
1333{
1334 union ccb *ccb;
1335 struct ch_softc *softc;
1336 void *mode_buffer;
1337 int mode_buffer_len;
1338 struct page_element_address_assignment *ea;
1339 struct page_device_capabilities *cap;
1340 int error, from, dbd;
1341 u_int8_t *moves, *exchanges;
1342
1343 error = 0;
1344
1345 softc = (struct ch_softc *)periph->softc;
1346
1347 ccb = cam_periph_getccb(periph, /*priority*/ 1);
1348
1349 /*
1350 * The scsi_mode_sense_data structure is just a convenience
1351 * structure that allows us to easily calculate the worst-case
1352 * storage size of the mode sense buffer.
1353 */
1354 mode_buffer_len = sizeof(struct scsi_mode_sense_data);
1355
1356 mode_buffer = malloc(mode_buffer_len, M_TEMP, M_NOWAIT);
1357
1358 if (mode_buffer == NULL) {
1359 printf("chgetparams: couldn't malloc mode sense data\n");
1360 return(ENOSPC);
1361 }
1362
1363 bzero(mode_buffer, mode_buffer_len);
1364
1365 if (softc->quirks & CH_Q_NO_DBD)
1366 dbd = FALSE;
1367 else
1368 dbd = TRUE;
1369
1370 /*
1371 * Get the element address assignment page.
1372 */
1373 scsi_mode_sense(&ccb->csio,
1374 /* retries */ 1,
1375 /* cbfcnp */ chdone,
1376 /* tag_action */ MSG_SIMPLE_Q_TAG,
1377 /* dbd */ dbd,
1378 /* page_code */ SMS_PAGE_CTRL_CURRENT,
1379 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
1380 /* param_buf */ (u_int8_t *)mode_buffer,
1381 /* param_len */ mode_buffer_len,
1382 /* sense_len */ SSD_FULL_SIZE,
1383 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1384
1385 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1386 /* sense_flags */ SF_RETRY_UA|SF_NO_PRINT,
1387 softc->device_stats);
1388
1389 if (error) {
1390 if (dbd) {
1391 struct scsi_mode_sense_6 *sms;
1392
1393 sms = (struct scsi_mode_sense_6 *)
1394 ccb->csio.cdb_io.cdb_bytes;
1395
1396 sms->byte2 &= ~SMS_DBD;
1397 error = cam_periph_runccb(ccb, cherror,
1398 /*cam_flags*/ CAM_RETRY_SELTO,
1399 /*sense_flags*/ SF_RETRY_UA,
1400 softc->device_stats);
1401 } else {
1402 /*
1403 * Since we disabled sense printing above, print
1404 * out the sense here since we got an error.
1405 */
1406 scsi_sense_print(&ccb->csio);
1407 }
1408
1409 if (error) {
1410 xpt_print(periph->path,
1411 "chgetparams: error getting element "
1412 "address page\n");
1413 xpt_release_ccb(ccb);
1414 free(mode_buffer, M_TEMP);
1415 return(error);
1416 }
1417 }
1418
1419 ea = (struct page_element_address_assignment *)
1420 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1421
1422 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
1423 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
1424 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
1425 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
1426 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
1427 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
1428 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
1429 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
1430
1431 bzero(mode_buffer, mode_buffer_len);
1432
1433 /*
1434 * Now get the device capabilities page.
1435 */
1436 scsi_mode_sense(&ccb->csio,
1437 /* retries */ 1,
1438 /* cbfcnp */ chdone,
1439 /* tag_action */ MSG_SIMPLE_Q_TAG,
1440 /* dbd */ dbd,
1441 /* page_code */ SMS_PAGE_CTRL_CURRENT,
1442 /* page */ CH_DEVICE_CAP_PAGE,
1443 /* param_buf */ (u_int8_t *)mode_buffer,
1444 /* param_len */ mode_buffer_len,
1445 /* sense_len */ SSD_FULL_SIZE,
1446 /* timeout */ CH_TIMEOUT_MODE_SENSE);
1447
1448 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
1449 /* sense_flags */ SF_RETRY_UA | SF_NO_PRINT,
1450 softc->device_stats);
1451
1452 if (error) {
1453 if (dbd) {
1454 struct scsi_mode_sense_6 *sms;
1455
1456 sms = (struct scsi_mode_sense_6 *)
1457 ccb->csio.cdb_io.cdb_bytes;
1458
1459 sms->byte2 &= ~SMS_DBD;
1460 error = cam_periph_runccb(ccb, cherror,
1461 /*cam_flags*/ CAM_RETRY_SELTO,
1462 /*sense_flags*/ SF_RETRY_UA,
1463 softc->device_stats);
1464 } else {
1465 /*
1466 * Since we disabled sense printing above, print
1467 * out the sense here since we got an error.
1468 */
1469 scsi_sense_print(&ccb->csio);
1470 }
1471
1472 if (error) {
1473 xpt_print(periph->path,
1474 "chgetparams: error getting device "
1475 "capabilities page\n");
1476 xpt_release_ccb(ccb);
1477 free(mode_buffer, M_TEMP);
1478 return(error);
1479 }
1480 }
1481
1482 xpt_release_ccb(ccb);
1483
1484 cap = (struct page_device_capabilities *)
1485 find_mode_page_6((struct scsi_mode_header_6 *)mode_buffer);
1486
1487 bzero(softc->sc_movemask, sizeof(softc->sc_movemask));
1488 bzero(softc->sc_exchangemask, sizeof(softc->sc_exchangemask));
1489 moves = cap->move_from;
1490 exchanges = cap->exchange_with;
1491 for (from = CHET_MT; from <= CHET_MAX; ++from) {
1492 softc->sc_movemask[from] = moves[from];
1493 softc->sc_exchangemask[from] = exchanges[from];
1494 }
1495
1496 free(mode_buffer, M_TEMP);
1497
1498 return(error);
1499}
1500
1501void
1502scsi_move_medium(struct ccb_scsiio *csio, u_int32_t retries,
1503 void (*cbfcnp)(struct cam_periph *, union ccb *),
1504 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1505 u_int32_t dst, int invert, u_int8_t sense_len,
1506 u_int32_t timeout)
1507{
1508 struct scsi_move_medium *scsi_cmd;
1509
1510 scsi_cmd = (struct scsi_move_medium *)&csio->cdb_io.cdb_bytes;
1511 bzero(scsi_cmd, sizeof(*scsi_cmd));
1512
1513 scsi_cmd->opcode = MOVE_MEDIUM;
1514
1515 scsi_ulto2b(tea, scsi_cmd->tea);
1516 scsi_ulto2b(src, scsi_cmd->src);
1517 scsi_ulto2b(dst, scsi_cmd->dst);
1518
1519 if (invert)
1520 scsi_cmd->invert |= MOVE_MEDIUM_INVERT;
1521
1522 cam_fill_csio(csio,
1523 retries,
1524 cbfcnp,
1525 /*flags*/ CAM_DIR_NONE,
1526 tag_action,
1527 /*data_ptr*/ NULL,
1528 /*dxfer_len*/ 0,
1529 sense_len,
1530 sizeof(*scsi_cmd),
1531 timeout);
1532}
1533
1534void
1535scsi_exchange_medium(struct ccb_scsiio *csio, u_int32_t retries,
1536 void (*cbfcnp)(struct cam_periph *, union ccb *),
1537 u_int8_t tag_action, u_int32_t tea, u_int32_t src,
1538 u_int32_t dst1, u_int32_t dst2, int invert1,
1539 int invert2, u_int8_t sense_len, u_int32_t timeout)
1540{
1541 struct scsi_exchange_medium *scsi_cmd;
1542
1543 scsi_cmd = (struct scsi_exchange_medium *)&csio->cdb_io.cdb_bytes;
1544 bzero(scsi_cmd, sizeof(*scsi_cmd));
1545
1546 scsi_cmd->opcode = EXCHANGE_MEDIUM;
1547
1548 scsi_ulto2b(tea, scsi_cmd->tea);
1549 scsi_ulto2b(src, scsi_cmd->src);
1550 scsi_ulto2b(dst1, scsi_cmd->fdst);
1551 scsi_ulto2b(dst2, scsi_cmd->sdst);
1552
1553 if (invert1)
1554 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV1;
1555
1556 if (invert2)
1557 scsi_cmd->invert |= EXCHANGE_MEDIUM_INV2;
1558
1559 cam_fill_csio(csio,
1560 retries,
1561 cbfcnp,
1562 /*flags*/ CAM_DIR_NONE,
1563 tag_action,
1564 /*data_ptr*/ NULL,
1565 /*dxfer_len*/ 0,
1566 sense_len,
1567 sizeof(*scsi_cmd),
1568 timeout);
1569}
1570
1571void
1572scsi_position_to_element(struct ccb_scsiio *csio, u_int32_t retries,
1573 void (*cbfcnp)(struct cam_periph *, union ccb *),
1574 u_int8_t tag_action, u_int32_t tea, u_int32_t dst,
1575 int invert, u_int8_t sense_len, u_int32_t timeout)
1576{
1577 struct scsi_position_to_element *scsi_cmd;
1578
1579 scsi_cmd = (struct scsi_position_to_element *)&csio->cdb_io.cdb_bytes;
1580 bzero(scsi_cmd, sizeof(*scsi_cmd));
1581
1582 scsi_cmd->opcode = POSITION_TO_ELEMENT;
1583
1584 scsi_ulto2b(tea, scsi_cmd->tea);
1585 scsi_ulto2b(dst, scsi_cmd->dst);
1586
1587 if (invert)
1588 scsi_cmd->invert |= POSITION_TO_ELEMENT_INVERT;
1589
1590 cam_fill_csio(csio,
1591 retries,
1592 cbfcnp,
1593 /*flags*/ CAM_DIR_NONE,
1594 tag_action,
1595 /*data_ptr*/ NULL,
1596 /*dxfer_len*/ 0,
1597 sense_len,
1598 sizeof(*scsi_cmd),
1599 timeout);
1600}
1601
1602void
1603scsi_read_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1604 void (*cbfcnp)(struct cam_periph *, union ccb *),
1605 u_int8_t tag_action, int voltag, u_int32_t sea,
1606 u_int32_t count, u_int8_t *data_ptr,
1607 u_int32_t dxfer_len, u_int8_t sense_len,
1608 u_int32_t timeout)
1609{
1610 struct scsi_read_element_status *scsi_cmd;
1611
1612 scsi_cmd = (struct scsi_read_element_status *)&csio->cdb_io.cdb_bytes;
1613 bzero(scsi_cmd, sizeof(*scsi_cmd));
1614
1615 scsi_cmd->opcode = READ_ELEMENT_STATUS;
1616
1617 scsi_ulto2b(sea, scsi_cmd->sea);
1618 scsi_ulto2b(count, scsi_cmd->count);
1619 scsi_ulto3b(dxfer_len, scsi_cmd->len);
1620
1621 if (voltag)
1622 scsi_cmd->byte2 |= READ_ELEMENT_STATUS_VOLTAG;
1623
1624 cam_fill_csio(csio,
1625 retries,
1626 cbfcnp,
1627 /*flags*/ CAM_DIR_IN,
1628 tag_action,
1629 data_ptr,
1630 dxfer_len,
1631 sense_len,
1632 sizeof(*scsi_cmd),
1633 timeout);
1634}
1635
1636void
1637scsi_initialize_element_status(struct ccb_scsiio *csio, u_int32_t retries,
1638 void (*cbfcnp)(struct cam_periph *, union ccb *),
1639 u_int8_t tag_action, u_int8_t sense_len,
1640 u_int32_t timeout)
1641{
1642 struct scsi_initialize_element_status *scsi_cmd;
1643
1644 scsi_cmd = (struct scsi_initialize_element_status *)
1645 &csio->cdb_io.cdb_bytes;
1646 bzero(scsi_cmd, sizeof(*scsi_cmd));
1647
1648 scsi_cmd->opcode = INITIALIZE_ELEMENT_STATUS;
1649
1650 cam_fill_csio(csio,
1651 retries,
1652 cbfcnp,
1653 /*flags*/ CAM_DIR_NONE,
1654 tag_action,
1655 /* data_ptr */ NULL,
1656 /* dxfer_len */ 0,
1657 sense_len,
1658 sizeof(*scsi_cmd),
1659 timeout);
1660}
1661
1662void
1663scsi_send_volume_tag(struct ccb_scsiio *csio, u_int32_t retries,
1664 void (*cbfcnp)(struct cam_periph *, union ccb *),
1665 u_int8_t tag_action,
1666 u_int16_t element_address,
1667 u_int8_t send_action_code,
1668 struct scsi_send_volume_tag_parameters *parameters,
1669 u_int8_t sense_len, u_int32_t timeout)
1670{
1671 struct scsi_send_volume_tag *scsi_cmd;
1672
1673 scsi_cmd = (struct scsi_send_volume_tag *) &csio->cdb_io.cdb_bytes;
1674 bzero(scsi_cmd, sizeof(*scsi_cmd));
1675
1676 scsi_cmd->opcode = SEND_VOLUME_TAG;
1677 scsi_ulto2b(element_address, scsi_cmd->ea);
1678 scsi_cmd->sac = send_action_code;
1679 scsi_ulto2b(sizeof(*parameters), scsi_cmd->pll);
1680
1681 cam_fill_csio(csio,
1682 retries,
1683 cbfcnp,
1684 /*flags*/ CAM_DIR_OUT,
1685 tag_action,
1686 /* data_ptr */ (u_int8_t *) parameters,
1687 sizeof(*parameters),
1688 sense_len,
1689 sizeof(*scsi_cmd),
1690 timeout);
1691}