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));
351 mpssas_remove_volume(struct mps_softc *sc, struct mps_command *tm)
353 MPI2_SCSI_TASK_MANAGE_REPLY *reply;
354 struct mpssas_target *targ;
359 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
360 handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
364 /* XXX retry the remove after the diag reset completes? */
365 mps_dprint(sc, MPS_FAULT,
366 "%s NULL reply resetting device 0x%04x\n", __func__,
368 mpssas_free_tm(sc, tm);
372 if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
373 MPI2_IOCSTATUS_SUCCESS) {
374 mps_dprint(sc, MPS_ERROR,
375 "IOCStatus = 0x%x while resetting device 0x%x\n",
376 le16toh(reply->IOCStatus), handle);
379 mps_dprint(sc, MPS_XINFO,
380 "Reset aborted %u commands\n", reply->TerminationCount);
381 mps_free_reply(sc, tm->cm_reply_data);
382 tm->cm_reply = NULL; /* Ensures the reply won't get re-freed */
384 mps_dprint(sc, MPS_XINFO,
385 "clearing target %u handle 0x%04x\n", targ->tid, handle);
388 * Don't clear target if remove fails because things will get confusing.
389 * Leave the devname and sasaddr intact so that we know to avoid reusing
390 * this target id if possible, and so we can assign the same target id
391 * to this device if it comes back in the future.
393 if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) ==
394 MPI2_IOCSTATUS_SUCCESS) {
397 targ->encl_handle = 0x0;
398 targ->encl_slot = 0x0;
399 targ->exp_dev_handle = 0x0;
401 targ->linkrate = 0x0;
406 mpssas_free_tm(sc, tm);
411 * No Need to call "MPI2_SAS_OP_REMOVE_DEVICE" For Volume removal.
412 * Otherwise Volume Delete is same as Bare Drive Removal.
415 mpssas_prepare_volume_remove(struct mpssas_softc *sassc, uint16_t handle)
417 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
418 struct mps_softc *sc;
419 struct mps_command *tm;
420 struct mpssas_target *targ = NULL;
422 MPS_FUNCTRACE(sassc->sc);
427 * If this is a WD controller, determine if the disk should be exposed
428 * to the OS or not. If disk should be exposed, return from this
429 * function without doing anything.
431 if (sc->WD_available && (sc->WD_hide_expose ==
432 MPS_WD_EXPOSE_ALWAYS)) {
437 targ = mpssas_find_target_by_handle(sassc, 0, handle);
439 /* FIXME: what is the action? */
440 /* We don't know about this device? */
441 mps_dprint(sc, MPS_ERROR,
442 "%s %d : invalid handle 0x%x \n", __func__,__LINE__, handle);
446 targ->flags |= MPSSAS_TARGET_INREMOVAL;
448 tm = mpssas_alloc_tm(sc);
450 mps_dprint(sc, MPS_ERROR,
451 "%s: command alloc failure\n", __func__);
455 mpssas_rescan_target(sc, targ);
457 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
458 req->DevHandle = targ->handle;
459 req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
461 /* SAS Hard Link Reset / SATA Link Reset */
462 req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
466 tm->cm_complete = mpssas_remove_volume;
467 tm->cm_complete_data = (void *)(uintptr_t)handle;
469 mps_dprint(sc, MPS_INFO, "%s: Sending reset for target ID %d\n",
470 __func__, targ->tid);
471 mpssas_prepare_for_tm(sc, tm, targ, CAM_LUN_WILDCARD);
473 mps_map_command(sc, tm);
477 * The MPT2 firmware performs debounce on the link to avoid transient link
478 * errors and false removals. When it does decide that link has been lost
479 * and a device need to go away, it expects that the host will perform a
480 * target reset and then an op remove. The reset has the side-effect of
481 * aborting any outstanding requests for the device, which is required for
482 * the op-remove to succeed. It's not clear if the host should check for
483 * the device coming back alive after the reset.
486 mpssas_prepare_remove(struct mpssas_softc *sassc, uint16_t handle)
488 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
489 struct mps_softc *sc;
490 struct mps_command *cm;
491 struct mpssas_target *targ = NULL;
493 MPS_FUNCTRACE(sassc->sc);
497 targ = mpssas_find_target_by_handle(sassc, 0, handle);
499 /* FIXME: what is the action? */
500 /* We don't know about this device? */
501 mps_dprint(sc, MPS_ERROR,
502 "%s : invalid handle 0x%x \n", __func__, handle);
506 targ->flags |= MPSSAS_TARGET_INREMOVAL;
508 cm = mpssas_alloc_tm(sc);
510 mps_dprint(sc, MPS_ERROR,
511 "%s: command alloc failure\n", __func__);
515 mpssas_rescan_target(sc, targ);
517 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)cm->cm_req;
518 req->DevHandle = htole16(targ->handle);
519 req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
521 /* SAS Hard Link Reset / SATA Link Reset */
522 req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
526 cm->cm_complete = mpssas_remove_device;
527 cm->cm_complete_data = (void *)(uintptr_t)handle;
529 mps_dprint(sc, MPS_INFO, "%s: Sending reset for target ID %d\n",
530 __func__, targ->tid);
531 mpssas_prepare_for_tm(sc, cm, targ, CAM_LUN_WILDCARD);
533 mps_map_command(sc, cm);
537 mpssas_remove_device(struct mps_softc *sc, struct mps_command *tm)
539 MPI2_SCSI_TASK_MANAGE_REPLY *reply;
540 MPI2_SAS_IOUNIT_CONTROL_REQUEST *req;
541 struct mpssas_target *targ;
546 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
547 handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
551 * Currently there should be no way we can hit this case. It only
552 * happens when we have a failure to allocate chain frames, and
553 * task management commands don't have S/G lists.
555 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
556 mps_dprint(sc, MPS_ERROR,
557 "%s: cm_flags = %#x for remove of handle %#04x! "
558 "This should not happen!\n", __func__, tm->cm_flags,
563 /* XXX retry the remove after the diag reset completes? */
564 mps_dprint(sc, MPS_FAULT,
565 "%s NULL reply resetting device 0x%04x\n", __func__,
567 mpssas_free_tm(sc, tm);
571 if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
572 MPI2_IOCSTATUS_SUCCESS) {
573 mps_dprint(sc, MPS_ERROR,
574 "IOCStatus = 0x%x while resetting device 0x%x\n",
575 le16toh(reply->IOCStatus), handle);
578 mps_dprint(sc, MPS_XINFO, "Reset aborted %u commands\n",
579 le32toh(reply->TerminationCount));
580 mps_free_reply(sc, tm->cm_reply_data);
581 tm->cm_reply = NULL; /* Ensures the reply won't get re-freed */
583 /* Reuse the existing command */
584 req = (MPI2_SAS_IOUNIT_CONTROL_REQUEST *)tm->cm_req;
585 memset(req, 0, sizeof(*req));
586 req->Function = MPI2_FUNCTION_SAS_IO_UNIT_CONTROL;
587 req->Operation = MPI2_SAS_OP_REMOVE_DEVICE;
588 req->DevHandle = htole16(handle);
590 tm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
591 tm->cm_complete = mpssas_remove_complete;
592 tm->cm_complete_data = (void *)(uintptr_t)handle;
595 * Wait to send the REMOVE_DEVICE until all the commands have cleared.
596 * They should be aborted or time out and we'll kick thus off there
599 if (TAILQ_FIRST(&targ->commands) == NULL) {
600 mps_dprint(sc, MPS_INFO, "No pending commands: starting remove_device\n");
601 mps_map_command(sc, tm);
602 targ->pending_remove_tm = NULL;
604 targ->pending_remove_tm = tm;
608 mps_dprint(sc, MPS_XINFO, "clearing target %u handle 0x%04x\n",
613 mpssas_remove_complete(struct mps_softc *sc, struct mps_command *tm)
615 MPI2_SAS_IOUNIT_CONTROL_REPLY *reply;
617 struct mpssas_target *targ;
618 struct mpssas_lun *lun;
622 reply = (MPI2_SAS_IOUNIT_CONTROL_REPLY *)tm->cm_reply;
623 handle = (uint16_t)(uintptr_t)tm->cm_complete_data;
627 * At this point, we should have no pending commands for the target.
628 * The remove target has just completed.
630 KASSERT(TAILQ_FIRST(&targ->commands) == NULL,
631 ("%s: no commands should be pending\n", __func__));
635 * Currently there should be no way we can hit this case. It only
636 * happens when we have a failure to allocate chain frames, and
637 * task management commands don't have S/G lists.
639 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
640 mps_dprint(sc, MPS_XINFO,
641 "%s: cm_flags = %#x for remove of handle %#04x! "
642 "This should not happen!\n", __func__, tm->cm_flags,
644 mpssas_free_tm(sc, tm);
649 /* most likely a chip reset */
650 mps_dprint(sc, MPS_FAULT,
651 "%s NULL reply removing device 0x%04x\n", __func__, handle);
652 mpssas_free_tm(sc, tm);
656 mps_dprint(sc, MPS_XINFO,
657 "%s on handle 0x%04x, IOCStatus= 0x%x\n", __func__,
658 handle, le16toh(reply->IOCStatus));
661 * Don't clear target if remove fails because things will get confusing.
662 * Leave the devname and sasaddr intact so that we know to avoid reusing
663 * this target id if possible, and so we can assign the same target id
664 * to this device if it comes back in the future.
666 if ((le16toh(reply->IOCStatus) & MPI2_IOCSTATUS_MASK) ==
667 MPI2_IOCSTATUS_SUCCESS) {
669 targ->encl_handle = 0x0;
670 targ->encl_slot = 0x0;
671 targ->exp_dev_handle = 0x0;
673 targ->linkrate = 0x0;
677 while(!SLIST_EMPTY(&targ->luns)) {
678 lun = SLIST_FIRST(&targ->luns);
679 SLIST_REMOVE_HEAD(&targ->luns, lun_link);
685 mpssas_free_tm(sc, tm);
689 mpssas_register_events(struct mps_softc *sc)
691 u32 events[MPI2_EVENT_NOTIFY_EVENTMASK_WORDS];
694 setbit(events, MPI2_EVENT_SAS_DEVICE_STATUS_CHANGE);
695 setbit(events, MPI2_EVENT_SAS_DISCOVERY);
696 setbit(events, MPI2_EVENT_SAS_BROADCAST_PRIMITIVE);
697 setbit(events, MPI2_EVENT_SAS_INIT_DEVICE_STATUS_CHANGE);
698 setbit(events, MPI2_EVENT_SAS_INIT_TABLE_OVERFLOW);
699 setbit(events, MPI2_EVENT_SAS_TOPOLOGY_CHANGE_LIST);
700 setbit(events, MPI2_EVENT_SAS_ENCL_DEVICE_STATUS_CHANGE);
701 setbit(events, MPI2_EVENT_IR_CONFIGURATION_CHANGE_LIST);
702 setbit(events, MPI2_EVENT_IR_VOLUME);
703 setbit(events, MPI2_EVENT_IR_PHYSICAL_DISK);
704 setbit(events, MPI2_EVENT_IR_OPERATION_STATUS);
705 setbit(events, MPI2_EVENT_LOG_ENTRY_ADDED);
707 mps_register_events(sc, events, mpssas_evt_handler, NULL,
708 &sc->sassc->mpssas_eh);
714 mps_attach_sas(struct mps_softc *sc)
716 struct mpssas_softc *sassc;
718 int unit, error = 0, reqs;
721 mps_dprint(sc, MPS_INIT, "%s entered\n", __func__);
723 sassc = malloc(sizeof(struct mpssas_softc), M_MPT2, M_WAITOK|M_ZERO);
726 * XXX MaxTargets could change during a reinit. Since we don't
727 * resize the targets[] array during such an event, cache the value
728 * of MaxTargets here so that we don't get into trouble later. This
729 * should move into the reinit logic.
731 sassc->maxtargets = sc->facts->MaxTargets + sc->facts->MaxVolumes;
732 sassc->targets = malloc(sizeof(struct mpssas_target) *
733 sassc->maxtargets, M_MPT2, M_WAITOK|M_ZERO);
737 reqs = sc->num_reqs - sc->num_prireqs - 1;
738 if ((sassc->devq = cam_simq_alloc(reqs)) == NULL) {
739 mps_dprint(sc, MPS_ERROR, "Cannot allocate SIMQ\n");
744 unit = device_get_unit(sc->mps_dev);
745 sassc->sim = cam_sim_alloc(mpssas_action, mpssas_poll, "mps", sassc,
746 unit, &sc->mps_mtx, reqs, reqs, sassc->devq);
747 if (sassc->sim == NULL) {
748 mps_dprint(sc, MPS_INIT|MPS_ERROR, "Cannot allocate SIM\n");
753 TAILQ_INIT(&sassc->ev_queue);
755 /* Initialize taskqueue for Event Handling */
756 TASK_INIT(&sassc->ev_task, 0, mpssas_firmware_event_work, sc);
757 sassc->ev_tq = taskqueue_create("mps_taskq", M_NOWAIT | M_ZERO,
758 taskqueue_thread_enqueue, &sassc->ev_tq);
759 taskqueue_start_threads(&sassc->ev_tq, 1, PRIBIO, "%s taskq",
760 device_get_nameunit(sc->mps_dev));
765 * XXX There should be a bus for every port on the adapter, but since
766 * we're just going to fake the topology for now, we'll pretend that
767 * everything is just a target on a single bus.
769 if ((error = xpt_bus_register(sassc->sim, sc->mps_dev, 0)) != 0) {
770 mps_dprint(sc, MPS_INIT|MPS_ERROR,
771 "Error %d registering SCSI bus\n", error);
777 * Assume that discovery events will start right away.
779 * Hold off boot until discovery is complete.
781 sassc->flags |= MPSSAS_IN_STARTUP | MPSSAS_IN_DISCOVERY;
782 sc->sassc->startup_refcount = 0;
783 mpssas_startup_increment(sassc);
785 callout_init(&sassc->discovery_callout, 1 /*mpsafe*/);
788 * Register for async events so we can determine the EEDP
789 * capabilities of devices.
791 status = xpt_create_path(&sassc->path, /*periph*/NULL,
792 cam_sim_path(sc->sassc->sim), CAM_TARGET_WILDCARD,
794 if (status != CAM_REQ_CMP) {
795 mps_dprint(sc, MPS_ERROR|MPS_INIT,
796 "Error %#x creating sim path\n", status);
801 event = AC_ADVINFO_CHANGED;
802 status = xpt_register_async(event, mpssas_async, sc,
804 if (status != CAM_REQ_CMP) {
805 mps_dprint(sc, MPS_ERROR,
806 "Error %#x registering async handler for "
807 "AC_ADVINFO_CHANGED events\n", status);
808 xpt_free_path(sassc->path);
812 if (status != CAM_REQ_CMP) {
814 * EEDP use is the exception, not the rule.
815 * Warn the user, but do not fail to attach.
817 mps_printf(sc, "EEDP capabilities disabled.\n");
822 mpssas_register_events(sc);
827 mps_dprint(sc, MPS_INIT, "%s exit error= %d\n", __func__, error);
832 mps_detach_sas(struct mps_softc *sc)
834 struct mpssas_softc *sassc;
835 struct mpssas_lun *lun, *lun_tmp;
836 struct mpssas_target *targ;
841 if (sc->sassc == NULL)
845 mps_deregister_events(sc, sassc->mpssas_eh);
848 * Drain and free the event handling taskqueue with the lock
849 * unheld so that any parallel processing tasks drain properly
850 * without deadlocking.
852 if (sassc->ev_tq != NULL)
853 taskqueue_free(sassc->ev_tq);
855 /* Make sure CAM doesn't wedge if we had to bail out early. */
858 while (sassc->startup_refcount != 0)
859 mpssas_startup_decrement(sassc);
861 /* Deregister our async handler */
862 if (sassc->path != NULL) {
863 xpt_register_async(0, mpssas_async, sc, sassc->path);
864 xpt_free_path(sassc->path);
868 if (sassc->flags & MPSSAS_IN_STARTUP)
869 xpt_release_simq(sassc->sim, 1);
871 if (sassc->sim != NULL) {
872 xpt_bus_deregister(cam_sim_path(sassc->sim));
873 cam_sim_free(sassc->sim, FALSE);
878 if (sassc->devq != NULL)
879 cam_simq_free(sassc->devq);
881 for(i=0; i< sassc->maxtargets ;i++) {
882 targ = &sassc->targets[i];
883 SLIST_FOREACH_SAFE(lun, &targ->luns, lun_link, lun_tmp) {
887 free(sassc->targets, M_MPT2);
895 mpssas_discovery_end(struct mpssas_softc *sassc)
897 struct mps_softc *sc = sassc->sc;
901 if (sassc->flags & MPSSAS_DISCOVERY_TIMEOUT_PENDING)
902 callout_stop(&sassc->discovery_callout);
905 * After discovery has completed, check the mapping table for any
906 * missing devices and update their missing counts. Only do this once
907 * whenever the driver is initialized so that missing counts aren't
908 * updated unnecessarily. Note that just because discovery has
909 * completed doesn't mean that events have been processed yet. The
910 * check_devices function is a callout timer that checks if ALL devices
911 * are missing. If so, it will wait a little longer for events to
912 * complete and keep resetting itself until some device in the mapping
913 * table is not missing, meaning that event processing has started.
915 if (sc->track_mapping_events) {
916 mps_dprint(sc, MPS_XINFO | MPS_MAPPING, "Discovery has "
917 "completed. Check for missing devices in the mapping "
919 callout_reset(&sc->device_check_callout,
920 MPS_MISSING_CHECK_DELAY * hz, mps_mapping_check_devices,
926 mpssas_action(struct cam_sim *sim, union ccb *ccb)
928 struct mpssas_softc *sassc;
930 sassc = cam_sim_softc(sim);
932 MPS_FUNCTRACE(sassc->sc);
933 mps_dprint(sassc->sc, MPS_TRACE, "ccb func_code 0x%x\n",
934 ccb->ccb_h.func_code);
935 mtx_assert(&sassc->sc->mps_mtx, MA_OWNED);
937 switch (ccb->ccb_h.func_code) {
940 struct ccb_pathinq *cpi = &ccb->cpi;
941 struct mps_softc *sc = sassc->sc;
943 cpi->version_num = 1;
944 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
945 cpi->target_sprt = 0;
946 cpi->hba_misc = PIM_NOBUSRESET | PIM_UNMAPPED | PIM_NOSCAN;
947 cpi->hba_eng_cnt = 0;
948 cpi->max_target = sassc->maxtargets - 1;
952 * initiator_id is set here to an ID outside the set of valid
953 * target IDs (including volumes).
955 cpi->initiator_id = sassc->maxtargets;
956 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
957 strlcpy(cpi->hba_vid, "Avago Tech", HBA_IDLEN);
958 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
959 cpi->unit_number = cam_sim_unit(sim);
960 cpi->bus_id = cam_sim_bus(sim);
961 cpi->base_transfer_speed = 150000;
962 cpi->transport = XPORT_SAS;
963 cpi->transport_version = 0;
964 cpi->protocol = PROTO_SCSI;
965 cpi->protocol_version = SCSI_REV_SPC;
966 cpi->maxio = sc->maxio;
967 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
970 case XPT_GET_TRAN_SETTINGS:
972 struct ccb_trans_settings *cts;
973 struct ccb_trans_settings_sas *sas;
974 struct ccb_trans_settings_scsi *scsi;
975 struct mpssas_target *targ;
978 sas = &cts->xport_specific.sas;
979 scsi = &cts->proto_specific.scsi;
981 KASSERT(cts->ccb_h.target_id < sassc->maxtargets,
982 ("Target %d out of bounds in XPT_GET_TRANS_SETTINGS\n",
983 cts->ccb_h.target_id));
984 targ = &sassc->targets[cts->ccb_h.target_id];
985 if (targ->handle == 0x0) {
986 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
990 cts->protocol_version = SCSI_REV_SPC2;
991 cts->transport = XPORT_SAS;
992 cts->transport_version = 0;
994 sas->valid = CTS_SAS_VALID_SPEED;
995 switch (targ->linkrate) {
997 sas->bitrate = 150000;
1000 sas->bitrate = 300000;
1003 sas->bitrate = 600000;
1009 cts->protocol = PROTO_SCSI;
1010 scsi->valid = CTS_SCSI_VALID_TQ;
1011 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
1013 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
1016 case XPT_CALC_GEOMETRY:
1017 cam_calc_geometry(&ccb->ccg, /*extended*/1);
1018 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
1021 mps_dprint(sassc->sc, MPS_XINFO, "mpssas_action XPT_RESET_DEV\n");
1022 mpssas_action_resetdev(sassc, ccb);
1027 mps_dprint(sassc->sc, MPS_XINFO,
1028 "mpssas_action faking success for abort or reset\n");
1029 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
1032 mpssas_action_scsiio(sassc, ccb);
1035 mpssas_action_smpio(sassc, ccb);
1038 mpssas_set_ccbstatus(ccb, CAM_FUNC_NOTAVAIL);
1046 mpssas_announce_reset(struct mps_softc *sc, uint32_t ac_code,
1047 target_id_t target_id, lun_id_t lun_id)
1049 path_id_t path_id = cam_sim_path(sc->sassc->sim);
1050 struct cam_path *path;
1052 mps_dprint(sc, MPS_XINFO, "%s code %x target %d lun %jx\n", __func__,
1053 ac_code, target_id, (uintmax_t)lun_id);
1055 if (xpt_create_path(&path, NULL,
1056 path_id, target_id, lun_id) != CAM_REQ_CMP) {
1057 mps_dprint(sc, MPS_ERROR, "unable to create path for reset "
1062 xpt_async(ac_code, path, NULL);
1063 xpt_free_path(path);
1067 mpssas_complete_all_commands(struct mps_softc *sc)
1069 struct mps_command *cm;
1074 mtx_assert(&sc->mps_mtx, MA_OWNED);
1076 /* complete all commands with a NULL reply */
1077 for (i = 1; i < sc->num_reqs; i++) {
1078 cm = &sc->commands[i];
1079 if (cm->cm_state == MPS_CM_STATE_FREE)
1082 cm->cm_state = MPS_CM_STATE_BUSY;
1083 cm->cm_reply = NULL;
1086 if (cm->cm_flags & MPS_CM_FLAGS_SATA_ID_TIMEOUT) {
1088 free(cm->cm_data, M_MPT2);
1092 if (cm->cm_flags & MPS_CM_FLAGS_POLLED)
1093 cm->cm_flags |= MPS_CM_FLAGS_COMPLETE;
1095 if (cm->cm_complete != NULL) {
1096 mpssas_log_command(cm, MPS_RECOVERY,
1097 "completing cm %p state %x ccb %p for diag reset\n",
1098 cm, cm->cm_state, cm->cm_ccb);
1100 cm->cm_complete(sc, cm);
1102 } else if (cm->cm_flags & MPS_CM_FLAGS_WAKEUP) {
1103 mpssas_log_command(cm, MPS_RECOVERY,
1104 "waking up cm %p state %x ccb %p for diag reset\n",
1105 cm, cm->cm_state, cm->cm_ccb);
1110 if ((completed == 0) && (cm->cm_state != MPS_CM_STATE_FREE)) {
1111 /* this should never happen, but if it does, log */
1112 mpssas_log_command(cm, MPS_RECOVERY,
1113 "cm %p state %x flags 0x%x ccb %p during diag "
1114 "reset\n", cm, cm->cm_state, cm->cm_flags,
1119 sc->io_cmds_active = 0;
1123 mpssas_handle_reinit(struct mps_softc *sc)
1127 /* Go back into startup mode and freeze the simq, so that CAM
1128 * doesn't send any commands until after we've rediscovered all
1129 * targets and found the proper device handles for them.
1131 * After the reset, portenable will trigger discovery, and after all
1132 * discovery-related activities have finished, the simq will be
1135 mps_dprint(sc, MPS_INIT, "%s startup\n", __func__);
1136 sc->sassc->flags |= MPSSAS_IN_STARTUP;
1137 sc->sassc->flags |= MPSSAS_IN_DISCOVERY;
1138 mpssas_startup_increment(sc->sassc);
1140 /* notify CAM of a bus reset */
1141 mpssas_announce_reset(sc, AC_BUS_RESET, CAM_TARGET_WILDCARD,
1144 /* complete and cleanup after all outstanding commands */
1145 mpssas_complete_all_commands(sc);
1147 mps_dprint(sc, MPS_INIT,
1148 "%s startup %u after command completion\n", __func__,
1149 sc->sassc->startup_refcount);
1151 /* zero all the target handles, since they may change after the
1152 * reset, and we have to rediscover all the targets and use the new
1155 for (i = 0; i < sc->sassc->maxtargets; i++) {
1156 if (sc->sassc->targets[i].outstanding != 0)
1157 mps_dprint(sc, MPS_INIT, "target %u outstanding %u\n",
1158 i, sc->sassc->targets[i].outstanding);
1159 sc->sassc->targets[i].handle = 0x0;
1160 sc->sassc->targets[i].exp_dev_handle = 0x0;
1161 sc->sassc->targets[i].outstanding = 0;
1162 sc->sassc->targets[i].flags = MPSSAS_TARGET_INDIAGRESET;
1167 mpssas_tm_timeout(void *data)
1169 struct mps_command *tm = data;
1170 struct mps_softc *sc = tm->cm_sc;
1172 mtx_assert(&sc->mps_mtx, MA_OWNED);
1174 mpssas_log_command(tm, MPS_INFO|MPS_RECOVERY,
1175 "task mgmt %p timed out\n", tm);
1177 KASSERT(tm->cm_state == MPS_CM_STATE_INQUEUE,
1178 ("command not inqueue\n"));
1180 tm->cm_state = MPS_CM_STATE_BUSY;
1185 mpssas_logical_unit_reset_complete(struct mps_softc *sc, struct mps_command *tm)
1187 MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1188 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1189 unsigned int cm_count = 0;
1190 struct mps_command *cm;
1191 struct mpssas_target *targ;
1193 callout_stop(&tm->cm_callout);
1195 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1196 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1200 * Currently there should be no way we can hit this case. It only
1201 * happens when we have a failure to allocate chain frames, and
1202 * task management commands don't have S/G lists.
1203 * XXXSL So should it be an assertion?
1205 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
1206 mps_dprint(sc, MPS_RECOVERY|MPS_ERROR,
1207 "%s: cm_flags = %#x for LUN reset! "
1208 "This should not happen!\n", __func__, tm->cm_flags);
1209 mpssas_free_tm(sc, tm);
1213 if (reply == NULL) {
1214 mps_dprint(sc, MPS_RECOVERY, "NULL reset reply for tm %p\n",
1216 if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) {
1217 /* this completion was due to a reset, just cleanup */
1218 mps_dprint(sc, MPS_RECOVERY, "Hardware undergoing "
1219 "reset, ignoring NULL LUN reset reply\n");
1221 mpssas_free_tm(sc, tm);
1224 /* we should have gotten a reply. */
1225 mps_dprint(sc, MPS_INFO|MPS_RECOVERY, "NULL reply on "
1226 "LUN reset attempt, resetting controller\n");
1232 mps_dprint(sc, MPS_RECOVERY,
1233 "logical unit reset status 0x%x code 0x%x count %u\n",
1234 le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1235 le32toh(reply->TerminationCount));
1238 * See if there are any outstanding commands for this LUN.
1239 * This could be made more efficient by using a per-LU data
1240 * structure of some sort.
1242 TAILQ_FOREACH(cm, &targ->commands, cm_link) {
1243 if (cm->cm_lun == tm->cm_lun)
1247 if (cm_count == 0) {
1248 mps_dprint(sc, MPS_RECOVERY|MPS_INFO,
1249 "Finished recovery after LUN reset for target %u\n",
1252 mpssas_announce_reset(sc, AC_SENT_BDR, targ->tid, tm->cm_lun);
1255 * We've finished recovery for this logical unit. check and
1256 * see if some other logical unit has a timedout command
1257 * that needs to be processed.
1259 cm = TAILQ_FIRST(&targ->timedout_commands);
1261 mps_dprint(sc, MPS_INFO|MPS_RECOVERY,
1262 "More commands to abort for target %u\n",
1264 mpssas_send_abort(sc, tm, cm);
1267 mpssas_free_tm(sc, tm);
1271 * If we still have commands for this LUN, the reset
1272 * effectively failed, regardless of the status reported.
1273 * Escalate to a target reset.
1275 mps_dprint(sc, MPS_INFO|MPS_RECOVERY,
1276 "logical unit reset complete for target %u, but still "
1277 "have %u command(s), sending target reset\n", targ->tid,
1279 mpssas_send_reset(sc, tm,
1280 MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET);
1285 mpssas_target_reset_complete(struct mps_softc *sc, struct mps_command *tm)
1287 MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1288 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1289 struct mpssas_target *targ;
1291 callout_stop(&tm->cm_callout);
1293 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1294 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1298 * Currently there should be no way we can hit this case. It only
1299 * happens when we have a failure to allocate chain frames, and
1300 * task management commands don't have S/G lists.
1302 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
1303 mps_dprint(sc, MPS_ERROR,"%s: cm_flags = %#x for target reset! "
1304 "This should not happen!\n", __func__, tm->cm_flags);
1305 mpssas_free_tm(sc, tm);
1309 if (reply == NULL) {
1310 mps_dprint(sc, MPS_RECOVERY,
1311 "NULL target reset reply for tm %pi TaskMID %u\n",
1312 tm, le16toh(req->TaskMID));
1313 if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) {
1314 /* this completion was due to a reset, just cleanup */
1315 mps_dprint(sc, MPS_RECOVERY, "Hardware undergoing "
1316 "reset, ignoring NULL target reset reply\n");
1318 mpssas_free_tm(sc, tm);
1320 /* we should have gotten a reply. */
1321 mps_dprint(sc, MPS_INFO|MPS_RECOVERY, "NULL reply on "
1322 "target reset attempt, resetting controller\n");
1328 mps_dprint(sc, MPS_RECOVERY,
1329 "target reset status 0x%x code 0x%x count %u\n",
1330 le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1331 le32toh(reply->TerminationCount));
1333 if (targ->outstanding == 0) {
1334 /* we've finished recovery for this target and all
1335 * of its logical units.
1337 mps_dprint(sc, MPS_RECOVERY|MPS_INFO,
1338 "Finished reset recovery for target %u\n", targ->tid);
1340 mpssas_announce_reset(sc, AC_SENT_BDR, tm->cm_targ->tid,
1344 mpssas_free_tm(sc, tm);
1347 * After a target reset, if this target still has
1348 * outstanding commands, the reset effectively failed,
1349 * regardless of the status reported. escalate.
1351 mps_dprint(sc, MPS_INFO|MPS_RECOVERY,
1352 "Target reset complete for target %u, but still have %u "
1353 "command(s), resetting controller\n", targ->tid,
1359 #define MPS_RESET_TIMEOUT 30
1362 mpssas_send_reset(struct mps_softc *sc, struct mps_command *tm, uint8_t type)
1364 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1365 struct mpssas_target *target;
1368 target = tm->cm_targ;
1369 if (target->handle == 0) {
1370 mps_dprint(sc, MPS_ERROR,"%s null devhandle for target_id %d\n",
1371 __func__, target->tid);
1375 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1376 req->DevHandle = htole16(target->handle);
1377 req->TaskType = type;
1379 if (type == MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET) {
1380 /* XXX Need to handle invalid LUNs */
1381 MPS_SET_LUN(req->LUN, tm->cm_lun);
1382 tm->cm_targ->logical_unit_resets++;
1383 mps_dprint(sc, MPS_RECOVERY|MPS_INFO,
1384 "Sending logical unit reset to target %u lun %d\n",
1385 target->tid, tm->cm_lun);
1386 tm->cm_complete = mpssas_logical_unit_reset_complete;
1387 mpssas_prepare_for_tm(sc, tm, target, tm->cm_lun);
1388 } else if (type == MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET) {
1390 * Target reset method =
1391 * SAS Hard Link Reset / SATA Link Reset
1393 req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
1394 tm->cm_targ->target_resets++;
1395 mps_dprint(sc, MPS_RECOVERY|MPS_INFO,
1396 "Sending target reset to target %u\n", target->tid);
1397 tm->cm_complete = mpssas_target_reset_complete;
1398 mpssas_prepare_for_tm(sc, tm, target, CAM_LUN_WILDCARD);
1400 mps_dprint(sc, MPS_ERROR, "unexpected reset type 0x%x\n", type);
1405 tm->cm_complete_data = (void *)tm;
1407 callout_reset(&tm->cm_callout, MPS_RESET_TIMEOUT * hz,
1408 mpssas_tm_timeout, tm);
1410 err = mps_map_command(sc, tm);
1412 mps_dprint(sc, MPS_ERROR|MPS_RECOVERY,
1413 "error %d sending reset type %u\n",
1421 mpssas_abort_complete(struct mps_softc *sc, struct mps_command *tm)
1423 struct mps_command *cm;
1424 MPI2_SCSI_TASK_MANAGE_REPLY *reply;
1425 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1426 struct mpssas_target *targ;
1428 callout_stop(&tm->cm_callout);
1430 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1431 reply = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
1435 * Currently there should be no way we can hit this case. It only
1436 * happens when we have a failure to allocate chain frames, and
1437 * task management commands don't have S/G lists.
1439 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
1440 mps_dprint(sc, MPS_RECOVERY,
1441 "cm_flags = %#x for abort %p TaskMID %u!\n",
1442 tm->cm_flags, tm, le16toh(req->TaskMID));
1443 mpssas_free_tm(sc, tm);
1447 if (reply == NULL) {
1448 mps_dprint(sc, MPS_RECOVERY,
1449 "NULL abort reply for tm %p TaskMID %u\n",
1450 tm, le16toh(req->TaskMID));
1451 if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) {
1452 /* this completion was due to a reset, just cleanup */
1453 mps_dprint(sc, MPS_RECOVERY, "Hardware undergoing "
1454 "reset, ignoring NULL abort reply\n");
1456 mpssas_free_tm(sc, tm);
1458 /* we should have gotten a reply. */
1459 mps_dprint(sc, MPS_INFO|MPS_RECOVERY, "NULL reply on "
1460 "abort attempt, resetting controller\n");
1466 mps_dprint(sc, MPS_RECOVERY,
1467 "abort TaskMID %u status 0x%x code 0x%x count %u\n",
1468 le16toh(req->TaskMID),
1469 le16toh(reply->IOCStatus), le32toh(reply->ResponseCode),
1470 le32toh(reply->TerminationCount));
1472 cm = TAILQ_FIRST(&tm->cm_targ->timedout_commands);
1475 * If there are no more timedout commands, we're done with
1476 * error recovery for this target.
1478 mps_dprint(sc, MPS_INFO|MPS_RECOVERY,
1479 "Finished abort recovery for target %u\n", targ->tid);
1482 mpssas_free_tm(sc, tm);
1483 } else if (le16toh(req->TaskMID) != cm->cm_desc.Default.SMID) {
1484 /* abort success, but we have more timedout commands to abort */
1485 mps_dprint(sc, MPS_INFO|MPS_RECOVERY,
1486 "Continuing abort recovery for target %u\n", targ->tid);
1488 mpssas_send_abort(sc, tm, cm);
1490 /* we didn't get a command completion, so the abort
1491 * failed as far as we're concerned. escalate.
1493 mps_dprint(sc, MPS_RECOVERY,
1494 "Abort failed for target %u, sending logical unit reset\n",
1497 mpssas_send_reset(sc, tm,
1498 MPI2_SCSITASKMGMT_TASKTYPE_LOGICAL_UNIT_RESET);
1502 #define MPS_ABORT_TIMEOUT 5
1505 mpssas_send_abort(struct mps_softc *sc, struct mps_command *tm, struct mps_command *cm)
1507 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
1508 struct mpssas_target *targ;
1512 if (targ->handle == 0) {
1513 mps_dprint(sc, MPS_ERROR|MPS_RECOVERY,
1514 "%s null devhandle for target_id %d\n",
1515 __func__, cm->cm_ccb->ccb_h.target_id);
1519 mpssas_log_command(cm, MPS_RECOVERY|MPS_INFO,
1520 "Aborting command %p\n", cm);
1522 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
1523 req->DevHandle = htole16(targ->handle);
1524 req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_ABORT_TASK;
1526 /* XXX Need to handle invalid LUNs */
1527 MPS_SET_LUN(req->LUN, cm->cm_ccb->ccb_h.target_lun);
1529 req->TaskMID = htole16(cm->cm_desc.Default.SMID);
1532 tm->cm_complete = mpssas_abort_complete;
1533 tm->cm_complete_data = (void *)tm;
1534 tm->cm_targ = cm->cm_targ;
1535 tm->cm_lun = cm->cm_lun;
1537 callout_reset(&tm->cm_callout, MPS_ABORT_TIMEOUT * hz,
1538 mpssas_tm_timeout, tm);
1542 mpssas_prepare_for_tm(sc, tm, targ, tm->cm_lun);
1544 err = mps_map_command(sc, tm);
1546 mps_dprint(sc, MPS_ERROR|MPS_RECOVERY,
1547 "error %d sending abort for cm %p SMID %u\n",
1548 err, cm, req->TaskMID);
1553 mpssas_scsiio_timeout(void *data)
1555 sbintime_t elapsed, now;
1557 struct mps_softc *sc;
1558 struct mps_command *cm;
1559 struct mpssas_target *targ;
1561 cm = (struct mps_command *)data;
1567 mtx_assert(&sc->mps_mtx, MA_OWNED);
1569 mps_dprint(sc, MPS_XINFO|MPS_RECOVERY, "Timeout checking cm %p\n", sc);
1572 * Run the interrupt handler to make sure it's not pending. This
1573 * isn't perfect because the command could have already completed
1574 * and been re-used, though this is unlikely.
1576 mps_intr_locked(sc);
1577 if (cm->cm_flags & MPS_CM_FLAGS_ON_RECOVERY) {
1578 mpssas_log_command(cm, MPS_XINFO,
1579 "SCSI command %p almost timed out\n", cm);
1583 if (cm->cm_ccb == NULL) {
1584 mps_dprint(sc, MPS_ERROR, "command timeout with NULL ccb\n");
1591 elapsed = now - ccb->ccb_h.qos.sim_data;
1592 mpssas_log_command(cm, MPS_INFO|MPS_RECOVERY,
1593 "Command timeout on target %u(0x%04x) %d set, %d.%d elapsed\n",
1594 targ->tid, targ->handle, ccb->ccb_h.timeout,
1595 sbintime_getsec(elapsed), elapsed & 0xffffffff);
1597 /* XXX first, check the firmware state, to see if it's still
1598 * operational. if not, do a diag reset.
1600 mpssas_set_ccbstatus(cm->cm_ccb, CAM_CMD_TIMEOUT);
1601 cm->cm_flags |= MPS_CM_FLAGS_ON_RECOVERY | MPS_CM_FLAGS_TIMEDOUT;
1602 TAILQ_INSERT_TAIL(&targ->timedout_commands, cm, cm_recovery);
1604 if (targ->tm != NULL) {
1605 /* target already in recovery, just queue up another
1606 * timedout command to be processed later.
1608 mps_dprint(sc, MPS_RECOVERY,
1609 "queued timedout cm %p for processing by tm %p\n",
1611 } else if ((targ->tm = mpssas_alloc_tm(sc)) != NULL) {
1612 mps_dprint(sc, MPS_RECOVERY|MPS_INFO,
1613 "Sending abort to target %u for SMID %d\n", targ->tid,
1614 cm->cm_desc.Default.SMID);
1615 mps_dprint(sc, MPS_RECOVERY, "timedout cm %p allocated tm %p\n",
1618 /* start recovery by aborting the first timedout command */
1619 mpssas_send_abort(sc, targ->tm, cm);
1621 /* XXX queue this target up for recovery once a TM becomes
1622 * available. The firmware only has a limited number of
1623 * HighPriority credits for the high priority requests used
1624 * for task management, and we ran out.
1626 * Isilon: don't worry about this for now, since we have
1627 * more credits than disks in an enclosure, and limit
1628 * ourselves to one TM per target for recovery.
1630 mps_dprint(sc, MPS_ERROR|MPS_RECOVERY,
1631 "timedout cm %p failed to allocate a tm\n", cm);
1637 mpssas_action_scsiio(struct mpssas_softc *sassc, union ccb *ccb)
1639 MPI2_SCSI_IO_REQUEST *req;
1640 struct ccb_scsiio *csio;
1641 struct mps_softc *sc;
1642 struct mpssas_target *targ;
1643 struct mpssas_lun *lun;
1644 struct mps_command *cm;
1645 uint8_t i, lba_byte, *ref_tag_addr;
1646 uint16_t eedp_flags;
1647 uint32_t mpi_control;
1651 mtx_assert(&sc->mps_mtx, MA_OWNED);
1654 KASSERT(csio->ccb_h.target_id < sassc->maxtargets,
1655 ("Target %d out of bounds in XPT_SCSI_IO\n",
1656 csio->ccb_h.target_id));
1657 targ = &sassc->targets[csio->ccb_h.target_id];
1658 mps_dprint(sc, MPS_TRACE, "ccb %p target flag %x\n", ccb, targ->flags);
1659 if (targ->handle == 0x0) {
1660 mps_dprint(sc, MPS_ERROR, "%s NULL handle for target %u\n",
1661 __func__, csio->ccb_h.target_id);
1662 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1666 if (targ->flags & MPS_TARGET_FLAGS_RAID_COMPONENT) {
1667 mps_dprint(sc, MPS_ERROR, "%s Raid component no SCSI IO "
1668 "supported %u\n", __func__, csio->ccb_h.target_id);
1669 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1674 * Sometimes, it is possible to get a command that is not "In
1675 * Progress" and was actually aborted by the upper layer. Check for
1676 * this here and complete the command without error.
1678 if (mpssas_get_ccbstatus(ccb) != CAM_REQ_INPROG) {
1679 mps_dprint(sc, MPS_TRACE, "%s Command is not in progress for "
1680 "target %u\n", __func__, csio->ccb_h.target_id);
1685 * If devinfo is 0 this will be a volume. In that case don't tell CAM
1686 * that the volume has timed out. We want volumes to be enumerated
1687 * until they are deleted/removed, not just failed. In either event,
1688 * we're removing the target due to a firmware event telling us
1689 * the device is now gone (as opposed to some transient event). Since
1690 * we're opting to remove failed devices from the OS's view, we need
1691 * to propagate that status up the stack.
1693 if (targ->flags & MPSSAS_TARGET_INREMOVAL) {
1694 if (targ->devinfo == 0)
1695 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
1697 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1702 if ((sc->mps_flags & MPS_FLAGS_SHUTDOWN) != 0) {
1703 mps_dprint(sc, MPS_INFO, "%s shutting down\n", __func__);
1704 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
1710 * If target has a reset in progress, freeze the devq and return. The
1711 * devq will be released when the TM reset is finished.
1713 if (targ->flags & MPSSAS_TARGET_INRESET) {
1714 ccb->ccb_h.status = CAM_BUSY | CAM_DEV_QFRZN;
1715 mps_dprint(sc, MPS_INFO, "%s: Freezing devq for target ID %d\n",
1716 __func__, targ->tid);
1717 xpt_freeze_devq(ccb->ccb_h.path, 1);
1722 cm = mps_alloc_command(sc);
1723 if (cm == NULL || (sc->mps_flags & MPS_FLAGS_DIAGRESET)) {
1725 mps_free_command(sc, cm);
1727 if ((sassc->flags & MPSSAS_QUEUE_FROZEN) == 0) {
1728 xpt_freeze_simq(sassc->sim, 1);
1729 sassc->flags |= MPSSAS_QUEUE_FROZEN;
1731 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1732 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1737 req = (MPI2_SCSI_IO_REQUEST *)cm->cm_req;
1738 bzero(req, sizeof(*req));
1739 req->DevHandle = htole16(targ->handle);
1740 req->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1742 req->SenseBufferLowAddress = htole32(cm->cm_sense_busaddr);
1743 req->SenseBufferLength = MPS_SENSE_LEN;
1745 req->ChainOffset = 0;
1746 req->SGLOffset0 = 24; /* 32bit word offset to the SGL */
1751 req->DataLength = htole32(csio->dxfer_len);
1752 req->BidirectionalDataLength = 0;
1753 req->IoFlags = htole16(csio->cdb_len);
1756 /* Note: BiDirectional transfers are not supported */
1757 switch (csio->ccb_h.flags & CAM_DIR_MASK) {
1759 mpi_control = MPI2_SCSIIO_CONTROL_READ;
1760 cm->cm_flags |= MPS_CM_FLAGS_DATAIN;
1763 mpi_control = MPI2_SCSIIO_CONTROL_WRITE;
1764 cm->cm_flags |= MPS_CM_FLAGS_DATAOUT;
1768 mpi_control = MPI2_SCSIIO_CONTROL_NODATATRANSFER;
1772 if (csio->cdb_len == 32)
1773 mpi_control |= 4 << MPI2_SCSIIO_CONTROL_ADDCDBLEN_SHIFT;
1775 * It looks like the hardware doesn't require an explicit tag
1776 * number for each transaction. SAM Task Management not supported
1779 switch (csio->tag_action) {
1780 case MSG_HEAD_OF_Q_TAG:
1781 mpi_control |= MPI2_SCSIIO_CONTROL_HEADOFQ;
1783 case MSG_ORDERED_Q_TAG:
1784 mpi_control |= MPI2_SCSIIO_CONTROL_ORDEREDQ;
1787 mpi_control |= MPI2_SCSIIO_CONTROL_ACAQ;
1789 case CAM_TAG_ACTION_NONE:
1790 case MSG_SIMPLE_Q_TAG:
1792 mpi_control |= MPI2_SCSIIO_CONTROL_SIMPLEQ;
1795 mpi_control |= sc->mapping_table[csio->ccb_h.target_id].TLR_bits;
1796 req->Control = htole32(mpi_control);
1797 if (MPS_SET_LUN(req->LUN, csio->ccb_h.target_lun) != 0) {
1798 mps_free_command(sc, cm);
1799 mpssas_set_ccbstatus(ccb, CAM_LUN_INVALID);
1804 if (csio->ccb_h.flags & CAM_CDB_POINTER)
1805 bcopy(csio->cdb_io.cdb_ptr, &req->CDB.CDB32[0], csio->cdb_len);
1807 bcopy(csio->cdb_io.cdb_bytes, &req->CDB.CDB32[0],csio->cdb_len);
1808 req->IoFlags = htole16(csio->cdb_len);
1811 * Check if EEDP is supported and enabled. If it is then check if the
1812 * SCSI opcode could be using EEDP. If so, make sure the LUN exists and
1813 * is formatted for EEDP support. If all of this is true, set CDB up
1814 * for EEDP transfer.
1816 eedp_flags = op_code_prot[req->CDB.CDB32[0]];
1817 if (sc->eedp_enabled && eedp_flags) {
1818 SLIST_FOREACH(lun, &targ->luns, lun_link) {
1819 if (lun->lun_id == csio->ccb_h.target_lun) {
1824 if ((lun != NULL) && (lun->eedp_formatted)) {
1825 req->EEDPBlockSize = htole16(lun->eedp_block_size);
1826 eedp_flags |= (MPI2_SCSIIO_EEDPFLAGS_INC_PRI_REFTAG |
1827 MPI2_SCSIIO_EEDPFLAGS_CHECK_REFTAG |
1828 MPI2_SCSIIO_EEDPFLAGS_CHECK_GUARD);
1829 req->EEDPFlags = htole16(eedp_flags);
1832 * If CDB less than 32, fill in Primary Ref Tag with
1833 * low 4 bytes of LBA. If CDB is 32, tag stuff is
1834 * already there. Also, set protection bit. FreeBSD
1835 * currently does not support CDBs bigger than 16, but
1836 * the code doesn't hurt, and will be here for the
1839 if (csio->cdb_len != 32) {
1840 lba_byte = (csio->cdb_len == 16) ? 6 : 2;
1841 ref_tag_addr = (uint8_t *)&req->CDB.EEDP32.
1842 PrimaryReferenceTag;
1843 for (i = 0; i < 4; i++) {
1845 req->CDB.CDB32[lba_byte + i];
1848 req->CDB.EEDP32.PrimaryReferenceTag =
1849 htole32(req->CDB.EEDP32.PrimaryReferenceTag);
1850 req->CDB.EEDP32.PrimaryApplicationTagMask =
1852 req->CDB.CDB32[1] = (req->CDB.CDB32[1] & 0x1F) |
1856 MPI2_SCSIIO_EEDPFLAGS_INC_PRI_APPTAG;
1857 req->EEDPFlags = htole16(eedp_flags);
1858 req->CDB.CDB32[10] = (req->CDB.CDB32[10] &
1864 cm->cm_length = csio->dxfer_len;
1865 if (cm->cm_length != 0) {
1867 cm->cm_flags |= MPS_CM_FLAGS_USE_CCB;
1871 cm->cm_sge = &req->SGL;
1872 cm->cm_sglsize = (32 - 24) * 4;
1873 cm->cm_desc.SCSIIO.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO;
1874 cm->cm_desc.SCSIIO.DevHandle = htole16(targ->handle);
1875 cm->cm_complete = mpssas_scsiio_complete;
1876 cm->cm_complete_data = ccb;
1878 cm->cm_lun = csio->ccb_h.target_lun;
1882 * If HBA is a WD and the command is not for a retry, try to build a
1883 * direct I/O message. If failed, or the command is for a retry, send
1884 * the I/O to the IR volume itself.
1886 if (sc->WD_valid_config) {
1887 if (ccb->ccb_h.sim_priv.entries[0].field == MPS_WD_RETRY) {
1888 mpssas_direct_drive_io(sassc, cm, ccb);
1890 mpssas_set_ccbstatus(ccb, CAM_REQ_INPROG);
1894 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
1895 if (csio->bio != NULL)
1896 biotrack(csio->bio, __func__);
1898 csio->ccb_h.qos.sim_data = sbinuptime();
1899 callout_reset_sbt(&cm->cm_callout, SBT_1MS * ccb->ccb_h.timeout, 0,
1900 mpssas_scsiio_timeout, cm, 0);
1903 targ->outstanding++;
1904 TAILQ_INSERT_TAIL(&targ->commands, cm, cm_link);
1905 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1907 mpssas_log_command(cm, MPS_XINFO, "%s cm %p ccb %p outstanding %u\n",
1908 __func__, cm, ccb, targ->outstanding);
1910 mps_map_command(sc, cm);
1915 * mps_sc_failed_io_info - translated non-succesfull SCSI_IO request
1918 mps_sc_failed_io_info(struct mps_softc *sc, struct ccb_scsiio *csio,
1919 Mpi2SCSIIOReply_t *mpi_reply)
1923 u16 ioc_status = le16toh(mpi_reply->IOCStatus) &
1924 MPI2_IOCSTATUS_MASK;
1925 u8 scsi_state = mpi_reply->SCSIState;
1926 u8 scsi_status = mpi_reply->SCSIStatus;
1927 u32 log_info = le32toh(mpi_reply->IOCLogInfo);
1928 const char *desc_ioc_state, *desc_scsi_status;
1930 if (log_info == 0x31170000)
1933 desc_ioc_state = mps_describe_table(mps_iocstatus_string,
1935 desc_scsi_status = mps_describe_table(mps_scsi_status_string,
1938 mps_dprint(sc, MPS_XINFO, "\thandle(0x%04x), ioc_status(%s)(0x%04x)\n",
1939 le16toh(mpi_reply->DevHandle), desc_ioc_state, ioc_status);
1942 *We can add more detail about underflow data here
1945 mps_dprint(sc, MPS_XINFO, "\tscsi_status(%s)(0x%02x), "
1946 "scsi_state %b\n", desc_scsi_status, scsi_status,
1947 scsi_state, "\20" "\1AutosenseValid" "\2AutosenseFailed"
1948 "\3NoScsiStatus" "\4Terminated" "\5Response InfoValid");
1950 if (sc->mps_debug & MPS_XINFO &&
1951 scsi_state & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
1952 mps_dprint(sc, MPS_XINFO, "-> Sense Buffer Data : Start :\n");
1953 scsi_sense_print(csio);
1954 mps_dprint(sc, MPS_XINFO, "-> Sense Buffer Data : End :\n");
1957 if (scsi_state & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) {
1958 response_info = le32toh(mpi_reply->ResponseInfo);
1959 response_bytes = (u8 *)&response_info;
1960 mps_dprint(sc, MPS_XINFO, "response code(0x%1x): %s\n",
1962 mps_describe_table(mps_scsi_taskmgmt_string,
1963 response_bytes[0]));
1968 mpssas_scsiio_complete(struct mps_softc *sc, struct mps_command *cm)
1970 MPI2_SCSI_IO_REPLY *rep;
1972 struct ccb_scsiio *csio;
1973 struct mpssas_softc *sassc;
1974 struct scsi_vpd_supported_page_list *vpd_list = NULL;
1975 u8 *TLR_bits, TLR_on;
1978 struct mpssas_target *target;
1979 target_id_t target_id;
1982 mps_dprint(sc, MPS_TRACE,
1983 "cm %p SMID %u ccb %p reply %p outstanding %u\n", cm,
1984 cm->cm_desc.Default.SMID, cm->cm_ccb, cm->cm_reply,
1985 cm->cm_targ->outstanding);
1987 callout_stop(&cm->cm_callout);
1988 mtx_assert(&sc->mps_mtx, MA_OWNED);
1991 ccb = cm->cm_complete_data;
1993 target_id = csio->ccb_h.target_id;
1994 rep = (MPI2_SCSI_IO_REPLY *)cm->cm_reply;
1996 * XXX KDM if the chain allocation fails, does it matter if we do
1997 * the sync and unload here? It is simpler to do it in every case,
1998 * assuming it doesn't cause problems.
2000 if (cm->cm_data != NULL) {
2001 if (cm->cm_flags & MPS_CM_FLAGS_DATAIN)
2002 dir = BUS_DMASYNC_POSTREAD;
2003 else if (cm->cm_flags & MPS_CM_FLAGS_DATAOUT)
2004 dir = BUS_DMASYNC_POSTWRITE;
2005 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap, dir);
2006 bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
2009 cm->cm_targ->completed++;
2010 cm->cm_targ->outstanding--;
2011 TAILQ_REMOVE(&cm->cm_targ->commands, cm, cm_link);
2012 ccb->ccb_h.status &= ~(CAM_STATUS_MASK | CAM_SIM_QUEUED);
2014 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
2015 if (ccb->csio.bio != NULL)
2016 biotrack(ccb->csio.bio, __func__);
2019 if (cm->cm_flags & MPS_CM_FLAGS_ON_RECOVERY) {
2020 TAILQ_REMOVE(&cm->cm_targ->timedout_commands, cm, cm_recovery);
2021 KASSERT(cm->cm_state == MPS_CM_STATE_BUSY,
2022 ("Not busy for CM_FLAGS_TIMEDOUT: %d\n", cm->cm_state));
2023 cm->cm_flags &= ~MPS_CM_FLAGS_ON_RECOVERY;
2024 if (cm->cm_reply != NULL)
2025 mpssas_log_command(cm, MPS_RECOVERY,
2026 "completed timedout cm %p ccb %p during recovery "
2027 "ioc %x scsi %x state %x xfer %u\n",
2028 cm, cm->cm_ccb, le16toh(rep->IOCStatus),
2029 rep->SCSIStatus, rep->SCSIState,
2030 le32toh(rep->TransferCount));
2032 mpssas_log_command(cm, MPS_RECOVERY,
2033 "completed timedout cm %p ccb %p during recovery\n",
2035 } else if (cm->cm_targ->tm != NULL) {
2036 if (cm->cm_reply != NULL)
2037 mpssas_log_command(cm, MPS_RECOVERY,
2038 "completed cm %p ccb %p during recovery "
2039 "ioc %x scsi %x state %x xfer %u\n",
2040 cm, cm->cm_ccb, le16toh(rep->IOCStatus),
2041 rep->SCSIStatus, rep->SCSIState,
2042 le32toh(rep->TransferCount));
2044 mpssas_log_command(cm, MPS_RECOVERY,
2045 "completed cm %p ccb %p during recovery\n",
2047 } else if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0) {
2048 mpssas_log_command(cm, MPS_RECOVERY,
2049 "reset completed cm %p ccb %p\n",
2053 if ((cm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
2055 * We ran into an error after we tried to map the command,
2056 * so we're getting a callback without queueing the command
2057 * to the hardware. So we set the status here, and it will
2058 * be retained below. We'll go through the "fast path",
2059 * because there can be no reply when we haven't actually
2060 * gone out to the hardware.
2062 mpssas_set_ccbstatus(ccb, CAM_REQUEUE_REQ);
2065 * Currently the only error included in the mask is
2066 * MPS_CM_FLAGS_CHAIN_FAILED, which means we're out of
2067 * chain frames. We need to freeze the queue until we get
2068 * a command that completed without this error, which will
2069 * hopefully have some chain frames attached that we can
2070 * use. If we wanted to get smarter about it, we would
2071 * only unfreeze the queue in this condition when we're
2072 * sure that we're getting some chain frames back. That's
2073 * probably unnecessary.
2075 if ((sassc->flags & MPSSAS_QUEUE_FROZEN) == 0) {
2076 xpt_freeze_simq(sassc->sim, 1);
2077 sassc->flags |= MPSSAS_QUEUE_FROZEN;
2078 mps_dprint(sc, MPS_XINFO, "Error sending command, "
2079 "freezing SIM queue\n");
2084 * If this is a Start Stop Unit command and it was issued by the driver
2085 * during shutdown, decrement the refcount to account for all of the
2086 * commands that were sent. All SSU commands should be completed before
2087 * shutdown completes, meaning SSU_refcount will be 0 after SSU_started
2090 if (sc->SSU_started && (csio->cdb_io.cdb_bytes[0] == START_STOP_UNIT)) {
2091 mps_dprint(sc, MPS_INFO, "Decrementing SSU count.\n");
2095 /* Take the fast path to completion */
2096 if (cm->cm_reply == NULL) {
2097 if (mpssas_get_ccbstatus(ccb) == CAM_REQ_INPROG) {
2098 if ((sc->mps_flags & MPS_FLAGS_DIAGRESET) != 0)
2099 mpssas_set_ccbstatus(ccb, CAM_SCSI_BUS_RESET);
2101 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
2102 ccb->csio.scsi_status = SCSI_STATUS_OK;
2104 if (sassc->flags & MPSSAS_QUEUE_FROZEN) {
2105 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2106 sassc->flags &= ~MPSSAS_QUEUE_FROZEN;
2107 mps_dprint(sc, MPS_XINFO,
2108 "Unfreezing SIM queue\n");
2113 * There are two scenarios where the status won't be
2114 * CAM_REQ_CMP. The first is if MPS_CM_FLAGS_ERROR_MASK is
2115 * set, the second is in the MPS_FLAGS_DIAGRESET above.
2117 if (mpssas_get_ccbstatus(ccb) != CAM_REQ_CMP) {
2119 * Freeze the dev queue so that commands are
2120 * executed in the correct order after error
2123 ccb->ccb_h.status |= CAM_DEV_QFRZN;
2124 xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1);
2126 mps_free_command(sc, cm);
2131 mpssas_log_command(cm, MPS_XINFO,
2132 "ioc %x scsi %x state %x xfer %u\n",
2133 le16toh(rep->IOCStatus), rep->SCSIStatus, rep->SCSIState,
2134 le32toh(rep->TransferCount));
2137 * If this is a Direct Drive I/O, reissue the I/O to the original IR
2138 * Volume if an error occurred (normal I/O retry). Use the original
2139 * CCB, but set a flag that this will be a retry so that it's sent to
2140 * the original volume. Free the command but reuse the CCB.
2142 if (cm->cm_flags & MPS_CM_FLAGS_DD_IO) {
2143 mps_free_command(sc, cm);
2144 ccb->ccb_h.sim_priv.entries[0].field = MPS_WD_RETRY;
2145 mpssas_action_scsiio(sassc, ccb);
2148 ccb->ccb_h.sim_priv.entries[0].field = 0;
2150 switch (le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK) {
2151 case MPI2_IOCSTATUS_SCSI_DATA_UNDERRUN:
2152 csio->resid = cm->cm_length - le32toh(rep->TransferCount);
2154 case MPI2_IOCSTATUS_SUCCESS:
2155 case MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR:
2157 if ((le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK) ==
2158 MPI2_IOCSTATUS_SCSI_RECOVERED_ERROR)
2159 mpssas_log_command(cm, MPS_XINFO, "recovered error\n");
2161 /* Completion failed at the transport level. */
2162 if (rep->SCSIState & (MPI2_SCSI_STATE_NO_SCSI_STATUS |
2163 MPI2_SCSI_STATE_TERMINATED)) {
2164 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2168 /* In a modern packetized environment, an autosense failure
2169 * implies that there's not much else that can be done to
2170 * recover the command.
2172 if (rep->SCSIState & MPI2_SCSI_STATE_AUTOSENSE_FAILED) {
2173 mpssas_set_ccbstatus(ccb, CAM_AUTOSENSE_FAIL);
2178 * CAM doesn't care about SAS Response Info data, but if this is
2179 * the state check if TLR should be done. If not, clear the
2180 * TLR_bits for the target.
2182 if ((rep->SCSIState & MPI2_SCSI_STATE_RESPONSE_INFO_VALID) &&
2183 ((le32toh(rep->ResponseInfo) &
2184 MPI2_SCSI_RI_MASK_REASONCODE) ==
2185 MPS_SCSI_RI_INVALID_FRAME)) {
2186 sc->mapping_table[target_id].TLR_bits =
2187 (u8)MPI2_SCSIIO_CONTROL_NO_TLR;
2191 * Intentionally override the normal SCSI status reporting
2192 * for these two cases. These are likely to happen in a
2193 * multi-initiator environment, and we want to make sure that
2194 * CAM retries these commands rather than fail them.
2196 if ((rep->SCSIStatus == MPI2_SCSI_STATUS_COMMAND_TERMINATED) ||
2197 (rep->SCSIStatus == MPI2_SCSI_STATUS_TASK_ABORTED)) {
2198 mpssas_set_ccbstatus(ccb, CAM_REQ_ABORTED);
2202 /* Handle normal status and sense */
2203 csio->scsi_status = rep->SCSIStatus;
2204 if (rep->SCSIStatus == MPI2_SCSI_STATUS_GOOD)
2205 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
2207 mpssas_set_ccbstatus(ccb, CAM_SCSI_STATUS_ERROR);
2209 if (rep->SCSIState & MPI2_SCSI_STATE_AUTOSENSE_VALID) {
2210 int sense_len, returned_sense_len;
2212 returned_sense_len = min(le32toh(rep->SenseCount),
2213 sizeof(struct scsi_sense_data));
2214 if (returned_sense_len < ccb->csio.sense_len)
2215 ccb->csio.sense_resid = ccb->csio.sense_len -
2218 ccb->csio.sense_resid = 0;
2220 sense_len = min(returned_sense_len,
2221 ccb->csio.sense_len - ccb->csio.sense_resid);
2222 bzero(&ccb->csio.sense_data,
2223 sizeof(ccb->csio.sense_data));
2224 bcopy(cm->cm_sense, &ccb->csio.sense_data, sense_len);
2225 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
2229 * Check if this is an INQUIRY command. If it's a VPD inquiry,
2230 * and it's page code 0 (Supported Page List), and there is
2231 * inquiry data, and this is for a sequential access device, and
2232 * the device is an SSP target, and TLR is supported by the
2233 * controller, turn the TLR_bits value ON if page 0x90 is
2236 if ((csio->cdb_io.cdb_bytes[0] == INQUIRY) &&
2237 (csio->cdb_io.cdb_bytes[1] & SI_EVPD) &&
2238 (csio->cdb_io.cdb_bytes[2] == SVPD_SUPPORTED_PAGE_LIST) &&
2239 ((csio->ccb_h.flags & CAM_DATA_MASK) == CAM_DATA_VADDR) &&
2240 (csio->data_ptr != NULL) &&
2241 ((csio->data_ptr[0] & 0x1f) == T_SEQUENTIAL) &&
2242 (sc->control_TLR) &&
2243 (sc->mapping_table[target_id].device_info &
2244 MPI2_SAS_DEVICE_INFO_SSP_TARGET)) {
2245 vpd_list = (struct scsi_vpd_supported_page_list *)
2247 TLR_bits = &sc->mapping_table[target_id].TLR_bits;
2248 *TLR_bits = (u8)MPI2_SCSIIO_CONTROL_NO_TLR;
2249 TLR_on = (u8)MPI2_SCSIIO_CONTROL_TLR_ON;
2250 alloc_len = ((u16)csio->cdb_io.cdb_bytes[3] << 8) +
2251 csio->cdb_io.cdb_bytes[4];
2252 alloc_len -= csio->resid;
2253 for (i = 0; i < MIN(vpd_list->length, alloc_len); i++) {
2254 if (vpd_list->list[i] == 0x90) {
2262 * If this is a SATA direct-access end device, mark it so that
2263 * a SCSI StartStopUnit command will be sent to it when the
2264 * driver is being shutdown.
2266 if ((csio->cdb_io.cdb_bytes[0] == INQUIRY) &&
2267 ((csio->data_ptr[0] & 0x1f) == T_DIRECT) &&
2268 (sc->mapping_table[target_id].device_info &
2269 MPI2_SAS_DEVICE_INFO_SATA_DEVICE) &&
2270 ((sc->mapping_table[target_id].device_info &
2271 MPI2_SAS_DEVICE_INFO_MASK_DEVICE_TYPE) ==
2272 MPI2_SAS_DEVICE_INFO_END_DEVICE)) {
2273 target = &sassc->targets[target_id];
2274 target->supports_SSU = TRUE;
2275 mps_dprint(sc, MPS_XINFO, "Target %d supports SSU\n",
2279 case MPI2_IOCSTATUS_SCSI_INVALID_DEVHANDLE:
2280 case MPI2_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
2282 * If devinfo is 0 this will be a volume. In that case don't
2283 * tell CAM that the volume is not there. We want volumes to
2284 * be enumerated until they are deleted/removed, not just
2287 if (cm->cm_targ->devinfo == 0)
2288 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
2290 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
2292 case MPI2_IOCSTATUS_INVALID_SGL:
2293 mps_print_scsiio_cmd(sc, cm);
2294 mpssas_set_ccbstatus(ccb, CAM_UNREC_HBA_ERROR);
2296 case MPI2_IOCSTATUS_SCSI_TASK_TERMINATED:
2298 * This is one of the responses that comes back when an I/O
2299 * has been aborted. If it is because of a timeout that we
2300 * initiated, just set the status to CAM_CMD_TIMEOUT.
2301 * Otherwise set it to CAM_REQ_ABORTED. The effect on the
2302 * command is the same (it gets retried, subject to the
2303 * retry counter), the only difference is what gets printed
2306 if (cm->cm_flags & MPS_CM_FLAGS_TIMEDOUT)
2307 mpssas_set_ccbstatus(ccb, CAM_CMD_TIMEOUT);
2309 mpssas_set_ccbstatus(ccb, CAM_REQ_ABORTED);
2311 case MPI2_IOCSTATUS_SCSI_DATA_OVERRUN:
2312 /* resid is ignored for this condition */
2314 mpssas_set_ccbstatus(ccb, CAM_DATA_RUN_ERR);
2316 case MPI2_IOCSTATUS_SCSI_IOC_TERMINATED:
2317 case MPI2_IOCSTATUS_SCSI_EXT_TERMINATED:
2319 * These can sometimes be transient transport-related
2320 * errors, and sometimes persistent drive-related errors.
2321 * We used to retry these without decrementing the retry
2322 * count by returning CAM_REQUEUE_REQ. Unfortunately, if
2323 * we hit a persistent drive problem that returns one of
2324 * these error codes, we would retry indefinitely. So,
2325 * return CAM_REQ_CMP_ERROR so that we decrement the retry
2326 * count and avoid infinite retries. We're taking the
2327 * potential risk of flagging false failures in the event
2328 * of a topology-related error (e.g. a SAS expander problem
2329 * causes a command addressed to a drive to fail), but
2330 * avoiding getting into an infinite retry loop. However,
2331 * if we get them while were moving a device, we should
2332 * fail the request as 'not there' because the device
2333 * is effectively gone.
2335 if (cm->cm_targ->flags & MPSSAS_TARGET_INREMOVAL)
2336 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
2338 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2339 mps_dprint(sc, MPS_INFO,
2340 "Controller reported %s tgt %u SMID %u loginfo %x%s\n",
2341 mps_describe_table(mps_iocstatus_string,
2342 le16toh(rep->IOCStatus) & MPI2_IOCSTATUS_MASK),
2343 target_id, cm->cm_desc.Default.SMID,
2344 le32toh(rep->IOCLogInfo),
2345 (cm->cm_targ->flags & MPSSAS_TARGET_INREMOVAL) ? " departing" : "");
2346 mps_dprint(sc, MPS_XINFO,
2347 "SCSIStatus %x SCSIState %x xfercount %u\n",
2348 rep->SCSIStatus, rep->SCSIState,
2349 le32toh(rep->TransferCount));
2351 case MPI2_IOCSTATUS_INVALID_FUNCTION:
2352 case MPI2_IOCSTATUS_INTERNAL_ERROR:
2353 case MPI2_IOCSTATUS_INVALID_VPID:
2354 case MPI2_IOCSTATUS_INVALID_FIELD:
2355 case MPI2_IOCSTATUS_INVALID_STATE:
2356 case MPI2_IOCSTATUS_OP_STATE_NOT_SUPPORTED:
2357 case MPI2_IOCSTATUS_SCSI_IO_DATA_ERROR:
2358 case MPI2_IOCSTATUS_SCSI_PROTOCOL_ERROR:
2359 case MPI2_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
2360 case MPI2_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
2362 mpssas_log_command(cm, MPS_XINFO,
2363 "completed ioc %x loginfo %x scsi %x state %x xfer %u\n",
2364 le16toh(rep->IOCStatus), le32toh(rep->IOCLogInfo),
2365 rep->SCSIStatus, rep->SCSIState,
2366 le32toh(rep->TransferCount));
2367 csio->resid = cm->cm_length;
2368 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2372 mps_sc_failed_io_info(sc,csio,rep);
2374 if (sassc->flags & MPSSAS_QUEUE_FROZEN) {
2375 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2376 sassc->flags &= ~MPSSAS_QUEUE_FROZEN;
2377 mps_dprint(sc, MPS_XINFO, "Command completed, "
2378 "unfreezing SIM queue\n");
2381 if (mpssas_get_ccbstatus(ccb) != CAM_REQ_CMP) {
2382 ccb->ccb_h.status |= CAM_DEV_QFRZN;
2383 xpt_freeze_devq(ccb->ccb_h.path, /*count*/ 1);
2387 * Check to see if we're removing the device. If so, and this is the
2388 * last command on the queue, proceed with the deferred removal of the
2389 * device. Note, for removing a volume, this won't trigger because
2390 * pending_remove_tm will be NULL.
2392 if (cm->cm_targ->flags & MPSSAS_TARGET_INREMOVAL) {
2393 if (TAILQ_FIRST(&cm->cm_targ->commands) == NULL &&
2394 cm->cm_targ->pending_remove_tm != NULL) {
2395 mps_dprint(sc, MPS_INFO, "Last pending command complete: starting remove_device\n");
2396 mps_map_command(sc, cm->cm_targ->pending_remove_tm);
2397 cm->cm_targ->pending_remove_tm = NULL;
2401 mps_free_command(sc, cm);
2405 /* All Request reached here are Endian safe */
2407 mpssas_direct_drive_io(struct mpssas_softc *sassc, struct mps_command *cm,
2409 pMpi2SCSIIORequest_t pIO_req;
2410 struct mps_softc *sc = sassc->sc;
2412 uint32_t physLBA, stripe_offset, stripe_unit;
2413 uint32_t io_size, column;
2414 uint8_t *ptrLBA, lba_idx, physLBA_byte, *CDB;
2417 * If this is a valid SCSI command (Read6, Read10, Read16, Write6,
2418 * Write10, or Write16), build a direct I/O message. Otherwise, the I/O
2419 * will be sent to the IR volume itself. Since Read6 and Write6 are a
2420 * bit different than the 10/16 CDBs, handle them separately.
2422 pIO_req = (pMpi2SCSIIORequest_t)cm->cm_req;
2423 CDB = pIO_req->CDB.CDB32;
2426 * Handle 6 byte CDBs.
2428 if ((pIO_req->DevHandle == sc->DD_dev_handle) && ((CDB[0] == READ_6) ||
2429 (CDB[0] == WRITE_6))) {
2431 * Get the transfer size in blocks.
2433 io_size = (cm->cm_length >> sc->DD_block_exponent);
2436 * Get virtual LBA given in the CDB.
2438 virtLBA = ((uint64_t)(CDB[1] & 0x1F) << 16) |
2439 ((uint64_t)CDB[2] << 8) | (uint64_t)CDB[3];
2442 * Check that LBA range for I/O does not exceed volume's
2445 if ((virtLBA + (uint64_t)io_size - 1) <=
2448 * Check if the I/O crosses a stripe boundary. If not,
2449 * translate the virtual LBA to a physical LBA and set
2450 * the DevHandle for the PhysDisk to be used. If it
2451 * does cross a boundary, do normal I/O. To get the
2452 * right DevHandle to use, get the map number for the
2453 * column, then use that map number to look up the
2454 * DevHandle of the PhysDisk.
2456 stripe_offset = (uint32_t)virtLBA &
2457 (sc->DD_stripe_size - 1);
2458 if ((stripe_offset + io_size) <= sc->DD_stripe_size) {
2459 physLBA = (uint32_t)virtLBA >>
2460 sc->DD_stripe_exponent;
2461 stripe_unit = physLBA / sc->DD_num_phys_disks;
2462 column = physLBA % sc->DD_num_phys_disks;
2463 pIO_req->DevHandle =
2464 htole16(sc->DD_column_map[column].dev_handle);
2465 /* ???? Is this endian safe*/
2466 cm->cm_desc.SCSIIO.DevHandle =
2469 physLBA = (stripe_unit <<
2470 sc->DD_stripe_exponent) + stripe_offset;
2471 ptrLBA = &pIO_req->CDB.CDB32[1];
2472 physLBA_byte = (uint8_t)(physLBA >> 16);
2473 *ptrLBA = physLBA_byte;
2474 ptrLBA = &pIO_req->CDB.CDB32[2];
2475 physLBA_byte = (uint8_t)(physLBA >> 8);
2476 *ptrLBA = physLBA_byte;
2477 ptrLBA = &pIO_req->CDB.CDB32[3];
2478 physLBA_byte = (uint8_t)physLBA;
2479 *ptrLBA = physLBA_byte;
2482 * Set flag that Direct Drive I/O is
2485 cm->cm_flags |= MPS_CM_FLAGS_DD_IO;
2492 * Handle 10, 12 or 16 byte CDBs.
2494 if ((pIO_req->DevHandle == sc->DD_dev_handle) && ((CDB[0] == READ_10) ||
2495 (CDB[0] == WRITE_10) || (CDB[0] == READ_16) ||
2496 (CDB[0] == WRITE_16) || (CDB[0] == READ_12) ||
2497 (CDB[0] == WRITE_12))) {
2499 * For 16-byte CDB's, verify that the upper 4 bytes of the CDB
2500 * are 0. If not, this is accessing beyond 2TB so handle it in
2501 * the else section. 10-byte and 12-byte CDB's are OK.
2502 * FreeBSD sends very rare 12 byte READ/WRITE, but driver is
2503 * ready to accept 12byte CDB for Direct IOs.
2505 if ((CDB[0] == READ_10 || CDB[0] == WRITE_10) ||
2506 (CDB[0] == READ_12 || CDB[0] == WRITE_12) ||
2507 !(CDB[2] | CDB[3] | CDB[4] | CDB[5])) {
2509 * Get the transfer size in blocks.
2511 io_size = (cm->cm_length >> sc->DD_block_exponent);
2514 * Get virtual LBA. Point to correct lower 4 bytes of
2515 * LBA in the CDB depending on command.
2517 lba_idx = ((CDB[0] == READ_12) ||
2518 (CDB[0] == WRITE_12) ||
2519 (CDB[0] == READ_10) ||
2520 (CDB[0] == WRITE_10))? 2 : 6;
2521 virtLBA = ((uint64_t)CDB[lba_idx] << 24) |
2522 ((uint64_t)CDB[lba_idx + 1] << 16) |
2523 ((uint64_t)CDB[lba_idx + 2] << 8) |
2524 (uint64_t)CDB[lba_idx + 3];
2527 * Check that LBA range for I/O does not exceed volume's
2530 if ((virtLBA + (uint64_t)io_size - 1) <=
2533 * Check if the I/O crosses a stripe boundary.
2534 * If not, translate the virtual LBA to a
2535 * physical LBA and set the DevHandle for the
2536 * PhysDisk to be used. If it does cross a
2537 * boundary, do normal I/O. To get the right
2538 * DevHandle to use, get the map number for the
2539 * column, then use that map number to look up
2540 * the DevHandle of the PhysDisk.
2542 stripe_offset = (uint32_t)virtLBA &
2543 (sc->DD_stripe_size - 1);
2544 if ((stripe_offset + io_size) <=
2545 sc->DD_stripe_size) {
2546 physLBA = (uint32_t)virtLBA >>
2547 sc->DD_stripe_exponent;
2548 stripe_unit = physLBA /
2549 sc->DD_num_phys_disks;
2551 sc->DD_num_phys_disks;
2552 pIO_req->DevHandle =
2553 htole16(sc->DD_column_map[column].
2555 cm->cm_desc.SCSIIO.DevHandle =
2558 physLBA = (stripe_unit <<
2559 sc->DD_stripe_exponent) +
2562 &pIO_req->CDB.CDB32[lba_idx];
2563 physLBA_byte = (uint8_t)(physLBA >> 24);
2564 *ptrLBA = physLBA_byte;
2566 &pIO_req->CDB.CDB32[lba_idx + 1];
2567 physLBA_byte = (uint8_t)(physLBA >> 16);
2568 *ptrLBA = physLBA_byte;
2570 &pIO_req->CDB.CDB32[lba_idx + 2];
2571 physLBA_byte = (uint8_t)(physLBA >> 8);
2572 *ptrLBA = physLBA_byte;
2574 &pIO_req->CDB.CDB32[lba_idx + 3];
2575 physLBA_byte = (uint8_t)physLBA;
2576 *ptrLBA = physLBA_byte;
2579 * Set flag that Direct Drive I/O is
2582 cm->cm_flags |= MPS_CM_FLAGS_DD_IO;
2587 * 16-byte CDB and the upper 4 bytes of the CDB are not
2588 * 0. Get the transfer size in blocks.
2590 io_size = (cm->cm_length >> sc->DD_block_exponent);
2595 virtLBA = ((uint64_t)CDB[2] << 54) |
2596 ((uint64_t)CDB[3] << 48) |
2597 ((uint64_t)CDB[4] << 40) |
2598 ((uint64_t)CDB[5] << 32) |
2599 ((uint64_t)CDB[6] << 24) |
2600 ((uint64_t)CDB[7] << 16) |
2601 ((uint64_t)CDB[8] << 8) |
2605 * Check that LBA range for I/O does not exceed volume's
2608 if ((virtLBA + (uint64_t)io_size - 1) <=
2611 * Check if the I/O crosses a stripe boundary.
2612 * If not, translate the virtual LBA to a
2613 * physical LBA and set the DevHandle for the
2614 * PhysDisk to be used. If it does cross a
2615 * boundary, do normal I/O. To get the right
2616 * DevHandle to use, get the map number for the
2617 * column, then use that map number to look up
2618 * the DevHandle of the PhysDisk.
2620 stripe_offset = (uint32_t)virtLBA &
2621 (sc->DD_stripe_size - 1);
2622 if ((stripe_offset + io_size) <=
2623 sc->DD_stripe_size) {
2624 physLBA = (uint32_t)(virtLBA >>
2625 sc->DD_stripe_exponent);
2626 stripe_unit = physLBA /
2627 sc->DD_num_phys_disks;
2629 sc->DD_num_phys_disks;
2630 pIO_req->DevHandle =
2631 htole16(sc->DD_column_map[column].
2633 cm->cm_desc.SCSIIO.DevHandle =
2636 physLBA = (stripe_unit <<
2637 sc->DD_stripe_exponent) +
2641 * Set upper 4 bytes of LBA to 0. We
2642 * assume that the phys disks are less
2643 * than 2 TB's in size. Then, set the
2646 pIO_req->CDB.CDB32[2] = 0;
2647 pIO_req->CDB.CDB32[3] = 0;
2648 pIO_req->CDB.CDB32[4] = 0;
2649 pIO_req->CDB.CDB32[5] = 0;
2650 ptrLBA = &pIO_req->CDB.CDB32[6];
2651 physLBA_byte = (uint8_t)(physLBA >> 24);
2652 *ptrLBA = physLBA_byte;
2653 ptrLBA = &pIO_req->CDB.CDB32[7];
2654 physLBA_byte = (uint8_t)(physLBA >> 16);
2655 *ptrLBA = physLBA_byte;
2656 ptrLBA = &pIO_req->CDB.CDB32[8];
2657 physLBA_byte = (uint8_t)(physLBA >> 8);
2658 *ptrLBA = physLBA_byte;
2659 ptrLBA = &pIO_req->CDB.CDB32[9];
2660 physLBA_byte = (uint8_t)physLBA;
2661 *ptrLBA = physLBA_byte;
2664 * Set flag that Direct Drive I/O is
2667 cm->cm_flags |= MPS_CM_FLAGS_DD_IO;
2675 mpssas_smpio_complete(struct mps_softc *sc, struct mps_command *cm)
2677 MPI2_SMP_PASSTHROUGH_REPLY *rpl;
2678 MPI2_SMP_PASSTHROUGH_REQUEST *req;
2682 ccb = cm->cm_complete_data;
2685 * Currently there should be no way we can hit this case. It only
2686 * happens when we have a failure to allocate chain frames, and SMP
2687 * commands require two S/G elements only. That should be handled
2688 * in the standard request size.
2690 if ((cm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
2691 mps_dprint(sc, MPS_ERROR,"%s: cm_flags = %#x on SMP request!\n",
2692 __func__, cm->cm_flags);
2693 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2697 rpl = (MPI2_SMP_PASSTHROUGH_REPLY *)cm->cm_reply;
2699 mps_dprint(sc, MPS_ERROR, "%s: NULL cm_reply!\n", __func__);
2700 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2704 req = (MPI2_SMP_PASSTHROUGH_REQUEST *)cm->cm_req;
2705 sasaddr = le32toh(req->SASAddress.Low);
2706 sasaddr |= ((uint64_t)(le32toh(req->SASAddress.High))) << 32;
2708 if ((le16toh(rpl->IOCStatus) & MPI2_IOCSTATUS_MASK) !=
2709 MPI2_IOCSTATUS_SUCCESS ||
2710 rpl->SASStatus != MPI2_SASSTATUS_SUCCESS) {
2711 mps_dprint(sc, MPS_XINFO, "%s: IOCStatus %04x SASStatus %02x\n",
2712 __func__, le16toh(rpl->IOCStatus), rpl->SASStatus);
2713 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
2717 mps_dprint(sc, MPS_XINFO, "%s: SMP request to SAS address "
2718 "%#jx completed successfully\n", __func__,
2719 (uintmax_t)sasaddr);
2721 if (ccb->smpio.smp_response[2] == SMP_FR_ACCEPTED)
2722 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
2724 mpssas_set_ccbstatus(ccb, CAM_SMP_STATUS_ERROR);
2728 * We sync in both directions because we had DMAs in the S/G list
2729 * in both directions.
2731 bus_dmamap_sync(sc->buffer_dmat, cm->cm_dmamap,
2732 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
2733 bus_dmamap_unload(sc->buffer_dmat, cm->cm_dmamap);
2734 mps_free_command(sc, cm);
2739 mpssas_send_smpcmd(struct mpssas_softc *sassc, union ccb *ccb, uint64_t sasaddr)
2741 struct mps_command *cm;
2742 uint8_t *request, *response;
2743 MPI2_SMP_PASSTHROUGH_REQUEST *req;
2744 struct mps_softc *sc;
2751 * XXX We don't yet support physical addresses here.
2753 switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) {
2754 case CAM_DATA_PADDR:
2755 case CAM_DATA_SG_PADDR:
2756 mps_dprint(sc, MPS_ERROR,
2757 "%s: physical addresses not supported\n", __func__);
2758 mpssas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2763 * The chip does not support more than one buffer for the
2764 * request or response.
2766 if ((ccb->smpio.smp_request_sglist_cnt > 1)
2767 || (ccb->smpio.smp_response_sglist_cnt > 1)) {
2768 mps_dprint(sc, MPS_ERROR,
2769 "%s: multiple request or response "
2770 "buffer segments not supported for SMP\n",
2772 mpssas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2778 * The CAM_SCATTER_VALID flag was originally implemented
2779 * for the XPT_SCSI_IO CCB, which only has one data pointer.
2780 * We have two. So, just take that flag to mean that we
2781 * might have S/G lists, and look at the S/G segment count
2782 * to figure out whether that is the case for each individual
2785 if (ccb->smpio.smp_request_sglist_cnt != 0) {
2786 bus_dma_segment_t *req_sg;
2788 req_sg = (bus_dma_segment_t *)ccb->smpio.smp_request;
2789 request = (uint8_t *)(uintptr_t)req_sg[0].ds_addr;
2791 request = ccb->smpio.smp_request;
2793 if (ccb->smpio.smp_response_sglist_cnt != 0) {
2794 bus_dma_segment_t *rsp_sg;
2796 rsp_sg = (bus_dma_segment_t *)ccb->smpio.smp_response;
2797 response = (uint8_t *)(uintptr_t)rsp_sg[0].ds_addr;
2799 response = ccb->smpio.smp_response;
2801 case CAM_DATA_VADDR:
2802 request = ccb->smpio.smp_request;
2803 response = ccb->smpio.smp_response;
2806 mpssas_set_ccbstatus(ccb, CAM_REQ_INVALID);
2811 cm = mps_alloc_command(sc);
2813 mps_dprint(sc, MPS_ERROR,
2814 "%s: cannot allocate command\n", __func__);
2815 mpssas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL);
2820 req = (MPI2_SMP_PASSTHROUGH_REQUEST *)cm->cm_req;
2821 bzero(req, sizeof(*req));
2822 req->Function = MPI2_FUNCTION_SMP_PASSTHROUGH;
2824 /* Allow the chip to use any route to this SAS address. */
2825 req->PhysicalPort = 0xff;
2827 req->RequestDataLength = htole16(ccb->smpio.smp_request_len);
2829 MPI2_SGLFLAGS_SYSTEM_ADDRESS_SPACE | MPI2_SGLFLAGS_SGL_TYPE_MPI;
2831 mps_dprint(sc, MPS_XINFO, "%s: sending SMP request to SAS "
2832 "address %#jx\n", __func__, (uintmax_t)sasaddr);
2834 mpi_init_sge(cm, req, &req->SGL);
2837 * Set up a uio to pass into mps_map_command(). This allows us to
2838 * do one map command, and one busdma call in there.
2840 cm->cm_uio.uio_iov = cm->cm_iovec;
2841 cm->cm_uio.uio_iovcnt = 2;
2842 cm->cm_uio.uio_segflg = UIO_SYSSPACE;
2845 * The read/write flag isn't used by busdma, but set it just in
2846 * case. This isn't exactly accurate, either, since we're going in
2849 cm->cm_uio.uio_rw = UIO_WRITE;
2851 cm->cm_iovec[0].iov_base = request;
2852 cm->cm_iovec[0].iov_len = le16toh(req->RequestDataLength);
2853 cm->cm_iovec[1].iov_base = response;
2854 cm->cm_iovec[1].iov_len = ccb->smpio.smp_response_len;
2856 cm->cm_uio.uio_resid = cm->cm_iovec[0].iov_len +
2857 cm->cm_iovec[1].iov_len;
2860 * Trigger a warning message in mps_data_cb() for the user if we
2861 * wind up exceeding two S/G segments. The chip expects one
2862 * segment for the request and another for the response.
2864 cm->cm_max_segs = 2;
2866 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
2867 cm->cm_complete = mpssas_smpio_complete;
2868 cm->cm_complete_data = ccb;
2871 * Tell the mapping code that we're using a uio, and that this is
2872 * an SMP passthrough request. There is a little special-case
2873 * logic there (in mps_data_cb()) to handle the bidirectional
2876 cm->cm_flags |= MPS_CM_FLAGS_USE_UIO | MPS_CM_FLAGS_SMP_PASS |
2877 MPS_CM_FLAGS_DATAIN | MPS_CM_FLAGS_DATAOUT;
2879 /* The chip data format is little endian. */
2880 req->SASAddress.High = htole32(sasaddr >> 32);
2881 req->SASAddress.Low = htole32(sasaddr);
2884 * XXX Note that we don't have a timeout/abort mechanism here.
2885 * From the manual, it looks like task management requests only
2886 * work for SCSI IO and SATA passthrough requests. We may need to
2887 * have a mechanism to retry requests in the event of a chip reset
2888 * at least. Hopefully the chip will insure that any errors short
2889 * of that are relayed back to the driver.
2891 error = mps_map_command(sc, cm);
2892 if ((error != 0) && (error != EINPROGRESS)) {
2893 mps_dprint(sc, MPS_ERROR,
2894 "%s: error %d returned from mps_map_command()\n",
2902 mps_free_command(sc, cm);
2903 mpssas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL);
2910 mpssas_action_smpio(struct mpssas_softc *sassc, union ccb *ccb)
2912 struct mps_softc *sc;
2913 struct mpssas_target *targ;
2914 uint64_t sasaddr = 0;
2919 * Make sure the target exists.
2921 KASSERT(ccb->ccb_h.target_id < sassc->maxtargets,
2922 ("Target %d out of bounds in XPT_SMP_IO\n", ccb->ccb_h.target_id));
2923 targ = &sassc->targets[ccb->ccb_h.target_id];
2924 if (targ->handle == 0x0) {
2925 mps_dprint(sc, MPS_ERROR,
2926 "%s: target %d does not exist!\n", __func__,
2927 ccb->ccb_h.target_id);
2928 mpssas_set_ccbstatus(ccb, CAM_SEL_TIMEOUT);
2934 * If this device has an embedded SMP target, we'll talk to it
2936 * figure out what the expander's address is.
2938 if ((targ->devinfo & MPI2_SAS_DEVICE_INFO_SMP_TARGET) != 0)
2939 sasaddr = targ->sasaddr;
2942 * If we don't have a SAS address for the expander yet, try
2943 * grabbing it from the page 0x83 information cached in the
2944 * transport layer for this target. LSI expanders report the
2945 * expander SAS address as the port-associated SAS address in
2946 * Inquiry VPD page 0x83. Maxim expanders don't report it in page
2949 * XXX KDM disable this for now, but leave it commented out so that
2950 * it is obvious that this is another possible way to get the SAS
2953 * The parent handle method below is a little more reliable, and
2954 * the other benefit is that it works for devices other than SES
2955 * devices. So you can send a SMP request to a da(4) device and it
2956 * will get routed to the expander that device is attached to.
2957 * (Assuming the da(4) device doesn't contain an SMP target...)
2961 sasaddr = xpt_path_sas_addr(ccb->ccb_h.path);
2965 * If we still don't have a SAS address for the expander, look for
2966 * the parent device of this device, which is probably the expander.
2969 #ifdef OLD_MPS_PROBE
2970 struct mpssas_target *parent_target;
2973 if (targ->parent_handle == 0x0) {
2974 mps_dprint(sc, MPS_ERROR,
2975 "%s: handle %d does not have a valid "
2976 "parent handle!\n", __func__, targ->handle);
2977 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
2980 #ifdef OLD_MPS_PROBE
2981 parent_target = mpssas_find_target_by_handle(sassc, 0,
2982 targ->parent_handle);
2984 if (parent_target == NULL) {
2985 mps_dprint(sc, MPS_ERROR,
2986 "%s: handle %d does not have a valid "
2987 "parent target!\n", __func__, targ->handle);
2988 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
2992 if ((parent_target->devinfo &
2993 MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0) {
2994 mps_dprint(sc, MPS_ERROR,
2995 "%s: handle %d parent %d does not "
2996 "have an SMP target!\n", __func__,
2997 targ->handle, parent_target->handle);
2998 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3003 sasaddr = parent_target->sasaddr;
3004 #else /* OLD_MPS_PROBE */
3005 if ((targ->parent_devinfo &
3006 MPI2_SAS_DEVICE_INFO_SMP_TARGET) == 0) {
3007 mps_dprint(sc, MPS_ERROR,
3008 "%s: handle %d parent %d does not "
3009 "have an SMP target!\n", __func__,
3010 targ->handle, targ->parent_handle);
3011 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3015 if (targ->parent_sasaddr == 0x0) {
3016 mps_dprint(sc, MPS_ERROR,
3017 "%s: handle %d parent handle %d does "
3018 "not have a valid SAS address!\n",
3019 __func__, targ->handle, targ->parent_handle);
3020 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3024 sasaddr = targ->parent_sasaddr;
3025 #endif /* OLD_MPS_PROBE */
3030 mps_dprint(sc, MPS_INFO,
3031 "%s: unable to find SAS address for handle %d\n",
3032 __func__, targ->handle);
3033 mpssas_set_ccbstatus(ccb, CAM_DEV_NOT_THERE);
3036 mpssas_send_smpcmd(sassc, ccb, sasaddr);
3046 mpssas_action_resetdev(struct mpssas_softc *sassc, union ccb *ccb)
3048 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
3049 struct mps_softc *sc;
3050 struct mps_command *tm;
3051 struct mpssas_target *targ;
3053 MPS_FUNCTRACE(sassc->sc);
3054 mtx_assert(&sassc->sc->mps_mtx, MA_OWNED);
3056 KASSERT(ccb->ccb_h.target_id < sassc->maxtargets,
3057 ("Target %d out of bounds in XPT_RESET_DEV\n",
3058 ccb->ccb_h.target_id));
3060 tm = mpssas_alloc_tm(sc);
3062 mps_dprint(sc, MPS_ERROR,
3063 "command alloc failure in mpssas_action_resetdev\n");
3064 mpssas_set_ccbstatus(ccb, CAM_RESRC_UNAVAIL);
3069 targ = &sassc->targets[ccb->ccb_h.target_id];
3070 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
3071 req->DevHandle = htole16(targ->handle);
3072 req->TaskType = MPI2_SCSITASKMGMT_TASKTYPE_TARGET_RESET;
3074 /* SAS Hard Link Reset / SATA Link Reset */
3075 req->MsgFlags = MPI2_SCSITASKMGMT_MSGFLAGS_LINK_RESET;
3078 tm->cm_complete = mpssas_resetdev_complete;
3079 tm->cm_complete_data = ccb;
3082 mpssas_prepare_for_tm(sc, tm, targ, CAM_LUN_WILDCARD);
3083 mps_map_command(sc, tm);
3087 mpssas_resetdev_complete(struct mps_softc *sc, struct mps_command *tm)
3089 MPI2_SCSI_TASK_MANAGE_REPLY *resp;
3093 mtx_assert(&sc->mps_mtx, MA_OWNED);
3095 resp = (MPI2_SCSI_TASK_MANAGE_REPLY *)tm->cm_reply;
3096 ccb = tm->cm_complete_data;
3099 * Currently there should be no way we can hit this case. It only
3100 * happens when we have a failure to allocate chain frames, and
3101 * task management commands don't have S/G lists.
3103 if ((tm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
3104 MPI2_SCSI_TASK_MANAGE_REQUEST *req;
3106 req = (MPI2_SCSI_TASK_MANAGE_REQUEST *)tm->cm_req;
3108 mps_dprint(sc, MPS_ERROR,
3109 "%s: cm_flags = %#x for reset of handle %#04x! "
3110 "This should not happen!\n", __func__, tm->cm_flags,
3112 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
3116 mps_dprint(sc, MPS_XINFO,
3117 "%s: IOCStatus = 0x%x ResponseCode = 0x%x\n", __func__,
3118 le16toh(resp->IOCStatus), le32toh(resp->ResponseCode));
3120 if (le32toh(resp->ResponseCode) == MPI2_SCSITASKMGMT_RSP_TM_COMPLETE) {
3121 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP);
3122 mpssas_announce_reset(sc, AC_SENT_BDR, tm->cm_targ->tid,
3126 mpssas_set_ccbstatus(ccb, CAM_REQ_CMP_ERR);
3130 mpssas_free_tm(sc, tm);
3135 mpssas_poll(struct cam_sim *sim)
3137 struct mpssas_softc *sassc;
3139 sassc = cam_sim_softc(sim);
3141 if (sassc->sc->mps_debug & MPS_TRACE) {
3142 /* frequent debug messages during a panic just slow
3143 * everything down too much.
3145 mps_printf(sassc->sc, "%s clearing MPS_TRACE\n", __func__);
3146 sassc->sc->mps_debug &= ~MPS_TRACE;
3149 mps_intr_locked(sassc->sc);
3153 mpssas_async(void *callback_arg, uint32_t code, struct cam_path *path,
3156 struct mps_softc *sc;
3158 sc = (struct mps_softc *)callback_arg;
3161 case AC_ADVINFO_CHANGED: {
3162 struct mpssas_target *target;
3163 struct mpssas_softc *sassc;
3164 struct scsi_read_capacity_data_long rcap_buf;
3165 struct ccb_dev_advinfo cdai;
3166 struct mpssas_lun *lun;
3171 buftype = (uintptr_t)arg;
3177 * We're only interested in read capacity data changes.
3179 if (buftype != CDAI_TYPE_RCAPLONG)
3183 * We should have a handle for this, but check to make sure.
3185 KASSERT(xpt_path_target_id(path) < sassc->maxtargets,
3186 ("Target %d out of bounds in mpssas_async\n",
3187 xpt_path_target_id(path)));
3188 target = &sassc->targets[xpt_path_target_id(path)];
3189 if (target->handle == 0)
3192 lunid = xpt_path_lun_id(path);
3194 SLIST_FOREACH(lun, &target->luns, lun_link) {
3195 if (lun->lun_id == lunid) {
3201 if (found_lun == 0) {
3202 lun = malloc(sizeof(struct mpssas_lun), M_MPT2,
3205 mps_dprint(sc, MPS_ERROR, "Unable to alloc "
3206 "LUN for EEDP support.\n");
3209 lun->lun_id = lunid;
3210 SLIST_INSERT_HEAD(&target->luns, lun, lun_link);
3213 bzero(&rcap_buf, sizeof(rcap_buf));
3214 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
3215 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
3216 cdai.ccb_h.flags = CAM_DIR_IN;
3217 cdai.buftype = CDAI_TYPE_RCAPLONG;
3218 cdai.flags = CDAI_FLAG_NONE;
3219 cdai.bufsiz = sizeof(rcap_buf);
3220 cdai.buf = (uint8_t *)&rcap_buf;
3221 xpt_action((union ccb *)&cdai);
3222 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
3223 cam_release_devq(cdai.ccb_h.path,
3226 if ((mpssas_get_ccbstatus((union ccb *)&cdai) == CAM_REQ_CMP)
3227 && (rcap_buf.prot & SRC16_PROT_EN)) {
3228 switch (rcap_buf.prot & SRC16_P_TYPE) {
3231 lun->eedp_formatted = TRUE;
3232 lun->eedp_block_size =
3233 scsi_4btoul(rcap_buf.length);
3237 lun->eedp_formatted = FALSE;
3238 lun->eedp_block_size = 0;
3242 lun->eedp_formatted = FALSE;
3243 lun->eedp_block_size = 0;
3253 * Set the INRESET flag for this target so that no I/O will be sent to
3254 * the target until the reset has completed. If an I/O request does
3255 * happen, the devq will be frozen. The CCB holds the path which is
3256 * used to release the devq. The devq is released and the CCB is freed
3257 * when the TM completes.
3260 mpssas_prepare_for_tm(struct mps_softc *sc, struct mps_command *tm,
3261 struct mpssas_target *target, lun_id_t lun_id)
3266 ccb = xpt_alloc_ccb_nowait();
3268 path_id = cam_sim_path(sc->sassc->sim);
3269 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, path_id,
3270 target->tid, lun_id) != CAM_REQ_CMP) {
3274 tm->cm_targ = target;
3275 target->flags |= MPSSAS_TARGET_INRESET;
3281 mpssas_startup(struct mps_softc *sc)
3285 * Send the port enable message and set the wait_for_port_enable flag.
3286 * This flag helps to keep the simq frozen until all discovery events
3289 sc->wait_for_port_enable = 1;
3290 mpssas_send_portenable(sc);
3295 mpssas_send_portenable(struct mps_softc *sc)
3297 MPI2_PORT_ENABLE_REQUEST *request;
3298 struct mps_command *cm;
3302 if ((cm = mps_alloc_command(sc)) == NULL)
3304 request = (MPI2_PORT_ENABLE_REQUEST *)cm->cm_req;
3305 request->Function = MPI2_FUNCTION_PORT_ENABLE;
3306 request->MsgFlags = 0;
3308 cm->cm_desc.Default.RequestFlags = MPI2_REQ_DESCRIPT_FLAGS_DEFAULT_TYPE;
3309 cm->cm_complete = mpssas_portenable_complete;
3313 mps_map_command(sc, cm);
3314 mps_dprint(sc, MPS_XINFO,
3315 "mps_send_portenable finished cm %p req %p complete %p\n",
3316 cm, cm->cm_req, cm->cm_complete);
3321 mpssas_portenable_complete(struct mps_softc *sc, struct mps_command *cm)
3323 MPI2_PORT_ENABLE_REPLY *reply;
3324 struct mpssas_softc *sassc;
3330 * Currently there should be no way we can hit this case. It only
3331 * happens when we have a failure to allocate chain frames, and
3332 * port enable commands don't have S/G lists.
3334 if ((cm->cm_flags & MPS_CM_FLAGS_ERROR_MASK) != 0) {
3335 mps_dprint(sc, MPS_ERROR, "%s: cm_flags = %#x for port enable! "
3336 "This should not happen!\n", __func__, cm->cm_flags);
3339 reply = (MPI2_PORT_ENABLE_REPLY *)cm->cm_reply;
3341 mps_dprint(sc, MPS_FAULT, "Portenable NULL reply\n");
3342 else if (le16toh(reply->IOCStatus & MPI2_IOCSTATUS_MASK) !=
3343 MPI2_IOCSTATUS_SUCCESS)
3344 mps_dprint(sc, MPS_FAULT, "Portenable failed\n");
3346 mps_free_command(sc, cm);
3349 * Get WarpDrive info after discovery is complete but before the scan
3350 * starts. At this point, all devices are ready to be exposed to the
3351 * OS. If devices should be hidden instead, take them out of the
3352 * 'targets' array before the scan. The devinfo for a disk will have
3353 * some info and a volume's will be 0. Use that to remove disks.
3355 mps_wd_config_pages(sc);
3358 * Done waiting for port enable to complete. Decrement the refcount.
3359 * If refcount is 0, discovery is complete and a rescan of the bus can
3360 * take place. Since the simq was explicitly frozen before port
3361 * enable, it must be explicitly released here to keep the
3362 * freeze/release count in sync.
3364 sc->wait_for_port_enable = 0;
3365 sc->port_enable_complete = 1;
3366 wakeup(&sc->port_enable_complete);
3367 mpssas_startup_decrement(sassc);
3371 mpssas_check_id(struct mpssas_softc *sassc, int id)
3373 struct mps_softc *sc = sassc->sc;
3377 ids = &sc->exclude_ids[0];
3378 while((name = strsep(&ids, ",")) != NULL) {
3379 if (name[0] == '\0')
3381 if (strtol(name, NULL, 0) == (long)id)
3389 mpssas_realloc_targets(struct mps_softc *sc, int maxtargets)
3391 struct mpssas_softc *sassc;
3392 struct mpssas_lun *lun, *lun_tmp;
3393 struct mpssas_target *targ;
3398 * The number of targets is based on IOC Facts, so free all of
3399 * the allocated LUNs for each target and then the target buffer
3402 for (i=0; i< maxtargets; i++) {
3403 targ = &sassc->targets[i];
3404 SLIST_FOREACH_SAFE(lun, &targ->luns, lun_link, lun_tmp) {
3408 free(sassc->targets, M_MPT2);
3410 sassc->targets = malloc(sizeof(struct mpssas_target) * maxtargets,
3411 M_MPT2, M_WAITOK|M_ZERO);