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