]> CyberLeo.Net >> Repos - FreeBSD/releng/9.3.git/blob - sys/dev/mrsas/mrsas_cam.c
Copy stable/9 to releng/9.3 as part of the 9.3-RELEASE cycle.
[FreeBSD/releng/9.3.git] / sys / dev / mrsas / mrsas_cam.c
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 */
35
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD$");
38
39 #include "dev/mrsas/mrsas.h" 
40
41 #include <cam/cam.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>
48
49 #include <cam/scsi/scsi_all.h>
50 #include <cam/scsi/scsi_message.h>
51 #include <sys/taskqueue.h>
52
53
54 /*
55  * Function prototypes
56  */
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,
64                     union ccb *ccb);
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,
87                     int nseg, int error);
88 static int32_t mrsas_startio(struct mrsas_softc *sc, struct cam_sim *sim, 
89                     union ccb *ccb);
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, 
92                     u_int16_t index);
93
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);
108
109
110 /**
111  * mrsas_cam_attach:        Main entry to CAM subsystem 
112  * input:                   Adapter instance soft state 
113  *
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.
117  */
118 int mrsas_cam_attach(struct mrsas_softc *sc)
119 {
120     struct cam_devq *devq;
121     int mrsas_cam_depth;
122
123     mrsas_cam_depth = sc->max_fw_cmds - MRSAS_INTERNAL_CMDS;
124     
125     if ((devq = cam_simq_alloc(mrsas_cam_depth)) == NULL) {
126         device_printf(sc->mrsas_dev, "Cannot allocate SIM queue\n");
127         return(ENOMEM);
128     }
129
130
131     /* 
132      * Create SIM for bus 0 and register, also create path 
133      */
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){
138         cam_simq_free(devq);
139         device_printf(sc->mrsas_dev, "Cannot register SIM\n");
140         return(ENXIO);
141     }
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);
146
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)
152     {
153         cam_sim_free(sc->sim_0, TRUE); // passing true frees the devq
154         mtx_unlock(&sc->sim_lock); 
155         return(ENXIO);
156     }
157     if (xpt_create_path(&sc->path_0, NULL, cam_sim_path(sc->sim_0),
158                          CAM_TARGET_WILDCARD,
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); 
163         return(ENXIO);
164     }
165     mtx_unlock(&sc->sim_lock);
166
167     /* 
168      * Create SIM for bus 1 and register, also create path 
169      */
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){
174         cam_simq_free(devq);
175         device_printf(sc->mrsas_dev, "Cannot register SIM\n");
176         return(ENXIO);
177     }
178
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);
183         return(ENXIO);
184     }
185     if (xpt_create_path(&sc->path_1, NULL, cam_sim_path(sc->sim_1),
186                          CAM_TARGET_WILDCARD,
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);
191         return(ENXIO);
192     }
193     mtx_unlock(&sc->sim_lock);
194
195 #if (__FreeBSD_version <= 704000)
196     if (mrsas_bus_scan(sc)){
197         device_printf(sc->mrsas_dev, "Error in bus scan.\n");
198         return(1);
199     }
200 #endif
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     mtx_lock(&sc->sim_lock);
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, FALSE);
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, TRUE);
226     }
227     mtx_unlock(&sc->sim_lock);
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 #if (__FreeBSD_version > 704000)
328             ccb->cpi.maxio = MRSAS_MAX_IO_SIZE;
329 #endif
330             ccb->ccb_h.status = CAM_REQ_CMP;
331             xpt_done(ccb);
332             break;
333         }
334         default:
335         {
336             ccb->ccb_h.status = CAM_REQ_INVALID;
337             xpt_done(ccb);
338             break;
339         }
340     }
341 }
342
343 /**
344  * mrsas_scsiio_timeout         Callback function for IO timed out
345  * input:                       mpt command context
346  *
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.
353  */
354 static void
355 mrsas_scsiio_timeout(void *data)
356 {
357     struct mrsas_mpt_cmd *cmd;
358     struct mrsas_softc *sc;
359
360     cmd = (struct mrsas_mpt_cmd *)data;
361     sc = cmd->sc;
362
363     if (cmd->ccb_ptr == NULL) {
364         printf("command timeout with NULL ccb\n");
365         return;
366     }
367
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.
372      */
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);
378 }
379
380 /**
381  * mrsas_startio:           SCSI IO entry point   
382  * input:                   Adapter instance soft state 
383  *                          pointer to CAM Control Block
384  *
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.
389  */
390 static int32_t mrsas_startio(struct mrsas_softc *sc, struct cam_sim *sim, 
391                              union ccb *ccb)
392 {
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;
397
398     if ((csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE){
399         ccb->ccb_h.status = CAM_REQ_CMP;
400         xpt_done(ccb);
401         return(0);
402     }
403
404     ccb_h->status |= CAM_SIM_QUEUED;
405     cmd = mrsas_get_mpt_cmd(sc);
406
407     if (!cmd) {
408         ccb_h->status |= CAM_REQUEUE_REQ;
409         xpt_done(ccb);
410         return(0);  
411     }
412
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;
418     } 
419     else 
420         cmd->flags = MRSAS_DIR_NONE; /* no data */
421
422 /* For FreeBSD 10.0 and higher */
423 #if (__FreeBSD_version >= 1000000)
424 /*
425  *      * XXX We don't yet support physical addresses here.
426  */ 
427     switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) {
428     case CAM_DATA_PADDR:
429     case CAM_DATA_SG_PADDR:
430          printf("%s: physical addresses not supported\n",
431                __func__);
432          mrsas_release_mpt_cmd(cmd);
433          ccb_h->status = CAM_REQ_INVALID;
434          ccb_h->status &= ~CAM_SIM_QUEUED;
435          goto done;
436     case CAM_DATA_SG:
437          printf("%s: scatter gather is not supported\n",
438                __func__);
439          mrsas_release_mpt_cmd(cmd);
440          ccb_h->status = CAM_REQ_INVALID;
441          goto done;
442         case CAM_DATA_VADDR:
443          if (csio->dxfer_len > MRSAS_MAX_IO_SIZE) {
444               mrsas_release_mpt_cmd(cmd);
445               ccb_h->status = CAM_REQ_TOO_BIG;
446               goto done;
447           }
448           cmd->length = csio->dxfer_len;
449           if (cmd->length) 
450               cmd->data = csio->data_ptr;
451         break;
452     default:
453         ccb->ccb_h.status = CAM_REQ_INVALID;
454         goto done;
455     }
456 #else
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;
462                  goto done;
463              }
464              cmd->length = csio->dxfer_len;
465              if (cmd->length) 
466                  cmd->data = csio->data_ptr;
467          } 
468          else { 
469              mrsas_release_mpt_cmd(cmd);
470              ccb_h->status = CAM_REQ_INVALID;
471              goto done;
472          }
473     } 
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;
478          goto done;
479     }
480 #endif
481     /* save ccb ptr */
482     cmd->ccb_ptr = ccb;
483
484     req_desc = mrsas_get_request_desc(sc, (cmd->index)-1);
485     if (!req_desc) {
486         device_printf(sc->mrsas_dev, "Cannot get request_descriptor.\n");
487         return (FAIL);
488     }
489     memset(req_desc, 0, sizeof(MRSAS_REQUEST_DESCRIPTOR_UNION));
490     cmd->request_desc = req_desc;
491
492     if (ccb_h->flags & CAM_CDB_POINTER)
493         bcopy(csio->cdb_io.cdb_ptr, cmd->io_request->CDB.CDB32, csio->cdb_len);
494     else
495         bcopy(csio->cdb_io.cdb_bytes, cmd->io_request->CDB.CDB32, csio->cdb_len);
496     mtx_lock(&sc->raidmap_lock);
497
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);
502             return(1);
503         }    
504     }
505     else { 
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);
509             return(1);
510         }
511     }
512     mtx_unlock(&sc->raidmap_lock);
513
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;
518
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;
523
524     req_desc = cmd->request_desc;
525     req_desc->SCSIIO.SMID = cmd->index;
526
527     /*
528      * Start timer for IO timeout. Default timeout value is 90 second.
529      */
530     callout_reset(&cmd->cm_callout, (sc->mrsas_io_timeout * hz) / 1000,
531         mrsas_scsiio_timeout, cmd);
532     atomic_inc(&sc->fw_outstanding);
533
534         if(atomic_read(&sc->fw_outstanding) > sc->io_cmds_highwater)
535         sc->io_cmds_highwater++;
536
537     mrsas_fire_cmd(sc, req_desc->addr.u.low, req_desc->addr.u.high);
538     return(0);
539
540 done:
541     xpt_done(ccb);
542     return(0);
543 }
544
545 /**
546  * mrsas_ldio_inq:           Determines if IO is read/write or inquiry   
547  * input:                        pointer to CAM Control Block
548  *
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.
551  */
552 int mrsas_ldio_inq(struct cam_sim *sim, union ccb *ccb) 
553 {
554     struct ccb_scsiio *csio = &(ccb->csio);
555
556     if (cam_sim_bus(sim) == 1)
557         return(0);
558
559     switch (csio->cdb_io.cdb_bytes[0]) {
560         case READ_10:
561         case WRITE_10:
562         case READ_12:
563         case WRITE_12:
564         case READ_6:
565         case WRITE_6:
566         case READ_16:
567         case WRITE_16:
568             return 1;
569         default:
570             return 0;
571     }
572 }
573
574 /**
575  * mrsas_get_mpt_cmd:            Get a cmd from free command pool  
576  * input:                        Adapter instance soft state 
577  *
578  * This function removes an MPT command from the command free list and 
579  * initializes it.
580  */
581 struct mrsas_mpt_cmd* mrsas_get_mpt_cmd(struct mrsas_softc *sc) 
582 {
583     struct mrsas_mpt_cmd *cmd = NULL;
584
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);
589     }
590     memset((uint8_t *)cmd->io_request, 0, MRSAS_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
591     cmd->data = NULL;
592     cmd->length = 0;
593     cmd->flags = 0;
594     cmd->error_code = 0;
595     cmd->load_balance = 0;
596     cmd->ccb_ptr = NULL;
597     mtx_unlock(&sc->mpt_cmd_pool_lock);
598
599     return cmd;
600 }
601
602 /**
603  * mrsas_release_mpt_cmd:      Return a cmd to free command pool  
604  * input:                      Command packet for return to free command pool 
605  *
606  * This function returns an MPT command to the free command list.
607  */
608 void mrsas_release_mpt_cmd(struct mrsas_mpt_cmd *cmd) 
609 {
610     struct mrsas_softc *sc = cmd->sc;
611
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);
616
617     return; 
618 }
619
620 /**
621  * mrsas_get_request_desc:     Get request descriptor from array  
622  * input:                      Adapter instance soft state
623  *                             SMID index 
624  *
625  * This function returns a pointer to the request descriptor.
626  */
627 MRSAS_REQUEST_DESCRIPTOR_UNION *
628 mrsas_get_request_desc(struct mrsas_softc *sc, u_int16_t index)
629 {
630     u_int8_t *p;
631
632     if (index >= sc->max_fw_cmds) {
633         device_printf(sc->mrsas_dev, "Invalid SMID (0x%x)request for desc\n", index);
634         return NULL;
635     }
636     p = sc->req_desc + sizeof(MRSAS_REQUEST_DESCRIPTOR_UNION) * index;
637
638     return (MRSAS_REQUEST_DESCRIPTOR_UNION *)p;
639 }
640
641 /**
642  * mrsas_build_ldio:       Builds an LDIO command  
643  * input:                  Adapter instance soft state
644  *                         Pointer to command packet
645  *                         Pointer to CCB 
646  *
647  * This function builds the LDIO command packet.  It returns 0 if the 
648  * command is built successfully, otherwise it returns a 1. 
649  */
650 int mrsas_build_ldio(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
651                       union ccb *ccb)
652 {
653     struct ccb_hdr *ccb_h = &(ccb->ccb_h);
654     struct ccb_scsiio *csio = &(ccb->csio);
655     u_int32_t device_id;
656     MRSAS_RAID_SCSI_IO_REQUEST *io_request;
657
658     device_id = ccb_h->target_id;
659
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;
664
665     /* just the cdb len, other flags zero, and ORed-in later for FP */ 
666     io_request->IoFlags = csio->cdb_len;
667
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"); 
670         
671     io_request->DataLength = cmd->length;
672
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);
677             return (FAIL);
678         }
679         io_request->RaidContext.numSGE = cmd->sge_count;
680     }
681     else {
682         device_printf(sc->mrsas_dev, "Data map/load failed.\n");
683         return(FAIL);
684     } 
685     return(0);
686 }
687
688 /**
689  * mrsas_setup_io:             Set up data including Fast Path I/O  
690  * input:                      Adapter instance soft state
691  *                             Pointer to command packet
692  *                             Pointer to CCB 
693  *
694  * This function builds the DCDB inquiry command.  It returns 0 if the 
695  * command is built successfully, otherwise it returns a 1. 
696  */
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)
700 {
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;
708      
709     start_lba_lo = 0;
710     start_lba_hi = 0;
711     fp_possible = 0;
712      
713     /*
714      * READ_6 (0x08) or WRITE_6 (0x0A) cdb
715      */
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;
722     }
723     /*
724      * READ_10 (0x28) or WRITE_6 (0x2A) cdb
725      */
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]);
733     }
734     /*
735      * READ_12 (0xA8) or WRITE_12 (0xAA) cdb
736      */
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]);
746     }
747     /*
748      * READ_16 (0x88) or WRITE_16 (0xx8A) cdb
749      */
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]);
763     }
764
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;
769
770     switch (ccb_h->flags & CAM_DIR_MASK) {
771         case CAM_DIR_IN:
772                 io_info.isRead = 1;
773                 break;
774         case CAM_DIR_OUT:
775                 io_info.isRead = 0;
776                 break;
777         case CAM_DIR_NONE:
778         default:
779         mrsas_dprint(sc, MRSAS_TRACE, "From %s : DMA Flag is %d \n", __func__, ccb_h->flags & CAM_DIR_MASK);
780                 break;
781     }
782
783     map_ptr = sc->raidmap_mem[(sc->map_id & 1)];
784     ld_block_size = MR_LdBlockSizeGet(device_id, map_ptr, sc);
785
786     if ((MR_TargetIdToLdGet(device_id, map_ptr) >= MAX_LOGICAL_DRIVES) || 
787             (!sc->fast_path_io)) {
788         io_request->RaidContext.regLockFlags = 0;
789         fp_possible = 0;
790     } 
791     else 
792     {
793         if (MR_BuildRaidContext(sc, &io_info, &io_request->RaidContext, map_ptr))
794             fp_possible = io_info.fpOkForIo;
795     }
796
797     if (fp_possible) {
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);
811         }
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], 
814                 &io_info);
815             cmd->load_balance = MRSAS_LOAD_BALANCE_FLAG;
816         } 
817         else
818             cmd->load_balance = 0;
819         cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
820         io_request->DevHandle = io_info.devHandle;
821     } 
822     else {
823         /* Not FP IO */
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;
833         }
834         io_request->Function = MRSAS_MPI2_FUNCTION_LD_IO_REQUEST;
835         io_request->DevHandle = device_id;
836     }
837     return(0);
838 }
839
840 /**
841  * mrsas_build_dcdb:       Builds an DCDB command  
842  * input:                  Adapter instance soft state
843  *                         Pointer to command packet
844  *                         Pointer to CCB 
845  *
846  * This function builds the DCDB inquiry command.  It returns 0 if the 
847  * command is built successfully, otherwise it returns a 1. 
848  */
849 int mrsas_build_dcdb(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
850                       union ccb *ccb, struct cam_sim *sim)
851 {
852     struct ccb_hdr *ccb_h = &(ccb->ccb_h);
853     u_int32_t device_id;
854     MR_FW_RAID_MAP_ALL *map_ptr;
855     MRSAS_RAID_SCSI_IO_REQUEST *io_request;
856
857     io_request = cmd->io_request;
858     device_id = ccb_h->target_id;
859     map_ptr = sc->raidmap_mem[(sc->map_id & 1)];
860
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;
879     }
880     else {
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);
885     }
886         
887     io_request->RaidContext.VirtualDiskTgtId = device_id;
888     io_request->LUN[1] = ccb_h->target_lun & 0xF;
889     io_request->DataLength = cmd->length;
890
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);
895             return (1);
896         }
897         io_request->RaidContext.numSGE = cmd->sge_count;
898     }
899     else {
900         device_printf(sc->mrsas_dev, "Data map/load failed.\n");
901         return(1);
902     } 
903     return(0);
904 }
905
906 /**
907  * mrsas_map_request:           Map and load data   
908  * input:                       Adapter instance soft state
909  *                              Pointer to command packet
910  *
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 
914  * returned.
915  */
916 int mrsas_map_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd)
917 {
918     u_int32_t retcode = 0;
919     struct cam_sim *sim;
920     int flag = BUS_DMA_NOWAIT;
921
922     sim = xpt_path_sim(cmd->ccb_ptr->ccb_h.path);
923
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);
930         if (retcode)
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);
935         } 
936     }
937     if (cmd->error_code)
938         return(1);
939     return(retcode);
940 }
941
942 /**
943  * mrsas_unmap_request:       Unmap and unload data   
944  * input:                     Adapter instance soft state
945  *                            Pointer to command packet
946  *
947  * This function unmaps and unloads data from OS.
948  */
949 void mrsas_unmap_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd)
950 {
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);
959     }
960 }
961
962 /**
963  * mrsas_data_load_cb:        Callback entry point   
964  * input:                     Pointer to command packet as argument 
965  *                            Pointer to segment
966  *                            Number of segments
967  *                            Error 
968  *
969  * This is the callback function of the bus dma map load.  It builds 
970  * the SG list.  
971  */
972 static void
973 mrsas_data_load_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
974 {
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;
980
981     if (error)
982     {
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;
987             return;
988         }
989     }
990
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");
999         return;
1000     }
1001
1002     io_request = cmd->io_request;
1003     sgl_ptr = (pMpi25IeeeSgeChain64_t)&io_request->SGL;
1004
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;
1009     }
1010
1011     if (nseg != 0) {
1012         for (i=0; i < nseg; i++) {
1013             sgl_ptr->Address = segs[i].ds_addr;
1014             sgl_ptr->Length = segs[i].ds_len;
1015             sgl_ptr->Flags = 0;
1016                 if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY)) {
1017                 if (i == nseg - 1)
1018                         sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1019             }
1020                 sgl_ptr++;
1021                 sg_processed = i + 1;
1022                 /* 
1023              * Prepare chain element 
1024              */
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;
1032                                            else
1033                                                            cmd->io_request->ChainOffset = 0;
1034                                         } else
1035                                                          cmd->io_request->ChainOffset = sc->chain_offset_io_request;
1036                                         sg_chain = sgl_ptr;
1037                                         if ((sc->device_id == MRSAS_INVADER) || (sc->device_id == MRSAS_FURY))
1038                                                  sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1039                                     else
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;
1044                         }
1045         }
1046     }
1047     cmd->sge_count = nseg;
1048 }
1049
1050 /**
1051  * mrsas_freeze_simq:        Freeze SIM queue   
1052  * input:                    Pointer to command packet  
1053  *                           Pointer to SIM
1054  *
1055  * This function freezes the sim queue.
1056  */
1057 static void mrsas_freeze_simq(struct mrsas_mpt_cmd *cmd, struct cam_sim *sim)
1058 {
1059     union ccb   *ccb = (union ccb *)(cmd->ccb_ptr);
1060
1061     xpt_freeze_simq(sim, 1);
1062     ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1063     ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1064 }
1065
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);
1069 }
1070             
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);
1074 }
1075
1076 /**
1077  * mrsas_cmd_done:           Perform remaining command completion 
1078  * input:                    Adapter instance soft state  
1079  *                           Pointer to command packet 
1080  *
1081  * This function calls ummap request and releases the MPT command. 
1082  */
1083 void mrsas_cmd_done(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd)
1084 {
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);
1092 }
1093
1094 /**
1095  * mrsas_poll:               Polling entry point 
1096  * input:                    Pointer to SIM  
1097  *
1098  * This is currently a stub function.
1099  */
1100 static void mrsas_poll(struct cam_sim *sim)
1101 {
1102     struct mrsas_softc *sc = (struct mrsas_softc *)cam_sim_softc(sim);
1103     mrsas_isr((void *) sc);
1104 }
1105
1106 /*
1107  * mrsas_bus_scan:           Perform bus scan 
1108  * input:                    Adapter instance soft state  
1109  *
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 
1112  * automatic. 
1113  */ 
1114 int mrsas_bus_scan(struct mrsas_softc *sc)
1115 {
1116     union ccb *ccb_0;
1117     union ccb *ccb_1;
1118
1119     mtx_lock(&sc->sim_lock);
1120     if ((ccb_0 = xpt_alloc_ccb()) == NULL) {
1121         mtx_unlock(&sc->sim_lock);
1122         return(ENOMEM);
1123     }
1124
1125     if ((ccb_1 = xpt_alloc_ccb()) == NULL) {
1126         xpt_free_ccb(ccb_0);
1127         mtx_unlock(&sc->sim_lock);
1128         return(ENOMEM);
1129     } 
1130
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);
1136         return(EIO);
1137     }
1138
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);
1144         return(EIO);
1145     }
1146
1147     xpt_rescan(ccb_0);
1148     xpt_rescan(ccb_1);
1149     mtx_unlock(&sc->sim_lock);
1150
1151     return(0);
1152 }
1153
1154 /*
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.
1159  */ 
1160 int mrsas_bus_scan_sim(struct mrsas_softc *sc, struct cam_sim *sim)
1161 {
1162     union ccb *ccb;
1163
1164     mtx_lock(&sc->sim_lock);
1165     if ((ccb = xpt_alloc_ccb()) == NULL) {
1166         mtx_unlock(&sc->sim_lock);
1167         return(ENOMEM);
1168     }
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){
1171         xpt_free_ccb(ccb);
1172         mtx_unlock(&sc->sim_lock);
1173         return(EIO);
1174     }
1175     xpt_rescan(ccb);
1176     mtx_unlock(&sc->sim_lock);
1177
1178     return(0);
1179 }