2 * Copyright (c) 2014, LSI Corp.
5 * Support: freebsdraid@lsi.com
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of the <ORGANIZATION> nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
29 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
31 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32 * POSSIBILITY OF SUCH DAMAGE.
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
39 #include "dev/mrsas/mrsas.h"
42 #include <cam/cam_ccb.h>
43 #include <cam/cam_sim.h>
44 #include <cam/cam_xpt_sim.h>
45 #include <cam/cam_debug.h>
46 #include <cam/cam_periph.h>
47 #include <cam/cam_xpt_periph.h>
49 #include <cam/scsi/scsi_all.h>
50 #include <cam/scsi/scsi_message.h>
51 #include <sys/taskqueue.h>
57 int mrsas_cam_attach(struct mrsas_softc *sc);
58 //int mrsas_ldio_inq(union ccb *ccb);
59 int mrsas_ldio_inq(struct cam_sim *sim, union ccb *ccb);
60 int mrsas_bus_scan(struct mrsas_softc *sc);
61 int mrsas_bus_scan_sim(struct mrsas_softc *sc, struct cam_sim *sim);
62 int mrsas_map_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd);
63 int mrsas_build_ldio(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
65 int mrsas_build_dcdb(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
66 union ccb *ccb, struct cam_sim *sim);
67 int mrsas_setup_io(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
68 union ccb *ccb, u_int32_t device_id,
69 MRSAS_RAID_SCSI_IO_REQUEST *io_request);
70 void mrsas_xpt_freeze(struct mrsas_softc *sc);
71 void mrsas_xpt_release(struct mrsas_softc *sc);
72 void mrsas_cam_detach(struct mrsas_softc *sc);
73 void mrsas_release_mpt_cmd(struct mrsas_mpt_cmd *cmd);
74 void mrsas_unmap_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd);
75 void mrsas_cmd_done(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd);
76 void mrsas_fire_cmd(struct mrsas_softc *sc, u_int32_t req_desc_lo,
77 u_int32_t req_desc_hi);
78 void mrsas_set_pd_lba(MRSAS_RAID_SCSI_IO_REQUEST *io_request, u_int8_t cdb_len,
79 struct IO_REQUEST_INFO *io_info, union ccb *ccb,
80 MR_FW_RAID_MAP_ALL *local_map_ptr, u_int32_t ref_tag,
81 u_int32_t ld_block_size);
82 static void mrsas_freeze_simq(struct mrsas_mpt_cmd *cmd, struct cam_sim *sim);
83 static void mrsas_poll(struct cam_sim *sim);
84 static void mrsas_action(struct cam_sim *sim, union ccb *ccb);
85 static void mrsas_scsiio_timeout(void *data);
86 static void mrsas_data_load_cb(void *arg, bus_dma_segment_t *segs,
88 static int32_t mrsas_startio(struct mrsas_softc *sc, struct cam_sim *sim,
90 struct mrsas_mpt_cmd * mrsas_get_mpt_cmd(struct mrsas_softc *sc);
91 MRSAS_REQUEST_DESCRIPTOR_UNION *mrsas_get_request_desc(struct mrsas_softc *sc,
94 extern u_int16_t MR_TargetIdToLdGet(u_int32_t ldTgtId, MR_FW_RAID_MAP_ALL *map);
95 extern u_int32_t MR_LdBlockSizeGet(u_int32_t ldTgtId, MR_FW_RAID_MAP_ALL *map,
96 struct mrsas_softc *sc);
97 extern void mrsas_isr(void *arg);
98 extern void mrsas_aen_handler(struct mrsas_softc *sc);
99 extern u_int8_t MR_BuildRaidContext(struct mrsas_softc *sc,
100 struct IO_REQUEST_INFO *io_info,RAID_CONTEXT *pRAID_Context,
101 MR_FW_RAID_MAP_ALL *map);
102 extern u_int16_t MR_LdSpanArrayGet(u_int32_t ld, u_int32_t span,
103 MR_FW_RAID_MAP_ALL *map);
104 extern u_int16_t mrsas_get_updated_dev_handle(PLD_LOAD_BALANCE_INFO lbInfo,
105 struct IO_REQUEST_INFO *io_info);
106 extern u_int8_t megasas_get_best_arm(PLD_LOAD_BALANCE_INFO lbInfo, u_int8_t arm,
107 u_int64_t block, u_int32_t count);
111 * mrsas_cam_attach: Main entry to CAM subsystem
112 * input: Adapter instance soft state
114 * This function is called from mrsas_attach() during initialization
115 * to perform SIM allocations and XPT bus registration. If the kernel
116 * version is 7.4 or earlier, it would also initiate a bus scan.
118 int mrsas_cam_attach(struct mrsas_softc *sc)
120 struct cam_devq *devq;
123 mrsas_cam_depth = sc->max_fw_cmds - MRSAS_INTERNAL_CMDS;
125 if ((devq = cam_simq_alloc(mrsas_cam_depth)) == NULL) {
126 device_printf(sc->mrsas_dev, "Cannot allocate SIM queue\n");
132 * Create SIM for bus 0 and register, also create path
134 sc->sim_0 = cam_sim_alloc(mrsas_action, mrsas_poll, "mrsas", sc,
135 device_get_unit(sc->mrsas_dev), &sc->sim_lock, mrsas_cam_depth,
136 mrsas_cam_depth, devq);
137 if (sc->sim_0 == NULL){
139 device_printf(sc->mrsas_dev, "Cannot register SIM\n");
142 /* Initialize taskqueue for Event Handling */
143 TASK_INIT(&sc->ev_task, 0, (void *)mrsas_aen_handler, sc);
144 sc->ev_tq = taskqueue_create("mrsas_taskq", M_NOWAIT | M_ZERO,
145 taskqueue_thread_enqueue, &sc->ev_tq);
147 /* Run the task queue with lowest priority */
148 taskqueue_start_threads(&sc->ev_tq, 1, 255, "%s taskq",
149 device_get_nameunit(sc->mrsas_dev));
150 mtx_lock(&sc->sim_lock);
151 if (xpt_bus_register(sc->sim_0, sc->mrsas_dev,0) != CAM_SUCCESS)
153 cam_sim_free(sc->sim_0, TRUE); // passing true frees the devq
154 mtx_unlock(&sc->sim_lock);
157 if (xpt_create_path(&sc->path_0, NULL, cam_sim_path(sc->sim_0),
159 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
160 xpt_bus_deregister(cam_sim_path(sc->sim_0));
161 cam_sim_free(sc->sim_0, TRUE); // passing true will free the devq
162 mtx_unlock(&sc->sim_lock);
165 mtx_unlock(&sc->sim_lock);
168 * Create SIM for bus 1 and register, also create path
170 sc->sim_1 = cam_sim_alloc(mrsas_action, mrsas_poll, "mrsas", sc,
171 device_get_unit(sc->mrsas_dev), &sc->sim_lock, mrsas_cam_depth,
172 mrsas_cam_depth, devq);
173 if (sc->sim_1 == NULL){
175 device_printf(sc->mrsas_dev, "Cannot register SIM\n");
179 mtx_lock(&sc->sim_lock);
180 if (xpt_bus_register(sc->sim_1, sc->mrsas_dev, 1) != CAM_SUCCESS){
181 cam_sim_free(sc->sim_1, TRUE); // passing true frees the devq
182 mtx_unlock(&sc->sim_lock);
185 if (xpt_create_path(&sc->path_1, NULL, cam_sim_path(sc->sim_1),
187 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
188 xpt_bus_deregister(cam_sim_path(sc->sim_1));
189 cam_sim_free(sc->sim_1, TRUE);
190 mtx_unlock(&sc->sim_lock);
193 mtx_unlock(&sc->sim_lock);
195 #if (__FreeBSD_version <= 704000)
196 if (mrsas_bus_scan(sc)){
197 device_printf(sc->mrsas_dev, "Error in bus scan.\n");
205 * mrsas_cam_detach: De-allocates and teardown CAM
206 * input: Adapter instance soft state
208 * De-registers and frees the paths and SIMs.
210 void mrsas_cam_detach(struct mrsas_softc *sc)
212 if (sc->ev_tq != NULL)
213 taskqueue_free(sc->ev_tq);
214 mtx_lock(&sc->sim_lock);
216 xpt_free_path(sc->path_0);
218 xpt_bus_deregister(cam_sim_path(sc->sim_0));
219 cam_sim_free(sc->sim_0, FALSE);
222 xpt_free_path(sc->path_1);
224 xpt_bus_deregister(cam_sim_path(sc->sim_1));
225 cam_sim_free(sc->sim_1, TRUE);
227 mtx_unlock(&sc->sim_lock);
231 * mrsas_action: SIM callback entry point
232 * input: pointer to SIM
233 * pointer to CAM Control Block
235 * This function processes CAM subsystem requests. The type of request is
236 * stored in ccb->ccb_h.func_code. The preprocessor #ifdef is necessary
237 * because ccb->cpi.maxio is not supported for FreeBSD version 7.4 or
240 static void mrsas_action(struct cam_sim *sim, union ccb *ccb)
242 struct mrsas_softc *sc = (struct mrsas_softc *)cam_sim_softc(sim);
243 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
246 switch (ccb->ccb_h.func_code) {
249 device_id = ccb_h->target_id;
252 * bus 0 is LD, bus 1 is for system-PD
254 if (cam_sim_bus(sim) == 1 &&
255 sc->pd_list[device_id].driveState != MR_PD_STATE_SYSTEM) {
256 ccb->ccb_h.status |= CAM_DEV_NOT_THERE;
260 if (mrsas_startio(sc, sim, ccb)){
261 ccb->ccb_h.status |= CAM_REQ_INVALID;
269 ccb->ccb_h.status = CAM_UA_ABORT;
278 case XPT_GET_TRAN_SETTINGS:
280 ccb->cts.protocol = PROTO_SCSI;
281 ccb->cts.protocol_version = SCSI_REV_2;
282 ccb->cts.transport = XPORT_SPI;
283 ccb->cts.transport_version = 2;
284 ccb->cts.xport_specific.spi.valid = CTS_SPI_VALID_DISC;
285 ccb->cts.xport_specific.spi.flags = CTS_SPI_FLAGS_DISC_ENB;
286 ccb->cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
287 ccb->cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
288 ccb->ccb_h.status = CAM_REQ_CMP;
292 case XPT_SET_TRAN_SETTINGS:
294 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
298 case XPT_CALC_GEOMETRY:
300 cam_calc_geometry(&ccb->ccg, 1);
306 ccb->cpi.version_num = 1;
307 ccb->cpi.hba_inquiry = 0;
308 ccb->cpi.target_sprt = 0;
309 ccb->cpi.hba_misc = 0;
310 ccb->cpi.hba_eng_cnt = 0;
311 ccb->cpi.max_lun = MRSAS_SCSI_MAX_LUNS;
312 ccb->cpi.unit_number = cam_sim_unit(sim);
313 ccb->cpi.bus_id = cam_sim_bus(sim);
314 ccb->cpi.initiator_id = MRSAS_SCSI_INITIATOR_ID;
315 ccb->cpi.base_transfer_speed = 150000;
316 strncpy(ccb->cpi.sim_vid, "FreeBSD", SIM_IDLEN);
317 strncpy(ccb->cpi.hba_vid, "LSI", HBA_IDLEN);
318 strncpy(ccb->cpi.dev_name, cam_sim_name(sim), DEV_IDLEN);
319 ccb->cpi.transport = XPORT_SPI;
320 ccb->cpi.transport_version = 2;
321 ccb->cpi.protocol = PROTO_SCSI;
322 ccb->cpi.protocol_version = SCSI_REV_2;
323 if (ccb->cpi.bus_id == 0)
324 ccb->cpi.max_target = MRSAS_MAX_LD-1;
326 ccb->cpi.max_target = MRSAS_MAX_PD-1;
327 #if (__FreeBSD_version > 704000)
328 ccb->cpi.maxio = MRSAS_MAX_IO_SIZE;
330 ccb->ccb_h.status = CAM_REQ_CMP;
336 ccb->ccb_h.status = CAM_REQ_INVALID;
344 * mrsas_scsiio_timeout Callback function for IO timed out
345 * input: mpt command context
347 * This function will execute after timeout value
348 * provided by ccb header from CAM layer, if timer expires.
349 * Driver will run timer for all DCDM and LDIO comming from CAM layer.
350 * This function is callback function for IO timeout and it runs in
351 * no-sleep context. Set do_timedout_reset in Adapter context so that
352 * it will execute OCR/Kill adpter from ocr_thread context.
355 mrsas_scsiio_timeout(void *data)
357 struct mrsas_mpt_cmd *cmd;
358 struct mrsas_softc *sc;
360 cmd = (struct mrsas_mpt_cmd *)data;
363 if (cmd->ccb_ptr == NULL) {
364 printf("command timeout with NULL ccb\n");
368 /* Below callout is dummy entry so that it will be
369 * cancelled from mrsas_cmd_done(). Now Controller will
370 * go to OCR/Kill Adapter based on OCR enable/disable
371 * property of Controller from ocr_thread context.
373 callout_reset(&cmd->cm_callout, (600000 * hz) / 1000,
374 mrsas_scsiio_timeout, cmd);
375 sc->do_timedout_reset = 1;
376 if(sc->ocr_thread_active)
377 wakeup(&sc->ocr_chan);
381 * mrsas_startio: SCSI IO entry point
382 * input: Adapter instance soft state
383 * pointer to CAM Control Block
385 * This function is the SCSI IO entry point and it initiates IO processing.
386 * It copies the IO and depending if the IO is read/write or inquiry, it would
387 * call mrsas_build_ldio() or mrsas_build_dcdb(), respectively. It returns
388 * 0 if the command is sent to firmware successfully, otherwise it returns 1.
390 static int32_t mrsas_startio(struct mrsas_softc *sc, struct cam_sim *sim,
393 struct mrsas_mpt_cmd *cmd;
394 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
395 struct ccb_scsiio *csio = &(ccb->csio);
396 MRSAS_REQUEST_DESCRIPTOR_UNION *req_desc;
398 if ((csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE){
399 ccb->ccb_h.status = CAM_REQ_CMP;
404 ccb_h->status |= CAM_SIM_QUEUED;
405 cmd = mrsas_get_mpt_cmd(sc);
408 ccb_h->status |= CAM_REQUEUE_REQ;
413 if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
414 if(ccb_h->flags & CAM_DIR_IN)
415 cmd->flags |= MRSAS_DIR_IN;
416 if(ccb_h->flags & CAM_DIR_OUT)
417 cmd->flags |= MRSAS_DIR_OUT;
420 cmd->flags = MRSAS_DIR_NONE; /* no data */
422 /* For FreeBSD 10.0 and higher */
423 #if (__FreeBSD_version >= 1000000)
425 * * XXX We don't yet support physical addresses here.
427 switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) {
429 case CAM_DATA_SG_PADDR:
430 printf("%s: physical addresses not supported\n",
432 mrsas_release_mpt_cmd(cmd);
433 ccb_h->status = CAM_REQ_INVALID;
434 ccb_h->status &= ~CAM_SIM_QUEUED;
437 printf("%s: scatter gather is not supported\n",
439 mrsas_release_mpt_cmd(cmd);
440 ccb_h->status = CAM_REQ_INVALID;
443 if (csio->dxfer_len > MRSAS_MAX_IO_SIZE) {
444 mrsas_release_mpt_cmd(cmd);
445 ccb_h->status = CAM_REQ_TOO_BIG;
448 cmd->length = csio->dxfer_len;
450 cmd->data = csio->data_ptr;
453 ccb->ccb_h.status = CAM_REQ_INVALID;
457 if (!(ccb_h->flags & CAM_DATA_PHYS)) { //Virtual data address
458 if (!(ccb_h->flags & CAM_SCATTER_VALID)) {
459 if (csio->dxfer_len > MRSAS_MAX_IO_SIZE) {
460 mrsas_release_mpt_cmd(cmd);
461 ccb_h->status = CAM_REQ_TOO_BIG;
464 cmd->length = csio->dxfer_len;
466 cmd->data = csio->data_ptr;
469 mrsas_release_mpt_cmd(cmd);
470 ccb_h->status = CAM_REQ_INVALID;
474 else { //Data addresses are physical.
475 mrsas_release_mpt_cmd(cmd);
476 ccb_h->status = CAM_REQ_INVALID;
477 ccb_h->status &= ~CAM_SIM_QUEUED;
484 req_desc = mrsas_get_request_desc(sc, (cmd->index)-1);
486 device_printf(sc->mrsas_dev, "Cannot get request_descriptor.\n");
489 memset(req_desc, 0, sizeof(MRSAS_REQUEST_DESCRIPTOR_UNION));
490 cmd->request_desc = req_desc;
492 if (ccb_h->flags & CAM_CDB_POINTER)
493 bcopy(csio->cdb_io.cdb_ptr, cmd->io_request->CDB.CDB32, csio->cdb_len);
495 bcopy(csio->cdb_io.cdb_bytes, cmd->io_request->CDB.CDB32, csio->cdb_len);
496 mtx_lock(&sc->raidmap_lock);
498 if (mrsas_ldio_inq(sim, ccb)) {
499 if (mrsas_build_ldio(sc, cmd, ccb)){
500 device_printf(sc->mrsas_dev, "Build LDIO failed.\n");
501 mtx_unlock(&sc->raidmap_lock);
506 if (mrsas_build_dcdb(sc, cmd, ccb, sim)) {
507 device_printf(sc->mrsas_dev, "Build DCDB failed.\n");
508 mtx_unlock(&sc->raidmap_lock);
512 mtx_unlock(&sc->raidmap_lock);
514 if (cmd->flags == MRSAS_DIR_IN) //from device
515 cmd->io_request->Control |= MPI2_SCSIIO_CONTROL_READ;
516 else if (cmd->flags == MRSAS_DIR_OUT) //to device
517 cmd->io_request->Control |= MPI2_SCSIIO_CONTROL_WRITE;
519 cmd->io_request->SGLFlags = MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
520 cmd->io_request->SGLOffset0 = offsetof(MRSAS_RAID_SCSI_IO_REQUEST, SGL)/4;
521 cmd->io_request->SenseBufferLowAddress = cmd->sense_phys_addr;
522 cmd->io_request->SenseBufferLength = MRSAS_SCSI_SENSE_BUFFERSIZE;
524 req_desc = cmd->request_desc;
525 req_desc->SCSIIO.SMID = cmd->index;
528 * Start timer for IO timeout. Default timeout value is 90 second.
530 callout_reset(&cmd->cm_callout, (sc->mrsas_io_timeout * hz) / 1000,
531 mrsas_scsiio_timeout, cmd);
532 atomic_inc(&sc->fw_outstanding);
534 if(atomic_read(&sc->fw_outstanding) > sc->io_cmds_highwater)
535 sc->io_cmds_highwater++;
537 mrsas_fire_cmd(sc, req_desc->addr.u.low, req_desc->addr.u.high);
546 * mrsas_ldio_inq: Determines if IO is read/write or inquiry
547 * input: pointer to CAM Control Block
549 * This function determines if the IO is read/write or inquiry. It returns a
550 * 1 if the IO is read/write and 0 if it is inquiry.
552 int mrsas_ldio_inq(struct cam_sim *sim, union ccb *ccb)
554 struct ccb_scsiio *csio = &(ccb->csio);
556 if (cam_sim_bus(sim) == 1)
559 switch (csio->cdb_io.cdb_bytes[0]) {
575 * mrsas_get_mpt_cmd: Get a cmd from free command pool
576 * input: Adapter instance soft state
578 * This function removes an MPT command from the command free list and
581 struct mrsas_mpt_cmd* mrsas_get_mpt_cmd(struct mrsas_softc *sc)
583 struct mrsas_mpt_cmd *cmd = NULL;
585 mtx_lock(&sc->mpt_cmd_pool_lock);
586 if (!TAILQ_EMPTY(&sc->mrsas_mpt_cmd_list_head)){
587 cmd = TAILQ_FIRST(&sc->mrsas_mpt_cmd_list_head);
588 TAILQ_REMOVE(&sc->mrsas_mpt_cmd_list_head, cmd, next);
590 memset((uint8_t *)cmd->io_request, 0, MRSAS_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
595 cmd->load_balance = 0;
597 mtx_unlock(&sc->mpt_cmd_pool_lock);
603 * mrsas_release_mpt_cmd: Return a cmd to free command pool
604 * input: Command packet for return to free command pool
606 * This function returns an MPT command to the free command list.
608 void mrsas_release_mpt_cmd(struct mrsas_mpt_cmd *cmd)
610 struct mrsas_softc *sc = cmd->sc;
612 mtx_lock(&sc->mpt_cmd_pool_lock);
613 cmd->sync_cmd_idx = (u_int32_t)MRSAS_ULONG_MAX;
614 TAILQ_INSERT_TAIL(&(sc->mrsas_mpt_cmd_list_head), cmd, next);
615 mtx_unlock(&sc->mpt_cmd_pool_lock);
621 * mrsas_get_request_desc: Get request descriptor from array
622 * input: Adapter instance soft state
625 * This function returns a pointer to the request descriptor.
627 MRSAS_REQUEST_DESCRIPTOR_UNION *
628 mrsas_get_request_desc(struct mrsas_softc *sc, u_int16_t index)
632 if (index >= sc->max_fw_cmds) {
633 device_printf(sc->mrsas_dev, "Invalid SMID (0x%x)request for desc\n", index);
636 p = sc->req_desc + sizeof(MRSAS_REQUEST_DESCRIPTOR_UNION) * index;
638 return (MRSAS_REQUEST_DESCRIPTOR_UNION *)p;
642 * mrsas_build_ldio: Builds an LDIO command
643 * input: Adapter instance soft state
644 * Pointer to command packet
647 * This function builds the LDIO command packet. It returns 0 if the
648 * command is built successfully, otherwise it returns a 1.
650 int mrsas_build_ldio(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
653 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
654 struct ccb_scsiio *csio = &(ccb->csio);
656 MRSAS_RAID_SCSI_IO_REQUEST *io_request;
658 device_id = ccb_h->target_id;
660 io_request = cmd->io_request;
661 io_request->RaidContext.VirtualDiskTgtId = device_id;
662 io_request->RaidContext.status = 0;
663 io_request->RaidContext.exStatus = 0;
665 /* just the cdb len, other flags zero, and ORed-in later for FP */
666 io_request->IoFlags = csio->cdb_len;
668 if (mrsas_setup_io(sc, cmd, ccb, device_id, io_request) != SUCCESS)
669 device_printf(sc->mrsas_dev, "Build ldio or fpio error\n");
671 io_request->DataLength = cmd->length;
673 if (mrsas_map_request(sc, cmd) == SUCCESS) {
674 if (cmd->sge_count > MRSAS_MAX_SGL) {
675 device_printf(sc->mrsas_dev, "Error: sge_count (0x%x) exceeds"
676 "max (0x%x) allowed\n", cmd->sge_count, sc->max_num_sge);
679 io_request->RaidContext.numSGE = cmd->sge_count;
682 device_printf(sc->mrsas_dev, "Data map/load failed.\n");
689 * mrsas_setup_io: Set up data including Fast Path I/O
690 * input: Adapter instance soft state
691 * Pointer to command packet
694 * This function builds the DCDB inquiry command. It returns 0 if the
695 * command is built successfully, otherwise it returns a 1.
697 int mrsas_setup_io(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
698 union ccb *ccb, u_int32_t device_id,
699 MRSAS_RAID_SCSI_IO_REQUEST *io_request)
701 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
702 struct ccb_scsiio *csio = &(ccb->csio);
703 struct IO_REQUEST_INFO io_info;
704 MR_FW_RAID_MAP_ALL *map_ptr;
705 u_int8_t fp_possible;
706 u_int32_t start_lba_hi, start_lba_lo, ld_block_size;
707 u_int32_t datalength = 0;
714 * READ_6 (0x08) or WRITE_6 (0x0A) cdb
716 if (csio->cdb_len == 6) {
717 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[4];
718 start_lba_lo = ((u_int32_t) csio->cdb_io.cdb_bytes[1] << 16) |
719 ((u_int32_t) csio->cdb_io.cdb_bytes[2] << 8) |
720 (u_int32_t) csio->cdb_io.cdb_bytes[3];
721 start_lba_lo &= 0x1FFFFF;
724 * READ_10 (0x28) or WRITE_6 (0x2A) cdb
726 else if (csio->cdb_len == 10) {
727 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[8] |
728 ((u_int32_t)csio->cdb_io.cdb_bytes[7] << 8);
729 start_lba_lo = ((u_int32_t) csio->cdb_io.cdb_bytes[2] << 24) |
730 ((u_int32_t) csio->cdb_io.cdb_bytes[3] << 16) |
731 (u_int32_t) csio->cdb_io.cdb_bytes[4] << 8 |
732 ((u_int32_t) csio->cdb_io.cdb_bytes[5]);
735 * READ_12 (0xA8) or WRITE_12 (0xAA) cdb
737 else if (csio->cdb_len == 12) {
738 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[6] << 24 |
739 ((u_int32_t)csio->cdb_io.cdb_bytes[7] << 16) |
740 ((u_int32_t)csio->cdb_io.cdb_bytes[8] << 8) |
741 ((u_int32_t)csio->cdb_io.cdb_bytes[9]);
742 start_lba_lo = ((u_int32_t) csio->cdb_io.cdb_bytes[2] << 24) |
743 ((u_int32_t) csio->cdb_io.cdb_bytes[3] << 16) |
744 (u_int32_t) csio->cdb_io.cdb_bytes[4] << 8 |
745 ((u_int32_t) csio->cdb_io.cdb_bytes[5]);
748 * READ_16 (0x88) or WRITE_16 (0xx8A) cdb
750 else if (csio->cdb_len == 16) {
751 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[10] << 24 |
752 ((u_int32_t)csio->cdb_io.cdb_bytes[11] << 16) |
753 ((u_int32_t)csio->cdb_io.cdb_bytes[12] << 8) |
754 ((u_int32_t)csio->cdb_io.cdb_bytes[13]);
755 start_lba_lo = ((u_int32_t) csio->cdb_io.cdb_bytes[6] << 24) |
756 ((u_int32_t) csio->cdb_io.cdb_bytes[7] << 16) |
757 (u_int32_t) csio->cdb_io.cdb_bytes[8] << 8 |
758 ((u_int32_t) csio->cdb_io.cdb_bytes[9]);
759 start_lba_hi = ((u_int32_t) csio->cdb_io.cdb_bytes[2] << 24) |
760 ((u_int32_t) csio->cdb_io.cdb_bytes[3] << 16) |
761 (u_int32_t) csio->cdb_io.cdb_bytes[4] << 8 |
762 ((u_int32_t) csio->cdb_io.cdb_bytes[5]);
765 memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
766 io_info.ldStartBlock = ((u_int64_t)start_lba_hi << 32) | start_lba_lo;
767 io_info.numBlocks = datalength;
768 io_info.ldTgtId = device_id;
770 switch (ccb_h->flags & CAM_DIR_MASK) {
779 mrsas_dprint(sc, MRSAS_TRACE, "From %s : DMA Flag is %d \n", __func__, ccb_h->flags & CAM_DIR_MASK);
783 map_ptr = sc->raidmap_mem[(sc->map_id & 1)];
784 ld_block_size = MR_LdBlockSizeGet(device_id, map_ptr, sc);
786 if ((MR_TargetIdToLdGet(device_id, map_ptr) >= MAX_LOGICAL_DRIVES) ||
787 (!sc->fast_path_io)) {
788 io_request->RaidContext.regLockFlags = 0;
793 if (MR_BuildRaidContext(sc, &io_info, &io_request->RaidContext, map_ptr))
794 fp_possible = io_info.fpOkForIo;
798 mrsas_set_pd_lba(io_request, csio->cdb_len, &io_info, ccb, map_ptr,
799 start_lba_lo, ld_block_size);
800 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
801 cmd->request_desc->SCSIIO.RequestFlags =
802 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
803 << MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
804 if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY)) {
805 if (io_request->RaidContext.regLockFlags == REGION_TYPE_UNUSED)
806 cmd->request_desc->SCSIIO.RequestFlags = (MRSAS_REQ_DESCRIPT_FLAGS_NO_LOCK << MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
807 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
808 io_request->RaidContext.nseg = 0x1;
809 io_request->IoFlags |= MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
810 io_request->RaidContext.regLockFlags |= (MR_RL_FLAGS_GRANT_DESTINATION_CUDA | MR_RL_FLAGS_SEQ_NUM_ENABLE);
812 if ((sc->load_balance_info[device_id].loadBalanceFlag) && (io_info.isRead)) {
813 io_info.devHandle = mrsas_get_updated_dev_handle(&sc->load_balance_info[device_id],
815 cmd->load_balance = MRSAS_LOAD_BALANCE_FLAG;
818 cmd->load_balance = 0;
819 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
820 io_request->DevHandle = io_info.devHandle;
824 io_request->RaidContext.timeoutValue = map_ptr->raidMap.fpPdIoTimeoutSec;
825 cmd->request_desc->SCSIIO.RequestFlags =
826 (MRSAS_REQ_DESCRIPT_FLAGS_LD_IO << MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
827 if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY)) {
828 if (io_request->RaidContext.regLockFlags == REGION_TYPE_UNUSED)
829 cmd->request_desc->SCSIIO.RequestFlags = (MRSAS_REQ_DESCRIPT_FLAGS_NO_LOCK << MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
830 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
831 io_request->RaidContext.regLockFlags |= (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 | MR_RL_FLAGS_SEQ_NUM_ENABLE);
832 io_request->RaidContext.nseg = 0x1;
834 io_request->Function = MRSAS_MPI2_FUNCTION_LD_IO_REQUEST;
835 io_request->DevHandle = device_id;
841 * mrsas_build_dcdb: Builds an DCDB command
842 * input: Adapter instance soft state
843 * Pointer to command packet
846 * This function builds the DCDB inquiry command. It returns 0 if the
847 * command is built successfully, otherwise it returns a 1.
849 int mrsas_build_dcdb(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
850 union ccb *ccb, struct cam_sim *sim)
852 struct ccb_hdr *ccb_h = &(ccb->ccb_h);
854 MR_FW_RAID_MAP_ALL *map_ptr;
855 MRSAS_RAID_SCSI_IO_REQUEST *io_request;
857 io_request = cmd->io_request;
858 device_id = ccb_h->target_id;
859 map_ptr = sc->raidmap_mem[(sc->map_id & 1)];
861 /* Check if this is for system PD */
862 if (cam_sim_bus(sim) == 1 &&
863 sc->pd_list[device_id].driveState == MR_PD_STATE_SYSTEM) {
864 io_request->Function = 0;
865 io_request->DevHandle = map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
866 io_request->RaidContext.timeoutValue = map_ptr->raidMap.fpPdIoTimeoutSec;
867 io_request->RaidContext.regLockFlags = 0;
868 io_request->RaidContext.regLockRowLBA = 0;
869 io_request->RaidContext.regLockLength = 0;
870 io_request->RaidContext.RAIDFlags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD <<
871 MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
872 if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY))
873 io_request->IoFlags |= MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
874 cmd->request_desc->SCSIIO.RequestFlags =
875 (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
876 MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
877 cmd->request_desc->SCSIIO.DevHandle =
878 map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
881 io_request->Function = MRSAS_MPI2_FUNCTION_LD_IO_REQUEST;
882 io_request->DevHandle = device_id;
883 cmd->request_desc->SCSIIO.RequestFlags =
884 (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO << MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
887 io_request->RaidContext.VirtualDiskTgtId = device_id;
888 io_request->LUN[1] = ccb_h->target_lun & 0xF;
889 io_request->DataLength = cmd->length;
891 if (mrsas_map_request(sc, cmd) == SUCCESS) {
892 if (cmd->sge_count > sc->max_num_sge) {
893 device_printf(sc->mrsas_dev, "Error: sge_count (0x%x) exceeds"
894 "max (0x%x) allowed\n", cmd->sge_count, sc->max_num_sge);
897 io_request->RaidContext.numSGE = cmd->sge_count;
900 device_printf(sc->mrsas_dev, "Data map/load failed.\n");
907 * mrsas_map_request: Map and load data
908 * input: Adapter instance soft state
909 * Pointer to command packet
911 * For data from OS, map and load the data buffer into bus space. The
912 * SG list is built in the callback. If the bus dmamap load is not
913 * successful, cmd->error_code will contain the error code and a 1 is
916 int mrsas_map_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd)
918 u_int32_t retcode = 0;
920 int flag = BUS_DMA_NOWAIT;
922 sim = xpt_path_sim(cmd->ccb_ptr->ccb_h.path);
924 if (cmd->data != NULL) {
925 mtx_lock(&sc->io_lock);
926 /* Map data buffer into bus space */
927 retcode = bus_dmamap_load(sc->data_tag, cmd->data_dmamap, cmd->data,
928 cmd->length, mrsas_data_load_cb, cmd, flag);
929 mtx_unlock(&sc->io_lock);
931 device_printf(sc->mrsas_dev, "bus_dmamap_load(): retcode = %d\n", retcode);
932 if (retcode == EINPROGRESS) {
933 device_printf(sc->mrsas_dev, "request load in progress\n");
934 mrsas_freeze_simq(cmd, sim);
943 * mrsas_unmap_request: Unmap and unload data
944 * input: Adapter instance soft state
945 * Pointer to command packet
947 * This function unmaps and unloads data from OS.
949 void mrsas_unmap_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd)
951 if (cmd->data != NULL) {
952 if (cmd->flags & MRSAS_DIR_IN)
953 bus_dmamap_sync(sc->data_tag, cmd->data_dmamap, BUS_DMASYNC_POSTREAD);
954 if (cmd->flags & MRSAS_DIR_OUT)
955 bus_dmamap_sync(sc->data_tag, cmd->data_dmamap, BUS_DMASYNC_POSTWRITE);
956 mtx_lock(&sc->io_lock);
957 bus_dmamap_unload(sc->data_tag, cmd->data_dmamap);
958 mtx_unlock(&sc->io_lock);
963 * mrsas_data_load_cb: Callback entry point
964 * input: Pointer to command packet as argument
969 * This is the callback function of the bus dma map load. It builds
973 mrsas_data_load_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
975 struct mrsas_mpt_cmd *cmd = (struct mrsas_mpt_cmd *)arg;
976 struct mrsas_softc *sc = cmd->sc;
977 MRSAS_RAID_SCSI_IO_REQUEST *io_request;
978 pMpi25IeeeSgeChain64_t sgl_ptr;
979 int i=0, sg_processed=0;
983 cmd->error_code = error;
984 device_printf(sc->mrsas_dev, "mrsas_data_load_cb: error=%d\n", error);
985 if (error == EFBIG) {
986 cmd->ccb_ptr->ccb_h.status = CAM_REQ_TOO_BIG;
991 if (cmd->flags & MRSAS_DIR_IN)
992 bus_dmamap_sync(cmd->sc->data_tag, cmd->data_dmamap,
993 BUS_DMASYNC_PREREAD);
994 if (cmd->flags & MRSAS_DIR_OUT)
995 bus_dmamap_sync(cmd->sc->data_tag, cmd->data_dmamap,
996 BUS_DMASYNC_PREWRITE);
997 if (nseg > sc->max_num_sge) {
998 device_printf(sc->mrsas_dev, "SGE count is too large or 0.\n");
1002 io_request = cmd->io_request;
1003 sgl_ptr = (pMpi25IeeeSgeChain64_t)&io_request->SGL;
1005 if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY)) {
1006 pMpi25IeeeSgeChain64_t sgl_ptr_end = sgl_ptr;
1007 sgl_ptr_end += sc->max_sge_in_main_msg - 1;
1008 sgl_ptr_end->Flags = 0;
1012 for (i=0; i < nseg; i++) {
1013 sgl_ptr->Address = segs[i].ds_addr;
1014 sgl_ptr->Length = segs[i].ds_len;
1016 if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY)) {
1018 sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1021 sg_processed = i + 1;
1023 * Prepare chain element
1025 if ((sg_processed == (sc->max_sge_in_main_msg - 1)) &&
1026 (nseg > sc->max_sge_in_main_msg)) {
1027 pMpi25IeeeSgeChain64_t sg_chain;
1028 if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY)) {
1029 if ((cmd->io_request->IoFlags & MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1030 != MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1031 cmd->io_request->ChainOffset = sc->chain_offset_io_request;
1033 cmd->io_request->ChainOffset = 0;
1035 cmd->io_request->ChainOffset = sc->chain_offset_io_request;
1037 if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY))
1038 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1040 sg_chain->Flags = (IEEE_SGE_FLAGS_CHAIN_ELEMENT | MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1041 sg_chain->Length = (sizeof(MPI2_SGE_IO_UNION) * (nseg - sg_processed));
1042 sg_chain->Address = cmd->chain_frame_phys_addr;
1043 sgl_ptr = (pMpi25IeeeSgeChain64_t)cmd->chain_frame;
1047 cmd->sge_count = nseg;
1051 * mrsas_freeze_simq: Freeze SIM queue
1052 * input: Pointer to command packet
1055 * This function freezes the sim queue.
1057 static void mrsas_freeze_simq(struct mrsas_mpt_cmd *cmd, struct cam_sim *sim)
1059 union ccb *ccb = (union ccb *)(cmd->ccb_ptr);
1061 xpt_freeze_simq(sim, 1);
1062 ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1063 ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1066 void mrsas_xpt_freeze(struct mrsas_softc *sc) {
1067 xpt_freeze_simq(sc->sim_0, 1);
1068 xpt_freeze_simq(sc->sim_1, 1);
1071 void mrsas_xpt_release(struct mrsas_softc *sc) {
1072 xpt_release_simq(sc->sim_0, 1);
1073 xpt_release_simq(sc->sim_1, 1);
1077 * mrsas_cmd_done: Perform remaining command completion
1078 * input: Adapter instance soft state
1079 * Pointer to command packet
1081 * This function calls ummap request and releases the MPT command.
1083 void mrsas_cmd_done(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd)
1085 callout_stop(&cmd->cm_callout);
1086 mrsas_unmap_request(sc, cmd);
1087 mtx_lock(&sc->sim_lock);
1088 xpt_done(cmd->ccb_ptr);
1089 cmd->ccb_ptr = NULL;
1090 mtx_unlock(&sc->sim_lock);
1091 mrsas_release_mpt_cmd(cmd);
1095 * mrsas_poll: Polling entry point
1096 * input: Pointer to SIM
1098 * This is currently a stub function.
1100 static void mrsas_poll(struct cam_sim *sim)
1102 struct mrsas_softc *sc = (struct mrsas_softc *)cam_sim_softc(sim);
1103 mrsas_isr((void *) sc);
1107 * mrsas_bus_scan: Perform bus scan
1108 * input: Adapter instance soft state
1110 * This mrsas_bus_scan function is needed for FreeBSD 7.x. Also, it should
1111 * not be called in FreeBSD 8.x and later versions, where the bus scan is
1114 int mrsas_bus_scan(struct mrsas_softc *sc)
1119 mtx_lock(&sc->sim_lock);
1120 if ((ccb_0 = xpt_alloc_ccb()) == NULL) {
1121 mtx_unlock(&sc->sim_lock);
1125 if ((ccb_1 = xpt_alloc_ccb()) == NULL) {
1126 xpt_free_ccb(ccb_0);
1127 mtx_unlock(&sc->sim_lock);
1131 if (xpt_create_path(&ccb_0->ccb_h.path, xpt_periph, cam_sim_path(sc->sim_0),
1132 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP){
1133 xpt_free_ccb(ccb_0);
1134 xpt_free_ccb(ccb_1);
1135 mtx_unlock(&sc->sim_lock);
1139 if (xpt_create_path(&ccb_1->ccb_h.path, xpt_periph, cam_sim_path(sc->sim_1),
1140 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP){
1141 xpt_free_ccb(ccb_0);
1142 xpt_free_ccb(ccb_1);
1143 mtx_unlock(&sc->sim_lock);
1149 mtx_unlock(&sc->sim_lock);
1155 * mrsas_bus_scan_sim: Perform bus scan per SIM
1156 * input: Adapter instance soft state
1157 * This function will be called from Event handler
1158 * on LD creation/deletion, JBOD on/off.
1160 int mrsas_bus_scan_sim(struct mrsas_softc *sc, struct cam_sim *sim)
1164 mtx_lock(&sc->sim_lock);
1165 if ((ccb = xpt_alloc_ccb()) == NULL) {
1166 mtx_unlock(&sc->sim_lock);
1169 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(sim),
1170 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP){
1172 mtx_unlock(&sc->sim_lock);
1176 mtx_unlock(&sc->sim_lock);