FreeBSD/Linux Kernel Cross Reference
sys/cam/scsi/scsi_ch.c
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: src/sys/cam/scsi/scsi_ch.c,v 1.46 2007/05/16 16:54:23 scottl Exp $");
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
101 static const u_int32_t CH_TIMEOUT_MODE_SENSE = 6000;
102 static const u_int32_t CH_TIMEOUT_MOVE_MEDIUM = 100000;
103 static const u_int32_t CH_TIMEOUT_EXCHANGE_MEDIUM = 100000;
104 static const u_int32_t CH_TIMEOUT_POSITION_TO_ELEMENT = 100000;
105 static const u_int32_t CH_TIMEOUT_READ_ELEMENT_STATUS = 10000;
106 static const u_int32_t CH_TIMEOUT_SEND_VOLTAG = 10000;
107 static const u_int32_t CH_TIMEOUT_INITIALIZE_ELEMENT_STATUS = 500000;
108
109 typedef enum {
110 CH_FLAG_INVALID = 0x001,
111 CH_FLAG_OPEN = 0x002
112 } ch_flags;
113
114 typedef enum {
115 CH_STATE_PROBE,
116 CH_STATE_NORMAL
117 } ch_state;
118
119 typedef enum {
120 CH_CCB_PROBE,
121 CH_CCB_WAITING
122 } ch_ccb_types;
123
124 typedef 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
132 struct 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
142 struct 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 static d_open_t chopen;
177 static d_close_t chclose;
178 static d_ioctl_t chioctl;
179 static periph_init_t chinit;
180 static periph_ctor_t chregister;
181 static periph_oninv_t choninvalidate;
182 static periph_dtor_t chcleanup;
183 static periph_start_t chstart;
184 static void chasync(void *callback_arg, u_int32_t code,
185 struct cam_path *path, void *arg);
186 static void chdone(struct cam_periph *periph,
187 union ccb *done_ccb);
188 static int cherror(union ccb *ccb, u_int32_t cam_flags,
189 u_int32_t sense_flags);
190 static int chmove(struct cam_periph *periph,
191 struct changer_move *cm);
192 static int chexchange(struct cam_periph *periph,
193 struct changer_exchange *ce);
194 static int chposition(struct cam_periph *periph,
195 struct changer_position *cp);
196 static int chgetelemstatus(struct cam_periph *periph,
197 struct changer_element_status_request *csr);
198 static int chsetvoltag(struct cam_periph *periph,
199 struct changer_set_voltag_request *csvr);
200 static int chielem(struct cam_periph *periph,
201 unsigned int timeout);
202 static int chgetparams(struct cam_periph *periph);
203
204 static struct periph_driver chdriver =
205 {
206 chinit, "ch",
207 TAILQ_HEAD_INITIALIZER(chdriver.units), /* generation */ 0
208 };
209
210 PERIPHDRIVER_DECLARE(ch, chdriver);
211
212 static struct cdevsw ch_cdevsw = {
213 .d_version = D_VERSION,
214 .d_flags = 0,
215 .d_open = chopen,
216 .d_close = chclose,
217 .d_ioctl = chioctl,
218 .d_name = "ch",
219 };
220
221 MALLOC_DEFINE(M_SCSICH, "scsi_ch", "scsi_ch buffers");
222
223 static void
224 chinit(void)
225 {
226 cam_status status;
227
228 /*
229 * Install a global async callback. This callback will
230 * receive async callbacks like "new device found".
231 */
232 status = xpt_register_async(AC_FOUND_DEVICE, chasync, NULL, NULL);
233
234 if (status != CAM_REQ_CMP) {
235 printf("ch: Failed to attach master async callback "
236 "due to status 0x%x!\n", status);
237 }
238 }
239
240 static void
241 choninvalidate(struct cam_periph *periph)
242 {
243 struct ch_softc *softc;
244
245 softc = (struct ch_softc *)periph->softc;
246
247 /*
248 * De-register any async callbacks.
249 */
250 xpt_register_async(0, chasync, periph, periph->path);
251
252 softc->flags |= CH_FLAG_INVALID;
253
254 xpt_print(periph->path, "lost device\n");
255
256 }
257
258 static void
259 chcleanup(struct cam_periph *periph)
260 {
261 struct ch_softc *softc;
262
263 softc = (struct ch_softc *)periph->softc;
264
265 devstat_remove_entry(softc->device_stats);
266 destroy_dev(softc->dev);
267 xpt_print(periph->path, "removing device entry\n");
268 free(softc, M_DEVBUF);
269 }
270
271 static void
272 chasync(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
273 {
274 struct cam_periph *periph;
275
276 periph = (struct cam_periph *)callback_arg;
277
278 switch(code) {
279 case AC_FOUND_DEVICE:
280 {
281 struct ccb_getdev *cgd;
282 cam_status status;
283
284 cgd = (struct ccb_getdev *)arg;
285 if (cgd == NULL)
286 break;
287
288 if (SID_TYPE(&cgd->inq_data)!= T_CHANGER)
289 break;
290
291 /*
292 * Allocate a peripheral instance for
293 * this device and start the probe
294 * process.
295 */
296 status = cam_periph_alloc(chregister, choninvalidate,
297 chcleanup, chstart, "ch",
298 CAM_PERIPH_BIO, cgd->ccb_h.path,
299 chasync, AC_FOUND_DEVICE, cgd);
300
301 if (status != CAM_REQ_CMP
302 && status != CAM_REQ_INPROG)
303 printf("chasync: Unable to probe new device "
304 "due to status 0x%x\n", status);
305
306 break;
307
308 }
309 default:
310 cam_periph_async(periph, code, path, arg);
311 break;
312 }
313 }
314
315 static cam_status
316 chregister(struct cam_periph *periph, void *arg)
317 {
318 struct ch_softc *softc;
319 struct ccb_getdev *cgd;
320
321 cgd = (struct ccb_getdev *)arg;
322 if (periph == NULL) {
323 printf("chregister: periph was NULL!!\n");
324 return(CAM_REQ_CMP_ERR);
325 }
326
327 if (cgd == NULL) {
328 printf("chregister: no getdev CCB, can't register device\n");
329 return(CAM_REQ_CMP_ERR);
330 }
331
332 softc = (struct ch_softc *)malloc(sizeof(*softc),M_DEVBUF,M_NOWAIT);
333
334 if (softc == NULL) {
335 printf("chregister: Unable to probe new device. "
336 "Unable to allocate softc\n");
337 return(CAM_REQ_CMP_ERR);
338 }
339
340 bzero(softc, sizeof(*softc));
341 softc->state = CH_STATE_PROBE;
342 periph->softc = softc;
343 softc->quirks = CH_Q_NONE;
344
345 /*
346 * Changers don't have a blocksize, and obviously don't support
347 * tagged queueing.
348 */
349 cam_periph_unlock(periph);
350 softc->device_stats = devstat_new_entry("ch",
351 periph->unit_number, 0,
352 DEVSTAT_NO_BLOCKSIZE | DEVSTAT_NO_ORDERED_TAGS,
353 SID_TYPE(&cgd->inq_data)| DEVSTAT_TYPE_IF_SCSI,
354 DEVSTAT_PRIORITY_OTHER);
355
356 /* Register the device */
357 softc->dev = make_dev(&ch_cdevsw, periph->unit_number, UID_ROOT,
358 GID_OPERATOR, 0600, "%s%d", periph->periph_name,
359 periph->unit_number);
360 cam_periph_lock(periph);
361 softc->dev->si_drv1 = periph;
362
363 /*
364 * Add an async callback so that we get
365 * notified if this device goes away.
366 */
367 xpt_register_async(AC_LOST_DEVICE, chasync, periph, periph->path);
368
369 /*
370 * Lock this periph until we are setup.
371 * This first call can't block
372 */
373 (void)cam_periph_hold(periph, PRIBIO);
374 xpt_schedule(periph, /*priority*/5);
375
376 return(CAM_REQ_CMP);
377 }
378
379 static int
380 chopen(struct cdev *dev, int flags, int fmt, struct thread *td)
381 {
382 struct cam_periph *periph;
383 struct ch_softc *softc;
384 int error;
385
386 periph = (struct cam_periph *)dev->si_drv1;
387 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
388 return (ENXIO);
389
390 softc = (struct ch_softc *)periph->softc;
391
392 cam_periph_lock(periph);
393
394 if (softc->flags & CH_FLAG_INVALID) {
395 cam_periph_unlock(periph);
396 cam_periph_release(periph);
397 return(ENXIO);
398 }
399
400 if ((softc->flags & CH_FLAG_OPEN) == 0)
401 softc->flags |= CH_FLAG_OPEN;
402 else
403 cam_periph_release(periph);
404
405 if ((error = cam_periph_hold(periph, PRIBIO | PCATCH)) != 0) {
406 cam_periph_unlock(periph);
407 cam_periph_release(periph);
408 return (error);
409 }
410
411 /*
412 * Load information about this changer device into the softc.
413 */
414 if ((error = chgetparams(periph)) != 0) {
415 softc->flags &= ~CH_FLAG_OPEN;
416 cam_periph_unlock(periph);
417 cam_periph_release(periph);
418 return(error);
419 }
420
421 cam_periph_unhold(periph);
422 cam_periph_unlock(periph);
423
424 return(error);
425 }
426
427 static int
428 chclose(struct cdev *dev, int flag, int fmt, struct thread *td)
429 {
430 struct cam_periph *periph;
431 struct ch_softc *softc;
432 int error;
433
434 error = 0;
435
436 periph = (struct cam_periph *)dev->si_drv1;
437 if (periph == NULL)
438 return(ENXIO);
439
440 softc = (struct ch_softc *)periph->softc;
441
442 cam_periph_lock(periph);
443
444 softc->flags &= ~CH_FLAG_OPEN;
445
446 cam_periph_unlock(periph);
447 cam_periph_release(periph);
448
449 return(0);
450 }
451
452 static void
453 chstart(struct cam_periph *periph, union ccb *start_ccb)
454 {
455 struct ch_softc *softc;
456
457 softc = (struct ch_softc *)periph->softc;
458
459 switch (softc->state) {
460 case CH_STATE_NORMAL:
461 {
462 if (periph->immediate_priority <= periph->pinfo.priority){
463 start_ccb->ccb_h.ccb_state = CH_CCB_WAITING;
464
465 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
466 periph_links.sle);
467 periph->immediate_priority = CAM_PRIORITY_NONE;
468 wakeup(&periph->ccb_list);
469 }
470 break;
471 }
472 case CH_STATE_PROBE:
473 {
474 int mode_buffer_len;
475 void *mode_buffer;
476
477 /*
478 * Include the block descriptor when calculating the mode
479 * buffer length,
480 */
481 mode_buffer_len = sizeof(struct scsi_mode_header_6) +
482 sizeof(struct scsi_mode_blk_desc) +
483 sizeof(struct page_element_address_assignment);
484
485 mode_buffer = malloc(mode_buffer_len, M_SCSICH, M_NOWAIT);
486
487 if (mode_buffer == NULL) {
488 printf("chstart: couldn't malloc mode sense data\n");
489 break;
490 }
491 bzero(mode_buffer, mode_buffer_len);
492
493 /*
494 * Get the element address assignment page.
495 */
496 scsi_mode_sense(&start_ccb->csio,
497 /* retries */ 1,
498 /* cbfcnp */ chdone,
499 /* tag_action */ MSG_SIMPLE_Q_TAG,
500 /* dbd */ (softc->quirks & CH_Q_NO_DBD) ?
501 FALSE : TRUE,
502 /* page_code */ SMS_PAGE_CTRL_CURRENT,
503 /* page */ CH_ELEMENT_ADDR_ASSIGN_PAGE,
504 /* param_buf */ (u_int8_t *)mode_buffer,
505 /* param_len */ mode_buffer_len,
506 /* sense_len */ SSD_FULL_SIZE,
507 /* timeout */ CH_TIMEOUT_MODE_SENSE);
508
509 start_ccb->ccb_h.ccb_bp = NULL;
510 start_ccb->ccb_h.ccb_state = CH_CCB_PROBE;
511 xpt_action(start_ccb);
512 break;
513 }
514 }
515 }
516
517 static void
518 chdone(struct cam_periph *periph, union ccb *done_ccb)
519 {
520 struct ch_softc *softc;
521 struct ccb_scsiio *csio;
522
523 softc = (struct ch_softc *)periph->softc;
524 csio = &done_ccb->csio;
525
526 switch(done_ccb->ccb_h.ccb_state) {
527 case CH_CCB_PROBE:
528 {
529 struct scsi_mode_header_6 *mode_header;
530 struct page_element_address_assignment *ea;
531 char announce_buf[80];
532
533
534 mode_header = (struct scsi_mode_header_6 *)csio->data_ptr;
535
536 ea = (struct page_element_address_assignment *)
537 find_mode_page_6(mode_header);
538
539 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP){
540
541 softc->sc_firsts[CHET_MT] = scsi_2btoul(ea->mtea);
542 softc->sc_counts[CHET_MT] = scsi_2btoul(ea->nmte);
543 softc->sc_firsts[CHET_ST] = scsi_2btoul(ea->fsea);
544 softc->sc_counts[CHET_ST] = scsi_2btoul(ea->nse);
545 softc->sc_firsts[CHET_IE] = scsi_2btoul(ea->fieea);
546 softc->sc_counts[CHET_IE] = scsi_2btoul(ea->niee);
547 softc->sc_firsts[CHET_DT] = scsi_2btoul(ea->fdtea);
548 softc->sc_counts[CHET_DT] = scsi_2btoul(ea->ndte);
549 softc->sc_picker = softc->sc_firsts[CHET_MT];
550
551 #define PLURAL(c) (c) == 1 ? "" : "s"
552 snprintf(announce_buf, sizeof(announce_buf),
553 "%d slot%s, %d drive%s, "
554 "%d picker%s, %d portal%s",
555 softc->sc_counts[CHET_ST],
556 PLURAL(softc->sc_counts[CHET_ST]),
557 softc->sc_counts[CHET_DT],
558 PLURAL(softc->sc_counts[CHET_DT]),
559 softc->sc_counts[CHET_MT],
560 PLURAL(softc->sc_counts[CHET_MT]),
561 softc->sc_counts[CHET_IE],
562 PLURAL(softc->sc_counts[CHET_IE]));
563 #undef PLURAL
564 } else {
565 int error;
566
567 error = cherror(done_ccb, CAM_RETRY_SELTO,
568 SF_RETRY_UA | SF_NO_PRINT);
569 /*
570 * Retry any UNIT ATTENTION type errors. They
571 * are expected at boot.
572 */
573 if (error == ERESTART) {
574 /*
575 * A retry was scheuled, so
576 * just return.
577 */
578 return;
579 } else if (error != 0) {
580 int retry_scheduled;
581 struct scsi_mode_sense_6 *sms;
582
583 sms = (struct scsi_mode_sense_6 *)
584 done_ccb->csio.cdb_io.cdb_bytes;
585
586 /*
587 * Check to see if block descriptors were
588 * disabled. Some devices don't like that.
589 * We're taking advantage of the fact that
590 * the first few bytes of the 6 and 10 byte
591 * mode sense commands are the same. If
592 * block descriptors were disabled, enable
593 * them and re-send the command.
594 */
595 if (sms->byte2 & SMS_DBD) {
596 sms->byte2 &= ~SMS_DBD;
597 xpt_action(done_ccb);
598 softc->quirks |= CH_Q_NO_DBD;
599 retry_scheduled = 1;
600 } else
601 retry_scheduled = 0;
602
603 /* Don't wedge this device's queue */
604 cam_release_devq(done_ccb->ccb_h.path,
605 /*relsim_flags*/0,
606 /*reduction*/0,
607 /*timeout*/0,
608 /*getcount_only*/0);
609
610 if (retry_scheduled)
611 return;
612
613 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK)
614 == CAM_SCSI_STATUS_ERROR)
615 scsi_sense_print(&done_ccb->csio);
616 else {
617 xpt_print(periph->path,
618 "got CAM status %#x\n",
619 done_ccb->ccb_h.status);
620 }
621 xpt_print(periph->path, "fatal error, failed "
622 "to attach to device\n");
623
624 cam_periph_invalidate(periph);
625
626 announce_buf[0] = '\0';
627 }
628 }
629 if (announce_buf[0] != '\0')
630 xpt_announce_periph(periph, announce_buf);
631 softc->state = CH_STATE_NORMAL;
632 free(mode_header, M_SCSICH);
633 /*
634 * Since our peripheral may be invalidated by an error
635 * above or an external event, we must release our CCB
636 * before releasing the probe lock on the peripheral.
637 * The peripheral will only go away once the last lock
638 * is removed, and we need it around for the CCB release
639 * operation.
640 */
641 xpt_release_ccb(done_ccb);
642 cam_periph_unhold(periph);
643 return;
644 }
645 case CH_CCB_WAITING:
646 {
647 /* Caller will release the CCB */
648 wakeup(&done_ccb->ccb_h.cbfcnp);
649 return;
650 }
651 default:
652 break;
653 }
654 xpt_release_ccb(done_ccb);
655 }
656
657 static int
658 cherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
659 {
660 struct ch_softc *softc;
661 struct cam_periph *periph;
662
663 periph = xpt_path_periph(ccb->ccb_h.path);
664 softc = (struct ch_softc *)periph->softc;
665
666 return (cam_periph_error(ccb, cam_flags, sense_flags,
667 &softc->saved_ccb));
668 }
669
670 static int
671 chioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
672 {
673 struct cam_periph *periph;
674 struct ch_softc *softc;
675 int error;
676
677 periph = (struct cam_periph *)dev->si_drv1;
678 if (periph == NULL)
679 return(ENXIO);
680
681 cam_periph_lock(periph);
682 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering chioctl\n"));
683
684 softc = (struct ch_softc *)periph->softc;
685
686 error = 0;
687
688 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
689 ("trying to do ioctl %#lx\n", cmd));
690
691 /*
692 * If this command can change the device's state, we must
693 * have the device open for writing.
694 */
695 switch (cmd) {
696 case CHIOGPICKER:
697 case CHIOGPARAMS:
698 case CHIOGSTATUS:
699 break;
700
701 default:
702 if ((flag & FWRITE) == 0) {
703 cam_periph_unlock(periph);
704 return (EBADF);
705 }
706 }
707
708 switch (cmd) {
709 case CHIOMOVE:
710 error = chmove(periph, (struct changer_move *)addr);
711 break;
712
713 case CHIOEXCHANGE:
714 error = chexchange(periph, (struct changer_exchange *)addr);
715 break;
716
717 case CHIOPOSITION:
718 error = chposition(periph, (struct changer_position *)addr);
719 break;
720
721 case CHIOGPICKER:
722 *(int *)addr = softc->sc_picker - softc->sc_firsts[CHET_MT];
723 break;
724
725 case CHIOSPICKER:
726 {
727 int new_picker = *(int *)addr;
728
729 if (new_picker > (softc->sc_counts[CHET_MT] - 1)) {
730 error = EINVAL;
731 break;
732 }
733 softc->sc_picker = softc->sc_firsts[CHET_MT] + new_picker;
734 break;
735 }
736 case CHIOGPARAMS:
737 {
738 struct changer_params *cp = (struct changer_params *)addr;
739
740 cp->cp_npickers = softc->sc_counts[CHET_MT];
741 cp->cp_nslots = softc->sc_counts[CHET_ST];
742 cp->cp_nportals = softc->sc_counts[CHET_IE];
743 cp->cp_ndrives = softc->sc_counts[CHET_DT];
744 break;
745 }
746 case CHIOIELEM:
747 error = chielem(periph, *(unsigned int *)addr);
748 break;
749
750 case CHIOGSTATUS:
751 {
752 error = chgetelemstatus(periph,
753 (struct changer_element_status_request *) addr);
754 break;
755 }
756
757 case CHIOSETVOLTAG:
758 {
759 error = chsetvoltag(periph,
760 (struct changer_set_voltag_request *) addr);
761 break;
762 }
763
764 /* Implement prevent/allow? */
765
766 default:
767 error = cam_periph_ioctl(periph, cmd, addr, cherror);
768 break;
769 }
770
771 cam_periph_unlock(periph);
772 return (error);
773 }
774
775 static int
776 chmove(struct cam_periph *periph, struct changer_move *cm)
777 {
778 struct ch_softc *softc;
779 u_int16_t fromelem, toelem;
780 union ccb *ccb;
781 int error;
782
783 error = 0;
784 softc = (struct ch_softc *)periph->softc;
785
786 /*
787 * Check arguments.
788 */
789 if ((cm->cm_fromtype > CHET_DT) || (cm->cm_totype > CHET_DT))
790 return (EINVAL);
791 if ((cm->cm_fromunit > (softc->sc_counts[cm->cm_fromtype] - 1)) ||
792 (cm->cm_tounit > (softc->sc_counts[cm->cm_totype] - 1)))
793 return (ENODEV);
794
795 /*
796 * Check the request against the changer's capabilities.
797 */
798 if ((softc->sc_movemask[cm->cm_fromtype] & (1 << cm->cm_totype)) == 0)
799 return (ENODEV);
800
801 /*
802 * Calculate the source and destination elements.
803 */
804 fromelem = softc->sc_firsts[cm->cm_fromtype] + cm->cm_fromunit;
805 toelem = softc->sc_firsts[cm->cm_totype] + cm->cm_tounit;
806
807 ccb = cam_periph_getccb(periph, /*priority*/ 1);
808
809 scsi_move_medium(&ccb->csio,
810 /* retries */ 1,
811 /* cbfcnp */ chdone,
812 /* tag_action */ MSG_SIMPLE_Q_TAG,
813 /* tea */ softc->sc_picker,
814 /* src */ fromelem,
815 /* dst */ toelem,
816 /* invert */ (cm->cm_flags & CM_INVERT) ? TRUE : FALSE,
817 /* sense_len */ SSD_FULL_SIZE,
818 /* timeout */ CH_TIMEOUT_MOVE_MEDIUM);
819
820 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
821 /*sense_flags*/ SF_RETRY_UA,
822 softc->device_stats);
823
824 xpt_release_ccb(ccb);
825
826 return(error);
827 }
828
829 static int
830 chexchange(struct cam_periph *periph, struct changer_exchange *ce)
831 {
832 struct ch_softc *softc;
833 u_int16_t src, dst1, dst2;
834 union ccb *ccb;
835 int error;
836
837 error = 0;
838 softc = (struct ch_softc *)periph->softc;
839 /*
840 * Check arguments.
841 */
842 if ((ce->ce_srctype > CHET_DT) || (ce->ce_fdsttype > CHET_DT) ||
843 (ce->ce_sdsttype > CHET_DT))
844 return (EINVAL);
845 if ((ce->ce_srcunit > (softc->sc_counts[ce->ce_srctype] - 1)) ||
846 (ce->ce_fdstunit > (softc->sc_counts[ce->ce_fdsttype] - 1)) ||
847 (ce->ce_sdstunit > (softc->sc_counts[ce->ce_sdsttype] - 1)))
848 return (ENODEV);
849
850 /*
851 * Check the request against the changer's capabilities.
852 */
853 if (((softc->sc_exchangemask[ce->ce_srctype] &
854 (1 << ce->ce_fdsttype)) == 0) ||
855 ((softc->sc_exchangemask[ce->ce_fdsttype] &
856 (1 << ce->ce_sdsttype)) == 0))
857 return (ENODEV);
858
859 /*
860 * Calculate the source and destination elements.
861 */
862 src = softc->sc_firsts[ce->ce_srctype] + ce->ce_srcunit;
863 dst1 = softc->sc_firsts[ce->ce_fdsttype] + ce->ce_fdstunit;
864 dst2 = softc->sc_firsts[ce->ce_sdsttype] + ce->ce_sdstunit;
865
866 ccb = cam_periph_getccb(periph, /*priority*/ 1);
867
868 scsi_exchange_medium(&ccb->csio,
869 /* retries */ 1,
870 /* cbfcnp */ chdone,
871 /* tag_action */ MSG_SIMPLE_Q_TAG,
872 /* tea */ softc->sc_picker,
873 /* src */ src,
874 /* dst1 */ dst1,
875 /* dst2 */ dst2,
876 /* invert1 */ (ce->ce_flags & CE_INVERT1) ?
877 TRUE : FALSE,
878 /* invert2 */ (ce->ce_flags & CE_INVERT2) ?
879 TRUE : FALSE,
880 /* sense_len */ SSD_FULL_SIZE,
881 /* timeout */ CH_TIMEOUT_EXCHANGE_MEDIUM);
882
883 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/CAM_RETRY_SELTO,
884 /*sense_flags*/ SF_RETRY_UA,
885 softc->device_stats);
886
887 xpt_release_ccb(ccb);
888
889 return(error);
890 }
891
892 static int
893 chposition(struct cam_periph *periph, struct changer_position *cp)
894 {
895 struct ch_softc *softc;
896 u_int16_t dst;
897 union ccb *ccb;
898 int error;
899
900 error = 0;
901 softc = (struct ch_softc *)periph->softc;
902
903 /*
904 * Check arguments.
905 */
906 if (cp->cp_type > CHET_DT)
907 return (EINVAL);
908 if (cp->cp_unit > (softc->sc_counts[cp->cp_type] - 1))
909 return (ENODEV);
910
911 /*
912 * Calculate the destination element.
913 */
914 dst = softc->sc_firsts[cp->cp_type] + cp->cp_unit;
915
916 ccb = cam_periph_getccb(periph, /*priority*/ 1);
917
918 scsi_position_to_element(&ccb->csio,
919 /* retries */ 1,
920 /* cbfcnp */ chdone,
921 /* tag_action */ MSG_SIMPLE_Q_TAG,
922 /* tea */ softc->sc_picker,
923 /* dst */ dst,
924 /* invert */ (cp->cp_flags & CP_INVERT) ?
925 TRUE : FALSE,
926 /* sense_len */ SSD_FULL_SIZE,
927 /* timeout */ CH_TIMEOUT_POSITION_TO_ELEMENT);
928
929 error = cam_periph_runccb(ccb, cherror, /*cam_flags*/ CAM_RETRY_SELTO,
930 /*sense_flags*/ SF_RETRY_UA,
931 softc->device_stats);
932
933 xpt_release_ccb(ccb);
934
935 return(error);
936 }
937
938 /*
939 * Copy a volume tag to a volume_tag struct, converting SCSI byte order
940 * to host native byte order in the volume serial number. The volume
941 * label as returned by the changer is transferred to user mode as
942 * nul-terminated string. Volume labels are truncated at the first
943 * space, as suggested by SCSI-2.
944 */
945 static void
946 copy_voltag(struct changer_voltag *uvoltag, struct volume_tag *voltag)
947 {
948 int i;
949 for (i=0; i<CH_VOLTAG_MAXLEN; i++) {
950 char c = voltag->vif[i];
951 if (c && c != ' ')
952 uvoltag->cv_volid[i] = c;
953 else
954 break;
955 }
956 uvoltag->cv_serial = scsi_2btoul(voltag->vsn);
957 }
958
959 /*
960 * Copy an an element status descriptor to a user-mode
961 * changer_element_status structure.
962 */
963
964 static void
965 copy_element_status(struct ch_softc *softc,
966 u_int16_t flags,
967 struct read_element_status_descriptor *desc,
968 struct changer_element_status *ces)
969 {
970 u_int16_t eaddr = scsi_2btoul(desc->eaddr);
971 u_int16_t et;
972
973 ces->ces_int_addr = eaddr;
974 /* set up logical address in element status */
975 for (et = CHET_MT; et <= CHET_DT; et++) {
976 if ((softc->sc_firsts[et] <= eaddr)
977 && ((softc->sc_firsts[et] + softc->sc_counts[et])
978 > eaddr)) {
979 ces->ces_addr = eaddr - softc->sc_firsts[et];
980 ces->ces_type = et;
981 break;
982 }
983 }
984
985 ces->ces_flags = desc->flags1;
986
987 ces->ces_sensecode = desc->sense_code;
988 ces->ces_sensequal = desc->sense_qual;
989
990 if (desc->flags2 & READ_ELEMENT_STATUS_INVERT)
991 ces->ces_flags |= CES_INVERT;
992
993 if (desc->flags2 & READ_ELEMENT_STATUS_SVALID) {
994
995 eaddr = scsi_2btoul(desc->ssea);
996
997 /* convert source address to logical format */
|