xref: /freebsd-12.1/sys/dev/aha/aha.c (revision 2ced5658)
1 /*-
2  * Generic register and struct definitions for the Adaptech 154x
3  * SCSI host adapters. Product specific probe and attach routines can
4  * be found in:
5  *      aha 1542A/1542B/1542C/1542CF/1542CP	aha_isa.c
6  */
7 /*-
8  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
9  *
10  * Copyright (c) 1998 M. Warner Losh.
11  * All Rights Reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * Derived from bt.c written by:
35  *
36  * Copyright (c) 1998 Justin T. Gibbs.
37  * All rights reserved.
38  *
39  * Redistribution and use in source and binary forms, with or without
40  * modification, are permitted provided that the following conditions
41  * are met:
42  * 1. Redistributions of source code must retain the above copyright
43  *    notice, this list of conditions, and the following disclaimer,
44  *    without modification, immediately at the beginning of the file.
45  * 2. The name of the author may not be used to endorse or promote products
46  *    derived from this software without specific prior written permission.
47  *
48  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
52  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58  * SUCH DAMAGE.
59  */
60 
61 #include <sys/cdefs.h>
62 __FBSDID("$FreeBSD$");
63 
64 #include <sys/param.h>
65 #include <sys/conf.h>
66 #include <sys/bus.h>
67 #include <sys/systm.h>
68 #include <sys/malloc.h>
69 #include <sys/kernel.h>
70 #include <sys/lock.h>
71 #include <sys/module.h>
72 #include <sys/mutex.h>
73 #include <sys/rman.h>
74 
75 #include <machine/bus.h>
76 
77 #include <cam/cam.h>
78 #include <cam/cam_ccb.h>
79 #include <cam/cam_sim.h>
80 #include <cam/cam_xpt_sim.h>
81 #include <cam/cam_debug.h>
82 
83 #include <cam/scsi/scsi_message.h>
84 
85 #include <dev/aha/ahareg.h>
86 
87 #define	PRVERB(x) do { if (bootverbose) device_printf x; } while (0)
88 
89 /* Macro to determine that a rev is potentially a new valid one
90  * so that the driver doesn't keep breaking on new revs as it
91  * did for the CF and CP.
92  */
93 #define PROBABLY_NEW_BOARD(REV) (REV > 0x43 && REV < 0x56)
94 
95 /* MailBox Management functions */
96 static __inline void	ahanextinbox(struct aha_softc *aha);
97 static __inline void	ahanextoutbox(struct aha_softc *aha);
98 
99 #define aha_name(aha)	device_get_nameunit(aha->dev)
100 
101 static __inline void
ahanextinbox(struct aha_softc * aha)102 ahanextinbox(struct aha_softc *aha)
103 {
104 	if (aha->cur_inbox == aha->last_inbox)
105 		aha->cur_inbox = aha->in_boxes;
106 	else
107 		aha->cur_inbox++;
108 }
109 
110 static __inline void
ahanextoutbox(struct aha_softc * aha)111 ahanextoutbox(struct aha_softc *aha)
112 {
113 	if (aha->cur_outbox == aha->last_outbox)
114 		aha->cur_outbox = aha->out_boxes;
115 	else
116 		aha->cur_outbox++;
117 }
118 
119 #define ahautoa24(u,s3)			\
120 	(s3)[0] = ((u) >> 16) & 0xff;	\
121 	(s3)[1] = ((u) >> 8) & 0xff;	\
122 	(s3)[2] = (u) & 0xff;
123 
124 #define aha_a24tou(s3) \
125 	(((s3)[0] << 16) | ((s3)[1] << 8) | (s3)[2])
126 
127 /* CCB Management functions */
128 static __inline uint32_t		ahaccbvtop(struct aha_softc *aha,
129 						  struct aha_ccb *accb);
130 static __inline struct aha_ccb*		ahaccbptov(struct aha_softc *aha,
131 						  uint32_t ccb_addr);
132 
133 static __inline uint32_t
ahaccbvtop(struct aha_softc * aha,struct aha_ccb * accb)134 ahaccbvtop(struct aha_softc *aha, struct aha_ccb *accb)
135 {
136 	return (aha->aha_ccb_physbase
137 	      + (uint32_t)((caddr_t)accb - (caddr_t)aha->aha_ccb_array));
138 }
139 static __inline struct aha_ccb *
ahaccbptov(struct aha_softc * aha,uint32_t ccb_addr)140 ahaccbptov(struct aha_softc *aha, uint32_t ccb_addr)
141 {
142 	return (aha->aha_ccb_array +
143 	      + ((struct aha_ccb*)(uintptr_t)ccb_addr -
144 	         (struct aha_ccb*)(uintptr_t)aha->aha_ccb_physbase));
145 }
146 
147 static struct aha_ccb*	ahagetccb(struct aha_softc *aha);
148 static __inline void	ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb);
149 static void		ahaallocccbs(struct aha_softc *aha);
150 static bus_dmamap_callback_t ahaexecuteccb;
151 static void		ahadone(struct aha_softc *aha, struct aha_ccb *accb,
152 			       aha_mbi_comp_code_t comp_code);
153 static void		aha_intr_locked(struct aha_softc *aha);
154 
155 /* Host adapter command functions */
156 static int	ahareset(struct aha_softc* aha, int hard_reset);
157 
158 /* Initialization functions */
159 static int			ahainitmboxes(struct aha_softc *aha);
160 static bus_dmamap_callback_t	ahamapmboxes;
161 static bus_dmamap_callback_t	ahamapccbs;
162 static bus_dmamap_callback_t	ahamapsgs;
163 
164 /* Transfer Negotiation Functions */
165 static void ahafetchtransinfo(struct aha_softc *aha,
166 			     struct ccb_trans_settings *cts);
167 
168 /* CAM SIM entry points */
169 #define ccb_accb_ptr spriv_ptr0
170 #define ccb_aha_ptr spriv_ptr1
171 static void	ahaaction(struct cam_sim *sim, union ccb *ccb);
172 static void	ahapoll(struct cam_sim *sim);
173 
174 /* Our timeout handler */
175 static void	ahatimeout(void *arg);
176 
177 /* Exported functions */
178 void
aha_alloc(struct aha_softc * aha)179 aha_alloc(struct aha_softc *aha)
180 {
181 
182 	SLIST_INIT(&aha->free_aha_ccbs);
183 	LIST_INIT(&aha->pending_ccbs);
184 	SLIST_INIT(&aha->sg_maps);
185 	aha->ccb_sg_opcode = INITIATOR_SG_CCB_WRESID;
186 	aha->ccb_ccb_opcode = INITIATOR_CCB_WRESID;
187 	mtx_init(&aha->lock, "aha", NULL, MTX_DEF);
188 }
189 
190 void
aha_free(struct aha_softc * aha)191 aha_free(struct aha_softc *aha)
192 {
193 	switch (aha->init_level) {
194 	default:
195 	case 8:
196 	{
197 		struct sg_map_node *sg_map;
198 
199 		while ((sg_map = SLIST_FIRST(&aha->sg_maps))!= NULL) {
200 			SLIST_REMOVE_HEAD(&aha->sg_maps, links);
201 			bus_dmamap_unload(aha->sg_dmat, sg_map->sg_dmamap);
202 			bus_dmamem_free(aha->sg_dmat, sg_map->sg_vaddr,
203 			    sg_map->sg_dmamap);
204 			free(sg_map, M_DEVBUF);
205 		}
206 		bus_dma_tag_destroy(aha->sg_dmat);
207 	}
208 	case 7:
209 		bus_dmamap_unload(aha->ccb_dmat, aha->ccb_dmamap);
210 	case 6:
211 		bus_dmamem_free(aha->ccb_dmat, aha->aha_ccb_array,
212 		    aha->ccb_dmamap);
213 	case 5:
214 		bus_dma_tag_destroy(aha->ccb_dmat);
215 	case 4:
216 		bus_dmamap_unload(aha->mailbox_dmat, aha->mailbox_dmamap);
217 	case 3:
218 		bus_dmamem_free(aha->mailbox_dmat, aha->in_boxes,
219 		    aha->mailbox_dmamap);
220 	case 2:
221 		bus_dma_tag_destroy(aha->buffer_dmat);
222 	case 1:
223 		bus_dma_tag_destroy(aha->mailbox_dmat);
224 	case 0:
225 		break;
226 	}
227 	mtx_destroy(&aha->lock);
228 }
229 
230 /*
231  * Probe the adapter and verify that the card is an Adaptec.
232  */
233 int
aha_probe(struct aha_softc * aha)234 aha_probe(struct aha_softc* aha)
235 {
236 	u_int	 status;
237 	u_int	 intstat;
238 	int	 error;
239 	board_id_data_t	board_id;
240 
241 	/*
242 	 * See if the three I/O ports look reasonable.
243 	 * Touch the minimal number of registers in the
244 	 * failure case.
245 	 */
246 	status = aha_inb(aha, STATUS_REG);
247 	if ((status == 0) ||
248 	    (status & (DIAG_ACTIVE|CMD_REG_BUSY | STATUS_REG_RSVD)) != 0) {
249 		PRVERB((aha->dev, "status reg test failed %x\n", status));
250 		return (ENXIO);
251 	}
252 
253 	intstat = aha_inb(aha, INTSTAT_REG);
254 	if ((intstat & INTSTAT_REG_RSVD) != 0) {
255 		PRVERB((aha->dev, "Failed Intstat Reg Test\n"));
256 		return (ENXIO);
257 	}
258 
259 	/*
260 	 * Looking good so far.  Final test is to reset the
261 	 * adapter and fetch the board ID and ensure we aren't
262 	 * looking at a BusLogic.
263 	 */
264 	if ((error = ahareset(aha, /*hard_reset*/TRUE)) != 0) {
265 		PRVERB((aha->dev, "Failed Reset\n"));
266 		return (ENXIO);
267 	}
268 
269 	/*
270 	 * Get the board ID.  We use this to see if we're dealing with
271 	 * a buslogic card or an aha card (or clone).
272 	 */
273 	error = aha_cmd(aha, AOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0,
274 	    (uint8_t*)&board_id, sizeof(board_id), DEFAULT_CMD_TIMEOUT);
275 	if (error != 0) {
276 		PRVERB((aha->dev, "INQUIRE failed %x\n", error));
277 		return (ENXIO);
278 	}
279 	aha->fw_major = board_id.firmware_rev_major;
280 	aha->fw_minor = board_id.firmware_rev_minor;
281 	aha->boardid = board_id.board_type;
282 
283 	/*
284 	 * The Buslogic cards have an id of either 0x41 or 0x42.  So
285 	 * if those come up in the probe, we test the geometry register
286 	 * of the board.  Adaptec boards that are this old will not have
287 	 * this register, and return 0xff, while buslogic cards will return
288 	 * something different.
289 	 *
290 	 * It appears that for reasons unknow, for the for the
291 	 * aha-1542B cards, we need to wait a little bit before trying
292 	 * to read the geometry register.  I picked 10ms since we have
293 	 * reports that a for loop to 1000 did the trick, and this
294 	 * errs on the side of conservatism.  Besides, no one will
295 	 * notice a 10mS delay here, even the 1542B card users :-)
296 	 *
297 	 * Some compatible cards return 0 here.  Some cards also
298 	 * seem to return 0x7f.
299 	 *
300 	 * XXX I'm not sure how this will impact other cloned cards
301 	 *
302 	 * This really should be replaced with the esetup command, since
303 	 * that appears to be more reliable.  This becomes more and more
304 	 * true over time as we discover more cards that don't read the
305 	 * geometry register consistently.
306 	 */
307 	if (aha->boardid <= 0x42) {
308 		/* Wait 10ms before reading */
309 		DELAY(10000);
310 		status = aha_inb(aha, GEOMETRY_REG);
311 		if (status != 0xff && status != 0x00 && status != 0x7f) {
312 			PRVERB((aha->dev, "Geometry Register test failed %#x\n",
313 				status));
314 			return (ENXIO);
315 		}
316 	}
317 
318 	return (0);
319 }
320 
321 /*
322  * Pull the boards setup information and record it in our softc.
323  */
324 int
aha_fetch_adapter_info(struct aha_softc * aha)325 aha_fetch_adapter_info(struct aha_softc *aha)
326 {
327 	setup_data_t	setup_info;
328 	config_data_t config_data;
329 	uint8_t length_param;
330 	int	 error;
331 	struct	aha_extbios extbios;
332 
333 	switch (aha->boardid) {
334 	case BOARD_1540_16HEAD_BIOS:
335 		snprintf(aha->model, sizeof(aha->model), "1540 16 head BIOS");
336 		break;
337 	case BOARD_1540_64HEAD_BIOS:
338 		snprintf(aha->model, sizeof(aha->model), "1540 64 head BIOS");
339 		break;
340 	case BOARD_1542:
341 		snprintf(aha->model, sizeof(aha->model), "1540/1542 64 head BIOS");
342 		break;
343 	case BOARD_1542C:
344 		snprintf(aha->model, sizeof(aha->model), "1542C");
345 		break;
346 	case BOARD_1542CF:
347 		snprintf(aha->model, sizeof(aha->model), "1542CF");
348 		break;
349 	case BOARD_1542CP:
350 		snprintf(aha->model, sizeof(aha->model), "1542CP");
351 		break;
352 	default:
353 		snprintf(aha->model, sizeof(aha->model), "Unknown");
354 		break;
355 	}
356 	/*
357 	 * If we are a new type of 1542 board (anything newer than a 1542C)
358 	 * then disable the extended bios so that the
359 	 * mailbox interface is unlocked.
360 	 * This is also true for the 1542B Version 3.20. First Adaptec
361 	 * board that supports >1Gb drives.
362 	 * No need to check the extended bios flags as some of the
363 	 * extensions that cause us problems are not flagged in that byte.
364 	 */
365 	if (PROBABLY_NEW_BOARD(aha->boardid) ||
366 		(aha->boardid == 0x41
367 		&& aha->fw_major == 0x31 &&
368 		aha->fw_minor >= 0x34)) {
369 		error = aha_cmd(aha, AOP_RETURN_EXT_BIOS_INFO, NULL,
370 		    /*paramlen*/0, (u_char *)&extbios, sizeof(extbios),
371 		    DEFAULT_CMD_TIMEOUT);
372 		if (error != 0) {
373 			device_printf(aha->dev,
374 			    "AOP_RETURN_EXT_BIOS_INFO - Failed.");
375 			return (error);
376 		}
377 		error = aha_cmd(aha, AOP_MBOX_IF_ENABLE, (uint8_t *)&extbios,
378 		    /*paramlen*/2, NULL, 0, DEFAULT_CMD_TIMEOUT);
379 		if (error != 0) {
380 			device_printf(aha->dev, "AOP_MBOX_IF_ENABLE - Failed.");
381 			return (error);
382 		}
383 	}
384 	if (aha->boardid < 0x41)
385 		device_printf(aha->dev, "Warning: aha-1542A won't work.\n");
386 
387 	aha->max_sg = 17;		/* Need >= 17 to do 64k I/O */
388 	aha->diff_bus = 0;
389 	aha->extended_lun = 0;
390 	aha->extended_trans = 0;
391 	aha->max_ccbs = 16;
392 	/* Determine Sync/Wide/Disc settings */
393 	length_param = sizeof(setup_info);
394 	error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, &length_param,
395 	    /*paramlen*/1, (uint8_t*)&setup_info, sizeof(setup_info),
396 	    DEFAULT_CMD_TIMEOUT);
397 	if (error != 0) {
398 		device_printf(aha->dev, "aha_fetch_adapter_info - Failed "
399 		    "Get Setup Info\n");
400 		return (error);
401 	}
402 	if (setup_info.initiate_sync != 0) {
403 		aha->sync_permitted = ALL_TARGETS;
404 	}
405 	aha->disc_permitted = ALL_TARGETS;
406 
407 	/* We need as many mailboxes as we can have ccbs */
408 	aha->num_boxes = aha->max_ccbs;
409 
410 	/* Determine our SCSI ID */
411 	error = aha_cmd(aha, AOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
412 	    (uint8_t*)&config_data, sizeof(config_data), DEFAULT_CMD_TIMEOUT);
413 	if (error != 0) {
414 		device_printf(aha->dev,
415 		    "aha_fetch_adapter_info - Failed Get Config\n");
416 		return (error);
417 	}
418 	aha->scsi_id = config_data.scsi_id;
419 	return (0);
420 }
421 
422 /*
423  * Start the board, ready for normal operation
424  */
425 int
aha_init(struct aha_softc * aha)426 aha_init(struct aha_softc* aha)
427 {
428 	/* Announce the Adapter */
429 	device_printf(aha->dev, "AHA-%s FW Rev. %c.%c (ID=%x) ",
430 	    aha->model, aha->fw_major, aha->fw_minor, aha->boardid);
431 
432 	if (aha->diff_bus != 0)
433 		printf("Diff ");
434 
435 	printf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", aha->scsi_id,
436 	    aha->max_ccbs);
437 
438 	/*
439 	 * Create our DMA tags.  These tags define the kinds of device
440 	 * accessible memory allocations and memory mappings we will
441 	 * need to perform during normal operation.
442 	 *
443 	 * Unless we need to further restrict the allocation, we rely
444 	 * on the restrictions of the parent dmat, hence the common
445 	 * use of MAXADDR and MAXSIZE.
446 	 */
447 
448 	/* DMA tag for mapping buffers into device visible space. */
449 	if (bus_dma_tag_create( /* parent	*/ aha->parent_dmat,
450 				/* alignment	*/ 1,
451 				/* boundary	*/ 0,
452 				/* lowaddr	*/ BUS_SPACE_MAXADDR,
453 				/* highaddr	*/ BUS_SPACE_MAXADDR,
454 				/* filter	*/ NULL,
455 				/* filterarg	*/ NULL,
456 				/* maxsize	*/ DFLTPHYS,
457 				/* nsegments	*/ AHA_NSEG,
458 				/* maxsegsz	*/ BUS_SPACE_MAXSIZE_24BIT,
459 				/* flags	*/ BUS_DMA_ALLOCNOW,
460 				/* lockfunc	*/ busdma_lock_mutex,
461 				/* lockarg	*/ &aha->lock,
462 				&aha->buffer_dmat) != 0) {
463 		goto error_exit;
464 	}
465 
466 	aha->init_level++;
467 	/* DMA tag for our mailboxes */
468 	if (bus_dma_tag_create(	/* parent	*/ aha->parent_dmat,
469 				/* alignment	*/ 1,
470 				/* boundary	*/ 0,
471 				/* lowaddr	*/ BUS_SPACE_MAXADDR,
472 				/* highaddr	*/ BUS_SPACE_MAXADDR,
473 				/* filter	*/ NULL,
474 				/* filterarg	*/ NULL,
475 				/* maxsize	*/ aha->num_boxes *
476 						   (sizeof(aha_mbox_in_t) +
477 						    sizeof(aha_mbox_out_t)),
478 				/* nsegments	*/ 1,
479 				/* maxsegsz	*/ BUS_SPACE_MAXSIZE_24BIT,
480 				/* flags	*/ 0,
481 				/* lockfunc	*/ NULL,
482 				/* lockarg	*/ NULL,
483 				&aha->mailbox_dmat) != 0) {
484 		goto error_exit;
485         }
486 
487 	aha->init_level++;
488 
489 	/* Allocation for our mailboxes */
490 	if (bus_dmamem_alloc(aha->mailbox_dmat, (void **)&aha->out_boxes,
491 	    BUS_DMA_NOWAIT, &aha->mailbox_dmamap) != 0)
492 		goto error_exit;
493 
494 	aha->init_level++;
495 
496 	/* And permanently map them */
497 	bus_dmamap_load(aha->mailbox_dmat, aha->mailbox_dmamap,
498 	    aha->out_boxes, aha->num_boxes * (sizeof(aha_mbox_in_t) +
499 	    sizeof(aha_mbox_out_t)), ahamapmboxes, aha, /*flags*/0);
500 
501 	aha->init_level++;
502 
503 	aha->in_boxes = (aha_mbox_in_t *)&aha->out_boxes[aha->num_boxes];
504 
505 	ahainitmboxes(aha);
506 
507 	/* DMA tag for our ccb structures */
508 	if (bus_dma_tag_create(	/* parent	*/ aha->parent_dmat,
509 				/* alignment	*/ 1,
510 				/* boundary	*/ 0,
511 				/* lowaddr	*/ BUS_SPACE_MAXADDR,
512 				/* highaddr	*/ BUS_SPACE_MAXADDR,
513 				/* filter	*/ NULL,
514 				/* filterarg	*/ NULL,
515 				/* maxsize	*/ aha->max_ccbs *
516 						   sizeof(struct aha_ccb),
517 				/* nsegments	*/ 1,
518 				/* maxsegsz	*/ BUS_SPACE_MAXSIZE_24BIT,
519 				/* flags	*/ 0,
520 				/* lockfunc	*/ NULL,
521 				/* lockarg	*/ NULL,
522 				&aha->ccb_dmat) != 0) {
523 		goto error_exit;
524         }
525 
526 	aha->init_level++;
527 
528 	/* Allocation for our ccbs */
529 	if (bus_dmamem_alloc(aha->ccb_dmat, (void **)&aha->aha_ccb_array,
530 	    BUS_DMA_NOWAIT, &aha->ccb_dmamap) != 0)
531 		goto error_exit;
532 
533 	aha->init_level++;
534 
535 	/* And permanently map them */
536 	bus_dmamap_load(aha->ccb_dmat, aha->ccb_dmamap, aha->aha_ccb_array,
537 	    aha->max_ccbs * sizeof(struct aha_ccb), ahamapccbs, aha, /*flags*/0);
538 
539 	aha->init_level++;
540 
541 	/* DMA tag for our S/G structures.  We allocate in page sized chunks */
542 	if (bus_dma_tag_create(	/* parent	*/ aha->parent_dmat,
543 				/* alignment	*/ 1,
544 				/* boundary	*/ 0,
545 				/* lowaddr	*/ BUS_SPACE_MAXADDR,
546 				/* highaddr	*/ BUS_SPACE_MAXADDR,
547 				/* filter	*/ NULL,
548 				/* filterarg	*/ NULL,
549 				/* maxsize	*/ PAGE_SIZE,
550 				/* nsegments	*/ 1,
551 				/* maxsegsz	*/ BUS_SPACE_MAXSIZE_24BIT,
552 				/* flags	*/ 0,
553 				/* lockfunc	*/ NULL,
554 				/* lockarg	*/ NULL,
555 				&aha->sg_dmat) != 0)
556 		goto error_exit;
557 
558 	aha->init_level++;
559 
560 	/* Perform initial CCB allocation */
561 	bzero(aha->aha_ccb_array, aha->max_ccbs * sizeof(struct aha_ccb));
562 	ahaallocccbs(aha);
563 
564 	if (aha->num_ccbs == 0) {
565 		device_printf(aha->dev,
566 		    "aha_init - Unable to allocate initial ccbs\n");
567 		goto error_exit;
568 	}
569 
570 	/*
571 	 * Note that we are going and return (to probe)
572 	 */
573 	return (0);
574 
575 error_exit:
576 
577 	return (ENXIO);
578 }
579 
580 int
aha_attach(struct aha_softc * aha)581 aha_attach(struct aha_softc *aha)
582 {
583 	int tagged_dev_openings;
584 	struct cam_devq *devq;
585 
586 	/*
587 	 * We don't do tagged queueing, since the aha cards don't
588 	 * support it.
589 	 */
590 	tagged_dev_openings = 0;
591 
592 	/*
593 	 * Create the device queue for our SIM.
594 	 */
595 	devq = cam_simq_alloc(aha->max_ccbs - 1);
596 	if (devq == NULL)
597 		return (ENOMEM);
598 
599 	/*
600 	 * Construct our SIM entry
601 	 */
602 	aha->sim = cam_sim_alloc(ahaaction, ahapoll, "aha", aha,
603 	    device_get_unit(aha->dev), &aha->lock, 2, tagged_dev_openings,
604 	    devq);
605 	if (aha->sim == NULL) {
606 		cam_simq_free(devq);
607 		return (ENOMEM);
608 	}
609 	mtx_lock(&aha->lock);
610 	if (xpt_bus_register(aha->sim, aha->dev, 0) != CAM_SUCCESS) {
611 		cam_sim_free(aha->sim, /*free_devq*/TRUE);
612 		mtx_unlock(&aha->lock);
613 		return (ENXIO);
614 	}
615 	if (xpt_create_path(&aha->path, /*periph*/NULL, cam_sim_path(aha->sim),
616 	    CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
617 		xpt_bus_deregister(cam_sim_path(aha->sim));
618 		cam_sim_free(aha->sim, /*free_devq*/TRUE);
619 		mtx_unlock(&aha->lock);
620 		return (ENXIO);
621 	}
622 	mtx_unlock(&aha->lock);
623 	gone_in_dev(aha->dev, 12, "aha(4) driver");
624 
625 	return (0);
626 }
627 
628 static void
ahaallocccbs(struct aha_softc * aha)629 ahaallocccbs(struct aha_softc *aha)
630 {
631 	struct aha_ccb *next_ccb;
632 	struct sg_map_node *sg_map;
633 	bus_addr_t physaddr;
634 	aha_sg_t *segs;
635 	int newcount;
636 	int i;
637 
638 	next_ccb = &aha->aha_ccb_array[aha->num_ccbs];
639 
640 	sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
641 
642 	if (sg_map == NULL)
643 		return;
644 
645 	/* Allocate S/G space for the next batch of CCBS */
646 	if (bus_dmamem_alloc(aha->sg_dmat, (void **)&sg_map->sg_vaddr,
647 	    BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
648 		free(sg_map, M_DEVBUF);
649 		return;
650 	}
651 
652 	SLIST_INSERT_HEAD(&aha->sg_maps, sg_map, links);
653 
654 	bus_dmamap_load(aha->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
655 	    PAGE_SIZE, ahamapsgs, aha, /*flags*/0);
656 
657 	segs = sg_map->sg_vaddr;
658 	physaddr = sg_map->sg_physaddr;
659 
660 	newcount = (PAGE_SIZE / (AHA_NSEG * sizeof(aha_sg_t)));
661 	for (i = 0; aha->num_ccbs < aha->max_ccbs && i < newcount; i++) {
662 		int error;
663 
664 		next_ccb->sg_list = segs;
665 		next_ccb->sg_list_phys = physaddr;
666 		next_ccb->flags = ACCB_FREE;
667 		callout_init_mtx(&next_ccb->timer, &aha->lock, 0);
668 		error = bus_dmamap_create(aha->buffer_dmat, /*flags*/0,
669 		    &next_ccb->dmamap);
670 		if (error != 0)
671 			break;
672 		SLIST_INSERT_HEAD(&aha->free_aha_ccbs, next_ccb, links);
673 		segs += AHA_NSEG;
674 		physaddr += (AHA_NSEG * sizeof(aha_sg_t));
675 		next_ccb++;
676 		aha->num_ccbs++;
677 	}
678 
679 	/* Reserve a CCB for error recovery */
680 	if (aha->recovery_accb == NULL) {
681 		aha->recovery_accb = SLIST_FIRST(&aha->free_aha_ccbs);
682 		SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
683 	}
684 }
685 
686 static __inline void
ahafreeccb(struct aha_softc * aha,struct aha_ccb * accb)687 ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb)
688 {
689 
690 	if (!dumping)
691 		mtx_assert(&aha->lock, MA_OWNED);
692 	if ((accb->flags & ACCB_ACTIVE) != 0)
693 		LIST_REMOVE(&accb->ccb->ccb_h, sim_links.le);
694 	if (aha->resource_shortage != 0
695 	    && (accb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
696 		accb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
697 		aha->resource_shortage = FALSE;
698 	}
699 	accb->flags = ACCB_FREE;
700 	SLIST_INSERT_HEAD(&aha->free_aha_ccbs, accb, links);
701 	aha->active_ccbs--;
702 }
703 
704 static struct aha_ccb*
ahagetccb(struct aha_softc * aha)705 ahagetccb(struct aha_softc *aha)
706 {
707 	struct	aha_ccb* accb;
708 
709 	if (!dumping)
710 		mtx_assert(&aha->lock, MA_OWNED);
711 	if ((accb = SLIST_FIRST(&aha->free_aha_ccbs)) != NULL) {
712 		SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
713 		aha->active_ccbs++;
714 	} else if (aha->num_ccbs < aha->max_ccbs) {
715 		ahaallocccbs(aha);
716 		accb = SLIST_FIRST(&aha->free_aha_ccbs);
717 		if (accb == NULL)
718 			device_printf(aha->dev, "Can't malloc ACCB\n");
719 		else {
720 			SLIST_REMOVE_HEAD(&aha->free_aha_ccbs, links);
721 			aha->active_ccbs++;
722 		}
723 	}
724 
725 	return (accb);
726 }
727 
728 static void
ahaaction(struct cam_sim * sim,union ccb * ccb)729 ahaaction(struct cam_sim *sim, union ccb *ccb)
730 {
731 	struct	aha_softc *aha;
732 
733 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("ahaaction\n"));
734 
735 	aha = (struct aha_softc *)cam_sim_softc(sim);
736 	mtx_assert(&aha->lock, MA_OWNED);
737 
738 	switch (ccb->ccb_h.func_code) {
739 	/* Common cases first */
740 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
741 	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */	{
742 		struct	aha_ccb	*accb;
743 		struct	aha_hccb *hccb;
744 
745 		/*
746 		 * Get an accb to use.
747 		 */
748 		if ((accb = ahagetccb(aha)) == NULL) {
749 			aha->resource_shortage = TRUE;
750 			xpt_freeze_simq(aha->sim, /*count*/1);
751 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
752 			xpt_done(ccb);
753 			return;
754 		}
755 		hccb = &accb->hccb;
756 
757 		/*
758 		 * So we can find the ACCB when an abort is requested
759 		 */
760 		accb->ccb = ccb;
761 		ccb->ccb_h.ccb_accb_ptr = accb;
762 		ccb->ccb_h.ccb_aha_ptr = aha;
763 
764 		/*
765 		 * Put all the arguments for the xfer in the accb
766 		 */
767 		hccb->target = ccb->ccb_h.target_id;
768 		hccb->lun = ccb->ccb_h.target_lun;
769 		hccb->ahastat = 0;
770 		hccb->sdstat = 0;
771 
772 		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
773 			struct ccb_scsiio *csio;
774 			struct ccb_hdr *ccbh;
775 			int error;
776 
777 			csio = &ccb->csio;
778 			ccbh = &csio->ccb_h;
779 			hccb->opcode = aha->ccb_ccb_opcode;
780 			hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) != 0;
781 			hccb->dataout = (ccb->ccb_h.flags & CAM_DIR_OUT) != 0;
782 			hccb->cmd_len = csio->cdb_len;
783 			if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) {
784 				ccb->ccb_h.status = CAM_REQ_INVALID;
785 				ahafreeccb(aha, accb);
786 				xpt_done(ccb);
787 				return;
788 			}
789 			hccb->sense_len = csio->sense_len;
790 			if ((ccbh->flags & CAM_CDB_POINTER) != 0) {
791 				if ((ccbh->flags & CAM_CDB_PHYS) == 0) {
792 					bcopy(csio->cdb_io.cdb_ptr,
793 					      hccb->scsi_cdb, hccb->cmd_len);
794 				} else {
795 					/* I guess I could map it in... */
796 					ccbh->status = CAM_REQ_INVALID;
797 					ahafreeccb(aha, accb);
798 					xpt_done(ccb);
799 					return;
800 				}
801 			} else {
802 				bcopy(csio->cdb_io.cdb_bytes,
803 				      hccb->scsi_cdb, hccb->cmd_len);
804 			}
805 			/*
806 			 * If we have any data to send with this command,
807 			 * map it into bus space.
808 			 */
809 
810 			error = bus_dmamap_load_ccb(
811 			    aha->buffer_dmat,
812 			    accb->dmamap,
813 			    ccb,
814 			    ahaexecuteccb,
815 			    accb,
816 			    /*flags*/0);
817 			if (error == EINPROGRESS) {
818 				/*
819 				 * So as to maintain ordering, freeze the
820 				 * controller queue until our mapping is
821 				 * returned.
822 				 */
823 				xpt_freeze_simq(aha->sim, 1);
824 				csio->ccb_h.status |= CAM_RELEASE_SIMQ;
825 			}
826 		} else {
827 			hccb->opcode = INITIATOR_BUS_DEV_RESET;
828 			/* No data transfer */
829 			hccb->datain = TRUE;
830 			hccb->dataout = TRUE;
831 			hccb->cmd_len = 0;
832 			hccb->sense_len = 0;
833 			ahaexecuteccb(accb, NULL, 0, 0);
834 		}
835 		break;
836 	}
837 	case XPT_ABORT:			/* Abort the specified CCB */
838 		/* XXX Implement */
839 		ccb->ccb_h.status = CAM_REQ_INVALID;
840 		xpt_done(ccb);
841 		break;
842 	case XPT_SET_TRAN_SETTINGS:
843 		/* XXX Implement */
844 		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
845 		xpt_done(ccb);
846 		break;
847 	case XPT_GET_TRAN_SETTINGS:
848 	/* Get default/user set transfer settings for the target */
849 	{
850 		struct	ccb_trans_settings *cts = &ccb->cts;
851 		u_int	target_mask = 0x01 << ccb->ccb_h.target_id;
852 		struct ccb_trans_settings_scsi *scsi =
853 		    &cts->proto_specific.scsi;
854 		struct ccb_trans_settings_spi *spi =
855 		    &cts->xport_specific.spi;
856 
857 		cts->protocol = PROTO_SCSI;
858 		cts->protocol_version = SCSI_REV_2;
859 		cts->transport = XPORT_SPI;
860 		cts->transport_version = 2;
861 		if (cts->type == CTS_TYPE_USER_SETTINGS) {
862 			spi->flags = 0;
863 			if ((aha->disc_permitted & target_mask) != 0)
864 				spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
865 			spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
866 			if ((aha->sync_permitted & target_mask) != 0) {
867 				if (aha->boardid >= BOARD_1542CF)
868 					spi->sync_period = 25;
869 				else
870 					spi->sync_period = 50;
871 			} else {
872 				spi->sync_period = 0;
873 			}
874 
875 			if (spi->sync_period != 0)
876 				spi->sync_offset = 15;
877 
878 			spi->valid = CTS_SPI_VALID_SYNC_RATE
879 				   | CTS_SPI_VALID_SYNC_OFFSET
880 				   | CTS_SPI_VALID_BUS_WIDTH
881 				   | CTS_SPI_VALID_DISC;
882 			scsi->valid = CTS_SCSI_VALID_TQ;
883 		} else {
884 			ahafetchtransinfo(aha, cts);
885 		}
886 
887 		ccb->ccb_h.status = CAM_REQ_CMP;
888 		xpt_done(ccb);
889 		break;
890 	}
891 	case XPT_CALC_GEOMETRY:
892 	{
893 		struct	  ccb_calc_geometry *ccg;
894 		uint32_t size_mb;
895 		uint32_t secs_per_cylinder;
896 
897 		ccg = &ccb->ccg;
898 		size_mb = ccg->volume_size
899 			/ ((1024L * 1024L) / ccg->block_size);
900 		if (size_mb >= 1024 && (aha->extended_trans != 0)) {
901 			if (size_mb >= 2048) {
902 				ccg->heads = 255;
903 				ccg->secs_per_track = 63;
904 			} else {
905 				ccg->heads = 128;
906 				ccg->secs_per_track = 32;
907 			}
908 		} else {
909 			ccg->heads = 64;
910 			ccg->secs_per_track = 32;
911 		}
912 		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
913 		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
914 		ccb->ccb_h.status = CAM_REQ_CMP;
915 		xpt_done(ccb);
916 		break;
917 	}
918 	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
919 		ahareset(aha, /*hardreset*/TRUE);
920 		ccb->ccb_h.status = CAM_REQ_CMP;
921 		xpt_done(ccb);
922 		break;
923 	case XPT_TERM_IO:		/* Terminate the I/O process */
924 		/* XXX Implement */
925 		ccb->ccb_h.status = CAM_REQ_INVALID;
926 		xpt_done(ccb);
927 		break;
928 	case XPT_PATH_INQ:		/* Path routing inquiry */
929 	{
930 		struct ccb_pathinq *cpi = &ccb->cpi;
931 
932 		cpi->version_num = 1; /* XXX??? */
933 		cpi->hba_inquiry = PI_SDTR_ABLE;
934 		cpi->target_sprt = 0;
935 		cpi->hba_misc = 0;
936 		cpi->hba_eng_cnt = 0;
937 		cpi->max_target = 7;
938 		cpi->max_lun = 7;
939 		cpi->initiator_id = aha->scsi_id;
940 		cpi->bus_id = cam_sim_bus(sim);
941 		cpi->base_transfer_speed = 3300;
942 		strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
943 		strlcpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
944 		strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
945 		cpi->unit_number = cam_sim_unit(sim);
946 		cpi->transport = XPORT_SPI;
947 		cpi->transport_version = 2;
948 		cpi->protocol = PROTO_SCSI;
949 		cpi->protocol_version = SCSI_REV_2;
950 		cpi->ccb_h.status = CAM_REQ_CMP;
951 		xpt_done(ccb);
952 		break;
953 	}
954 	default:
955 		ccb->ccb_h.status = CAM_REQ_INVALID;
956 		xpt_done(ccb);
957 		break;
958 	}
959 }
960 
961 static void
ahaexecuteccb(void * arg,bus_dma_segment_t * dm_segs,int nseg,int error)962 ahaexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
963 {
964 	struct	 aha_ccb *accb;
965 	union	 ccb *ccb;
966 	struct	 aha_softc *aha;
967 	uint32_t paddr;
968 
969 	accb = (struct aha_ccb *)arg;
970 	ccb = accb->ccb;
971 	aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
972 
973 	if (error != 0) {
974 		if (error != EFBIG)
975 			device_printf(aha->dev,
976 			    "Unexepected error 0x%x returned from "
977 			    "bus_dmamap_load\n", error);
978 		if (ccb->ccb_h.status == CAM_REQ_INPROG) {
979 			xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
980 			ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
981 		}
982 		ahafreeccb(aha, accb);
983 		xpt_done(ccb);
984 		return;
985 	}
986 
987 	if (nseg != 0) {
988 		aha_sg_t *sg;
989 		bus_dma_segment_t *end_seg;
990 		bus_dmasync_op_t op;
991 
992 		end_seg = dm_segs + nseg;
993 
994 		/* Copy the segments into our SG list */
995 		sg = accb->sg_list;
996 		while (dm_segs < end_seg) {
997 			ahautoa24(dm_segs->ds_len, sg->len);
998 			ahautoa24(dm_segs->ds_addr, sg->addr);
999 			sg++;
1000 			dm_segs++;
1001 		}
1002 
1003 		if (nseg > 1) {
1004 			accb->hccb.opcode = aha->ccb_sg_opcode;
1005 			ahautoa24((sizeof(aha_sg_t) * nseg),
1006 			    accb->hccb.data_len);
1007 			ahautoa24(accb->sg_list_phys, accb->hccb.data_addr);
1008 		} else {
1009 			bcopy(accb->sg_list->len, accb->hccb.data_len, 3);
1010 			bcopy(accb->sg_list->addr, accb->hccb.data_addr, 3);
1011 		}
1012 
1013 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1014 			op = BUS_DMASYNC_PREREAD;
1015 		else
1016 			op = BUS_DMASYNC_PREWRITE;
1017 
1018 		bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1019 
1020 	} else {
1021 		accb->hccb.opcode = INITIATOR_CCB;
1022 		ahautoa24(0, accb->hccb.data_len);
1023 		ahautoa24(0, accb->hccb.data_addr);
1024 	}
1025 
1026 	/*
1027 	 * Last time we need to check if this CCB needs to
1028 	 * be aborted.
1029 	 */
1030 	if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1031 		if (nseg != 0)
1032 			bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1033 		ahafreeccb(aha, accb);
1034 		xpt_done(ccb);
1035 		return;
1036 	}
1037 
1038 	accb->flags = ACCB_ACTIVE;
1039 	ccb->ccb_h.status |= CAM_SIM_QUEUED;
1040 	LIST_INSERT_HEAD(&aha->pending_ccbs, &ccb->ccb_h, sim_links.le);
1041 
1042 	callout_reset_sbt(&accb->timer, SBT_1MS * ccb->ccb_h.timeout, 0,
1043 	    ahatimeout, accb, 0);
1044 
1045 	/* Tell the adapter about this command */
1046 	if (aha->cur_outbox->action_code != AMBO_FREE) {
1047 		/*
1048 		 * We should never encounter a busy mailbox.
1049 		 * If we do, warn the user, and treat it as
1050 		 * a resource shortage.  If the controller is
1051 		 * hung, one of the pending transactions will
1052 		 * timeout causing us to start recovery operations.
1053 		 */
1054 		device_printf(aha->dev,
1055 		    "Encountered busy mailbox with %d out of %d "
1056 		    "commands active!!!", aha->active_ccbs, aha->max_ccbs);
1057 		callout_stop(&accb->timer);
1058 		if (nseg != 0)
1059 			bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1060 		ahafreeccb(aha, accb);
1061 		aha->resource_shortage = TRUE;
1062 		xpt_freeze_simq(aha->sim, /*count*/1);
1063 		ccb->ccb_h.status = CAM_REQUEUE_REQ;
1064 		xpt_done(ccb);
1065 		return;
1066 	}
1067 	paddr = ahaccbvtop(aha, accb);
1068 	ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1069 	aha->cur_outbox->action_code = AMBO_START;
1070 	aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1071 
1072 	ahanextoutbox(aha);
1073 }
1074 
1075 void
aha_intr(void * arg)1076 aha_intr(void *arg)
1077 {
1078 	struct	aha_softc *aha;
1079 
1080 	aha = arg;
1081 	mtx_lock(&aha->lock);
1082 	aha_intr_locked(aha);
1083 	mtx_unlock(&aha->lock);
1084 }
1085 
1086 void
aha_intr_locked(struct aha_softc * aha)1087 aha_intr_locked(struct aha_softc *aha)
1088 {
1089 	u_int	intstat;
1090 	uint32_t paddr;
1091 
1092 	while (((intstat = aha_inb(aha, INTSTAT_REG)) & INTR_PENDING) != 0) {
1093 		if ((intstat & CMD_COMPLETE) != 0) {
1094 			aha->latched_status = aha_inb(aha, STATUS_REG);
1095 			aha->command_cmp = TRUE;
1096 		}
1097 
1098 		aha_outb(aha, CONTROL_REG, RESET_INTR);
1099 
1100 		if ((intstat & IMB_LOADED) != 0) {
1101 			while (aha->cur_inbox->comp_code != AMBI_FREE) {
1102 				paddr = aha_a24tou(aha->cur_inbox->ccb_addr);
1103 				ahadone(aha, ahaccbptov(aha, paddr),
1104 				    aha->cur_inbox->comp_code);
1105 				aha->cur_inbox->comp_code = AMBI_FREE;
1106 				ahanextinbox(aha);
1107 			}
1108 		}
1109 
1110 		if ((intstat & SCSI_BUS_RESET) != 0) {
1111 			ahareset(aha, /*hardreset*/FALSE);
1112 		}
1113 	}
1114 }
1115 
1116 static void
ahadone(struct aha_softc * aha,struct aha_ccb * accb,aha_mbi_comp_code_t comp_code)1117 ahadone(struct aha_softc *aha, struct aha_ccb *accb, aha_mbi_comp_code_t comp_code)
1118 {
1119 	union  ccb	  *ccb;
1120 	struct ccb_scsiio *csio;
1121 
1122 	ccb = accb->ccb;
1123 	csio = &accb->ccb->csio;
1124 
1125 	if ((accb->flags & ACCB_ACTIVE) == 0) {
1126 		device_printf(aha->dev,
1127 		    "ahadone - Attempt to free non-active ACCB %p\n",
1128 		    (void *)accb);
1129 		return;
1130 	}
1131 
1132 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1133 		bus_dmasync_op_t op;
1134 
1135 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1136 			op = BUS_DMASYNC_POSTREAD;
1137 		else
1138 			op = BUS_DMASYNC_POSTWRITE;
1139 		bus_dmamap_sync(aha->buffer_dmat, accb->dmamap, op);
1140 		bus_dmamap_unload(aha->buffer_dmat, accb->dmamap);
1141 	}
1142 
1143 	if (accb == aha->recovery_accb) {
1144 		/*
1145 		 * The recovery ACCB does not have a CCB associated
1146 		 * with it, so short circuit the normal error handling.
1147 		 * We now traverse our list of pending CCBs and process
1148 		 * any that were terminated by the recovery CCBs action.
1149 		 * We also reinstate timeouts for all remaining, pending,
1150 		 * CCBs.
1151 		 */
1152 		struct cam_path *path;
1153 		struct ccb_hdr *ccb_h;
1154 		cam_status error;
1155 
1156 		/* Notify all clients that a BDR occurred */
1157 		error = xpt_create_path(&path, /*periph*/NULL,
1158 		    cam_sim_path(aha->sim), accb->hccb.target,
1159 		    CAM_LUN_WILDCARD);
1160 
1161 		if (error == CAM_REQ_CMP) {
1162 			xpt_async(AC_SENT_BDR, path, NULL);
1163 			xpt_free_path(path);
1164 		}
1165 
1166 		ccb_h = LIST_FIRST(&aha->pending_ccbs);
1167 		while (ccb_h != NULL) {
1168 			struct aha_ccb *pending_accb;
1169 
1170 			pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1171 			if (pending_accb->hccb.target == accb->hccb.target) {
1172 				pending_accb->hccb.ahastat = AHASTAT_HA_BDR;
1173 				ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1174 				ahadone(aha, pending_accb, AMBI_ERROR);
1175 			} else {
1176 				callout_reset_sbt(&pending_accb->timer,
1177 				    SBT_1MS * ccb_h->timeout, 0, ahatimeout,
1178 				    pending_accb, 0);
1179 				ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1180 			}
1181 		}
1182 		device_printf(aha->dev, "No longer in timeout\n");
1183 		return;
1184 	}
1185 
1186 	callout_stop(&accb->timer);
1187 
1188 	switch (comp_code) {
1189 	case AMBI_FREE:
1190 		device_printf(aha->dev,
1191 		    "ahadone - CCB completed with free status!\n");
1192 		break;
1193 	case AMBI_NOT_FOUND:
1194 		device_printf(aha->dev,
1195 		    "ahadone - CCB Abort failed to find CCB\n");
1196 		break;
1197 	case AMBI_ABORT:
1198 	case AMBI_ERROR:
1199 		/* An error occurred */
1200 		if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1201 			csio->resid = 0;
1202 		else
1203 			csio->resid = aha_a24tou(accb->hccb.data_len);
1204 		switch(accb->hccb.ahastat) {
1205 		case AHASTAT_DATARUN_ERROR:
1206 		{
1207 			if (csio->resid <= 0) {
1208 				csio->ccb_h.status = CAM_DATA_RUN_ERR;
1209 				break;
1210 			}
1211 			/* FALLTHROUGH */
1212 		}
1213 		case AHASTAT_NOERROR:
1214 			csio->scsi_status = accb->hccb.sdstat;
1215 			csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1216 			switch(csio->scsi_status) {
1217 			case SCSI_STATUS_CHECK_COND:
1218 			case SCSI_STATUS_CMD_TERMINATED:
1219 				csio->ccb_h.status |= CAM_AUTOSNS_VALID;
1220 				/*
1221 				 * The aha writes the sense data at different
1222 				 * offsets based on the scsi cmd len
1223 				 */
1224 				bcopy((caddr_t) &accb->hccb.scsi_cdb +
1225 				    accb->hccb.cmd_len,
1226 				    (caddr_t) &csio->sense_data,
1227 				    accb->hccb.sense_len);
1228 				break;
1229 			default:
1230 				break;
1231 			case SCSI_STATUS_OK:
1232 				csio->ccb_h.status = CAM_REQ_CMP;
1233 				break;
1234 			}
1235 			break;
1236 		case AHASTAT_SELTIMEOUT:
1237 			csio->ccb_h.status = CAM_SEL_TIMEOUT;
1238 			break;
1239 		case AHASTAT_UNEXPECTED_BUSFREE:
1240 			csio->ccb_h.status = CAM_UNEXP_BUSFREE;
1241 			break;
1242 		case AHASTAT_INVALID_PHASE:
1243 			csio->ccb_h.status = CAM_SEQUENCE_FAIL;
1244 			break;
1245 		case AHASTAT_INVALID_ACTION_CODE:
1246 			panic("%s: Inavlid Action code", aha_name(aha));
1247 			break;
1248 		case AHASTAT_INVALID_OPCODE:
1249 			if (accb->hccb.opcode < INITIATOR_CCB_WRESID)
1250 				panic("%s: Invalid CCB Opcode %x hccb = %p",
1251 				    aha_name(aha), accb->hccb.opcode,
1252 				    &accb->hccb);
1253 			device_printf(aha->dev,
1254 			    "AHA-1540A compensation failed\n");
1255 			xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1256 			csio->ccb_h.status = CAM_REQUEUE_REQ;
1257 			break;
1258 		case AHASTAT_LINKED_CCB_LUN_MISMATCH:
1259 			/* We don't even support linked commands... */
1260 			panic("%s: Linked CCB Lun Mismatch", aha_name(aha));
1261 			break;
1262 		case AHASTAT_INVALID_CCB_OR_SG_PARAM:
1263 			panic("%s: Invalid CCB or SG list", aha_name(aha));
1264 			break;
1265 		case AHASTAT_HA_SCSI_BUS_RESET:
1266 			if ((csio->ccb_h.status & CAM_STATUS_MASK)
1267 			    != CAM_CMD_TIMEOUT)
1268 				csio->ccb_h.status = CAM_SCSI_BUS_RESET;
1269 			break;
1270 		case AHASTAT_HA_BDR:
1271 			if ((accb->flags & ACCB_DEVICE_RESET) == 0)
1272 				csio->ccb_h.status = CAM_BDR_SENT;
1273 			else
1274 				csio->ccb_h.status = CAM_CMD_TIMEOUT;
1275 			break;
1276 		}
1277 		if (csio->ccb_h.status != CAM_REQ_CMP) {
1278 			xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
1279 			csio->ccb_h.status |= CAM_DEV_QFRZN;
1280 		}
1281 		if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1282 			ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1283 		ahafreeccb(aha, accb);
1284 		xpt_done(ccb);
1285 		break;
1286 	case AMBI_OK:
1287 		/* All completed without incident */
1288 		/* XXX DO WE NEED TO COPY SENSE BYTES HERE???? XXX */
1289 		/* I don't think so since it works???? */
1290 		ccb->ccb_h.status |= CAM_REQ_CMP;
1291 		if ((accb->flags & ACCB_RELEASE_SIMQ) != 0)
1292 			ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1293 		ahafreeccb(aha, accb);
1294 		xpt_done(ccb);
1295 		break;
1296 	}
1297 }
1298 
1299 static int
ahareset(struct aha_softc * aha,int hard_reset)1300 ahareset(struct aha_softc* aha, int hard_reset)
1301 {
1302 	struct	 ccb_hdr *ccb_h;
1303 	u_int	 status;
1304 	u_int	 timeout;
1305 	uint8_t reset_type;
1306 
1307 	if (hard_reset != 0)
1308 		reset_type = HARD_RESET;
1309 	else
1310 		reset_type = SOFT_RESET;
1311 	aha_outb(aha, CONTROL_REG, reset_type);
1312 
1313 	/* Wait 5sec. for Diagnostic start */
1314 	timeout = 5 * 10000;
1315 	while (--timeout) {
1316 		status = aha_inb(aha, STATUS_REG);
1317 		if ((status & DIAG_ACTIVE) != 0)
1318 			break;
1319 		DELAY(100);
1320 	}
1321 	if (timeout == 0) {
1322 		PRVERB((aha->dev, "ahareset - Diagnostic Active failed to "
1323 		    "assert. status = %#x\n", status));
1324 		return (ETIMEDOUT);
1325 	}
1326 
1327 	/* Wait 10sec. for Diagnostic end */
1328 	timeout = 10 * 10000;
1329 	while (--timeout) {
1330 		status = aha_inb(aha, STATUS_REG);
1331 		if ((status & DIAG_ACTIVE) == 0)
1332 			break;
1333 		DELAY(100);
1334 	}
1335 	if (timeout == 0) {
1336 		panic("%s: ahareset - Diagnostic Active failed to drop. "
1337 		    "status = 0x%x\n", aha_name(aha), status);
1338 		return (ETIMEDOUT);
1339 	}
1340 
1341 	/* Wait for the host adapter to become ready or report a failure */
1342 	timeout = 10000;
1343 	while (--timeout) {
1344 		status = aha_inb(aha, STATUS_REG);
1345 		if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
1346 			break;
1347 		DELAY(100);
1348 	}
1349 	if (timeout == 0) {
1350 		device_printf(aha->dev, "ahareset - Host adapter failed to "
1351 		    "come ready. status = 0x%x\n", status);
1352 		return (ETIMEDOUT);
1353 	}
1354 
1355 	/* If the diagnostics failed, tell the user */
1356 	if ((status & DIAG_FAIL) != 0
1357 	 || (status & HA_READY) == 0) {
1358 		device_printf(aha->dev, "ahareset - Adapter failed diag\n");
1359 
1360 		if ((status & DATAIN_REG_READY) != 0)
1361 			device_printf(aha->dev, "ahareset - Host Adapter "
1362 			    "Error code = 0x%x\n", aha_inb(aha, DATAIN_REG));
1363 		return (ENXIO);
1364 	}
1365 
1366 	/* If we've attached to the XPT, tell it about the event */
1367 	if (aha->path != NULL)
1368 		xpt_async(AC_BUS_RESET, aha->path, NULL);
1369 
1370 	/*
1371 	 * Perform completion processing for all outstanding CCBs.
1372 	 */
1373 	while ((ccb_h = LIST_FIRST(&aha->pending_ccbs)) != NULL) {
1374 		struct aha_ccb *pending_accb;
1375 
1376 		pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1377 		pending_accb->hccb.ahastat = AHASTAT_HA_SCSI_BUS_RESET;
1378 		ahadone(aha, pending_accb, AMBI_ERROR);
1379 	}
1380 
1381 	/* If we've allocated mailboxes, initialize them */
1382 	/* Must be done after we've aborted our queue, or aha_cmd fails */
1383 	if (aha->init_level > 4)
1384 		ahainitmboxes(aha);
1385 
1386 	return (0);
1387 }
1388 
1389 /*
1390  * Send a command to the adapter.
1391  */
1392 int
aha_cmd(struct aha_softc * aha,aha_op_t opcode,uint8_t * params,u_int param_len,uint8_t * reply_data,u_int reply_len,u_int cmd_timeout)1393 aha_cmd(struct aha_softc *aha, aha_op_t opcode, uint8_t *params,
1394 	u_int param_len, uint8_t *reply_data, u_int reply_len,
1395 	u_int cmd_timeout)
1396 {
1397 	u_int	timeout;
1398 	u_int	status;
1399 	u_int	saved_status;
1400 	u_int	intstat;
1401 	u_int	reply_buf_size;
1402 	int	cmd_complete;
1403 	int	error;
1404 
1405 	/* No data returned to start */
1406 	reply_buf_size = reply_len;
1407 	reply_len = 0;
1408 	intstat = 0;
1409 	cmd_complete = 0;
1410 	saved_status = 0;
1411 	error = 0;
1412 
1413 	/*
1414 	 * All commands except for the "start mailbox" and the "enable
1415 	 * outgoing mailbox read interrupt" commands cannot be issued
1416 	 * while there are pending transactions.  Freeze our SIMQ
1417 	 * and wait for all completions to occur if necessary.
1418 	 */
1419 	timeout = 10000;
1420 	while (LIST_FIRST(&aha->pending_ccbs) != NULL && --timeout) {
1421 		/* Fire the interrupt handler in case interrupts are blocked */
1422 		aha_intr(aha);
1423 		DELAY(10);
1424 	}
1425 
1426 	if (timeout == 0) {
1427 		device_printf(aha->dev,
1428 		    "aha_cmd: Timeout waiting for adapter idle\n");
1429 		return (ETIMEDOUT);
1430 	}
1431 	aha->command_cmp = 0;
1432 	/*
1433 	 * Wait up to 10 sec. for the adapter to become
1434 	 * ready to accept commands.
1435 	 */
1436 	timeout = 100000;
1437 	while (--timeout) {
1438 		status = aha_inb(aha, STATUS_REG);
1439 		if ((status & HA_READY) != 0 && (status & CMD_REG_BUSY) == 0)
1440 			break;
1441 		/*
1442 		 * Throw away any pending data which may be
1443 		 * left over from earlier commands that we
1444 		 * timedout on.
1445 		 */
1446 		if ((status & DATAIN_REG_READY) != 0)
1447 			(void)aha_inb(aha, DATAIN_REG);
1448 		DELAY(100);
1449 	}
1450 	if (timeout == 0) {
1451 		device_printf(aha->dev, "aha_cmd: Timeout waiting for adapter"
1452 		    " ready, status = 0x%x\n", status);
1453 		return (ETIMEDOUT);
1454 	}
1455 
1456 	/*
1457 	 * Send the opcode followed by any necessary parameter bytes.
1458 	 */
1459 	aha_outb(aha, COMMAND_REG, opcode);
1460 
1461 	/*
1462 	 * Wait for up to 1sec to get the parameter list sent
1463 	 */
1464 	timeout = 10000;
1465 	while (param_len && --timeout) {
1466 		DELAY(100);
1467 		status = aha_inb(aha, STATUS_REG);
1468 		intstat = aha_inb(aha, INTSTAT_REG);
1469 
1470 		if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1471 		 == (INTR_PENDING|CMD_COMPLETE)) {
1472 			saved_status = status;
1473 			cmd_complete = 1;
1474 			break;
1475 		}
1476 
1477 		if (aha->command_cmp != 0) {
1478 			saved_status = aha->latched_status;
1479 			cmd_complete = 1;
1480 			break;
1481 		}
1482 		if ((status & DATAIN_REG_READY) != 0)
1483 			break;
1484 		if ((status & CMD_REG_BUSY) == 0) {
1485 			aha_outb(aha, COMMAND_REG, *params++);
1486 			param_len--;
1487 			timeout = 10000;
1488 		}
1489 	}
1490 	if (timeout == 0) {
1491 		device_printf(aha->dev, "aha_cmd: Timeout sending parameters, "
1492 		    "status = 0x%x\n", status);
1493 		error = ETIMEDOUT;
1494 	}
1495 
1496 	/*
1497 	 * For all other commands, we wait for any output data
1498 	 * and the final comand completion interrupt.
1499 	 */
1500 	while (cmd_complete == 0 && --cmd_timeout) {
1501 
1502 		status = aha_inb(aha, STATUS_REG);
1503 		intstat = aha_inb(aha, INTSTAT_REG);
1504 
1505 		if (aha->command_cmp != 0) {
1506 			cmd_complete = 1;
1507 			saved_status = aha->latched_status;
1508 		} else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1509 			== (INTR_PENDING|CMD_COMPLETE)) {
1510 			/*
1511 			 * Our poll (in case interrupts are blocked)
1512 			 * saw the CMD_COMPLETE interrupt.
1513 			 */
1514 			cmd_complete = 1;
1515 			saved_status = status;
1516 		}
1517 		if ((status & DATAIN_REG_READY) != 0) {
1518 			uint8_t data;
1519 
1520 			data = aha_inb(aha, DATAIN_REG);
1521 			if (reply_len < reply_buf_size) {
1522 				*reply_data++ = data;
1523 			} else {
1524 				device_printf(aha->dev,
1525 				    "aha_cmd - Discarded reply data "
1526 				    "byte for opcode 0x%x\n", opcode);
1527 			}
1528 			/*
1529 			 * Reset timeout to ensure at least a second
1530 			 * between response bytes.
1531 			 */
1532 			cmd_timeout = MAX(cmd_timeout, 10000);
1533 			reply_len++;
1534 		}
1535 		DELAY(100);
1536 	}
1537 	if (cmd_timeout == 0) {
1538 		device_printf(aha->dev, "aha_cmd: Timeout: status = 0x%x, "
1539 		    "intstat = 0x%x, reply_len = %d\n", status, intstat,
1540 		    reply_len);
1541 		return (ETIMEDOUT);
1542 	}
1543 
1544 	/*
1545 	 * Clear any pending interrupts.  Block interrupts so our
1546 	 * interrupt handler is not re-entered.
1547 	 */
1548 	aha_intr(aha);
1549 
1550 	if (error != 0)
1551 		return (error);
1552 
1553 	/*
1554 	 * If the command was rejected by the controller, tell the caller.
1555 	 */
1556 	if ((saved_status & CMD_INVALID) != 0) {
1557 		PRVERB((aha->dev, "Invalid Command 0x%x\n", opcode));
1558 		/*
1559 		 * Some early adapters may not recover properly from
1560 		 * an invalid command.  If it appears that the controller
1561 		 * has wedged (i.e. status was not cleared by our interrupt
1562 		 * reset above), perform a soft reset.
1563       		 */
1564 		DELAY(1000);
1565 		status = aha_inb(aha, STATUS_REG);
1566 		if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
1567 			      CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
1568 		 || (status & (HA_READY|INIT_REQUIRED))
1569 		  != (HA_READY|INIT_REQUIRED))
1570 			ahareset(aha, /*hard_reset*/FALSE);
1571 		return (EINVAL);
1572 	}
1573 
1574 	if (param_len > 0) {
1575 		/* The controller did not accept the full argument list */
1576 		PRVERB((aha->dev, "Controller did not accept full argument "
1577 		    "list (%d > 0)\n", param_len));
1578 	 	return (E2BIG);
1579 	}
1580 
1581 	if (reply_len != reply_buf_size) {
1582 		/* Too much or too little data received */
1583 		PRVERB((aha->dev, "data received mismatch (%d != %d)\n",
1584 		    reply_len, reply_buf_size));
1585 		return (EMSGSIZE);
1586 	}
1587 
1588 	/* We were successful */
1589 	return (0);
1590 }
1591 
1592 static int
ahainitmboxes(struct aha_softc * aha)1593 ahainitmboxes(struct aha_softc *aha)
1594 {
1595 	int error;
1596 	init_24b_mbox_params_t init_mbox;
1597 
1598 	bzero(aha->in_boxes, sizeof(aha_mbox_in_t) * aha->num_boxes);
1599 	bzero(aha->out_boxes, sizeof(aha_mbox_out_t) * aha->num_boxes);
1600 	aha->cur_inbox = aha->in_boxes;
1601 	aha->last_inbox = aha->in_boxes + aha->num_boxes - 1;
1602 	aha->cur_outbox = aha->out_boxes;
1603 	aha->last_outbox = aha->out_boxes + aha->num_boxes - 1;
1604 
1605 	/* Tell the adapter about them */
1606 	init_mbox.num_mboxes = aha->num_boxes;
1607 	ahautoa24(aha->mailbox_physbase, init_mbox.base_addr);
1608 	error = aha_cmd(aha, AOP_INITIALIZE_MBOX, (uint8_t *)&init_mbox,
1609 	    /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
1610 	    /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
1611 
1612 	if (error != 0)
1613 		printf("ahainitmboxes: Initialization command failed\n");
1614 	return (error);
1615 }
1616 
1617 /*
1618  * Update the XPT's idea of the negotiated transfer
1619  * parameters for a particular target.
1620  */
1621 static void
ahafetchtransinfo(struct aha_softc * aha,struct ccb_trans_settings * cts)1622 ahafetchtransinfo(struct aha_softc *aha, struct ccb_trans_settings* cts)
1623 {
1624 	setup_data_t	setup_info;
1625 	u_int		target;
1626 	u_int		targ_offset;
1627 	u_int		sync_period;
1628 	int		error;
1629 	uint8_t	param;
1630 	targ_syncinfo_t	sync_info;
1631 	struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
1632 
1633 	target = cts->ccb_h.target_id;
1634 	targ_offset = (target & 0x7);
1635 
1636 	/*
1637 	 * Inquire Setup Information.  This command retreives
1638 	 * the sync info for older models.
1639 	 */
1640 	param = sizeof(setup_info);
1641 	error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, &param, /*paramlen*/1,
1642 	    (uint8_t*)&setup_info, sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
1643 
1644 	if (error != 0) {
1645 		device_printf(aha->dev,
1646 		    "ahafetchtransinfo - Inquire Setup Info Failed %d\n",
1647 		    error);
1648 		return;
1649 	}
1650 
1651 	sync_info = setup_info.syncinfo[targ_offset];
1652 
1653 	if (sync_info.sync == 0)
1654 		spi->sync_offset = 0;
1655 	else
1656 		spi->sync_offset = sync_info.offset;
1657 
1658 	spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1659 
1660 	if (aha->boardid >= BOARD_1542CF)
1661 		sync_period = 1000;
1662 	else
1663 		sync_period = 2000;
1664 	sync_period += 500 * sync_info.period;
1665 
1666 	/* Convert ns value to standard SCSI sync rate */
1667 	if (spi->sync_offset != 0)
1668 		spi->sync_period = scsi_calc_syncparam(sync_period);
1669 	else
1670 		spi->sync_period = 0;
1671 
1672 	spi->valid = CTS_SPI_VALID_SYNC_RATE
1673 		   | CTS_SPI_VALID_SYNC_OFFSET
1674 		   | CTS_SPI_VALID_BUS_WIDTH;
1675         xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
1676 }
1677 
1678 static void
ahamapmboxes(void * arg,bus_dma_segment_t * segs,int nseg,int error)1679 ahamapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1680 {
1681 	struct aha_softc* aha;
1682 
1683 	aha = (struct aha_softc*)arg;
1684 	aha->mailbox_physbase = segs->ds_addr;
1685 }
1686 
1687 static void
ahamapccbs(void * arg,bus_dma_segment_t * segs,int nseg,int error)1688 ahamapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1689 {
1690 	struct aha_softc* aha;
1691 
1692 	aha = (struct aha_softc*)arg;
1693 	aha->aha_ccb_physbase = segs->ds_addr;
1694 }
1695 
1696 static void
ahamapsgs(void * arg,bus_dma_segment_t * segs,int nseg,int error)1697 ahamapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1698 {
1699 
1700 	struct aha_softc* aha;
1701 
1702 	aha = (struct aha_softc*)arg;
1703 	SLIST_FIRST(&aha->sg_maps)->sg_physaddr = segs->ds_addr;
1704 }
1705 
1706 static void
ahapoll(struct cam_sim * sim)1707 ahapoll(struct cam_sim *sim)
1708 {
1709 	aha_intr_locked(cam_sim_softc(sim));
1710 }
1711 
1712 static void
ahatimeout(void * arg)1713 ahatimeout(void *arg)
1714 {
1715 	struct aha_ccb	*accb;
1716 	union  ccb	*ccb;
1717 	struct aha_softc *aha;
1718 	uint32_t	paddr;
1719 	struct ccb_hdr *ccb_h;
1720 
1721 	accb = (struct aha_ccb *)arg;
1722 	ccb = accb->ccb;
1723 	aha = (struct aha_softc *)ccb->ccb_h.ccb_aha_ptr;
1724 	mtx_assert(&aha->lock, MA_OWNED);
1725 	xpt_print_path(ccb->ccb_h.path);
1726 	printf("CCB %p - timed out\n", (void *)accb);
1727 
1728 	if ((accb->flags & ACCB_ACTIVE) == 0) {
1729 		xpt_print_path(ccb->ccb_h.path);
1730 		printf("CCB %p - timed out CCB already completed\n",
1731 		    (void *)accb);
1732 		return;
1733 	}
1734 
1735 	/*
1736 	 * In order to simplify the recovery process, we ask the XPT
1737 	 * layer to halt the queue of new transactions and we traverse
1738 	 * the list of pending CCBs and remove their timeouts. This
1739 	 * means that the driver attempts to clear only one error
1740 	 * condition at a time.  In general, timeouts that occur
1741 	 * close together are related anyway, so there is no benefit
1742 	 * in attempting to handle errors in parallel.  Timeouts will
1743 	 * be reinstated when the recovery process ends.
1744 	 */
1745 	if ((accb->flags & ACCB_DEVICE_RESET) == 0) {
1746 		if ((accb->flags & ACCB_RELEASE_SIMQ) == 0) {
1747 			xpt_freeze_simq(aha->sim, /*count*/1);
1748 			accb->flags |= ACCB_RELEASE_SIMQ;
1749 		}
1750 
1751 		ccb_h = LIST_FIRST(&aha->pending_ccbs);
1752 		while (ccb_h != NULL) {
1753 			struct aha_ccb *pending_accb;
1754 
1755 			pending_accb = (struct aha_ccb *)ccb_h->ccb_accb_ptr;
1756 			callout_stop(&pending_accb->timer);
1757 			ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1758 		}
1759 	}
1760 
1761 	if ((accb->flags & ACCB_DEVICE_RESET) != 0
1762 	 || aha->cur_outbox->action_code != AMBO_FREE) {
1763 		/*
1764 		 * Try a full host adapter/SCSI bus reset.
1765 		 * We do this only if we have already attempted
1766 		 * to clear the condition with a BDR, or we cannot
1767 		 * attempt a BDR for lack of mailbox resources.
1768 		 */
1769 		ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1770 		ahareset(aha, /*hardreset*/TRUE);
1771 		device_printf(aha->dev, "No longer in timeout\n");
1772 	} else {
1773 		/*
1774 		 * Send a Bus Device Reset message:
1775 		 * The target that is holding up the bus may not
1776 		 * be the same as the one that triggered this timeout
1777 		 * (different commands have different timeout lengths),
1778 		 * but we have no way of determining this from our
1779 		 * timeout handler.  Our strategy here is to queue a
1780 		 * BDR message to the target of the timed out command.
1781 		 * If this fails, we'll get another timeout 2 seconds
1782 		 * later which will attempt a bus reset.
1783 		 */
1784 		accb->flags |= ACCB_DEVICE_RESET;
1785 		callout_reset(&accb->timer, 2 * hz, ahatimeout, accb);
1786 		aha->recovery_accb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
1787 
1788 		/* No Data Transfer */
1789 		aha->recovery_accb->hccb.datain = TRUE;
1790 		aha->recovery_accb->hccb.dataout = TRUE;
1791 		aha->recovery_accb->hccb.ahastat = 0;
1792 		aha->recovery_accb->hccb.sdstat = 0;
1793 		aha->recovery_accb->hccb.target = ccb->ccb_h.target_id;
1794 
1795 		/* Tell the adapter about this command */
1796 		paddr = ahaccbvtop(aha, aha->recovery_accb);
1797 		ahautoa24(paddr, aha->cur_outbox->ccb_addr);
1798 		aha->cur_outbox->action_code = AMBO_START;
1799 		aha_outb(aha, COMMAND_REG, AOP_START_MBOX);
1800 		ahanextoutbox(aha);
1801 	}
1802 }
1803 
1804 int
aha_detach(struct aha_softc * aha)1805 aha_detach(struct aha_softc *aha)
1806 {
1807 	mtx_lock(&aha->lock);
1808 	xpt_async(AC_LOST_DEVICE, aha->path, NULL);
1809 	xpt_free_path(aha->path);
1810 	xpt_bus_deregister(cam_sim_path(aha->sim));
1811 	cam_sim_free(aha->sim, /*free_devq*/TRUE);
1812 	mtx_unlock(&aha->lock);
1813 	/* XXX: Drain all timers? */
1814 	return (0);
1815 }
1816 MODULE_DEPEND(aha, cam, 1, 1, 1);
1817