isp_pci.c revision 1.8 1 /* $NetBSD: isp_pci.c,v 1.8 1997/03/28 22:16:06 cgd Exp $ */
2
3 /*
4 * PCI specific probe and attach routines for Qlogic ISP SCSI adapters.
5 *
6 * Copyright (c) 1997 by Matthew Jacob
7 * NASA AMES Research Center
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice immediately at the beginning of the file, without modification,
15 * this list of conditions, and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. The name of the author may not be used to endorse or promote products
20 * derived from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
26 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
33 */
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/malloc.h>
38 #include <sys/kernel.h>
39 #include <sys/queue.h>
40 #include <sys/device.h>
41 #include <machine/bus.h>
42 #include <machine/intr.h>
43 #include <scsi/scsi_all.h>
44 #include <scsi/scsiconf.h>
45 #include <dev/pci/pcireg.h>
46 #include <dev/pci/pcivar.h>
47 #include <dev/pci/pcidevs.h>
48 #include <vm/vm.h>
49
50 #include <dev/ic/ispreg.h>
51 #include <dev/ic/ispvar.h>
52 #include <dev/ic/ispmbox.h>
53 #include <dev/microcode/isp/asm_pci.h>
54
55 #ifdef __alpha__ /* XXX */
56 /* XXX XXX NEED REAL DMA MAPPING SUPPORT XXX XXX */
57 extern vm_offset_t alpha_XXX_dmamap(vm_offset_t);
58 #undef vtophys
59 #define vtophys(va) alpha_XXX_dmamap((vm_offset_t) va)
60 #endif
61 #define KVTOPHYS(x) vtophys(x)
62
63
64 static u_int16_t isp_pci_rd_reg __P((struct ispsoftc *, int));
65 static void isp_pci_wr_reg __P((struct ispsoftc *, int, u_int16_t));
66 static vm_offset_t
67 isp_pci_mbxdma __P((struct ispsoftc *, vm_offset_t, u_int32_t));
68 static int
69 isp_pci_dmasetup __P((struct ispsoftc *, struct scsi_xfer *, ispreq_t *,
70 u_int8_t *, u_int8_t));
71
72 static void isp_pci_reset1 __P((struct ispsoftc *));
73
74 static struct ispmdvec mdvec = {
75 isp_pci_rd_reg,
76 isp_pci_wr_reg,
77 isp_pci_mbxdma,
78 isp_pci_dmasetup,
79 NULL,
80 NULL,
81 isp_pci_reset1,
82 ISP_RISC_CODE,
83 ISP_CODE_LENGTH,
84 ISP_CODE_ORG,
85 /* BIU_PCI_CONF1_FIFO_16 | BIU_BURST_ENABLE */ 0
86 };
87
88 #define PCI_QLOGIC_ISP \
89 ((PCI_PRODUCT_QLOGIC_ISP1020 << 16) | PCI_VENDOR_QLOGIC)
90
91 #define IO_MAP_REG 0x10
92 #define MEM_MAP_REG 0x14
93
94 int isp_pci_prefer_io = 0; /* 1 -> map via I/O (patchable data) */
95
96
97 #ifdef __BROKEN_INDIRECT_CONFIG
98 static int isp_pci_probe __P((struct device *, void *, void *));
99 #else
100 static int isp_pci_probe __P((struct device *, struct cfdata *, void *));
101 #endif
102 static void isp_pci_attach __P((struct device *, struct device *, void *));
103
104 struct isp_pcisoftc {
105 struct ispsoftc pci_isp;
106 bus_space_tag_t pci_st;
107 bus_space_handle_t pci_sh;
108 void * pci_ih;
109 };
110
111 struct cfattach isp_pci_ca = {
112 sizeof (struct isp_pcisoftc), isp_pci_probe, isp_pci_attach
113 };
114
115 static int
116 isp_pci_probe(parent, match, aux)
117 struct device *parent;
118 #ifdef __BROKEN_INDIRECT_CONFIG
119 void *match, *aux;
120 #else
121 struct cfdata *match;
122 void *aux;
123 #endif
124 {
125 struct pci_attach_args *pa = aux;
126
127 if (pa->pa_id == PCI_QLOGIC_ISP) {
128 return (1);
129 } else {
130 return (0);
131 }
132 }
133
134
135 static void
136 isp_pci_attach(parent, self, aux)
137 struct device *parent, *self;
138 void *aux;
139 {
140 struct pci_attach_args *pa = aux;
141 struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) self;
142 bus_addr_t busbase;
143 bus_size_t bussize;
144 bus_space_tag_t st;
145 bus_space_handle_t sh;
146 pci_intr_handle_t ih;
147 const char *intrstr;
148
149 if (isp_pci_prefer_io) {
150 if (pci_io_find(pa->pa_pc, pa->pa_tag, IO_MAP_REG, &busbase,
151 &bussize)) {
152 printf(": unable to find PCI I/O base\n");
153 return;
154 }
155 st = pa->pa_iot;
156 if (bus_space_map(st, busbase, bussize, 0, &sh)) {
157 printf(": unable to map I/O registers\n");
158 return;
159 }
160 } else {
161 if (pci_mem_find(pa->pa_pc, pa->pa_tag, MEM_MAP_REG, &busbase,
162 &bussize, NULL)) {
163 printf(": unable to find PCI memory base\n");
164 return;
165 }
166 st = pa->pa_memt;
167 if (bus_space_map(st, busbase, bussize, 0, &sh)) {
168 printf(": unable to map memory registers\n");
169 return;
170 }
171 }
172 printf("\n");
173
174 pcs->pci_st = st;
175 pcs->pci_sh = sh;
176 pcs->pci_isp.isp_mdvec = &mdvec;
177 isp_reset(&pcs->pci_isp);
178 if (pcs->pci_isp.isp_state != ISP_RESETSTATE) {
179 return;
180 }
181 isp_init(&pcs->pci_isp);
182 if (pcs->pci_isp.isp_state != ISP_INITSTATE) {
183 isp_uninit(&pcs->pci_isp);
184 return;
185 }
186
187 if (pci_intr_map(pa->pa_pc, pa->pa_intrtag, pa->pa_intrpin,
188 pa->pa_intrline, &ih)) {
189 printf("%s: couldn't map interrupt\n", pcs->pci_isp.isp_name);
190 isp_uninit(&pcs->pci_isp);
191 return;
192 }
193
194 intrstr = pci_intr_string(pa->pa_pc, ih);
195 if (intrstr == NULL)
196 intrstr = "<I dunno>";
197 pcs->pci_ih =
198 pci_intr_establish(pa->pa_pc, ih, IPL_BIO, isp_intr, &pcs->pci_isp);
199 if (pcs->pci_ih == NULL) {
200 printf("%s: couldn't establish interrupt at %s\n",
201 pcs->pci_isp.isp_name, intrstr);
202 isp_uninit(&pcs->pci_isp);
203 return;
204 }
205 printf("%s: interrupting at %s\n", pcs->pci_isp.isp_name, intrstr);
206
207 /*
208 * Do Generic attach now.
209 */
210 isp_attach(&pcs->pci_isp);
211 if (pcs->pci_isp.isp_state != ISP_RUNSTATE) {
212 isp_uninit(&pcs->pci_isp);
213 }
214 }
215
216 #define PCI_BIU_REGS_OFF 0x00
217 #define PCI_MBOX_REGS_OFF 0x70
218 #define PCI_SXP_REGS_OFF 0x80
219 #define PCI_RISC_REGS_OFF 0x80
220
221 static u_int16_t
222 isp_pci_rd_reg(isp, regoff)
223 struct ispsoftc *isp;
224 int regoff;
225 {
226 struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
227 int offset;
228 if ((regoff & BIU_BLOCK) != 0) {
229 offset = PCI_BIU_REGS_OFF;
230 } else if ((regoff & MBOX_BLOCK) != 0) {
231 offset = PCI_MBOX_REGS_OFF;
232 } else if ((regoff & SXP_BLOCK) != 0) {
233 offset = PCI_SXP_REGS_OFF;
234 /*
235 * XXX
236 */
237 panic("SXP Registers not accessible yet!");
238 } else {
239 offset = PCI_RISC_REGS_OFF;
240 }
241 regoff &= 0xff;
242 offset += regoff;
243 return bus_space_read_2(pcs->pci_st, pcs->pci_sh, offset);
244 }
245
246 static void
247 isp_pci_wr_reg(isp, regoff, val)
248 struct ispsoftc *isp;
249 int regoff;
250 u_int16_t val;
251 {
252 struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
253 int offset;
254 if ((regoff & BIU_BLOCK) != 0) {
255 offset = PCI_BIU_REGS_OFF;
256 } else if ((regoff & MBOX_BLOCK) != 0) {
257 offset = PCI_MBOX_REGS_OFF;
258 } else if ((regoff & SXP_BLOCK) != 0) {
259 offset = PCI_SXP_REGS_OFF;
260 /*
261 * XXX
262 */
263 panic("SXP Registers not accessible yet!");
264 } else {
265 offset = PCI_RISC_REGS_OFF;
266 }
267 regoff &= 0xff;
268 offset += regoff;
269 bus_space_write_2(pcs->pci_st, pcs->pci_sh, offset, val);
270 }
271
272 static vm_offset_t
273 isp_pci_mbxdma(isp, kva, len)
274 struct ispsoftc *isp;
275 vm_offset_t kva;
276 u_int32_t len;
277 {
278 vm_offset_t pg, start, s1;
279
280 start = KVTOPHYS(kva);
281
282 pg = kva + NBPG;
283 s1 = (start >> PGSHIFT) + 1;
284 len -= NBPG;
285
286 while ((int32_t)len > 0) {
287 if (s1 != (KVTOPHYS(pg) >> PGSHIFT)) {
288 printf("%s: mailboxes across noncontiguous pages\n",
289 isp->isp_name);
290 return ((vm_offset_t) 0);
291 }
292 len -= NBPG;
293 pg += NBPG;
294 s1++;
295 }
296 return (start);
297 }
298
299 /*
300 * TODO: reduce the number of segments by
301 * cchecking for adjacent physical page.
302 */
303
304 static int
305 isp_pci_dmasetup(isp, xs, rq, iptrp, optr)
306 struct ispsoftc *isp;
307 struct scsi_xfer *xs;
308 ispreq_t *rq;
309 u_int8_t *iptrp;
310 u_int8_t optr;
311 {
312 ispcontreq_t *crq;
313 unsigned long thiskv, nextkv;
314 int datalen, amt;
315
316 if (xs->datalen == 0) {
317 rq->req_seg_count = 1;
318 rq->req_flags |= REQFLAG_DATA_IN;
319 return (0);
320 }
321
322 if (xs->flags & SCSI_DATA_IN) {
323 rq->req_flags |= REQFLAG_DATA_IN;
324 } else {
325 rq->req_flags |= REQFLAG_DATA_OUT;
326 }
327 datalen = xs->datalen;
328 thiskv = (unsigned long) xs->data;
329
330 while (datalen && rq->req_seg_count < ISP_RQDSEG) {
331 nextkv = (thiskv + NBPG) & ~(NBPG-1);
332 amt = nextkv - thiskv;
333 if (amt > datalen)
334 amt = datalen;
335 rq->req_dataseg[rq->req_seg_count].ds_count = amt;
336 rq->req_dataseg[rq->req_seg_count].ds_base = KVTOPHYS(thiskv);
337 #if 0
338 printf("%s: seg%d: 0x%lx..0x%lx\n", isp->isp_name,
339 rq->req_seg_count, thiskv,
340 thiskv + (unsigned long) amt);
341 #endif
342 datalen -= amt;
343 thiskv = nextkv;
344 rq->req_seg_count++;
345 }
346
347 if (datalen == 0) {
348 return (0);
349 }
350
351 do {
352 int seg;
353 crq = (ispcontreq_t *) &isp->isp_rquest[*iptrp][0];
354 *iptrp = (*iptrp + 1) & (RQUEST_QUEUE_LEN - 1);
355 if (*iptrp == optr) {
356 printf("%s: Request Queue Overflow++\n",
357 isp->isp_name);
358 return (1);
359 }
360 rq->req_header.rqs_entry_count++;
361 bzero((void *)crq, sizeof (*crq));
362 crq->req_header.rqs_entry_count = 1;
363 crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
364 seg = 0;
365 while (datalen && seg < ISP_CDSEG) {
366 nextkv = (thiskv + NBPG) & ~(NBPG-1);
367 amt = nextkv - thiskv;
368 if (amt > datalen)
369 amt = datalen;
370 crq->req_dataseg[seg].ds_count = amt;
371 crq->req_dataseg[seg].ds_base = KVTOPHYS(thiskv);
372 #if 0
373 printf("%s: Cont%d seg%d: 0x%lx..0x%lx\n",
374 isp->isp_name, rq->req_header.rqs_entry_count,
375 seg, thiskv, thiskv + (unsigned long) amt);
376 #endif
377 datalen -= amt;
378 thiskv = nextkv;
379 rq->req_seg_count++;
380 seg++;
381 }
382 } while (datalen > 0);
383 return (0);
384 }
385
386 static void
387 isp_pci_reset1(isp)
388 struct ispsoftc *isp;
389 {
390 /* Make sure the BIOS is disabled */
391 isp_pci_wr_reg(isp, HCCR, PCI_HCCR_CMD_BIOS);
392 }
393