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