xref: /dflybsd-src/sys/dev/disk/buslogic/bt.c (revision fda7d3889b1114d34ad3a52a7257a2b80fe24e4c)
1 /*
2  * Generic driver for the BusLogic MultiMaster SCSI host adapters
3  * Product specific probe and attach routines can be found in:
4  * sys/dev/buslogic/bt_pci.c	BT-946, BT-948, BT-956, BT-958 cards
5  *
6  * Copyright (c) 1998, 1999 Justin T. Gibbs.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions, and the following disclaimer,
14  *    without modification, immediately at the beginning of the file.
15  * 2. The name of the author may not be used to endorse or promote products
16  *    derived from this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * $FreeBSD: src/sys/dev/buslogic/bt.c,v 1.25.2.1 2000/08/02 22:32:26 peter Exp $
31  */
32 
33  /*
34   * Special thanks to Leonard N. Zubkoff for writing such a complete and
35   * well documented Mylex/BusLogic MultiMaster driver for Linux.  Support
36   * in this driver for the wide range of MultiMaster controllers and
37   * firmware revisions, with their otherwise undocumented quirks, would not
38   * have been possible without his efforts.
39   */
40 
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/malloc.h>
44 #include <sys/buf.h>
45 #include <sys/kernel.h>
46 #include <sys/sysctl.h>
47 #include <sys/bus.h>
48 #include <sys/rman.h>
49 #include <sys/thread2.h>
50 
51 #include <machine/clock.h>
52 
53 #include <bus/cam/cam.h>
54 #include <bus/cam/cam_ccb.h>
55 #include <bus/cam/cam_sim.h>
56 #include <bus/cam/cam_xpt_sim.h>
57 #include <bus/cam/cam_debug.h>
58 #include <bus/cam/scsi/scsi_message.h>
59 
60 #include <vm/vm.h>
61 #include <vm/pmap.h>
62 
63 #include "btreg.h"
64 
65 /* MailBox Management functions */
66 static __inline void	btnextinbox(struct bt_softc *bt);
67 static __inline void	btnextoutbox(struct bt_softc *bt);
68 
69 static __inline void
70 btnextinbox(struct bt_softc *bt)
71 {
72 	if (bt->cur_inbox == bt->last_inbox)
73 		bt->cur_inbox = bt->in_boxes;
74 	else
75 		bt->cur_inbox++;
76 }
77 
78 static __inline void
79 btnextoutbox(struct bt_softc *bt)
80 {
81 	if (bt->cur_outbox == bt->last_outbox)
82 		bt->cur_outbox = bt->out_boxes;
83 	else
84 		bt->cur_outbox++;
85 }
86 
87 /* CCB Mangement functions */
88 static __inline u_int32_t		btccbvtop(struct bt_softc *bt,
89 						  struct bt_ccb *bccb);
90 static __inline struct bt_ccb*		btccbptov(struct bt_softc *bt,
91 						  u_int32_t ccb_addr);
92 static __inline u_int32_t		btsensepaddr(struct bt_softc *bt,
93 						     struct bt_ccb *bccb);
94 static __inline struct scsi_sense_data* btsensevaddr(struct bt_softc *bt,
95 						     struct bt_ccb *bccb);
96 
97 static __inline u_int32_t
98 btccbvtop(struct bt_softc *bt, struct bt_ccb *bccb)
99 {
100 	return (bt->bt_ccb_physbase
101 	      + (u_int32_t)((caddr_t)bccb - (caddr_t)bt->bt_ccb_array));
102 }
103 
104 static __inline struct bt_ccb *
105 btccbptov(struct bt_softc *bt, u_int32_t ccb_addr)
106 {
107 	return (bt->bt_ccb_array +
108 	        ((struct bt_ccb*)(uintptr_t)ccb_addr-(struct bt_ccb*)(uintptr_t)bt->bt_ccb_physbase));
109 }
110 
111 static __inline u_int32_t
112 btsensepaddr(struct bt_softc *bt, struct bt_ccb *bccb)
113 {
114 	u_int index;
115 
116 	index = (u_int)(bccb - bt->bt_ccb_array);
117 	return (bt->sense_buffers_physbase
118 		+ (index * sizeof(struct scsi_sense_data)));
119 }
120 
121 static __inline struct scsi_sense_data *
122 btsensevaddr(struct bt_softc *bt, struct bt_ccb *bccb)
123 {
124 	u_int index;
125 
126 	index = (u_int)(bccb - bt->bt_ccb_array);
127 	return (bt->sense_buffers + index);
128 }
129 
130 static __inline struct bt_ccb*	btgetccb(struct bt_softc *bt);
131 static __inline void		btfreeccb(struct bt_softc *bt,
132 					  struct bt_ccb *bccb);
133 static void		btallocccbs(struct bt_softc *bt);
134 static bus_dmamap_callback_t btexecuteccb;
135 static void		btdone(struct bt_softc *bt, struct bt_ccb *bccb,
136 			       bt_mbi_comp_code_t comp_code);
137 
138 /* Host adapter command functions */
139 static int	btreset(struct bt_softc* bt, int hard_reset);
140 
141 /* Initialization functions */
142 static int			btinitmboxes(struct bt_softc *bt);
143 static bus_dmamap_callback_t	btmapmboxes;
144 static bus_dmamap_callback_t	btmapccbs;
145 static bus_dmamap_callback_t	btmapsgs;
146 
147 /* Transfer Negotiation Functions */
148 static void btfetchtransinfo(struct bt_softc *bt,
149 			     struct ccb_trans_settings *cts);
150 
151 /* CAM SIM entry points */
152 #define ccb_bccb_ptr spriv_ptr0
153 #define ccb_bt_ptr spriv_ptr1
154 static void	btaction(struct cam_sim *sim, union ccb *ccb);
155 static void	btpoll(struct cam_sim *sim);
156 
157 /* Our timeout handler */
158 timeout_t bttimeout;
159 
160 u_long bt_unit = 0;
161 
162 /* Exported functions */
163 void
164 bt_init_softc(device_t dev, struct resource *port,
165 	      struct resource *irq, struct resource *drq)
166 {
167 	struct bt_softc *bt = device_get_softc(dev);
168 
169 	SLIST_INIT(&bt->free_bt_ccbs);
170 	LIST_INIT(&bt->pending_ccbs);
171 	SLIST_INIT(&bt->sg_maps);
172 	bt->dev = dev;
173 	bt->unit = device_get_unit(dev);
174 	bt->port = port;
175 	bt->irq = irq;
176 	bt->drq = drq;
177 	bt->tag = rman_get_bustag(port);
178 	bt->bsh = rman_get_bushandle(port);
179 }
180 
181 void
182 bt_free_softc(device_t dev)
183 {
184 	struct bt_softc *bt = device_get_softc(dev);
185 
186 	switch (bt->init_level) {
187 	default:
188 	case 11:
189 		bus_dmamap_unload(bt->sense_dmat, bt->sense_dmamap);
190 	case 10:
191 		bus_dmamem_free(bt->sense_dmat, bt->sense_buffers,
192 				bt->sense_dmamap);
193 	case 9:
194 		bus_dma_tag_destroy(bt->sense_dmat);
195 	case 8:
196 	{
197 		struct sg_map_node *sg_map;
198 
199 		while ((sg_map = SLIST_FIRST(&bt->sg_maps))!= NULL) {
200 			SLIST_REMOVE_HEAD(&bt->sg_maps, links);
201 			bus_dmamap_unload(bt->sg_dmat,
202 					  sg_map->sg_dmamap);
203 			bus_dmamem_free(bt->sg_dmat, sg_map->sg_vaddr,
204 					sg_map->sg_dmamap);
205 			kfree(sg_map, M_DEVBUF);
206 		}
207 		bus_dma_tag_destroy(bt->sg_dmat);
208 	}
209 	case 7:
210 		bus_dmamap_unload(bt->ccb_dmat, bt->ccb_dmamap);
211 	case 6:
212 		bus_dmamem_free(bt->ccb_dmat, bt->bt_ccb_array,
213 				bt->ccb_dmamap);
214 		bus_dmamap_destroy(bt->ccb_dmat, bt->ccb_dmamap);
215 	case 5:
216 		bus_dma_tag_destroy(bt->ccb_dmat);
217 	case 4:
218 		bus_dmamap_unload(bt->mailbox_dmat, bt->mailbox_dmamap);
219 	case 3:
220 		bus_dmamem_free(bt->mailbox_dmat, bt->in_boxes,
221 				bt->mailbox_dmamap);
222 		bus_dmamap_destroy(bt->mailbox_dmat, bt->mailbox_dmamap);
223 	case 2:
224 		bus_dma_tag_destroy(bt->buffer_dmat);
225 	case 1:
226 		bus_dma_tag_destroy(bt->mailbox_dmat);
227 	case 0:
228 		break;
229 	}
230 }
231 
232 /*
233  * Probe the adapter and verify that the card is a BusLogic.
234  */
235 int
236 bt_probe(device_t dev)
237 {
238 	struct bt_softc *bt = device_get_softc(dev);
239 	esetup_info_data_t esetup_info;
240 	u_int	 status;
241 	u_int	 intstat;
242 	u_int	 geometry;
243 	int	 error;
244 	u_int8_t param;
245 
246 	/*
247 	 * See if the three I/O ports look reasonable.
248 	 * Touch the minimal number of registers in the
249 	 * failure case.
250 	 */
251 	status = bt_inb(bt, STATUS_REG);
252 	if ((status == 0)
253 	 || (status & (DIAG_ACTIVE|CMD_REG_BUSY|
254 		       STATUS_REG_RSVD|CMD_INVALID)) != 0) {
255 		if (bootverbose)
256 			device_printf(dev, "Failed Status Reg Test - %x\n",
257 			       status);
258 		return (ENXIO);
259 	}
260 
261 	intstat = bt_inb(bt, INTSTAT_REG);
262 	if ((intstat & INTSTAT_REG_RSVD) != 0) {
263 		device_printf(dev, "Failed Intstat Reg Test\n");
264 		return (ENXIO);
265 	}
266 
267 	geometry = bt_inb(bt, GEOMETRY_REG);
268 	if (geometry == 0xFF) {
269 		if (bootverbose)
270 			device_printf(dev, "Failed Geometry Reg Test\n");
271 		return (ENXIO);
272 	}
273 
274 	/*
275 	 * Looking good so far.  Final test is to reset the
276 	 * adapter and attempt to fetch the extended setup
277 	 * information.  This should filter out all 1542 cards.
278 	 */
279 	if ((error = btreset(bt, /*hard_reset*/TRUE)) != 0) {
280 		if (bootverbose)
281 			device_printf(dev, "Failed Reset\n");
282 		return (ENXIO);
283 	}
284 
285 	param = sizeof(esetup_info);
286 	error = bt_cmd(bt, BOP_INQUIRE_ESETUP_INFO, &param, /*parmlen*/1,
287 		       (u_int8_t*)&esetup_info, sizeof(esetup_info),
288 		       DEFAULT_CMD_TIMEOUT);
289 	if (error != 0) {
290 		return (ENXIO);
291 	}
292 
293 	return (0);
294 }
295 
296 /*
297  * Pull the boards setup information and record it in our softc.
298  */
299 int
300 bt_fetch_adapter_info(device_t dev)
301 {
302 	struct bt_softc *bt = device_get_softc(dev);
303 	board_id_data_t	board_id;
304 	esetup_info_data_t esetup_info;
305 	config_data_t config_data;
306 	int	 error;
307 	u_int8_t length_param;
308 
309 	/* First record the firmware version */
310 	error = bt_cmd(bt, BOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0,
311 		       (u_int8_t*)&board_id, sizeof(board_id),
312 		       DEFAULT_CMD_TIMEOUT);
313 	if (error != 0) {
314 		device_printf(dev, "bt_fetch_adapter_info - Failed Get Board Info\n");
315 		return (error);
316 	}
317 	bt->firmware_ver[0] = board_id.firmware_rev_major;
318 	bt->firmware_ver[1] = '.';
319 	bt->firmware_ver[2] = board_id.firmware_rev_minor;
320 	bt->firmware_ver[3] = '\0';
321 
322 	/*
323 	 * Depending on the firmware major and minor version,
324 	 * we may be able to fetch additional minor version info.
325 	 */
326 	if (bt->firmware_ver[0] > '0') {
327 
328 		error = bt_cmd(bt, BOP_INQUIRE_FW_VER_3DIG, NULL, /*parmlen*/0,
329 			       (u_int8_t*)&bt->firmware_ver[3], 1,
330 			       DEFAULT_CMD_TIMEOUT);
331 		if (error != 0) {
332 			device_printf(dev,
333 				      "bt_fetch_adapter_info - Failed Get "
334 				      "Firmware 3rd Digit\n");
335 			return (error);
336 		}
337 		if (bt->firmware_ver[3] == ' ')
338 			bt->firmware_ver[3] = '\0';
339 		bt->firmware_ver[4] = '\0';
340 	}
341 
342 	if (strcmp(bt->firmware_ver, "3.3") >= 0) {
343 
344 		error = bt_cmd(bt, BOP_INQUIRE_FW_VER_4DIG, NULL, /*parmlen*/0,
345 			       (u_int8_t*)&bt->firmware_ver[4], 1,
346 			       DEFAULT_CMD_TIMEOUT);
347 		if (error != 0) {
348 			device_printf(dev,
349 				      "bt_fetch_adapter_info - Failed Get "
350 				      "Firmware 4th Digit\n");
351 			return (error);
352 		}
353 		if (bt->firmware_ver[4] == ' ')
354 			bt->firmware_ver[4] = '\0';
355 		bt->firmware_ver[5] = '\0';
356 	}
357 
358 	/*
359 	 * Some boards do not handle the "recently documented"
360 	 * Inquire Board Model Number command correctly or do not give
361 	 * exact information.  Use the Firmware and Extended Setup
362 	 * information in these cases to come up with the right answer.
363 	 * The major firmware revision number indicates:
364 	 *
365 	 * 	5.xx	BusLogic "W" Series Host Adapters:
366 	 *		BT-948/958/958D
367 	 *	4.xx	BusLogic "C" Series Host Adapters:
368 	 *		BT-946C/956C/956CD/747C/757C/757CD/445C/545C/540CF
369 	 *	3.xx	BusLogic "S" Series Host Adapters:
370 	 *		BT-747S/747D/757S/757D/445S/545S/542D
371 	 *		BT-542B/742A (revision H)
372 	 *	2.xx	BusLogic "A" Series Host Adapters:
373 	 *		BT-542B/742A (revision G and below)
374 	 */
375 	length_param = sizeof(esetup_info);
376 	error = bt_cmd(bt, BOP_INQUIRE_ESETUP_INFO, &length_param, /*parmlen*/1,
377 		       (u_int8_t*)&esetup_info, sizeof(esetup_info),
378 		       DEFAULT_CMD_TIMEOUT);
379 	if (error != 0) {
380 		return (error);
381 	}
382 
383   	bt->bios_addr = esetup_info.bios_addr << 12;
384 
385 	if (esetup_info.bus_type == 'A'
386 	 && bt->firmware_ver[0] == '2') {
387 		ksnprintf(bt->model, sizeof(bt->model), "542B");
388 	} else if (esetup_info.bus_type == 'E'
389 		&& (strncmp(bt->firmware_ver, "2.1", 3) == 0
390 		 || strncmp(bt->firmware_ver, "2.20", 4) == 0)) {
391 		ksnprintf(bt->model, sizeof(bt->model), "742A");
392 	} else {
393 		ha_model_data_t model_data;
394 		int i;
395 
396 		length_param = sizeof(model_data);
397 		error = bt_cmd(bt, BOP_INQUIRE_MODEL, &length_param, 1,
398 			       (u_int8_t*)&model_data, sizeof(model_data),
399 			       DEFAULT_CMD_TIMEOUT);
400 		if (error != 0) {
401 			device_printf(dev,
402 				      "bt_fetch_adapter_info - Failed Inquire "
403 				      "Model Number\n");
404 			return (error);
405 		}
406 		for (i = 0; i < sizeof(model_data.ascii_model); i++) {
407 			bt->model[i] = model_data.ascii_model[i];
408 			if (bt->model[i] == ' ')
409 				break;
410 		}
411 		bt->model[i] = '\0';
412 	}
413 
414 	bt->level_trigger_ints = esetup_info.level_trigger_ints ? 1 : 0;
415 
416 	/* SG element limits */
417 	bt->max_sg = esetup_info.max_sg;
418 
419 	/* Set feature flags */
420 	bt->wide_bus = esetup_info.wide_bus;
421 	bt->diff_bus = esetup_info.diff_bus;
422 	bt->ultra_scsi = esetup_info.ultra_scsi;
423 
424 	if ((bt->firmware_ver[0] == '5')
425 	 || (bt->firmware_ver[0] == '4' && bt->wide_bus))
426 		bt->extended_lun = TRUE;
427 
428 	bt->strict_rr = (strcmp(bt->firmware_ver, "3.31") >= 0);
429 
430 	bt->extended_trans =
431 	    ((bt_inb(bt, GEOMETRY_REG) & EXTENDED_TRANSLATION) != 0);
432 
433 	/*
434 	 * Determine max CCB count and whether tagged queuing is
435 	 * available based on controller type. Tagged queuing
436 	 * only works on 'W' series adapters, 'C' series adapters
437 	 * with firmware of rev 4.42 and higher, and 'S' series
438 	 * adapters with firmware of rev 3.35 and higher.  The
439 	 * maximum CCB counts are as follows:
440 	 *
441 	 *	192	BT-948/958/958D
442 	 *	100	BT-946C/956C/956CD/747C/757C/757CD/445C
443 	 * 	50	BT-545C/540CF
444 	 * 	30	BT-747S/747D/757S/757D/445S/545S/542D/542B/742A
445 	 */
446 	if (bt->firmware_ver[0] == '5') {
447 		bt->max_ccbs = 192;
448 		bt->tag_capable = TRUE;
449 	} else if (bt->firmware_ver[0] == '4') {
450 		if (bt->model[0] == '5')
451 			bt->max_ccbs = 50;
452 		else
453 			bt->max_ccbs = 100;
454 		bt->tag_capable = (strcmp(bt->firmware_ver, "4.22") >= 0);
455 	} else {
456 		bt->max_ccbs = 30;
457 		if (bt->firmware_ver[0] == '3'
458 		 && (strcmp(bt->firmware_ver, "3.35") >= 0))
459 			bt->tag_capable = TRUE;
460 		else
461 			bt->tag_capable = FALSE;
462 	}
463 
464 	if (bt->tag_capable != FALSE)
465 		bt->tags_permitted = ALL_TARGETS;
466 
467 	/* Determine Sync/Wide/Disc settings */
468 	if (bt->firmware_ver[0] >= '4') {
469 		auto_scsi_data_t auto_scsi_data;
470 		fetch_lram_params_t fetch_lram_params;
471 		int error;
472 
473 		/*
474 		 * These settings are stored in the
475 		 * AutoSCSI data in LRAM of 'W' and 'C'
476 		 * adapters.
477 		 */
478 		fetch_lram_params.offset = AUTO_SCSI_BYTE_OFFSET;
479 		fetch_lram_params.response_len = sizeof(auto_scsi_data);
480 		error = bt_cmd(bt, BOP_FETCH_LRAM,
481 			       (u_int8_t*)&fetch_lram_params,
482 			       sizeof(fetch_lram_params),
483 			       (u_int8_t*)&auto_scsi_data,
484 			       sizeof(auto_scsi_data), DEFAULT_CMD_TIMEOUT);
485 
486 		if (error != 0) {
487 			device_printf(dev,
488 				      "bt_fetch_adapter_info - Failed "
489 				      "Get Auto SCSI Info\n");
490 			return (error);
491 		}
492 
493 		bt->disc_permitted = auto_scsi_data.low_disc_permitted
494 				   | (auto_scsi_data.high_disc_permitted << 8);
495 		bt->sync_permitted = auto_scsi_data.low_sync_permitted
496 				   | (auto_scsi_data.high_sync_permitted << 8);
497 		bt->fast_permitted = auto_scsi_data.low_fast_permitted
498 				   | (auto_scsi_data.high_fast_permitted << 8);
499 		bt->ultra_permitted = auto_scsi_data.low_ultra_permitted
500 				   | (auto_scsi_data.high_ultra_permitted << 8);
501 		bt->wide_permitted = auto_scsi_data.low_wide_permitted
502 				   | (auto_scsi_data.high_wide_permitted << 8);
503 
504 		if (bt->ultra_scsi == FALSE)
505 			bt->ultra_permitted = 0;
506 
507 		if (bt->wide_bus == FALSE)
508 			bt->wide_permitted = 0;
509 	} else {
510 		/*
511 		 * 'S' and 'A' series have this information in the setup
512 		 * information structure.
513 		 */
514 		setup_data_t	setup_info;
515 
516 		length_param = sizeof(setup_info);
517 		error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, &length_param,
518 			       /*paramlen*/1, (u_int8_t*)&setup_info,
519 			       sizeof(setup_info), DEFAULT_CMD_TIMEOUT);
520 
521 		if (error != 0) {
522 			device_printf(dev,
523 				      "bt_fetch_adapter_info - Failed "
524 				      "Get Setup Info\n");
525 			return (error);
526 		}
527 
528 		if (setup_info.initiate_sync != 0) {
529 			bt->sync_permitted = ALL_TARGETS;
530 
531 			if (bt->model[0] == '7') {
532 				if (esetup_info.sync_neg10MB != 0)
533 					bt->fast_permitted = ALL_TARGETS;
534 				if (strcmp(bt->model, "757") == 0)
535 					bt->wide_permitted = ALL_TARGETS;
536 			}
537 		}
538 		bt->disc_permitted = ALL_TARGETS;
539 	}
540 
541 	/* We need as many mailboxes as we can have ccbs */
542 	bt->num_boxes = bt->max_ccbs;
543 
544 	/* Determine our SCSI ID */
545 
546 	error = bt_cmd(bt, BOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,
547 		       (u_int8_t*)&config_data, sizeof(config_data),
548 		       DEFAULT_CMD_TIMEOUT);
549 	if (error != 0) {
550 		device_printf(dev,
551 			      "bt_fetch_adapter_info - Failed Get Config\n");
552 		return (error);
553 	}
554 	bt->scsi_id = config_data.scsi_id;
555 
556 	return (0);
557 }
558 
559 /*
560  * Start the board, ready for normal operation
561  */
562 int
563 bt_init(device_t dev)
564 {
565 	struct bt_softc *bt = device_get_softc(dev);
566 
567 	/* Announce the Adapter */
568 	device_printf(dev, "BT-%s FW Rev. %s ", bt->model, bt->firmware_ver);
569 
570 	if (bt->ultra_scsi != 0)
571 		kprintf("Ultra ");
572 
573 	if (bt->wide_bus != 0)
574 		kprintf("Wide ");
575 	else
576 		kprintf("Narrow ");
577 
578 	if (bt->diff_bus != 0)
579 		kprintf("Diff ");
580 
581 	kprintf("SCSI Host Adapter, SCSI ID %d, %d CCBs\n", bt->scsi_id,
582 	       bt->max_ccbs);
583 
584 	/*
585 	 * Create our DMA tags.  These tags define the kinds of device
586 	 * accessible memory allocations and memory mappings we will
587 	 * need to perform during normal operation.
588 	 *
589 	 * Unless we need to further restrict the allocation, we rely
590 	 * on the restrictions of the parent dmat, hence the common
591 	 * use of MAXADDR and MAXSIZE.
592 	 */
593 
594 	/* DMA tag for mapping buffers into device visible space. */
595 	if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
596 			       /*lowaddr*/BUS_SPACE_MAXADDR,
597 			       /*highaddr*/BUS_SPACE_MAXADDR,
598 			       /*filter*/NULL, /*filterarg*/NULL,
599 			       /*maxsize*/MAXBSIZE, /*nsegments*/BT_NSEG,
600 			       /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
601 			       /*flags*/BUS_DMA_ALLOCNOW,
602 			       &bt->buffer_dmat) != 0) {
603 		goto error_exit;
604 	}
605 
606 	bt->init_level++;
607 	/* DMA tag for our mailboxes */
608 	if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
609 			       /*lowaddr*/BUS_SPACE_MAXADDR,
610 			       /*highaddr*/BUS_SPACE_MAXADDR,
611 			       /*filter*/NULL, /*filterarg*/NULL,
612 			       bt->num_boxes * (sizeof(bt_mbox_in_t)
613 					      + sizeof(bt_mbox_out_t)),
614 			       /*nsegments*/1,
615 			       /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
616 			       /*flags*/0, &bt->mailbox_dmat) != 0) {
617 		goto error_exit;
618         }
619 
620 	bt->init_level++;
621 
622 	/* Allocation for our mailboxes */
623 	if (bus_dmamem_alloc(bt->mailbox_dmat, (void *)&bt->out_boxes,
624 			     BUS_DMA_NOWAIT, &bt->mailbox_dmamap) != 0) {
625 		goto error_exit;
626 	}
627 
628 	bt->init_level++;
629 
630 	/* And permanently map them */
631 	bus_dmamap_load(bt->mailbox_dmat, bt->mailbox_dmamap,
632        			bt->out_boxes,
633 			bt->num_boxes * (sizeof(bt_mbox_in_t)
634 				       + sizeof(bt_mbox_out_t)),
635 			btmapmboxes, bt, /*flags*/0);
636 
637 	bt->init_level++;
638 
639 	bt->in_boxes = (bt_mbox_in_t *)&bt->out_boxes[bt->num_boxes];
640 
641 	btinitmboxes(bt);
642 
643 	/* DMA tag for our ccb structures */
644 	if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
645 			       /*lowaddr*/BUS_SPACE_MAXADDR,
646 			       /*highaddr*/BUS_SPACE_MAXADDR,
647 			       /*filter*/NULL, /*filterarg*/NULL,
648 			       bt->max_ccbs * sizeof(struct bt_ccb),
649 			       /*nsegments*/1,
650 			       /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
651 			       /*flags*/0, &bt->ccb_dmat) != 0) {
652 		goto error_exit;
653         }
654 
655 	bt->init_level++;
656 
657 	/* Allocation for our ccbs */
658 	if (bus_dmamem_alloc(bt->ccb_dmat, (void *)&bt->bt_ccb_array,
659 			     BUS_DMA_NOWAIT, &bt->ccb_dmamap) != 0) {
660 		goto error_exit;
661 	}
662 
663 	bt->init_level++;
664 
665 	/* And permanently map them */
666 	bus_dmamap_load(bt->ccb_dmat, bt->ccb_dmamap,
667        			bt->bt_ccb_array,
668 			bt->max_ccbs * sizeof(struct bt_ccb),
669 			btmapccbs, bt, /*flags*/0);
670 
671 	bt->init_level++;
672 
673 	/* DMA tag for our S/G structures.  We allocate in page sized chunks */
674 	if (bus_dma_tag_create(bt->parent_dmat, /*alignment*/1, /*boundary*/0,
675 			       /*lowaddr*/BUS_SPACE_MAXADDR,
676 			       /*highaddr*/BUS_SPACE_MAXADDR,
677 			       /*filter*/NULL, /*filterarg*/NULL,
678 			       PAGE_SIZE, /*nsegments*/1,
679 			       /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
680 			       /*flags*/0, &bt->sg_dmat) != 0) {
681 		goto error_exit;
682         }
683 
684 	bt->init_level++;
685 
686 	/* Perform initial CCB allocation */
687 	bzero(bt->bt_ccb_array, bt->max_ccbs * sizeof(struct bt_ccb));
688 	btallocccbs(bt);
689 
690 	if (bt->num_ccbs == 0) {
691 		device_printf(dev,
692 			      "bt_init - Unable to allocate initial ccbs\n");
693 		goto error_exit;
694 	}
695 
696 	/*
697 	 * Note that we are going and return (to probe)
698 	 */
699 	return 0;
700 
701 error_exit:
702 
703 	return (ENXIO);
704 }
705 
706 int
707 bt_attach(device_t dev)
708 {
709 	struct bt_softc *bt = device_get_softc(dev);
710 	int tagged_dev_openings;
711 	struct cam_devq *devq;
712 	int error;
713 
714 	/*
715 	 * We reserve 1 ccb for error recovery, so don't
716 	 * tell the XPT about it.
717 	 */
718 	if (bt->tag_capable != 0)
719 		tagged_dev_openings = bt->max_ccbs - 1;
720 	else
721 		tagged_dev_openings = 0;
722 
723 	/*
724 	 * Create the device queue for our SIM.
725 	 */
726 	devq = cam_simq_alloc(bt->max_ccbs - 1);
727 	if (devq == NULL)
728 		return (ENOMEM);
729 
730 	/*
731 	 * Construct our SIM entry
732 	 */
733 	bt->sim = cam_sim_alloc(btaction, btpoll, "bt", bt, bt->unit,
734 				&sim_mplock, 2, tagged_dev_openings, devq);
735 	cam_simq_release(devq);
736 	if (bt->sim == NULL)
737 		return (ENOMEM);
738 
739 	if (xpt_bus_register(bt->sim, 0) != CAM_SUCCESS) {
740 		cam_sim_free(bt->sim);
741 		return (ENXIO);
742 	}
743 
744 	if (xpt_create_path(&bt->path, /*periph*/NULL,
745 			    cam_sim_path(bt->sim), CAM_TARGET_WILDCARD,
746 			    CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
747 		xpt_bus_deregister(cam_sim_path(bt->sim));
748 		cam_sim_free(bt->sim);
749 		return (ENXIO);
750 	}
751 
752 	/*
753 	 * Setup interrupt.
754 	 */
755 	error = bus_setup_intr(dev, bt->irq, 0,
756 			       bt_intr, bt, &bt->ih, NULL);
757 	if (error) {
758 		device_printf(dev, "bus_setup_intr() failed: %d\n", error);
759 		return (error);
760 	}
761 
762 	return (0);
763 }
764 
765 static void
766 btallocccbs(struct bt_softc *bt)
767 {
768 	struct bt_ccb *next_ccb;
769 	struct sg_map_node *sg_map;
770 	bus_addr_t physaddr;
771 	bt_sg_t *segs;
772 	int newcount;
773 	int i;
774 
775 	if (bt->num_ccbs >= bt->max_ccbs)
776 		/* Can't allocate any more */
777 		return;
778 
779 	next_ccb = &bt->bt_ccb_array[bt->num_ccbs];
780 
781 	sg_map = kmalloc(sizeof(*sg_map), M_DEVBUF, M_WAITOK);
782 
783 	/* Allocate S/G space for the next batch of CCBS */
784 	if (bus_dmamem_alloc(bt->sg_dmat, (void *)&sg_map->sg_vaddr,
785 			     BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
786 		kfree(sg_map, M_DEVBUF);
787 		goto error_exit;
788 	}
789 
790 	SLIST_INSERT_HEAD(&bt->sg_maps, sg_map, links);
791 
792 	bus_dmamap_load(bt->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
793 			PAGE_SIZE, btmapsgs, bt, /*flags*/0);
794 
795 	segs = sg_map->sg_vaddr;
796 	physaddr = sg_map->sg_physaddr;
797 
798 	newcount = (PAGE_SIZE / (BT_NSEG * sizeof(bt_sg_t)));
799 	for (i = 0; bt->num_ccbs < bt->max_ccbs && i < newcount; i++) {
800 		int error;
801 
802 		next_ccb->sg_list = segs;
803 		next_ccb->sg_list_phys = physaddr;
804 		next_ccb->flags = BCCB_FREE;
805 		error = bus_dmamap_create(bt->buffer_dmat, /*flags*/0,
806 					  &next_ccb->dmamap);
807 		if (error != 0)
808 			break;
809 		SLIST_INSERT_HEAD(&bt->free_bt_ccbs, next_ccb, links);
810 		segs += BT_NSEG;
811 		physaddr += (BT_NSEG * sizeof(bt_sg_t));
812 		next_ccb++;
813 		bt->num_ccbs++;
814 	}
815 
816 	/* Reserve a CCB for error recovery */
817 	if (bt->recovery_bccb == NULL) {
818 		bt->recovery_bccb = SLIST_FIRST(&bt->free_bt_ccbs);
819 		SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
820 	}
821 
822 	if (SLIST_FIRST(&bt->free_bt_ccbs) != NULL)
823 		return;
824 
825 error_exit:
826 	device_printf(bt->dev, "Can't malloc BCCBs\n");
827 }
828 
829 static __inline void
830 btfreeccb(struct bt_softc *bt, struct bt_ccb *bccb)
831 {
832 	crit_enter();
833 	if ((bccb->flags & BCCB_ACTIVE) != 0)
834 		LIST_REMOVE(&bccb->ccb->ccb_h, sim_links.le);
835 	if (bt->resource_shortage != 0
836 	 && (bccb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
837 		bccb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
838 		bt->resource_shortage = FALSE;
839 	}
840 	bccb->flags = BCCB_FREE;
841 	SLIST_INSERT_HEAD(&bt->free_bt_ccbs, bccb, links);
842 	bt->active_ccbs--;
843 	crit_exit();
844 }
845 
846 static __inline struct bt_ccb*
847 btgetccb(struct bt_softc *bt)
848 {
849 	struct	bt_ccb* bccb;
850 
851 	crit_enter();
852 	if ((bccb = SLIST_FIRST(&bt->free_bt_ccbs)) != NULL) {
853 		SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
854 		bt->active_ccbs++;
855 	} else {
856 		btallocccbs(bt);
857 		bccb = SLIST_FIRST(&bt->free_bt_ccbs);
858 		if (bccb != NULL) {
859 			SLIST_REMOVE_HEAD(&bt->free_bt_ccbs, links);
860 			bt->active_ccbs++;
861 		}
862 	}
863 	crit_exit();
864 
865 	return (bccb);
866 }
867 
868 static void
869 btaction(struct cam_sim *sim, union ccb *ccb)
870 {
871 	struct	bt_softc *bt;
872 
873 	CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("btaction\n"));
874 
875 	bt = (struct bt_softc *)cam_sim_softc(sim);
876 
877 	switch (ccb->ccb_h.func_code) {
878 	/* Common cases first */
879 	case XPT_SCSI_IO:	/* Execute the requested I/O operation */
880 	case XPT_RESET_DEV:	/* Bus Device Reset the specified SCSI device */
881 	{
882 		struct	bt_ccb	*bccb;
883 		struct	bt_hccb *hccb;
884 
885 		/*
886 		 * get a bccb to use.
887 		 */
888 		if ((bccb = btgetccb(bt)) == NULL) {
889 			crit_enter();
890 			bt->resource_shortage = TRUE;
891 			crit_exit();
892 			xpt_freeze_simq(bt->sim, /*count*/1);
893 			ccb->ccb_h.status = CAM_REQUEUE_REQ;
894 			xpt_done(ccb);
895 			return;
896 		}
897 
898 		hccb = &bccb->hccb;
899 
900 		/*
901 		 * So we can find the BCCB when an abort is requested
902 		 */
903 		bccb->ccb = ccb;
904 		ccb->ccb_h.ccb_bccb_ptr = bccb;
905 		ccb->ccb_h.ccb_bt_ptr = bt;
906 
907 		/*
908 		 * Put all the arguments for the xfer in the bccb
909 		 */
910 		hccb->target_id = ccb->ccb_h.target_id;
911 		hccb->target_lun = ccb->ccb_h.target_lun;
912 		hccb->btstat = 0;
913 		hccb->sdstat = 0;
914 
915 		if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
916 			struct ccb_scsiio *csio;
917 			struct ccb_hdr *ccbh;
918 
919 			csio = &ccb->csio;
920 			ccbh = &csio->ccb_h;
921 			hccb->opcode = INITIATOR_CCB_WRESID;
922 			hccb->datain = (ccb->ccb_h.flags & CAM_DIR_IN) ? 1 : 0;
923 			hccb->dataout =(ccb->ccb_h.flags & CAM_DIR_OUT) ? 1 : 0;
924 			hccb->cmd_len = csio->cdb_len;
925 			if (hccb->cmd_len > sizeof(hccb->scsi_cdb)) {
926 				ccb->ccb_h.status = CAM_REQ_INVALID;
927 				btfreeccb(bt, bccb);
928 				xpt_done(ccb);
929 				return;
930 			}
931 			hccb->sense_len = csio->sense_len;
932 			if ((ccbh->flags & CAM_TAG_ACTION_VALID) != 0
933 			 && ccb->csio.tag_action != CAM_TAG_ACTION_NONE) {
934 				hccb->tag_enable = TRUE;
935 				hccb->tag_type = (ccb->csio.tag_action & 0x3);
936 			} else {
937 				hccb->tag_enable = FALSE;
938 				hccb->tag_type = 0;
939 			}
940 			if ((ccbh->flags & CAM_CDB_POINTER) != 0) {
941 				if ((ccbh->flags & CAM_CDB_PHYS) == 0) {
942 					bcopy(csio->cdb_io.cdb_ptr,
943 					      hccb->scsi_cdb, hccb->cmd_len);
944 				} else {
945 					/* I guess I could map it in... */
946 					ccbh->status = CAM_REQ_INVALID;
947 					btfreeccb(bt, bccb);
948 					xpt_done(ccb);
949 					return;
950 				}
951 			} else {
952 				bcopy(csio->cdb_io.cdb_bytes,
953 				      hccb->scsi_cdb, hccb->cmd_len);
954 			}
955 			/* If need be, bounce our sense buffer */
956 			if (bt->sense_buffers != NULL) {
957 				hccb->sense_addr = btsensepaddr(bt, bccb);
958 			} else {
959 				hccb->sense_addr = vtophys(&csio->sense_data);
960 			}
961 			/*
962 			 * If we have any data to send with this command,
963 			 * map it into bus space.
964 			 */
965 		        /* Only use S/G if there is a transfer */
966 			if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
967 				if ((ccbh->flags & CAM_SCATTER_VALID) == 0) {
968 					/*
969 					 * We've been given a pointer
970 					 * to a single buffer.
971 					 */
972 					if ((ccbh->flags & CAM_DATA_PHYS)==0) {
973 						int error;
974 
975 						crit_enter();
976 						error = bus_dmamap_load(
977 						    bt->buffer_dmat,
978 						    bccb->dmamap,
979 						    csio->data_ptr,
980 						    csio->dxfer_len,
981 						    btexecuteccb,
982 						    bccb,
983 						    /*flags*/0);
984 						if (error == EINPROGRESS) {
985 							/*
986 							 * So as to maintain
987 							 * ordering, freeze the
988 							 * controller queue
989 							 * until our mapping is
990 							 * returned.
991 							 */
992 							xpt_freeze_simq(bt->sim,
993 									1);
994 							csio->ccb_h.status |=
995 							    CAM_RELEASE_SIMQ;
996 						}
997 						crit_exit();
998 					} else {
999 						struct bus_dma_segment seg;
1000 
1001 						/* Pointer to physical buffer */
1002 						seg.ds_addr =
1003 						    (bus_addr_t)csio->data_ptr;
1004 						seg.ds_len = csio->dxfer_len;
1005 						btexecuteccb(bccb, &seg, 1, 0);
1006 					}
1007 				} else {
1008 					struct bus_dma_segment *segs;
1009 
1010 					if ((ccbh->flags & CAM_DATA_PHYS) != 0)
1011 						panic("btaction - Physical "
1012 						      "segment pointers "
1013 						      "unsupported");
1014 
1015 					if ((ccbh->flags&CAM_SG_LIST_PHYS)==0)
1016 						panic("btaction - Virtual "
1017 						      "segment addresses "
1018 						      "unsupported");
1019 
1020 					/* Just use the segments provided */
1021 					segs = (struct bus_dma_segment *)
1022 					    csio->data_ptr;
1023 					btexecuteccb(bccb, segs,
1024 						     csio->sglist_cnt, 0);
1025 				}
1026 			} else {
1027 				btexecuteccb(bccb, NULL, 0, 0);
1028 			}
1029 		} else {
1030 			hccb->opcode = INITIATOR_BUS_DEV_RESET;
1031 			/* No data transfer */
1032 			hccb->datain = TRUE;
1033 			hccb->dataout = TRUE;
1034 			hccb->cmd_len = 0;
1035 			hccb->sense_len = 0;
1036 			hccb->tag_enable = FALSE;
1037 			hccb->tag_type = 0;
1038 			btexecuteccb(bccb, NULL, 0, 0);
1039 		}
1040 		break;
1041 	}
1042 	case XPT_EN_LUN:		/* Enable LUN as a target */
1043 	case XPT_TARGET_IO:		/* Execute target I/O request */
1044 	case XPT_ACCEPT_TARGET_IO:	/* Accept Host Target Mode CDB */
1045 	case XPT_CONT_TARGET_IO:	/* Continue Host Target I/O Connection*/
1046 	case XPT_ABORT:			/* Abort the specified CCB */
1047 		/* XXX Implement */
1048 		ccb->ccb_h.status = CAM_REQ_INVALID;
1049 		xpt_done(ccb);
1050 		break;
1051 	case XPT_SET_TRAN_SETTINGS:
1052 	{
1053 		/* XXX Implement */
1054 		ccb->ccb_h.status = CAM_PROVIDE_FAIL;
1055 		xpt_done(ccb);
1056 		break;
1057 	}
1058 	case XPT_GET_TRAN_SETTINGS:
1059 	/* Get default/user set transfer settings for the target */
1060 	{
1061 		struct	ccb_trans_settings *cts;
1062 		u_int	target_mask;
1063 
1064 		cts = &ccb->cts;
1065 		target_mask = 0x01 << ccb->ccb_h.target_id;
1066 		if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
1067 			struct ccb_trans_settings_scsi *scsi =
1068 			    &cts->proto_specific.scsi;
1069 			struct ccb_trans_settings_spi *spi =
1070 			    &cts->xport_specific.spi;
1071 			cts->protocol = PROTO_SCSI;
1072 			cts->protocol_version = SCSI_REV_2;
1073 			cts->transport = XPORT_SPI;
1074 			cts->transport_version = 2;
1075 
1076 			scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
1077 			spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
1078 
1079 			if ((bt->disc_permitted & target_mask) != 0)
1080 				spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
1081 			if ((bt->tags_permitted & target_mask) != 0)
1082 				scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
1083 
1084 			if ((bt->ultra_permitted & target_mask) != 0)
1085 				spi->sync_period = 12;
1086 			else if ((bt->fast_permitted & target_mask) != 0)
1087 				spi->sync_period = 25;
1088 			else if ((bt->sync_permitted & target_mask) != 0)
1089 				spi->sync_period = 50;
1090 			else
1091 				spi->sync_period = 0;
1092 
1093 			if (spi->sync_period != 0)
1094 				spi->sync_offset = 15;
1095 
1096 			spi->valid |= CTS_SPI_VALID_SYNC_RATE;
1097 			spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
1098 
1099 			spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
1100 			if ((bt->wide_permitted & target_mask) != 0)
1101 				spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
1102 			else
1103 				spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1104 
1105 			if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
1106 				scsi->valid = CTS_SCSI_VALID_TQ;
1107 				spi->valid |= CTS_SPI_VALID_DISC;
1108 			} else
1109 				scsi->valid = 0;
1110 		} else {
1111 			btfetchtransinfo(bt, cts);
1112 		}
1113 
1114 		ccb->ccb_h.status = CAM_REQ_CMP;
1115 		xpt_done(ccb);
1116 		break;
1117 	}
1118 	case XPT_CALC_GEOMETRY:
1119 	{
1120 		struct	  ccb_calc_geometry *ccg;
1121 		u_int32_t size_mb;
1122 		u_int32_t secs_per_cylinder;
1123 
1124 		ccg = &ccb->ccg;
1125 		size_mb = ccg->volume_size
1126 			/ ((1024L * 1024L) / ccg->block_size);
1127 
1128 		if (size_mb >= 1024 && (bt->extended_trans != 0)) {
1129 			if (size_mb >= 2048) {
1130 				ccg->heads = 255;
1131 				ccg->secs_per_track = 63;
1132 			} else {
1133 				ccg->heads = 128;
1134 				ccg->secs_per_track = 32;
1135 			}
1136 		} else {
1137 			ccg->heads = 64;
1138 			ccg->secs_per_track = 32;
1139 		}
1140 		secs_per_cylinder = ccg->heads * ccg->secs_per_track;
1141 		ccg->cylinders = ccg->volume_size / secs_per_cylinder;
1142 		ccb->ccb_h.status = CAM_REQ_CMP;
1143 		xpt_done(ccb);
1144 		break;
1145 	}
1146 	case XPT_RESET_BUS:		/* Reset the specified SCSI bus */
1147 	{
1148 		btreset(bt, /*hardreset*/TRUE);
1149 		ccb->ccb_h.status = CAM_REQ_CMP;
1150 		xpt_done(ccb);
1151 		break;
1152 	}
1153 	case XPT_TERM_IO:		/* Terminate the I/O process */
1154 		/* XXX Implement */
1155 		ccb->ccb_h.status = CAM_REQ_INVALID;
1156 		xpt_done(ccb);
1157 		break;
1158 	case XPT_PATH_INQ:		/* Path routing inquiry */
1159 	{
1160 		struct ccb_pathinq *cpi = &ccb->cpi;
1161 
1162 		cpi->version_num = 1; /* XXX??? */
1163 		cpi->hba_inquiry = PI_SDTR_ABLE;
1164 		if (bt->tag_capable != 0)
1165 			cpi->hba_inquiry |= PI_TAG_ABLE;
1166 		if (bt->wide_bus != 0)
1167 			cpi->hba_inquiry |= PI_WIDE_16;
1168 		cpi->target_sprt = 0;
1169 		cpi->hba_misc = 0;
1170 		cpi->hba_eng_cnt = 0;
1171 		cpi->max_target = bt->wide_bus ? 15 : 7;
1172 		cpi->max_lun = 7;
1173 		cpi->initiator_id = bt->scsi_id;
1174 		cpi->bus_id = cam_sim_bus(sim);
1175 		cpi->base_transfer_speed = 3300;
1176 		strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
1177 		strncpy(cpi->hba_vid, "BusLogic", HBA_IDLEN);
1178 		strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
1179 		cpi->unit_number = cam_sim_unit(sim);
1180 		cpi->ccb_h.status = CAM_REQ_CMP;
1181 		cpi->transport = XPORT_SPI;
1182 		cpi->transport_version = 2;
1183 		cpi->protocol = PROTO_SCSI;
1184 		cpi->protocol_version = SCSI_REV_2;
1185 		xpt_done(ccb);
1186 		break;
1187 	}
1188 	default:
1189 		ccb->ccb_h.status = CAM_REQ_INVALID;
1190 		xpt_done(ccb);
1191 		break;
1192 	}
1193 }
1194 
1195 static void
1196 btexecuteccb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
1197 {
1198 	struct	 bt_ccb *bccb;
1199 	union	 ccb *ccb;
1200 	struct	 bt_softc *bt;
1201 
1202 	bccb = (struct bt_ccb *)arg;
1203 	ccb = bccb->ccb;
1204 	bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr;
1205 
1206 	if (error != 0) {
1207 		if (error != EFBIG)
1208 			device_printf(bt->dev,
1209 				      "Unexpected error 0x%x returned from "
1210 				      "bus_dmamap_load\n", error);
1211 		if (ccb->ccb_h.status == CAM_REQ_INPROG) {
1212 			xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1213 			ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
1214 		}
1215 		btfreeccb(bt, bccb);
1216 		xpt_done(ccb);
1217 		return;
1218 	}
1219 
1220 	if (nseg != 0) {
1221 		bt_sg_t *sg;
1222 		bus_dma_segment_t *end_seg;
1223 		bus_dmasync_op_t op;
1224 
1225 		end_seg = dm_segs + nseg;
1226 
1227 		/* Copy the segments into our SG list */
1228 		sg = bccb->sg_list;
1229 		while (dm_segs < end_seg) {
1230 			sg->len = dm_segs->ds_len;
1231 			sg->addr = dm_segs->ds_addr;
1232 			sg++;
1233 			dm_segs++;
1234 		}
1235 
1236 		if (nseg > 1) {
1237 			bccb->hccb.opcode = INITIATOR_SG_CCB_WRESID;
1238 			bccb->hccb.data_len = sizeof(bt_sg_t) * nseg;
1239 			bccb->hccb.data_addr = bccb->sg_list_phys;
1240 		} else {
1241 			bccb->hccb.data_len = bccb->sg_list->len;
1242 			bccb->hccb.data_addr = bccb->sg_list->addr;
1243 		}
1244 
1245 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1246 			op = BUS_DMASYNC_PREREAD;
1247 		else
1248 			op = BUS_DMASYNC_PREWRITE;
1249 
1250 		bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op);
1251 
1252 	} else {
1253 		bccb->hccb.opcode = INITIATOR_CCB;
1254 		bccb->hccb.data_len = 0;
1255 		bccb->hccb.data_addr = 0;
1256 	}
1257 
1258 	crit_enter();
1259 
1260 	/*
1261 	 * Last time we need to check if this CCB needs to
1262 	 * be aborted.
1263 	 */
1264 	if (ccb->ccb_h.status != CAM_REQ_INPROG) {
1265 		if (nseg != 0)
1266 			bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1267 		btfreeccb(bt, bccb);
1268 		xpt_done(ccb);
1269 		crit_exit();
1270 		return;
1271 	}
1272 
1273 	bccb->flags = BCCB_ACTIVE;
1274 	ccb->ccb_h.status |= CAM_SIM_QUEUED;
1275 	LIST_INSERT_HEAD(&bt->pending_ccbs, &ccb->ccb_h, sim_links.le);
1276 
1277 	callout_reset(&ccb->ccb_h.timeout_ch, (ccb->ccb_h.timeout * hz) / 1000,
1278 	    bttimeout, bccb);
1279 
1280 	/* Tell the adapter about this command */
1281 	bt->cur_outbox->ccb_addr = btccbvtop(bt, bccb);
1282 	if (bt->cur_outbox->action_code != BMBO_FREE) {
1283 		/*
1284 		 * We should never encounter a busy mailbox.
1285 		 * If we do, warn the user, and treat it as
1286 		 * a resource shortage.  If the controller is
1287 		 * hung, one of the pending transactions will
1288 		 * timeout causing us to start recovery operations.
1289 		 */
1290 		device_printf(bt->dev,
1291 			      "Encountered busy mailbox with %d out of %d "
1292 			      "commands active!!!\n", bt->active_ccbs,
1293 			      bt->max_ccbs);
1294 		callout_stop(&ccb->ccb_h.timeout_ch);
1295 		if (nseg != 0)
1296 			bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1297 		btfreeccb(bt, bccb);
1298 		bt->resource_shortage = TRUE;
1299 		xpt_freeze_simq(bt->sim, /*count*/1);
1300 		ccb->ccb_h.status = CAM_REQUEUE_REQ;
1301 		xpt_done(ccb);
1302 		crit_exit();
1303 		return;
1304 	}
1305 	bt->cur_outbox->action_code = BMBO_START;
1306 	bt_outb(bt, COMMAND_REG, BOP_START_MBOX);
1307 	btnextoutbox(bt);
1308 	crit_exit();
1309 }
1310 
1311 void
1312 bt_intr(void *arg)
1313 {
1314 	struct	bt_softc *bt;
1315 	u_int	intstat;
1316 
1317 	bt = (struct bt_softc *)arg;
1318 	while (((intstat = bt_inb(bt, INTSTAT_REG)) & INTR_PENDING) != 0) {
1319 
1320 		if ((intstat & CMD_COMPLETE) != 0) {
1321 			bt->latched_status = bt_inb(bt, STATUS_REG);
1322 			bt->command_cmp = TRUE;
1323 		}
1324 
1325 		bt_outb(bt, CONTROL_REG, RESET_INTR);
1326 
1327 		if ((intstat & IMB_LOADED) != 0) {
1328 			while (bt->cur_inbox->comp_code != BMBI_FREE) {
1329 				btdone(bt,
1330 				       btccbptov(bt, bt->cur_inbox->ccb_addr),
1331 				       bt->cur_inbox->comp_code);
1332 				bt->cur_inbox->comp_code = BMBI_FREE;
1333 				btnextinbox(bt);
1334 			}
1335 		}
1336 
1337 		if ((intstat & SCSI_BUS_RESET) != 0) {
1338 			btreset(bt, /*hardreset*/FALSE);
1339 		}
1340 	}
1341 }
1342 
1343 static void
1344 btdone(struct bt_softc *bt, struct bt_ccb *bccb, bt_mbi_comp_code_t comp_code)
1345 {
1346 	union  ccb	  *ccb;
1347 	struct ccb_scsiio *csio;
1348 
1349 	ccb = bccb->ccb;
1350 	csio = &bccb->ccb->csio;
1351 
1352 	if ((bccb->flags & BCCB_ACTIVE) == 0) {
1353 		device_printf(bt->dev,
1354 			      "btdone - Attempt to free non-active BCCB %p\n",
1355 			      (void *)bccb);
1356 		return;
1357 	}
1358 
1359 	if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1360 		bus_dmasync_op_t op;
1361 
1362 		if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1363 			op = BUS_DMASYNC_POSTREAD;
1364 		else
1365 			op = BUS_DMASYNC_POSTWRITE;
1366 		bus_dmamap_sync(bt->buffer_dmat, bccb->dmamap, op);
1367 		bus_dmamap_unload(bt->buffer_dmat, bccb->dmamap);
1368 	}
1369 
1370 	if (bccb == bt->recovery_bccb) {
1371 		/*
1372 		 * The recovery BCCB does not have a CCB associated
1373 		 * with it, so short circuit the normal error handling.
1374 		 * We now traverse our list of pending CCBs and process
1375 		 * any that were terminated by the recovery CCBs action.
1376 		 * We also reinstate timeouts for all remaining, pending,
1377 		 * CCBs.
1378 		 */
1379 		struct cam_path *path;
1380 		struct ccb_hdr *ccb_h;
1381 		cam_status error;
1382 
1383 		/* Notify all clients that a BDR occured */
1384 		error = xpt_create_path(&path, /*periph*/NULL,
1385 					cam_sim_path(bt->sim),
1386 					bccb->hccb.target_id,
1387 					CAM_LUN_WILDCARD);
1388 
1389 		if (error == CAM_REQ_CMP)
1390 			xpt_async(AC_SENT_BDR, path, NULL);
1391 
1392 		ccb_h = LIST_FIRST(&bt->pending_ccbs);
1393 		while (ccb_h != NULL) {
1394 			struct bt_ccb *pending_bccb;
1395 
1396 			pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
1397 			if (pending_bccb->hccb.target_id
1398 			 == bccb->hccb.target_id) {
1399 				pending_bccb->hccb.btstat = BTSTAT_HA_BDR;
1400 				ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1401 				btdone(bt, pending_bccb, BMBI_ERROR);
1402 			} else {
1403 				callout_reset(&ccb_h->timeout_ch,
1404 				    (ccb_h->timeout * hz) / 1000,
1405 				    bttimeout, pending_bccb);
1406 				ccb_h = LIST_NEXT(ccb_h, sim_links.le);
1407 			}
1408 		}
1409 		device_printf(bt->dev, "No longer in timeout\n");
1410 		return;
1411 	}
1412 
1413 	callout_stop(&ccb->ccb_h.timeout_ch);
1414 
1415 	switch (comp_code) {
1416 	case BMBI_FREE:
1417 		device_printf(bt->dev,
1418 			      "btdone - CCB completed with free status!\n");
1419 		break;
1420 	case BMBI_NOT_FOUND:
1421 		device_printf(bt->dev,
1422 			      "btdone - CCB Abort failed to find CCB\n");
1423 		break;
1424 	case BMBI_ABORT:
1425 	case BMBI_ERROR:
1426 		if (bootverbose) {
1427 			kprintf("bt: ccb %p - error %x occurred.  "
1428 			       "btstat = %x, sdstat = %x\n",
1429 			       (void *)bccb, comp_code, bccb->hccb.btstat,
1430 			       bccb->hccb.sdstat);
1431 		}
1432 		/* An error occured */
1433 		switch(bccb->hccb.btstat) {
1434 		case BTSTAT_DATARUN_ERROR:
1435 			if (bccb->hccb.data_len == 0) {
1436 				/*
1437 				 * At least firmware 4.22, does this
1438 				 * for a QUEUE FULL condition.
1439 				 */
1440 				bccb->hccb.sdstat = SCSI_STATUS_QUEUE_FULL;
1441 			} else if (bccb->hccb.data_len < 0) {
1442 				csio->ccb_h.status = CAM_DATA_RUN_ERR;
1443 				break;
1444 			}
1445 			/* FALLTHROUGH */
1446 		case BTSTAT_NOERROR:
1447 		case BTSTAT_LINKED_CMD_COMPLETE:
1448 		case BTSTAT_LINKED_CMD_FLAG_COMPLETE:
1449 		case BTSTAT_DATAUNDERUN_ERROR:
1450 
1451 			csio->scsi_status = bccb->hccb.sdstat;
1452 			csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR;
1453 			switch(csio->scsi_status) {
1454 			case SCSI_STATUS_CHECK_COND:
1455 			case SCSI_STATUS_CMD_TERMINATED:
1456 				csio->ccb_h.status |= CAM_AUTOSNS_VALID;
1457 				/* Bounce sense back if necessary */
1458 				if (bt->sense_buffers != NULL) {
1459 					csio->sense_data =
1460 					    *btsensevaddr(bt, bccb);
1461 				}
1462 				break;
1463 			default:
1464 				break;
1465 			case SCSI_STATUS_OK:
1466 				csio->ccb_h.status = CAM_REQ_CMP;
1467 				break;
1468 			}
1469 			csio->resid = bccb->hccb.data_len;
1470 			break;
1471 		case BTSTAT_SELTIMEOUT:
1472 			csio->ccb_h.status = CAM_SEL_TIMEOUT;
1473 			break;
1474 		case BTSTAT_UNEXPECTED_BUSFREE:
1475 			csio->ccb_h.status = CAM_UNEXP_BUSFREE;
1476 			break;
1477 		case BTSTAT_INVALID_PHASE:
1478 			csio->ccb_h.status = CAM_SEQUENCE_FAIL;
1479 			break;
1480 		case BTSTAT_INVALID_ACTION_CODE:
1481 			panic("%s: Inavlid Action code", bt_name(bt));
1482 			break;
1483 		case BTSTAT_INVALID_OPCODE:
1484 			panic("%s: Inavlid CCB Opcode code", bt_name(bt));
1485 			break;
1486 		case BTSTAT_LINKED_CCB_LUN_MISMATCH:
1487 			/* We don't even support linked commands... */
1488 			panic("%s: Linked CCB Lun Mismatch", bt_name(bt));
1489 			break;
1490 		case BTSTAT_INVALID_CCB_OR_SG_PARAM:
1491 			panic("%s: Invalid CCB or SG list", bt_name(bt));
1492 			break;
1493 		case BTSTAT_AUTOSENSE_FAILED:
1494 			csio->ccb_h.status = CAM_AUTOSENSE_FAIL;
1495 			break;
1496 		case BTSTAT_TAGGED_MSG_REJECTED:
1497 		{
1498 			struct ccb_trans_settings neg;
1499 			struct ccb_trans_settings_scsi *scsi =
1500 			    &neg.proto_specific.scsi;
1501 
1502 			neg.protocol = PROTO_SCSI;
1503 			neg.protocol_version = SCSI_REV_2;
1504 			neg.transport = XPORT_SPI;
1505 			neg.transport_version = 2;
1506 			scsi->valid = CTS_SCSI_VALID_TQ;
1507 			scsi->flags = 0;
1508 			xpt_print_path(csio->ccb_h.path);
1509 			kprintf("refuses tagged commands.  Performing "
1510 			       "non-tagged I/O\n");
1511 			xpt_setup_ccb(&neg.ccb_h, csio->ccb_h.path,
1512 				      /*priority*/1);
1513 			xpt_async(AC_TRANSFER_NEG, csio->ccb_h.path, &neg);
1514 			bt->tags_permitted &= ~(0x01 << csio->ccb_h.target_id);
1515 			csio->ccb_h.status = CAM_MSG_REJECT_REC;
1516 			break;
1517 		}
1518 		case BTSTAT_UNSUPPORTED_MSG_RECEIVED:
1519 			/*
1520 			 * XXX You would think that this is
1521 			 *     a recoverable error... Hmmm.
1522 			 */
1523 			csio->ccb_h.status = CAM_REQ_CMP_ERR;
1524 			break;
1525 		case BTSTAT_HA_SOFTWARE_ERROR:
1526 		case BTSTAT_HA_WATCHDOG_ERROR:
1527 		case BTSTAT_HARDWARE_FAILURE:
1528 			/* Hardware reset ??? Can we recover ??? */
1529 			csio->ccb_h.status = CAM_NO_HBA;
1530 			break;
1531 		case BTSTAT_TARGET_IGNORED_ATN:
1532 		case BTSTAT_OTHER_SCSI_BUS_RESET:
1533 		case BTSTAT_HA_SCSI_BUS_RESET:
1534 			if ((csio->ccb_h.status & CAM_STATUS_MASK)
1535 			 != CAM_CMD_TIMEOUT)
1536 				csio->ccb_h.status = CAM_SCSI_BUS_RESET;
1537 			break;
1538 		case BTSTAT_HA_BDR:
1539 			if ((bccb->flags & BCCB_DEVICE_RESET) == 0)
1540 				csio->ccb_h.status = CAM_BDR_SENT;
1541 			else
1542 				csio->ccb_h.status = CAM_CMD_TIMEOUT;
1543 			break;
1544 		case BTSTAT_INVALID_RECONNECT:
1545 		case BTSTAT_ABORT_QUEUE_GENERATED:
1546 			csio->ccb_h.status = CAM_REQ_TERMIO;
1547 			break;
1548 		case BTSTAT_SCSI_PERROR_DETECTED:
1549 			csio->ccb_h.status = CAM_UNCOR_PARITY;
1550 			break;
1551 		}
1552 		if (csio->ccb_h.status != CAM_REQ_CMP) {
1553 			xpt_freeze_devq(csio->ccb_h.path, /*count*/1);
1554 			csio->ccb_h.status |= CAM_DEV_QFRZN;
1555 		}
1556 		if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0)
1557 			ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1558 		btfreeccb(bt, bccb);
1559 		xpt_done(ccb);
1560 		break;
1561 	case BMBI_OK:
1562 		/* All completed without incident */
1563 		ccb->ccb_h.status |= CAM_REQ_CMP;
1564 		if ((bccb->flags & BCCB_RELEASE_SIMQ) != 0)
1565 			ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
1566 		btfreeccb(bt, bccb);
1567 		xpt_done(ccb);
1568 		break;
1569 	}
1570 }
1571 
1572 static int
1573 btreset(struct bt_softc* bt, int hard_reset)
1574 {
1575 	struct	 ccb_hdr *ccb_h;
1576 	u_int	 status;
1577 	u_int	 timeout;
1578 	u_int8_t reset_type;
1579 
1580 	if (hard_reset != 0)
1581 		reset_type = HARD_RESET;
1582 	else
1583 		reset_type = SOFT_RESET;
1584 	bt_outb(bt, CONTROL_REG, reset_type);
1585 
1586 	/* Wait 5sec. for Diagnostic start */
1587 	timeout = 5 * 10000;
1588 	while (--timeout) {
1589 		status = bt_inb(bt, STATUS_REG);
1590 		if ((status & DIAG_ACTIVE) != 0)
1591 			break;
1592 		DELAY(100);
1593 	}
1594 	if (timeout == 0) {
1595 		if (bootverbose)
1596 			kprintf("%s: btreset - Diagnostic Active failed to "
1597 				"assert. status = 0x%x\n", bt_name(bt), status);
1598 		return (ETIMEDOUT);
1599 	}
1600 
1601 	/* Wait 10sec. for Diagnostic end */
1602 	timeout = 10 * 10000;
1603 	while (--timeout) {
1604 		status = bt_inb(bt, STATUS_REG);
1605 		if ((status & DIAG_ACTIVE) == 0)
1606 			break;
1607 		DELAY(100);
1608 	}
1609 	if (timeout == 0) {
1610 		panic("%s: btreset - Diagnostic Active failed to drop. "
1611 		       "status = 0x%x\n", bt_name(bt), status);
1612 		return (ETIMEDOUT);
1613 	}
1614 
1615 	/* Wait for the host adapter to become ready or report a failure */
1616 	timeout = 10000;
1617 	while (--timeout) {
1618 		status = bt_inb(bt, STATUS_REG);
1619 		if ((status & (DIAG_FAIL|HA_READY|DATAIN_REG_READY)) != 0)
1620 			break;
1621 		DELAY(100);
1622 	}
1623 	if (timeout == 0) {
1624 		kprintf("%s: btreset - Host adapter failed to come ready. "
1625 		       "status = 0x%x\n", bt_name(bt), status);
1626 		return (ETIMEDOUT);
1627 	}
1628 
1629 	/* If the diagnostics failed, tell the user */
1630 	if ((status & DIAG_FAIL) != 0
1631 	 || (status & HA_READY) == 0) {
1632 		kprintf("%s: btreset - Adapter failed diagnostics\n",
1633 		       bt_name(bt));
1634 
1635 		if ((status & DATAIN_REG_READY) != 0)
1636 			kprintf("%s: btreset - Host Adapter Error code = 0x%x\n",
1637 			       bt_name(bt), bt_inb(bt, DATAIN_REG));
1638 		return (ENXIO);
1639 	}
1640 
1641 	/* If we've allocated mailboxes, initialize them */
1642 	if (bt->init_level > 4)
1643 		btinitmboxes(bt);
1644 
1645 	/* If we've attached to the XPT, tell it about the event */
1646 	if (bt->path != NULL)
1647 		xpt_async(AC_BUS_RESET, bt->path, NULL);
1648 
1649 	/*
1650 	 * Perform completion processing for all outstanding CCBs.
1651 	 */
1652 	while ((ccb_h = LIST_FIRST(&bt->pending_ccbs)) != NULL) {
1653 		struct bt_ccb *pending_bccb;
1654 
1655 		pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
1656 		pending_bccb->hccb.btstat = BTSTAT_HA_SCSI_BUS_RESET;
1657 		btdone(bt, pending_bccb, BMBI_ERROR);
1658 	}
1659 
1660 	return (0);
1661 }
1662 
1663 /*
1664  * Send a command to the adapter.
1665  */
1666 int
1667 bt_cmd(struct bt_softc *bt, bt_op_t opcode, u_int8_t *params, u_int param_len,
1668       u_int8_t *reply_data, u_int reply_len, u_int cmd_timeout)
1669 {
1670 	u_int	timeout;
1671 	u_int	status;
1672 	u_int	saved_status;
1673 	u_int	intstat;
1674 	u_int	reply_buf_size;
1675 	int	cmd_complete;
1676 	int	error;
1677 
1678 	/* No data returned to start */
1679 	reply_buf_size = reply_len;
1680 	reply_len = 0;
1681 	intstat = 0;
1682 	cmd_complete = 0;
1683 	saved_status = 0;
1684 	error = 0;
1685 
1686 	bt->command_cmp = 0;
1687 	/*
1688 	 * Wait up to 10 sec. for the adapter to become
1689 	 * ready to accept commands.
1690 	 */
1691 	timeout = 100000;
1692 	while (--timeout) {
1693 		status = bt_inb(bt, STATUS_REG);
1694 		if ((status & HA_READY) != 0
1695 		 && (status & CMD_REG_BUSY) == 0)
1696 			break;
1697 		/*
1698 		 * Throw away any pending data which may be
1699 		 * left over from earlier commands that we
1700 		 * timedout on.
1701 		 */
1702 		if ((status & DATAIN_REG_READY) != 0)
1703 			(void)bt_inb(bt, DATAIN_REG);
1704 		DELAY(100);
1705 	}
1706 	if (timeout == 0) {
1707 		kprintf("%s: bt_cmd: Timeout waiting for adapter ready, "
1708 		       "status = 0x%x\n", bt_name(bt), status);
1709 		return (ETIMEDOUT);
1710 	}
1711 
1712 	/*
1713 	 * Send the opcode followed by any necessary parameter bytes.
1714 	 */
1715 	bt_outb(bt, COMMAND_REG, opcode);
1716 
1717 	/*
1718 	 * Wait for up to 1sec for each byte of the the
1719 	 * parameter list sent to be sent.
1720 	 */
1721 	timeout = 10000;
1722 	while (param_len && --timeout) {
1723 		DELAY(100);
1724 		crit_enter();
1725 		status = bt_inb(bt, STATUS_REG);
1726 		intstat = bt_inb(bt, INTSTAT_REG);
1727 		crit_exit();
1728 
1729 		if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1730 		 == (INTR_PENDING|CMD_COMPLETE)) {
1731 			saved_status = status;
1732 			cmd_complete = 1;
1733 			break;
1734 		}
1735 		if (bt->command_cmp != 0) {
1736 			saved_status = bt->latched_status;
1737 			cmd_complete = 1;
1738 			break;
1739 		}
1740 		if ((status & DATAIN_REG_READY) != 0)
1741 			break;
1742 		if ((status & CMD_REG_BUSY) == 0) {
1743 			bt_outb(bt, COMMAND_REG, *params++);
1744 			param_len--;
1745 			timeout = 10000;
1746 		}
1747 	}
1748 	if (timeout == 0) {
1749 		kprintf("%s: bt_cmd: Timeout sending parameters, "
1750 		       "status = 0x%x\n", bt_name(bt), status);
1751 		cmd_complete = 1;
1752 		saved_status = status;
1753 		error = ETIMEDOUT;
1754 	}
1755 
1756 	/*
1757 	 * Wait for the command to complete.
1758 	 */
1759 	while (cmd_complete == 0 && --cmd_timeout) {
1760 
1761 		crit_enter();
1762 		status = bt_inb(bt, STATUS_REG);
1763 		intstat = bt_inb(bt, INTSTAT_REG);
1764 		/*
1765 		 * It may be that this command was issued with
1766 		 * controller interrupts disabled.  We'll never
1767 		 * get to our command if an incoming mailbox
1768 		 * interrupt is pending, so take care of completed
1769 		 * mailbox commands by calling our interrupt handler.
1770 		 */
1771 		if ((intstat & (INTR_PENDING|IMB_LOADED))
1772 		 == (INTR_PENDING|IMB_LOADED))
1773 			bt_intr(bt);
1774 		crit_exit();
1775 
1776 		if (bt->command_cmp != 0) {
1777  			/*
1778 			 * Our interrupt handler saw CMD_COMPLETE
1779 			 * status before we did.
1780 			 */
1781 			cmd_complete = 1;
1782 			saved_status = bt->latched_status;
1783 		} else if ((intstat & (INTR_PENDING|CMD_COMPLETE))
1784 			== (INTR_PENDING|CMD_COMPLETE)) {
1785 			/*
1786 			 * Our poll (in case interrupts are blocked)
1787 			 * saw the CMD_COMPLETE interrupt.
1788 			 */
1789 			cmd_complete = 1;
1790 			saved_status = status;
1791 		} else if (opcode == BOP_MODIFY_IO_ADDR
1792 			&& (status & CMD_REG_BUSY) == 0) {
1793 			/*
1794 			 * The BOP_MODIFY_IO_ADDR does not issue a CMD_COMPLETE,
1795 			 * but it should update the status register.  So, we
1796 			 * consider this command complete when the CMD_REG_BUSY
1797 			 * status clears.
1798 			 */
1799 			saved_status = status;
1800 			cmd_complete = 1;
1801 		} else if ((status & DATAIN_REG_READY) != 0) {
1802 			u_int8_t data;
1803 
1804 			data = bt_inb(bt, DATAIN_REG);
1805 			if (reply_len < reply_buf_size) {
1806 				*reply_data++ = data;
1807 			} else {
1808 				kprintf("%s: bt_cmd - Discarded reply data byte "
1809 				       "for opcode 0x%x\n", bt_name(bt),
1810 				       opcode);
1811 			}
1812 			/*
1813 			 * Reset timeout to ensure at least a second
1814 			 * between response bytes.
1815 			 */
1816 			cmd_timeout = MAX(cmd_timeout, 10000);
1817 			reply_len++;
1818 
1819 		} else if ((opcode == BOP_FETCH_LRAM)
1820 			&& (status & HA_READY) != 0) {
1821 				saved_status = status;
1822 				cmd_complete = 1;
1823 		}
1824 		DELAY(100);
1825 	}
1826 	if (cmd_timeout == 0) {
1827 		kprintf("%s: bt_cmd: Timeout waiting for command (%x) "
1828 		       "to complete.\n%s: status = 0x%x, intstat = 0x%x, "
1829 		       "rlen %d\n", bt_name(bt), opcode,
1830 		       bt_name(bt), status, intstat, reply_len);
1831 		error = (ETIMEDOUT);
1832 	}
1833 
1834 	/*
1835 	 * Clear any pending interrupts.  Block interrupts so our
1836 	 * interrupt handler is not re-entered.
1837 	 */
1838 	crit_enter();
1839 	bt_intr(bt);
1840 	crit_exit();
1841 
1842 	if (error != 0)
1843 		return (error);
1844 
1845 	/*
1846 	 * If the command was rejected by the controller, tell the caller.
1847 	 */
1848 	if ((saved_status & CMD_INVALID) != 0) {
1849 		/*
1850 		 * Some early adapters may not recover properly from
1851 		 * an invalid command.  If it appears that the controller
1852 		 * has wedged (i.e. status was not cleared by our interrupt
1853 		 * reset above), perform a soft reset.
1854       		 */
1855 		if (bootverbose)
1856 			kprintf("%s: Invalid Command 0x%x\n", bt_name(bt),
1857 				opcode);
1858 		DELAY(1000);
1859 		status = bt_inb(bt, STATUS_REG);
1860 		if ((status & (CMD_INVALID|STATUS_REG_RSVD|DATAIN_REG_READY|
1861 			      CMD_REG_BUSY|DIAG_FAIL|DIAG_ACTIVE)) != 0
1862 		 || (status & (HA_READY|INIT_REQUIRED))
1863 		  != (HA_READY|INIT_REQUIRED)) {
1864 			btreset(bt, /*hard_reset*/FALSE);
1865 		}
1866 		return (EINVAL);
1867 	}
1868 
1869 	if (param_len > 0) {
1870 		/* The controller did not accept the full argument list */
1871 	 	return (E2BIG);
1872 	}
1873 
1874 	if (reply_len != reply_buf_size) {
1875 		/* Too much or too little data received */
1876 		return (EMSGSIZE);
1877 	}
1878 
1879 	/* We were successful */
1880 	return (0);
1881 }
1882 
1883 static int
1884 btinitmboxes(struct bt_softc *bt) {
1885 	init_32b_mbox_params_t init_mbox;
1886 	int error;
1887 
1888 	bzero(bt->in_boxes, sizeof(bt_mbox_in_t) * bt->num_boxes);
1889 	bzero(bt->out_boxes, sizeof(bt_mbox_out_t) * bt->num_boxes);
1890 	bt->cur_inbox = bt->in_boxes;
1891 	bt->last_inbox = bt->in_boxes + bt->num_boxes - 1;
1892 	bt->cur_outbox = bt->out_boxes;
1893 	bt->last_outbox = bt->out_boxes + bt->num_boxes - 1;
1894 
1895 	/* Tell the adapter about them */
1896 	init_mbox.num_boxes = bt->num_boxes;
1897 	init_mbox.base_addr[0] = bt->mailbox_physbase & 0xFF;
1898 	init_mbox.base_addr[1] = (bt->mailbox_physbase >> 8) & 0xFF;
1899 	init_mbox.base_addr[2] = (bt->mailbox_physbase >> 16) & 0xFF;
1900 	init_mbox.base_addr[3] = (bt->mailbox_physbase >> 24) & 0xFF;
1901 	error = bt_cmd(bt, BOP_INITIALIZE_32BMBOX, (u_int8_t *)&init_mbox,
1902 		       /*parmlen*/sizeof(init_mbox), /*reply_buf*/NULL,
1903 		       /*reply_len*/0, DEFAULT_CMD_TIMEOUT);
1904 
1905 	if (error != 0)
1906 		kprintf("btinitmboxes: Initialization command failed\n");
1907 	else if (bt->strict_rr != 0) {
1908 		/*
1909 		 * If the controller supports
1910 		 * strict round robin mode,
1911 		 * enable it
1912 		 */
1913 		u_int8_t param;
1914 
1915 		param = 0;
1916 		error = bt_cmd(bt, BOP_ENABLE_STRICT_RR, &param, 1,
1917 			       /*reply_buf*/NULL, /*reply_len*/0,
1918 			       DEFAULT_CMD_TIMEOUT);
1919 
1920 		if (error != 0) {
1921 			kprintf("btinitmboxes: Unable to enable strict RR\n");
1922 			error = 0;
1923 		} else if (bootverbose) {
1924 			kprintf("%s: Using Strict Round Robin Mailbox Mode\n",
1925 			       bt_name(bt));
1926 		}
1927 	}
1928 
1929 	return (error);
1930 }
1931 
1932 /*
1933  * Update the XPT's idea of the negotiated transfer
1934  * parameters for a particular target.
1935  */
1936 static void
1937 btfetchtransinfo(struct bt_softc *bt, struct ccb_trans_settings *cts)
1938 {
1939 	setup_data_t	setup_info;
1940 	u_int		target;
1941 	u_int		targ_offset;
1942 	u_int		targ_mask;
1943 	u_int		sync_period;
1944 	u_int		sync_offset;
1945 	u_int		bus_width;
1946 	int		error;
1947 	u_int8_t	param;
1948 	targ_syncinfo_t	sync_info;
1949 	struct ccb_trans_settings_scsi *scsi =
1950 	    &cts->proto_specific.scsi;
1951 	struct ccb_trans_settings_spi *spi =
1952 	    &cts->xport_specific.spi;
1953 
1954 	spi->valid = 0;
1955 	scsi->valid = 0;
1956 
1957 	target = cts->ccb_h.target_id;
1958 	targ_offset = (target & 0x7);
1959 	targ_mask = (0x01 << targ_offset);
1960 
1961 	/*
1962 	 * Inquire Setup Information.  This command retreives the
1963 	 * Wide negotiation status for recent adapters as well as
1964 	 * the sync info for older models.
1965 	 */
1966 	param = sizeof(setup_info);
1967 	error = bt_cmd(bt, BOP_INQUIRE_SETUP_INFO, &param, /*paramlen*/1,
1968 		       (u_int8_t*)&setup_info, sizeof(setup_info),
1969 		       DEFAULT_CMD_TIMEOUT);
1970 
1971 	if (error != 0) {
1972 		kprintf("%s: btfetchtransinfo - Inquire Setup Info Failed %x\n",
1973 		       bt_name(bt), error);
1974 		return;
1975 	}
1976 
1977 	sync_info = (target < 8) ? setup_info.low_syncinfo[targ_offset]
1978 				 : setup_info.high_syncinfo[targ_offset];
1979 
1980 	if (sync_info.sync == 0)
1981 		sync_offset = 0;
1982 	else
1983 		sync_offset = sync_info.offset;
1984 
1985 
1986 	bus_width = MSG_EXT_WDTR_BUS_8_BIT;
1987 	if (strcmp(bt->firmware_ver, "5.06L") >= 0) {
1988 		u_int wide_active;
1989 
1990 		wide_active =
1991 		    (target < 8) ? (setup_info.low_wide_active & targ_mask)
1992 		    		 : (setup_info.high_wide_active & targ_mask);
1993 
1994 		if (wide_active)
1995 			bus_width = MSG_EXT_WDTR_BUS_16_BIT;
1996 	} else if ((bt->wide_permitted & targ_mask) != 0) {
1997 		struct ccb_getdev cgd;
1998 
1999 		/*
2000 		 * Prior to rev 5.06L, wide status isn't provided,
2001 		 * so we "guess" that wide transfers are in effect
2002 		 * if the user settings allow for wide and the inquiry
2003 		 * data for the device indicates that it can handle
2004 		 * wide transfers.
2005 		 */
2006 		xpt_setup_ccb(&cgd.ccb_h, cts->ccb_h.path, /*priority*/1);
2007 		cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2008 		xpt_action((union ccb *)&cgd);
2009 		if ((cgd.ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
2010 		 && (cgd.inq_data.flags & SID_WBus16) != 0)
2011 			bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2012 	}
2013 
2014 	if (bt->firmware_ver[0] >= '3') {
2015 		/*
2016 		 * For adapters that can do fast or ultra speeds,
2017 		 * use the more exact Target Sync Information command.
2018 		 */
2019 		target_sync_info_data_t sync_info;
2020 
2021 		param = sizeof(sync_info);
2022 		error = bt_cmd(bt, BOP_TARG_SYNC_INFO, &param, /*paramlen*/1,
2023 			       (u_int8_t*)&sync_info, sizeof(sync_info),
2024 			       DEFAULT_CMD_TIMEOUT);
2025 
2026 		if (error != 0) {
2027 			kprintf("%s: btfetchtransinfo - Inquire Sync "
2028 			       "Info Failed 0x%x\n", bt_name(bt), error);
2029 			return;
2030 		}
2031 		sync_period = sync_info.sync_rate[target] * 100;
2032 	} else {
2033 		sync_period = 2000 + (500 * sync_info.period);
2034 	}
2035 
2036 	cts->protocol = PROTO_SCSI;
2037 	cts->protocol_version = SCSI_REV_2;
2038 	cts->transport = XPORT_SPI;
2039 	cts->transport_version = 2;
2040 
2041 	spi->sync_period = sync_period;
2042 	spi->valid |= CTS_SPI_VALID_SYNC_RATE;
2043 	spi->sync_offset = sync_offset;
2044 	spi->valid |= CTS_SPI_VALID_SYNC_OFFSET;
2045 
2046 	spi->valid |= CTS_SPI_VALID_BUS_WIDTH;
2047 	spi->bus_width = bus_width;
2048 
2049 	if (cts->ccb_h.target_lun != CAM_LUN_WILDCARD) {
2050 		scsi->valid = CTS_SCSI_VALID_TQ;
2051 		spi->valid |= CTS_SPI_VALID_DISC;
2052 	} else
2053 		scsi->valid = 0;
2054 
2055         xpt_async(AC_TRANSFER_NEG, cts->ccb_h.path, cts);
2056 }
2057 
2058 static void
2059 btmapmboxes(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2060 {
2061 	struct bt_softc* bt;
2062 
2063 	bt = (struct bt_softc*)arg;
2064 	bt->mailbox_physbase = segs->ds_addr;
2065 }
2066 
2067 static void
2068 btmapccbs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2069 {
2070 	struct bt_softc* bt;
2071 
2072 	bt = (struct bt_softc*)arg;
2073 	bt->bt_ccb_physbase = segs->ds_addr;
2074 }
2075 
2076 static void
2077 btmapsgs(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2078 {
2079 
2080 	struct bt_softc* bt;
2081 
2082 	bt = (struct bt_softc*)arg;
2083 	SLIST_FIRST(&bt->sg_maps)->sg_physaddr = segs->ds_addr;
2084 }
2085 
2086 static void
2087 btpoll(struct cam_sim *sim)
2088 {
2089 	bt_intr(cam_sim_softc(sim));
2090 }
2091 
2092 void
2093 bttimeout(void *arg)
2094 {
2095 	struct bt_ccb	*bccb;
2096 	union  ccb	*ccb;
2097 	struct bt_softc *bt;
2098 
2099 	bccb = (struct bt_ccb *)arg;
2100 	ccb = bccb->ccb;
2101 	bt = (struct bt_softc *)ccb->ccb_h.ccb_bt_ptr;
2102 	xpt_print_path(ccb->ccb_h.path);
2103 	kprintf("CCB %p - timed out\n", (void *)bccb);
2104 
2105 	crit_enter();
2106 
2107 	if ((bccb->flags & BCCB_ACTIVE) == 0) {
2108 		xpt_print_path(ccb->ccb_h.path);
2109 		kprintf("CCB %p - timed out CCB already completed\n",
2110 		       (void *)bccb);
2111 		crit_exit();
2112 		return;
2113 	}
2114 
2115 	/*
2116 	 * In order to simplify the recovery process, we ask the XPT
2117 	 * layer to halt the queue of new transactions and we traverse
2118 	 * the list of pending CCBs and remove their timeouts. This
2119 	 * means that the driver attempts to clear only one error
2120 	 * condition at a time.  In general, timeouts that occur
2121 	 * close together are related anyway, so there is no benefit
2122 	 * in attempting to handle errors in parrallel.  Timeouts will
2123 	 * be reinstated when the recovery process ends.
2124 	 */
2125 	if ((bccb->flags & BCCB_DEVICE_RESET) == 0) {
2126 		struct ccb_hdr *ccb_h;
2127 
2128 		if ((bccb->flags & BCCB_RELEASE_SIMQ) == 0) {
2129 			xpt_freeze_simq(bt->sim, /*count*/1);
2130 			bccb->flags |= BCCB_RELEASE_SIMQ;
2131 		}
2132 
2133 		ccb_h = LIST_FIRST(&bt->pending_ccbs);
2134 		while (ccb_h != NULL) {
2135 			struct bt_ccb *pending_bccb;
2136 
2137 			pending_bccb = (struct bt_ccb *)ccb_h->ccb_bccb_ptr;
2138 			callout_stop(&ccb_h->timeout_ch);
2139 			ccb_h = LIST_NEXT(ccb_h, sim_links.le);
2140 		}
2141 	}
2142 
2143 	if ((bccb->flags & BCCB_DEVICE_RESET) != 0
2144 	 || bt->cur_outbox->action_code != BMBO_FREE
2145 	 || ((bccb->hccb.tag_enable == TRUE)
2146 	  && (bt->firmware_ver[0] < '5'))) {
2147 		/*
2148 		 * Try a full host adapter/SCSI bus reset.
2149 		 * We do this only if we have already attempted
2150 		 * to clear the condition with a BDR, or we cannot
2151 		 * attempt a BDR for lack of mailbox resources
2152 		 * or because of faulty firmware.  It turns out
2153 		 * that firmware versions prior to 5.xx treat BDRs
2154 		 * as untagged commands that cannot be sent until
2155 		 * all outstanding tagged commands have been processed.
2156 		 * This makes it somewhat difficult to use a BDR to
2157 		 * clear up a problem with an uncompleted tagged command.
2158 		 */
2159 		ccb->ccb_h.status = CAM_CMD_TIMEOUT;
2160 		btreset(bt, /*hardreset*/TRUE);
2161 		kprintf("%s: No longer in timeout\n", bt_name(bt));
2162 	} else {
2163 		/*
2164 		 * Send a Bus Device Reset message:
2165 		 * The target that is holding up the bus may not
2166 		 * be the same as the one that triggered this timeout
2167 		 * (different commands have different timeout lengths),
2168 		 * but we have no way of determining this from our
2169 		 * timeout handler.  Our strategy here is to queue a
2170 		 * BDR message to the target of the timed out command.
2171 		 * If this fails, we'll get another timeout 2 seconds
2172 		 * later which will attempt a bus reset.
2173 		 */
2174 		bccb->flags |= BCCB_DEVICE_RESET;
2175 		callout_reset(&ccb->ccb_h.timeout_ch, 2 * hz, bttimeout, bccb);
2176 
2177 		bt->recovery_bccb->hccb.opcode = INITIATOR_BUS_DEV_RESET;
2178 
2179 		/* No Data Transfer */
2180 		bt->recovery_bccb->hccb.datain = TRUE;
2181 		bt->recovery_bccb->hccb.dataout = TRUE;
2182 		bt->recovery_bccb->hccb.btstat = 0;
2183 		bt->recovery_bccb->hccb.sdstat = 0;
2184 		bt->recovery_bccb->hccb.target_id = ccb->ccb_h.target_id;
2185 
2186 		/* Tell the adapter about this command */
2187 		bt->cur_outbox->ccb_addr = btccbvtop(bt, bt->recovery_bccb);
2188 		bt->cur_outbox->action_code = BMBO_START;
2189 		bt_outb(bt, COMMAND_REG, BOP_START_MBOX);
2190 		btnextoutbox(bt);
2191 	}
2192 
2193 	crit_exit();
2194 }
2195 
2196