xref: /dragonfly/sys/dev/raid/mrsas/mrsas_cam.c (revision ffe53622)
1 /*
2  * Copyright (c) 2014, LSI Corp.
3  * All rights reserved.
4  * Author: Marian Choy
5  * Support: freebsdraid@lsi.com
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
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
16  *    distribution.
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.
20  *
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.
33  *
34  * $FreeBSD: head/sys/dev/mrsas/mrsas_cam.c 265555 2014-05-07 16:16:49Z ambrisko $
35  */
36 
37 #include <dev/raid/mrsas/mrsas.h>
38 
39 #include <bus/cam/cam.h>
40 #include <bus/cam/cam_ccb.h>
41 #include <bus/cam/cam_sim.h>
42 #include <bus/cam/cam_xpt_sim.h>
43 #include <bus/cam/cam_debug.h>
44 #include <bus/cam/cam_periph.h>
45 #include <bus/cam/cam_xpt_periph.h>
46 
47 #include <bus/cam/scsi/scsi_all.h>
48 #include <bus/cam/scsi/scsi_message.h>
49 #include <sys/taskqueue.h>
50 
51 
52 /*
53  * Function prototypes
54  */
55 int mrsas_cam_attach(struct mrsas_softc *sc);
56 //int mrsas_ldio_inq(union ccb *ccb);
57 int mrsas_ldio_inq(struct cam_sim *sim, union ccb *ccb);
58 int mrsas_bus_scan(struct mrsas_softc *sc);
59 int mrsas_bus_scan_sim(struct mrsas_softc *sc, struct cam_sim *sim);
60 int mrsas_map_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd);
61 int mrsas_build_ldio(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
62                     union ccb *ccb);
63 int mrsas_build_dcdb(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
64                     union ccb *ccb, struct cam_sim *sim);
65 int mrsas_setup_io(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
66                     union ccb *ccb, u_int32_t device_id,
67                     MRSAS_RAID_SCSI_IO_REQUEST *io_request);
68 void mrsas_xpt_freeze(struct mrsas_softc *sc);
69 void mrsas_xpt_release(struct mrsas_softc *sc);
70 void mrsas_cam_detach(struct mrsas_softc *sc);
71 void mrsas_release_mpt_cmd(struct mrsas_mpt_cmd *cmd);
72 void mrsas_unmap_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd);
73 void mrsas_cmd_done(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd);
74 void mrsas_fire_cmd(struct mrsas_softc *sc, u_int32_t req_desc_lo,
75                     u_int32_t req_desc_hi);
76 void mrsas_set_pd_lba(MRSAS_RAID_SCSI_IO_REQUEST *io_request, u_int8_t cdb_len,
77                     struct IO_REQUEST_INFO *io_info, union ccb *ccb,
78                     MR_FW_RAID_MAP_ALL *local_map_ptr, u_int32_t ref_tag,
79                     u_int32_t ld_block_size);
80 static void mrsas_freeze_simq(struct mrsas_mpt_cmd *cmd, struct cam_sim *sim);
81 static void mrsas_poll(struct cam_sim *sim);
82 static void mrsas_action(struct cam_sim *sim, union ccb *ccb);
83 static void mrsas_scsiio_timeout(void *data);
84 static void mrsas_data_load_cb(void *arg, bus_dma_segment_t *segs,
85                     int nseg, int error);
86 static int32_t mrsas_startio(struct mrsas_softc *sc, struct cam_sim *sim,
87                     union ccb *ccb);
88 static void mrsas_rescan_callback(struct cam_periph *, union ccb *);
89 struct mrsas_mpt_cmd * mrsas_get_mpt_cmd(struct mrsas_softc *sc);
90 MRSAS_REQUEST_DESCRIPTOR_UNION *mrsas_get_request_desc(struct mrsas_softc *sc,
91                     u_int16_t index);
92 
93 extern u_int16_t MR_TargetIdToLdGet(u_int32_t ldTgtId, MR_FW_RAID_MAP_ALL *map);
94 extern u_int32_t MR_LdBlockSizeGet(u_int32_t ldTgtId, MR_FW_RAID_MAP_ALL *map,
95     struct mrsas_softc *sc);
96 extern void mrsas_isr(void *arg);
97 extern void mrsas_aen_handler(struct mrsas_softc *sc);
98 extern u_int8_t MR_BuildRaidContext(struct mrsas_softc *sc,
99         struct IO_REQUEST_INFO *io_info,RAID_CONTEXT *pRAID_Context,
100         MR_FW_RAID_MAP_ALL *map);
101 extern u_int16_t MR_LdSpanArrayGet(u_int32_t ld, u_int32_t span,
102         MR_FW_RAID_MAP_ALL *map);
103 extern u_int16_t mrsas_get_updated_dev_handle(PLD_LOAD_BALANCE_INFO lbInfo,
104         struct IO_REQUEST_INFO *io_info);
105 extern u_int8_t megasas_get_best_arm(PLD_LOAD_BALANCE_INFO lbInfo, u_int8_t arm,
106        u_int64_t block, u_int32_t count);
107 
108 
109 /**
110  * mrsas_cam_attach:        Main entry to CAM subsystem
111  * input:                   Adapter instance soft state
112  *
113  * This function is called from mrsas_attach() during initialization
114  * to perform SIM allocations and XPT bus registration.  If the kernel
115  * version is 7.4 or earlier, it would also initiate a bus scan.
116  */
117 int mrsas_cam_attach(struct mrsas_softc *sc)
118 {
119     struct cam_devq *devq;
120     int mrsas_cam_depth;
121 
122     mrsas_cam_depth = sc->max_fw_cmds - MRSAS_INTERNAL_CMDS;
123 
124     if ((devq = cam_simq_alloc(mrsas_cam_depth)) == NULL) {
125         device_printf(sc->mrsas_dev, "Cannot allocate SIM queue\n");
126         return(ENOMEM);
127     }
128 
129 
130     /*
131      * Create SIM for bus 0 and register, also create path
132      */
133     sc->sim_0 = cam_sim_alloc(mrsas_action, mrsas_poll, "mrsas", sc,
134         device_get_unit(sc->mrsas_dev), &sc->sim_lock, mrsas_cam_depth,
135         mrsas_cam_depth, devq);
136     if (sc->sim_0 == NULL){
137         device_printf(sc->mrsas_dev, "Cannot register SIM\n");
138         cam_simq_release(devq);
139         return(ENXIO);
140     }
141     /* Initialize taskqueue for Event Handling */
142     TASK_INIT(&sc->ev_task, 0, (void *)mrsas_aen_handler, sc);
143     sc->ev_tq = taskqueue_create("mrsas_taskq", M_NOWAIT | M_ZERO,
144         taskqueue_thread_enqueue, &sc->ev_tq);
145 
146     /* Run the task queue with lowest priority */
147     taskqueue_start_threads(&sc->ev_tq, 1, TDPRI_KERN_DAEMON,
148 			    -1, "%s taskq",
149         device_get_nameunit(sc->mrsas_dev));
150     lockmgr(&sc->sim_lock, LK_EXCLUSIVE);
151     if (xpt_bus_register(sc->sim_0, 0) != CAM_SUCCESS)
152     {
153         cam_simq_release(devq);
154         cam_sim_free(sc->sim_0);
155         lockmgr(&sc->sim_lock, LK_RELEASE);
156         return(ENXIO);
157     }
158     if (xpt_create_path(&sc->path_0, NULL, cam_sim_path(sc->sim_0),
159                          CAM_TARGET_WILDCARD,
160                          CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
161         xpt_bus_deregister(cam_sim_path(sc->sim_0));
162         cam_simq_release(devq);
163         cam_sim_free(sc->sim_0);
164         lockmgr(&sc->sim_lock, LK_RELEASE);
165         return(ENXIO);
166     }
167     lockmgr(&sc->sim_lock, LK_RELEASE);
168 
169     /*
170      * Create SIM for bus 1 and register, also create path
171      */
172     sc->sim_1 = cam_sim_alloc(mrsas_action, mrsas_poll, "mrsas", sc,
173         device_get_unit(sc->mrsas_dev), &sc->sim_lock, mrsas_cam_depth,
174         mrsas_cam_depth, devq);
175     cam_simq_release(devq);
176     if (sc->sim_1 == NULL){
177         device_printf(sc->mrsas_dev, "Cannot register SIM\n");
178         return(ENXIO);
179     }
180 
181     lockmgr(&sc->sim_lock, LK_EXCLUSIVE);
182     if (xpt_bus_register(sc->sim_1, 1) != CAM_SUCCESS){
183 	cam_sim_free(sc->sim_1);
184         lockmgr(&sc->sim_lock, LK_RELEASE);
185         return(ENXIO);
186     }
187     if (xpt_create_path(&sc->path_1, NULL, cam_sim_path(sc->sim_1),
188                          CAM_TARGET_WILDCARD,
189                          CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
190         xpt_bus_deregister(cam_sim_path(sc->sim_1));
191         cam_sim_free(sc->sim_1);
192         lockmgr(&sc->sim_lock, LK_RELEASE);
193         return(ENXIO);
194     }
195     lockmgr(&sc->sim_lock, LK_RELEASE);
196 
197     if (mrsas_bus_scan(sc)){
198         device_printf(sc->mrsas_dev, "Error in bus scan.\n");
199         return(1);
200     }
201     return(0);
202 }
203 
204 /**
205  * mrsas_cam_detach:        De-allocates and teardown CAM
206  * input:                   Adapter instance soft state
207  *
208  * De-registers and frees the paths and SIMs.
209  */
210 void mrsas_cam_detach(struct mrsas_softc *sc)
211 {
212     if (sc->ev_tq != NULL)
213         taskqueue_free(sc->ev_tq);
214     lockmgr(&sc->sim_lock, LK_EXCLUSIVE);
215     if (sc->path_0)
216         xpt_free_path(sc->path_0);
217     if (sc->sim_0) {
218         xpt_bus_deregister(cam_sim_path(sc->sim_0));
219         cam_sim_free(sc->sim_0);
220     }
221     if (sc->path_1)
222         xpt_free_path(sc->path_1);
223     if (sc->sim_1) {
224         xpt_bus_deregister(cam_sim_path(sc->sim_1));
225         cam_sim_free(sc->sim_1);
226     }
227     lockmgr(&sc->sim_lock, LK_RELEASE);
228 }
229 
230 /**
231  * mrsas_action:            SIM callback entry point
232  * input:                   pointer to SIM
233  *                          pointer to CAM Control Block
234  *
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
238  * earlier.
239  */
240 static void mrsas_action(struct cam_sim *sim, union ccb *ccb)
241 {
242     struct mrsas_softc *sc = (struct mrsas_softc *)cam_sim_softc(sim);
243     struct ccb_hdr *ccb_h = &(ccb->ccb_h);
244     u_int32_t device_id;
245 
246     switch (ccb->ccb_h.func_code) {
247         case XPT_SCSI_IO:
248         {
249             device_id = ccb_h->target_id;
250 
251             /*
252              * bus 0 is LD, bus 1 is for system-PD
253              */
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;
257                 xpt_done(ccb);
258             }
259             else {
260                 if (mrsas_startio(sc, sim, ccb)){
261                     ccb->ccb_h.status |= CAM_REQ_INVALID;
262                     xpt_done(ccb);
263                 }
264             }
265             break;
266         }
267         case XPT_ABORT:
268         {
269             ccb->ccb_h.status = CAM_UA_ABORT;
270             xpt_done(ccb);
271             break;
272         }
273         case XPT_RESET_BUS:
274         {
275             xpt_done(ccb);
276             break;
277         }
278         case XPT_GET_TRAN_SETTINGS:
279         {
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;
289             xpt_done(ccb);
290             break;
291         }
292         case XPT_SET_TRAN_SETTINGS:
293         {
294             ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
295             xpt_done(ccb);
296             break;
297         }
298         case XPT_CALC_GEOMETRY:
299         {
300             cam_calc_geometry(&ccb->ccg, 1);
301             xpt_done(ccb);
302             break;
303         }
304         case XPT_PATH_INQ:
305         {
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;
325             else
326                 ccb->cpi.max_target = MRSAS_MAX_PD-1;
327             ccb->cpi.maxio = MRSAS_MAX_IO_SIZE;
328             ccb->ccb_h.status = CAM_REQ_CMP;
329             xpt_done(ccb);
330             break;
331         }
332         default:
333         {
334             ccb->ccb_h.status = CAM_REQ_INVALID;
335             xpt_done(ccb);
336             break;
337         }
338     }
339 }
340 
341 /**
342  * mrsas_scsiio_timeout         Callback function for IO timed out
343  * input:                       mpt command context
344  *
345  * This function will execute after timeout value
346  * provided by ccb header from CAM layer, if timer expires.
347  * Driver will run timer for all DCDM and LDIO comming from CAM layer.
348  * This function is callback function for IO timeout and it runs in
349  * no-sleep context. Set do_timedout_reset in Adapter context so that
350  * it will execute OCR/Kill adpter from ocr_thread context.
351  */
352 static void
353 mrsas_scsiio_timeout(void *data)
354 {
355     struct mrsas_mpt_cmd *cmd;
356     struct mrsas_softc *sc;
357 
358     cmd = (struct mrsas_mpt_cmd *)data;
359     sc = cmd->sc;
360 
361     if (cmd->ccb_ptr == NULL) {
362         kprintf("command timeout with NULL ccb\n");
363         return;
364     }
365 
366     /* Below callout is dummy entry so that it will be
367      * cancelled from mrsas_cmd_done(). Now Controller will
368      * go to OCR/Kill Adapter based on OCR enable/disable
369      * property of Controller from ocr_thread context.
370      */
371     callout_reset(&cmd->cm_callout, (600000 * hz) / 1000,
372     mrsas_scsiio_timeout, cmd);
373     sc->do_timedout_reset = 1;
374     if(sc->ocr_thread_active)
375         wakeup(&sc->ocr_chan);
376 }
377 
378 /**
379  * mrsas_startio:           SCSI IO entry point
380  * input:                   Adapter instance soft state
381  *                          pointer to CAM Control Block
382  *
383  * This function is the SCSI IO entry point and it initiates IO processing.
384  * It copies the IO and depending if the IO is read/write or inquiry, it would
385  * call mrsas_build_ldio() or mrsas_build_dcdb(), respectively.  It returns
386  * 0 if the command is sent to firmware successfully, otherwise it returns 1.
387  */
388 static int32_t mrsas_startio(struct mrsas_softc *sc, struct cam_sim *sim,
389                              union ccb *ccb)
390 {
391     struct mrsas_mpt_cmd *cmd;
392     struct ccb_hdr *ccb_h = &(ccb->ccb_h);
393     struct ccb_scsiio *csio = &(ccb->csio);
394     MRSAS_REQUEST_DESCRIPTOR_UNION *req_desc;
395 
396     if ((csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE){
397         ccb->ccb_h.status = CAM_REQ_CMP;
398         xpt_done(ccb);
399         return(0);
400     }
401 
402     ccb_h->status |= CAM_SIM_QUEUED;
403     cmd = mrsas_get_mpt_cmd(sc);
404 
405     if (!cmd) {
406         ccb_h->status |= CAM_REQUEUE_REQ;
407         xpt_done(ccb);
408         return(0);
409     }
410 
411     if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
412         if(ccb_h->flags & CAM_DIR_IN)
413             cmd->flags |= MRSAS_DIR_IN;
414         if(ccb_h->flags & CAM_DIR_OUT)
415             cmd->flags |= MRSAS_DIR_OUT;
416     }
417     else
418         cmd->flags = MRSAS_DIR_NONE; /* no data */
419 
420 /* For FreeBSD 10.0 and higher */
421 #if 0 /* XXX (__FreeBSD_version >= 1000000) */
422 /*
423  *      * XXX We don't yet support physical addresses here.
424  */
425     switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) {
426     case CAM_DATA_PADDR:
427     case CAM_DATA_SG_PADDR:
428          kprintf("%s: physical addresses not supported\n",
429                __func__);
430          mrsas_release_mpt_cmd(cmd);
431          ccb_h->status = CAM_REQ_INVALID;
432          ccb_h->status &= ~CAM_SIM_QUEUED;
433          goto done;
434     case CAM_DATA_SG:
435          kprintf("%s: scatter gather is not supported\n",
436                __func__);
437          mrsas_release_mpt_cmd(cmd);
438          ccb_h->status = CAM_REQ_INVALID;
439          goto done;
440 	case CAM_DATA_VADDR:
441          if (csio->dxfer_len > MRSAS_MAX_IO_SIZE) {
442               mrsas_release_mpt_cmd(cmd);
443               ccb_h->status = CAM_REQ_TOO_BIG;
444               goto done;
445           }
446           cmd->length = csio->dxfer_len;
447           if (cmd->length)
448               cmd->data = csio->data_ptr;
449         break;
450     default:
451         ccb->ccb_h.status = CAM_REQ_INVALID;
452         goto done;
453     }
454 #else
455     if (!(ccb_h->flags & CAM_DATA_PHYS)) {  //Virtual data address
456          if (!(ccb_h->flags & CAM_SCATTER_VALID)) {
457              if (csio->dxfer_len > MRSAS_MAX_IO_SIZE) {
458                  mrsas_release_mpt_cmd(cmd);
459                  ccb_h->status = CAM_REQ_TOO_BIG;
460                  goto done;
461              }
462              cmd->length = csio->dxfer_len;
463              if (cmd->length)
464                  cmd->data = csio->data_ptr;
465          }
466          else {
467              mrsas_release_mpt_cmd(cmd);
468              ccb_h->status = CAM_REQ_INVALID;
469              goto done;
470          }
471     }
472     else { //Data addresses are physical.
473          mrsas_release_mpt_cmd(cmd);
474          ccb_h->status = CAM_REQ_INVALID;
475          ccb_h->status &= ~CAM_SIM_QUEUED;
476          goto done;
477     }
478 #endif
479     /* save ccb ptr */
480     cmd->ccb_ptr = ccb;
481 
482     req_desc = mrsas_get_request_desc(sc, (cmd->index)-1);
483     if (!req_desc) {
484         device_printf(sc->mrsas_dev, "Cannot get request_descriptor.\n");
485         return (FAIL);
486     }
487     memset(req_desc, 0, sizeof(MRSAS_REQUEST_DESCRIPTOR_UNION));
488     cmd->request_desc = req_desc;
489 
490     if (ccb_h->flags & CAM_CDB_POINTER)
491         bcopy(csio->cdb_io.cdb_ptr, cmd->io_request->CDB.CDB32, csio->cdb_len);
492     else
493         bcopy(csio->cdb_io.cdb_bytes, cmd->io_request->CDB.CDB32, csio->cdb_len);
494     lockmgr(&sc->raidmap_lock, LK_EXCLUSIVE);
495 
496     if (mrsas_ldio_inq(sim, ccb)) {
497         if (mrsas_build_ldio(sc, cmd, ccb)){
498             device_printf(sc->mrsas_dev, "Build LDIO failed.\n");
499 	    lockmgr(&sc->raidmap_lock, LK_RELEASE);
500             return(1);
501         }
502     }
503     else {
504         if (mrsas_build_dcdb(sc, cmd, ccb, sim)) {
505             device_printf(sc->mrsas_dev, "Build DCDB failed.\n");
506 	    lockmgr(&sc->raidmap_lock, LK_RELEASE);
507             return(1);
508         }
509     }
510     lockmgr(&sc->raidmap_lock, LK_RELEASE);
511 
512     if (cmd->flags == MRSAS_DIR_IN) //from device
513         cmd->io_request->Control |= MPI2_SCSIIO_CONTROL_READ;
514     else if (cmd->flags == MRSAS_DIR_OUT)  //to device
515         cmd->io_request->Control |= MPI2_SCSIIO_CONTROL_WRITE;
516 
517     cmd->io_request->SGLFlags = MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
518     cmd->io_request->SGLOffset0 = offsetof(MRSAS_RAID_SCSI_IO_REQUEST, SGL)/4;
519     cmd->io_request->SenseBufferLowAddress = cmd->sense_phys_addr;
520     cmd->io_request->SenseBufferLength = MRSAS_SCSI_SENSE_BUFFERSIZE;
521 
522     req_desc = cmd->request_desc;
523     req_desc->SCSIIO.SMID = cmd->index;
524 
525     /*
526      * Start timer for IO timeout. Default timeout value is 90 second.
527      */
528     callout_reset(&cmd->cm_callout, (sc->mrsas_io_timeout * hz) / 1000,
529         mrsas_scsiio_timeout, cmd);
530     atomic_inc(&sc->fw_outstanding);
531 
532 	if(atomic_read(&sc->fw_outstanding) > sc->io_cmds_highwater)
533         sc->io_cmds_highwater++;
534 
535     mrsas_fire_cmd(sc, req_desc->addr.u.low, req_desc->addr.u.high);
536     return(0);
537 
538 done:
539     xpt_done(ccb);
540     return(0);
541 }
542 
543 /**
544  * mrsas_ldio_inq:           Determines if IO is read/write or inquiry
545  * input:                        pointer to CAM Control Block
546  *
547  * This function determines if the IO is read/write or inquiry.  It returns a
548  * 1 if the IO is read/write and 0 if it is inquiry.
549  */
550 int mrsas_ldio_inq(struct cam_sim *sim, union ccb *ccb)
551 {
552     struct ccb_scsiio *csio = &(ccb->csio);
553 
554     if (cam_sim_bus(sim) == 1)
555         return(0);
556 
557     switch (csio->cdb_io.cdb_bytes[0]) {
558         case READ_10:
559         case WRITE_10:
560         case READ_12:
561         case WRITE_12:
562         case READ_6:
563         case WRITE_6:
564         case READ_16:
565         case WRITE_16:
566             return 1;
567         default:
568             return 0;
569     }
570 }
571 
572 /**
573  * mrsas_get_mpt_cmd:            Get a cmd from free command pool
574  * input:                        Adapter instance soft state
575  *
576  * This function removes an MPT command from the command free list and
577  * initializes it.
578  */
579 struct mrsas_mpt_cmd* mrsas_get_mpt_cmd(struct mrsas_softc *sc)
580 {
581     struct mrsas_mpt_cmd *cmd = NULL;
582 
583     lockmgr(&sc->mpt_cmd_pool_lock, LK_EXCLUSIVE);
584     if (!TAILQ_EMPTY(&sc->mrsas_mpt_cmd_list_head)){
585         cmd = TAILQ_FIRST(&sc->mrsas_mpt_cmd_list_head);
586         TAILQ_REMOVE(&sc->mrsas_mpt_cmd_list_head, cmd, next);
587     }
588     memset((uint8_t *)cmd->io_request, 0, MRSAS_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
589     cmd->data = NULL;
590     cmd->length = 0;
591     cmd->flags = 0;
592     cmd->error_code = 0;
593     cmd->load_balance = 0;
594     cmd->ccb_ptr = NULL;
595     lockmgr(&sc->mpt_cmd_pool_lock, LK_RELEASE);
596 
597     return cmd;
598 }
599 
600 /**
601  * mrsas_release_mpt_cmd:      Return a cmd to free command pool
602  * input:                      Command packet for return to free command pool
603  *
604  * This function returns an MPT command to the free command list.
605  */
606 void mrsas_release_mpt_cmd(struct mrsas_mpt_cmd *cmd)
607 {
608     struct mrsas_softc *sc = cmd->sc;
609 
610     lockmgr(&sc->mpt_cmd_pool_lock, LK_EXCLUSIVE);
611     cmd->sync_cmd_idx = (u_int32_t)MRSAS_ULONG_MAX;
612     TAILQ_INSERT_TAIL(&(sc->mrsas_mpt_cmd_list_head), cmd, next);
613     lockmgr(&sc->mpt_cmd_pool_lock, LK_RELEASE);
614 
615     return;
616 }
617 
618 /**
619  * mrsas_get_request_desc:     Get request descriptor from array
620  * input:                      Adapter instance soft state
621  *                             SMID index
622  *
623  * This function returns a pointer to the request descriptor.
624  */
625 MRSAS_REQUEST_DESCRIPTOR_UNION *
626 mrsas_get_request_desc(struct mrsas_softc *sc, u_int16_t index)
627 {
628     u_int8_t *p;
629 
630     if (index >= sc->max_fw_cmds) {
631         device_printf(sc->mrsas_dev, "Invalid SMID (0x%x)request for desc\n", index);
632         return NULL;
633     }
634     p = sc->req_desc + sizeof(MRSAS_REQUEST_DESCRIPTOR_UNION) * index;
635 
636     return (MRSAS_REQUEST_DESCRIPTOR_UNION *)p;
637 }
638 
639 /**
640  * mrsas_build_ldio:       Builds an LDIO command
641  * input:                  Adapter instance soft state
642  *                         Pointer to command packet
643  *                         Pointer to CCB
644  *
645  * This function builds the LDIO command packet.  It returns 0 if the
646  * command is built successfully, otherwise it returns a 1.
647  */
648 int mrsas_build_ldio(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
649                       union ccb *ccb)
650 {
651     struct ccb_hdr *ccb_h = &(ccb->ccb_h);
652     struct ccb_scsiio *csio = &(ccb->csio);
653     u_int32_t device_id;
654     MRSAS_RAID_SCSI_IO_REQUEST *io_request;
655 
656     device_id = ccb_h->target_id;
657 
658     io_request = cmd->io_request;
659     io_request->RaidContext.VirtualDiskTgtId = device_id;
660     io_request->RaidContext.status = 0;
661     io_request->RaidContext.exStatus = 0;
662 
663     /* just the cdb len, other flags zero, and ORed-in later for FP */
664     io_request->IoFlags = csio->cdb_len;
665 
666     if (mrsas_setup_io(sc, cmd, ccb, device_id, io_request) != SUCCESS)
667         device_printf(sc->mrsas_dev, "Build ldio or fpio error\n");
668 
669     io_request->DataLength = cmd->length;
670 
671     if (mrsas_map_request(sc, cmd) == SUCCESS) {
672         if (cmd->sge_count > MRSAS_MAX_SGL) {
673             device_printf(sc->mrsas_dev, "Error: sge_count (0x%x) exceeds"
674                 "max (0x%x) allowed\n", cmd->sge_count, sc->max_num_sge);
675             return (FAIL);
676         }
677         io_request->RaidContext.numSGE = cmd->sge_count;
678     }
679     else {
680         device_printf(sc->mrsas_dev, "Data map/load failed.\n");
681         return(FAIL);
682     }
683     return(0);
684 }
685 
686 /**
687  * mrsas_setup_io:             Set up data including Fast Path I/O
688  * input:                      Adapter instance soft state
689  *                             Pointer to command packet
690  *                             Pointer to CCB
691  *
692  * This function builds the DCDB inquiry command.  It returns 0 if the
693  * command is built successfully, otherwise it returns a 1.
694  */
695 int mrsas_setup_io(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
696                     union ccb *ccb, u_int32_t device_id,
697                     MRSAS_RAID_SCSI_IO_REQUEST *io_request)
698 {
699     struct ccb_hdr *ccb_h = &(ccb->ccb_h);
700     struct ccb_scsiio *csio = &(ccb->csio);
701     struct IO_REQUEST_INFO io_info;
702     MR_FW_RAID_MAP_ALL *map_ptr;
703     u_int8_t fp_possible;
704     u_int32_t start_lba_hi, start_lba_lo, ld_block_size;
705     u_int32_t datalength = 0;
706 
707     start_lba_lo = 0;
708     start_lba_hi = 0;
709     fp_possible = 0;
710 
711     /*
712      * READ_6 (0x08) or WRITE_6 (0x0A) cdb
713      */
714     if (csio->cdb_len == 6) {
715         datalength = (u_int32_t)csio->cdb_io.cdb_bytes[4];
716         start_lba_lo = ((u_int32_t) csio->cdb_io.cdb_bytes[1] << 16) |
717                        ((u_int32_t) csio->cdb_io.cdb_bytes[2] << 8) |
718                         (u_int32_t) csio->cdb_io.cdb_bytes[3];
719         start_lba_lo &= 0x1FFFFF;
720     }
721     /*
722      * READ_10 (0x28) or WRITE_6 (0x2A) cdb
723      */
724     else if (csio->cdb_len == 10) {
725         datalength = (u_int32_t)csio->cdb_io.cdb_bytes[8] |
726                        ((u_int32_t)csio->cdb_io.cdb_bytes[7] << 8);
727         start_lba_lo = ((u_int32_t) csio->cdb_io.cdb_bytes[2] << 24) |
728                        ((u_int32_t) csio->cdb_io.cdb_bytes[3] << 16) |
729                         (u_int32_t) csio->cdb_io.cdb_bytes[4] << 8 |
730                         ((u_int32_t) csio->cdb_io.cdb_bytes[5]);
731     }
732     /*
733      * READ_12 (0xA8) or WRITE_12 (0xAA) cdb
734      */
735     else if (csio->cdb_len == 12) {
736         datalength = (u_int32_t)csio->cdb_io.cdb_bytes[6] << 24 |
737                        ((u_int32_t)csio->cdb_io.cdb_bytes[7] << 16) |
738                        ((u_int32_t)csio->cdb_io.cdb_bytes[8] << 8) |
739                        ((u_int32_t)csio->cdb_io.cdb_bytes[9]);
740         start_lba_lo = ((u_int32_t) csio->cdb_io.cdb_bytes[2] << 24) |
741                        ((u_int32_t) csio->cdb_io.cdb_bytes[3] << 16) |
742                         (u_int32_t) csio->cdb_io.cdb_bytes[4] << 8 |
743                         ((u_int32_t) csio->cdb_io.cdb_bytes[5]);
744     }
745     /*
746      * READ_16 (0x88) or WRITE_16 (0xx8A) cdb
747      */
748     else if (csio->cdb_len == 16) {
749         datalength = (u_int32_t)csio->cdb_io.cdb_bytes[10] << 24 |
750                        ((u_int32_t)csio->cdb_io.cdb_bytes[11] << 16) |
751                        ((u_int32_t)csio->cdb_io.cdb_bytes[12] << 8) |
752                        ((u_int32_t)csio->cdb_io.cdb_bytes[13]);
753         start_lba_lo = ((u_int32_t) csio->cdb_io.cdb_bytes[6] << 24) |
754                        ((u_int32_t) csio->cdb_io.cdb_bytes[7] << 16) |
755                         (u_int32_t) csio->cdb_io.cdb_bytes[8] << 8 |
756                         ((u_int32_t) csio->cdb_io.cdb_bytes[9]);
757         start_lba_hi = ((u_int32_t) csio->cdb_io.cdb_bytes[2] << 24) |
758                        ((u_int32_t) csio->cdb_io.cdb_bytes[3] << 16) |
759                         (u_int32_t) csio->cdb_io.cdb_bytes[4] << 8 |
760                         ((u_int32_t) csio->cdb_io.cdb_bytes[5]);
761     }
762 
763     memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
764     io_info.ldStartBlock = ((u_int64_t)start_lba_hi << 32) | start_lba_lo;
765     io_info.numBlocks = datalength;
766     io_info.ldTgtId = device_id;
767 
768     switch (ccb_h->flags & CAM_DIR_MASK) {
769 	case CAM_DIR_IN:
770 		io_info.isRead = 1;
771 		break;
772 	case CAM_DIR_OUT:
773 		io_info.isRead = 0;
774 		break;
775 	case CAM_DIR_NONE:
776 	default:
777     	mrsas_dprint(sc, MRSAS_TRACE, "From %s : DMA Flag is %d \n", __func__, ccb_h->flags & CAM_DIR_MASK);
778 		break;
779     }
780 
781     map_ptr = sc->raidmap_mem[(sc->map_id & 1)];
782     ld_block_size = MR_LdBlockSizeGet(device_id, map_ptr, sc);
783 
784     if ((MR_TargetIdToLdGet(device_id, map_ptr) >= MAX_LOGICAL_DRIVES) ||
785             (!sc->fast_path_io)) {
786         io_request->RaidContext.regLockFlags = 0;
787         fp_possible = 0;
788     }
789     else
790     {
791         if (MR_BuildRaidContext(sc, &io_info, &io_request->RaidContext, map_ptr))
792             fp_possible = io_info.fpOkForIo;
793     }
794 
795     if (fp_possible) {
796         mrsas_set_pd_lba(io_request, csio->cdb_len, &io_info, ccb, map_ptr,
797             start_lba_lo, ld_block_size);
798         io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
799         cmd->request_desc->SCSIIO.RequestFlags =
800                         (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY
801                          << MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
802 	    if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY)) {
803 		    if (io_request->RaidContext.regLockFlags == REGION_TYPE_UNUSED)
804                 cmd->request_desc->SCSIIO.RequestFlags = (MRSAS_REQ_DESCRIPT_FLAGS_NO_LOCK << MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
805             io_request->RaidContext.Type = MPI2_TYPE_CUDA;
806             io_request->RaidContext.nseg = 0x1;
807             io_request->IoFlags |= MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
808             io_request->RaidContext.regLockFlags |= (MR_RL_FLAGS_GRANT_DESTINATION_CUDA | MR_RL_FLAGS_SEQ_NUM_ENABLE);
809         }
810         if ((sc->load_balance_info[device_id].loadBalanceFlag) && (io_info.isRead)) {
811             io_info.devHandle = mrsas_get_updated_dev_handle(&sc->load_balance_info[device_id],
812                 &io_info);
813             cmd->load_balance = MRSAS_LOAD_BALANCE_FLAG;
814         }
815         else
816             cmd->load_balance = 0;
817         cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
818         io_request->DevHandle = io_info.devHandle;
819     }
820     else {
821         /* Not FP IO */
822         io_request->RaidContext.timeoutValue = map_ptr->raidMap.fpPdIoTimeoutSec;
823         cmd->request_desc->SCSIIO.RequestFlags =
824             (MRSAS_REQ_DESCRIPT_FLAGS_LD_IO << MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
825 	    if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY)) {
826             if (io_request->RaidContext.regLockFlags == REGION_TYPE_UNUSED)
827                 cmd->request_desc->SCSIIO.RequestFlags = (MRSAS_REQ_DESCRIPT_FLAGS_NO_LOCK << MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
828             io_request->RaidContext.Type = MPI2_TYPE_CUDA;
829             io_request->RaidContext.regLockFlags |= (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 | MR_RL_FLAGS_SEQ_NUM_ENABLE);
830             io_request->RaidContext.nseg = 0x1;
831         }
832         io_request->Function = MRSAS_MPI2_FUNCTION_LD_IO_REQUEST;
833         io_request->DevHandle = device_id;
834     }
835     return(0);
836 }
837 
838 /**
839  * mrsas_build_dcdb:       Builds an DCDB command
840  * input:                  Adapter instance soft state
841  *                         Pointer to command packet
842  *                         Pointer to CCB
843  *
844  * This function builds the DCDB inquiry command.  It returns 0 if the
845  * command is built successfully, otherwise it returns a 1.
846  */
847 int mrsas_build_dcdb(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
848                       union ccb *ccb, struct cam_sim *sim)
849 {
850     struct ccb_hdr *ccb_h = &(ccb->ccb_h);
851     u_int32_t device_id;
852     MR_FW_RAID_MAP_ALL *map_ptr;
853     MRSAS_RAID_SCSI_IO_REQUEST *io_request;
854 
855     io_request = cmd->io_request;
856     device_id = ccb_h->target_id;
857     map_ptr = sc->raidmap_mem[(sc->map_id & 1)];
858 
859     /* Check if this is for system PD */
860     if (cam_sim_bus(sim) == 1 &&
861             sc->pd_list[device_id].driveState == MR_PD_STATE_SYSTEM) {
862         io_request->Function = 0;
863         io_request->DevHandle = map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
864         io_request->RaidContext.timeoutValue = map_ptr->raidMap.fpPdIoTimeoutSec;
865         io_request->RaidContext.regLockFlags = 0;
866         io_request->RaidContext.regLockRowLBA = 0;
867         io_request->RaidContext.regLockLength = 0;
868         io_request->RaidContext.RAIDFlags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD <<
869             MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
870 	    if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY))
871             io_request->IoFlags |= MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
872         cmd->request_desc->SCSIIO.RequestFlags =
873             (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
874             MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
875         cmd->request_desc->SCSIIO.DevHandle =
876             map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
877     }
878     else {
879         io_request->Function  = MRSAS_MPI2_FUNCTION_LD_IO_REQUEST;
880         io_request->DevHandle = device_id;
881         cmd->request_desc->SCSIIO.RequestFlags =
882             (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO << MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
883     }
884 
885     io_request->RaidContext.VirtualDiskTgtId = device_id;
886     io_request->LUN[1] = ccb_h->target_lun & 0xF;
887     io_request->DataLength = cmd->length;
888 
889     if (mrsas_map_request(sc, cmd) == SUCCESS) {
890         if (cmd->sge_count > sc->max_num_sge) {
891             device_printf(sc->mrsas_dev, "Error: sge_count (0x%x) exceeds"
892                 "max (0x%x) allowed\n", cmd->sge_count, sc->max_num_sge);
893             return (1);
894         }
895         io_request->RaidContext.numSGE = cmd->sge_count;
896     }
897     else {
898         device_printf(sc->mrsas_dev, "Data map/load failed.\n");
899         return(1);
900     }
901     return(0);
902 }
903 
904 /**
905  * mrsas_map_request:           Map and load data
906  * input:                       Adapter instance soft state
907  *                              Pointer to command packet
908  *
909  * For data from OS, map and load the data buffer into bus space.  The
910  * SG list is built in the callback.  If the  bus dmamap load is not
911  * successful, cmd->error_code will contain the  error code and a 1 is
912  * returned.
913  */
914 int mrsas_map_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd)
915 {
916     u_int32_t retcode = 0;
917     struct cam_sim *sim;
918     int flag = BUS_DMA_NOWAIT;
919 
920     sim = xpt_path_sim(cmd->ccb_ptr->ccb_h.path);
921 
922     if (cmd->data != NULL) {
923         lockmgr(&sc->io_lock, LK_EXCLUSIVE);
924         /* Map data buffer into bus space */
925         retcode = bus_dmamap_load(sc->data_tag, cmd->data_dmamap, cmd->data,
926                               cmd->length, mrsas_data_load_cb, cmd, flag);
927         lockmgr(&sc->io_lock, LK_RELEASE);
928 	if (retcode)
929 	    device_printf(sc->mrsas_dev, "bus_dmamap_load(): retcode = %d\n", retcode);
930         if (retcode == EINPROGRESS) {
931             device_printf(sc->mrsas_dev, "request load in progress\n");
932             mrsas_freeze_simq(cmd, sim);
933         }
934     }
935     if (cmd->error_code)
936         return(1);
937     return(retcode);
938 }
939 
940 /**
941  * mrsas_unmap_request:       Unmap and unload data
942  * input:                     Adapter instance soft state
943  *                            Pointer to command packet
944  *
945  * This function unmaps and unloads data from OS.
946  */
947 void mrsas_unmap_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd)
948 {
949     if (cmd->data != NULL) {
950         if (cmd->flags & MRSAS_DIR_IN)
951             bus_dmamap_sync(sc->data_tag, cmd->data_dmamap, BUS_DMASYNC_POSTREAD);
952         if (cmd->flags & MRSAS_DIR_OUT)
953             bus_dmamap_sync(sc->data_tag, cmd->data_dmamap, BUS_DMASYNC_POSTWRITE);
954         lockmgr(&sc->io_lock, LK_EXCLUSIVE);
955         bus_dmamap_unload(sc->data_tag, cmd->data_dmamap);
956         lockmgr(&sc->io_lock, LK_RELEASE);
957     }
958 }
959 
960 /**
961  * mrsas_data_load_cb:        Callback entry point
962  * input:                     Pointer to command packet as argument
963  *                            Pointer to segment
964  *                            Number of segments
965  *                            Error
966  *
967  * This is the callback function of the bus dma map load.  It builds
968  * the SG list.
969  */
970 static void
971 mrsas_data_load_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
972 {
973     struct mrsas_mpt_cmd *cmd = (struct mrsas_mpt_cmd *)arg;
974     struct mrsas_softc *sc = cmd->sc;
975     MRSAS_RAID_SCSI_IO_REQUEST *io_request;
976     pMpi25IeeeSgeChain64_t sgl_ptr;
977     int i=0, sg_processed=0;
978 
979     if (error)
980     {
981         cmd->error_code = error;
982         device_printf(sc->mrsas_dev, "mrsas_data_load_cb: error=%d\n", error);
983         if (error == EFBIG) {
984             cmd->ccb_ptr->ccb_h.status = CAM_REQ_TOO_BIG;
985             return;
986         }
987     }
988 
989     if (cmd->flags & MRSAS_DIR_IN)
990         bus_dmamap_sync(cmd->sc->data_tag, cmd->data_dmamap,
991                                             BUS_DMASYNC_PREREAD);
992     if (cmd->flags & MRSAS_DIR_OUT)
993         bus_dmamap_sync(cmd->sc->data_tag, cmd->data_dmamap,
994                                             BUS_DMASYNC_PREWRITE);
995     if (nseg > sc->max_num_sge) {
996         device_printf(sc->mrsas_dev, "SGE count is too large or 0.\n");
997         return;
998     }
999 
1000     io_request = cmd->io_request;
1001     sgl_ptr = (pMpi25IeeeSgeChain64_t)&io_request->SGL;
1002 
1003     if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY)) {
1004         pMpi25IeeeSgeChain64_t sgl_ptr_end = sgl_ptr;
1005         sgl_ptr_end += sc->max_sge_in_main_msg - 1;
1006         sgl_ptr_end->Flags = 0;
1007     }
1008 
1009     if (nseg != 0) {
1010         for (i=0; i < nseg; i++) {
1011             sgl_ptr->Address = segs[i].ds_addr;
1012             sgl_ptr->Length = segs[i].ds_len;
1013             sgl_ptr->Flags = 0;
1014 	        if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY)) {
1015             	if (i == nseg - 1)
1016                 	sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1017             }
1018 	    	sgl_ptr++;
1019 	    	sg_processed = i + 1;
1020 	    	/*
1021              * Prepare chain element
1022              */
1023 			if ((sg_processed == (sc->max_sge_in_main_msg - 1)) &&
1024 				(nseg > sc->max_sge_in_main_msg)) {
1025 					pMpi25IeeeSgeChain64_t sg_chain;
1026 					if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY)) {
1027 						if ((cmd->io_request->IoFlags & MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1028 													!= MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1029 							 cmd->io_request->ChainOffset = sc->chain_offset_io_request;
1030 					   else
1031 							   cmd->io_request->ChainOffset = 0;
1032 					} else
1033 							 cmd->io_request->ChainOffset = sc->chain_offset_io_request;
1034 					sg_chain = sgl_ptr;
1035 					if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY))
1036 						 sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1037 				    else
1038 						 sg_chain->Flags = (IEEE_SGE_FLAGS_CHAIN_ELEMENT | MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1039 					sg_chain->Length = (sizeof(MPI2_SGE_IO_UNION) * (nseg - sg_processed));
1040 					sg_chain->Address = cmd->chain_frame_phys_addr;
1041 					sgl_ptr = (pMpi25IeeeSgeChain64_t)cmd->chain_frame;
1042 			}
1043         }
1044     }
1045     cmd->sge_count = nseg;
1046 }
1047 
1048 /**
1049  * mrsas_freeze_simq:        Freeze SIM queue
1050  * input:                    Pointer to command packet
1051  *                           Pointer to SIM
1052  *
1053  * This function freezes the sim queue.
1054  */
1055 static void mrsas_freeze_simq(struct mrsas_mpt_cmd *cmd, struct cam_sim *sim)
1056 {
1057     union ccb   *ccb = (union ccb *)(cmd->ccb_ptr);
1058 
1059     xpt_freeze_simq(sim, 1);
1060     ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1061     ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1062 }
1063 
1064 void mrsas_xpt_freeze(struct mrsas_softc *sc) {
1065     xpt_freeze_simq(sc->sim_0, 1);
1066     xpt_freeze_simq(sc->sim_1, 1);
1067 }
1068 
1069 void mrsas_xpt_release(struct mrsas_softc *sc) {
1070     xpt_release_simq(sc->sim_0, 1);
1071     xpt_release_simq(sc->sim_1, 1);
1072 }
1073 
1074 /**
1075  * mrsas_cmd_done:           Perform remaining command completion
1076  * input:                    Adapter instance soft state
1077  *                           Pointer to command packet
1078  *
1079  * This function calls ummap request and releases the MPT command.
1080  */
1081 void mrsas_cmd_done(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd)
1082 {
1083     callout_stop(&cmd->cm_callout);
1084     mrsas_unmap_request(sc, cmd);
1085     lockmgr(&sc->sim_lock, LK_EXCLUSIVE);
1086     xpt_done(cmd->ccb_ptr);
1087     cmd->ccb_ptr = NULL;
1088     lockmgr(&sc->sim_lock, LK_RELEASE);
1089     mrsas_release_mpt_cmd(cmd);
1090 }
1091 
1092 /**
1093  * mrsas_poll:               Polling entry point
1094  * input:                    Pointer to SIM
1095  *
1096  * This is currently a stub function.
1097  */
1098 static void mrsas_poll(struct cam_sim *sim)
1099 {
1100     struct mrsas_softc *sc = (struct mrsas_softc *)cam_sim_softc(sim);
1101     mrsas_isr((void *) sc);
1102 }
1103 
1104 static void
1105 mrsas_rescan_callback(struct cam_periph *periph, union ccb *ccb)
1106 {
1107 	xpt_free_path(ccb->ccb_h.path);
1108 	xpt_free_ccb(ccb);
1109 }
1110 
1111 /*
1112  * mrsas_bus_scan:           Perform bus scan
1113  * input:                    Adapter instance soft state
1114  *
1115  * This mrsas_bus_scan function is needed for FreeBSD 7.x.  Also, it should
1116  * not be called in FreeBSD 8.x and later versions, where the bus scan is
1117  * automatic.
1118  */
1119 int mrsas_bus_scan(struct mrsas_softc *sc)
1120 {
1121     union ccb *ccb_0;
1122     union ccb *ccb_1;
1123 
1124     lockmgr(&sc->sim_lock, LK_EXCLUSIVE);
1125     if ((ccb_0 = xpt_alloc_ccb()) == NULL) {
1126         lockmgr(&sc->sim_lock, LK_RELEASE);
1127         return(ENOMEM);
1128     }
1129 
1130     if ((ccb_1 = xpt_alloc_ccb()) == NULL) {
1131 	xpt_free_ccb(ccb_0);
1132         lockmgr(&sc->sim_lock, LK_RELEASE);
1133         return(ENOMEM);
1134     }
1135 
1136     if (xpt_create_path(&ccb_0->ccb_h.path, xpt_periph, cam_sim_path(sc->sim_0),
1137             CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP){
1138         xpt_free_ccb(ccb_0);
1139         xpt_free_ccb(ccb_1);
1140         lockmgr(&sc->sim_lock, LK_RELEASE);
1141         return(EIO);
1142     }
1143 
1144     if (xpt_create_path(&ccb_1->ccb_h.path, xpt_periph, cam_sim_path(sc->sim_1),
1145             CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP){
1146         xpt_free_ccb(ccb_0);
1147         xpt_free_ccb(ccb_1);
1148         lockmgr(&sc->sim_lock, LK_RELEASE);
1149         return(EIO);
1150     }
1151 
1152     xpt_setup_ccb(&ccb_0->ccb_h, ccb_0->ccb_h.path, 5/*priority (low)*/);
1153     ccb_0->ccb_h.func_code = XPT_SCAN_BUS;
1154     ccb_0->ccb_h.cbfcnp = mrsas_rescan_callback;
1155     ccb_0->crcn.flags = CAM_FLAG_NONE;
1156     xpt_action(ccb_0); /* scan is now in progress */
1157 
1158     xpt_setup_ccb(&ccb_1->ccb_h, ccb_1->ccb_h.path, 5/*priority (low)*/);
1159     ccb_1->ccb_h.func_code = XPT_SCAN_BUS;
1160     ccb_1->ccb_h.cbfcnp = mrsas_rescan_callback;
1161     ccb_1->crcn.flags = CAM_FLAG_NONE;
1162     xpt_action(ccb_1); /* scan is now in progress */
1163 
1164     lockmgr(&sc->sim_lock, LK_RELEASE);
1165 
1166     return(0);
1167 }
1168 
1169 /*
1170  * mrsas_bus_scan_sim:		Perform bus scan per SIM
1171  * input:					Adapter instance soft state
1172  * 							This function will be called from Event handler
1173  * 							on LD creation/deletion, JBOD on/off.
1174  */
1175 int mrsas_bus_scan_sim(struct mrsas_softc *sc, struct cam_sim *sim)
1176 {
1177     union ccb *ccb;
1178 
1179     lockmgr(&sc->sim_lock, LK_EXCLUSIVE);
1180     if ((ccb = xpt_alloc_ccb()) == NULL) {
1181         lockmgr(&sc->sim_lock, LK_RELEASE);
1182         return(ENOMEM);
1183     }
1184     if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(sim),
1185             CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP){
1186         xpt_free_ccb(ccb);
1187         lockmgr(&sc->sim_lock, LK_RELEASE);
1188         return(EIO);
1189     }
1190     xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5/*priority (low)*/);
1191     ccb->ccb_h.func_code = XPT_SCAN_BUS;
1192     ccb->ccb_h.cbfcnp = mrsas_rescan_callback;
1193     ccb->crcn.flags = CAM_FLAG_NONE;
1194     xpt_action(ccb); /* scan is now in progress */
1195 
1196     lockmgr(&sc->sim_lock, LK_RELEASE);
1197 
1198     return(0);
1199 }
1200