xref: /freebsd/sys/dev/isp/isp_pci.c (revision 11afcc8f9f96d657b8e6f7547c02c1957331fc96)
1 /* $FreeBSD$ */
2 /* $Id: isp_pci.c,v 1.1 1998/04/22 18:10:34 mjacob Exp $ */
3 /*
4  * PCI specific probe and attach routines for Qlogic ISP SCSI adapters.
5  * FreeBSD Version.
6  *
7  *---------------------------------------
8  * Copyright (c) 1997, 1998 by Matthew Jacob
9  * NASA/Ames Research Center
10  * All rights reserved.
11  *---------------------------------------
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice immediately at the beginning of the file, without modification,
18  *    this list of conditions, and the following disclaimer.
19  * 2. Redistributions in binary form must reproduce the above copyright
20  *    notice, this list of conditions and the following disclaimer in the
21  *    documentation and/or other materials provided with the distribution.
22  * 3. The name of the author may not be used to endorse or promote products
23  *    derived from this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
29  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  */
37 #include <pci.h>
38 #if	NPCI > 0
39 
40 #include <dev/isp/isp_freebsd.h>
41 #include <dev/isp/asm_pci.h>
42 
43 #include <pci/pcireg.h>
44 #include <pci/pcivar.h>
45 
46 static u_int16_t isp_pci_rd_reg __P((struct ispsoftc *, int));
47 static void isp_pci_wr_reg __P((struct ispsoftc *, int, u_int16_t));
48 static int isp_pci_mbxdma __P((struct ispsoftc *));
49 static int isp_pci_dmasetup __P((struct ispsoftc *, ISP_SCSI_XFER_T *,
50 	ispreq_t *, u_int8_t *, u_int8_t));
51 
52 static void isp_pci_reset1 __P((struct ispsoftc *));
53 static void isp_pci_dumpregs __P((struct ispsoftc *));
54 
55 static struct ispmdvec mdvec = {
56 	isp_pci_rd_reg,
57 	isp_pci_wr_reg,
58 	isp_pci_mbxdma,
59 	isp_pci_dmasetup,
60 	NULL,
61 	NULL,
62 	isp_pci_reset1,
63 	isp_pci_dumpregs,
64 	ISP_RISC_CODE,
65 	ISP_CODE_LENGTH,
66 	ISP_CODE_ORG,
67 	ISP_CODE_VERSION,
68 	BIU_PCI_CONF1_FIFO_64 | BIU_BURST_ENABLE,
69 	60	/* MAGIC- all known PCI card implementations are 60MHz */
70 };
71 
72 static struct ispmdvec mdvec_2100 = {
73 	isp_pci_rd_reg,
74 	isp_pci_wr_reg,
75 	isp_pci_mbxdma,
76 	isp_pci_dmasetup,
77 	NULL,
78 	NULL,
79 	isp_pci_reset1,
80 	isp_pci_dumpregs,
81 	ISP2100_RISC_CODE,
82 	ISP2100_CODE_LENGTH,
83 	ISP2100_CODE_ORG,
84 	ISP2100_CODE_VERSION,
85 	BIU_PCI_CONF1_FIFO_64 | BIU_BURST_ENABLE,
86 	60	/* MAGIC- all known PCI card implementations are 60MHz */
87 };
88 
89 #ifndef	PCIM_CMD_INVEN
90 #define	PCIM_CMD_INVEN	0x10
91 #endif
92 #ifndef	PCIM_CMD_BUSMASTEREN
93 #define	PCIM_CMD_BUSMASTEREN	0x0004
94 #endif
95 
96 #ifndef	PCI_VENDOR_QLOGIC
97 #define	PCI_VENDOR_QLOGIC		0x1077
98 #endif
99 
100 #ifndef	PCI_PRODUCT_QLOGIC_ISP1020
101 #define	PCI_PRODUCT_QLOGIC_ISP1020	0x1020
102 #endif
103 
104 #define	PCI_QLOGIC_ISP	\
105 	((PCI_PRODUCT_QLOGIC_ISP1020 << 16) | PCI_VENDOR_QLOGIC)
106 
107 #ifndef	PCI_PRODUCT_QLOGIC_ISP2100
108 #define	PCI_PRODUCT_QLOGIC_ISP2100	0x2100
109 #endif
110 
111 #define	PCI_QLOGIC_ISP2100	\
112 	((PCI_PRODUCT_QLOGIC_ISP2100 << 16) | PCI_VENDOR_QLOGIC)
113 
114 #define IO_MAP_REG	0x10
115 #define MEM_MAP_REG	0x14
116 
117 
118 static char *isp_pci_probe __P((pcici_t tag, pcidi_t type));
119 static void isp_pci_attach __P((pcici_t config_d, int unit));
120 
121 
122 
123 #define	I386_BUS_SPACE_IO	0
124 #define	I386_BUS_SPACE_MEM	1
125 typedef int bus_space_tag_t;
126 typedef u_long bus_space_handle_t;
127 #define	bus_space_read_2(st, sh, offset)	\
128 	(st == I386_BUS_SPACE_IO)? \
129 		inw((u_int16_t)sh + offset) : *((u_int16_t *) sh)
130 #define	bus_space_write_2(st, sh, offset, val)	\
131 	if (st == I386_BUS_SPACE_IO) outw((u_int16_t)sh + offset, val); else \
132 		*((u_int16_t *) sh) = val
133 
134 
135 struct isp_pcisoftc {
136 	struct ispsoftc		pci_isp;
137         pcici_t			pci_id;
138 	bus_space_tag_t		pci_st;
139 	bus_space_handle_t	pci_sh;
140 	union {
141 		sdparam	_x;
142 		struct {
143 			fcparam _a;
144 			char _b[ISP2100_SCRLEN];
145 		} _y;
146 	} _z;
147 };
148 
149 static u_long isp_unit;
150 
151 struct pci_device isp_pci_driver = {
152 	"isp",
153 	isp_pci_probe,
154 	isp_pci_attach,
155 	&isp_unit,
156 	NULL
157 };
158 DATA_SET (pcidevice_set, isp_pci_driver);
159 
160 
161 static char *
162 isp_pci_probe(tag, type)
163 	pcici_t tag;
164 	pcidi_t type;
165 {
166 	static int oneshot = 1;
167 	char *x;
168 
169         switch (type) {
170 	case PCI_QLOGIC_ISP:
171 		x = "Qlogic ISP 10X0 PCI SCSI Adapter";
172 		break;
173 	case PCI_QLOGIC_ISP2100:
174 		x = "Qlogic ISP 2100 PCI FC-AL Adapter";
175 		break;
176 	default:
177 		return (NULL);
178 	}
179 	if (oneshot) {
180 		oneshot = 0;
181 		printf("***Qlogic ISP Driver, FreeBSD NonCam Version\n***%s\n",
182 			ISP_VERSION_STRING);
183 	}
184 	return (x);
185 }
186 
187 
188 static void
189 isp_pci_attach(config_id, unit)
190         pcici_t config_id;
191         int unit;
192 {
193 	int mapped;
194 	u_int16_t io_port;
195 	u_int32_t data;
196 	struct isp_pcisoftc *pcs;
197 	struct ispsoftc *isp;
198 	vm_offset_t vaddr, paddr;
199 	ISP_LOCKVAL_DECL;
200 
201 
202 	pcs = malloc(sizeof (struct isp_pcisoftc), M_DEVBUF, M_NOWAIT);
203 	if (pcs == NULL) {
204 		printf("isp%d: cannot allocate softc\n", unit);
205 		return;
206 	}
207 	bzero(pcs, sizeof (struct isp_pcisoftc));
208 
209 	vaddr = paddr = NULL;
210 	mapped = 0;
211 	data = pci_conf_read(config_id, PCI_COMMAND_STATUS_REG);
212 	if (mapped == 0 && (data & PCI_COMMAND_IO_ENABLE)) {
213 		if (pci_map_port(config_id, PCI_MAP_REG_START, &io_port)) {
214 			pcs->pci_st = I386_BUS_SPACE_IO;
215 			pcs->pci_sh = io_port;
216 			mapped++;
217 		}
218 	}
219 	if (mapped == 0 && (data & PCI_COMMAND_MEM_ENABLE)) {
220 		if (pci_map_mem(config_id, PCI_MAP_REG_START, &vaddr, &paddr)) {
221 			pcs->pci_st = I386_BUS_SPACE_MEM;
222 			pcs->pci_sh = vaddr;
223 			mapped++;
224 		}
225 	}
226 	if (mapped == 0) {
227 		printf("isp%d: unable to map any ports!\n", unit);
228 		free(pcs, M_DEVBUF);
229 		return;
230 	}
231 	printf("isp%d: using %s space register mapping\n", unit,
232 		pcs->pci_st == I386_BUS_SPACE_IO? "I/O" : "Memory");
233 
234 	isp = &pcs->pci_isp;
235 	(void) sprintf(isp->isp_name, "isp%d", unit);
236 	isp->isp_osinfo.unit = unit;
237 
238 	data = pci_conf_read(config_id, PCI_ID_REG);
239 	if (data == PCI_QLOGIC_ISP) {
240 		isp->isp_mdvec = &mdvec;
241 		isp->isp_type = ISP_HA_SCSI_UNKNOWN;
242 		isp->isp_param = &pcs->_z._x;
243 	} else if (data == PCI_QLOGIC_ISP2100) {
244 		isp->isp_mdvec = &mdvec_2100;
245 		isp->isp_type = ISP_HA_FC_2100;
246 		isp->isp_param = &pcs->_z._y._a;
247 
248 		ISP_LOCK;
249 		data = pci_conf_read(config_id, PCI_COMMAND_STATUS_REG);
250 		data |= PCIM_CMD_BUSMASTEREN | PCIM_CMD_INVEN;
251 		pci_conf_write(config_id, PCI_COMMAND_STATUS_REG, data);
252 
253 		/*
254 		 * Wierd- we need to clear the lsb in offset 0x30 to take the
255 		 * chip out of reset state.
256 		 */
257 		data = pci_conf_read(config_id, 0x30);
258 		data &= ~1;
259 		pci_conf_write(config_id, 0x30, data);
260 		ISP_UNLOCK;
261 	} else {
262 		free(pcs, M_DEVBUF);
263 		return;
264 	}
265 
266 	if (pci_map_int(config_id, (void (*)(void *))isp_intr,
267 	    (void *)isp, &IMASK) == 0) {
268 		printf("%s: could not map interrupt\n", isp->isp_name);
269 		free(pcs, M_DEVBUF);
270 		return;
271 	}
272 
273 	pcs->pci_id = config_id;
274 	ISP_LOCK;
275 	isp_reset(isp);
276 	if (isp->isp_state != ISP_RESETSTATE) {
277 		ISP_UNLOCK;
278 		free(pcs, M_DEVBUF);
279 		return;
280 	}
281 	isp_init(isp);
282 	if (isp->isp_state != ISP_INITSTATE) {
283 		isp_uninit(isp);
284 		ISP_UNLOCK;
285 		free(pcs, M_DEVBUF);
286 		return;
287 	}
288 	isp_attach(isp);
289 	if (isp->isp_state != ISP_RUNSTATE) {
290 		isp_uninit(isp);
291 		ISP_UNLOCK;
292 		free(pcs, M_DEVBUF);
293 		return;
294 	}
295 	ISP_UNLOCK;
296 }
297 
298 #define  PCI_BIU_REGS_OFF		BIU_REGS_OFF
299 
300 static u_int16_t
301 isp_pci_rd_reg(isp, regoff)
302 	struct ispsoftc *isp;
303 	int regoff;
304 {
305 	u_int16_t rv;
306 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
307 	int offset, oldsxp = 0;
308 
309 	if ((regoff & BIU_BLOCK) != 0) {
310 		offset = PCI_BIU_REGS_OFF;
311 	} else if ((regoff & MBOX_BLOCK) != 0) {
312 		if (isp->isp_type & ISP_HA_SCSI)
313 			offset = PCI_MBOX_REGS_OFF;
314 		else
315 			offset = PCI_MBOX_REGS2100_OFF;
316 	} else if ((regoff & SXP_BLOCK) != 0) {
317 		offset = PCI_SXP_REGS_OFF;
318 		/*
319 		 * We will assume that someone has paused the RISC processor.
320 		 */
321 		oldsxp = isp_pci_rd_reg(isp, BIU_CONF1);
322 		isp_pci_wr_reg(isp, BIU_CONF1, oldsxp & ~BIU_PCI_CONF1_SXP);
323 	} else {
324 		offset = PCI_RISC_REGS_OFF;
325 	}
326 	regoff &= 0xff;
327 	offset += regoff;
328 	rv = bus_space_read_2(pcs->pci_st, pcs->pci_sh, offset);
329 	if ((regoff & SXP_BLOCK) != 0) {
330 		isp_pci_wr_reg(isp, BIU_CONF1, oldsxp);
331 	}
332 	return (rv);
333 }
334 
335 static void
336 isp_pci_wr_reg(isp, regoff, val)
337 	struct ispsoftc *isp;
338 	int regoff;
339 	u_int16_t val;
340 {
341 	struct isp_pcisoftc *pcs = (struct isp_pcisoftc *) isp;
342 	int offset, oldsxp = 0;
343 	if ((regoff & BIU_BLOCK) != 0) {
344 		offset = PCI_BIU_REGS_OFF;
345 	} else if ((regoff & MBOX_BLOCK) != 0) {
346 		if (isp->isp_type & ISP_HA_SCSI)
347 			offset = PCI_MBOX_REGS_OFF;
348 		else
349 			offset = PCI_MBOX_REGS2100_OFF;
350 	} else if ((regoff & SXP_BLOCK) != 0) {
351 		offset = PCI_SXP_REGS_OFF;
352 		/*
353 		 * We will assume that someone has paused the RISC processor.
354 		 */
355 		oldsxp = isp_pci_rd_reg(isp, BIU_CONF1);
356 		isp_pci_wr_reg(isp, BIU_CONF1, oldsxp & ~BIU_PCI_CONF1_SXP);
357 	} else {
358 		offset = PCI_RISC_REGS_OFF;
359 	}
360 	regoff &= 0xff;
361 	offset += regoff;
362 	bus_space_write_2(pcs->pci_st, pcs->pci_sh, offset, val);
363 	if ((regoff & SXP_BLOCK) != 0) {
364 		isp_pci_wr_reg(isp, BIU_CONF1, oldsxp);
365 	}
366 }
367 
368 static int
369 isp_pci_mbxdma(isp)
370 	struct ispsoftc *isp;
371 {
372 	struct isp_pcisoftc *pci = (struct isp_pcisoftc *)isp;
373 	u_int32_t len;
374 	int rseg;
375 
376 	/* XXXX CHECK FOR ALIGNMENT */
377 	/*
378 	 * Allocate and map the request queue.
379 	 */
380 	len = ISP_QUEUE_SIZE(RQUEST_QUEUE_LEN(isp));
381 	isp->isp_rquest = malloc(len, M_DEVBUF, M_NOWAIT);
382 	if (isp->isp_rquest == NULL) {
383 		printf("%s: cannot malloc request queue\n", isp->isp_name);
384 		return (1);
385 	}
386 	isp->isp_rquest_dma = vtophys(isp->isp_rquest);
387 
388 #if	0
389 	printf("RQUEST=0x%x (0x%x)...", isp->isp_rquest, isp->isp_rquest_dma);
390 #endif
391 
392 	/*
393 	 * Allocate and map the result queue.
394 	 */
395 	len = ISP_QUEUE_SIZE(RESULT_QUEUE_LEN(isp));
396 	isp->isp_result = malloc(len, M_DEVBUF, M_NOWAIT);
397 	if (isp->isp_result == NULL) {
398 		free(isp->isp_rquest, M_DEVBUF);
399 		printf("%s: cannot malloc result queue\n", isp->isp_name);
400 		return (1);
401 	}
402 	isp->isp_result_dma = vtophys(isp->isp_result);
403 #if	0
404 	printf("RESULT=0x%x (0x%x)\n", isp->isp_result, isp->isp_result_dma);
405 #endif
406 	if (isp->isp_type & ISP_HA_FC) {
407 		fcparam *fcp = isp->isp_param;
408 		len = ISP2100_SCRLEN;
409 		fcp->isp_scratch = (volatile caddr_t) &pci->_z._y._b;
410 		fcp->isp_scdma = vtophys(fcp->isp_scratch);
411 	}
412 	return (0);
413 }
414 
415 static int
416 isp_pci_dmasetup(isp, xs, rq, iptrp, optr)
417 	struct ispsoftc *isp;
418 	ISP_SCSI_XFER_T *xs;
419 	ispreq_t *rq;
420 	u_int8_t *iptrp;
421 	u_int8_t optr;
422 {
423 	struct isp_pcisoftc *pci = (struct isp_pcisoftc *)isp;
424 	ispcontreq_t *crq;
425 	vm_offset_t vaddr;
426 	int drq, seglim;
427 	u_int32_t paddr, nextpaddr, datalen, size, *ctrp;
428 
429 	if (xs->datalen == 0) {
430 		rq->req_seg_count = 1;
431 		return (0);
432 	}
433 
434 	if (xs->flags & SCSI_DATA_IN) {
435 		drq = REQFLAG_DATA_IN;
436 	} else {
437 		drq = REQFLAG_DATA_OUT;
438 	}
439 
440 	if (isp->isp_type & ISP_HA_FC) {
441 		seglim = ISP_RQDSEG_T2;
442 		((ispreqt2_t *)rq)->req_totalcnt = xs->datalen;
443 		((ispreqt2_t *)rq)->req_flags |= drq;
444 	} else {
445 		seglim = ISP_RQDSEG;
446 		rq->req_flags |= drq;
447 	}
448 
449 	datalen = xs->datalen;;
450 	vaddr = (vm_offset_t) xs->data;
451 	paddr = vtophys(vaddr);
452 
453 	while (datalen != 0 && rq->req_seg_count < seglim) {
454 		if (isp->isp_type & ISP_HA_FC) {
455 			ispreqt2_t *rq2 = (ispreqt2_t *)rq;
456 			rq2->req_dataseg[rq2->req_seg_count].ds_base = paddr;
457 			ctrp = &rq2->req_dataseg[rq2->req_seg_count].ds_count;
458 		} else {
459 			rq->req_dataseg[rq->req_seg_count].ds_base = paddr;
460 			ctrp = &rq->req_dataseg[rq->req_seg_count].ds_count;
461 		}
462 		nextpaddr = paddr;
463 		*(ctrp) = 0;
464 
465 		while (datalen != 0 && paddr == nextpaddr) {
466 			nextpaddr = (paddr & (~PAGE_MASK)) + PAGE_SIZE;
467 			size = nextpaddr - paddr;
468 			if (size > datalen)
469 				size = datalen;
470 
471 			*(ctrp) += size;
472 			vaddr += size;
473 			datalen -= size;
474 			if (datalen != 0)
475 				paddr = vtophys(vaddr);
476 
477 		}
478 #if	0
479 		if (isp->isp_type & ISP_HA_FC) {
480 			ispreqt2_t *rq2 = (ispreqt2_t *)rq;
481 			printf("%s: seg0[%d] cnt 0x%x paddr 0x%08x\n",
482 			    isp->isp_name, rq->req_seg_count,
483 			    rq2->req_dataseg[rq2->req_seg_count].ds_count,
484 			    rq2->req_dataseg[rq2->req_seg_count].ds_base);
485 		} else {
486 			printf("%s: seg0[%d] cnt 0x%x paddr 0x%08x\n",
487 			    isp->isp_name, rq->req_seg_count,
488 			    rq->req_dataseg[rq->req_seg_count].ds_count,
489 			    rq->req_dataseg[rq->req_seg_count].ds_base);
490 		}
491 #endif
492 		rq->req_seg_count++;
493 	}
494 
495 
496 
497 	if (datalen == 0)
498 		return (0);
499 
500 	paddr = vtophys(vaddr);
501 	while (datalen > 0) {
502 		crq = (ispcontreq_t *) ISP_QUEUE_ENTRY(isp->isp_rquest, *iptrp);
503 		*iptrp = (*iptrp + 1) & (RQUEST_QUEUE_LEN(isp) - 1);
504 		if (*iptrp == optr) {
505 			printf("%s: Request Queue Overflow\n", isp->isp_name);
506 			return (EFBIG);
507 		}
508 		rq->req_header.rqs_entry_count++;
509 		bzero((void *)crq, sizeof (*crq));
510 		crq->req_header.rqs_entry_count = 1;
511 		crq->req_header.rqs_entry_type = RQSTYPE_DATASEG;
512 
513 		for (seglim = 0; datalen != 0 && seglim < ISP_CDSEG; seglim++) {
514 			crq->req_dataseg[seglim].ds_base = paddr;
515 			ctrp = &crq->req_dataseg[seglim].ds_count;
516 			*(ctrp) = 0;
517 			nextpaddr = paddr;
518 			while (datalen != 0 && paddr == nextpaddr) {
519 				nextpaddr = (paddr & (~PAGE_MASK)) + PAGE_SIZE;
520 				size = nextpaddr - paddr;
521 				if (size > datalen)
522 					size = datalen;
523 
524 				*(ctrp) += size;
525 				vaddr += size;
526 				datalen -= size;
527 				if (datalen != 0)
528 					paddr = vtophys(vaddr);
529 			}
530 #if	0
531 			printf("%s: seg%d[%d] cnt 0x%x paddr 0x%08x\n",
532 			    isp->isp_name, rq->req_header.rqs_entry_count-1,
533 			    seglim, crq->req_dataseg[seglim].ds_count,
534 			    crq->req_dataseg[seglim].ds_base);
535 #endif
536 			rq->req_seg_count++;
537 		}
538 	}
539 
540 	return (0);
541 }
542 
543 static void
544 isp_pci_reset1(isp)
545 	struct ispsoftc *isp;
546 {
547 	/* Make sure the BIOS is disabled */
548 	isp_pci_wr_reg(isp, HCCR, PCI_HCCR_CMD_BIOS);
549 }
550 
551 static void
552 isp_pci_dumpregs(isp)
553 	struct ispsoftc *isp;
554 {
555 	struct isp_pcisoftc *pci = (struct isp_pcisoftc *)isp;
556 	printf("%s: PCI Status Command/Status=%lx\n", pci->pci_isp.isp_name,
557 	    pci_conf_read(pci->pci_id, PCI_COMMAND_STATUS_REG));
558 }
559 #endif
560