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