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