xref: /freebsd-14.2/sys/dev/aic7xxx/aic7xxx_osm.c (revision 7c3092e3)
1 /*-
2  * Bus independent FreeBSD shim for the aic7xxx based Adaptec SCSI controllers
3  *
4  * Copyright (c) 1994-2001 Justin T. Gibbs.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification.
13  * 2. The name of the author may not be used to endorse or promote products
14  *    derived from this software without specific prior written permission.
15  *
16  * Alternatively, this software may be distributed under the terms of the
17  * GNU Public License ("GPL").
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  * $Id: //depot/aic7xxx/freebsd/dev/aic7xxx/aic7xxx_osm.c#20 $
32  */
33 
34 #include <sys/cdefs.h>
35 #include <dev/aic7xxx/aic7xxx_osm.h>
36 #include <dev/aic7xxx/aic7xxx_inline.h>
37 
38 #include <sys/kthread.h>
39 
40 #ifndef AHC_TMODE_ENABLE
41 #define AHC_TMODE_ENABLE 0
42 #endif
43 
44 #include <dev/aic7xxx/aic_osm_lib.c>
45 
46 #define ccb_scb_ptr spriv_ptr0
47 
48 #if 0
49 static void	ahc_dump_targcmd(struct target_cmd *cmd);
50 #endif
51 static int	ahc_modevent(module_t mod, int type, void *data);
52 static void	ahc_action(struct cam_sim *sim, union ccb *ccb);
53 static void	ahc_get_tran_settings(struct ahc_softc *ahc,
54 				      int our_id, char channel,
55 				      struct ccb_trans_settings *cts);
56 static void	ahc_async(void *callback_arg, uint32_t code,
57 			  struct cam_path *path, void *arg);
58 static void	ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs,
59 				int nsegments, int error);
60 static void	ahc_poll(struct cam_sim *sim);
61 static void	ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim,
62 			       struct ccb_scsiio *csio, struct scb *scb);
63 static void	ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim,
64 			      union ccb *ccb);
65 static int	ahc_create_path(struct ahc_softc *ahc,
66 				char channel, u_int target, u_int lun,
67 				struct cam_path **path);
68 
69 static int
ahc_create_path(struct ahc_softc * ahc,char channel,u_int target,u_int lun,struct cam_path ** path)70 ahc_create_path(struct ahc_softc *ahc, char channel, u_int target,
71 	        u_int lun, struct cam_path **path)
72 {
73 	path_id_t path_id;
74 
75 	if (channel == 'B')
76 		path_id = cam_sim_path(ahc->platform_data->sim_b);
77 	else
78 		path_id = cam_sim_path(ahc->platform_data->sim);
79 
80 	return (xpt_create_path(path, /*periph*/NULL,
81 				path_id, target, lun));
82 }
83 
84 int
ahc_map_int(struct ahc_softc * ahc)85 ahc_map_int(struct ahc_softc *ahc)
86 {
87 	int error;
88 	int zero;
89 	int shareable;
90 
91 	zero = 0;
92 	shareable = (ahc->flags & AHC_EDGE_INTERRUPT) ? 0: RF_SHAREABLE;
93 	ahc->platform_data->irq =
94 	    bus_alloc_resource_any(ahc->dev_softc, SYS_RES_IRQ, &zero,
95 				   RF_ACTIVE | shareable);
96 	if (ahc->platform_data->irq == NULL) {
97 		device_printf(ahc->dev_softc,
98 			      "bus_alloc_resource() failed to allocate IRQ\n");
99 		return (ENOMEM);
100 	}
101 	ahc->platform_data->irq_res_type = SYS_RES_IRQ;
102 
103 	/* Hook up our interrupt handler */
104 	error = bus_setup_intr(ahc->dev_softc, ahc->platform_data->irq,
105 			       INTR_TYPE_CAM|INTR_MPSAFE, NULL,
106 			       ahc_platform_intr, ahc, &ahc->platform_data->ih);
107 
108 	if (error != 0)
109 		device_printf(ahc->dev_softc, "bus_setup_intr() failed: %d\n",
110 			      error);
111 	return (error);
112 }
113 
114 int
aic7770_map_registers(struct ahc_softc * ahc,u_int unused_ioport_arg)115 aic7770_map_registers(struct ahc_softc *ahc, u_int unused_ioport_arg)
116 {
117 	struct	resource *regs;
118 	int	rid;
119 
120 	rid = 0;
121 	regs = bus_alloc_resource_any(ahc->dev_softc, SYS_RES_IOPORT, &rid,
122 				      RF_ACTIVE);
123 	if (regs == NULL) {
124 		device_printf(ahc->dev_softc, "Unable to map I/O space?!\n");
125 		return ENOMEM;
126 	}
127 	ahc->platform_data->regs_res_type = SYS_RES_IOPORT;
128 	ahc->platform_data->regs_res_id = rid;
129 	ahc->platform_data->regs = regs;
130 	ahc->tag = rman_get_bustag(regs);
131 	ahc->bsh = rman_get_bushandle(regs);
132 	return (0);
133 }
134 
135 /*
136  * Attach all the sub-devices we can find
137  */
138 int
ahc_attach(struct ahc_softc * ahc)139 ahc_attach(struct ahc_softc *ahc)
140 {
141 	char   ahc_info[256];
142 	struct ccb_setasync csa;
143 	struct cam_devq *devq;
144 	int bus_id;
145 	int bus_id2;
146 	struct cam_sim *sim;
147 	struct cam_sim *sim2;
148 	struct cam_path *path;
149 	struct cam_path *path2;
150 	int count;
151 
152 	count = 0;
153 	sim = NULL;
154 	sim2 = NULL;
155 	path = NULL;
156 	path2 = NULL;
157 
158 	/*
159 	 * Create a thread to perform all recovery.
160 	 */
161 	if (ahc_spawn_recovery_thread(ahc) != 0)
162 		goto fail;
163 
164 	ahc_controller_info(ahc, ahc_info);
165 	printf("%s\n", ahc_info);
166 	ahc_lock(ahc);
167 
168 	/*
169 	 * Attach secondary channel first if the user has
170 	 * declared it the primary channel.
171 	 */
172 	if ((ahc->features & AHC_TWIN) != 0
173 	 && (ahc->flags & AHC_PRIMARY_CHANNEL) != 0) {
174 		bus_id = 1;
175 		bus_id2 = 0;
176 	} else {
177 		bus_id = 0;
178 		bus_id2 = 1;
179 	}
180 
181 	/*
182 	 * Create the device queue for our SIM(s).
183 	 */
184 	devq = cam_simq_alloc(AHC_MAX_QUEUE);
185 	if (devq == NULL)
186 		goto fail;
187 
188 	/*
189 	 * Construct our first channel SIM entry
190 	 */
191 	sim = cam_sim_alloc(ahc_action, ahc_poll, "ahc", ahc,
192 			    device_get_unit(ahc->dev_softc),
193 			    &ahc->platform_data->mtx, 1, AHC_MAX_QUEUE, devq);
194 	if (sim == NULL) {
195 		cam_simq_free(devq);
196 		goto fail;
197 	}
198 
199 	if (xpt_bus_register(sim, ahc->dev_softc, bus_id) != CAM_SUCCESS) {
200 		cam_sim_free(sim, /*free_devq*/TRUE);
201 		sim = NULL;
202 		goto fail;
203 	}
204 
205 	if (xpt_create_path(&path, /*periph*/NULL,
206 			    cam_sim_path(sim), CAM_TARGET_WILDCARD,
207 			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
208 		xpt_bus_deregister(cam_sim_path(sim));
209 		cam_sim_free(sim, /*free_devq*/TRUE);
210 		sim = NULL;
211 		goto fail;
212 	}
213 
214 	memset(&csa, 0, sizeof(csa));
215 	xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
216 	csa.ccb_h.func_code = XPT_SASYNC_CB;
217 	csa.event_enable = AC_LOST_DEVICE;
218 	csa.callback = ahc_async;
219 	csa.callback_arg = sim;
220 	xpt_action((union ccb *)&csa);
221 	count++;
222 
223 	if (ahc->features & AHC_TWIN) {
224 		sim2 = cam_sim_alloc(ahc_action, ahc_poll, "ahc",
225 				    ahc, device_get_unit(ahc->dev_softc),
226 				    &ahc->platform_data->mtx, 1,
227 				    AHC_MAX_QUEUE, devq);
228 
229 		if (sim2 == NULL) {
230 			printf("ahc_attach: Unable to attach second "
231 			       "bus due to resource shortage");
232 			goto fail;
233 		}
234 
235 		if (xpt_bus_register(sim2, ahc->dev_softc, bus_id2) !=
236 		    CAM_SUCCESS) {
237 			printf("ahc_attach: Unable to attach second "
238 			       "bus due to resource shortage");
239 			/*
240 			 * We do not want to destroy the device queue
241 			 * because the first bus is using it.
242 			 */
243 			cam_sim_free(sim2, /*free_devq*/FALSE);
244 			goto fail;
245 		}
246 
247 		if (xpt_create_path(&path2, /*periph*/NULL,
248 				    cam_sim_path(sim2),
249 				    CAM_TARGET_WILDCARD,
250 				    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
251 			xpt_bus_deregister(cam_sim_path(sim2));
252 			cam_sim_free(sim2, /*free_devq*/FALSE);
253 			sim2 = NULL;
254 			goto fail;
255 		}
256 		xpt_setup_ccb(&csa.ccb_h, path2, /*priority*/5);
257 		csa.ccb_h.func_code = XPT_SASYNC_CB;
258 		csa.event_enable = AC_LOST_DEVICE;
259 		csa.callback = ahc_async;
260 		csa.callback_arg = sim2;
261 		xpt_action((union ccb *)&csa);
262 		count++;
263 	}
264 
265 fail:
266 	if ((ahc->features & AHC_TWIN) != 0
267 	 && (ahc->flags & AHC_PRIMARY_CHANNEL) != 0) {
268 		ahc->platform_data->sim_b = sim;
269 		ahc->platform_data->path_b = path;
270 		ahc->platform_data->sim = sim2;
271 		ahc->platform_data->path = path2;
272 	} else {
273 		ahc->platform_data->sim = sim;
274 		ahc->platform_data->path = path;
275 		ahc->platform_data->sim_b = sim2;
276 		ahc->platform_data->path_b = path2;
277 	}
278 	ahc_unlock(ahc);
279 
280 	if (count != 0) {
281 		/* We have to wait until after any system dumps... */
282 		ahc->platform_data->eh =
283 		    EVENTHANDLER_REGISTER(shutdown_final, ahc_shutdown,
284 					  ahc, SHUTDOWN_PRI_DEFAULT);
285 		ahc_intr_enable(ahc, TRUE);
286 	}
287 
288 	return (count);
289 }
290 
291 /*
292  * Catch an interrupt from the adapter
293  */
294 void
ahc_platform_intr(void * arg)295 ahc_platform_intr(void *arg)
296 {
297 	struct	ahc_softc *ahc;
298 
299 	ahc = (struct ahc_softc *)arg;
300 	ahc_lock(ahc);
301 	ahc_intr(ahc);
302 	ahc_unlock(ahc);
303 }
304 
305 static void
ahc_sync_ccb(struct ahc_softc * ahc,struct scb * scb,union ccb * ccb,bool post)306 ahc_sync_ccb(struct ahc_softc *ahc, struct scb *scb, union ccb *ccb, bool post)
307 {
308 	bus_dmasync_op_t op;
309 	uint32_t rdmask;
310 
311 	if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO)
312 		rdmask = CAM_DIR_OUT;
313 	else
314 		rdmask = CAM_DIR_IN;
315 
316 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) == rdmask)
317 		op = post ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_PREREAD;
318 	else
319 		op = post ? BUS_DMASYNC_POSTWRITE : BUS_DMASYNC_PREWRITE;
320 
321 	bus_dmamap_sync(ahc->buffer_dmat, scb->dmamap, op);
322 }
323 
324 /*
325  * We have an scb which has been processed by the
326  * adaptor, now we look to see how the operation
327  * went.
328  */
329 void
ahc_done(struct ahc_softc * ahc,struct scb * scb)330 ahc_done(struct ahc_softc *ahc, struct scb *scb)
331 {
332 	union ccb *ccb;
333 
334 	CAM_DEBUG(scb->io_ctx->ccb_h.path, CAM_DEBUG_TRACE,
335 		  ("ahc_done - scb %d\n", scb->hscb->tag));
336 
337 	ccb = scb->io_ctx;
338 	LIST_REMOVE(scb, pending_links);
339 	if ((scb->flags & SCB_TIMEDOUT) != 0)
340 		LIST_REMOVE(scb, timedout_links);
341 	if ((scb->flags & SCB_UNTAGGEDQ) != 0) {
342 		struct scb_tailq *untagged_q;
343 		int target_offset;
344 
345 		target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
346 		untagged_q = &ahc->untagged_queues[target_offset];
347 		TAILQ_REMOVE(untagged_q, scb, links.tqe);
348 		scb->flags &= ~SCB_UNTAGGEDQ;
349 		ahc_run_untagged_queue(ahc, untagged_q);
350 	}
351 
352 	callout_stop(&scb->io_timer);
353 
354 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
355 		ahc_sync_ccb(ahc, scb, ccb, true);
356 		bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
357 	}
358 
359 	if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
360 		struct cam_path *ccb_path;
361 
362 		/*
363 		 * If we have finally disconnected, clean up our
364 		 * pending device state.
365 		 * XXX - There may be error states that cause where
366 		 *       we will remain connected.
367 		 */
368 		ccb_path = ccb->ccb_h.path;
369 		if (ahc->pending_device != NULL
370 		 && xpt_path_comp(ahc->pending_device->path, ccb_path) == 0) {
371 			if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
372 				ahc->pending_device = NULL;
373 			} else {
374 				if (bootverbose) {
375 					xpt_print_path(ccb->ccb_h.path);
376 					printf("Still connected\n");
377 				}
378 				aic_freeze_ccb(ccb);
379 			}
380 		}
381 
382 		if (aic_get_transaction_status(scb) == CAM_REQ_INPROG)
383 			ccb->ccb_h.status |= CAM_REQ_CMP;
384 		ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
385 		ahc_free_scb(ahc, scb);
386 		xpt_done(ccb);
387 		return;
388 	}
389 
390 	/*
391 	 * If the recovery SCB completes, we have to be
392 	 * out of our timeout.
393 	 */
394 	if ((scb->flags & SCB_RECOVERY_SCB) != 0) {
395 		struct	scb *list_scb;
396 
397 		ahc->scb_data->recovery_scbs--;
398 
399 		if (aic_get_transaction_status(scb) == CAM_BDR_SENT
400 		 || aic_get_transaction_status(scb) == CAM_REQ_ABORTED)
401 			aic_set_transaction_status(scb, CAM_CMD_TIMEOUT);
402 
403 		if (ahc->scb_data->recovery_scbs == 0) {
404 			/*
405 			 * All recovery actions have completed successfully,
406 			 * so reinstate the timeouts for all other pending
407 			 * commands.
408 			 */
409 			LIST_FOREACH(list_scb, &ahc->pending_scbs,
410 				     pending_links) {
411 				aic_scb_timer_reset(list_scb,
412 						    aic_get_timeout(scb));
413 			}
414 
415 			ahc_print_path(ahc, scb);
416 			printf("no longer in timeout, status = %x\n",
417 			       ccb->ccb_h.status);
418 		}
419 	}
420 
421 	/* Don't clobber any existing error state */
422 	if (aic_get_transaction_status(scb) == CAM_REQ_INPROG) {
423 		ccb->ccb_h.status |= CAM_REQ_CMP;
424 	} else if ((scb->flags & SCB_SENSE) != 0) {
425 		/*
426 		 * We performed autosense retrieval.
427 		 *
428 		 * Zero any sense not transferred by the
429 		 * device.  The SCSI spec mandates that any
430 		 * untransfered data should be assumed to be
431 		 * zero.  Complete the 'bounce' of sense information
432 		 * through buffers accessible via bus-space by
433 		 * copying it into the clients csio.
434 		 */
435 		memset(&ccb->csio.sense_data, 0, sizeof(ccb->csio.sense_data));
436 		memcpy(&ccb->csio.sense_data,
437 		       ahc_get_sense_buf(ahc, scb),
438 		       (aic_le32toh(scb->sg_list->len) & AHC_SG_LEN_MASK)
439 		       - ccb->csio.sense_resid);
440 		scb->io_ctx->ccb_h.status |= CAM_AUTOSNS_VALID;
441 	}
442 	ccb->ccb_h.status &= ~CAM_SIM_QUEUED;
443 	ahc_free_scb(ahc, scb);
444 	xpt_done(ccb);
445 }
446 
447 static void
ahc_action(struct cam_sim * sim,union ccb * ccb)448 ahc_action(struct cam_sim *sim, union ccb *ccb)
449 {
450 	struct	ahc_softc *ahc;
451 	struct	ahc_tmode_lstate *lstate;
452 	u_int	target_id;
453 	u_int	our_id;
454 
455 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahc_action\n"));
456 
457 	ahc = (struct ahc_softc *)cam_sim_softc(sim);
458 
459 	target_id = ccb->ccb_h.target_id;
460 	our_id = SIM_SCSI_ID(ahc, sim);
461 
462 	switch (ccb->ccb_h.func_code) {
463 	/* Common cases first */
464 	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
465 	case XPT_CONT_TARGET_IO:/* Continue Host Target I/O Connection*/
466 	{
467 		struct	   ahc_tmode_tstate *tstate;
468 		cam_status status;
469 
470 		status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
471 					     &lstate, TRUE);
472 
473 		if (status != CAM_REQ_CMP) {
474 			if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
475 				/* Response from the black hole device */
476 				tstate = NULL;
477 				lstate = ahc->black_hole;
478 			} else {
479 				ccb->ccb_h.status = status;
480 				xpt_done(ccb);
481 				break;
482 			}
483 		}
484 		if (ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO) {
485 			SLIST_INSERT_HEAD(&lstate->accept_tios, &ccb->ccb_h,
486 					  sim_links.sle);
487 			ccb->ccb_h.status = CAM_REQ_INPROG;
488 			if ((ahc->flags & AHC_TQINFIFO_BLOCKED) != 0)
489 				ahc_run_tqinfifo(ahc, /*paused*/FALSE);
490 			break;
491 		}
492 
493 		/*
494 		 * The target_id represents the target we attempt to
495 		 * select.  In target mode, this is the initiator of
496 		 * the original command.
497 		 */
498 		our_id = target_id;
499 		target_id = ccb->csio.init_id;
500 		/* FALLTHROUGH */
501 	}
502 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
503 	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
504 	{
505 		struct	scb *scb;
506 		struct	hardware_scb *hscb;
507 
508 		if ((ahc->flags & AHC_INITIATORROLE) == 0
509 		 && (ccb->ccb_h.func_code == XPT_SCSI_IO
510 		  || ccb->ccb_h.func_code == XPT_RESET_DEV)) {
511 			ccb->ccb_h.status = CAM_PROVIDE_FAIL;
512 			xpt_done(ccb);
513 			return;
514 		}
515 
516 		/*
517 		 * get an scb to use.
518 		 */
519 		if ((scb = ahc_get_scb(ahc)) == NULL) {
520 			xpt_freeze_simq(sim, /*count*/1);
521 			ahc->flags |= AHC_RESOURCE_SHORTAGE;
522 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
523 			xpt_done(ccb);
524 			return;
525 		}
526 
527 		hscb = scb->hscb;
528 
529 		CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
530 			  ("start scb(%p)\n", scb));
531 		scb->io_ctx = ccb;
532 		/*
533 		 * So we can find the SCB when an abort is requested
534 		 */
535 		ccb->ccb_h.ccb_scb_ptr = scb;
536 
537 		/*
538 		 * Put all the arguments for the xfer in the scb
539 		 */
540 		hscb->control = 0;
541 		hscb->scsiid = BUILD_SCSIID(ahc, sim, target_id, our_id);
542 		hscb->lun = ccb->ccb_h.target_lun;
543 		if (ccb->ccb_h.func_code == XPT_RESET_DEV) {
544 			hscb->cdb_len = 0;
545 			scb->flags |= SCB_DEVICE_RESET;
546 			hscb->control |= MK_MESSAGE;
547 			ahc_execute_scb(scb, NULL, 0, 0);
548 		} else {
549 			if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
550 				struct target_data *tdata;
551 
552 				tdata = &hscb->shared_data.tdata;
553 				if (ahc->pending_device == lstate)
554 					scb->flags |= SCB_TARGET_IMMEDIATE;
555 				hscb->control |= TARGET_SCB;
556 				scb->flags |= SCB_TARGET_SCB;
557 				tdata->target_phases = 0;
558 				if ((ccb->ccb_h.flags & CAM_SEND_STATUS) != 0) {
559 					tdata->target_phases |= SPHASE_PENDING;
560 					tdata->scsi_status =
561 					    ccb->csio.scsi_status;
562 				}
563 	 			if (ccb->ccb_h.flags & CAM_DIS_DISCONNECT)
564 					tdata->target_phases |= NO_DISCONNECT;
565 
566 				tdata->initiator_tag = ccb->csio.tag_id;
567 			}
568 			if (ccb->ccb_h.flags & CAM_TAG_ACTION_VALID)
569 				hscb->control |= ccb->csio.tag_action;
570 
571 			ahc_setup_data(ahc, sim, &ccb->csio, scb);
572 		}
573 		break;
574 	}
575 	case XPT_NOTIFY_ACKNOWLEDGE:
576 	case XPT_IMMEDIATE_NOTIFY:
577 	{
578 		struct	   ahc_tmode_tstate *tstate;
579 		struct	   ahc_tmode_lstate *lstate;
580 		cam_status status;
581 
582 		status = ahc_find_tmode_devs(ahc, sim, ccb, &tstate,
583 					     &lstate, TRUE);
584 
585 		if (status != CAM_REQ_CMP) {
586 			ccb->ccb_h.status = status;
587 			xpt_done(ccb);
588 			break;
589 		}
590 		SLIST_INSERT_HEAD(&lstate->immed_notifies, &ccb->ccb_h,
591 				  sim_links.sle);
592 		ccb->ccb_h.status = CAM_REQ_INPROG;
593 		ahc_send_lstate_events(ahc, lstate);
594 		break;
595 	}
596 	case XPT_EN_LUN:		/* Enable LUN as a target */
597 		ahc_handle_en_lun(ahc, sim, ccb);
598 		xpt_done(ccb);
599 		break;
600 	case XPT_ABORT:			/* Abort the specified CCB */
601 	{
602 		ahc_abort_ccb(ahc, sim, ccb);
603 		break;
604 	}
605 	case XPT_SET_TRAN_SETTINGS:
606 	{
607 		struct	ahc_devinfo devinfo;
608 		struct	ccb_trans_settings *cts;
609 		struct	ccb_trans_settings_scsi *scsi;
610 		struct	ccb_trans_settings_spi *spi;
611 		struct	ahc_initiator_tinfo *tinfo;
612 		struct	ahc_tmode_tstate *tstate;
613 		uint16_t *discenable;
614 		uint16_t *tagenable;
615 		u_int	update_type;
616 
617 		cts = &ccb->cts;
618 		scsi = &cts->proto_specific.scsi;
619 		spi = &cts->xport_specific.spi;
620 		ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
621 				    cts->ccb_h.target_id,
622 				    cts->ccb_h.target_lun,
623 				    SIM_CHANNEL(ahc, sim),
624 				    ROLE_UNKNOWN);
625 		tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
626 					    devinfo.our_scsiid,
627 					    devinfo.target, &tstate);
628 		update_type = 0;
629 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
630 			update_type |= AHC_TRANS_GOAL;
631 			discenable = &tstate->discenable;
632 			tagenable = &tstate->tagenable;
633 			tinfo->curr.protocol_version =
634 			    cts->protocol_version;
635 			tinfo->curr.transport_version =
636 			    cts->transport_version;
637 			tinfo->goal.protocol_version =
638 			    cts->protocol_version;
639 			tinfo->goal.transport_version =
640 			    cts->transport_version;
641 		} else if (cts->type == CTS_TYPE_USER_SETTINGS) {
642 			update_type |= AHC_TRANS_USER;
643 			discenable = &ahc->user_discenable;
644 			tagenable = &ahc->user_tagenable;
645 			tinfo->user.protocol_version =
646 			    cts->protocol_version;
647 			tinfo->user.transport_version =
648 			    cts->transport_version;
649 		} else {
650 			ccb->ccb_h.status = CAM_REQ_INVALID;
651 			xpt_done(ccb);
652 			break;
653 		}
654 
655 		if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
656 			if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
657 				*discenable |= devinfo.target_mask;
658 			else
659 				*discenable &= ~devinfo.target_mask;
660 		}
661 
662 		if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
663 			if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
664 				*tagenable |= devinfo.target_mask;
665 			else
666 				*tagenable &= ~devinfo.target_mask;
667 		}
668 
669 		if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
670 			ahc_validate_width(ahc, /*tinfo limit*/NULL,
671 					   &spi->bus_width, ROLE_UNKNOWN);
672 			ahc_set_width(ahc, &devinfo, spi->bus_width,
673 				      update_type, /*paused*/FALSE);
674 		}
675 
676 		if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0) {
677 			if (update_type == AHC_TRANS_USER)
678 				spi->ppr_options = tinfo->user.ppr_options;
679 			else
680 				spi->ppr_options = tinfo->goal.ppr_options;
681 		}
682 
683 		if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0) {
684 			if (update_type == AHC_TRANS_USER)
685 				spi->sync_offset = tinfo->user.offset;
686 			else
687 				spi->sync_offset = tinfo->goal.offset;
688 		}
689 
690 		if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
691 			if (update_type == AHC_TRANS_USER)
692 				spi->sync_period = tinfo->user.period;
693 			else
694 				spi->sync_period = tinfo->goal.period;
695 		}
696 
697 		if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
698 		 || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
699 			struct ahc_syncrate *syncrate;
700 			u_int maxsync;
701 
702 			if ((ahc->features & AHC_ULTRA2) != 0)
703 				maxsync = AHC_SYNCRATE_DT;
704 			else if ((ahc->features & AHC_ULTRA) != 0)
705 				maxsync = AHC_SYNCRATE_ULTRA;
706 			else
707 				maxsync = AHC_SYNCRATE_FAST;
708 
709 			if (spi->bus_width != MSG_EXT_WDTR_BUS_16_BIT)
710 				spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
711 
712 			syncrate = ahc_find_syncrate(ahc, &spi->sync_period,
713 						     &spi->ppr_options,
714 						     maxsync);
715 			ahc_validate_offset(ahc, /*tinfo limit*/NULL,
716 					    syncrate, &spi->sync_offset,
717 					    spi->bus_width, ROLE_UNKNOWN);
718 
719 			/* We use a period of 0 to represent async */
720 			if (spi->sync_offset == 0) {
721 				spi->sync_period = 0;
722 				spi->ppr_options = 0;
723 			}
724 
725 			ahc_set_syncrate(ahc, &devinfo, syncrate,
726 					 spi->sync_period, spi->sync_offset,
727 					 spi->ppr_options, update_type,
728 					 /*paused*/FALSE);
729 		}
730 		ccb->ccb_h.status = CAM_REQ_CMP;
731 		xpt_done(ccb);
732 		break;
733 	}
734 	case XPT_GET_TRAN_SETTINGS:
735 	/* Get default/user set transfer settings for the target */
736 	{
737 		ahc_get_tran_settings(ahc, SIM_SCSI_ID(ahc, sim),
738 				      SIM_CHANNEL(ahc, sim), &ccb->cts);
739 		xpt_done(ccb);
740 		break;
741 	}
742 	case XPT_CALC_GEOMETRY:
743 	{
744 		int extended;
745 
746 		extended = SIM_IS_SCSIBUS_B(ahc, sim)
747 			 ? ahc->flags & AHC_EXTENDED_TRANS_B
748 			 : ahc->flags & AHC_EXTENDED_TRANS_A;
749 		aic_calc_geometry(&ccb->ccg, extended);
750 		xpt_done(ccb);
751 		break;
752 	}
753 	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
754 	{
755 		int  found;
756 
757 		found = ahc_reset_channel(ahc, SIM_CHANNEL(ahc, sim),
758 					  /*initiate reset*/TRUE);
759 		if (bootverbose) {
760 			xpt_print_path(SIM_PATH(ahc, sim));
761 			printf("SCSI bus reset delivered. "
762 			       "%d SCBs aborted.\n", found);
763 		}
764 		ccb->ccb_h.status = CAM_REQ_CMP;
765 		xpt_done(ccb);
766 		break;
767 	}
768 	case XPT_TERM_IO:		/* Terminate the I/O process */
769 		/* XXX Implement */
770 		ccb->ccb_h.status = CAM_REQ_INVALID;
771 		xpt_done(ccb);
772 		break;
773 	case XPT_PATH_INQ:		/* Path routing inquiry */
774 	{
775 		struct ccb_pathinq *cpi = &ccb->cpi;
776 
777 		cpi->version_num = 1; /* XXX??? */
778 		cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE;
779 		if ((ahc->features & AHC_WIDE) != 0)
780 			cpi->hba_inquiry |= PI_WIDE_16;
781 		if ((ahc->features & AHC_TARGETMODE) != 0) {
782 			cpi->target_sprt = PIT_PROCESSOR
783 					 | PIT_DISCONNECT
784 					 | PIT_TERM_IO;
785 		} else {
786 			cpi->target_sprt = 0;
787 		}
788 		cpi->hba_misc = 0;
789 		cpi->hba_eng_cnt = 0;
790 		cpi->max_target = (ahc->features & AHC_WIDE) ? 15 : 7;
791 		cpi->max_lun = AHC_NUM_LUNS - 1;
792 		if (SIM_IS_SCSIBUS_B(ahc, sim)) {
793 			cpi->initiator_id = ahc->our_id_b;
794 			if ((ahc->flags & AHC_RESET_BUS_B) == 0)
795 				cpi->hba_misc |= PIM_NOBUSRESET;
796 		} else {
797 			cpi->initiator_id = ahc->our_id;
798 			if ((ahc->flags & AHC_RESET_BUS_A) == 0)
799 				cpi->hba_misc |= PIM_NOBUSRESET;
800 		}
801 		cpi->bus_id = cam_sim_bus(sim);
802 		cpi->base_transfer_speed = 3300;
803 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
804 		strlcpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
805 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
806 		cpi->unit_number = cam_sim_unit(sim);
807 		cpi->protocol = PROTO_SCSI;
808 		cpi->protocol_version = SCSI_REV_2;
809 		cpi->transport = XPORT_SPI;
810 		cpi->transport_version = 2;
811 		cpi->xport_specific.spi.ppr_options = SID_SPI_CLOCK_ST;
812 		if ((ahc->features & AHC_DT) != 0) {
813 			cpi->transport_version = 3;
814 			cpi->xport_specific.spi.ppr_options =
815 			    SID_SPI_CLOCK_DT_ST;
816 		}
817 		cpi->ccb_h.status = CAM_REQ_CMP;
818 		xpt_done(ccb);
819 		break;
820 	}
821 	default:
822 		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
823 		xpt_done(ccb);
824 		break;
825 	}
826 }
827 
828 static void
ahc_get_tran_settings(struct ahc_softc * ahc,int our_id,char channel,struct ccb_trans_settings * cts)829 ahc_get_tran_settings(struct ahc_softc *ahc, int our_id, char channel,
830 		      struct ccb_trans_settings *cts)
831 {
832 	struct	ahc_devinfo devinfo;
833 	struct	ccb_trans_settings_scsi *scsi;
834 	struct	ccb_trans_settings_spi *spi;
835 	struct	ahc_initiator_tinfo *targ_info;
836 	struct	ahc_tmode_tstate *tstate;
837 	struct	ahc_transinfo *tinfo;
838 
839 	scsi = &cts->proto_specific.scsi;
840 	spi = &cts->xport_specific.spi;
841 	ahc_compile_devinfo(&devinfo, our_id,
842 			    cts->ccb_h.target_id,
843 			    cts->ccb_h.target_lun,
844 			    channel, ROLE_UNKNOWN);
845 	targ_info = ahc_fetch_transinfo(ahc, devinfo.channel,
846 					devinfo.our_scsiid,
847 					devinfo.target, &tstate);
848 
849 	if (cts->type == CTS_TYPE_CURRENT_SETTINGS)
850 		tinfo = &targ_info->curr;
851 	else
852 		tinfo = &targ_info->user;
853 
854 	scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
855 	spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
856 	if (cts->type == CTS_TYPE_USER_SETTINGS) {
857 		if ((ahc->user_discenable & devinfo.target_mask) != 0)
858 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
859 
860 		if ((ahc->user_tagenable & devinfo.target_mask) != 0)
861 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
862 	} else {
863 		if ((tstate->discenable & devinfo.target_mask) != 0)
864 			spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
865 
866 		if ((tstate->tagenable & devinfo.target_mask) != 0)
867 			scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
868 	}
869 	cts->protocol_version = tinfo->protocol_version;
870 	cts->transport_version = tinfo->transport_version;
871 
872 	spi->sync_period = tinfo->period;
873 	spi->sync_offset = tinfo->offset;
874 	spi->bus_width = tinfo->width;
875 	spi->ppr_options = tinfo->ppr_options;
876 
877 	cts->protocol = PROTO_SCSI;
878 	cts->transport = XPORT_SPI;
879 	spi->valid = CTS_SPI_VALID_SYNC_RATE
880 		   | CTS_SPI_VALID_SYNC_OFFSET
881 		   | CTS_SPI_VALID_BUS_WIDTH
882 		   | CTS_SPI_VALID_PPR_OPTIONS;
883 
884 	if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
885 		scsi->valid = CTS_SCSI_VALID_TQ;
886 		spi->valid |= CTS_SPI_VALID_DISC;
887 	} else {
888 		scsi->valid = 0;
889 	}
890 
891 	cts->ccb_h.status = CAM_REQ_CMP;
892 }
893 
894 static void
ahc_async(void * callback_arg,uint32_t code,struct cam_path * path,void * arg)895 ahc_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg)
896 {
897 	struct ahc_softc *ahc;
898 	struct cam_sim *sim;
899 
900 	sim = (struct cam_sim *)callback_arg;
901 	ahc = (struct ahc_softc *)cam_sim_softc(sim);
902 	switch (code) {
903 	case AC_LOST_DEVICE:
904 	{
905 		struct	ahc_devinfo devinfo;
906 
907 		ahc_compile_devinfo(&devinfo, SIM_SCSI_ID(ahc, sim),
908 				    xpt_path_target_id(path),
909 				    xpt_path_lun_id(path),
910 				    SIM_CHANNEL(ahc, sim),
911 				    ROLE_UNKNOWN);
912 
913 		/*
914 		 * Revert to async/narrow transfers
915 		 * for the next device.
916 		 */
917 		ahc_set_width(ahc, &devinfo, MSG_EXT_WDTR_BUS_8_BIT,
918 			      AHC_TRANS_GOAL|AHC_TRANS_CUR, /*paused*/FALSE);
919 		ahc_set_syncrate(ahc, &devinfo, /*syncrate*/NULL,
920 				 /*period*/0, /*offset*/0, /*ppr_options*/0,
921 				 AHC_TRANS_GOAL|AHC_TRANS_CUR,
922 				 /*paused*/FALSE);
923 		break;
924 	}
925 	default:
926 		break;
927 	}
928 }
929 
930 static void
ahc_execute_scb(void * arg,bus_dma_segment_t * dm_segs,int nsegments,int error)931 ahc_execute_scb(void *arg, bus_dma_segment_t *dm_segs, int nsegments,
932 		int error)
933 {
934 	struct	scb *scb;
935 	union	ccb *ccb;
936 	struct	ahc_softc *ahc;
937 	struct	ahc_initiator_tinfo *tinfo;
938 	struct	ahc_tmode_tstate *tstate;
939 	u_int	mask;
940 
941 	scb = (struct scb *)arg;
942 	ccb = scb->io_ctx;
943 	ahc = scb->ahc_softc;
944 
945 	if (error != 0) {
946 		if (error == EFBIG)
947 			aic_set_transaction_status(scb, CAM_REQ_TOO_BIG);
948 		else
949 			aic_set_transaction_status(scb, CAM_REQ_CMP_ERR);
950 		if (nsegments != 0)
951 			bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
952 		ahc_free_scb(ahc, scb);
953 		xpt_done(ccb);
954 		return;
955 	}
956 	if (nsegments != 0) {
957 		struct	  ahc_dma_seg *sg;
958 		bus_dma_segment_t *end_seg;
959 
960 		end_seg = dm_segs + nsegments;
961 
962 		/* Copy the segments into our SG list */
963 		sg = scb->sg_list;
964 		while (dm_segs < end_seg) {
965 			uint32_t len;
966 
967 			sg->addr = aic_htole32(dm_segs->ds_addr);
968 			len = dm_segs->ds_len
969 			    | ((dm_segs->ds_addr >> 8) & 0x7F000000);
970 			sg->len = aic_htole32(len);
971 			sg++;
972 			dm_segs++;
973 		}
974 
975 		/*
976 		 * Note where to find the SG entries in bus space.
977 		 * We also set the full residual flag which the
978 		 * sequencer will clear as soon as a data transfer
979 		 * occurs.
980 		 */
981 		scb->hscb->sgptr = aic_htole32(scb->sg_list_phys|SG_FULL_RESID);
982 
983 		ahc_sync_ccb(ahc, scb, ccb, false);
984 
985 		if (ccb->ccb_h.func_code == XPT_CONT_TARGET_IO) {
986 			struct target_data *tdata;
987 
988 			tdata = &scb->hscb->shared_data.tdata;
989 			tdata->target_phases |= DPHASE_PENDING;
990 			/*
991 			 * CAM data direction is relative to the initiator.
992 			 */
993 			if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
994 				tdata->data_phase = P_DATAOUT;
995 			else
996 				tdata->data_phase = P_DATAIN;
997 
998 			/*
999 			 * If the transfer is of an odd length and in the
1000 			 * "in" direction (scsi->HostBus), then it may
1001 			 * trigger a bug in the 'WideODD' feature of
1002 			 * non-Ultra2 chips.  Force the total data-length
1003 			 * to be even by adding an extra, 1 byte, SG,
1004 			 * element.  We do this even if we are not currently
1005 			 * negotiated wide as negotiation could occur before
1006 			 * this command is executed.
1007 			 */
1008 			if ((ahc->bugs & AHC_TMODE_WIDEODD_BUG) != 0
1009 			 && (ccb->csio.dxfer_len & 0x1) != 0
1010 			 && (ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
1011 				nsegments++;
1012 				if (nsegments > AHC_NSEG) {
1013 					aic_set_transaction_status(scb,
1014 					    CAM_REQ_TOO_BIG);
1015 					bus_dmamap_unload(ahc->buffer_dmat,
1016 							  scb->dmamap);
1017 					ahc_free_scb(ahc, scb);
1018 					xpt_done(ccb);
1019 					return;
1020 				}
1021 				sg->addr = aic_htole32(ahc->dma_bug_buf);
1022 				sg->len = aic_htole32(1);
1023 				sg++;
1024 			}
1025 		}
1026 		sg--;
1027 		sg->len |= aic_htole32(AHC_DMA_LAST_SEG);
1028 
1029 		/* Copy the first SG into the "current" data pointer area */
1030 		scb->hscb->dataptr = scb->sg_list->addr;
1031 		scb->hscb->datacnt = scb->sg_list->len;
1032 	} else {
1033 		scb->hscb->sgptr = aic_htole32(SG_LIST_NULL);
1034 		scb->hscb->dataptr = 0;
1035 		scb->hscb->datacnt = 0;
1036 	}
1037 
1038 	scb->sg_count = nsegments;
1039 
1040 	/*
1041 	 * Last time we need to check if this SCB needs to
1042 	 * be aborted.
1043 	 */
1044 	if (aic_get_transaction_status(scb) != CAM_REQ_INPROG) {
1045 		if (nsegments != 0)
1046 			bus_dmamap_unload(ahc->buffer_dmat, scb->dmamap);
1047 		ahc_free_scb(ahc, scb);
1048 		xpt_done(ccb);
1049 		return;
1050 	}
1051 
1052 	tinfo = ahc_fetch_transinfo(ahc, SCSIID_CHANNEL(ahc, scb->hscb->scsiid),
1053 				    SCSIID_OUR_ID(scb->hscb->scsiid),
1054 				    SCSIID_TARGET(ahc, scb->hscb->scsiid),
1055 				    &tstate);
1056 
1057 	mask = SCB_GET_TARGET_MASK(ahc, scb);
1058 	scb->hscb->scsirate = tinfo->scsirate;
1059 	scb->hscb->scsioffset = tinfo->curr.offset;
1060 	if ((tstate->ultraenb & mask) != 0)
1061 		scb->hscb->control |= ULTRAENB;
1062 
1063 	if ((tstate->discenable & mask) != 0
1064 	 && (ccb->ccb_h.flags & CAM_DIS_DISCONNECT) == 0)
1065 		scb->hscb->control |= DISCENB;
1066 
1067 	if ((ccb->ccb_h.flags & CAM_NEGOTIATE) != 0
1068 	 && (tinfo->goal.width != 0
1069 	  || tinfo->goal.offset != 0
1070 	  || tinfo->goal.ppr_options != 0)) {
1071 		scb->flags |= SCB_NEGOTIATE;
1072 		scb->hscb->control |= MK_MESSAGE;
1073 	} else if ((tstate->auto_negotiate & mask) != 0) {
1074 		scb->flags |= SCB_AUTO_NEGOTIATE;
1075 		scb->hscb->control |= MK_MESSAGE;
1076 	}
1077 
1078 	LIST_INSERT_HEAD(&ahc->pending_scbs, scb, pending_links);
1079 
1080 	ccb->ccb_h.status |= CAM_SIM_QUEUED;
1081 
1082 	/*
1083 	 * We only allow one untagged transaction
1084 	 * per target in the initiator role unless
1085 	 * we are storing a full busy target *lun*
1086 	 * table in SCB space.
1087 	 */
1088 	if ((scb->hscb->control & (TARGET_SCB|TAG_ENB)) == 0
1089 	 && (ahc->flags & AHC_SCB_BTT) == 0) {
1090 		struct scb_tailq *untagged_q;
1091 		int target_offset;
1092 
1093 		target_offset = SCB_GET_TARGET_OFFSET(ahc, scb);
1094 		untagged_q = &(ahc->untagged_queues[target_offset]);
1095 		TAILQ_INSERT_TAIL(untagged_q, scb, links.tqe);
1096 		scb->flags |= SCB_UNTAGGEDQ;
1097 		if (TAILQ_FIRST(untagged_q) != scb) {
1098 			return;
1099 		}
1100 	}
1101 	scb->flags |= SCB_ACTIVE;
1102 
1103 	/*
1104 	 * Timers are disabled while recovery is in progress.
1105 	 */
1106 	aic_scb_timer_start(scb);
1107 
1108 	if ((scb->flags & SCB_TARGET_IMMEDIATE) != 0) {
1109 		/* Define a mapping from our tag to the SCB. */
1110 		ahc->scb_data->scbindex[scb->hscb->tag] = scb;
1111 		ahc_pause(ahc);
1112 		if ((ahc->flags & AHC_PAGESCBS) == 0)
1113 			ahc_outb(ahc, SCBPTR, scb->hscb->tag);
1114 		ahc_outb(ahc, TARG_IMMEDIATE_SCB, scb->hscb->tag);
1115 		ahc_unpause(ahc);
1116 	} else {
1117 		ahc_queue_scb(ahc, scb);
1118 	}
1119 }
1120 
1121 static void
ahc_poll(struct cam_sim * sim)1122 ahc_poll(struct cam_sim *sim)
1123 {
1124 	struct ahc_softc *ahc;
1125 
1126 	ahc = (struct ahc_softc *)cam_sim_softc(sim);
1127 	ahc_intr(ahc);
1128 }
1129 
1130 static void
ahc_setup_data(struct ahc_softc * ahc,struct cam_sim * sim,struct ccb_scsiio * csio,struct scb * scb)1131 ahc_setup_data(struct ahc_softc *ahc, struct cam_sim *sim,
1132 	       struct ccb_scsiio *csio, struct scb *scb)
1133 {
1134 	struct hardware_scb *hscb;
1135 	struct ccb_hdr *ccb_h;
1136 	int error;
1137 
1138 	hscb = scb->hscb;
1139 	ccb_h = &csio->ccb_h;
1140 
1141 	csio->resid = 0;
1142 	csio->sense_resid = 0;
1143 	if (ccb_h->func_code == XPT_SCSI_IO) {
1144 		hscb->cdb_len = csio->cdb_len;
1145 		if ((ccb_h->flags & CAM_CDB_POINTER) != 0) {
1146 			if (hscb->cdb_len > sizeof(hscb->cdb32)
1147 			 || (ccb_h->flags & CAM_CDB_PHYS) != 0) {
1148 				aic_set_transaction_status(scb,
1149 							   CAM_REQ_INVALID);
1150 				ahc_free_scb(ahc, scb);
1151 				xpt_done((union ccb *)csio);
1152 				return;
1153 			}
1154 			if (hscb->cdb_len > 12) {
1155 				memcpy(hscb->cdb32,
1156 				       csio->cdb_io.cdb_ptr,
1157 				       hscb->cdb_len);
1158 				scb->flags |= SCB_CDB32_PTR;
1159 			} else {
1160 				memcpy(hscb->shared_data.cdb,
1161 				       csio->cdb_io.cdb_ptr,
1162 				       hscb->cdb_len);
1163 			}
1164 		} else {
1165 			if (hscb->cdb_len > 12) {
1166 				memcpy(hscb->cdb32, csio->cdb_io.cdb_bytes,
1167 				       hscb->cdb_len);
1168 				scb->flags |= SCB_CDB32_PTR;
1169 			} else {
1170 				memcpy(hscb->shared_data.cdb,
1171 				       csio->cdb_io.cdb_bytes,
1172 				       hscb->cdb_len);
1173 			}
1174 		}
1175 	}
1176 
1177 	error = bus_dmamap_load_ccb(ahc->buffer_dmat,
1178 				    scb->dmamap,
1179 				    (union ccb *)csio,
1180 				    ahc_execute_scb,
1181 				    scb,
1182 				    0);
1183 	if (error == EINPROGRESS) {
1184 		/*
1185 		 * So as to maintain ordering,
1186 		 * freeze the controller queue
1187 		 * until our mapping is
1188 		 * returned.
1189 		 */
1190 		xpt_freeze_simq(sim, /*count*/1);
1191 		scb->io_ctx->ccb_h.status |= CAM_RELEASE_SIMQ;
1192 	}
1193 }
1194 
1195 static void
ahc_abort_ccb(struct ahc_softc * ahc,struct cam_sim * sim,union ccb * ccb)1196 ahc_abort_ccb(struct ahc_softc *ahc, struct cam_sim *sim, union ccb *ccb)
1197 {
1198 	union ccb *abort_ccb;
1199 
1200 	abort_ccb = ccb->cab.abort_ccb;
1201 	switch (abort_ccb->ccb_h.func_code) {
1202 	case XPT_ACCEPT_TARGET_IO:
1203 	case XPT_IMMEDIATE_NOTIFY:
1204 	case XPT_CONT_TARGET_IO:
1205 	{
1206 		struct ahc_tmode_tstate *tstate;
1207 		struct ahc_tmode_lstate *lstate;
1208 		struct ccb_hdr_slist *list;
1209 		cam_status status;
1210 
1211 		status = ahc_find_tmode_devs(ahc, sim, abort_ccb, &tstate,
1212 					     &lstate, TRUE);
1213 
1214 		if (status != CAM_REQ_CMP) {
1215 			ccb->ccb_h.status = status;
1216 			break;
1217 		}
1218 
1219 		if (abort_ccb->ccb_h.func_code == XPT_ACCEPT_TARGET_IO)
1220 			list = &lstate->accept_tios;
1221 		else if (abort_ccb->ccb_h.func_code == XPT_IMMEDIATE_NOTIFY)
1222 			list = &lstate->immed_notifies;
1223 		else
1224 			list = NULL;
1225 
1226 		if (list != NULL) {
1227 			struct ccb_hdr *curelm;
1228 			int found;
1229 
1230 			curelm = SLIST_FIRST(list);
1231 			found = 0;
1232 			if (curelm == &abort_ccb->ccb_h) {
1233 				found = 1;
1234 				SLIST_REMOVE_HEAD(list, sim_links.sle);
1235 			} else {
1236 				while(curelm != NULL) {
1237 					struct ccb_hdr *nextelm;
1238 
1239 					nextelm =
1240 					    SLIST_NEXT(curelm, sim_links.sle);
1241 
1242 					if (nextelm == &abort_ccb->ccb_h) {
1243 						found = 1;
1244 						SLIST_NEXT(curelm,
1245 							   sim_links.sle) =
1246 						    SLIST_NEXT(nextelm,
1247 							       sim_links.sle);
1248 						break;
1249 					}
1250 					curelm = nextelm;
1251 				}
1252 			}
1253 
1254 			if (found) {
1255 				abort_ccb->ccb_h.status = CAM_REQ_ABORTED;
1256 				xpt_done(abort_ccb);
1257 				ccb->ccb_h.status = CAM_REQ_CMP;
1258 			} else {
1259 				xpt_print_path(abort_ccb->ccb_h.path);
1260 				printf("Not found\n");
1261 				ccb->ccb_h.status = CAM_PATH_INVALID;
1262 			}
1263 			break;
1264 		}
1265 		/* FALLTHROUGH */
1266 	}
1267 	case XPT_SCSI_IO:
1268 		/* XXX Fully implement the hard ones */
1269 		ccb->ccb_h.status = CAM_UA_ABORT;
1270 		break;
1271 	default:
1272 		ccb->ccb_h.status = CAM_REQ_INVALID;
1273 		break;
1274 	}
1275 	xpt_done(ccb);
1276 }
1277 
1278 void
ahc_send_async(struct ahc_softc * ahc,char channel,u_int target,u_int lun,ac_code code,void * opt_arg)1279 ahc_send_async(struct ahc_softc *ahc, char channel, u_int target,
1280 		u_int lun, ac_code code, void *opt_arg)
1281 {
1282 	struct	ccb_trans_settings cts;
1283 	struct cam_path *path;
1284 	void *arg;
1285 	int error;
1286 
1287 	arg = NULL;
1288 	error = ahc_create_path(ahc, channel, target, lun, &path);
1289 
1290 	if (error != CAM_REQ_CMP)
1291 		return;
1292 
1293 	switch (code) {
1294 	case AC_TRANSFER_NEG:
1295 	{
1296 		struct	ccb_trans_settings_scsi *scsi;
1297 
1298 		cts.type = CTS_TYPE_CURRENT_SETTINGS;
1299 		scsi = &cts.proto_specific.scsi;
1300 		cts.ccb_h.path = path;
1301 		cts.ccb_h.target_id = target;
1302 		cts.ccb_h.target_lun = lun;
1303 		ahc_get_tran_settings(ahc, channel == 'A' ? ahc->our_id
1304 							  : ahc->our_id_b,
1305 				      channel, &cts);
1306 		arg = &cts;
1307 		scsi->valid &= ~CTS_SCSI_VALID_TQ;
1308 		scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1309 		if (opt_arg == NULL)
1310 			break;
1311 		if (*((ahc_queue_alg *)opt_arg) == AHC_QUEUE_TAGGED)
1312 			scsi->flags |= ~CTS_SCSI_FLAGS_TAG_ENB;
1313 		scsi->valid |= CTS_SCSI_VALID_TQ;
1314 		break;
1315 	}
1316 	case AC_SENT_BDR:
1317 	case AC_BUS_RESET:
1318 		break;
1319 	default:
1320 		panic("ahc_send_async: Unexpected async event");
1321 	}
1322 	xpt_async(code, path, arg);
1323 	xpt_free_path(path);
1324 }
1325 
1326 void
ahc_platform_set_tags(struct ahc_softc * ahc,struct ahc_devinfo * devinfo,int enable)1327 ahc_platform_set_tags(struct ahc_softc *ahc,
1328 		      struct ahc_devinfo *devinfo, int enable)
1329 {
1330 }
1331 
1332 int
ahc_platform_alloc(struct ahc_softc * ahc,void * platform_arg)1333 ahc_platform_alloc(struct ahc_softc *ahc, void *platform_arg)
1334 {
1335 	ahc->platform_data = malloc(sizeof(struct ahc_platform_data), M_DEVBUF,
1336 	    M_NOWAIT | M_ZERO);
1337 	if (ahc->platform_data == NULL)
1338 		return (ENOMEM);
1339 	return (0);
1340 }
1341 
1342 void
ahc_platform_free(struct ahc_softc * ahc)1343 ahc_platform_free(struct ahc_softc *ahc)
1344 {
1345 	struct ahc_platform_data *pdata;
1346 
1347 	pdata = ahc->platform_data;
1348 	if (pdata != NULL) {
1349 		if (pdata->regs != NULL)
1350 			bus_release_resource(ahc->dev_softc,
1351 					     pdata->regs_res_type,
1352 					     pdata->regs_res_id,
1353 					     pdata->regs);
1354 
1355 		if (pdata->irq != NULL)
1356 			bus_release_resource(ahc->dev_softc,
1357 					     pdata->irq_res_type,
1358 					     0, pdata->irq);
1359 
1360 		if (pdata->sim_b != NULL) {
1361 			xpt_async(AC_LOST_DEVICE, pdata->path_b, NULL);
1362 			xpt_free_path(pdata->path_b);
1363 			xpt_bus_deregister(cam_sim_path(pdata->sim_b));
1364 			cam_sim_free(pdata->sim_b, /*free_devq*/TRUE);
1365 		}
1366 		if (pdata->sim != NULL) {
1367 			xpt_async(AC_LOST_DEVICE, pdata->path, NULL);
1368 			xpt_free_path(pdata->path);
1369 			xpt_bus_deregister(cam_sim_path(pdata->sim));
1370 			cam_sim_free(pdata->sim, /*free_devq*/TRUE);
1371 		}
1372 		if (pdata->eh != NULL)
1373 			EVENTHANDLER_DEREGISTER(shutdown_final, pdata->eh);
1374 		free(ahc->platform_data, M_DEVBUF);
1375 	}
1376 }
1377 
1378 int
ahc_softc_comp(struct ahc_softc * lahc,struct ahc_softc * rahc)1379 ahc_softc_comp(struct ahc_softc *lahc, struct ahc_softc *rahc)
1380 {
1381 	/* We don't sort softcs under FreeBSD so report equal always */
1382 	return (0);
1383 }
1384 
1385 int
ahc_detach(device_t dev)1386 ahc_detach(device_t dev)
1387 {
1388 	struct ahc_softc *ahc;
1389 
1390 	device_printf(dev, "detaching device\n");
1391 	ahc = device_get_softc(dev);
1392 	ahc_lock(ahc);
1393 	TAILQ_REMOVE(&ahc_tailq, ahc, links);
1394 	ahc_intr_enable(ahc, FALSE);
1395 	bus_teardown_intr(dev, ahc->platform_data->irq, ahc->platform_data->ih);
1396 	ahc_unlock(ahc);
1397 	ahc_free(ahc);
1398 	return (0);
1399 }
1400 
1401 #if 0
1402 static void
1403 ahc_dump_targcmd(struct target_cmd *cmd)
1404 {
1405 	uint8_t *byte;
1406 	uint8_t *last_byte;
1407 	int i;
1408 
1409 	byte = &cmd->initiator_channel;
1410 	/* Debugging info for received commands */
1411 	last_byte = &cmd[1].initiator_channel;
1412 
1413 	i = 0;
1414 	while (byte < last_byte) {
1415 		if (i == 0)
1416 			printf("\t");
1417 		printf("%#x", *byte++);
1418 		i++;
1419 		if (i == 8) {
1420 			printf("\n");
1421 			i = 0;
1422 		} else {
1423 			printf(", ");
1424 		}
1425 	}
1426 }
1427 #endif
1428 
1429 static int
ahc_modevent(module_t mod,int type,void * data)1430 ahc_modevent(module_t mod, int type, void *data)
1431 {
1432 	/* XXX Deal with busy status on unload. */
1433 	/* XXX Deal with unknown events */
1434 	return 0;
1435 }
1436 
1437 static moduledata_t ahc_mod = {
1438 	"ahc",
1439 	ahc_modevent,
1440 	NULL
1441 };
1442 
1443 DECLARE_MODULE(ahc, ahc_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
1444 MODULE_DEPEND(ahc, cam, 1, 1, 1);
1445 MODULE_VERSION(ahc, 1);
1446