]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mrsas/mrsas_cam.c
Import bhyve_graphics into CURRENT. Thanks to all who tested
[FreeBSD/FreeBSD.git] / sys / dev / mrsas / mrsas_cam.c
1 /*
2  * Copyright (c) 2015, AVAGO Tech. All rights reserved. Author: Marian Choy
3  * Copyright (c) 2014, LSI Corp. All rights reserved. Author: Marian Choy
4  * Support: freebsdraid@avagotech.com
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are
8  * met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer. 2. Redistributions
12  * in binary form must reproduce the above copyright notice, this list of
13  * conditions and the following disclaimer in the documentation and/or other
14  * materials provided with the distribution. 3. Neither the name of the
15  * <ORGANIZATION> nor the names of its contributors may be used to endorse or
16  * promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  *
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include "dev/mrsas/mrsas.h"
37
38 #include <cam/cam.h>
39 #include <cam/cam_ccb.h>
40 #include <cam/cam_sim.h>
41 #include <cam/cam_xpt_sim.h>
42 #include <cam/cam_debug.h>
43 #include <cam/cam_periph.h>
44 #include <cam/cam_xpt_periph.h>
45
46 #include <cam/scsi/scsi_all.h>
47 #include <cam/scsi/scsi_message.h>
48 #include <sys/taskqueue.h>
49 #include <sys/kernel.h>
50
51
52 #include <sys/time.h>                   /* XXX for pcpu.h */
53 #include <sys/pcpu.h>                   /* XXX for PCPU_GET */
54
55 #define smp_processor_id()  PCPU_GET(cpuid)
56
57 /*
58  * Function prototypes
59  */
60 int     mrsas_cam_attach(struct mrsas_softc *sc);
61 int     mrsas_find_io_type(struct cam_sim *sim, union ccb *ccb);
62 int     mrsas_bus_scan(struct mrsas_softc *sc);
63 int     mrsas_bus_scan_sim(struct mrsas_softc *sc, struct cam_sim *sim);
64 int 
65 mrsas_map_request(struct mrsas_softc *sc,
66     struct mrsas_mpt_cmd *cmd, union ccb *ccb);
67 int
68 mrsas_build_ldio_rw(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
69     union ccb *ccb);
70 int
71 mrsas_build_ldio_nonrw(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
72     union ccb *ccb);
73 int
74 mrsas_build_syspdio(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
75     union ccb *ccb, struct cam_sim *sim, u_int8_t fp_possible);
76 int
77 mrsas_setup_io(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
78     union ccb *ccb, u_int32_t device_id,
79     MRSAS_RAID_SCSI_IO_REQUEST * io_request);
80 void    mrsas_xpt_freeze(struct mrsas_softc *sc);
81 void    mrsas_xpt_release(struct mrsas_softc *sc);
82 void    mrsas_cam_detach(struct mrsas_softc *sc);
83 void    mrsas_release_mpt_cmd(struct mrsas_mpt_cmd *cmd);
84 void    mrsas_unmap_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd);
85 void    mrsas_cmd_done(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd);
86 void
87 mrsas_fire_cmd(struct mrsas_softc *sc, u_int32_t req_desc_lo,
88     u_int32_t req_desc_hi);
89 void
90 mrsas_set_pd_lba(MRSAS_RAID_SCSI_IO_REQUEST * io_request,
91     u_int8_t cdb_len, struct IO_REQUEST_INFO *io_info, union ccb *ccb,
92     MR_DRV_RAID_MAP_ALL * local_map_ptr, u_int32_t ref_tag,
93     u_int32_t ld_block_size);
94 static void mrsas_freeze_simq(struct mrsas_mpt_cmd *cmd, struct cam_sim *sim);
95 static void mrsas_cam_poll(struct cam_sim *sim);
96 static void mrsas_action(struct cam_sim *sim, union ccb *ccb);
97 static void mrsas_scsiio_timeout(void *data);
98 static void
99 mrsas_data_load_cb(void *arg, bus_dma_segment_t *segs,
100     int nseg, int error);
101 static int32_t
102 mrsas_startio(struct mrsas_softc *sc, struct cam_sim *sim,
103     union ccb *ccb);
104 struct mrsas_mpt_cmd *mrsas_get_mpt_cmd(struct mrsas_softc *sc);
105 MRSAS_REQUEST_DESCRIPTOR_UNION *
106         mrsas_get_request_desc(struct mrsas_softc *sc, u_int16_t index);
107
108 extern u_int16_t MR_TargetIdToLdGet(u_int32_t ldTgtId, MR_DRV_RAID_MAP_ALL * map);
109 extern u_int32_t
110 MR_LdBlockSizeGet(u_int32_t ldTgtId, MR_DRV_RAID_MAP_ALL * map,
111     struct mrsas_softc *sc);
112 extern void mrsas_isr(void *arg);
113 extern void mrsas_aen_handler(struct mrsas_softc *sc);
114 extern u_int8_t
115 MR_BuildRaidContext(struct mrsas_softc *sc,
116     struct IO_REQUEST_INFO *io_info, RAID_CONTEXT * pRAID_Context,
117     MR_DRV_RAID_MAP_ALL * map);
118 extern u_int16_t
119 MR_LdSpanArrayGet(u_int32_t ld, u_int32_t span,
120     MR_DRV_RAID_MAP_ALL * map);
121 extern u_int16_t 
122 mrsas_get_updated_dev_handle(struct mrsas_softc *sc,
123     PLD_LOAD_BALANCE_INFO lbInfo, struct IO_REQUEST_INFO *io_info);
124 extern u_int8_t
125 megasas_get_best_arm(PLD_LOAD_BALANCE_INFO lbInfo, u_int8_t arm,
126     u_int64_t block, u_int32_t count);
127 extern int mrsas_complete_cmd(struct mrsas_softc *sc, u_int32_t MSIxIndex);
128
129
130 /*
131  * mrsas_cam_attach:    Main entry to CAM subsystem
132  * input:                               Adapter instance soft state
133  *
134  * This function is called from mrsas_attach() during initialization to perform
135  * SIM allocations and XPT bus registration.  If the kernel version is 7.4 or
136  * earlier, it would also initiate a bus scan.
137  */
138 int
139 mrsas_cam_attach(struct mrsas_softc *sc)
140 {
141         struct cam_devq *devq;
142         int mrsas_cam_depth;
143
144         mrsas_cam_depth = sc->max_fw_cmds - MRSAS_INTERNAL_CMDS;
145
146         if ((devq = cam_simq_alloc(mrsas_cam_depth)) == NULL) {
147                 device_printf(sc->mrsas_dev, "Cannot allocate SIM queue\n");
148                 return (ENOMEM);
149         }
150         /*
151          * Create SIM for bus 0 and register, also create path
152          */
153         sc->sim_0 = cam_sim_alloc(mrsas_action, mrsas_cam_poll, "mrsas", sc,
154             device_get_unit(sc->mrsas_dev), &sc->sim_lock, mrsas_cam_depth,
155             mrsas_cam_depth, devq);
156         if (sc->sim_0 == NULL) {
157                 cam_simq_free(devq);
158                 device_printf(sc->mrsas_dev, "Cannot register SIM\n");
159                 return (ENXIO);
160         }
161         /* Initialize taskqueue for Event Handling */
162         TASK_INIT(&sc->ev_task, 0, (void *)mrsas_aen_handler, sc);
163         sc->ev_tq = taskqueue_create("mrsas_taskq", M_NOWAIT | M_ZERO,
164             taskqueue_thread_enqueue, &sc->ev_tq);
165
166         /* Run the task queue with lowest priority */
167         taskqueue_start_threads(&sc->ev_tq, 1, 255, "%s taskq",
168             device_get_nameunit(sc->mrsas_dev));
169         mtx_lock(&sc->sim_lock);
170         if (xpt_bus_register(sc->sim_0, sc->mrsas_dev, 0) != CAM_SUCCESS) {
171                 cam_sim_free(sc->sim_0, TRUE);  /* passing true frees the devq */
172                 mtx_unlock(&sc->sim_lock);
173                 return (ENXIO);
174         }
175         if (xpt_create_path(&sc->path_0, NULL, cam_sim_path(sc->sim_0),
176             CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
177                 xpt_bus_deregister(cam_sim_path(sc->sim_0));
178                 cam_sim_free(sc->sim_0, TRUE);  /* passing true will free the
179                                                  * devq */
180                 mtx_unlock(&sc->sim_lock);
181                 return (ENXIO);
182         }
183         mtx_unlock(&sc->sim_lock);
184
185         /*
186          * Create SIM for bus 1 and register, also create path
187          */
188         sc->sim_1 = cam_sim_alloc(mrsas_action, mrsas_cam_poll, "mrsas", sc,
189             device_get_unit(sc->mrsas_dev), &sc->sim_lock, mrsas_cam_depth,
190             mrsas_cam_depth, devq);
191         if (sc->sim_1 == NULL) {
192                 cam_simq_free(devq);
193                 device_printf(sc->mrsas_dev, "Cannot register SIM\n");
194                 return (ENXIO);
195         }
196         mtx_lock(&sc->sim_lock);
197         if (xpt_bus_register(sc->sim_1, sc->mrsas_dev, 1) != CAM_SUCCESS) {
198                 cam_sim_free(sc->sim_1, TRUE);  /* passing true frees the devq */
199                 mtx_unlock(&sc->sim_lock);
200                 return (ENXIO);
201         }
202         if (xpt_create_path(&sc->path_1, NULL, cam_sim_path(sc->sim_1),
203             CAM_TARGET_WILDCARD,
204             CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
205                 xpt_bus_deregister(cam_sim_path(sc->sim_1));
206                 cam_sim_free(sc->sim_1, TRUE);
207                 mtx_unlock(&sc->sim_lock);
208                 return (ENXIO);
209         }
210         mtx_unlock(&sc->sim_lock);
211
212 #if (__FreeBSD_version <= 704000)
213         if (mrsas_bus_scan(sc)) {
214                 device_printf(sc->mrsas_dev, "Error in bus scan.\n");
215                 return (1);
216         }
217 #endif
218         return (0);
219 }
220
221 /*
222  * mrsas_cam_detach:    De-allocates and teardown CAM
223  * input:                               Adapter instance soft state
224  *
225  * De-registers and frees the paths and SIMs.
226  */
227 void
228 mrsas_cam_detach(struct mrsas_softc *sc)
229 {
230         if (sc->ev_tq != NULL)
231                 taskqueue_free(sc->ev_tq);
232         mtx_lock(&sc->sim_lock);
233         if (sc->path_0)
234                 xpt_free_path(sc->path_0);
235         if (sc->sim_0) {
236                 xpt_bus_deregister(cam_sim_path(sc->sim_0));
237                 cam_sim_free(sc->sim_0, FALSE);
238         }
239         if (sc->path_1)
240                 xpt_free_path(sc->path_1);
241         if (sc->sim_1) {
242                 xpt_bus_deregister(cam_sim_path(sc->sim_1));
243                 cam_sim_free(sc->sim_1, TRUE);
244         }
245         mtx_unlock(&sc->sim_lock);
246 }
247
248 /*
249  * mrsas_action:        SIM callback entry point
250  * input:                       pointer to SIM pointer to CAM Control Block
251  *
252  * This function processes CAM subsystem requests. The type of request is stored
253  * in ccb->ccb_h.func_code.  The preprocessor #ifdef is necessary because
254  * ccb->cpi.maxio is not supported for FreeBSD version 7.4 or earlier.
255  */
256 static void
257 mrsas_action(struct cam_sim *sim, union ccb *ccb)
258 {
259         struct mrsas_softc *sc = (struct mrsas_softc *)cam_sim_softc(sim);
260         struct ccb_hdr *ccb_h = &(ccb->ccb_h);
261         u_int32_t device_id;
262
263         switch (ccb->ccb_h.func_code) {
264         case XPT_SCSI_IO:
265                 {
266                         device_id = ccb_h->target_id;
267
268                         /*
269                          * bus 0 is LD, bus 1 is for system-PD
270                          */
271                         if (cam_sim_bus(sim) == 1 &&
272                             sc->pd_list[device_id].driveState != MR_PD_STATE_SYSTEM) {
273                                 ccb->ccb_h.status |= CAM_DEV_NOT_THERE;
274                                 xpt_done(ccb);
275                         } else {
276                                 if (mrsas_startio(sc, sim, ccb)) {
277                                         ccb->ccb_h.status |= CAM_REQ_INVALID;
278                                         xpt_done(ccb);
279                                 }
280                         }
281                         break;
282                 }
283         case XPT_ABORT:
284                 {
285                         ccb->ccb_h.status = CAM_UA_ABORT;
286                         xpt_done(ccb);
287                         break;
288                 }
289         case XPT_RESET_BUS:
290                 {
291                         xpt_done(ccb);
292                         break;
293                 }
294         case XPT_GET_TRAN_SETTINGS:
295                 {
296                         ccb->cts.protocol = PROTO_SCSI;
297                         ccb->cts.protocol_version = SCSI_REV_2;
298                         ccb->cts.transport = XPORT_SPI;
299                         ccb->cts.transport_version = 2;
300                         ccb->cts.xport_specific.spi.valid = CTS_SPI_VALID_DISC;
301                         ccb->cts.xport_specific.spi.flags = CTS_SPI_FLAGS_DISC_ENB;
302                         ccb->cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
303                         ccb->cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
304                         ccb->ccb_h.status = CAM_REQ_CMP;
305                         xpt_done(ccb);
306                         break;
307                 }
308         case XPT_SET_TRAN_SETTINGS:
309                 {
310                         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
311                         xpt_done(ccb);
312                         break;
313                 }
314         case XPT_CALC_GEOMETRY:
315                 {
316                         cam_calc_geometry(&ccb->ccg, 1);
317                         xpt_done(ccb);
318                         break;
319                 }
320         case XPT_PATH_INQ:
321                 {
322                         ccb->cpi.version_num = 1;
323                         ccb->cpi.hba_inquiry = 0;
324                         ccb->cpi.target_sprt = 0;
325 #if (__FreeBSD_version >= 902001)
326                         ccb->cpi.hba_misc = PIM_UNMAPPED;
327 #else
328                         ccb->cpi.hba_misc = 0;
329 #endif
330                         ccb->cpi.hba_eng_cnt = 0;
331                         ccb->cpi.max_lun = MRSAS_SCSI_MAX_LUNS;
332                         ccb->cpi.unit_number = cam_sim_unit(sim);
333                         ccb->cpi.bus_id = cam_sim_bus(sim);
334                         ccb->cpi.initiator_id = MRSAS_SCSI_INITIATOR_ID;
335                         ccb->cpi.base_transfer_speed = 150000;
336                         strncpy(ccb->cpi.sim_vid, "FreeBSD", SIM_IDLEN);
337                         strncpy(ccb->cpi.hba_vid, "AVAGO", HBA_IDLEN);
338                         strncpy(ccb->cpi.dev_name, cam_sim_name(sim), DEV_IDLEN);
339                         ccb->cpi.transport = XPORT_SPI;
340                         ccb->cpi.transport_version = 2;
341                         ccb->cpi.protocol = PROTO_SCSI;
342                         ccb->cpi.protocol_version = SCSI_REV_2;
343                         if (ccb->cpi.bus_id == 0)
344                                 ccb->cpi.max_target = MRSAS_MAX_PD - 1;
345                         else
346                                 ccb->cpi.max_target = MRSAS_MAX_LD_IDS - 1;
347 #if (__FreeBSD_version > 704000)
348                         ccb->cpi.maxio = sc->max_num_sge * MRSAS_PAGE_SIZE;
349 #endif
350                         ccb->ccb_h.status = CAM_REQ_CMP;
351                         xpt_done(ccb);
352                         break;
353                 }
354         default:
355                 {
356                         ccb->ccb_h.status = CAM_REQ_INVALID;
357                         xpt_done(ccb);
358                         break;
359                 }
360         }
361 }
362
363 /*
364  * mrsas_scsiio_timeout:        Callback function for IO timed out
365  * input:                                       mpt command context
366  *
367  * This function will execute after timeout value provided by ccb header from
368  * CAM layer, if timer expires. Driver will run timer for all DCDM and LDIO
369  * coming from CAM layer. This function is callback function for IO timeout
370  * and it runs in no-sleep context. Set do_timedout_reset in Adapter context
371  * so that it will execute OCR/Kill adpter from ocr_thread context.
372  */
373 static void
374 mrsas_scsiio_timeout(void *data)
375 {
376         struct mrsas_mpt_cmd *cmd;
377         struct mrsas_softc *sc;
378
379         cmd = (struct mrsas_mpt_cmd *)data;
380         sc = cmd->sc;
381
382         if (cmd->ccb_ptr == NULL) {
383                 printf("command timeout with NULL ccb\n");
384                 return;
385         }
386         /*
387          * Below callout is dummy entry so that it will be cancelled from
388          * mrsas_cmd_done(). Now Controller will go to OCR/Kill Adapter based
389          * on OCR enable/disable property of Controller from ocr_thread
390          * context.
391          */
392 #if (__FreeBSD_version >= 1000510)
393         callout_reset_sbt(&cmd->cm_callout, SBT_1S * 600, 0,
394             mrsas_scsiio_timeout, cmd, 0);
395 #else
396         callout_reset(&cmd->cm_callout, (600000 * hz) / 1000,
397             mrsas_scsiio_timeout, cmd);
398 #endif
399         sc->do_timedout_reset = SCSIIO_TIMEOUT_OCR;
400         if (sc->ocr_thread_active)
401                 wakeup(&sc->ocr_chan);
402 }
403
404 /*
405  * mrsas_startio:       SCSI IO entry point
406  * input:                       Adapter instance soft state
407  *                                      pointer to CAM Control Block
408  *
409  * This function is the SCSI IO entry point and it initiates IO processing. It
410  * copies the IO and depending if the IO is read/write or inquiry, it would
411  * call mrsas_build_ldio() or mrsas_build_dcdb(), respectively.  It returns 0
412  * if the command is sent to firmware successfully, otherwise it returns 1.
413  */
414 static int32_t
415 mrsas_startio(struct mrsas_softc *sc, struct cam_sim *sim,
416     union ccb *ccb)
417 {
418         struct mrsas_mpt_cmd *cmd;
419         struct ccb_hdr *ccb_h = &(ccb->ccb_h);
420         struct ccb_scsiio *csio = &(ccb->csio);
421         MRSAS_REQUEST_DESCRIPTOR_UNION *req_desc;
422         u_int8_t cmd_type;
423
424         if ((csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
425                 ccb->ccb_h.status = CAM_REQ_CMP;
426                 xpt_done(ccb);
427                 return (0);
428         }
429         ccb_h->status |= CAM_SIM_QUEUED;
430         cmd = mrsas_get_mpt_cmd(sc);
431
432         if (!cmd) {
433                 ccb_h->status |= CAM_REQUEUE_REQ;
434                 xpt_done(ccb);
435                 return (0);
436         }
437         if ((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
438                 if (ccb_h->flags & CAM_DIR_IN)
439                         cmd->flags |= MRSAS_DIR_IN;
440                 if (ccb_h->flags & CAM_DIR_OUT)
441                         cmd->flags |= MRSAS_DIR_OUT;
442         } else
443                 cmd->flags = MRSAS_DIR_NONE;    /* no data */
444
445 /* For FreeBSD 9.2 and higher */
446 #if (__FreeBSD_version >= 902001)
447         /*
448          * XXX We don't yet support physical addresses here.
449          */
450         switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) {
451         case CAM_DATA_PADDR:
452         case CAM_DATA_SG_PADDR:
453                 device_printf(sc->mrsas_dev, "%s: physical addresses not supported\n",
454                     __func__);
455                 mrsas_release_mpt_cmd(cmd);
456                 ccb_h->status = CAM_REQ_INVALID;
457                 ccb_h->status &= ~CAM_SIM_QUEUED;
458                 goto done;
459         case CAM_DATA_SG:
460                 device_printf(sc->mrsas_dev, "%s: scatter gather is not supported\n",
461                     __func__);
462                 mrsas_release_mpt_cmd(cmd);
463                 ccb_h->status = CAM_REQ_INVALID;
464                 goto done;
465         case CAM_DATA_VADDR:
466                 if (csio->dxfer_len > (sc->max_num_sge * MRSAS_PAGE_SIZE)) {
467                         mrsas_release_mpt_cmd(cmd);
468                         ccb_h->status = CAM_REQ_TOO_BIG;
469                         goto done;
470                 }
471                 cmd->length = csio->dxfer_len;
472                 if (cmd->length)
473                         cmd->data = csio->data_ptr;
474                 break;
475         case CAM_DATA_BIO:
476                 if (csio->dxfer_len > (sc->max_num_sge * MRSAS_PAGE_SIZE)) {
477                         mrsas_release_mpt_cmd(cmd);
478                         ccb_h->status = CAM_REQ_TOO_BIG;
479                         goto done;
480                 }
481                 cmd->length = csio->dxfer_len;
482                 if (cmd->length)
483                         cmd->data = csio->data_ptr;
484                 break;
485         default:
486                 ccb->ccb_h.status = CAM_REQ_INVALID;
487                 goto done;
488         }
489 #else
490         if (!(ccb_h->flags & CAM_DATA_PHYS)) {  /* Virtual data address */
491                 if (!(ccb_h->flags & CAM_SCATTER_VALID)) {
492                         if (csio->dxfer_len > (sc->max_num_sge * MRSAS_PAGE_SIZE)) {
493                                 mrsas_release_mpt_cmd(cmd);
494                                 ccb_h->status = CAM_REQ_TOO_BIG;
495                                 goto done;
496                         }
497                         cmd->length = csio->dxfer_len;
498                         if (cmd->length)
499                                 cmd->data = csio->data_ptr;
500                 } else {
501                         mrsas_release_mpt_cmd(cmd);
502                         ccb_h->status = CAM_REQ_INVALID;
503                         goto done;
504                 }
505         } else {                        /* Data addresses are physical. */
506                 mrsas_release_mpt_cmd(cmd);
507                 ccb_h->status = CAM_REQ_INVALID;
508                 ccb_h->status &= ~CAM_SIM_QUEUED;
509                 goto done;
510         }
511 #endif
512         /* save ccb ptr */
513         cmd->ccb_ptr = ccb;
514
515         req_desc = mrsas_get_request_desc(sc, (cmd->index) - 1);
516         if (!req_desc) {
517                 device_printf(sc->mrsas_dev, "Cannot get request_descriptor.\n");
518                 return (FAIL);
519         }
520         memset(req_desc, 0, sizeof(MRSAS_REQUEST_DESCRIPTOR_UNION));
521         cmd->request_desc = req_desc;
522
523         if (ccb_h->flags & CAM_CDB_POINTER)
524                 bcopy(csio->cdb_io.cdb_ptr, cmd->io_request->CDB.CDB32, csio->cdb_len);
525         else
526                 bcopy(csio->cdb_io.cdb_bytes, cmd->io_request->CDB.CDB32, csio->cdb_len);
527         mtx_lock(&sc->raidmap_lock);
528
529         /* Check for IO type READ-WRITE targeted for Logical Volume */
530         cmd_type = mrsas_find_io_type(sim, ccb);
531         switch (cmd_type) {
532         case READ_WRITE_LDIO:
533                 /* Build READ-WRITE IO for Logical Volume  */
534                 if (mrsas_build_ldio_rw(sc, cmd, ccb)) {
535                         device_printf(sc->mrsas_dev, "Build RW LDIO failed.\n");
536                         mtx_unlock(&sc->raidmap_lock);
537                         return (1);
538                 }
539                 break;
540         case NON_READ_WRITE_LDIO:
541                 /* Build NON READ-WRITE IO for Logical Volume  */
542                 if (mrsas_build_ldio_nonrw(sc, cmd, ccb)) {
543                         device_printf(sc->mrsas_dev, "Build NON-RW LDIO failed.\n");
544                         mtx_unlock(&sc->raidmap_lock);
545                         return (1);
546                 }
547                 break;
548         case READ_WRITE_SYSPDIO:
549         case NON_READ_WRITE_SYSPDIO:
550                 if (sc->secure_jbod_support &&
551                     (cmd_type == NON_READ_WRITE_SYSPDIO)) {
552                         /* Build NON-RW IO for JBOD */
553                         if (mrsas_build_syspdio(sc, cmd, ccb, sim, 0)) {
554                                 device_printf(sc->mrsas_dev,
555                                     "Build SYSPDIO failed.\n");
556                                 mtx_unlock(&sc->raidmap_lock);
557                                 return (1);
558                         }
559                 } else {
560                         /* Build RW IO for JBOD */
561                         if (mrsas_build_syspdio(sc, cmd, ccb, sim, 1)) {
562                                 device_printf(sc->mrsas_dev,
563                                     "Build SYSPDIO failed.\n");
564                                 mtx_unlock(&sc->raidmap_lock);
565                                 return (1);
566                         }
567                 }
568         }
569         mtx_unlock(&sc->raidmap_lock);
570
571         if (cmd->flags == MRSAS_DIR_IN) /* from device */
572                 cmd->io_request->Control |= MPI2_SCSIIO_CONTROL_READ;
573         else if (cmd->flags == MRSAS_DIR_OUT)   /* to device */
574                 cmd->io_request->Control |= MPI2_SCSIIO_CONTROL_WRITE;
575
576         cmd->io_request->SGLFlags = MPI2_SGE_FLAGS_64_BIT_ADDRESSING;
577         cmd->io_request->SGLOffset0 = offsetof(MRSAS_RAID_SCSI_IO_REQUEST, SGL) / 4;
578         cmd->io_request->SenseBufferLowAddress = cmd->sense_phys_addr;
579         cmd->io_request->SenseBufferLength = MRSAS_SCSI_SENSE_BUFFERSIZE;
580
581         req_desc = cmd->request_desc;
582         req_desc->SCSIIO.SMID = cmd->index;
583
584         /*
585          * Start timer for IO timeout. Default timeout value is 90 second.
586          */
587 #if (__FreeBSD_version >= 1000510)
588         callout_reset_sbt(&cmd->cm_callout, SBT_1S * 600, 0,
589             mrsas_scsiio_timeout, cmd, 0);
590 #else
591         callout_reset(&cmd->cm_callout, (600000 * hz) / 1000,
592             mrsas_scsiio_timeout, cmd);
593 #endif
594         mrsas_atomic_inc(&sc->fw_outstanding);
595
596         if (mrsas_atomic_read(&sc->fw_outstanding) > sc->io_cmds_highwater)
597                 sc->io_cmds_highwater++;
598
599         mrsas_fire_cmd(sc, req_desc->addr.u.low, req_desc->addr.u.high);
600         return (0);
601
602 done:
603         xpt_done(ccb);
604         return (0);
605 }
606
607 /*
608  * mrsas_find_io_type:  Determines if IO is read/write or inquiry
609  * input:                       pointer to CAM Control Block
610  *
611  * This function determines if the IO is read/write or inquiry.  It returns a 1
612  * if the IO is read/write and 0 if it is inquiry.
613  */
614 int 
615 mrsas_find_io_type(struct cam_sim *sim, union ccb *ccb)
616 {
617         struct ccb_scsiio *csio = &(ccb->csio);
618
619         switch (csio->cdb_io.cdb_bytes[0]) {
620         case READ_10:
621         case WRITE_10:
622         case READ_12:
623         case WRITE_12:
624         case READ_6:
625         case WRITE_6:
626         case READ_16:
627         case WRITE_16:
628                 return (cam_sim_bus(sim) ?
629                     READ_WRITE_SYSPDIO : READ_WRITE_LDIO);
630         default:
631                 return (cam_sim_bus(sim) ?
632                     NON_READ_WRITE_SYSPDIO : NON_READ_WRITE_LDIO);
633         }
634 }
635
636 /*
637  * mrsas_get_mpt_cmd:   Get a cmd from free command pool
638  * input:                               Adapter instance soft state
639  *
640  * This function removes an MPT command from the command free list and
641  * initializes it.
642  */
643 struct mrsas_mpt_cmd *
644 mrsas_get_mpt_cmd(struct mrsas_softc *sc)
645 {
646         struct mrsas_mpt_cmd *cmd = NULL;
647
648         mtx_lock(&sc->mpt_cmd_pool_lock);
649         if (!TAILQ_EMPTY(&sc->mrsas_mpt_cmd_list_head)) {
650                 cmd = TAILQ_FIRST(&sc->mrsas_mpt_cmd_list_head);
651                 TAILQ_REMOVE(&sc->mrsas_mpt_cmd_list_head, cmd, next);
652         } else {
653                 goto out;
654         }
655
656         memset((uint8_t *)cmd->io_request, 0, MRSAS_MPI2_RAID_DEFAULT_IO_FRAME_SIZE);
657         cmd->data = NULL;
658         cmd->length = 0;
659         cmd->flags = 0;
660         cmd->error_code = 0;
661         cmd->load_balance = 0;
662         cmd->ccb_ptr = NULL;
663
664 out:
665         mtx_unlock(&sc->mpt_cmd_pool_lock);
666         return cmd;
667 }
668
669 /*
670  * mrsas_release_mpt_cmd:       Return a cmd to free command pool
671  * input:                                       Command packet for return to free command pool
672  *
673  * This function returns an MPT command to the free command list.
674  */
675 void
676 mrsas_release_mpt_cmd(struct mrsas_mpt_cmd *cmd)
677 {
678         struct mrsas_softc *sc = cmd->sc;
679
680         mtx_lock(&sc->mpt_cmd_pool_lock);
681         cmd->sync_cmd_idx = (u_int32_t)MRSAS_ULONG_MAX;
682         TAILQ_INSERT_TAIL(&(sc->mrsas_mpt_cmd_list_head), cmd, next);
683         mtx_unlock(&sc->mpt_cmd_pool_lock);
684
685         return;
686 }
687
688 /*
689  * mrsas_get_request_desc:      Get request descriptor from array
690  * input:                                       Adapter instance soft state
691  *                                                      SMID index
692  *
693  * This function returns a pointer to the request descriptor.
694  */
695 MRSAS_REQUEST_DESCRIPTOR_UNION *
696 mrsas_get_request_desc(struct mrsas_softc *sc, u_int16_t index)
697 {
698         u_int8_t *p;
699
700         if (index >= sc->max_fw_cmds) {
701                 device_printf(sc->mrsas_dev, "Invalid SMID (0x%x)request for desc\n", index);
702                 return NULL;
703         }
704         p = sc->req_desc + sizeof(MRSAS_REQUEST_DESCRIPTOR_UNION) * index;
705
706         return (MRSAS_REQUEST_DESCRIPTOR_UNION *) p;
707 }
708
709 /*
710  * mrsas_build_ldio_rw: Builds an LDIO command
711  * input:                               Adapter instance soft state
712  *                                              Pointer to command packet
713  *                                              Pointer to CCB
714  *
715  * This function builds the LDIO command packet.  It returns 0 if the command is
716  * built successfully, otherwise it returns a 1.
717  */
718 int
719 mrsas_build_ldio_rw(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
720     union ccb *ccb)
721 {
722         struct ccb_hdr *ccb_h = &(ccb->ccb_h);
723         struct ccb_scsiio *csio = &(ccb->csio);
724         u_int32_t device_id;
725         MRSAS_RAID_SCSI_IO_REQUEST *io_request;
726
727         device_id = ccb_h->target_id;
728
729         io_request = cmd->io_request;
730         io_request->RaidContext.VirtualDiskTgtId = device_id;
731         io_request->RaidContext.status = 0;
732         io_request->RaidContext.exStatus = 0;
733
734         /* just the cdb len, other flags zero, and ORed-in later for FP */
735         io_request->IoFlags = csio->cdb_len;
736
737         if (mrsas_setup_io(sc, cmd, ccb, device_id, io_request) != SUCCESS)
738                 device_printf(sc->mrsas_dev, "Build ldio or fpio error\n");
739
740         io_request->DataLength = cmd->length;
741
742         if (mrsas_map_request(sc, cmd, ccb) == SUCCESS) {
743                 if (cmd->sge_count > sc->max_num_sge) {
744                         device_printf(sc->mrsas_dev, "Error: sge_count (0x%x) exceeds"
745                             "max (0x%x) allowed\n", cmd->sge_count, sc->max_num_sge);
746                         return (FAIL);
747                 }
748                 /*
749                  * numSGE store lower 8 bit of sge_count. numSGEExt store
750                  * higher 8 bit of sge_count
751                  */
752                 io_request->RaidContext.numSGE = cmd->sge_count;
753                 io_request->RaidContext.numSGEExt = (uint8_t)(cmd->sge_count >> 8);
754
755         } else {
756                 device_printf(sc->mrsas_dev, "Data map/load failed.\n");
757                 return (FAIL);
758         }
759         return (0);
760 }
761
762 /*
763  * mrsas_setup_io:      Set up data including Fast Path I/O
764  * input:                       Adapter instance soft state
765  *                                      Pointer to command packet
766  *                                      Pointer to CCB
767  *
768  * This function builds the DCDB inquiry command.  It returns 0 if the command
769  * is built successfully, otherwise it returns a 1.
770  */
771 int
772 mrsas_setup_io(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
773     union ccb *ccb, u_int32_t device_id,
774     MRSAS_RAID_SCSI_IO_REQUEST * io_request)
775 {
776         struct ccb_hdr *ccb_h = &(ccb->ccb_h);
777         struct ccb_scsiio *csio = &(ccb->csio);
778         struct IO_REQUEST_INFO io_info;
779         MR_DRV_RAID_MAP_ALL *map_ptr;
780         u_int8_t fp_possible;
781         u_int32_t start_lba_hi, start_lba_lo, ld_block_size;
782         u_int32_t datalength = 0;
783
784         start_lba_lo = 0;
785         start_lba_hi = 0;
786         fp_possible = 0;
787
788         /*
789          * READ_6 (0x08) or WRITE_6 (0x0A) cdb
790          */
791         if (csio->cdb_len == 6) {
792                 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[4];
793                 start_lba_lo = ((u_int32_t)csio->cdb_io.cdb_bytes[1] << 16) |
794                     ((u_int32_t)csio->cdb_io.cdb_bytes[2] << 8) |
795                     (u_int32_t)csio->cdb_io.cdb_bytes[3];
796                 start_lba_lo &= 0x1FFFFF;
797         }
798         /*
799          * READ_10 (0x28) or WRITE_6 (0x2A) cdb
800          */
801         else if (csio->cdb_len == 10) {
802                 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[8] |
803                     ((u_int32_t)csio->cdb_io.cdb_bytes[7] << 8);
804                 start_lba_lo = ((u_int32_t)csio->cdb_io.cdb_bytes[2] << 24) |
805                     ((u_int32_t)csio->cdb_io.cdb_bytes[3] << 16) |
806                     (u_int32_t)csio->cdb_io.cdb_bytes[4] << 8 |
807                     ((u_int32_t)csio->cdb_io.cdb_bytes[5]);
808         }
809         /*
810          * READ_12 (0xA8) or WRITE_12 (0xAA) cdb
811          */
812         else if (csio->cdb_len == 12) {
813                 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[6] << 24 |
814                     ((u_int32_t)csio->cdb_io.cdb_bytes[7] << 16) |
815                     ((u_int32_t)csio->cdb_io.cdb_bytes[8] << 8) |
816                     ((u_int32_t)csio->cdb_io.cdb_bytes[9]);
817                 start_lba_lo = ((u_int32_t)csio->cdb_io.cdb_bytes[2] << 24) |
818                     ((u_int32_t)csio->cdb_io.cdb_bytes[3] << 16) |
819                     (u_int32_t)csio->cdb_io.cdb_bytes[4] << 8 |
820                     ((u_int32_t)csio->cdb_io.cdb_bytes[5]);
821         }
822         /*
823          * READ_16 (0x88) or WRITE_16 (0xx8A) cdb
824          */
825         else if (csio->cdb_len == 16) {
826                 datalength = (u_int32_t)csio->cdb_io.cdb_bytes[10] << 24 |
827                     ((u_int32_t)csio->cdb_io.cdb_bytes[11] << 16) |
828                     ((u_int32_t)csio->cdb_io.cdb_bytes[12] << 8) |
829                     ((u_int32_t)csio->cdb_io.cdb_bytes[13]);
830                 start_lba_lo = ((u_int32_t)csio->cdb_io.cdb_bytes[6] << 24) |
831                     ((u_int32_t)csio->cdb_io.cdb_bytes[7] << 16) |
832                     (u_int32_t)csio->cdb_io.cdb_bytes[8] << 8 |
833                     ((u_int32_t)csio->cdb_io.cdb_bytes[9]);
834                 start_lba_hi = ((u_int32_t)csio->cdb_io.cdb_bytes[2] << 24) |
835                     ((u_int32_t)csio->cdb_io.cdb_bytes[3] << 16) |
836                     (u_int32_t)csio->cdb_io.cdb_bytes[4] << 8 |
837                     ((u_int32_t)csio->cdb_io.cdb_bytes[5]);
838         }
839         memset(&io_info, 0, sizeof(struct IO_REQUEST_INFO));
840         io_info.ldStartBlock = ((u_int64_t)start_lba_hi << 32) | start_lba_lo;
841         io_info.numBlocks = datalength;
842         io_info.ldTgtId = device_id;
843
844         switch (ccb_h->flags & CAM_DIR_MASK) {
845         case CAM_DIR_IN:
846                 io_info.isRead = 1;
847                 break;
848         case CAM_DIR_OUT:
849                 io_info.isRead = 0;
850                 break;
851         case CAM_DIR_NONE:
852         default:
853                 mrsas_dprint(sc, MRSAS_TRACE, "From %s : DMA Flag is %d \n", __func__, ccb_h->flags & CAM_DIR_MASK);
854                 break;
855         }
856
857         map_ptr = sc->ld_drv_map[(sc->map_id & 1)];
858         ld_block_size = MR_LdBlockSizeGet(device_id, map_ptr, sc);
859
860         if ((MR_TargetIdToLdGet(device_id, map_ptr) >= MAX_LOGICAL_DRIVES_EXT) ||
861             (!sc->fast_path_io)) {
862                 io_request->RaidContext.regLockFlags = 0;
863                 fp_possible = 0;
864         } else {
865                 if (MR_BuildRaidContext(sc, &io_info, &io_request->RaidContext, map_ptr))
866                         fp_possible = io_info.fpOkForIo;
867         }
868
869         cmd->request_desc->SCSIIO.MSIxIndex =
870             sc->msix_vectors ? smp_processor_id() % sc->msix_vectors : 0;
871
872
873         if (fp_possible) {
874                 mrsas_set_pd_lba(io_request, csio->cdb_len, &io_info, ccb, map_ptr,
875                     start_lba_lo, ld_block_size);
876                 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
877                 cmd->request_desc->SCSIIO.RequestFlags =
878                     (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
879                     MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
880                 if ((sc->device_id == MRSAS_INVADER) ||
881                     (sc->device_id == MRSAS_FURY) ||
882                     (sc->device_id == MRSAS_INTRUDER) ||
883                     (sc->device_id == MRSAS_INTRUDER_24) ||
884                     (sc->device_id == MRSAS_CUTLASS_52) ||
885                     (sc->device_id == MRSAS_CUTLASS_53)) {
886                         if (io_request->RaidContext.regLockFlags == REGION_TYPE_UNUSED)
887                                 cmd->request_desc->SCSIIO.RequestFlags =
888                                     (MRSAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
889                                     MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
890                         io_request->RaidContext.Type = MPI2_TYPE_CUDA;
891                         io_request->RaidContext.nseg = 0x1;
892                         io_request->IoFlags |= MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
893                         io_request->RaidContext.regLockFlags |=
894                             (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
895                             MR_RL_FLAGS_SEQ_NUM_ENABLE);
896                 }
897                 if ((sc->load_balance_info[device_id].loadBalanceFlag) &&
898                     (io_info.isRead)) {
899                         io_info.devHandle =
900                             mrsas_get_updated_dev_handle(sc,
901                             &sc->load_balance_info[device_id], &io_info);
902                         cmd->load_balance = MRSAS_LOAD_BALANCE_FLAG;
903                         cmd->pd_r1_lb = io_info.pd_after_lb;
904                 } else
905                         cmd->load_balance = 0;
906                 cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
907                 io_request->DevHandle = io_info.devHandle;
908         } else {
909                 /* Not FP IO */
910                 io_request->RaidContext.timeoutValue = map_ptr->raidMap.fpPdIoTimeoutSec;
911                 cmd->request_desc->SCSIIO.RequestFlags =
912                     (MRSAS_REQ_DESCRIPT_FLAGS_LD_IO <<
913                     MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
914                 if ((sc->device_id == MRSAS_INVADER) ||
915                     (sc->device_id == MRSAS_FURY) ||
916                     (sc->device_id == MRSAS_INTRUDER) ||
917                     (sc->device_id == MRSAS_INTRUDER_24) ||
918                     (sc->device_id == MRSAS_CUTLASS_52) ||
919                     (sc->device_id == MRSAS_CUTLASS_53)) {
920                         if (io_request->RaidContext.regLockFlags == REGION_TYPE_UNUSED)
921                                 cmd->request_desc->SCSIIO.RequestFlags =
922                                     (MRSAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
923                                     MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
924                         io_request->RaidContext.Type = MPI2_TYPE_CUDA;
925                         io_request->RaidContext.regLockFlags |=
926                             (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
927                             MR_RL_FLAGS_SEQ_NUM_ENABLE);
928                         io_request->RaidContext.nseg = 0x1;
929                 }
930                 io_request->Function = MRSAS_MPI2_FUNCTION_LD_IO_REQUEST;
931                 io_request->DevHandle = device_id;
932         }
933         return (0);
934 }
935
936 /*
937  * mrsas_build_ldio_nonrw:      Builds an LDIO command
938  * input:                               Adapter instance soft state
939  *                                              Pointer to command packet
940  *                                              Pointer to CCB
941  *
942  * This function builds the LDIO command packet.  It returns 0 if the command is
943  * built successfully, otherwise it returns a 1.
944  */
945 int
946 mrsas_build_ldio_nonrw(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
947     union ccb *ccb)
948 {
949         struct ccb_hdr *ccb_h = &(ccb->ccb_h);
950         u_int32_t device_id;
951         MRSAS_RAID_SCSI_IO_REQUEST *io_request;
952
953         io_request = cmd->io_request;
954         device_id = ccb_h->target_id;
955
956         /* FW path for LD Non-RW (SCSI management commands) */
957         io_request->Function = MRSAS_MPI2_FUNCTION_LD_IO_REQUEST;
958         io_request->DevHandle = device_id;
959         cmd->request_desc->SCSIIO.RequestFlags =
960             (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
961             MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
962
963         io_request->RaidContext.VirtualDiskTgtId = device_id;
964         io_request->LUN[1] = ccb_h->target_lun & 0xF;
965         io_request->DataLength = cmd->length;
966
967         if (mrsas_map_request(sc, cmd, ccb) == SUCCESS) {
968                 if (cmd->sge_count > sc->max_num_sge) {
969                         device_printf(sc->mrsas_dev, "Error: sge_count (0x%x) exceeds"
970                             "max (0x%x) allowed\n", cmd->sge_count, sc->max_num_sge);
971                         return (1);
972                 }
973                 /*
974                  * numSGE store lower 8 bit of sge_count. numSGEExt store
975                  * higher 8 bit of sge_count
976                  */
977                 io_request->RaidContext.numSGE = cmd->sge_count;
978                 io_request->RaidContext.numSGEExt = (uint8_t)(cmd->sge_count >> 8);
979         } else {
980                 device_printf(sc->mrsas_dev, "Data map/load failed.\n");
981                 return (1);
982         }
983         return (0);
984 }
985
986 /*
987  * mrsas_build_syspdio: Builds an DCDB command
988  * input:                               Adapter instance soft state
989  *                                              Pointer to command packet
990  *                                              Pointer to CCB
991  *
992  * This function builds the DCDB inquiry command.  It returns 0 if the command
993  * is built successfully, otherwise it returns a 1.
994  */
995 int
996 mrsas_build_syspdio(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
997     union ccb *ccb, struct cam_sim *sim, u_int8_t fp_possible)
998 {
999         struct ccb_hdr *ccb_h = &(ccb->ccb_h);
1000         u_int32_t device_id;
1001         MR_DRV_RAID_MAP_ALL *local_map_ptr;
1002         MRSAS_RAID_SCSI_IO_REQUEST *io_request;
1003         struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1004
1005         pd_sync = (void *)sc->jbodmap_mem[(sc->pd_seq_map_id - 1) & 1];
1006
1007         io_request = cmd->io_request;
1008         device_id = ccb_h->target_id;
1009         local_map_ptr = sc->ld_drv_map[(sc->map_id & 1)];
1010         io_request->RaidContext.RAIDFlags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
1011             << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1012         io_request->RaidContext.regLockFlags = 0;
1013         io_request->RaidContext.regLockRowLBA = 0;
1014         io_request->RaidContext.regLockLength = 0;
1015
1016         /* If FW supports PD sequence number */
1017         if (sc->use_seqnum_jbod_fp &&
1018             sc->pd_list[device_id].driveType == 0x00) {
1019                 //printf("Using Drv seq num\n");
1020                 io_request->RaidContext.VirtualDiskTgtId = device_id + 255;
1021                 io_request->RaidContext.configSeqNum = pd_sync->seq[device_id].seqNum;
1022                 io_request->DevHandle = pd_sync->seq[device_id].devHandle;
1023                 io_request->RaidContext.regLockFlags |=
1024                     (MR_RL_FLAGS_SEQ_NUM_ENABLE | MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
1025                 io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1026                 io_request->RaidContext.nseg = 0x1;
1027         } else if (sc->fast_path_io) {
1028                 //printf("Using LD RAID map\n");
1029                 io_request->RaidContext.VirtualDiskTgtId = device_id;
1030                 io_request->RaidContext.configSeqNum = 0;
1031                 local_map_ptr = sc->ld_drv_map[(sc->map_id & 1)];
1032                 io_request->DevHandle =
1033                     local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1034         } else {
1035                 //printf("Using FW PATH\n");
1036                 /* Want to send all IO via FW path */
1037                 io_request->RaidContext.VirtualDiskTgtId = device_id;
1038                 io_request->RaidContext.configSeqNum = 0;
1039                 io_request->DevHandle = 0xFFFF;
1040         }
1041
1042         cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
1043         cmd->request_desc->SCSIIO.MSIxIndex =
1044             sc->msix_vectors ? smp_processor_id() % sc->msix_vectors : 0;
1045
1046         if (!fp_possible) {
1047                 /* system pd firmware path */
1048                 io_request->Function = MRSAS_MPI2_FUNCTION_LD_IO_REQUEST;
1049                 cmd->request_desc->SCSIIO.RequestFlags =
1050                     (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1051                     MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1052                 io_request->RaidContext.timeoutValue =
1053                     local_map_ptr->raidMap.fpPdIoTimeoutSec;
1054                 io_request->RaidContext.VirtualDiskTgtId = device_id;
1055         } else {
1056                 /* system pd fast path */
1057                 io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1058                 io_request->RaidContext.timeoutValue = local_map_ptr->raidMap.fpPdIoTimeoutSec;
1059
1060                 /*
1061                  * NOTE - For system pd RW cmds only IoFlags will be FAST_PATH
1062                  * Because the NON RW cmds will now go via FW Queue
1063                  * and not the Exception queue
1064                  */
1065                 io_request->IoFlags |= MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
1066
1067                 cmd->request_desc->SCSIIO.RequestFlags =
1068                     (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1069                     MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1070         }
1071
1072         io_request->LUN[1] = ccb_h->target_lun & 0xF;
1073         io_request->DataLength = cmd->length;
1074
1075         if (mrsas_map_request(sc, cmd, ccb) == SUCCESS) {
1076                 if (cmd->sge_count > sc->max_num_sge) {
1077                         device_printf(sc->mrsas_dev, "Error: sge_count (0x%x) exceeds"
1078                             "max (0x%x) allowed\n", cmd->sge_count, sc->max_num_sge);
1079                         return (1);
1080                 }
1081                 /*
1082                  * numSGE store lower 8 bit of sge_count. numSGEExt store
1083                  * higher 8 bit of sge_count
1084                  */
1085                 io_request->RaidContext.numSGE = cmd->sge_count;
1086                 io_request->RaidContext.numSGEExt = (uint8_t)(cmd->sge_count >> 8);
1087         } else {
1088                 device_printf(sc->mrsas_dev, "Data map/load failed.\n");
1089                 return (1);
1090         }
1091         return (0);
1092 }
1093
1094 /*
1095  * mrsas_map_request:   Map and load data
1096  * input:                               Adapter instance soft state
1097  *                                              Pointer to command packet
1098  *
1099  * For data from OS, map and load the data buffer into bus space.  The SG list
1100  * is built in the callback.  If the  bus dmamap load is not successful,
1101  * cmd->error_code will contain the  error code and a 1 is returned.
1102  */
1103 int 
1104 mrsas_map_request(struct mrsas_softc *sc,
1105     struct mrsas_mpt_cmd *cmd, union ccb *ccb)
1106 {
1107         u_int32_t retcode = 0;
1108         struct cam_sim *sim;
1109
1110         sim = xpt_path_sim(cmd->ccb_ptr->ccb_h.path);
1111
1112         if (cmd->data != NULL) {
1113                 /* Map data buffer into bus space */
1114                 mtx_lock(&sc->io_lock);
1115 #if (__FreeBSD_version >= 902001)
1116                 retcode = bus_dmamap_load_ccb(sc->data_tag, cmd->data_dmamap, ccb,
1117                     mrsas_data_load_cb, cmd, 0);
1118 #else
1119                 retcode = bus_dmamap_load(sc->data_tag, cmd->data_dmamap, cmd->data,
1120                     cmd->length, mrsas_data_load_cb, cmd, BUS_DMA_NOWAIT);
1121 #endif
1122                 mtx_unlock(&sc->io_lock);
1123                 if (retcode)
1124                         device_printf(sc->mrsas_dev, "bus_dmamap_load(): retcode = %d\n", retcode);
1125                 if (retcode == EINPROGRESS) {
1126                         device_printf(sc->mrsas_dev, "request load in progress\n");
1127                         mrsas_freeze_simq(cmd, sim);
1128                 }
1129         }
1130         if (cmd->error_code)
1131                 return (1);
1132         return (retcode);
1133 }
1134
1135 /*
1136  * mrsas_unmap_request: Unmap and unload data
1137  * input:                               Adapter instance soft state
1138  *                                              Pointer to command packet
1139  *
1140  * This function unmaps and unloads data from OS.
1141  */
1142 void
1143 mrsas_unmap_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd)
1144 {
1145         if (cmd->data != NULL) {
1146                 if (cmd->flags & MRSAS_DIR_IN)
1147                         bus_dmamap_sync(sc->data_tag, cmd->data_dmamap, BUS_DMASYNC_POSTREAD);
1148                 if (cmd->flags & MRSAS_DIR_OUT)
1149                         bus_dmamap_sync(sc->data_tag, cmd->data_dmamap, BUS_DMASYNC_POSTWRITE);
1150                 mtx_lock(&sc->io_lock);
1151                 bus_dmamap_unload(sc->data_tag, cmd->data_dmamap);
1152                 mtx_unlock(&sc->io_lock);
1153         }
1154 }
1155
1156 /*
1157  * mrsas_data_load_cb:  Callback entry point
1158  * input:                               Pointer to command packet as argument
1159  *                                              Pointer to segment
1160  *                                              Number of segments Error
1161  *
1162  * This is the callback function of the bus dma map load.  It builds the SG
1163  * list.
1164  */
1165 static void
1166 mrsas_data_load_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1167 {
1168         struct mrsas_mpt_cmd *cmd = (struct mrsas_mpt_cmd *)arg;
1169         struct mrsas_softc *sc = cmd->sc;
1170         MRSAS_RAID_SCSI_IO_REQUEST *io_request;
1171         pMpi25IeeeSgeChain64_t sgl_ptr;
1172         int i = 0, sg_processed = 0;
1173
1174         if (error) {
1175                 cmd->error_code = error;
1176                 device_printf(sc->mrsas_dev, "mrsas_data_load_cb: error=%d\n", error);
1177                 if (error == EFBIG) {
1178                         cmd->ccb_ptr->ccb_h.status = CAM_REQ_TOO_BIG;
1179                         return;
1180                 }
1181         }
1182         if (cmd->flags & MRSAS_DIR_IN)
1183                 bus_dmamap_sync(cmd->sc->data_tag, cmd->data_dmamap,
1184                     BUS_DMASYNC_PREREAD);
1185         if (cmd->flags & MRSAS_DIR_OUT)
1186                 bus_dmamap_sync(cmd->sc->data_tag, cmd->data_dmamap,
1187                     BUS_DMASYNC_PREWRITE);
1188         if (nseg > sc->max_num_sge) {
1189                 device_printf(sc->mrsas_dev, "SGE count is too large or 0.\n");
1190                 return;
1191         }
1192         io_request = cmd->io_request;
1193         sgl_ptr = (pMpi25IeeeSgeChain64_t)&io_request->SGL;
1194
1195         if ((sc->device_id == MRSAS_INVADER) ||
1196             (sc->device_id == MRSAS_FURY) ||
1197             (sc->device_id == MRSAS_INTRUDER) ||
1198             (sc->device_id == MRSAS_INTRUDER_24) ||
1199             (sc->device_id == MRSAS_CUTLASS_52) ||
1200             (sc->device_id == MRSAS_CUTLASS_53)) {
1201                 pMpi25IeeeSgeChain64_t sgl_ptr_end = sgl_ptr;
1202
1203                 sgl_ptr_end += sc->max_sge_in_main_msg - 1;
1204                 sgl_ptr_end->Flags = 0;
1205         }
1206         if (nseg != 0) {
1207                 for (i = 0; i < nseg; i++) {
1208                         sgl_ptr->Address = segs[i].ds_addr;
1209                         sgl_ptr->Length = segs[i].ds_len;
1210                         sgl_ptr->Flags = 0;
1211                         if ((sc->device_id == MRSAS_INVADER) ||
1212                             (sc->device_id == MRSAS_FURY) ||
1213                             (sc->device_id == MRSAS_INTRUDER) ||
1214                             (sc->device_id == MRSAS_INTRUDER_24) ||
1215                             (sc->device_id == MRSAS_CUTLASS_52) ||
1216                             (sc->device_id == MRSAS_CUTLASS_53)) {
1217                                 if (i == nseg - 1)
1218                                         sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1219                         }
1220                         sgl_ptr++;
1221                         sg_processed = i + 1;
1222                         if ((sg_processed == (sc->max_sge_in_main_msg - 1)) &&
1223                             (nseg > sc->max_sge_in_main_msg)) {
1224                                 pMpi25IeeeSgeChain64_t sg_chain;
1225
1226                                 if ((sc->device_id == MRSAS_INVADER) ||
1227                                     (sc->device_id == MRSAS_FURY) ||
1228                                     (sc->device_id == MRSAS_INTRUDER) ||
1229                                     (sc->device_id == MRSAS_INTRUDER_24) ||
1230                                     (sc->device_id == MRSAS_CUTLASS_52) ||
1231                                     (sc->device_id == MRSAS_CUTLASS_53)) {
1232                                         if ((cmd->io_request->IoFlags & MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1233                                             != MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1234                                                 cmd->io_request->ChainOffset = sc->chain_offset_io_request;
1235                                         else
1236                                                 cmd->io_request->ChainOffset = 0;
1237                                 } else
1238                                         cmd->io_request->ChainOffset = sc->chain_offset_io_request;
1239                                 sg_chain = sgl_ptr;
1240                                 if ((sc->device_id == MRSAS_INVADER) ||
1241                                     (sc->device_id == MRSAS_FURY) ||
1242                                     (sc->device_id == MRSAS_INTRUDER) ||
1243                                     (sc->device_id == MRSAS_INTRUDER_24) ||
1244                                     (sc->device_id == MRSAS_CUTLASS_52) ||
1245                                     (sc->device_id == MRSAS_CUTLASS_53))
1246                                         sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1247                                 else
1248                                         sg_chain->Flags = (IEEE_SGE_FLAGS_CHAIN_ELEMENT | MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1249                                 sg_chain->Length = (sizeof(MPI2_SGE_IO_UNION) * (nseg - sg_processed));
1250                                 sg_chain->Address = cmd->chain_frame_phys_addr;
1251                                 sgl_ptr = (pMpi25IeeeSgeChain64_t)cmd->chain_frame;
1252                         }
1253                 }
1254         }
1255         cmd->sge_count = nseg;
1256 }
1257
1258 /*
1259  * mrsas_freeze_simq:   Freeze SIM queue
1260  * input:                               Pointer to command packet
1261  *                                              Pointer to SIM
1262  *
1263  * This function freezes the sim queue.
1264  */
1265 static void
1266 mrsas_freeze_simq(struct mrsas_mpt_cmd *cmd, struct cam_sim *sim)
1267 {
1268         union ccb *ccb = (union ccb *)(cmd->ccb_ptr);
1269
1270         xpt_freeze_simq(sim, 1);
1271         ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1272         ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1273 }
1274
1275 void
1276 mrsas_xpt_freeze(struct mrsas_softc *sc)
1277 {
1278         xpt_freeze_simq(sc->sim_0, 1);
1279         xpt_freeze_simq(sc->sim_1, 1);
1280 }
1281
1282 void
1283 mrsas_xpt_release(struct mrsas_softc *sc)
1284 {
1285         xpt_release_simq(sc->sim_0, 1);
1286         xpt_release_simq(sc->sim_1, 1);
1287 }
1288
1289 /*
1290  * mrsas_cmd_done:      Perform remaining command completion
1291  * input:                       Adapter instance soft state  Pointer to command packet
1292  *
1293  * This function calls ummap request and releases the MPT command.
1294  */
1295 void
1296 mrsas_cmd_done(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd)
1297 {
1298         callout_stop(&cmd->cm_callout);
1299         mrsas_unmap_request(sc, cmd);
1300         mtx_lock(&sc->sim_lock);
1301         xpt_done(cmd->ccb_ptr);
1302         cmd->ccb_ptr = NULL;
1303         mtx_unlock(&sc->sim_lock);
1304         mrsas_release_mpt_cmd(cmd);
1305 }
1306
1307 /*
1308  * mrsas_cam_poll:      Polling entry point
1309  * input:                       Pointer to SIM
1310  *
1311  * This is currently a stub function.
1312  */
1313 static void
1314 mrsas_cam_poll(struct cam_sim *sim)
1315 {
1316         int i;
1317         struct mrsas_softc *sc = (struct mrsas_softc *)cam_sim_softc(sim);
1318
1319         if (sc->msix_vectors != 0){
1320                 for (i=0; i<sc->msix_vectors; i++){
1321                         mrsas_complete_cmd(sc, i);
1322                 }
1323         } else {
1324                 mrsas_complete_cmd(sc, 0);
1325         }
1326 }
1327
1328 /*
1329  * mrsas_bus_scan:      Perform bus scan
1330  * input:                       Adapter instance soft state
1331  *
1332  * This mrsas_bus_scan function is needed for FreeBSD 7.x.  Also, it should not
1333  * be called in FreeBSD 8.x and later versions, where the bus scan is
1334  * automatic.
1335  */
1336 int
1337 mrsas_bus_scan(struct mrsas_softc *sc)
1338 {
1339         union ccb *ccb_0;
1340         union ccb *ccb_1;
1341
1342         if ((ccb_0 = xpt_alloc_ccb()) == NULL) {
1343                 return (ENOMEM);
1344         }
1345         if ((ccb_1 = xpt_alloc_ccb()) == NULL) {
1346                 xpt_free_ccb(ccb_0);
1347                 return (ENOMEM);
1348         }
1349         mtx_lock(&sc->sim_lock);
1350         if (xpt_create_path(&ccb_0->ccb_h.path, xpt_periph, cam_sim_path(sc->sim_0),
1351             CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1352                 xpt_free_ccb(ccb_0);
1353                 xpt_free_ccb(ccb_1);
1354                 mtx_unlock(&sc->sim_lock);
1355                 return (EIO);
1356         }
1357         if (xpt_create_path(&ccb_1->ccb_h.path, xpt_periph, cam_sim_path(sc->sim_1),
1358             CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1359                 xpt_free_ccb(ccb_0);
1360                 xpt_free_ccb(ccb_1);
1361                 mtx_unlock(&sc->sim_lock);
1362                 return (EIO);
1363         }
1364         mtx_unlock(&sc->sim_lock);
1365         xpt_rescan(ccb_0);
1366         xpt_rescan(ccb_1);
1367
1368         return (0);
1369 }
1370
1371 /*
1372  * mrsas_bus_scan_sim:  Perform bus scan per SIM
1373  * input:                               adapter instance soft state
1374  *
1375  * This function will be called from Event handler on LD creation/deletion,
1376  * JBOD on/off.
1377  */
1378 int
1379 mrsas_bus_scan_sim(struct mrsas_softc *sc, struct cam_sim *sim)
1380 {
1381         union ccb *ccb;
1382
1383         if ((ccb = xpt_alloc_ccb()) == NULL) {
1384                 return (ENOMEM);
1385         }
1386         mtx_lock(&sc->sim_lock);
1387         if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(sim),
1388             CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1389                 xpt_free_ccb(ccb);
1390                 mtx_unlock(&sc->sim_lock);
1391                 return (EIO);
1392         }
1393         mtx_unlock(&sc->sim_lock);
1394         xpt_rescan(ccb);
1395
1396         return (0);
1397 }