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