2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Yahoo! Inc.
5 * Copyright (c) 2011-2015 LSI Corp.
6 * Copyright (c) 2013-2015 Avago Technologies
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Avago Technologies (LSI) MPT-Fusion Host Adapter FreeBSD
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 /* Communications core for Avago Technologies (LSI) MPT2 */
40 /* TODO Move headers to mpsvar */
41 #include <sys/types.h>
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/selinfo.h>
46 #include <sys/module.h>
50 #include <sys/malloc.h>
52 #include <sys/sysctl.h>
53 #include <sys/endian.h>
54 #include <sys/queue.h>
55 #include <sys/kthread.h>
56 #include <sys/taskqueue.h>
59 #include <machine/bus.h>
60 #include <machine/resource.h>
63 #include <machine/stdarg.h>
66 #include <cam/cam_ccb.h>
67 #include <cam/cam_xpt.h>
68 #include <cam/cam_debug.h>
69 #include <cam/cam_sim.h>
70 #include <cam/cam_xpt_sim.h>
71 #include <cam/cam_xpt_periph.h>
72 #include <cam/cam_periph.h>
73 #include <cam/scsi/scsi_all.h>
74 #include <cam/scsi/scsi_message.h>
75 #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 static void mpssas_smpio_complete(struct mps_softc *sc, struct mps_command *cm);
133 static void mpssas_send_smpcmd(struct mpssas_softc *sassc, union ccb *ccb,
135 static void mpssas_action_smpio(struct mpssas_softc *sassc, union ccb *ccb);
136 static void mpssas_resetdev_complete(struct mps_softc *, struct mps_command *);
137 static void mpssas_async(void *callback_arg, uint32_t code,
138 struct cam_path *path, void *arg);
139 static int mpssas_send_portenable(struct mps_softc *sc);
140 static void mpssas_portenable_complete(struct mps_softc *sc,
141 struct mps_command *cm);
143 struct mpssas_target *
144 mpssas_find_target_by_handle(struct mpssas_softc *sassc, int start, uint16_t handle)
146 struct mpssas_target *target;
149 for (i = start; i < sassc->maxtargets; i++) {
150 target = &sassc->targets[i];
151 if (target->handle == handle)
158 /* we need to freeze the simq during attach and diag reset, to avoid failing
159 * commands before device handles have been found by discovery. Since
160 * discovery involves reading config pages and possibly sending commands,
161 * discovery actions may continue even after we receive the end of discovery
162 * event, so refcount discovery actions instead of assuming we can unfreeze
163 * the simq when we get the event.
166 mpssas_startup_increment(struct mpssas_softc *sassc)
168 MPS_FUNCTRACE(sassc->sc);
170 if ((sassc->flags & MPSSAS_IN_STARTUP) != 0) {
171 if (sassc->startup_refcount++ == 0) {
172 /* just starting, freeze the simq */
173 mps_dprint(sassc->sc, MPS_INIT,
174 "%s freezing simq\n", __func__);
176 xpt_freeze_simq(sassc->sim, 1);
178 mps_dprint(sassc->sc, MPS_INIT, "%s refcount %u\n", __func__,
179 sassc->startup_refcount);
184 mpssas_release_simq_reinit(struct mpssas_softc *sassc)
186 if (sassc->flags & MPSSAS_QUEUE_FROZEN) {
187 sassc->flags &= ~MPSSAS_QUEUE_FROZEN;
188 xpt_release_simq(sassc->sim, 1);
189 mps_dprint(sassc->sc, MPS_INFO, "Unfreezing SIM queue\n");
194 mpssas_startup_decrement(struct mpssas_softc *sassc)
196 MPS_FUNCTRACE(sassc->sc);
198 if ((sassc->flags & MPSSAS_IN_STARTUP) != 0) {
199 if (--sassc->startup_refcount == 0) {
200 /* finished all discovery-related actions, release
201 * the simq and rescan for the latest topology.
203 mps_dprint(sassc->sc, MPS_INIT,
204 "%s releasing simq\n", __func__);
205 sassc->flags &= ~MPSSAS_IN_STARTUP;
206 xpt_release_simq(sassc->sim, 1);
209 mps_dprint(sassc->sc, MPS_INIT, "%s refcount %u\n", __func__,
210 sassc->startup_refcount);
215 * The firmware requires us to stop sending commands when we're doing task
217 * XXX The logic for serializing the device has been made lazy and moved to
218 * mpssas_prepare_for_tm().
221 mpssas_alloc_tm(struct mps_softc *sc)
223 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
224 struct mps_command *tm;
226 tm = mps_alloc_high_priority_command(sc);
230 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
231 req->Function = MPI2_FUNCTION_SCSI_TASK_MGMT;
236 mpssas_free_tm(struct mps_softc *sc, struct mps_command *tm)
238 int target_id = 0xFFFFFFFF;
244 * For TM's the devq is frozen for the device. Unfreeze it here and
245 * free the resources used for freezing the devq. Must clear the
246 * INRESET flag as well or scsi I/O will not work.
248 if (tm->cm_targ != NULL) {
249 tm->cm_targ->flags &= ~MPSSAS_TARGET_INRESET;
250 target_id = tm->cm_targ->tid;
253 mps_dprint(sc, MPS_INFO, "Unfreezing devq for target ID %d\n",
255 xpt_release_devq(tm->cm_ccb->ccb_h.path, 1, TRUE);
256 xpt_free_path(tm->cm_ccb->ccb_h.path);
257 xpt_free_ccb(tm->cm_ccb);
260 mps_free_high_priority_command(sc, tm);
264 mpssas_rescan_target(struct mps_softc *sc, struct mpssas_target *targ)
266 struct mpssas_softc *sassc = sc->sassc;
268 target_id_t targetid;
272 pathid = cam_sim_path(sassc->sim);
274 targetid = CAM_TARGET_WILDCARD;
276 targetid = targ - sassc->targets;
279 * Allocate a CCB and schedule a rescan.
281 ccb = xpt_alloc_ccb_nowait();
283 mps_dprint(sc, MPS_ERROR, "unable to alloc CCB for rescan\n");
287 if (xpt_create_path(&ccb->ccb_h.path, NULL, pathid,
288 targetid, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
289 mps_dprint(sc, MPS_ERROR, "unable to create path for rescan\n");
294 if (targetid == CAM_TARGET_WILDCARD)
295 ccb->ccb_h.func_code = XPT_SCAN_BUS;
297 ccb->ccb_h.func_code = XPT_SCAN_TGT;
299 mps_dprint(sc, MPS_TRACE, "%s targetid %u\n", __func__, targetid);
304 mpssas_log_command(struct mps_command *cm, u_int level, const char *fmt, ...)
314 /* No need to be in here if debugging isn't enabled */
315 if ((cm->cm_sc->mps_debug & level) == 0)
318 sbuf_new(&sb, str, sizeof(str), 0);
322 if (cm->cm_ccb != NULL) {
323 xpt_path_string(cm->cm_ccb->csio.ccb_h.path, path_str,
325 sbuf_cat(&sb, path_str);
326 if (cm->cm_ccb->ccb_h.func_code == XPT_SCSI_IO) {
327 scsi_command_string(&cm->cm_ccb->csio, &sb);
328 sbuf_printf(&sb, "length %d ",
329 cm->cm_ccb->csio.dxfer_len);
333 sbuf_printf(&sb, "(noperiph:%s%d:%u:%u:%u): ",
334 cam_sim_name(cm->cm_sc->sassc->sim),
335 cam_sim_unit(cm->cm_sc->sassc->sim),
336 cam_sim_bus(cm->cm_sc->sassc->sim),
337 cm->cm_targ ? cm->cm_targ->tid : 0xFFFFFFFF,
341 sbuf_printf(&sb, "SMID %u ", cm->cm_desc.Default.SMID);
342 sbuf_vprintf(&sb, fmt, ap);
344 mps_print_field(cm->cm_sc, "%s", sbuf_data(&sb));
350 mpssas_remove_volume(struct mps_softc *sc, struct mps_command *tm)
352 MPI2_SCSI_TASK_MANAGE_REPLY *reply;
353 struct mpssas_target *targ;
358 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
359 handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
363 /* XXX retry the remove after the diag reset completes? */
364 mps_dprint(sc, MPS_FAULT,
365 "%s NULL reply resetting device 0x%04x\n", __func__,
367 mpssas_free_tm(sc, tm);
371 if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
372 MPI2_IOCSTATUS_SUCCESS) {
373 mps_dprint(sc, MPS_ERROR,
374 "IOCStatus = 0x%x while resetting device 0x%x\n",
375 le16toh(reply->IOCStatus), handle);
378 mps_dprint(sc, MPS_XINFO,
379 "Reset aborted %u commands\n", reply->TerminationCount);
380 mps_free_reply(sc, tm->cm_reply_data);
381 tm->cm_reply = NULL; /* Ensures the reply won't get re-freed */
383 mps_dprint(sc, MPS_XINFO,
384 "clearing target %u handle 0x%04x\n", targ->tid, handle);
387 * Don't clear target if remove fails because things will get confusing.
388 * Leave the devname and sasaddr intact so that we know to avoid reusing
389 * this target id if possible, and so we can assign the same target id
390 * to this device if it comes back in the future.
392 if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) ==
393 MPI2_IOCSTATUS_SUCCESS) {
396 targ->encl_handle = 0x0;
397 targ->encl_slot = 0x0;
398 targ->exp_dev_handle = 0x0;
400 targ->linkrate = 0x0;
405 mpssas_free_tm(sc, tm);
409 * No Need to call "MPI2_SAS_OP_REMOVE_DEVICE" For Volume removal.
410 * Otherwise Volume Delete is same as Bare Drive Removal.
413 mpssas_prepare_volume_remove(struct mpssas_softc *sassc, uint16_t handle)
415 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
416 struct mps_softc *sc;
417 struct mps_command *tm;
418 struct mpssas_target *targ = NULL;
420 MPS_FUNCTRACE(sassc->sc);
425 * If this is a WD controller, determine if the disk should be exposed
426 * to the OS or not. If disk should be exposed, return from this
427 * function without doing anything.
429 if (sc->WD_available && (sc->WD_hide_expose ==
430 MPS_WD_EXPOSE_ALWAYS)) {
435 targ = mpssas_find_target_by_handle(sassc, 0, handle);
437 /* FIXME: what is the action? */
438 /* We don't know about this device? */
439 mps_dprint(sc, MPS_ERROR,
440 "%s %d : invalid handle 0x%x \n", __func__,__LINE__, handle);
444 targ->flags |= MPSSAS_TARGET_INREMOVAL;
446 tm = mpssas_alloc_tm(sc);
448 mps_dprint(sc, MPS_ERROR,
449 "%s: command alloc failure\n", __func__);
453 mpssas_rescan_target(sc, targ);
455 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
456 req->DevHandle = targ->handle;
457 req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
459 /* SAS Hard Link Reset / SATA Link Reset */
460 req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
464 tm->cm_complete = mpssas_remove_volume;
465 tm->cm_complete_data = (void *)(uintptr_t)handle;
467 mps_dprint(sc, MPS_INFO, "%s: Sending reset for target ID %d\n",
468 __func__, targ->tid);
469 mpssas_prepare_for_tm(sc, tm, targ, CAM_LUN_WILDCARD);
471 mps_map_command(sc, tm);
475 * The MPT2 firmware performs debounce on the link to avoid transient link
476 * errors and false removals. When it does decide that link has been lost
477 * and a device need to go away, it expects that the host will perform a
478 * target reset and then an op remove. The reset has the side-effect of
479 * aborting any outstanding requests for the device, which is required for
480 * the op-remove to succeed. It's not clear if the host should check for
481 * the device coming back alive after the reset.
484 mpssas_prepare_remove(struct mpssas_softc *sassc, uint16_t handle)
486 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
487 struct mps_softc *sc;
488 struct mps_command *cm;
489 struct mpssas_target *targ = NULL;
491 MPS_FUNCTRACE(sassc->sc);
495 targ = mpssas_find_target_by_handle(sassc, 0, handle);
497 /* FIXME: what is the action? */
498 /* We don't know about this device? */
499 mps_dprint(sc, MPS_ERROR,
500 "%s : invalid handle 0x%x \n", __func__, handle);
504 targ->flags |= MPSSAS_TARGET_INREMOVAL;
506 cm = mpssas_alloc_tm(sc);
508 mps_dprint(sc, MPS_ERROR,
509 "%s: command alloc failure\n", __func__);
513 mpssas_rescan_target(sc, targ);
515 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req;
516 req->DevHandle = htole16(targ->handle);
517 req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
519 /* SAS Hard Link Reset / SATA Link Reset */
520 req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
524 cm->cm_complete = mpssas_remove_device;
525 cm->cm_complete_data = (void *)(uintptr_t)handle;
527 mps_dprint(sc, MPS_INFO, "%s: Sending reset for target ID %d\n",
528 __func__, targ->tid);
529 mpssas_prepare_for_tm(sc, cm, targ, CAM_LUN_WILDCARD);
531 mps_map_command(sc, cm);
535 mpssas_remove_device(struct mps_softc *sc, struct mps_command *tm)
537 MPI2_SCSI_TASK_MANAGE_REPLY *reply;
538 MPI2_SAS_IOUNIT_CONTROL_REQUEST *req;
539 struct mpssas_target *targ;
544 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
545 handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
549 * Currently there should be no way we can hit this case. It only
550 * happens when we have a failure to allocate chain frames, and
551 * task management commands don't have S/G lists.
553 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
554 mps_dprint(sc, MPS_ERROR,
555 "%s: cm_flags = %#x for remove of handle %#04x! "
556 "This should not happen!\n", __func__, tm->cm_flags,
561 /* XXX retry the remove after the diag reset completes? */
562 mps_dprint(sc, MPS_FAULT,
563 "%s NULL reply resetting device 0x%04x\n", __func__,
565 mpssas_free_tm(sc, tm);
569 if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
570 MPI2_IOCSTATUS_SUCCESS) {
571 mps_dprint(sc, MPS_ERROR,
572 "IOCStatus = 0x%x while resetting device 0x%x\n",
573 le16toh(reply->IOCStatus), handle);
576 mps_dprint(sc, MPS_XINFO, "Reset aborted %u commands\n",
577 le32toh(reply->TerminationCount));
578 mps_free_reply(sc, tm->cm_reply_data);
579 tm->cm_reply = NULL; /* Ensures the reply won't get re-freed */
581 /* Reuse the existing command */
582 req = (MPI2_SAS_IOUNIT_CONTROL_REQUEST *)tm->cm_req;
583 memset(req, 0, sizeof(*req));
584 req->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
585 req->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
586 req->DevHandle = htole16(handle);
588 tm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
589 tm->cm_complete = mpssas_remove_complete;
590 tm->cm_complete_data = (void *)(uintptr_t)handle;
593 * Wait to send the REMOVE_DEVICE until all the commands have cleared.
594 * They should be aborted or time out and we'll kick thus off there
597 if (TAILQ_FIRST(&targ->commands) == NULL) {
598 mps_dprint(sc, MPS_INFO, "No pending commands: starting remove_device\n");
599 mps_map_command(sc, tm);
600 targ->pending_remove_tm = NULL;
602 targ->pending_remove_tm = tm;
605 mps_dprint(sc, MPS_XINFO, "clearing target %u handle 0x%04x\n",
610 mpssas_remove_complete(struct mps_softc *sc, struct mps_command *tm)
612 MPI2_SAS_IOUNIT_CONTROL_REPLY *reply;
614 struct mpssas_target *targ;
615 struct mpssas_lun *lun;
619 reply = (MPI2_SAS_IOUNIT_CONTROL_REPLY *)tm->cm_reply;
620 handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
624 * At this point, we should have no pending commands for the target.
625 * The remove target has just completed.
627 KASSERT(TAILQ_FIRST(&targ->commands) == NULL,
628 ("%s: no commands should be pending\n", __func__));
631 * Currently there should be no way we can hit this case. It only
632 * happens when we have a failure to allocate chain frames, and
633 * task management commands don't have S/G lists.
635 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
636 mps_dprint(sc, MPS_XINFO,
637 "%s: cm_flags = %#x for remove of handle %#04x! "
638 "This should not happen!\n", __func__, tm->cm_flags,
640 mpssas_free_tm(sc, tm);
645 /* most likely a chip reset */
646 mps_dprint(sc, MPS_FAULT,
647 "%s NULL reply removing device 0x%04x\n", __func__, handle);
648 mpssas_free_tm(sc, tm);
652 mps_dprint(sc, MPS_XINFO,
653 "%s on handle 0x%04x, IOCStatus= 0x%x\n", __func__,
654 handle, le16toh(reply->IOCStatus));
657 * Don't clear target if remove fails because things will get confusing.
658 * Leave the devname and sasaddr intact so that we know to avoid reusing
659 * this target id if possible, and so we can assign the same target id
660 * to this device if it comes back in the future.
662 if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) ==
663 MPI2_IOCSTATUS_SUCCESS) {
665 targ->encl_handle = 0x0;
666 targ->encl_slot = 0x0;
667 targ->exp_dev_handle = 0x0;
669 targ->linkrate = 0x0;
673 while(!SLIST_EMPTY(&targ->luns)) {
674 lun = SLIST_FIRST(&targ->luns);
675 SLIST_REMOVE_HEAD(&targ->luns, lun_link);
680 mpssas_free_tm(sc, tm);
684 mpssas_register_events(struct mps_softc *sc)
686 u32 events[MPI2_EVENT_NOTIFY_EVENTMASK_WORDS];
689 setbit(events, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
690 setbit(events, MPI2_EVENT_SAS_DISCOVERY);
691 setbit(events, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
692 setbit(events, MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE);
693 setbit(events, MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW);
694 setbit(events, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
695 setbit(events, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
696 setbit(events, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
697 setbit(events, MPI2_EVENT_IR_VOLUME);
698 setbit(events, MPI2_EVENT_IR_PHYSICAL_DISK);
699 setbit(events, MPI2_EVENT_IR_OPERATION_STATUS);
700 setbit(events, MPI2_EVENT_LOG_ENTRY_ADDED);
702 mps_register_events(sc, events, mpssas_evt_handler, NULL,
703 &sc->sassc->mpssas_eh);
709 mps_attach_sas(struct mps_softc *sc)
711 struct mpssas_softc *sassc;
713 int unit, error = 0, reqs;
716 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__);
718 sassc = malloc(sizeof(struct mpssas_softc), M_MPT2, M_WAITOK|M_ZERO);
721 * XXX MaxTargets could change during a reinit. Since we don't
722 * resize the targets[] array during such an event, cache the value
723 * of MaxTargets here so that we don't get into trouble later. This
724 * should move into the reinit logic.
726 sassc->maxtargets = sc->facts->MaxTargets + sc->facts->MaxVolumes;
727 sassc->targets = malloc(sizeof(struct mpssas_target) *
728 sassc->maxtargets, M_MPT2, M_WAITOK|M_ZERO);
732 reqs = sc->num_reqs - sc->num_prireqs - 1;
733 if ((sassc->devq = cam_simq_alloc(reqs)) == NULL) {
734 mps_dprint(sc, MPS_ERROR, "Cannot allocate SIMQ\n");
739 unit = device_get_unit(sc->mps_dev);
740 sassc->sim = cam_sim_alloc(mpssas_action, mpssas_poll, "mps", sassc,
741 unit, &sc->mps_mtx, reqs, reqs, sassc->devq);
742 if (sassc->sim == NULL) {
743 mps_dprint(sc, MPS_INIT|MPS_ERROR, "Cannot allocate SIM\n");
748 TAILQ_INIT(&sassc->ev_queue);
750 /* Initialize taskqueue for Event Handling */
751 TASK_INIT(&sassc->ev_task, 0, mpssas_firmware_event_work, sc);
752 sassc->ev_tq = taskqueue_create("mps_taskq", M_NOWAIT | M_ZERO,
753 taskqueue_thread_enqueue, &sassc->ev_tq);
754 taskqueue_start_threads(&sassc->ev_tq, 1, PRIBIO, "%s taskq",
755 device_get_nameunit(sc->mps_dev));
760 * XXX There should be a bus for every port on the adapter, but since
761 * we're just going to fake the topology for now, we'll pretend that
762 * everything is just a target on a single bus.
764 if ((error = xpt_bus_register(sassc->sim, sc->mps_dev, 0)) != 0) {
765 mps_dprint(sc, MPS_INIT|MPS_ERROR,
766 "Error %d registering SCSI bus\n", error);
772 * Assume that discovery events will start right away.
774 * Hold off boot until discovery is complete.
776 sassc->flags |= MPSSAS_IN_STARTUP | MPSSAS_IN_DISCOVERY;
777 sc->sassc->startup_refcount = 0;
778 mpssas_startup_increment(sassc);
780 callout_init(&sassc->discovery_callout, 1 /*mpsafe*/);
783 * Register for async events so we can determine the EEDP
784 * capabilities of devices.
786 status = xpt_create_path(&sassc->path, /*periph*/NULL,
787 cam_sim_path(sc->sassc->sim), CAM_TARGET_WILDCARD,
789 if (status != CAM_REQ_CMP) {
790 mps_dprint(sc, MPS_ERROR|MPS_INIT,
791 "Error %#x creating sim path\n", status);
796 event = AC_ADVINFO_CHANGED;
797 status = xpt_register_async(event, mpssas_async, sc,
799 if (status != CAM_REQ_CMP) {
800 mps_dprint(sc, MPS_ERROR,
801 "Error %#x registering async handler for "
802 "AC_ADVINFO_CHANGED events\n", status);
803 xpt_free_path(sassc->path);
807 if (status != CAM_REQ_CMP) {
809 * EEDP use is the exception, not the rule.
810 * Warn the user, but do not fail to attach.
812 mps_printf(sc, "EEDP capabilities disabled.\n");
817 mpssas_register_events(sc);
822 mps_dprint(sc, MPS_INIT, "%s exit error= %d\n", __func__, error);
827 mps_detach_sas(struct mps_softc *sc)
829 struct mpssas_softc *sassc;
830 struct mpssas_lun *lun, *lun_tmp;
831 struct mpssas_target *targ;
836 if (sc->sassc == NULL)
840 mps_deregister_events(sc, sassc->mpssas_eh);
843 * Drain and free the event handling taskqueue with the lock
844 * unheld so that any parallel processing tasks drain properly
845 * without deadlocking.
847 if (sassc->ev_tq != NULL)
848 taskqueue_free(sassc->ev_tq);
850 /* Make sure CAM doesn't wedge if we had to bail out early. */
853 while (sassc->startup_refcount != 0)
854 mpssas_startup_decrement(sassc);
856 /* Deregister our async handler */
857 if (sassc->path != NULL) {
858 xpt_register_async(0, mpssas_async, sc, sassc->path);
859 xpt_free_path(sassc->path);
863 if (sassc->flags & MPSSAS_IN_STARTUP)
864 xpt_release_simq(sassc->sim, 1);
866 if (sassc->sim != NULL) {
867 xpt_bus_deregister(cam_sim_path(sassc->sim));
868 cam_sim_free(sassc->sim, FALSE);
873 if (sassc->devq != NULL)
874 cam_simq_free(sassc->devq);
876 for(i=0; i< sassc->maxtargets ;i++) {
877 targ = &sassc->targets[i];
878 SLIST_FOREACH_SAFE(lun, &targ->luns, lun_link, lun_tmp) {
882 free(sassc->targets, M_MPT2);
890 mpssas_discovery_end(struct mpssas_softc *sassc)
892 struct mps_softc *sc = sassc->sc;
896 if (sassc->flags & MPSSAS_DISCOVERY_TIMEOUT_PENDING)
897 callout_stop(&sassc->discovery_callout);
900 * After discovery has completed, check the mapping table for any
901 * missing devices and update their missing counts. Only do this once
902 * whenever the driver is initialized so that missing counts aren't
903 * updated unnecessarily. Note that just because discovery has
904 * completed doesn't mean that events have been processed yet. The
905 * check_devices function is a callout timer that checks if ALL devices
906 * are missing. If so, it will wait a little longer for events to
907 * complete and keep resetting itself until some device in the mapping
908 * table is not missing, meaning that event processing has started.
910 if (sc->track_mapping_events) {
911 mps_dprint(sc, MPS_XINFO | MPS_MAPPING, "Discovery has "
912 "completed. Check for missing devices in the mapping "
914 callout_reset(&sc->device_check_callout,
915 MPS_MISSING_CHECK_DELAY * hz, mps_mapping_check_devices,
921 mpssas_action(struct cam_sim *sim, union ccb *ccb)
923 struct mpssas_softc *sassc;
925 sassc = cam_sim_softc(sim);
927 MPS_FUNCTRACE(sassc->sc);
928 mps_dprint(sassc->sc, MPS_TRACE, "ccb func_code 0x%x\n",
929 ccb->ccb_h.func_code);
930 mtx_assert(&sassc->sc->mps_mtx, MA_OWNED);
932 switch (ccb->ccb_h.func_code) {
935 struct ccb_pathinq *cpi = &ccb->cpi;
936 struct mps_softc *sc = sassc->sc;
938 cpi->version_num = 1;
939 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
940 cpi->target_sprt = 0;
941 cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED | PIM_NOSCAN;
942 cpi->hba_eng_cnt = 0;
943 cpi->max_target = sassc->maxtargets - 1;
947 * initiator_id is set here to an ID outside the set of valid
948 * target IDs (including volumes).
950 cpi->initiator_id = sassc->maxtargets;
951 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
952 strlcpy(cpi->hba_vid, "Avago Tech", HBA_IDLEN);
953 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
954 cpi->unit_number = cam_sim_unit(sim);
955 cpi->bus_id = cam_sim_bus(sim);
956 cpi->base_transfer_speed = 150000;
957 cpi->transport = XPORT_SAS;
958 cpi->transport_version = 0;
959 cpi->protocol = PROTO_SCSI;
960 cpi->protocol_version = SCSI_REV_SPC;
961 cpi->maxio = sc->maxio;
962 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
965 case XPT_GET_TRAN_SETTINGS:
967 struct ccb_trans_settings *cts;
968 struct ccb_trans_settings_sas *sas;
969 struct ccb_trans_settings_scsi *scsi;
970 struct mpssas_target *targ;
973 sas = &cts->xport_specific.sas;
974 scsi = &cts->proto_specific.scsi;
976 KASSERT(cts->ccb_h.target_id < sassc->maxtargets,
977 ("Target %d out of bounds in XPT_GET_TRANS_SETTINGS\n",
978 cts->ccb_h.target_id));
979 targ = &sassc->targets[cts->ccb_h.target_id];
980 if (targ->handle == 0x0) {
981 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
985 cts->protocol_version = SCSI_REV_SPC2;
986 cts->transport = XPORT_SAS;
987 cts->transport_version = 0;
989 sas->valid = CTS_SAS_VALID_SPEED;
990 switch (targ->linkrate) {
992 sas->bitrate = 150000;
995 sas->bitrate = 300000;
998 sas->bitrate = 600000;
1004 cts->protocol = PROTO_SCSI;
1005 scsi->valid = CTS_SCSI_VALID_TQ;
1006 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
1008 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
1011 case XPT_CALC_GEOMETRY:
1012 cam_calc_geometry(&ccb->ccg, /*extended*/1);
1013 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
1016 mps_dprint(sassc->sc, MPS_XINFO, "mpssas_action XPT_RESET_DEV\n");
1017 mpssas_action_resetdev(sassc, ccb);
1022 mps_dprint(sassc->sc, MPS_XINFO,
1023 "mpssas_action faking success for abort or reset\n");
1024 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
1027 mpssas_action_scsiio(sassc, ccb);
1030 mpssas_action_smpio(sassc, ccb);
1033 mpssas_set_ccbstatus(ccb, CAM_FUNC_NOTAVAIL);
1041 mpssas_announce_reset(struct mps_softc *sc, uint32_t ac_code,
1042 target_id_t target_id, lun_id_t lun_id)
1044 path_id_t path_id = cam_sim_path(sc->sassc->sim);
1045 struct cam_path *path;
1047 mps_dprint(sc, MPS_XINFO, "%s code %x target %d lun %jx\n", __func__,
1048 ac_code, target_id, (uintmax_t)lun_id);
1050 if (xpt_create_path(&path, NULL,
1051 path_id, target_id, lun_id) != CAM_REQ_CMP) {
1052 mps_dprint(sc, MPS_ERROR, "unable to create path for reset "
1057 xpt_async(ac_code, path, NULL);
1058 xpt_free_path(path);
1062 mpssas_complete_all_commands(struct mps_softc *sc)
1064 struct mps_command *cm;
1069 mtx_assert(&sc->mps_mtx, MA_OWNED);
1071 /* complete all commands with a NULL reply */
1072 for (i = 1; i < sc->num_reqs; i++) {
1073 cm = &sc->commands[i];
1074 if (cm->cm_state == MPS_CM_STATE_FREE)
1077 cm->cm_state = MPS_CM_STATE_BUSY;
1078 cm->cm_reply = NULL;
1081 if (cm->cm_flags & MPS_CM_FLAGS_SATA_ID_TIMEOUT) {
1083 free(cm->cm_data, M_MPT2);
1087 if (cm->cm_flags & MPS_CM_FLAGS_POLLED)
1088 cm->cm_flags |= MPS_CM_FLAGS_COMPLETE;
1090 if (cm->cm_complete != NULL) {
1091 mpssas_log_command(cm, MPS_RECOVERY,
1092 "completing cm %p state %x ccb %p for diag reset\n",
1093 cm, cm->cm_state, cm->cm_ccb);
1095 cm->cm_complete(sc, cm);
1097 } else if (cm->cm_flags & MPS_CM_FLAGS_WAKEUP) {
1098 mpssas_log_command(cm, MPS_RECOVERY,
1099 "waking up cm %p state %x ccb %p for diag reset\n",
1100 cm, cm->cm_state, cm->cm_ccb);
1105 if ((completed == 0) && (cm->cm_state != MPS_CM_STATE_FREE)) {
1106 /* this should never happen, but if it does, log */
1107 mpssas_log_command(cm, MPS_RECOVERY,
1108 "cm %p state %x flags 0x%x ccb %p during diag "
1109 "reset\n", cm, cm->cm_state, cm->cm_flags,
1114 sc->io_cmds_active = 0;
1118 mpssas_handle_reinit(struct mps_softc *sc)
1122 /* Go back into startup mode and freeze the simq, so that CAM
1123 * doesn't send any commands until after we've rediscovered all
1124 * targets and found the proper device handles for them.
1126 * After the reset, portenable will trigger discovery, and after all
1127 * discovery-related activities have finished, the simq will be
1130 mps_dprint(sc, MPS_INIT, "%s startup\n", __func__);
1131 sc->sassc->flags |= MPSSAS_IN_STARTUP;
1132 sc->sassc->flags |= MPSSAS_IN_DISCOVERY;
1133 mpssas_startup_increment(sc->sassc);
1135 /* notify CAM of a bus reset */
1136 mpssas_announce_reset(sc, AC_BUS_RESET, CAM_TARGET_WILDCARD,
1139 /* complete and cleanup after all outstanding commands */
1140 mpssas_complete_all_commands(sc);
1142 mps_dprint(sc, MPS_INIT,
1143 "%s startup %u after command completion\n", __func__,
1144 sc->sassc->startup_refcount);
1146 /* zero all the target handles, since they may change after the
1147 * reset, and we have to rediscover all the targets and use the new
1150 for (i = 0; i < sc->sassc->maxtargets; i++) {
1151 if (sc->sassc->targets[i].outstanding != 0)
1152 mps_dprint(sc, MPS_INIT, "target %u outstanding %u\n",
1153 i, sc->sassc->targets[i].outstanding);
1154 sc->sassc->targets[i].handle = 0x0;
1155 sc->sassc->targets[i].exp_dev_handle = 0x0;
1156 sc->sassc->targets[i].outstanding = 0;
1157 sc->sassc->targets[i].flags = MPSSAS_TARGET_INDIAGRESET;
1162 mpssas_tm_timeout(void *data)
1164 struct mps_command *tm = data;
1165 struct mps_softc *sc = tm->cm_sc;
1167 mtx_assert(&sc->mps_mtx, MA_OWNED);
1169 mpssas_log_command(tm, MPS_INFO|MPS_RECOVERY,
1170 "task mgmt %p timed out\n", tm);
1172 KASSERT(tm->cm_state == MPS_CM_STATE_INQUEUE,
1173 ("command not inqueue\n"));
1175 tm->cm_state = MPS_CM_STATE_BUSY;
1180 mpssas_logical_unit_reset_complete(struct mps_softc *sc, struct mps_command *tm)
1182 MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1183 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1184 unsigned int cm_count = 0;
1185 struct mps_command *cm;
1186 struct mpssas_target *targ;
1188 callout_stop(&tm->cm_callout);
1190 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1191 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1195 * Currently there should be no way we can hit this case. It only
1196 * happens when we have a failure to allocate chain frames, and
1197 * task management commands don't have S/G lists.
1198 * XXXSL So should it be an assertion?
1200 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
1201 mps_dprint(sc, MPS_RECOVERY|MPS_ERROR,
1202 "%s: cm_flags = %#x for LUN reset! "
1203 "This should not happen!\n", __func__, tm->cm_flags);
1204 mpssas_free_tm(sc, tm);
1208 if (reply == NULL) {
1209 mps_dprint(sc, MPS_RECOVERY, "NULL reset reply for tm %p\n",
1211 if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) {
1212 /* this completion was due to a reset, just cleanup */
1213 mps_dprint(sc, MPS_RECOVERY, "Hardware undergoing "
1214 "reset, ignoring NULL LUN reset reply\n");
1216 mpssas_free_tm(sc, tm);
1219 /* we should have gotten a reply. */
1220 mps_dprint(sc, MPS_INFO|MPS_RECOVERY, "NULL reply on "
1221 "LUN reset attempt, resetting controller\n");
1227 mps_dprint(sc, MPS_RECOVERY,
1228 "logical unit reset status 0x%x code 0x%x count %u\n",
1229 le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1230 le32toh(reply->TerminationCount));
1233 * See if there are any outstanding commands for this LUN.
1234 * This could be made more efficient by using a per-LU data
1235 * structure of some sort.
1237 TAILQ_FOREACH(cm, &targ->commands, cm_link) {
1238 if (cm->cm_lun == tm->cm_lun)
1242 if (cm_count == 0) {
1243 mps_dprint(sc, MPS_RECOVERY|MPS_INFO,
1244 "Finished recovery after LUN reset for target %u\n",
1247 mpssas_announce_reset(sc, AC_SENT_BDR, targ->tid, tm->cm_lun);
1250 * We've finished recovery for this logical unit. check and
1251 * see if some other logical unit has a timedout command
1252 * that needs to be processed.
1254 cm = TAILQ_FIRST(&targ->timedout_commands);
1256 mps_dprint(sc, MPS_INFO|MPS_RECOVERY,
1257 "More commands to abort for target %u\n",
1259 mpssas_send_abort(sc, tm, cm);
1262 mpssas_free_tm(sc, tm);
1266 * If we still have commands for this LUN, the reset
1267 * effectively failed, regardless of the status reported.
1268 * Escalate to a target reset.
1270 mps_dprint(sc, MPS_INFO|MPS_RECOVERY,
1271 "logical unit reset complete for target %u, but still "
1272 "have %u command(s), sending target reset\n", targ->tid,
1274 mpssas_send_reset(sc, tm,
1275 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
1280 mpssas_target_reset_complete(struct mps_softc *sc, struct mps_command *tm)
1282 MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1283 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1284 struct mpssas_target *targ;
1286 callout_stop(&tm->cm_callout);
1288 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1289 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1293 * Currently there should be no way we can hit this case. It only
1294 * happens when we have a failure to allocate chain frames, and
1295 * task management commands don't have S/G lists.
1297 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
1298 mps_dprint(sc, MPS_ERROR,"%s: cm_flags = %#x for target reset! "
1299 "This should not happen!\n", __func__, tm->cm_flags);
1300 mpssas_free_tm(sc, tm);
1304 if (reply == NULL) {
1305 mps_dprint(sc, MPS_RECOVERY,
1306 "NULL target reset reply for tm %pi TaskMID %u\n",
1307 tm, le16toh(req->TaskMID));
1308 if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) {
1309 /* this completion was due to a reset, just cleanup */
1310 mps_dprint(sc, MPS_RECOVERY, "Hardware undergoing "
1311 "reset, ignoring NULL target reset reply\n");
1313 mpssas_free_tm(sc, tm);
1315 /* we should have gotten a reply. */
1316 mps_dprint(sc, MPS_INFO|MPS_RECOVERY, "NULL reply on "
1317 "target reset attempt, resetting controller\n");
1323 mps_dprint(sc, MPS_RECOVERY,
1324 "target reset status 0x%x code 0x%x count %u\n",
1325 le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1326 le32toh(reply->TerminationCount));
1328 if (targ->outstanding == 0) {
1329 /* we've finished recovery for this target and all
1330 * of its logical units.
1332 mps_dprint(sc, MPS_RECOVERY|MPS_INFO,
1333 "Finished reset recovery for target %u\n", targ->tid);
1335 mpssas_announce_reset(sc, AC_SENT_BDR, tm->cm_targ->tid,
1339 mpssas_free_tm(sc, tm);
1342 * After a target reset, if this target still has
1343 * outstanding commands, the reset effectively failed,
1344 * regardless of the status reported. escalate.
1346 mps_dprint(sc, MPS_INFO|MPS_RECOVERY,
1347 "Target reset complete for target %u, but still have %u "
1348 "command(s), resetting controller\n", targ->tid,
1354 #define MPS_RESET_TIMEOUT 30
1357 mpssas_send_reset(struct mps_softc *sc, struct mps_command *tm, uint8_t type)
1359 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1360 struct mpssas_target *target;
1363 target = tm->cm_targ;
1364 if (target->handle == 0) {
1365 mps_dprint(sc, MPS_ERROR,"%s null devhandle for target_id %d\n",
1366 __func__, target->tid);
1370 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1371 req->DevHandle = htole16(target->handle);
1372 req->TaskType = type;
1374 if (type == MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET) {
1375 /* XXX Need to handle invalid LUNs */
1376 MPS_SET_LUN(req->LUN, tm->cm_lun);
1377 tm->cm_targ->logical_unit_resets++;
1378 mps_dprint(sc, MPS_RECOVERY|MPS_INFO,
1379 "Sending logical unit reset to target %u lun %d\n",
1380 target->tid, tm->cm_lun);
1381 tm->cm_complete = mpssas_logical_unit_reset_complete;
1382 mpssas_prepare_for_tm(sc, tm, target, tm->cm_lun);
1383 } else if (type == MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET) {
1385 * Target reset method =
1386 * SAS Hard Link Reset / SATA Link Reset
1388 req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
1389 tm->cm_targ->target_resets++;
1390 mps_dprint(sc, MPS_RECOVERY|MPS_INFO,
1391 "Sending target reset to target %u\n", target->tid);
1392 tm->cm_complete = mpssas_target_reset_complete;
1393 mpssas_prepare_for_tm(sc, tm, target, CAM_LUN_WILDCARD);
1395 mps_dprint(sc, MPS_ERROR, "unexpected reset type 0x%x\n", type);
1400 tm->cm_complete_data = (void *)tm;
1402 callout_reset(&tm->cm_callout, MPS_RESET_TIMEOUT * hz,
1403 mpssas_tm_timeout, tm);
1405 err = mps_map_command(sc, tm);
1407 mps_dprint(sc, MPS_ERROR|MPS_RECOVERY,
1408 "error %d sending reset type %u\n",
1415 mpssas_abort_complete(struct mps_softc *sc, struct mps_command *tm)
1417 struct mps_command *cm;
1418 MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1419 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1420 struct mpssas_target *targ;
1422 callout_stop(&tm->cm_callout);
1424 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1425 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1429 * Currently there should be no way we can hit this case. It only
1430 * happens when we have a failure to allocate chain frames, and
1431 * task management commands don't have S/G lists.
1433 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
1434 mps_dprint(sc, MPS_RECOVERY,
1435 "cm_flags = %#x for abort %p TaskMID %u!\n",
1436 tm->cm_flags, tm, le16toh(req->TaskMID));
1437 mpssas_free_tm(sc, tm);
1441 if (reply == NULL) {
1442 mps_dprint(sc, MPS_RECOVERY,
1443 "NULL abort reply for tm %p TaskMID %u\n",
1444 tm, le16toh(req->TaskMID));
1445 if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) {
1446 /* this completion was due to a reset, just cleanup */
1447 mps_dprint(sc, MPS_RECOVERY, "Hardware undergoing "
1448 "reset, ignoring NULL abort reply\n");
1450 mpssas_free_tm(sc, tm);
1452 /* we should have gotten a reply. */
1453 mps_dprint(sc, MPS_INFO|MPS_RECOVERY, "NULL reply on "
1454 "abort attempt, resetting controller\n");
1460 mps_dprint(sc, MPS_RECOVERY,
1461 "abort TaskMID %u status 0x%x code 0x%x count %u\n",
1462 le16toh(req->TaskMID),
1463 le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1464 le32toh(reply->TerminationCount));
1466 cm = TAILQ_FIRST(&tm->cm_targ->timedout_commands);
1469 * If there are no more timedout commands, we're done with
1470 * error recovery for this target.
1472 mps_dprint(sc, MPS_INFO|MPS_RECOVERY,
1473 "Finished abort recovery for target %u\n", targ->tid);
1476 mpssas_free_tm(sc, tm);
1477 } else if (le16toh(req->TaskMID) != cm->cm_desc.Default.SMID) {
1478 /* abort success, but we have more timedout commands to abort */
1479 mps_dprint(sc, MPS_INFO|MPS_RECOVERY,
1480 "Continuing abort recovery for target %u\n", targ->tid);
1482 mpssas_send_abort(sc, tm, cm);
1484 /* we didn't get a command completion, so the abort
1485 * failed as far as we're concerned. escalate.
1487 mps_dprint(sc, MPS_RECOVERY,
1488 "Abort failed for target %u, sending logical unit reset\n",
1491 mpssas_send_reset(sc, tm,
1492 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET);
1496 #define MPS_ABORT_TIMEOUT 5
1499 mpssas_send_abort(struct mps_softc *sc, struct mps_command *tm, struct mps_command *cm)
1501 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1502 struct mpssas_target *targ;
1506 if (targ->handle == 0) {
1507 mps_dprint(sc, MPS_ERROR|MPS_RECOVERY,
1508 "%s null devhandle for target_id %d\n",
1509 __func__, cm->cm_ccb->ccb_h.target_id);
1513 mpssas_log_command(cm, MPS_RECOVERY|MPS_INFO,
1514 "Aborting command %p\n", cm);
1516 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1517 req->DevHandle = htole16(targ->handle);
1518 req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK;
1520 /* XXX Need to handle invalid LUNs */
1521 MPS_SET_LUN(req->LUN, cm->cm_ccb->ccb_h.target_lun);
1523 req->TaskMID = htole16(cm->cm_desc.Default.SMID);
1526 tm->cm_complete = mpssas_abort_complete;
1527 tm->cm_complete_data = (void *)tm;
1528 tm->cm_targ = cm->cm_targ;
1529 tm->cm_lun = cm->cm_lun;
1531 callout_reset(&tm->cm_callout, MPS_ABORT_TIMEOUT * hz,
1532 mpssas_tm_timeout, tm);
1536 mpssas_prepare_for_tm(sc, tm, targ, tm->cm_lun);
1538 err = mps_map_command(sc, tm);
1540 mps_dprint(sc, MPS_ERROR|MPS_RECOVERY,
1541 "error %d sending abort for cm %p SMID %u\n",
1542 err, cm, req->TaskMID);
1547 mpssas_scsiio_timeout(void *data)
1549 sbintime_t elapsed, now;
1551 struct mps_softc *sc;
1552 struct mps_command *cm;
1553 struct mpssas_target *targ;
1555 cm = (struct mps_command *)data;
1561 mtx_assert(&sc->mps_mtx, MA_OWNED);
1563 mps_dprint(sc, MPS_XINFO|MPS_RECOVERY, "Timeout checking cm %p\n", sc);
1566 * Run the interrupt handler to make sure it's not pending. This
1567 * isn't perfect because the command could have already completed
1568 * and been re-used, though this is unlikely.
1570 mps_intr_locked(sc);
1571 if (cm->cm_flags & MPS_CM_FLAGS_ON_RECOVERY) {
1572 mpssas_log_command(cm, MPS_XINFO,
1573 "SCSI command %p almost timed out\n", cm);
1577 if (cm->cm_ccb == NULL) {
1578 mps_dprint(sc, MPS_ERROR, "command timeout with NULL ccb\n");
1585 elapsed = now - ccb->ccb_h.qos.sim_data;
1586 mpssas_log_command(cm, MPS_INFO|MPS_RECOVERY,
1587 "Command timeout on target %u(0x%04x) %d set, %d.%d elapsed\n",
1588 targ->tid, targ->handle, ccb->ccb_h.timeout,
1589 sbintime_getsec(elapsed), elapsed & 0xffffffff);
1591 /* XXX first, check the firmware state, to see if it's still
1592 * operational. if not, do a diag reset.
1594 mpssas_set_ccbstatus(cm->cm_ccb, CAM_CMD_TIMEOUT);
1595 cm->cm_flags |= MPS_CM_FLAGS_ON_RECOVERY | MPS_CM_FLAGS_TIMEDOUT;
1596 TAILQ_INSERT_TAIL(&targ->timedout_commands, cm, cm_recovery);
1598 if (targ->tm != NULL) {
1599 /* target already in recovery, just queue up another
1600 * timedout command to be processed later.
1602 mps_dprint(sc, MPS_RECOVERY,
1603 "queued timedout cm %p for processing by tm %p\n",
1605 } else if ((targ->tm = mpssas_alloc_tm(sc)) != NULL) {
1606 mps_dprint(sc, MPS_RECOVERY|MPS_INFO,
1607 "Sending abort to target %u for SMID %d\n", targ->tid,
1608 cm->cm_desc.Default.SMID);
1609 mps_dprint(sc, MPS_RECOVERY, "timedout cm %p allocated tm %p\n",
1612 /* start recovery by aborting the first timedout command */
1613 mpssas_send_abort(sc, targ->tm, cm);
1615 /* XXX queue this target up for recovery once a TM becomes
1616 * available. The firmware only has a limited number of
1617 * HighPriority credits for the high priority requests used
1618 * for task management, and we ran out.
1620 * Isilon: don't worry about this for now, since we have
1621 * more credits than disks in an enclosure, and limit
1622 * ourselves to one TM per target for recovery.
1624 mps_dprint(sc, MPS_ERROR|MPS_RECOVERY,
1625 "timedout cm %p failed to allocate a tm\n", cm);
1631 mpssas_action_scsiio(struct mpssas_softc *sassc, union ccb *ccb)
1633 MPI2_SCSI_IO_REQUEST *req;
1634 struct ccb_scsiio *csio;
1635 struct mps_softc *sc;
1636 struct mpssas_target *targ;
1637 struct mpssas_lun *lun;
1638 struct mps_command *cm;
1639 uint8_t i, lba_byte, *ref_tag_addr;
1640 uint16_t eedp_flags;
1641 uint32_t mpi_control;
1645 mtx_assert(&sc->mps_mtx, MA_OWNED);
1648 KASSERT(csio->ccb_h.target_id < sassc->maxtargets,
1649 ("Target %d out of bounds in XPT_SCSI_IO\n",
1650 csio->ccb_h.target_id));
1651 targ = &sassc->targets[csio->ccb_h.target_id];
1652 mps_dprint(sc, MPS_TRACE, "ccb %p target flag %x\n", ccb, targ->flags);
1653 if (targ->handle == 0x0) {
1654 mps_dprint(sc, MPS_ERROR, "%s NULL handle for target %u\n",
1655 __func__, csio->ccb_h.target_id);
1656 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1660 if (targ->flags & MPS_TARGET_FLAGS_RAID_COMPONENT) {
1661 mps_dprint(sc, MPS_ERROR, "%s Raid component no SCSI IO "
1662 "supported %u\n", __func__, csio->ccb_h.target_id);
1663 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1668 * Sometimes, it is possible to get a command that is not "In
1669 * Progress" and was actually aborted by the upper layer. Check for
1670 * this here and complete the command without error.
1672 if (mpssas_get_ccbstatus(ccb) != CAM_REQ_INPROG) {
1673 mps_dprint(sc, MPS_TRACE, "%s Command is not in progress for "
1674 "target %u\n", __func__, csio->ccb_h.target_id);
1679 * If devinfo is 0 this will be a volume. In that case don't tell CAM
1680 * that the volume has timed out. We want volumes to be enumerated
1681 * until they are deleted/removed, not just failed. In either event,
1682 * we're removing the target due to a firmware event telling us
1683 * the device is now gone (as opposed to some transient event). Since
1684 * we're opting to remove failed devices from the OS's view, we need
1685 * to propagate that status up the stack.
1687 if (targ->flags & MPSSAS_TARGET_INREMOVAL) {
1688 if (targ->devinfo == 0)
1689 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
1691 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1696 if ((sc->mps_flags & MPS_FLAGS_SHUTDOWN) != 0) {
1697 mps_dprint(sc, MPS_INFO, "%s shutting down\n", __func__);
1698 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1704 * If target has a reset in progress, freeze the devq and return. The
1705 * devq will be released when the TM reset is finished.
1707 if (targ->flags & MPSSAS_TARGET_INRESET) {
1708 ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
1709 mps_dprint(sc, MPS_INFO, "%s: Freezing devq for target ID %d\n",
1710 __func__, targ->tid);
1711 xpt_freeze_devq(ccb->ccb_h.path, 1);
1716 cm = mps_alloc_command(sc);
1717 if (cm == NULL || (sc->mps_flags & MPS_FLAGS_DIAGRESET)) {
1719 mps_free_command(sc, cm);
1721 if ((sassc->flags & MPSSAS_QUEUE_FROZEN) == 0) {
1722 xpt_freeze_simq(sassc->sim, 1);
1723 sassc->flags |= MPSSAS_QUEUE_FROZEN;
1725 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1726 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1731 req = (MPI2_SCSI_IO_REQUEST *)cm->cm_req;
1732 bzero(req, sizeof(*req));
1733 req->DevHandle = htole16(targ->handle);
1734 req->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1736 req->SenseBufferLowAddress = htole32(cm->cm_sense_busaddr);
1737 req->SenseBufferLength = MPS_SENSE_LEN;
1739 req->ChainOffset = 0;
1740 req->SGLOffset0 = 24; /* 32bit word offset to the SGL */
1745 req->DataLength = htole32(csio->dxfer_len);
1746 req->BidirectionalDataLength = 0;
1747 req->IoFlags = htole16(csio->cdb_len);
1750 /* Note: BiDirectional transfers are not supported */
1751 switch (csio->ccb_h.flags & CAM_DIR_MASK) {
1753 mpi_control = MPI2_SCSIIO_CONTROL_READ;
1754 cm->cm_flags |= MPS_CM_FLAGS_DATAIN;
1757 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
1758 cm->cm_flags |= MPS_CM_FLAGS_DATAOUT;
1762 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
1766 if (csio->cdb_len == 32)
1767 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
1769 * It looks like the hardware doesn't require an explicit tag
1770 * number for each transaction. SAM Task Management not supported
1773 switch (csio->tag_action) {
1774 case MSG_HEAD_OF_Q_TAG:
1775 mpi_control |= MPI2_SCSIIO_CONTROL_HEADOFQ;
1777 case MSG_ORDERED_Q_TAG:
1778 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
1781 mpi_control |= MPI2_SCSIIO_CONTROL_ACAQ;
1783 case CAM_TAG_ACTION_NONE:
1784 case MSG_SIMPLE_Q_TAG:
1786 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
1789 mpi_control |= sc->mapping_table[csio->ccb_h.target_id].TLR_bits;
1790 req->Control = htole32(mpi_control);
1791 if (MPS_SET_LUN(req->LUN, csio->ccb_h.target_lun) != 0) {
1792 mps_free_command(sc, cm);
1793 mpssas_set_ccbstatus(ccb, CAM_LUN_INVALID);
1798 if (csio->ccb_h.flags & CAM_CDB_POINTER)
1799 bcopy(csio->cdb_io.cdb_ptr, &req->CDB.CDB32[0], csio->cdb_len);
1801 bcopy(csio->cdb_io.cdb_bytes, &req->CDB.CDB32[0],csio->cdb_len);
1802 req->IoFlags = htole16(csio->cdb_len);
1805 * Check if EEDP is supported and enabled. If it is then check if the
1806 * SCSI opcode could be using EEDP. If so, make sure the LUN exists and
1807 * is formatted for EEDP support. If all of this is true, set CDB up
1808 * for EEDP transfer.
1810 eedp_flags = op_code_prot[req->CDB.CDB32[0]];
1811 if (sc->eedp_enabled && eedp_flags) {
1812 SLIST_FOREACH(lun, &targ->luns, lun_link) {
1813 if (lun->lun_id == csio->ccb_h.target_lun) {
1818 if ((lun != NULL) && (lun->eedp_formatted)) {
1819 req->EEDPBlockSize = htole16(lun->eedp_block_size);
1820 eedp_flags |= (MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1821 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1822 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
1823 req->EEDPFlags = htole16(eedp_flags);
1826 * If CDB less than 32, fill in Primary Ref Tag with
1827 * low 4 bytes of LBA. If CDB is 32, tag stuff is
1828 * already there. Also, set protection bit. FreeBSD
1829 * currently does not support CDBs bigger than 16, but
1830 * the code doesn't hurt, and will be here for the
1833 if (csio->cdb_len != 32) {
1834 lba_byte = (csio->cdb_len == 16) ? 6 : 2;
1835 ref_tag_addr = (uint8_t *)&req->CDB.EEDP32.
1836 PrimaryReferenceTag;
1837 for (i = 0; i < 4; i++) {
1839 req->CDB.CDB32[lba_byte + i];
1842 req->CDB.EEDP32.PrimaryReferenceTag =
1843 htole32(req->CDB.EEDP32.PrimaryReferenceTag);
1844 req->CDB.EEDP32.PrimaryApplicationTagMask =
1846 req->CDB.CDB32[1] = (req->CDB.CDB32[1] & 0x1F) |
1850 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_APPTAG;
1851 req->EEDPFlags = htole16(eedp_flags);
1852 req->CDB.CDB32[10] = (req->CDB.CDB32[10] &
1858 cm->cm_length = csio->dxfer_len;
1859 if (cm->cm_length != 0) {
1861 cm->cm_flags |= MPS_CM_FLAGS_USE_CCB;
1865 cm->cm_sge = &req->SGL;
1866 cm->cm_sglsize = (32 - 24) * 4;
1867 cm->cm_desc.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
1868 cm->cm_desc.SCSIIO.DevHandle = htole16(targ->handle);
1869 cm->cm_complete = mpssas_scsiio_complete;
1870 cm->cm_complete_data = ccb;
1872 cm->cm_lun = csio->ccb_h.target_lun;
1876 * If HBA is a WD and the command is not for a retry, try to build a
1877 * direct I/O message. If failed, or the command is for a retry, send
1878 * the I/O to the IR volume itself.
1880 if (sc->WD_valid_config) {
1881 if (ccb->ccb_h.sim_priv.entries[0].field == MPS_WD_RETRY) {
1882 mpssas_direct_drive_io(sassc, cm, ccb);
1884 mpssas_set_ccbstatus(ccb, CAM_REQ_INPROG);
1888 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
1889 if (csio->bio != NULL)
1890 biotrack(csio->bio, __func__);
1892 csio->ccb_h.qos.sim_data = sbinuptime();
1893 callout_reset_sbt(&cm->cm_callout, SBT_1MS * ccb->ccb_h.timeout, 0,
1894 mpssas_scsiio_timeout, cm, 0);
1897 targ->outstanding++;
1898 TAILQ_INSERT_TAIL(&targ->commands, cm, cm_link);
1899 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1901 mpssas_log_command(cm, MPS_XINFO, "%s cm %p ccb %p outstanding %u\n",
1902 __func__, cm, ccb, targ->outstanding);
1904 mps_map_command(sc, cm);
1909 * mps_sc_failed_io_info - translated non-succesfull SCSI_IO request
1912 mps_sc_failed_io_info(struct mps_softc *sc, struct ccb_scsiio *csio,
1913 Mpi2SCSIIOReply_t *mpi_reply)
1917 u16 ioc_status = le16toh(mpi_reply->IOCStatus) &
1918 MPI2_IOCSTATUS_MASK;
1919 u8 scsi_state = mpi_reply->SCSIState;
1920 u8 scsi_status = mpi_reply->SCSIStatus;
1921 u32 log_info = le32toh(mpi_reply->IOCLogInfo);
1922 const char *desc_ioc_state, *desc_scsi_status;
1924 if (log_info == 0x31170000)
1927 desc_ioc_state = mps_describe_table(mps_iocstatus_string,
1929 desc_scsi_status = mps_describe_table(mps_scsi_status_string,
1932 mps_dprint(sc, MPS_XINFO, "\thandle(0x%04x), ioc_status(%s)(0x%04x)\n",
1933 le16toh(mpi_reply->DevHandle), desc_ioc_state, ioc_status);
1936 *We can add more detail about underflow data here
1939 mps_dprint(sc, MPS_XINFO, "\tscsi_status(%s)(0x%02x), "
1940 "scsi_state %b\n", desc_scsi_status, scsi_status,
1941 scsi_state, "\20" "\1AutosenseValid" "\2AutosenseFailed"
1942 "\3NoScsiStatus" "\4Terminated" "\5Response InfoValid");
1944 if (sc->mps_debug & MPS_XINFO &&
1945 scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
1946 mps_dprint(sc, MPS_XINFO, "-> Sense Buffer Data : Start :\n");
1947 scsi_sense_print(csio);
1948 mps_dprint(sc, MPS_XINFO, "-> Sense Buffer Data : End :\n");
1951 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
1952 response_info = le32toh(mpi_reply->ResponseInfo);
1953 response_bytes = (u8 *)&response_info;
1954 mps_dprint(sc, MPS_XINFO, "response code(0x%1x): %s\n",
1956 mps_describe_table(mps_scsi_taskmgmt_string,
1957 response_bytes[0]));
1962 mpssas_scsiio_complete(struct mps_softc *sc, struct mps_command *cm)
1964 MPI2_SCSI_IO_REPLY *rep;
1966 struct ccb_scsiio *csio;
1967 struct mpssas_softc *sassc;
1968 struct scsi_vpd_supported_page_list *vpd_list = NULL;
1969 u8 *TLR_bits, TLR_on;
1972 struct mpssas_target *target;
1973 target_id_t target_id;
1976 mps_dprint(sc, MPS_TRACE,
1977 "cm %p SMID %u ccb %p reply %p outstanding %u\n", cm,
1978 cm->cm_desc.Default.SMID, cm->cm_ccb, cm->cm_reply,
1979 cm->cm_targ->outstanding);
1981 callout_stop(&cm->cm_callout);
1982 mtx_assert(&sc->mps_mtx, MA_OWNED);
1985 ccb = cm->cm_complete_data;
1987 target_id = csio->ccb_h.target_id;
1988 rep = (MPI2_SCSI_IO_REPLY *)cm->cm_reply;
1990 * XXX KDM if the chain allocation fails, does it matter if we do
1991 * the sync and unload here? It is simpler to do it in every case,
1992 * assuming it doesn't cause problems.
1994 if (cm->cm_data != NULL) {
1995 if (cm->cm_flags & MPS_CM_FLAGS_DATAIN)
1996 dir = BUS_DMASYNC_POSTREAD;
1997 else if (cm->cm_flags & MPS_CM_FLAGS_DATAOUT)
1998 dir = BUS_DMASYNC_POSTWRITE;
1999 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir);
2000 bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
2003 cm->cm_targ->completed++;
2004 cm->cm_targ->outstanding--;
2005 TAILQ_REMOVE(&cm->cm_targ->commands, cm, cm_link);
2006 ccb->ccb_h.status &= ~(CAM_STATUS_MASK | CAM_SIM_QUEUED);
2008 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
2009 if (ccb->csio.bio != NULL)
2010 biotrack(ccb->csio.bio, __func__);
2013 if (cm->cm_flags & MPS_CM_FLAGS_ON_RECOVERY) {
2014 TAILQ_REMOVE(&cm->cm_targ->timedout_commands, cm, cm_recovery);
2015 KASSERT(cm->cm_state == MPS_CM_STATE_BUSY,
2016 ("Not busy for CM_FLAGS_TIMEDOUT: %d\n", cm->cm_state));
2017 cm->cm_flags &= ~MPS_CM_FLAGS_ON_RECOVERY;
2018 if (cm->cm_reply != NULL)
2019 mpssas_log_command(cm, MPS_RECOVERY,
2020 "completed timedout cm %p ccb %p during recovery "
2021 "ioc %x scsi %x state %x xfer %u\n",
2022 cm, cm->cm_ccb, le16toh(rep->IOCStatus),
2023 rep->SCSIStatus, rep->SCSIState,
2024 le32toh(rep->TransferCount));
2026 mpssas_log_command(cm, MPS_RECOVERY,
2027 "completed timedout cm %p ccb %p during recovery\n",
2029 } else if (cm->cm_targ->tm != NULL) {
2030 if (cm->cm_reply != NULL)
2031 mpssas_log_command(cm, MPS_RECOVERY,
2032 "completed cm %p ccb %p during recovery "
2033 "ioc %x scsi %x state %x xfer %u\n",
2034 cm, cm->cm_ccb, le16toh(rep->IOCStatus),
2035 rep->SCSIStatus, rep->SCSIState,
2036 le32toh(rep->TransferCount));
2038 mpssas_log_command(cm, MPS_RECOVERY,
2039 "completed cm %p ccb %p during recovery\n",
2041 } else if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) {
2042 mpssas_log_command(cm, MPS_RECOVERY,
2043 "reset completed cm %p ccb %p\n",
2047 if ((cm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
2049 * We ran into an error after we tried to map the command,
2050 * so we're getting a callback without queueing the command
2051 * to the hardware. So we set the status here, and it will
2052 * be retained below. We'll go through the "fast path",
2053 * because there can be no reply when we haven't actually
2054 * gone out to the hardware.
2056 mpssas_set_ccbstatus(ccb, CAM_REQUEUE_REQ);
2059 * Currently the only error included in the mask is
2060 * MPS_CM_FLAGS_CHAIN_FAILED, which means we're out of
2061 * chain frames. We need to freeze the queue until we get
2062 * a command that completed without this error, which will
2063 * hopefully have some chain frames attached that we can
2064 * use. If we wanted to get smarter about it, we would
2065 * only unfreeze the queue in this condition when we're
2066 * sure that we're getting some chain frames back. That's
2067 * probably unnecessary.
2069 if ((sassc->flags & MPSSAS_QUEUE_FROZEN) == 0) {
2070 xpt_freeze_simq(sassc->sim, 1);
2071 sassc->flags |= MPSSAS_QUEUE_FROZEN;
2072 mps_dprint(sc, MPS_XINFO, "Error sending command, "
2073 "freezing SIM queue\n");
2078 * If this is a Start Stop Unit command and it was issued by the driver
2079 * during shutdown, decrement the refcount to account for all of the
2080 * commands that were sent. All SSU commands should be completed before
2081 * shutdown completes, meaning SSU_refcount will be 0 after SSU_started
2084 if (sc->SSU_started && (csio->cdb_io.cdb_bytes[0] == START_STOP_UNIT)) {
2085 mps_dprint(sc, MPS_INFO, "Decrementing SSU count.\n");
2089 /* Take the fast path to completion */
2090 if (cm->cm_reply == NULL) {
2091 if (mpssas_get_ccbstatus(ccb) == CAM_REQ_INPROG) {
2092 if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0)
2093 mpssas_set_ccbstatus(ccb, CAM_SCSI_BUS_RESET);
2095 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
2096 ccb->csio.scsi_status = SCSI_STATUS_OK;
2098 if (sassc->flags & MPSSAS_QUEUE_FROZEN) {
2099 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2100 sassc->flags &= ~MPSSAS_QUEUE_FROZEN;
2101 mps_dprint(sc, MPS_XINFO,
2102 "Unfreezing SIM queue\n");
2107 * There are two scenarios where the status won't be
2108 * CAM_REQ_CMP. The first is if MPS_CM_FLAGS_ERROR_MASK is
2109 * set, the second is in the MPS_FLAGS_DIAGRESET above.
2111 if (mpssas_get_ccbstatus(ccb) != CAM_REQ_CMP) {
2113 * Freeze the dev queue so that commands are
2114 * executed in the correct order after error
2117 ccb->ccb_h.status |= CAM_DEV_QFRZN;
2118 xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1);
2120 mps_free_command(sc, cm);
2125 mpssas_log_command(cm, MPS_XINFO,
2126 "ioc %x scsi %x state %x xfer %u\n",
2127 le16toh(rep->IOCStatus), rep->SCSIStatus, rep->SCSIState,
2128 le32toh(rep->TransferCount));
2131 * If this is a Direct Drive I/O, reissue the I/O to the original IR
2132 * Volume if an error occurred (normal I/O retry). Use the original
2133 * CCB, but set a flag that this will be a retry so that it's sent to
2134 * the original volume. Free the command but reuse the CCB.
2136 if (cm->cm_flags & MPS_CM_FLAGS_DD_IO) {
2137 mps_free_command(sc, cm);
2138 ccb->ccb_h.sim_priv.entries[0].field = MPS_WD_RETRY;
2139 mpssas_action_scsiio(sassc, ccb);
2142 ccb->ccb_h.sim_priv.entries[0].field = 0;
2144 switch (le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK) {
2145 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
2146 csio->resid = cm->cm_length - le32toh(rep->TransferCount);
2148 case MPI2_IOCSTATUS_SUCCESS:
2149 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
2151 if ((le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK) ==
2152 MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR)
2153 mpssas_log_command(cm, MPS_XINFO, "recovered error\n");
2155 /* Completion failed at the transport level. */
2156 if (rep->SCSIState & (MPI2_SCSI_STATE_NO_SCSI_STATUS |
2157 MPI2_SCSI_STATE_TERMINATED)) {
2158 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2162 /* In a modern packetized environment, an autosense failure
2163 * implies that there's not much else that can be done to
2164 * recover the command.
2166 if (rep->SCSIState & MPI2_SCSI_STATE_AUTOSENSE_FAILED) {
2167 mpssas_set_ccbstatus(ccb, CAM_AUTOSENSE_FAIL);
2172 * CAM doesn't care about SAS Response Info data, but if this is
2173 * the state check if TLR should be done. If not, clear the
2174 * TLR_bits for the target.
2176 if ((rep->SCSIState & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) &&
2177 ((le32toh(rep->ResponseInfo) &
2178 MPI2_SCSI_RI_MASK_REASONCODE) ==
2179 MPS_SCSI_RI_INVALID_FRAME)) {
2180 sc->mapping_table[target_id].TLR_bits =
2181 (u8)MPI2_SCSIIO_CONTROL_NO_TLR;
2185 * Intentionally override the normal SCSI status reporting
2186 * for these two cases. These are likely to happen in a
2187 * multi-initiator environment, and we want to make sure that
2188 * CAM retries these commands rather than fail them.
2190 if ((rep->SCSIStatus == MPI2_SCSI_STATUS_COMMAND_TERMINATED) ||
2191 (rep->SCSIStatus == MPI2_SCSI_STATUS_TASK_ABORTED)) {
2192 mpssas_set_ccbstatus(ccb, CAM_REQ_ABORTED);
2196 /* Handle normal status and sense */
2197 csio->scsi_status = rep->SCSIStatus;
2198 if (rep->SCSIStatus == MPI2_SCSI_STATUS_GOOD)
2199 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
2201 mpssas_set_ccbstatus(ccb, CAM_SCSI_STATUS_ERROR);
2203 if (rep->SCSIState & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2204 int sense_len, returned_sense_len;
2206 returned_sense_len = min(le32toh(rep->SenseCount),
2207 sizeof(struct scsi_sense_data));
2208 if (returned_sense_len < ccb->csio.sense_len)
2209 ccb->csio.sense_resid = ccb->csio.sense_len -
2212 ccb->csio.sense_resid = 0;
2214 sense_len = min(returned_sense_len,
2215 ccb->csio.sense_len - ccb->csio.sense_resid);
2216 bzero(&ccb->csio.sense_data,
2217 sizeof(ccb->csio.sense_data));
2218 bcopy(cm->cm_sense, &ccb->csio.sense_data, sense_len);
2219 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
2223 * Check if this is an INQUIRY command. If it's a VPD inquiry,
2224 * and it's page code 0 (Supported Page List), and there is
2225 * inquiry data, and this is for a sequential access device, and
2226 * the device is an SSP target, and TLR is supported by the
2227 * controller, turn the TLR_bits value ON if page 0x90 is
2230 if ((csio->cdb_io.cdb_bytes[0] == INQUIRY) &&
2231 (csio->cdb_io.cdb_bytes[1] & SI_EVPD) &&
2232 (csio->cdb_io.cdb_bytes[2] == SVPD_SUPPORTED_PAGE_LIST) &&
2233 ((csio->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR) &&
2234 (csio->data_ptr != NULL) &&
2235 ((csio->data_ptr[0] & 0x1f) == T_SEQUENTIAL) &&
2236 (sc->control_TLR) &&
2237 (sc->mapping_table[target_id].device_info &
2238 MPI2_SAS_DEVICE_INFO_SSP_TARGET)) {
2239 vpd_list = (struct scsi_vpd_supported_page_list *)
2241 TLR_bits = &sc->mapping_table[target_id].TLR_bits;
2242 *TLR_bits = (u8)MPI2_SCSIIO_CONTROL_NO_TLR;
2243 TLR_on = (u8)MPI2_SCSIIO_CONTROL_TLR_ON;
2244 alloc_len = ((u16)csio->cdb_io.cdb_bytes[3] << 8) +
2245 csio->cdb_io.cdb_bytes[4];
2246 alloc_len -= csio->resid;
2247 for (i = 0; i < MIN(vpd_list->length, alloc_len); i++) {
2248 if (vpd_list->list[i] == 0x90) {
2256 * If this is a SATA direct-access end device, mark it so that
2257 * a SCSI StartStopUnit command will be sent to it when the
2258 * driver is being shutdown.
2260 if ((csio->cdb_io.cdb_bytes[0] == INQUIRY) &&
2261 ((csio->data_ptr[0] & 0x1f) == T_DIRECT) &&
2262 (sc->mapping_table[target_id].device_info &
2263 MPI2_SAS_DEVICE_INFO_SATA_DEVICE) &&
2264 ((sc->mapping_table[target_id].device_info &
2265 MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) ==
2266 MPI2_SAS_DEVICE_INFO_END_DEVICE)) {
2267 target = &sassc->targets[target_id];
2268 target->supports_SSU = TRUE;
2269 mps_dprint(sc, MPS_XINFO, "Target %d supports SSU\n",
2273 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
2274 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2276 * If devinfo is 0 this will be a volume. In that case don't
2277 * tell CAM that the volume is not there. We want volumes to
2278 * be enumerated until they are deleted/removed, not just
2281 if (cm->cm_targ->devinfo == 0)
2282 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
2284 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
2286 case MPI2_IOCSTATUS_INVALID_SGL:
2287 mps_print_scsiio_cmd(sc, cm);
2288 mpssas_set_ccbstatus(ccb, CAM_UNREC_HBA_ERROR);
2290 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
2292 * This is one of the responses that comes back when an I/O
2293 * has been aborted. If it is because of a timeout that we
2294 * initiated, just set the status to CAM_CMD_TIMEOUT.
2295 * Otherwise set it to CAM_REQ_ABORTED. The effect on the
2296 * command is the same (it gets retried, subject to the
2297 * retry counter), the only difference is what gets printed
2300 if (cm->cm_flags & MPS_CM_FLAGS_TIMEDOUT)
2301 mpssas_set_ccbstatus(ccb, CAM_CMD_TIMEOUT);
2303 mpssas_set_ccbstatus(ccb, CAM_REQ_ABORTED);
2305 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
2306 /* resid is ignored for this condition */
2308 mpssas_set_ccbstatus(ccb, CAM_DATA_RUN_ERR);
2310 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
2311 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
2313 * These can sometimes be transient transport-related
2314 * errors, and sometimes persistent drive-related errors.
2315 * We used to retry these without decrementing the retry
2316 * count by returning CAM_REQUEUE_REQ. Unfortunately, if
2317 * we hit a persistent drive problem that returns one of
2318 * these error codes, we would retry indefinitely. So,
2319 * return CAM_REQ_CMP_ERROR so that we decrement the retry
2320 * count and avoid infinite retries. We're taking the
2321 * potential risk of flagging false failures in the event
2322 * of a topology-related error (e.g. a SAS expander problem
2323 * causes a command addressed to a drive to fail), but
2324 * avoiding getting into an infinite retry loop. However,
2325 * if we get them while were moving a device, we should
2326 * fail the request as 'not there' because the device
2327 * is effectively gone.
2329 if (cm->cm_targ->flags & MPSSAS_TARGET_INREMOVAL)
2330 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
2332 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2333 mps_dprint(sc, MPS_INFO,
2334 "Controller reported %s tgt %u SMID %u loginfo %x%s\n",
2335 mps_describe_table(mps_iocstatus_string,
2336 le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK),
2337 target_id, cm->cm_desc.Default.SMID,
2338 le32toh(rep->IOCLogInfo),
2339 (cm->cm_targ->flags & MPSSAS_TARGET_INREMOVAL) ? " departing" : "");
2340 mps_dprint(sc, MPS_XINFO,
2341 "SCSIStatus %x SCSIState %x xfercount %u\n",
2342 rep->SCSIStatus, rep->SCSIState,
2343 le32toh(rep->TransferCount));
2345 case MPI2_IOCSTATUS_INVALID_FUNCTION:
2346 case MPI2_IOCSTATUS_INTERNAL_ERROR:
2347 case MPI2_IOCSTATUS_INVALID_VPID:
2348 case MPI2_IOCSTATUS_INVALID_FIELD:
2349 case MPI2_IOCSTATUS_INVALID_STATE:
2350 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
2351 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
2352 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2353 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2354 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2356 mpssas_log_command(cm, MPS_XINFO,
2357 "completed ioc %x loginfo %x scsi %x state %x xfer %u\n",
2358 le16toh(rep->IOCStatus), le32toh(rep->IOCLogInfo),
2359 rep->SCSIStatus, rep->SCSIState,
2360 le32toh(rep->TransferCount));
2361 csio->resid = cm->cm_length;
2362 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2366 mps_sc_failed_io_info(sc,csio,rep);
2368 if (sassc->flags & MPSSAS_QUEUE_FROZEN) {
2369 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2370 sassc->flags &= ~MPSSAS_QUEUE_FROZEN;
2371 mps_dprint(sc, MPS_XINFO, "Command completed, "
2372 "unfreezing SIM queue\n");
2375 if (mpssas_get_ccbstatus(ccb) != CAM_REQ_CMP) {
2376 ccb->ccb_h.status |= CAM_DEV_QFRZN;
2377 xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1);
2381 * Check to see if we're removing the device. If so, and this is the
2382 * last command on the queue, proceed with the deferred removal of the
2383 * device. Note, for removing a volume, this won't trigger because
2384 * pending_remove_tm will be NULL.
2386 if (cm->cm_targ->flags & MPSSAS_TARGET_INREMOVAL) {
2387 if (TAILQ_FIRST(&cm->cm_targ->commands) == NULL &&
2388 cm->cm_targ->pending_remove_tm != NULL) {
2389 mps_dprint(sc, MPS_INFO, "Last pending command complete: starting remove_device\n");
2390 mps_map_command(sc, cm->cm_targ->pending_remove_tm);
2391 cm->cm_targ->pending_remove_tm = NULL;
2395 mps_free_command(sc, cm);
2399 /* All Request reached here are Endian safe */
2401 mpssas_direct_drive_io(struct mpssas_softc *sassc, struct mps_command *cm,
2403 pMpi2SCSIIORequest_t pIO_req;
2404 struct mps_softc *sc = sassc->sc;
2406 uint32_t physLBA, stripe_offset, stripe_unit;
2407 uint32_t io_size, column;
2408 uint8_t *ptrLBA, lba_idx, physLBA_byte, *CDB;
2411 * If this is a valid SCSI command (Read6, Read10, Read16, Write6,
2412 * Write10, or Write16), build a direct I/O message. Otherwise, the I/O
2413 * will be sent to the IR volume itself. Since Read6 and Write6 are a
2414 * bit different than the 10/16 CDBs, handle them separately.
2416 pIO_req = (pMpi2SCSIIORequest_t)cm->cm_req;
2417 CDB = pIO_req->CDB.CDB32;
2420 * Handle 6 byte CDBs.
2422 if ((pIO_req->DevHandle == sc->DD_dev_handle) && ((CDB[0] == READ_6) ||
2423 (CDB[0] == WRITE_6))) {
2425 * Get the transfer size in blocks.
2427 io_size = (cm->cm_length >> sc->DD_block_exponent);
2430 * Get virtual LBA given in the CDB.
2432 virtLBA = ((uint64_t)(CDB[1] & 0x1F) << 16) |
2433 ((uint64_t)CDB[2] << 8) | (uint64_t)CDB[3];
2436 * Check that LBA range for I/O does not exceed volume's
2439 if ((virtLBA + (uint64_t)io_size - 1) <=
2442 * Check if the I/O crosses a stripe boundary. If not,
2443 * translate the virtual LBA to a physical LBA and set
2444 * the DevHandle for the PhysDisk to be used. If it
2445 * does cross a boundary, do normal I/O. To get the
2446 * right DevHandle to use, get the map number for the
2447 * column, then use that map number to look up the
2448 * DevHandle of the PhysDisk.
2450 stripe_offset = (uint32_t)virtLBA &
2451 (sc->DD_stripe_size - 1);
2452 if ((stripe_offset + io_size) <= sc->DD_stripe_size) {
2453 physLBA = (uint32_t)virtLBA >>
2454 sc->DD_stripe_exponent;
2455 stripe_unit = physLBA / sc->DD_num_phys_disks;
2456 column = physLBA % sc->DD_num_phys_disks;
2457 pIO_req->DevHandle =
2458 htole16(sc->DD_column_map[column].dev_handle);
2459 /* ???? Is this endian safe*/
2460 cm->cm_desc.SCSIIO.DevHandle =
2463 physLBA = (stripe_unit <<
2464 sc->DD_stripe_exponent) + stripe_offset;
2465 ptrLBA = &pIO_req->CDB.CDB32[1];
2466 physLBA_byte = (uint8_t)(physLBA >> 16);
2467 *ptrLBA = physLBA_byte;
2468 ptrLBA = &pIO_req->CDB.CDB32[2];
2469 physLBA_byte = (uint8_t)(physLBA >> 8);
2470 *ptrLBA = physLBA_byte;
2471 ptrLBA = &pIO_req->CDB.CDB32[3];
2472 physLBA_byte = (uint8_t)physLBA;
2473 *ptrLBA = physLBA_byte;
2476 * Set flag that Direct Drive I/O is
2479 cm->cm_flags |= MPS_CM_FLAGS_DD_IO;
2486 * Handle 10, 12 or 16 byte CDBs.
2488 if ((pIO_req->DevHandle == sc->DD_dev_handle) && ((CDB[0] == READ_10) ||
2489 (CDB[0] == WRITE_10) || (CDB[0] == READ_16) ||
2490 (CDB[0] == WRITE_16) || (CDB[0] == READ_12) ||
2491 (CDB[0] == WRITE_12))) {
2493 * For 16-byte CDB's, verify that the upper 4 bytes of the CDB
2494 * are 0. If not, this is accessing beyond 2TB so handle it in
2495 * the else section. 10-byte and 12-byte CDB's are OK.
2496 * FreeBSD sends very rare 12 byte READ/WRITE, but driver is
2497 * ready to accept 12byte CDB for Direct IOs.
2499 if ((CDB[0] == READ_10 || CDB[0] == WRITE_10) ||
2500 (CDB[0] == READ_12 || CDB[0] == WRITE_12) ||
2501 !(CDB[2] | CDB[3] | CDB[4] | CDB[5])) {
2503 * Get the transfer size in blocks.
2505 io_size = (cm->cm_length >> sc->DD_block_exponent);
2508 * Get virtual LBA. Point to correct lower 4 bytes of
2509 * LBA in the CDB depending on command.
2511 lba_idx = ((CDB[0] == READ_12) ||
2512 (CDB[0] == WRITE_12) ||
2513 (CDB[0] == READ_10) ||
2514 (CDB[0] == WRITE_10))? 2 : 6;
2515 virtLBA = ((uint64_t)CDB[lba_idx] << 24) |
2516 ((uint64_t)CDB[lba_idx + 1] << 16) |
2517 ((uint64_t)CDB[lba_idx + 2] << 8) |
2518 (uint64_t)CDB[lba_idx + 3];
2521 * Check that LBA range for I/O does not exceed volume's
2524 if ((virtLBA + (uint64_t)io_size - 1) <=
2527 * Check if the I/O crosses a stripe boundary.
2528 * If not, translate the virtual LBA to a
2529 * physical LBA and set the DevHandle for the
2530 * PhysDisk to be used. If it does cross a
2531 * boundary, do normal I/O. To get the right
2532 * DevHandle to use, get the map number for the
2533 * column, then use that map number to look up
2534 * the DevHandle of the PhysDisk.
2536 stripe_offset = (uint32_t)virtLBA &
2537 (sc->DD_stripe_size - 1);
2538 if ((stripe_offset + io_size) <=
2539 sc->DD_stripe_size) {
2540 physLBA = (uint32_t)virtLBA >>
2541 sc->DD_stripe_exponent;
2542 stripe_unit = physLBA /
2543 sc->DD_num_phys_disks;
2545 sc->DD_num_phys_disks;
2546 pIO_req->DevHandle =
2547 htole16(sc->DD_column_map[column].
2549 cm->cm_desc.SCSIIO.DevHandle =
2552 physLBA = (stripe_unit <<
2553 sc->DD_stripe_exponent) +
2556 &pIO_req->CDB.CDB32[lba_idx];
2557 physLBA_byte = (uint8_t)(physLBA >> 24);
2558 *ptrLBA = physLBA_byte;
2560 &pIO_req->CDB.CDB32[lba_idx + 1];
2561 physLBA_byte = (uint8_t)(physLBA >> 16);
2562 *ptrLBA = physLBA_byte;
2564 &pIO_req->CDB.CDB32[lba_idx + 2];
2565 physLBA_byte = (uint8_t)(physLBA >> 8);
2566 *ptrLBA = physLBA_byte;
2568 &pIO_req->CDB.CDB32[lba_idx + 3];
2569 physLBA_byte = (uint8_t)physLBA;
2570 *ptrLBA = physLBA_byte;
2573 * Set flag that Direct Drive I/O is
2576 cm->cm_flags |= MPS_CM_FLAGS_DD_IO;
2581 * 16-byte CDB and the upper 4 bytes of the CDB are not
2582 * 0. Get the transfer size in blocks.
2584 io_size = (cm->cm_length >> sc->DD_block_exponent);
2589 virtLBA = ((uint64_t)CDB[2] << 54) |
2590 ((uint64_t)CDB[3] << 48) |
2591 ((uint64_t)CDB[4] << 40) |
2592 ((uint64_t)CDB[5] << 32) |
2593 ((uint64_t)CDB[6] << 24) |
2594 ((uint64_t)CDB[7] << 16) |
2595 ((uint64_t)CDB[8] << 8) |
2599 * Check that LBA range for I/O does not exceed volume's
2602 if ((virtLBA + (uint64_t)io_size - 1) <=
2605 * Check if the I/O crosses a stripe boundary.
2606 * If not, translate the virtual LBA to a
2607 * physical LBA and set the DevHandle for the
2608 * PhysDisk to be used. If it does cross a
2609 * boundary, do normal I/O. To get the right
2610 * DevHandle to use, get the map number for the
2611 * column, then use that map number to look up
2612 * the DevHandle of the PhysDisk.
2614 stripe_offset = (uint32_t)virtLBA &
2615 (sc->DD_stripe_size - 1);
2616 if ((stripe_offset + io_size) <=
2617 sc->DD_stripe_size) {
2618 physLBA = (uint32_t)(virtLBA >>
2619 sc->DD_stripe_exponent);
2620 stripe_unit = physLBA /
2621 sc->DD_num_phys_disks;
2623 sc->DD_num_phys_disks;
2624 pIO_req->DevHandle =
2625 htole16(sc->DD_column_map[column].
2627 cm->cm_desc.SCSIIO.DevHandle =
2630 physLBA = (stripe_unit <<
2631 sc->DD_stripe_exponent) +
2635 * Set upper 4 bytes of LBA to 0. We
2636 * assume that the phys disks are less
2637 * than 2 TB's in size. Then, set the
2640 pIO_req->CDB.CDB32[2] = 0;
2641 pIO_req->CDB.CDB32[3] = 0;
2642 pIO_req->CDB.CDB32[4] = 0;
2643 pIO_req->CDB.CDB32[5] = 0;
2644 ptrLBA = &pIO_req->CDB.CDB32[6];
2645 physLBA_byte = (uint8_t)(physLBA >> 24);
2646 *ptrLBA = physLBA_byte;
2647 ptrLBA = &pIO_req->CDB.CDB32[7];
2648 physLBA_byte = (uint8_t)(physLBA >> 16);
2649 *ptrLBA = physLBA_byte;
2650 ptrLBA = &pIO_req->CDB.CDB32[8];
2651 physLBA_byte = (uint8_t)(physLBA >> 8);
2652 *ptrLBA = physLBA_byte;
2653 ptrLBA = &pIO_req->CDB.CDB32[9];
2654 physLBA_byte = (uint8_t)physLBA;
2655 *ptrLBA = physLBA_byte;
2658 * Set flag that Direct Drive I/O is
2661 cm->cm_flags |= MPS_CM_FLAGS_DD_IO;
2669 mpssas_smpio_complete(struct mps_softc *sc, struct mps_command *cm)
2671 MPI2_SMP_PASSTHROUGH_REPLY *rpl;
2672 MPI2_SMP_PASSTHROUGH_REQUEST *req;
2676 ccb = cm->cm_complete_data;
2679 * Currently there should be no way we can hit this case. It only
2680 * happens when we have a failure to allocate chain frames, and SMP
2681 * commands require two S/G elements only. That should be handled
2682 * in the standard request size.
2684 if ((cm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
2685 mps_dprint(sc, MPS_ERROR,"%s: cm_flags = %#x on SMP request!\n",
2686 __func__, cm->cm_flags);
2687 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2691 rpl = (MPI2_SMP_PASSTHROUGH_REPLY *)cm->cm_reply;
2693 mps_dprint(sc, MPS_ERROR, "%s: NULL cm_reply!\n", __func__);
2694 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2698 req = (MPI2_SMP_PASSTHROUGH_REQUEST *)cm->cm_req;
2699 sasaddr = le32toh(req->SASAddress.Low);
2700 sasaddr |= ((uint64_t)(le32toh(req->SASAddress.High))) << 32;
2702 if ((le16toh(rpl->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
2703 MPI2_IOCSTATUS_SUCCESS ||
2704 rpl->SASStatus != MPI2_SASSTATUS_SUCCESS) {
2705 mps_dprint(sc, MPS_XINFO, "%s: IOCStatus %04x SASStatus %02x\n",
2706 __func__, le16toh(rpl->IOCStatus), rpl->SASStatus);
2707 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2711 mps_dprint(sc, MPS_XINFO, "%s: SMP request to SAS address "
2712 "%#jx completed successfully\n", __func__,
2713 (uintmax_t)sasaddr);
2715 if (ccb->smpio.smp_response[2] == SMP_FR_ACCEPTED)
2716 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
2718 mpssas_set_ccbstatus(ccb, CAM_SMP_STATUS_ERROR);
2722 * We sync in both directions because we had DMAs in the S/G list
2723 * in both directions.
2725 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap,
2726 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2727 bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
2728 mps_free_command(sc, cm);
2733 mpssas_send_smpcmd(struct mpssas_softc *sassc, union ccb *ccb, uint64_t sasaddr)
2735 struct mps_command *cm;
2736 uint8_t *request, *response;
2737 MPI2_SMP_PASSTHROUGH_REQUEST *req;
2738 struct mps_softc *sc;
2745 * XXX We don't yet support physical addresses here.
2747 switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) {
2748 case CAM_DATA_PADDR:
2749 case CAM_DATA_SG_PADDR:
2750 mps_dprint(sc, MPS_ERROR,
2751 "%s: physical addresses not supported\n", __func__);
2752 mpssas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2757 * The chip does not support more than one buffer for the
2758 * request or response.
2760 if ((ccb->smpio.smp_request_sglist_cnt > 1)
2761 || (ccb->smpio.smp_response_sglist_cnt > 1)) {
2762 mps_dprint(sc, MPS_ERROR,
2763 "%s: multiple request or response "
2764 "buffer segments not supported for SMP\n",
2766 mpssas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2772 * The CAM_SCATTER_VALID flag was originally implemented
2773 * for the XPT_SCSI_IO CCB, which only has one data pointer.
2774 * We have two. So, just take that flag to mean that we
2775 * might have S/G lists, and look at the S/G segment count
2776 * to figure out whether that is the case for each individual
2779 if (ccb->smpio.smp_request_sglist_cnt != 0) {
2780 bus_dma_segment_t *req_sg;
2782 req_sg = (bus_dma_segment_t *)ccb->smpio.smp_request;
2783 request = (uint8_t *)(uintptr_t)req_sg[0].ds_addr;
2785 request = ccb->smpio.smp_request;
2787 if (ccb->smpio.smp_response_sglist_cnt != 0) {
2788 bus_dma_segment_t *rsp_sg;
2790 rsp_sg = (bus_dma_segment_t *)ccb->smpio.smp_response;
2791 response = (uint8_t *)(uintptr_t)rsp_sg[0].ds_addr;
2793 response = ccb->smpio.smp_response;
2795 case CAM_DATA_VADDR:
2796 request = ccb->smpio.smp_request;
2797 response = ccb->smpio.smp_response;
2800 mpssas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2805 cm = mps_alloc_command(sc);
2807 mps_dprint(sc, MPS_ERROR,
2808 "%s: cannot allocate command\n", __func__);
2809 mpssas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL);
2814 req = (MPI2_SMP_PASSTHROUGH_REQUEST *)cm->cm_req;
2815 bzero(req, sizeof(*req));
2816 req->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
2818 /* Allow the chip to use any route to this SAS address. */
2819 req->PhysicalPort = 0xff;
2821 req->RequestDataLength = htole16(ccb->smpio.smp_request_len);
2823 MPI2_SGLFLAGS_SYSTEM_ADDRESS_SPACE | MPI2_SGLFLAGS_SGL_TYPE_MPI;
2825 mps_dprint(sc, MPS_XINFO, "%s: sending SMP request to SAS "
2826 "address %#jx\n", __func__, (uintmax_t)sasaddr);
2828 mpi_init_sge(cm, req, &req->SGL);
2831 * Set up a uio to pass into mps_map_command(). This allows us to
2832 * do one map command, and one busdma call in there.
2834 cm->cm_uio.uio_iov = cm->cm_iovec;
2835 cm->cm_uio.uio_iovcnt = 2;
2836 cm->cm_uio.uio_segflg = UIO_SYSSPACE;
2839 * The read/write flag isn't used by busdma, but set it just in
2840 * case. This isn't exactly accurate, either, since we're going in
2843 cm->cm_uio.uio_rw = UIO_WRITE;
2845 cm->cm_iovec[0].iov_base = request;
2846 cm->cm_iovec[0].iov_len = le16toh(req->RequestDataLength);
2847 cm->cm_iovec[1].iov_base = response;
2848 cm->cm_iovec[1].iov_len = ccb->smpio.smp_response_len;
2850 cm->cm_uio.uio_resid = cm->cm_iovec[0].iov_len +
2851 cm->cm_iovec[1].iov_len;
2854 * Trigger a warning message in mps_data_cb() for the user if we
2855 * wind up exceeding two S/G segments. The chip expects one
2856 * segment for the request and another for the response.
2858 cm->cm_max_segs = 2;
2860 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2861 cm->cm_complete = mpssas_smpio_complete;
2862 cm->cm_complete_data = ccb;
2865 * Tell the mapping code that we're using a uio, and that this is
2866 * an SMP passthrough request. There is a little special-case
2867 * logic there (in mps_data_cb()) to handle the bidirectional
2870 cm->cm_flags |= MPS_CM_FLAGS_USE_UIO | MPS_CM_FLAGS_SMP_PASS |
2871 MPS_CM_FLAGS_DATAIN | MPS_CM_FLAGS_DATAOUT;
2873 /* The chip data format is little endian. */
2874 req->SASAddress.High = htole32(sasaddr >> 32);
2875 req->SASAddress.Low = htole32(sasaddr);
2878 * XXX Note that we don't have a timeout/abort mechanism here.
2879 * From the manual, it looks like task management requests only
2880 * work for SCSI IO and SATA passthrough requests. We may need to
2881 * have a mechanism to retry requests in the event of a chip reset
2882 * at least. Hopefully the chip will insure that any errors short
2883 * of that are relayed back to the driver.
2885 error = mps_map_command(sc, cm);
2886 if ((error != 0) && (error != EINPROGRESS)) {
2887 mps_dprint(sc, MPS_ERROR,
2888 "%s: error %d returned from mps_map_command()\n",
2896 mps_free_command(sc, cm);
2897 mpssas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL);
2904 mpssas_action_smpio(struct mpssas_softc *sassc, union ccb *ccb)
2906 struct mps_softc *sc;
2907 struct mpssas_target *targ;
2908 uint64_t sasaddr = 0;
2913 * Make sure the target exists.
2915 KASSERT(ccb->ccb_h.target_id < sassc->maxtargets,
2916 ("Target %d out of bounds in XPT_SMP_IO\n", ccb->ccb_h.target_id));
2917 targ = &sassc->targets[ccb->ccb_h.target_id];
2918 if (targ->handle == 0x0) {
2919 mps_dprint(sc, MPS_ERROR,
2920 "%s: target %d does not exist!\n", __func__,
2921 ccb->ccb_h.target_id);
2922 mpssas_set_ccbstatus(ccb, CAM_SEL_TIMEOUT);
2928 * If this device has an embedded SMP target, we'll talk to it
2930 * figure out what the expander's address is.
2932 if ((targ->devinfo & MPI2_SAS_DEVICE_INFO_SMP_TARGET) != 0)
2933 sasaddr = targ->sasaddr;
2936 * If we don't have a SAS address for the expander yet, try
2937 * grabbing it from the page 0x83 information cached in the
2938 * transport layer for this target. LSI expanders report the
2939 * expander SAS address as the port-associated SAS address in
2940 * Inquiry VPD page 0x83. Maxim expanders don't report it in page
2943 * XXX KDM disable this for now, but leave it commented out so that
2944 * it is obvious that this is another possible way to get the SAS
2947 * The parent handle method below is a little more reliable, and
2948 * the other benefit is that it works for devices other than SES
2949 * devices. So you can send a SMP request to a da(4) device and it
2950 * will get routed to the expander that device is attached to.
2951 * (Assuming the da(4) device doesn't contain an SMP target...)
2955 sasaddr = xpt_path_sas_addr(ccb->ccb_h.path);
2959 * If we still don't have a SAS address for the expander, look for
2960 * the parent device of this device, which is probably the expander.
2963 #ifdef OLD_MPS_PROBE
2964 struct mpssas_target *parent_target;
2967 if (targ->parent_handle == 0x0) {
2968 mps_dprint(sc, MPS_ERROR,
2969 "%s: handle %d does not have a valid "
2970 "parent handle!\n", __func__, targ->handle);
2971 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
2974 #ifdef OLD_MPS_PROBE
2975 parent_target = mpssas_find_target_by_handle(sassc, 0,
2976 targ->parent_handle);
2978 if (parent_target == NULL) {
2979 mps_dprint(sc, MPS_ERROR,
2980 "%s: handle %d does not have a valid "
2981 "parent target!\n", __func__, targ->handle);
2982 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
2986 if ((parent_target->devinfo &
2987 MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0) {
2988 mps_dprint(sc, MPS_ERROR,
2989 "%s: handle %d parent %d does not "
2990 "have an SMP target!\n", __func__,
2991 targ->handle, parent_target->handle);
2992 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
2996 sasaddr = parent_target->sasaddr;
2997 #else /* OLD_MPS_PROBE */
2998 if ((targ->parent_devinfo &
2999 MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0) {
3000 mps_dprint(sc, MPS_ERROR,
3001 "%s: handle %d parent %d does not "
3002 "have an SMP target!\n", __func__,
3003 targ->handle, targ->parent_handle);
3004 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3007 if (targ->parent_sasaddr == 0x0) {
3008 mps_dprint(sc, MPS_ERROR,
3009 "%s: handle %d parent handle %d does "
3010 "not have a valid SAS address!\n",
3011 __func__, targ->handle, targ->parent_handle);
3012 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3016 sasaddr = targ->parent_sasaddr;
3017 #endif /* OLD_MPS_PROBE */
3021 mps_dprint(sc, MPS_INFO,
3022 "%s: unable to find SAS address for handle %d\n",
3023 __func__, targ->handle);
3024 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3027 mpssas_send_smpcmd(sassc, ccb, sasaddr);
3037 mpssas_action_resetdev(struct mpssas_softc *sassc, union ccb *ccb)
3039 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
3040 struct mps_softc *sc;
3041 struct mps_command *tm;
3042 struct mpssas_target *targ;
3044 MPS_FUNCTRACE(sassc->sc);
3045 mtx_assert(&sassc->sc->mps_mtx, MA_OWNED);
3047 KASSERT(ccb->ccb_h.target_id < sassc->maxtargets,
3048 ("Target %d out of bounds in XPT_RESET_DEV\n",
3049 ccb->ccb_h.target_id));
3051 tm = mpssas_alloc_tm(sc);
3053 mps_dprint(sc, MPS_ERROR,
3054 "command alloc failure in mpssas_action_resetdev\n");
3055 mpssas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL);
3060 targ = &sassc->targets[ccb->ccb_h.target_id];
3061 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
3062 req->DevHandle = htole16(targ->handle);
3063 req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3065 /* SAS Hard Link Reset / SATA Link Reset */
3066 req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
3069 tm->cm_complete = mpssas_resetdev_complete;
3070 tm->cm_complete_data = ccb;
3073 mpssas_prepare_for_tm(sc, tm, targ, CAM_LUN_WILDCARD);
3074 mps_map_command(sc, tm);
3078 mpssas_resetdev_complete(struct mps_softc *sc, struct mps_command *tm)
3080 MPI2_SCSI_TASK_MANAGE_REPLY *resp;
3084 mtx_assert(&sc->mps_mtx, MA_OWNED);
3086 resp = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
3087 ccb = tm->cm_complete_data;
3090 * Currently there should be no way we can hit this case. It only
3091 * happens when we have a failure to allocate chain frames, and
3092 * task management commands don't have S/G lists.
3094 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
3095 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
3097 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
3099 mps_dprint(sc, MPS_ERROR,
3100 "%s: cm_flags = %#x for reset of handle %#04x! "
3101 "This should not happen!\n", __func__, tm->cm_flags,
3103 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
3107 mps_dprint(sc, MPS_XINFO,
3108 "%s: IOCStatus = 0x%x ResponseCode = 0x%x\n", __func__,
3109 le16toh(resp->IOCStatus), le32toh(resp->ResponseCode));
3111 if (le32toh(resp->ResponseCode) == MPI2_SCSITASKMGMT_RSP_TM_COMPLETE) {
3112 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
3113 mpssas_announce_reset(sc, AC_SENT_BDR, tm->cm_targ->tid,
3117 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
3121 mpssas_free_tm(sc, tm);
3126 mpssas_poll(struct cam_sim *sim)
3128 struct mpssas_softc *sassc;
3130 sassc = cam_sim_softc(sim);
3132 if (sassc->sc->mps_debug & MPS_TRACE) {
3133 /* frequent debug messages during a panic just slow
3134 * everything down too much.
3136 mps_printf(sassc->sc, "%s clearing MPS_TRACE\n", __func__);
3137 sassc->sc->mps_debug &= ~MPS_TRACE;
3140 mps_intr_locked(sassc->sc);
3144 mpssas_async(void *callback_arg, uint32_t code, struct cam_path *path,
3147 struct mps_softc *sc;
3149 sc = (struct mps_softc *)callback_arg;
3152 case AC_ADVINFO_CHANGED: {
3153 struct mpssas_target *target;
3154 struct mpssas_softc *sassc;
3155 struct scsi_read_capacity_data_long rcap_buf;
3156 struct ccb_dev_advinfo cdai;
3157 struct mpssas_lun *lun;
3162 buftype = (uintptr_t)arg;
3168 * We're only interested in read capacity data changes.
3170 if (buftype != CDAI_TYPE_RCAPLONG)
3174 * We should have a handle for this, but check to make sure.
3176 KASSERT(xpt_path_target_id(path) < sassc->maxtargets,
3177 ("Target %d out of bounds in mpssas_async\n",
3178 xpt_path_target_id(path)));
3179 target = &sassc->targets[xpt_path_target_id(path)];
3180 if (target->handle == 0)
3183 lunid = xpt_path_lun_id(path);
3185 SLIST_FOREACH(lun, &target->luns, lun_link) {
3186 if (lun->lun_id == lunid) {
3192 if (found_lun == 0) {
3193 lun = malloc(sizeof(struct mpssas_lun), M_MPT2,
3196 mps_dprint(sc, MPS_ERROR, "Unable to alloc "
3197 "LUN for EEDP support.\n");
3200 lun->lun_id = lunid;
3201 SLIST_INSERT_HEAD(&target->luns, lun, lun_link);
3204 bzero(&rcap_buf, sizeof(rcap_buf));
3205 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
3206 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3207 cdai.ccb_h.flags = CAM_DIR_IN;
3208 cdai.buftype = CDAI_TYPE_RCAPLONG;
3209 cdai.flags = CDAI_FLAG_NONE;
3210 cdai.bufsiz = sizeof(rcap_buf);
3211 cdai.buf = (uint8_t *)&rcap_buf;
3212 xpt_action((union ccb *)&cdai);
3213 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3214 cam_release_devq(cdai.ccb_h.path,
3217 if ((mpssas_get_ccbstatus((union ccb *)&cdai) == CAM_REQ_CMP)
3218 && (rcap_buf.prot & SRC16_PROT_EN)) {
3219 switch (rcap_buf.prot & SRC16_P_TYPE) {
3222 lun->eedp_formatted = TRUE;
3223 lun->eedp_block_size =
3224 scsi_4btoul(rcap_buf.length);
3228 lun->eedp_formatted = FALSE;
3229 lun->eedp_block_size = 0;
3233 lun->eedp_formatted = FALSE;
3234 lun->eedp_block_size = 0;
3244 * Set the INRESET flag for this target so that no I/O will be sent to
3245 * the target until the reset has completed. If an I/O request does
3246 * happen, the devq will be frozen. The CCB holds the path which is
3247 * used to release the devq. The devq is released and the CCB is freed
3248 * when the TM completes.
3251 mpssas_prepare_for_tm(struct mps_softc *sc, struct mps_command *tm,
3252 struct mpssas_target *target, lun_id_t lun_id)
3257 ccb = xpt_alloc_ccb_nowait();
3259 path_id = cam_sim_path(sc->sassc->sim);
3260 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, path_id,
3261 target->tid, lun_id) != CAM_REQ_CMP) {
3265 tm->cm_targ = target;
3266 target->flags |= MPSSAS_TARGET_INRESET;
3272 mpssas_startup(struct mps_softc *sc)
3276 * Send the port enable message and set the wait_for_port_enable flag.
3277 * This flag helps to keep the simq frozen until all discovery events
3280 sc->wait_for_port_enable = 1;
3281 mpssas_send_portenable(sc);
3286 mpssas_send_portenable(struct mps_softc *sc)
3288 MPI2_PORT_ENABLE_REQUEST *request;
3289 struct mps_command *cm;
3293 if ((cm = mps_alloc_command(sc)) == NULL)
3295 request = (MPI2_PORT_ENABLE_REQUEST *)cm->cm_req;
3296 request->Function = MPI2_FUNCTION_PORT_ENABLE;
3297 request->MsgFlags = 0;
3299 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3300 cm->cm_complete = mpssas_portenable_complete;
3304 mps_map_command(sc, cm);
3305 mps_dprint(sc, MPS_XINFO,
3306 "mps_send_portenable finished cm %p req %p complete %p\n",
3307 cm, cm->cm_req, cm->cm_complete);
3312 mpssas_portenable_complete(struct mps_softc *sc, struct mps_command *cm)
3314 MPI2_PORT_ENABLE_REPLY *reply;
3315 struct mpssas_softc *sassc;
3321 * Currently there should be no way we can hit this case. It only
3322 * happens when we have a failure to allocate chain frames, and
3323 * port enable commands don't have S/G lists.
3325 if ((cm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
3326 mps_dprint(sc, MPS_ERROR, "%s: cm_flags = %#x for port enable! "
3327 "This should not happen!\n", __func__, cm->cm_flags);
3330 reply = (MPI2_PORT_ENABLE_REPLY *)cm->cm_reply;
3332 mps_dprint(sc, MPS_FAULT, "Portenable NULL reply\n");
3333 else if (le16toh(reply->IOCStatus & MPI2_IOCSTATUS_MASK) !=
3334 MPI2_IOCSTATUS_SUCCESS)
3335 mps_dprint(sc, MPS_FAULT, "Portenable failed\n");
3337 mps_free_command(sc, cm);
3340 * Get WarpDrive info after discovery is complete but before the scan
3341 * starts. At this point, all devices are ready to be exposed to the
3342 * OS. If devices should be hidden instead, take them out of the
3343 * 'targets' array before the scan. The devinfo for a disk will have
3344 * some info and a volume's will be 0. Use that to remove disks.
3346 mps_wd_config_pages(sc);
3349 * Done waiting for port enable to complete. Decrement the refcount.
3350 * If refcount is 0, discovery is complete and a rescan of the bus can
3351 * take place. Since the simq was explicitly frozen before port
3352 * enable, it must be explicitly released here to keep the
3353 * freeze/release count in sync.
3355 sc->wait_for_port_enable = 0;
3356 sc->port_enable_complete = 1;
3357 wakeup(&sc->port_enable_complete);
3358 mpssas_startup_decrement(sassc);
3362 mpssas_check_id(struct mpssas_softc *sassc, int id)
3364 struct mps_softc *sc = sassc->sc;
3368 ids = &sc->exclude_ids[0];
3369 while((name = strsep(&ids, ",")) != NULL) {
3370 if (name[0] == '\0')
3372 if (strtol(name, NULL, 0) == (long)id)
3380 mpssas_realloc_targets(struct mps_softc *sc, int maxtargets)
3382 struct mpssas_softc *sassc;
3383 struct mpssas_lun *lun, *lun_tmp;
3384 struct mpssas_target *targ;
3389 * The number of targets is based on IOC Facts, so free all of
3390 * the allocated LUNs for each target and then the target buffer
3393 for (i=0; i< maxtargets; i++) {
3394 targ = &sassc->targets[i];
3395 SLIST_FOREACH_SAFE(lun, &targ->luns, lun_link, lun_tmp) {
3399 free(sassc->targets, M_MPT2);
3401 sassc->targets = malloc(sizeof(struct mpssas_target) * maxtargets,
3402 M_MPT2, M_WAITOK|M_ZERO);