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