2 * Copyright (c) 1997-2007 Kenneth D. Merry
3 * Copyright (c) 2013, 2014, 2015 Spectra Logic Corporation
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13 * substantially similar to the "NO WARRANTY" disclaimer below
14 * ("Disclaimer") and any redistribution must be conditioned upon
15 * including a substantially similar Disclaimer requirement for further
16 * binary redistribution.
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGES.
31 * Authors: Ken Merry (Spectra Logic Corporation)
35 * This is eventually intended to be:
36 * - A basic data transfer/copy utility
37 * - A simple benchmark utility
38 * - An example of how to use the asynchronous pass(4) driver interface.
40 #include <sys/cdefs.h>
41 #include <sys/ioctl.h>
42 #include <sys/stdint.h>
43 #include <sys/types.h>
44 #include <sys/endian.h>
45 #include <sys/param.h>
48 #include <sys/event.h>
53 #include <sys/bus_dma.h>
60 #include <semaphore.h>
74 #include <cam/cam_debug.h>
75 #include <cam/cam_ccb.h>
76 #include <cam/scsi/scsi_all.h>
77 #include <cam/scsi/scsi_da.h>
78 #include <cam/scsi/scsi_pass.h>
79 #include <cam/scsi/scsi_message.h>
80 #include <cam/scsi/smp_all.h>
81 #include <cam/nvme/nvme_all.h>
87 CAMDD_CMD_NONE = 0x00000000,
88 CAMDD_CMD_HELP = 0x00000001,
89 CAMDD_CMD_WRITE = 0x00000002,
90 CAMDD_CMD_READ = 0x00000003
94 CAMDD_ARG_NONE = 0x00000000,
95 CAMDD_ARG_VERBOSE = 0x00000001,
96 CAMDD_ARG_ERR_RECOVER = 0x00000080,
100 CAMDD_DEV_NONE = 0x00,
101 CAMDD_DEV_PASS = 0x01,
102 CAMDD_DEV_FILE = 0x02
105 struct camdd_io_opts {
106 camdd_dev_type dev_type;
109 uint64_t queue_depth;
122 struct camdd_buf_indirect {
124 * Pointer to the source buffer.
126 struct camdd_buf *src_buf;
129 * Offset into the source buffer, in bytes.
133 * Pointer to the starting point in the source buffer.
138 * Length of this chunk in bytes.
143 struct camdd_buf_data {
145 * Buffer allocated when we allocate this camdd_buf. This should
146 * be the size of the blocksize for this device.
151 * The amount of backing store allocated in buf. Generally this
152 * will be the blocksize of the device.
157 * The amount of data that was put into the buffer (on reads) or
158 * the amount of data we have put onto the src_list so far (on
164 * The amount of data that was not transferred.
169 * Starting byte offset on the reader.
171 uint64_t src_start_offset;
174 * CCB used for pass(4) device targets.
179 * Number of scatter/gather segments.
184 * Set if we had to tack on an extra buffer to round the transfer
185 * up to a sector size.
190 * Scatter/gather list used generally when we're the writer for a
193 bus_dma_segment_t *segs;
196 * Scatter/gather list used generally when we're the writer for a
197 * file or block device;
202 union camdd_buf_types {
203 struct camdd_buf_indirect indirect;
204 struct camdd_buf_data data;
210 CAMDD_STATUS_SHORT_IO,
216 camdd_buf_type buf_type;
217 union camdd_buf_types buf_type_spec;
219 camdd_buf_status status;
225 * A reference count of how many indirect buffers point to this
231 * A link back to our parent device.
233 struct camdd_dev *dev;
234 STAILQ_ENTRY(camdd_buf) links;
235 STAILQ_ENTRY(camdd_buf) work_links;
238 * A count of the buffers on the src_list.
243 * List of buffers from our partner thread that are the components
244 * of this buffer for the I/O. Uses src_links.
246 STAILQ_HEAD(,camdd_buf) src_list;
247 STAILQ_ENTRY(camdd_buf) src_links;
250 #define NUM_DEV_TYPES 2
252 struct camdd_dev_pass {
255 struct cam_device *dev;
273 CAMDD_FF_NONE = 0x00,
274 CAMDD_FF_CAN_SEEK = 0x01
277 struct camdd_dev_file {
280 char filename[MAXPATHLEN + 1];
281 camdd_file_type file_type;
282 camdd_file_flags file_flags;
286 struct camdd_dev_block {
292 union camdd_dev_spec {
293 struct camdd_dev_pass pass;
294 struct camdd_dev_file file;
295 struct camdd_dev_block block;
299 CAMDD_DEV_FLAG_NONE = 0x00,
300 CAMDD_DEV_FLAG_EOF = 0x01,
301 CAMDD_DEV_FLAG_PEER_EOF = 0x02,
302 CAMDD_DEV_FLAG_ACTIVE = 0x04,
303 CAMDD_DEV_FLAG_EOF_SENT = 0x08,
304 CAMDD_DEV_FLAG_EOF_QUEUED = 0x10
308 camdd_dev_type dev_type;
309 union camdd_dev_spec dev_spec;
310 camdd_dev_flags flags;
311 char device_name[MAXPATHLEN+1];
313 uint32_t sector_size;
315 uint64_t sector_io_limit;
321 uint64_t start_offset_bytes;
322 uint64_t next_io_pos_bytes;
323 uint64_t next_peer_pos_bytes;
324 uint64_t next_completion_pos_bytes;
325 uint64_t peer_bytes_queued;
326 uint64_t bytes_transferred;
327 uint32_t target_queue_depth;
328 uint32_t cur_active_io;
330 uint32_t extra_buf_len;
331 struct camdd_dev *peer_dev;
332 pthread_mutex_t mutex;
336 int (*run)(struct camdd_dev *dev);
337 int (*fetch)(struct camdd_dev *dev);
340 * Buffers that are available for I/O. Uses links.
342 STAILQ_HEAD(,camdd_buf) free_queue;
345 * Free indirect buffers. These are used for breaking a large
346 * buffer into multiple pieces.
348 STAILQ_HEAD(,camdd_buf) free_indirect_queue;
351 * Buffers that have been queued to the kernel. Uses links.
353 STAILQ_HEAD(,camdd_buf) active_queue;
356 * Will generally contain one of our buffers that is waiting for enough
357 * I/O from our partner thread to be able to execute. This will
358 * generally happen when our per-I/O-size is larger than the
359 * partner thread's per-I/O-size. Uses links.
361 STAILQ_HEAD(,camdd_buf) pending_queue;
364 * Number of buffers on the pending queue
366 int num_pending_queue;
369 * Buffers that are filled and ready to execute. This is used when
370 * our partner (reader) thread sends us blocks that are larger than
371 * our blocksize, and so we have to split them into multiple pieces.
373 STAILQ_HEAD(,camdd_buf) run_queue;
376 * Number of buffers on the run queue.
380 STAILQ_HEAD(,camdd_buf) reorder_queue;
382 int num_reorder_queue;
385 * Buffers that have been queued to us by our partner thread
386 * (generally the reader thread) to be written out. Uses
389 STAILQ_HEAD(,camdd_buf) work_queue;
392 * Buffers that have been completed by our partner thread. Uses
395 STAILQ_HEAD(,camdd_buf) peer_done_queue;
398 * Number of buffers on the peer done queue.
400 uint32_t num_peer_done_queue;
403 * A list of buffers that we have queued to our peer thread. Uses
406 STAILQ_HEAD(,camdd_buf) peer_work_queue;
409 * Number of buffers on the peer work queue.
411 uint32_t num_peer_work_queue;
414 static sem_t camdd_sem;
415 static sig_atomic_t need_exit = 0;
416 static sig_atomic_t error_exit = 0;
417 static sig_atomic_t need_status = 0;
420 #define min(a, b) (a < b) ? a : b
424 /* Generically useful offsets into the peripheral private area */
425 #define ppriv_ptr0 periph_priv.entries[0].ptr
426 #define ppriv_ptr1 periph_priv.entries[1].ptr
427 #define ppriv_field0 periph_priv.entries[0].field
428 #define ppriv_field1 periph_priv.entries[1].field
430 #define ccb_buf ppriv_ptr0
432 #define CAMDD_FILE_DEFAULT_BLOCK 524288
433 #define CAMDD_FILE_DEFAULT_DEPTH 1
434 #define CAMDD_PASS_MAX_BLOCK 1048576
435 #define CAMDD_PASS_DEFAULT_DEPTH 6
436 #define CAMDD_PASS_RW_TIMEOUT 60 * 1000
438 static int parse_btl(char *tstr, int *bus, int *target, int *lun);
439 void camdd_free_dev(struct camdd_dev *dev);
440 struct camdd_dev *camdd_alloc_dev(camdd_dev_type dev_type,
441 struct kevent *new_ke, int num_ke,
442 int retry_count, int timeout);
443 static struct camdd_buf *camdd_alloc_buf(struct camdd_dev *dev,
444 camdd_buf_type buf_type);
445 void camdd_release_buf(struct camdd_buf *buf);
446 struct camdd_buf *camdd_get_buf(struct camdd_dev *dev, camdd_buf_type buf_type);
447 int camdd_buf_sg_create(struct camdd_buf *buf, int iovec,
448 uint32_t sector_size, uint32_t *num_sectors_used,
449 int *double_buf_needed);
450 uint32_t camdd_buf_get_len(struct camdd_buf *buf);
451 void camdd_buf_add_child(struct camdd_buf *buf, struct camdd_buf *child_buf);
452 int camdd_probe_tape(int fd, char *filename, uint64_t *max_iosize,
453 uint64_t *max_blk, uint64_t *min_blk, uint64_t *blk_gran);
454 int camdd_probe_pass_scsi(struct cam_device *cam_dev, union ccb *ccb,
455 camdd_argmask arglist, int probe_retry_count,
456 int probe_timeout, uint64_t *maxsector, uint32_t *block_len);
457 int camdd_probe_pass_nvme(struct cam_device *cam_dev, union ccb *ccb,
458 camdd_argmask arglist, int probe_retry_count,
459 int probe_timeout, uint64_t *maxsector, uint32_t *block_len);
460 struct camdd_dev *camdd_probe_file(int fd, struct camdd_io_opts *io_opts,
461 int retry_count, int timeout);
462 struct camdd_dev *camdd_probe_pass(struct cam_device *cam_dev,
463 struct camdd_io_opts *io_opts,
464 camdd_argmask arglist, int probe_retry_count,
465 int probe_timeout, int io_retry_count,
467 void nvme_read_write(struct ccb_nvmeio *nvmeio, uint32_t retries,
468 void (*cbfcnp)(struct cam_periph *, union ccb *),
469 uint32_t nsid, int readop, uint64_t lba,
470 uint32_t block_count, uint8_t *data_ptr, uint32_t dxfer_len,
472 void *camdd_file_worker(void *arg);
473 camdd_buf_status camdd_ccb_status(union ccb *ccb, int protocol);
474 int camdd_get_cgd(struct cam_device *device, struct ccb_getdev *cgd);
475 int camdd_queue_peer_buf(struct camdd_dev *dev, struct camdd_buf *buf);
476 int camdd_complete_peer_buf(struct camdd_dev *dev, struct camdd_buf *peer_buf);
477 void camdd_peer_done(struct camdd_buf *buf);
478 void camdd_complete_buf(struct camdd_dev *dev, struct camdd_buf *buf,
480 int camdd_pass_fetch(struct camdd_dev *dev);
481 int camdd_file_run(struct camdd_dev *dev);
482 int camdd_pass_run(struct camdd_dev *dev);
483 int camdd_get_next_lba_len(struct camdd_dev *dev, uint64_t *lba, ssize_t *len);
484 int camdd_queue(struct camdd_dev *dev, struct camdd_buf *read_buf);
485 void camdd_get_depth(struct camdd_dev *dev, uint32_t *our_depth,
486 uint32_t *peer_depth, uint32_t *our_bytes,
487 uint32_t *peer_bytes);
488 void *camdd_worker(void *arg);
489 void camdd_sig_handler(int sig);
490 void camdd_print_status(struct camdd_dev *camdd_dev,
491 struct camdd_dev *other_dev,
492 struct timespec *start_time);
493 int camdd_rw(struct camdd_io_opts *io_opts, camdd_argmask arglist,
494 int num_io_opts, uint64_t max_io, int retry_count, int timeout);
495 int camdd_parse_io_opts(char *args, int is_write,
496 struct camdd_io_opts *io_opts);
500 * Parse out a bus, or a bus, target and lun in the following
506 * Returns the number of parsed components, or 0.
509 parse_btl(char *tstr, int *bus, int *target, int *lun)
514 while (isspace(*tstr) && (*tstr != '\0'))
517 tmpstr = (char *)strtok(tstr, ":");
518 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
519 *bus = strtol(tmpstr, NULL, 0);
521 tmpstr = (char *)strtok(NULL, ":");
522 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
523 *target = strtol(tmpstr, NULL, 0);
525 tmpstr = (char *)strtok(NULL, ":");
526 if ((tmpstr != NULL) && (*tmpstr != '\0')) {
527 *lun = strtol(tmpstr, NULL, 0);
537 * XXX KDM clean up and free all of the buffers on the queue!
540 camdd_free_dev(struct camdd_dev *dev)
545 switch (dev->dev_type) {
546 case CAMDD_DEV_FILE: {
547 struct camdd_dev_file *file_dev = &dev->dev_spec.file;
549 if (file_dev->fd != -1)
551 free(file_dev->tmp_buf);
554 case CAMDD_DEV_PASS: {
555 struct camdd_dev_pass *pass_dev = &dev->dev_spec.pass;
557 if (pass_dev->dev != NULL)
558 cam_close_device(pass_dev->dev);
569 camdd_alloc_dev(camdd_dev_type dev_type, struct kevent *new_ke, int num_ke,
570 int retry_count, int timeout)
572 struct camdd_dev *dev = NULL;
577 dev = calloc(1, sizeof(*dev));
579 warn("%s: unable to malloc %zu bytes", __func__, sizeof(*dev));
583 dev->dev_type = dev_type;
584 dev->io_timeout = timeout;
585 dev->retry_count = retry_count;
586 STAILQ_INIT(&dev->free_queue);
587 STAILQ_INIT(&dev->free_indirect_queue);
588 STAILQ_INIT(&dev->active_queue);
589 STAILQ_INIT(&dev->pending_queue);
590 STAILQ_INIT(&dev->run_queue);
591 STAILQ_INIT(&dev->reorder_queue);
592 STAILQ_INIT(&dev->work_queue);
593 STAILQ_INIT(&dev->peer_done_queue);
594 STAILQ_INIT(&dev->peer_work_queue);
595 retval = pthread_mutex_init(&dev->mutex, NULL);
597 warnc(retval, "%s: failed to initialize mutex", __func__);
601 retval = pthread_cond_init(&dev->cond, NULL);
603 warnc(retval, "%s: failed to initialize condition variable",
610 warn("%s: Unable to create kqueue", __func__);
614 ke_size = sizeof(struct kevent) * (num_ke + 4);
615 ke = calloc(1, ke_size);
617 warn("%s: unable to malloc %zu bytes", __func__, ke_size);
621 bcopy(new_ke, ke, num_ke * sizeof(struct kevent));
623 EV_SET(&ke[num_ke++], (uintptr_t)&dev->work_queue, EVFILT_USER,
624 EV_ADD|EV_ENABLE|EV_CLEAR, 0,0, 0);
625 EV_SET(&ke[num_ke++], (uintptr_t)&dev->peer_done_queue, EVFILT_USER,
626 EV_ADD|EV_ENABLE|EV_CLEAR, 0,0, 0);
627 EV_SET(&ke[num_ke++], SIGINFO, EVFILT_SIGNAL, EV_ADD|EV_ENABLE, 0,0,0);
628 EV_SET(&ke[num_ke++], SIGINT, EVFILT_SIGNAL, EV_ADD|EV_ENABLE, 0,0,0);
630 retval = kevent(dev->kq, ke, num_ke, NULL, 0, NULL);
632 warn("%s: Unable to register kevents", __func__);
645 static struct camdd_buf *
646 camdd_alloc_buf(struct camdd_dev *dev, camdd_buf_type buf_type)
648 struct camdd_buf *buf = NULL;
649 uint8_t *data_ptr = NULL;
652 * We only need to allocate data space for data buffers.
656 data_ptr = malloc(dev->blocksize);
657 if (data_ptr == NULL) {
658 warn("unable to allocate %u bytes", dev->blocksize);
666 buf = calloc(1, sizeof(*buf));
668 warn("unable to allocate %zu bytes", sizeof(*buf));
672 buf->buf_type = buf_type;
675 case CAMDD_BUF_DATA: {
676 struct camdd_buf_data *data;
678 data = &buf->buf_type_spec.data;
680 data->alloc_len = dev->blocksize;
681 data->buf = data_ptr;
684 case CAMDD_BUF_INDIRECT:
689 STAILQ_INIT(&buf->src_list);
700 camdd_release_buf(struct camdd_buf *buf)
702 struct camdd_dev *dev;
706 switch (buf->buf_type) {
707 case CAMDD_BUF_DATA: {
708 struct camdd_buf_data *data;
710 data = &buf->buf_type_spec.data;
712 if (data->segs != NULL) {
713 if (data->extra_buf != 0) {
717 data->segs[data->sg_count - 1].ds_addr;
724 } else if (data->iovec != NULL) {
725 if (data->extra_buf != 0) {
726 free(data->iovec[data->sg_count - 1].iov_base);
733 STAILQ_INSERT_TAIL(&dev->free_queue, buf, links);
736 case CAMDD_BUF_INDIRECT:
737 STAILQ_INSERT_TAIL(&dev->free_indirect_queue, buf, links);
740 err(1, "%s: Invalid buffer type %d for released buffer",
741 __func__, buf->buf_type);
747 camdd_get_buf(struct camdd_dev *dev, camdd_buf_type buf_type)
749 struct camdd_buf *buf = NULL;
753 buf = STAILQ_FIRST(&dev->free_queue);
755 struct camdd_buf_data *data;
759 STAILQ_REMOVE_HEAD(&dev->free_queue, links);
760 data = &buf->buf_type_spec.data;
761 data_ptr = data->buf;
762 alloc_len = data->alloc_len;
763 bzero(buf, sizeof(*buf));
764 data->buf = data_ptr;
765 data->alloc_len = alloc_len;
768 case CAMDD_BUF_INDIRECT:
769 buf = STAILQ_FIRST(&dev->free_indirect_queue);
771 STAILQ_REMOVE_HEAD(&dev->free_indirect_queue, links);
773 bzero(buf, sizeof(*buf));
777 warnx("Unknown buffer type %d requested", buf_type);
783 return (camdd_alloc_buf(dev, buf_type));
785 STAILQ_INIT(&buf->src_list);
787 buf->buf_type = buf_type;
794 camdd_buf_sg_create(struct camdd_buf *buf, int iovec, uint32_t sector_size,
795 uint32_t *num_sectors_used, int *double_buf_needed)
797 struct camdd_buf *tmp_buf;
798 struct camdd_buf_data *data;
799 uint8_t *extra_buf = NULL;
800 size_t extra_buf_len = 0;
801 int extra_buf_attached = 0;
804 data = &buf->buf_type_spec.data;
806 data->sg_count = buf->src_count;
808 * Compose a scatter/gather list from all of the buffers in the list.
809 * If the length of the buffer isn't a multiple of the sector size,
810 * we'll have to add an extra buffer. This should only happen
811 * at the end of a transfer.
813 if ((data->fill_len % sector_size) != 0) {
814 extra_buf_len = sector_size - (data->fill_len % sector_size);
815 extra_buf = calloc(extra_buf_len, 1);
816 if (extra_buf == NULL) {
817 warn("%s: unable to allocate %zu bytes for extra "
818 "buffer space", __func__, extra_buf_len);
826 data->segs = calloc(data->sg_count, sizeof(bus_dma_segment_t));
827 if (data->segs == NULL) {
828 warn("%s: unable to allocate %zu bytes for S/G list",
829 __func__, sizeof(bus_dma_segment_t) *
836 data->iovec = calloc(data->sg_count, sizeof(struct iovec));
837 if (data->iovec == NULL) {
838 warn("%s: unable to allocate %zu bytes for S/G list",
839 __func__, sizeof(struct iovec) * data->sg_count);
845 for (i = 0, tmp_buf = STAILQ_FIRST(&buf->src_list);
846 i < buf->src_count && tmp_buf != NULL; i++,
847 tmp_buf = STAILQ_NEXT(tmp_buf, src_links)) {
849 if (tmp_buf->buf_type == CAMDD_BUF_DATA) {
850 struct camdd_buf_data *tmp_data;
852 tmp_data = &tmp_buf->buf_type_spec.data;
854 data->segs[i].ds_addr =
855 (bus_addr_t) tmp_data->buf;
856 data->segs[i].ds_len = tmp_data->fill_len -
859 data->iovec[i].iov_base = tmp_data->buf;
860 data->iovec[i].iov_len = tmp_data->fill_len -
863 if (((tmp_data->fill_len - tmp_data->resid) %
865 *double_buf_needed = 1;
867 struct camdd_buf_indirect *tmp_ind;
869 tmp_ind = &tmp_buf->buf_type_spec.indirect;
871 data->segs[i].ds_addr =
872 (bus_addr_t)tmp_ind->start_ptr;
873 data->segs[i].ds_len = tmp_ind->len;
875 data->iovec[i].iov_base = tmp_ind->start_ptr;
876 data->iovec[i].iov_len = tmp_ind->len;
878 if ((tmp_ind->len % sector_size) != 0)
879 *double_buf_needed = 1;
883 if (extra_buf != NULL) {
885 data->segs[i].ds_addr = (bus_addr_t)extra_buf;
886 data->segs[i].ds_len = extra_buf_len;
888 data->iovec[i].iov_base = extra_buf;
889 data->iovec[i].iov_len = extra_buf_len;
891 extra_buf_attached = 1;
894 if ((tmp_buf != NULL) || (i != data->sg_count)) {
895 warnx("buffer source count does not match "
896 "number of buffers in list!");
903 *num_sectors_used = (data->fill_len + extra_buf_len) /
905 } else if (extra_buf_attached == 0) {
907 * If extra_buf isn't attached yet, we need to free it
918 camdd_buf_get_len(struct camdd_buf *buf)
922 if (buf->buf_type != CAMDD_BUF_DATA) {
923 struct camdd_buf_indirect *indirect;
925 indirect = &buf->buf_type_spec.indirect;
928 struct camdd_buf_data *data;
930 data = &buf->buf_type_spec.data;
931 len = data->fill_len;
938 camdd_buf_add_child(struct camdd_buf *buf, struct camdd_buf *child_buf)
940 struct camdd_buf_data *data;
942 assert(buf->buf_type == CAMDD_BUF_DATA);
944 data = &buf->buf_type_spec.data;
946 STAILQ_INSERT_TAIL(&buf->src_list, child_buf, src_links);
949 data->fill_len += camdd_buf_get_len(child_buf);
957 } camdd_status_item_index;
959 static struct camdd_status_items {
961 struct mt_status_entry *entry;
962 } req_status_items[] = {
965 { "blk_gran", NULL },
966 { "max_effective_iosize", NULL }
970 camdd_probe_tape(int fd, char *filename, uint64_t *max_iosize,
971 uint64_t *max_blk, uint64_t *min_blk, uint64_t *blk_gran)
973 struct mt_status_data status_data;
974 char *xml_str = NULL;
978 retval = mt_get_xml_str(fd, MTIOCEXTGET, &xml_str);
980 err(1, "Couldn't get XML string from %s", filename);
982 retval = mt_get_status(xml_str, &status_data);
983 if (retval != XML_STATUS_OK) {
984 warn("couldn't get status for %s", filename);
990 if (status_data.error != 0) {
991 warnx("%s", status_data.error_str);
996 for (i = 0; i < nitems(req_status_items); i++) {
999 name = __DECONST(char *, req_status_items[i].name);
1000 req_status_items[i].entry = mt_status_entry_find(&status_data,
1002 if (req_status_items[i].entry == NULL) {
1003 errx(1, "Cannot find status entry %s",
1004 req_status_items[i].name);
1008 *max_iosize = req_status_items[CAMDD_TS_EFF_IOSIZE].entry->value_unsigned;
1009 *max_blk= req_status_items[CAMDD_TS_MAX_BLK].entry->value_unsigned;
1010 *min_blk= req_status_items[CAMDD_TS_MIN_BLK].entry->value_unsigned;
1011 *blk_gran = req_status_items[CAMDD_TS_BLK_GRAN].entry->value_unsigned;
1015 mt_status_free(&status_data);
1021 camdd_probe_file(int fd, struct camdd_io_opts *io_opts, int retry_count,
1024 struct camdd_dev *dev = NULL;
1025 struct camdd_dev_file *file_dev;
1026 uint64_t blocksize = io_opts->blocksize;
1028 dev = camdd_alloc_dev(CAMDD_DEV_FILE, NULL, 0, retry_count, timeout);
1032 file_dev = &dev->dev_spec.file;
1034 strlcpy(file_dev->filename, io_opts->dev_name,
1035 sizeof(file_dev->filename));
1036 strlcpy(dev->device_name, io_opts->dev_name, sizeof(dev->device_name));
1038 dev->blocksize = CAMDD_FILE_DEFAULT_BLOCK;
1040 dev->blocksize = blocksize;
1042 if ((io_opts->queue_depth != 0)
1043 && (io_opts->queue_depth != 1)) {
1044 warnx("Queue depth %ju for %s ignored, only 1 outstanding "
1045 "command supported", (uintmax_t)io_opts->queue_depth,
1048 dev->target_queue_depth = CAMDD_FILE_DEFAULT_DEPTH;
1049 dev->run = camdd_file_run;
1053 * We can effectively access files on byte boundaries. We'll reset
1054 * this for devices like disks that can be accessed on sector
1057 dev->sector_size = 1;
1059 if ((fd != STDIN_FILENO)
1060 && (fd != STDOUT_FILENO)) {
1063 retval = fstat(fd, &file_dev->sb);
1065 warn("Cannot stat %s", dev->device_name);
1068 if (S_ISREG(file_dev->sb.st_mode)) {
1069 file_dev->file_type = CAMDD_FILE_REG;
1070 } else if (S_ISCHR(file_dev->sb.st_mode)) {
1073 if (ioctl(fd, FIODTYPE, &type) == -1)
1074 err(1, "FIODTYPE ioctl failed on %s",
1078 file_dev->file_type = CAMDD_FILE_TAPE;
1079 else if (type & D_DISK)
1080 file_dev->file_type = CAMDD_FILE_DISK;
1081 else if (type & D_MEM)
1082 file_dev->file_type = CAMDD_FILE_MEM;
1083 else if (type & D_TTY)
1084 file_dev->file_type = CAMDD_FILE_TTY;
1086 } else if (S_ISDIR(file_dev->sb.st_mode)) {
1087 errx(1, "cannot operate on directory %s",
1089 } else if (S_ISFIFO(file_dev->sb.st_mode)) {
1090 file_dev->file_type = CAMDD_FILE_PIPE;
1092 errx(1, "Cannot determine file type for %s",
1095 switch (file_dev->file_type) {
1096 case CAMDD_FILE_REG:
1097 if (file_dev->sb.st_size != 0)
1098 dev->max_sector = file_dev->sb.st_size - 1;
1100 dev->max_sector = 0;
1101 file_dev->file_flags |= CAMDD_FF_CAN_SEEK;
1103 case CAMDD_FILE_TAPE: {
1104 uint64_t max_iosize, max_blk, min_blk, blk_gran;
1106 * Check block limits and maximum effective iosize.
1107 * Make sure the blocksize is within the block
1108 * limits (and a multiple of the minimum blocksize)
1109 * and that the blocksize is <= maximum effective
1112 retval = camdd_probe_tape(fd, dev->device_name,
1113 &max_iosize, &max_blk, &min_blk, &blk_gran);
1115 errx(1, "Unable to probe tape %s",
1119 * The blocksize needs to be <= the maximum
1120 * effective I/O size of the tape device. Note
1121 * that this also takes into account the maximum
1122 * blocksize reported by READ BLOCK LIMITS.
1124 if (dev->blocksize > max_iosize) {
1125 warnx("Blocksize %u too big for %s, limiting "
1126 "to %ju", dev->blocksize, dev->device_name,
1128 dev->blocksize = max_iosize;
1132 * The blocksize needs to be at least min_blk;
1134 if (dev->blocksize < min_blk) {
1135 warnx("Blocksize %u too small for %s, "
1136 "increasing to %ju", dev->blocksize,
1137 dev->device_name, min_blk);
1138 dev->blocksize = min_blk;
1142 * And the blocksize needs to be a multiple of
1143 * the block granularity.
1146 && (dev->blocksize % (1 << blk_gran))) {
1147 warnx("Blocksize %u for %s not a multiple of "
1148 "%d, adjusting to %d", dev->blocksize,
1149 dev->device_name, (1 << blk_gran),
1150 dev->blocksize & ~((1 << blk_gran) - 1));
1151 dev->blocksize &= ~((1 << blk_gran) - 1);
1154 if (dev->blocksize == 0) {
1155 errx(1, "Unable to derive valid blocksize for "
1156 "%s", dev->device_name);
1160 * For tape drives, set the sector size to the
1161 * blocksize so that we make sure not to write
1162 * less than the blocksize out to the drive.
1164 dev->sector_size = dev->blocksize;
1167 case CAMDD_FILE_DISK: {
1169 unsigned int sector_size;
1171 file_dev->file_flags |= CAMDD_FF_CAN_SEEK;
1173 if (ioctl(fd, DIOCGSECTORSIZE, §or_size) == -1) {
1174 err(1, "DIOCGSECTORSIZE ioctl failed on %s",
1178 if (sector_size == 0) {
1179 errx(1, "DIOCGSECTORSIZE ioctl returned "
1180 "invalid sector size %u for %s",
1181 sector_size, dev->device_name);
1184 if (ioctl(fd, DIOCGMEDIASIZE, &media_size) == -1) {
1185 err(1, "DIOCGMEDIASIZE ioctl failed on %s",
1189 if (media_size == 0) {
1190 errx(1, "DIOCGMEDIASIZE ioctl returned "
1191 "invalid media size %ju for %s",
1192 (uintmax_t)media_size, dev->device_name);
1195 if (dev->blocksize % sector_size) {
1196 errx(1, "%s blocksize %u not a multiple of "
1197 "sector size %u", dev->device_name,
1198 dev->blocksize, sector_size);
1201 dev->sector_size = sector_size;
1202 dev->max_sector = (media_size / sector_size) - 1;
1205 case CAMDD_FILE_MEM:
1206 file_dev->file_flags |= CAMDD_FF_CAN_SEEK;
1213 if ((io_opts->offset != 0)
1214 && ((file_dev->file_flags & CAMDD_FF_CAN_SEEK) == 0)) {
1215 warnx("Offset %ju specified for %s, but we cannot seek on %s",
1216 io_opts->offset, io_opts->dev_name, io_opts->dev_name);
1220 else if ((io_opts->offset != 0)
1221 && ((io_opts->offset % dev->sector_size) != 0)) {
1222 warnx("Offset %ju for %s is not a multiple of the "
1223 "sector size %u", io_opts->offset,
1224 io_opts->dev_name, dev->sector_size);
1227 dev->start_offset_bytes = io_opts->offset;
1235 camdd_free_dev(dev);
1240 * Get a get device CCB for the specified device.
1243 camdd_get_cgd(struct cam_device *device, struct ccb_getdev *cgd)
1248 ccb = cam_getccb(device);
1251 warnx("%s: couldn't allocate CCB", __func__);
1255 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->cgd);
1257 ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1259 if (cam_send_ccb(device, ccb) < 0) {
1260 warn("%s: error sending Get Device Information CCB", __func__);
1261 cam_error_print(device, ccb, CAM_ESF_ALL,
1262 CAM_EPF_ALL, stderr);
1267 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1268 cam_error_print(device, ccb, CAM_ESF_ALL,
1269 CAM_EPF_ALL, stderr);
1274 bcopy(&ccb->cgd, cgd, sizeof(struct ccb_getdev));
1283 camdd_probe_pass_scsi(struct cam_device *cam_dev, union ccb *ccb,
1284 camdd_argmask arglist, int probe_retry_count,
1285 int probe_timeout, uint64_t *maxsector, uint32_t *block_len)
1287 struct scsi_read_capacity_data rcap;
1288 struct scsi_read_capacity_data_long rcaplong;
1292 warnx("%s: error passed ccb is NULL", __func__);
1296 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
1298 scsi_read_capacity(&ccb->csio,
1299 /*retries*/ probe_retry_count,
1301 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1304 /*timeout*/ probe_timeout ? probe_timeout : 5000);
1306 /* Disable freezing the device queue */
1307 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1309 if (arglist & CAMDD_ARG_ERR_RECOVER)
1310 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1312 if (cam_send_ccb(cam_dev, ccb) < 0) {
1313 warn("error sending READ CAPACITY command");
1315 cam_error_print(cam_dev, ccb, CAM_ESF_ALL,
1316 CAM_EPF_ALL, stderr);
1321 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1322 cam_error_print(cam_dev, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
1326 *maxsector = scsi_4btoul(rcap.addr);
1327 *block_len = scsi_4btoul(rcap.length);
1330 * A last block of 2^32-1 means that the true capacity is over 2TB,
1331 * and we need to issue the long READ CAPACITY to get the real
1332 * capacity. Otherwise, we're all set.
1334 if (*maxsector != 0xffffffff) {
1339 scsi_read_capacity_16(&ccb->csio,
1340 /*retries*/ probe_retry_count,
1342 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1346 (uint8_t *)&rcaplong,
1348 /*sense_len*/ SSD_FULL_SIZE,
1349 /*timeout*/ probe_timeout ? probe_timeout : 5000);
1351 /* Disable freezing the device queue */
1352 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1354 if (arglist & CAMDD_ARG_ERR_RECOVER)
1355 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1357 if (cam_send_ccb(cam_dev, ccb) < 0) {
1358 warn("error sending READ CAPACITY (16) command");
1359 cam_error_print(cam_dev, ccb, CAM_ESF_ALL,
1360 CAM_EPF_ALL, stderr);
1364 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1365 cam_error_print(cam_dev, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
1369 *maxsector = scsi_8btou64(rcaplong.addr);
1370 *block_len = scsi_4btoul(rcaplong.length);
1379 camdd_probe_pass_nvme(struct cam_device *cam_dev, union ccb *ccb,
1380 camdd_argmask arglist, int probe_retry_count,
1381 int probe_timeout, uint64_t *maxsector, uint32_t *block_len)
1383 struct nvme_command *nc = NULL;
1384 struct nvme_namespace_data nsdata;
1385 uint32_t nsid = cam_dev->target_lun & UINT32_MAX;
1386 uint8_t format = 0, lbads = 0;
1390 warnx("%s: error passed ccb is NULL", __func__);
1394 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->nvmeio);
1396 /* Send Identify Namespace to get block size and capacity */
1397 nc = &ccb->nvmeio.cmd;
1398 nc->opc = NVME_OPC_IDENTIFY;
1401 nc->cdw10 = 0; /* Identify Namespace is CNS = 0 */
1403 cam_fill_nvmeadmin(&ccb->nvmeio,
1404 /*retries*/ probe_retry_count,
1411 /* Disable freezing the device queue */
1412 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
1414 if (arglist & CAMDD_ARG_ERR_RECOVER)
1415 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
1417 if (cam_send_ccb(cam_dev, ccb) < 0) {
1418 warn("error sending Identify Namespace command");
1420 cam_error_print(cam_dev, ccb, CAM_ESF_ALL,
1421 CAM_EPF_ALL, stderr);
1426 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1427 cam_error_print(cam_dev, ccb, CAM_ESF_ALL, CAM_EPF_ALL, stderr);
1431 *maxsector = nsdata.nsze;
1432 /* The LBA Data Size (LBADS) is reported as a power of 2 */
1433 format = nsdata.flbas & NVME_NS_DATA_FLBAS_FORMAT_MASK;
1434 lbads = (nsdata.lbaf[format] >> NVME_NS_DATA_LBAF_LBADS_SHIFT) &
1435 NVME_NS_DATA_LBAF_LBADS_MASK;
1436 *block_len = 1 << lbads;
1445 * Need to implement this. Do a basic probe:
1446 * - Check the inquiry data, make sure we're talking to a device that we
1447 * can reasonably expect to talk to -- direct, RBC, CD, WORM.
1448 * - Send a test unit ready, make sure the device is available.
1449 * - Get the capacity and block size.
1452 camdd_probe_pass(struct cam_device *cam_dev, struct camdd_io_opts *io_opts,
1453 camdd_argmask arglist, int probe_retry_count,
1454 int probe_timeout, int io_retry_count, int io_timeout)
1457 uint64_t maxsector = 0;
1458 uint32_t cpi_maxio, max_iosize, pass_numblocks;
1459 uint32_t block_len = 0;
1460 struct camdd_dev *dev = NULL;
1461 struct camdd_dev_pass *pass_dev;
1463 struct ccb_getdev cgd;
1465 int scsi_dev_type = T_NODEVICE;
1467 if ((retval = camdd_get_cgd(cam_dev, &cgd)) != 0) {
1468 warnx("%s: error retrieving CGD", __func__);
1472 ccb = cam_getccb(cam_dev);
1475 warnx("%s: error allocating ccb", __func__);
1479 switch (cgd.protocol) {
1481 scsi_dev_type = SID_TYPE(&cam_dev->inq_data);
1484 * For devices that support READ CAPACITY, we'll attempt to get the
1485 * capacity. Otherwise, we really don't support tape or other
1486 * devices via SCSI passthrough, so just return an error in that case.
1488 switch (scsi_dev_type) {
1497 errx(1, "Unsupported SCSI device type %d", scsi_dev_type);
1498 break; /*NOTREACHED*/
1501 if ((retval = camdd_probe_pass_scsi(cam_dev, ccb, probe_retry_count,
1502 arglist, probe_timeout, &maxsector,
1508 if ((retval = camdd_probe_pass_nvme(cam_dev, ccb, probe_retry_count,
1509 arglist, probe_timeout, &maxsector,
1515 errx(1, "Unsupported PROTO type %d", cgd.protocol);
1516 break; /*NOTREACHED*/
1519 if (block_len == 0) {
1520 warnx("Sector size for %s%u is 0, cannot continue",
1521 cam_dev->device_name, cam_dev->dev_unit_num);
1525 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->cpi);
1527 ccb->ccb_h.func_code = XPT_PATH_INQ;
1528 ccb->ccb_h.flags = CAM_DIR_NONE;
1529 ccb->ccb_h.retry_count = 1;
1531 if (cam_send_ccb(cam_dev, ccb) < 0) {
1532 warn("error sending XPT_PATH_INQ CCB");
1534 cam_error_print(cam_dev, ccb, CAM_ESF_ALL,
1535 CAM_EPF_ALL, stderr);
1539 EV_SET(&ke, cam_dev->fd, EVFILT_READ, EV_ADD|EV_ENABLE, 0, 0, 0);
1541 dev = camdd_alloc_dev(CAMDD_DEV_PASS, &ke, 1, io_retry_count,
1546 pass_dev = &dev->dev_spec.pass;
1547 pass_dev->scsi_dev_type = scsi_dev_type;
1548 pass_dev->protocol = cgd.protocol;
1549 pass_dev->dev = cam_dev;
1550 pass_dev->max_sector = maxsector;
1551 pass_dev->block_len = block_len;
1552 pass_dev->cpi_maxio = ccb->cpi.maxio;
1553 snprintf(dev->device_name, sizeof(dev->device_name), "%s%u",
1554 pass_dev->dev->device_name, pass_dev->dev->dev_unit_num);
1555 dev->sector_size = block_len;
1556 dev->max_sector = maxsector;
1560 * Determine the optimal blocksize to use for this device.
1564 * If the controller has not specified a maximum I/O size,
1565 * just go with 128K as a somewhat conservative value.
1567 if (pass_dev->cpi_maxio == 0)
1570 cpi_maxio = pass_dev->cpi_maxio;
1573 * If the controller has a large maximum I/O size, limit it
1574 * to something smaller so that the kernel doesn't have trouble
1575 * allocating buffers to copy data in and out for us.
1576 * XXX KDM this is until we have unmapped I/O support in the kernel.
1578 max_iosize = min(cpi_maxio, CAMDD_PASS_MAX_BLOCK);
1581 * If we weren't able to get a block size for some reason,
1582 * default to 512 bytes.
1584 block_len = pass_dev->block_len;
1589 * Figure out how many blocksize chunks will fit in the
1592 pass_numblocks = max_iosize / block_len;
1595 * And finally, multiple the number of blocks by the LBA
1596 * length to get our maximum block size;
1598 dev->blocksize = pass_numblocks * block_len;
1600 if (io_opts->blocksize != 0) {
1601 if ((io_opts->blocksize % dev->sector_size) != 0) {
1602 warnx("Blocksize %ju for %s is not a multiple of "
1603 "sector size %u", (uintmax_t)io_opts->blocksize,
1604 dev->device_name, dev->sector_size);
1607 dev->blocksize = io_opts->blocksize;
1609 dev->target_queue_depth = CAMDD_PASS_DEFAULT_DEPTH;
1610 if (io_opts->queue_depth != 0)
1611 dev->target_queue_depth = io_opts->queue_depth;
1613 if (io_opts->offset != 0) {
1614 if (io_opts->offset > (dev->max_sector * dev->sector_size)) {
1615 warnx("Offset %ju is past the end of device %s",
1616 io_opts->offset, dev->device_name);
1620 else if ((io_opts->offset % dev->sector_size) != 0) {
1621 warnx("Offset %ju for %s is not a multiple of the "
1622 "sector size %u", io_opts->offset,
1623 dev->device_name, dev->sector_size);
1626 dev->start_offset_bytes = io_opts->offset;
1630 dev->min_cmd_size = io_opts->min_cmd_size;
1632 dev->run = camdd_pass_run;
1633 dev->fetch = camdd_pass_fetch;
1643 camdd_free_dev(dev);
1649 nvme_read_write(struct ccb_nvmeio *nvmeio, uint32_t retries,
1650 void (*cbfcnp)(struct cam_periph *, union ccb *),
1651 uint32_t nsid, int readop, uint64_t lba,
1652 uint32_t block_count, uint8_t *data_ptr, uint32_t dxfer_len,
1655 struct nvme_command *nc = &nvmeio->cmd;
1657 nc->opc = readop ? NVME_OPC_READ : NVME_OPC_WRITE;
1661 nc->cdw10 = lba & UINT32_MAX;
1662 nc->cdw11 = lba >> 32;
1664 /* NLB (bits 15:0) is a zero based value */
1665 nc->cdw12 = (block_count - 1) & UINT16_MAX;
1667 cam_fill_nvmeio(nvmeio,
1670 readop ? CAM_DIR_IN : CAM_DIR_OUT,
1677 camdd_worker(void *arg)
1679 struct camdd_dev *dev = arg;
1680 struct camdd_buf *buf;
1681 struct timespec ts, *kq_ts;
1686 pthread_mutex_lock(&dev->mutex);
1688 dev->flags |= CAMDD_DEV_FLAG_ACTIVE;
1695 * XXX KDM check the reorder queue depth?
1697 if (dev->write_dev == 0) {
1698 uint32_t our_depth, peer_depth, peer_bytes, our_bytes;
1699 uint32_t target_depth = dev->target_queue_depth;
1700 uint32_t peer_target_depth =
1701 dev->peer_dev->target_queue_depth;
1702 uint32_t peer_blocksize = dev->peer_dev->blocksize;
1704 camdd_get_depth(dev, &our_depth, &peer_depth,
1705 &our_bytes, &peer_bytes);
1708 while (((our_depth < target_depth)
1709 && (peer_depth < peer_target_depth))
1710 || ((peer_bytes + our_bytes) <
1711 (peer_blocksize * 2))) {
1713 while (((our_depth + peer_depth) <
1714 (target_depth + peer_target_depth))
1715 || ((peer_bytes + our_bytes) <
1716 (peer_blocksize * 3))) {
1718 retval = camdd_queue(dev, NULL);
1721 else if (retval != 0) {
1726 camdd_get_depth(dev, &our_depth, &peer_depth,
1727 &our_bytes, &peer_bytes);
1731 * See if we have any I/O that is ready to execute.
1733 buf = STAILQ_FIRST(&dev->run_queue);
1735 while (dev->target_queue_depth > dev->cur_active_io) {
1736 retval = dev->run(dev);
1738 dev->flags |= CAMDD_DEV_FLAG_EOF;
1741 } else if (retval != 0) {
1748 * We've reached EOF, or our partner has reached EOF.
1750 if ((dev->flags & CAMDD_DEV_FLAG_EOF)
1751 || (dev->flags & CAMDD_DEV_FLAG_PEER_EOF)) {
1752 if (dev->write_dev != 0) {
1753 if ((STAILQ_EMPTY(&dev->work_queue))
1754 && (dev->num_run_queue == 0)
1755 && (dev->cur_active_io == 0)) {
1760 * If we're the reader, and the writer
1761 * got EOF, he is already done. If we got
1762 * the EOF, then we need to wait until
1763 * everything is flushed out for the writer.
1765 if (dev->flags & CAMDD_DEV_FLAG_PEER_EOF) {
1767 } else if ((dev->num_peer_work_queue == 0)
1768 && (dev->num_peer_done_queue == 0)
1769 && (dev->cur_active_io == 0)
1770 && (dev->num_run_queue == 0)) {
1775 * XXX KDM need to do something about the pending
1776 * queue and cleanup resources.
1780 if ((dev->write_dev == 0)
1781 && (dev->cur_active_io == 0)
1782 && (dev->peer_bytes_queued < dev->peer_dev->blocksize))
1788 * Run kevent to see if there are events to process.
1790 pthread_mutex_unlock(&dev->mutex);
1791 retval = kevent(dev->kq, NULL, 0, &ke, 1, kq_ts);
1792 pthread_mutex_lock(&dev->mutex);
1794 warn("%s: error returned from kevent",__func__);
1796 } else if (retval != 0) {
1797 switch (ke.filter) {
1799 if (dev->fetch != NULL) {
1800 retval = dev->fetch(dev);
1809 * We register for this so we don't get
1810 * an error as a result of a SIGINFO or a
1811 * SIGINT. It will actually get handled
1812 * by the signal handler. If we get a
1813 * SIGINT, bail out without printing an
1814 * error message. Any other signals
1815 * will result in the error message above.
1817 if (ke.ident == SIGINT)
1823 * Check to see if the other thread has
1824 * queued any I/O for us to do. (In this
1825 * case we're the writer.)
1827 for (buf = STAILQ_FIRST(&dev->work_queue);
1829 buf = STAILQ_FIRST(&dev->work_queue)) {
1830 STAILQ_REMOVE_HEAD(&dev->work_queue,
1832 retval = camdd_queue(dev, buf);
1834 * We keep going unless we get an
1835 * actual error. If we get EOF, we
1836 * still want to remove the buffers
1837 * from the queue and send the back
1838 * to the reader thread.
1848 * Next check to see if the other thread has
1849 * queued any completed buffers back to us.
1850 * (In this case we're the reader.)
1852 for (buf = STAILQ_FIRST(&dev->peer_done_queue);
1854 buf = STAILQ_FIRST(&dev->peer_done_queue)){
1856 &dev->peer_done_queue, work_links);
1857 dev->num_peer_done_queue--;
1858 camdd_peer_done(buf);
1862 warnx("%s: unknown kevent filter %d",
1863 __func__, ke.filter);
1871 dev->flags &= ~CAMDD_DEV_FLAG_ACTIVE;
1873 /* XXX KDM cleanup resources here? */
1875 pthread_mutex_unlock(&dev->mutex);
1878 sem_post(&camdd_sem);
1884 * Simplistic translation of CCB status to our local status.
1887 camdd_ccb_status(union ccb *ccb, int protocol)
1889 camdd_buf_status status = CAMDD_STATUS_NONE;
1890 cam_status ccb_status;
1892 ccb_status = ccb->ccb_h.status & CAM_STATUS_MASK;
1896 switch (ccb_status) {
1898 if (ccb->csio.resid == 0) {
1899 status = CAMDD_STATUS_OK;
1900 } else if (ccb->csio.dxfer_len > ccb->csio.resid) {
1901 status = CAMDD_STATUS_SHORT_IO;
1903 status = CAMDD_STATUS_EOF;
1907 case CAM_SCSI_STATUS_ERROR: {
1908 switch (ccb->csio.scsi_status) {
1909 case SCSI_STATUS_OK:
1910 case SCSI_STATUS_COND_MET:
1911 case SCSI_STATUS_INTERMED:
1912 case SCSI_STATUS_INTERMED_COND_MET:
1913 status = CAMDD_STATUS_OK;
1915 case SCSI_STATUS_CMD_TERMINATED:
1916 case SCSI_STATUS_CHECK_COND:
1917 case SCSI_STATUS_QUEUE_FULL:
1918 case SCSI_STATUS_BUSY:
1919 case SCSI_STATUS_RESERV_CONFLICT:
1921 status = CAMDD_STATUS_ERROR;
1927 status = CAMDD_STATUS_ERROR;
1932 switch (ccb_status) {
1934 status = CAMDD_STATUS_OK;
1937 status = CAMDD_STATUS_ERROR;
1942 status = CAMDD_STATUS_ERROR;
1950 * Queue a buffer to our peer's work thread for writing.
1952 * Returns 0 for success, -1 for failure, 1 if the other thread exited.
1955 camdd_queue_peer_buf(struct camdd_dev *dev, struct camdd_buf *buf)
1958 STAILQ_HEAD(, camdd_buf) local_queue;
1959 struct camdd_buf *buf1, *buf2;
1960 struct camdd_buf_data *data = NULL;
1961 uint64_t peer_bytes_queued = 0;
1965 STAILQ_INIT(&local_queue);
1968 * Since we're the reader, we need to queue our I/O to the writer
1969 * in sequential order in order to make sure it gets written out
1970 * in sequential order.
1972 * Check the next expected I/O starting offset. If this doesn't
1973 * match, put it on the reorder queue.
1975 if ((buf->lba * dev->sector_size) != dev->next_completion_pos_bytes) {
1978 * If there is nothing on the queue, there is no sorting
1981 if (STAILQ_EMPTY(&dev->reorder_queue)) {
1982 STAILQ_INSERT_TAIL(&dev->reorder_queue, buf, links);
1983 dev->num_reorder_queue++;
1988 * Sort in ascending order by starting LBA. There should
1989 * be no identical LBAs.
1991 for (buf1 = STAILQ_FIRST(&dev->reorder_queue); buf1 != NULL;
1993 buf2 = STAILQ_NEXT(buf1, links);
1994 if (buf->lba < buf1->lba) {
1996 * If we're less than the first one, then
1997 * we insert at the head of the list
1998 * because this has to be the first element
2001 STAILQ_INSERT_HEAD(&dev->reorder_queue,
2003 dev->num_reorder_queue++;
2005 } else if (buf->lba > buf1->lba) {
2007 STAILQ_INSERT_TAIL(&dev->reorder_queue,
2009 dev->num_reorder_queue++;
2011 } else if (buf->lba < buf2->lba) {
2012 STAILQ_INSERT_AFTER(&dev->reorder_queue,
2014 dev->num_reorder_queue++;
2018 errx(1, "Found buffers with duplicate LBA %ju!",
2026 * We're the next expected I/O completion, so put ourselves
2027 * on the local queue to be sent to the writer. We use
2028 * work_links here so that we can queue this to the
2029 * peer_work_queue before taking the buffer off of the
2032 dev->next_completion_pos_bytes += buf->len;
2033 STAILQ_INSERT_TAIL(&local_queue, buf, work_links);
2036 * Go through the reorder queue looking for more sequential
2037 * I/O and add it to the local queue.
2039 for (buf1 = STAILQ_FIRST(&dev->reorder_queue); buf1 != NULL;
2040 buf1 = STAILQ_FIRST(&dev->reorder_queue)) {
2042 * As soon as we see an I/O that is out of sequence,
2045 if ((buf1->lba * dev->sector_size) !=
2046 dev->next_completion_pos_bytes)
2049 STAILQ_REMOVE_HEAD(&dev->reorder_queue, links);
2050 dev->num_reorder_queue--;
2051 STAILQ_INSERT_TAIL(&local_queue, buf1, work_links);
2052 dev->next_completion_pos_bytes += buf1->len;
2057 * Setup the event to let the other thread know that it has work
2060 EV_SET(&ke, (uintptr_t)&dev->peer_dev->work_queue, EVFILT_USER, 0,
2061 NOTE_TRIGGER, 0, NULL);
2064 * Put this on our shadow queue so that we know what we've queued
2065 * to the other thread.
2067 STAILQ_FOREACH_SAFE(buf1, &local_queue, work_links, buf2) {
2068 if (buf1->buf_type != CAMDD_BUF_DATA) {
2069 errx(1, "%s: should have a data buffer, not an "
2070 "indirect buffer", __func__);
2072 data = &buf1->buf_type_spec.data;
2075 * We only need to send one EOF to the writer, and don't
2076 * need to continue sending EOFs after that.
2078 if (buf1->status == CAMDD_STATUS_EOF) {
2079 if (dev->flags & CAMDD_DEV_FLAG_EOF_SENT) {
2080 STAILQ_REMOVE(&local_queue, buf1, camdd_buf,
2082 camdd_release_buf(buf1);
2086 dev->flags |= CAMDD_DEV_FLAG_EOF_SENT;
2090 STAILQ_INSERT_TAIL(&dev->peer_work_queue, buf1, links);
2091 peer_bytes_queued += (data->fill_len - data->resid);
2092 dev->peer_bytes_queued += (data->fill_len - data->resid);
2093 dev->num_peer_work_queue++;
2096 if (STAILQ_FIRST(&local_queue) == NULL)
2100 * Drop our mutex and pick up the other thread's mutex. We need to
2101 * do this to avoid deadlocks.
2103 pthread_mutex_unlock(&dev->mutex);
2104 pthread_mutex_lock(&dev->peer_dev->mutex);
2106 if (dev->peer_dev->flags & CAMDD_DEV_FLAG_ACTIVE) {
2108 * Put the buffers on the other thread's incoming work queue.
2110 for (buf1 = STAILQ_FIRST(&local_queue); buf1 != NULL;
2111 buf1 = STAILQ_FIRST(&local_queue)) {
2112 STAILQ_REMOVE_HEAD(&local_queue, work_links);
2113 STAILQ_INSERT_TAIL(&dev->peer_dev->work_queue, buf1,
2117 * Send an event to the other thread's kqueue to let it know
2118 * that there is something on the work queue.
2120 retval = kevent(dev->peer_dev->kq, &ke, 1, NULL, 0, NULL);
2122 warn("%s: unable to add peer work_queue kevent",
2129 pthread_mutex_unlock(&dev->peer_dev->mutex);
2130 pthread_mutex_lock(&dev->mutex);
2133 * If the other side isn't active, run through the queue and
2134 * release all of the buffers.
2137 for (buf1 = STAILQ_FIRST(&local_queue); buf1 != NULL;
2138 buf1 = STAILQ_FIRST(&local_queue)) {
2139 STAILQ_REMOVE_HEAD(&local_queue, work_links);
2140 STAILQ_REMOVE(&dev->peer_work_queue, buf1, camdd_buf,
2142 dev->num_peer_work_queue--;
2143 camdd_release_buf(buf1);
2145 dev->peer_bytes_queued -= peer_bytes_queued;
2154 * Return a buffer to the reader thread when we have completed writing it.
2157 camdd_complete_peer_buf(struct camdd_dev *dev, struct camdd_buf *peer_buf)
2163 * Setup the event to let the other thread know that we have
2164 * completed a buffer.
2166 EV_SET(&ke, (uintptr_t)&dev->peer_dev->peer_done_queue, EVFILT_USER, 0,
2167 NOTE_TRIGGER, 0, NULL);
2170 * Drop our lock and acquire the other thread's lock before
2173 pthread_mutex_unlock(&dev->mutex);
2174 pthread_mutex_lock(&dev->peer_dev->mutex);
2177 * Put the buffer on the reader thread's peer done queue now that
2178 * we have completed it.
2180 STAILQ_INSERT_TAIL(&dev->peer_dev->peer_done_queue, peer_buf,
2182 dev->peer_dev->num_peer_done_queue++;
2185 * Send an event to the peer thread to let it know that we've added
2186 * something to its peer done queue.
2188 retval = kevent(dev->peer_dev->kq, &ke, 1, NULL, 0, NULL);
2190 warn("%s: unable to add peer_done_queue kevent", __func__);
2195 * Drop the other thread's lock and reacquire ours.
2197 pthread_mutex_unlock(&dev->peer_dev->mutex);
2198 pthread_mutex_lock(&dev->mutex);
2204 * Free a buffer that was written out by the writer thread and returned to
2205 * the reader thread.
2208 camdd_peer_done(struct camdd_buf *buf)
2210 struct camdd_dev *dev;
2211 struct camdd_buf_data *data;
2214 if (buf->buf_type != CAMDD_BUF_DATA) {
2215 errx(1, "%s: should have a data buffer, not an "
2216 "indirect buffer", __func__);
2219 data = &buf->buf_type_spec.data;
2221 STAILQ_REMOVE(&dev->peer_work_queue, buf, camdd_buf, links);
2222 dev->num_peer_work_queue--;
2223 dev->peer_bytes_queued -= (data->fill_len - data->resid);
2225 if (buf->status == CAMDD_STATUS_EOF)
2226 dev->flags |= CAMDD_DEV_FLAG_PEER_EOF;
2228 STAILQ_INSERT_TAIL(&dev->free_queue, buf, links);
2232 * Assumes caller holds the lock for this device.
2235 camdd_complete_buf(struct camdd_dev *dev, struct camdd_buf *buf,
2241 * If we're the reader, we need to send the completed I/O
2242 * to the writer. If we're the writer, we need to just
2243 * free up resources, or let the reader know if we've
2244 * encountered an error.
2246 if (dev->write_dev == 0) {
2247 retval = camdd_queue_peer_buf(dev, buf);
2251 struct camdd_buf *tmp_buf, *next_buf;
2253 STAILQ_FOREACH_SAFE(tmp_buf, &buf->src_list, src_links,
2255 struct camdd_buf *src_buf;
2256 struct camdd_buf_indirect *indirect;
2258 STAILQ_REMOVE(&buf->src_list, tmp_buf,
2259 camdd_buf, src_links);
2261 tmp_buf->status = buf->status;
2263 if (tmp_buf->buf_type == CAMDD_BUF_DATA) {
2264 camdd_complete_peer_buf(dev, tmp_buf);
2268 indirect = &tmp_buf->buf_type_spec.indirect;
2269 src_buf = indirect->src_buf;
2270 src_buf->refcount--;
2272 * XXX KDM we probably need to account for
2273 * exactly how many bytes we were able to
2274 * write. Allocate the residual to the
2275 * first N buffers? Or just track the
2276 * number of bytes written? Right now the reader
2277 * doesn't do anything with a residual.
2279 src_buf->status = buf->status;
2280 if (src_buf->refcount <= 0)
2281 camdd_complete_peer_buf(dev, src_buf);
2282 STAILQ_INSERT_TAIL(&dev->free_indirect_queue,
2286 STAILQ_INSERT_TAIL(&dev->free_queue, buf, links);
2291 * Fetch all completed commands from the pass(4) device.
2293 * Returns the number of commands received, or -1 if any of the commands
2294 * completed with an error. Returns 0 if no commands are available.
2297 camdd_pass_fetch(struct camdd_dev *dev)
2299 struct camdd_dev_pass *pass_dev = &dev->dev_spec.pass;
2301 int retval = 0, num_fetched = 0, error_count = 0;
2303 pthread_mutex_unlock(&dev->mutex);
2305 * XXX KDM we don't distinguish between EFAULT and ENOENT.
2307 while ((retval = ioctl(pass_dev->dev->fd, CAMIOGET, &ccb)) != -1) {
2308 struct camdd_buf *buf;
2309 struct camdd_buf_data *data;
2310 cam_status ccb_status;
2313 buf = ccb.ccb_h.ccb_buf;
2314 data = &buf->buf_type_spec.data;
2315 buf_ccb = &data->ccb;
2320 * Copy the CCB back out so we get status, sense data, etc.
2322 bcopy(&ccb, buf_ccb, sizeof(ccb));
2324 pthread_mutex_lock(&dev->mutex);
2327 * We're now done, so take this off the active queue.
2329 STAILQ_REMOVE(&dev->active_queue, buf, camdd_buf, links);
2330 dev->cur_active_io--;
2332 ccb_status = ccb.ccb_h.status & CAM_STATUS_MASK;
2333 if (ccb_status != CAM_REQ_CMP) {
2334 cam_error_print(pass_dev->dev, &ccb, CAM_ESF_ALL,
2335 CAM_EPF_ALL, stderr);
2338 switch (pass_dev->protocol) {
2340 data->resid = ccb.csio.resid;
2341 dev->bytes_transferred += (ccb.csio.dxfer_len - ccb.csio.resid);
2345 dev->bytes_transferred += ccb.nvmeio.dxfer_len;
2352 if (buf->status == CAMDD_STATUS_NONE)
2353 buf->status = camdd_ccb_status(&ccb, pass_dev->protocol);
2354 if (buf->status == CAMDD_STATUS_ERROR)
2356 else if (buf->status == CAMDD_STATUS_EOF) {
2358 * Once we queue this buffer to our partner thread,
2359 * he will know that we've hit EOF.
2361 dev->flags |= CAMDD_DEV_FLAG_EOF;
2364 camdd_complete_buf(dev, buf, &error_count);
2367 * Unlock in preparation for the ioctl call.
2369 pthread_mutex_unlock(&dev->mutex);
2372 pthread_mutex_lock(&dev->mutex);
2374 if (error_count > 0)
2377 return (num_fetched);
2381 * Returns -1 for error, 0 for success/continue, and 1 for resource
2382 * shortage/stop processing.
2385 camdd_file_run(struct camdd_dev *dev)
2387 struct camdd_dev_file *file_dev = &dev->dev_spec.file;
2388 struct camdd_buf_data *data;
2389 struct camdd_buf *buf;
2391 int retval = 0, write_dev = dev->write_dev;
2392 int error_count = 0, no_resources = 0, double_buf_needed = 0;
2393 uint32_t num_sectors = 0, db_len = 0;
2395 buf = STAILQ_FIRST(&dev->run_queue);
2399 } else if ((dev->write_dev == 0)
2400 && (dev->flags & (CAMDD_DEV_FLAG_EOF |
2401 CAMDD_DEV_FLAG_EOF_SENT))) {
2402 STAILQ_REMOVE(&dev->run_queue, buf, camdd_buf, links);
2403 dev->num_run_queue--;
2404 buf->status = CAMDD_STATUS_EOF;
2410 * If we're writing, we need to go through the source buffer list
2411 * and create an S/G list.
2413 if (write_dev != 0) {
2414 retval = camdd_buf_sg_create(buf, /*iovec*/ 1,
2415 dev->sector_size, &num_sectors, &double_buf_needed);
2422 STAILQ_REMOVE(&dev->run_queue, buf, camdd_buf, links);
2423 dev->num_run_queue--;
2425 data = &buf->buf_type_spec.data;
2428 * pread(2) and pwrite(2) offsets are byte offsets.
2430 io_offset = buf->lba * dev->sector_size;
2433 * Unlock the mutex while we read or write.
2435 pthread_mutex_unlock(&dev->mutex);
2438 * Note that we don't need to double buffer if we're the reader
2439 * because in that case, we have allocated a single buffer of
2440 * sufficient size to do the read. This copy is necessary on
2441 * writes because if one of the components of the S/G list is not
2442 * a sector size multiple, the kernel will reject the write. This
2443 * is unfortunate but not surprising. So this will make sure that
2444 * we're using a single buffer that is a multiple of the sector size.
2446 if ((double_buf_needed != 0)
2447 && (data->sg_count > 1)
2448 && (write_dev != 0)) {
2449 uint32_t cur_offset;
2452 if (file_dev->tmp_buf == NULL)
2453 file_dev->tmp_buf = calloc(dev->blocksize, 1);
2454 if (file_dev->tmp_buf == NULL) {
2455 buf->status = CAMDD_STATUS_ERROR;
2457 pthread_mutex_lock(&dev->mutex);
2460 for (i = 0, cur_offset = 0; i < data->sg_count; i++) {
2461 bcopy(data->iovec[i].iov_base,
2462 &file_dev->tmp_buf[cur_offset],
2463 data->iovec[i].iov_len);
2464 cur_offset += data->iovec[i].iov_len;
2466 db_len = cur_offset;
2469 if (file_dev->file_flags & CAMDD_FF_CAN_SEEK) {
2470 if (write_dev == 0) {
2472 * XXX KDM is there any way we would need a S/G
2475 retval = pread(file_dev->fd, data->buf,
2476 buf->len, io_offset);
2478 if (double_buf_needed != 0) {
2479 retval = pwrite(file_dev->fd, file_dev->tmp_buf,
2481 } else if (data->sg_count == 0) {
2482 retval = pwrite(file_dev->fd, data->buf,
2483 data->fill_len, io_offset);
2485 retval = pwritev(file_dev->fd, data->iovec,
2486 data->sg_count, io_offset);
2490 if (write_dev == 0) {
2492 * XXX KDM is there any way we would need a S/G
2495 retval = read(file_dev->fd, data->buf, buf->len);
2497 if (double_buf_needed != 0) {
2498 retval = write(file_dev->fd, file_dev->tmp_buf,
2500 } else if (data->sg_count == 0) {
2501 retval = write(file_dev->fd, data->buf,
2504 retval = writev(file_dev->fd, data->iovec,
2510 /* We're done, re-acquire the lock */
2511 pthread_mutex_lock(&dev->mutex);
2513 if (retval >= (ssize_t)data->fill_len) {
2515 * If the bytes transferred is more than the request size,
2516 * that indicates an overrun, which should only happen at
2517 * the end of a transfer if we have to round up to a sector
2520 if (buf->status == CAMDD_STATUS_NONE)
2521 buf->status = CAMDD_STATUS_OK;
2523 dev->bytes_transferred += retval;
2524 } else if (retval == -1) {
2525 warn("Error %s %s", (write_dev) ? "writing to" :
2526 "reading from", file_dev->filename);
2528 buf->status = CAMDD_STATUS_ERROR;
2529 data->resid = data->fill_len;
2532 if (dev->debug == 0)
2535 if ((double_buf_needed != 0)
2536 && (write_dev != 0)) {
2537 fprintf(stderr, "%s: fd %d, DB buf %p, len %u lba %ju "
2538 "offset %ju\n", __func__, file_dev->fd,
2539 file_dev->tmp_buf, db_len, (uintmax_t)buf->lba,
2540 (uintmax_t)io_offset);
2541 } else if (data->sg_count == 0) {
2542 fprintf(stderr, "%s: fd %d, buf %p, len %u, lba %ju "
2543 "offset %ju\n", __func__, file_dev->fd, data->buf,
2544 data->fill_len, (uintmax_t)buf->lba,
2545 (uintmax_t)io_offset);
2549 fprintf(stderr, "%s: fd %d, len %u, lba %ju "
2550 "offset %ju\n", __func__, file_dev->fd,
2551 data->fill_len, (uintmax_t)buf->lba,
2552 (uintmax_t)io_offset);
2554 for (i = 0; i < data->sg_count; i++) {
2555 fprintf(stderr, "index %d ptr %p len %zu\n",
2556 i, data->iovec[i].iov_base,
2557 data->iovec[i].iov_len);
2560 } else if (retval == 0) {
2561 buf->status = CAMDD_STATUS_EOF;
2562 if (dev->debug != 0)
2563 printf("%s: got EOF from %s!\n", __func__,
2564 file_dev->filename);
2565 data->resid = data->fill_len;
2567 } else if (retval < (ssize_t)data->fill_len) {
2568 if (buf->status == CAMDD_STATUS_NONE)
2569 buf->status = CAMDD_STATUS_SHORT_IO;
2570 data->resid = data->fill_len - retval;
2571 dev->bytes_transferred += retval;
2576 if (buf->status == CAMDD_STATUS_EOF) {
2577 struct camdd_buf *buf2;
2578 dev->flags |= CAMDD_DEV_FLAG_EOF;
2579 STAILQ_FOREACH(buf2, &dev->run_queue, links)
2580 buf2->status = CAMDD_STATUS_EOF;
2583 camdd_complete_buf(dev, buf, &error_count);
2586 if (error_count != 0)
2588 else if (no_resources != 0)
2595 * Execute one command from the run queue. Returns 0 for success, 1 for
2596 * stop processing, and -1 for error.
2599 camdd_pass_run(struct camdd_dev *dev)
2601 struct camdd_buf *buf = NULL;
2602 struct camdd_dev_pass *pass_dev = &dev->dev_spec.pass;
2603 struct camdd_buf_data *data;
2604 uint32_t num_blocks, sectors_used = 0;
2606 int retval = 0, is_write = dev->write_dev;
2607 int double_buf_needed = 0;
2609 buf = STAILQ_FIRST(&dev->run_queue);
2616 * If we're writing, we need to go through the source buffer list
2617 * and create an S/G list.
2619 if (is_write != 0) {
2620 retval = camdd_buf_sg_create(buf, /*iovec*/ 0,dev->sector_size,
2621 §ors_used, &double_buf_needed);
2628 STAILQ_REMOVE(&dev->run_queue, buf, camdd_buf, links);
2629 dev->num_run_queue--;
2631 data = &buf->buf_type_spec.data;
2634 * In almost every case the number of blocks should be the device
2635 * block size. The exception may be at the end of an I/O stream
2636 * for a partial block or at the end of a device.
2639 num_blocks = sectors_used;
2641 num_blocks = data->fill_len / pass_dev->block_len;
2645 switch (pass_dev->protocol) {
2647 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->csio);
2649 scsi_read_write(&ccb->csio,
2650 /*retries*/ dev->retry_count,
2652 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2653 /*readop*/ (dev->write_dev == 0) ? SCSI_RW_READ :
2656 /*minimum_cmd_size*/ dev->min_cmd_size,
2658 /*block_count*/ num_blocks,
2659 /*data_ptr*/ (data->sg_count != 0) ?
2660 (uint8_t *)data->segs : data->buf,
2661 /*dxfer_len*/ (num_blocks * pass_dev->block_len),
2662 /*sense_len*/ SSD_FULL_SIZE,
2663 /*timeout*/ dev->io_timeout);
2665 if (data->sg_count != 0) {
2666 ccb->csio.sglist_cnt = data->sg_count;
2670 CCB_CLEAR_ALL_EXCEPT_HDR(&ccb->nvmeio);
2672 nvme_read_write(&ccb->nvmeio,
2673 /*retries*/ dev->retry_count,
2675 /*nsid*/ pass_dev->dev->target_lun & UINT32_MAX,
2676 /*readop*/ dev->write_dev == 0,
2678 /*block_count*/ num_blocks,
2679 /*data_ptr*/ (data->sg_count != 0) ?
2680 (uint8_t *)data->segs : data->buf,
2681 /*dxfer_len*/ (num_blocks * pass_dev->block_len),
2682 /*timeout*/ dev->io_timeout);
2684 ccb->nvmeio.sglist_cnt = data->sg_count;
2691 /* Disable freezing the device queue */
2692 ccb->ccb_h.flags |= CAM_DEV_QFRZDIS;
2694 if (dev->retry_count != 0)
2695 ccb->ccb_h.flags |= CAM_PASS_ERR_RECOVER;
2697 if (data->sg_count != 0) {
2698 ccb->ccb_h.flags |= CAM_DATA_SG;
2702 * Store a pointer to the buffer in the CCB. The kernel will
2703 * restore this when we get it back, and we'll use it to identify
2704 * the buffer this CCB came from.
2706 ccb->ccb_h.ccb_buf = buf;
2709 * Unlock our mutex in preparation for issuing the ioctl.
2711 pthread_mutex_unlock(&dev->mutex);
2713 * Queue the CCB to the pass(4) driver.
2715 if (ioctl(pass_dev->dev->fd, CAMIOQUEUE, ccb) == -1) {
2716 pthread_mutex_lock(&dev->mutex);
2718 warn("%s: error sending CAMIOQUEUE ioctl to %s%u", __func__,
2719 pass_dev->dev->device_name, pass_dev->dev->dev_unit_num);
2720 warn("%s: CCB address is %p", __func__, ccb);
2723 STAILQ_INSERT_TAIL(&dev->free_queue, buf, links);
2725 pthread_mutex_lock(&dev->mutex);
2727 dev->cur_active_io++;
2728 STAILQ_INSERT_TAIL(&dev->active_queue, buf, links);
2736 camdd_get_next_lba_len(struct camdd_dev *dev, uint64_t *lba, ssize_t *len)
2738 uint32_t num_blocks;
2741 *lba = dev->next_io_pos_bytes / dev->sector_size;
2742 *len = dev->blocksize;
2743 num_blocks = *len / dev->sector_size;
2746 * If max_sector is 0, then we have no set limit. This can happen
2747 * if we're writing to a file in a filesystem, or reading from
2748 * something like /dev/zero.
2750 if ((dev->max_sector != 0)
2751 || (dev->sector_io_limit != 0)) {
2752 uint64_t max_sector;
2754 if ((dev->max_sector != 0)
2755 && (dev->sector_io_limit != 0))
2756 max_sector = min(dev->sector_io_limit, dev->max_sector);
2757 else if (dev->max_sector != 0)
2758 max_sector = dev->max_sector;
2760 max_sector = dev->sector_io_limit;
2764 * Check to see whether we're starting off past the end of
2765 * the device. If so, we need to just send an EOF
2766 * notification to the writer.
2768 if (*lba > max_sector) {
2771 } else if (((*lba + num_blocks) > max_sector + 1)
2772 || ((*lba + num_blocks) < *lba)) {
2774 * If we get here (but pass the first check), we
2775 * can trim the request length down to go to the
2776 * end of the device.
2778 num_blocks = (max_sector + 1) - *lba;
2779 *len = num_blocks * dev->sector_size;
2784 dev->next_io_pos_bytes += *len;
2790 * Returns 0 for success, 1 for EOF detected, and -1 for failure.
2793 camdd_queue(struct camdd_dev *dev, struct camdd_buf *read_buf)
2795 struct camdd_buf *buf = NULL;
2796 struct camdd_buf_data *data;
2798 struct camdd_buf_data *rb_data;
2799 int is_write = dev->write_dev;
2800 int eof_flush_needed = 0;
2804 * If we've gotten EOF or our partner has, we should not continue
2805 * queueing I/O. If we're a writer, though, we should continue
2806 * to write any buffers that don't have EOF status.
2808 if ((dev->flags & CAMDD_DEV_FLAG_EOF)
2809 || ((dev->flags & CAMDD_DEV_FLAG_PEER_EOF)
2810 && (is_write == 0))) {
2812 * Tell the worker thread that we have seen EOF.
2817 * If we're the writer, send the buffer back with EOF status.
2820 read_buf->status = CAMDD_STATUS_EOF;
2822 camdd_complete_peer_buf(dev, read_buf);
2827 if (is_write == 0) {
2828 buf = camdd_get_buf(dev, CAMDD_BUF_DATA);
2833 data = &buf->buf_type_spec.data;
2835 retval = camdd_get_next_lba_len(dev, &buf->lba, &buf->len);
2837 buf->status = CAMDD_STATUS_EOF;
2840 && ((dev->flags & (CAMDD_DEV_FLAG_EOF_SENT |
2841 CAMDD_DEV_FLAG_EOF_QUEUED)) != 0)) {
2842 camdd_release_buf(buf);
2845 dev->flags |= CAMDD_DEV_FLAG_EOF_QUEUED;
2848 data->fill_len = buf->len;
2849 data->src_start_offset = buf->lba * dev->sector_size;
2852 * Put this on the run queue.
2854 STAILQ_INSERT_TAIL(&dev->run_queue, buf, links);
2855 dev->num_run_queue++;
2862 * Check for new EOF status from the reader.
2864 if ((read_buf->status == CAMDD_STATUS_EOF)
2865 || (read_buf->status == CAMDD_STATUS_ERROR)) {
2866 dev->flags |= CAMDD_DEV_FLAG_PEER_EOF;
2867 if ((STAILQ_FIRST(&dev->pending_queue) == NULL)
2868 && (read_buf->len == 0)) {
2869 camdd_complete_peer_buf(dev, read_buf);
2873 eof_flush_needed = 1;
2877 * See if we have a buffer we're composing with pieces from our
2880 buf = STAILQ_FIRST(&dev->pending_queue);
2885 retval = camdd_get_next_lba_len(dev, &lba, &len);
2887 read_buf->status = CAMDD_STATUS_EOF;
2890 dev->flags |= CAMDD_DEV_FLAG_EOF;
2891 camdd_complete_peer_buf(dev, read_buf);
2897 * If we don't have a pending buffer, we need to grab a new
2898 * one from the free list or allocate another one.
2900 buf = camdd_get_buf(dev, CAMDD_BUF_DATA);
2909 STAILQ_INSERT_TAIL(&dev->pending_queue, buf, links);
2910 dev->num_pending_queue++;
2913 data = &buf->buf_type_spec.data;
2915 rb_data = &read_buf->buf_type_spec.data;
2917 if ((rb_data->src_start_offset != dev->next_peer_pos_bytes)
2918 && (dev->debug != 0)) {
2919 printf("%s: WARNING: reader offset %#jx != expected offset "
2920 "%#jx\n", __func__, (uintmax_t)rb_data->src_start_offset,
2921 (uintmax_t)dev->next_peer_pos_bytes);
2923 dev->next_peer_pos_bytes = rb_data->src_start_offset +
2924 (rb_data->fill_len - rb_data->resid);
2926 new_len = (rb_data->fill_len - rb_data->resid) + data->fill_len;
2927 if (new_len < buf->len) {
2929 * There are three cases here:
2930 * 1. We need more data to fill up a block, so we put
2931 * this I/O on the queue and wait for more I/O.
2932 * 2. We have a pending buffer in the queue that is
2933 * smaller than our blocksize, but we got an EOF. So we
2934 * need to go ahead and flush the write out.
2935 * 3. We got an error.
2939 * Increment our fill length.
2941 data->fill_len += (rb_data->fill_len - rb_data->resid);
2944 * Add the new read buffer to the list for writing.
2946 STAILQ_INSERT_TAIL(&buf->src_list, read_buf, src_links);
2948 /* Increment the count */
2951 if (eof_flush_needed == 0) {
2953 * We need to exit, because we don't have enough
2959 * Take the buffer off of the pending queue.
2961 STAILQ_REMOVE(&dev->pending_queue, buf, camdd_buf,
2963 dev->num_pending_queue--;
2966 * If we need an EOF flush, but there is no data
2967 * to flush, go ahead and return this buffer.
2969 if (data->fill_len == 0) {
2970 camdd_complete_buf(dev, buf, /*error_count*/0);
2976 * Put this on the next queue for execution.
2978 STAILQ_INSERT_TAIL(&dev->run_queue, buf, links);
2979 dev->num_run_queue++;
2981 } else if (new_len == buf->len) {
2983 * We have enough data to completey fill one block,
2984 * so we're ready to issue the I/O.
2988 * Take the buffer off of the pending queue.
2990 STAILQ_REMOVE(&dev->pending_queue, buf, camdd_buf, links);
2991 dev->num_pending_queue--;
2994 * Add the new read buffer to the list for writing.
2996 STAILQ_INSERT_TAIL(&buf->src_list, read_buf, src_links);
2998 /* Increment the count */
3002 * Increment our fill length.
3004 data->fill_len += (rb_data->fill_len - rb_data->resid);
3007 * Put this on the next queue for execution.
3009 STAILQ_INSERT_TAIL(&dev->run_queue, buf, links);
3010 dev->num_run_queue++;
3012 struct camdd_buf *idb;
3013 struct camdd_buf_indirect *indirect;
3014 uint32_t len_to_go, cur_offset;
3017 idb = camdd_get_buf(dev, CAMDD_BUF_INDIRECT);
3022 indirect = &idb->buf_type_spec.indirect;
3023 indirect->src_buf = read_buf;
3024 read_buf->refcount++;
3025 indirect->offset = 0;
3026 indirect->start_ptr = rb_data->buf;
3028 * We've already established that there is more
3029 * data in read_buf than we have room for in our
3030 * current write request. So this particular chunk
3031 * of the request should just be the remainder
3032 * needed to fill up a block.
3034 indirect->len = buf->len - (data->fill_len - data->resid);
3036 camdd_buf_add_child(buf, idb);
3039 * This buffer is ready to execute, so we can take
3040 * it off the pending queue and put it on the run
3043 STAILQ_REMOVE(&dev->pending_queue, buf, camdd_buf,
3045 dev->num_pending_queue--;
3046 STAILQ_INSERT_TAIL(&dev->run_queue, buf, links);
3047 dev->num_run_queue++;
3049 cur_offset = indirect->offset + indirect->len;
3052 * The resulting I/O would be too large to fit in
3053 * one block. We need to split this I/O into
3054 * multiple pieces. Allocate as many buffers as needed.
3056 for (len_to_go = rb_data->fill_len - rb_data->resid -
3057 indirect->len; len_to_go > 0;) {
3058 struct camdd_buf *new_buf;
3059 struct camdd_buf_data *new_data;
3063 retval = camdd_get_next_lba_len(dev, &lba, &len);
3067 * The device has already been marked
3068 * as EOF, and there is no space left.
3073 new_buf = camdd_get_buf(dev, CAMDD_BUF_DATA);
3074 if (new_buf == NULL) {
3082 idb = camdd_get_buf(dev, CAMDD_BUF_INDIRECT);
3088 indirect = &idb->buf_type_spec.indirect;
3090 indirect->src_buf = read_buf;
3091 read_buf->refcount++;
3092 indirect->offset = cur_offset;
3093 indirect->start_ptr = rb_data->buf + cur_offset;
3094 indirect->len = min(len_to_go, new_buf->len);
3096 if (((indirect->len % dev->sector_size) != 0)
3097 || ((indirect->offset % dev->sector_size) != 0)) {
3098 warnx("offset %ju len %ju not aligned with "
3099 "sector size %u", indirect->offset,
3100 (uintmax_t)indirect->len, dev->sector_size);
3103 cur_offset += indirect->len;
3104 len_to_go -= indirect->len;
3106 camdd_buf_add_child(new_buf, idb);
3108 new_data = &new_buf->buf_type_spec.data;
3110 if ((new_data->fill_len == new_buf->len)
3111 || (eof_flush_needed != 0)) {
3112 STAILQ_INSERT_TAIL(&dev->run_queue,
3114 dev->num_run_queue++;
3115 } else if (new_data->fill_len < buf->len) {
3116 STAILQ_INSERT_TAIL(&dev->pending_queue,
3118 dev->num_pending_queue++;
3120 warnx("%s: too much data in new "
3121 "buffer!", __func__);
3133 camdd_get_depth(struct camdd_dev *dev, uint32_t *our_depth,
3134 uint32_t *peer_depth, uint32_t *our_bytes, uint32_t *peer_bytes)
3136 *our_depth = dev->cur_active_io + dev->num_run_queue;
3137 if (dev->num_peer_work_queue >
3138 dev->num_peer_done_queue)
3139 *peer_depth = dev->num_peer_work_queue -
3140 dev->num_peer_done_queue;
3143 *our_bytes = *our_depth * dev->blocksize;
3144 *peer_bytes = dev->peer_bytes_queued;
3148 camdd_sig_handler(int sig)
3157 sem_post(&camdd_sem);
3161 camdd_print_status(struct camdd_dev *camdd_dev, struct camdd_dev *other_dev,
3162 struct timespec *start_time)
3164 struct timespec done_time;
3166 long double mb_sec, total_sec;
3169 error = clock_gettime(CLOCK_MONOTONIC_PRECISE, &done_time);
3171 warn("Unable to get done time");
3175 timespecsub(&done_time, start_time, &done_time);
3177 total_ns = done_time.tv_nsec + (done_time.tv_sec * 1000000000);
3178 total_sec = total_ns;
3179 total_sec /= 1000000000;
3181 fprintf(stderr, "%ju bytes %s %s\n%ju bytes %s %s\n"
3182 "%.4Lf seconds elapsed\n",
3183 (uintmax_t)camdd_dev->bytes_transferred,
3184 (camdd_dev->write_dev == 0) ? "read from" : "written to",
3185 camdd_dev->device_name,
3186 (uintmax_t)other_dev->bytes_transferred,
3187 (other_dev->write_dev == 0) ? "read from" : "written to",
3188 other_dev->device_name, total_sec);
3190 mb_sec = min(other_dev->bytes_transferred,camdd_dev->bytes_transferred);
3191 mb_sec /= 1024 * 1024;
3192 mb_sec *= 1000000000;
3194 fprintf(stderr, "%.2Lf MB/sec\n", mb_sec);
3198 camdd_rw(struct camdd_io_opts *io_opts, camdd_argmask arglist, int num_io_opts,
3199 uint64_t max_io, int retry_count, int timeout)
3201 struct cam_device *new_cam_dev = NULL;
3202 struct camdd_dev *devs[2];
3203 struct timespec start_time;
3204 pthread_t threads[2];
3209 bzero(devs, sizeof(devs));
3211 if (num_io_opts != 2) {
3212 warnx("Must have one input and one output path");
3217 for (i = 0; i < num_io_opts; i++) {
3218 switch (io_opts[i].dev_type) {
3219 case CAMDD_DEV_PASS: {
3220 if (isdigit(io_opts[i].dev_name[0])) {
3221 int bus = 0, target = 0, lun = 0;
3224 /* device specified as bus:target[:lun] */
3225 rv = parse_btl(io_opts[i].dev_name, &bus,
3228 warnx("numeric device specification "
3229 "must be either bus:target, or "
3234 /* default to 0 if lun was not specified */
3238 new_cam_dev = cam_open_btl(bus, target, lun,
3243 if (cam_get_device(io_opts[i].dev_name, name,
3244 sizeof name, &unit) == -1) {
3245 warnx("%s", cam_errbuf);
3249 new_cam_dev = cam_open_spec_device(name, unit,
3253 if (new_cam_dev == NULL) {
3254 warnx("%s", cam_errbuf);
3259 devs[i] = camdd_probe_pass(new_cam_dev,
3260 /*io_opts*/ &io_opts[i],
3262 /*probe_retry_count*/ 3,
3263 /*probe_timeout*/ 5000,
3264 /*io_retry_count*/ retry_count,
3265 /*io_timeout*/ timeout);
3266 if (devs[i] == NULL) {
3267 warn("Unable to probe device %s%u",
3268 new_cam_dev->device_name,
3269 new_cam_dev->dev_unit_num);
3275 case CAMDD_DEV_FILE: {
3278 if (io_opts[i].dev_name[0] == '-') {
3279 if (io_opts[i].write_dev != 0)
3284 if (io_opts[i].write_dev != 0) {
3285 fd = open(io_opts[i].dev_name,
3286 O_RDWR | O_CREAT, S_IWUSR |S_IRUSR);
3288 fd = open(io_opts[i].dev_name,
3293 warn("error opening file %s",
3294 io_opts[i].dev_name);
3299 devs[i] = camdd_probe_file(fd, &io_opts[i],
3300 retry_count, timeout);
3301 if (devs[i] == NULL) {
3309 warnx("Unknown device type %d (%s)",
3310 io_opts[i].dev_type, io_opts[i].dev_name);
3313 break; /*NOTREACHED */
3316 devs[i]->write_dev = io_opts[i].write_dev;
3318 devs[i]->start_offset_bytes = io_opts[i].offset;
3321 devs[i]->sector_io_limit =
3322 (devs[i]->start_offset_bytes /
3323 devs[i]->sector_size) +
3324 (max_io / devs[i]->sector_size) - 1;
3327 devs[i]->next_io_pos_bytes = devs[i]->start_offset_bytes;
3328 devs[i]->next_completion_pos_bytes =devs[i]->start_offset_bytes;
3331 devs[0]->peer_dev = devs[1];
3332 devs[1]->peer_dev = devs[0];
3333 devs[0]->next_peer_pos_bytes = devs[0]->peer_dev->next_io_pos_bytes;
3334 devs[1]->next_peer_pos_bytes = devs[1]->peer_dev->next_io_pos_bytes;
3336 sem_init(&camdd_sem, /*pshared*/ 0, 0);
3338 signal(SIGINFO, camdd_sig_handler);
3339 signal(SIGINT, camdd_sig_handler);
3341 error = clock_gettime(CLOCK_MONOTONIC_PRECISE, &start_time);
3343 warn("Unable to get start time");
3347 for (i = 0; i < num_io_opts; i++) {
3348 error = pthread_create(&threads[i], NULL, camdd_worker,
3351 warnc(error, "pthread_create() failed");
3357 if ((sem_wait(&camdd_sem) == -1)
3358 || (need_exit != 0)) {
3361 for (i = 0; i < num_io_opts; i++) {
3362 EV_SET(&ke, (uintptr_t)&devs[i]->work_queue,
3363 EVFILT_USER, 0, NOTE_TRIGGER, 0, NULL);
3365 devs[i]->flags |= CAMDD_DEV_FLAG_EOF;
3367 error = kevent(devs[i]->kq, &ke, 1, NULL, 0,
3370 warn("%s: unable to wake up thread",
3375 } else if (need_status != 0) {
3376 camdd_print_status(devs[0], devs[1], &start_time);
3380 for (i = 0; i < num_io_opts; i++) {
3381 pthread_join(threads[i], NULL);
3384 camdd_print_status(devs[0], devs[1], &start_time);
3388 for (i = 0; i < num_io_opts; i++)
3389 camdd_free_dev(devs[i]);
3391 return (error + error_exit);
3398 "usage: camdd <-i|-o pass=pass0,bs=1M,offset=1M,depth=4>\n"
3399 " <-i|-o file=/tmp/file,bs=512K,offset=1M>\n"
3400 " <-i|-o file=/dev/da0,bs=512K,offset=1M>\n"
3401 " <-i|-o file=/dev/nsa0,bs=512K>\n"
3402 " [-C retry_count][-E][-m max_io_amt][-t timeout_secs][-v][-h]\n"
3403 "Option description\n"
3404 "-i <arg=val> Specify input device/file and parameters\n"
3405 "-o <arg=val> Specify output device/file and parameters\n"
3406 "Input and Output parameters\n"
3407 "pass=name Specify a pass(4) device like pass0 or /dev/pass0\n"
3408 "file=name Specify a file or device, /tmp/foo, /dev/da0, /dev/null\n"
3409 " or - for stdin/stdout\n"
3410 "bs=blocksize Specify blocksize in bytes, or using K, M, G, etc. suffix\n"
3411 "offset=len Specify starting offset in bytes or using K, M, G suffix\n"
3412 " NOTE: offset cannot be specified on tapes, pipes, stdin/out\n"
3413 "depth=N Specify a numeric queue depth. This only applies to pass(4)\n"
3414 "mcs=N Specify a minimum cmd size for pass(4) read/write commands\n"
3415 "Optional arguments\n"
3416 "-C retry_cnt Specify a retry count for pass(4) devices\n"
3417 "-E Enable CAM error recovery for pass(4) devices\n"
3418 "-m max_io Specify the maximum amount to be transferred in bytes or\n"
3419 " using K, G, M, etc. suffixes\n"
3420 "-t timeout Specify the I/O timeout to use with pass(4) devices\n"
3421 "-v Enable verbose error recovery\n"
3422 "-h Print this message\n");
3427 camdd_parse_io_opts(char *args, int is_write, struct camdd_io_opts *io_opts)
3429 char *tmpstr, *tmpstr2;
3430 char *orig_tmpstr = NULL;
3433 io_opts->write_dev = is_write;
3435 tmpstr = strdup(args);
3436 if (tmpstr == NULL) {
3437 warn("strdup failed");
3441 orig_tmpstr = tmpstr;
3442 while ((tmpstr2 = strsep(&tmpstr, ",")) != NULL) {
3446 * If the user creates an empty parameter by putting in two
3447 * commas, skip over it and look for the next field.
3449 if (*tmpstr2 == '\0')
3452 name = strsep(&tmpstr2, "=");
3453 if (*name == '\0') {
3454 warnx("Got empty I/O parameter name");
3458 value = strsep(&tmpstr2, "=");
3460 || (*value == '\0')) {
3461 warnx("Empty I/O parameter value for %s", name);
3465 if (strncasecmp(name, "file", 4) == 0) {
3466 io_opts->dev_type = CAMDD_DEV_FILE;
3467 io_opts->dev_name = strdup(value);
3468 if (io_opts->dev_name == NULL) {
3469 warn("Error allocating memory");
3473 } else if (strncasecmp(name, "pass", 4) == 0) {
3474 io_opts->dev_type = CAMDD_DEV_PASS;
3475 io_opts->dev_name = strdup(value);
3476 if (io_opts->dev_name == NULL) {
3477 warn("Error allocating memory");
3481 } else if ((strncasecmp(name, "bs", 2) == 0)
3482 || (strncasecmp(name, "blocksize", 9) == 0)) {
3483 retval = expand_number(value, &io_opts->blocksize);
3485 warn("expand_number(3) failed on %s=%s", name,
3490 } else if (strncasecmp(name, "depth", 5) == 0) {
3493 io_opts->queue_depth = strtoull(value, &endptr, 0);
3494 if (*endptr != '\0') {
3495 warnx("invalid queue depth %s", value);
3499 } else if (strncasecmp(name, "mcs", 3) == 0) {
3502 io_opts->min_cmd_size = strtol(value, &endptr, 0);
3503 if ((*endptr != '\0')
3504 || ((io_opts->min_cmd_size > 16)
3505 || (io_opts->min_cmd_size < 0))) {
3506 warnx("invalid minimum cmd size %s", value);
3510 } else if (strncasecmp(name, "offset", 6) == 0) {
3511 retval = expand_number(value, &io_opts->offset);
3513 warn("expand_number(3) failed on %s=%s", name,
3518 } else if (strncasecmp(name, "debug", 5) == 0) {
3521 io_opts->debug = strtoull(value, &endptr, 0);
3522 if (*endptr != '\0') {
3523 warnx("invalid debug level %s", value);
3528 warnx("Unrecognized parameter %s=%s", name, value);
3538 main(int argc, char **argv)
3541 camdd_argmask arglist = CAMDD_ARG_NONE;
3542 int timeout = 0, retry_count = 1;
3544 uint64_t max_io = 0;
3545 struct camdd_io_opts *opt_list = NULL;
3552 opt_list = calloc(2, sizeof(struct camdd_io_opts));
3553 if (opt_list == NULL) {
3554 warn("Unable to allocate option list");
3559 while ((c = getopt(argc, argv, "C:Ehi:m:o:t:v")) != -1){
3562 retry_count = strtol(optarg, NULL, 0);
3563 if (retry_count < 0)
3564 errx(1, "retry count %d is < 0",
3568 arglist |= CAMDD_ARG_ERR_RECOVER;
3573 && (opt_list[0].dev_type != CAMDD_DEV_NONE))
3575 && (opt_list[1].dev_type != CAMDD_DEV_NONE))) {
3576 errx(1, "Only one input and output path "
3579 error = camdd_parse_io_opts(optarg, (c == 'o') ? 1 : 0,
3580 (c == 'o') ? &opt_list[1] : &opt_list[0]);
3585 error = expand_number(optarg, &max_io);
3587 warn("invalid maximum I/O amount %s", optarg);
3593 timeout = strtol(optarg, NULL, 0);
3595 errx(1, "invalid timeout %d", timeout);
3596 /* Convert the timeout from seconds to ms */
3600 arglist |= CAMDD_ARG_VERBOSE;
3606 break; /*NOTREACHED*/
3610 if ((opt_list[0].dev_type == CAMDD_DEV_NONE)
3611 || (opt_list[1].dev_type == CAMDD_DEV_NONE))
3612 errx(1, "Must specify both -i and -o");
3615 * Set the timeout if the user hasn't specified one.
3618 timeout = CAMDD_PASS_RW_TIMEOUT;
3620 error = camdd_rw(opt_list, arglist, 2, max_io, retry_count, timeout);