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