1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2004-07 Applied Micro Circuits Corporation.
5 * Copyright (c) 2004-05 Vinod Kashyap.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice, this list of conditions and the following disclaimer in the
15 *    documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	$FreeBSD$
30 */
31
32/*
33 * AMCC'S 3ware driver for 9000 series storage controllers.
34 *
35 * Author: Vinod Kashyap
36 * Modifications by: Adam Radford
37 * Modifications by: Manjunath Ranganathaiah
38 */
39
40
41/*
42 * FreeBSD CAM related functions.
43 */
44
45
46#include <dev/twa/tw_osl_includes.h>
47
48#include <cam/cam.h>
49#include <cam/cam_ccb.h>
50#include <cam/cam_sim.h>
51#include <cam/cam_xpt_sim.h>
52#include <cam/cam_debug.h>
53#include <cam/cam_periph.h>
54
55#include <cam/scsi/scsi_all.h>
56#include <cam/scsi/scsi_message.h>
57
58static TW_VOID	twa_action(struct cam_sim *sim, union ccb *ccb);
59static TW_VOID	twa_poll(struct cam_sim *sim);
60
61static TW_INT32	tw_osli_execute_scsi(struct tw_osli_req_context *req,
62	union ccb *ccb);
63
64
65
66/*
67 * Function name:	tw_osli_cam_attach
68 * Description:		Attaches the driver to CAM.
69 *
70 * Input:		sc	-- ptr to OSL internal ctlr context
71 * Output:		None
72 * Return value:	0	-- success
73 *			non-zero-- failure
74 */
75TW_INT32
76tw_osli_cam_attach(struct twa_softc *sc)
77{
78	struct cam_devq		*devq;
79
80	tw_osli_dbg_dprintf(3, sc, "entered");
81
82	/*
83	 * Create the device queue for our SIM.
84	 */
85	if ((devq = cam_simq_alloc(TW_OSLI_MAX_NUM_IOS)) == NULL) {
86		tw_osli_printf(sc, "error = %d",
87			TW_CL_SEVERITY_ERROR_STRING,
88			TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
89			0x2100,
90			"Failed to create SIM device queue",
91			ENOMEM);
92		return(ENOMEM);
93	}
94
95	/*
96	 * Create a SIM entry.  Though we can support TW_OSLI_MAX_NUM_REQUESTS
97	 * simultaneous requests, we claim to be able to handle only
98	 * TW_OSLI_MAX_NUM_IOS (two less), so that we always have a request
99	 * packet available to service ioctls and AENs.
100	 */
101	tw_osli_dbg_dprintf(3, sc, "Calling cam_sim_alloc");
102	sc->sim = cam_sim_alloc(twa_action, twa_poll, "twa", sc,
103			device_get_unit(sc->bus_dev), sc->sim_lock,
104			TW_OSLI_MAX_NUM_IOS, 1, devq);
105	if (sc->sim == NULL) {
106		cam_simq_free(devq);
107		tw_osli_printf(sc, "error = %d",
108			TW_CL_SEVERITY_ERROR_STRING,
109			TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
110			0x2101,
111			"Failed to create a SIM entry",
112			ENOMEM);
113		return(ENOMEM);
114	}
115
116	/*
117	 * Register the bus.
118	 */
119	tw_osli_dbg_dprintf(3, sc, "Calling xpt_bus_register");
120	mtx_lock(sc->sim_lock);
121	if (xpt_bus_register(sc->sim, sc->bus_dev, 0) != CAM_SUCCESS) {
122		cam_sim_free(sc->sim, TRUE);
123		sc->sim = NULL; /* so cam_detach will not try to free it */
124		tw_osli_printf(sc, "error = %d",
125			TW_CL_SEVERITY_ERROR_STRING,
126			TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
127			0x2102,
128			"Failed to register the bus",
129			ENXIO);
130		mtx_unlock(sc->sim_lock);
131		return(ENXIO);
132	}
133
134	tw_osli_dbg_dprintf(3, sc, "Calling xpt_create_path");
135	if (xpt_create_path(&sc->path, NULL,
136				cam_sim_path(sc->sim),
137				CAM_TARGET_WILDCARD,
138				CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
139		xpt_bus_deregister(cam_sim_path (sc->sim));
140		/* Passing TRUE to cam_sim_free will free the devq as well. */
141		cam_sim_free(sc->sim, TRUE);
142		tw_osli_printf(sc, "error = %d",
143			TW_CL_SEVERITY_ERROR_STRING,
144			TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
145			0x2103,
146			"Failed to create path",
147			ENXIO);
148		mtx_unlock(sc->sim_lock);
149		return(ENXIO);
150	}
151	mtx_unlock(sc->sim_lock);
152
153	tw_osli_dbg_dprintf(3, sc, "exiting");
154	return(0);
155}
156
157
158
159/*
160 * Function name:	tw_osli_cam_detach
161 * Description:		Detaches the driver from CAM.
162 *
163 * Input:		sc	-- ptr to OSL internal ctlr context
164 * Output:		None
165 * Return value:	None
166 */
167TW_VOID
168tw_osli_cam_detach(struct twa_softc *sc)
169{
170	tw_osli_dbg_dprintf(3, sc, "entered");
171
172	mtx_lock(sc->sim_lock);
173
174	if (sc->path)
175		xpt_free_path(sc->path);
176	if (sc->sim) {
177		xpt_bus_deregister(cam_sim_path(sc->sim));
178		/* Passing TRUE to cam_sim_free will free the devq as well. */
179		cam_sim_free(sc->sim, TRUE);
180	}
181	/* It's ok have 1 hold count while destroying the mutex */
182	mtx_destroy(sc->sim_lock);
183}
184
185
186
187/*
188 * Function name:	tw_osli_execute_scsi
189 * Description:		Build a fw cmd, based on a CAM style ccb, and
190 *			send it down.
191 *
192 * Input:		req	-- ptr to OSL internal request context
193 *			ccb	-- ptr to CAM style ccb
194 * Output:		None
195 * Return value:	0	-- success
196 *			non-zero-- failure
197 */
198TW_INT32
199tw_osli_execute_scsi(struct tw_osli_req_context *req, union ccb *ccb)
200{
201	struct twa_softc		*sc = req->ctlr;
202	struct tw_cl_req_packet		*req_pkt;
203	struct tw_cl_scsi_req_packet	*scsi_req;
204	struct ccb_hdr			*ccb_h = &(ccb->ccb_h);
205	struct ccb_scsiio		*csio = &(ccb->csio);
206	TW_INT32			error;
207
208	tw_osli_dbg_dprintf(10, sc, "SCSI I/O request 0x%x",
209		csio->cdb_io.cdb_bytes[0]);
210
211	if (ccb_h->target_id >= TW_CL_MAX_NUM_UNITS) {
212		tw_osli_dbg_dprintf(3, sc, "Invalid target. PTL = %x %x %jx",
213			ccb_h->path_id, ccb_h->target_id,
214			(uintmax_t)ccb_h->target_lun);
215		ccb_h->status |= CAM_TID_INVALID;
216		xpt_done(ccb);
217		return(1);
218	}
219	if (ccb_h->target_lun >= TW_CL_MAX_NUM_LUNS) {
220		tw_osli_dbg_dprintf(3, sc, "Invalid lun. PTL = %x %x %jx",
221			ccb_h->path_id, ccb_h->target_id,
222			(uintmax_t)ccb_h->target_lun);
223		ccb_h->status |= CAM_LUN_INVALID;
224		xpt_done(ccb);
225		return(1);
226	}
227
228	if(ccb_h->flags & CAM_CDB_PHYS) {
229		tw_osli_printf(sc, "",
230			TW_CL_SEVERITY_ERROR_STRING,
231			TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
232			0x2105,
233			"Physical CDB address!");
234		ccb_h->status = CAM_REQ_INVALID;
235		xpt_done(ccb);
236		return(1);
237	}
238
239	/*
240	 * We are going to work on this request.  Mark it as enqueued (though
241	 * we don't actually queue it...)
242	 */
243	ccb_h->status |= CAM_SIM_QUEUED;
244
245	if((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
246		if(ccb_h->flags & CAM_DIR_IN)
247			req->flags |= TW_OSLI_REQ_FLAGS_DATA_IN;
248		else
249			req->flags |= TW_OSLI_REQ_FLAGS_DATA_OUT;
250	}
251
252	/* Build the CL understood request packet for SCSI cmds. */
253	req_pkt = &req->req_pkt;
254	req_pkt->status = 0;
255	req_pkt->tw_osl_callback = tw_osl_complete_io;
256	scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
257	scsi_req->unit = ccb_h->target_id;
258	scsi_req->lun = ccb_h->target_lun;
259	scsi_req->sense_len = 0;
260	scsi_req->sense_data = (TW_UINT8 *)(&csio->sense_data);
261	scsi_req->scsi_status = 0;
262	if(ccb_h->flags & CAM_CDB_POINTER)
263		scsi_req->cdb = csio->cdb_io.cdb_ptr;
264	else
265		scsi_req->cdb = csio->cdb_io.cdb_bytes;
266	scsi_req->cdb_len = csio->cdb_len;
267
268	if (csio->dxfer_len > TW_CL_MAX_IO_SIZE) {
269		tw_osli_printf(sc, "size = %d",
270			TW_CL_SEVERITY_ERROR_STRING,
271			TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
272			0x2106,
273			"I/O size too big",
274			csio->dxfer_len);
275		ccb_h->status = CAM_REQ_TOO_BIG;
276		ccb_h->status &= ~CAM_SIM_QUEUED;
277		xpt_done(ccb);
278		return(1);
279	}
280	if ((ccb_h->flags & CAM_DATA_MASK) == CAM_DATA_VADDR) {
281		if ((req->length = csio->dxfer_len) != 0) {
282			req->data = csio->data_ptr;
283			scsi_req->sgl_entries = 1;
284		}
285	} else
286		req->flags |= TW_OSLI_REQ_FLAGS_CCB;
287	req->deadline = tw_osl_get_local_time() + (ccb_h->timeout / 1000);
288
289	/*
290	 * twa_map_load_data_callback will fill in the SGL,
291	 * and submit the I/O.
292	 */
293	error = tw_osli_map_request(req);
294	if ((error) && (req->flags & TW_OSLI_REQ_FLAGS_FAILED)) {
295		req->deadline = 0;
296		ccb_h->status = CAM_REQ_CMP_ERR;
297		ccb_h->status &= ~CAM_SIM_QUEUED;
298		xpt_done(ccb);
299	}
300	return(error);
301}
302
303
304
305/*
306 * Function name:	twa_action
307 * Description:		Driver entry point for CAM's use.
308 *
309 * Input:		sim	-- sim corresponding to the ctlr
310 *			ccb	-- ptr to CAM request
311 * Output:		None
312 * Return value:	None
313 */
314TW_VOID
315twa_action(struct cam_sim *sim, union ccb *ccb)
316{
317	struct twa_softc	*sc = (struct twa_softc *)cam_sim_softc(sim);
318	struct ccb_hdr		*ccb_h = &(ccb->ccb_h);
319
320	switch (ccb_h->func_code) {
321	case XPT_SCSI_IO:	/* SCSI I/O */
322	{
323		struct tw_osli_req_context	*req;
324
325		req = tw_osli_get_request(sc);
326		if (req == NULL) {
327			tw_osli_dbg_dprintf(2, sc, "Cannot get request pkt.");
328			/*
329			 * Freeze the simq to maintain ccb ordering.  The next
330			 * ccb that gets completed will unfreeze the simq.
331			 */
332			ccb_h->status &= ~CAM_SIM_QUEUED;
333			ccb_h->status |= CAM_REQUEUE_REQ;
334			xpt_done(ccb);
335			break;
336		}
337
338		if ((tw_cl_is_reset_needed(&(req->ctlr->ctlr_handle)))) {
339			ccb_h->status &= ~CAM_SIM_QUEUED;
340			ccb_h->status |= CAM_REQUEUE_REQ;
341			xpt_done(ccb);
342			tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
343			break;
344		}
345
346		req->req_handle.osl_req_ctxt = req;
347		req->req_handle.is_io = TW_CL_TRUE;
348		req->orig_req = ccb;
349		if (tw_osli_execute_scsi(req, ccb))
350			tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
351		break;
352	}
353
354	case XPT_ABORT:
355		tw_osli_dbg_dprintf(2, sc, "Abort request.");
356		ccb_h->status = CAM_UA_ABORT;
357		xpt_done(ccb);
358		break;
359
360	case XPT_RESET_BUS:
361		tw_cl_create_event(&(sc->ctlr_handle), TW_CL_FALSE,
362			TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
363			0x2108, 0x3, TW_CL_SEVERITY_INFO_STRING,
364			"Received Reset Bus request from CAM",
365			" ");
366
367		tw_cl_set_reset_needed(&(sc->ctlr_handle));
368		ccb_h->status = CAM_REQ_CMP;
369		xpt_done(ccb);
370		break;
371
372	case XPT_SET_TRAN_SETTINGS:
373		tw_osli_dbg_dprintf(3, sc, "XPT_SET_TRAN_SETTINGS");
374
375		/*
376		 * This command is not supported, since it's very specific
377		 * to SCSI, and we are doing ATA.
378		 */
379  		ccb_h->status = CAM_FUNC_NOTAVAIL;
380  		xpt_done(ccb);
381  		break;
382
383	case XPT_GET_TRAN_SETTINGS:
384	{
385		struct ccb_trans_settings	*cts = &ccb->cts;
386		struct ccb_trans_settings_scsi *scsi =
387		    &cts->proto_specific.scsi;
388		struct ccb_trans_settings_spi *spi =
389		    &cts->xport_specific.spi;
390
391		cts->protocol = PROTO_SCSI;
392		cts->protocol_version = SCSI_REV_2;
393		cts->transport = XPORT_SPI;
394		cts->transport_version = 2;
395
396		spi->valid = CTS_SPI_VALID_DISC;
397		spi->flags = CTS_SPI_FLAGS_DISC_ENB;
398		scsi->valid = CTS_SCSI_VALID_TQ;
399		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
400		tw_osli_dbg_dprintf(3, sc, "XPT_GET_TRAN_SETTINGS");
401		ccb_h->status = CAM_REQ_CMP;
402		xpt_done(ccb);
403		break;
404	}
405
406	case XPT_CALC_GEOMETRY:
407		tw_osli_dbg_dprintf(3, sc, "XPT_CALC_GEOMETRY");
408		cam_calc_geometry(&ccb->ccg, 1/* extended */);
409		xpt_done(ccb);
410		break;
411
412	case XPT_PATH_INQ:    /* Path inquiry -- get twa properties */
413	{
414		struct ccb_pathinq	*path_inq = &ccb->cpi;
415
416		tw_osli_dbg_dprintf(3, sc, "XPT_PATH_INQ request");
417
418		path_inq->version_num = 1;
419		path_inq->hba_inquiry = 0;
420		path_inq->target_sprt = 0;
421		path_inq->hba_misc = 0;
422		path_inq->hba_eng_cnt = 0;
423		path_inq->max_target = TW_CL_MAX_NUM_UNITS;
424		path_inq->max_lun = TW_CL_MAX_NUM_LUNS - 1;
425		path_inq->unit_number = cam_sim_unit(sim);
426		path_inq->bus_id = cam_sim_bus(sim);
427		path_inq->initiator_id = TW_CL_MAX_NUM_UNITS;
428		path_inq->base_transfer_speed = 100000;
429		strlcpy(path_inq->sim_vid, "FreeBSD", SIM_IDLEN);
430		strlcpy(path_inq->hba_vid, "3ware", HBA_IDLEN);
431		strlcpy(path_inq->dev_name, cam_sim_name(sim), DEV_IDLEN);
432		path_inq->transport = XPORT_SPI;
433		path_inq->transport_version = 2;
434		path_inq->protocol = PROTO_SCSI;
435		path_inq->protocol_version = SCSI_REV_2;
436		path_inq->maxio = TW_CL_MAX_IO_SIZE;
437		ccb_h->status = CAM_REQ_CMP;
438		xpt_done(ccb);
439		break;
440	}
441
442	default:
443		tw_osli_dbg_dprintf(3, sc, "func_code = %x", ccb_h->func_code);
444		ccb_h->status = CAM_REQ_INVALID;
445		xpt_done(ccb);
446		break;
447	}
448}
449
450
451
452/*
453 * Function name:	twa_poll
454 * Description:		Driver entry point called when interrupts are not
455 *			available.
456 *
457 * Input:		sim	-- sim corresponding to the controller
458 * Output:		None
459 * Return value:	None
460 */
461TW_VOID
462twa_poll(struct cam_sim *sim)
463{
464	struct twa_softc *sc = (struct twa_softc *)(cam_sim_softc(sim));
465
466	tw_osli_dbg_dprintf(3, sc, "entering; sc = %p", sc);
467	tw_cl_interrupt(&(sc->ctlr_handle));
468	tw_osli_dbg_dprintf(3, sc, "exiting; sc = %p", sc);
469}
470
471
472
473/*
474 * Function name:	tw_osli_request_bus_scan
475 * Description:		Requests CAM for a scan of the bus.
476 *
477 * Input:		sc	-- ptr to per ctlr structure
478 * Output:		None
479 * Return value:	0	-- success
480 *			non-zero-- failure
481 */
482TW_INT32
483tw_osli_request_bus_scan(struct twa_softc *sc)
484{
485	union ccb	*ccb;
486
487	tw_osli_dbg_dprintf(3, sc, "entering");
488
489	/* If we get here before sc->sim is initialized, return an error. */
490	if (!(sc->sim))
491		return(ENXIO);
492	if ((ccb = xpt_alloc_ccb()) == NULL)
493		return(ENOMEM);
494	mtx_lock(sc->sim_lock);
495	if (xpt_create_path(&ccb->ccb_h.path, NULL, cam_sim_path(sc->sim),
496	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
497		xpt_free_ccb(ccb);
498		mtx_unlock(sc->sim_lock);
499		return(EIO);
500	}
501
502	xpt_rescan(ccb);
503	mtx_unlock(sc->sim_lock);
504	return(0);
505}
506
507
508
509/*
510 * Function name:	tw_osli_disallow_new_requests
511 * Description:		Calls the appropriate CAM function, so as to freeze
512 *			the flow of new requests from CAM to this controller.
513 *
514 * Input:		sc	-- ptr to OSL internal ctlr context
515 *			req_handle -- ptr to request handle sent by OSL.
516 * Output:		None
517 * Return value:	None
518 */
519TW_VOID
520tw_osli_disallow_new_requests(struct twa_softc *sc,
521	struct tw_cl_req_handle *req_handle)
522{
523	/* Only freeze/release the simq for IOs */
524	if (req_handle->is_io) {
525		struct tw_osli_req_context	*req = req_handle->osl_req_ctxt;
526		union ccb			*ccb = (union ccb *)(req->orig_req);
527
528		xpt_freeze_simq(sc->sim, 1);
529		ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
530	}
531}
532
533
534
535/*
536 * Function name:	tw_osl_timeout
537 * Description:		Call to timeout().
538 *
539 * Input:		req_handle -- ptr to request handle sent by OSL.
540 * Output:		None
541 * Return value:	None
542 */
543TW_VOID
544tw_osl_timeout(struct tw_cl_req_handle *req_handle)
545{
546	struct tw_osli_req_context	*req = req_handle->osl_req_ctxt;
547	union ccb			*ccb = (union ccb *)(req->orig_req);
548	struct ccb_hdr			*ccb_h = &(ccb->ccb_h);
549
550	req->deadline = tw_osl_get_local_time() + (ccb_h->timeout / 1000);
551}
552
553
554
555/*
556 * Function name:	tw_osl_untimeout
557 * Description:		Inverse of call to timeout().
558 *
559 * Input:		req_handle -- ptr to request handle sent by OSL.
560 * Output:		None
561 * Return value:	None
562 */
563TW_VOID
564tw_osl_untimeout(struct tw_cl_req_handle *req_handle)
565{
566	struct tw_osli_req_context	*req = req_handle->osl_req_ctxt;
567
568	req->deadline = 0;
569}
570
571
572
573/*
574 * Function name:	tw_osl_scan_bus
575 * Description:		CL calls this function to request for a bus scan.
576 *
577 * Input:		ctlr_handle	-- ptr to controller handle
578 * Output:		None
579 * Return value:	None
580 */
581TW_VOID
582tw_osl_scan_bus(struct tw_cl_ctlr_handle *ctlr_handle)
583{
584	struct twa_softc	*sc = ctlr_handle->osl_ctlr_ctxt;
585	TW_INT32		error;
586
587	if ((error = tw_osli_request_bus_scan(sc)))
588		tw_osli_printf(sc, "error = %d",
589			TW_CL_SEVERITY_ERROR_STRING,
590			TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
591			0x2109,
592			"Bus scan request to CAM failed",
593			error);
594}
595
596
597
598/*
599 * Function name:	tw_osl_complete_io
600 * Description:		Called to complete CAM scsi requests.
601 *
602 * Input:		req_handle	-- ptr to request handle
603 * Output:		None
604 * Return value:	None
605 */
606TW_VOID
607tw_osl_complete_io(struct tw_cl_req_handle *req_handle)
608{
609	struct tw_osli_req_context	*req = req_handle->osl_req_ctxt;
610	struct tw_cl_req_packet		*req_pkt =
611		(struct tw_cl_req_packet *)(&req->req_pkt);
612	struct tw_cl_scsi_req_packet	*scsi_req;
613	struct twa_softc		*sc = req->ctlr;
614	union ccb			*ccb = (union ccb *)(req->orig_req);
615
616	tw_osli_dbg_dprintf(10, sc, "entering");
617
618	if (req->state != TW_OSLI_REQ_STATE_BUSY)
619		tw_osli_printf(sc, "request = %p, status = %d",
620			TW_CL_SEVERITY_ERROR_STRING,
621			TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
622			0x210A,
623			"Unposted command completed!!",
624			req, req->state);
625
626	/*
627	 * Remove request from the busy queue.  Just mark it complete.
628	 * There's no need to move it into the complete queue as we are
629	 * going to be done with it right now.
630	 */
631	req->state = TW_OSLI_REQ_STATE_COMPLETE;
632	tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
633
634	tw_osli_unmap_request(req);
635
636	req->deadline = 0;
637	if (req->error_code) {
638		/* This request never got submitted to the firmware. */
639		if (req->error_code == EBUSY) {
640			/*
641			 * Cmd queue is full, or the Common Layer is out of
642			 * resources.  The simq will already have been frozen.
643			 * When this ccb gets completed will unfreeze the simq.
644			 */
645			ccb->ccb_h.status |= CAM_REQUEUE_REQ;
646		}
647		else if (req->error_code == EFBIG)
648			ccb->ccb_h.status = CAM_REQ_TOO_BIG;
649		else
650			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
651	} else {
652		scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
653		if (req_pkt->status == TW_CL_ERR_REQ_SUCCESS)
654			ccb->ccb_h.status = CAM_REQ_CMP;
655		else {
656			if (req_pkt->status & TW_CL_ERR_REQ_INVALID_TARGET)
657				ccb->ccb_h.status |= CAM_SEL_TIMEOUT;
658			else if (req_pkt->status & TW_CL_ERR_REQ_INVALID_LUN)
659				ccb->ccb_h.status |= CAM_DEV_NOT_THERE;
660			else if (req_pkt->status & TW_CL_ERR_REQ_SCSI_ERROR)
661				ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
662			else if (req_pkt->status & TW_CL_ERR_REQ_BUS_RESET)
663				ccb->ccb_h.status |= (CAM_REQUEUE_REQ | CAM_SCSI_BUS_RESET);
664			/*
665			 * If none of the above errors occurred, simply
666			 * mark completion error.
667			 */
668			if (ccb->ccb_h.status == 0)
669				ccb->ccb_h.status = CAM_REQ_CMP_ERR;
670
671			if (req_pkt->status & TW_CL_ERR_REQ_AUTO_SENSE_VALID) {
672				ccb->csio.sense_len = scsi_req->sense_len;
673				ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
674			}
675		}
676
677		ccb->csio.scsi_status = scsi_req->scsi_status;
678	}
679
680	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
681	mtx_lock(sc->sim_lock);
682	xpt_done(ccb);
683	mtx_unlock(sc->sim_lock);
684	if (! req->error_code)
685		 /* twa_action will free the request otherwise */
686		tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
687}
688
689