2 * Copyright (c) 2015, AVAGO Tech. All rights reserved. Author: Marian Choy
3 * Copyright (c) 2014, LSI Corp. All rights reserved. Author: Marian Choy
4 * Support: freebsdraid@avagotech.com
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
10 * 1. Redistributions of source code must retain the above copyright notice,
11 * this list of conditions and the following disclaimer. 2. Redistributions
12 * in binary form must reproduce the above copyright notice, this list of
13 * conditions and the following disclaimer in the documentation and/or other
14 * materials provided with the distribution. 3. Neither the name of the
15 * <ORGANIZATION> nor the names of its contributors may be used to endorse or
16 * promote products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include "dev/mrsas/mrsas.h"
39 #include <cam/cam_ccb.h>
40 #include <cam/cam_sim.h>
41 #include <cam/cam_xpt_sim.h>
42 #include <cam/cam_debug.h>
43 #include <cam/cam_periph.h>
44 #include <cam/cam_xpt_periph.h>
46 #include <cam/scsi/scsi_all.h>
47 #include <cam/scsi/scsi_message.h>
48 #include <sys/taskqueue.h>
49 #include <sys/kernel.h>
52 #include <sys/time.h> /* XXX for pcpu.h */
53 #include <sys/pcpu.h> /* XXX for PCPU_GET */
55 #define smp_processor_id() PCPU_GET(cpuid)
60 int mrsas_cam_attach(struct mrsas_softc *sc);
61 int mrsas_find_io_type(struct cam_sim *sim, union ccb *ccb);
62 int mrsas_bus_scan(struct mrsas_softc *sc);
63 int mrsas_bus_scan_sim(struct mrsas_softc *sc, struct cam_sim *sim);
65 mrsas_map_request(struct mrsas_softc *sc,
66 struct mrsas_mpt_cmd *cmd, union ccb *ccb);
68 mrsas_build_ldio_rw(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
71 mrsas_build_ldio_nonrw(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
74 mrsas_build_syspdio(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
75 union ccb *ccb, struct cam_sim *sim, u_int8_t fp_possible);
77 mrsas_setup_io(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
78 union ccb *ccb, u_int32_t device_id,
79 MRSAS_RAID_SCSI_IO_REQUEST * io_request);
80 void mrsas_xpt_freeze(struct mrsas_softc *sc);
81 void mrsas_xpt_release(struct mrsas_softc *sc);
82 void mrsas_cam_detach(struct mrsas_softc *sc);
83 void mrsas_release_mpt_cmd(struct mrsas_mpt_cmd *cmd);
84 void mrsas_unmap_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd);
85 void mrsas_cmd_done(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd);
87 mrsas_fire_cmd(struct mrsas_softc *sc, u_int32_t req_desc_lo,
88 u_int32_t req_desc_hi);
90 mrsas_set_pd_lba(MRSAS_RAID_SCSI_IO_REQUEST * io_request,
91 u_int8_t cdb_len, struct IO_REQUEST_INFO *io_info, union ccb *ccb,
92 MR_DRV_RAID_MAP_ALL * local_map_ptr, u_int32_t ref_tag,
93 u_int32_t ld_block_size);
94 static void mrsas_freeze_simq(struct mrsas_mpt_cmd *cmd, struct cam_sim *sim);
95 static void mrsas_cam_poll(struct cam_sim *sim);
96 static void mrsas_action(struct cam_sim *sim, union ccb *ccb);
97 static void mrsas_scsiio_timeout(void *data);
99 mrsas_data_load_cb(void *arg, bus_dma_segment_t *segs,
100 int nseg, int error);
102 mrsas_startio(struct mrsas_softc *sc, struct cam_sim *sim,
104 struct mrsas_mpt_cmd *mrsas_get_mpt_cmd(struct mrsas_softc *sc);
105 MRSAS_REQUEST_DESCRIPTOR_UNION *
106 mrsas_get_request_desc(struct mrsas_softc *sc, u_int16_t index);
108 extern u_int16_t MR_TargetIdToLdGet(u_int32_t ldTgtId, MR_DRV_RAID_MAP_ALL * map);
110 MR_LdBlockSizeGet(u_int32_t ldTgtId, MR_DRV_RAID_MAP_ALL * map,
111 struct mrsas_softc *sc);
112 extern void mrsas_isr(void *arg);
113 extern void mrsas_aen_handler(struct mrsas_softc *sc);
115 MR_BuildRaidContext(struct mrsas_softc *sc,
116 struct IO_REQUEST_INFO *io_info, RAID_CONTEXT * pRAID_Context,
117 MR_DRV_RAID_MAP_ALL * map);
119 MR_LdSpanArrayGet(u_int32_t ld, u_int32_t span,
120 MR_DRV_RAID_MAP_ALL * map);
122 mrsas_get_updated_dev_handle(struct mrsas_softc *sc,
123 PLD_LOAD_BALANCE_INFO lbInfo, struct IO_REQUEST_INFO *io_info);
125 megasas_get_best_arm(PLD_LOAD_BALANCE_INFO lbInfo, u_int8_t arm,
126 u_int64_t block, u_int32_t count);
127 extern int mrsas_complete_cmd(struct mrsas_softc *sc, u_int32_t MSIxIndex);
131 * mrsas_cam_attach: Main entry to CAM subsystem
132 * input: Adapter instance soft state
134 * This function is called from mrsas_attach() during initialization to perform
135 * SIM allocations and XPT bus registration. If the kernel version is 7.4 or
136 * earlier, it would also initiate a bus scan.
139 mrsas_cam_attach(struct mrsas_softc *sc)
141 struct cam_devq *devq;
144 mrsas_cam_depth = sc->max_fw_cmds - MRSAS_INTERNAL_CMDS;
146 if ((devq = cam_simq_alloc(mrsas_cam_depth)) == NULL) {
147 device_printf(sc->mrsas_dev, "Cannot allocate SIM queue\n");
151 * Create SIM for bus 0 and register, also create path
153 sc->sim_0 = cam_sim_alloc(mrsas_action, mrsas_cam_poll, "mrsas", sc,
154 device_get_unit(sc->mrsas_dev), &sc->sim_lock, mrsas_cam_depth,
155 mrsas_cam_depth, devq);
156 if (sc->sim_0 == NULL) {
158 device_printf(sc->mrsas_dev, "Cannot register SIM\n");
161 /* Initialize taskqueue for Event Handling */
162 TASK_INIT(&sc->ev_task, 0, (void *)mrsas_aen_handler, sc);
163 sc->ev_tq = taskqueue_create("mrsas_taskq", M_NOWAIT | M_ZERO,
164 taskqueue_thread_enqueue, &sc->ev_tq);
166 /* Run the task queue with lowest priority */
167 taskqueue_start_threads(&sc->ev_tq, 1, 255, "%s taskq",
168 device_get_nameunit(sc->mrsas_dev));
169 mtx_lock(&sc->sim_lock);
170 if (xpt_bus_register(sc->sim_0, sc->mrsas_dev, 0) != CAM_SUCCESS) {
171 cam_sim_free(sc->sim_0, TRUE); /* passing true frees the devq */
172 mtx_unlock(&sc->sim_lock);
175 if (xpt_create_path(&sc->path_0, NULL, cam_sim_path(sc->sim_0),
176 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
177 xpt_bus_deregister(cam_sim_path(sc->sim_0));
178 cam_sim_free(sc->sim_0, TRUE); /* passing true will free the
180 mtx_unlock(&sc->sim_lock);
183 mtx_unlock(&sc->sim_lock);
186 * Create SIM for bus 1 and register, also create path
188 sc->sim_1 = cam_sim_alloc(mrsas_action, mrsas_cam_poll, "mrsas", sc,
189 device_get_unit(sc->mrsas_dev), &sc->sim_lock, mrsas_cam_depth,
190 mrsas_cam_depth, devq);
191 if (sc->sim_1 == NULL) {
193 device_printf(sc->mrsas_dev, "Cannot register SIM\n");
196 mtx_lock(&sc->sim_lock);
197 if (xpt_bus_register(sc->sim_1, sc->mrsas_dev, 1) != CAM_SUCCESS) {
198 cam_sim_free(sc->sim_1, TRUE); /* passing true frees the devq */
199 mtx_unlock(&sc->sim_lock);
202 if (xpt_create_path(&sc->path_1, NULL, cam_sim_path(sc->sim_1),
204 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
205 xpt_bus_deregister(cam_sim_path(sc->sim_1));
206 cam_sim_free(sc->sim_1, TRUE);
207 mtx_unlock(&sc->sim_lock);
210 mtx_unlock(&sc->sim_lock);
212 #if (__FreeBSD_version <= 704000)
213 if (mrsas_bus_scan(sc)) {
214 device_printf(sc->mrsas_dev, "Error in bus scan.\n");
222 * mrsas_cam_detach: De-allocates and teardown CAM
223 * input: Adapter instance soft state
225 * De-registers and frees the paths and SIMs.
228 mrsas_cam_detach(struct mrsas_softc *sc)
230 if (sc->ev_tq != NULL)
231 taskqueue_free(sc->ev_tq);
232 mtx_lock(&sc->sim_lock);
234 xpt_free_path(sc->path_0);
236 xpt_bus_deregister(cam_sim_path(sc->sim_0));
237 cam_sim_free(sc->sim_0, FALSE);
240 xpt_free_path(sc->path_1);
242 xpt_bus_deregister(cam_sim_path(sc->sim_1));
243 cam_sim_free(sc->sim_1, TRUE);
245 mtx_unlock(&sc->sim_lock);
249 * mrsas_action: SIM callback entry point
250 * input: pointer to SIM pointer to CAM Control Block
252 * This function processes CAM subsystem requests. The type of request is stored
253 * in ccb->ccb_h.func_code. The preprocessor #ifdef is necessary because
254 * ccb->cpi.maxio is not supported for FreeBSD version 7.4 or earlier.
257 mrsas_action(struct cam_sim *sim, union ccb *ccb)
259 struct mrsas_softc *sc = (struct mrsas_softc *)cam_sim_softc(sim);
260 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
263 switch (ccb->ccb_h.func_code) {
266 device_id = ccb_h->target_id;
269 * bus 0 is LD, bus 1 is for system-PD
271 if (cam_sim_bus(sim) == 1 &&
272 sc->pd_list[device_id].driveState != MR_PD_STATE_SYSTEM) {
273 ccb->ccb_h.status |= CAM_DEV_NOT_THERE;
276 if (mrsas_startio(sc, sim, ccb)) {
277 ccb->ccb_h.status |= CAM_REQ_INVALID;
285 ccb->ccb_h.status = CAM_UA_ABORT;
294 case XPT_GET_TRAN_SETTINGS:
296 ccb->cts.protocol = PROTO_SCSI;
297 ccb->cts.protocol_version = SCSI_REV_2;
298 ccb->cts.transport = XPORT_SPI;
299 ccb->cts.transport_version = 2;
300 ccb->cts.xport_specific.spi.valid = CTS_SPI_VALID_DISC;
301 ccb->cts.xport_specific.spi.flags = CTS_SPI_FLAGS_DISC_ENB;
302 ccb->cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
303 ccb->cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
304 ccb->ccb_h.status = CAM_REQ_CMP;
308 case XPT_SET_TRAN_SETTINGS:
310 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
314 case XPT_CALC_GEOMETRY:
316 cam_calc_geometry(&ccb->ccg, 1);
322 ccb->cpi.version_num = 1;
323 ccb->cpi.hba_inquiry = 0;
324 ccb->cpi.target_sprt = 0;
325 #if (__FreeBSD_version >= 902001)
326 ccb->cpi.hba_misc = PIM_UNMAPPED;
328 ccb->cpi.hba_misc = 0;
330 ccb->cpi.hba_eng_cnt = 0;
331 ccb->cpi.max_lun = MRSAS_SCSI_MAX_LUNS;
332 ccb->cpi.unit_number = cam_sim_unit(sim);
333 ccb->cpi.bus_id = cam_sim_bus(sim);
334 ccb->cpi.initiator_id = MRSAS_SCSI_INITIATOR_ID;
335 ccb->cpi.base_transfer_speed = 150000;
336 strncpy(ccb->cpi.sim_vid, "FreeBSD", SIM_IDLEN);
337 strncpy(ccb->cpi.hba_vid, "AVAGO", HBA_IDLEN);
338 strncpy(ccb->cpi.dev_name, cam_sim_name(sim), DEV_IDLEN);
339 ccb->cpi.transport = XPORT_SPI;
340 ccb->cpi.transport_version = 2;
341 ccb->cpi.protocol = PROTO_SCSI;
342 ccb->cpi.protocol_version = SCSI_REV_2;
343 if (ccb->cpi.bus_id == 0)
344 ccb->cpi.max_target = MRSAS_MAX_PD - 1;
346 ccb->cpi.max_target = MRSAS_MAX_LD_IDS - 1;
347 #if (__FreeBSD_version > 704000)
348 ccb->cpi.maxio = sc->max_num_sge * MRSAS_PAGE_SIZE;
350 ccb->ccb_h.status = CAM_REQ_CMP;
356 ccb->ccb_h.status = CAM_REQ_INVALID;
364 * mrsas_scsiio_timeout: Callback function for IO timed out
365 * input: mpt command context
367 * This function will execute after timeout value provided by ccb header from
368 * CAM layer, if timer expires. Driver will run timer for all DCDM and LDIO
369 * coming from CAM layer. This function is callback function for IO timeout
370 * and it runs in no-sleep context. Set do_timedout_reset in Adapter context
371 * so that it will execute OCR/Kill adpter from ocr_thread context.
374 mrsas_scsiio_timeout(void *data)
376 struct mrsas_mpt_cmd *cmd;
377 struct mrsas_softc *sc;
379 cmd = (struct mrsas_mpt_cmd *)data;
382 if (cmd->ccb_ptr == NULL) {
383 printf("command timeout with NULL ccb\n");
387 * Below callout is dummy entry so that it will be cancelled from
388 * mrsas_cmd_done(). Now Controller will go to OCR/Kill Adapter based
389 * on OCR enable/disable property of Controller from ocr_thread
392 #if (__FreeBSD_version >= 1000510)
393 callout_reset_sbt(&cmd->cm_callout, SBT_1S * 600, 0,
394 mrsas_scsiio_timeout, cmd, 0);
396 callout_reset(&cmd->cm_callout, (600000 * hz) / 1000,
397 mrsas_scsiio_timeout, cmd);
399 sc->do_timedout_reset = SCSIIO_TIMEOUT_OCR;
400 if (sc->ocr_thread_active)
401 wakeup(&sc->ocr_chan);
405 * mrsas_startio: SCSI IO entry point
406 * input: Adapter instance soft state
407 * pointer to CAM Control Block
409 * This function is the SCSI IO entry point and it initiates IO processing. It
410 * copies the IO and depending if the IO is read/write or inquiry, it would
411 * call mrsas_build_ldio() or mrsas_build_dcdb(), respectively. It returns 0
412 * if the command is sent to firmware successfully, otherwise it returns 1.
415 mrsas_startio(struct mrsas_softc *sc, struct cam_sim *sim,
418 struct mrsas_mpt_cmd *cmd;
419 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
420 struct ccb_scsiio *csio = &(ccb->csio);
421 MRSAS_REQUEST_DESCRIPTOR_UNION *req_desc;
424 if ((csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
425 ccb->ccb_h.status = CAM_REQ_CMP;
429 ccb_h->status |= CAM_SIM_QUEUED;
430 cmd = mrsas_get_mpt_cmd(sc);
433 ccb_h->status |= CAM_REQUEUE_REQ;
437 if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
438 if (ccb_h->flags & CAM_DIR_IN)
439 cmd->flags |= MRSAS_DIR_IN;
440 if (ccb_h->flags & CAM_DIR_OUT)
441 cmd->flags |= MRSAS_DIR_OUT;
443 cmd->flags = MRSAS_DIR_NONE; /* no data */
445 /* For FreeBSD 9.2 and higher */
446 #if (__FreeBSD_version >= 902001)
448 * XXX We don't yet support physical addresses here.
450 switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) {
452 case CAM_DATA_SG_PADDR:
453 device_printf(sc->mrsas_dev, "%s: physical addresses not supported\n",
455 mrsas_release_mpt_cmd(cmd);
456 ccb_h->status = CAM_REQ_INVALID;
457 ccb_h->status &= ~CAM_SIM_QUEUED;
460 device_printf(sc->mrsas_dev, "%s: scatter gather is not supported\n",
462 mrsas_release_mpt_cmd(cmd);
463 ccb_h->status = CAM_REQ_INVALID;
466 if (csio->dxfer_len > (sc->max_num_sge * MRSAS_PAGE_SIZE)) {
467 mrsas_release_mpt_cmd(cmd);
468 ccb_h->status = CAM_REQ_TOO_BIG;
471 cmd->length = csio->dxfer_len;
473 cmd->data = csio->data_ptr;
476 if (csio->dxfer_len > (sc->max_num_sge * MRSAS_PAGE_SIZE)) {
477 mrsas_release_mpt_cmd(cmd);
478 ccb_h->status = CAM_REQ_TOO_BIG;
481 cmd->length = csio->dxfer_len;
483 cmd->data = csio->data_ptr;
486 ccb->ccb_h.status = CAM_REQ_INVALID;
490 if (!(ccb_h->flags & CAM_DATA_PHYS)) { /* Virtual data address */
491 if (!(ccb_h->flags & CAM_SCATTER_VALID)) {
492 if (csio->dxfer_len > (sc->max_num_sge * MRSAS_PAGE_SIZE)) {
493 mrsas_release_mpt_cmd(cmd);
494 ccb_h->status = CAM_REQ_TOO_BIG;
497 cmd->length = csio->dxfer_len;
499 cmd->data = csio->data_ptr;
501 mrsas_release_mpt_cmd(cmd);
502 ccb_h->status = CAM_REQ_INVALID;
505 } else { /* Data addresses are physical. */
506 mrsas_release_mpt_cmd(cmd);
507 ccb_h->status = CAM_REQ_INVALID;
508 ccb_h->status &= ~CAM_SIM_QUEUED;
515 req_desc = mrsas_get_request_desc(sc, (cmd->index) - 1);
517 device_printf(sc->mrsas_dev, "Cannot get request_descriptor.\n");
520 memset(req_desc, 0, sizeof(MRSAS_REQUEST_DESCRIPTOR_UNION));
521 cmd->request_desc = req_desc;
523 if (ccb_h->flags & CAM_CDB_POINTER)
524 bcopy(csio->cdb_io.cdb_ptr, cmd->io_request->CDB.CDB32, csio->cdb_len);
526 bcopy(csio->cdb_io.cdb_bytes, cmd->io_request->CDB.CDB32, csio->cdb_len);
527 mtx_lock(&sc->raidmap_lock);
529 /* Check for IO type READ-WRITE targeted for Logical Volume */
530 cmd_type = mrsas_find_io_type(sim, ccb);
532 case READ_WRITE_LDIO:
533 /* Build READ-WRITE IO for Logical Volume */
534 if (mrsas_build_ldio_rw(sc, cmd, ccb)) {
535 device_printf(sc->mrsas_dev, "Build RW LDIO failed.\n");
536 mtx_unlock(&sc->raidmap_lock);
540 case NON_READ_WRITE_LDIO:
541 /* Build NON READ-WRITE IO for Logical Volume */
542 if (mrsas_build_ldio_nonrw(sc, cmd, ccb)) {
543 device_printf(sc->mrsas_dev, "Build NON-RW LDIO failed.\n");
544 mtx_unlock(&sc->raidmap_lock);
548 case READ_WRITE_SYSPDIO:
549 case NON_READ_WRITE_SYSPDIO:
550 if (sc->secure_jbod_support &&
551 (cmd_type == NON_READ_WRITE_SYSPDIO)) {
552 /* Build NON-RW IO for JBOD */
553 if (mrsas_build_syspdio(sc, cmd, ccb, sim, 0)) {
554 device_printf(sc->mrsas_dev,
555 "Build SYSPDIO failed.\n");
556 mtx_unlock(&sc->raidmap_lock);
560 /* Build RW IO for JBOD */
561 if (mrsas_build_syspdio(sc, cmd, ccb, sim, 1)) {
562 device_printf(sc->mrsas_dev,
563 "Build SYSPDIO failed.\n");
564 mtx_unlock(&sc->raidmap_lock);
569 mtx_unlock(&sc->raidmap_lock);
571 if (cmd->flags == MRSAS_DIR_IN) /* from device */
572 cmd->io_request->Control |= MPI2_SCSIIO_CONTROL_READ;
573 else if (cmd->flags == MRSAS_DIR_OUT) /* to device */
574 cmd->io_request->Control |= MPI2_SCSIIO_CONTROL_WRITE;
576 cmd->io_request->SGLFlags = MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
577 cmd->io_request->SGLOffset0 = offsetof(MRSAS_RAID_SCSI_IO_REQUEST, SGL) / 4;
578 cmd->io_request->SenseBufferLowAddress = cmd->sense_phys_addr;
579 cmd->io_request->SenseBufferLength = MRSAS_SCSI_SENSE_BUFFERSIZE;
581 req_desc = cmd->request_desc;
582 req_desc->SCSIIO.SMID = cmd->index;
585 * Start timer for IO timeout. Default timeout value is 90 second.
587 #if (__FreeBSD_version >= 1000510)
588 callout_reset_sbt(&cmd->cm_callout, SBT_1S * 600, 0,
589 mrsas_scsiio_timeout, cmd, 0);
591 callout_reset(&cmd->cm_callout, (600000 * hz) / 1000,
592 mrsas_scsiio_timeout, cmd);
594 mrsas_atomic_inc(&sc->fw_outstanding);
596 if (mrsas_atomic_read(&sc->fw_outstanding) > sc->io_cmds_highwater)
597 sc->io_cmds_highwater++;
599 mrsas_fire_cmd(sc, req_desc->addr.u.low, req_desc->addr.u.high);
608 * mrsas_find_io_type: Determines if IO is read/write or inquiry
609 * input: pointer to CAM Control Block
611 * This function determines if the IO is read/write or inquiry. It returns a 1
612 * if the IO is read/write and 0 if it is inquiry.
615 mrsas_find_io_type(struct cam_sim *sim, union ccb *ccb)
617 struct ccb_scsiio *csio = &(ccb->csio);
619 switch (csio->cdb_io.cdb_bytes[0]) {
628 return (cam_sim_bus(sim) ?
629 READ_WRITE_SYSPDIO : READ_WRITE_LDIO);
631 return (cam_sim_bus(sim) ?
632 NON_READ_WRITE_SYSPDIO : NON_READ_WRITE_LDIO);
637 * mrsas_get_mpt_cmd: Get a cmd from free command pool
638 * input: Adapter instance soft state
640 * This function removes an MPT command from the command free list and
643 struct mrsas_mpt_cmd *
644 mrsas_get_mpt_cmd(struct mrsas_softc *sc)
646 struct mrsas_mpt_cmd *cmd = NULL;
648 mtx_lock(&sc->mpt_cmd_pool_lock);
649 if (!TAILQ_EMPTY(&sc->mrsas_mpt_cmd_list_head)) {
650 cmd = TAILQ_FIRST(&sc->mrsas_mpt_cmd_list_head);
651 TAILQ_REMOVE(&sc->mrsas_mpt_cmd_list_head, cmd, next);
656 memset((uint8_t *)cmd->io_request, 0, MRSAS_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
661 cmd->load_balance = 0;
665 mtx_unlock(&sc->mpt_cmd_pool_lock);
670 * mrsas_release_mpt_cmd: Return a cmd to free command pool
671 * input: Command packet for return to free command pool
673 * This function returns an MPT command to the free command list.
676 mrsas_release_mpt_cmd(struct mrsas_mpt_cmd *cmd)
678 struct mrsas_softc *sc = cmd->sc;
680 mtx_lock(&sc->mpt_cmd_pool_lock);
681 cmd->sync_cmd_idx = (u_int32_t)MRSAS_ULONG_MAX;
682 TAILQ_INSERT_TAIL(&(sc->mrsas_mpt_cmd_list_head), cmd, next);
683 mtx_unlock(&sc->mpt_cmd_pool_lock);
689 * mrsas_get_request_desc: Get request descriptor from array
690 * input: Adapter instance soft state
693 * This function returns a pointer to the request descriptor.
695 MRSAS_REQUEST_DESCRIPTOR_UNION *
696 mrsas_get_request_desc(struct mrsas_softc *sc, u_int16_t index)
700 if (index >= sc->max_fw_cmds) {
701 device_printf(sc->mrsas_dev, "Invalid SMID (0x%x)request for desc\n", index);
704 p = sc->req_desc + sizeof(MRSAS_REQUEST_DESCRIPTOR_UNION) * index;
706 return (MRSAS_REQUEST_DESCRIPTOR_UNION *) p;
710 * mrsas_build_ldio_rw: Builds an LDIO command
711 * input: Adapter instance soft state
712 * Pointer to command packet
715 * This function builds the LDIO command packet. It returns 0 if the command is
716 * built successfully, otherwise it returns a 1.
719 mrsas_build_ldio_rw(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
722 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
723 struct ccb_scsiio *csio = &(ccb->csio);
725 MRSAS_RAID_SCSI_IO_REQUEST *io_request;
727 device_id = ccb_h->target_id;
729 io_request = cmd->io_request;
730 io_request->RaidContext.VirtualDiskTgtId = device_id;
731 io_request->RaidContext.status = 0;
732 io_request->RaidContext.exStatus = 0;
734 /* just the cdb len, other flags zero, and ORed-in later for FP */
735 io_request->IoFlags = csio->cdb_len;
737 if (mrsas_setup_io(sc, cmd, ccb, device_id, io_request) != SUCCESS)
738 device_printf(sc->mrsas_dev, "Build ldio or fpio error\n");
740 io_request->DataLength = cmd->length;
742 if (mrsas_map_request(sc, cmd, ccb) == SUCCESS) {
743 if (cmd->sge_count > sc->max_num_sge) {
744 device_printf(sc->mrsas_dev, "Error: sge_count (0x%x) exceeds"
745 "max (0x%x) allowed\n", cmd->sge_count, sc->max_num_sge);
749 * numSGE store lower 8 bit of sge_count. numSGEExt store
750 * higher 8 bit of sge_count
752 io_request->RaidContext.numSGE = cmd->sge_count;
753 io_request->RaidContext.numSGEExt = (uint8_t)(cmd->sge_count >> 8);
756 device_printf(sc->mrsas_dev, "Data map/load failed.\n");
763 * mrsas_setup_io: Set up data including Fast Path I/O
764 * input: Adapter instance soft state
765 * Pointer to command packet
768 * This function builds the DCDB inquiry command. It returns 0 if the command
769 * is built successfully, otherwise it returns a 1.
772 mrsas_setup_io(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
773 union ccb *ccb, u_int32_t device_id,
774 MRSAS_RAID_SCSI_IO_REQUEST * io_request)
776 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
777 struct ccb_scsiio *csio = &(ccb->csio);
778 struct IO_REQUEST_INFO io_info;
779 MR_DRV_RAID_MAP_ALL *map_ptr;
780 u_int8_t fp_possible;
781 u_int32_t start_lba_hi, start_lba_lo, ld_block_size;
782 u_int32_t datalength = 0;
789 * READ_6 (0x08) or WRITE_6 (0x0A) cdb
791 if (csio->cdb_len == 6) {
792 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[4];
793 start_lba_lo = ((u_int32_t)csio->cdb_io.cdb_bytes[1] << 16) |
794 ((u_int32_t)csio->cdb_io.cdb_bytes[2] << 8) |
795 (u_int32_t)csio->cdb_io.cdb_bytes[3];
796 start_lba_lo &= 0x1FFFFF;
799 * READ_10 (0x28) or WRITE_6 (0x2A) cdb
801 else if (csio->cdb_len == 10) {
802 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[8] |
803 ((u_int32_t)csio->cdb_io.cdb_bytes[7] << 8);
804 start_lba_lo = ((u_int32_t)csio->cdb_io.cdb_bytes[2] << 24) |
805 ((u_int32_t)csio->cdb_io.cdb_bytes[3] << 16) |
806 (u_int32_t)csio->cdb_io.cdb_bytes[4] << 8 |
807 ((u_int32_t)csio->cdb_io.cdb_bytes[5]);
810 * READ_12 (0xA8) or WRITE_12 (0xAA) cdb
812 else if (csio->cdb_len == 12) {
813 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[6] << 24 |
814 ((u_int32_t)csio->cdb_io.cdb_bytes[7] << 16) |
815 ((u_int32_t)csio->cdb_io.cdb_bytes[8] << 8) |
816 ((u_int32_t)csio->cdb_io.cdb_bytes[9]);
817 start_lba_lo = ((u_int32_t)csio->cdb_io.cdb_bytes[2] << 24) |
818 ((u_int32_t)csio->cdb_io.cdb_bytes[3] << 16) |
819 (u_int32_t)csio->cdb_io.cdb_bytes[4] << 8 |
820 ((u_int32_t)csio->cdb_io.cdb_bytes[5]);
823 * READ_16 (0x88) or WRITE_16 (0xx8A) cdb
825 else if (csio->cdb_len == 16) {
826 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[10] << 24 |
827 ((u_int32_t)csio->cdb_io.cdb_bytes[11] << 16) |
828 ((u_int32_t)csio->cdb_io.cdb_bytes[12] << 8) |
829 ((u_int32_t)csio->cdb_io.cdb_bytes[13]);
830 start_lba_lo = ((u_int32_t)csio->cdb_io.cdb_bytes[6] << 24) |
831 ((u_int32_t)csio->cdb_io.cdb_bytes[7] << 16) |
832 (u_int32_t)csio->cdb_io.cdb_bytes[8] << 8 |
833 ((u_int32_t)csio->cdb_io.cdb_bytes[9]);
834 start_lba_hi = ((u_int32_t)csio->cdb_io.cdb_bytes[2] << 24) |
835 ((u_int32_t)csio->cdb_io.cdb_bytes[3] << 16) |
836 (u_int32_t)csio->cdb_io.cdb_bytes[4] << 8 |
837 ((u_int32_t)csio->cdb_io.cdb_bytes[5]);
839 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
840 io_info.ldStartBlock = ((u_int64_t)start_lba_hi << 32) | start_lba_lo;
841 io_info.numBlocks = datalength;
842 io_info.ldTgtId = device_id;
844 switch (ccb_h->flags & CAM_DIR_MASK) {
853 mrsas_dprint(sc, MRSAS_TRACE, "From %s : DMA Flag is %d \n", __func__, ccb_h->flags & CAM_DIR_MASK);
857 map_ptr = sc->ld_drv_map[(sc->map_id & 1)];
858 ld_block_size = MR_LdBlockSizeGet(device_id, map_ptr, sc);
860 if ((MR_TargetIdToLdGet(device_id, map_ptr) >= MAX_LOGICAL_DRIVES_EXT) ||
861 (!sc->fast_path_io)) {
862 io_request->RaidContext.regLockFlags = 0;
865 if (MR_BuildRaidContext(sc, &io_info, &io_request->RaidContext, map_ptr))
866 fp_possible = io_info.fpOkForIo;
869 cmd->request_desc->SCSIIO.MSIxIndex =
870 sc->msix_vectors ? smp_processor_id() % sc->msix_vectors : 0;
874 mrsas_set_pd_lba(io_request, csio->cdb_len, &io_info, ccb, map_ptr,
875 start_lba_lo, ld_block_size);
876 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
877 cmd->request_desc->SCSIIO.RequestFlags =
878 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
879 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
880 if ((sc->device_id == MRSAS_INVADER) ||
881 (sc->device_id == MRSAS_FURY) ||
882 (sc->device_id == MRSAS_INTRUDER) ||
883 (sc->device_id == MRSAS_INTRUDER_24) ||
884 (sc->device_id == MRSAS_CUTLASS_52) ||
885 (sc->device_id == MRSAS_CUTLASS_53)) {
886 if (io_request->RaidContext.regLockFlags == REGION_TYPE_UNUSED)
887 cmd->request_desc->SCSIIO.RequestFlags =
888 (MRSAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
889 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
890 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
891 io_request->RaidContext.nseg = 0x1;
892 io_request->IoFlags |= MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
893 io_request->RaidContext.regLockFlags |=
894 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
895 MR_RL_FLAGS_SEQ_NUM_ENABLE);
897 if ((sc->load_balance_info[device_id].loadBalanceFlag) &&
900 mrsas_get_updated_dev_handle(sc,
901 &sc->load_balance_info[device_id], &io_info);
902 cmd->load_balance = MRSAS_LOAD_BALANCE_FLAG;
903 cmd->pd_r1_lb = io_info.pd_after_lb;
905 cmd->load_balance = 0;
906 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
907 io_request->DevHandle = io_info.devHandle;
910 io_request->RaidContext.timeoutValue = map_ptr->raidMap.fpPdIoTimeoutSec;
911 cmd->request_desc->SCSIIO.RequestFlags =
912 (MRSAS_REQ_DESCRIPT_FLAGS_LD_IO <<
913 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
914 if ((sc->device_id == MRSAS_INVADER) ||
915 (sc->device_id == MRSAS_FURY) ||
916 (sc->device_id == MRSAS_INTRUDER) ||
917 (sc->device_id == MRSAS_INTRUDER_24) ||
918 (sc->device_id == MRSAS_CUTLASS_52) ||
919 (sc->device_id == MRSAS_CUTLASS_53)) {
920 if (io_request->RaidContext.regLockFlags == REGION_TYPE_UNUSED)
921 cmd->request_desc->SCSIIO.RequestFlags =
922 (MRSAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
923 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
924 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
925 io_request->RaidContext.regLockFlags |=
926 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
927 MR_RL_FLAGS_SEQ_NUM_ENABLE);
928 io_request->RaidContext.nseg = 0x1;
930 io_request->Function = MRSAS_MPI2_FUNCTION_LD_IO_REQUEST;
931 io_request->DevHandle = device_id;
937 * mrsas_build_ldio_nonrw: Builds an LDIO command
938 * input: Adapter instance soft state
939 * Pointer to command packet
942 * This function builds the LDIO command packet. It returns 0 if the command is
943 * built successfully, otherwise it returns a 1.
946 mrsas_build_ldio_nonrw(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
949 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
951 MRSAS_RAID_SCSI_IO_REQUEST *io_request;
953 io_request = cmd->io_request;
954 device_id = ccb_h->target_id;
956 /* FW path for LD Non-RW (SCSI management commands) */
957 io_request->Function = MRSAS_MPI2_FUNCTION_LD_IO_REQUEST;
958 io_request->DevHandle = device_id;
959 cmd->request_desc->SCSIIO.RequestFlags =
960 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
961 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
963 io_request->RaidContext.VirtualDiskTgtId = device_id;
964 io_request->LUN[1] = ccb_h->target_lun & 0xF;
965 io_request->DataLength = cmd->length;
967 if (mrsas_map_request(sc, cmd, ccb) == SUCCESS) {
968 if (cmd->sge_count > sc->max_num_sge) {
969 device_printf(sc->mrsas_dev, "Error: sge_count (0x%x) exceeds"
970 "max (0x%x) allowed\n", cmd->sge_count, sc->max_num_sge);
974 * numSGE store lower 8 bit of sge_count. numSGEExt store
975 * higher 8 bit of sge_count
977 io_request->RaidContext.numSGE = cmd->sge_count;
978 io_request->RaidContext.numSGEExt = (uint8_t)(cmd->sge_count >> 8);
980 device_printf(sc->mrsas_dev, "Data map/load failed.\n");
987 * mrsas_build_syspdio: Builds an DCDB command
988 * input: Adapter instance soft state
989 * Pointer to command packet
992 * This function builds the DCDB inquiry command. It returns 0 if the command
993 * is built successfully, otherwise it returns a 1.
996 mrsas_build_syspdio(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
997 union ccb *ccb, struct cam_sim *sim, u_int8_t fp_possible)
999 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
1000 u_int32_t device_id;
1001 MR_DRV_RAID_MAP_ALL *local_map_ptr;
1002 MRSAS_RAID_SCSI_IO_REQUEST *io_request;
1003 struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1005 pd_sync = (void *)sc->jbodmap_mem[(sc->pd_seq_map_id - 1) & 1];
1007 io_request = cmd->io_request;
1008 device_id = ccb_h->target_id;
1009 local_map_ptr = sc->ld_drv_map[(sc->map_id & 1)];
1010 io_request->RaidContext.RAIDFlags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
1011 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1012 io_request->RaidContext.regLockFlags = 0;
1013 io_request->RaidContext.regLockRowLBA = 0;
1014 io_request->RaidContext.regLockLength = 0;
1016 /* If FW supports PD sequence number */
1017 if (sc->use_seqnum_jbod_fp &&
1018 sc->pd_list[device_id].driveType == 0x00) {
1019 //printf("Using Drv seq num\n");
1020 io_request->RaidContext.VirtualDiskTgtId = device_id + 255;
1021 io_request->RaidContext.configSeqNum = pd_sync->seq[device_id].seqNum;
1022 io_request->DevHandle = pd_sync->seq[device_id].devHandle;
1023 io_request->RaidContext.regLockFlags |=
1024 (MR_RL_FLAGS_SEQ_NUM_ENABLE | MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
1025 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1026 io_request->RaidContext.nseg = 0x1;
1027 } else if (sc->fast_path_io) {
1028 //printf("Using LD RAID map\n");
1029 io_request->RaidContext.VirtualDiskTgtId = device_id;
1030 io_request->RaidContext.configSeqNum = 0;
1031 local_map_ptr = sc->ld_drv_map[(sc->map_id & 1)];
1032 io_request->DevHandle =
1033 local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1035 //printf("Using FW PATH\n");
1036 /* Want to send all IO via FW path */
1037 io_request->RaidContext.VirtualDiskTgtId = device_id;
1038 io_request->RaidContext.configSeqNum = 0;
1039 io_request->DevHandle = 0xFFFF;
1042 cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
1043 cmd->request_desc->SCSIIO.MSIxIndex =
1044 sc->msix_vectors ? smp_processor_id() % sc->msix_vectors : 0;
1047 /* system pd firmware path */
1048 io_request->Function = MRSAS_MPI2_FUNCTION_LD_IO_REQUEST;
1049 cmd->request_desc->SCSIIO.RequestFlags =
1050 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1051 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1052 io_request->RaidContext.timeoutValue =
1053 local_map_ptr->raidMap.fpPdIoTimeoutSec;
1054 io_request->RaidContext.VirtualDiskTgtId = device_id;
1056 /* system pd fast path */
1057 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1058 io_request->RaidContext.timeoutValue = local_map_ptr->raidMap.fpPdIoTimeoutSec;
1061 * NOTE - For system pd RW cmds only IoFlags will be FAST_PATH
1062 * Because the NON RW cmds will now go via FW Queue
1063 * and not the Exception queue
1065 io_request->IoFlags |= MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
1067 cmd->request_desc->SCSIIO.RequestFlags =
1068 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1069 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1072 io_request->LUN[1] = ccb_h->target_lun & 0xF;
1073 io_request->DataLength = cmd->length;
1075 if (mrsas_map_request(sc, cmd, ccb) == SUCCESS) {
1076 if (cmd->sge_count > sc->max_num_sge) {
1077 device_printf(sc->mrsas_dev, "Error: sge_count (0x%x) exceeds"
1078 "max (0x%x) allowed\n", cmd->sge_count, sc->max_num_sge);
1082 * numSGE store lower 8 bit of sge_count. numSGEExt store
1083 * higher 8 bit of sge_count
1085 io_request->RaidContext.numSGE = cmd->sge_count;
1086 io_request->RaidContext.numSGEExt = (uint8_t)(cmd->sge_count >> 8);
1088 device_printf(sc->mrsas_dev, "Data map/load failed.\n");
1095 * mrsas_map_request: Map and load data
1096 * input: Adapter instance soft state
1097 * Pointer to command packet
1099 * For data from OS, map and load the data buffer into bus space. The SG list
1100 * is built in the callback. If the bus dmamap load is not successful,
1101 * cmd->error_code will contain the error code and a 1 is returned.
1104 mrsas_map_request(struct mrsas_softc *sc,
1105 struct mrsas_mpt_cmd *cmd, union ccb *ccb)
1107 u_int32_t retcode = 0;
1108 struct cam_sim *sim;
1110 sim = xpt_path_sim(cmd->ccb_ptr->ccb_h.path);
1112 if (cmd->data != NULL) {
1113 /* Map data buffer into bus space */
1114 mtx_lock(&sc->io_lock);
1115 #if (__FreeBSD_version >= 902001)
1116 retcode = bus_dmamap_load_ccb(sc->data_tag, cmd->data_dmamap, ccb,
1117 mrsas_data_load_cb, cmd, 0);
1119 retcode = bus_dmamap_load(sc->data_tag, cmd->data_dmamap, cmd->data,
1120 cmd->length, mrsas_data_load_cb, cmd, BUS_DMA_NOWAIT);
1122 mtx_unlock(&sc->io_lock);
1124 device_printf(sc->mrsas_dev, "bus_dmamap_load(): retcode = %d\n", retcode);
1125 if (retcode == EINPROGRESS) {
1126 device_printf(sc->mrsas_dev, "request load in progress\n");
1127 mrsas_freeze_simq(cmd, sim);
1130 if (cmd->error_code)
1136 * mrsas_unmap_request: Unmap and unload data
1137 * input: Adapter instance soft state
1138 * Pointer to command packet
1140 * This function unmaps and unloads data from OS.
1143 mrsas_unmap_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd)
1145 if (cmd->data != NULL) {
1146 if (cmd->flags & MRSAS_DIR_IN)
1147 bus_dmamap_sync(sc->data_tag, cmd->data_dmamap, BUS_DMASYNC_POSTREAD);
1148 if (cmd->flags & MRSAS_DIR_OUT)
1149 bus_dmamap_sync(sc->data_tag, cmd->data_dmamap, BUS_DMASYNC_POSTWRITE);
1150 mtx_lock(&sc->io_lock);
1151 bus_dmamap_unload(sc->data_tag, cmd->data_dmamap);
1152 mtx_unlock(&sc->io_lock);
1157 * mrsas_data_load_cb: Callback entry point
1158 * input: Pointer to command packet as argument
1159 * Pointer to segment
1160 * Number of segments Error
1162 * This is the callback function of the bus dma map load. It builds the SG
1166 mrsas_data_load_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1168 struct mrsas_mpt_cmd *cmd = (struct mrsas_mpt_cmd *)arg;
1169 struct mrsas_softc *sc = cmd->sc;
1170 MRSAS_RAID_SCSI_IO_REQUEST *io_request;
1171 pMpi25IeeeSgeChain64_t sgl_ptr;
1172 int i = 0, sg_processed = 0;
1175 cmd->error_code = error;
1176 device_printf(sc->mrsas_dev, "mrsas_data_load_cb: error=%d\n", error);
1177 if (error == EFBIG) {
1178 cmd->ccb_ptr->ccb_h.status = CAM_REQ_TOO_BIG;
1182 if (cmd->flags & MRSAS_DIR_IN)
1183 bus_dmamap_sync(cmd->sc->data_tag, cmd->data_dmamap,
1184 BUS_DMASYNC_PREREAD);
1185 if (cmd->flags & MRSAS_DIR_OUT)
1186 bus_dmamap_sync(cmd->sc->data_tag, cmd->data_dmamap,
1187 BUS_DMASYNC_PREWRITE);
1188 if (nseg > sc->max_num_sge) {
1189 device_printf(sc->mrsas_dev, "SGE count is too large or 0.\n");
1192 io_request = cmd->io_request;
1193 sgl_ptr = (pMpi25IeeeSgeChain64_t)&io_request->SGL;
1195 if ((sc->device_id == MRSAS_INVADER) ||
1196 (sc->device_id == MRSAS_FURY) ||
1197 (sc->device_id == MRSAS_INTRUDER) ||
1198 (sc->device_id == MRSAS_INTRUDER_24) ||
1199 (sc->device_id == MRSAS_CUTLASS_52) ||
1200 (sc->device_id == MRSAS_CUTLASS_53)) {
1201 pMpi25IeeeSgeChain64_t sgl_ptr_end = sgl_ptr;
1203 sgl_ptr_end += sc->max_sge_in_main_msg - 1;
1204 sgl_ptr_end->Flags = 0;
1207 for (i = 0; i < nseg; i++) {
1208 sgl_ptr->Address = segs[i].ds_addr;
1209 sgl_ptr->Length = segs[i].ds_len;
1211 if ((sc->device_id == MRSAS_INVADER) ||
1212 (sc->device_id == MRSAS_FURY) ||
1213 (sc->device_id == MRSAS_INTRUDER) ||
1214 (sc->device_id == MRSAS_INTRUDER_24) ||
1215 (sc->device_id == MRSAS_CUTLASS_52) ||
1216 (sc->device_id == MRSAS_CUTLASS_53)) {
1218 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1221 sg_processed = i + 1;
1222 if ((sg_processed == (sc->max_sge_in_main_msg - 1)) &&
1223 (nseg > sc->max_sge_in_main_msg)) {
1224 pMpi25IeeeSgeChain64_t sg_chain;
1226 if ((sc->device_id == MRSAS_INVADER) ||
1227 (sc->device_id == MRSAS_FURY) ||
1228 (sc->device_id == MRSAS_INTRUDER) ||
1229 (sc->device_id == MRSAS_INTRUDER_24) ||
1230 (sc->device_id == MRSAS_CUTLASS_52) ||
1231 (sc->device_id == MRSAS_CUTLASS_53)) {
1232 if ((cmd->io_request->IoFlags & MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1233 != MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1234 cmd->io_request->ChainOffset = sc->chain_offset_io_request;
1236 cmd->io_request->ChainOffset = 0;
1238 cmd->io_request->ChainOffset = sc->chain_offset_io_request;
1240 if ((sc->device_id == MRSAS_INVADER) ||
1241 (sc->device_id == MRSAS_FURY) ||
1242 (sc->device_id == MRSAS_INTRUDER) ||
1243 (sc->device_id == MRSAS_INTRUDER_24) ||
1244 (sc->device_id == MRSAS_CUTLASS_52) ||
1245 (sc->device_id == MRSAS_CUTLASS_53))
1246 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1248 sg_chain->Flags = (IEEE_SGE_FLAGS_CHAIN_ELEMENT | MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1249 sg_chain->Length = (sizeof(MPI2_SGE_IO_UNION) * (nseg - sg_processed));
1250 sg_chain->Address = cmd->chain_frame_phys_addr;
1251 sgl_ptr = (pMpi25IeeeSgeChain64_t)cmd->chain_frame;
1255 cmd->sge_count = nseg;
1259 * mrsas_freeze_simq: Freeze SIM queue
1260 * input: Pointer to command packet
1263 * This function freezes the sim queue.
1266 mrsas_freeze_simq(struct mrsas_mpt_cmd *cmd, struct cam_sim *sim)
1268 union ccb *ccb = (union ccb *)(cmd->ccb_ptr);
1270 xpt_freeze_simq(sim, 1);
1271 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1272 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1276 mrsas_xpt_freeze(struct mrsas_softc *sc)
1278 xpt_freeze_simq(sc->sim_0, 1);
1279 xpt_freeze_simq(sc->sim_1, 1);
1283 mrsas_xpt_release(struct mrsas_softc *sc)
1285 xpt_release_simq(sc->sim_0, 1);
1286 xpt_release_simq(sc->sim_1, 1);
1290 * mrsas_cmd_done: Perform remaining command completion
1291 * input: Adapter instance soft state Pointer to command packet
1293 * This function calls ummap request and releases the MPT command.
1296 mrsas_cmd_done(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd)
1298 callout_stop(&cmd->cm_callout);
1299 mrsas_unmap_request(sc, cmd);
1300 mtx_lock(&sc->sim_lock);
1301 xpt_done(cmd->ccb_ptr);
1302 cmd->ccb_ptr = NULL;
1303 mtx_unlock(&sc->sim_lock);
1304 mrsas_release_mpt_cmd(cmd);
1308 * mrsas_cam_poll: Polling entry point
1309 * input: Pointer to SIM
1311 * This is currently a stub function.
1314 mrsas_cam_poll(struct cam_sim *sim)
1317 struct mrsas_softc *sc = (struct mrsas_softc *)cam_sim_softc(sim);
1319 if (sc->msix_vectors != 0){
1320 for (i=0; i<sc->msix_vectors; i++){
1321 mrsas_complete_cmd(sc, i);
1324 mrsas_complete_cmd(sc, 0);
1329 * mrsas_bus_scan: Perform bus scan
1330 * input: Adapter instance soft state
1332 * This mrsas_bus_scan function is needed for FreeBSD 7.x. Also, it should not
1333 * be called in FreeBSD 8.x and later versions, where the bus scan is
1337 mrsas_bus_scan(struct mrsas_softc *sc)
1342 if ((ccb_0 = xpt_alloc_ccb()) == NULL) {
1345 if ((ccb_1 = xpt_alloc_ccb()) == NULL) {
1346 xpt_free_ccb(ccb_0);
1349 mtx_lock(&sc->sim_lock);
1350 if (xpt_create_path(&ccb_0->ccb_h.path, xpt_periph, cam_sim_path(sc->sim_0),
1351 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1352 xpt_free_ccb(ccb_0);
1353 xpt_free_ccb(ccb_1);
1354 mtx_unlock(&sc->sim_lock);
1357 if (xpt_create_path(&ccb_1->ccb_h.path, xpt_periph, cam_sim_path(sc->sim_1),
1358 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1359 xpt_free_ccb(ccb_0);
1360 xpt_free_ccb(ccb_1);
1361 mtx_unlock(&sc->sim_lock);
1364 mtx_unlock(&sc->sim_lock);
1372 * mrsas_bus_scan_sim: Perform bus scan per SIM
1373 * input: adapter instance soft state
1375 * This function will be called from Event handler on LD creation/deletion,
1379 mrsas_bus_scan_sim(struct mrsas_softc *sc, struct cam_sim *sim)
1383 if ((ccb = xpt_alloc_ccb()) == NULL) {
1386 mtx_lock(&sc->sim_lock);
1387 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(sim),
1388 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1390 mtx_unlock(&sc->sim_lock);
1393 mtx_unlock(&sc->sim_lock);