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