xref: /freebsd/sys/dev/mrsas/mrsas_cam.c (revision 8ef24a0d4b28fe230e20637f56869cc4148cd2ca)
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 			if (io_request->RaidContext.regLockFlags == REGION_TYPE_UNUSED)
885 				cmd->request_desc->SCSIIO.RequestFlags =
886 				    (MRSAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
887 				    MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
888 			io_request->RaidContext.Type = MPI2_TYPE_CUDA;
889 			io_request->RaidContext.nseg = 0x1;
890 			io_request->IoFlags |= MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
891 			io_request->RaidContext.regLockFlags |=
892 			    (MR_RL_FLAGS_GRANT_DESTINATION_CUDA |
893 			    MR_RL_FLAGS_SEQ_NUM_ENABLE);
894 		}
895 		if ((sc->load_balance_info[device_id].loadBalanceFlag) &&
896 		    (io_info.isRead)) {
897 			io_info.devHandle =
898 			    mrsas_get_updated_dev_handle(sc,
899 			    &sc->load_balance_info[device_id], &io_info);
900 			cmd->load_balance = MRSAS_LOAD_BALANCE_FLAG;
901 			cmd->pd_r1_lb = io_info.pd_after_lb;
902 		} else
903 			cmd->load_balance = 0;
904 		cmd->request_desc->SCSIIO.DevHandle = io_info.devHandle;
905 		io_request->DevHandle = io_info.devHandle;
906 	} else {
907 		/* Not FP IO */
908 		io_request->RaidContext.timeoutValue = map_ptr->raidMap.fpPdIoTimeoutSec;
909 		cmd->request_desc->SCSIIO.RequestFlags =
910 		    (MRSAS_REQ_DESCRIPT_FLAGS_LD_IO <<
911 		    MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
912 		if ((sc->device_id == MRSAS_INVADER) ||
913 		    (sc->device_id == MRSAS_FURY) ||
914 		    (sc->device_id == MRSAS_INTRUDER) ||
915 		    (sc->device_id == MRSAS_INTRUDER_24)) {
916 			if (io_request->RaidContext.regLockFlags == REGION_TYPE_UNUSED)
917 				cmd->request_desc->SCSIIO.RequestFlags =
918 				    (MRSAS_REQ_DESCRIPT_FLAGS_NO_LOCK <<
919 				    MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
920 			io_request->RaidContext.Type = MPI2_TYPE_CUDA;
921 			io_request->RaidContext.regLockFlags |=
922 			    (MR_RL_FLAGS_GRANT_DESTINATION_CPU0 |
923 			    MR_RL_FLAGS_SEQ_NUM_ENABLE);
924 			io_request->RaidContext.nseg = 0x1;
925 		}
926 		io_request->Function = MRSAS_MPI2_FUNCTION_LD_IO_REQUEST;
927 		io_request->DevHandle = device_id;
928 	}
929 	return (0);
930 }
931 
932 /*
933  * mrsas_build_ldio_nonrw:	Builds an LDIO command
934  * input:				Adapter instance soft state
935  * 						Pointer to command packet
936  * 						Pointer to CCB
937  *
938  * This function builds the LDIO command packet.  It returns 0 if the command is
939  * built successfully, otherwise it returns a 1.
940  */
941 int
942 mrsas_build_ldio_nonrw(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
943     union ccb *ccb)
944 {
945 	struct ccb_hdr *ccb_h = &(ccb->ccb_h);
946 	u_int32_t device_id;
947 	MRSAS_RAID_SCSI_IO_REQUEST *io_request;
948 
949 	io_request = cmd->io_request;
950 	device_id = ccb_h->target_id;
951 
952 	/* FW path for LD Non-RW (SCSI management commands) */
953 	io_request->Function = MRSAS_MPI2_FUNCTION_LD_IO_REQUEST;
954 	io_request->DevHandle = device_id;
955 	cmd->request_desc->SCSIIO.RequestFlags =
956 	    (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
957 	    MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
958 
959 	io_request->RaidContext.VirtualDiskTgtId = device_id;
960 	io_request->LUN[1] = ccb_h->target_lun & 0xF;
961 	io_request->DataLength = cmd->length;
962 
963 	if (mrsas_map_request(sc, cmd, ccb) == SUCCESS) {
964 		if (cmd->sge_count > sc->max_num_sge) {
965 			device_printf(sc->mrsas_dev, "Error: sge_count (0x%x) exceeds"
966 			    "max (0x%x) allowed\n", cmd->sge_count, sc->max_num_sge);
967 			return (1);
968 		}
969 		/*
970 		 * numSGE store lower 8 bit of sge_count. numSGEExt store
971 		 * higher 8 bit of sge_count
972 		 */
973 		io_request->RaidContext.numSGE = cmd->sge_count;
974 		io_request->RaidContext.numSGEExt = (uint8_t)(cmd->sge_count >> 8);
975 	} else {
976 		device_printf(sc->mrsas_dev, "Data map/load failed.\n");
977 		return (1);
978 	}
979 	return (0);
980 }
981 
982 /*
983  * mrsas_build_syspdio:	Builds an DCDB command
984  * input:				Adapter instance soft state
985  * 						Pointer to command packet
986  * 						Pointer to CCB
987  *
988  * This function builds the DCDB inquiry command.  It returns 0 if the command
989  * is built successfully, otherwise it returns a 1.
990  */
991 int
992 mrsas_build_syspdio(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd,
993     union ccb *ccb, struct cam_sim *sim, u_int8_t fp_possible)
994 {
995 	struct ccb_hdr *ccb_h = &(ccb->ccb_h);
996 	u_int32_t device_id;
997 	MR_DRV_RAID_MAP_ALL *local_map_ptr;
998 	MRSAS_RAID_SCSI_IO_REQUEST *io_request;
999 	struct MR_PD_CFG_SEQ_NUM_SYNC *pd_sync;
1000 
1001 	pd_sync = (void *)sc->jbodmap_mem[(sc->pd_seq_map_id - 1) & 1];
1002 
1003 	io_request = cmd->io_request;
1004 	device_id = ccb_h->target_id;
1005 	local_map_ptr = sc->ld_drv_map[(sc->map_id & 1)];
1006 	io_request->RaidContext.RAIDFlags = MR_RAID_FLAGS_IO_SUB_TYPE_SYSTEM_PD
1007 	    << MR_RAID_CTX_RAID_FLAGS_IO_SUB_TYPE_SHIFT;
1008 	io_request->RaidContext.regLockFlags = 0;
1009 	io_request->RaidContext.regLockRowLBA = 0;
1010 	io_request->RaidContext.regLockLength = 0;
1011 
1012 	/* If FW supports PD sequence number */
1013 	if (sc->use_seqnum_jbod_fp &&
1014 	    sc->pd_list[device_id].driveType == 0x00) {
1015 		//printf("Using Drv seq num\n");
1016 		io_request->RaidContext.VirtualDiskTgtId = device_id + 255;
1017 		io_request->RaidContext.configSeqNum = pd_sync->seq[device_id].seqNum;
1018 		io_request->DevHandle = pd_sync->seq[device_id].devHandle;
1019 		io_request->RaidContext.regLockFlags |=
1020 		    (MR_RL_FLAGS_SEQ_NUM_ENABLE | MR_RL_FLAGS_GRANT_DESTINATION_CUDA);
1021 		io_request->RaidContext.Type = MPI2_TYPE_CUDA;
1022 		io_request->RaidContext.nseg = 0x1;
1023 	} else if (sc->fast_path_io) {
1024 		//printf("Using LD RAID map\n");
1025 		io_request->RaidContext.VirtualDiskTgtId = device_id;
1026 		io_request->RaidContext.configSeqNum = 0;
1027 		local_map_ptr = sc->ld_drv_map[(sc->map_id & 1)];
1028 		io_request->DevHandle =
1029 		    local_map_ptr->raidMap.devHndlInfo[device_id].curDevHdl;
1030 	} else {
1031 		//printf("Using FW PATH\n");
1032 		/* Want to send all IO via FW path */
1033 		io_request->RaidContext.VirtualDiskTgtId = device_id;
1034 		io_request->RaidContext.configSeqNum = 0;
1035 		io_request->DevHandle = 0xFFFF;
1036 	}
1037 
1038 	cmd->request_desc->SCSIIO.DevHandle = io_request->DevHandle;
1039 	cmd->request_desc->SCSIIO.MSIxIndex =
1040 	    sc->msix_vectors ? smp_processor_id() % sc->msix_vectors : 0;
1041 
1042 	if (!fp_possible) {
1043 		/* system pd firmware path */
1044 		io_request->Function = MRSAS_MPI2_FUNCTION_LD_IO_REQUEST;
1045 		cmd->request_desc->SCSIIO.RequestFlags =
1046 		    (MPI2_REQ_DESCRIPT_FLAGS_SCSI_IO <<
1047 		    MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1048 		io_request->RaidContext.timeoutValue =
1049 		    local_map_ptr->raidMap.fpPdIoTimeoutSec;
1050 		io_request->RaidContext.VirtualDiskTgtId = device_id;
1051 	} else {
1052 		/* system pd fast path */
1053 		io_request->Function = MPI2_FUNCTION_SCSI_IO_REQUEST;
1054 		io_request->RaidContext.timeoutValue = local_map_ptr->raidMap.fpPdIoTimeoutSec;
1055 
1056 		/*
1057 		 * NOTE - For system pd RW cmds only IoFlags will be FAST_PATH
1058 		 * Because the NON RW cmds will now go via FW Queue
1059 		 * and not the Exception queue
1060 		 */
1061 		io_request->IoFlags |= MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH;
1062 
1063 		cmd->request_desc->SCSIIO.RequestFlags =
1064 		    (MPI2_REQ_DESCRIPT_FLAGS_HIGH_PRIORITY <<
1065 		    MRSAS_REQ_DESCRIPT_FLAGS_TYPE_SHIFT);
1066 	}
1067 
1068 	io_request->LUN[1] = ccb_h->target_lun & 0xF;
1069 	io_request->DataLength = cmd->length;
1070 
1071 	if (mrsas_map_request(sc, cmd, ccb) == SUCCESS) {
1072 		if (cmd->sge_count > sc->max_num_sge) {
1073 			device_printf(sc->mrsas_dev, "Error: sge_count (0x%x) exceeds"
1074 			    "max (0x%x) allowed\n", cmd->sge_count, sc->max_num_sge);
1075 			return (1);
1076 		}
1077 		/*
1078 		 * numSGE store lower 8 bit of sge_count. numSGEExt store
1079 		 * higher 8 bit of sge_count
1080 		 */
1081 		io_request->RaidContext.numSGE = cmd->sge_count;
1082 		io_request->RaidContext.numSGEExt = (uint8_t)(cmd->sge_count >> 8);
1083 	} else {
1084 		device_printf(sc->mrsas_dev, "Data map/load failed.\n");
1085 		return (1);
1086 	}
1087 	return (0);
1088 }
1089 
1090 /*
1091  * mrsas_map_request:	Map and load data
1092  * input:				Adapter instance soft state
1093  * 						Pointer to command packet
1094  *
1095  * For data from OS, map and load the data buffer into bus space.  The SG list
1096  * is built in the callback.  If the  bus dmamap load is not successful,
1097  * cmd->error_code will contain the  error code and a 1 is returned.
1098  */
1099 int
1100 mrsas_map_request(struct mrsas_softc *sc,
1101     struct mrsas_mpt_cmd *cmd, union ccb *ccb)
1102 {
1103 	u_int32_t retcode = 0;
1104 	struct cam_sim *sim;
1105 
1106 	sim = xpt_path_sim(cmd->ccb_ptr->ccb_h.path);
1107 
1108 	if (cmd->data != NULL) {
1109 		/* Map data buffer into bus space */
1110 		mtx_lock(&sc->io_lock);
1111 #if (__FreeBSD_version >= 902001)
1112 		retcode = bus_dmamap_load_ccb(sc->data_tag, cmd->data_dmamap, ccb,
1113 		    mrsas_data_load_cb, cmd, 0);
1114 #else
1115 		retcode = bus_dmamap_load(sc->data_tag, cmd->data_dmamap, cmd->data,
1116 		    cmd->length, mrsas_data_load_cb, cmd, BUS_DMA_NOWAIT);
1117 #endif
1118 		mtx_unlock(&sc->io_lock);
1119 		if (retcode)
1120 			device_printf(sc->mrsas_dev, "bus_dmamap_load(): retcode = %d\n", retcode);
1121 		if (retcode == EINPROGRESS) {
1122 			device_printf(sc->mrsas_dev, "request load in progress\n");
1123 			mrsas_freeze_simq(cmd, sim);
1124 		}
1125 	}
1126 	if (cmd->error_code)
1127 		return (1);
1128 	return (retcode);
1129 }
1130 
1131 /*
1132  * mrsas_unmap_request:	Unmap and unload data
1133  * input:				Adapter instance soft state
1134  * 						Pointer to command packet
1135  *
1136  * This function unmaps and unloads data from OS.
1137  */
1138 void
1139 mrsas_unmap_request(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd)
1140 {
1141 	if (cmd->data != NULL) {
1142 		if (cmd->flags & MRSAS_DIR_IN)
1143 			bus_dmamap_sync(sc->data_tag, cmd->data_dmamap, BUS_DMASYNC_POSTREAD);
1144 		if (cmd->flags & MRSAS_DIR_OUT)
1145 			bus_dmamap_sync(sc->data_tag, cmd->data_dmamap, BUS_DMASYNC_POSTWRITE);
1146 		mtx_lock(&sc->io_lock);
1147 		bus_dmamap_unload(sc->data_tag, cmd->data_dmamap);
1148 		mtx_unlock(&sc->io_lock);
1149 	}
1150 }
1151 
1152 /*
1153  * mrsas_data_load_cb:	Callback entry point
1154  * input:				Pointer to command packet as argument
1155  * 						Pointer to segment
1156  * 						Number of segments Error
1157  *
1158  * This is the callback function of the bus dma map load.  It builds the SG
1159  * list.
1160  */
1161 static void
1162 mrsas_data_load_cb(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1163 {
1164 	struct mrsas_mpt_cmd *cmd = (struct mrsas_mpt_cmd *)arg;
1165 	struct mrsas_softc *sc = cmd->sc;
1166 	MRSAS_RAID_SCSI_IO_REQUEST *io_request;
1167 	pMpi25IeeeSgeChain64_t sgl_ptr;
1168 	int i = 0, sg_processed = 0;
1169 
1170 	if (error) {
1171 		cmd->error_code = error;
1172 		device_printf(sc->mrsas_dev, "mrsas_data_load_cb: error=%d\n", error);
1173 		if (error == EFBIG) {
1174 			cmd->ccb_ptr->ccb_h.status = CAM_REQ_TOO_BIG;
1175 			return;
1176 		}
1177 	}
1178 	if (cmd->flags & MRSAS_DIR_IN)
1179 		bus_dmamap_sync(cmd->sc->data_tag, cmd->data_dmamap,
1180 		    BUS_DMASYNC_PREREAD);
1181 	if (cmd->flags & MRSAS_DIR_OUT)
1182 		bus_dmamap_sync(cmd->sc->data_tag, cmd->data_dmamap,
1183 		    BUS_DMASYNC_PREWRITE);
1184 	if (nseg > sc->max_num_sge) {
1185 		device_printf(sc->mrsas_dev, "SGE count is too large or 0.\n");
1186 		return;
1187 	}
1188 	io_request = cmd->io_request;
1189 	sgl_ptr = (pMpi25IeeeSgeChain64_t)&io_request->SGL;
1190 
1191 	if ((sc->device_id == MRSAS_INVADER) ||
1192 	    (sc->device_id == MRSAS_FURY) ||
1193 	    (sc->device_id == MRSAS_INTRUDER) ||
1194 	    (sc->device_id == MRSAS_INTRUDER_24)) {
1195 		pMpi25IeeeSgeChain64_t sgl_ptr_end = sgl_ptr;
1196 
1197 		sgl_ptr_end += sc->max_sge_in_main_msg - 1;
1198 		sgl_ptr_end->Flags = 0;
1199 	}
1200 	if (nseg != 0) {
1201 		for (i = 0; i < nseg; i++) {
1202 			sgl_ptr->Address = segs[i].ds_addr;
1203 			sgl_ptr->Length = segs[i].ds_len;
1204 			sgl_ptr->Flags = 0;
1205 			if ((sc->device_id == MRSAS_INVADER) ||
1206 			    (sc->device_id == MRSAS_FURY) ||
1207 			    (sc->device_id == MRSAS_INTRUDER) ||
1208 			    (sc->device_id == MRSAS_INTRUDER_24)) {
1209 				if (i == nseg - 1)
1210 					sgl_ptr->Flags = IEEE_SGE_FLAGS_END_OF_LIST;
1211 			}
1212 			sgl_ptr++;
1213 			sg_processed = i + 1;
1214 			if ((sg_processed == (sc->max_sge_in_main_msg - 1)) &&
1215 			    (nseg > sc->max_sge_in_main_msg)) {
1216 				pMpi25IeeeSgeChain64_t sg_chain;
1217 
1218 				if ((sc->device_id == MRSAS_INVADER) ||
1219 				    (sc->device_id == MRSAS_FURY) ||
1220 				    (sc->device_id == MRSAS_INTRUDER) ||
1221 				    (sc->device_id == MRSAS_INTRUDER_24)) {
1222 					if ((cmd->io_request->IoFlags & MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1223 					    != MPI25_SAS_DEVICE0_FLAGS_ENABLED_FAST_PATH)
1224 						cmd->io_request->ChainOffset = sc->chain_offset_io_request;
1225 					else
1226 						cmd->io_request->ChainOffset = 0;
1227 				} else
1228 					cmd->io_request->ChainOffset = sc->chain_offset_io_request;
1229 				sg_chain = sgl_ptr;
1230 				if ((sc->device_id == MRSAS_INVADER) ||
1231 				    (sc->device_id == MRSAS_FURY) ||
1232 				    (sc->device_id == MRSAS_INTRUDER) ||
1233 				    (sc->device_id == MRSAS_INTRUDER_24))
1234 					sg_chain->Flags = IEEE_SGE_FLAGS_CHAIN_ELEMENT;
1235 				else
1236 					sg_chain->Flags = (IEEE_SGE_FLAGS_CHAIN_ELEMENT | MPI2_IEEE_SGE_FLAGS_IOCPLBNTA_ADDR);
1237 				sg_chain->Length = (sizeof(MPI2_SGE_IO_UNION) * (nseg - sg_processed));
1238 				sg_chain->Address = cmd->chain_frame_phys_addr;
1239 				sgl_ptr = (pMpi25IeeeSgeChain64_t)cmd->chain_frame;
1240 			}
1241 		}
1242 	}
1243 	cmd->sge_count = nseg;
1244 }
1245 
1246 /*
1247  * mrsas_freeze_simq:	Freeze SIM queue
1248  * input:				Pointer to command packet
1249  * 						Pointer to SIM
1250  *
1251  * This function freezes the sim queue.
1252  */
1253 static void
1254 mrsas_freeze_simq(struct mrsas_mpt_cmd *cmd, struct cam_sim *sim)
1255 {
1256 	union ccb *ccb = (union ccb *)(cmd->ccb_ptr);
1257 
1258 	xpt_freeze_simq(sim, 1);
1259 	ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1260 	ccb->ccb_h.status |= CAM_REQUEUE_REQ;
1261 }
1262 
1263 void
1264 mrsas_xpt_freeze(struct mrsas_softc *sc)
1265 {
1266 	xpt_freeze_simq(sc->sim_0, 1);
1267 	xpt_freeze_simq(sc->sim_1, 1);
1268 }
1269 
1270 void
1271 mrsas_xpt_release(struct mrsas_softc *sc)
1272 {
1273 	xpt_release_simq(sc->sim_0, 1);
1274 	xpt_release_simq(sc->sim_1, 1);
1275 }
1276 
1277 /*
1278  * mrsas_cmd_done:	Perform remaining command completion
1279  * input:			Adapter instance soft state  Pointer to command packet
1280  *
1281  * This function calls ummap request and releases the MPT command.
1282  */
1283 void
1284 mrsas_cmd_done(struct mrsas_softc *sc, struct mrsas_mpt_cmd *cmd)
1285 {
1286 	callout_stop(&cmd->cm_callout);
1287 	mrsas_unmap_request(sc, cmd);
1288 	mtx_lock(&sc->sim_lock);
1289 	xpt_done(cmd->ccb_ptr);
1290 	cmd->ccb_ptr = NULL;
1291 	mtx_unlock(&sc->sim_lock);
1292 	mrsas_release_mpt_cmd(cmd);
1293 }
1294 
1295 /*
1296  * mrsas_cam_poll:	Polling entry point
1297  * input:			Pointer to SIM
1298  *
1299  * This is currently a stub function.
1300  */
1301 static void
1302 mrsas_cam_poll(struct cam_sim *sim)
1303 {
1304 	int i;
1305 	struct mrsas_softc *sc = (struct mrsas_softc *)cam_sim_softc(sim);
1306 
1307 	if (sc->msix_vectors != 0){
1308 		for (i=0; i<sc->msix_vectors; i++){
1309 			mrsas_complete_cmd(sc, i);
1310 		}
1311 	} else {
1312 		mrsas_complete_cmd(sc, 0);
1313 	}
1314 }
1315 
1316 /*
1317  * mrsas_bus_scan:	Perform bus scan
1318  * input:			Adapter instance soft state
1319  *
1320  * This mrsas_bus_scan function is needed for FreeBSD 7.x.  Also, it should not
1321  * be called in FreeBSD 8.x and later versions, where the bus scan is
1322  * automatic.
1323  */
1324 int
1325 mrsas_bus_scan(struct mrsas_softc *sc)
1326 {
1327 	union ccb *ccb_0;
1328 	union ccb *ccb_1;
1329 
1330 	if ((ccb_0 = xpt_alloc_ccb()) == NULL) {
1331 		return (ENOMEM);
1332 	}
1333 	if ((ccb_1 = xpt_alloc_ccb()) == NULL) {
1334 		xpt_free_ccb(ccb_0);
1335 		return (ENOMEM);
1336 	}
1337 	mtx_lock(&sc->sim_lock);
1338 	if (xpt_create_path(&ccb_0->ccb_h.path, xpt_periph, cam_sim_path(sc->sim_0),
1339 	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1340 		xpt_free_ccb(ccb_0);
1341 		xpt_free_ccb(ccb_1);
1342 		mtx_unlock(&sc->sim_lock);
1343 		return (EIO);
1344 	}
1345 	if (xpt_create_path(&ccb_1->ccb_h.path, xpt_periph, cam_sim_path(sc->sim_1),
1346 	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1347 		xpt_free_ccb(ccb_0);
1348 		xpt_free_ccb(ccb_1);
1349 		mtx_unlock(&sc->sim_lock);
1350 		return (EIO);
1351 	}
1352 	mtx_unlock(&sc->sim_lock);
1353 	xpt_rescan(ccb_0);
1354 	xpt_rescan(ccb_1);
1355 
1356 	return (0);
1357 }
1358 
1359 /*
1360  * mrsas_bus_scan_sim:	Perform bus scan per SIM
1361  * input:				adapter instance soft state
1362  *
1363  * This function will be called from Event handler on LD creation/deletion,
1364  * JBOD on/off.
1365  */
1366 int
1367 mrsas_bus_scan_sim(struct mrsas_softc *sc, struct cam_sim *sim)
1368 {
1369 	union ccb *ccb;
1370 
1371 	if ((ccb = xpt_alloc_ccb()) == NULL) {
1372 		return (ENOMEM);
1373 	}
1374 	mtx_lock(&sc->sim_lock);
1375 	if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(sim),
1376 	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
1377 		xpt_free_ccb(ccb);
1378 		mtx_unlock(&sc->sim_lock);
1379 		return (EIO);
1380 	}
1381 	mtx_unlock(&sc->sim_lock);
1382 	xpt_rescan(ccb);
1383 
1384 	return (0);
1385 }
1386