2 * Copyright (c) 2004, 2005 Silicon Graphics International Corp.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * substantially similar to the "NO WARRANTY" disclaimer below
13 * ("Disclaimer") and any redistribution must be conditioned upon
14 * including a substantially similar Disclaimer requirement for further
15 * binary redistribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGES.
30 * $Id: //depot/users/kenm/FreeBSD-test2/sys/cam/ctl/ctl_frontend_internal.c#5 $
33 * CTL kernel internal frontend target driver. This allows kernel-level
34 * clients to send commands into CTL.
36 * This has elements of a FETD (e.g. it has to set tag numbers, initiator,
37 * port, target, and LUN) and elements of an initiator (LUN discovery and
38 * probing, error recovery, command initiation). Even though this has some
39 * initiator type elements, this is not intended to be a full fledged
40 * initiator layer. It is only intended to send a limited number of
41 * commands to a well known target layer.
43 * To be able to fulfill the role of a full initiator layer, it would need
44 * a whole lot more functionality.
46 * Author: Ken Merry <ken@FreeBSD.org>
50 #include <sys/cdefs.h>
51 __FBSDID("$FreeBSD$");
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/types.h>
57 #include <sys/malloc.h>
58 #include <sys/module.h>
60 #include <sys/mutex.h>
61 #include <sys/condvar.h>
62 #include <sys/queue.h>
64 #include <sys/sysctl.h>
65 #include <cam/scsi/scsi_all.h>
66 #include <cam/scsi/scsi_da.h>
67 #include <cam/ctl/ctl_io.h>
68 #include <cam/ctl/ctl.h>
69 #include <cam/ctl/ctl_frontend.h>
70 #include <cam/ctl/ctl_frontend_internal.h>
71 #include <cam/ctl/ctl_backend.h>
72 #include <cam/ctl/ctl_ioctl.h>
73 #include <cam/ctl/ctl_util.h>
74 #include <cam/ctl/ctl_ha.h>
75 #include <cam/ctl/ctl_private.h>
76 #include <cam/ctl/ctl_mem_pool.h>
77 #include <cam/ctl/ctl_debug.h>
78 #include <cam/ctl/ctl_scsi_all.h>
79 #include <cam/ctl/ctl_error.h>
83 * - overall metatask, different potential metatask types (e.g. forced
84 * shutdown, gentle shutdown)
85 * - forced shutdown metatask:
86 * - states: report luns, pending, done?
87 * - list of luns pending, with the relevant I/O for that lun attached.
88 * This would allow moving ahead on LUNs with no errors, and going
89 * into error recovery on LUNs with problems. Per-LUN states might
90 * include inquiry, stop/offline, done.
92 * Use LUN enable for LUN list instead of getting it manually? We'd still
93 * need inquiry data for each LUN.
95 * How to handle processor LUN w.r.t. found/stopped counts?
104 struct cfi_task_startstop {
110 /* XXX KDM add more fields here */
114 struct cfi_task_startstop startstop;
117 struct cfi_metatask {
118 cfi_tasktype tasktype;
119 cfi_mt_status status;
120 union cfi_taskinfo taskinfo;
121 struct ctl_mem_element *element;
123 STAILQ_ENTRY(cfi_metatask) links;
128 CFI_ERR_RETRY = 0x000,
129 CFI_ERR_FAIL = 0x001,
130 CFI_ERR_LUN_RESET = 0x002,
131 CFI_ERR_MASK = 0x0ff,
132 CFI_ERR_NO_DECREMENT = 0x100
142 CFI_LUN_READCAPACITY,
143 CFI_LUN_READCAPACITY_16,
148 struct ctl_id target_id;
150 struct scsi_inquiry_data inq_data;
153 int blocksize_powerof2;
154 uint32_t cur_tag_num;
156 struct ctl_mem_element *element;
157 struct cfi_softc *softc;
158 STAILQ_HEAD(, cfi_lun_io) io_list;
159 STAILQ_ENTRY(cfi_lun) links;
164 struct cfi_metatask *metatask;
165 cfi_error_policy policy;
166 void (*done_function)(union ctl_io *io);
167 union ctl_io *ctl_io;
168 struct cfi_lun_io *orig_lun_io;
169 STAILQ_ENTRY(cfi_lun_io) links;
178 struct ctl_frontend fe;
182 STAILQ_HEAD(, cfi_lun) lun_list;
183 STAILQ_HEAD(, cfi_metatask) metatask_list;
184 struct ctl_mem_pool lun_pool;
185 struct ctl_mem_pool metatask_pool;
188 MALLOC_DEFINE(M_CTL_CFI, "ctlcfi", "CTL CFI");
190 static struct cfi_softc fetd_internal_softc;
193 void cfi_shutdown(void) __unused;
194 static void cfi_online(void *arg);
195 static void cfi_offline(void *arg);
196 static int cfi_targ_enable(void *arg, struct ctl_id targ_id);
197 static int cfi_targ_disable(void *arg, struct ctl_id targ_id);
198 static int cfi_lun_enable(void *arg, struct ctl_id target_id, int lun_id);
199 static int cfi_lun_disable(void *arg, struct ctl_id target_id, int lun_id);
200 static void cfi_datamove(union ctl_io *io);
201 static cfi_error_action cfi_checkcond_parse(union ctl_io *io,
202 struct cfi_lun_io *lun_io);
203 static cfi_error_action cfi_error_parse(union ctl_io *io,
204 struct cfi_lun_io *lun_io);
205 static void cfi_init_io(union ctl_io *io, struct cfi_lun *lun,
206 struct cfi_metatask *metatask, cfi_error_policy policy,
207 int retries, struct cfi_lun_io *orig_lun_io,
208 void (*done_function)(union ctl_io *io));
209 static void cfi_done(union ctl_io *io);
210 static void cfi_lun_probe_done(union ctl_io *io);
211 static void cfi_lun_probe(struct cfi_lun *lun, int have_lock);
212 static void cfi_metatask_done(struct cfi_softc *softc,
213 struct cfi_metatask *metatask);
214 static void cfi_metatask_bbr_errorparse(struct cfi_metatask *metatask,
216 static void cfi_metatask_io_done(union ctl_io *io);
217 static void cfi_err_recovery_done(union ctl_io *io);
218 static void cfi_lun_io_done(union ctl_io *io);
220 static int cfi_module_event_handler(module_t, int /*modeventtype_t*/, void *);
222 static moduledata_t cfi_moduledata = {
224 cfi_module_event_handler,
228 DECLARE_MODULE(ctlcfi, cfi_moduledata, SI_SUB_CONFIGURE, SI_ORDER_FOURTH);
229 MODULE_VERSION(ctlcfi, 1);
230 MODULE_DEPEND(ctlcfi, ctl, 1, 1, 1);
235 struct cfi_softc *softc;
236 struct ctl_frontend *fe;
239 softc = &fetd_internal_softc;
245 if (sizeof(struct cfi_lun_io) > CTL_PORT_PRIV_SIZE) {
246 printf("%s: size of struct cfi_lun_io %zd > "
247 "CTL_PORT_PRIV_SIZE %d\n", __func__,
248 sizeof(struct cfi_lun_io),
251 memset(softc, 0, sizeof(*softc));
253 mtx_init(&softc->lock, "CTL frontend mutex", NULL, MTX_DEF);
254 softc->flags |= CTL_FLAG_MASTER_SHELF;
256 STAILQ_INIT(&softc->lun_list);
257 STAILQ_INIT(&softc->metatask_list);
258 sprintf(softc->fe_name, "CTL internal");
259 fe->port_type = CTL_PORT_INTERNAL;
260 fe->num_requested_ctl_io = 100;
261 fe->port_name = softc->fe_name;
262 fe->port_online = cfi_online;
263 fe->port_offline = cfi_offline;
264 fe->onoff_arg = softc;
265 fe->targ_enable = cfi_targ_enable;
266 fe->targ_disable = cfi_targ_disable;
267 fe->lun_enable = cfi_lun_enable;
268 fe->lun_disable = cfi_lun_disable;
269 fe->targ_lun_arg = softc;
270 fe->fe_datamove = cfi_datamove;
271 fe->fe_done = cfi_done;
272 fe->max_targets = 15;
273 fe->max_target_id = 15;
275 if (ctl_frontend_register(fe, (softc->flags & CTL_FLAG_MASTER_SHELF)) != 0)
277 printf("%s: internal frontend registration failed\n", __func__);
282 if (ctl_init_mem_pool(&softc->lun_pool,
283 sizeof(struct cfi_lun),
284 CTL_MEM_POOL_PERM_GROW, /*grow_inc*/ 3,
285 /* initial_pool_size */ CTL_MAX_LUNS) != 0) {
286 printf("%s: can't initialize LUN memory pool\n", __func__);
291 if (ctl_init_mem_pool(&softc->metatask_pool,
292 sizeof(struct cfi_metatask),
293 CTL_MEM_POOL_PERM_GROW, /*grow_inc*/ 3,
294 /*initial_pool_size*/ 10) != 0) {
295 printf("%s: can't initialize metatask memory pool\n", __func__);
307 ctl_shrink_mem_pool(&softc->metatask_pool);
310 ctl_shrink_mem_pool(&softc->lun_pool);
313 ctl_frontend_deregister(fe);
325 struct cfi_softc *softc;
327 softc = &fetd_internal_softc;
330 * XXX KDM need to clear out any I/O pending on each LUN.
332 if (ctl_frontend_deregister(&softc->fe) != 0)
333 printf("%s: ctl_frontend_deregister() failed\n", __func__);
335 if (ctl_shrink_mem_pool(&softc->lun_pool) != 0)
336 printf("%s: error shrinking LUN pool\n", __func__);
338 if (ctl_shrink_mem_pool(&softc->metatask_pool) != 0)
339 printf("%s: error shrinking LUN pool\n", __func__);
343 cfi_module_event_handler(module_t mod, int what, void *arg)
357 cfi_online(void *arg)
359 struct cfi_softc *softc;
362 softc = (struct cfi_softc *)arg;
364 softc->flags |= CFI_ONLINE;
367 * Go through and kick off the probe for each lun. Should we check
368 * the LUN flags here to determine whether or not to probe it?
370 mtx_lock(&softc->lock);
371 STAILQ_FOREACH(lun, &softc->lun_list, links)
372 cfi_lun_probe(lun, /*have_lock*/ 1);
373 mtx_unlock(&softc->lock);
377 cfi_offline(void *arg)
379 struct cfi_softc *softc;
381 softc = (struct cfi_softc *)arg;
383 softc->flags &= ~CFI_ONLINE;
387 cfi_targ_enable(void *arg, struct ctl_id targ_id)
393 cfi_targ_disable(void *arg, struct ctl_id targ_id)
399 cfi_lun_enable(void *arg, struct ctl_id target_id, int lun_id)
401 struct ctl_mem_element *element;
402 struct cfi_softc *softc;
406 softc = (struct cfi_softc *)arg;
409 mtx_lock(&softc->lock);
410 STAILQ_FOREACH(lun, &softc->lun_list, links) {
411 if ((lun->target_id.id == target_id.id)
412 && (lun->lun_id == lun_id)) {
417 mtx_unlock(&softc->lock);
420 * If we already have this target/LUN, there is no reason to add
421 * it to our lists again.
426 element = ctl_alloc_mem_element(&softc->lun_pool, /*can_wait*/ 0);
428 if (element == NULL) {
429 printf("%s: unable to allocate LUN structure\n", __func__);
433 lun = (struct cfi_lun *)element->bytes;
435 lun->element = element;
436 lun->target_id = target_id;
437 lun->lun_id = lun_id;
438 lun->cur_tag_num = 0;
439 lun->state = CFI_LUN_INQUIRY;
441 STAILQ_INIT(&lun->io_list);
443 mtx_lock(&softc->lock);
444 STAILQ_INSERT_TAIL(&softc->lun_list, lun, links);
445 mtx_unlock(&softc->lock);
447 cfi_lun_probe(lun, /*have_lock*/ 0);
453 cfi_lun_disable(void *arg, struct ctl_id target_id, int lun_id)
455 struct cfi_softc *softc;
459 softc = (struct cfi_softc *)arg;
464 * XXX KDM need to do an invalidate and then a free when any
465 * pending I/O has completed. Or do we? CTL won't free a LUN
466 * while any I/O is pending. So we won't get this notification
467 * unless any I/O we have pending on a LUN has completed.
469 mtx_lock(&softc->lock);
470 STAILQ_FOREACH(lun, &softc->lun_list, links) {
471 if ((lun->target_id.id == target_id.id)
472 && (lun->lun_id == lun_id)) {
478 STAILQ_REMOVE(&softc->lun_list, lun, cfi_lun, links);
480 mtx_unlock(&softc->lock);
483 printf("%s: can't find target %ju lun %d\n", __func__,
484 (uintmax_t)target_id.id, lun_id);
488 ctl_free_mem_element(lun->element);
494 cfi_datamove(union ctl_io *io)
496 struct ctl_sg_entry *ext_sglist, *kern_sglist;
497 struct ctl_sg_entry ext_entry, kern_entry;
498 int ext_sglen, ext_sg_entries, kern_sg_entries;
499 int ext_sg_start, ext_offset;
500 int len_to_copy, len_copied;
501 int kern_watermark, ext_watermark;
502 int ext_sglist_malloced;
503 struct ctl_scsiio *ctsio;
506 ext_sglist_malloced = 0;
511 CTL_DEBUG_PRINT(("%s\n", __func__));
516 * If this is the case, we're probably doing a BBR read and don't
517 * actually need to transfer the data. This will effectively
518 * bit-bucket the data.
520 if (ctsio->ext_data_ptr == NULL)
524 * To simplify things here, if we have a single buffer, stick it in
525 * a S/G entry and just make it a single entry S/G list.
527 if (ctsio->io_hdr.flags & CTL_FLAG_EDPTR_SGLIST) {
530 ext_sglen = ctsio->ext_sg_entries * sizeof(*ext_sglist);
532 ext_sglist = (struct ctl_sg_entry *)malloc(ext_sglen, M_CTL_CFI,
534 ext_sglist_malloced = 1;
535 if (memcpy(ext_sglist, ctsio->ext_data_ptr, ext_sglen) != 0) {
536 ctl_set_internal_failure(ctsio,
541 ext_sg_entries = ctsio->ext_sg_entries;
543 for (i = 0; i < ext_sg_entries; i++) {
544 if ((len_seen + ext_sglist[i].len) >=
545 ctsio->ext_data_filled) {
547 ext_offset = ctsio->ext_data_filled - len_seen;
550 len_seen += ext_sglist[i].len;
553 ext_sglist = &ext_entry;
554 ext_sglist->addr = ctsio->ext_data_ptr;
555 ext_sglist->len = ctsio->ext_data_len;
558 ext_offset = ctsio->ext_data_filled;
561 if (ctsio->kern_sg_entries > 0) {
562 kern_sglist = (struct ctl_sg_entry *)ctsio->kern_data_ptr;
563 kern_sg_entries = ctsio->kern_sg_entries;
565 kern_sglist = &kern_entry;
566 kern_sglist->addr = ctsio->kern_data_ptr;
567 kern_sglist->len = ctsio->kern_data_len;
573 ext_watermark = ext_offset;
575 for (i = ext_sg_start, j = 0;
576 i < ext_sg_entries && j < kern_sg_entries;) {
577 uint8_t *ext_ptr, *kern_ptr;
579 len_to_copy = ctl_min(ext_sglist[i].len - ext_watermark,
580 kern_sglist[j].len - kern_watermark);
582 ext_ptr = (uint8_t *)ext_sglist[i].addr;
583 ext_ptr = ext_ptr + ext_watermark;
584 if (io->io_hdr.flags & CTL_FLAG_BUS_ADDR) {
588 panic("need to implement bus address support");
590 kern_ptr = bus_to_virt(kern_sglist[j].addr);
593 kern_ptr = (uint8_t *)kern_sglist[j].addr;
594 kern_ptr = kern_ptr + kern_watermark;
596 kern_watermark += len_to_copy;
597 ext_watermark += len_to_copy;
599 if ((ctsio->io_hdr.flags & CTL_FLAG_DATA_MASK) ==
601 CTL_DEBUG_PRINT(("%s: copying %d bytes to user\n",
602 __func__, len_to_copy));
603 CTL_DEBUG_PRINT(("%s: from %p to %p\n", __func__,
605 memcpy(ext_ptr, kern_ptr, len_to_copy);
607 CTL_DEBUG_PRINT(("%s: copying %d bytes from user\n",
608 __func__, len_to_copy));
609 CTL_DEBUG_PRINT(("%s: from %p to %p\n", __func__,
611 memcpy(kern_ptr, ext_ptr, len_to_copy);
614 len_copied += len_to_copy;
616 if (ext_sglist[i].len == ext_watermark) {
621 if (kern_sglist[j].len == kern_watermark) {
627 ctsio->ext_data_filled += len_copied;
629 CTL_DEBUG_PRINT(("%s: ext_sg_entries: %d, kern_sg_entries: %d\n",
630 __func__, ext_sg_entries, kern_sg_entries));
631 CTL_DEBUG_PRINT(("%s: ext_data_len = %d, kern_data_len = %d\n",
632 __func__, ctsio->ext_data_len, ctsio->kern_data_len));
635 /* XXX KDM set residual?? */
638 if (ext_sglist_malloced != 0)
639 free(ext_sglist, M_CTL_CFI);
641 io->scsiio.be_move_done(io);
647 * For any sort of check condition, busy, etc., we just retry. We do not
648 * decrement the retry count for unit attention type errors. These are
649 * normal, and we want to save the retry count for "real" errors. Otherwise,
650 * we could end up with situations where a command will succeed in some
651 * situations and fail in others, depending on whether a unit attention is
652 * pending. Also, some of our error recovery actions, most notably the
653 * LUN reset action, will cause a unit attention.
655 * We can add more detail here later if necessary.
657 static cfi_error_action
658 cfi_checkcond_parse(union ctl_io *io, struct cfi_lun_io *lun_io)
660 cfi_error_action error_action;
661 int error_code, sense_key, asc, ascq;
664 * Default to retrying the command.
666 error_action = CFI_ERR_RETRY;
668 scsi_extract_sense_len(&io->scsiio.sense_data,
669 io->scsiio.sense_len,
676 switch (error_code) {
677 case SSD_DEFERRED_ERROR:
678 case SSD_DESC_DEFERRED_ERROR:
679 error_action |= CFI_ERR_NO_DECREMENT;
681 case SSD_CURRENT_ERROR:
682 case SSD_DESC_CURRENT_ERROR:
685 case SSD_KEY_UNIT_ATTENTION:
686 error_action |= CFI_ERR_NO_DECREMENT;
688 case SSD_KEY_HARDWARE_ERROR:
690 * This is our generic "something bad happened"
691 * error code. It often isn't recoverable.
693 if ((asc == 0x44) && (ascq == 0x00))
694 error_action = CFI_ERR_FAIL;
696 case SSD_KEY_NOT_READY:
698 * If the LUN is powered down, there likely isn't
699 * much point in retrying right now.
701 if ((asc == 0x04) && (ascq == 0x02))
702 error_action = CFI_ERR_FAIL;
704 * If the LUN is offline, there probably isn't much
705 * point in retrying, either.
707 if ((asc == 0x04) && (ascq == 0x03))
708 error_action = CFI_ERR_FAIL;
714 return (error_action);
717 static cfi_error_action
718 cfi_error_parse(union ctl_io *io, struct cfi_lun_io *lun_io)
720 cfi_error_action error_action;
722 error_action = CFI_ERR_RETRY;
724 switch (io->io_hdr.io_type) {
726 switch (io->io_hdr.status & CTL_STATUS_MASK) {
728 switch (io->scsiio.scsi_status) {
729 case SCSI_STATUS_RESERV_CONFLICT:
731 * For a reservation conflict, we'll usually
732 * want the hard error recovery policy, so
733 * we'll reset the LUN.
735 if (lun_io->policy == CFI_ERR_HARD)
742 case SCSI_STATUS_CHECK_COND:
744 error_action = cfi_checkcond_parse(io, lun_io);
749 error_action = CFI_ERR_RETRY;
755 * In theory task management commands shouldn't fail...
757 error_action = CFI_ERR_RETRY;
760 printf("%s: invalid ctl_io type %d\n", __func__,
762 panic("%s: invalid ctl_io type %d\n", __func__,
767 return (error_action);
771 cfi_init_io(union ctl_io *io, struct cfi_lun *lun,
772 struct cfi_metatask *metatask, cfi_error_policy policy, int retries,
773 struct cfi_lun_io *orig_lun_io,
774 void (*done_function)(union ctl_io *io))
776 struct cfi_lun_io *lun_io;
778 io->io_hdr.nexus.initid.id = 7;
779 io->io_hdr.nexus.targ_port = lun->softc->fe.targ_port;
780 io->io_hdr.nexus.targ_target.id = lun->target_id.id;
781 io->io_hdr.nexus.targ_lun = lun->lun_id;
782 io->io_hdr.retries = retries;
783 lun_io = (struct cfi_lun_io *)io->io_hdr.port_priv;
784 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr = lun_io;
786 lun_io->metatask = metatask;
788 lun_io->policy = policy;
789 lun_io->orig_lun_io = orig_lun_io;
790 lun_io->done_function = done_function;
792 * We only set the tag number for SCSI I/Os. For task management
793 * commands, the tag number is only really needed for aborts, so
794 * the caller can set it if necessary.
796 switch (io->io_hdr.io_type) {
798 io->scsiio.tag_num = lun->cur_tag_num++;
807 cfi_done(union ctl_io *io)
809 struct cfi_lun_io *lun_io;
810 struct cfi_softc *softc;
813 lun_io = (struct cfi_lun_io *)
814 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
820 * Very minimal retry logic. We basically retry if we got an error
821 * back, and the retry count is greater than 0. If we ever want
822 * more sophisticated initiator type behavior, the CAM error
823 * recovery code in ../common might be helpful.
825 if (((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS)
826 && (io->io_hdr.retries > 0)) {
827 ctl_io_status old_status;
828 cfi_error_action error_action;
830 error_action = cfi_error_parse(io, lun_io);
832 switch (error_action & CFI_ERR_MASK) {
835 break; /* NOTREACHED */
836 case CFI_ERR_LUN_RESET: {
837 union ctl_io *new_io;
838 struct cfi_lun_io *new_lun_io;
840 new_io = ctl_alloc_io(softc->fe.ctl_pool_ref);
841 if (new_io == NULL) {
842 printf("%s: unable to allocate ctl_io for "
843 "error recovery\n", __func__);
848 new_io->io_hdr.io_type = CTL_IO_TASK;
849 new_io->taskio.task_action = CTL_TASK_LUN_RESET;
854 /*policy*/ CFI_ERR_SOFT,
856 /*orig_lun_io*/lun_io,
857 /*done_function*/ cfi_err_recovery_done);
860 new_lun_io = (struct cfi_lun_io *)
861 new_io->io_hdr.port_priv;
863 mtx_lock(&lun->softc->lock);
864 STAILQ_INSERT_TAIL(&lun->io_list, new_lun_io, links);
865 mtx_unlock(&lun->softc->lock);
872 if ((error_action & CFI_ERR_NO_DECREMENT) == 0)
873 io->io_hdr.retries--;
877 old_status = io->io_hdr.status;
878 io->io_hdr.status = CTL_STATUS_NONE;
880 io->io_hdr.flags &= ~CTL_FLAG_ALREADY_DONE;
882 io->io_hdr.flags &= ~CTL_FLAG_ABORT;
883 io->io_hdr.flags &= ~CTL_FLAG_SENT_2OTHER_SC;
885 if (ctl_queue(io) != CTL_RETVAL_COMPLETE) {
886 printf("%s: error returned from ctl_queue()!\n",
888 io->io_hdr.status = old_status;
893 lun_io->done_function(io);
897 cfi_lun_probe_done(union ctl_io *io)
900 struct cfi_lun_io *lun_io;
902 lun_io = (struct cfi_lun_io *)
903 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
906 switch (lun->state) {
907 case CFI_LUN_INQUIRY: {
908 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
909 /* print out something here?? */
910 printf("%s: LUN %d probe failed because inquiry "
911 "failed\n", __func__, lun->lun_id);
912 ctl_io_error_print(io, NULL);
915 if (SID_TYPE(&lun->inq_data) != T_DIRECT) {
918 lun->state = CFI_LUN_READY;
919 ctl_scsi_path_string(io, path_str,
921 printf("%s", path_str);
922 scsi_print_inquiry(&lun->inq_data);
924 lun->state = CFI_LUN_READCAPACITY;
925 cfi_lun_probe(lun, /*have_lock*/ 0);
928 mtx_lock(&lun->softc->lock);
929 STAILQ_REMOVE(&lun->io_list, lun_io, cfi_lun_io, links);
930 mtx_unlock(&lun->softc->lock);
934 case CFI_LUN_READCAPACITY:
935 case CFI_LUN_READCAPACITY_16: {
942 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SUCCESS) {
943 printf("%s: LUN %d probe failed because READ CAPACITY "
944 "failed\n", __func__, lun->lun_id);
945 ctl_io_error_print(io, NULL);
948 if (lun->state == CFI_LUN_READCAPACITY) {
949 struct scsi_read_capacity_data *rdcap;
951 rdcap = (struct scsi_read_capacity_data *)
952 io->scsiio.ext_data_ptr;
954 maxlba = scsi_4btoul(rdcap->addr);
955 blocksize = scsi_4btoul(rdcap->length);
956 if (blocksize == 0) {
957 printf("%s: LUN %d has invalid "
958 "blocksize 0, probe aborted\n",
959 __func__, lun->lun_id);
960 } else if (maxlba == 0xffffffff) {
961 lun->state = CFI_LUN_READCAPACITY_16;
962 cfi_lun_probe(lun, /*have_lock*/ 0);
964 lun->state = CFI_LUN_READY;
966 struct scsi_read_capacity_data_long *rdcap_long;
969 scsi_read_capacity_data_long *)
970 io->scsiio.ext_data_ptr;
971 maxlba = scsi_8btou64(rdcap_long->addr);
972 blocksize = scsi_4btoul(rdcap_long->length);
974 if (blocksize == 0) {
975 printf("%s: LUN %d has invalid "
976 "blocksize 0, probe aborted\n",
977 __func__, lun->lun_id);
979 lun->state = CFI_LUN_READY;
983 if (lun->state == CFI_LUN_READY) {
986 lun->num_blocks = maxlba + 1;
987 lun->blocksize = blocksize;
990 * If this is true, the blocksize is a power of 2.
991 * We already checked for 0 above.
993 if (((blocksize - 1) & blocksize) == 0) {
996 for (i = 0; i < 32; i++) {
997 if ((blocksize & (1 << i)) != 0) {
998 lun->blocksize_powerof2 = i;
1003 ctl_scsi_path_string(io, path_str,sizeof(path_str));
1004 printf("%s", path_str);
1005 scsi_print_inquiry(&lun->inq_data);
1006 printf("%s %ju blocks, blocksize %d\n", path_str,
1007 (uintmax_t)maxlba + 1, blocksize);
1009 mtx_lock(&lun->softc->lock);
1010 STAILQ_REMOVE(&lun->io_list, lun_io, cfi_lun_io, links);
1011 mtx_unlock(&lun->softc->lock);
1012 free(io->scsiio.ext_data_ptr, M_CTL_CFI);
1018 mtx_lock(&lun->softc->lock);
1019 /* How did we get here?? */
1020 STAILQ_REMOVE(&lun->io_list, lun_io, cfi_lun_io, links);
1021 mtx_unlock(&lun->softc->lock);
1028 cfi_lun_probe(struct cfi_lun *lun, int have_lock)
1032 mtx_lock(&lun->softc->lock);
1033 if ((lun->softc->flags & CFI_ONLINE) == 0) {
1035 mtx_unlock(&lun->softc->lock);
1039 mtx_unlock(&lun->softc->lock);
1041 switch (lun->state) {
1042 case CFI_LUN_INQUIRY: {
1043 struct cfi_lun_io *lun_io;
1046 io = ctl_alloc_io(lun->softc->fe.ctl_pool_ref);
1048 printf("%s: unable to alloc ctl_io for target %ju "
1049 "lun %d probe\n", __func__,
1050 (uintmax_t)lun->target_id.id, lun->lun_id);
1053 ctl_scsi_inquiry(io,
1054 /*data_ptr*/(uint8_t *)&lun->inq_data,
1055 /*data_len*/ sizeof(lun->inq_data),
1058 /*tag_type*/ CTL_TAG_SIMPLE,
1064 /*policy*/ CFI_ERR_SOFT,
1066 /*orig_lun_io*/ NULL,
1068 cfi_lun_probe_done);
1070 lun_io = (struct cfi_lun_io *)io->io_hdr.port_priv;
1073 mtx_lock(&lun->softc->lock);
1074 STAILQ_INSERT_TAIL(&lun->io_list, lun_io, links);
1076 mtx_unlock(&lun->softc->lock);
1078 if (ctl_queue(io) != CTL_RETVAL_COMPLETE) {
1079 printf("%s: error returned from ctl_queue()!\n",
1081 STAILQ_REMOVE(&lun->io_list, lun_io,
1087 case CFI_LUN_READCAPACITY:
1088 case CFI_LUN_READCAPACITY_16: {
1089 struct cfi_lun_io *lun_io;
1093 io = ctl_alloc_io(lun->softc->fe.ctl_pool_ref);
1095 printf("%s: unable to alloc ctl_io for target %ju "
1096 "lun %d probe\n", __func__,
1097 (uintmax_t)lun->target_id.id, lun->lun_id);
1101 dataptr = malloc(sizeof(struct scsi_read_capacity_data_long),
1102 M_CTL_CFI, M_NOWAIT);
1103 if (dataptr == NULL) {
1104 printf("%s: unable to allocate SCSI read capacity "
1105 "buffer for target %ju lun %d\n", __func__,
1106 (uintmax_t)lun->target_id.id, lun->lun_id);
1109 if (lun->state == CFI_LUN_READCAPACITY) {
1110 ctl_scsi_read_capacity(io,
1111 /*data_ptr*/ dataptr,
1113 sizeof(struct scsi_read_capacity_data_long),
1117 /*tag_type*/ CTL_TAG_SIMPLE,
1120 ctl_scsi_read_capacity_16(io,
1121 /*data_ptr*/ dataptr,
1123 sizeof(struct scsi_read_capacity_data_long),
1127 /*tag_type*/ CTL_TAG_SIMPLE,
1133 /*policy*/ CFI_ERR_SOFT,
1135 /*orig_lun_io*/ NULL,
1136 /*done_function*/ cfi_lun_probe_done);
1138 lun_io = (struct cfi_lun_io *)io->io_hdr.port_priv;
1141 mtx_lock(&lun->softc->lock);
1142 STAILQ_INSERT_TAIL(&lun->io_list, lun_io, links);
1144 mtx_unlock(&lun->softc->lock);
1146 if (ctl_queue(io) != CTL_RETVAL_COMPLETE) {
1147 printf("%s: error returned from ctl_queue()!\n",
1149 STAILQ_REMOVE(&lun->io_list, lun_io,
1151 free(dataptr, M_CTL_CFI);
1158 /* Why were we called? */
1164 cfi_metatask_done(struct cfi_softc *softc, struct cfi_metatask *metatask)
1166 mtx_lock(&softc->lock);
1167 STAILQ_REMOVE(&softc->metatask_list, metatask, cfi_metatask, links);
1168 mtx_unlock(&softc->lock);
1171 * Return status to the caller. Caller allocated storage, and is
1172 * responsible for calling cfi_free_metatask to release it once
1173 * they've seen the status.
1175 metatask->callback(metatask->callback_arg, metatask);
1179 cfi_metatask_bbr_errorparse(struct cfi_metatask *metatask, union ctl_io *io)
1181 int error_code, sense_key, asc, ascq;
1183 if (metatask->tasktype != CFI_TASK_BBRREAD)
1186 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS) {
1187 metatask->status = CFI_MT_SUCCESS;
1188 metatask->taskinfo.bbrread.status = CFI_BBR_SUCCESS;
1192 if ((io->io_hdr.status & CTL_STATUS_MASK) != CTL_SCSI_ERROR) {
1193 metatask->status = CFI_MT_ERROR;
1194 metatask->taskinfo.bbrread.status = CFI_BBR_ERROR;
1198 metatask->taskinfo.bbrread.scsi_status = io->scsiio.scsi_status;
1199 memcpy(&metatask->taskinfo.bbrread.sense_data, &io->scsiio.sense_data,
1200 ctl_min(sizeof(metatask->taskinfo.bbrread.sense_data),
1201 sizeof(io->scsiio.sense_data)));
1203 if (io->scsiio.scsi_status == SCSI_STATUS_RESERV_CONFLICT) {
1204 metatask->status = CFI_MT_ERROR;
1205 metatask->taskinfo.bbrread.status = CFI_BBR_RESERV_CONFLICT;
1209 if (io->scsiio.scsi_status != SCSI_STATUS_CHECK_COND) {
1210 metatask->status = CFI_MT_ERROR;
1211 metatask->taskinfo.bbrread.status = CFI_BBR_SCSI_ERROR;
1215 scsi_extract_sense_len(&io->scsiio.sense_data,
1216 io->scsiio.sense_len,
1223 switch (error_code) {
1224 case SSD_DEFERRED_ERROR:
1225 case SSD_DESC_DEFERRED_ERROR:
1226 metatask->status = CFI_MT_ERROR;
1227 metatask->taskinfo.bbrread.status = CFI_BBR_SCSI_ERROR;
1229 case SSD_CURRENT_ERROR:
1230 case SSD_DESC_CURRENT_ERROR:
1232 struct scsi_sense_data *sense;
1234 sense = &io->scsiio.sense_data;
1236 if ((asc == 0x04) && (ascq == 0x02)) {
1237 metatask->status = CFI_MT_ERROR;
1238 metatask->taskinfo.bbrread.status = CFI_BBR_LUN_STOPPED;
1239 } else if ((asc == 0x04) && (ascq == 0x03)) {
1240 metatask->status = CFI_MT_ERROR;
1241 metatask->taskinfo.bbrread.status =
1242 CFI_BBR_LUN_OFFLINE_CTL;
1243 } else if ((asc == 0x44) && (ascq == 0x00)) {
1245 if (sense->sense_key_spec[0] & SSD_SCS_VALID) {
1246 uint16_t retry_count;
1248 retry_count = sense->sense_key_spec[1] << 8 |
1249 sense->sense_key_spec[2];
1250 if (((retry_count & 0xf000) == CSC_RAIDCORE)
1251 && ((retry_count & 0x0f00) == CSC_SHELF_SW)
1252 && ((retry_count & 0xff) ==
1253 RC_STS_DEVICE_OFFLINE)) {
1254 metatask->status = CFI_MT_ERROR;
1255 metatask->taskinfo.bbrread.status =
1256 CFI_BBR_LUN_OFFLINE_RC;
1258 metatask->status = CFI_MT_ERROR;
1259 metatask->taskinfo.bbrread.status =
1263 #endif /* NEEDTOPORT */
1264 metatask->status = CFI_MT_ERROR;
1265 metatask->taskinfo.bbrread.status =
1271 metatask->status = CFI_MT_ERROR;
1272 metatask->taskinfo.bbrread.status = CFI_BBR_SCSI_ERROR;
1280 cfi_metatask_io_done(union ctl_io *io)
1282 struct cfi_lun_io *lun_io;
1283 struct cfi_metatask *metatask;
1284 struct cfi_softc *softc;
1285 struct cfi_lun *lun;
1287 lun_io = (struct cfi_lun_io *)
1288 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1293 metatask = lun_io->metatask;
1295 switch (metatask->tasktype) {
1296 case CFI_TASK_STARTUP:
1297 case CFI_TASK_SHUTDOWN: {
1298 int failed, done, is_start;
1302 if (metatask->tasktype == CFI_TASK_STARTUP)
1307 mtx_lock(&softc->lock);
1308 if ((io->io_hdr.status & CTL_STATUS_MASK) == CTL_SUCCESS)
1309 metatask->taskinfo.startstop.luns_complete++;
1311 metatask->taskinfo.startstop.luns_failed++;
1314 if ((metatask->taskinfo.startstop.luns_complete +
1315 metatask->taskinfo.startstop.luns_failed) >=
1316 metatask->taskinfo.startstop.total_luns)
1319 mtx_unlock(&softc->lock);
1322 printf("%s: LUN %d %s request failed\n", __func__,
1323 lun_io->lun->lun_id, (is_start == 1) ? "start" :
1325 ctl_io_error_print(io, &lun_io->lun->inq_data);
1328 if (metatask->taskinfo.startstop.luns_failed > 0)
1329 metatask->status = CFI_MT_ERROR;
1331 metatask->status = CFI_MT_SUCCESS;
1332 cfi_metatask_done(softc, metatask);
1334 mtx_lock(&softc->lock);
1335 STAILQ_REMOVE(&lun->io_list, lun_io, cfi_lun_io, links);
1336 mtx_unlock(&softc->lock);
1341 case CFI_TASK_BBRREAD: {
1343 * Translate the SCSI error into an enumeration.
1345 cfi_metatask_bbr_errorparse(metatask, io);
1347 mtx_lock(&softc->lock);
1348 STAILQ_REMOVE(&lun->io_list, lun_io, cfi_lun_io, links);
1349 mtx_unlock(&softc->lock);
1353 cfi_metatask_done(softc, metatask);
1358 * This shouldn't happen.
1360 mtx_lock(&softc->lock);
1361 STAILQ_REMOVE(&lun->io_list, lun_io, cfi_lun_io, links);
1362 mtx_unlock(&softc->lock);
1370 cfi_err_recovery_done(union ctl_io *io)
1372 struct cfi_lun_io *lun_io, *orig_lun_io;
1373 struct cfi_lun *lun;
1374 union ctl_io *orig_io;
1376 lun_io = (struct cfi_lun_io *)io->io_hdr.port_priv;
1377 orig_lun_io = lun_io->orig_lun_io;
1378 orig_io = orig_lun_io->ctl_io;
1381 if (io->io_hdr.status != CTL_SUCCESS) {
1382 printf("%s: error recovery action failed. Original "
1383 "error:\n", __func__);
1385 ctl_io_error_print(orig_lun_io->ctl_io, &lun->inq_data);
1387 printf("%s: error from error recovery action:\n", __func__);
1389 ctl_io_error_print(io, &lun->inq_data);
1391 printf("%s: trying original command again...\n", __func__);
1394 mtx_lock(&lun->softc->lock);
1395 STAILQ_REMOVE(&lun->io_list, lun_io, cfi_lun_io, links);
1396 mtx_unlock(&lun->softc->lock);
1399 orig_io->io_hdr.retries--;
1400 orig_io->io_hdr.status = CTL_STATUS_NONE;
1402 if (ctl_queue(orig_io) != CTL_RETVAL_COMPLETE) {
1403 printf("%s: error returned from ctl_queue()!\n", __func__);
1404 STAILQ_REMOVE(&lun->io_list, orig_lun_io,
1406 ctl_free_io(orig_io);
1411 cfi_lun_io_done(union ctl_io *io)
1413 struct cfi_lun *lun;
1414 struct cfi_lun_io *lun_io;
1416 lun_io = (struct cfi_lun_io *)
1417 io->io_hdr.ctl_private[CTL_PRIV_FRONTEND].ptr;
1420 if (lun_io->metatask == NULL) {
1421 printf("%s: I/O has no metatask pointer, discarding\n",
1423 STAILQ_REMOVE(&lun->io_list, lun_io, cfi_lun_io, links);
1427 cfi_metatask_io_done(io);
1431 cfi_action(struct cfi_metatask *metatask)
1433 struct cfi_softc *softc;
1435 softc = &fetd_internal_softc;
1437 mtx_lock(&softc->lock);
1439 STAILQ_INSERT_TAIL(&softc->metatask_list, metatask, links);
1441 if ((softc->flags & CFI_ONLINE) == 0) {
1442 mtx_unlock(&softc->lock);
1443 metatask->status = CFI_MT_PORT_OFFLINE;
1444 cfi_metatask_done(softc, metatask);
1447 mtx_unlock(&softc->lock);
1449 switch (metatask->tasktype) {
1450 case CFI_TASK_STARTUP:
1451 case CFI_TASK_SHUTDOWN: {
1453 int da_luns, ios_allocated, do_start;
1454 struct cfi_lun *lun;
1455 STAILQ_HEAD(, ctl_io_hdr) tmp_io_list;
1459 STAILQ_INIT(&tmp_io_list);
1461 if (metatask->tasktype == CFI_TASK_STARTUP)
1466 mtx_lock(&softc->lock);
1467 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1468 if (lun->state != CFI_LUN_READY)
1471 if (SID_TYPE(&lun->inq_data) != T_DIRECT)
1474 io = ctl_alloc_io(softc->fe.ctl_pool_ref);
1477 STAILQ_INSERT_TAIL(&tmp_io_list, &io->io_hdr,
1482 if (ios_allocated < da_luns) {
1483 printf("%s: error allocating ctl_io for %s\n",
1484 __func__, (do_start == 1) ? "startup" :
1486 da_luns = ios_allocated;
1489 metatask->taskinfo.startstop.total_luns = da_luns;
1491 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1492 struct cfi_lun_io *lun_io;
1494 if (lun->state != CFI_LUN_READY)
1497 if (SID_TYPE(&lun->inq_data) != T_DIRECT)
1500 io = (union ctl_io *)STAILQ_FIRST(&tmp_io_list);
1504 STAILQ_REMOVE(&tmp_io_list, &io->io_hdr, ctl_io_hdr,
1507 ctl_scsi_start_stop(io,
1511 /*power_conditions*/
1514 /*ctl_tag_type*/ CTL_TAG_ORDERED,
1519 /*metatask*/ metatask,
1520 /*policy*/ CFI_ERR_HARD,
1522 /*orig_lun_io*/ NULL,
1523 /*done_function*/ cfi_lun_io_done);
1525 lun_io = (struct cfi_lun_io *) io->io_hdr.port_priv;
1527 STAILQ_INSERT_TAIL(&lun->io_list, lun_io, links);
1529 if (ctl_queue(io) != CTL_RETVAL_COMPLETE) {
1530 printf("%s: error returned from ctl_queue()!\n",
1532 STAILQ_REMOVE(&lun->io_list, lun_io,
1535 metatask->taskinfo.startstop.total_luns--;
1539 if (STAILQ_FIRST(&tmp_io_list) != NULL) {
1540 printf("%s: error: tmp_io_list != NULL\n", __func__);
1541 for (io = (union ctl_io *)STAILQ_FIRST(&tmp_io_list);
1543 io = (union ctl_io *)STAILQ_FIRST(&tmp_io_list)) {
1544 STAILQ_REMOVE(&tmp_io_list, &io->io_hdr,
1549 mtx_unlock(&softc->lock);
1553 case CFI_TASK_BBRREAD: {
1555 struct cfi_lun *lun;
1556 struct cfi_lun_io *lun_io;
1557 cfi_bbrread_status status;
1559 uint32_t num_blocks;
1561 status = CFI_BBR_SUCCESS;
1563 req_lun_num = metatask->taskinfo.bbrread.lun_num;
1565 mtx_lock(&softc->lock);
1566 STAILQ_FOREACH(lun, &softc->lun_list, links) {
1567 if (lun->lun_id != req_lun_num)
1569 if (lun->state != CFI_LUN_READY) {
1570 status = CFI_BBR_LUN_UNCONFIG;
1577 status = CFI_BBR_NO_LUN;
1579 if (status != CFI_BBR_SUCCESS) {
1580 metatask->status = CFI_MT_ERROR;
1581 metatask->taskinfo.bbrread.status = status;
1582 mtx_unlock(&softc->lock);
1583 cfi_metatask_done(softc, metatask);
1588 * Convert the number of bytes given into blocks and check
1589 * that the number of bytes is a multiple of the blocksize.
1590 * CTL will verify that the LBA is okay.
1592 if (lun->blocksize_powerof2 != 0) {
1593 if ((metatask->taskinfo.bbrread.len &
1594 (lun->blocksize - 1)) != 0) {
1595 metatask->status = CFI_MT_ERROR;
1596 metatask->taskinfo.bbrread.status =
1598 cfi_metatask_done(softc, metatask);
1602 num_blocks = metatask->taskinfo.bbrread.len >>
1603 lun->blocksize_powerof2;
1606 * XXX KDM this could result in floating point
1607 * division, which isn't supported in the kernel on
1610 if ((metatask->taskinfo.bbrread.len %
1611 lun->blocksize) != 0) {
1612 metatask->status = CFI_MT_ERROR;
1613 metatask->taskinfo.bbrread.status =
1615 cfi_metatask_done(softc, metatask);
1620 * XXX KDM this could result in floating point
1621 * division in some cases.
1623 num_blocks = metatask->taskinfo.bbrread.len /
1628 io = ctl_alloc_io(softc->fe.ctl_pool_ref);
1630 metatask->status = CFI_MT_ERROR;
1631 metatask->taskinfo.bbrread.status = CFI_BBR_NO_MEM;
1632 mtx_unlock(&softc->lock);
1633 cfi_metatask_done(softc, metatask);
1638 * XXX KDM need to do a read capacity to get the blocksize
1641 ctl_scsi_read_write(io,
1643 /*data_len*/ metatask->taskinfo.bbrread.len,
1646 /*minimum_cdb_size*/ 0,
1647 /*lba*/ metatask->taskinfo.bbrread.lba,
1648 /*num_blocks*/ num_blocks,
1649 /*tag_type*/ CTL_TAG_SIMPLE,
1654 /*metatask*/ metatask,
1655 /*policy*/ CFI_ERR_SOFT,
1657 /*orig_lun_io*/ NULL,
1658 /*done_function*/ cfi_lun_io_done);
1660 lun_io = (struct cfi_lun_io *)io->io_hdr.port_priv;
1662 STAILQ_INSERT_TAIL(&lun->io_list, lun_io, links);
1664 if (ctl_queue(io) != CTL_RETVAL_COMPLETE) {
1665 printf("%s: error returned from ctl_queue()!\n",
1667 STAILQ_REMOVE(&lun->io_list, lun_io, cfi_lun_io, links);
1669 metatask->status = CFI_MT_ERROR;
1670 metatask->taskinfo.bbrread.status = CFI_BBR_ERROR;
1671 mtx_unlock(&softc->lock);
1672 cfi_metatask_done(softc, metatask);
1676 mtx_unlock(&softc->lock);
1680 panic("invalid metatask type %d", metatask->tasktype);
1681 break; /* NOTREACHED */
1687 cfi_shutdown_shelf(cfi_cb_t callback, void *callback_arg)
1689 struct ctl_mem_element *element;
1690 struct cfi_softc *softc;
1691 struct cfi_metatask *metatask;
1693 softc = &fetd_internal_softc;
1695 element = ctl_alloc_mem_element(&softc->metatask_pool, /*can_wait*/ 0);
1696 if (element == NULL) {
1697 callback(callback_arg,
1698 /*status*/ CFI_MT_ERROR,
1700 /*sluns_complete*/ 0,
1701 /*sluns_failed*/ 0);
1705 metatask = (struct cfi_metatask *)element->bytes;
1707 memset(metatask, 0, sizeof(*metatask));
1708 metatask->tasktype = CFI_TASK_SHUTDOWN;
1709 metatask->status = CFI_MT_NONE;
1710 metatask->taskinfo.startstop.callback = callback;
1711 metatask->taskinfo.startstop.callback_arg = callback_arg;
1712 metatask->element = element;
1714 cfi_action(softc, metatask);
1717 * - send a report luns to lun 0, get LUN list.
1718 * - send an inquiry to each lun
1719 * - send a stop/offline to each direct access LUN
1720 * - if we get a reservation conflict, reset the LUN and then
1721 * retry sending the stop/offline
1722 * - return status back to the caller
1727 cfi_start_shelf(cfi_cb_t callback, void *callback_arg)
1729 struct ctl_mem_element *element;
1730 struct cfi_softc *softc;
1731 struct cfi_metatask *metatask;
1733 softc = &fetd_internal_softc;
1735 element = ctl_alloc_mem_element(&softc->metatask_pool, /*can_wait*/ 0);
1736 if (element == NULL) {
1737 callback(callback_arg,
1738 /*status*/ CFI_MT_ERROR,
1740 /*sluns_complete*/ 0,
1741 /*sluns_failed*/ 0);
1745 metatask = (struct cfi_metatask *)element->bytes;
1747 memset(metatask, 0, sizeof(*metatask));
1748 metatask->tasktype = CFI_TASK_STARTUP;
1749 metatask->status = CFI_MT_NONE;
1750 metatask->taskinfo.startstop.callback = callback;
1751 metatask->taskinfo.startstop.callback_arg = callback_arg;
1752 metatask->element = element;
1754 cfi_action(softc, metatask);
1757 * - send a report luns to lun 0, get LUN list.
1758 * - send an inquiry to each lun
1759 * - send a stop/offline to each direct access LUN
1760 * - if we get a reservation conflict, reset the LUN and then
1761 * retry sending the stop/offline
1762 * - return status back to the caller
1768 struct cfi_metatask *
1769 cfi_alloc_metatask(int can_wait)
1771 struct ctl_mem_element *element;
1772 struct cfi_metatask *metatask;
1773 struct cfi_softc *softc;
1775 softc = &fetd_internal_softc;
1777 element = ctl_alloc_mem_element(&softc->metatask_pool, can_wait);
1778 if (element == NULL)
1781 metatask = (struct cfi_metatask *)element->bytes;
1782 memset(metatask, 0, sizeof(*metatask));
1783 metatask->status = CFI_MT_NONE;
1784 metatask->element = element;
1790 cfi_free_metatask(struct cfi_metatask *metatask)
1792 ctl_free_mem_element(metatask->element);