xref: /freebsd/sys/dev/ispfw/ispfw.c (revision 77a0943ded95b9e6438f7db70c4a28e4d93946d4)
1 /* $FreeBSD$ */
2 /*
3  * ISP Firmware Helper Pseudo Device for FreeBSD
4  *
5  * Copyright (c) 2000, by Matthew Jacob
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice immediately at the beginning of the file, without modification,
13  *    this list of conditions, and the following disclaimer.
14  * 2. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 
34 #include <dev/ispfw/asm_1040.h>
35 #include <dev/ispfw/asm_1080.h>
36 #include <dev/ispfw/asm_12160.h>
37 #include <dev/ispfw/asm_2100.h>
38 #include <dev/ispfw/asm_2200.h>
39 
40 #define	ISPFW_VERSION	0
41 
42 #define	PCI_PRODUCT_QLOGIC_ISP1020	0x1020
43 #define	PCI_PRODUCT_QLOGIC_ISP1080	0x1080
44 #define	PCI_PRODUCT_QLOGIC_ISP12160	0x1216
45 #define	PCI_PRODUCT_QLOGIC_ISP1240	0x1240
46 #define	PCI_PRODUCT_QLOGIC_ISP1280	0x1280
47 #define	PCI_PRODUCT_QLOGIC_ISP2100	0x2100
48 #define	PCI_PRODUCT_QLOGIC_ISP2200	0x2200
49 
50 typedef void ispfwfunc __P((int, int, int, const u_int16_t **));
51 extern ispfwfunc *isp_get_firmware_p;
52 static void isp_get_firmware __P((int, int, int, const u_int16_t **));
53 
54 static int ncallers = 0;
55 static const u_int16_t ***callp = NULL;
56 static int addcaller(const u_int16_t **);
57 
58 static int
59 addcaller(const u_int16_t **caller)
60 {
61 	const u_int16_t ***newcallp;
62 	int i;
63 	for (i = 0; i < ncallers; i++) {
64 		if (callp[i] == caller)
65 			return (1);
66 	}
67 	newcallp = malloc((ncallers + 1) * sizeof (const u_int16_t ***),
68 	    M_DEVBUF, M_NOWAIT);
69 	if (newcallp == NULL) {
70 		return (0);
71 	}
72 	for (i = 0; i < ncallers; i++) {
73 		newcallp[i] = callp[i];
74 	}
75 	newcallp[ncallers] = caller;
76 	if (ncallers++)
77 		free(callp, M_DEVBUF);
78 	callp = newcallp;
79 	return (1);
80 }
81 
82 static void
83 isp_get_firmware(int version, int tgtmode, int devid, const u_int16_t **ptrp)
84 {
85 	const u_int16_t *rp = NULL;
86 
87 	if (version == ISPFW_VERSION) {
88 		switch (devid) {
89 		case PCI_PRODUCT_QLOGIC_ISP1020:
90 			if (tgtmode)
91 				rp = isp_1040_risc_code_it;
92 			else
93 				rp = isp_1040_risc_code;
94 			break;
95 		case PCI_PRODUCT_QLOGIC_ISP1080:
96 		case PCI_PRODUCT_QLOGIC_ISP1240:
97 		case PCI_PRODUCT_QLOGIC_ISP1280:
98 			if (tgtmode)
99 				rp = isp_1080_risc_code_it;
100 			else
101 				rp = isp_1080_risc_code;
102 			break;
103 		case PCI_PRODUCT_QLOGIC_ISP12160:
104 			rp = isp_12160_risc_code;
105 			break;
106 		case PCI_PRODUCT_QLOGIC_ISP2100:
107 			rp = isp_2100_risc_code;
108 			break;
109 		case PCI_PRODUCT_QLOGIC_ISP2200:
110 			rp = isp_2200_risc_code;
111 		default:
112 			break;
113 		}
114 	}
115 	if (rp && addcaller(ptrp)) {
116 		*ptrp = rp;
117 	}
118 }
119 
120 static int
121 isp_module_handler(module_t mod, int what, void *arg)
122 {
123 	switch (what) {
124 	case MOD_LOAD:
125 		isp_get_firmware_p = isp_get_firmware;
126 		break;
127 	case MOD_UNLOAD:
128 		isp_get_firmware_p = NULL;
129 		if (ncallers)  {
130 			int i;
131 			for (i = 0; i < ncallers; i++) {
132 				*callp[i] = NULL;
133 			}
134 			free(callp, M_DEVBUF);
135 		}
136 		break;
137 	default:
138 		break;
139 	}
140 	return (0);
141 }
142 static moduledata_t ispfw_mod = {
143 	"ispfw", isp_module_handler, NULL
144 };
145 DECLARE_MODULE(ispfw, ispfw_mod, SI_SUB_DRIVERS, SI_ORDER_THIRD);
146 MODULE_VERSION(ispfw, ISPFW_VERSION);
147 MODULE_DEPEND(ispfw, isp, 1, 1, 1);
148