aha.c

来自「基于组件方式开发操作系统的OSKIT源代码」· C语言 代码 · 共 1,946 行 · 第 1/4 页

C
1,946
字号
/* * Generic register and struct definitions for the Adaptech 154x/164x * SCSI host adapters. Product specific probe and attach routines can * be found in: *      aha 1540/1542B/1542C/1542CF/1542CP	aha_isa.c * * Copyright (c) 1998 M. Warner Losh. * All Rights Reserved. * * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions, and the following disclaimer, *    without modification, immediately at the beginning of the file. * 2. The name of the author may not be used to endorse or promote products *    derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Derived from bt.c written by: * * Copyright (c) 1998 Justin T. Gibbs. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright *    notice, this list of conditions, and the following disclaimer, *    without modification, immediately at the beginning of the file. * 2. The name of the author may not be used to endorse or promote products *    derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * *      $Id: aha.c,v 1.19.2.3 1999/05/15 05:02:49 imp Exp $ */#include "pnp.h"#include <sys/param.h>#include <sys/systm.h> #include <sys/malloc.h>#include <sys/buf.h>#include <sys/kernel.h>#include <sys/sysctl.h> #include <machine/bus_pio.h>#include <machine/bus.h>#include <machine/clock.h>#include <cam/cam.h>#include <cam/cam_ccb.h>#include <cam/cam_sim.h>#include <cam/cam_xpt_sim.h>#include <cam/cam_debug.h>#include <cam/scsi/scsi_message.h>#include <vm/vm.h>#include <vm/pmap.h>#if NPNP > 0#include <i386/isa/isa_device.h>#include <i386/isa/pnp.h>		/* XXX pnp isn't x86 only */#endif #include <dev/aha/ahareg.h>struct aha_softc *aha_softcs[NAHATOT];#define MIN(a, b) ((a) < (b) ? (a) : (b))#define	PRVERB(x) if (bootverbose) printf x/* Macro to determine that a rev is potentially a new valid one * so that the driver doesn't keep breaking on new revs as it * did for the CF and CP. */#define PROBABLY_NEW_BOARD(REV) (REV > 0x43 && REV < 0x56)/* MailBox Management functions */static __inline void	ahanextinbox(struct aha_softc *aha);static __inline void	ahanextoutbox(struct aha_softc *aha);static __inline voidahanextinbox(struct aha_softc *aha){	if (aha->cur_inbox == aha->last_inbox)		aha->cur_inbox = aha->in_boxes;	else		aha->cur_inbox++;}static __inline voidahanextoutbox(struct aha_softc *aha){	if (aha->cur_outbox == aha->last_outbox)		aha->cur_outbox = aha->out_boxes;	else		aha->cur_outbox++;}#define ahautoa24(u,s3)			\	(s3)[0] = ((u) >> 16) & 0xff;	\	(s3)[1] = ((u) >> 8) & 0xff;	\	(s3)[2] = (u) & 0xff;#define aha_a24tou(s3) \	(((s3)[0] << 16) | ((s3)[1] << 8) | (s3)[2])/* CCB Mangement functions */static __inline u_int32_t		ahaccbvtop(struct aha_softc *aha,						  struct aha_ccb *accb);static __inline struct aha_ccb*		ahaccbptov(struct aha_softc *aha,						  u_int32_t ccb_addr);static __inline u_int32_tahaccbvtop(struct aha_softc *aha, struct aha_ccb *accb){	return (aha->aha_ccb_physbase	      + (u_int32_t)((caddr_t)accb - (caddr_t)aha->aha_ccb_array));}static __inline struct aha_ccb *ahaccbptov(struct aha_softc *aha, u_int32_t ccb_addr){	return (aha->aha_ccb_array +	      + ((struct aha_ccb*)ccb_addr-(struct aha_ccb*)aha->aha_ccb_physbase));}static struct aha_ccb*	ahagetccb(struct aha_softc *aha);static __inline void	ahafreeccb(struct aha_softc *aha, struct aha_ccb *accb);static void		ahaallocccbs(struct aha_softc *aha);static bus_dmamap_callback_t ahaexecuteccb;static void		ahadone(struct aha_softc *aha, struct aha_ccb *accb,			       aha_mbi_comp_code_t comp_code);/* Host adapter command functions */static int	ahareset(struct aha_softc* aha, int hard_reset);/* Initialization functions */static int			ahainitmboxes(struct aha_softc *aha);static bus_dmamap_callback_t	ahamapmboxes;static bus_dmamap_callback_t	ahamapccbs;static bus_dmamap_callback_t	ahamapsgs;/* Transfer Negotiation Functions */static void ahafetchtransinfo(struct aha_softc *aha,			     struct ccb_trans_settings *cts);/* CAM SIM entry points */#define ccb_accb_ptr spriv_ptr0#define ccb_aha_ptr spriv_ptr1static void	ahaaction(struct cam_sim *sim, union ccb *ccb);static void	ahapoll(struct cam_sim *sim);/* Our timeout handler */timeout_t ahatimeout;u_long aha_unit = 0;/* * Do our own re-probe protection until a configuration * manager can do it for us.  This ensures that we don't * reprobe a card already found by the EISA or PCI probes. */struct aha_isa_port aha_isa_ports[] ={	{ 0x130, 0, 4 },	{ 0x134, 0, 5 },	{ 0x230, 0, 2 },	{ 0x234, 0, 3 },	{ 0x330, 0, 0 },	{ 0x334, 0, 1 }};/* * I/O ports listed in the order enumerated by the * card for certain op codes. */u_int16_t aha_board_ports[] ={	0x330,	0x334,	0x230,	0x234,	0x130,	0x134};/* Exported functions */struct aha_softc *aha_alloc(int unit, bus_space_tag_t tag, bus_space_handle_t bsh){	struct  aha_softc *aha;  	if (unit != AHA_TEMP_UNIT) {		if (unit >= NAHATOT) {			printf("aha: unit number (%d) too high\n", unit);			return NULL;		}		/*		 * Allocate a storage area for us		 */		if (aha_softcs[unit]) {    			printf("aha%d: memory already allocated\n", unit);			return NULL;    		}	}	aha = malloc(sizeof(struct aha_softc), M_DEVBUF, M_NOWAIT);	if (!aha) {		printf("aha%d: cannot malloc!\n", unit);		return NULL;    	}	bzero(aha, sizeof(struct aha_softc));	SLIST_INIT(&aha->free_aha_ccbs);	LIST_INIT(&aha->pending_ccbs);	SLIST_INIT(&aha->sg_maps);	aha->unit = unit;	aha->tag = tag;	aha->bsh = bsh;	aha->ccb_sg_opcode = INITIATOR_SG_CCB_WRESID;	aha->ccb_ccb_opcode = INITIATOR_CCB_WRESID;	if (aha->unit != AHA_TEMP_UNIT) {		aha_softcs[unit] = aha;	}	return (aha);}voidaha_free(struct aha_softc *aha){	switch (aha->init_level) {	default:	case 8:	{		struct sg_map_node *sg_map;		while ((sg_map = SLIST_FIRST(&aha->sg_maps))!= NULL) {			SLIST_REMOVE_HEAD(&aha->sg_maps, links);			bus_dmamap_unload(aha->sg_dmat,					  sg_map->sg_dmamap);			bus_dmamem_free(aha->sg_dmat, sg_map->sg_vaddr,					sg_map->sg_dmamap);			free(sg_map, M_DEVBUF);		}		bus_dma_tag_destroy(aha->sg_dmat);	}	case 7:		bus_dmamap_unload(aha->ccb_dmat, aha->ccb_dmamap);	case 6:		bus_dmamap_destroy(aha->ccb_dmat, aha->ccb_dmamap);		bus_dmamem_free(aha->ccb_dmat, aha->aha_ccb_array,				aha->ccb_dmamap);	case 5:		bus_dma_tag_destroy(aha->ccb_dmat);	case 4:		bus_dmamap_unload(aha->mailbox_dmat, aha->mailbox_dmamap);	case 3:		bus_dmamem_free(aha->mailbox_dmat, aha->in_boxes,				aha->mailbox_dmamap);		bus_dmamap_destroy(aha->mailbox_dmat, aha->mailbox_dmamap);	case 2:		bus_dma_tag_destroy(aha->buffer_dmat);	case 1:		bus_dma_tag_destroy(aha->mailbox_dmat);	case 0:	}	if (aha->unit != AHA_TEMP_UNIT) {		aha_softcs[aha->unit] = NULL;	}	free(aha, M_DEVBUF);}/* * Probe the adapter and verify that the card is an Adaptec. */intaha_probe(struct aha_softc* aha){	u_int	 status;	u_int	 intstat;	int	 error;	board_id_data_t	board_id;	/*	 * See if the three I/O ports look reasonable.	 * Touch the minimal number of registers in the	 * failure case.	 */	status = aha_inb(aha, STATUS_REG);	if ((status == 0)	 || (status & (DIAG_ACTIVE|CMD_REG_BUSY|		       STATUS_REG_RSVD)) != 0) {		PRVERB(("%s: status reg test failed %x\n", aha_name(aha),			status));		return (ENXIO);	}	intstat = aha_inb(aha, INTSTAT_REG);	if ((intstat & INTSTAT_REG_RSVD) != 0) {		PRVERB(("%s: Failed Intstat Reg Test\n", aha_name(aha)));		return (ENXIO);	}	/*	 * Looking good so far.  Final test is to reset the	 * adapter and fetch the board ID and ensure we aren't	 * looking at a BusLogic.	 */	if ((error = ahareset(aha, /*hard_reset*/TRUE)) != 0) {		if (bootverbose)			printf("%s: Failed Reset\n", aha_name(aha));		return (ENXIO);	}	/*	 * Get the board ID.  We use this to see if we're dealing with	 * a buslogic card or a aha card (or clone).	 */	error = aha_cmd(aha, AOP_INQUIRE_BOARD_ID, NULL, /*parmlen*/0,		       (u_int8_t*)&board_id, sizeof(board_id),		       DEFAULT_CMD_TIMEOUT);	if (error != 0) {		PRVERB(("%s: INQUIRE failed %x\n", aha_name(aha), error));		return (ENXIO);	}	aha->fw_major = board_id.firmware_rev_major;	aha->fw_minor = board_id.firmware_rev_minor;	aha->boardid = board_id.board_type;	/*	 * The Buslogic cards have an id of either 0x41 or 0x42.  So	 * if those come up in the probe, we test the geometry register	 * of the board.  Adaptec boards that are this old will not have	 * this register, and return 0xff, while buslogic cards will return	 * something different.  Other aha cards return 0x00 or 0x7f, so	 * use them as well.  No buslogic cards seem to return these	 * values.	 *	 * It appears that for reasons unknow, for the for the	 * aha-1542B cards, we need to wait a little bit before trying	 * to read the geometry register.  I picked 10ms since we have	 * reports that a for loop to 1000 did the trick, and this	 * errs on the side of conservatism.  Besides, no one will	 * notice a 10mS delay here, even the 1542B card users :-)	 *	 * Some compatible cards return 0 here.	 *	 * XXX I'm not sure how this will impact other cloned cards 	 *	 * This really should be replaced with the esetup command, since	 * that appears to be more reliable.	 */	if (aha->boardid <= 0x42) {		/* Wait 10ms before reading */		DELAY(10000);		status = aha_inb(aha, GEOMETRY_REG);		if (status != 0xff && status != 0x00 && status != 0x7f) {			PRVERB(("%s: Geometry Register test failed\n",				aha_name(aha)));			return (ENXIO);		}	}		return (0);}/* * Pull the boards setup information and record it in our softc. */intaha_fetch_adapter_info(struct aha_softc *aha){	setup_data_t	setup_info;	config_data_t config_data;	u_int8_t length_param;	int	 error;	struct	aha_extbios extbios;		switch (aha->boardid) {	case BOARD_1540_16HEAD_BIOS:		snprintf(aha->model, sizeof(aha->model), "1540 16 head BIOS");		break;	case BOARD_1540_64HEAD_BIOS:		snprintf(aha->model, sizeof(aha->model), "1540 64 head BIOS");		break;	case BOARD_1542:		snprintf(aha->model, sizeof(aha->model), "1540/1542 64 head BIOS");		break;	case BOARD_1640:		snprintf(aha->model, sizeof(aha->model), "1640");		break;	case BOARD_1740:		snprintf(aha->model, sizeof(aha->model), "1740A/1742A/1744");		break;	case BOARD_1542C:		snprintf(aha->model, sizeof(aha->model), "1542C");		break;	case BOARD_1542CF:		snprintf(aha->model, sizeof(aha->model), "1542CF");		break;	case BOARD_1542CP:		snprintf(aha->model, sizeof(aha->model), "1542CP");		break;	default:		snprintf(aha->model, sizeof(aha->model), "Unknown");		break;	}	/*	 * If we are a new type of 1542 board (anything newer than a 1542C)	 * then disable the extended bios so that the	 * mailbox interface is unlocked.	 * This is also true for the 1542B Version 3.20. First Adaptec	 * board that supports >1Gb drives.	 * No need to check the extended bios flags as some of the	 * extensions that cause us problems are not flagged in that byte.	 */	if (PROBABLY_NEW_BOARD(aha->boardid) ||		(aha->boardid == 0x41		&& aha->fw_major == 0x31 && 		aha->fw_minor >= 0x34)) {		error = aha_cmd(aha, AOP_RETURN_EXT_BIOS_INFO, NULL,			/*paramlen*/0, (u_char *)&extbios, sizeof(extbios),			DEFAULT_CMD_TIMEOUT);		error = aha_cmd(aha, AOP_MBOX_IF_ENABLE, (u_int8_t *)&extbios,			/*paramlen*/2, NULL, 0, DEFAULT_CMD_TIMEOUT);	}	if (aha->boardid < 0x41)		printf("%s: Warning: aha-1542A won't likely work.\n",			aha_name(aha));	aha->max_sg = 17;		/* Need >= 17 to do 64k I/O */	aha->diff_bus = 0;	aha->extended_lun = 0;	aha->extended_trans = 0;	aha->max_ccbs = 16;	/* Determine Sync/Wide/Disc settings */	length_param = sizeof(setup_info);	error = aha_cmd(aha, AOP_INQUIRE_SETUP_INFO, &length_param,		       /*paramlen*/1, (u_int8_t*)&setup_info,		       sizeof(setup_info), DEFAULT_CMD_TIMEOUT);	if (error != 0) {		printf("%s: aha_fetch_adapter_info - Failed "		       "Get Setup Info\n", aha_name(aha));		return (error);	}	if (setup_info.initiate_sync != 0) {		aha->sync_permitted = ALL_TARGETS;	}	aha->disc_permitted = ALL_TARGETS;	/* We need as many mailboxes as we can have ccbs */	aha->num_boxes = aha->max_ccbs;	/* Determine our SCSI ID */		error = aha_cmd(aha, AOP_INQUIRE_CONFIG, NULL, /*parmlen*/0,		       (u_int8_t*)&config_data, sizeof(config_data),		       DEFAULT_CMD_TIMEOUT);	if (error != 0) {		printf("%s: aha_fetch_adapter_info - Failed Get Config\n",		       aha_name(aha));

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?