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