2 * Copyright (c) 2014, LSI Corp. All rights reserved. Author: Marian Choy
3 * Support: freebsdraid@lsi.com
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer. 2. Redistributions
11 * in binary form must reproduce the above copyright notice, this list of
12 * conditions and the following disclaimer in the documentation and/or other
13 * materials provided with the distribution. 3. Neither the name of the
14 * <ORGANIZATION> nor the names of its contributors may be used to endorse or
15 * promote products derived from this software without specific prior written
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include "dev/mrsas/mrsas.h"
38 #include <cam/cam_ccb.h>
39 #include <cam/cam_sim.h>
40 #include <cam/cam_xpt_sim.h>
41 #include <cam/cam_debug.h>
42 #include <cam/cam_periph.h>
43 #include <cam/cam_xpt_periph.h>
45 #include <cam/scsi/scsi_all.h>
46 #include <cam/scsi/scsi_message.h>
47 #include <sys/taskqueue.h>
48 #include <sys/kernel.h>
51 #include <sys/time.h> /* XXX for pcpu.h */
52 #include <sys/pcpu.h> /* XXX for PCPU_GET */
54 #define smp_processor_id() PCPU_GET(cpuid)
59 int mrsas_cam_attach(struct mrsas_softc *sc);
60 int mrsas_ldio_inq(struct cam_sim *sim, union ccb *ccb);
61 int mrsas_bus_scan(struct mrsas_softc *sc);
62 int mrsas_bus_scan_sim(struct mrsas_softc *sc, struct cam_sim *sim);
63 int mrsas_map_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd);
65 mrsas_build_ldio(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
68 mrsas_build_dcdb(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
69 union ccb *ccb, struct cam_sim *sim);
71 mrsas_setup_io(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
72 union ccb *ccb, u_int32_t device_id,
73 MRSAS_RAID_SCSI_IO_REQUEST * io_request);
74 void mrsas_xpt_freeze(struct mrsas_softc *sc);
75 void mrsas_xpt_release(struct mrsas_softc *sc);
76 void mrsas_cam_detach(struct mrsas_softc *sc);
77 void mrsas_release_mpt_cmd(struct mrsas_mpt_cmd *cmd);
78 void mrsas_unmap_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd);
79 void mrsas_cmd_done(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd);
81 mrsas_fire_cmd(struct mrsas_softc *sc, u_int32_t req_desc_lo,
82 u_int32_t req_desc_hi);
84 mrsas_set_pd_lba(MRSAS_RAID_SCSI_IO_REQUEST * io_request,
85 u_int8_t cdb_len, struct IO_REQUEST_INFO *io_info, union ccb *ccb,
86 MR_DRV_RAID_MAP_ALL * local_map_ptr, u_int32_t ref_tag,
87 u_int32_t ld_block_size);
88 static void mrsas_freeze_simq(struct mrsas_mpt_cmd *cmd, struct cam_sim *sim);
89 static void mrsas_cam_poll(struct cam_sim *sim);
90 static void mrsas_action(struct cam_sim *sim, union ccb *ccb);
91 static void mrsas_scsiio_timeout(void *data);
93 mrsas_data_load_cb(void *arg, bus_dma_segment_t *segs,
96 mrsas_startio(struct mrsas_softc *sc, struct cam_sim *sim,
98 struct mrsas_mpt_cmd *mrsas_get_mpt_cmd(struct mrsas_softc *sc);
99 MRSAS_REQUEST_DESCRIPTOR_UNION *
100 mrsas_get_request_desc(struct mrsas_softc *sc, u_int16_t index);
102 extern u_int16_t MR_TargetIdToLdGet(u_int32_t ldTgtId, MR_DRV_RAID_MAP_ALL * map);
104 MR_LdBlockSizeGet(u_int32_t ldTgtId, MR_DRV_RAID_MAP_ALL * map,
105 struct mrsas_softc *sc);
106 extern void mrsas_isr(void *arg);
107 extern void mrsas_aen_handler(struct mrsas_softc *sc);
109 MR_BuildRaidContext(struct mrsas_softc *sc,
110 struct IO_REQUEST_INFO *io_info, RAID_CONTEXT * pRAID_Context,
111 MR_DRV_RAID_MAP_ALL * map);
113 MR_LdSpanArrayGet(u_int32_t ld, u_int32_t span,
114 MR_DRV_RAID_MAP_ALL * map);
116 mrsas_get_updated_dev_handle(PLD_LOAD_BALANCE_INFO lbInfo,
117 struct IO_REQUEST_INFO *io_info);
119 megasas_get_best_arm(PLD_LOAD_BALANCE_INFO lbInfo, u_int8_t arm,
120 u_int64_t block, u_int32_t count);
124 * mrsas_cam_attach: Main entry to CAM subsystem
125 * input: Adapter instance soft state
127 * This function is called from mrsas_attach() during initialization to perform
128 * SIM allocations and XPT bus registration. If the kernel version is 7.4 or
129 * earlier, it would also initiate a bus scan.
132 mrsas_cam_attach(struct mrsas_softc *sc)
134 struct cam_devq *devq;
137 mrsas_cam_depth = sc->max_fw_cmds - MRSAS_INTERNAL_CMDS;
139 if ((devq = cam_simq_alloc(mrsas_cam_depth)) == NULL) {
140 device_printf(sc->mrsas_dev, "Cannot allocate SIM queue\n");
144 * Create SIM for bus 0 and register, also create path
146 sc->sim_0 = cam_sim_alloc(mrsas_action, mrsas_cam_poll, "mrsas", sc,
147 device_get_unit(sc->mrsas_dev), &sc->sim_lock, mrsas_cam_depth,
148 mrsas_cam_depth, devq);
149 if (sc->sim_0 == NULL) {
151 device_printf(sc->mrsas_dev, "Cannot register SIM\n");
154 /* Initialize taskqueue for Event Handling */
155 TASK_INIT(&sc->ev_task, 0, (void *)mrsas_aen_handler, sc);
156 sc->ev_tq = taskqueue_create("mrsas_taskq", M_NOWAIT | M_ZERO,
157 taskqueue_thread_enqueue, &sc->ev_tq);
159 /* Run the task queue with lowest priority */
160 taskqueue_start_threads(&sc->ev_tq, 1, 255, "%s taskq",
161 device_get_nameunit(sc->mrsas_dev));
162 mtx_lock(&sc->sim_lock);
163 if (xpt_bus_register(sc->sim_0, sc->mrsas_dev, 0) != CAM_SUCCESS) {
164 cam_sim_free(sc->sim_0, TRUE); /* passing true frees the devq */
165 mtx_unlock(&sc->sim_lock);
168 if (xpt_create_path(&sc->path_0, NULL, cam_sim_path(sc->sim_0),
169 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
170 xpt_bus_deregister(cam_sim_path(sc->sim_0));
171 cam_sim_free(sc->sim_0, TRUE); /* passing true will free the
173 mtx_unlock(&sc->sim_lock);
176 mtx_unlock(&sc->sim_lock);
179 * Create SIM for bus 1 and register, also create path
181 sc->sim_1 = cam_sim_alloc(mrsas_action, mrsas_cam_poll, "mrsas", sc,
182 device_get_unit(sc->mrsas_dev), &sc->sim_lock, mrsas_cam_depth,
183 mrsas_cam_depth, devq);
184 if (sc->sim_1 == NULL) {
186 device_printf(sc->mrsas_dev, "Cannot register SIM\n");
189 mtx_lock(&sc->sim_lock);
190 if (xpt_bus_register(sc->sim_1, sc->mrsas_dev, 1) != CAM_SUCCESS) {
191 cam_sim_free(sc->sim_1, TRUE); /* passing true frees the devq */
192 mtx_unlock(&sc->sim_lock);
195 if (xpt_create_path(&sc->path_1, NULL, cam_sim_path(sc->sim_1),
197 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
198 xpt_bus_deregister(cam_sim_path(sc->sim_1));
199 cam_sim_free(sc->sim_1, TRUE);
200 mtx_unlock(&sc->sim_lock);
203 mtx_unlock(&sc->sim_lock);
205 #if (__FreeBSD_version <= 704000)
206 if (mrsas_bus_scan(sc)) {
207 device_printf(sc->mrsas_dev, "Error in bus scan.\n");
215 * mrsas_cam_detach: De-allocates and teardown CAM
216 * input: Adapter instance soft state
218 * De-registers and frees the paths and SIMs.
221 mrsas_cam_detach(struct mrsas_softc *sc)
223 if (sc->ev_tq != NULL)
224 taskqueue_free(sc->ev_tq);
225 mtx_lock(&sc->sim_lock);
227 xpt_free_path(sc->path_0);
229 xpt_bus_deregister(cam_sim_path(sc->sim_0));
230 cam_sim_free(sc->sim_0, FALSE);
233 xpt_free_path(sc->path_1);
235 xpt_bus_deregister(cam_sim_path(sc->sim_1));
236 cam_sim_free(sc->sim_1, TRUE);
238 mtx_unlock(&sc->sim_lock);
242 * mrsas_action: SIM callback entry point
243 * input: pointer to SIM pointer to CAM Control Block
245 * This function processes CAM subsystem requests. The type of request is stored
246 * in ccb->ccb_h.func_code. The preprocessor #ifdef is necessary because
247 * ccb->cpi.maxio is not supported for FreeBSD version 7.4 or earlier.
250 mrsas_action(struct cam_sim *sim, union ccb *ccb)
252 struct mrsas_softc *sc = (struct mrsas_softc *)cam_sim_softc(sim);
253 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
256 switch (ccb->ccb_h.func_code) {
259 device_id = ccb_h->target_id;
262 * bus 0 is LD, bus 1 is for system-PD
264 if (cam_sim_bus(sim) == 1 &&
265 sc->pd_list[device_id].driveState != MR_PD_STATE_SYSTEM) {
266 ccb->ccb_h.status |= CAM_DEV_NOT_THERE;
269 if (mrsas_startio(sc, sim, ccb)) {
270 ccb->ccb_h.status |= CAM_REQ_INVALID;
278 ccb->ccb_h.status = CAM_UA_ABORT;
287 case XPT_GET_TRAN_SETTINGS:
289 ccb->cts.protocol = PROTO_SCSI;
290 ccb->cts.protocol_version = SCSI_REV_2;
291 ccb->cts.transport = XPORT_SPI;
292 ccb->cts.transport_version = 2;
293 ccb->cts.xport_specific.spi.valid = CTS_SPI_VALID_DISC;
294 ccb->cts.xport_specific.spi.flags = CTS_SPI_FLAGS_DISC_ENB;
295 ccb->cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
296 ccb->cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
297 ccb->ccb_h.status = CAM_REQ_CMP;
301 case XPT_SET_TRAN_SETTINGS:
303 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
307 case XPT_CALC_GEOMETRY:
309 cam_calc_geometry(&ccb->ccg, 1);
315 ccb->cpi.version_num = 1;
316 ccb->cpi.hba_inquiry = 0;
317 ccb->cpi.target_sprt = 0;
318 ccb->cpi.hba_misc = 0;
319 ccb->cpi.hba_eng_cnt = 0;
320 ccb->cpi.max_lun = MRSAS_SCSI_MAX_LUNS;
321 ccb->cpi.unit_number = cam_sim_unit(sim);
322 ccb->cpi.bus_id = cam_sim_bus(sim);
323 ccb->cpi.initiator_id = MRSAS_SCSI_INITIATOR_ID;
324 ccb->cpi.base_transfer_speed = 150000;
325 strncpy(ccb->cpi.sim_vid, "FreeBSD", SIM_IDLEN);
326 strncpy(ccb->cpi.hba_vid, "LSI", HBA_IDLEN);
327 strncpy(ccb->cpi.dev_name, cam_sim_name(sim), DEV_IDLEN);
328 ccb->cpi.transport = XPORT_SPI;
329 ccb->cpi.transport_version = 2;
330 ccb->cpi.protocol = PROTO_SCSI;
331 ccb->cpi.protocol_version = SCSI_REV_2;
332 if (ccb->cpi.bus_id == 0)
333 ccb->cpi.max_target = MRSAS_MAX_PD - 1;
335 ccb->cpi.max_target = MRSAS_MAX_LD_IDS - 1;
336 #if (__FreeBSD_version > 704000)
337 ccb->cpi.maxio = MRSAS_MAX_IO_SIZE;
339 ccb->ccb_h.status = CAM_REQ_CMP;
345 ccb->ccb_h.status = CAM_REQ_INVALID;
353 * mrsas_scsiio_timeout: Callback function for IO timed out
354 * input: mpt command context
356 * This function will execute after timeout value provided by ccb header from
357 * CAM layer, if timer expires. Driver will run timer for all DCDM and LDIO
358 * comming from CAM layer. This function is callback function for IO timeout
359 * and it runs in no-sleep context. Set do_timedout_reset in Adapter context
360 * so that it will execute OCR/Kill adpter from ocr_thread context.
363 mrsas_scsiio_timeout(void *data)
365 struct mrsas_mpt_cmd *cmd;
366 struct mrsas_softc *sc;
368 cmd = (struct mrsas_mpt_cmd *)data;
371 if (cmd->ccb_ptr == NULL) {
372 printf("command timeout with NULL ccb\n");
376 * Below callout is dummy entry so that it will be cancelled from
377 * mrsas_cmd_done(). Now Controller will go to OCR/Kill Adapter based
378 * on OCR enable/disable property of Controller from ocr_thread
381 callout_reset(&cmd->cm_callout, (600000 * hz) / 1000,
382 mrsas_scsiio_timeout, cmd);
383 sc->do_timedout_reset = 1;
384 if (sc->ocr_thread_active)
385 wakeup(&sc->ocr_chan);
389 * mrsas_startio: SCSI IO entry point
390 * input: Adapter instance soft state
391 * pointer to CAM Control Block
393 * This function is the SCSI IO entry point and it initiates IO processing. It
394 * copies the IO and depending if the IO is read/write or inquiry, it would
395 * call mrsas_build_ldio() or mrsas_build_dcdb(), respectively. It returns 0
396 * if the command is sent to firmware successfully, otherwise it returns 1.
399 mrsas_startio(struct mrsas_softc *sc, struct cam_sim *sim,
402 struct mrsas_mpt_cmd *cmd;
403 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
404 struct ccb_scsiio *csio = &(ccb->csio);
405 MRSAS_REQUEST_DESCRIPTOR_UNION *req_desc;
407 if ((csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
408 ccb->ccb_h.status = CAM_REQ_CMP;
412 ccb_h->status |= CAM_SIM_QUEUED;
413 cmd = mrsas_get_mpt_cmd(sc);
416 ccb_h->status |= CAM_REQUEUE_REQ;
420 if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
421 if (ccb_h->flags & CAM_DIR_IN)
422 cmd->flags |= MRSAS_DIR_IN;
423 if (ccb_h->flags & CAM_DIR_OUT)
424 cmd->flags |= MRSAS_DIR_OUT;
426 cmd->flags = MRSAS_DIR_NONE; /* no data */
428 /* For FreeBSD 10.0 and higher */
429 #if (__FreeBSD_version >= 1000000)
431 * XXX We don't yet support physical addresses here.
433 switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) {
435 case CAM_DATA_SG_PADDR:
436 device_printf(sc->mrsas_dev, "%s: physical addresses not supported\n",
438 mrsas_release_mpt_cmd(cmd);
439 ccb_h->status = CAM_REQ_INVALID;
440 ccb_h->status &= ~CAM_SIM_QUEUED;
443 device_printf(sc->mrsas_dev, "%s: scatter gather is not supported\n",
445 mrsas_release_mpt_cmd(cmd);
446 ccb_h->status = CAM_REQ_INVALID;
449 if (csio->dxfer_len > MRSAS_MAX_IO_SIZE) {
450 mrsas_release_mpt_cmd(cmd);
451 ccb_h->status = CAM_REQ_TOO_BIG;
454 cmd->length = csio->dxfer_len;
456 cmd->data = csio->data_ptr;
459 ccb->ccb_h.status = CAM_REQ_INVALID;
463 if (!(ccb_h->flags & CAM_DATA_PHYS)) { /* Virtual data address */
464 if (!(ccb_h->flags & CAM_SCATTER_VALID)) {
465 if (csio->dxfer_len > MRSAS_MAX_IO_SIZE) {
466 mrsas_release_mpt_cmd(cmd);
467 ccb_h->status = CAM_REQ_TOO_BIG;
470 cmd->length = csio->dxfer_len;
472 cmd->data = csio->data_ptr;
474 mrsas_release_mpt_cmd(cmd);
475 ccb_h->status = CAM_REQ_INVALID;
478 } else { /* Data addresses are physical. */
479 mrsas_release_mpt_cmd(cmd);
480 ccb_h->status = CAM_REQ_INVALID;
481 ccb_h->status &= ~CAM_SIM_QUEUED;
488 req_desc = mrsas_get_request_desc(sc, (cmd->index) - 1);
490 device_printf(sc->mrsas_dev, "Cannot get request_descriptor.\n");
493 memset(req_desc, 0, sizeof(MRSAS_REQUEST_DESCRIPTOR_UNION));
494 cmd->request_desc = req_desc;
496 if (ccb_h->flags & CAM_CDB_POINTER)
497 bcopy(csio->cdb_io.cdb_ptr, cmd->io_request->CDB.CDB32, csio->cdb_len);
499 bcopy(csio->cdb_io.cdb_bytes, cmd->io_request->CDB.CDB32, csio->cdb_len);
500 mtx_lock(&sc->raidmap_lock);
502 if (mrsas_ldio_inq(sim, ccb)) {
503 if (mrsas_build_ldio(sc, cmd, ccb)) {
504 device_printf(sc->mrsas_dev, "Build LDIO failed.\n");
505 mtx_unlock(&sc->raidmap_lock);
509 if (mrsas_build_dcdb(sc, cmd, ccb, sim)) {
510 device_printf(sc->mrsas_dev, "Build DCDB failed.\n");
511 mtx_unlock(&sc->raidmap_lock);
515 mtx_unlock(&sc->raidmap_lock);
517 if (cmd->flags == MRSAS_DIR_IN) /* from device */
518 cmd->io_request->Control |= MPI2_SCSIIO_CONTROL_READ;
519 else if (cmd->flags == MRSAS_DIR_OUT) /* to device */
520 cmd->io_request->Control |= MPI2_SCSIIO_CONTROL_WRITE;
522 cmd->io_request->SGLFlags = MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
523 cmd->io_request->SGLOffset0 = offsetof(MRSAS_RAID_SCSI_IO_REQUEST, SGL) / 4;
524 cmd->io_request->SenseBufferLowAddress = cmd->sense_phys_addr;
525 cmd->io_request->SenseBufferLength = MRSAS_SCSI_SENSE_BUFFERSIZE;
527 req_desc = cmd->request_desc;
528 req_desc->SCSIIO.SMID = cmd->index;
531 * Start timer for IO timeout. Default timeout value is 90 second.
533 callout_reset(&cmd->cm_callout, (sc->mrsas_io_timeout * hz) / 1000,
534 mrsas_scsiio_timeout, cmd);
535 mrsas_atomic_inc(&sc->fw_outstanding);
537 if (mrsas_atomic_read(&sc->fw_outstanding) > sc->io_cmds_highwater)
538 sc->io_cmds_highwater++;
540 mrsas_fire_cmd(sc, req_desc->addr.u.low, req_desc->addr.u.high);
549 * mrsas_ldio_inq: Determines if IO is read/write or inquiry
550 * input: pointer to CAM Control Block
552 * This function determines if the IO is read/write or inquiry. It returns a 1
553 * if the IO is read/write and 0 if it is inquiry.
556 mrsas_ldio_inq(struct cam_sim *sim, union ccb *ccb)
558 struct ccb_scsiio *csio = &(ccb->csio);
560 if (cam_sim_bus(sim) == 1)
563 switch (csio->cdb_io.cdb_bytes[0]) {
579 * mrsas_get_mpt_cmd: Get a cmd from free command pool
580 * input: Adapter instance soft state
582 * This function removes an MPT command from the command free list and
585 struct mrsas_mpt_cmd *
586 mrsas_get_mpt_cmd(struct mrsas_softc *sc)
588 struct mrsas_mpt_cmd *cmd = NULL;
590 mtx_lock(&sc->mpt_cmd_pool_lock);
591 if (!TAILQ_EMPTY(&sc->mrsas_mpt_cmd_list_head)) {
592 cmd = TAILQ_FIRST(&sc->mrsas_mpt_cmd_list_head);
593 TAILQ_REMOVE(&sc->mrsas_mpt_cmd_list_head, cmd, next);
595 memset((uint8_t *)cmd->io_request, 0, MRSAS_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
600 cmd->load_balance = 0;
602 mtx_unlock(&sc->mpt_cmd_pool_lock);
608 * mrsas_release_mpt_cmd: Return a cmd to free command pool
609 * input: Command packet for return to free command pool
611 * This function returns an MPT command to the free command list.
614 mrsas_release_mpt_cmd(struct mrsas_mpt_cmd *cmd)
616 struct mrsas_softc *sc = cmd->sc;
618 mtx_lock(&sc->mpt_cmd_pool_lock);
619 cmd->sync_cmd_idx = (u_int32_t)MRSAS_ULONG_MAX;
620 TAILQ_INSERT_TAIL(&(sc->mrsas_mpt_cmd_list_head), cmd, next);
621 mtx_unlock(&sc->mpt_cmd_pool_lock);
627 * mrsas_get_request_desc: Get request descriptor from array
628 * input: Adapter instance soft state
631 * This function returns a pointer to the request descriptor.
633 MRSAS_REQUEST_DESCRIPTOR_UNION *
634 mrsas_get_request_desc(struct mrsas_softc *sc, u_int16_t index)
638 if (index >= sc->max_fw_cmds) {
639 device_printf(sc->mrsas_dev, "Invalid SMID (0x%x)request for desc\n", index);
642 p = sc->req_desc + sizeof(MRSAS_REQUEST_DESCRIPTOR_UNION) * index;
644 return (MRSAS_REQUEST_DESCRIPTOR_UNION *) p;
648 * mrsas_build_ldio: Builds an LDIO command
649 * input: Adapter instance soft state
650 * Pointer to command packet
653 * This function builds the LDIO command packet. It returns 0 if the command is
654 * built successfully, otherwise it returns a 1.
657 mrsas_build_ldio(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
660 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
661 struct ccb_scsiio *csio = &(ccb->csio);
663 MRSAS_RAID_SCSI_IO_REQUEST *io_request;
665 device_id = ccb_h->target_id;
667 io_request = cmd->io_request;
668 io_request->RaidContext.VirtualDiskTgtId = device_id;
669 io_request->RaidContext.status = 0;
670 io_request->RaidContext.exStatus = 0;
672 /* just the cdb len, other flags zero, and ORed-in later for FP */
673 io_request->IoFlags = csio->cdb_len;
675 if (mrsas_setup_io(sc, cmd, ccb, device_id, io_request) != SUCCESS)
676 device_printf(sc->mrsas_dev, "Build ldio or fpio error\n");
678 io_request->DataLength = cmd->length;
680 if (mrsas_map_request(sc, cmd) == SUCCESS) {
681 if (cmd->sge_count > MRSAS_MAX_SGL) {
682 device_printf(sc->mrsas_dev, "Error: sge_count (0x%x) exceeds"
683 "max (0x%x) allowed\n", cmd->sge_count, sc->max_num_sge);
686 io_request->RaidContext.numSGE = cmd->sge_count;
688 device_printf(sc->mrsas_dev, "Data map/load failed.\n");
695 * mrsas_setup_io: Set up data including Fast Path I/O
696 * input: Adapter instance soft state
697 * Pointer to command packet
700 * This function builds the DCDB inquiry command. It returns 0 if the command
701 * is built successfully, otherwise it returns a 1.
704 mrsas_setup_io(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
705 union ccb *ccb, u_int32_t device_id,
706 MRSAS_RAID_SCSI_IO_REQUEST * io_request)
708 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
709 struct ccb_scsiio *csio = &(ccb->csio);
710 struct IO_REQUEST_INFO io_info;
711 MR_DRV_RAID_MAP_ALL *map_ptr;
712 u_int8_t fp_possible;
713 u_int32_t start_lba_hi, start_lba_lo, ld_block_size;
714 u_int32_t datalength = 0;
721 * READ_6 (0x08) or WRITE_6 (0x0A) cdb
723 if (csio->cdb_len == 6) {
724 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[4];
725 start_lba_lo = ((u_int32_t)csio->cdb_io.cdb_bytes[1] << 16) |
726 ((u_int32_t)csio->cdb_io.cdb_bytes[2] << 8) |
727 (u_int32_t)csio->cdb_io.cdb_bytes[3];
728 start_lba_lo &= 0x1FFFFF;
731 * READ_10 (0x28) or WRITE_6 (0x2A) cdb
733 else if (csio->cdb_len == 10) {
734 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[8] |
735 ((u_int32_t)csio->cdb_io.cdb_bytes[7] << 8);
736 start_lba_lo = ((u_int32_t)csio->cdb_io.cdb_bytes[2] << 24) |
737 ((u_int32_t)csio->cdb_io.cdb_bytes[3] << 16) |
738 (u_int32_t)csio->cdb_io.cdb_bytes[4] << 8 |
739 ((u_int32_t)csio->cdb_io.cdb_bytes[5]);
742 * READ_12 (0xA8) or WRITE_12 (0xAA) cdb
744 else if (csio->cdb_len == 12) {
745 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[6] << 24 |
746 ((u_int32_t)csio->cdb_io.cdb_bytes[7] << 16) |
747 ((u_int32_t)csio->cdb_io.cdb_bytes[8] << 8) |
748 ((u_int32_t)csio->cdb_io.cdb_bytes[9]);
749 start_lba_lo = ((u_int32_t)csio->cdb_io.cdb_bytes[2] << 24) |
750 ((u_int32_t)csio->cdb_io.cdb_bytes[3] << 16) |
751 (u_int32_t)csio->cdb_io.cdb_bytes[4] << 8 |
752 ((u_int32_t)csio->cdb_io.cdb_bytes[5]);
755 * READ_16 (0x88) or WRITE_16 (0xx8A) cdb
757 else if (csio->cdb_len == 16) {
758 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[10] << 24 |
759 ((u_int32_t)csio->cdb_io.cdb_bytes[11] << 16) |
760 ((u_int32_t)csio->cdb_io.cdb_bytes[12] << 8) |
761 ((u_int32_t)csio->cdb_io.cdb_bytes[13]);
762 start_lba_lo = ((u_int32_t)csio->cdb_io.cdb_bytes[6] << 24) |
763 ((u_int32_t)csio->cdb_io.cdb_bytes[7] << 16) |
764 (u_int32_t)csio->cdb_io.cdb_bytes[8] << 8 |
765 ((u_int32_t)csio->cdb_io.cdb_bytes[9]);
766 start_lba_hi = ((u_int32_t)csio->cdb_io.cdb_bytes[2] << 24) |
767 ((u_int32_t)csio->cdb_io.cdb_bytes[3] << 16) |
768 (u_int32_t)csio->cdb_io.cdb_bytes[4] << 8 |
769 ((u_int32_t)csio->cdb_io.cdb_bytes[5]);
771 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
772 io_info.ldStartBlock = ((u_int64_t)start_lba_hi << 32) | start_lba_lo;
773 io_info.numBlocks = datalength;
774 io_info.ldTgtId = device_id;
776 switch (ccb_h->flags & CAM_DIR_MASK) {
785 mrsas_dprint(sc, MRSAS_TRACE, "From %s : DMA Flag is %d \n", __func__, ccb_h->flags & CAM_DIR_MASK);
789 map_ptr = sc->ld_drv_map[(sc->map_id & 1)];
790 ld_block_size = MR_LdBlockSizeGet(device_id, map_ptr, sc);
792 if ((MR_TargetIdToLdGet(device_id, map_ptr) >= MAX_LOGICAL_DRIVES_EXT) ||
793 (!sc->fast_path_io)) {
794 io_request->RaidContext.regLockFlags = 0;
797 if (MR_BuildRaidContext(sc, &io_info, &io_request->RaidContext, map_ptr))
798 fp_possible = io_info.fpOkForIo;
801 cmd->request_desc->SCSIIO.MSIxIndex =
802 sc->msix_vectors ? smp_processor_id() % sc->msix_vectors : 0;
806 mrsas_set_pd_lba(io_request, csio->cdb_len, &io_info, ccb, map_ptr,
807 start_lba_lo, ld_block_size);
808 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
809 cmd->request_desc->SCSIIO.RequestFlags =
810 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
811 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
812 if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY)) {
813 if (io_request->RaidContext.regLockFlags == REGION_TYPE_UNUSED)
814 cmd->request_desc->SCSIIO.RequestFlags =
815 (MRSAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
816 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
817 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
818 io_request->RaidContext.nseg = 0x1;
819 io_request->IoFlags |= MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
820 io_request->RaidContext.regLockFlags |=
821 (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
822 MR_RL_FLAGS_SEQ_NUM_ENABLE);
824 if ((sc->load_balance_info[device_id].loadBalanceFlag) &&
827 mrsas_get_updated_dev_handle(&sc->load_balance_info[device_id],
829 cmd->load_balance = MRSAS_LOAD_BALANCE_FLAG;
831 cmd->load_balance = 0;
832 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
833 io_request->DevHandle = io_info.devHandle;
836 io_request->RaidContext.timeoutValue = map_ptr->raidMap.fpPdIoTimeoutSec;
837 cmd->request_desc->SCSIIO.RequestFlags =
838 (MRSAS_REQ_DESCRIPT_FLAGS_LD_IO <<
839 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
840 if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY)) {
841 if (io_request->RaidContext.regLockFlags == REGION_TYPE_UNUSED)
842 cmd->request_desc->SCSIIO.RequestFlags =
843 (MRSAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
844 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
845 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
846 io_request->RaidContext.regLockFlags |=
847 (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
848 MR_RL_FLAGS_SEQ_NUM_ENABLE);
849 io_request->RaidContext.nseg = 0x1;
851 io_request->Function = MRSAS_MPI2_FUNCTION_LD_IO_REQUEST;
852 io_request->DevHandle = device_id;
858 * mrsas_build_dcdb: Builds an DCDB command
859 * input: Adapter instance soft state
860 * Pointer to command packet
863 * This function builds the DCDB inquiry command. It returns 0 if the command
864 * is built successfully, otherwise it returns a 1.
867 mrsas_build_dcdb(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
868 union ccb *ccb, struct cam_sim *sim)
870 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
872 MR_DRV_RAID_MAP_ALL *map_ptr;
873 MRSAS_RAID_SCSI_IO_REQUEST *io_request;
875 io_request = cmd->io_request;
876 device_id = ccb_h->target_id;
877 map_ptr = sc->ld_drv_map[(sc->map_id & 1)];
879 /* Check if this is for system PD */
880 if (cam_sim_bus(sim) == 1 &&
881 sc->pd_list[device_id].driveState == MR_PD_STATE_SYSTEM) {
882 io_request->Function = 0;
883 io_request->DevHandle = map_ptr->raidMap.devHndlInfo[device_id].
885 io_request->RaidContext.timeoutValue = map_ptr->raidMap.fpPdIoTimeoutSec;
886 io_request->RaidContext.regLockFlags = 0;
887 io_request->RaidContext.regLockRowLBA = 0;
888 io_request->RaidContext.regLockLength = 0;
890 io_request->RaidContext.RAIDFlags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
891 << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
892 if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY))
893 io_request->IoFlags |= MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
894 cmd->request_desc->SCSIIO.RequestFlags =
895 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
896 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
897 cmd->request_desc->SCSIIO.DevHandle =
898 map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
899 cmd->request_desc->SCSIIO.MSIxIndex =
900 sc->msix_vectors ? smp_processor_id() % sc->msix_vectors : 0;
903 io_request->Function = MRSAS_MPI2_FUNCTION_LD_IO_REQUEST;
904 io_request->DevHandle = device_id;
905 cmd->request_desc->SCSIIO.RequestFlags =
906 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
907 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
910 io_request->RaidContext.VirtualDiskTgtId = device_id;
911 io_request->LUN[1] = ccb_h->target_lun & 0xF;
912 io_request->DataLength = cmd->length;
914 if (mrsas_map_request(sc, cmd) == SUCCESS) {
915 if (cmd->sge_count > sc->max_num_sge) {
916 device_printf(sc->mrsas_dev, "Error: sge_count (0x%x) exceeds"
917 "max (0x%x) allowed\n", cmd->sge_count, sc->max_num_sge);
920 io_request->RaidContext.numSGE = cmd->sge_count;
922 device_printf(sc->mrsas_dev, "Data map/load failed.\n");
929 * mrsas_map_request: Map and load data
930 * input: Adapter instance soft state
931 * Pointer to command packet
933 * For data from OS, map and load the data buffer into bus space. The SG list
934 * is built in the callback. If the bus dmamap load is not successful,
935 * cmd->error_code will contain the error code and a 1 is returned.
938 mrsas_map_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd)
940 u_int32_t retcode = 0;
942 int flag = BUS_DMA_NOWAIT;
944 sim = xpt_path_sim(cmd->ccb_ptr->ccb_h.path);
946 if (cmd->data != NULL) {
947 mtx_lock(&sc->io_lock);
948 /* Map data buffer into bus space */
949 retcode = bus_dmamap_load(sc->data_tag, cmd->data_dmamap, cmd->data,
950 cmd->length, mrsas_data_load_cb, cmd, flag);
951 mtx_unlock(&sc->io_lock);
953 device_printf(sc->mrsas_dev, "bus_dmamap_load(): retcode = %d\n", retcode);
954 if (retcode == EINPROGRESS) {
955 device_printf(sc->mrsas_dev, "request load in progress\n");
956 mrsas_freeze_simq(cmd, sim);
965 * mrsas_unmap_request: Unmap and unload data
966 * input: Adapter instance soft state
967 * Pointer to command packet
969 * This function unmaps and unloads data from OS.
972 mrsas_unmap_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd)
974 if (cmd->data != NULL) {
975 if (cmd->flags & MRSAS_DIR_IN)
976 bus_dmamap_sync(sc->data_tag, cmd->data_dmamap, BUS_DMASYNC_POSTREAD);
977 if (cmd->flags & MRSAS_DIR_OUT)
978 bus_dmamap_sync(sc->data_tag, cmd->data_dmamap, BUS_DMASYNC_POSTWRITE);
979 mtx_lock(&sc->io_lock);
980 bus_dmamap_unload(sc->data_tag, cmd->data_dmamap);
981 mtx_unlock(&sc->io_lock);
986 * mrsas_data_load_cb: Callback entry point
987 * input: Pointer to command packet as argument
989 * Number of segments Error
991 * This is the callback function of the bus dma map load. It builds the SG
995 mrsas_data_load_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
997 struct mrsas_mpt_cmd *cmd = (struct mrsas_mpt_cmd *)arg;
998 struct mrsas_softc *sc = cmd->sc;
999 MRSAS_RAID_SCSI_IO_REQUEST *io_request;
1000 pMpi25IeeeSgeChain64_t sgl_ptr;
1001 int i = 0, sg_processed = 0;
1004 cmd->error_code = error;
1005 device_printf(sc->mrsas_dev, "mrsas_data_load_cb: error=%d\n", error);
1006 if (error == EFBIG) {
1007 cmd->ccb_ptr->ccb_h.status = CAM_REQ_TOO_BIG;
1011 if (cmd->flags & MRSAS_DIR_IN)
1012 bus_dmamap_sync(cmd->sc->data_tag, cmd->data_dmamap,
1013 BUS_DMASYNC_PREREAD);
1014 if (cmd->flags & MRSAS_DIR_OUT)
1015 bus_dmamap_sync(cmd->sc->data_tag, cmd->data_dmamap,
1016 BUS_DMASYNC_PREWRITE);
1017 if (nseg > sc->max_num_sge) {
1018 device_printf(sc->mrsas_dev, "SGE count is too large or 0.\n");
1021 io_request = cmd->io_request;
1022 sgl_ptr = (pMpi25IeeeSgeChain64_t)&io_request->SGL;
1024 if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY)) {
1025 pMpi25IeeeSgeChain64_t sgl_ptr_end = sgl_ptr;
1027 sgl_ptr_end += sc->max_sge_in_main_msg - 1;
1028 sgl_ptr_end->Flags = 0;
1031 for (i = 0; i < nseg; i++) {
1032 sgl_ptr->Address = segs[i].ds_addr;
1033 sgl_ptr->Length = segs[i].ds_len;
1035 if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY)) {
1037 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1040 sg_processed = i + 1;
1041 if ((sg_processed == (sc->max_sge_in_main_msg - 1)) &&
1042 (nseg > sc->max_sge_in_main_msg)) {
1043 pMpi25IeeeSgeChain64_t sg_chain;
1045 if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY)) {
1046 if ((cmd->io_request->IoFlags & MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1047 != MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1048 cmd->io_request->ChainOffset = sc->chain_offset_io_request;
1050 cmd->io_request->ChainOffset = 0;
1052 cmd->io_request->ChainOffset = sc->chain_offset_io_request;
1054 if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY))
1055 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1057 sg_chain->Flags = (IEEE_SGE_FLAGS_CHAIN_ELEMENT | MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1058 sg_chain->Length = (sizeof(MPI2_SGE_IO_UNION) * (nseg - sg_processed));
1059 sg_chain->Address = cmd->chain_frame_phys_addr;
1060 sgl_ptr = (pMpi25IeeeSgeChain64_t)cmd->chain_frame;
1064 cmd->sge_count = nseg;
1068 * mrsas_freeze_simq: Freeze SIM queue
1069 * input: Pointer to command packet
1072 * This function freezes the sim queue.
1075 mrsas_freeze_simq(struct mrsas_mpt_cmd *cmd, struct cam_sim *sim)
1077 union ccb *ccb = (union ccb *)(cmd->ccb_ptr);
1079 xpt_freeze_simq(sim, 1);
1080 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1081 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1085 mrsas_xpt_freeze(struct mrsas_softc *sc)
1087 xpt_freeze_simq(sc->sim_0, 1);
1088 xpt_freeze_simq(sc->sim_1, 1);
1092 mrsas_xpt_release(struct mrsas_softc *sc)
1094 xpt_release_simq(sc->sim_0, 1);
1095 xpt_release_simq(sc->sim_1, 1);
1099 * mrsas_cmd_done: Perform remaining command completion
1100 * input: Adapter instance soft state Pointer to command packet
1102 * This function calls ummap request and releases the MPT command.
1105 mrsas_cmd_done(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd)
1107 callout_stop(&cmd->cm_callout);
1108 mrsas_unmap_request(sc, cmd);
1109 mtx_lock(&sc->sim_lock);
1110 xpt_done(cmd->ccb_ptr);
1111 cmd->ccb_ptr = NULL;
1112 mtx_unlock(&sc->sim_lock);
1113 mrsas_release_mpt_cmd(cmd);
1117 * mrsas_cam_poll: Polling entry point
1118 * input: Pointer to SIM
1120 * This is currently a stub function.
1123 mrsas_cam_poll(struct cam_sim *sim)
1125 struct mrsas_softc *sc = (struct mrsas_softc *)cam_sim_softc(sim);
1127 mrsas_isr((void *)sc);
1131 * mrsas_bus_scan: Perform bus scan
1132 * input: Adapter instance soft state
1134 * This mrsas_bus_scan function is needed for FreeBSD 7.x. Also, it should not
1135 * be called in FreeBSD 8.x and later versions, where the bus scan is
1139 mrsas_bus_scan(struct mrsas_softc *sc)
1144 if ((ccb_0 = xpt_alloc_ccb()) == NULL) {
1147 if ((ccb_1 = xpt_alloc_ccb()) == NULL) {
1148 xpt_free_ccb(ccb_0);
1151 mtx_lock(&sc->sim_lock);
1152 if (xpt_create_path(&ccb_0->ccb_h.path, xpt_periph, cam_sim_path(sc->sim_0),
1153 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1154 xpt_free_ccb(ccb_0);
1155 xpt_free_ccb(ccb_1);
1156 mtx_unlock(&sc->sim_lock);
1159 if (xpt_create_path(&ccb_1->ccb_h.path, xpt_periph, cam_sim_path(sc->sim_1),
1160 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1161 xpt_free_ccb(ccb_0);
1162 xpt_free_ccb(ccb_1);
1163 mtx_unlock(&sc->sim_lock);
1166 mtx_unlock(&sc->sim_lock);
1174 * mrsas_bus_scan_sim: Perform bus scan per SIM
1175 * input: adapter instance soft state
1177 * This function will be called from Event handler on LD creation/deletion,
1181 mrsas_bus_scan_sim(struct mrsas_softc *sc, struct cam_sim *sim)
1185 if ((ccb = xpt_alloc_ccb()) == NULL) {
1188 mtx_lock(&sc->sim_lock);
1189 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(sim),
1190 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1192 mtx_unlock(&sc->sim_lock);
1195 mtx_unlock(&sc->sim_lock);