2 * FreeBSD/CAM specific routines for LSI '909 FC adapters.
5 * Copyright (c) 2000, 2001 by Greg Ansley
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice immediately at the beginning of the file, without modification,
12 * this list of conditions, and the following disclaimer.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * Copyright (c) 2002, 2006 by Matthew Jacob
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions are
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
38 * substantially similar to the "NO WARRANTY" disclaimer below
39 * ("Disclaimer") and any redistribution must be conditioned upon including
40 * a substantially similar Disclaimer requirement for further binary
42 * 3. Neither the names of the above listed copyright holders nor the names
43 * of any contributors may be used to endorse or promote products derived
44 * from this software without specific prior written permission.
46 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
47 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
50 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
51 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
52 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
53 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
54 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
55 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
56 * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 * Support from Chris Ellsworth in order to make SAS adapters work
59 * is gratefully acknowledged.
61 * Support from LSI-Logic has also gone a great deal toward making this a
62 * workable subsystem and is gratefully acknowledged.
65 * Copyright (c) 2004, Avid Technology, Inc. and its contributors.
66 * Copyright (c) 2005, WHEEL Sp. z o.o.
67 * Copyright (c) 2004, 2005 Justin T. Gibbs
68 * All rights reserved.
70 * Redistribution and use in source and binary forms, with or without
71 * modification, are permitted provided that the following conditions are
73 * 1. Redistributions of source code must retain the above copyright
74 * notice, this list of conditions and the following disclaimer.
75 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
76 * substantially similar to the "NO WARRANTY" disclaimer below
77 * ("Disclaimer") and any redistribution must be conditioned upon including
78 * a substantially similar Disclaimer requirement for further binary
80 * 3. Neither the names of the above listed copyright holders nor the names
81 * of any contributors may be used to endorse or promote products derived
82 * from this software without specific prior written permission.
84 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
85 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
86 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
87 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
88 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
89 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
90 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
91 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
92 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
93 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
94 * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
96 #include <sys/cdefs.h>
97 __FBSDID("$FreeBSD$");
99 #include <dev/mpt/mpt.h>
100 #include <dev/mpt/mpt_cam.h>
101 #include <dev/mpt/mpt_raid.h>
103 #include "dev/mpt/mpilib/mpi_ioc.h" /* XXX Fix Event Handling!!! */
104 #include "dev/mpt/mpilib/mpi_init.h"
105 #include "dev/mpt/mpilib/mpi_targ.h"
106 #include "dev/mpt/mpilib/mpi_fc.h"
107 #include "dev/mpt/mpilib/mpi_sas.h"
108 #if __FreeBSD_version >= 500000
109 #include <sys/sysctl.h>
111 #include <sys/callout.h>
112 #include <sys/kthread.h>
114 #if __FreeBSD_version >= 700025
115 #ifndef CAM_NEW_TRAN_CODE
116 #define CAM_NEW_TRAN_CODE 1
120 static void mpt_poll(struct cam_sim *);
121 static timeout_t mpt_timeout;
122 static void mpt_action(struct cam_sim *, union ccb *);
124 mpt_get_spi_settings(struct mpt_softc *, struct ccb_trans_settings *);
125 static void mpt_setwidth(struct mpt_softc *, int, int);
126 static void mpt_setsync(struct mpt_softc *, int, int, int);
127 static int mpt_update_spi_config(struct mpt_softc *, int);
128 static void mpt_calc_geometry(struct ccb_calc_geometry *ccg, int extended);
130 static mpt_reply_handler_t mpt_scsi_reply_handler;
131 static mpt_reply_handler_t mpt_scsi_tmf_reply_handler;
132 static mpt_reply_handler_t mpt_fc_els_reply_handler;
133 static int mpt_scsi_reply_frame_handler(struct mpt_softc *, request_t *,
134 MSG_DEFAULT_REPLY *);
135 static int mpt_bus_reset(struct mpt_softc *, target_id_t, lun_id_t, int);
136 static int mpt_fc_reset_link(struct mpt_softc *, int);
138 static int mpt_spawn_recovery_thread(struct mpt_softc *mpt);
139 static void mpt_terminate_recovery_thread(struct mpt_softc *mpt);
140 static void mpt_recovery_thread(void *arg);
141 static void mpt_recover_commands(struct mpt_softc *mpt);
143 static int mpt_scsi_send_tmf(struct mpt_softc *, u_int, u_int, u_int,
144 u_int, u_int, u_int, int);
146 static void mpt_fc_post_els(struct mpt_softc *mpt, request_t *, int);
147 static void mpt_post_target_command(struct mpt_softc *, request_t *, int);
148 static int mpt_add_els_buffers(struct mpt_softc *mpt);
149 static int mpt_add_target_commands(struct mpt_softc *mpt);
150 static int mpt_enable_lun(struct mpt_softc *, target_id_t, lun_id_t);
151 static int mpt_disable_lun(struct mpt_softc *, target_id_t, lun_id_t);
152 static void mpt_target_start_io(struct mpt_softc *, union ccb *);
153 static cam_status mpt_abort_target_ccb(struct mpt_softc *, union ccb *);
154 static int mpt_abort_target_cmd(struct mpt_softc *, request_t *);
155 static void mpt_scsi_tgt_status(struct mpt_softc *, union ccb *, request_t *,
156 uint8_t, uint8_t const *);
158 mpt_scsi_tgt_tsk_mgmt(struct mpt_softc *, request_t *, mpt_task_mgmt_t,
159 tgt_resource_t *, int);
160 static void mpt_tgt_dump_tgt_state(struct mpt_softc *, request_t *);
161 static void mpt_tgt_dump_req_state(struct mpt_softc *, request_t *);
162 static mpt_reply_handler_t mpt_scsi_tgt_reply_handler;
163 static mpt_reply_handler_t mpt_sata_pass_reply_handler;
165 static uint32_t scsi_io_handler_id = MPT_HANDLER_ID_NONE;
166 static uint32_t scsi_tmf_handler_id = MPT_HANDLER_ID_NONE;
167 static uint32_t fc_els_handler_id = MPT_HANDLER_ID_NONE;
168 static uint32_t sata_pass_handler_id = MPT_HANDLER_ID_NONE;
170 static mpt_probe_handler_t mpt_cam_probe;
171 static mpt_attach_handler_t mpt_cam_attach;
172 static mpt_enable_handler_t mpt_cam_enable;
173 static mpt_ready_handler_t mpt_cam_ready;
174 static mpt_event_handler_t mpt_cam_event;
175 static mpt_reset_handler_t mpt_cam_ioc_reset;
176 static mpt_detach_handler_t mpt_cam_detach;
178 static struct mpt_personality mpt_cam_personality =
181 .probe = mpt_cam_probe,
182 .attach = mpt_cam_attach,
183 .enable = mpt_cam_enable,
184 .ready = mpt_cam_ready,
185 .event = mpt_cam_event,
186 .reset = mpt_cam_ioc_reset,
187 .detach = mpt_cam_detach,
190 DECLARE_MPT_PERSONALITY(mpt_cam, SI_ORDER_SECOND);
191 MODULE_DEPEND(mpt_cam, cam, 1, 1, 1);
193 int mpt_enable_sata_wc = -1;
194 TUNABLE_INT("hw.mpt.enable_sata_wc", &mpt_enable_sata_wc);
197 mpt_cam_probe(struct mpt_softc *mpt)
202 * Only attach to nodes that support the initiator or target role
203 * (or want to) or have RAID physical devices that need CAM pass-thru
206 if (mpt->do_cfg_role) {
207 role = mpt->cfg_role;
211 if ((role & (MPT_ROLE_TARGET|MPT_ROLE_INITIATOR)) != 0 ||
212 (mpt->ioc_page2 != NULL && mpt->ioc_page2->MaxPhysDisks != 0)) {
219 mpt_cam_attach(struct mpt_softc *mpt)
221 struct cam_devq *devq;
222 mpt_handler_t handler;
227 TAILQ_INIT(&mpt->request_timeout_list);
228 maxq = (mpt->ioc_facts.GlobalCredits < MPT_MAX_REQUESTS(mpt))?
229 mpt->ioc_facts.GlobalCredits : MPT_MAX_REQUESTS(mpt);
231 handler.reply_handler = mpt_scsi_reply_handler;
232 error = mpt_register_handler(mpt, MPT_HANDLER_REPLY, handler,
233 &scsi_io_handler_id);
239 handler.reply_handler = mpt_scsi_tmf_reply_handler;
240 error = mpt_register_handler(mpt, MPT_HANDLER_REPLY, handler,
241 &scsi_tmf_handler_id);
248 * If we're fibre channel and could support target mode, we register
249 * an ELS reply handler and give it resources.
251 if (mpt->is_fc && (mpt->role & MPT_ROLE_TARGET) != 0) {
252 handler.reply_handler = mpt_fc_els_reply_handler;
253 error = mpt_register_handler(mpt, MPT_HANDLER_REPLY, handler,
259 if (mpt_add_els_buffers(mpt) == FALSE) {
264 maxq -= mpt->els_cmds_allocated;
268 * If we support target mode, we register a reply handler for it,
269 * but don't add command resources until we actually enable target
272 if (mpt->is_fc && (mpt->role & MPT_ROLE_TARGET) != 0) {
273 handler.reply_handler = mpt_scsi_tgt_reply_handler;
274 error = mpt_register_handler(mpt, MPT_HANDLER_REPLY, handler,
275 &mpt->scsi_tgt_handler_id);
283 handler.reply_handler = mpt_sata_pass_reply_handler;
284 error = mpt_register_handler(mpt, MPT_HANDLER_REPLY, handler,
285 &sata_pass_handler_id);
293 * We keep one request reserved for timeout TMF requests.
295 mpt->tmf_req = mpt_get_request(mpt, FALSE);
296 if (mpt->tmf_req == NULL) {
297 mpt_prt(mpt, "Unable to allocate dedicated TMF request!\n");
304 * Mark the request as free even though not on the free list.
305 * There is only one TMF request allowed to be outstanding at
306 * a time and the TMF routines perform their own allocation
307 * tracking using the standard state flags.
309 mpt->tmf_req->state = REQ_STATE_FREE;
313 * The rest of this is CAM foo, for which we need to drop our lock
317 if (mpt_spawn_recovery_thread(mpt) != 0) {
318 mpt_prt(mpt, "Unable to spawn recovery thread!\n");
324 * Create the device queue for our SIM(s).
326 devq = cam_simq_alloc(maxq);
328 mpt_prt(mpt, "Unable to allocate CAM SIMQ!\n");
334 * Construct our SIM entry.
337 mpt_sim_alloc(mpt_action, mpt_poll, "mpt", mpt, 1, maxq, devq);
338 if (mpt->sim == NULL) {
339 mpt_prt(mpt, "Unable to allocate CAM SIM!\n");
346 * Register exactly this bus.
349 if (mpt_xpt_bus_register(mpt->sim, mpt->dev, 0) != CAM_SUCCESS) {
350 mpt_prt(mpt, "Bus registration Failed!\n");
356 if (xpt_create_path(&mpt->path, NULL, cam_sim_path(mpt->sim),
357 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
358 mpt_prt(mpt, "Unable to allocate Path!\n");
366 * Only register a second bus for RAID physical
367 * devices if the controller supports RAID.
369 if (mpt->ioc_page2 == NULL || mpt->ioc_page2->MaxPhysDisks == 0) {
374 * Create a "bus" to export all hidden disks to CAM.
377 mpt_sim_alloc(mpt_action, mpt_poll, "mpt", mpt, 1, maxq, devq);
378 if (mpt->phydisk_sim == NULL) {
379 mpt_prt(mpt, "Unable to allocate Physical Disk CAM SIM!\n");
388 if (mpt_xpt_bus_register(mpt->phydisk_sim, mpt->dev, 1) !=
390 mpt_prt(mpt, "Physical Disk Bus registration Failed!\n");
396 if (xpt_create_path(&mpt->phydisk_path, NULL,
397 cam_sim_path(mpt->phydisk_sim),
398 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
399 mpt_prt(mpt, "Unable to allocate Physical Disk Path!\n");
405 mpt_lprt(mpt, MPT_PRT_DEBUG, "attached cam\n");
414 * Read FC configuration information
417 mpt_read_config_info_fc(struct mpt_softc *mpt)
419 char *topology = NULL;
422 rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_FC_PORT, 0,
423 0, &mpt->mpt_fcport_page0.Header, FALSE, 5000);
427 mpt_lprt(mpt, MPT_PRT_DEBUG, "FC Port Page 0 Header: %x %x %x %x\n",
428 mpt->mpt_fcport_page0.Header.PageVersion,
429 mpt->mpt_fcport_page0.Header.PageLength,
430 mpt->mpt_fcport_page0.Header.PageNumber,
431 mpt->mpt_fcport_page0.Header.PageType);
434 rv = mpt_read_cur_cfg_page(mpt, 0, &mpt->mpt_fcport_page0.Header,
435 sizeof(mpt->mpt_fcport_page0), FALSE, 5000);
437 mpt_prt(mpt, "failed to read FC Port Page 0\n");
440 mpt2host_config_page_fc_port_0(&mpt->mpt_fcport_page0);
442 mpt->mpt_fcport_speed = mpt->mpt_fcport_page0.CurrentSpeed;
444 switch (mpt->mpt_fcport_page0.Flags &
445 MPI_FCPORTPAGE0_FLAGS_ATTACH_TYPE_MASK) {
446 case MPI_FCPORTPAGE0_FLAGS_ATTACH_NO_INIT:
447 mpt->mpt_fcport_speed = 0;
448 topology = "<NO LOOP>";
450 case MPI_FCPORTPAGE0_FLAGS_ATTACH_POINT_TO_POINT:
453 case MPI_FCPORTPAGE0_FLAGS_ATTACH_PRIVATE_LOOP:
454 topology = "NL-Port";
456 case MPI_FCPORTPAGE0_FLAGS_ATTACH_FABRIC_DIRECT:
459 case MPI_FCPORTPAGE0_FLAGS_ATTACH_PUBLIC_LOOP:
460 topology = "FL-Port";
463 mpt->mpt_fcport_speed = 0;
468 mpt_lprt(mpt, MPT_PRT_INFO,
469 "FC Port Page 0: Topology <%s> WWNN 0x%08x%08x WWPN 0x%08x%08x "
470 "Speed %u-Gbit\n", topology,
471 mpt->mpt_fcport_page0.WWNN.High,
472 mpt->mpt_fcport_page0.WWNN.Low,
473 mpt->mpt_fcport_page0.WWPN.High,
474 mpt->mpt_fcport_page0.WWPN.Low,
475 mpt->mpt_fcport_speed);
476 #if __FreeBSD_version >= 500000
479 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(mpt->dev);
480 struct sysctl_oid *tree = device_get_sysctl_tree(mpt->dev);
482 snprintf(mpt->scinfo.fc.wwnn,
483 sizeof (mpt->scinfo.fc.wwnn), "0x%08x%08x",
484 mpt->mpt_fcport_page0.WWNN.High,
485 mpt->mpt_fcport_page0.WWNN.Low);
487 snprintf(mpt->scinfo.fc.wwpn,
488 sizeof (mpt->scinfo.fc.wwpn), "0x%08x%08x",
489 mpt->mpt_fcport_page0.WWPN.High,
490 mpt->mpt_fcport_page0.WWPN.Low);
492 SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
493 "wwnn", CTLFLAG_RD, mpt->scinfo.fc.wwnn, 0,
494 "World Wide Node Name");
496 SYSCTL_ADD_STRING(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
497 "wwpn", CTLFLAG_RD, mpt->scinfo.fc.wwpn, 0,
498 "World Wide Port Name");
507 * Set FC configuration information.
510 mpt_set_initial_config_fc(struct mpt_softc *mpt)
513 CONFIG_PAGE_FC_PORT_1 fc;
518 r = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_FC_PORT, 1, 0,
519 &fc.Header, FALSE, 5000);
521 mpt_prt(mpt, "failed to read FC page 1 header\n");
522 return (mpt_fc_reset_link(mpt, 1));
525 r = mpt_read_cfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_NVRAM, 0,
526 &fc.Header, sizeof (fc), FALSE, 5000);
528 mpt_prt(mpt, "failed to read FC page 1\n");
529 return (mpt_fc_reset_link(mpt, 1));
531 mpt2host_config_page_fc_port_1(&fc);
534 * Check our flags to make sure we support the role we want.
540 if (fl & MPI_FCPORTPAGE1_FLAGS_PROT_FCP_INIT) {
541 role |= MPT_ROLE_INITIATOR;
543 if (fl & MPI_FCPORTPAGE1_FLAGS_PROT_FCP_TARG) {
544 role |= MPT_ROLE_TARGET;
547 fl &= ~MPI_FCPORTPAGE1_FLAGS_PROT_MASK;
549 if (mpt->do_cfg_role == 0) {
550 role = mpt->cfg_role;
552 mpt->do_cfg_role = 0;
555 if (role != mpt->cfg_role) {
556 if (mpt->cfg_role & MPT_ROLE_INITIATOR) {
557 if ((role & MPT_ROLE_INITIATOR) == 0) {
558 mpt_prt(mpt, "adding initiator role\n");
559 fl |= MPI_FCPORTPAGE1_FLAGS_PROT_FCP_INIT;
562 mpt_prt(mpt, "keeping initiator role\n");
564 } else if (role & MPT_ROLE_INITIATOR) {
565 mpt_prt(mpt, "removing initiator role\n");
568 if (mpt->cfg_role & MPT_ROLE_TARGET) {
569 if ((role & MPT_ROLE_TARGET) == 0) {
570 mpt_prt(mpt, "adding target role\n");
571 fl |= MPI_FCPORTPAGE1_FLAGS_PROT_FCP_TARG;
574 mpt_prt(mpt, "keeping target role\n");
576 } else if (role & MPT_ROLE_TARGET) {
577 mpt_prt(mpt, "removing target role\n");
580 mpt->role = mpt->cfg_role;
583 if (fl & MPI_FCPORTPAGE1_FLAGS_PROT_FCP_TARG) {
584 if ((fl & MPI_FCPORTPAGE1_FLAGS_TARGET_MODE_OXID) == 0) {
585 mpt_prt(mpt, "adding OXID option\n");
586 fl |= MPI_FCPORTPAGE1_FLAGS_TARGET_MODE_OXID;
593 host2mpt_config_page_fc_port_1(&fc);
594 r = mpt_write_cfg_page(mpt,
595 MPI_CONFIG_ACTION_PAGE_WRITE_NVRAM, 0, &fc.Header,
596 sizeof(fc), FALSE, 5000);
598 mpt_prt(mpt, "failed to update NVRAM with changes\n");
601 mpt_prt(mpt, "NOTE: NVRAM changes will not take "
602 "effect until next reboot or IOC reset\n");
608 mptsas_sas_io_unit_pg0(struct mpt_softc *mpt, struct mptsas_portinfo *portinfo)
610 ConfigExtendedPageHeader_t hdr;
611 struct mptsas_phyinfo *phyinfo;
612 SasIOUnitPage0_t *buffer;
615 error = mpt_read_extcfg_header(mpt, MPI_SASIOUNITPAGE0_PAGEVERSION,
616 0, 0, MPI_CONFIG_EXTPAGETYPE_SAS_IO_UNIT,
620 if (hdr.ExtPageLength == 0) {
625 len = hdr.ExtPageLength * 4;
626 buffer = malloc(len, M_DEVBUF, M_NOWAIT|M_ZERO);
627 if (buffer == NULL) {
632 error = mpt_read_extcfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
633 0, &hdr, buffer, len, 0, 10000);
635 free(buffer, M_DEVBUF);
639 portinfo->num_phys = buffer->NumPhys;
640 portinfo->phy_info = malloc(sizeof(*portinfo->phy_info) *
641 portinfo->num_phys, M_DEVBUF, M_NOWAIT|M_ZERO);
642 if (portinfo->phy_info == NULL) {
643 free(buffer, M_DEVBUF);
648 for (i = 0; i < portinfo->num_phys; i++) {
649 phyinfo = &portinfo->phy_info[i];
650 phyinfo->phy_num = i;
651 phyinfo->port_id = buffer->PhyData[i].Port;
652 phyinfo->negotiated_link_rate =
653 buffer->PhyData[i].NegotiatedLinkRate;
655 le16toh(buffer->PhyData[i].ControllerDevHandle);
658 free(buffer, M_DEVBUF);
664 mptsas_sas_phy_pg0(struct mpt_softc *mpt, struct mptsas_phyinfo *phy_info,
665 uint32_t form, uint32_t form_specific)
667 ConfigExtendedPageHeader_t hdr;
668 SasPhyPage0_t *buffer;
671 error = mpt_read_extcfg_header(mpt, MPI_SASPHY0_PAGEVERSION, 0, 0,
672 MPI_CONFIG_EXTPAGETYPE_SAS_PHY, &hdr,
676 if (hdr.ExtPageLength == 0) {
681 buffer = malloc(sizeof(SasPhyPage0_t), M_DEVBUF, M_NOWAIT|M_ZERO);
682 if (buffer == NULL) {
687 error = mpt_read_extcfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
688 form + form_specific, &hdr, buffer,
689 sizeof(SasPhyPage0_t), 0, 10000);
691 free(buffer, M_DEVBUF);
695 phy_info->hw_link_rate = buffer->HwLinkRate;
696 phy_info->programmed_link_rate = buffer->ProgrammedLinkRate;
697 phy_info->identify.dev_handle = le16toh(buffer->OwnerDevHandle);
698 phy_info->attached.dev_handle = le16toh(buffer->AttachedDevHandle);
700 free(buffer, M_DEVBUF);
706 mptsas_sas_device_pg0(struct mpt_softc *mpt, struct mptsas_devinfo *device_info,
707 uint32_t form, uint32_t form_specific)
709 ConfigExtendedPageHeader_t hdr;
710 SasDevicePage0_t *buffer;
711 uint64_t sas_address;
714 bzero(device_info, sizeof(*device_info));
715 error = mpt_read_extcfg_header(mpt, MPI_SASDEVICE0_PAGEVERSION, 0, 0,
716 MPI_CONFIG_EXTPAGETYPE_SAS_DEVICE,
720 if (hdr.ExtPageLength == 0) {
725 buffer = malloc(sizeof(SasDevicePage0_t), M_DEVBUF, M_NOWAIT|M_ZERO);
726 if (buffer == NULL) {
731 error = mpt_read_extcfg_page(mpt, MPI_CONFIG_ACTION_PAGE_READ_CURRENT,
732 form + form_specific, &hdr, buffer,
733 sizeof(SasDevicePage0_t), 0, 10000);
735 free(buffer, M_DEVBUF);
739 device_info->dev_handle = le16toh(buffer->DevHandle);
740 device_info->parent_dev_handle = le16toh(buffer->ParentDevHandle);
741 device_info->enclosure_handle = le16toh(buffer->EnclosureHandle);
742 device_info->slot = le16toh(buffer->Slot);
743 device_info->phy_num = buffer->PhyNum;
744 device_info->physical_port = buffer->PhysicalPort;
745 device_info->target_id = buffer->TargetID;
746 device_info->bus = buffer->Bus;
747 bcopy(&buffer->SASAddress, &sas_address, sizeof(uint64_t));
748 device_info->sas_address = le64toh(sas_address);
749 device_info->device_info = le32toh(buffer->DeviceInfo);
751 free(buffer, M_DEVBUF);
757 * Read SAS configuration information. Nothing to do yet.
760 mpt_read_config_info_sas(struct mpt_softc *mpt)
762 struct mptsas_portinfo *portinfo;
763 struct mptsas_phyinfo *phyinfo;
766 portinfo = malloc(sizeof(*portinfo), M_DEVBUF, M_NOWAIT|M_ZERO);
767 if (portinfo == NULL)
770 error = mptsas_sas_io_unit_pg0(mpt, portinfo);
772 free(portinfo, M_DEVBUF);
776 for (i = 0; i < portinfo->num_phys; i++) {
777 phyinfo = &portinfo->phy_info[i];
778 error = mptsas_sas_phy_pg0(mpt, phyinfo,
779 (MPI_SAS_PHY_PGAD_FORM_PHY_NUMBER <<
780 MPI_SAS_PHY_PGAD_FORM_SHIFT), i);
783 error = mptsas_sas_device_pg0(mpt, &phyinfo->identify,
784 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
785 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
789 phyinfo->identify.phy_num = phyinfo->phy_num = i;
790 if (phyinfo->attached.dev_handle)
791 error = mptsas_sas_device_pg0(mpt,
793 (MPI_SAS_DEVICE_PGAD_FORM_HANDLE <<
794 MPI_SAS_DEVICE_PGAD_FORM_SHIFT),
795 phyinfo->attached.dev_handle);
799 mpt->sas_portinfo = portinfo;
804 mptsas_set_sata_wc(struct mpt_softc *mpt, struct mptsas_devinfo *devinfo,
807 SataPassthroughRequest_t *pass;
811 req = mpt_get_request(mpt, 0);
815 pass = req->req_vbuf;
816 bzero(pass, sizeof(SataPassthroughRequest_t));
817 pass->Function = MPI_FUNCTION_SATA_PASSTHROUGH;
818 pass->TargetID = devinfo->target_id;
819 pass->Bus = devinfo->bus;
820 pass->PassthroughFlags = 0;
821 pass->ConnectionRate = MPI_SATA_PT_REQ_CONNECT_RATE_NEGOTIATED;
822 pass->DataLength = 0;
823 pass->MsgContext = htole32(req->index | sata_pass_handler_id);
824 pass->CommandFIS[0] = 0x27;
825 pass->CommandFIS[1] = 0x80;
826 pass->CommandFIS[2] = 0xef;
827 pass->CommandFIS[3] = (enabled) ? 0x02 : 0x82;
828 pass->CommandFIS[7] = 0x40;
829 pass->CommandFIS[15] = 0x08;
831 mpt_check_doorbell(mpt);
832 mpt_send_cmd(mpt, req);
833 error = mpt_wait_req(mpt, req, REQ_STATE_DONE, REQ_STATE_DONE, 0,
836 mpt_free_request(mpt, req);
837 printf("error %d sending passthrough\n", error);
841 status = le16toh(req->IOCStatus);
842 if (status != MPI_IOCSTATUS_SUCCESS) {
843 mpt_free_request(mpt, req);
844 printf("IOCSTATUS %d\n", status);
848 mpt_free_request(mpt, req);
852 * Set SAS configuration information. Nothing to do yet.
855 mpt_set_initial_config_sas(struct mpt_softc *mpt)
857 struct mptsas_phyinfo *phyinfo;
860 if ((mpt_enable_sata_wc != -1) && (mpt->sas_portinfo != NULL)) {
861 for (i = 0; i < mpt->sas_portinfo->num_phys; i++) {
862 phyinfo = &mpt->sas_portinfo->phy_info[i];
863 if (phyinfo->attached.dev_handle == 0)
865 if ((phyinfo->attached.device_info &
866 MPI_SAS_DEVICE_INFO_SATA_DEVICE) == 0)
869 device_printf(mpt->dev,
870 "%sabling SATA WC on phy %d\n",
871 (mpt_enable_sata_wc) ? "En" : "Dis", i);
872 mptsas_set_sata_wc(mpt, &phyinfo->attached,
881 mpt_sata_pass_reply_handler(struct mpt_softc *mpt, request_t *req,
882 uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
886 if (reply_frame != NULL) {
887 req->IOCStatus = le16toh(reply_frame->IOCStatus);
889 req->state &= ~REQ_STATE_QUEUED;
890 req->state |= REQ_STATE_DONE;
891 TAILQ_REMOVE(&mpt->request_pending_list, req, links);
892 if ((req->state & REQ_STATE_NEED_WAKEUP) != 0) {
894 } else if ((req->state & REQ_STATE_TIMEDOUT) != 0) {
896 * Whew- we can free this request (late completion)
898 mpt_free_request(mpt, req);
906 * Read SCSI configuration information
909 mpt_read_config_info_spi(struct mpt_softc *mpt)
913 rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_SCSI_PORT, 0, 0,
914 &mpt->mpt_port_page0.Header, FALSE, 5000);
918 mpt_lprt(mpt, MPT_PRT_DEBUG, "SPI Port Page 0 Header: %x %x %x %x\n",
919 mpt->mpt_port_page0.Header.PageVersion,
920 mpt->mpt_port_page0.Header.PageLength,
921 mpt->mpt_port_page0.Header.PageNumber,
922 mpt->mpt_port_page0.Header.PageType);
924 rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_SCSI_PORT, 1, 0,
925 &mpt->mpt_port_page1.Header, FALSE, 5000);
929 mpt_lprt(mpt, MPT_PRT_DEBUG, "SPI Port Page 1 Header: %x %x %x %x\n",
930 mpt->mpt_port_page1.Header.PageVersion,
931 mpt->mpt_port_page1.Header.PageLength,
932 mpt->mpt_port_page1.Header.PageNumber,
933 mpt->mpt_port_page1.Header.PageType);
935 rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_SCSI_PORT, 2, 0,
936 &mpt->mpt_port_page2.Header, FALSE, 5000);
940 mpt_lprt(mpt, MPT_PRT_DEBUG, "SPI Port Page 2 Header: %x %x %x %x\n",
941 mpt->mpt_port_page2.Header.PageVersion,
942 mpt->mpt_port_page2.Header.PageLength,
943 mpt->mpt_port_page2.Header.PageNumber,
944 mpt->mpt_port_page2.Header.PageType);
946 for (i = 0; i < 16; i++) {
947 rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_SCSI_DEVICE,
948 0, i, &mpt->mpt_dev_page0[i].Header, FALSE, 5000);
952 mpt_lprt(mpt, MPT_PRT_DEBUG,
953 "SPI Target %d Device Page 0 Header: %x %x %x %x\n", i,
954 mpt->mpt_dev_page0[i].Header.PageVersion,
955 mpt->mpt_dev_page0[i].Header.PageLength,
956 mpt->mpt_dev_page0[i].Header.PageNumber,
957 mpt->mpt_dev_page0[i].Header.PageType);
959 rv = mpt_read_cfg_header(mpt, MPI_CONFIG_PAGETYPE_SCSI_DEVICE,
960 1, i, &mpt->mpt_dev_page1[i].Header, FALSE, 5000);
964 mpt_lprt(mpt, MPT_PRT_DEBUG,
965 "SPI Target %d Device Page 1 Header: %x %x %x %x\n", i,
966 mpt->mpt_dev_page1[i].Header.PageVersion,
967 mpt->mpt_dev_page1[i].Header.PageLength,
968 mpt->mpt_dev_page1[i].Header.PageNumber,
969 mpt->mpt_dev_page1[i].Header.PageType);
973 * At this point, we don't *have* to fail. As long as we have
974 * valid config header information, we can (barely) lurch
978 rv = mpt_read_cur_cfg_page(mpt, 0, &mpt->mpt_port_page0.Header,
979 sizeof(mpt->mpt_port_page0), FALSE, 5000);
981 mpt_prt(mpt, "failed to read SPI Port Page 0\n");
983 mpt2host_config_page_scsi_port_0(&mpt->mpt_port_page0);
984 mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
985 "SPI Port Page 0: Capabilities %x PhysicalInterface %x\n",
986 mpt->mpt_port_page0.Capabilities,
987 mpt->mpt_port_page0.PhysicalInterface);
990 rv = mpt_read_cur_cfg_page(mpt, 0, &mpt->mpt_port_page1.Header,
991 sizeof(mpt->mpt_port_page1), FALSE, 5000);
993 mpt_prt(mpt, "failed to read SPI Port Page 1\n");
995 mpt2host_config_page_scsi_port_1(&mpt->mpt_port_page1);
996 mpt_lprt(mpt, MPT_PRT_DEBUG,
997 "SPI Port Page 1: Configuration %x OnBusTimerValue %x\n",
998 mpt->mpt_port_page1.Configuration,
999 mpt->mpt_port_page1.OnBusTimerValue);
1002 rv = mpt_read_cur_cfg_page(mpt, 0, &mpt->mpt_port_page2.Header,
1003 sizeof(mpt->mpt_port_page2), FALSE, 5000);
1005 mpt_prt(mpt, "failed to read SPI Port Page 2\n");
1007 mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
1008 "Port Page 2: Flags %x Settings %x\n",
1009 mpt->mpt_port_page2.PortFlags,
1010 mpt->mpt_port_page2.PortSettings);
1011 mpt2host_config_page_scsi_port_2(&mpt->mpt_port_page2);
1012 for (i = 0; i < 16; i++) {
1013 mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
1014 " Port Page 2 Tgt %d: timo %x SF %x Flags %x\n",
1015 i, mpt->mpt_port_page2.DeviceSettings[i].Timeout,
1016 mpt->mpt_port_page2.DeviceSettings[i].SyncFactor,
1017 mpt->mpt_port_page2.DeviceSettings[i].DeviceFlags);
1021 for (i = 0; i < 16; i++) {
1022 rv = mpt_read_cur_cfg_page(mpt, i,
1023 &mpt->mpt_dev_page0[i].Header, sizeof(*mpt->mpt_dev_page0),
1027 "cannot read SPI Target %d Device Page 0\n", i);
1030 mpt2host_config_page_scsi_device_0(&mpt->mpt_dev_page0[i]);
1031 mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
1032 "target %d page 0: Negotiated Params %x Information %x\n",
1033 i, mpt->mpt_dev_page0[i].NegotiatedParameters,
1034 mpt->mpt_dev_page0[i].Information);
1036 rv = mpt_read_cur_cfg_page(mpt, i,
1037 &mpt->mpt_dev_page1[i].Header, sizeof(*mpt->mpt_dev_page1),
1041 "cannot read SPI Target %d Device Page 1\n", i);
1044 mpt2host_config_page_scsi_device_1(&mpt->mpt_dev_page1[i]);
1045 mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
1046 "target %d page 1: Requested Params %x Configuration %x\n",
1047 i, mpt->mpt_dev_page1[i].RequestedParameters,
1048 mpt->mpt_dev_page1[i].Configuration);
1054 * Validate SPI configuration information.
1056 * In particular, validate SPI Port Page 1.
1059 mpt_set_initial_config_spi(struct mpt_softc *mpt)
1061 int i, pp1val = ((1 << mpt->mpt_ini_id) << 16) | mpt->mpt_ini_id;
1064 mpt->mpt_disc_enable = 0xff;
1065 mpt->mpt_tag_enable = 0;
1067 if (mpt->mpt_port_page1.Configuration != pp1val) {
1068 CONFIG_PAGE_SCSI_PORT_1 tmp;
1070 mpt_prt(mpt, "SPI Port Page 1 Config value bad (%x)- should "
1071 "be %x\n", mpt->mpt_port_page1.Configuration, pp1val);
1072 tmp = mpt->mpt_port_page1;
1073 tmp.Configuration = pp1val;
1074 host2mpt_config_page_scsi_port_1(&tmp);
1075 error = mpt_write_cur_cfg_page(mpt, 0,
1076 &tmp.Header, sizeof(tmp), FALSE, 5000);
1080 error = mpt_read_cur_cfg_page(mpt, 0,
1081 &tmp.Header, sizeof(tmp), FALSE, 5000);
1085 mpt2host_config_page_scsi_port_1(&tmp);
1086 if (tmp.Configuration != pp1val) {
1088 "failed to reset SPI Port Page 1 Config value\n");
1091 mpt->mpt_port_page1 = tmp;
1095 * The purpose of this exercise is to get
1096 * all targets back to async/narrow.
1098 * We skip this step if the BIOS has already negotiated
1099 * speeds with the targets.
1101 i = mpt->mpt_port_page2.PortSettings &
1102 MPI_SCSIPORTPAGE2_PORT_MASK_NEGO_MASTER_SETTINGS;
1103 if (i == MPI_SCSIPORTPAGE2_PORT_ALL_MASTER_SETTINGS) {
1104 mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
1105 "honoring BIOS transfer negotiations\n");
1107 for (i = 0; i < 16; i++) {
1108 mpt->mpt_dev_page1[i].RequestedParameters = 0;
1109 mpt->mpt_dev_page1[i].Configuration = 0;
1110 (void) mpt_update_spi_config(mpt, i);
1117 mpt_cam_enable(struct mpt_softc *mpt)
1125 if (mpt_read_config_info_fc(mpt)) {
1128 if (mpt_set_initial_config_fc(mpt)) {
1131 } else if (mpt->is_sas) {
1132 if (mpt_read_config_info_sas(mpt)) {
1135 if (mpt_set_initial_config_sas(mpt)) {
1138 } else if (mpt->is_spi) {
1139 if (mpt_read_config_info_spi(mpt)) {
1142 if (mpt_set_initial_config_spi(mpt)) {
1154 mpt_cam_ready(struct mpt_softc *mpt)
1157 * If we're in target mode, hang out resources now
1158 * so we don't cause the world to hang talking to us.
1160 if (mpt->is_fc && (mpt->role & MPT_ROLE_TARGET)) {
1162 * Try to add some target command resources
1165 if (mpt_add_target_commands(mpt) == FALSE) {
1166 mpt_prt(mpt, "failed to add target commands\n");
1174 mpt_cam_detach(struct mpt_softc *mpt)
1176 mpt_handler_t handler;
1180 mpt_terminate_recovery_thread(mpt);
1182 handler.reply_handler = mpt_scsi_reply_handler;
1183 mpt_deregister_handler(mpt, MPT_HANDLER_REPLY, handler,
1184 scsi_io_handler_id);
1185 handler.reply_handler = mpt_scsi_tmf_reply_handler;
1186 mpt_deregister_handler(mpt, MPT_HANDLER_REPLY, handler,
1187 scsi_tmf_handler_id);
1188 handler.reply_handler = mpt_fc_els_reply_handler;
1189 mpt_deregister_handler(mpt, MPT_HANDLER_REPLY, handler,
1191 handler.reply_handler = mpt_scsi_tgt_reply_handler;
1192 mpt_deregister_handler(mpt, MPT_HANDLER_REPLY, handler,
1193 mpt->scsi_tgt_handler_id);
1194 handler.reply_handler = mpt_sata_pass_reply_handler;
1195 mpt_deregister_handler(mpt, MPT_HANDLER_REPLY, handler,
1196 sata_pass_handler_id);
1198 if (mpt->tmf_req != NULL) {
1199 mpt->tmf_req->state = REQ_STATE_ALLOCATED;
1200 mpt_free_request(mpt, mpt->tmf_req);
1201 mpt->tmf_req = NULL;
1203 if (mpt->sas_portinfo != NULL) {
1204 free(mpt->sas_portinfo, M_DEVBUF);
1205 mpt->sas_portinfo = NULL;
1209 if (mpt->sim != NULL) {
1210 xpt_free_path(mpt->path);
1211 xpt_bus_deregister(cam_sim_path(mpt->sim));
1212 cam_sim_free(mpt->sim, TRUE);
1216 if (mpt->phydisk_sim != NULL) {
1217 xpt_free_path(mpt->phydisk_path);
1218 xpt_bus_deregister(cam_sim_path(mpt->phydisk_sim));
1219 cam_sim_free(mpt->phydisk_sim, TRUE);
1220 mpt->phydisk_sim = NULL;
1224 /* This routine is used after a system crash to dump core onto the swap device.
1227 mpt_poll(struct cam_sim *sim)
1229 struct mpt_softc *mpt;
1231 mpt = (struct mpt_softc *)cam_sim_softc(sim);
1236 * Watchdog timeout routine for SCSI requests.
1239 mpt_timeout(void *arg)
1242 struct mpt_softc *mpt;
1245 ccb = (union ccb *)arg;
1246 mpt = ccb->ccb_h.ccb_mpt_ptr;
1249 req = ccb->ccb_h.ccb_req_ptr;
1250 mpt_prt(mpt, "request %p:%u timed out for ccb %p (req->ccb %p)\n", req,
1251 req->serno, ccb, req->ccb);
1252 /* XXX: WHAT ARE WE TRYING TO DO HERE? */
1253 if ((req->state & REQ_STATE_QUEUED) == REQ_STATE_QUEUED) {
1254 TAILQ_REMOVE(&mpt->request_pending_list, req, links);
1255 TAILQ_INSERT_TAIL(&mpt->request_timeout_list, req, links);
1256 req->state |= REQ_STATE_TIMEDOUT;
1257 mpt_wakeup_recovery_thread(mpt);
1263 * Callback routine from "bus_dmamap_load" or, in simple cases, called directly.
1265 * Takes a list of physical segments and builds the SGL for SCSI IO command
1266 * and forwards the commard to the IOC after one last check that CAM has not
1267 * aborted the transaction.
1270 mpt_execute_req_a64(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1272 request_t *req, *trq;
1275 struct mpt_softc *mpt;
1277 uint32_t flags, nxt_off;
1279 MSG_REQUEST_HEADER *hdrp;
1284 req = (request_t *)arg;
1287 mpt = ccb->ccb_h.ccb_mpt_ptr;
1288 req = ccb->ccb_h.ccb_req_ptr;
1290 hdrp = req->req_vbuf;
1291 mpt_off = req->req_vbuf;
1293 if (error == 0 && ((uint32_t)nseg) >= mpt->max_seg_cnt) {
1298 switch (hdrp->Function) {
1299 case MPI_FUNCTION_SCSI_IO_REQUEST:
1300 case MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
1302 sglp = &((PTR_MSG_SCSI_IO_REQUEST)hdrp)->SGL;
1304 case MPI_FUNCTION_TARGET_ASSIST:
1306 sglp = &((PTR_MSG_TARGET_ASSIST_REQUEST)hdrp)->SGL;
1309 mpt_prt(mpt, "bad fct 0x%x in mpt_execute_req_a64\n",
1316 if (error == 0 && ((uint32_t)nseg) >= mpt->max_seg_cnt) {
1318 mpt_prt(mpt, "segment count %d too large (max %u)\n",
1319 nseg, mpt->max_seg_cnt);
1324 if (error != EFBIG && error != ENOMEM) {
1325 mpt_prt(mpt, "mpt_execute_req_a64: err %d\n", error);
1327 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
1329 mpt_freeze_ccb(ccb);
1330 if (error == EFBIG) {
1331 status = CAM_REQ_TOO_BIG;
1332 } else if (error == ENOMEM) {
1333 if (mpt->outofbeer == 0) {
1335 xpt_freeze_simq(mpt->sim, 1);
1336 mpt_lprt(mpt, MPT_PRT_DEBUG,
1339 status = CAM_REQUEUE_REQ;
1341 status = CAM_REQ_CMP_ERR;
1343 mpt_set_ccb_status(ccb, status);
1345 if (hdrp->Function == MPI_FUNCTION_TARGET_ASSIST) {
1346 request_t *cmd_req =
1347 MPT_TAG_2_REQ(mpt, ccb->csio.tag_id);
1348 MPT_TGT_STATE(mpt, cmd_req)->state = TGT_STATE_IN_CAM;
1349 MPT_TGT_STATE(mpt, cmd_req)->ccb = NULL;
1350 MPT_TGT_STATE(mpt, cmd_req)->req = NULL;
1352 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1353 KASSERT(ccb->ccb_h.status, ("zero ccb sts at %d\n", __LINE__));
1355 CAMLOCK_2_MPTLOCK(mpt);
1356 mpt_free_request(mpt, req);
1357 MPTLOCK_2_CAMLOCK(mpt);
1362 * No data to transfer?
1363 * Just make a single simple SGL with zero length.
1366 if (mpt->verbose >= MPT_PRT_DEBUG) {
1367 int tidx = ((char *)sglp) - mpt_off;
1368 memset(&mpt_off[tidx], 0xff, MPT_REQUEST_AREA - tidx);
1372 SGE_SIMPLE32 *se1 = (SGE_SIMPLE32 *) sglp;
1373 MPI_pSGE_SET_FLAGS(se1,
1374 (MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER |
1375 MPI_SGE_FLAGS_SIMPLE_ELEMENT | MPI_SGE_FLAGS_END_OF_LIST));
1376 se1->FlagsLength = htole32(se1->FlagsLength);
1381 flags = MPI_SGE_FLAGS_SIMPLE_ELEMENT | MPI_SGE_FLAGS_64_BIT_ADDRESSING;
1383 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1384 flags |= MPI_SGE_FLAGS_HOST_TO_IOC;
1387 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1388 flags |= MPI_SGE_FLAGS_HOST_TO_IOC;
1392 if (!(ccb->ccb_h.flags & (CAM_SG_LIST_PHYS|CAM_DATA_PHYS))) {
1393 bus_dmasync_op_t op;
1395 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1396 op = BUS_DMASYNC_PREREAD;
1398 op = BUS_DMASYNC_PREWRITE;
1401 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1402 op = BUS_DMASYNC_PREWRITE;
1404 op = BUS_DMASYNC_PREREAD;
1407 bus_dmamap_sync(mpt->buffer_dmat, req->dmap, op);
1411 * Okay, fill in what we can at the end of the command frame.
1412 * If we have up to MPT_NSGL_FIRST, we can fit them all into
1413 * the command frame.
1415 * Otherwise, we fill up through MPT_NSGL_FIRST less one
1416 * SIMPLE64 pointers and start doing CHAIN64 entries after
1420 if (nseg < MPT_NSGL_FIRST(mpt)) {
1424 * Leave room for CHAIN element
1426 first_lim = MPT_NSGL_FIRST(mpt) - 1;
1429 se = (SGE_SIMPLE64 *) sglp;
1430 for (seg = 0; seg < first_lim; seg++, se++, dm_segs++) {
1433 memset(se, 0, sizeof (*se));
1434 se->Address.Low = htole32(dm_segs->ds_addr & 0xffffffff);
1435 if (sizeof(bus_addr_t) > 4) {
1437 htole32(((uint64_t)dm_segs->ds_addr) >> 32);
1439 MPI_pSGE_SET_LENGTH(se, dm_segs->ds_len);
1441 if (seg == first_lim - 1) {
1442 tf |= MPI_SGE_FLAGS_LAST_ELEMENT;
1444 if (seg == nseg - 1) {
1445 tf |= MPI_SGE_FLAGS_END_OF_LIST |
1446 MPI_SGE_FLAGS_END_OF_BUFFER;
1448 MPI_pSGE_SET_FLAGS(se, tf);
1449 se->FlagsLength = htole32(se->FlagsLength);
1457 * Tell the IOC where to find the first chain element.
1459 hdrp->ChainOffset = ((char *)se - (char *)hdrp) >> 2;
1460 nxt_off = MPT_RQSL(mpt);
1464 * Make up the rest of the data segments out of a chain element
1465 * (contiained in the current request frame) which points to
1466 * SIMPLE64 elements in the next request frame, possibly ending
1467 * with *another* chain element (if there's more).
1469 while (seg < nseg) {
1471 uint32_t tf, cur_off;
1472 bus_addr_t chain_list_addr;
1475 * Point to the chain descriptor. Note that the chain
1476 * descriptor is at the end of the *previous* list (whether
1479 ce = (SGE_CHAIN64 *) se;
1482 * Before we change our current pointer, make sure we won't
1483 * overflow the request area with this frame. Note that we
1484 * test against 'greater than' here as it's okay in this case
1485 * to have next offset be just outside the request area.
1487 if ((nxt_off + MPT_RQSL(mpt)) > MPT_REQUEST_AREA) {
1488 nxt_off = MPT_REQUEST_AREA;
1493 * Set our SGE element pointer to the beginning of the chain
1494 * list and update our next chain list offset.
1496 se = (SGE_SIMPLE64 *) &mpt_off[nxt_off];
1498 nxt_off += MPT_RQSL(mpt);
1501 * Now initialized the chain descriptor.
1503 memset(ce, 0, sizeof (*ce));
1506 * Get the physical address of the chain list.
1508 chain_list_addr = trq->req_pbuf;
1509 chain_list_addr += cur_off;
1510 if (sizeof (bus_addr_t) > 4) {
1512 htole32(((uint64_t)chain_list_addr) >> 32);
1514 ce->Address.Low = htole32(chain_list_addr & 0xffffffff);
1515 ce->Flags = MPI_SGE_FLAGS_CHAIN_ELEMENT |
1516 MPI_SGE_FLAGS_64_BIT_ADDRESSING;
1519 * If we have more than a frame's worth of segments left,
1520 * set up the chain list to have the last element be another
1523 if ((nseg - seg) > MPT_NSGL(mpt)) {
1524 this_seg_lim = seg + MPT_NSGL(mpt) - 1;
1526 * The length of the chain is the length in bytes of the
1527 * number of segments plus the next chain element.
1529 * The next chain descriptor offset is the length,
1530 * in words, of the number of segments.
1532 ce->Length = (this_seg_lim - seg) *
1533 sizeof (SGE_SIMPLE64);
1534 ce->NextChainOffset = ce->Length >> 2;
1535 ce->Length += sizeof (SGE_CHAIN64);
1537 this_seg_lim = nseg;
1538 ce->Length = (this_seg_lim - seg) *
1539 sizeof (SGE_SIMPLE64);
1541 ce->Length = htole16(ce->Length);
1544 * Fill in the chain list SGE elements with our segment data.
1546 * If we're the last element in this chain list, set the last
1547 * element flag. If we're the completely last element period,
1548 * set the end of list and end of buffer flags.
1550 while (seg < this_seg_lim) {
1551 memset(se, 0, sizeof (*se));
1552 se->Address.Low = htole32(dm_segs->ds_addr &
1554 if (sizeof (bus_addr_t) > 4) {
1556 htole32(((uint64_t)dm_segs->ds_addr) >> 32);
1558 MPI_pSGE_SET_LENGTH(se, dm_segs->ds_len);
1560 if (seg == this_seg_lim - 1) {
1561 tf |= MPI_SGE_FLAGS_LAST_ELEMENT;
1563 if (seg == nseg - 1) {
1564 tf |= MPI_SGE_FLAGS_END_OF_LIST |
1565 MPI_SGE_FLAGS_END_OF_BUFFER;
1567 MPI_pSGE_SET_FLAGS(se, tf);
1568 se->FlagsLength = htole32(se->FlagsLength);
1576 * If we have more segments to do and we've used up all of
1577 * the space in a request area, go allocate another one
1578 * and chain to that.
1580 if (seg < nseg && nxt_off >= MPT_REQUEST_AREA) {
1583 CAMLOCK_2_MPTLOCK(mpt);
1584 nrq = mpt_get_request(mpt, FALSE);
1585 MPTLOCK_2_CAMLOCK(mpt);
1593 * Append the new request area on the tail of our list.
1595 if ((trq = req->chain) == NULL) {
1598 while (trq->chain != NULL) {
1604 mpt_off = trq->req_vbuf;
1605 if (mpt->verbose >= MPT_PRT_DEBUG) {
1606 memset(mpt_off, 0xff, MPT_REQUEST_AREA);
1614 * Last time we need to check if this CCB needs to be aborted.
1616 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
1617 if (hdrp->Function == MPI_FUNCTION_TARGET_ASSIST) {
1618 request_t *cmd_req =
1619 MPT_TAG_2_REQ(mpt, ccb->csio.tag_id);
1620 MPT_TGT_STATE(mpt, cmd_req)->state = TGT_STATE_IN_CAM;
1621 MPT_TGT_STATE(mpt, cmd_req)->ccb = NULL;
1622 MPT_TGT_STATE(mpt, cmd_req)->req = NULL;
1625 "mpt_execute_req_a64: I/O cancelled (status 0x%x)\n",
1626 ccb->ccb_h.status & CAM_STATUS_MASK);
1627 if (nseg && (ccb->ccb_h.flags & CAM_SG_LIST_PHYS) == 0) {
1628 bus_dmamap_unload(mpt->buffer_dmat, req->dmap);
1630 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1631 KASSERT(ccb->ccb_h.status, ("zero ccb sts at %d\n", __LINE__));
1633 CAMLOCK_2_MPTLOCK(mpt);
1634 mpt_free_request(mpt, req);
1635 MPTLOCK_2_CAMLOCK(mpt);
1639 ccb->ccb_h.status |= CAM_SIM_QUEUED;
1640 if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
1641 mpt_req_timeout(req, (ccb->ccb_h.timeout * hz) / 1000,
1644 if (mpt->verbose > MPT_PRT_DEBUG) {
1646 mpt_print_request(req->req_vbuf);
1647 for (trq = req->chain; trq; trq = trq->chain) {
1648 printf(" Additional Chain Area %d\n", nc++);
1649 mpt_dump_sgl(trq->req_vbuf, 0);
1653 if (hdrp->Function == MPI_FUNCTION_TARGET_ASSIST) {
1654 request_t *cmd_req = MPT_TAG_2_REQ(mpt, ccb->csio.tag_id);
1655 mpt_tgt_state_t *tgt = MPT_TGT_STATE(mpt, cmd_req);
1656 #ifdef WE_TRUST_AUTO_GOOD_STATUS
1657 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) &&
1658 csio->scsi_status == SCSI_STATUS_OK && tgt->resid == 0) {
1659 tgt->state = TGT_STATE_MOVING_DATA_AND_STATUS;
1661 tgt->state = TGT_STATE_MOVING_DATA;
1664 tgt->state = TGT_STATE_MOVING_DATA;
1667 CAMLOCK_2_MPTLOCK(mpt);
1668 mpt_send_cmd(mpt, req);
1669 MPTLOCK_2_CAMLOCK(mpt);
1673 mpt_execute_req(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1675 request_t *req, *trq;
1678 struct mpt_softc *mpt;
1680 uint32_t flags, nxt_off;
1682 MSG_REQUEST_HEADER *hdrp;
1687 req = (request_t *)arg;
1690 mpt = ccb->ccb_h.ccb_mpt_ptr;
1691 req = ccb->ccb_h.ccb_req_ptr;
1693 hdrp = req->req_vbuf;
1694 mpt_off = req->req_vbuf;
1697 if (error == 0 && ((uint32_t)nseg) >= mpt->max_seg_cnt) {
1702 switch (hdrp->Function) {
1703 case MPI_FUNCTION_SCSI_IO_REQUEST:
1704 case MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
1705 sglp = &((PTR_MSG_SCSI_IO_REQUEST)hdrp)->SGL;
1707 case MPI_FUNCTION_TARGET_ASSIST:
1709 sglp = &((PTR_MSG_TARGET_ASSIST_REQUEST)hdrp)->SGL;
1712 mpt_prt(mpt, "bad fct 0x%x in mpt_execute_req\n",
1719 if (error == 0 && ((uint32_t)nseg) >= mpt->max_seg_cnt) {
1721 mpt_prt(mpt, "segment count %d too large (max %u)\n",
1722 nseg, mpt->max_seg_cnt);
1727 if (error != EFBIG && error != ENOMEM) {
1728 mpt_prt(mpt, "mpt_execute_req: err %d\n", error);
1730 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INPROG) {
1732 mpt_freeze_ccb(ccb);
1733 if (error == EFBIG) {
1734 status = CAM_REQ_TOO_BIG;
1735 } else if (error == ENOMEM) {
1736 if (mpt->outofbeer == 0) {
1738 xpt_freeze_simq(mpt->sim, 1);
1739 mpt_lprt(mpt, MPT_PRT_DEBUG,
1742 status = CAM_REQUEUE_REQ;
1744 status = CAM_REQ_CMP_ERR;
1746 mpt_set_ccb_status(ccb, status);
1748 if (hdrp->Function == MPI_FUNCTION_TARGET_ASSIST) {
1749 request_t *cmd_req =
1750 MPT_TAG_2_REQ(mpt, ccb->csio.tag_id);
1751 MPT_TGT_STATE(mpt, cmd_req)->state = TGT_STATE_IN_CAM;
1752 MPT_TGT_STATE(mpt, cmd_req)->ccb = NULL;
1753 MPT_TGT_STATE(mpt, cmd_req)->req = NULL;
1755 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
1756 KASSERT(ccb->ccb_h.status, ("zero ccb sts at %d\n", __LINE__));
1758 CAMLOCK_2_MPTLOCK(mpt);
1759 mpt_free_request(mpt, req);
1760 MPTLOCK_2_CAMLOCK(mpt);
1765 * No data to transfer?
1766 * Just make a single simple SGL with zero length.
1769 if (mpt->verbose >= MPT_PRT_DEBUG) {
1770 int tidx = ((char *)sglp) - mpt_off;
1771 memset(&mpt_off[tidx], 0xff, MPT_REQUEST_AREA - tidx);
1775 SGE_SIMPLE32 *se1 = (SGE_SIMPLE32 *) sglp;
1776 MPI_pSGE_SET_FLAGS(se1,
1777 (MPI_SGE_FLAGS_LAST_ELEMENT | MPI_SGE_FLAGS_END_OF_BUFFER |
1778 MPI_SGE_FLAGS_SIMPLE_ELEMENT | MPI_SGE_FLAGS_END_OF_LIST));
1779 se1->FlagsLength = htole32(se1->FlagsLength);
1784 flags = MPI_SGE_FLAGS_SIMPLE_ELEMENT;
1786 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1787 flags |= MPI_SGE_FLAGS_HOST_TO_IOC;
1790 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1791 flags |= MPI_SGE_FLAGS_HOST_TO_IOC;
1795 if (!(ccb->ccb_h.flags & (CAM_SG_LIST_PHYS|CAM_DATA_PHYS))) {
1796 bus_dmasync_op_t op;
1798 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1799 op = BUS_DMASYNC_PREREAD;
1801 op = BUS_DMASYNC_PREWRITE;
1804 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
1805 op = BUS_DMASYNC_PREWRITE;
1807 op = BUS_DMASYNC_PREREAD;
1810 bus_dmamap_sync(mpt->buffer_dmat, req->dmap, op);
1814 * Okay, fill in what we can at the end of the command frame.
1815 * If we have up to MPT_NSGL_FIRST, we can fit them all into
1816 * the command frame.
1818 * Otherwise, we fill up through MPT_NSGL_FIRST less one
1819 * SIMPLE32 pointers and start doing CHAIN32 entries after
1823 if (nseg < MPT_NSGL_FIRST(mpt)) {
1827 * Leave room for CHAIN element
1829 first_lim = MPT_NSGL_FIRST(mpt) - 1;
1832 se = (SGE_SIMPLE32 *) sglp;
1833 for (seg = 0; seg < first_lim; seg++, se++, dm_segs++) {
1836 memset(se, 0,sizeof (*se));
1837 se->Address = htole32(dm_segs->ds_addr);
1841 MPI_pSGE_SET_LENGTH(se, dm_segs->ds_len);
1843 if (seg == first_lim - 1) {
1844 tf |= MPI_SGE_FLAGS_LAST_ELEMENT;
1846 if (seg == nseg - 1) {
1847 tf |= MPI_SGE_FLAGS_END_OF_LIST |
1848 MPI_SGE_FLAGS_END_OF_BUFFER;
1850 MPI_pSGE_SET_FLAGS(se, tf);
1851 se->FlagsLength = htole32(se->FlagsLength);
1859 * Tell the IOC where to find the first chain element.
1861 hdrp->ChainOffset = ((char *)se - (char *)hdrp) >> 2;
1862 nxt_off = MPT_RQSL(mpt);
1866 * Make up the rest of the data segments out of a chain element
1867 * (contiained in the current request frame) which points to
1868 * SIMPLE32 elements in the next request frame, possibly ending
1869 * with *another* chain element (if there's more).
1871 while (seg < nseg) {
1873 uint32_t tf, cur_off;
1874 bus_addr_t chain_list_addr;
1877 * Point to the chain descriptor. Note that the chain
1878 * descriptor is at the end of the *previous* list (whether
1881 ce = (SGE_CHAIN32 *) se;
1884 * Before we change our current pointer, make sure we won't
1885 * overflow the request area with this frame. Note that we
1886 * test against 'greater than' here as it's okay in this case
1887 * to have next offset be just outside the request area.
1889 if ((nxt_off + MPT_RQSL(mpt)) > MPT_REQUEST_AREA) {
1890 nxt_off = MPT_REQUEST_AREA;
1895 * Set our SGE element pointer to the beginning of the chain
1896 * list and update our next chain list offset.
1898 se = (SGE_SIMPLE32 *) &mpt_off[nxt_off];
1900 nxt_off += MPT_RQSL(mpt);
1903 * Now initialized the chain descriptor.
1905 memset(ce, 0, sizeof (*ce));
1908 * Get the physical address of the chain list.
1910 chain_list_addr = trq->req_pbuf;
1911 chain_list_addr += cur_off;
1915 ce->Address = htole32(chain_list_addr);
1916 ce->Flags = MPI_SGE_FLAGS_CHAIN_ELEMENT;
1920 * If we have more than a frame's worth of segments left,
1921 * set up the chain list to have the last element be another
1924 if ((nseg - seg) > MPT_NSGL(mpt)) {
1925 this_seg_lim = seg + MPT_NSGL(mpt) - 1;
1927 * The length of the chain is the length in bytes of the
1928 * number of segments plus the next chain element.
1930 * The next chain descriptor offset is the length,
1931 * in words, of the number of segments.
1933 ce->Length = (this_seg_lim - seg) *
1934 sizeof (SGE_SIMPLE32);
1935 ce->NextChainOffset = ce->Length >> 2;
1936 ce->Length += sizeof (SGE_CHAIN32);
1938 this_seg_lim = nseg;
1939 ce->Length = (this_seg_lim - seg) *
1940 sizeof (SGE_SIMPLE32);
1942 ce->Length = htole16(ce->Length);
1945 * Fill in the chain list SGE elements with our segment data.
1947 * If we're the last element in this chain list, set the last
1948 * element flag. If we're the completely last element period,
1949 * set the end of list and end of buffer flags.
1951 while (seg < this_seg_lim) {
1952 memset(se, 0, sizeof (*se));
1953 se->Address = htole32(dm_segs->ds_addr);
1958 MPI_pSGE_SET_LENGTH(se, dm_segs->ds_len);
1960 if (seg == this_seg_lim - 1) {
1961 tf |= MPI_SGE_FLAGS_LAST_ELEMENT;
1963 if (seg == nseg - 1) {
1964 tf |= MPI_SGE_FLAGS_END_OF_LIST |
1965 MPI_SGE_FLAGS_END_OF_BUFFER;
1967 MPI_pSGE_SET_FLAGS(se, tf);
1968 se->FlagsLength = htole32(se->FlagsLength);
1976 * If we have more segments to do and we've used up all of
1977 * the space in a request area, go allocate another one
1978 * and chain to that.
1980 if (seg < nseg && nxt_off >= MPT_REQUEST_AREA) {
1983 CAMLOCK_2_MPTLOCK(mpt);
1984 nrq = mpt_get_request(mpt, FALSE);
1985 MPTLOCK_2_CAMLOCK(mpt);
1993 * Append the new request area on the tail of our list.
1995 if ((trq = req->chain) == NULL) {
1998 while (trq->chain != NULL) {
2004 mpt_off = trq->req_vbuf;
2005 if (mpt->verbose >= MPT_PRT_DEBUG) {
2006 memset(mpt_off, 0xff, MPT_REQUEST_AREA);
2014 * Last time we need to check if this CCB needs to be aborted.
2016 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
2017 if (hdrp->Function == MPI_FUNCTION_TARGET_ASSIST) {
2018 request_t *cmd_req =
2019 MPT_TAG_2_REQ(mpt, ccb->csio.tag_id);
2020 MPT_TGT_STATE(mpt, cmd_req)->state = TGT_STATE_IN_CAM;
2021 MPT_TGT_STATE(mpt, cmd_req)->ccb = NULL;
2022 MPT_TGT_STATE(mpt, cmd_req)->req = NULL;
2025 "mpt_execute_req: I/O cancelled (status 0x%x)\n",
2026 ccb->ccb_h.status & CAM_STATUS_MASK);
2027 if (nseg && (ccb->ccb_h.flags & CAM_SG_LIST_PHYS) == 0) {
2028 bus_dmamap_unload(mpt->buffer_dmat, req->dmap);
2030 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2031 KASSERT(ccb->ccb_h.status, ("zero ccb sts at %d\n", __LINE__));
2033 CAMLOCK_2_MPTLOCK(mpt);
2034 mpt_free_request(mpt, req);
2035 MPTLOCK_2_CAMLOCK(mpt);
2039 ccb->ccb_h.status |= CAM_SIM_QUEUED;
2040 if (ccb->ccb_h.timeout != CAM_TIME_INFINITY) {
2041 mpt_req_timeout(req, (ccb->ccb_h.timeout * hz) / 1000,
2044 if (mpt->verbose > MPT_PRT_DEBUG) {
2046 mpt_print_request(req->req_vbuf);
2047 for (trq = req->chain; trq; trq = trq->chain) {
2048 printf(" Additional Chain Area %d\n", nc++);
2049 mpt_dump_sgl(trq->req_vbuf, 0);
2053 if (hdrp->Function == MPI_FUNCTION_TARGET_ASSIST) {
2054 request_t *cmd_req = MPT_TAG_2_REQ(mpt, ccb->csio.tag_id);
2055 mpt_tgt_state_t *tgt = MPT_TGT_STATE(mpt, cmd_req);
2056 #ifdef WE_TRUST_AUTO_GOOD_STATUS
2057 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) &&
2058 csio->scsi_status == SCSI_STATUS_OK && tgt->resid == 0) {
2059 tgt->state = TGT_STATE_MOVING_DATA_AND_STATUS;
2061 tgt->state = TGT_STATE_MOVING_DATA;
2064 tgt->state = TGT_STATE_MOVING_DATA;
2067 CAMLOCK_2_MPTLOCK(mpt);
2068 mpt_send_cmd(mpt, req);
2069 MPTLOCK_2_CAMLOCK(mpt);
2073 mpt_start(struct cam_sim *sim, union ccb *ccb)
2076 struct mpt_softc *mpt;
2077 MSG_SCSI_IO_REQUEST *mpt_req;
2078 struct ccb_scsiio *csio = &ccb->csio;
2079 struct ccb_hdr *ccbh = &ccb->ccb_h;
2080 bus_dmamap_callback_t *cb;
2084 /* Get the pointer for the physical addapter */
2085 mpt = ccb->ccb_h.ccb_mpt_ptr;
2086 raid_passthru = (sim == mpt->phydisk_sim);
2088 CAMLOCK_2_MPTLOCK(mpt);
2089 if ((req = mpt_get_request(mpt, FALSE)) == NULL) {
2090 if (mpt->outofbeer == 0) {
2092 xpt_freeze_simq(mpt->sim, 1);
2093 mpt_lprt(mpt, MPT_PRT_DEBUG, "FREEZEQ\n");
2095 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2096 mpt_set_ccb_status(ccb, CAM_REQUEUE_REQ);
2097 MPTLOCK_2_CAMLOCK(mpt);
2102 mpt_req_not_spcl(mpt, req, "mpt_start", __LINE__);
2104 MPTLOCK_2_CAMLOCK(mpt);
2106 if (sizeof (bus_addr_t) > 4) {
2107 cb = mpt_execute_req_a64;
2109 cb = mpt_execute_req;
2113 * Link the ccb and the request structure so we can find
2114 * the other knowing either the request or the ccb
2117 ccb->ccb_h.ccb_req_ptr = req;
2119 /* Now we build the command for the IOC */
2120 mpt_req = req->req_vbuf;
2121 memset(mpt_req, 0, sizeof (MSG_SCSI_IO_REQUEST));
2123 mpt_req->Function = MPI_FUNCTION_SCSI_IO_REQUEST;
2124 if (raid_passthru) {
2125 mpt_req->Function = MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH;
2126 CAMLOCK_2_MPTLOCK(mpt);
2127 if (mpt_map_physdisk(mpt, ccb, &tgt) != 0) {
2128 MPTLOCK_2_CAMLOCK(mpt);
2129 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2130 mpt_set_ccb_status(ccb, CAM_DEV_NOT_THERE);
2134 MPTLOCK_2_CAMLOCK(mpt);
2135 mpt_req->Bus = 0; /* we never set bus here */
2137 tgt = ccb->ccb_h.target_id;
2138 mpt_req->Bus = 0; /* XXX */
2141 mpt_req->SenseBufferLength =
2142 (csio->sense_len < MPT_SENSE_SIZE) ?
2143 csio->sense_len : MPT_SENSE_SIZE;
2146 * We use the message context to find the request structure when we
2147 * Get the command completion interrupt from the IOC.
2149 mpt_req->MsgContext = htole32(req->index | scsi_io_handler_id);
2151 /* Which physical device to do the I/O on */
2152 mpt_req->TargetID = tgt;
2154 /* We assume a single level LUN type */
2155 if (ccb->ccb_h.target_lun >= MPT_MAX_LUNS) {
2156 mpt_req->LUN[0] = 0x40 | ((ccb->ccb_h.target_lun >> 8) & 0x3f);
2157 mpt_req->LUN[1] = ccb->ccb_h.target_lun & 0xff;
2159 mpt_req->LUN[1] = ccb->ccb_h.target_lun;
2162 /* Set the direction of the transfer */
2163 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2164 mpt_req->Control = MPI_SCSIIO_CONTROL_READ;
2165 } else if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
2166 mpt_req->Control = MPI_SCSIIO_CONTROL_WRITE;
2168 mpt_req->Control = MPI_SCSIIO_CONTROL_NODATATRANSFER;
2171 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) {
2172 switch(ccb->csio.tag_action) {
2173 case MSG_HEAD_OF_Q_TAG:
2174 mpt_req->Control |= MPI_SCSIIO_CONTROL_HEADOFQ;
2177 mpt_req->Control |= MPI_SCSIIO_CONTROL_ACAQ;
2179 case MSG_ORDERED_Q_TAG:
2180 mpt_req->Control |= MPI_SCSIIO_CONTROL_ORDEREDQ;
2182 case MSG_SIMPLE_Q_TAG:
2184 mpt_req->Control |= MPI_SCSIIO_CONTROL_SIMPLEQ;
2188 if (mpt->is_fc || mpt->is_sas) {
2189 mpt_req->Control |= MPI_SCSIIO_CONTROL_SIMPLEQ;
2191 /* XXX No such thing for a target doing packetized. */
2192 mpt_req->Control |= MPI_SCSIIO_CONTROL_UNTAGGED;
2197 if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) {
2198 mpt_req->Control |= MPI_SCSIIO_CONTROL_NO_DISCONNECT;
2201 mpt_req->Control = htole32(mpt_req->Control);
2203 /* Copy the scsi command block into place */
2204 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
2205 bcopy(csio->cdb_io.cdb_ptr, mpt_req->CDB, csio->cdb_len);
2207 bcopy(csio->cdb_io.cdb_bytes, mpt_req->CDB, csio->cdb_len);
2210 mpt_req->CDBLength = csio->cdb_len;
2211 mpt_req->DataLength = htole32(csio->dxfer_len);
2212 mpt_req->SenseBufferLowAddr = htole32(req->sense_pbuf);
2215 * Do a *short* print here if we're set to MPT_PRT_DEBUG
2217 if (mpt->verbose == MPT_PRT_DEBUG) {
2219 mpt_prt(mpt, "mpt_start: %s op 0x%x ",
2220 (mpt_req->Function == MPI_FUNCTION_SCSI_IO_REQUEST)?
2221 "SCSI_IO_REQUEST" : "SCSI_IO_PASSTHRU", mpt_req->CDB[0]);
2222 df = mpt_req->Control & MPI_SCSIIO_CONTROL_DATADIRECTION_MASK;
2223 if (df != MPI_SCSIIO_CONTROL_NODATATRANSFER) {
2224 mpt_prtc(mpt, "(%s %u byte%s ",
2225 (df == MPI_SCSIIO_CONTROL_READ)?
2226 "read" : "write", csio->dxfer_len,
2227 (csio->dxfer_len == 1)? ")" : "s)");
2229 mpt_prtc(mpt, "tgt %u lun %u req %p:%u\n", tgt,
2230 ccb->ccb_h.target_lun, req, req->serno);
2234 * If we have any data to send with this command map it into bus space.
2236 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
2237 if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
2239 * We've been given a pointer to a single buffer.
2241 if ((ccbh->flags & CAM_DATA_PHYS) == 0) {
2243 * Virtual address that needs to translated into
2244 * one or more physical address ranges.
2247 int s = splsoftvm();
2248 error = bus_dmamap_load(mpt->buffer_dmat,
2249 req->dmap, csio->data_ptr, csio->dxfer_len,
2252 if (error == EINPROGRESS) {
2254 * So as to maintain ordering,
2255 * freeze the controller queue
2256 * until our mapping is
2259 xpt_freeze_simq(mpt->sim, 1);
2260 ccbh->status |= CAM_RELEASE_SIMQ;
2264 * We have been given a pointer to single
2267 struct bus_dma_segment seg;
2269 (bus_addr_t)(vm_offset_t)csio->data_ptr;
2270 seg.ds_len = csio->dxfer_len;
2271 (*cb)(req, &seg, 1, 0);
2275 * We have been given a list of addresses.
2276 * This case could be easily supported but they are not
2277 * currently generated by the CAM subsystem so there
2278 * is no point in wasting the time right now.
2280 struct bus_dma_segment *segs;
2281 if ((ccbh->flags & CAM_SG_LIST_PHYS) == 0) {
2282 (*cb)(req, NULL, 0, EFAULT);
2284 /* Just use the segments provided */
2285 segs = (struct bus_dma_segment *)csio->data_ptr;
2286 (*cb)(req, segs, csio->sglist_cnt, 0);
2290 (*cb)(req, NULL, 0, 0);
2295 mpt_bus_reset(struct mpt_softc *mpt, target_id_t tgt, lun_id_t lun,
2302 error = mpt_scsi_send_tmf(mpt,
2303 (tgt != CAM_TARGET_WILDCARD || lun != CAM_LUN_WILDCARD) ?
2304 MPI_SCSITASKMGMT_TASKTYPE_TARGET_RESET :
2305 MPI_SCSITASKMGMT_TASKTYPE_RESET_BUS,
2306 mpt->is_fc ? MPI_SCSITASKMGMT_MSGFLAGS_LIP_RESET_OPTION : 0,
2307 0, /* XXX How do I get the channel ID? */
2308 tgt != CAM_TARGET_WILDCARD ? tgt : 0,
2309 lun != CAM_LUN_WILDCARD ? lun : 0,
2314 * mpt_scsi_send_tmf hard resets on failure, so no
2315 * need to do so here.
2318 "mpt_bus_reset: mpt_scsi_send_tmf returned %d\n", error);
2322 /* Wait for bus reset to be processed by the IOC. */
2323 error = mpt_wait_req(mpt, mpt->tmf_req, REQ_STATE_DONE,
2324 REQ_STATE_DONE, sleep_ok, 5000);
2326 status = le16toh(mpt->tmf_req->IOCStatus);
2327 response = mpt->tmf_req->ResponseCode;
2328 mpt->tmf_req->state = REQ_STATE_FREE;
2331 mpt_prt(mpt, "mpt_bus_reset: Reset timed-out. "
2332 "Resetting controller.\n");
2333 mpt_reset(mpt, TRUE);
2337 if ((status & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
2338 mpt_prt(mpt, "mpt_bus_reset: TMF IOC Status 0x%x. "
2339 "Resetting controller.\n", status);
2340 mpt_reset(mpt, TRUE);
2344 if (response != MPI_SCSITASKMGMT_RSP_TM_SUCCEEDED &&
2345 response != MPI_SCSITASKMGMT_RSP_TM_COMPLETE) {
2346 mpt_prt(mpt, "mpt_bus_reset: TMF Response 0x%x. "
2347 "Resetting controller.\n", response);
2348 mpt_reset(mpt, TRUE);
2355 mpt_fc_reset_link(struct mpt_softc *mpt, int dowait)
2359 PTR_MSG_FC_PRIMITIVE_SEND_REQUEST fc;
2361 req = mpt_get_request(mpt, FALSE);
2366 memset(fc, 0, sizeof(*fc));
2367 fc->SendFlags = MPI_FC_PRIM_SEND_FLAGS_RESET_LINK;
2368 fc->Function = MPI_FUNCTION_FC_PRIMITIVE_SEND;
2369 fc->MsgContext = htole32(req->index | fc_els_handler_id);
2370 mpt_send_cmd(mpt, req);
2372 r = mpt_wait_req(mpt, req, REQ_STATE_DONE,
2373 REQ_STATE_DONE, FALSE, 60 * 1000);
2375 mpt_free_request(mpt, req);
2382 mpt_cam_event(struct mpt_softc *mpt, request_t *req,
2383 MSG_EVENT_NOTIFY_REPLY *msg)
2385 uint32_t data0, data1;
2387 data0 = le32toh(msg->Data[0]);
2388 data1 = le32toh(msg->Data[1]);
2389 switch(msg->Event & 0xFF) {
2390 case MPI_EVENT_UNIT_ATTENTION:
2391 mpt_prt(mpt, "UNIT ATTENTION: Bus: 0x%02x TargetID: 0x%02x\n",
2392 (data0 >> 8) & 0xff, data0 & 0xff);
2395 case MPI_EVENT_IOC_BUS_RESET:
2396 /* We generated a bus reset */
2397 mpt_prt(mpt, "IOC Generated Bus Reset Port: %d\n",
2398 (data0 >> 8) & 0xff);
2399 xpt_async(AC_BUS_RESET, mpt->path, NULL);
2402 case MPI_EVENT_EXT_BUS_RESET:
2403 /* Someone else generated a bus reset */
2404 mpt_prt(mpt, "External Bus Reset Detected\n");
2406 * These replies don't return EventData like the MPI
2409 xpt_async(AC_BUS_RESET, mpt->path, NULL);
2412 case MPI_EVENT_RESCAN:
2413 #if __FreeBSD_version >= 600000
2418 * In general this means a device has been added to the loop.
2420 mpt_prt(mpt, "Rescan Port: %d\n", (data0 >> 8) & 0xff);
2421 if (mpt->ready == 0) {
2424 if (mpt->phydisk_sim) {
2425 pathid = cam_sim_path(mpt->phydisk_sim);
2427 pathid = cam_sim_path(mpt->sim);
2429 MPTLOCK_2_CAMLOCK(mpt);
2431 * Allocate a CCB, create a wildcard path for this bus,
2432 * and schedule a rescan.
2434 ccb = xpt_alloc_ccb_nowait();
2436 mpt_prt(mpt, "unable to alloc CCB for rescan\n");
2437 CAMLOCK_2_MPTLOCK(mpt);
2441 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, pathid,
2442 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2443 CAMLOCK_2_MPTLOCK(mpt);
2444 mpt_prt(mpt, "unable to create path for rescan\n");
2449 CAMLOCK_2_MPTLOCK(mpt);
2453 mpt_prt(mpt, "Rescan Port: %d\n", (data0 >> 8) & 0xff);
2456 case MPI_EVENT_LINK_STATUS_CHANGE:
2457 mpt_prt(mpt, "Port %d: LinkState: %s\n",
2458 (data1 >> 8) & 0xff,
2459 ((data0 & 0xff) == 0)? "Failed" : "Active");
2462 case MPI_EVENT_LOOP_STATE_CHANGE:
2463 switch ((data0 >> 16) & 0xff) {
2466 "Port 0x%x: FC LinkEvent: LIP(%02x,%02x) "
2467 "(Loop Initialization)\n",
2468 (data1 >> 8) & 0xff,
2469 (data0 >> 8) & 0xff,
2471 switch ((data0 >> 8) & 0xff) {
2473 if ((data0 & 0xff) == 0xF7) {
2474 mpt_prt(mpt, "Device needs AL_PA\n");
2476 mpt_prt(mpt, "Device %02x doesn't like "
2482 if ((data0 & 0xff) == 0xF7) {
2483 mpt_prt(mpt, "Device had loop failure "
2484 "at its receiver prior to acquiring"
2487 mpt_prt(mpt, "Device %02x detected loop"
2488 " failure at its receiver\n",
2493 mpt_prt(mpt, "Device %02x requests that device "
2494 "%02x reset itself\n",
2496 (data0 >> 8) & 0xFF);
2501 mpt_prt(mpt, "Port 0x%x: FC LinkEvent: "
2502 "LPE(%02x,%02x) (Loop Port Enable)\n",
2503 (data1 >> 8) & 0xff, /* Port */
2504 (data0 >> 8) & 0xff, /* Character 3 */
2505 (data0 ) & 0xff /* Character 4 */);
2508 mpt_prt(mpt, "Port 0x%x: FC LinkEvent: "
2509 "LPB(%02x,%02x) (Loop Port Bypass)\n",
2510 (data1 >> 8) & 0xff, /* Port */
2511 (data0 >> 8) & 0xff, /* Character 3 */
2512 (data0 ) & 0xff /* Character 4 */);
2515 mpt_prt(mpt, "Port 0x%x: FC LinkEvent: Unknown "
2516 "FC event (%02x %02x %02x)\n",
2517 (data1 >> 8) & 0xff, /* Port */
2518 (data0 >> 16) & 0xff, /* Event */
2519 (data0 >> 8) & 0xff, /* Character 3 */
2520 (data0 ) & 0xff /* Character 4 */);
2524 case MPI_EVENT_LOGOUT:
2525 mpt_prt(mpt, "FC Logout Port: %d N_PortID: %02x\n",
2526 (data1 >> 8) & 0xff, data0);
2528 case MPI_EVENT_QUEUE_FULL:
2530 struct cam_sim *sim;
2531 struct cam_path *tmppath;
2532 struct ccb_relsim crs;
2533 PTR_EVENT_DATA_QUEUE_FULL pqf;
2536 pqf = (PTR_EVENT_DATA_QUEUE_FULL)msg->Data;
2537 pqf->CurrentDepth = le16toh(pqf->CurrentDepth);
2538 mpt_prt(mpt, "QUEUE FULL EVENT: Bus 0x%02x Target 0x%02x Depth "
2539 "%d\n", pqf->Bus, pqf->TargetID, pqf->CurrentDepth);
2540 if (mpt->phydisk_sim) {
2541 sim = mpt->phydisk_sim;
2545 MPTLOCK_2_CAMLOCK(mpt);
2546 for (lun_id = 0; lun_id < MPT_MAX_LUNS; lun_id++) {
2547 if (xpt_create_path(&tmppath, NULL, cam_sim_path(sim),
2548 pqf->TargetID, lun_id) != CAM_REQ_CMP) {
2549 mpt_prt(mpt, "unable to create a path to send "
2551 CAMLOCK_2_MPTLOCK(mpt);
2554 xpt_setup_ccb(&crs.ccb_h, tmppath, 5);
2555 crs.ccb_h.func_code = XPT_REL_SIMQ;
2556 crs.release_flags = RELSIM_ADJUST_OPENINGS;
2557 crs.openings = pqf->CurrentDepth - 1;
2558 xpt_action((union ccb *)&crs);
2559 if (crs.ccb_h.status != CAM_REQ_CMP) {
2560 mpt_prt(mpt, "XPT_REL_SIMQ failed\n");
2562 xpt_free_path(tmppath);
2564 CAMLOCK_2_MPTLOCK(mpt);
2567 case MPI_EVENT_EVENT_CHANGE:
2568 case MPI_EVENT_INTEGRATED_RAID:
2569 case MPI_EVENT_SAS_DEVICE_STATUS_CHANGE:
2570 case MPI_EVENT_SAS_SES:
2573 mpt_lprt(mpt, MPT_PRT_WARN, "mpt_cam_event: 0x%x\n",
2581 * Reply path for all SCSI I/O requests, called from our
2582 * interrupt handler by extracting our handler index from
2583 * the MsgContext field of the reply from the IOC.
2585 * This routine is optimized for the common case of a
2586 * completion without error. All exception handling is
2587 * offloaded to non-inlined helper routines to minimize
2591 mpt_scsi_reply_handler(struct mpt_softc *mpt, request_t *req,
2592 uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
2594 MSG_SCSI_IO_REQUEST *scsi_req;
2597 if (req->state == REQ_STATE_FREE) {
2598 mpt_prt(mpt, "mpt_scsi_reply_handler: req already free\n");
2602 scsi_req = (MSG_SCSI_IO_REQUEST *)req->req_vbuf;
2605 mpt_prt(mpt, "mpt_scsi_reply_handler: req %p:%u with no ccb\n",
2610 mpt_req_untimeout(req, mpt_timeout, ccb);
2611 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
2613 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
2614 bus_dmasync_op_t op;
2616 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
2617 op = BUS_DMASYNC_POSTREAD;
2619 op = BUS_DMASYNC_POSTWRITE;
2620 bus_dmamap_sync(mpt->buffer_dmat, req->dmap, op);
2621 bus_dmamap_unload(mpt->buffer_dmat, req->dmap);
2624 if (reply_frame == NULL) {
2626 * Context only reply, completion without error status.
2628 ccb->csio.resid = 0;
2629 mpt_set_ccb_status(ccb, CAM_REQ_CMP);
2630 ccb->csio.scsi_status = SCSI_STATUS_OK;
2632 mpt_scsi_reply_frame_handler(mpt, req, reply_frame);
2635 if (mpt->outofbeer) {
2636 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
2638 mpt_lprt(mpt, MPT_PRT_DEBUG, "THAWQ\n");
2640 if (scsi_req->CDB[0] == INQUIRY && (scsi_req->CDB[1] & SI_EVPD) == 0) {
2641 struct scsi_inquiry_data *iq =
2642 (struct scsi_inquiry_data *)ccb->csio.data_ptr;
2643 if (scsi_req->Function ==
2644 MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH) {
2646 * Fake out the device type so that only the
2647 * pass-thru device will attach.
2649 iq->device &= ~0x1F;
2650 iq->device |= T_NODEVICE;
2653 if (mpt->verbose == MPT_PRT_DEBUG) {
2654 mpt_prt(mpt, "mpt_scsi_reply_handler: %p:%u complete\n",
2657 KASSERT(ccb->ccb_h.status, ("zero ccb sts at %d\n", __LINE__));
2658 MPTLOCK_2_CAMLOCK(mpt);
2660 CAMLOCK_2_MPTLOCK(mpt);
2661 if ((req->state & REQ_STATE_TIMEDOUT) == 0) {
2662 TAILQ_REMOVE(&mpt->request_pending_list, req, links);
2664 mpt_prt(mpt, "completing timedout/aborted req %p:%u\n",
2666 TAILQ_REMOVE(&mpt->request_timeout_list, req, links);
2668 KASSERT((req->state & REQ_STATE_NEED_WAKEUP) == 0,
2669 ("CCB req needed wakeup"));
2671 mpt_req_not_spcl(mpt, req, "mpt_scsi_reply_handler", __LINE__);
2673 mpt_free_request(mpt, req);
2678 mpt_scsi_tmf_reply_handler(struct mpt_softc *mpt, request_t *req,
2679 uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
2681 MSG_SCSI_TASK_MGMT_REPLY *tmf_reply;
2683 KASSERT(req == mpt->tmf_req, ("TMF Reply not using mpt->tmf_req"));
2685 mpt_req_not_spcl(mpt, req, "mpt_scsi_tmf_reply_handler", __LINE__);
2687 tmf_reply = (MSG_SCSI_TASK_MGMT_REPLY *)reply_frame;
2688 /* Record IOC Status and Response Code of TMF for any waiters. */
2689 req->IOCStatus = le16toh(tmf_reply->IOCStatus);
2690 req->ResponseCode = tmf_reply->ResponseCode;
2692 mpt_lprt(mpt, MPT_PRT_DEBUG, "TMF complete: req %p:%u status 0x%x\n",
2693 req, req->serno, le16toh(tmf_reply->IOCStatus));
2694 TAILQ_REMOVE(&mpt->request_pending_list, req, links);
2695 if ((req->state & REQ_STATE_NEED_WAKEUP) != 0) {
2696 req->state |= REQ_STATE_DONE;
2699 mpt->tmf_req->state = REQ_STATE_FREE;
2705 * XXX: Move to definitions file
2723 mpt_fc_els_send_response(struct mpt_softc *mpt, request_t *req,
2724 PTR_MSG_LINK_SERVICE_BUFFER_POST_REPLY rp, U8 length)
2727 MSG_LINK_SERVICE_RSP_REQUEST tmp;
2728 PTR_MSG_LINK_SERVICE_RSP_REQUEST rsp;
2731 * We are going to reuse the ELS request to send this response back.
2734 memset(rsp, 0, sizeof(*rsp));
2736 #ifdef USE_IMMEDIATE_LINK_DATA
2738 * Apparently the IMMEDIATE stuff doesn't seem to work.
2740 rsp->RspFlags = LINK_SERVICE_RSP_FLAGS_IMMEDIATE;
2742 rsp->RspLength = length;
2743 rsp->Function = MPI_FUNCTION_FC_LINK_SRVC_RSP;
2744 rsp->MsgContext = htole32(req->index | fc_els_handler_id);
2747 * Copy over information from the original reply frame to
2748 * it's correct place in the response.
2750 memcpy((U8 *)rsp + 0x0c, (U8 *)rp + 0x1c, 24);
2753 * And now copy back the temporary area to the original frame.
2755 memcpy(req->req_vbuf, rsp, sizeof (MSG_LINK_SERVICE_RSP_REQUEST));
2756 rsp = req->req_vbuf;
2758 #ifdef USE_IMMEDIATE_LINK_DATA
2759 memcpy((U8 *)&rsp->SGL, &((U8 *)req->req_vbuf)[MPT_RQSL(mpt)], length);
2762 PTR_SGE_SIMPLE32 se = (PTR_SGE_SIMPLE32) &rsp->SGL;
2763 bus_addr_t paddr = req->req_pbuf;
2764 paddr += MPT_RQSL(mpt);
2767 MPI_SGE_FLAGS_HOST_TO_IOC |
2768 MPI_SGE_FLAGS_SIMPLE_ELEMENT |
2769 MPI_SGE_FLAGS_LAST_ELEMENT |
2770 MPI_SGE_FLAGS_END_OF_LIST |
2771 MPI_SGE_FLAGS_END_OF_BUFFER;
2772 fl <<= MPI_SGE_FLAGS_SHIFT;
2774 se->FlagsLength = htole32(fl);
2775 se->Address = htole32((uint32_t) paddr);
2782 mpt_send_cmd(mpt, req);
2786 mpt_fc_els_reply_handler(struct mpt_softc *mpt, request_t *req,
2787 uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
2789 PTR_MSG_LINK_SERVICE_BUFFER_POST_REPLY rp =
2790 (PTR_MSG_LINK_SERVICE_BUFFER_POST_REPLY) reply_frame;
2794 U16 status = le16toh(reply_frame->IOCStatus);
2797 int do_refresh = TRUE;
2800 KASSERT(mpt_req_on_free_list(mpt, req) == 0,
2801 ("fc_els_reply_handler: req %p:%u for function %x on freelist!",
2802 req, req->serno, rp->Function));
2803 if (rp->Function != MPI_FUNCTION_FC_PRIMITIVE_SEND) {
2804 mpt_req_spcl(mpt, req, "fc_els_reply_handler", __LINE__);
2806 mpt_req_not_spcl(mpt, req, "fc_els_reply_handler", __LINE__);
2809 mpt_lprt(mpt, MPT_PRT_DEBUG,
2810 "FC_ELS Complete: req %p:%u, reply %p function %x\n",
2811 req, req->serno, reply_frame, reply_frame->Function);
2813 if (status != MPI_IOCSTATUS_SUCCESS) {
2814 mpt_prt(mpt, "ELS REPLY STATUS 0x%x for Function %x\n",
2815 status, reply_frame->Function);
2816 if (status == MPI_IOCSTATUS_INVALID_STATE) {
2818 * XXX: to get around shutdown issue
2827 * If the function of a link service response, we recycle the
2828 * response to be a refresh for a new link service request.
2830 * The request pointer is bogus in this case and we have to fetch
2831 * it based upon the TransactionContext.
2833 if (rp->Function == MPI_FUNCTION_FC_LINK_SRVC_RSP) {
2834 /* Freddie Uncle Charlie Katie */
2835 /* We don't get the IOINDEX as part of the Link Svc Rsp */
2836 for (ioindex = 0; ioindex < mpt->els_cmds_allocated; ioindex++)
2837 if (mpt->els_cmd_ptrs[ioindex] == req) {
2841 KASSERT(ioindex < mpt->els_cmds_allocated,
2842 ("can't find my mommie!"));
2844 /* remove from active list as we're going to re-post it */
2845 TAILQ_REMOVE(&mpt->request_pending_list, req, links);
2846 req->state &= ~REQ_STATE_QUEUED;
2847 req->state |= REQ_STATE_DONE;
2848 mpt_fc_post_els(mpt, req, ioindex);
2852 if (rp->Function == MPI_FUNCTION_FC_PRIMITIVE_SEND) {
2853 /* remove from active list as we're done */
2854 TAILQ_REMOVE(&mpt->request_pending_list, req, links);
2855 req->state &= ~REQ_STATE_QUEUED;
2856 req->state |= REQ_STATE_DONE;
2857 if (req->state & REQ_STATE_TIMEDOUT) {
2858 mpt_lprt(mpt, MPT_PRT_DEBUG,
2859 "Sync Primitive Send Completed After Timeout\n");
2860 mpt_free_request(mpt, req);
2861 } else if ((req->state & REQ_STATE_NEED_WAKEUP) == 0) {
2862 mpt_lprt(mpt, MPT_PRT_DEBUG,
2863 "Async Primitive Send Complete\n");
2864 mpt_free_request(mpt, req);
2866 mpt_lprt(mpt, MPT_PRT_DEBUG,
2867 "Sync Primitive Send Complete- Waking Waiter\n");
2873 if (rp->Function != MPI_FUNCTION_FC_LINK_SRVC_BUF_POST) {
2874 mpt_prt(mpt, "unexpected ELS_REPLY: Function 0x%x Flags %x "
2875 "Length %d Message Flags %x\n", rp->Function, rp->Flags,
2876 rp->MsgLength, rp->MsgFlags);
2880 if (rp->MsgLength <= 5) {
2882 * This is just a ack of an original ELS buffer post
2884 mpt_lprt(mpt, MPT_PRT_DEBUG,
2885 "RECV'd ACK of FC_ELS buf post %p:%u\n", req, req->serno);
2890 rctl = (le32toh(rp->Rctl_Did) & MPI_FC_RCTL_MASK) >> MPI_FC_RCTL_SHIFT;
2891 type = (le32toh(rp->Type_Fctl) & MPI_FC_TYPE_MASK) >> MPI_FC_TYPE_SHIFT;
2893 elsbuf = &((U32 *)req->req_vbuf)[MPT_RQSL(mpt)/sizeof (U32)];
2894 cmd = be32toh(elsbuf[0]) >> 24;
2896 if (rp->Flags & MPI_LS_BUF_POST_REPLY_FLAG_NO_RSP_NEEDED) {
2897 mpt_lprt(mpt, MPT_PRT_ALWAYS, "ELS_REPLY: response unneeded\n");
2901 ioindex = le32toh(rp->TransactionContext);
2902 req = mpt->els_cmd_ptrs[ioindex];
2904 if (rctl == ELS && type == 1) {
2908 * Send back a PRLI ACC
2910 mpt_prt(mpt, "PRLI from 0x%08x%08x\n",
2911 le32toh(rp->Wwn.PortNameHigh),
2912 le32toh(rp->Wwn.PortNameLow));
2913 elsbuf[0] = htobe32(0x02100014);
2914 elsbuf[1] |= htobe32(0x00000100);
2915 elsbuf[4] = htobe32(0x00000002);
2916 if (mpt->role & MPT_ROLE_TARGET)
2917 elsbuf[4] |= htobe32(0x00000010);
2918 if (mpt->role & MPT_ROLE_INITIATOR)
2919 elsbuf[4] |= htobe32(0x00000020);
2920 /* remove from active list as we're done */
2921 TAILQ_REMOVE(&mpt->request_pending_list, req, links);
2922 req->state &= ~REQ_STATE_QUEUED;
2923 req->state |= REQ_STATE_DONE;
2924 mpt_fc_els_send_response(mpt, req, rp, 20);
2928 memset(elsbuf, 0, 5 * (sizeof (U32)));
2929 elsbuf[0] = htobe32(0x02100014);
2930 elsbuf[1] = htobe32(0x08000100);
2931 mpt_prt(mpt, "PRLO from 0x%08x%08x\n",
2932 le32toh(rp->Wwn.PortNameHigh),
2933 le32toh(rp->Wwn.PortNameLow));
2934 /* remove from active list as we're done */
2935 TAILQ_REMOVE(&mpt->request_pending_list, req, links);
2936 req->state &= ~REQ_STATE_QUEUED;
2937 req->state |= REQ_STATE_DONE;
2938 mpt_fc_els_send_response(mpt, req, rp, 20);
2942 mpt_prt(mpt, "ELS TYPE 1 COMMAND: %x\n", cmd);
2945 } else if (rctl == ABTS && type == 0) {
2946 uint16_t rx_id = le16toh(rp->Rxid);
2947 uint16_t ox_id = le16toh(rp->Oxid);
2948 request_t *tgt_req = NULL;
2951 "ELS: ABTS OX_ID 0x%x RX_ID 0x%x from 0x%08x%08x\n",
2952 ox_id, rx_id, le32toh(rp->Wwn.PortNameHigh),
2953 le32toh(rp->Wwn.PortNameLow));
2954 if (rx_id >= mpt->mpt_max_tgtcmds) {
2955 mpt_prt(mpt, "Bad RX_ID 0x%x\n", rx_id);
2956 } else if (mpt->tgt_cmd_ptrs == NULL) {
2957 mpt_prt(mpt, "No TGT CMD PTRS\n");
2959 tgt_req = mpt->tgt_cmd_ptrs[rx_id];
2962 mpt_tgt_state_t *tgt = MPT_TGT_STATE(mpt, tgt_req);
2963 union ccb *ccb = tgt->ccb;
2967 * Check to make sure we have the correct command
2968 * The reply descriptor in the target state should
2969 * should contain an IoIndex that should match the
2972 * It'd be nice to have OX_ID to crosscheck with
2975 ct_id = GET_IO_INDEX(tgt->reply_desc);
2977 if (ct_id != rx_id) {
2978 mpt_lprt(mpt, MPT_PRT_ERROR, "ABORT Mismatch: "
2979 "RX_ID received=0x%x; RX_ID in cmd=0x%x\n",
2987 "CCB (%p): lun %u flags %x status %x\n",
2988 ccb, ccb->ccb_h.target_lun,
2989 ccb->ccb_h.flags, ccb->ccb_h.status);
2991 mpt_prt(mpt, "target state 0x%x resid %u xfrd %u rpwrd "
2992 "%x nxfers %x\n", tgt->state,
2993 tgt->resid, tgt->bytes_xfered, tgt->reply_desc,
2996 if (mpt_abort_target_cmd(mpt, tgt_req)) {
2997 mpt_prt(mpt, "unable to start TargetAbort\n");
3000 mpt_prt(mpt, "no back pointer for RX_ID 0x%x\n", rx_id);
3002 memset(elsbuf, 0, 5 * (sizeof (U32)));
3003 elsbuf[0] = htobe32(0);
3004 elsbuf[1] = htobe32((ox_id << 16) | rx_id);
3005 elsbuf[2] = htobe32(0x000ffff);
3007 * Dork with the reply frame so that the reponse to it
3010 rp->Rctl_Did += ((BA_ACC - ABTS) << MPI_FC_RCTL_SHIFT);
3011 /* remove from active list as we're done */
3012 TAILQ_REMOVE(&mpt->request_pending_list, req, links);
3013 req->state &= ~REQ_STATE_QUEUED;
3014 req->state |= REQ_STATE_DONE;
3015 mpt_fc_els_send_response(mpt, req, rp, 12);
3018 mpt_prt(mpt, "ELS: RCTL %x TYPE %x CMD %x\n", rctl, type, cmd);
3020 if (do_refresh == TRUE) {
3021 /* remove from active list as we're done */
3022 TAILQ_REMOVE(&mpt->request_pending_list, req, links);
3023 req->state &= ~REQ_STATE_QUEUED;
3024 req->state |= REQ_STATE_DONE;
3025 mpt_fc_post_els(mpt, req, ioindex);
3031 * Clean up all SCSI Initiator personality state in response
3032 * to a controller reset.
3035 mpt_cam_ioc_reset(struct mpt_softc *mpt, int type)
3038 * The pending list is already run down by
3039 * the generic handler. Perform the same
3040 * operation on the timed out request list.
3042 mpt_complete_request_chain(mpt, &mpt->request_timeout_list,
3043 MPI_IOCSTATUS_INVALID_STATE);
3046 * XXX: We need to repost ELS and Target Command Buffers?
3050 * Inform the XPT that a bus reset has occurred.
3052 xpt_async(AC_BUS_RESET, mpt->path, NULL);
3056 * Parse additional completion information in the reply
3057 * frame for SCSI I/O requests.
3060 mpt_scsi_reply_frame_handler(struct mpt_softc *mpt, request_t *req,
3061 MSG_DEFAULT_REPLY *reply_frame)
3064 MSG_SCSI_IO_REPLY *scsi_io_reply;
3068 MPT_DUMP_REPLY_FRAME(mpt, reply_frame);
3069 KASSERT(reply_frame->Function == MPI_FUNCTION_SCSI_IO_REQUEST
3070 || reply_frame->Function == MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH,
3071 ("MPT SCSI I/O Handler called with incorrect reply type"));
3072 KASSERT((reply_frame->MsgFlags & MPI_MSGFLAGS_CONTINUATION_REPLY) == 0,
3073 ("MPT SCSI I/O Handler called with continuation reply"));
3075 scsi_io_reply = (MSG_SCSI_IO_REPLY *)reply_frame;
3076 ioc_status = le16toh(scsi_io_reply->IOCStatus);
3077 ioc_status &= MPI_IOCSTATUS_MASK;
3078 sstate = scsi_io_reply->SCSIState;
3082 ccb->csio.dxfer_len - le32toh(scsi_io_reply->TransferCount);
3084 if ((sstate & MPI_SCSI_STATE_AUTOSENSE_VALID) != 0
3085 && (ccb->ccb_h.flags & (CAM_SENSE_PHYS | CAM_SENSE_PTR)) == 0) {
3086 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
3087 ccb->csio.sense_resid =
3088 ccb->csio.sense_len - le32toh(scsi_io_reply->SenseCount);
3089 bcopy(req->sense_vbuf, &ccb->csio.sense_data,
3090 min(ccb->csio.sense_len,
3091 le32toh(scsi_io_reply->SenseCount)));
3094 if ((sstate & MPI_SCSI_STATE_QUEUE_TAG_REJECTED) != 0) {
3096 * Tag messages rejected, but non-tagged retry
3099 mpt_set_tags(mpt, devinfo, MPT_QUEUE_NONE);
3103 switch(ioc_status) {
3104 case MPI_IOCSTATUS_SCSI_RESIDUAL_MISMATCH:
3107 * Linux driver indicates that a zero
3108 * transfer length with this error code
3109 * indicates a CRC error.
3111 * No need to swap the bytes for checking
3114 if (scsi_io_reply->TransferCount == 0) {
3115 mpt_set_ccb_status(ccb, CAM_UNCOR_PARITY);
3119 case MPI_IOCSTATUS_SCSI_DATA_UNDERRUN:
3120 case MPI_IOCSTATUS_SUCCESS:
3121 case MPI_IOCSTATUS_SCSI_RECOVERED_ERROR:
3122 if ((sstate & MPI_SCSI_STATE_NO_SCSI_STATUS) != 0) {
3124 * Status was never returned for this transaction.
3126 mpt_set_ccb_status(ccb, CAM_UNEXP_BUSFREE);
3127 } else if (scsi_io_reply->SCSIStatus != SCSI_STATUS_OK) {
3128 ccb->csio.scsi_status = scsi_io_reply->SCSIStatus;
3129 mpt_set_ccb_status(ccb, CAM_SCSI_STATUS_ERROR);
3130 if ((sstate & MPI_SCSI_STATE_AUTOSENSE_FAILED) != 0)
3131 mpt_set_ccb_status(ccb, CAM_AUTOSENSE_FAIL);
3132 } else if ((sstate & MPI_SCSI_STATE_RESPONSE_INFO_VALID) != 0) {
3134 /* XXX Handle SPI-Packet and FCP-2 reponse info. */
3135 mpt_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
3137 mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3139 case MPI_IOCSTATUS_SCSI_DATA_OVERRUN:
3140 mpt_set_ccb_status(ccb, CAM_DATA_RUN_ERR);
3142 case MPI_IOCSTATUS_SCSI_IO_DATA_ERROR:
3143 mpt_set_ccb_status(ccb, CAM_UNCOR_PARITY);
3145 case MPI_IOCSTATUS_SCSI_DEVICE_NOT_THERE:
3147 * Since selection timeouts and "device really not
3148 * there" are grouped into this error code, report
3149 * selection timeout. Selection timeouts are
3150 * typically retried before giving up on the device
3151 * whereas "device not there" errors are considered
3154 mpt_set_ccb_status(ccb, CAM_SEL_TIMEOUT);
3156 case MPI_IOCSTATUS_SCSI_PROTOCOL_ERROR:
3157 mpt_set_ccb_status(ccb, CAM_SEQUENCE_FAIL);
3159 case MPI_IOCSTATUS_SCSI_INVALID_BUS:
3160 mpt_set_ccb_status(ccb, CAM_PATH_INVALID);
3162 case MPI_IOCSTATUS_SCSI_INVALID_TARGETID:
3163 mpt_set_ccb_status(ccb, CAM_TID_INVALID);
3165 case MPI_IOCSTATUS_SCSI_TASK_MGMT_FAILED:
3166 ccb->ccb_h.status = CAM_UA_TERMIO;
3168 case MPI_IOCSTATUS_INVALID_STATE:
3170 * The IOC has been reset. Emulate a bus reset.
3173 case MPI_IOCSTATUS_SCSI_EXT_TERMINATED:
3174 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
3176 case MPI_IOCSTATUS_SCSI_TASK_TERMINATED:
3177 case MPI_IOCSTATUS_SCSI_IOC_TERMINATED:
3179 * Don't clobber any timeout status that has
3180 * already been set for this transaction. We
3181 * want the SCSI layer to be able to differentiate
3182 * between the command we aborted due to timeout
3183 * and any innocent bystanders.
3185 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG)
3187 mpt_set_ccb_status(ccb, CAM_REQ_TERMIO);
3190 case MPI_IOCSTATUS_INSUFFICIENT_RESOURCES:
3191 mpt_set_ccb_status(ccb, CAM_RESRC_UNAVAIL);
3193 case MPI_IOCSTATUS_BUSY:
3194 mpt_set_ccb_status(ccb, CAM_BUSY);
3196 case MPI_IOCSTATUS_INVALID_FUNCTION:
3197 case MPI_IOCSTATUS_INVALID_SGL:
3198 case MPI_IOCSTATUS_INTERNAL_ERROR:
3199 case MPI_IOCSTATUS_INVALID_FIELD:
3202 * Some of the above may need to kick
3203 * of a recovery action!!!!
3205 ccb->ccb_h.status = CAM_UNREC_HBA_ERROR;
3209 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
3210 mpt_freeze_ccb(ccb);
3217 mpt_action(struct cam_sim *sim, union ccb *ccb)
3219 struct mpt_softc *mpt;
3220 struct ccb_trans_settings *cts;
3225 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("mpt_action\n"));
3227 mpt = (struct mpt_softc *)cam_sim_softc(sim);
3228 raid_passthru = (sim == mpt->phydisk_sim);
3229 MPT_LOCK_ASSERT(mpt);
3231 tgt = ccb->ccb_h.target_id;
3232 lun = ccb->ccb_h.target_lun;
3233 if (raid_passthru &&
3234 ccb->ccb_h.func_code != XPT_PATH_INQ &&
3235 ccb->ccb_h.func_code != XPT_RESET_BUS &&
3236 ccb->ccb_h.func_code != XPT_RESET_DEV) {
3237 CAMLOCK_2_MPTLOCK(mpt);
3238 if (mpt_map_physdisk(mpt, ccb, &tgt) != 0) {
3239 MPTLOCK_2_CAMLOCK(mpt);
3240 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3241 mpt_set_ccb_status(ccb, CAM_DEV_NOT_THERE);
3245 MPTLOCK_2_CAMLOCK(mpt);
3247 ccb->ccb_h.ccb_mpt_ptr = mpt;
3249 switch (ccb->ccb_h.func_code) {
3250 case XPT_SCSI_IO: /* Execute the requested I/O operation */
3252 * Do a couple of preliminary checks...
3254 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
3255 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) != 0) {
3256 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3257 mpt_set_ccb_status(ccb, CAM_REQ_INVALID);
3261 /* Max supported CDB length is 16 bytes */
3262 /* XXX Unless we implement the new 32byte message type */
3263 if (ccb->csio.cdb_len >
3264 sizeof (((PTR_MSG_SCSI_IO_REQUEST)0)->CDB)) {
3265 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3266 mpt_set_ccb_status(ccb, CAM_REQ_INVALID);
3269 #ifdef MPT_TEST_MULTIPATH
3270 if (mpt->failure_id == ccb->ccb_h.target_id) {
3271 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3272 mpt_set_ccb_status(ccb, CAM_SEL_TIMEOUT);
3276 ccb->csio.scsi_status = SCSI_STATUS_OK;
3277 mpt_start(sim, ccb);
3281 if (raid_passthru) {
3282 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3283 mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3287 if (ccb->ccb_h.func_code == XPT_RESET_BUS) {
3289 xpt_print(ccb->ccb_h.path, "reset bus\n");
3292 xpt_print(ccb->ccb_h.path, "reset device\n");
3294 CAMLOCK_2_MPTLOCK(mpt);
3295 (void) mpt_bus_reset(mpt, tgt, lun, FALSE);
3296 MPTLOCK_2_CAMLOCK(mpt);
3299 * mpt_bus_reset is always successful in that it
3300 * will fall back to a hard reset should a bus
3301 * reset attempt fail.
3303 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3304 mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3309 union ccb *accb = ccb->cab.abort_ccb;
3310 CAMLOCK_2_MPTLOCK(mpt);
3311 switch (accb->ccb_h.func_code) {
3312 case XPT_ACCEPT_TARGET_IO:
3313 case XPT_IMMED_NOTIFY:
3314 ccb->ccb_h.status = mpt_abort_target_ccb(mpt, ccb);
3316 case XPT_CONT_TARGET_IO:
3317 mpt_prt(mpt, "cannot abort active CTIOs yet\n");
3318 ccb->ccb_h.status = CAM_UA_ABORT;
3321 ccb->ccb_h.status = CAM_UA_ABORT;
3324 ccb->ccb_h.status = CAM_REQ_INVALID;
3327 MPTLOCK_2_CAMLOCK(mpt);
3331 #ifdef CAM_NEW_TRAN_CODE
3332 #define IS_CURRENT_SETTINGS(c) ((c)->type == CTS_TYPE_CURRENT_SETTINGS)
3334 #define IS_CURRENT_SETTINGS(c) ((c)->flags & CCB_TRANS_CURRENT_SETTINGS)
3336 #define DP_DISC_ENABLE 0x1
3337 #define DP_DISC_DISABL 0x2
3338 #define DP_DISC (DP_DISC_ENABLE|DP_DISC_DISABL)
3340 #define DP_TQING_ENABLE 0x4
3341 #define DP_TQING_DISABL 0x8
3342 #define DP_TQING (DP_TQING_ENABLE|DP_TQING_DISABL)
3344 #define DP_WIDE 0x10
3345 #define DP_NARROW 0x20
3346 #define DP_WIDTH (DP_WIDE|DP_NARROW)
3348 #define DP_SYNC 0x40
3350 case XPT_SET_TRAN_SETTINGS: /* Nexus Settings */
3352 #ifdef CAM_NEW_TRAN_CODE
3353 struct ccb_trans_settings_scsi *scsi;
3354 struct ccb_trans_settings_spi *spi;
3363 if (mpt->is_fc || mpt->is_sas) {
3364 mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3368 #ifdef CAM_NEW_TRAN_CODE
3369 scsi = &cts->proto_specific.scsi;
3370 spi = &cts->xport_specific.spi;
3373 * We can be called just to valid transport and proto versions
3375 if (scsi->valid == 0 && spi->valid == 0) {
3376 mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3382 * Skip attempting settings on RAID volume disks.
3383 * Other devices on the bus get the normal treatment.
3385 if (mpt->phydisk_sim && raid_passthru == 0 &&
3386 mpt_is_raid_volume(mpt, tgt) != 0) {
3387 mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
3388 "no transfer settings for RAID vols\n");
3389 mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3393 i = mpt->mpt_port_page2.PortSettings &
3394 MPI_SCSIPORTPAGE2_PORT_MASK_NEGO_MASTER_SETTINGS;
3395 j = mpt->mpt_port_page2.PortFlags &
3396 MPI_SCSIPORTPAGE2_PORT_FLAGS_DV_MASK;
3397 if (i == MPI_SCSIPORTPAGE2_PORT_ALL_MASTER_SETTINGS &&
3398 j == MPI_SCSIPORTPAGE2_PORT_FLAGS_OFF_DV) {
3399 mpt_lprt(mpt, MPT_PRT_ALWAYS,
3400 "honoring BIOS transfer negotiations\n");
3401 mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3409 #ifndef CAM_NEW_TRAN_CODE
3410 if ((cts->valid & CCB_TRANS_DISC_VALID) != 0) {
3411 dval |= (cts->flags & CCB_TRANS_DISC_ENB) ?
3412 DP_DISC_ENABLE : DP_DISC_DISABL;
3415 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
3416 dval |= (cts->flags & CCB_TRANS_TAG_ENB) ?
3417 DP_TQING_ENABLE : DP_TQING_DISABL;
3420 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) != 0) {
3421 dval |= cts->bus_width ? DP_WIDE : DP_NARROW;
3424 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) &&
3425 (cts->valid & CCB_TRANS_SYNC_OFFSET_VALID)) {
3427 period = cts->sync_period;
3428 offset = cts->sync_offset;
3431 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
3432 dval |= ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0) ?
3433 DP_DISC_ENABLE : DP_DISC_DISABL;
3436 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
3437 dval |= ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) ?
3438 DP_TQING_ENABLE : DP_TQING_DISABL;
3441 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
3442 dval |= (spi->bus_width == MSG_EXT_WDTR_BUS_16_BIT) ?
3443 DP_WIDE : DP_NARROW;
3446 if (spi->valid & CTS_SPI_VALID_SYNC_OFFSET) {
3448 offset = spi->sync_offset;
3450 PTR_CONFIG_PAGE_SCSI_DEVICE_1 ptr =
3451 &mpt->mpt_dev_page1[tgt];
3452 offset = ptr->RequestedParameters;
3453 offset &= MPI_SCSIDEVPAGE1_RP_MAX_SYNC_OFFSET_MASK;
3454 offset >>= MPI_SCSIDEVPAGE1_RP_SHIFT_MAX_SYNC_OFFSET;
3456 if (spi->valid & CTS_SPI_VALID_SYNC_RATE) {
3458 period = spi->sync_period;
3460 PTR_CONFIG_PAGE_SCSI_DEVICE_1 ptr =
3461 &mpt->mpt_dev_page1[tgt];
3462 period = ptr->RequestedParameters;
3463 period &= MPI_SCSIDEVPAGE1_RP_MIN_SYNC_PERIOD_MASK;
3464 period >>= MPI_SCSIDEVPAGE1_RP_SHIFT_MIN_SYNC_PERIOD;
3467 CAMLOCK_2_MPTLOCK(mpt);
3468 if (dval & DP_DISC_ENABLE) {
3469 mpt->mpt_disc_enable |= (1 << tgt);
3470 } else if (dval & DP_DISC_DISABL) {
3471 mpt->mpt_disc_enable &= ~(1 << tgt);
3473 if (dval & DP_TQING_ENABLE) {
3474 mpt->mpt_tag_enable |= (1 << tgt);
3475 } else if (dval & DP_TQING_DISABL) {
3476 mpt->mpt_tag_enable &= ~(1 << tgt);
3478 if (dval & DP_WIDTH) {
3479 mpt_setwidth(mpt, tgt, 1);
3481 if (dval & DP_SYNC) {
3482 mpt_setsync(mpt, tgt, period, offset);
3485 MPTLOCK_2_CAMLOCK(mpt);
3486 mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3489 mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
3490 "set [%d]: 0x%x period 0x%x offset %d\n",
3491 tgt, dval, period, offset);
3492 if (mpt_update_spi_config(mpt, tgt)) {
3493 mpt_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
3495 mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3497 MPTLOCK_2_CAMLOCK(mpt);
3500 case XPT_GET_TRAN_SETTINGS:
3502 #ifdef CAM_NEW_TRAN_CODE
3503 struct ccb_trans_settings_scsi *scsi;
3505 cts->protocol = PROTO_SCSI;
3507 struct ccb_trans_settings_fc *fc =
3508 &cts->xport_specific.fc;
3509 cts->protocol_version = SCSI_REV_SPC;
3510 cts->transport = XPORT_FC;
3511 cts->transport_version = 0;
3512 fc->valid = CTS_FC_VALID_SPEED;
3513 fc->bitrate = 100000;
3514 } else if (mpt->is_sas) {
3515 struct ccb_trans_settings_sas *sas =
3516 &cts->xport_specific.sas;
3517 cts->protocol_version = SCSI_REV_SPC2;
3518 cts->transport = XPORT_SAS;
3519 cts->transport_version = 0;
3520 sas->valid = CTS_SAS_VALID_SPEED;
3521 sas->bitrate = 300000;
3523 cts->protocol_version = SCSI_REV_2;
3524 cts->transport = XPORT_SPI;
3525 cts->transport_version = 2;
3526 if (mpt_get_spi_settings(mpt, cts) != 0) {
3527 mpt_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
3531 scsi = &cts->proto_specific.scsi;
3532 scsi->valid = CTS_SCSI_VALID_TQ;
3533 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3537 cts->flags = CCB_TRANS_TAG_ENB | CCB_TRANS_DISC_ENB;
3538 cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
3539 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3540 } else if (mpt->is_sas) {
3541 cts->flags = CCB_TRANS_TAG_ENB | CCB_TRANS_DISC_ENB;
3542 cts->valid = CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
3543 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3544 } else if (mpt_get_spi_settings(mpt, cts) != 0) {
3545 mpt_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
3549 mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3552 case XPT_CALC_GEOMETRY:
3554 struct ccb_calc_geometry *ccg;
3557 if (ccg->block_size == 0) {
3558 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
3559 mpt_set_ccb_status(ccb, CAM_REQ_INVALID);
3562 mpt_calc_geometry(ccg, /*extended*/1);
3563 KASSERT(ccb->ccb_h.status, ("zero ccb sts at %d\n", __LINE__));
3566 case XPT_PATH_INQ: /* Path routing inquiry */
3568 struct ccb_pathinq *cpi = &ccb->cpi;
3570 cpi->version_num = 1;
3571 cpi->target_sprt = 0;
3572 cpi->hba_eng_cnt = 0;
3573 cpi->max_target = mpt->port_facts[0].MaxDevices - 1;
3575 * FC cards report MAX_DEVICES of 512, but
3576 * the MSG_SCSI_IO_REQUEST target id field
3577 * is only 8 bits. Until we fix the driver
3578 * to support 'channels' for bus overflow,
3581 if (cpi->max_target > 255) {
3582 cpi->max_target = 255;
3586 * VMware ESX reports > 16 devices and then dies when we probe.
3588 if (mpt->is_spi && cpi->max_target > 15) {
3589 cpi->max_target = 15;
3594 cpi->max_lun = MPT_MAX_LUNS;
3595 cpi->initiator_id = mpt->mpt_ini_id;
3596 cpi->bus_id = cam_sim_bus(sim);
3599 * The base speed is the speed of the underlying connection.
3601 #ifdef CAM_NEW_TRAN_CODE
3602 cpi->protocol = PROTO_SCSI;
3604 cpi->hba_misc = PIM_NOBUSRESET;
3605 cpi->base_transfer_speed = 100000;
3606 cpi->hba_inquiry = PI_TAG_ABLE;
3607 cpi->transport = XPORT_FC;
3608 cpi->transport_version = 0;
3609 cpi->protocol_version = SCSI_REV_SPC;
3610 } else if (mpt->is_sas) {
3611 cpi->hba_misc = PIM_NOBUSRESET;
3612 cpi->base_transfer_speed = 300000;
3613 cpi->hba_inquiry = PI_TAG_ABLE;
3614 cpi->transport = XPORT_SAS;
3615 cpi->transport_version = 0;
3616 cpi->protocol_version = SCSI_REV_SPC2;
3618 cpi->hba_misc = PIM_SEQSCAN;
3619 cpi->base_transfer_speed = 3300;
3620 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
3621 cpi->transport = XPORT_SPI;
3622 cpi->transport_version = 2;
3623 cpi->protocol_version = SCSI_REV_2;
3627 cpi->hba_misc = PIM_NOBUSRESET;
3628 cpi->base_transfer_speed = 100000;
3629 cpi->hba_inquiry = PI_TAG_ABLE;
3630 } else if (mpt->is_sas) {
3631 cpi->hba_misc = PIM_NOBUSRESET;
3632 cpi->base_transfer_speed = 300000;
3633 cpi->hba_inquiry = PI_TAG_ABLE;
3635 cpi->hba_misc = PIM_SEQSCAN;
3636 cpi->base_transfer_speed = 3300;
3637 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
3642 * We give our fake RAID passhtru bus a width that is MaxVolumes
3643 * wide and restrict it to one lun.
3645 if (raid_passthru) {
3646 cpi->max_target = mpt->ioc_page2->MaxPhysDisks - 1;
3647 cpi->initiator_id = cpi->max_target + 1;
3651 if ((mpt->role & MPT_ROLE_INITIATOR) == 0) {
3652 cpi->hba_misc |= PIM_NOINITIATOR;
3654 if (mpt->is_fc && (mpt->role & MPT_ROLE_TARGET)) {
3656 PIT_PROCESSOR | PIT_DISCONNECT | PIT_TERM_IO;
3658 cpi->target_sprt = 0;
3660 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
3661 strncpy(cpi->hba_vid, "LSI", HBA_IDLEN);
3662 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3663 cpi->unit_number = cam_sim_unit(sim);
3664 cpi->ccb_h.status = CAM_REQ_CMP;
3667 case XPT_EN_LUN: /* Enable LUN as a target */
3671 CAMLOCK_2_MPTLOCK(mpt);
3672 if (ccb->cel.enable)
3673 result = mpt_enable_lun(mpt,
3674 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
3676 result = mpt_disable_lun(mpt,
3677 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
3678 MPTLOCK_2_CAMLOCK(mpt);
3680 mpt_set_ccb_status(ccb, CAM_REQ_CMP);
3682 mpt_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
3686 case XPT_NOTIFY_ACK: /* recycle notify ack */
3687 case XPT_IMMED_NOTIFY: /* Add Immediate Notify Resource */
3688 case XPT_ACCEPT_TARGET_IO: /* Add Accept Target IO Resource */
3690 tgt_resource_t *trtp;
3691 lun_id_t lun = ccb->ccb_h.target_lun;
3692 ccb->ccb_h.sim_priv.entries[0].field = 0;
3693 ccb->ccb_h.sim_priv.entries[1].ptr = mpt;
3694 ccb->ccb_h.flags = 0;
3696 if (lun == CAM_LUN_WILDCARD) {
3697 if (ccb->ccb_h.target_id != CAM_TARGET_WILDCARD) {
3698 mpt_set_ccb_status(ccb, CAM_REQ_INVALID);
3701 trtp = &mpt->trt_wildcard;
3702 } else if (lun >= MPT_MAX_LUNS) {
3703 mpt_set_ccb_status(ccb, CAM_REQ_INVALID);
3706 trtp = &mpt->trt[lun];
3708 CAMLOCK_2_MPTLOCK(mpt);
3709 if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
3710 mpt_lprt(mpt, MPT_PRT_DEBUG1,
3711 "Put FREE ATIO %p lun %d\n", ccb, lun);
3712 STAILQ_INSERT_TAIL(&trtp->atios, &ccb->ccb_h,
3714 } else if (ccb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
3715 mpt_lprt(mpt, MPT_PRT_DEBUG1,
3716 "Put FREE INOT lun %d\n", lun);
3717 STAILQ_INSERT_TAIL(&trtp->inots, &ccb->ccb_h,
3720 mpt_lprt(mpt, MPT_PRT_ALWAYS, "Got Notify ACK\n");
3722 mpt_set_ccb_status(ccb, CAM_REQ_INPROG);
3723 MPTLOCK_2_CAMLOCK(mpt);
3726 case XPT_CONT_TARGET_IO:
3727 CAMLOCK_2_MPTLOCK(mpt);
3728 mpt_target_start_io(mpt, ccb);
3729 MPTLOCK_2_CAMLOCK(mpt);
3733 ccb->ccb_h.status = CAM_REQ_INVALID;
3740 mpt_get_spi_settings(struct mpt_softc *mpt, struct ccb_trans_settings *cts)
3742 #ifdef CAM_NEW_TRAN_CODE
3743 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3744 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
3747 uint32_t dval, pval, oval;
3750 if (IS_CURRENT_SETTINGS(cts) == 0) {
3751 tgt = cts->ccb_h.target_id;
3752 } else if (xpt_path_sim(cts->ccb_h.path) == mpt->phydisk_sim) {
3753 if (mpt_map_physdisk(mpt, (union ccb *)cts, &tgt)) {
3757 tgt = cts->ccb_h.target_id;
3761 * We aren't looking at Port Page 2 BIOS settings here-
3762 * sometimes these have been known to be bogus XXX.
3764 * For user settings, we pick the max from port page 0
3766 * For current settings we read the current settings out from
3767 * device page 0 for that target.
3769 if (IS_CURRENT_SETTINGS(cts)) {
3770 CONFIG_PAGE_SCSI_DEVICE_0 tmp;
3773 CAMLOCK_2_MPTLOCK(mpt);
3774 tmp = mpt->mpt_dev_page0[tgt];
3775 rv = mpt_read_cur_cfg_page(mpt, tgt, &tmp.Header,
3776 sizeof(tmp), FALSE, 5000);
3778 MPTLOCK_2_CAMLOCK(mpt);
3779 mpt_prt(mpt, "can't get tgt %d config page 0\n", tgt);
3782 mpt2host_config_page_scsi_device_0(&tmp);
3784 MPTLOCK_2_CAMLOCK(mpt);
3785 mpt_lprt(mpt, MPT_PRT_DEBUG,
3786 "mpt_get_spi_settings[%d]: current NP %x Info %x\n", tgt,
3787 tmp.NegotiatedParameters, tmp.Information);
3788 dval |= (tmp.NegotiatedParameters & MPI_SCSIDEVPAGE0_NP_WIDE) ?
3789 DP_WIDE : DP_NARROW;
3790 dval |= (mpt->mpt_disc_enable & (1 << tgt)) ?
3791 DP_DISC_ENABLE : DP_DISC_DISABL;
3792 dval |= (mpt->mpt_tag_enable & (1 << tgt)) ?
3793 DP_TQING_ENABLE : DP_TQING_DISABL;
3794 oval = tmp.NegotiatedParameters;
3795 oval &= MPI_SCSIDEVPAGE0_NP_NEG_SYNC_OFFSET_MASK;
3796 oval >>= MPI_SCSIDEVPAGE0_NP_SHIFT_SYNC_OFFSET;
3797 pval = tmp.NegotiatedParameters;
3798 pval &= MPI_SCSIDEVPAGE0_NP_NEG_SYNC_PERIOD_MASK;
3799 pval >>= MPI_SCSIDEVPAGE0_NP_SHIFT_SYNC_PERIOD;
3800 mpt->mpt_dev_page0[tgt] = tmp;
3802 dval = DP_WIDE|DP_DISC_ENABLE|DP_TQING_ENABLE|DP_SYNC;
3803 oval = mpt->mpt_port_page0.Capabilities;
3804 oval = MPI_SCSIPORTPAGE0_CAP_GET_MAX_SYNC_OFFSET(oval);
3805 pval = mpt->mpt_port_page0.Capabilities;
3806 pval = MPI_SCSIPORTPAGE0_CAP_GET_MIN_SYNC_PERIOD(pval);
3809 #ifndef CAM_NEW_TRAN_CODE
3810 cts->flags &= ~(CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB);
3812 cts->sync_period = pval;
3813 cts->sync_offset = oval;
3814 cts->valid |= CCB_TRANS_SYNC_RATE_VALID;
3815 cts->valid |= CCB_TRANS_SYNC_OFFSET_VALID;
3816 cts->valid |= CCB_TRANS_BUS_WIDTH_VALID;
3817 if (dval & DP_WIDE) {
3818 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3820 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3822 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
3823 cts->valid |= CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID;
3824 if (dval & DP_DISC_ENABLE) {
3825 cts->flags |= CCB_TRANS_DISC_ENB;
3827 if (dval & DP_TQING_ENABLE) {
3828 cts->flags |= CCB_TRANS_TAG_ENB;
3836 spi->sync_offset = oval;
3837 spi->sync_period = pval;
3838 spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
3839 spi->valid |= CTS_SPI_VALID_SYNC_RATE;
3840 spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
3841 if (dval & DP_WIDE) {
3842 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
3844 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
3846 if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
3847 scsi->valid = CTS_SCSI_VALID_TQ;
3848 if (dval & DP_TQING_ENABLE) {
3849 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
3851 spi->valid |= CTS_SPI_VALID_DISC;
3852 if (dval & DP_DISC_ENABLE) {
3853 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
3857 mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
3858 "mpt_get_spi_settings[%d]: %s flags 0x%x per 0x%x off=%d\n", tgt,
3859 IS_CURRENT_SETTINGS(cts)? "ACTIVE" : "NVRAM ", dval, pval, oval);
3864 mpt_setwidth(struct mpt_softc *mpt, int tgt, int onoff)
3866 PTR_CONFIG_PAGE_SCSI_DEVICE_1 ptr;
3868 ptr = &mpt->mpt_dev_page1[tgt];
3870 ptr->RequestedParameters |= MPI_SCSIDEVPAGE1_RP_WIDE;
3872 ptr->RequestedParameters &= ~MPI_SCSIDEVPAGE1_RP_WIDE;
3877 mpt_setsync(struct mpt_softc *mpt, int tgt, int period, int offset)
3879 PTR_CONFIG_PAGE_SCSI_DEVICE_1 ptr;
3881 ptr = &mpt->mpt_dev_page1[tgt];
3882 ptr->RequestedParameters &= ~MPI_SCSIDEVPAGE1_RP_MIN_SYNC_PERIOD_MASK;
3883 ptr->RequestedParameters &= ~MPI_SCSIDEVPAGE1_RP_MAX_SYNC_OFFSET_MASK;
3884 ptr->RequestedParameters &= ~MPI_SCSIDEVPAGE1_RP_DT;
3885 ptr->RequestedParameters &= ~MPI_SCSIDEVPAGE1_RP_QAS;
3886 ptr->RequestedParameters &= ~MPI_SCSIDEVPAGE1_RP_IU;
3890 ptr->RequestedParameters |=
3891 period << MPI_SCSIDEVPAGE1_RP_SHIFT_MIN_SYNC_PERIOD;
3892 ptr->RequestedParameters |=
3893 offset << MPI_SCSIDEVPAGE1_RP_SHIFT_MAX_SYNC_OFFSET;
3895 ptr->RequestedParameters |= MPI_SCSIDEVPAGE1_RP_DT;
3898 ptr->RequestedParameters |= MPI_SCSIDEVPAGE1_RP_QAS;
3899 ptr->RequestedParameters |= MPI_SCSIDEVPAGE1_RP_IU;
3904 mpt_update_spi_config(struct mpt_softc *mpt, int tgt)
3906 CONFIG_PAGE_SCSI_DEVICE_1 tmp;
3909 mpt_lprt(mpt, MPT_PRT_NEGOTIATION,
3910 "mpt_update_spi_config[%d].page1: Requested Params 0x%08x\n",
3911 tgt, mpt->mpt_dev_page1[tgt].RequestedParameters);
3912 tmp = mpt->mpt_dev_page1[tgt];
3913 host2mpt_config_page_scsi_device_1(&tmp);
3914 rv = mpt_write_cur_cfg_page(mpt, tgt,
3915 &tmp.Header, sizeof(tmp), FALSE, 5000);
3917 mpt_prt(mpt, "mpt_update_spi_config: write cur page failed\n");
3924 mpt_calc_geometry(struct ccb_calc_geometry *ccg, int extended)
3926 #if __FreeBSD_version >= 500000
3927 cam_calc_geometry(ccg, extended);
3930 uint32_t secs_per_cylinder;
3932 if (ccg->block_size == 0) {
3933 ccg->ccb_h.status = CAM_REQ_INVALID;
3936 size_mb = ccg->volume_size / ((1024L * 1024L) / ccg->block_size);
3937 if (size_mb > 1024 && extended) {
3939 ccg->secs_per_track = 63;
3942 ccg->secs_per_track = 32;
3944 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
3945 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
3946 ccg->ccb_h.status = CAM_REQ_CMP;
3950 /****************************** Timeout Recovery ******************************/
3952 mpt_spawn_recovery_thread(struct mpt_softc *mpt)
3956 error = mpt_kthread_create(mpt_recovery_thread, mpt,
3957 &mpt->recovery_thread, /*flags*/0,
3958 /*altstack*/0, "mpt_recovery%d", mpt->unit);
3963 mpt_terminate_recovery_thread(struct mpt_softc *mpt)
3965 if (mpt->recovery_thread == NULL) {
3968 mpt->shutdwn_recovery = 1;
3971 * Sleep on a slightly different location
3972 * for this interlock just for added safety.
3974 mpt_sleep(mpt, &mpt->recovery_thread, PUSER, "thtrm", 0);
3978 mpt_recovery_thread(void *arg)
3980 struct mpt_softc *mpt;
3982 mpt = (struct mpt_softc *)arg;
3985 if (TAILQ_EMPTY(&mpt->request_timeout_list) != 0) {
3986 if (mpt->shutdwn_recovery == 0) {
3987 mpt_sleep(mpt, mpt, PUSER, "idle", 0);
3990 if (mpt->shutdwn_recovery != 0) {
3993 mpt_recover_commands(mpt);
3995 mpt->recovery_thread = NULL;
3996 wakeup(&mpt->recovery_thread);
3998 mpt_kthread_exit(0);
4002 mpt_scsi_send_tmf(struct mpt_softc *mpt, u_int type, u_int flags,
4003 u_int channel, u_int target, u_int lun, u_int abort_ctx, int sleep_ok)
4005 MSG_SCSI_TASK_MGMT *tmf_req;
4009 * Wait for any current TMF request to complete.
4010 * We're only allowed to issue one TMF at a time.
4012 error = mpt_wait_req(mpt, mpt->tmf_req, REQ_STATE_FREE, REQ_STATE_FREE,
4013 sleep_ok, MPT_TMF_MAX_TIMEOUT);
4015 mpt_reset(mpt, TRUE);
4019 mpt_assign_serno(mpt, mpt->tmf_req);
4020 mpt->tmf_req->state = REQ_STATE_ALLOCATED|REQ_STATE_QUEUED;
4022 tmf_req = (MSG_SCSI_TASK_MGMT *)mpt->tmf_req->req_vbuf;
4023 memset(tmf_req, 0, sizeof(*tmf_req));
4024 tmf_req->TargetID = target;
4025 tmf_req->Bus = channel;
4026 tmf_req->Function = MPI_FUNCTION_SCSI_TASK_MGMT;
4027 tmf_req->TaskType = type;
4028 tmf_req->MsgFlags = flags;
4029 tmf_req->MsgContext =
4030 htole32(mpt->tmf_req->index | scsi_tmf_handler_id);
4031 if (lun > MPT_MAX_LUNS) {
4032 tmf_req->LUN[0] = 0x40 | ((lun >> 8) & 0x3f);
4033 tmf_req->LUN[1] = lun & 0xff;
4035 tmf_req->LUN[1] = lun;
4037 tmf_req->TaskMsgContext = abort_ctx;
4039 mpt_lprt(mpt, MPT_PRT_DEBUG,
4040 "Issuing TMF %p:%u with MsgContext of 0x%x\n", mpt->tmf_req,
4041 mpt->tmf_req->serno, tmf_req->MsgContext);
4042 if (mpt->verbose > MPT_PRT_DEBUG) {
4043 mpt_print_request(tmf_req);
4046 KASSERT(mpt_req_on_pending_list(mpt, mpt->tmf_req) == 0,
4047 ("mpt_scsi_send_tmf: tmf_req already on pending list"));
4048 TAILQ_INSERT_HEAD(&mpt->request_pending_list, mpt->tmf_req, links);
4049 error = mpt_send_handshake_cmd(mpt, sizeof(*tmf_req), tmf_req);
4050 if (error != MPT_OK) {
4051 TAILQ_REMOVE(&mpt->request_pending_list, mpt->tmf_req, links);
4052 mpt->tmf_req->state = REQ_STATE_FREE;
4053 mpt_reset(mpt, TRUE);
4059 * When a command times out, it is placed on the requeust_timeout_list
4060 * and we wake our recovery thread. The MPT-Fusion architecture supports
4061 * only a single TMF operation at a time, so we serially abort/bdr, etc,
4062 * the timedout transactions. The next TMF is issued either by the
4063 * completion handler of the current TMF waking our recovery thread,
4064 * or the TMF timeout handler causing a hard reset sequence.
4067 mpt_recover_commands(struct mpt_softc *mpt)
4073 if (TAILQ_EMPTY(&mpt->request_timeout_list) != 0) {
4075 * No work to do- leave.
4077 mpt_prt(mpt, "mpt_recover_commands: no requests.\n");
4082 * Flush any commands whose completion coincides with their timeout.
4086 if (TAILQ_EMPTY(&mpt->request_timeout_list) != 0) {
4088 * The timedout commands have already
4089 * completed. This typically means
4090 * that either the timeout value was on
4091 * the hairy edge of what the device
4092 * requires or - more likely - interrupts
4093 * are not happening.
4095 mpt_prt(mpt, "Timedout requests already complete. "
4096 "Interrupts may not be functioning.\n");
4097 mpt_enable_ints(mpt);
4102 * We have no visibility into the current state of the
4103 * controller, so attempt to abort the commands in the
4104 * order they timed-out. For initiator commands, we
4105 * depend on the reply handler pulling requests off
4108 while ((req = TAILQ_FIRST(&mpt->request_timeout_list)) != NULL) {
4111 MSG_REQUEST_HEADER *hdrp = req->req_vbuf;
4113 mpt_prt(mpt, "attempting to abort req %p:%u function %x\n",
4114 req, req->serno, hdrp->Function);
4117 mpt_prt(mpt, "null ccb in timed out request. "
4118 "Resetting Controller.\n");
4119 mpt_reset(mpt, TRUE);
4122 mpt_set_ccb_status(ccb, CAM_CMD_TIMEOUT);
4125 * Check to see if this is not an initiator command and
4126 * deal with it differently if it is.
4128 switch (hdrp->Function) {
4129 case MPI_FUNCTION_SCSI_IO_REQUEST:
4130 case MPI_FUNCTION_RAID_SCSI_IO_PASSTHROUGH:
4134 * XXX: FIX ME: need to abort target assists...
4136 mpt_prt(mpt, "just putting it back on the pend q\n");
4137 TAILQ_REMOVE(&mpt->request_timeout_list, req, links);
4138 TAILQ_INSERT_HEAD(&mpt->request_pending_list, req,
4143 error = mpt_scsi_send_tmf(mpt,
4144 MPI_SCSITASKMGMT_TASKTYPE_ABORT_TASK,
4145 0, 0, ccb->ccb_h.target_id, ccb->ccb_h.target_lun,
4146 htole32(req->index | scsi_io_handler_id), TRUE);
4150 * mpt_scsi_send_tmf hard resets on failure, so no
4151 * need to do so here. Our queue should be emptied
4152 * by the hard reset.
4157 error = mpt_wait_req(mpt, mpt->tmf_req, REQ_STATE_DONE,
4158 REQ_STATE_DONE, TRUE, 500);
4160 status = le16toh(mpt->tmf_req->IOCStatus);
4161 response = mpt->tmf_req->ResponseCode;
4162 mpt->tmf_req->state = REQ_STATE_FREE;
4166 * If we've errored out,, reset the controller.
4168 mpt_prt(mpt, "mpt_recover_commands: abort timed-out. "
4169 "Resetting controller\n");
4170 mpt_reset(mpt, TRUE);
4174 if ((status & MPI_IOCSTATUS_MASK) != MPI_IOCSTATUS_SUCCESS) {
4175 mpt_prt(mpt, "mpt_recover_commands: IOC Status 0x%x. "
4176 "Resetting controller.\n", status);
4177 mpt_reset(mpt, TRUE);
4181 if (response != MPI_SCSITASKMGMT_RSP_TM_SUCCEEDED &&
4182 response != MPI_SCSITASKMGMT_RSP_TM_COMPLETE) {
4183 mpt_prt(mpt, "mpt_recover_commands: TMF Response 0x%x. "
4184 "Resetting controller.\n", response);
4185 mpt_reset(mpt, TRUE);
4188 mpt_prt(mpt, "abort of req %p:%u completed\n", req, req->serno);
4192 /************************ Target Mode Support ****************************/
4194 mpt_fc_post_els(struct mpt_softc *mpt, request_t *req, int ioindex)
4196 MSG_LINK_SERVICE_BUFFER_POST_REQUEST *fc;
4197 PTR_SGE_TRANSACTION32 tep;
4198 PTR_SGE_SIMPLE32 se;
4202 paddr = req->req_pbuf;
4203 paddr += MPT_RQSL(mpt);
4206 memset(fc, 0, MPT_REQUEST_AREA);
4207 fc->BufferCount = 1;
4208 fc->Function = MPI_FUNCTION_FC_LINK_SRVC_BUF_POST;
4209 fc->MsgContext = htole32(req->index | fc_els_handler_id);
4212 * Okay, set up ELS buffer pointers. ELS buffer pointers
4213 * consist of a TE SGL element (with details length of zero)
4214 * followe by a SIMPLE SGL element which holds the address
4218 tep = (PTR_SGE_TRANSACTION32) &fc->SGL;
4220 tep->ContextSize = 4;
4222 tep->TransactionContext[0] = htole32(ioindex);
4224 se = (PTR_SGE_SIMPLE32) &tep->TransactionDetails[0];
4226 MPI_SGE_FLAGS_HOST_TO_IOC |
4227 MPI_SGE_FLAGS_SIMPLE_ELEMENT |
4228 MPI_SGE_FLAGS_LAST_ELEMENT |
4229 MPI_SGE_FLAGS_END_OF_LIST |
4230 MPI_SGE_FLAGS_END_OF_BUFFER;
4231 fl <<= MPI_SGE_FLAGS_SHIFT;
4232 fl |= (MPT_NRFM(mpt) - MPT_RQSL(mpt));
4233 se->FlagsLength = htole32(fl);
4234 se->Address = htole32((uint32_t) paddr);
4235 mpt_lprt(mpt, MPT_PRT_DEBUG,
4236 "add ELS index %d ioindex %d for %p:%u\n",
4237 req->index, ioindex, req, req->serno);
4238 KASSERT(((req->state & REQ_STATE_LOCKED) != 0),
4239 ("mpt_fc_post_els: request not locked"));
4240 mpt_send_cmd(mpt, req);
4244 mpt_post_target_command(struct mpt_softc *mpt, request_t *req, int ioindex)
4246 PTR_MSG_TARGET_CMD_BUFFER_POST_REQUEST fc;
4247 PTR_CMD_BUFFER_DESCRIPTOR cb;
4250 paddr = req->req_pbuf;
4251 paddr += MPT_RQSL(mpt);
4252 memset(req->req_vbuf, 0, MPT_REQUEST_AREA);
4253 MPT_TGT_STATE(mpt, req)->state = TGT_STATE_LOADING;
4256 fc->BufferCount = 1;
4257 fc->Function = MPI_FUNCTION_TARGET_CMD_BUFFER_POST;
4258 fc->MsgContext = htole32(req->index | mpt->scsi_tgt_handler_id);
4260 cb = &fc->Buffer[0];
4261 cb->IoIndex = htole16(ioindex);
4262 cb->u.PhysicalAddress32 = htole32((U32) paddr);
4264 mpt_check_doorbell(mpt);
4265 mpt_send_cmd(mpt, req);
4269 mpt_add_els_buffers(struct mpt_softc *mpt)
4273 if (mpt->is_fc == 0) {
4277 if (mpt->els_cmds_allocated) {
4281 mpt->els_cmd_ptrs = malloc(MPT_MAX_ELS * sizeof (request_t *),
4282 M_DEVBUF, M_NOWAIT | M_ZERO);
4284 if (mpt->els_cmd_ptrs == NULL) {
4289 * Feed the chip some ELS buffer resources
4291 for (i = 0; i < MPT_MAX_ELS; i++) {
4292 request_t *req = mpt_get_request(mpt, FALSE);
4296 req->state |= REQ_STATE_LOCKED;
4297 mpt->els_cmd_ptrs[i] = req;
4298 mpt_fc_post_els(mpt, req, i);
4302 mpt_prt(mpt, "unable to add ELS buffer resources\n");
4303 free(mpt->els_cmd_ptrs, M_DEVBUF);
4304 mpt->els_cmd_ptrs = NULL;
4307 if (i != MPT_MAX_ELS) {
4308 mpt_lprt(mpt, MPT_PRT_INFO,
4309 "only added %d of %d ELS buffers\n", i, MPT_MAX_ELS);
4311 mpt->els_cmds_allocated = i;
4316 mpt_add_target_commands(struct mpt_softc *mpt)
4320 if (mpt->tgt_cmd_ptrs) {
4324 max = MPT_MAX_REQUESTS(mpt) >> 1;
4325 if (max > mpt->mpt_max_tgtcmds) {
4326 max = mpt->mpt_max_tgtcmds;
4329 malloc(max * sizeof (request_t *), M_DEVBUF, M_NOWAIT | M_ZERO);
4330 if (mpt->tgt_cmd_ptrs == NULL) {
4332 "mpt_add_target_commands: could not allocate cmd ptrs\n");
4336 for (i = 0; i < max; i++) {
4339 req = mpt_get_request(mpt, FALSE);
4343 req->state |= REQ_STATE_LOCKED;
4344 mpt->tgt_cmd_ptrs[i] = req;
4345 mpt_post_target_command(mpt, req, i);
4350 mpt_lprt(mpt, MPT_PRT_ERROR, "could not add any target bufs\n");
4351 free(mpt->tgt_cmd_ptrs, M_DEVBUF);
4352 mpt->tgt_cmd_ptrs = NULL;
4356 mpt->tgt_cmds_allocated = i;
4359 mpt_lprt(mpt, MPT_PRT_INFO,
4360 "added %d of %d target bufs\n", i, max);
4366 mpt_enable_lun(struct mpt_softc *mpt, target_id_t tgt, lun_id_t lun)
4368 if (tgt == CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
4370 } else if (lun >= MPT_MAX_LUNS) {
4372 } else if (tgt != CAM_TARGET_WILDCARD && tgt != 0) {
4375 if (mpt->tenabled == 0) {
4377 (void) mpt_fc_reset_link(mpt, 0);
4381 if (lun == CAM_LUN_WILDCARD) {
4382 mpt->trt_wildcard.enabled = 1;
4384 mpt->trt[lun].enabled = 1;
4390 mpt_disable_lun(struct mpt_softc *mpt, target_id_t tgt, lun_id_t lun)
4393 if (tgt == CAM_TARGET_WILDCARD && lun == CAM_LUN_WILDCARD) {
4395 } else if (lun >= MPT_MAX_LUNS) {
4397 } else if (tgt != CAM_TARGET_WILDCARD && tgt != 0) {
4400 if (lun == CAM_LUN_WILDCARD) {
4401 mpt->trt_wildcard.enabled = 0;
4403 mpt->trt[lun].enabled = 0;
4405 for (i = 0; i < MPT_MAX_LUNS; i++) {
4406 if (mpt->trt[lun].enabled) {
4410 if (i == MPT_MAX_LUNS && mpt->twildcard == 0) {
4412 (void) mpt_fc_reset_link(mpt, 0);
4420 * Called with MPT lock held
4423 mpt_target_start_io(struct mpt_softc *mpt, union ccb *ccb)
4425 struct ccb_scsiio *csio = &ccb->csio;
4426 request_t *cmd_req = MPT_TAG_2_REQ(mpt, csio->tag_id);
4427 mpt_tgt_state_t *tgt = MPT_TGT_STATE(mpt, cmd_req);
4429 switch (tgt->state) {
4430 case TGT_STATE_IN_CAM:
4432 case TGT_STATE_MOVING_DATA:
4433 mpt_set_ccb_status(ccb, CAM_REQUEUE_REQ);
4434 xpt_freeze_simq(mpt->sim, 1);
4435 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
4436 tgt->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
4437 MPTLOCK_2_CAMLOCK(mpt);
4439 CAMLOCK_2_MPTLOCK(mpt);
4442 mpt_prt(mpt, "ccb %p flags 0x%x tag 0x%08x had bad request "
4443 "starting I/O\n", ccb, csio->ccb_h.flags, csio->tag_id);
4444 mpt_tgt_dump_req_state(mpt, cmd_req);
4445 mpt_set_ccb_status(ccb, CAM_REQ_CMP_ERR);
4446 MPTLOCK_2_CAMLOCK(mpt);
4448 CAMLOCK_2_MPTLOCK(mpt);
4452 if (csio->dxfer_len) {
4453 bus_dmamap_callback_t *cb;
4454 PTR_MSG_TARGET_ASSIST_REQUEST ta;
4457 KASSERT((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE,
4458 ("dxfer_len %u but direction is NONE\n", csio->dxfer_len));
4460 if ((req = mpt_get_request(mpt, FALSE)) == NULL) {
4461 if (mpt->outofbeer == 0) {
4463 xpt_freeze_simq(mpt->sim, 1);
4464 mpt_lprt(mpt, MPT_PRT_DEBUG, "FREEZEQ\n");
4466 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
4467 mpt_set_ccb_status(ccb, CAM_REQUEUE_REQ);
4468 MPTLOCK_2_CAMLOCK(mpt);
4470 CAMLOCK_2_MPTLOCK(mpt);
4473 ccb->ccb_h.status = CAM_SIM_QUEUED | CAM_REQ_INPROG;
4474 if (sizeof (bus_addr_t) > 4) {
4475 cb = mpt_execute_req_a64;
4477 cb = mpt_execute_req;
4481 ccb->ccb_h.ccb_req_ptr = req;
4484 * Record the currently active ccb and the
4485 * request for it in our target state area.
4490 memset(req->req_vbuf, 0, MPT_RQSL(mpt));
4494 PTR_MPI_TARGET_SSP_CMD_BUFFER ssp =
4496 ta->QueueTag = ssp->InitiatorTag;
4497 } else if (mpt->is_spi) {
4498 PTR_MPI_TARGET_SCSI_SPI_CMD_BUFFER sp =
4500 ta->QueueTag = sp->Tag;
4502 ta->Function = MPI_FUNCTION_TARGET_ASSIST;
4503 ta->MsgContext = htole32(req->index | mpt->scsi_tgt_handler_id);
4504 ta->ReplyWord = htole32(tgt->reply_desc);
4505 if (csio->ccb_h.target_lun > MPT_MAX_LUNS) {
4507 0x40 | ((csio->ccb_h.target_lun >> 8) & 0x3f);
4508 ta->LUN[1] = csio->ccb_h.target_lun & 0xff;
4510 ta->LUN[1] = csio->ccb_h.target_lun;
4513 ta->RelativeOffset = tgt->bytes_xfered;
4514 ta->DataLength = ccb->csio.dxfer_len;
4515 if (ta->DataLength > tgt->resid) {
4516 ta->DataLength = tgt->resid;
4520 * XXX Should be done after data transfer completes?
4522 tgt->resid -= csio->dxfer_len;
4523 tgt->bytes_xfered += csio->dxfer_len;
4525 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
4526 ta->TargetAssistFlags |=
4527 TARGET_ASSIST_FLAGS_DATA_DIRECTION;
4530 #ifdef WE_TRUST_AUTO_GOOD_STATUS
4531 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) &&
4532 csio->scsi_status == SCSI_STATUS_OK && tgt->resid == 0) {
4533 ta->TargetAssistFlags |=
4534 TARGET_ASSIST_FLAGS_AUTO_STATUS;
4537 tgt->state = TGT_STATE_SETTING_UP_FOR_DATA;
4539 mpt_lprt(mpt, MPT_PRT_DEBUG,
4540 "DATA_CCB %p tag %x %u bytes %u resid flg %x req %p:%u "
4541 "nxtstate=%d\n", csio, csio->tag_id, csio->dxfer_len,
4542 tgt->resid, ccb->ccb_h.flags, req, req->serno, tgt->state);
4544 MPTLOCK_2_CAMLOCK(mpt);
4545 if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
4546 if ((ccb->ccb_h.flags & CAM_DATA_PHYS) == 0) {
4548 int s = splsoftvm();
4549 error = bus_dmamap_load(mpt->buffer_dmat,
4550 req->dmap, csio->data_ptr, csio->dxfer_len,
4553 if (error == EINPROGRESS) {
4554 xpt_freeze_simq(mpt->sim, 1);
4555 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
4559 * We have been given a pointer to single
4562 struct bus_dma_segment seg;
4563 seg.ds_addr = (bus_addr_t)
4564 (vm_offset_t)csio->data_ptr;
4565 seg.ds_len = csio->dxfer_len;
4566 (*cb)(req, &seg, 1, 0);
4570 * We have been given a list of addresses.
4571 * This case could be easily supported but they are not
4572 * currently generated by the CAM subsystem so there
4573 * is no point in wasting the time right now.
4575 struct bus_dma_segment *sgs;
4576 if ((ccb->ccb_h.flags & CAM_SG_LIST_PHYS) == 0) {
4577 (*cb)(req, NULL, 0, EFAULT);
4579 /* Just use the segments provided */
4580 sgs = (struct bus_dma_segment *)csio->data_ptr;
4581 (*cb)(req, sgs, csio->sglist_cnt, 0);
4584 CAMLOCK_2_MPTLOCK(mpt);
4586 uint8_t *sp = NULL, sense[MPT_SENSE_SIZE];
4589 * XXX: I don't know why this seems to happen, but
4590 * XXX: completing the CCB seems to make things happy.
4591 * XXX: This seems to happen if the initiator requests
4592 * XXX: enough data that we have to do multiple CTIOs.
4594 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
4595 mpt_lprt(mpt, MPT_PRT_DEBUG,
4596 "Meaningless STATUS CCB (%p): flags %x status %x "
4597 "resid %d bytes_xfered %u\n", ccb, ccb->ccb_h.flags,
4598 ccb->ccb_h.status, tgt->resid, tgt->bytes_xfered);
4599 mpt_set_ccb_status(ccb, CAM_REQ_CMP);
4600 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
4601 MPTLOCK_2_CAMLOCK(mpt);
4603 CAMLOCK_2_MPTLOCK(mpt);
4606 if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
4608 memcpy(sp, &csio->sense_data,
4609 min(csio->sense_len, MPT_SENSE_SIZE));
4611 mpt_scsi_tgt_status(mpt, ccb, cmd_req, csio->scsi_status, sp);
4616 mpt_scsi_tgt_local(struct mpt_softc *mpt, request_t *cmd_req,
4617 uint32_t lun, int send, uint8_t *data, size_t length)
4619 mpt_tgt_state_t *tgt;
4620 PTR_MSG_TARGET_ASSIST_REQUEST ta;
4628 * We enter with resid set to the data load for the command.
4630 tgt = MPT_TGT_STATE(mpt, cmd_req);
4631 if (length == 0 || tgt->resid == 0) {
4633 mpt_scsi_tgt_status(mpt, NULL, cmd_req, 0, NULL);
4637 if ((req = mpt_get_request(mpt, FALSE)) == NULL) {
4638 mpt_prt(mpt, "out of resources- dropping local response\n");
4644 memset(req->req_vbuf, 0, MPT_RQSL(mpt));
4648 PTR_MPI_TARGET_SSP_CMD_BUFFER ssp = cmd_req->req_vbuf;
4649 ta->QueueTag = ssp->InitiatorTag;
4650 } else if (mpt->is_spi) {
4651 PTR_MPI_TARGET_SCSI_SPI_CMD_BUFFER sp = cmd_req->req_vbuf;
4652 ta->QueueTag = sp->Tag;
4654 ta->Function = MPI_FUNCTION_TARGET_ASSIST;
4655 ta->MsgContext = htole32(req->index | mpt->scsi_tgt_handler_id);
4656 ta->ReplyWord = htole32(tgt->reply_desc);
4657 if (lun > MPT_MAX_LUNS) {
4658 ta->LUN[0] = 0x40 | ((lun >> 8) & 0x3f);
4659 ta->LUN[1] = lun & 0xff;
4663 ta->RelativeOffset = 0;
4664 ta->DataLength = length;
4666 dptr = req->req_vbuf;
4667 dptr += MPT_RQSL(mpt);
4668 pptr = req->req_pbuf;
4669 pptr += MPT_RQSL(mpt);
4670 memcpy(dptr, data, min(length, MPT_RQSL(mpt)));
4672 se = (SGE_SIMPLE32 *) &ta->SGL[0];
4673 memset(se, 0,sizeof (*se));
4675 flags = MPI_SGE_FLAGS_SIMPLE_ELEMENT;
4677 ta->TargetAssistFlags |= TARGET_ASSIST_FLAGS_DATA_DIRECTION;
4678 flags |= MPI_SGE_FLAGS_HOST_TO_IOC;
4681 MPI_pSGE_SET_LENGTH(se, length);
4682 flags |= MPI_SGE_FLAGS_LAST_ELEMENT;
4683 flags |= MPI_SGE_FLAGS_END_OF_LIST | MPI_SGE_FLAGS_END_OF_BUFFER;
4684 MPI_pSGE_SET_FLAGS(se, flags);
4688 tgt->resid -= length;
4689 tgt->bytes_xfered = length;
4690 #ifdef WE_TRUST_AUTO_GOOD_STATUS
4691 tgt->state = TGT_STATE_MOVING_DATA_AND_STATUS;
4693 tgt->state = TGT_STATE_MOVING_DATA;
4695 mpt_send_cmd(mpt, req);
4699 * Abort queued up CCBs
4702 mpt_abort_target_ccb(struct mpt_softc *mpt, union ccb *ccb)
4704 struct mpt_hdr_stailq *lp;
4705 struct ccb_hdr *srch;
4707 union ccb *accb = ccb->cab.abort_ccb;
4708 tgt_resource_t *trtp;
4710 mpt_lprt(mpt, MPT_PRT_DEBUG, "aborting ccb %p\n", accb);
4712 if (ccb->ccb_h.target_lun == CAM_LUN_WILDCARD) {
4713 trtp = &mpt->trt_wildcard;
4715 trtp = &mpt->trt[ccb->ccb_h.target_lun];
4718 if (accb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
4720 } else if (accb->ccb_h.func_code == XPT_IMMED_NOTIFY) {
4723 return (CAM_REQ_INVALID);
4726 STAILQ_FOREACH(srch, lp, sim_links.stqe) {
4727 if (srch == &accb->ccb_h) {
4729 STAILQ_REMOVE(lp, srch, ccb_hdr, sim_links.stqe);
4734 accb->ccb_h.status = CAM_REQ_ABORTED;
4736 return (CAM_REQ_CMP);
4738 mpt_prt(mpt, "mpt_abort_tgt_ccb: CCB %p not found\n", ccb);
4739 return (CAM_PATH_INVALID);
4743 * Ask the MPT to abort the current target command
4746 mpt_abort_target_cmd(struct mpt_softc *mpt, request_t *cmd_req)
4750 PTR_MSG_TARGET_MODE_ABORT abtp;
4752 req = mpt_get_request(mpt, FALSE);
4756 abtp = req->req_vbuf;
4757 memset(abtp, 0, sizeof (*abtp));
4759 abtp->MsgContext = htole32(req->index | mpt->scsi_tgt_handler_id);
4760 abtp->AbortType = TARGET_MODE_ABORT_TYPE_EXACT_IO;
4761 abtp->Function = MPI_FUNCTION_TARGET_MODE_ABORT;
4762 abtp->ReplyWord = htole32(MPT_TGT_STATE(mpt, cmd_req)->reply_desc);
4764 if (mpt->is_fc || mpt->is_sas) {
4765 mpt_send_cmd(mpt, req);
4767 error = mpt_send_handshake_cmd(mpt, sizeof(*req), req);
4773 * WE_TRUST_AUTO_GOOD_STATUS- I've found that setting
4774 * TARGET_STATUS_SEND_FLAGS_AUTO_GOOD_STATUS leads the
4775 * FC929 to set bogus FC_RSP fields (nonzero residuals
4776 * but w/o RESID fields set). This causes QLogic initiators
4777 * to think maybe that a frame was lost.
4779 * WE_CAN_USE_AUTO_REPOST- we can't use AUTO_REPOST because
4780 * we use allocated requests to do TARGET_ASSIST and we
4781 * need to know when to release them.
4785 mpt_scsi_tgt_status(struct mpt_softc *mpt, union ccb *ccb, request_t *cmd_req,
4786 uint8_t status, uint8_t const *sense_data)
4789 mpt_tgt_state_t *tgt;
4790 PTR_MSG_TARGET_STATUS_SEND_REQUEST tp;
4796 cmd_vbuf = cmd_req->req_vbuf;
4797 cmd_vbuf += MPT_RQSL(mpt);
4798 tgt = MPT_TGT_STATE(mpt, cmd_req);
4800 if ((req = mpt_get_request(mpt, FALSE)) == NULL) {
4801 if (mpt->outofbeer == 0) {
4803 xpt_freeze_simq(mpt->sim, 1);
4804 mpt_lprt(mpt, MPT_PRT_DEBUG, "FREEZEQ\n");
4807 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
4808 mpt_set_ccb_status(ccb, CAM_REQUEUE_REQ);
4809 MPTLOCK_2_CAMLOCK(mpt);
4811 CAMLOCK_2_MPTLOCK(mpt);
4814 "could not allocate status request- dropping\n");
4820 ccb->ccb_h.ccb_mpt_ptr = mpt;
4821 ccb->ccb_h.ccb_req_ptr = req;
4825 * Record the currently active ccb, if any, and the
4826 * request for it in our target state area.
4830 tgt->state = TGT_STATE_SENDING_STATUS;
4833 paddr = req->req_pbuf;
4834 paddr += MPT_RQSL(mpt);
4836 memset(tp, 0, sizeof (*tp));
4837 tp->Function = MPI_FUNCTION_TARGET_STATUS_SEND;
4839 PTR_MPI_TARGET_FCP_CMD_BUFFER fc =
4840 (PTR_MPI_TARGET_FCP_CMD_BUFFER) cmd_vbuf;
4844 sts_vbuf = req->req_vbuf;
4845 sts_vbuf += MPT_RQSL(mpt);
4846 rsp = (uint32_t *) sts_vbuf;
4847 memcpy(tp->LUN, fc->FcpLun, sizeof (tp->LUN));
4850 * The MPI_TARGET_FCP_RSP_BUFFER define is unfortunate.
4851 * It has to be big-endian in memory and is organized
4852 * in 32 bit words, which are much easier to deal with
4853 * as words which are swizzled as needed.
4855 * All we're filling here is the FC_RSP payload.
4856 * We may just have the chip synthesize it if
4857 * we have no residual and an OK status.
4860 memset(rsp, 0, sizeof (MPI_TARGET_FCP_RSP_BUFFER));
4864 rsp[2] |= 0x800; /* XXXX NEED MNEMONIC!!!! */
4865 rsp[3] = htobe32(tgt->resid);
4866 #ifdef WE_TRUST_AUTO_GOOD_STATUS
4867 resplen = sizeof (MPI_TARGET_FCP_RSP_BUFFER);
4870 if (status == SCSI_STATUS_CHECK_COND) {
4873 rsp[2] |= 0x200; /* XXXX NEED MNEMONIC!!!! */
4874 rsp[4] = htobe32(MPT_SENSE_SIZE);
4876 memcpy(&rsp[8], sense_data, MPT_SENSE_SIZE);
4878 mpt_prt(mpt, "mpt_scsi_tgt_status: CHECK CONDI"
4879 "TION but no sense data?\n");
4880 memset(&rsp, 0, MPT_SENSE_SIZE);
4882 for (i = 8; i < (8 + (MPT_SENSE_SIZE >> 2)); i++) {
4883 rsp[i] = htobe32(rsp[i]);
4885 #ifdef WE_TRUST_AUTO_GOOD_STATUS
4886 resplen = sizeof (MPI_TARGET_FCP_RSP_BUFFER);
4889 #ifndef WE_TRUST_AUTO_GOOD_STATUS
4890 resplen = sizeof (MPI_TARGET_FCP_RSP_BUFFER);
4892 rsp[2] = htobe32(rsp[2]);
4893 } else if (mpt->is_sas) {
4894 PTR_MPI_TARGET_SSP_CMD_BUFFER ssp =
4895 (PTR_MPI_TARGET_SSP_CMD_BUFFER) cmd_vbuf;
4896 memcpy(tp->LUN, ssp->LogicalUnitNumber, sizeof (tp->LUN));
4898 PTR_MPI_TARGET_SCSI_SPI_CMD_BUFFER sp =
4899 (PTR_MPI_TARGET_SCSI_SPI_CMD_BUFFER) cmd_vbuf;
4900 tp->StatusCode = status;
4901 tp->QueueTag = htole16(sp->Tag);
4902 memcpy(tp->LUN, sp->LogicalUnitNumber, sizeof (tp->LUN));
4905 tp->ReplyWord = htole32(tgt->reply_desc);
4906 tp->MsgContext = htole32(req->index | mpt->scsi_tgt_handler_id);
4908 #ifdef WE_CAN_USE_AUTO_REPOST
4909 tp->MsgFlags = TARGET_STATUS_SEND_FLAGS_REPOST_CMD_BUFFER;
4911 if (status == SCSI_STATUS_OK && resplen == 0) {
4912 tp->MsgFlags |= TARGET_STATUS_SEND_FLAGS_AUTO_GOOD_STATUS;
4914 tp->StatusDataSGE.u.Address32 = htole32((uint32_t) paddr);
4916 MPI_SGE_FLAGS_HOST_TO_IOC |
4917 MPI_SGE_FLAGS_SIMPLE_ELEMENT |
4918 MPI_SGE_FLAGS_LAST_ELEMENT |
4919 MPI_SGE_FLAGS_END_OF_LIST |
4920 MPI_SGE_FLAGS_END_OF_BUFFER;
4921 fl <<= MPI_SGE_FLAGS_SHIFT;
4923 tp->StatusDataSGE.FlagsLength = htole32(fl);
4926 mpt_lprt(mpt, MPT_PRT_DEBUG,
4927 "STATUS_CCB %p (wit%s sense) tag %x req %p:%u resid %u\n",
4928 ccb, sense_data?"h" : "hout", ccb? ccb->csio.tag_id : -1, req,
4929 req->serno, tgt->resid);
4931 ccb->ccb_h.status = CAM_SIM_QUEUED | CAM_REQ_INPROG;
4932 mpt_req_timeout(req, 60 * hz, mpt_timeout, ccb);
4934 mpt_send_cmd(mpt, req);
4938 mpt_scsi_tgt_tsk_mgmt(struct mpt_softc *mpt, request_t *req, mpt_task_mgmt_t fc,
4939 tgt_resource_t *trtp, int init_id)
4941 struct ccb_immed_notify *inot;
4942 mpt_tgt_state_t *tgt;
4944 tgt = MPT_TGT_STATE(mpt, req);
4945 inot = (struct ccb_immed_notify *) STAILQ_FIRST(&trtp->inots);
4947 mpt_lprt(mpt, MPT_PRT_WARN, "no INOTSs- sending back BSY\n");
4948 mpt_scsi_tgt_status(mpt, NULL, req, SCSI_STATUS_BUSY, NULL);
4951 STAILQ_REMOVE_HEAD(&trtp->inots, sim_links.stqe);
4952 mpt_lprt(mpt, MPT_PRT_DEBUG1,
4953 "Get FREE INOT %p lun %d\n", inot, inot->ccb_h.target_lun);
4955 memset(&inot->sense_data, 0, sizeof (inot->sense_data));
4956 inot->sense_len = 0;
4957 memset(inot->message_args, 0, sizeof (inot->message_args));
4958 inot->initiator_id = init_id; /* XXX */
4961 * This is a somewhat grotesque attempt to map from task management
4962 * to old style SCSI messages. God help us all.
4965 case MPT_ABORT_TASK_SET:
4966 inot->message_args[0] = MSG_ABORT_TAG;
4968 case MPT_CLEAR_TASK_SET:
4969 inot->message_args[0] = MSG_CLEAR_TASK_SET;
4971 case MPT_TARGET_RESET:
4972 inot->message_args[0] = MSG_TARGET_RESET;
4975 inot->message_args[0] = MSG_CLEAR_ACA;
4977 case MPT_TERMINATE_TASK:
4978 inot->message_args[0] = MSG_ABORT_TAG;
4981 inot->message_args[0] = MSG_NOOP;
4984 tgt->ccb = (union ccb *) inot;
4985 inot->ccb_h.status = CAM_MESSAGE_RECV|CAM_DEV_QFRZN;
4986 MPTLOCK_2_CAMLOCK(mpt);
4987 xpt_done((union ccb *)inot);
4988 CAMLOCK_2_MPTLOCK(mpt);
4992 mpt_scsi_tgt_atio(struct mpt_softc *mpt, request_t *req, uint32_t reply_desc)
4994 static uint8_t null_iqd[SHORT_INQUIRY_LENGTH] = {
4995 0x7f, 0x00, 0x02, 0x02, 0x20, 0x00, 0x00, 0x32,
4996 'F', 'R', 'E', 'E', 'B', 'S', 'D', ' ',
4997 'L', 'S', 'I', '-', 'L', 'O', 'G', 'I',
4998 'C', ' ', 'N', 'U', 'L', 'D', 'E', 'V',
5001 struct ccb_accept_tio *atiop;
5004 mpt_tgt_state_t *tgt;
5005 tgt_resource_t *trtp = NULL;
5010 mpt_task_mgmt_t fct = MPT_NIL_TMT_VALUE;
5014 * First, DMA sync the received command-
5015 * which is in the *request* * phys area.
5017 * XXX: We could optimize this for a range
5019 bus_dmamap_sync(mpt->request_dmat, mpt->request_dmap,
5020 BUS_DMASYNC_POSTREAD);
5023 * Stash info for the current command where we can get at it later.
5025 vbuf = req->req_vbuf;
5026 vbuf += MPT_RQSL(mpt);
5029 * Get our state pointer set up.
5031 tgt = MPT_TGT_STATE(mpt, req);
5032 if (tgt->state != TGT_STATE_LOADED) {
5033 mpt_tgt_dump_req_state(mpt, req);
5034 panic("bad target state in mpt_scsi_tgt_atio");
5036 memset(tgt, 0, sizeof (mpt_tgt_state_t));
5037 tgt->state = TGT_STATE_IN_CAM;
5038 tgt->reply_desc = reply_desc;
5039 ioindex = GET_IO_INDEX(reply_desc);
5040 if (mpt->verbose >= MPT_PRT_DEBUG) {
5041 mpt_dump_data(mpt, "mpt_scsi_tgt_atio response", vbuf,
5042 max(sizeof (MPI_TARGET_FCP_CMD_BUFFER),
5043 max(sizeof (MPI_TARGET_SSP_CMD_BUFFER),
5044 sizeof (MPI_TARGET_SCSI_SPI_CMD_BUFFER))));
5047 PTR_MPI_TARGET_FCP_CMD_BUFFER fc;
5048 fc = (PTR_MPI_TARGET_FCP_CMD_BUFFER) vbuf;
5049 if (fc->FcpCntl[2]) {
5051 * Task Management Request
5053 switch (fc->FcpCntl[2]) {
5055 fct = MPT_ABORT_TASK_SET;
5058 fct = MPT_CLEAR_TASK_SET;
5061 fct = MPT_TARGET_RESET;
5064 fct = MPT_CLEAR_ACA;
5067 fct = MPT_TERMINATE_TASK;
5070 mpt_prt(mpt, "CORRUPTED TASK MGMT BITS: 0x%x\n",
5072 mpt_scsi_tgt_status(mpt, 0, req,
5077 switch (fc->FcpCntl[1]) {
5079 tag_action = MSG_SIMPLE_Q_TAG;
5082 tag_action = MSG_HEAD_OF_Q_TAG;
5085 tag_action = MSG_ORDERED_Q_TAG;
5089 * Bah. Ignore Untagged Queing and ACA
5091 tag_action = MSG_SIMPLE_Q_TAG;
5095 tgt->resid = be32toh(fc->FcpDl);
5097 lunptr = fc->FcpLun;
5098 itag = be16toh(fc->OptionalOxid);
5099 } else if (mpt->is_sas) {
5100 PTR_MPI_TARGET_SSP_CMD_BUFFER ssp;
5101 ssp = (PTR_MPI_TARGET_SSP_CMD_BUFFER) vbuf;
5103 lunptr = ssp->LogicalUnitNumber;
5104 itag = ssp->InitiatorTag;
5106 PTR_MPI_TARGET_SCSI_SPI_CMD_BUFFER sp;
5107 sp = (PTR_MPI_TARGET_SCSI_SPI_CMD_BUFFER) vbuf;
5109 lunptr = sp->LogicalUnitNumber;
5114 * Generate a simple lun
5116 switch (lunptr[0] & 0xc0) {
5118 lun = ((lunptr[0] & 0x3f) << 8) | lunptr[1];
5124 mpt_lprt(mpt, MPT_PRT_ERROR, "cannot handle this type lun\n");
5130 * Deal with non-enabled or bad luns here.
5132 if (lun >= MPT_MAX_LUNS || mpt->tenabled == 0 ||
5133 mpt->trt[lun].enabled == 0) {
5134 if (mpt->twildcard) {
5135 trtp = &mpt->trt_wildcard;
5136 } else if (fct == MPT_NIL_TMT_VALUE) {
5138 * In this case, we haven't got an upstream listener
5139 * for either a specific lun or wildcard luns. We
5140 * have to make some sensible response. For regular
5141 * inquiry, just return some NOT HERE inquiry data.
5142 * For VPD inquiry, report illegal field in cdb.
5143 * For REQUEST SENSE, just return NO SENSE data.
5144 * REPORT LUNS gets illegal command.
5145 * All other commands get 'no such device'.
5147 uint8_t *sp, cond, buf[MPT_SENSE_SIZE];
5150 memset(buf, 0, MPT_SENSE_SIZE);
5151 cond = SCSI_STATUS_CHECK_COND;
5156 tgt->tag_id = MPT_MAKE_TAGID(mpt, req, ioindex);
5166 len = min(tgt->resid, cdbp[4]);
5167 len = min(len, sizeof (null_iqd));
5168 mpt_lprt(mpt, MPT_PRT_DEBUG,
5169 "local inquiry %ld bytes\n", (long) len);
5170 mpt_scsi_tgt_local(mpt, req, lun, 1,
5177 len = min(tgt->resid, cdbp[4]);
5178 len = min(len, sizeof (buf));
5179 mpt_lprt(mpt, MPT_PRT_DEBUG,
5180 "local reqsense %ld bytes\n", (long) len);
5181 mpt_scsi_tgt_local(mpt, req, lun, 1,
5186 mpt_lprt(mpt, MPT_PRT_DEBUG, "REPORT LUNS\n");
5190 mpt_lprt(mpt, MPT_PRT_DEBUG,
5191 "CMD 0x%x to unmanaged lun %u\n",
5196 mpt_scsi_tgt_status(mpt, NULL, req, cond, sp);
5199 /* otherwise, leave trtp NULL */
5201 trtp = &mpt->trt[lun];
5205 * Deal with any task management
5207 if (fct != MPT_NIL_TMT_VALUE) {
5209 mpt_prt(mpt, "task mgmt function %x but no listener\n",
5211 mpt_scsi_tgt_status(mpt, 0, req,
5214 mpt_scsi_tgt_tsk_mgmt(mpt, req, fct, trtp,
5215 GET_INITIATOR_INDEX(reply_desc));
5221 atiop = (struct ccb_accept_tio *) STAILQ_FIRST(&trtp->atios);
5222 if (atiop == NULL) {
5223 mpt_lprt(mpt, MPT_PRT_WARN,
5224 "no ATIOs for lun %u- sending back %s\n", lun,
5225 mpt->tenabled? "QUEUE FULL" : "BUSY");
5226 mpt_scsi_tgt_status(mpt, NULL, req,
5227 mpt->tenabled? SCSI_STATUS_QUEUE_FULL : SCSI_STATUS_BUSY,
5231 STAILQ_REMOVE_HEAD(&trtp->atios, sim_links.stqe);
5232 mpt_lprt(mpt, MPT_PRT_DEBUG1,
5233 "Get FREE ATIO %p lun %d\n", atiop, atiop->ccb_h.target_lun);
5234 atiop->ccb_h.ccb_mpt_ptr = mpt;
5235 atiop->ccb_h.status = CAM_CDB_RECVD;
5236 atiop->ccb_h.target_lun = lun;
5237 atiop->sense_len = 0;
5238 atiop->init_id = GET_INITIATOR_INDEX(reply_desc);
5239 atiop->cdb_len = mpt_cdblen(cdbp[0], 16);
5240 memcpy(atiop->cdb_io.cdb_bytes, cdbp, atiop->cdb_len);
5243 * The tag we construct here allows us to find the
5244 * original request that the command came in with.
5246 * This way we don't have to depend on anything but the
5247 * tag to find things when CCBs show back up from CAM.
5249 atiop->tag_id = MPT_MAKE_TAGID(mpt, req, ioindex);
5250 tgt->tag_id = atiop->tag_id;
5252 atiop->tag_action = tag_action;
5253 atiop->ccb_h.flags = CAM_TAG_ACTION_VALID;
5255 if (mpt->verbose >= MPT_PRT_DEBUG) {
5257 mpt_prt(mpt, "START_CCB %p for lun %u CDB=<", atiop,
5258 atiop->ccb_h.target_lun);
5259 for (i = 0; i < atiop->cdb_len; i++) {
5260 mpt_prtc(mpt, "%02x%c", cdbp[i] & 0xff,
5261 (i == (atiop->cdb_len - 1))? '>' : ' ');
5263 mpt_prtc(mpt, " itag %x tag %x rdesc %x dl=%u\n",
5264 itag, atiop->tag_id, tgt->reply_desc, tgt->resid);
5267 MPTLOCK_2_CAMLOCK(mpt);
5268 xpt_done((union ccb *)atiop);
5269 CAMLOCK_2_MPTLOCK(mpt);
5273 mpt_tgt_dump_tgt_state(struct mpt_softc *mpt, request_t *req)
5275 mpt_tgt_state_t *tgt = MPT_TGT_STATE(mpt, req);
5277 mpt_prt(mpt, "req %p:%u tgt:rdesc 0x%x resid %u xfrd %u ccb %p treq %p "
5278 "nx %d tag 0x%08x state=%d\n", req, req->serno, tgt->reply_desc,
5279 tgt->resid, tgt->bytes_xfered, tgt->ccb, tgt->req, tgt->nxfers,
5280 tgt->tag_id, tgt->state);
5284 mpt_tgt_dump_req_state(struct mpt_softc *mpt, request_t *req)
5286 mpt_prt(mpt, "req %p:%u index %u (%x) state %x\n", req, req->serno,
5287 req->index, req->index, req->state);
5288 mpt_tgt_dump_tgt_state(mpt, req);
5292 mpt_scsi_tgt_reply_handler(struct mpt_softc *mpt, request_t *req,
5293 uint32_t reply_desc, MSG_DEFAULT_REPLY *reply_frame)
5299 if (reply_frame == NULL) {
5301 * Figure out what the state of the command is.
5303 mpt_tgt_state_t *tgt = MPT_TGT_STATE(mpt, req);
5306 mpt_req_spcl(mpt, req, "turbo scsi_tgt_reply", __LINE__);
5308 mpt_req_not_spcl(mpt, tgt->req,
5309 "turbo scsi_tgt_reply associated req", __LINE__);
5312 switch(tgt->state) {
5313 case TGT_STATE_LOADED:
5315 * This is a new command starting.
5317 mpt_scsi_tgt_atio(mpt, req, reply_desc);
5319 case TGT_STATE_MOVING_DATA:
5321 uint8_t *sp = NULL, sense[MPT_SENSE_SIZE];
5324 if (tgt->req == NULL) {
5325 panic("mpt: turbo target reply with null "
5326 "associated request moving data");
5330 if (tgt->is_local == 0) {
5331 panic("mpt: turbo target reply with "
5332 "null associated ccb moving data");
5335 mpt_lprt(mpt, MPT_PRT_DEBUG,
5336 "TARGET_ASSIST local done\n");
5337 TAILQ_REMOVE(&mpt->request_pending_list,
5339 mpt_free_request(mpt, tgt->req);
5341 mpt_scsi_tgt_status(mpt, NULL, req,
5347 mpt_req_untimeout(req, mpt_timeout, ccb);
5348 mpt_lprt(mpt, MPT_PRT_DEBUG,
5349 "TARGET_ASSIST %p (req %p:%u) done tag 0x%x\n",
5350 ccb, tgt->req, tgt->req->serno, ccb->csio.tag_id);
5352 * Free the Target Assist Request
5354 KASSERT(tgt->req->ccb == ccb,
5355 ("tgt->req %p:%u tgt->req->ccb %p", tgt->req,
5356 tgt->req->serno, tgt->req->ccb));
5357 TAILQ_REMOVE(&mpt->request_pending_list,
5359 mpt_free_request(mpt, tgt->req);
5363 * Do we need to send status now? That is, are
5364 * we done with all our data transfers?
5366 if ((ccb->ccb_h.flags & CAM_SEND_STATUS) == 0) {
5367 mpt_set_ccb_status(ccb, CAM_REQ_CMP);
5368 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
5369 KASSERT(ccb->ccb_h.status,
5370 ("zero ccb sts at %d\n", __LINE__));
5371 tgt->state = TGT_STATE_IN_CAM;
5372 if (mpt->outofbeer) {
5373 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
5375 mpt_lprt(mpt, MPT_PRT_DEBUG, "THAWQ\n");
5377 MPTLOCK_2_CAMLOCK(mpt);
5379 CAMLOCK_2_MPTLOCK(mpt);
5383 * Otherwise, send status (and sense)
5385 if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
5387 memcpy(sp, &ccb->csio.sense_data,
5388 min(ccb->csio.sense_len, MPT_SENSE_SIZE));
5390 mpt_scsi_tgt_status(mpt, ccb, req,
5391 ccb->csio.scsi_status, sp);
5394 case TGT_STATE_SENDING_STATUS:
5395 case TGT_STATE_MOVING_DATA_AND_STATUS:
5400 if (tgt->req == NULL) {
5401 panic("mpt: turbo target reply with null "
5402 "associated request sending status");
5409 TGT_STATE_MOVING_DATA_AND_STATUS) {
5412 mpt_req_untimeout(req, mpt_timeout, ccb);
5413 if (ccb->ccb_h.flags & CAM_SEND_SENSE) {
5414 ccb->ccb_h.status |= CAM_SENT_SENSE;
5416 mpt_lprt(mpt, MPT_PRT_DEBUG,
5417 "TARGET_STATUS tag %x sts %x flgs %x req "
5418 "%p\n", ccb->csio.tag_id, ccb->ccb_h.status,
5419 ccb->ccb_h.flags, tgt->req);
5421 * Free the Target Send Status Request
5423 KASSERT(tgt->req->ccb == ccb,
5424 ("tgt->req %p:%u tgt->req->ccb %p",
5425 tgt->req, tgt->req->serno, tgt->req->ccb));
5427 * Notify CAM that we're done
5429 mpt_set_ccb_status(ccb, CAM_REQ_CMP);
5430 ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
5431 KASSERT(ccb->ccb_h.status,
5432 ("ZERO ccb sts at %d\n", __LINE__));
5435 mpt_lprt(mpt, MPT_PRT_DEBUG,
5436 "TARGET_STATUS non-CAM for req %p:%u\n",
5437 tgt->req, tgt->req->serno);
5439 TAILQ_REMOVE(&mpt->request_pending_list,
5441 mpt_free_request(mpt, tgt->req);
5445 * And re-post the Command Buffer.
5446 * This will reset the state.
5448 ioindex = GET_IO_INDEX(reply_desc);
5449 TAILQ_REMOVE(&mpt->request_pending_list, req, links);
5451 mpt_post_target_command(mpt, req, ioindex);
5454 * And post a done for anyone who cares
5457 if (mpt->outofbeer) {
5458 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
5460 mpt_lprt(mpt, MPT_PRT_DEBUG, "THAWQ\n");
5462 MPTLOCK_2_CAMLOCK(mpt);
5464 CAMLOCK_2_MPTLOCK(mpt);
5468 case TGT_STATE_NIL: /* XXX This Never Happens XXX */
5469 tgt->state = TGT_STATE_LOADED;
5472 mpt_prt(mpt, "Unknown Target State 0x%x in Context "
5473 "Reply Function\n", tgt->state);
5478 status = le16toh(reply_frame->IOCStatus);
5479 if (status != MPI_IOCSTATUS_SUCCESS) {
5480 dbg = MPT_PRT_ERROR;
5482 dbg = MPT_PRT_DEBUG1;
5486 "SCSI_TGT REPLY: req=%p:%u reply=%p func=%x IOCstatus 0x%x\n",
5487 req, req->serno, reply_frame, reply_frame->Function, status);
5489 switch (reply_frame->Function) {
5490 case MPI_FUNCTION_TARGET_CMD_BUFFER_POST:
5492 mpt_tgt_state_t *tgt;
5494 mpt_req_spcl(mpt, req, "tgt reply BUFFER POST", __LINE__);
5496 if (status != MPI_IOCSTATUS_SUCCESS) {
5502 tgt = MPT_TGT_STATE(mpt, req);
5503 KASSERT(tgt->state == TGT_STATE_LOADING,
5504 ("bad state 0x%x on reply to buffer post\n", tgt->state));
5505 mpt_assign_serno(mpt, req);
5506 tgt->state = TGT_STATE_LOADED;
5509 case MPI_FUNCTION_TARGET_ASSIST:
5511 mpt_req_not_spcl(mpt, req, "tgt reply TARGET ASSIST", __LINE__);
5513 mpt_prt(mpt, "target assist completion\n");
5514 TAILQ_REMOVE(&mpt->request_pending_list, req, links);
5515 mpt_free_request(mpt, req);
5517 case MPI_FUNCTION_TARGET_STATUS_SEND:
5519 mpt_req_not_spcl(mpt, req, "tgt reply STATUS SEND", __LINE__);
5521 mpt_prt(mpt, "status send completion\n");
5522 TAILQ_REMOVE(&mpt->request_pending_list, req, links);
5523 mpt_free_request(mpt, req);
5525 case MPI_FUNCTION_TARGET_MODE_ABORT:
5527 PTR_MSG_TARGET_MODE_ABORT_REPLY abtrp =
5528 (PTR_MSG_TARGET_MODE_ABORT_REPLY) reply_frame;
5529 PTR_MSG_TARGET_MODE_ABORT abtp =
5530 (PTR_MSG_TARGET_MODE_ABORT) req->req_vbuf;
5531 uint32_t cc = GET_IO_INDEX(le32toh(abtp->ReplyWord));
5533 mpt_req_not_spcl(mpt, req, "tgt reply TMODE ABORT", __LINE__);
5535 mpt_prt(mpt, "ABORT RX_ID 0x%x Complete; status 0x%x cnt %u\n",
5536 cc, le16toh(abtrp->IOCStatus), le32toh(abtrp->AbortCount));
5537 TAILQ_REMOVE(&mpt->request_pending_list, req, links);
5538 mpt_free_request(mpt, req);
5542 mpt_prt(mpt, "Unknown Target Address Reply Function code: "
5543 "0x%x\n", reply_frame->Function);