4ca642b0302b129c9195ce0d268f02793b2e29d3
[dragonfly.git] / sys / dev / disk / ispfw / ispfw.c
1 /* $FreeBSD: src/sys/dev/ispfw/ispfw.c,v 1.2.2.5 2002/10/12 00:13:09 mjacob Exp $ */
2 /* $DragonFly: src/sys/dev/disk/ispfw/ispfw.c,v 1.4 2003/08/27 10:35:17 rob Exp $ */
3 /*
4  * ISP Firmware Helper Pseudo Device for FreeBSD
5  *
6  * Copyright (c) 2000, by Matthew Jacob
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 immediately at the beginning of the file, without modification,
14  *    this list of conditions, and the following disclaimer.
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
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/buf.h>
34 #include <sys/disk.h>
35 #include <sys/kernel.h>
36 #include <sys/malloc.h>
37 #include <sys/linker.h>
38
39 #include "asm_1040.h"
40 #include "asm_1080.h"
41 #include "asm_12160.h"
42 #include "asm_2100.h"
43 #include "asm_2200.h"
44 #include "asm_2300.h"
45
46 #define ISPFW_VERSION   0
47
48 #define PCI_PRODUCT_QLOGIC_ISP1020      0x1020
49 #define PCI_PRODUCT_QLOGIC_ISP1080      0x1080
50 #define PCI_PRODUCT_QLOGIC_ISP10160     0x1016
51 #define PCI_PRODUCT_QLOGIC_ISP12160     0x1216
52 #define PCI_PRODUCT_QLOGIC_ISP1240      0x1240
53 #define PCI_PRODUCT_QLOGIC_ISP1280      0x1280
54 #define PCI_PRODUCT_QLOGIC_ISP2100      0x2100
55 #define PCI_PRODUCT_QLOGIC_ISP2200      0x2200
56 #define PCI_PRODUCT_QLOGIC_ISP2300      0x2300
57 #define PCI_PRODUCT_QLOGIC_ISP2312      0x2312
58
59 typedef void ispfwfunc (int, int, int, const u_int16_t **);
60 extern ispfwfunc *isp_get_firmware_p;
61 static void isp_get_firmware (int, int, int, const u_int16_t **);
62
63 static int ncallers = 0;
64 static const u_int16_t ***callp = NULL;
65 static int addcaller(const u_int16_t **);
66
67 static int
68 addcaller(const u_int16_t **caller)
69 {
70         const u_int16_t ***newcallp;
71         int i;
72         for (i = 0; i < ncallers; i++) {
73                 if (callp[i] == caller)
74                         return (1);
75         }
76         newcallp = malloc((ncallers + 1) * sizeof (const u_int16_t ***),
77             M_DEVBUF, M_NOWAIT);
78         if (newcallp == NULL) {
79                 return (0);
80         }
81         for (i = 0; i < ncallers; i++) {
82                 newcallp[i] = callp[i];
83         }
84         newcallp[ncallers] = caller;
85         if (ncallers++)
86                 free(callp, M_DEVBUF);
87         callp = newcallp;
88         return (1);
89 }
90
91 static void
92 isp_get_firmware(int version, int tgtmode, int devid, const u_int16_t **ptrp)
93 {
94         const u_int16_t *rp = NULL;
95
96         if (version == ISPFW_VERSION) {
97                 switch (devid) {
98                 case PCI_PRODUCT_QLOGIC_ISP1020:
99                         if (tgtmode)
100                                 rp = isp_1040_risc_code_it;
101                         else
102                                 rp = isp_1040_risc_code;
103                         break;
104                 case PCI_PRODUCT_QLOGIC_ISP1080:
105                 case PCI_PRODUCT_QLOGIC_ISP1240:
106                 case PCI_PRODUCT_QLOGIC_ISP1280:
107                         if (tgtmode)
108                                 rp = isp_1080_risc_code_it;
109                         else
110                                 rp = isp_1080_risc_code;
111                         break;
112                 case PCI_PRODUCT_QLOGIC_ISP10160:
113                 case PCI_PRODUCT_QLOGIC_ISP12160:
114                         if (tgtmode)
115                                 rp = isp_12160_risc_code_it;
116                         else
117                                 rp = isp_12160_risc_code;
118                         break;
119                 case PCI_PRODUCT_QLOGIC_ISP2100:
120                         rp = isp_2100_risc_code;
121                         break;
122                 case PCI_PRODUCT_QLOGIC_ISP2200:
123                         rp = isp_2200_risc_code;
124                         break;
125                 case PCI_PRODUCT_QLOGIC_ISP2300:
126                 case PCI_PRODUCT_QLOGIC_ISP2312:
127                         rp = isp_2300_risc_code;
128                         break;
129                 default:
130                         break;
131                 }
132         }
133         if (rp && addcaller(ptrp)) {
134                 *ptrp = rp;
135         }
136 }
137
138 static int
139 isp_module_handler(module_t mod, int what, void *arg)
140 {
141         switch (what) {
142         case MOD_LOAD:
143                 isp_get_firmware_p = isp_get_firmware;
144                 break;
145         case MOD_UNLOAD:
146                 isp_get_firmware_p = NULL;
147                 if (ncallers)  {
148                         int i;
149                         for (i = 0; i < ncallers; i++) {
150                                 *callp[i] = NULL;
151                         }
152                         free(callp, M_DEVBUF);
153                 }
154                 break;
155         default:
156                 break;
157         }
158         return (0);
159 }
160 static moduledata_t ispfw_mod = {
161         "ispfw", isp_module_handler, NULL
162 };
163 DECLARE_MODULE(ispfw, ispfw_mod, SI_SUB_DRIVERS, SI_ORDER_THIRD);