2 * Copyright (c) 2009 Yahoo! Inc.
3 * Copyright (c) 2011-2015 LSI Corp.
4 * Copyright (c) 2013-2015 Avago Technologies
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Avago Technologies (LSI) MPT-Fusion Host Adapter FreeBSD
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 /* Communications core for Avago Technologies (LSI) MPT2 */
38 /* TODO Move headers to mpsvar */
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/kernel.h>
43 #include <sys/selinfo.h>
44 #include <sys/module.h>
48 #include <sys/malloc.h>
50 #include <sys/sysctl.h>
51 #include <sys/endian.h>
52 #include <sys/queue.h>
53 #include <sys/kthread.h>
54 #include <sys/taskqueue.h>
57 #include <machine/bus.h>
58 #include <machine/resource.h>
61 #include <machine/stdarg.h>
64 #include <cam/cam_ccb.h>
65 #include <cam/cam_xpt.h>
66 #include <cam/cam_debug.h>
67 #include <cam/cam_sim.h>
68 #include <cam/cam_xpt_sim.h>
69 #include <cam/cam_xpt_periph.h>
70 #include <cam/cam_periph.h>
71 #include <cam/scsi/scsi_all.h>
72 #include <cam/scsi/scsi_message.h>
73 #if __FreeBSD_version >= 900026
74 #include <cam/scsi/smp_all.h>
77 #include <dev/mps/mpi/mpi2_type.h>
78 #include <dev/mps/mpi/mpi2.h>
79 #include <dev/mps/mpi/mpi2_ioc.h>
80 #include <dev/mps/mpi/mpi2_sas.h>
81 #include <dev/mps/mpi/mpi2_cnfg.h>
82 #include <dev/mps/mpi/mpi2_init.h>
83 #include <dev/mps/mpi/mpi2_tool.h>
84 #include <dev/mps/mps_ioctl.h>
85 #include <dev/mps/mpsvar.h>
86 #include <dev/mps/mps_table.h>
87 #include <dev/mps/mps_sas.h>
89 #define MPSSAS_DISCOVERY_TIMEOUT 20
90 #define MPSSAS_MAX_DISCOVERY_TIMEOUTS 10 /* 200 seconds */
93 * static array to check SCSI OpCode for EEDP protection bits
95 #define PRO_R MPI2_SCSIIO_EEDPFLAGS_CHECK_REMOVE_OP
96 #define PRO_W MPI2_SCSIIO_EEDPFLAGS_INSERT_OP
97 #define PRO_V MPI2_SCSIIO_EEDPFLAGS_INSERT_OP
98 static uint8_t op_code_prot[256] = {
99 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
100 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
101 0, 0, 0, 0, 0, 0, 0, 0, PRO_R, 0, PRO_W, 0, 0, 0, PRO_W, PRO_V,
102 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
103 0, PRO_W, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
104 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
105 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
106 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
107 0, 0, 0, 0, 0, 0, 0, 0, PRO_R, 0, PRO_W, 0, 0, 0, PRO_W, PRO_V,
108 0, 0, 0, PRO_W, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
109 0, 0, 0, 0, 0, 0, 0, 0, PRO_R, 0, PRO_W, 0, 0, 0, PRO_W, PRO_V,
110 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
111 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
112 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
113 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
114 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
117 MALLOC_DEFINE(M_MPSSAS, "MPSSAS", "MPS SAS memory");
119 static void mpssas_remove_device(struct mps_softc *, struct mps_command *);
120 static void mpssas_remove_complete(struct mps_softc *, struct mps_command *);
121 static void mpssas_action(struct cam_sim *sim, union ccb *ccb);
122 static void mpssas_poll(struct cam_sim *sim);
123 static int mpssas_send_abort(struct mps_softc *sc, struct mps_command *tm,
124 struct mps_command *cm);
125 static void mpssas_scsiio_timeout(void *data);
126 static void mpssas_abort_complete(struct mps_softc *sc, struct mps_command *cm);
127 static void mpssas_direct_drive_io(struct mpssas_softc *sassc,
128 struct mps_command *cm, union ccb *ccb);
129 static void mpssas_action_scsiio(struct mpssas_softc *, union ccb *);
130 static void mpssas_scsiio_complete(struct mps_softc *, struct mps_command *);
131 static void mpssas_action_resetdev(struct mpssas_softc *, union ccb *);
132 #if __FreeBSD_version >= 900026
133 static void mpssas_smpio_complete(struct mps_softc *sc, struct mps_command *cm);
134 static void mpssas_send_smpcmd(struct mpssas_softc *sassc, union ccb *ccb,
136 static void mpssas_action_smpio(struct mpssas_softc *sassc, union ccb *ccb);
137 #endif //FreeBSD_version >= 900026
138 static void mpssas_resetdev_complete(struct mps_softc *, struct mps_command *);
139 static void mpssas_async(void *callback_arg, uint32_t code,
140 struct cam_path *path, void *arg);
141 #if (__FreeBSD_version < 901503) || \
142 ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000006))
143 static void mpssas_check_eedp(struct mps_softc *sc, struct cam_path *path,
144 struct ccb_getdev *cgd);
145 static void mpssas_read_cap_done(struct cam_periph *periph, union ccb *done_ccb);
147 static int mpssas_send_portenable(struct mps_softc *sc);
148 static void mpssas_portenable_complete(struct mps_softc *sc,
149 struct mps_command *cm);
151 struct mpssas_target *
152 mpssas_find_target_by_handle(struct mpssas_softc *sassc, int start, uint16_t handle)
154 struct mpssas_target *target;
157 for (i = start; i < sassc->maxtargets; i++) {
158 target = &sassc->targets[i];
159 if (target->handle == handle)
166 /* we need to freeze the simq during attach and diag reset, to avoid failing
167 * commands before device handles have been found by discovery. Since
168 * discovery involves reading config pages and possibly sending commands,
169 * discovery actions may continue even after we receive the end of discovery
170 * event, so refcount discovery actions instead of assuming we can unfreeze
171 * the simq when we get the event.
174 mpssas_startup_increment(struct mpssas_softc *sassc)
176 MPS_FUNCTRACE(sassc->sc);
178 if ((sassc->flags & MPSSAS_IN_STARTUP) != 0) {
179 if (sassc->startup_refcount++ == 0) {
180 /* just starting, freeze the simq */
181 mps_dprint(sassc->sc, MPS_INIT,
182 "%s freezing simq\n", __func__);
183 #if __FreeBSD_version >= 1000039
186 xpt_freeze_simq(sassc->sim, 1);
188 mps_dprint(sassc->sc, MPS_INIT, "%s refcount %u\n", __func__,
189 sassc->startup_refcount);
194 mpssas_release_simq_reinit(struct mpssas_softc *sassc)
196 if (sassc->flags & MPSSAS_QUEUE_FROZEN) {
197 sassc->flags &= ~MPSSAS_QUEUE_FROZEN;
198 xpt_release_simq(sassc->sim, 1);
199 mps_dprint(sassc->sc, MPS_INFO, "Unfreezing SIM queue\n");
204 mpssas_startup_decrement(struct mpssas_softc *sassc)
206 MPS_FUNCTRACE(sassc->sc);
208 if ((sassc->flags & MPSSAS_IN_STARTUP) != 0) {
209 if (--sassc->startup_refcount == 0) {
210 /* finished all discovery-related actions, release
211 * the simq and rescan for the latest topology.
213 mps_dprint(sassc->sc, MPS_INIT,
214 "%s releasing simq\n", __func__);
215 sassc->flags &= ~MPSSAS_IN_STARTUP;
216 xpt_release_simq(sassc->sim, 1);
217 #if __FreeBSD_version >= 1000039
220 mpssas_rescan_target(sassc->sc, NULL);
223 mps_dprint(sassc->sc, MPS_INIT, "%s refcount %u\n", __func__,
224 sassc->startup_refcount);
228 /* The firmware requires us to stop sending commands when we're doing task
229 * management, so refcount the TMs and keep the simq frozen when any are in
233 mpssas_alloc_tm(struct mps_softc *sc)
235 struct mps_command *tm;
237 tm = mps_alloc_high_priority_command(sc);
242 mpssas_free_tm(struct mps_softc *sc, struct mps_command *tm)
248 * For TM's the devq is frozen for the device. Unfreeze it here and
249 * free the resources used for freezing the devq. Must clear the
250 * INRESET flag as well or scsi I/O will not work.
252 if (tm->cm_targ != NULL) {
253 tm->cm_targ->flags &= ~MPSSAS_TARGET_INRESET;
256 mps_dprint(sc, MPS_INFO, "Unfreezing devq for target ID %d\n",
258 xpt_release_devq(tm->cm_ccb->ccb_h.path, 1, TRUE);
259 xpt_free_path(tm->cm_ccb->ccb_h.path);
260 xpt_free_ccb(tm->cm_ccb);
263 mps_free_high_priority_command(sc, tm);
267 mpssas_rescan_target(struct mps_softc *sc, struct mpssas_target *targ)
269 struct mpssas_softc *sassc = sc->sassc;
271 target_id_t targetid;
275 pathid = cam_sim_path(sassc->sim);
277 targetid = CAM_TARGET_WILDCARD;
279 targetid = targ - sassc->targets;
282 * Allocate a CCB and schedule a rescan.
284 ccb = xpt_alloc_ccb_nowait();
286 mps_dprint(sc, MPS_ERROR, "unable to alloc CCB for rescan\n");
290 if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid,
291 targetid, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
292 mps_dprint(sc, MPS_ERROR, "unable to create path for rescan\n");
297 if (targetid == CAM_TARGET_WILDCARD)
298 ccb->ccb_h.func_code = XPT_SCAN_BUS;
300 ccb->ccb_h.func_code = XPT_SCAN_TGT;
302 mps_dprint(sc, MPS_TRACE, "%s targetid %u\n", __func__, targetid);
307 mpssas_log_command(struct mps_command *cm, u_int level, const char *fmt, ...)
317 /* No need to be in here if debugging isn't enabled */
318 if ((cm->cm_sc->mps_debug & level) == 0)
321 sbuf_new(&sb, str, sizeof(str), 0);
325 if (cm->cm_ccb != NULL) {
326 xpt_path_string(cm->cm_ccb->csio.ccb_h.path, path_str,
328 sbuf_cat(&sb, path_str);
329 if (cm->cm_ccb->ccb_h.func_code == XPT_SCSI_IO) {
330 scsi_command_string(&cm->cm_ccb->csio, &sb);
331 sbuf_printf(&sb, "length %d ",
332 cm->cm_ccb->csio.dxfer_len);
336 sbuf_printf(&sb, "(noperiph:%s%d:%u:%u:%u): ",
337 cam_sim_name(cm->cm_sc->sassc->sim),
338 cam_sim_unit(cm->cm_sc->sassc->sim),
339 cam_sim_bus(cm->cm_sc->sassc->sim),
340 cm->cm_targ ? cm->cm_targ->tid : 0xFFFFFFFF,
344 sbuf_printf(&sb, "SMID %u ", cm->cm_desc.Default.SMID);
345 sbuf_vprintf(&sb, fmt, ap);
347 mps_dprint_field(cm->cm_sc, level, "%s", sbuf_data(&sb));
354 mpssas_remove_volume(struct mps_softc *sc, struct mps_command *tm)
356 MPI2_SCSI_TASK_MANAGE_REPLY *reply;
357 struct mpssas_target *targ;
362 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
363 handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
367 /* XXX retry the remove after the diag reset completes? */
368 mps_dprint(sc, MPS_FAULT,
369 "%s NULL reply resetting device 0x%04x\n", __func__,
371 mpssas_free_tm(sc, tm);
375 if (reply->IOCStatus != MPI2_IOCSTATUS_SUCCESS) {
376 mps_dprint(sc, MPS_FAULT,
377 "IOCStatus = 0x%x while resetting device 0x%x\n",
378 reply->IOCStatus, handle);
379 mpssas_free_tm(sc, tm);
383 mps_dprint(sc, MPS_XINFO,
384 "Reset aborted %u commands\n", reply->TerminationCount);
385 mps_free_reply(sc, tm->cm_reply_data);
386 tm->cm_reply = NULL; /* Ensures the reply won't get re-freed */
388 mps_dprint(sc, MPS_XINFO,
389 "clearing target %u handle 0x%04x\n", targ->tid, handle);
392 * Don't clear target if remove fails because things will get confusing.
393 * Leave the devname and sasaddr intact so that we know to avoid reusing
394 * this target id if possible, and so we can assign the same target id
395 * to this device if it comes back in the future.
397 if (reply->IOCStatus == MPI2_IOCSTATUS_SUCCESS) {
400 targ->encl_handle = 0x0;
401 targ->encl_slot = 0x0;
402 targ->exp_dev_handle = 0x0;
404 targ->linkrate = 0x0;
409 mpssas_free_tm(sc, tm);
414 * No Need to call "MPI2_SAS_OP_REMOVE_DEVICE" For Volume removal.
415 * Otherwise Volume Delete is same as Bare Drive Removal.
418 mpssas_prepare_volume_remove(struct mpssas_softc *sassc, uint16_t handle)
420 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
421 struct mps_softc *sc;
422 struct mps_command *cm;
423 struct mpssas_target *targ = NULL;
425 MPS_FUNCTRACE(sassc->sc);
430 * If this is a WD controller, determine if the disk should be exposed
431 * to the OS or not. If disk should be exposed, return from this
432 * function without doing anything.
434 if (sc->WD_available && (sc->WD_hide_expose ==
435 MPS_WD_EXPOSE_ALWAYS)) {
440 targ = mpssas_find_target_by_handle(sassc, 0, handle);
442 /* FIXME: what is the action? */
443 /* We don't know about this device? */
444 mps_dprint(sc, MPS_ERROR,
445 "%s %d : invalid handle 0x%x \n", __func__,__LINE__, handle);
449 targ->flags |= MPSSAS_TARGET_INREMOVAL;
451 cm = mpssas_alloc_tm(sc);
453 mps_dprint(sc, MPS_ERROR,
454 "%s: command alloc failure\n", __func__);
458 mpssas_rescan_target(sc, targ);
460 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req;
461 req->DevHandle = targ->handle;
462 req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
463 req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
465 /* SAS Hard Link Reset / SATA Link Reset */
466 req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
470 cm->cm_desc.HighPriority.RequestFlags =
471 MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
472 cm->cm_complete = mpssas_remove_volume;
473 cm->cm_complete_data = (void *)(uintptr_t)handle;
475 mps_dprint(sc, MPS_INFO, "%s: Sending reset for target ID %d\n",
476 __func__, targ->tid);
477 mpssas_prepare_for_tm(sc, cm, targ, CAM_LUN_WILDCARD);
479 mps_map_command(sc, cm);
483 * The MPT2 firmware performs debounce on the link to avoid transient link
484 * errors and false removals. When it does decide that link has been lost
485 * and a device need to go away, it expects that the host will perform a
486 * target reset and then an op remove. The reset has the side-effect of
487 * aborting any outstanding requests for the device, which is required for
488 * the op-remove to succeed. It's not clear if the host should check for
489 * the device coming back alive after the reset.
492 mpssas_prepare_remove(struct mpssas_softc *sassc, uint16_t handle)
494 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
495 struct mps_softc *sc;
496 struct mps_command *cm;
497 struct mpssas_target *targ = NULL;
499 MPS_FUNCTRACE(sassc->sc);
503 targ = mpssas_find_target_by_handle(sassc, 0, handle);
505 /* FIXME: what is the action? */
506 /* We don't know about this device? */
507 mps_dprint(sc, MPS_ERROR,
508 "%s : invalid handle 0x%x \n", __func__, handle);
512 targ->flags |= MPSSAS_TARGET_INREMOVAL;
514 cm = mpssas_alloc_tm(sc);
516 mps_dprint(sc, MPS_ERROR,
517 "%s: command alloc failure\n", __func__);
521 mpssas_rescan_target(sc, targ);
523 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req;
524 memset(req, 0, sizeof(*req));
525 req->DevHandle = htole16(targ->handle);
526 req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
527 req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
529 /* SAS Hard Link Reset / SATA Link Reset */
530 req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
534 cm->cm_desc.HighPriority.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
535 cm->cm_complete = mpssas_remove_device;
536 cm->cm_complete_data = (void *)(uintptr_t)handle;
538 mps_dprint(sc, MPS_INFO, "%s: Sending reset for target ID %d\n",
539 __func__, targ->tid);
540 mpssas_prepare_for_tm(sc, cm, targ, CAM_LUN_WILDCARD);
542 mps_map_command(sc, cm);
546 mpssas_remove_device(struct mps_softc *sc, struct mps_command *tm)
548 MPI2_SCSI_TASK_MANAGE_REPLY *reply;
549 MPI2_SAS_IOUNIT_CONTROL_REQUEST *req;
550 struct mpssas_target *targ;
551 struct mps_command *next_cm;
556 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
557 handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
561 * Currently there should be no way we can hit this case. It only
562 * happens when we have a failure to allocate chain frames, and
563 * task management commands don't have S/G lists.
565 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
566 mps_dprint(sc, MPS_ERROR,
567 "%s: cm_flags = %#x for remove of handle %#04x! "
568 "This should not happen!\n", __func__, tm->cm_flags,
570 mpssas_free_tm(sc, tm);
575 /* XXX retry the remove after the diag reset completes? */
576 mps_dprint(sc, MPS_FAULT,
577 "%s NULL reply reseting device 0x%04x\n", __func__, handle);
578 mpssas_free_tm(sc, tm);
582 if (le16toh(reply->IOCStatus) != MPI2_IOCSTATUS_SUCCESS) {
583 mps_dprint(sc, MPS_FAULT,
584 "IOCStatus = 0x%x while resetting device 0x%x\n",
585 le16toh(reply->IOCStatus), handle);
586 mpssas_free_tm(sc, tm);
590 mps_dprint(sc, MPS_XINFO, "Reset aborted %u commands\n",
591 le32toh(reply->TerminationCount));
592 mps_free_reply(sc, tm->cm_reply_data);
593 tm->cm_reply = NULL; /* Ensures the reply won't get re-freed */
595 /* Reuse the existing command */
596 req = (MPI2_SAS_IOUNIT_CONTROL_REQUEST *)tm->cm_req;
597 memset(req, 0, sizeof(*req));
598 req->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
599 req->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
600 req->DevHandle = htole16(handle);
602 tm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
603 tm->cm_complete = mpssas_remove_complete;
604 tm->cm_complete_data = (void *)(uintptr_t)handle;
606 mps_map_command(sc, tm);
608 mps_dprint(sc, MPS_XINFO, "clearing target %u handle 0x%04x\n",
610 TAILQ_FOREACH_SAFE(tm, &targ->commands, cm_link, next_cm) {
613 mps_dprint(sc, MPS_XINFO, "Completing missed command %p\n", tm);
614 ccb = tm->cm_complete_data;
615 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
616 mpssas_scsiio_complete(sc, tm);
621 mpssas_remove_complete(struct mps_softc *sc, struct mps_command *tm)
623 MPI2_SAS_IOUNIT_CONTROL_REPLY *reply;
625 struct mpssas_target *targ;
626 struct mpssas_lun *lun;
630 reply = (MPI2_SAS_IOUNIT_CONTROL_REPLY *)tm->cm_reply;
631 handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
634 * Currently there should be no way we can hit this case. It only
635 * happens when we have a failure to allocate chain frames, and
636 * task management commands don't have S/G lists.
638 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
639 mps_dprint(sc, MPS_XINFO,
640 "%s: cm_flags = %#x for remove of handle %#04x! "
641 "This should not happen!\n", __func__, tm->cm_flags,
643 mpssas_free_tm(sc, tm);
648 /* most likely a chip reset */
649 mps_dprint(sc, MPS_FAULT,
650 "%s NULL reply removing device 0x%04x\n", __func__, handle);
651 mpssas_free_tm(sc, tm);
655 mps_dprint(sc, MPS_XINFO,
656 "%s on handle 0x%04x, IOCStatus= 0x%x\n", __func__,
657 handle, le16toh(reply->IOCStatus));
660 * Don't clear target if remove fails because things will get confusing.
661 * Leave the devname and sasaddr intact so that we know to avoid reusing
662 * this target id if possible, and so we can assign the same target id
663 * to this device if it comes back in the future.
665 if (le16toh(reply->IOCStatus) == MPI2_IOCSTATUS_SUCCESS) {
668 targ->encl_handle = 0x0;
669 targ->encl_slot = 0x0;
670 targ->exp_dev_handle = 0x0;
672 targ->linkrate = 0x0;
676 while(!SLIST_EMPTY(&targ->luns)) {
677 lun = SLIST_FIRST(&targ->luns);
678 SLIST_REMOVE_HEAD(&targ->luns, lun_link);
684 mpssas_free_tm(sc, tm);
688 mpssas_register_events(struct mps_softc *sc)
690 u32 events[MPI2_EVENT_NOTIFY_EVENTMASK_WORDS];
693 setbit(events, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
694 setbit(events, MPI2_EVENT_SAS_DISCOVERY);
695 setbit(events, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
696 setbit(events, MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE);
697 setbit(events, MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW);
698 setbit(events, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
699 setbit(events, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
700 setbit(events, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
701 setbit(events, MPI2_EVENT_IR_VOLUME);
702 setbit(events, MPI2_EVENT_IR_PHYSICAL_DISK);
703 setbit(events, MPI2_EVENT_IR_OPERATION_STATUS);
704 setbit(events, MPI2_EVENT_LOG_ENTRY_ADDED);
706 mps_register_events(sc, events, mpssas_evt_handler, NULL,
707 &sc->sassc->mpssas_eh);
713 mps_attach_sas(struct mps_softc *sc)
715 struct mpssas_softc *sassc;
721 sassc = malloc(sizeof(struct mpssas_softc), M_MPT2, M_WAITOK|M_ZERO);
723 device_printf(sc->mps_dev, "Cannot allocate memory %s %d\n",
729 * XXX MaxTargets could change during a reinit. Since we don't
730 * resize the targets[] array during such an event, cache the value
731 * of MaxTargets here so that we don't get into trouble later. This
732 * should move into the reinit logic.
734 sassc->maxtargets = sc->facts->MaxTargets;
735 sassc->targets = malloc(sizeof(struct mpssas_target) *
736 sassc->maxtargets, M_MPT2, M_WAITOK|M_ZERO);
737 if(!sassc->targets) {
738 device_printf(sc->mps_dev, "Cannot allocate memory %s %d\n",
746 if ((sassc->devq = cam_simq_alloc(sc->num_reqs)) == NULL) {
747 mps_dprint(sc, MPS_ERROR, "Cannot allocate SIMQ\n");
752 unit = device_get_unit(sc->mps_dev);
753 sassc->sim = cam_sim_alloc(mpssas_action, mpssas_poll, "mps", sassc,
754 unit, &sc->mps_mtx, sc->num_reqs, sc->num_reqs, sassc->devq);
755 if (sassc->sim == NULL) {
756 mps_dprint(sc, MPS_ERROR, "Cannot allocate SIM\n");
761 TAILQ_INIT(&sassc->ev_queue);
763 /* Initialize taskqueue for Event Handling */
764 TASK_INIT(&sassc->ev_task, 0, mpssas_firmware_event_work, sc);
765 sassc->ev_tq = taskqueue_create("mps_taskq", M_NOWAIT | M_ZERO,
766 taskqueue_thread_enqueue, &sassc->ev_tq);
767 taskqueue_start_threads(&sassc->ev_tq, 1, PRIBIO, "%s taskq",
768 device_get_nameunit(sc->mps_dev));
773 * XXX There should be a bus for every port on the adapter, but since
774 * we're just going to fake the topology for now, we'll pretend that
775 * everything is just a target on a single bus.
777 if ((error = xpt_bus_register(sassc->sim, sc->mps_dev, 0)) != 0) {
778 mps_dprint(sc, MPS_ERROR, "Error %d registering SCSI bus\n",
785 * Assume that discovery events will start right away.
787 * Hold off boot until discovery is complete.
789 sassc->flags |= MPSSAS_IN_STARTUP | MPSSAS_IN_DISCOVERY;
790 sc->sassc->startup_refcount = 0;
791 mpssas_startup_increment(sassc);
793 callout_init(&sassc->discovery_callout, 1 /*mpsafe*/);
796 * Register for async events so we can determine the EEDP
797 * capabilities of devices.
799 status = xpt_create_path(&sassc->path, /*periph*/NULL,
800 cam_sim_path(sc->sassc->sim), CAM_TARGET_WILDCARD,
802 if (status != CAM_REQ_CMP) {
803 mps_printf(sc, "Error %#x creating sim path\n", status);
808 #if (__FreeBSD_version >= 1000006) || \
809 ((__FreeBSD_version >= 901503) && (__FreeBSD_version < 1000000))
810 event = AC_ADVINFO_CHANGED;
812 event = AC_FOUND_DEVICE;
814 status = xpt_register_async(event, mpssas_async, sc,
816 if (status != CAM_REQ_CMP) {
817 mps_dprint(sc, MPS_ERROR,
818 "Error %#x registering async handler for "
819 "AC_ADVINFO_CHANGED events\n", status);
820 xpt_free_path(sassc->path);
824 if (status != CAM_REQ_CMP) {
826 * EEDP use is the exception, not the rule.
827 * Warn the user, but do not fail to attach.
829 mps_printf(sc, "EEDP capabilities disabled.\n");
834 mpssas_register_events(sc);
842 mps_detach_sas(struct mps_softc *sc)
844 struct mpssas_softc *sassc;
845 struct mpssas_lun *lun, *lun_tmp;
846 struct mpssas_target *targ;
851 if (sc->sassc == NULL)
855 mps_deregister_events(sc, sassc->mpssas_eh);
858 * Drain and free the event handling taskqueue with the lock
859 * unheld so that any parallel processing tasks drain properly
860 * without deadlocking.
862 if (sassc->ev_tq != NULL)
863 taskqueue_free(sassc->ev_tq);
865 /* Make sure CAM doesn't wedge if we had to bail out early. */
868 /* Deregister our async handler */
869 if (sassc->path != NULL) {
870 xpt_register_async(0, mpssas_async, sc, sassc->path);
871 xpt_free_path(sassc->path);
875 if (sassc->flags & MPSSAS_IN_STARTUP)
876 xpt_release_simq(sassc->sim, 1);
878 if (sassc->sim != NULL) {
879 xpt_bus_deregister(cam_sim_path(sassc->sim));
880 cam_sim_free(sassc->sim, FALSE);
883 sassc->flags |= MPSSAS_SHUTDOWN;
886 if (sassc->devq != NULL)
887 cam_simq_free(sassc->devq);
889 for(i=0; i< sassc->maxtargets ;i++) {
890 targ = &sassc->targets[i];
891 SLIST_FOREACH_SAFE(lun, &targ->luns, lun_link, lun_tmp) {
895 free(sassc->targets, M_MPT2);
903 mpssas_discovery_end(struct mpssas_softc *sassc)
905 struct mps_softc *sc = sassc->sc;
909 if (sassc->flags & MPSSAS_DISCOVERY_TIMEOUT_PENDING)
910 callout_stop(&sassc->discovery_callout);
915 mpssas_action(struct cam_sim *sim, union ccb *ccb)
917 struct mpssas_softc *sassc;
919 sassc = cam_sim_softc(sim);
921 MPS_FUNCTRACE(sassc->sc);
922 mps_dprint(sassc->sc, MPS_TRACE, "ccb func_code 0x%x\n",
923 ccb->ccb_h.func_code);
924 mtx_assert(&sassc->sc->mps_mtx, MA_OWNED);
926 switch (ccb->ccb_h.func_code) {
929 struct ccb_pathinq *cpi = &ccb->cpi;
931 cpi->version_num = 1;
932 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
933 cpi->target_sprt = 0;
934 #if __FreeBSD_version >= 1000039
935 cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED | PIM_NOSCAN;
937 cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED;
939 cpi->hba_eng_cnt = 0;
940 cpi->max_target = sassc->maxtargets - 1;
942 cpi->initiator_id = sassc->maxtargets - 1;
943 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
944 strncpy(cpi->hba_vid, "Avago Tech (LSI)", HBA_IDLEN);
945 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
946 cpi->unit_number = cam_sim_unit(sim);
947 cpi->bus_id = cam_sim_bus(sim);
948 cpi->base_transfer_speed = 150000;
949 cpi->transport = XPORT_SAS;
950 cpi->transport_version = 0;
951 cpi->protocol = PROTO_SCSI;
952 cpi->protocol_version = SCSI_REV_SPC;
953 #if __FreeBSD_version >= 800001
955 * XXX KDM where does this number come from?
957 cpi->maxio = 256 * 1024;
959 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
962 case XPT_GET_TRAN_SETTINGS:
964 struct ccb_trans_settings *cts;
965 struct ccb_trans_settings_sas *sas;
966 struct ccb_trans_settings_scsi *scsi;
967 struct mpssas_target *targ;
970 sas = &cts->xport_specific.sas;
971 scsi = &cts->proto_specific.scsi;
973 KASSERT(cts->ccb_h.target_id < sassc->maxtargets,
974 ("Target %d out of bounds in XPT_GET_TRANS_SETTINGS\n",
975 cts->ccb_h.target_id));
976 targ = &sassc->targets[cts->ccb_h.target_id];
977 if (targ->handle == 0x0) {
978 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
982 cts->protocol_version = SCSI_REV_SPC2;
983 cts->transport = XPORT_SAS;
984 cts->transport_version = 0;
986 sas->valid = CTS_SAS_VALID_SPEED;
987 switch (targ->linkrate) {
989 sas->bitrate = 150000;
992 sas->bitrate = 300000;
995 sas->bitrate = 600000;
1001 cts->protocol = PROTO_SCSI;
1002 scsi->valid = CTS_SCSI_VALID_TQ;
1003 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
1005 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
1008 case XPT_CALC_GEOMETRY:
1009 cam_calc_geometry(&ccb->ccg, /*extended*/1);
1010 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
1013 mps_dprint(sassc->sc, MPS_XINFO, "mpssas_action XPT_RESET_DEV\n");
1014 mpssas_action_resetdev(sassc, ccb);
1019 mps_dprint(sassc->sc, MPS_XINFO,
1020 "mpssas_action faking success for abort or reset\n");
1021 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
1024 mpssas_action_scsiio(sassc, ccb);
1026 #if __FreeBSD_version >= 900026
1028 mpssas_action_smpio(sassc, ccb);
1032 mpssas_set_ccbstatus(ccb, CAM_FUNC_NOTAVAIL);
1040 mpssas_announce_reset(struct mps_softc *sc, uint32_t ac_code,
1041 target_id_t target_id, lun_id_t lun_id)
1043 path_id_t path_id = cam_sim_path(sc->sassc->sim);
1044 struct cam_path *path;
1046 mps_dprint(sc, MPS_XINFO, "%s code %x target %d lun %jx\n", __func__,
1047 ac_code, target_id, (uintmax_t)lun_id);
1049 if (xpt_create_path(&path, NULL,
1050 path_id, target_id, lun_id) != CAM_REQ_CMP) {
1051 mps_dprint(sc, MPS_ERROR, "unable to create path for reset "
1056 xpt_async(ac_code, path, NULL);
1057 xpt_free_path(path);
1061 mpssas_complete_all_commands(struct mps_softc *sc)
1063 struct mps_command *cm;
1068 mtx_assert(&sc->mps_mtx, MA_OWNED);
1070 /* complete all commands with a NULL reply */
1071 for (i = 1; i < sc->num_reqs; i++) {
1072 cm = &sc->commands[i];
1073 cm->cm_reply = NULL;
1076 if (cm->cm_flags & MPS_CM_FLAGS_POLLED)
1077 cm->cm_flags |= MPS_CM_FLAGS_COMPLETE;
1079 if (cm->cm_complete != NULL) {
1080 mpssas_log_command(cm, MPS_RECOVERY,
1081 "completing cm %p state %x ccb %p for diag reset\n",
1082 cm, cm->cm_state, cm->cm_ccb);
1084 cm->cm_complete(sc, cm);
1088 if (cm->cm_flags & MPS_CM_FLAGS_WAKEUP) {
1089 mpssas_log_command(cm, MPS_RECOVERY,
1090 "waking up cm %p state %x ccb %p for diag reset\n",
1091 cm, cm->cm_state, cm->cm_ccb);
1096 if (cm->cm_sc->io_cmds_active != 0) {
1097 cm->cm_sc->io_cmds_active--;
1099 mps_dprint(cm->cm_sc, MPS_INFO, "Warning: "
1100 "io_cmds_active is out of sync - resynching to "
1104 if ((completed == 0) && (cm->cm_state != MPS_CM_STATE_FREE)) {
1105 /* this should never happen, but if it does, log */
1106 mpssas_log_command(cm, MPS_RECOVERY,
1107 "cm %p state %x flags 0x%x ccb %p during diag "
1108 "reset\n", cm, cm->cm_state, cm->cm_flags,
1115 mpssas_handle_reinit(struct mps_softc *sc)
1119 /* Go back into startup mode and freeze the simq, so that CAM
1120 * doesn't send any commands until after we've rediscovered all
1121 * targets and found the proper device handles for them.
1123 * After the reset, portenable will trigger discovery, and after all
1124 * discovery-related activities have finished, the simq will be
1127 mps_dprint(sc, MPS_INIT, "%s startup\n", __func__);
1128 sc->sassc->flags |= MPSSAS_IN_STARTUP;
1129 sc->sassc->flags |= MPSSAS_IN_DISCOVERY;
1130 mpssas_startup_increment(sc->sassc);
1132 /* notify CAM of a bus reset */
1133 mpssas_announce_reset(sc, AC_BUS_RESET, CAM_TARGET_WILDCARD,
1136 /* complete and cleanup after all outstanding commands */
1137 mpssas_complete_all_commands(sc);
1139 mps_dprint(sc, MPS_INIT,
1140 "%s startup %u after command completion\n", __func__,
1141 sc->sassc->startup_refcount);
1143 /* zero all the target handles, since they may change after the
1144 * reset, and we have to rediscover all the targets and use the new
1147 for (i = 0; i < sc->sassc->maxtargets; i++) {
1148 if (sc->sassc->targets[i].outstanding != 0)
1149 mps_dprint(sc, MPS_INIT, "target %u outstanding %u\n",
1150 i, sc->sassc->targets[i].outstanding);
1151 sc->sassc->targets[i].handle = 0x0;
1152 sc->sassc->targets[i].exp_dev_handle = 0x0;
1153 sc->sassc->targets[i].outstanding = 0;
1154 sc->sassc->targets[i].flags = MPSSAS_TARGET_INDIAGRESET;
1159 mpssas_tm_timeout(void *data)
1161 struct mps_command *tm = data;
1162 struct mps_softc *sc = tm->cm_sc;
1164 mtx_assert(&sc->mps_mtx, MA_OWNED);
1166 mpssas_log_command(tm, MPS_INFO|MPS_RECOVERY,
1167 "task mgmt %p timed out\n", tm);
1172 mpssas_logical_unit_reset_complete(struct mps_softc *sc, struct mps_command *tm)
1174 MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1175 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1176 unsigned int cm_count = 0;
1177 struct mps_command *cm;
1178 struct mpssas_target *targ;
1180 callout_stop(&tm->cm_callout);
1182 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1183 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1187 * Currently there should be no way we can hit this case. It only
1188 * happens when we have a failure to allocate chain frames, and
1189 * task management commands don't have S/G lists.
1190 * XXXSL So should it be an assertion?
1192 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
1193 mps_dprint(sc, MPS_ERROR, "%s: cm_flags = %#x for LUN reset! "
1194 "This should not happen!\n", __func__, tm->cm_flags);
1195 mpssas_free_tm(sc, tm);
1199 if (reply == NULL) {
1200 mpssas_log_command(tm, MPS_RECOVERY,
1201 "NULL reset reply for tm %p\n", tm);
1202 if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) {
1203 /* this completion was due to a reset, just cleanup */
1205 mpssas_free_tm(sc, tm);
1208 /* we should have gotten a reply. */
1214 mpssas_log_command(tm, MPS_RECOVERY,
1215 "logical unit reset status 0x%x code 0x%x count %u\n",
1216 le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1217 le32toh(reply->TerminationCount));
1219 /* See if there are any outstanding commands for this LUN.
1220 * This could be made more efficient by using a per-LU data
1221 * structure of some sort.
1223 TAILQ_FOREACH(cm, &targ->commands, cm_link) {
1224 if (cm->cm_lun == tm->cm_lun)
1228 if (cm_count == 0) {
1229 mpssas_log_command(tm, MPS_RECOVERY|MPS_INFO,
1230 "logical unit %u finished recovery after reset\n",
1233 mpssas_announce_reset(sc, AC_SENT_BDR, tm->cm_targ->tid,
1236 /* we've finished recovery for this logical unit. check and
1237 * see if some other logical unit has a timedout command
1238 * that needs to be processed.
1240 cm = TAILQ_FIRST(&targ->timedout_commands);
1242 mpssas_send_abort(sc, tm, cm);
1246 mpssas_free_tm(sc, tm);
1250 /* if we still have commands for this LUN, the reset
1251 * effectively failed, regardless of the status reported.
1252 * Escalate to a target reset.
1254 mpssas_log_command(tm, MPS_RECOVERY,
1255 "logical unit reset complete for tm %p, but still have %u command(s)\n",
1257 mpssas_send_reset(sc, tm,
1258 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
1263 mpssas_target_reset_complete(struct mps_softc *sc, struct mps_command *tm)
1265 MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1266 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1267 struct mpssas_target *targ;
1269 callout_stop(&tm->cm_callout);
1271 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1272 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1276 * Currently there should be no way we can hit this case. It only
1277 * happens when we have a failure to allocate chain frames, and
1278 * task management commands don't have S/G lists.
1280 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
1281 mps_dprint(sc, MPS_ERROR,"%s: cm_flags = %#x for target reset! "
1282 "This should not happen!\n", __func__, tm->cm_flags);
1283 mpssas_free_tm(sc, tm);
1287 if (reply == NULL) {
1288 mpssas_log_command(tm, MPS_RECOVERY,
1289 "NULL reset reply for tm %p\n", tm);
1290 if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) {
1291 /* this completion was due to a reset, just cleanup */
1293 mpssas_free_tm(sc, tm);
1296 /* we should have gotten a reply. */
1302 mpssas_log_command(tm, MPS_RECOVERY,
1303 "target reset status 0x%x code 0x%x count %u\n",
1304 le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1305 le32toh(reply->TerminationCount));
1307 if (targ->outstanding == 0) {
1308 /* we've finished recovery for this target and all
1309 * of its logical units.
1311 mpssas_log_command(tm, MPS_RECOVERY|MPS_INFO,
1312 "recovery finished after target reset\n");
1314 mpssas_announce_reset(sc, AC_SENT_BDR, tm->cm_targ->tid,
1318 mpssas_free_tm(sc, tm);
1321 /* after a target reset, if this target still has
1322 * outstanding commands, the reset effectively failed,
1323 * regardless of the status reported. escalate.
1325 mpssas_log_command(tm, MPS_RECOVERY,
1326 "target reset complete for tm %p, but still have %u command(s)\n",
1327 tm, targ->outstanding);
1332 #define MPS_RESET_TIMEOUT 30
1335 mpssas_send_reset(struct mps_softc *sc, struct mps_command *tm, uint8_t type)
1337 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1338 struct mpssas_target *target;
1341 target = tm->cm_targ;
1342 if (target->handle == 0) {
1343 mps_dprint(sc, MPS_ERROR,"%s null devhandle for target_id %d\n",
1344 __func__, target->tid);
1348 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1349 req->DevHandle = htole16(target->handle);
1350 req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
1351 req->TaskType = type;
1353 if (type == MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET) {
1354 /* XXX Need to handle invalid LUNs */
1355 MPS_SET_LUN(req->LUN, tm->cm_lun);
1356 tm->cm_targ->logical_unit_resets++;
1357 mpssas_log_command(tm, MPS_RECOVERY|MPS_INFO,
1358 "sending logical unit reset\n");
1359 tm->cm_complete = mpssas_logical_unit_reset_complete;
1360 mpssas_prepare_for_tm(sc, tm, target, tm->cm_lun);
1362 else if (type == MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET) {
1364 * Target reset method =
1365 * SAS Hard Link Reset / SATA Link Reset
1367 req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
1368 tm->cm_targ->target_resets++;
1369 mpssas_log_command(tm, MPS_RECOVERY|MPS_INFO,
1370 "sending target reset\n");
1371 tm->cm_complete = mpssas_target_reset_complete;
1372 mpssas_prepare_for_tm(sc, tm, target, CAM_LUN_WILDCARD);
1375 mps_dprint(sc, MPS_ERROR, "unexpected reset type 0x%x\n", type);
1380 tm->cm_desc.HighPriority.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
1381 tm->cm_complete_data = (void *)tm;
1383 callout_reset(&tm->cm_callout, MPS_RESET_TIMEOUT * hz,
1384 mpssas_tm_timeout, tm);
1386 err = mps_map_command(sc, tm);
1388 mpssas_log_command(tm, MPS_RECOVERY,
1389 "error %d sending reset type %u\n",
1397 mpssas_abort_complete(struct mps_softc *sc, struct mps_command *tm)
1399 struct mps_command *cm;
1400 MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1401 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1402 struct mpssas_target *targ;
1404 callout_stop(&tm->cm_callout);
1406 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1407 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1411 * Currently there should be no way we can hit this case. It only
1412 * happens when we have a failure to allocate chain frames, and
1413 * task management commands don't have S/G lists.
1415 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
1416 mpssas_log_command(tm, MPS_RECOVERY,
1417 "cm_flags = %#x for abort %p TaskMID %u!\n",
1418 tm->cm_flags, tm, le16toh(req->TaskMID));
1419 mpssas_free_tm(sc, tm);
1423 if (reply == NULL) {
1424 mpssas_log_command(tm, MPS_RECOVERY,
1425 "NULL abort reply for tm %p TaskMID %u\n",
1426 tm, le16toh(req->TaskMID));
1427 if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) {
1428 /* this completion was due to a reset, just cleanup */
1430 mpssas_free_tm(sc, tm);
1433 /* we should have gotten a reply. */
1439 mpssas_log_command(tm, MPS_RECOVERY,
1440 "abort TaskMID %u status 0x%x code 0x%x count %u\n",
1441 le16toh(req->TaskMID),
1442 le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1443 le32toh(reply->TerminationCount));
1445 cm = TAILQ_FIRST(&tm->cm_targ->timedout_commands);
1447 /* if there are no more timedout commands, we're done with
1448 * error recovery for this target.
1450 mpssas_log_command(tm, MPS_RECOVERY,
1451 "finished recovery after aborting TaskMID %u\n",
1452 le16toh(req->TaskMID));
1455 mpssas_free_tm(sc, tm);
1457 else if (le16toh(req->TaskMID) != cm->cm_desc.Default.SMID) {
1458 /* abort success, but we have more timedout commands to abort */
1459 mpssas_log_command(tm, MPS_RECOVERY,
1460 "continuing recovery after aborting TaskMID %u\n",
1461 le16toh(req->TaskMID));
1463 mpssas_send_abort(sc, tm, cm);
1466 /* we didn't get a command completion, so the abort
1467 * failed as far as we're concerned. escalate.
1469 mpssas_log_command(tm, MPS_RECOVERY,
1470 "abort failed for TaskMID %u tm %p\n",
1471 le16toh(req->TaskMID), tm);
1473 mpssas_send_reset(sc, tm,
1474 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET);
1478 #define MPS_ABORT_TIMEOUT 5
1481 mpssas_send_abort(struct mps_softc *sc, struct mps_command *tm, struct mps_command *cm)
1483 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1484 struct mpssas_target *targ;
1488 if (targ->handle == 0) {
1489 mps_dprint(sc, MPS_ERROR,"%s null devhandle for target_id %d\n",
1490 __func__, cm->cm_ccb->ccb_h.target_id);
1494 mpssas_log_command(tm, MPS_RECOVERY|MPS_INFO,
1495 "Aborting command %p\n", cm);
1497 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1498 req->DevHandle = htole16(targ->handle);
1499 req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
1500 req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK;
1502 /* XXX Need to handle invalid LUNs */
1503 MPS_SET_LUN(req->LUN, cm->cm_ccb->ccb_h.target_lun);
1505 req->TaskMID = htole16(cm->cm_desc.Default.SMID);
1508 tm->cm_desc.HighPriority.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
1509 tm->cm_complete = mpssas_abort_complete;
1510 tm->cm_complete_data = (void *)tm;
1511 tm->cm_targ = cm->cm_targ;
1512 tm->cm_lun = cm->cm_lun;
1514 callout_reset(&tm->cm_callout, MPS_ABORT_TIMEOUT * hz,
1515 mpssas_tm_timeout, tm);
1519 mps_dprint(sc, MPS_INFO, "Sending reset from %s for target ID %d\n",
1520 __func__, targ->tid);
1521 mpssas_prepare_for_tm(sc, tm, targ, tm->cm_lun);
1523 err = mps_map_command(sc, tm);
1525 mpssas_log_command(tm, MPS_RECOVERY,
1526 "error %d sending abort for cm %p SMID %u\n",
1527 err, cm, req->TaskMID);
1532 mpssas_scsiio_timeout(void *data)
1534 struct mps_softc *sc;
1535 struct mps_command *cm;
1536 struct mpssas_target *targ;
1538 cm = (struct mps_command *)data;
1542 mtx_assert(&sc->mps_mtx, MA_OWNED);
1544 mps_dprint(sc, MPS_XINFO, "Timeout checking cm %p\n", sc);
1547 * Run the interrupt handler to make sure it's not pending. This
1548 * isn't perfect because the command could have already completed
1549 * and been re-used, though this is unlikely.
1551 mps_intr_locked(sc);
1552 if (cm->cm_state == MPS_CM_STATE_FREE) {
1553 mpssas_log_command(cm, MPS_XINFO,
1554 "SCSI command %p almost timed out\n", cm);
1558 if (cm->cm_ccb == NULL) {
1559 mps_dprint(sc, MPS_ERROR, "command timeout with NULL ccb\n");
1563 mpssas_log_command(cm, MPS_INFO, "command timeout cm %p ccb %p\n",
1569 /* XXX first, check the firmware state, to see if it's still
1570 * operational. if not, do a diag reset.
1572 mpssas_set_ccbstatus(cm->cm_ccb, CAM_CMD_TIMEOUT);
1573 cm->cm_state = MPS_CM_STATE_TIMEDOUT;
1574 TAILQ_INSERT_TAIL(&targ->timedout_commands, cm, cm_recovery);
1576 if (targ->tm != NULL) {
1577 /* target already in recovery, just queue up another
1578 * timedout command to be processed later.
1580 mps_dprint(sc, MPS_RECOVERY,
1581 "queued timedout cm %p for processing by tm %p\n",
1584 else if ((targ->tm = mpssas_alloc_tm(sc)) != NULL) {
1585 mps_dprint(sc, MPS_RECOVERY, "timedout cm %p allocated tm %p\n",
1588 /* start recovery by aborting the first timedout command */
1589 mpssas_send_abort(sc, targ->tm, cm);
1592 /* XXX queue this target up for recovery once a TM becomes
1593 * available. The firmware only has a limited number of
1594 * HighPriority credits for the high priority requests used
1595 * for task management, and we ran out.
1597 * Isilon: don't worry about this for now, since we have
1598 * more credits than disks in an enclosure, and limit
1599 * ourselves to one TM per target for recovery.
1601 mps_dprint(sc, MPS_RECOVERY,
1602 "timedout cm %p failed to allocate a tm\n", cm);
1608 mpssas_action_scsiio(struct mpssas_softc *sassc, union ccb *ccb)
1610 MPI2_SCSI_IO_REQUEST *req;
1611 struct ccb_scsiio *csio;
1612 struct mps_softc *sc;
1613 struct mpssas_target *targ;
1614 struct mpssas_lun *lun;
1615 struct mps_command *cm;
1616 uint8_t i, lba_byte, *ref_tag_addr;
1617 uint16_t eedp_flags;
1618 uint32_t mpi_control;
1622 mtx_assert(&sc->mps_mtx, MA_OWNED);
1625 KASSERT(csio->ccb_h.target_id < sassc->maxtargets,
1626 ("Target %d out of bounds in XPT_SCSI_IO\n",
1627 csio->ccb_h.target_id));
1628 targ = &sassc->targets[csio->ccb_h.target_id];
1629 mps_dprint(sc, MPS_TRACE, "ccb %p target flag %x\n", ccb, targ->flags);
1630 if (targ->handle == 0x0) {
1631 mps_dprint(sc, MPS_ERROR, "%s NULL handle for target %u\n",
1632 __func__, csio->ccb_h.target_id);
1633 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1637 if (targ->flags & MPS_TARGET_FLAGS_RAID_COMPONENT) {
1638 mps_dprint(sc, MPS_ERROR, "%s Raid component no SCSI IO "
1639 "supported %u\n", __func__, csio->ccb_h.target_id);
1640 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1645 * Sometimes, it is possible to get a command that is not "In
1646 * Progress" and was actually aborted by the upper layer. Check for
1647 * this here and complete the command without error.
1649 if (mpssas_get_ccbstatus(ccb) != CAM_REQ_INPROG) {
1650 mps_dprint(sc, MPS_TRACE, "%s Command is not in progress for "
1651 "target %u\n", __func__, csio->ccb_h.target_id);
1656 * If devinfo is 0 this will be a volume. In that case don't tell CAM
1657 * that the volume has timed out. We want volumes to be enumerated
1658 * until they are deleted/removed, not just failed.
1660 if (targ->flags & MPSSAS_TARGET_INREMOVAL) {
1661 if (targ->devinfo == 0)
1662 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
1664 mpssas_set_ccbstatus(ccb, CAM_SEL_TIMEOUT);
1669 if ((sc->mps_flags & MPS_FLAGS_SHUTDOWN) != 0) {
1670 mps_dprint(sc, MPS_INFO, "%s shutting down\n", __func__);
1671 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1677 * If target has a reset in progress, freeze the devq and return. The
1678 * devq will be released when the TM reset is finished.
1680 if (targ->flags & MPSSAS_TARGET_INRESET) {
1681 ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
1682 mps_dprint(sc, MPS_INFO, "%s: Freezing devq for target ID %d\n",
1683 __func__, targ->tid);
1684 xpt_freeze_devq(ccb->ccb_h.path, 1);
1689 cm = mps_alloc_command(sc);
1690 if (cm == NULL || (sc->mps_flags & MPS_FLAGS_DIAGRESET)) {
1692 mps_free_command(sc, cm);
1694 if ((sassc->flags & MPSSAS_QUEUE_FROZEN) == 0) {
1695 xpt_freeze_simq(sassc->sim, 1);
1696 sassc->flags |= MPSSAS_QUEUE_FROZEN;
1698 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1699 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1704 req = (MPI2_SCSI_IO_REQUEST *)cm->cm_req;
1705 bzero(req, sizeof(*req));
1706 req->DevHandle = htole16(targ->handle);
1707 req->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1709 req->SenseBufferLowAddress = htole32(cm->cm_sense_busaddr);
1710 req->SenseBufferLength = MPS_SENSE_LEN;
1712 req->ChainOffset = 0;
1713 req->SGLOffset0 = 24; /* 32bit word offset to the SGL */
1718 req->DataLength = htole32(csio->dxfer_len);
1719 req->BidirectionalDataLength = 0;
1720 req->IoFlags = htole16(csio->cdb_len);
1723 /* Note: BiDirectional transfers are not supported */
1724 switch (csio->ccb_h.flags & CAM_DIR_MASK) {
1726 mpi_control = MPI2_SCSIIO_CONTROL_READ;
1727 cm->cm_flags |= MPS_CM_FLAGS_DATAIN;
1730 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
1731 cm->cm_flags |= MPS_CM_FLAGS_DATAOUT;
1735 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
1739 if (csio->cdb_len == 32)
1740 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
1742 * It looks like the hardware doesn't require an explicit tag
1743 * number for each transaction. SAM Task Management not supported
1746 switch (csio->tag_action) {
1747 case MSG_HEAD_OF_Q_TAG:
1748 mpi_control |= MPI2_SCSIIO_CONTROL_HEADOFQ;
1750 case MSG_ORDERED_Q_TAG:
1751 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
1754 mpi_control |= MPI2_SCSIIO_CONTROL_ACAQ;
1756 case CAM_TAG_ACTION_NONE:
1757 case MSG_SIMPLE_Q_TAG:
1759 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
1762 mpi_control |= sc->mapping_table[csio->ccb_h.target_id].TLR_bits;
1763 req->Control = htole32(mpi_control);
1764 if (MPS_SET_LUN(req->LUN, csio->ccb_h.target_lun) != 0) {
1765 mps_free_command(sc, cm);
1766 mpssas_set_ccbstatus(ccb, CAM_LUN_INVALID);
1771 if (csio->ccb_h.flags & CAM_CDB_POINTER)
1772 bcopy(csio->cdb_io.cdb_ptr, &req->CDB.CDB32[0], csio->cdb_len);
1774 bcopy(csio->cdb_io.cdb_bytes, &req->CDB.CDB32[0],csio->cdb_len);
1775 req->IoFlags = htole16(csio->cdb_len);
1778 * Check if EEDP is supported and enabled. If it is then check if the
1779 * SCSI opcode could be using EEDP. If so, make sure the LUN exists and
1780 * is formatted for EEDP support. If all of this is true, set CDB up
1781 * for EEDP transfer.
1783 eedp_flags = op_code_prot[req->CDB.CDB32[0]];
1784 if (sc->eedp_enabled && eedp_flags) {
1785 SLIST_FOREACH(lun, &targ->luns, lun_link) {
1786 if (lun->lun_id == csio->ccb_h.target_lun) {
1791 if ((lun != NULL) && (lun->eedp_formatted)) {
1792 req->EEDPBlockSize = htole16(lun->eedp_block_size);
1793 eedp_flags |= (MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1794 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1795 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
1796 req->EEDPFlags = htole16(eedp_flags);
1799 * If CDB less than 32, fill in Primary Ref Tag with
1800 * low 4 bytes of LBA. If CDB is 32, tag stuff is
1801 * already there. Also, set protection bit. FreeBSD
1802 * currently does not support CDBs bigger than 16, but
1803 * the code doesn't hurt, and will be here for the
1806 if (csio->cdb_len != 32) {
1807 lba_byte = (csio->cdb_len == 16) ? 6 : 2;
1808 ref_tag_addr = (uint8_t *)&req->CDB.EEDP32.
1809 PrimaryReferenceTag;
1810 for (i = 0; i < 4; i++) {
1812 req->CDB.CDB32[lba_byte + i];
1815 req->CDB.EEDP32.PrimaryReferenceTag =
1816 htole32(req->CDB.EEDP32.PrimaryReferenceTag);
1817 req->CDB.EEDP32.PrimaryApplicationTagMask =
1819 req->CDB.CDB32[1] = (req->CDB.CDB32[1] & 0x1F) |
1823 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_APPTAG;
1824 req->EEDPFlags = htole16(eedp_flags);
1825 req->CDB.CDB32[10] = (req->CDB.CDB32[10] &
1831 cm->cm_length = csio->dxfer_len;
1832 if (cm->cm_length != 0) {
1834 cm->cm_flags |= MPS_CM_FLAGS_USE_CCB;
1838 cm->cm_sge = &req->SGL;
1839 cm->cm_sglsize = (32 - 24) * 4;
1840 cm->cm_desc.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
1841 cm->cm_desc.SCSIIO.DevHandle = htole16(targ->handle);
1842 cm->cm_complete = mpssas_scsiio_complete;
1843 cm->cm_complete_data = ccb;
1845 cm->cm_lun = csio->ccb_h.target_lun;
1849 * If HBA is a WD and the command is not for a retry, try to build a
1850 * direct I/O message. If failed, or the command is for a retry, send
1851 * the I/O to the IR volume itself.
1853 if (sc->WD_valid_config) {
1854 if (ccb->ccb_h.sim_priv.entries[0].field == MPS_WD_RETRY) {
1855 mpssas_direct_drive_io(sassc, cm, ccb);
1857 mpssas_set_ccbstatus(ccb, CAM_REQ_INPROG);
1861 callout_reset_sbt(&cm->cm_callout, SBT_1MS * ccb->ccb_h.timeout, 0,
1862 mpssas_scsiio_timeout, cm, 0);
1865 targ->outstanding++;
1866 TAILQ_INSERT_TAIL(&targ->commands, cm, cm_link);
1867 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1869 mpssas_log_command(cm, MPS_XINFO, "%s cm %p ccb %p outstanding %u\n",
1870 __func__, cm, ccb, targ->outstanding);
1872 mps_map_command(sc, cm);
1877 mps_response_code(struct mps_softc *sc, u8 response_code)
1881 switch (response_code) {
1882 case MPI2_SCSITASKMGMT_RSP_TM_COMPLETE:
1883 desc = "task management request completed";
1885 case MPI2_SCSITASKMGMT_RSP_INVALID_FRAME:
1886 desc = "invalid frame";
1888 case MPI2_SCSITASKMGMT_RSP_TM_NOT_SUPPORTED:
1889 desc = "task management request not supported";
1891 case MPI2_SCSITASKMGMT_RSP_TM_FAILED:
1892 desc = "task management request failed";
1894 case MPI2_SCSITASKMGMT_RSP_TM_SUCCEEDED:
1895 desc = "task management request succeeded";
1897 case MPI2_SCSITASKMGMT_RSP_TM_INVALID_LUN:
1898 desc = "invalid lun";
1901 desc = "overlapped tag attempted";
1903 case MPI2_SCSITASKMGMT_RSP_IO_QUEUED_ON_IOC:
1904 desc = "task queued, however not sent to target";
1910 mps_dprint(sc, MPS_XINFO, "response_code(0x%01x): %s\n",
1911 response_code, desc);
1914 * mps_sc_failed_io_info - translated non-succesfull SCSI_IO request
1917 mps_sc_failed_io_info(struct mps_softc *sc, struct ccb_scsiio *csio,
1918 Mpi2SCSIIOReply_t *mpi_reply)
1922 u16 ioc_status = le16toh(mpi_reply->IOCStatus) &
1923 MPI2_IOCSTATUS_MASK;
1924 u8 scsi_state = mpi_reply->SCSIState;
1925 u8 scsi_status = mpi_reply->SCSIStatus;
1926 char *desc_ioc_state = NULL;
1927 char *desc_scsi_status = NULL;
1928 char *desc_scsi_state = sc->tmp_string;
1929 u32 log_info = le32toh(mpi_reply->IOCLogInfo);
1931 if (log_info == 0x31170000)
1934 switch (ioc_status) {
1935 case MPI2_IOCSTATUS_SUCCESS:
1936 desc_ioc_state = "success";
1938 case MPI2_IOCSTATUS_INVALID_FUNCTION:
1939 desc_ioc_state = "invalid function";
1941 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
1942 desc_ioc_state = "scsi recovered error";
1944 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
1945 desc_ioc_state = "scsi invalid dev handle";
1947 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
1948 desc_ioc_state = "scsi device not there";
1950 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
1951 desc_ioc_state = "scsi data overrun";
1953 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
1954 desc_ioc_state = "scsi data underrun";
1956 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
1957 desc_ioc_state = "scsi io data error";
1959 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
1960 desc_ioc_state = "scsi protocol error";
1962 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
1963 desc_ioc_state = "scsi task terminated";
1965 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
1966 desc_ioc_state = "scsi residual mismatch";
1968 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
1969 desc_ioc_state = "scsi task mgmt failed";
1971 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
1972 desc_ioc_state = "scsi ioc terminated";
1974 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
1975 desc_ioc_state = "scsi ext terminated";
1977 case MPI2_IOCSTATUS_EEDP_GUARD_ERROR:
1978 desc_ioc_state = "eedp guard error";
1980 case MPI2_IOCSTATUS_EEDP_REF_TAG_ERROR:
1981 desc_ioc_state = "eedp ref tag error";
1983 case MPI2_IOCSTATUS_EEDP_APP_TAG_ERROR:
1984 desc_ioc_state = "eedp app tag error";
1987 desc_ioc_state = "unknown";
1991 switch (scsi_status) {
1992 case MPI2_SCSI_STATUS_GOOD:
1993 desc_scsi_status = "good";
1995 case MPI2_SCSI_STATUS_CHECK_CONDITION:
1996 desc_scsi_status = "check condition";
1998 case MPI2_SCSI_STATUS_CONDITION_MET:
1999 desc_scsi_status = "condition met";
2001 case MPI2_SCSI_STATUS_BUSY:
2002 desc_scsi_status = "busy";
2004 case MPI2_SCSI_STATUS_INTERMEDIATE:
2005 desc_scsi_status = "intermediate";
2007 case MPI2_SCSI_STATUS_INTERMEDIATE_CONDMET:
2008 desc_scsi_status = "intermediate condmet";
2010 case MPI2_SCSI_STATUS_RESERVATION_CONFLICT:
2011 desc_scsi_status = "reservation conflict";
2013 case MPI2_SCSI_STATUS_COMMAND_TERMINATED:
2014 desc_scsi_status = "command terminated";
2016 case MPI2_SCSI_STATUS_TASK_SET_FULL:
2017 desc_scsi_status = "task set full";
2019 case MPI2_SCSI_STATUS_ACA_ACTIVE:
2020 desc_scsi_status = "aca active";
2022 case MPI2_SCSI_STATUS_TASK_ABORTED:
2023 desc_scsi_status = "task aborted";
2026 desc_scsi_status = "unknown";
2030 desc_scsi_state[0] = '\0';
2032 desc_scsi_state = " ";
2033 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID)
2034 strcat(desc_scsi_state, "response info ");
2035 if (scsi_state & MPI2_SCSI_STATE_TERMINATED)
2036 strcat(desc_scsi_state, "state terminated ");
2037 if (scsi_state & MPI2_SCSI_STATE_NO_SCSI_STATUS)
2038 strcat(desc_scsi_state, "no status ");
2039 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_FAILED)
2040 strcat(desc_scsi_state, "autosense failed ");
2041 if (scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID)
2042 strcat(desc_scsi_state, "autosense valid ");
2044 mps_dprint(sc, MPS_XINFO, "\thandle(0x%04x), ioc_status(%s)(0x%04x)\n",
2045 le16toh(mpi_reply->DevHandle), desc_ioc_state, ioc_status);
2046 /* We can add more detail about underflow data here
2049 mps_dprint(sc, MPS_XINFO, "\tscsi_status(%s)(0x%02x), "
2050 "scsi_state(%s)(0x%02x)\n", desc_scsi_status, scsi_status,
2051 desc_scsi_state, scsi_state);
2053 if (sc->mps_debug & MPS_XINFO &&
2054 scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2055 mps_dprint(sc, MPS_XINFO, "-> Sense Buffer Data : Start :\n");
2056 scsi_sense_print(csio);
2057 mps_dprint(sc, MPS_XINFO, "-> Sense Buffer Data : End :\n");
2060 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
2061 response_info = le32toh(mpi_reply->ResponseInfo);
2062 response_bytes = (u8 *)&response_info;
2063 mps_response_code(sc,response_bytes[0]);
2068 mpssas_scsiio_complete(struct mps_softc *sc, struct mps_command *cm)
2070 MPI2_SCSI_IO_REPLY *rep;
2072 struct ccb_scsiio *csio;
2073 struct mpssas_softc *sassc;
2074 struct scsi_vpd_supported_page_list *vpd_list = NULL;
2075 u8 *TLR_bits, TLR_on;
2078 struct mpssas_target *target;
2079 target_id_t target_id;
2082 mps_dprint(sc, MPS_TRACE,
2083 "cm %p SMID %u ccb %p reply %p outstanding %u\n", cm,
2084 cm->cm_desc.Default.SMID, cm->cm_ccb, cm->cm_reply,
2085 cm->cm_targ->outstanding);
2087 callout_stop(&cm->cm_callout);
2088 mtx_assert(&sc->mps_mtx, MA_OWNED);
2091 ccb = cm->cm_complete_data;
2093 target_id = csio->ccb_h.target_id;
2094 rep = (MPI2_SCSI_IO_REPLY *)cm->cm_reply;
2096 * XXX KDM if the chain allocation fails, does it matter if we do
2097 * the sync and unload here? It is simpler to do it in every case,
2098 * assuming it doesn't cause problems.
2100 if (cm->cm_data != NULL) {
2101 if (cm->cm_flags & MPS_CM_FLAGS_DATAIN)
2102 dir = BUS_DMASYNC_POSTREAD;
2103 else if (cm->cm_flags & MPS_CM_FLAGS_DATAOUT)
2104 dir = BUS_DMASYNC_POSTWRITE;
2105 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir);
2106 bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
2109 cm->cm_targ->completed++;
2110 cm->cm_targ->outstanding--;
2111 TAILQ_REMOVE(&cm->cm_targ->commands, cm, cm_link);
2112 ccb->ccb_h.status &= ~(CAM_STATUS_MASK | CAM_SIM_QUEUED);
2114 if (cm->cm_state == MPS_CM_STATE_TIMEDOUT) {
2115 TAILQ_REMOVE(&cm->cm_targ->timedout_commands, cm, cm_recovery);
2116 if (cm->cm_reply != NULL)
2117 mpssas_log_command(cm, MPS_RECOVERY,
2118 "completed timedout cm %p ccb %p during recovery "
2119 "ioc %x scsi %x state %x xfer %u\n",
2121 le16toh(rep->IOCStatus), rep->SCSIStatus, rep->SCSIState,
2122 le32toh(rep->TransferCount));
2124 mpssas_log_command(cm, MPS_RECOVERY,
2125 "completed timedout cm %p ccb %p during recovery\n",
2127 } else if (cm->cm_targ->tm != NULL) {
2128 if (cm->cm_reply != NULL)
2129 mpssas_log_command(cm, MPS_RECOVERY,
2130 "completed cm %p ccb %p during recovery "
2131 "ioc %x scsi %x state %x xfer %u\n",
2133 le16toh(rep->IOCStatus), rep->SCSIStatus, rep->SCSIState,
2134 le32toh(rep->TransferCount));
2136 mpssas_log_command(cm, MPS_RECOVERY,
2137 "completed cm %p ccb %p during recovery\n",
2139 } else if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) {
2140 mpssas_log_command(cm, MPS_RECOVERY,
2141 "reset completed cm %p ccb %p\n",
2145 if ((cm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
2147 * We ran into an error after we tried to map the command,
2148 * so we're getting a callback without queueing the command
2149 * to the hardware. So we set the status here, and it will
2150 * be retained below. We'll go through the "fast path",
2151 * because there can be no reply when we haven't actually
2152 * gone out to the hardware.
2154 mpssas_set_ccbstatus(ccb, CAM_REQUEUE_REQ);
2157 * Currently the only error included in the mask is
2158 * MPS_CM_FLAGS_CHAIN_FAILED, which means we're out of
2159 * chain frames. We need to freeze the queue until we get
2160 * a command that completed without this error, which will
2161 * hopefully have some chain frames attached that we can
2162 * use. If we wanted to get smarter about it, we would
2163 * only unfreeze the queue in this condition when we're
2164 * sure that we're getting some chain frames back. That's
2165 * probably unnecessary.
2167 if ((sassc->flags & MPSSAS_QUEUE_FROZEN) == 0) {
2168 xpt_freeze_simq(sassc->sim, 1);
2169 sassc->flags |= MPSSAS_QUEUE_FROZEN;
2170 mps_dprint(sc, MPS_XINFO, "Error sending command, "
2171 "freezing SIM queue\n");
2176 * If this is a Start Stop Unit command and it was issued by the driver
2177 * during shutdown, decrement the refcount to account for all of the
2178 * commands that were sent. All SSU commands should be completed before
2179 * shutdown completes, meaning SSU_refcount will be 0 after SSU_started
2182 if (sc->SSU_started && (csio->cdb_io.cdb_bytes[0] == START_STOP_UNIT)) {
2183 mps_dprint(sc, MPS_INFO, "Decrementing SSU count.\n");
2187 /* Take the fast path to completion */
2188 if (cm->cm_reply == NULL) {
2189 if (mpssas_get_ccbstatus(ccb) == CAM_REQ_INPROG) {
2190 if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0)
2191 mpssas_set_ccbstatus(ccb, CAM_SCSI_BUS_RESET);
2193 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
2194 ccb->csio.scsi_status = SCSI_STATUS_OK;
2196 if (sassc->flags & MPSSAS_QUEUE_FROZEN) {
2197 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2198 sassc->flags &= ~MPSSAS_QUEUE_FROZEN;
2199 mps_dprint(sc, MPS_XINFO,
2200 "Unfreezing SIM queue\n");
2205 * There are two scenarios where the status won't be
2206 * CAM_REQ_CMP. The first is if MPS_CM_FLAGS_ERROR_MASK is
2207 * set, the second is in the MPS_FLAGS_DIAGRESET above.
2209 if (mpssas_get_ccbstatus(ccb) != CAM_REQ_CMP) {
2211 * Freeze the dev queue so that commands are
2212 * executed in the correct order after error
2215 ccb->ccb_h.status |= CAM_DEV_QFRZN;
2216 xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1);
2218 mps_free_command(sc, cm);
2223 mpssas_log_command(cm, MPS_XINFO,
2224 "ioc %x scsi %x state %x xfer %u\n",
2225 le16toh(rep->IOCStatus), rep->SCSIStatus, rep->SCSIState,
2226 le32toh(rep->TransferCount));
2229 * If this is a Direct Drive I/O, reissue the I/O to the original IR
2230 * Volume if an error occurred (normal I/O retry). Use the original
2231 * CCB, but set a flag that this will be a retry so that it's sent to
2232 * the original volume. Free the command but reuse the CCB.
2234 if (cm->cm_flags & MPS_CM_FLAGS_DD_IO) {
2235 mps_free_command(sc, cm);
2236 ccb->ccb_h.sim_priv.entries[0].field = MPS_WD_RETRY;
2237 mpssas_action_scsiio(sassc, ccb);
2240 ccb->ccb_h.sim_priv.entries[0].field = 0;
2242 switch (le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK) {
2243 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
2244 csio->resid = cm->cm_length - le32toh(rep->TransferCount);
2246 case MPI2_IOCSTATUS_SUCCESS:
2247 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
2249 if ((le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK) ==
2250 MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR)
2251 mpssas_log_command(cm, MPS_XINFO, "recovered error\n");
2253 /* Completion failed at the transport level. */
2254 if (rep->SCSIState & (MPI2_SCSI_STATE_NO_SCSI_STATUS |
2255 MPI2_SCSI_STATE_TERMINATED)) {
2256 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2260 /* In a modern packetized environment, an autosense failure
2261 * implies that there's not much else that can be done to
2262 * recover the command.
2264 if (rep->SCSIState & MPI2_SCSI_STATE_AUTOSENSE_FAILED) {
2265 mpssas_set_ccbstatus(ccb, CAM_AUTOSENSE_FAIL);
2270 * CAM doesn't care about SAS Response Info data, but if this is
2271 * the state check if TLR should be done. If not, clear the
2272 * TLR_bits for the target.
2274 if ((rep->SCSIState & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) &&
2275 ((le32toh(rep->ResponseInfo) &
2276 MPI2_SCSI_RI_MASK_REASONCODE) ==
2277 MPS_SCSI_RI_INVALID_FRAME)) {
2278 sc->mapping_table[target_id].TLR_bits =
2279 (u8)MPI2_SCSIIO_CONTROL_NO_TLR;
2283 * Intentionally override the normal SCSI status reporting
2284 * for these two cases. These are likely to happen in a
2285 * multi-initiator environment, and we want to make sure that
2286 * CAM retries these commands rather than fail them.
2288 if ((rep->SCSIStatus == MPI2_SCSI_STATUS_COMMAND_TERMINATED) ||
2289 (rep->SCSIStatus == MPI2_SCSI_STATUS_TASK_ABORTED)) {
2290 mpssas_set_ccbstatus(ccb, CAM_REQ_ABORTED);
2294 /* Handle normal status and sense */
2295 csio->scsi_status = rep->SCSIStatus;
2296 if (rep->SCSIStatus == MPI2_SCSI_STATUS_GOOD)
2297 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
2299 mpssas_set_ccbstatus(ccb, CAM_SCSI_STATUS_ERROR);
2301 if (rep->SCSIState & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2302 int sense_len, returned_sense_len;
2304 returned_sense_len = min(le32toh(rep->SenseCount),
2305 sizeof(struct scsi_sense_data));
2306 if (returned_sense_len < ccb->csio.sense_len)
2307 ccb->csio.sense_resid = ccb->csio.sense_len -
2310 ccb->csio.sense_resid = 0;
2312 sense_len = min(returned_sense_len,
2313 ccb->csio.sense_len - ccb->csio.sense_resid);
2314 bzero(&ccb->csio.sense_data,
2315 sizeof(ccb->csio.sense_data));
2316 bcopy(cm->cm_sense, &ccb->csio.sense_data, sense_len);
2317 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
2321 * Check if this is an INQUIRY command. If it's a VPD inquiry,
2322 * and it's page code 0 (Supported Page List), and there is
2323 * inquiry data, and this is for a sequential access device, and
2324 * the device is an SSP target, and TLR is supported by the
2325 * controller, turn the TLR_bits value ON if page 0x90 is
2328 if ((csio->cdb_io.cdb_bytes[0] == INQUIRY) &&
2329 (csio->cdb_io.cdb_bytes[1] & SI_EVPD) &&
2330 (csio->cdb_io.cdb_bytes[2] == SVPD_SUPPORTED_PAGE_LIST) &&
2331 ((csio->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR) &&
2332 (csio->data_ptr != NULL) &&
2333 ((csio->data_ptr[0] & 0x1f) == T_SEQUENTIAL) &&
2334 (sc->control_TLR) &&
2335 (sc->mapping_table[target_id].device_info &
2336 MPI2_SAS_DEVICE_INFO_SSP_TARGET)) {
2337 vpd_list = (struct scsi_vpd_supported_page_list *)
2339 TLR_bits = &sc->mapping_table[target_id].TLR_bits;
2340 *TLR_bits = (u8)MPI2_SCSIIO_CONTROL_NO_TLR;
2341 TLR_on = (u8)MPI2_SCSIIO_CONTROL_TLR_ON;
2342 alloc_len = ((u16)csio->cdb_io.cdb_bytes[3] << 8) +
2343 csio->cdb_io.cdb_bytes[4];
2344 alloc_len -= csio->resid;
2345 for (i = 0; i < MIN(vpd_list->length, alloc_len); i++) {
2346 if (vpd_list->list[i] == 0x90) {
2354 * If this is a SATA direct-access end device, mark it so that
2355 * a SCSI StartStopUnit command will be sent to it when the
2356 * driver is being shutdown.
2358 if ((csio->cdb_io.cdb_bytes[0] == INQUIRY) &&
2359 ((csio->data_ptr[0] & 0x1f) == T_DIRECT) &&
2360 (sc->mapping_table[target_id].device_info &
2361 MPI2_SAS_DEVICE_INFO_SATA_DEVICE) &&
2362 ((sc->mapping_table[target_id].device_info &
2363 MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) ==
2364 MPI2_SAS_DEVICE_INFO_END_DEVICE)) {
2365 target = &sassc->targets[target_id];
2366 target->supports_SSU = TRUE;
2367 mps_dprint(sc, MPS_XINFO, "Target %d supports SSU\n",
2371 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
2372 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2374 * If devinfo is 0 this will be a volume. In that case don't
2375 * tell CAM that the volume is not there. We want volumes to
2376 * be enumerated until they are deleted/removed, not just
2379 if (cm->cm_targ->devinfo == 0)
2380 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
2382 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
2384 case MPI2_IOCSTATUS_INVALID_SGL:
2385 mps_print_scsiio_cmd(sc, cm);
2386 mpssas_set_ccbstatus(ccb, CAM_UNREC_HBA_ERROR);
2388 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
2390 * This is one of the responses that comes back when an I/O
2391 * has been aborted. If it is because of a timeout that we
2392 * initiated, just set the status to CAM_CMD_TIMEOUT.
2393 * Otherwise set it to CAM_REQ_ABORTED. The effect on the
2394 * command is the same (it gets retried, subject to the
2395 * retry counter), the only difference is what gets printed
2398 if (cm->cm_state == MPS_CM_STATE_TIMEDOUT)
2399 mpssas_set_ccbstatus(ccb, CAM_CMD_TIMEOUT);
2401 mpssas_set_ccbstatus(ccb, CAM_REQ_ABORTED);
2403 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
2404 /* resid is ignored for this condition */
2406 mpssas_set_ccbstatus(ccb, CAM_DATA_RUN_ERR);
2408 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
2409 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
2411 * These can sometimes be transient transport-related
2412 * errors, and sometimes persistent drive-related errors.
2413 * We used to retry these without decrementing the retry
2414 * count by returning CAM_REQUEUE_REQ. Unfortunately, if
2415 * we hit a persistent drive problem that returns one of
2416 * these error codes, we would retry indefinitely. So,
2417 * return CAM_REQ_CMP_ERROR so that we decrement the retry
2418 * count and avoid infinite retries. We're taking the
2419 * potential risk of flagging false failures in the event
2420 * of a topology-related error (e.g. a SAS expander problem
2421 * causes a command addressed to a drive to fail), but
2422 * avoiding getting into an infinite retry loop.
2424 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2425 mpssas_log_command(cm, MPS_INFO,
2426 "terminated ioc %x scsi %x state %x xfer %u\n",
2427 le16toh(rep->IOCStatus), rep->SCSIStatus, rep->SCSIState,
2428 le32toh(rep->TransferCount));
2430 case MPI2_IOCSTATUS_INVALID_FUNCTION:
2431 case MPI2_IOCSTATUS_INTERNAL_ERROR:
2432 case MPI2_IOCSTATUS_INVALID_VPID:
2433 case MPI2_IOCSTATUS_INVALID_FIELD:
2434 case MPI2_IOCSTATUS_INVALID_STATE:
2435 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
2436 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
2437 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2438 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2439 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2441 mpssas_log_command(cm, MPS_XINFO,
2442 "completed ioc %x scsi %x state %x xfer %u\n",
2443 le16toh(rep->IOCStatus), rep->SCSIStatus, rep->SCSIState,
2444 le32toh(rep->TransferCount));
2445 csio->resid = cm->cm_length;
2446 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2450 mps_sc_failed_io_info(sc,csio,rep);
2452 if (sassc->flags & MPSSAS_QUEUE_FROZEN) {
2453 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2454 sassc->flags &= ~MPSSAS_QUEUE_FROZEN;
2455 mps_dprint(sc, MPS_XINFO, "Command completed, "
2456 "unfreezing SIM queue\n");
2459 if (mpssas_get_ccbstatus(ccb) != CAM_REQ_CMP) {
2460 ccb->ccb_h.status |= CAM_DEV_QFRZN;
2461 xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1);
2464 mps_free_command(sc, cm);
2468 /* All Request reached here are Endian safe */
2470 mpssas_direct_drive_io(struct mpssas_softc *sassc, struct mps_command *cm,
2472 pMpi2SCSIIORequest_t pIO_req;
2473 struct mps_softc *sc = sassc->sc;
2475 uint32_t physLBA, stripe_offset, stripe_unit;
2476 uint32_t io_size, column;
2477 uint8_t *ptrLBA, lba_idx, physLBA_byte, *CDB;
2480 * If this is a valid SCSI command (Read6, Read10, Read16, Write6,
2481 * Write10, or Write16), build a direct I/O message. Otherwise, the I/O
2482 * will be sent to the IR volume itself. Since Read6 and Write6 are a
2483 * bit different than the 10/16 CDBs, handle them separately.
2485 pIO_req = (pMpi2SCSIIORequest_t)cm->cm_req;
2486 CDB = pIO_req->CDB.CDB32;
2489 * Handle 6 byte CDBs.
2491 if ((pIO_req->DevHandle == sc->DD_dev_handle) && ((CDB[0] == READ_6) ||
2492 (CDB[0] == WRITE_6))) {
2494 * Get the transfer size in blocks.
2496 io_size = (cm->cm_length >> sc->DD_block_exponent);
2499 * Get virtual LBA given in the CDB.
2501 virtLBA = ((uint64_t)(CDB[1] & 0x1F) << 16) |
2502 ((uint64_t)CDB[2] << 8) | (uint64_t)CDB[3];
2505 * Check that LBA range for I/O does not exceed volume's
2508 if ((virtLBA + (uint64_t)io_size - 1) <=
2511 * Check if the I/O crosses a stripe boundary. If not,
2512 * translate the virtual LBA to a physical LBA and set
2513 * the DevHandle for the PhysDisk to be used. If it
2514 * does cross a boundry, do normal I/O. To get the
2515 * right DevHandle to use, get the map number for the
2516 * column, then use that map number to look up the
2517 * DevHandle of the PhysDisk.
2519 stripe_offset = (uint32_t)virtLBA &
2520 (sc->DD_stripe_size - 1);
2521 if ((stripe_offset + io_size) <= sc->DD_stripe_size) {
2522 physLBA = (uint32_t)virtLBA >>
2523 sc->DD_stripe_exponent;
2524 stripe_unit = physLBA / sc->DD_num_phys_disks;
2525 column = physLBA % sc->DD_num_phys_disks;
2526 pIO_req->DevHandle =
2527 htole16(sc->DD_column_map[column].dev_handle);
2528 /* ???? Is this endian safe*/
2529 cm->cm_desc.SCSIIO.DevHandle =
2532 physLBA = (stripe_unit <<
2533 sc->DD_stripe_exponent) + stripe_offset;
2534 ptrLBA = &pIO_req->CDB.CDB32[1];
2535 physLBA_byte = (uint8_t)(physLBA >> 16);
2536 *ptrLBA = physLBA_byte;
2537 ptrLBA = &pIO_req->CDB.CDB32[2];
2538 physLBA_byte = (uint8_t)(physLBA >> 8);
2539 *ptrLBA = physLBA_byte;
2540 ptrLBA = &pIO_req->CDB.CDB32[3];
2541 physLBA_byte = (uint8_t)physLBA;
2542 *ptrLBA = physLBA_byte;
2545 * Set flag that Direct Drive I/O is
2548 cm->cm_flags |= MPS_CM_FLAGS_DD_IO;
2555 * Handle 10, 12 or 16 byte CDBs.
2557 if ((pIO_req->DevHandle == sc->DD_dev_handle) && ((CDB[0] == READ_10) ||
2558 (CDB[0] == WRITE_10) || (CDB[0] == READ_16) ||
2559 (CDB[0] == WRITE_16) || (CDB[0] == READ_12) ||
2560 (CDB[0] == WRITE_12))) {
2562 * For 16-byte CDB's, verify that the upper 4 bytes of the CDB
2563 * are 0. If not, this is accessing beyond 2TB so handle it in
2564 * the else section. 10-byte and 12-byte CDB's are OK.
2565 * FreeBSD sends very rare 12 byte READ/WRITE, but driver is
2566 * ready to accept 12byte CDB for Direct IOs.
2568 if ((CDB[0] == READ_10 || CDB[0] == WRITE_10) ||
2569 (CDB[0] == READ_12 || CDB[0] == WRITE_12) ||
2570 !(CDB[2] | CDB[3] | CDB[4] | CDB[5])) {
2572 * Get the transfer size in blocks.
2574 io_size = (cm->cm_length >> sc->DD_block_exponent);
2577 * Get virtual LBA. Point to correct lower 4 bytes of
2578 * LBA in the CDB depending on command.
2580 lba_idx = ((CDB[0] == READ_12) ||
2581 (CDB[0] == WRITE_12) ||
2582 (CDB[0] == READ_10) ||
2583 (CDB[0] == WRITE_10))? 2 : 6;
2584 virtLBA = ((uint64_t)CDB[lba_idx] << 24) |
2585 ((uint64_t)CDB[lba_idx + 1] << 16) |
2586 ((uint64_t)CDB[lba_idx + 2] << 8) |
2587 (uint64_t)CDB[lba_idx + 3];
2590 * Check that LBA range for I/O does not exceed volume's
2593 if ((virtLBA + (uint64_t)io_size - 1) <=
2596 * Check if the I/O crosses a stripe boundary.
2597 * If not, translate the virtual LBA to a
2598 * physical LBA and set the DevHandle for the
2599 * PhysDisk to be used. If it does cross a
2600 * boundry, do normal I/O. To get the right
2601 * DevHandle to use, get the map number for the
2602 * column, then use that map number to look up
2603 * the DevHandle of the PhysDisk.
2605 stripe_offset = (uint32_t)virtLBA &
2606 (sc->DD_stripe_size - 1);
2607 if ((stripe_offset + io_size) <=
2608 sc->DD_stripe_size) {
2609 physLBA = (uint32_t)virtLBA >>
2610 sc->DD_stripe_exponent;
2611 stripe_unit = physLBA /
2612 sc->DD_num_phys_disks;
2614 sc->DD_num_phys_disks;
2615 pIO_req->DevHandle =
2616 htole16(sc->DD_column_map[column].
2618 cm->cm_desc.SCSIIO.DevHandle =
2621 physLBA = (stripe_unit <<
2622 sc->DD_stripe_exponent) +
2625 &pIO_req->CDB.CDB32[lba_idx];
2626 physLBA_byte = (uint8_t)(physLBA >> 24);
2627 *ptrLBA = physLBA_byte;
2629 &pIO_req->CDB.CDB32[lba_idx + 1];
2630 physLBA_byte = (uint8_t)(physLBA >> 16);
2631 *ptrLBA = physLBA_byte;
2633 &pIO_req->CDB.CDB32[lba_idx + 2];
2634 physLBA_byte = (uint8_t)(physLBA >> 8);
2635 *ptrLBA = physLBA_byte;
2637 &pIO_req->CDB.CDB32[lba_idx + 3];
2638 physLBA_byte = (uint8_t)physLBA;
2639 *ptrLBA = physLBA_byte;
2642 * Set flag that Direct Drive I/O is
2645 cm->cm_flags |= MPS_CM_FLAGS_DD_IO;
2650 * 16-byte CDB and the upper 4 bytes of the CDB are not
2651 * 0. Get the transfer size in blocks.
2653 io_size = (cm->cm_length >> sc->DD_block_exponent);
2658 virtLBA = ((uint64_t)CDB[2] << 54) |
2659 ((uint64_t)CDB[3] << 48) |
2660 ((uint64_t)CDB[4] << 40) |
2661 ((uint64_t)CDB[5] << 32) |
2662 ((uint64_t)CDB[6] << 24) |
2663 ((uint64_t)CDB[7] << 16) |
2664 ((uint64_t)CDB[8] << 8) |
2668 * Check that LBA range for I/O does not exceed volume's
2671 if ((virtLBA + (uint64_t)io_size - 1) <=
2674 * Check if the I/O crosses a stripe boundary.
2675 * If not, translate the virtual LBA to a
2676 * physical LBA and set the DevHandle for the
2677 * PhysDisk to be used. If it does cross a
2678 * boundry, do normal I/O. To get the right
2679 * DevHandle to use, get the map number for the
2680 * column, then use that map number to look up
2681 * the DevHandle of the PhysDisk.
2683 stripe_offset = (uint32_t)virtLBA &
2684 (sc->DD_stripe_size - 1);
2685 if ((stripe_offset + io_size) <=
2686 sc->DD_stripe_size) {
2687 physLBA = (uint32_t)(virtLBA >>
2688 sc->DD_stripe_exponent);
2689 stripe_unit = physLBA /
2690 sc->DD_num_phys_disks;
2692 sc->DD_num_phys_disks;
2693 pIO_req->DevHandle =
2694 htole16(sc->DD_column_map[column].
2696 cm->cm_desc.SCSIIO.DevHandle =
2699 physLBA = (stripe_unit <<
2700 sc->DD_stripe_exponent) +
2704 * Set upper 4 bytes of LBA to 0. We
2705 * assume that the phys disks are less
2706 * than 2 TB's in size. Then, set the
2709 pIO_req->CDB.CDB32[2] = 0;
2710 pIO_req->CDB.CDB32[3] = 0;
2711 pIO_req->CDB.CDB32[4] = 0;
2712 pIO_req->CDB.CDB32[5] = 0;
2713 ptrLBA = &pIO_req->CDB.CDB32[6];
2714 physLBA_byte = (uint8_t)(physLBA >> 24);
2715 *ptrLBA = physLBA_byte;
2716 ptrLBA = &pIO_req->CDB.CDB32[7];
2717 physLBA_byte = (uint8_t)(physLBA >> 16);
2718 *ptrLBA = physLBA_byte;
2719 ptrLBA = &pIO_req->CDB.CDB32[8];
2720 physLBA_byte = (uint8_t)(physLBA >> 8);
2721 *ptrLBA = physLBA_byte;
2722 ptrLBA = &pIO_req->CDB.CDB32[9];
2723 physLBA_byte = (uint8_t)physLBA;
2724 *ptrLBA = physLBA_byte;
2727 * Set flag that Direct Drive I/O is
2730 cm->cm_flags |= MPS_CM_FLAGS_DD_IO;
2737 #if __FreeBSD_version >= 900026
2739 mpssas_smpio_complete(struct mps_softc *sc, struct mps_command *cm)
2741 MPI2_SMP_PASSTHROUGH_REPLY *rpl;
2742 MPI2_SMP_PASSTHROUGH_REQUEST *req;
2746 ccb = cm->cm_complete_data;
2749 * Currently there should be no way we can hit this case. It only
2750 * happens when we have a failure to allocate chain frames, and SMP
2751 * commands require two S/G elements only. That should be handled
2752 * in the standard request size.
2754 if ((cm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
2755 mps_dprint(sc, MPS_ERROR,"%s: cm_flags = %#x on SMP request!\n",
2756 __func__, cm->cm_flags);
2757 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2761 rpl = (MPI2_SMP_PASSTHROUGH_REPLY *)cm->cm_reply;
2763 mps_dprint(sc, MPS_ERROR, "%s: NULL cm_reply!\n", __func__);
2764 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2768 req = (MPI2_SMP_PASSTHROUGH_REQUEST *)cm->cm_req;
2769 sasaddr = le32toh(req->SASAddress.Low);
2770 sasaddr |= ((uint64_t)(le32toh(req->SASAddress.High))) << 32;
2772 if ((le16toh(rpl->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
2773 MPI2_IOCSTATUS_SUCCESS ||
2774 rpl->SASStatus != MPI2_SASSTATUS_SUCCESS) {
2775 mps_dprint(sc, MPS_XINFO, "%s: IOCStatus %04x SASStatus %02x\n",
2776 __func__, le16toh(rpl->IOCStatus), rpl->SASStatus);
2777 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2781 mps_dprint(sc, MPS_XINFO, "%s: SMP request to SAS address "
2782 "%#jx completed successfully\n", __func__,
2783 (uintmax_t)sasaddr);
2785 if (ccb->smpio.smp_response[2] == SMP_FR_ACCEPTED)
2786 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
2788 mpssas_set_ccbstatus(ccb, CAM_SMP_STATUS_ERROR);
2792 * We sync in both directions because we had DMAs in the S/G list
2793 * in both directions.
2795 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap,
2796 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2797 bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
2798 mps_free_command(sc, cm);
2803 mpssas_send_smpcmd(struct mpssas_softc *sassc, union ccb *ccb, uint64_t sasaddr)
2805 struct mps_command *cm;
2806 uint8_t *request, *response;
2807 MPI2_SMP_PASSTHROUGH_REQUEST *req;
2808 struct mps_softc *sc;
2817 * XXX We don't yet support physical addresses here.
2819 switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) {
2820 case CAM_DATA_PADDR:
2821 case CAM_DATA_SG_PADDR:
2822 mps_dprint(sc, MPS_ERROR,
2823 "%s: physical addresses not supported\n", __func__);
2824 mpssas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2829 * The chip does not support more than one buffer for the
2830 * request or response.
2832 if ((ccb->smpio.smp_request_sglist_cnt > 1)
2833 || (ccb->smpio.smp_response_sglist_cnt > 1)) {
2834 mps_dprint(sc, MPS_ERROR,
2835 "%s: multiple request or response "
2836 "buffer segments not supported for SMP\n",
2838 mpssas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2844 * The CAM_SCATTER_VALID flag was originally implemented
2845 * for the XPT_SCSI_IO CCB, which only has one data pointer.
2846 * We have two. So, just take that flag to mean that we
2847 * might have S/G lists, and look at the S/G segment count
2848 * to figure out whether that is the case for each individual
2851 if (ccb->smpio.smp_request_sglist_cnt != 0) {
2852 bus_dma_segment_t *req_sg;
2854 req_sg = (bus_dma_segment_t *)ccb->smpio.smp_request;
2855 request = (uint8_t *)(uintptr_t)req_sg[0].ds_addr;
2857 request = ccb->smpio.smp_request;
2859 if (ccb->smpio.smp_response_sglist_cnt != 0) {
2860 bus_dma_segment_t *rsp_sg;
2862 rsp_sg = (bus_dma_segment_t *)ccb->smpio.smp_response;
2863 response = (uint8_t *)(uintptr_t)rsp_sg[0].ds_addr;
2865 response = ccb->smpio.smp_response;
2867 case CAM_DATA_VADDR:
2868 request = ccb->smpio.smp_request;
2869 response = ccb->smpio.smp_response;
2872 mpssas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2877 cm = mps_alloc_command(sc);
2879 mps_dprint(sc, MPS_ERROR,
2880 "%s: cannot allocate command\n", __func__);
2881 mpssas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL);
2886 req = (MPI2_SMP_PASSTHROUGH_REQUEST *)cm->cm_req;
2887 bzero(req, sizeof(*req));
2888 req->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
2890 /* Allow the chip to use any route to this SAS address. */
2891 req->PhysicalPort = 0xff;
2893 req->RequestDataLength = htole16(ccb->smpio.smp_request_len);
2895 MPI2_SGLFLAGS_SYSTEM_ADDRESS_SPACE | MPI2_SGLFLAGS_SGL_TYPE_MPI;
2897 mps_dprint(sc, MPS_XINFO, "%s: sending SMP request to SAS "
2898 "address %#jx\n", __func__, (uintmax_t)sasaddr);
2900 mpi_init_sge(cm, req, &req->SGL);
2903 * Set up a uio to pass into mps_map_command(). This allows us to
2904 * do one map command, and one busdma call in there.
2906 cm->cm_uio.uio_iov = cm->cm_iovec;
2907 cm->cm_uio.uio_iovcnt = 2;
2908 cm->cm_uio.uio_segflg = UIO_SYSSPACE;
2911 * The read/write flag isn't used by busdma, but set it just in
2912 * case. This isn't exactly accurate, either, since we're going in
2915 cm->cm_uio.uio_rw = UIO_WRITE;
2917 cm->cm_iovec[0].iov_base = request;
2918 cm->cm_iovec[0].iov_len = le16toh(req->RequestDataLength);
2919 cm->cm_iovec[1].iov_base = response;
2920 cm->cm_iovec[1].iov_len = ccb->smpio.smp_response_len;
2922 cm->cm_uio.uio_resid = cm->cm_iovec[0].iov_len +
2923 cm->cm_iovec[1].iov_len;
2926 * Trigger a warning message in mps_data_cb() for the user if we
2927 * wind up exceeding two S/G segments. The chip expects one
2928 * segment for the request and another for the response.
2930 cm->cm_max_segs = 2;
2932 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2933 cm->cm_complete = mpssas_smpio_complete;
2934 cm->cm_complete_data = ccb;
2937 * Tell the mapping code that we're using a uio, and that this is
2938 * an SMP passthrough request. There is a little special-case
2939 * logic there (in mps_data_cb()) to handle the bidirectional
2942 cm->cm_flags |= MPS_CM_FLAGS_USE_UIO | MPS_CM_FLAGS_SMP_PASS |
2943 MPS_CM_FLAGS_DATAIN | MPS_CM_FLAGS_DATAOUT;
2945 /* The chip data format is little endian. */
2946 req->SASAddress.High = htole32(sasaddr >> 32);
2947 req->SASAddress.Low = htole32(sasaddr);
2950 * XXX Note that we don't have a timeout/abort mechanism here.
2951 * From the manual, it looks like task management requests only
2952 * work for SCSI IO and SATA passthrough requests. We may need to
2953 * have a mechanism to retry requests in the event of a chip reset
2954 * at least. Hopefully the chip will insure that any errors short
2955 * of that are relayed back to the driver.
2957 error = mps_map_command(sc, cm);
2958 if ((error != 0) && (error != EINPROGRESS)) {
2959 mps_dprint(sc, MPS_ERROR,
2960 "%s: error %d returned from mps_map_command()\n",
2968 mps_free_command(sc, cm);
2969 mpssas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL);
2976 mpssas_action_smpio(struct mpssas_softc *sassc, union ccb *ccb)
2978 struct mps_softc *sc;
2979 struct mpssas_target *targ;
2980 uint64_t sasaddr = 0;
2985 * Make sure the target exists.
2987 KASSERT(ccb->ccb_h.target_id < sassc->maxtargets,
2988 ("Target %d out of bounds in XPT_SMP_IO\n", ccb->ccb_h.target_id));
2989 targ = &sassc->targets[ccb->ccb_h.target_id];
2990 if (targ->handle == 0x0) {
2991 mps_dprint(sc, MPS_ERROR,
2992 "%s: target %d does not exist!\n", __func__,
2993 ccb->ccb_h.target_id);
2994 mpssas_set_ccbstatus(ccb, CAM_SEL_TIMEOUT);
3000 * If this device has an embedded SMP target, we'll talk to it
3002 * figure out what the expander's address is.
3004 if ((targ->devinfo & MPI2_SAS_DEVICE_INFO_SMP_TARGET) != 0)
3005 sasaddr = targ->sasaddr;
3008 * If we don't have a SAS address for the expander yet, try
3009 * grabbing it from the page 0x83 information cached in the
3010 * transport layer for this target. LSI expanders report the
3011 * expander SAS address as the port-associated SAS address in
3012 * Inquiry VPD page 0x83. Maxim expanders don't report it in page
3015 * XXX KDM disable this for now, but leave it commented out so that
3016 * it is obvious that this is another possible way to get the SAS
3019 * The parent handle method below is a little more reliable, and
3020 * the other benefit is that it works for devices other than SES
3021 * devices. So you can send a SMP request to a da(4) device and it
3022 * will get routed to the expander that device is attached to.
3023 * (Assuming the da(4) device doesn't contain an SMP target...)
3027 sasaddr = xpt_path_sas_addr(ccb->ccb_h.path);
3031 * If we still don't have a SAS address for the expander, look for
3032 * the parent device of this device, which is probably the expander.
3035 #ifdef OLD_MPS_PROBE
3036 struct mpssas_target *parent_target;
3039 if (targ->parent_handle == 0x0) {
3040 mps_dprint(sc, MPS_ERROR,
3041 "%s: handle %d does not have a valid "
3042 "parent handle!\n", __func__, targ->handle);
3043 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3046 #ifdef OLD_MPS_PROBE
3047 parent_target = mpssas_find_target_by_handle(sassc, 0,
3048 targ->parent_handle);
3050 if (parent_target == NULL) {
3051 mps_dprint(sc, MPS_ERROR,
3052 "%s: handle %d does not have a valid "
3053 "parent target!\n", __func__, targ->handle);
3054 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3058 if ((parent_target->devinfo &
3059 MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0) {
3060 mps_dprint(sc, MPS_ERROR,
3061 "%s: handle %d parent %d does not "
3062 "have an SMP target!\n", __func__,
3063 targ->handle, parent_target->handle);
3064 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3069 sasaddr = parent_target->sasaddr;
3070 #else /* OLD_MPS_PROBE */
3071 if ((targ->parent_devinfo &
3072 MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0) {
3073 mps_dprint(sc, MPS_ERROR,
3074 "%s: handle %d parent %d does not "
3075 "have an SMP target!\n", __func__,
3076 targ->handle, targ->parent_handle);
3077 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3081 if (targ->parent_sasaddr == 0x0) {
3082 mps_dprint(sc, MPS_ERROR,
3083 "%s: handle %d parent handle %d does "
3084 "not have a valid SAS address!\n",
3085 __func__, targ->handle, targ->parent_handle);
3086 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3090 sasaddr = targ->parent_sasaddr;
3091 #endif /* OLD_MPS_PROBE */
3096 mps_dprint(sc, MPS_INFO,
3097 "%s: unable to find SAS address for handle %d\n",
3098 __func__, targ->handle);
3099 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3102 mpssas_send_smpcmd(sassc, ccb, sasaddr);
3110 #endif //__FreeBSD_version >= 900026
3113 mpssas_action_resetdev(struct mpssas_softc *sassc, union ccb *ccb)
3115 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
3116 struct mps_softc *sc;
3117 struct mps_command *tm;
3118 struct mpssas_target *targ;
3120 MPS_FUNCTRACE(sassc->sc);
3121 mtx_assert(&sassc->sc->mps_mtx, MA_OWNED);
3123 KASSERT(ccb->ccb_h.target_id < sassc->maxtargets,
3124 ("Target %d out of bounds in XPT_RESET_DEV\n",
3125 ccb->ccb_h.target_id));
3127 tm = mps_alloc_command(sc);
3129 mps_dprint(sc, MPS_ERROR,
3130 "command alloc failure in mpssas_action_resetdev\n");
3131 mpssas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL);
3136 targ = &sassc->targets[ccb->ccb_h.target_id];
3137 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
3138 req->DevHandle = htole16(targ->handle);
3139 req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
3140 req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3142 /* SAS Hard Link Reset / SATA Link Reset */
3143 req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
3146 tm->cm_desc.HighPriority.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY;
3147 tm->cm_complete = mpssas_resetdev_complete;
3148 tm->cm_complete_data = ccb;
3150 targ->flags |= MPSSAS_TARGET_INRESET;
3152 mps_map_command(sc, tm);
3156 mpssas_resetdev_complete(struct mps_softc *sc, struct mps_command *tm)
3158 MPI2_SCSI_TASK_MANAGE_REPLY *resp;
3162 mtx_assert(&sc->mps_mtx, MA_OWNED);
3164 resp = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
3165 ccb = tm->cm_complete_data;
3168 * Currently there should be no way we can hit this case. It only
3169 * happens when we have a failure to allocate chain frames, and
3170 * task management commands don't have S/G lists.
3172 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
3173 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
3175 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
3177 mps_dprint(sc, MPS_ERROR,
3178 "%s: cm_flags = %#x for reset of handle %#04x! "
3179 "This should not happen!\n", __func__, tm->cm_flags,
3181 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
3185 mps_dprint(sc, MPS_XINFO,
3186 "%s: IOCStatus = 0x%x ResponseCode = 0x%x\n", __func__,
3187 le16toh(resp->IOCStatus), le32toh(resp->ResponseCode));
3189 if (le32toh(resp->ResponseCode) == MPI2_SCSITASKMGMT_RSP_TM_COMPLETE) {
3190 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
3191 mpssas_announce_reset(sc, AC_SENT_BDR, tm->cm_targ->tid,
3195 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
3199 mpssas_free_tm(sc, tm);
3204 mpssas_poll(struct cam_sim *sim)
3206 struct mpssas_softc *sassc;
3208 sassc = cam_sim_softc(sim);
3210 if (sassc->sc->mps_debug & MPS_TRACE) {
3211 /* frequent debug messages during a panic just slow
3212 * everything down too much.
3214 mps_printf(sassc->sc, "%s clearing MPS_TRACE\n", __func__);
3215 sassc->sc->mps_debug &= ~MPS_TRACE;
3218 mps_intr_locked(sassc->sc);
3222 mpssas_async(void *callback_arg, uint32_t code, struct cam_path *path,
3225 struct mps_softc *sc;
3227 sc = (struct mps_softc *)callback_arg;
3230 #if (__FreeBSD_version >= 1000006) || \
3231 ((__FreeBSD_version >= 901503) && (__FreeBSD_version < 1000000))
3232 case AC_ADVINFO_CHANGED: {
3233 struct mpssas_target *target;
3234 struct mpssas_softc *sassc;
3235 struct scsi_read_capacity_data_long rcap_buf;
3236 struct ccb_dev_advinfo cdai;
3237 struct mpssas_lun *lun;
3242 buftype = (uintptr_t)arg;
3248 * We're only interested in read capacity data changes.
3250 if (buftype != CDAI_TYPE_RCAPLONG)
3254 * We should have a handle for this, but check to make sure.
3256 KASSERT(xpt_path_target_id(path) < sassc->maxtargets,
3257 ("Target %d out of bounds in mpssas_async\n",
3258 xpt_path_target_id(path)));
3259 target = &sassc->targets[xpt_path_target_id(path)];
3260 if (target->handle == 0)
3263 lunid = xpt_path_lun_id(path);
3265 SLIST_FOREACH(lun, &target->luns, lun_link) {
3266 if (lun->lun_id == lunid) {
3272 if (found_lun == 0) {
3273 lun = malloc(sizeof(struct mpssas_lun), M_MPT2,
3276 mps_dprint(sc, MPS_ERROR, "Unable to alloc "
3277 "LUN for EEDP support.\n");
3280 lun->lun_id = lunid;
3281 SLIST_INSERT_HEAD(&target->luns, lun, lun_link);
3284 bzero(&rcap_buf, sizeof(rcap_buf));
3285 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
3286 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3287 cdai.ccb_h.flags = CAM_DIR_IN;
3288 cdai.buftype = CDAI_TYPE_RCAPLONG;
3289 #if (__FreeBSD_version >= 1100061) || \
3290 ((__FreeBSD_version >= 1001510) && (__FreeBSD_version < 1100000))
3291 cdai.flags = CDAI_FLAG_NONE;
3295 cdai.bufsiz = sizeof(rcap_buf);
3296 cdai.buf = (uint8_t *)&rcap_buf;
3297 xpt_action((union ccb *)&cdai);
3298 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3299 cam_release_devq(cdai.ccb_h.path,
3302 if ((mpssas_get_ccbstatus((union ccb *)&cdai) == CAM_REQ_CMP)
3303 && (rcap_buf.prot & SRC16_PROT_EN)) {
3304 lun->eedp_formatted = TRUE;
3305 lun->eedp_block_size = scsi_4btoul(rcap_buf.length);
3307 lun->eedp_formatted = FALSE;
3308 lun->eedp_block_size = 0;
3313 case AC_FOUND_DEVICE: {
3314 struct ccb_getdev *cgd;
3317 mpssas_check_eedp(sc, path, cgd);
3326 #if (__FreeBSD_version < 901503) || \
3327 ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000006))
3329 mpssas_check_eedp(struct mps_softc *sc, struct cam_path *path,
3330 struct ccb_getdev *cgd)
3332 struct mpssas_softc *sassc = sc->sassc;
3333 struct ccb_scsiio *csio;
3334 struct scsi_read_capacity_16 *scsi_cmd;
3335 struct scsi_read_capacity_eedp *rcap_buf;
3337 target_id_t targetid;
3340 struct cam_path *local_path;
3341 struct mpssas_target *target;
3342 struct mpssas_lun *lun;
3347 pathid = cam_sim_path(sassc->sim);
3348 targetid = xpt_path_target_id(path);
3349 lunid = xpt_path_lun_id(path);
3351 KASSERT(targetid < sassc->maxtargets,
3352 ("Target %d out of bounds in mpssas_check_eedp\n",
3354 target = &sassc->targets[targetid];
3355 if (target->handle == 0x0)
3359 * Determine if the device is EEDP capable.
3361 * If this flag is set in the inquiry data,
3362 * the device supports protection information,
3363 * and must support the 16 byte read
3364 * capacity command, otherwise continue without
3365 * sending read cap 16
3367 if ((cgd->inq_data.spc3_flags & SPC3_SID_PROTECT) == 0)
3371 * Issue a READ CAPACITY 16 command. This info
3372 * is used to determine if the LUN is formatted
3375 ccb = xpt_alloc_ccb_nowait();
3377 mps_dprint(sc, MPS_ERROR, "Unable to alloc CCB "
3378 "for EEDP support.\n");
3382 if (xpt_create_path(&local_path, xpt_periph,
3383 pathid, targetid, lunid) != CAM_REQ_CMP) {
3384 mps_dprint(sc, MPS_ERROR, "Unable to create "
3385 "path for EEDP support\n");
3391 * If LUN is already in list, don't create a new
3395 SLIST_FOREACH(lun, &target->luns, lun_link) {
3396 if (lun->lun_id == lunid) {
3402 lun = malloc(sizeof(struct mpssas_lun), M_MPT2,
3405 mps_dprint(sc, MPS_ERROR,
3406 "Unable to alloc LUN for EEDP support.\n");
3407 xpt_free_path(local_path);
3411 lun->lun_id = lunid;
3412 SLIST_INSERT_HEAD(&target->luns, lun,
3416 xpt_path_string(local_path, path_str, sizeof(path_str));
3418 mps_dprint(sc, MPS_INFO, "Sending read cap: path %s handle %d\n",
3419 path_str, target->handle);
3422 * Issue a READ CAPACITY 16 command for the LUN.
3423 * The mpssas_read_cap_done function will load
3424 * the read cap info into the LUN struct.
3426 rcap_buf = malloc(sizeof(struct scsi_read_capacity_eedp),
3427 M_MPT2, M_NOWAIT | M_ZERO);
3428 if (rcap_buf == NULL) {
3429 mps_dprint(sc, MPS_FAULT,
3430 "Unable to alloc read capacity buffer for EEDP support.\n");
3431 xpt_free_path(ccb->ccb_h.path);
3435 xpt_setup_ccb(&ccb->ccb_h, local_path, CAM_PRIORITY_XPT);
3437 csio->ccb_h.func_code = XPT_SCSI_IO;
3438 csio->ccb_h.flags = CAM_DIR_IN;
3439 csio->ccb_h.retry_count = 4;
3440 csio->ccb_h.cbfcnp = mpssas_read_cap_done;
3441 csio->ccb_h.timeout = 60000;
3442 csio->data_ptr = (uint8_t *)rcap_buf;
3443 csio->dxfer_len = sizeof(struct scsi_read_capacity_eedp);
3444 csio->sense_len = MPS_SENSE_LEN;
3445 csio->cdb_len = sizeof(*scsi_cmd);
3446 csio->tag_action = MSG_SIMPLE_Q_TAG;
3448 scsi_cmd = (struct scsi_read_capacity_16 *)&csio->cdb_io.cdb_bytes;
3449 bzero(scsi_cmd, sizeof(*scsi_cmd));
3450 scsi_cmd->opcode = 0x9E;
3451 scsi_cmd->service_action = SRC16_SERVICE_ACTION;
3452 ((uint8_t *)scsi_cmd)[13] = sizeof(struct scsi_read_capacity_eedp);
3454 ccb->ccb_h.ppriv_ptr1 = sassc;
3459 mpssas_read_cap_done(struct cam_periph *periph, union ccb *done_ccb)
3461 struct mpssas_softc *sassc;
3462 struct mpssas_target *target;
3463 struct mpssas_lun *lun;
3464 struct scsi_read_capacity_eedp *rcap_buf;
3466 if (done_ccb == NULL)
3469 /* Driver need to release devq, it Scsi command is
3470 * generated by driver internally.
3471 * Currently there is a single place where driver
3472 * calls scsi command internally. In future if driver
3473 * calls more scsi command internally, it needs to release
3474 * devq internally, since those command will not go back to
3477 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) ) {
3478 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
3479 xpt_release_devq(done_ccb->ccb_h.path,
3480 /*count*/ 1, /*run_queue*/TRUE);
3483 rcap_buf = (struct scsi_read_capacity_eedp *)done_ccb->csio.data_ptr;
3486 * Get the LUN ID for the path and look it up in the LUN list for the
3489 sassc = (struct mpssas_softc *)done_ccb->ccb_h.ppriv_ptr1;
3490 KASSERT(done_ccb->ccb_h.target_id < sassc->maxtargets,
3491 ("Target %d out of bounds in mpssas_read_cap_done\n",
3492 done_ccb->ccb_h.target_id));
3493 target = &sassc->targets[done_ccb->ccb_h.target_id];
3494 SLIST_FOREACH(lun, &target->luns, lun_link) {
3495 if (lun->lun_id != done_ccb->ccb_h.target_lun)
3499 * Got the LUN in the target's LUN list. Fill it in
3500 * with EEDP info. If the READ CAP 16 command had some
3501 * SCSI error (common if command is not supported), mark
3502 * the lun as not supporting EEDP and set the block size
3505 if ((mpssas_get_ccbstatus(done_ccb) != CAM_REQ_CMP)
3506 || (done_ccb->csio.scsi_status != SCSI_STATUS_OK)) {
3507 lun->eedp_formatted = FALSE;
3508 lun->eedp_block_size = 0;
3512 if (rcap_buf->protect & 0x01) {
3513 mps_dprint(sassc->sc, MPS_INFO, "LUN %d for "
3514 "target ID %d is formatted for EEDP "
3515 "support.\n", done_ccb->ccb_h.target_lun,
3516 done_ccb->ccb_h.target_id);
3517 lun->eedp_formatted = TRUE;
3518 lun->eedp_block_size = scsi_4btoul(rcap_buf->length);
3523 // Finished with this CCB and path.
3524 free(rcap_buf, M_MPT2);
3525 xpt_free_path(done_ccb->ccb_h.path);
3526 xpt_free_ccb(done_ccb);
3528 #endif /* (__FreeBSD_version < 901503) || \
3529 ((__FreeBSD_version >= 1000000) && (__FreeBSD_version < 1000006)) */
3532 mpssas_prepare_for_tm(struct mps_softc *sc, struct mps_command *tm,
3533 struct mpssas_target *target, lun_id_t lun_id)
3539 * Set the INRESET flag for this target so that no I/O will be sent to
3540 * the target until the reset has completed. If an I/O request does
3541 * happen, the devq will be frozen. The CCB holds the path which is
3542 * used to release the devq. The devq is released and the CCB is freed
3543 * when the TM completes.
3545 ccb = xpt_alloc_ccb_nowait();
3547 path_id = cam_sim_path(sc->sassc->sim);
3548 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, path_id,
3549 target->tid, lun_id) != CAM_REQ_CMP) {
3553 tm->cm_targ = target;
3554 target->flags |= MPSSAS_TARGET_INRESET;
3560 mpssas_startup(struct mps_softc *sc)
3564 * Send the port enable message and set the wait_for_port_enable flag.
3565 * This flag helps to keep the simq frozen until all discovery events
3568 sc->wait_for_port_enable = 1;
3569 mpssas_send_portenable(sc);
3574 mpssas_send_portenable(struct mps_softc *sc)
3576 MPI2_PORT_ENABLE_REQUEST *request;
3577 struct mps_command *cm;
3581 if ((cm = mps_alloc_command(sc)) == NULL)
3583 request = (MPI2_PORT_ENABLE_REQUEST *)cm->cm_req;
3584 request->Function = MPI2_FUNCTION_PORT_ENABLE;
3585 request->MsgFlags = 0;
3587 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3588 cm->cm_complete = mpssas_portenable_complete;
3592 mps_map_command(sc, cm);
3593 mps_dprint(sc, MPS_XINFO,
3594 "mps_send_portenable finished cm %p req %p complete %p\n",
3595 cm, cm->cm_req, cm->cm_complete);
3600 mpssas_portenable_complete(struct mps_softc *sc, struct mps_command *cm)
3602 MPI2_PORT_ENABLE_REPLY *reply;
3603 struct mpssas_softc *sassc;
3609 * Currently there should be no way we can hit this case. It only
3610 * happens when we have a failure to allocate chain frames, and
3611 * port enable commands don't have S/G lists.
3613 if ((cm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
3614 mps_dprint(sc, MPS_ERROR, "%s: cm_flags = %#x for port enable! "
3615 "This should not happen!\n", __func__, cm->cm_flags);
3618 reply = (MPI2_PORT_ENABLE_REPLY *)cm->cm_reply;
3620 mps_dprint(sc, MPS_FAULT, "Portenable NULL reply\n");
3621 else if (le16toh(reply->IOCStatus & MPI2_IOCSTATUS_MASK) !=
3622 MPI2_IOCSTATUS_SUCCESS)
3623 mps_dprint(sc, MPS_FAULT, "Portenable failed\n");
3625 mps_free_command(sc, cm);
3626 if (sc->mps_ich.ich_arg != NULL) {
3627 mps_dprint(sc, MPS_XINFO, "disestablish config intrhook\n");
3628 config_intrhook_disestablish(&sc->mps_ich);
3629 sc->mps_ich.ich_arg = NULL;
3633 * Get WarpDrive info after discovery is complete but before the scan
3634 * starts. At this point, all devices are ready to be exposed to the
3635 * OS. If devices should be hidden instead, take them out of the
3636 * 'targets' array before the scan. The devinfo for a disk will have
3637 * some info and a volume's will be 0. Use that to remove disks.
3639 mps_wd_config_pages(sc);
3642 * Done waiting for port enable to complete. Decrement the refcount.
3643 * If refcount is 0, discovery is complete and a rescan of the bus can
3644 * take place. Since the simq was explicitly frozen before port
3645 * enable, it must be explicitly released here to keep the
3646 * freeze/release count in sync.
3648 sc->wait_for_port_enable = 0;
3649 sc->port_enable_complete = 1;
3650 wakeup(&sc->port_enable_complete);
3651 mpssas_startup_decrement(sassc);
3655 mpssas_check_id(struct mpssas_softc *sassc, int id)
3657 struct mps_softc *sc = sassc->sc;
3661 ids = &sc->exclude_ids[0];
3662 while((name = strsep(&ids, ",")) != NULL) {
3663 if (name[0] == '\0')
3665 if (strtol(name, NULL, 0) == (long)id)
3673 mpssas_realloc_targets(struct mps_softc *sc, int maxtargets)
3675 struct mpssas_softc *sassc;
3676 struct mpssas_lun *lun, *lun_tmp;
3677 struct mpssas_target *targ;
3682 * The number of targets is based on IOC Facts, so free all of
3683 * the allocated LUNs for each target and then the target buffer
3686 for (i=0; i< maxtargets; i++) {
3687 targ = &sassc->targets[i];
3688 SLIST_FOREACH_SAFE(lun, &targ->luns, lun_link, lun_tmp) {
3692 free(sassc->targets, M_MPT2);
3694 sassc->targets = malloc(sizeof(struct mpssas_target) * maxtargets,
3695 M_MPT2, M_WAITOK|M_ZERO);
3696 if (!sassc->targets) {
3697 panic("%s failed to alloc targets with error %d\n",