xref: /freebsd-14.2/sys/dev/aic7xxx/aic79xx_osm.c (revision 7c3092e3)
1 /*-
2  * Bus independent FreeBSD shim for the aic79xx based Adaptec SCSI controllers
3  *
4  * Copyright (c) 1994-2002, 2004 Justin T. Gibbs.
5  * Copyright (c) 2001-2002 Adaptec Inc.
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  *    without modification.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * Alternatively, this software may be distributed under the terms of the
18  * GNU Public License ("GPL").
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  * $Id: //depot/aic7xxx/freebsd/dev/aic7xxx/aic79xx_osm.c#35 $
33  */
34 
35 #include <sys/cdefs.h>
36 #include <dev/aic7xxx/aic79xx_osm.h>
37 #include <dev/aic7xxx/aic79xx_inline.h>
38 
39 #include <sys/kthread.h>
40 
41 #include "opt_ddb.h"
42 #ifdef DDB
43 #include <ddb/ddb.h>
44 #endif
45 
46 #ifndef AHD_TMODE_ENABLE
47 #define AHD_TMODE_ENABLE 0
48 #endif
49 
50 #include <dev/aic7xxx/aic_osm_lib.c>
51 
52 #define ccb_scb_ptr spriv_ptr0
53 
54 #if 0
55 static void	ahd_dump_targcmd(struct target_cmd *cmd);
56 #endif
57 static int	ahd_modevent(module_t mod, int type, void *data);
58 static void	ahd_action(struct cam_sim *sim, union ccb *ccb);
59 static void	ahd_set_tran_settings(struct ahd_softc *ahd,
60 				      int our_id, char channel,
61 				      struct ccb_trans_settings *cts);
62 static void	ahd_get_tran_settings(struct ahd_softc *ahd,
63 				      int our_id, char channel,
64 				      struct ccb_trans_settings *cts);
65 static void	ahd_async(void *callback_arg, uint32_t code,
66 			  struct cam_path *path, void *arg);
67 static void	ahd_execute_scb(void *arg, bus_dma_segment_t *dm_segs,
68 				int nsegments, int error);
69 static void	ahd_poll(struct cam_sim *sim);
70 static void	ahd_setup_data(struct ahd_softc *ahd, struct cam_sim *sim,
71 			       struct ccb_scsiio *csio, struct scb *scb);
72 static void	ahd_abort_ccb(struct ahd_softc *ahd, struct cam_sim *sim,
73 			      union ccb *ccb);
74 static int	ahd_create_path(struct ahd_softc *ahd,
75 				char channel, u_int target, u_int lun,
76 				struct cam_path **path);
77 
78 static const char *ahd_sysctl_node_elements[] = {
79 	"root",
80 	"summary",
81 	"debug"
82 };
83 
84 #ifndef NO_SYSCTL_DESCR
85 static const char *ahd_sysctl_node_descriptions[] = {
86 	"root error collection for aic79xx controllers",
87 	"summary collection for aic79xx controllers",
88 	"debug collection for aic79xx controllers"
89 };
90 #endif
91 
92 static const char *ahd_sysctl_errors_elements[] = {
93 	"Cerrors",
94 	"Uerrors",
95 	"Ferrors"
96 };
97 
98 #ifndef NO_SYSCTL_DESCR
99 static const char *ahd_sysctl_errors_descriptions[] = {
100 	"Correctable errors",
101 	"Uncorrectable errors",
102 	"Fatal errors"
103 };
104 #endif
105 
106 static int
ahd_set_debugcounters(SYSCTL_HANDLER_ARGS)107 ahd_set_debugcounters(SYSCTL_HANDLER_ARGS)
108 {
109 	struct ahd_softc *sc;
110 	int error, tmpv;
111 
112 	tmpv = 0;
113 	sc = arg1;
114 	error = sysctl_handle_int(oidp, &tmpv, 0, req);
115 	if (error != 0 || req->newptr == NULL)
116 		return (error);
117 	if (tmpv < 0 || tmpv >= AHD_ERRORS_NUMBER)
118 		return (EINVAL);
119 	sc->summerr[arg2] = tmpv;
120 	return (0);
121 }
122 
123 static int
ahd_clear_allcounters(SYSCTL_HANDLER_ARGS)124 ahd_clear_allcounters(SYSCTL_HANDLER_ARGS)
125 {
126 	struct ahd_softc *sc;
127 	int error, tmpv;
128 
129 	tmpv = 0;
130 	sc = arg1;
131 	error = sysctl_handle_int(oidp, &tmpv, 0, req);
132 	if (error != 0 || req->newptr == NULL)
133 		return (error);
134 	if (tmpv != 0)
135 		bzero(sc->summerr, sizeof(sc->summerr));
136 	return (0);
137 }
138 
139 static int
ahd_create_path(struct ahd_softc * ahd,char channel,u_int target,u_int lun,struct cam_path ** path)140 ahd_create_path(struct ahd_softc *ahd, char channel, u_int target,
141 	        u_int lun, struct cam_path **path)
142 {
143 	path_id_t path_id;
144 
145 	path_id = cam_sim_path(ahd->platform_data->sim);
146 	return (xpt_create_path(path, /*periph*/NULL,
147 				path_id, target, lun));
148 }
149 
150 void
ahd_sysctl(struct ahd_softc * ahd)151 ahd_sysctl(struct ahd_softc *ahd)
152 {
153 	u_int i;
154 
155 	for (i = 0; i < AHD_SYSCTL_NUMBER; i++)
156 		sysctl_ctx_init(&ahd->sysctl_ctx[i]);
157 
158 	ahd->sysctl_tree[AHD_SYSCTL_ROOT] =
159 	    SYSCTL_ADD_NODE(&ahd->sysctl_ctx[AHD_SYSCTL_ROOT],
160 	        SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
161 		device_get_nameunit(ahd->dev_softc),
162 		CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
163 		ahd_sysctl_node_descriptions[AHD_SYSCTL_ROOT]);
164 	    SYSCTL_ADD_PROC(&ahd->sysctl_ctx[AHD_SYSCTL_ROOT],
165 	        SYSCTL_CHILDREN(ahd->sysctl_tree[AHD_SYSCTL_ROOT]), OID_AUTO,
166 		"clear", CTLTYPE_UINT | CTLFLAG_RW | CTLFLAG_MPSAFE, ahd,
167 		0, ahd_clear_allcounters, "IU", "Clear all counters");
168 
169 	for (i = AHD_SYSCTL_SUMMARY; i < AHD_SYSCTL_NUMBER; i++)
170 		ahd->sysctl_tree[i] =
171 		    SYSCTL_ADD_NODE(&ahd->sysctl_ctx[i],
172 		        SYSCTL_CHILDREN(ahd->sysctl_tree[AHD_SYSCTL_ROOT]),
173 			OID_AUTO, ahd_sysctl_node_elements[i],
174 			CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
175 			ahd_sysctl_node_descriptions[i]);
176 
177 	for (i = AHD_ERRORS_CORRECTABLE; i < AHD_ERRORS_NUMBER; i++) {
178 		SYSCTL_ADD_UINT(&ahd->sysctl_ctx[AHD_SYSCTL_SUMMARY],
179 				SYSCTL_CHILDREN(ahd->sysctl_tree[AHD_SYSCTL_SUMMARY]),
180 				OID_AUTO, ahd_sysctl_errors_elements[i],
181 				CTLFLAG_RD, &ahd->summerr[i], i,
182 				ahd_sysctl_errors_descriptions[i]);
183 		SYSCTL_ADD_PROC(&ahd->sysctl_ctx[AHD_SYSCTL_DEBUG],
184 		    SYSCTL_CHILDREN(ahd->sysctl_tree[AHD_SYSCTL_DEBUG]),
185 		    OID_AUTO, ahd_sysctl_errors_elements[i],
186 		    CTLFLAG_RW | CTLTYPE_UINT | CTLFLAG_MPSAFE, ahd, i,
187 		    ahd_set_debugcounters, "IU",
188 		    ahd_sysctl_errors_descriptions[i]);
189 	}
190 }
191 
192 int
ahd_map_int(struct ahd_softc * ahd)193 ahd_map_int(struct ahd_softc *ahd)
194 {
195 	int error;
196 
197 	/* Hook up our interrupt handler */
198 	error = bus_setup_intr(ahd->dev_softc, ahd->platform_data->irq,
199 			       INTR_TYPE_CAM|INTR_MPSAFE, NULL,
200 			       ahd_platform_intr, ahd, &ahd->platform_data->ih);
201 	if (error != 0)
202 		device_printf(ahd->dev_softc, "bus_setup_intr() failed: %d\n",
203 			      error);
204 	return (error);
205 }
206 
207 /*
208  * Attach all the sub-devices we can find
209  */
210 int
ahd_attach(struct ahd_softc * ahd)211 ahd_attach(struct ahd_softc *ahd)
212 {
213 	char   ahd_info[256];
214 	struct ccb_setasync csa;
215 	struct cam_devq *devq;
216 	struct cam_sim *sim;
217 	struct cam_path *path;
218 	int count;
219 
220 	count = 0;
221 	devq = NULL;
222 	sim = NULL;
223 	path = NULL;
224 
225 	/*
226 	 * Create a thread to perform all recovery.
227 	 */
228 	if (ahd_spawn_recovery_thread(ahd) != 0)
229 		goto fail;
230 
231 	ahd_controller_info(ahd, ahd_info);
232 	printf("%s\n", ahd_info);
233 	ahd_lock(ahd);
234 
235 	/*
236 	 * Create the device queue for our SIM(s).
237 	 */
238 	devq = cam_simq_alloc(AHD_MAX_QUEUE);
239 	if (devq == NULL)
240 		goto fail;
241 
242 	/*
243 	 * Construct our SIM entry
244 	 */
245 	sim = cam_sim_alloc(ahd_action, ahd_poll, "ahd", ahd,
246 			    device_get_unit(ahd->dev_softc),
247 			    &ahd->platform_data->mtx, 1, /*XXX*/256, devq);
248 	if (sim == NULL) {
249 		cam_simq_free(devq);
250 		goto fail;
251 	}
252 
253 	if (xpt_bus_register(sim, ahd->dev_softc, /*bus_id*/0) != CAM_SUCCESS) {
254 		cam_sim_free(sim, /*free_devq*/TRUE);
255 		sim = NULL;
256 		goto fail;
257 	}
258 
259 	if (xpt_create_path(&path, /*periph*/NULL,
260 			    cam_sim_path(sim), CAM_TARGET_WILDCARD,
261 			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
262 		xpt_bus_deregister(cam_sim_path(sim));
263 		cam_sim_free(sim, /*free_devq*/TRUE);
264 		sim = NULL;
265 		goto fail;
266 	}
267 
268 	memset(&csa, 0, sizeof(csa));
269 	xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
270 	csa.ccb_h.func_code = XPT_SASYNC_CB;
271 	csa.event_enable = AC_LOST_DEVICE;
272 	csa.callback = ahd_async;
273 	csa.callback_arg = sim;
274 	xpt_action((union ccb *)&csa);
275 	count++;
276 
277 fail:
278 	ahd->platform_data->sim = sim;
279 	ahd->platform_data->path = path;
280 	ahd_unlock(ahd);
281 	if (count != 0) {
282 		/* We have to wait until after any system dumps... */
283 		ahd->platform_data->eh =
284 		    EVENTHANDLER_REGISTER(shutdown_final, ahd_shutdown,
285 					  ahd, SHUTDOWN_PRI_DEFAULT);
286 		ahd_intr_enable(ahd, TRUE);
287 	}
288 
289 	return (count);
290 }
291 
292 /*
293  * Catch an interrupt from the adapter
294  */
295 void
ahd_platform_intr(void * arg)296 ahd_platform_intr(void *arg)
297 {
298 	struct	ahd_softc *ahd;
299 
300 	ahd = (struct ahd_softc *)arg;
301 	ahd_lock(ahd);
302 	ahd_intr(ahd);
303 	ahd_unlock(ahd);
304 }
305 
306 static void
ahd_sync_ccb(struct ahd_softc * ahd,struct scb * scb,union ccb * ccb,bool post)307 ahd_sync_ccb(struct ahd_softc *ahd, struct scb *scb, union ccb *ccb, bool post)
308 {
309 	bus_dmasync_op_t op;
310 	uint32_t rdmask;
311 
312 	if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO)
313 		rdmask = CAM_DIR_OUT;
314 	else
315 		rdmask = CAM_DIR_IN;
316 
317 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) == rdmask)
318 		op = post ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_PREREAD;
319 	else
320 		op = post ? BUS_DMASYNC_POSTWRITE : BUS_DMASYNC_PREWRITE;
321 
322 	bus_dmamap_sync(ahd->buffer_dmat, scb->dmamap, op);
323 }
324 
325 /*
326  * We have an scb which has been processed by the
327  * adaptor, now we look to see how the operation
328  * went.
329  */
330 void
ahd_done(struct ahd_softc * ahd,struct scb * scb)331 ahd_done(struct ahd_softc *ahd, struct scb *scb)
332 {
333 	union ccb *ccb;
334 
335 	CAM_DEBUG(scb->io_ctx->ccb_h.path, CAM_DEBUG_TRACE,
336 		  ("ahd_done - scb %d\n", SCB_GET_TAG(scb)));
337 
338 	ccb = scb->io_ctx;
339 	LIST_REMOVE(scb, pending_links);
340 	if ((scb->flags & SCB_TIMEDOUT) != 0)
341 		LIST_REMOVE(scb, timedout_links);
342 
343 	callout_stop(&scb->io_timer);
344 
345 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
346 		ahd_sync_ccb(ahd, scb, ccb, true);
347 		bus_dmamap_unload(ahd->buffer_dmat, scb->dmamap);
348 	}
349 
350 #ifdef AHD_TARGET_MODE
351 	if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
352 		struct cam_path *ccb_path;
353 
354 		/*
355 		 * If we have finally disconnected, clean up our
356 		 * pending device state.
357 		 * XXX - There may be error states that cause where
358 		 *       we will remain connected.
359 		 */
360 		ccb_path = ccb->ccb_h.path;
361 		if (ahd->pending_device != NULL
362 		 && xpt_path_comp(ahd->pending_device->path, ccb_path) == 0) {
363 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
364 				ahd->pending_device = NULL;
365 			} else {
366 				xpt_print_path(ccb->ccb_h.path);
367 				printf("Still disconnected\n");
368 				ahd_freeze_ccb(ccb);
369 			}
370 		}
371 
372 		if (aic_get_transaction_status(scb) == CAM_REQ_INPROG)
373 			ccb->ccb_h.status |= CAM_REQ_CMP;
374 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
375 		ahd_free_scb(ahd, scb);
376 		xpt_done(ccb);
377 		return;
378 	}
379 #endif
380 
381 	if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
382 		struct	scb *list_scb;
383 
384 		ahd->scb_data.recovery_scbs--;
385 
386 		if (aic_get_transaction_status(scb) == CAM_BDR_SENT
387 		 || aic_get_transaction_status(scb) == CAM_REQ_ABORTED)
388 			aic_set_transaction_status(scb, CAM_CMD_TIMEOUT);
389 
390 		if (ahd->scb_data.recovery_scbs == 0) {
391 			/*
392 			 * All recovery actions have completed successfully,
393 			 * so reinstate the timeouts for all other pending
394 			 * commands.
395 			 */
396 			LIST_FOREACH(list_scb,
397 				     &ahd->pending_scbs, pending_links) {
398 				aic_scb_timer_reset(list_scb,
399 						    aic_get_timeout(scb));
400 			}
401 
402 			ahd_print_path(ahd, scb);
403 			printf("no longer in timeout, status = %x\n",
404 			       ccb->ccb_h.status);
405 		}
406 	}
407 
408 	/* Don't clobber any existing error state */
409 	if (aic_get_transaction_status(scb) == CAM_REQ_INPROG) {
410 		ccb->ccb_h.status |= CAM_REQ_CMP;
411 	} else if ((scb->flags & SCB_SENSE) != 0) {
412 		/*
413 		 * We performed autosense retrieval.
414 		 *
415 		 * Zero any sense not transferred by the
416 		 * device.  The SCSI spec mandates that any
417 		 * untransfered data should be assumed to be
418 		 * zero.  Complete the 'bounce' of sense information
419 		 * through buffers accessible via bus-space by
420 		 * copying it into the clients csio.
421 		 */
422 		memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
423 		memcpy(&ccb->csio.sense_data,
424 		       ahd_get_sense_buf(ahd, scb),
425 /* XXX What size do we want to use??? */
426 			sizeof(ccb->csio.sense_data)
427 		       - ccb->csio.sense_resid);
428 		scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
429 	} else if ((scb->flags & SCB_PKT_SENSE) != 0) {
430 		struct scsi_status_iu_header *siu;
431 		u_int sense_len;
432 
433 		/*
434 		 * Copy only the sense data into the provided buffer.
435 		 */
436 		siu = (struct scsi_status_iu_header *)scb->sense_data;
437 		sense_len = MIN(scsi_4btoul(siu->sense_length),
438 				sizeof(ccb->csio.sense_data));
439 		memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
440 		memcpy(&ccb->csio.sense_data,
441 		       ahd_get_sense_buf(ahd, scb) + SIU_SENSE_OFFSET(siu),
442 		       sense_len);
443 #ifdef AHD_DEBUG
444 		if ((ahd_debug & AHD_SHOW_SENSE) != 0) {
445 			uint8_t *sense_data = (uint8_t *)&ccb->csio.sense_data;
446 			u_int i;
447 
448 			printf("Copied %d bytes of sense data offset %d:",
449 			       sense_len, SIU_SENSE_OFFSET(siu));
450 			for (i = 0; i < sense_len; i++)
451 				printf(" 0x%x", *sense_data++);
452 			printf("\n");
453 		}
454 #endif
455 		scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
456 	}
457 	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
458 	ahd_free_scb(ahd, scb);
459 	xpt_done(ccb);
460 }
461 
462 static void
ahd_action(struct cam_sim * sim,union ccb * ccb)463 ahd_action(struct cam_sim *sim, union ccb *ccb)
464 {
465 	struct	ahd_softc *ahd;
466 #ifdef AHD_TARGET_MODE
467 	struct	ahd_tmode_lstate *lstate;
468 #endif
469 	u_int	target_id;
470 	u_int	our_id;
471 
472 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahd_action\n"));
473 
474 	ahd = (struct ahd_softc *)cam_sim_softc(sim);
475 
476 	target_id = ccb->ccb_h.target_id;
477 	our_id = SIM_SCSI_ID(ahd, sim);
478 
479 	switch (ccb->ccb_h.func_code) {
480 	/* Common cases first */
481 #ifdef AHD_TARGET_MODE
482 	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
483 	case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/
484 	{
485 		struct	   ahd_tmode_tstate *tstate;
486 		cam_status status;
487 
488 		status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate,
489 					     &lstate, TRUE);
490 
491 		if (status != CAM_REQ_CMP) {
492 			if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
493 				/* Response from the black hole device */
494 				tstate = NULL;
495 				lstate = ahd->black_hole;
496 			} else {
497 				ccb->ccb_h.status = status;
498 				xpt_done(ccb);
499 				break;
500 			}
501 		}
502 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
503 			SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h,
504 					  sim_links.sle);
505 			ccb->ccb_h.status = CAM_REQ_INPROG;
506 			if ((ahd->flags & AHD_TQINFIFO_BLOCKED) != 0)
507 				ahd_run_tqinfifo(ahd, /*paused*/FALSE);
508 			break;
509 		}
510 
511 		/*
512 		 * The target_id represents the target we attempt to
513 		 * select.  In target mode, this is the initiator of
514 		 * the original command.
515 		 */
516 		our_id = target_id;
517 		target_id = ccb->csio.init_id;
518 		/* FALLTHROUGH */
519 	}
520 #endif
521 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
522 	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
523 	{
524 		struct	scb *scb;
525 		struct	hardware_scb *hscb;
526 		struct	ahd_initiator_tinfo *tinfo;
527 		struct	ahd_tmode_tstate *tstate;
528 		u_int	col_idx;
529 
530 		if ((ahd->flags & AHD_INITIATORROLE) == 0
531 		 && (ccb->ccb_h.func_code == XPT_SCSI_IO
532 		  || ccb->ccb_h.func_code == XPT_RESET_DEV)) {
533 			ccb->ccb_h.status = CAM_PROVIDE_FAIL;
534 			xpt_done(ccb);
535 			return;
536 		}
537 
538 		/*
539 		 * get an scb to use.
540 		 */
541 		tinfo = ahd_fetch_transinfo(ahd, 'A', our_id,
542 					    target_id, &tstate);
543 		if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) == 0
544 		 || (tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0
545 		 || ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
546 			col_idx = AHD_NEVER_COL_IDX;
547 		} else {
548 			col_idx = AHD_BUILD_COL_IDX(target_id,
549 						    ccb->ccb_h.target_lun);
550 		}
551 		if ((scb = ahd_get_scb(ahd, col_idx)) == NULL) {
552 			xpt_freeze_simq(sim, /*count*/1);
553 			ahd->flags |= AHD_RESOURCE_SHORTAGE;
554 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
555 			xpt_done(ccb);
556 			return;
557 		}
558 
559 		hscb = scb->hscb;
560 
561 		CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
562 			  ("start scb(%p)\n", scb));
563 		scb->io_ctx = ccb;
564 		/*
565 		 * So we can find the SCB when an abort is requested
566 		 */
567 		ccb->ccb_h.ccb_scb_ptr = scb;
568 
569 		/*
570 		 * Put all the arguments for the xfer in the scb
571 		 */
572 		hscb->control = 0;
573 		hscb->scsiid = BUILD_SCSIID(ahd, sim, target_id, our_id);
574 		hscb->lun = ccb->ccb_h.target_lun;
575 		if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
576 			hscb->cdb_len = 0;
577 			scb->flags |= SCB_DEVICE_RESET;
578 			hscb->control |= MK_MESSAGE;
579 			hscb->task_management = SIU_TASKMGMT_LUN_RESET;
580 			ahd_execute_scb(scb, NULL, 0, 0);
581 		} else {
582 #ifdef AHD_TARGET_MODE
583 			if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
584 				struct target_data *tdata;
585 
586 				tdata = &hscb->shared_data.tdata;
587 				if (ahd->pending_device == lstate)
588 					scb->flags |= SCB_TARGET_IMMEDIATE;
589 				hscb->control |= TARGET_SCB;
590 				tdata->target_phases = 0;
591 				if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
592 					tdata->target_phases |= SPHASE_PENDING;
593 					tdata->scsi_status =
594 					    ccb->csio.scsi_status;
595 				}
596 	 			if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
597 					tdata->target_phases |= NO_DISCONNECT;
598 
599 				tdata->initiator_tag =
600 				    ahd_htole16(ccb->csio.tag_id);
601 			}
602 #endif
603 			hscb->task_management = 0;
604 			if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID)
605 				hscb->control |= ccb->csio.tag_action;
606 
607 			ahd_setup_data(ahd, sim, &ccb->csio, scb);
608 		}
609 		break;
610 	}
611 #ifdef AHD_TARGET_MODE
612 	case XPT_NOTIFY_ACKNOWLEDGE:
613 	case XPT_IMMEDIATE_NOTIFY:
614 	{
615 		struct	   ahd_tmode_tstate *tstate;
616 		struct	   ahd_tmode_lstate *lstate;
617 		cam_status status;
618 
619 		status = ahd_find_tmode_devs(ahd, sim, ccb, &tstate,
620 					     &lstate, TRUE);
621 
622 		if (status != CAM_REQ_CMP) {
623 			ccb->ccb_h.status = status;
624 			xpt_done(ccb);
625 			break;
626 		}
627 		SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h,
628 				  sim_links.sle);
629 		ccb->ccb_h.status = CAM_REQ_INPROG;
630 		ahd_send_lstate_events(ahd, lstate);
631 		break;
632 	}
633 	case XPT_EN_LUN:		/* Enable LUN as a target */
634 		ahd_handle_en_lun(ahd, sim, ccb);
635 		xpt_done(ccb);
636 		break;
637 #endif
638 	case XPT_ABORT:			/* Abort the specified CCB */
639 	{
640 		ahd_abort_ccb(ahd, sim, ccb);
641 		break;
642 	}
643 	case XPT_SET_TRAN_SETTINGS:
644 	{
645 		ahd_set_tran_settings(ahd, SIM_SCSI_ID(ahd, sim),
646 				      SIM_CHANNEL(ahd, sim), &ccb->cts);
647 		xpt_done(ccb);
648 		break;
649 	}
650 	case XPT_GET_TRAN_SETTINGS:
651 	/* Get default/user set transfer settings for the target */
652 	{
653 		ahd_get_tran_settings(ahd, SIM_SCSI_ID(ahd, sim),
654 				      SIM_CHANNEL(ahd, sim), &ccb->cts);
655 		xpt_done(ccb);
656 		break;
657 	}
658 	case XPT_CALC_GEOMETRY:
659 	{
660 		aic_calc_geometry(&ccb->ccg, ahd->flags & AHD_EXTENDED_TRANS_A);
661 		xpt_done(ccb);
662 		break;
663 	}
664 	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
665 	{
666 		int  found;
667 
668 		found = ahd_reset_channel(ahd, SIM_CHANNEL(ahd, sim),
669 					  /*initiate reset*/TRUE);
670 		if (bootverbose) {
671 			xpt_print_path(SIM_PATH(ahd, sim));
672 			printf("SCSI bus reset delivered. "
673 			       "%d SCBs aborted.\n", found);
674 		}
675 		ccb->ccb_h.status = CAM_REQ_CMP;
676 		xpt_done(ccb);
677 		break;
678 	}
679 	case XPT_TERM_IO:		/* Terminate the I/O process */
680 		/* XXX Implement */
681 		ccb->ccb_h.status = CAM_REQ_INVALID;
682 		xpt_done(ccb);
683 		break;
684 	case XPT_PATH_INQ:		/* Path routing inquiry */
685 	{
686 		struct ccb_pathinq *cpi = &ccb->cpi;
687 
688 		cpi->version_num = 1; /* XXX??? */
689 		cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
690 		if ((ahd->features & AHD_WIDE) != 0)
691 			cpi->hba_inquiry |= PI_WIDE_16;
692 		if ((ahd->features & AHD_TARGETMODE) != 0) {
693 			cpi->target_sprt = PIT_PROCESSOR
694 					 | PIT_DISCONNECT
695 					 | PIT_TERM_IO;
696 		} else {
697 			cpi->target_sprt = 0;
698 		}
699 		cpi->hba_misc = 0;
700 		cpi->hba_eng_cnt = 0;
701 		cpi->max_target = (ahd->features & AHD_WIDE) ? 15 : 7;
702 		cpi->max_lun = AHD_NUM_LUNS_NONPKT - 1;
703 		cpi->initiator_id = ahd->our_id;
704 		if ((ahd->flags & AHD_RESET_BUS_A) == 0) {
705 			cpi->hba_misc |= PIM_NOBUSRESET;
706 		}
707 		cpi->bus_id = cam_sim_bus(sim);
708 		cpi->base_transfer_speed = 3300;
709 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
710 		strlcpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
711 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
712 		cpi->unit_number = cam_sim_unit(sim);
713 		cpi->protocol = PROTO_SCSI;
714 		cpi->protocol_version = SCSI_REV_2;
715 		cpi->transport = XPORT_SPI;
716 		cpi->transport_version = 4;
717 		cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_DT_ST
718 						    | SID_SPI_IUS
719 						    | SID_SPI_QAS;
720 		cpi->ccb_h.status = CAM_REQ_CMP;
721 		xpt_done(ccb);
722 		break;
723 	}
724 	default:
725 		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
726 		xpt_done(ccb);
727 		break;
728 	}
729 }
730 
731 static void
ahd_set_tran_settings(struct ahd_softc * ahd,int our_id,char channel,struct ccb_trans_settings * cts)732 ahd_set_tran_settings(struct ahd_softc *ahd, int our_id, char channel,
733 		      struct ccb_trans_settings *cts)
734 {
735 	struct	  ahd_devinfo devinfo;
736 	struct	  ccb_trans_settings_scsi *scsi;
737 	struct	  ccb_trans_settings_spi *spi;
738 	struct	  ahd_initiator_tinfo *tinfo;
739 	struct	  ahd_tmode_tstate *tstate;
740 	uint16_t *discenable;
741 	uint16_t *tagenable;
742 	u_int	  update_type;
743 
744 	scsi = &cts->proto_specific.scsi;
745 	spi = &cts->xport_specific.spi;
746 	ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
747 			    cts->ccb_h.target_id,
748 			    cts->ccb_h.target_lun,
749 			    SIM_CHANNEL(ahd, sim),
750 			    ROLE_UNKNOWN);
751 	tinfo = ahd_fetch_transinfo(ahd, devinfo.channel,
752 				    devinfo.our_scsiid,
753 				    devinfo.target, &tstate);
754 	update_type = 0;
755 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
756 		update_type |= AHD_TRANS_GOAL;
757 		discenable = &tstate->discenable;
758 		tagenable = &tstate->tagenable;
759 		tinfo->curr.protocol_version = cts->protocol_version;
760 		tinfo->curr.transport_version = cts->transport_version;
761 		tinfo->goal.protocol_version = cts->protocol_version;
762 		tinfo->goal.transport_version = cts->transport_version;
763 	} else if (cts->type == CTS_TYPE_USER_SETTINGS) {
764 		update_type |= AHD_TRANS_USER;
765 		discenable = &ahd->user_discenable;
766 		tagenable = &ahd->user_tagenable;
767 		tinfo->user.protocol_version = cts->protocol_version;
768 		tinfo->user.transport_version = cts->transport_version;
769 	} else {
770 		cts->ccb_h.status = CAM_REQ_INVALID;
771 		return;
772 	}
773 
774 	if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
775 		if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
776 			*discenable |= devinfo.target_mask;
777 		else
778 			*discenable &= ~devinfo.target_mask;
779 	}
780 
781 	if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
782 		if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
783 			*tagenable |= devinfo.target_mask;
784 		else
785 			*tagenable &= ~devinfo.target_mask;
786 	}
787 
788 	if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
789 		ahd_validate_width(ahd, /*tinfo limit*/NULL,
790 				   &spi->bus_width, ROLE_UNKNOWN);
791 		ahd_set_width(ahd, &devinfo, spi->bus_width,
792 			      update_type, /*paused*/FALSE);
793 	}
794 
795 	if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) {
796 		if (update_type == AHD_TRANS_USER)
797 			spi->ppr_options = tinfo->user.ppr_options;
798 		else
799 			spi->ppr_options = tinfo->goal.ppr_options;
800 	}
801 
802 	if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) {
803 		if (update_type == AHD_TRANS_USER)
804 			spi->sync_offset = tinfo->user.offset;
805 		else
806 			spi->sync_offset = tinfo->goal.offset;
807 	}
808 
809 	if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
810 		if (update_type == AHD_TRANS_USER)
811 			spi->sync_period = tinfo->user.period;
812 		else
813 			spi->sync_period = tinfo->goal.period;
814 	}
815 
816 	if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
817 	 || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
818 		u_int	maxsync;
819 
820 		maxsync = AHD_SYNCRATE_MAX;
821 
822 		if (spi->bus_width != MSG_EXT_WDTR_BUS_16_BIT)
823 			spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
824 
825 		if ((*discenable & devinfo.target_mask) == 0)
826 			spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
827 
828 		ahd_find_syncrate(ahd, &spi->sync_period,
829 				  &spi->ppr_options, maxsync);
830 		ahd_validate_offset(ahd, /*tinfo limit*/NULL,
831 				    spi->sync_period, &spi->sync_offset,
832 				    spi->bus_width, ROLE_UNKNOWN);
833 
834 		/* We use a period of 0 to represent async */
835 		if (spi->sync_offset == 0) {
836 			spi->sync_period = 0;
837 			spi->ppr_options = 0;
838 		}
839 
840 		ahd_set_syncrate(ahd, &devinfo, spi->sync_period,
841 				 spi->sync_offset, spi->ppr_options,
842 				 update_type, /*paused*/FALSE);
843 	}
844 	cts->ccb_h.status = CAM_REQ_CMP;
845 }
846 
847 static void
ahd_get_tran_settings(struct ahd_softc * ahd,int our_id,char channel,struct ccb_trans_settings * cts)848 ahd_get_tran_settings(struct ahd_softc *ahd, int our_id, char channel,
849 		      struct ccb_trans_settings *cts)
850 {
851 	struct	ahd_devinfo devinfo;
852 	struct	ccb_trans_settings_scsi *scsi;
853 	struct	ccb_trans_settings_spi *spi;
854 	struct	ahd_initiator_tinfo *targ_info;
855 	struct	ahd_tmode_tstate *tstate;
856 	struct	ahd_transinfo *tinfo;
857 
858 	scsi = &cts->proto_specific.scsi;
859 	spi = &cts->xport_specific.spi;
860 	ahd_compile_devinfo(&devinfo, our_id,
861 			    cts->ccb_h.target_id,
862 			    cts->ccb_h.target_lun,
863 			    channel, ROLE_UNKNOWN);
864 	targ_info = ahd_fetch_transinfo(ahd, devinfo.channel,
865 					devinfo.our_scsiid,
866 					devinfo.target, &tstate);
867 
868 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
869 		tinfo = &targ_info->curr;
870 	else
871 		tinfo = &targ_info->user;
872 
873 	scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
874 	spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
875 	if (cts->type == CTS_TYPE_USER_SETTINGS) {
876 		if ((ahd->user_discenable & devinfo.target_mask) != 0)
877 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
878 
879 		if ((ahd->user_tagenable & devinfo.target_mask) != 0)
880 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
881 	} else {
882 		if ((tstate->discenable & devinfo.target_mask) != 0)
883 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
884 
885 		if ((tstate->tagenable & devinfo.target_mask) != 0)
886 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
887 	}
888 	cts->protocol_version = tinfo->protocol_version;
889 	cts->transport_version = tinfo->transport_version;
890 
891 	spi->sync_period = tinfo->period;
892 	spi->sync_offset = tinfo->offset;
893 	spi->bus_width = tinfo->width;
894 	spi->ppr_options = tinfo->ppr_options;
895 
896 	cts->protocol = PROTO_SCSI;
897 	cts->transport = XPORT_SPI;
898 	spi->valid = CTS_SPI_VALID_SYNC_RATE
899 		   | CTS_SPI_VALID_SYNC_OFFSET
900 		   | CTS_SPI_VALID_BUS_WIDTH
901 		   | CTS_SPI_VALID_PPR_OPTIONS;
902 
903 	if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
904 		scsi->valid = CTS_SCSI_VALID_TQ;
905 		spi->valid |= CTS_SPI_VALID_DISC;
906 	} else {
907 		scsi->valid = 0;
908 	}
909 
910 	cts->ccb_h.status = CAM_REQ_CMP;
911 }
912 
913 static void
ahd_async(void * callback_arg,uint32_t code,struct cam_path * path,void * arg)914 ahd_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
915 {
916 	struct ahd_softc *ahd;
917 	struct cam_sim *sim;
918 
919 	sim = (struct cam_sim *)callback_arg;
920 	ahd = (struct ahd_softc *)cam_sim_softc(sim);
921 	switch (code) {
922 	case AC_LOST_DEVICE:
923 	{
924 		struct	ahd_devinfo devinfo;
925 
926 		ahd_compile_devinfo(&devinfo, SIM_SCSI_ID(ahd, sim),
927 				    xpt_path_target_id(path),
928 				    xpt_path_lun_id(path),
929 				    SIM_CHANNEL(ahd, sim),
930 				    ROLE_UNKNOWN);
931 
932 		/*
933 		 * Revert to async/narrow transfers
934 		 * for the next device.
935 		 */
936 		ahd_set_width(ahd, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
937 			      AHD_TRANS_GOAL|AHD_TRANS_CUR, /*paused*/FALSE);
938 		ahd_set_syncrate(ahd, &devinfo, /*period*/0, /*offset*/0,
939 				 /*ppr_options*/0, AHD_TRANS_GOAL|AHD_TRANS_CUR,
940 				 /*paused*/FALSE);
941 		break;
942 	}
943 	default:
944 		break;
945 	}
946 }
947 
948 static void
ahd_execute_scb(void * arg,bus_dma_segment_t * dm_segs,int nsegments,int error)949 ahd_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
950 		int error)
951 {
952 	struct	scb *scb;
953 	union	ccb *ccb;
954 	struct	ahd_softc *ahd;
955 	struct	ahd_initiator_tinfo *tinfo;
956 	struct	ahd_tmode_tstate *tstate;
957 	u_int	mask;
958 
959 	scb = (struct scb *)arg;
960 	ccb = scb->io_ctx;
961 	ahd = scb->ahd_softc;
962 
963 	if (error != 0) {
964 		if (error == EFBIG)
965 			aic_set_transaction_status(scb, CAM_REQ_TOO_BIG);
966 		else
967 			aic_set_transaction_status(scb, CAM_REQ_CMP_ERR);
968 		if (nsegments != 0)
969 			bus_dmamap_unload(ahd->buffer_dmat, scb->dmamap);
970 		ahd_free_scb(ahd, scb);
971 		xpt_done(ccb);
972 		return;
973 	}
974 	scb->sg_count = 0;
975 	if (nsegments != 0) {
976 		void *sg;
977 		u_int i;
978 
979 		/* Copy the segments into our SG list */
980 		for (i = nsegments, sg = scb->sg_list; i > 0; i--) {
981 			sg = ahd_sg_setup(ahd, scb, sg, dm_segs->ds_addr,
982 					  dm_segs->ds_len,
983 					  /*last*/i == 1);
984 			dm_segs++;
985 		}
986 
987 		ahd_sync_ccb(ahd, scb, ccb, false);
988 
989 		if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
990 			struct target_data *tdata;
991 
992 			tdata = &scb->hscb->shared_data.tdata;
993 			tdata->target_phases |= DPHASE_PENDING;
994 			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
995 				tdata->data_phase = P_DATAOUT;
996 			else
997 				tdata->data_phase = P_DATAIN;
998 		}
999 	}
1000 
1001 	/*
1002 	 * Last time we need to check if this SCB needs to
1003 	 * be aborted.
1004 	 */
1005 	if (aic_get_transaction_status(scb) != CAM_REQ_INPROG) {
1006 		if (nsegments != 0)
1007 			bus_dmamap_unload(ahd->buffer_dmat,
1008 					  scb->dmamap);
1009 		ahd_free_scb(ahd, scb);
1010 		xpt_done(ccb);
1011 		return;
1012 	}
1013 
1014 	tinfo = ahd_fetch_transinfo(ahd, SCSIID_CHANNEL(ahd, scb->hscb->scsiid),
1015 				    SCSIID_OUR_ID(scb->hscb->scsiid),
1016 				    SCSIID_TARGET(ahd, scb->hscb->scsiid),
1017 				    &tstate);
1018 
1019 	mask = SCB_GET_TARGET_MASK(ahd, scb);
1020 
1021 	if ((tstate->discenable & mask) != 0
1022 	 && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
1023 		scb->hscb->control |= DISCENB;
1024 
1025 	if ((tinfo->curr.ppr_options & MSG_EXT_PPR_IU_REQ) != 0) {
1026 		scb->flags |= SCB_PACKETIZED;
1027 		if (scb->hscb->task_management != 0)
1028 			scb->hscb->control &= ~MK_MESSAGE;
1029 	}
1030 
1031 	if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
1032 	 && (tinfo->goal.width != 0
1033 	  || tinfo->goal.period != 0
1034 	  || tinfo->goal.ppr_options != 0)) {
1035 		scb->flags |= SCB_NEGOTIATE;
1036 		scb->hscb->control |= MK_MESSAGE;
1037 	} else if ((tstate->auto_negotiate & mask) != 0) {
1038 		scb->flags |= SCB_AUTO_NEGOTIATE;
1039 		scb->hscb->control |= MK_MESSAGE;
1040 	}
1041 
1042 	LIST_INSERT_HEAD(&ahd->pending_scbs, scb, pending_links);
1043 
1044 	ccb->ccb_h.status |= CAM_SIM_QUEUED;
1045 
1046 	aic_scb_timer_start(scb);
1047 
1048 	if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
1049 		/* Define a mapping from our tag to the SCB. */
1050 		ahd->scb_data.scbindex[SCB_GET_TAG(scb)] = scb;
1051 		ahd_pause(ahd);
1052 		ahd_set_scbptr(ahd, SCB_GET_TAG(scb));
1053 		ahd_outb(ahd, RETURN_1, CONT_MSG_LOOP_TARG);
1054 		ahd_unpause(ahd);
1055 	} else {
1056 		ahd_queue_scb(ahd, scb);
1057 	}
1058 
1059 }
1060 
1061 static void
ahd_poll(struct cam_sim * sim)1062 ahd_poll(struct cam_sim *sim)
1063 {
1064 	ahd_intr(cam_sim_softc(sim));
1065 }
1066 
1067 static void
ahd_setup_data(struct ahd_softc * ahd,struct cam_sim * sim,struct ccb_scsiio * csio,struct scb * scb)1068 ahd_setup_data(struct ahd_softc *ahd, struct cam_sim *sim,
1069 	       struct ccb_scsiio *csio, struct scb *scb)
1070 {
1071 	struct hardware_scb *hscb;
1072 	struct ccb_hdr *ccb_h;
1073 	int error;
1074 
1075 	hscb = scb->hscb;
1076 	ccb_h = &csio->ccb_h;
1077 
1078 	csio->resid = 0;
1079 	csio->sense_resid = 0;
1080 	if (ccb_h->func_code == XPT_SCSI_IO) {
1081 		hscb->cdb_len = csio->cdb_len;
1082 		if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
1083 			if (hscb->cdb_len > MAX_CDB_LEN
1084 			 && (ccb_h->flags & CAM_CDB_PHYS) == 0) {
1085 				/*
1086 				 * Should CAM start to support CDB sizes
1087 				 * greater than 16 bytes, we could use
1088 				 * the sense buffer to store the CDB.
1089 				 */
1090 				aic_set_transaction_status(scb,
1091 							   CAM_REQ_INVALID);
1092 				ahd_free_scb(ahd, scb);
1093 				xpt_done((union ccb *)csio);
1094 				return;
1095 			}
1096 			if ((ccb_h->flags & CAM_CDB_PHYS) != 0) {
1097 				hscb->shared_data.idata.cdb_from_host.cdbptr =
1098 				   aic_htole64((uintptr_t)csio->cdb_io.cdb_ptr);
1099 				hscb->shared_data.idata.cdb_from_host.cdblen =
1100 				   csio->cdb_len;
1101 				hscb->cdb_len |= SCB_CDB_LEN_PTR;
1102 			} else {
1103 				memcpy(hscb->shared_data.idata.cdb,
1104 				       csio->cdb_io.cdb_ptr,
1105 				       hscb->cdb_len);
1106 			}
1107 		} else {
1108 			if (hscb->cdb_len > MAX_CDB_LEN) {
1109 				aic_set_transaction_status(scb,
1110 							   CAM_REQ_INVALID);
1111 				ahd_free_scb(ahd, scb);
1112 				xpt_done((union ccb *)csio);
1113 				return;
1114 			}
1115 			memcpy(hscb->shared_data.idata.cdb,
1116 			       csio->cdb_io.cdb_bytes, hscb->cdb_len);
1117 		}
1118 	}
1119 
1120 	error = bus_dmamap_load_ccb(ahd->buffer_dmat,
1121 				    scb->dmamap,
1122 				    (union ccb *)csio,
1123 				    ahd_execute_scb,
1124 				    scb, /*flags*/0);
1125 	if (error == EINPROGRESS) {
1126 		/*
1127 		 * So as to maintain ordering, freeze the controller queue
1128 		 * until our mapping is returned.
1129 		 */
1130 		xpt_freeze_simq(sim, /*count*/1);
1131 		scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
1132 	}
1133 }
1134 
1135 static void
ahd_abort_ccb(struct ahd_softc * ahd,struct cam_sim * sim,union ccb * ccb)1136 ahd_abort_ccb(struct ahd_softc *ahd, struct cam_sim *sim, union ccb *ccb)
1137 {
1138 	union ccb *abort_ccb;
1139 
1140 	abort_ccb = ccb->cab.abort_ccb;
1141 	switch (abort_ccb->ccb_h.func_code) {
1142 #ifdef AHD_TARGET_MODE
1143 	case XPT_ACCEPT_TARGET_IO:
1144 	case XPT_IMMEDIATE_NOTIFY:
1145 	case XPT_CONT_TARGET_IO:
1146 	{
1147 		struct ahd_tmode_tstate *tstate;
1148 		struct ahd_tmode_lstate *lstate;
1149 		struct ccb_hdr_slist *list;
1150 		cam_status status;
1151 
1152 		status = ahd_find_tmode_devs(ahd, sim, abort_ccb, &tstate,
1153 					     &lstate, TRUE);
1154 
1155 		if (status != CAM_REQ_CMP) {
1156 			ccb->ccb_h.status = status;
1157 			break;
1158 		}
1159 
1160 		if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1161 			list = &lstate->accept_tios;
1162 		else if (abort_ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY)
1163 			list = &lstate->immed_notifies;
1164 		else
1165 			list = NULL;
1166 
1167 		if (list != NULL) {
1168 			struct ccb_hdr *curelm;
1169 			int found;
1170 
1171 			curelm = SLIST_FIRST(list);
1172 			found = 0;
1173 			if (curelm == &abort_ccb->ccb_h) {
1174 				found = 1;
1175 				SLIST_REMOVE_HEAD(list, sim_links.sle);
1176 			} else {
1177 				while(curelm != NULL) {
1178 					struct ccb_hdr *nextelm;
1179 
1180 					nextelm =
1181 					    SLIST_NEXT(curelm, sim_links.sle);
1182 
1183 					if (nextelm == &abort_ccb->ccb_h) {
1184 						found = 1;
1185 						SLIST_NEXT(curelm,
1186 							   sim_links.sle) =
1187 						    SLIST_NEXT(nextelm,
1188 							       sim_links.sle);
1189 						break;
1190 					}
1191 					curelm = nextelm;
1192 				}
1193 			}
1194 
1195 			if (found) {
1196 				abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1197 				xpt_done(abort_ccb);
1198 				ccb->ccb_h.status = CAM_REQ_CMP;
1199 			} else {
1200 				xpt_print_path(abort_ccb->ccb_h.path);
1201 				printf("Not found\n");
1202 				ccb->ccb_h.status = CAM_PATH_INVALID;
1203 			}
1204 			break;
1205 		}
1206 		/* FALLTHROUGH */
1207 	}
1208 #endif
1209 	case XPT_SCSI_IO:
1210 		/* XXX Fully implement the hard ones */
1211 		ccb->ccb_h.status = CAM_UA_ABORT;
1212 		break;
1213 	default:
1214 		ccb->ccb_h.status = CAM_REQ_INVALID;
1215 		break;
1216 	}
1217 	xpt_done(ccb);
1218 }
1219 
1220 void
ahd_send_async(struct ahd_softc * ahd,char channel,u_int target,u_int lun,ac_code code,void * opt_arg)1221 ahd_send_async(struct ahd_softc *ahd, char channel, u_int target,
1222 		u_int lun, ac_code code, void *opt_arg)
1223 {
1224 	struct	ccb_trans_settings cts;
1225 	struct cam_path *path;
1226 	void *arg;
1227 	int error;
1228 
1229 	arg = NULL;
1230 	error = ahd_create_path(ahd, channel, target, lun, &path);
1231 
1232 	if (error != CAM_REQ_CMP)
1233 		return;
1234 
1235 	switch (code) {
1236 	case AC_TRANSFER_NEG:
1237 	{
1238 		struct	ccb_trans_settings_scsi *scsi;
1239 
1240 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1241 		scsi = &cts.proto_specific.scsi;
1242 		cts.ccb_h.path = path;
1243 		cts.ccb_h.target_id = target;
1244 		cts.ccb_h.target_lun = lun;
1245 		ahd_get_tran_settings(ahd, ahd->our_id, channel, &cts);
1246 		arg = &cts;
1247 		scsi->valid &= ~CTS_SCSI_VALID_TQ;
1248 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1249 		if (opt_arg == NULL)
1250 			break;
1251 		if (*((ahd_queue_alg *)opt_arg) == AHD_QUEUE_TAGGED)
1252 			scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1253 		scsi->valid |= CTS_SCSI_VALID_TQ;
1254 		break;
1255 	}
1256 	case AC_SENT_BDR:
1257 	case AC_BUS_RESET:
1258 		break;
1259 	default:
1260 		panic("ahd_send_async: Unexpected async event");
1261 	}
1262 	xpt_async(code, path, arg);
1263 	xpt_free_path(path);
1264 }
1265 
1266 void
ahd_platform_set_tags(struct ahd_softc * ahd,struct ahd_devinfo * devinfo,int enable)1267 ahd_platform_set_tags(struct ahd_softc *ahd,
1268 		      struct ahd_devinfo *devinfo, int enable)
1269 {
1270 }
1271 
1272 int
ahd_platform_alloc(struct ahd_softc * ahd,void * platform_arg)1273 ahd_platform_alloc(struct ahd_softc *ahd, void *platform_arg)
1274 {
1275 	ahd->platform_data = malloc(sizeof(struct ahd_platform_data), M_DEVBUF,
1276 	    M_NOWAIT | M_ZERO);
1277 	if (ahd->platform_data == NULL)
1278 		return (ENOMEM);
1279 	return (0);
1280 }
1281 
1282 void
ahd_platform_free(struct ahd_softc * ahd)1283 ahd_platform_free(struct ahd_softc *ahd)
1284 {
1285 	struct ahd_platform_data *pdata;
1286 
1287 	pdata = ahd->platform_data;
1288 	if (pdata != NULL) {
1289 		if (pdata->regs[0] != NULL)
1290 			bus_release_resource(ahd->dev_softc,
1291 					     pdata->regs_res_type[0],
1292 					     pdata->regs_res_id[0],
1293 					     pdata->regs[0]);
1294 
1295 		if (pdata->regs[1] != NULL)
1296 			bus_release_resource(ahd->dev_softc,
1297 					     pdata->regs_res_type[1],
1298 					     pdata->regs_res_id[1],
1299 					     pdata->regs[1]);
1300 
1301 		if (pdata->irq != NULL)
1302 			bus_release_resource(ahd->dev_softc,
1303 					     pdata->irq_res_type,
1304 					     0, pdata->irq);
1305 
1306 		if (pdata->sim != NULL) {
1307 			xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1308 			xpt_free_path(pdata->path);
1309 			xpt_bus_deregister(cam_sim_path(pdata->sim));
1310 			cam_sim_free(pdata->sim, /*free_devq*/TRUE);
1311 		}
1312 		if (pdata->eh != NULL)
1313 			EVENTHANDLER_DEREGISTER(shutdown_final, pdata->eh);
1314 		free(ahd->platform_data, M_DEVBUF);
1315 	}
1316 }
1317 
1318 int
ahd_softc_comp(struct ahd_softc * lahd,struct ahd_softc * rahd)1319 ahd_softc_comp(struct ahd_softc *lahd, struct ahd_softc *rahd)
1320 {
1321 	/* We don't sort softcs under FreeBSD so report equal always */
1322 	return (0);
1323 }
1324 
1325 int
ahd_detach(device_t dev)1326 ahd_detach(device_t dev)
1327 {
1328 	struct ahd_softc *ahd;
1329 
1330 	device_printf(dev, "detaching device\n");
1331 	ahd = device_get_softc(dev);
1332 	ahd_lock(ahd);
1333 	TAILQ_REMOVE(&ahd_tailq, ahd, links);
1334 	ahd_intr_enable(ahd, FALSE);
1335 	bus_teardown_intr(dev, ahd->platform_data->irq, ahd->platform_data->ih);
1336 	ahd_unlock(ahd);
1337 	ahd_free(ahd);
1338 	return (0);
1339 }
1340 
1341 #if 0
1342 static void
1343 ahd_dump_targcmd(struct target_cmd *cmd)
1344 {
1345 	uint8_t *byte;
1346 	uint8_t *last_byte;
1347 	int i;
1348 
1349 	byte = &cmd->initiator_channel;
1350 	/* Debugging info for received commands */
1351 	last_byte = &cmd[1].initiator_channel;
1352 
1353 	i = 0;
1354 	while (byte < last_byte) {
1355 		if (i == 0)
1356 			printf("\t");
1357 		printf("%#x", *byte++);
1358 		i++;
1359 		if (i == 8) {
1360 			printf("\n");
1361 			i = 0;
1362 		} else {
1363 			printf(", ");
1364 		}
1365 	}
1366 }
1367 #endif
1368 
1369 static int
ahd_modevent(module_t mod,int type,void * data)1370 ahd_modevent(module_t mod, int type, void *data)
1371 {
1372 	/* XXX Deal with busy status on unload. */
1373 	/* XXX Deal with unknown events */
1374 	return 0;
1375 }
1376 
1377 static moduledata_t ahd_mod = {
1378 	"ahd",
1379 	ahd_modevent,
1380 	NULL
1381 };
1382 
1383 /********************************** DDB Hooks *********************************/
1384 #ifdef DDB
1385 static struct ahd_softc *ahd_ddb_softc;
1386 static int ahd_ddb_paused;
1387 static int ahd_ddb_paused_on_entry;
DB_COMMAND(ahd_sunit,ahd_ddb_sunit)1388 DB_COMMAND(ahd_sunit, ahd_ddb_sunit)
1389 {
1390 	struct ahd_softc *list_ahd;
1391 
1392 	ahd_ddb_softc = NULL;
1393 	TAILQ_FOREACH(list_ahd, &ahd_tailq, links) {
1394 		if (list_ahd->unit == addr)
1395 			ahd_ddb_softc = list_ahd;
1396 	}
1397 	if (ahd_ddb_softc == NULL)
1398 		db_error("No matching softc found!\n");
1399 }
1400 
DB_COMMAND(ahd_pause,ahd_ddb_pause)1401 DB_COMMAND(ahd_pause, ahd_ddb_pause)
1402 {
1403 	if (ahd_ddb_softc == NULL) {
1404 		db_error("Must set unit with ahd_sunit first!\n");
1405 		return;
1406 	}
1407 	if (ahd_ddb_paused == 0) {
1408 		ahd_ddb_paused++;
1409 		if (ahd_is_paused(ahd_ddb_softc)) {
1410 			ahd_ddb_paused_on_entry++;
1411 			return;
1412 		}
1413 		ahd_pause(ahd_ddb_softc);
1414 	}
1415 }
1416 
DB_COMMAND(ahd_unpause,ahd_ddb_unpause)1417 DB_COMMAND(ahd_unpause, ahd_ddb_unpause)
1418 {
1419 	if (ahd_ddb_softc == NULL) {
1420 		db_error("Must set unit with ahd_sunit first!\n");
1421 		return;
1422 	}
1423 	if (ahd_ddb_paused != 0) {
1424 		ahd_ddb_paused = 0;
1425 		if (ahd_ddb_paused_on_entry)
1426 			return;
1427 		ahd_unpause(ahd_ddb_softc);
1428 	} else if (ahd_ddb_paused_on_entry != 0) {
1429 		/* Two unpauses to clear a paused on entry. */
1430 		ahd_ddb_paused_on_entry = 0;
1431 		ahd_unpause(ahd_ddb_softc);
1432 	}
1433 }
1434 
DB_COMMAND(ahd_in,ahd_ddb_in)1435 DB_COMMAND(ahd_in, ahd_ddb_in)
1436 {
1437 	int c;
1438 	int size;
1439 
1440 	if (ahd_ddb_softc == NULL) {
1441 		db_error("Must set unit with ahd_sunit first!\n");
1442 		return;
1443 	}
1444 	if (have_addr == 0)
1445 		return;
1446 
1447 	size = 1;
1448 	while ((c = *modif++) != '\0') {
1449 		switch (c) {
1450 		case 'b':
1451 			size = 1;
1452 			break;
1453 		case 'w':
1454 			size = 2;
1455 			break;
1456 		case 'l':
1457 			size = 4;
1458 		break;
1459 		}
1460 	}
1461 
1462 	if (count <= 0)
1463 		count = 1;
1464 	while (--count >= 0) {
1465 		db_printf("%04lx (M)%x: \t", (u_long)addr,
1466 			  ahd_inb(ahd_ddb_softc, MODE_PTR));
1467 		switch (size) {
1468 		case 1:
1469 			db_printf("%02x\n", ahd_inb(ahd_ddb_softc, addr));
1470 			break;
1471 		case 2:
1472 			db_printf("%04x\n", ahd_inw(ahd_ddb_softc, addr));
1473 			break;
1474 		case 4:
1475 			db_printf("%08x\n", ahd_inl(ahd_ddb_softc, addr));
1476 			break;
1477 		}
1478 	}
1479 }
1480 
DB_COMMAND_FLAGS(ahd_out,ahd_ddb_out,CS_MORE)1481 DB_COMMAND_FLAGS(ahd_out, ahd_ddb_out, CS_MORE)
1482 {
1483 	db_expr_t old_value;
1484 	db_expr_t new_value;
1485 	int	  size;
1486 
1487 	if (ahd_ddb_softc == NULL) {
1488 		db_error("Must set unit with ahd_sunit first!\n");
1489 		return;
1490 	}
1491 
1492 	switch (modif[0]) {
1493 	case '\0':
1494 	case 'b':
1495 		size = 1;
1496 		break;
1497 	case 'h':
1498 		size = 2;
1499 		break;
1500 	case 'l':
1501 		size = 4;
1502 		break;
1503 	default:
1504 		db_error("Unknown size\n");
1505 		return;
1506 	}
1507 
1508 	while (db_expression(&new_value)) {
1509 		switch (size) {
1510 		default:
1511 		case 1:
1512 			old_value = ahd_inb(ahd_ddb_softc, addr);
1513 			ahd_outb(ahd_ddb_softc, addr, new_value);
1514 			break;
1515 		case 2:
1516 			old_value = ahd_inw(ahd_ddb_softc, addr);
1517 			ahd_outw(ahd_ddb_softc, addr, new_value);
1518 			break;
1519 		case 4:
1520 			old_value = ahd_inl(ahd_ddb_softc, addr);
1521 			ahd_outl(ahd_ddb_softc, addr, new_value);
1522 			break;
1523 		}
1524 		db_printf("%04lx (M)%x: \t0x%lx\t=\t0x%lx",
1525 			  (u_long)addr, ahd_inb(ahd_ddb_softc, MODE_PTR),
1526 			  (u_long)old_value, (u_long)new_value);
1527 		addr += size;
1528 	}
1529 	db_skip_to_eol();
1530 }
1531 
DB_COMMAND(ahd_dump,ahd_ddb_dump)1532 DB_COMMAND(ahd_dump, ahd_ddb_dump)
1533 {
1534 	if (ahd_ddb_softc == NULL) {
1535 		db_error("Must set unit with ahd_sunit first!\n");
1536 		return;
1537 	}
1538 	ahd_dump_card_state(ahd_ddb_softc);
1539 }
1540 
1541 #endif
1542 
1543 DECLARE_MODULE(ahd, ahd_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1544 MODULE_DEPEND(ahd, cam, 1, 1, 1);
1545 MODULE_VERSION(ahd, 1);
1546