xref: /dragonfly/sys/dev/raid/twa/tw_osl_cam.c (revision dcd37f7d)
1 /*
2  * Copyright (c) 2004-07 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: src/sys/dev/twa/tw_osl_cam.c,v 1.14 2010/06/09 21:40:38 delphij Exp $
28  */
29 
30 /*
31  * AMCC'S 3ware driver for 9000 series storage controllers.
32  *
33  * Author: Vinod Kashyap
34  * Modifications by: Adam Radford
35  * Modifications by: Manjunath Ranganathaiah
36  */
37 
38 
39 /*
40  * FreeBSD CAM related functions.
41  */
42 
43 
44 #include <dev/raid/twa/tw_osl_includes.h>
45 
46 #include <bus/cam/cam.h>
47 #include <bus/cam/cam_ccb.h>
48 #include <bus/cam/cam_sim.h>
49 #include <bus/cam/cam_xpt_sim.h>
50 #include <bus/cam/cam_debug.h>
51 #include <bus/cam/cam_periph.h>
52 #include <bus/cam/cam_xpt_periph.h>
53 
54 #include <bus/cam/scsi/scsi_all.h>
55 #include <bus/cam/scsi/scsi_message.h>
56 
57 static TW_VOID	twa_action(struct cam_sim *sim, union ccb *ccb);
58 static TW_VOID	twa_poll(struct cam_sim *sim);
59 static TW_VOID	twa_timeout(TW_VOID *arg);
60 static TW_VOID	twa_bus_scan_cb(struct cam_periph *periph, union ccb *ccb);
61 
62 static 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  */
76 TW_INT32
77 tw_osli_cam_attach(struct twa_softc *sc)
78 {
79 	struct cam_devq		*devq;
80 
81 	tw_osli_dbg_dprintf(3, sc, "entered");
82 
83 	/*
84 	 * Create the device queue for our SIM.
85 	 */
86 	if ((devq = cam_simq_alloc(TW_OSLI_MAX_NUM_REQUESTS)) == NULL) {
87 		tw_osli_printf(sc, "error = %d",
88 			TW_CL_SEVERITY_ERROR_STRING,
89 			TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
90 			0x2100,
91 			"Failed to create SIM device queue",
92 			ENOMEM);
93 		return(ENOMEM);
94 	}
95 
96 	/*
97 	 * Create a SIM entry.  Though we can support TW_OSLI_MAX_NUM_REQUESTS
98 	 * simultaneous requests, we claim to be able to handle only
99 	 * TW_OSLI_MAX_NUM_IOS (two less), so that we always have a request
100 	 * packet available to service ioctls and AENs.
101 	 */
102 	tw_osli_dbg_dprintf(3, sc, "Calling cam_sim_alloc");
103 	sc->sim = cam_sim_alloc(twa_action, twa_poll, "twa", sc,
104 			device_get_unit(sc->bus_dev), sc->sim_lock,
105 			TW_OSLI_MAX_NUM_IOS, 1, devq);
106 	if (sc->sim == NULL) {
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 	lockmgr(sc->sim_lock, LK_EXCLUSIVE);
121 	if (xpt_bus_register(sc->sim, 0) != CAM_SUCCESS) {
122 		cam_sim_free(sc->sim);
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 		lockmgr(sc->sim_lock, LK_RELEASE);
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);
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 		lockmgr(sc->sim_lock, LK_RELEASE);
149 		return(ENXIO);
150 	}
151 	lockmgr(sc->sim_lock, LK_RELEASE);
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  */
167 TW_VOID
168 tw_osli_cam_detach(struct twa_softc *sc)
169 {
170 	tw_osli_dbg_dprintf(3, sc, "entered");
171 
172 	lockmgr(sc->sim_lock, LK_EXCLUSIVE);
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);
180 	}
181 	/* It's ok have 1 hold count while destroying the mutex */
182 	lockuninit(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  */
198 TW_INT32
199 tw_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 %x",
213 			ccb_h->path_id, ccb_h->target_id, ccb_h->target_lun);
214 		ccb_h->status |= CAM_TID_INVALID;
215 		xpt_done(ccb);
216 		return(1);
217 	}
218 	if (ccb_h->target_lun >= TW_CL_MAX_NUM_LUNS) {
219 		tw_osli_dbg_dprintf(3, sc, "Invalid lun. PTL = %x %x %x",
220 			ccb_h->path_id, ccb_h->target_id, ccb_h->target_lun);
221 		ccb_h->status |= CAM_LUN_INVALID;
222 		xpt_done(ccb);
223 		return(1);
224 	}
225 
226 	if(ccb_h->flags & CAM_CDB_PHYS) {
227 		tw_osli_printf(sc, "",
228 			TW_CL_SEVERITY_ERROR_STRING,
229 			TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
230 			0x2105,
231 			"Physical CDB address!");
232 		ccb_h->status = CAM_REQ_INVALID;
233 		xpt_done(ccb);
234 		return(1);
235 	}
236 
237 	/*
238 	 * We are going to work on this request.  Mark it as enqueued (though
239 	 * we don't actually queue it...)
240 	 */
241 	ccb_h->status |= CAM_SIM_QUEUED;
242 
243 	if((ccb_h->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
244 		if(ccb_h->flags & CAM_DIR_IN)
245 			req->flags |= TW_OSLI_REQ_FLAGS_DATA_IN;
246 		else
247 			req->flags |= TW_OSLI_REQ_FLAGS_DATA_OUT;
248 	}
249 
250 	/* Build the CL understood request packet for SCSI cmds. */
251 	req_pkt = &req->req_pkt;
252 	req_pkt->status = 0;
253 	req_pkt->tw_osl_callback = tw_osl_complete_io;
254 	scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
255 	scsi_req->unit = ccb_h->target_id;
256 	scsi_req->lun = ccb_h->target_lun;
257 	scsi_req->sense_len = 0;
258 	scsi_req->sense_data = (TW_UINT8 *)(&csio->sense_data);
259 	scsi_req->scsi_status = 0;
260 	if(ccb_h->flags & CAM_CDB_POINTER)
261 		scsi_req->cdb = csio->cdb_io.cdb_ptr;
262 	else
263 		scsi_req->cdb = csio->cdb_io.cdb_bytes;
264 	scsi_req->cdb_len = csio->cdb_len;
265 
266 	if (!(ccb_h->flags & CAM_DATA_PHYS)) {
267 		/* Virtual data addresses.  Need to convert them... */
268 		tw_osli_dbg_dprintf(3, sc,
269 			"XPT_SCSI_IO: Single virtual address!");
270 		if (!(ccb_h->flags & CAM_SCATTER_VALID)) {
271 			if (csio->dxfer_len > TW_CL_MAX_IO_SIZE) {
272 				tw_osli_printf(sc, "size = %d",
273 					TW_CL_SEVERITY_ERROR_STRING,
274 					TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
275 					0x2106,
276 					"I/O size too big",
277 					csio->dxfer_len);
278 				ccb_h->status = CAM_REQ_TOO_BIG;
279 				xpt_done(ccb);
280 				return(1);
281 			}
282 
283 			if ((req->length = csio->dxfer_len)) {
284 				req->data = csio->data_ptr;
285 				scsi_req->sgl_entries = 1;
286 			}
287 		} else {
288 			tw_osli_printf(sc, "",
289 				TW_CL_SEVERITY_ERROR_STRING,
290 				TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
291 				0x2107,
292 				"XPT_SCSI_IO: Got SGList");
293 			ccb_h->status = CAM_REQ_INVALID;
294 			xpt_done(ccb);
295 			return(1);
296 		}
297 	} else {
298 		/* Data addresses are physical. */
299 		tw_osli_printf(sc, "",
300 			TW_CL_SEVERITY_ERROR_STRING,
301 			TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
302 			0x2108,
303 			"XPT_SCSI_IO: Physical data addresses");
304 		ccb_h->status = CAM_REQ_INVALID;
305 		ccb_h->status &= ~CAM_SIM_QUEUED;
306 		xpt_done(ccb);
307 		return(1);
308 	}
309 
310 	callout_reset(&ccb->ccb_h.timeout_ch,
311 		(ccb_h->timeout * hz) / 1000, twa_timeout, req);
312 	/*
313 	 * twa_map_load_data_callback will fill in the SGL,
314 	 * and submit the I/O.
315 	 */
316 	error = tw_osli_map_request(req);
317 	return(error);
318 }
319 
320 
321 
322 /*
323  * Function name:	twa_action
324  * Description:		Driver entry point for CAM's use.
325  *
326  * Input:		sim	-- sim corresponding to the ctlr
327  *			ccb	-- ptr to CAM request
328  * Output:		None
329  * Return value:	None
330  */
331 TW_VOID
332 twa_action(struct cam_sim *sim, union ccb *ccb)
333 {
334 	struct twa_softc	*sc = (struct twa_softc *)cam_sim_softc(sim);
335 	struct ccb_hdr		*ccb_h = &(ccb->ccb_h);
336 
337 	switch (ccb_h->func_code) {
338 	case XPT_SCSI_IO:	/* SCSI I/O */
339 	{
340 		struct tw_osli_req_context	*req;
341 
342 		req = tw_osli_get_request(sc);
343 		if (req == NULL) {
344 			tw_osli_dbg_dprintf(2, sc, "Cannot get request pkt.");
345 			/*
346 			 * Freeze the simq to maintain ccb ordering.  The next
347 			 * ccb that gets completed will unfreeze the simq.
348 			 */
349 			ccb_h->status |= CAM_REQUEUE_REQ;
350 			xpt_done(ccb);
351 			break;
352 		}
353 		req->req_handle.osl_req_ctxt = req;
354 		req->req_handle.is_io = TW_CL_TRUE;
355 		req->orig_req = ccb;
356 		if (tw_osli_execute_scsi(req, ccb))
357 			tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
358 		break;
359 	}
360 
361 	case XPT_ABORT:
362 		tw_osli_dbg_dprintf(2, sc, "Abort request.");
363 		ccb_h->status = CAM_UA_ABORT;
364 		xpt_done(ccb);
365 		break;
366 
367 	case XPT_RESET_BUS:
368 		tw_cl_create_event(&(sc->ctlr_handle), TW_CL_TRUE,
369 			TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
370 			0x2108, 0x3, TW_CL_SEVERITY_INFO_STRING,
371 			"Received Reset Bus request from CAM",
372 			" ");
373 
374 		lockmgr(sc->sim_lock, LK_RELEASE);
375 		if (tw_cl_reset_ctlr(&sc->ctlr_handle)) {
376 			tw_cl_create_event(&(sc->ctlr_handle), TW_CL_TRUE,
377 				TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
378 				0x2109, 0x1, TW_CL_SEVERITY_ERROR_STRING,
379 				"Failed to reset bus",
380 				" ");
381 			ccb_h->status = CAM_REQ_CMP_ERR;
382 		}
383 		else
384 			ccb_h->status = CAM_REQ_CMP;
385 
386 		lockmgr(sc->sim_lock, LK_EXCLUSIVE);
387 		xpt_done(ccb);
388 		break;
389 
390 	case XPT_SET_TRAN_SETTINGS:
391 		tw_osli_dbg_dprintf(3, sc, "XPT_SET_TRAN_SETTINGS");
392 
393 		/*
394 		 * This command is not supported, since it's very specific
395 		 * to SCSI, and we are doing ATA.
396 		 */
397 		ccb_h->status = CAM_FUNC_NOTAVAIL;
398 		xpt_done(ccb);
399 		break;
400 
401 	case XPT_GET_TRAN_SETTINGS:
402 	{
403 		struct ccb_trans_settings	*cts = &ccb->cts;
404 		struct ccb_trans_settings_scsi *scsi =
405 		    &cts->proto_specific.scsi;
406 		struct ccb_trans_settings_spi *spi =
407 		    &cts->xport_specific.spi;
408 
409 		cts->protocol = PROTO_SCSI;
410 		cts->protocol_version = SCSI_REV_2;
411 		cts->transport = XPORT_SPI;
412 		cts->transport_version = 2;
413 
414 		spi->valid = CTS_SPI_VALID_DISC;
415 		spi->flags = CTS_SPI_FLAGS_DISC_ENB;
416 		scsi->valid = CTS_SCSI_VALID_TQ;
417 		scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
418 		tw_osli_dbg_dprintf(3, sc, "XPT_GET_TRAN_SETTINGS");
419 		ccb_h->status = CAM_REQ_CMP;
420 		xpt_done(ccb);
421 		break;
422 	}
423 
424 	case XPT_CALC_GEOMETRY:
425 		tw_osli_dbg_dprintf(3, sc, "XPT_CALC_GEOMETRY");
426 		cam_calc_geometry(&ccb->ccg, 1/* extended */);
427 		xpt_done(ccb);
428 		break;
429 
430 	case XPT_PATH_INQ:    /* Path inquiry -- get twa properties */
431 	{
432 		struct ccb_pathinq	*path_inq = &ccb->cpi;
433 
434 		tw_osli_dbg_dprintf(3, sc, "XPT_PATH_INQ request");
435 
436 		path_inq->version_num = 1;
437 		path_inq->hba_inquiry = 0;
438 		path_inq->target_sprt = 0;
439 		path_inq->hba_misc = 0;
440 		path_inq->hba_eng_cnt = 0;
441 		path_inq->max_target = TW_CL_MAX_NUM_UNITS;
442 		path_inq->max_lun = TW_CL_MAX_NUM_LUNS - 1;
443 		path_inq->unit_number = cam_sim_unit(sim);
444 		path_inq->bus_id = cam_sim_bus(sim);
445 		path_inq->initiator_id = TW_CL_MAX_NUM_UNITS;
446 		path_inq->base_transfer_speed = 100000;
447 		strncpy(path_inq->sim_vid, "FreeBSD", SIM_IDLEN);
448 		strncpy(path_inq->hba_vid, "3ware", HBA_IDLEN);
449 		strncpy(path_inq->dev_name, cam_sim_name(sim), DEV_IDLEN);
450                 path_inq->transport = XPORT_SPI;
451                 path_inq->transport_version = 2;
452                 path_inq->protocol = PROTO_SCSI;
453                 path_inq->protocol_version = SCSI_REV_2;
454 		ccb_h->status = CAM_REQ_CMP;
455 		xpt_done(ccb);
456 		break;
457 	}
458 
459 	default:
460 		tw_osli_dbg_dprintf(3, sc, "func_code = %x", ccb_h->func_code);
461 		ccb_h->status = CAM_REQ_INVALID;
462 		xpt_done(ccb);
463 		break;
464 	}
465 }
466 
467 
468 
469 /*
470  * Function name:	twa_poll
471  * Description:		Driver entry point called when interrupts are not
472  *			available.
473  *
474  * Input:		sim	-- sim corresponding to the controller
475  * Output:		None
476  * Return value:	None
477  */
478 TW_VOID
479 twa_poll(struct cam_sim *sim)
480 {
481 	struct twa_softc *sc = (struct twa_softc *)(cam_sim_softc(sim));
482 
483 	tw_osli_dbg_dprintf(3, sc, "entering; sc = %p", sc);
484 	tw_cl_interrupt(&(sc->ctlr_handle));
485 	tw_osli_dbg_dprintf(3, sc, "exiting; sc = %p", sc);
486 }
487 
488 
489 
490 /*
491  * Function name:	twa_timeout
492  * Description:		Driver entry point for being alerted on a request
493  *			timing out.
494  *
495  * Input:		arg	-- ptr to timed out request
496  * Output:		None
497  * Return value:	None
498  */
499 static TW_VOID
500 twa_timeout(TW_VOID *arg)
501 {
502 	struct tw_osli_req_context	*req =
503 		(struct tw_osli_req_context *)arg;
504 
505 	tw_cl_create_event(&(req->ctlr->ctlr_handle), TW_CL_TRUE,
506 		TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
507 		0x210B, 0x1, TW_CL_SEVERITY_ERROR_STRING,
508 		"Request timed out!",
509 		"request = %p", req);
510 	tw_cl_reset_ctlr(&(req->ctlr->ctlr_handle));
511 }
512 
513 
514 
515 /*
516  * Function name:	tw_osli_request_bus_scan
517  * Description:		Requests CAM for a scan of the bus.
518  *
519  * Input:		sc	-- ptr to per ctlr structure
520  * Output:		None
521  * Return value:	0	-- success
522  *			non-zero-- failure
523  */
524 TW_INT32
525 tw_osli_request_bus_scan(struct twa_softc *sc)
526 {
527 	union ccb	*ccb;
528 
529 	tw_osli_dbg_dprintf(3, sc, "entering");
530 
531 	/* If we get here before sc->sim is initialized, return an error. */
532 	if (!(sc->sim))
533 		return(ENXIO);
534 	if ((ccb = xpt_alloc_ccb()) == NULL)
535 		return(ENOMEM);
536 	lockmgr(sc->sim_lock, LK_EXCLUSIVE);
537 	if (xpt_create_path(&ccb->ccb_h.path, xpt_periph, cam_sim_path(sc->sim),
538 	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
539 		xpt_free_ccb(ccb);
540 		lockmgr(sc->sim_lock, LK_RELEASE);
541 		return(EIO);
542 	}
543 
544 	xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5/*priority (low)*/);
545 	ccb->ccb_h.func_code = XPT_SCAN_BUS;
546 	ccb->ccb_h.cbfcnp = twa_bus_scan_cb;
547 	ccb->crcn.flags = CAM_FLAG_NONE;
548 	xpt_action(ccb);
549 
550 	lockmgr(sc->sim_lock, LK_RELEASE);
551 	return(0);
552 }
553 
554 
555 
556 /*
557  * Function name:	twa_bus_scan_cb
558  * Description:		Callback from CAM on a bus scan request.
559  *
560  * Input:		periph	-- we don't use this
561  *			ccb	-- bus scan request ccb that we sent to CAM
562  * Output:		None
563  * Return value:	None
564  */
565 static TW_VOID
566 twa_bus_scan_cb(struct cam_periph *periph, union ccb *ccb)
567 {
568 	tw_osli_dbg_printf(3, "entering");
569 
570 	if (ccb->ccb_h.status != CAM_REQ_CMP)
571 		kprintf("cam_scan_callback: failure status = %x\n",
572 			ccb->ccb_h.status);
573 	else
574 		tw_osli_dbg_printf(3, "success");
575 
576 	xpt_free_path(ccb->ccb_h.path);
577 	kfree(ccb, M_TEMP);
578 }
579 
580 
581 
582 /*
583  * Function name:	tw_osli_disallow_new_requests
584  * Description:		Calls the appropriate CAM function, so as to freeze
585  *			the flow of new requests from CAM to this controller.
586  *
587  * Input:		sc	-- ptr to OSL internal ctlr context
588  *			req_handle -- ptr to request handle sent by OSL.
589  * Output:		None
590  * Return value:	None
591  */
592 TW_VOID
593 tw_osli_disallow_new_requests(struct twa_softc *sc,
594 	struct tw_cl_req_handle *req_handle)
595 {
596 	/* Only freeze/release the simq for IOs */
597 	if (req_handle->is_io) {
598 		struct tw_osli_req_context	*req = req_handle->osl_req_ctxt;
599 		union ccb			*ccb = (union ccb *)(req->orig_req);
600 
601 		xpt_freeze_simq(sc->sim, 1);
602 		ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
603 	}
604 }
605 
606 
607 
608 /*
609  * Function name:	tw_osl_ctlr_busy
610  * Description:		CL calls this function on cmd queue full or otherwise,
611  *			when it is too busy to accept new requests.
612  *
613  * Input:		ctlr_handle	-- ptr to controller handle
614  *			req_handle	-- ptr to request handle sent by OSL.
615  * Output:		None
616  * Return value:	None
617  */
618 TW_VOID
619 tw_osl_ctlr_busy(struct tw_cl_ctlr_handle *ctlr_handle,
620 	struct tw_cl_req_handle *req_handle)
621 {
622 	tw_osli_disallow_new_requests(ctlr_handle->osl_ctlr_ctxt, req_handle);
623 }
624 
625 
626 
627 /*
628  * Function name:	tw_osl_scan_bus
629  * Description:		CL calls this function to request for a bus scan.
630  *
631  * Input:		ctlr_handle	-- ptr to controller handle
632  * Output:		None
633  * Return value:	None
634  */
635 TW_VOID
636 tw_osl_scan_bus(struct tw_cl_ctlr_handle *ctlr_handle)
637 {
638 	struct twa_softc	*sc = ctlr_handle->osl_ctlr_ctxt;
639 	TW_INT32		error;
640 
641 	if ((error = tw_osli_request_bus_scan(sc)))
642 		tw_osli_printf(sc, "error = %d",
643 			TW_CL_SEVERITY_ERROR_STRING,
644 			TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
645 			0x2109,
646 			"Bus scan request to CAM failed",
647 			error);
648 }
649 
650 
651 
652 /*
653  * Function name:	tw_osl_complete_io
654  * Description:		Called to complete CAM scsi requests.
655  *
656  * Input:		req_handle	-- ptr to request handle
657  * Output:		None
658  * Return value:	None
659  */
660 TW_VOID
661 tw_osl_complete_io(struct tw_cl_req_handle *req_handle)
662 {
663 	struct tw_osli_req_context	*req = req_handle->osl_req_ctxt;
664 	struct tw_cl_req_packet		*req_pkt =
665 		(struct tw_cl_req_packet *)(&req->req_pkt);
666 	struct tw_cl_scsi_req_packet	*scsi_req;
667 	struct twa_softc		*sc = req->ctlr;
668 	union ccb			*ccb = (union ccb *)(req->orig_req);
669 
670 	tw_osli_dbg_dprintf(10, sc, "entering");
671 
672 	if (req->state != TW_OSLI_REQ_STATE_BUSY)
673 		tw_osli_printf(sc, "request = %p, status = %d",
674 			TW_CL_SEVERITY_ERROR_STRING,
675 			TW_CL_MESSAGE_SOURCE_FREEBSD_DRIVER,
676 			0x210A,
677 			"Unposted command completed!!",
678 			req, req->state);
679 
680 	/*
681 	 * Remove request from the busy queue.  Just mark it complete.
682 	 * There's no need to move it into the complete queue as we are
683 	 * going to be done with it right now.
684 	 */
685 	req->state = TW_OSLI_REQ_STATE_COMPLETE;
686 	tw_osli_req_q_remove_item(req, TW_OSLI_BUSY_Q);
687 
688 	tw_osli_unmap_request(req);
689 
690 	callout_stop(&ccb->ccb_h.timeout_ch);
691 	if (req->error_code) {
692 		/* This request never got submitted to the firmware. */
693 		if (req->error_code == EBUSY) {
694 			/*
695 			 * Cmd queue is full, or the Common Layer is out of
696 			 * resources.  The simq will already have been frozen.
697 			 * When this ccb gets completed will unfreeze the simq.
698 			 */
699 			ccb->ccb_h.status |= CAM_REQUEUE_REQ;
700 		}
701 		else if (req->error_code == EFBIG)
702 			ccb->ccb_h.status = CAM_REQ_TOO_BIG;
703 		else
704 			ccb->ccb_h.status = CAM_REQ_CMP_ERR;
705 	} else {
706 		scsi_req = &(req_pkt->gen_req_pkt.scsi_req);
707 		if (req_pkt->status == TW_CL_ERR_REQ_SUCCESS)
708 			ccb->ccb_h.status = CAM_REQ_CMP;
709 		else {
710 			if (req_pkt->status & TW_CL_ERR_REQ_INVALID_TARGET)
711 				ccb->ccb_h.status |= CAM_TID_INVALID;
712 			else if (req_pkt->status & TW_CL_ERR_REQ_INVALID_LUN)
713 				ccb->ccb_h.status |= CAM_LUN_INVALID;
714 			else if (req_pkt->status & TW_CL_ERR_REQ_SCSI_ERROR)
715 				ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
716 			else if (req_pkt->status & TW_CL_ERR_REQ_BUS_RESET)
717 				ccb->ccb_h.status |= CAM_SCSI_BUS_RESET;
718 			/*
719 			 * If none of the above errors occurred, simply
720 			 * mark completion error.
721 			 */
722 			if (ccb->ccb_h.status == 0)
723 				ccb->ccb_h.status = CAM_REQ_CMP_ERR;
724 
725 			if (req_pkt->status & TW_CL_ERR_REQ_AUTO_SENSE_VALID) {
726 				ccb->csio.sense_len = scsi_req->sense_len;
727 				ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
728 			}
729 		}
730 
731 		ccb->csio.scsi_status = scsi_req->scsi_status;
732 	}
733 
734 	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
735 	lockmgr(sc->sim_lock, LK_EXCLUSIVE);
736 	xpt_done(ccb);
737 	lockmgr(sc->sim_lock, LK_RELEASE);
738 	if (! req->error_code)
739 		 /* twa_action will free the request otherwise */
740 		tw_osli_req_q_insert_tail(req, TW_OSLI_FREE_Q);
741 }
742