xref: /freebsd/sys/dev/mpt/mpt_pci.c (revision f856af0466c076beef4ea9b15d088e1119a945b8)
1 /*-
2  * PCI specific probe and attach routines for LSI Fusion Adapters
3  * FreeBSD Version.
4  *
5  * Copyright (c) 2000, 2001 by Greg Ansley
6  * Partially derived from Matt Jacob's ISP driver.
7  * Copyright (c) 1997, 1998, 1999, 2000, 2001, 2002 by Matthew Jacob
8  * Feral Software
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice immediately at the beginning of the file, without modification,
16  *    this list of conditions, and the following disclaimer.
17  * 2. The name of the author may not be used to endorse or promote products
18  *    derived from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
24  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  */
32 /*-
33  * Copyright (c) 2002, 2006 by Matthew Jacob
34  * All rights reserved.
35  *
36  * Redistribution and use in source and binary forms, with or without
37  * modification, are permitted provided that the following conditions are
38  * met:
39  * 1. Redistributions of source code must retain the above copyright
40  *    notice, this list of conditions and the following disclaimer.
41  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
42  *    substantially similar to the "NO WARRANTY" disclaimer below
43  *    ("Disclaimer") and any redistribution must be conditioned upon including
44  *    a substantially similar Disclaimer requirement for further binary
45  *    redistribution.
46  * 3. Neither the names of the above listed copyright holders nor the names
47  *    of any contributors may be used to endorse or promote products derived
48  *    from this software without specific prior written permission.
49  *
50  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
51  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
54  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
55  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
56  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
57  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
58  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
59  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
60  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
61  *
62  * Support from Chris Ellsworth in order to make SAS adapters work
63  * is gratefully acknowledged.
64  *
65  * Support from LSI-Logic has also gone a great deal toward making this a
66  * workable subsystem and is gratefully acknowledged.
67  */
68 /*
69  * Copyright (c) 2004, Avid Technology, Inc. and its contributors.
70  * Copyright (c) 2005, WHEEL Sp. z o.o.
71  * Copyright (c) 2004, 2005 Justin T. Gibbs
72  * All rights reserved.
73  *
74  * Redistribution and use in source and binary forms, with or without
75  * modification, are permitted provided that the following conditions are
76  * met:
77  * 1. Redistributions of source code must retain the above copyright
78  *    notice, this list of conditions and the following disclaimer.
79  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
80  *    substantially similar to the "NO WARRANTY" disclaimer below
81  *    ("Disclaimer") and any redistribution must be conditioned upon including
82  *    a substantially similar Disclaimer requirement for further binary
83  *    redistribution.
84  * 3. Neither the names of the above listed copyright holders nor the names
85  *    of any contributors may be used to endorse or promote products derived
86  *    from this software without specific prior written permission.
87  *
88  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
89  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
90  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
91  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
92  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
93  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
94  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
95  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
96  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
97  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF THE COPYRIGHT
98  * OWNER OR CONTRIBUTOR IS ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
99  */
100 
101 #include <sys/cdefs.h>
102 __FBSDID("$FreeBSD$");
103 
104 #include <dev/mpt/mpt.h>
105 #include <dev/mpt/mpt_cam.h>
106 #include <dev/mpt/mpt_raid.h>
107 
108 #if __FreeBSD_version < 700000
109 #define	pci_msi_count(x)	0
110 #define	pci_msi_enable(x)	0
111 #define	pci_alloc_msi(x, y)	1
112 #define	pci_release_msi(x)	do { ; } while (0)
113 #endif
114 
115 #ifndef	PCI_VENDOR_LSI
116 #define	PCI_VENDOR_LSI			0x1000
117 #endif
118 
119 #ifndef	PCI_PRODUCT_LSI_FC909
120 #define	PCI_PRODUCT_LSI_FC909		0x0620
121 #endif
122 
123 #ifndef	PCI_PRODUCT_LSI_FC909A
124 #define	PCI_PRODUCT_LSI_FC909A		0x0621
125 #endif
126 
127 #ifndef	PCI_PRODUCT_LSI_FC919
128 #define	PCI_PRODUCT_LSI_FC919		0x0624
129 #endif
130 
131 #ifndef	PCI_PRODUCT_LSI_FC929
132 #define	PCI_PRODUCT_LSI_FC929		0x0622
133 #endif
134 
135 #ifndef	PCI_PRODUCT_LSI_FC929X
136 #define	PCI_PRODUCT_LSI_FC929X		0x0626
137 #endif
138 
139 #ifndef	PCI_PRODUCT_LSI_FC919X
140 #define	PCI_PRODUCT_LSI_FC919X		0x0628
141 #endif
142 
143 #ifndef	PCI_PRODUCT_LSI_FC7X04X
144 #define	PCI_PRODUCT_LSI_FC7X04X		0x0640
145 #endif
146 
147 #ifndef	PCI_PRODUCT_LSI_FC646
148 #define	PCI_PRODUCT_LSI_FC646		0x0646
149 #endif
150 
151 #ifndef	PCI_PRODUCT_LSI_1030
152 #define	PCI_PRODUCT_LSI_1030		0x0030
153 #endif
154 
155 #ifndef	PCI_PRODUCT_LSI_SAS1064
156 #define PCI_PRODUCT_LSI_SAS1064		0x0050
157 #endif
158 
159 #ifndef PCI_PRODUCT_LSI_SAS1064A
160 #define PCI_PRODUCT_LSI_SAS1064A	0x005C
161 #endif
162 
163 #ifndef PCI_PRODUCT_LSI_SAS1064E
164 #define PCI_PRODUCT_LSI_SAS1064E	0x0056
165 #endif
166 
167 #ifndef PCI_PRODUCT_LSI_SAS1066
168 #define PCI_PRODUCT_LSI_SAS1066		0x005E
169 #endif
170 
171 #ifndef PCI_PRODUCT_LSI_SAS1066E
172 #define PCI_PRODUCT_LSI_SAS1066E	0x005A
173 #endif
174 
175 #ifndef PCI_PRODUCT_LSI_SAS1068
176 #define PCI_PRODUCT_LSI_SAS1068		0x0054
177 #endif
178 
179 #ifndef PCI_PRODUCT_LSI_SAS1068E
180 #define PCI_PRODUCT_LSI_SAS1068E	0x0058
181 #endif
182 
183 #ifndef PCI_PRODUCT_LSI_SAS1078
184 #define PCI_PRODUCT_LSI_SAS1078		0x0060
185 #endif
186 
187 #ifndef	PCIM_CMD_SERRESPEN
188 #define	PCIM_CMD_SERRESPEN	0x0100
189 #endif
190 
191 
192 #define	MPT_IO_BAR	0
193 #define	MPT_MEM_BAR	1
194 
195 static int mpt_pci_probe(device_t);
196 static int mpt_pci_attach(device_t);
197 static void mpt_free_bus_resources(struct mpt_softc *mpt);
198 static int mpt_pci_detach(device_t);
199 static int mpt_pci_shutdown(device_t);
200 static int mpt_dma_mem_alloc(struct mpt_softc *mpt);
201 static void mpt_dma_mem_free(struct mpt_softc *mpt);
202 static void mpt_read_config_regs(struct mpt_softc *mpt);
203 static void mpt_pci_intr(void *);
204 
205 static device_method_t mpt_methods[] = {
206 	/* Device interface */
207 	DEVMETHOD(device_probe,		mpt_pci_probe),
208 	DEVMETHOD(device_attach,	mpt_pci_attach),
209 	DEVMETHOD(device_detach,	mpt_pci_detach),
210 	DEVMETHOD(device_shutdown,	mpt_pci_shutdown),
211 	{ 0, 0 }
212 };
213 
214 static driver_t mpt_driver = {
215 	"mpt", mpt_methods, sizeof(struct mpt_softc)
216 };
217 static devclass_t mpt_devclass;
218 DRIVER_MODULE(mpt, pci, mpt_driver, mpt_devclass, 0, 0);
219 MODULE_DEPEND(mpt, pci, 1, 1, 1);
220 MODULE_VERSION(mpt, 1);
221 
222 static int
223 mpt_pci_probe(device_t dev)
224 {
225 	char *desc;
226 
227 	if (pci_get_vendor(dev) != PCI_VENDOR_LSI) {
228 		return (ENXIO);
229 	}
230 
231 	switch ((pci_get_device(dev) & ~1)) {
232 	case PCI_PRODUCT_LSI_FC909:
233 		desc = "LSILogic FC909 FC Adapter";
234 		break;
235 	case PCI_PRODUCT_LSI_FC909A:
236 		desc = "LSILogic FC909A FC Adapter";
237 		break;
238 	case PCI_PRODUCT_LSI_FC919:
239 		desc = "LSILogic FC919 FC Adapter";
240 		break;
241 	case PCI_PRODUCT_LSI_FC929:
242 		desc = "Dual LSILogic FC929 FC Adapter";
243 		break;
244 	case PCI_PRODUCT_LSI_FC919X:
245 		desc = "LSILogic FC919 FC PCI-X Adapter";
246 		break;
247 	case PCI_PRODUCT_LSI_FC929X:
248 		desc = "Dual LSILogic FC929X 2Gb/s FC PCI-X Adapter";
249 		break;
250 	case PCI_PRODUCT_LSI_FC646:
251 		desc = "Dual LSILogic FC7X04X 4Gb/s FC PCI-Express Adapter";
252 		break;
253 	case PCI_PRODUCT_LSI_FC7X04X:
254 		desc = "Dual LSILogic FC7X04X 4Gb/s FC PCI-X Adapter";
255 		break;
256 	case PCI_PRODUCT_LSI_1030:
257 		desc = "LSILogic 1030 Ultra4 Adapter";
258 		break;
259 	case PCI_PRODUCT_LSI_SAS1064:
260 	case PCI_PRODUCT_LSI_SAS1064A:
261 	case PCI_PRODUCT_LSI_SAS1064E:
262 	case PCI_PRODUCT_LSI_SAS1066:
263 	case PCI_PRODUCT_LSI_SAS1066E:
264 	case PCI_PRODUCT_LSI_SAS1068:
265 	case PCI_PRODUCT_LSI_SAS1068E:
266 	case PCI_PRODUCT_LSI_SAS1078:
267 		desc = "LSILogic SAS/SATA Adapter";
268 		break;
269 	default:
270 		return (ENXIO);
271 	}
272 
273 	device_set_desc(dev, desc);
274 	return (0);
275 }
276 
277 #if	__FreeBSD_version < 500000
278 static void
279 mpt_set_options(struct mpt_softc *mpt)
280 {
281 	int bitmap;
282 
283 	bitmap = 0;
284 	if (getenv_int("mpt_disable", &bitmap)) {
285 		if (bitmap & (1 << mpt->unit)) {
286 			mpt->disabled = 1;
287 		}
288 	}
289 	bitmap = 0;
290 	if (getenv_int("mpt_debug", &bitmap)) {
291 		if (bitmap & (1 << mpt->unit)) {
292 			mpt->verbose = MPT_PRT_DEBUG;
293 		}
294 	}
295 	bitmap = 0;
296 	if (getenv_int("mpt_debug1", &bitmap)) {
297 		if (bitmap & (1 << mpt->unit)) {
298 			mpt->verbose = MPT_PRT_DEBUG1;
299 		}
300 	}
301 	bitmap = 0;
302 	if (getenv_int("mpt_debug2", &bitmap)) {
303 		if (bitmap & (1 << mpt->unit)) {
304 			mpt->verbose = MPT_PRT_DEBUG2;
305 		}
306 	}
307 	bitmap = 0;
308 	if (getenv_int("mpt_debug3", &bitmap)) {
309 		if (bitmap & (1 << mpt->unit)) {
310 			mpt->verbose = MPT_PRT_DEBUG3;
311 		}
312 	}
313 
314 	mpt->cfg_role = MPT_ROLE_DEFAULT;
315 	bitmap = 0;
316 	if (getenv_int("mpt_nil_role", &bitmap)) {
317 		if (bitmap & (1 << mpt->unit)) {
318 			mpt->cfg_role = 0;
319 		}
320 		mpt->do_cfg_role = 1;
321 	}
322 	bitmap = 0;
323 	if (getenv_int("mpt_tgt_role", &bitmap)) {
324 		if (bitmap & (1 << mpt->unit)) {
325 			mpt->cfg_role |= MPT_ROLE_TARGET;
326 		}
327 		mpt->do_cfg_role = 1;
328 	}
329 	bitmap = 0;
330 	if (getenv_int("mpt_ini_role", &bitmap)) {
331 		if (bitmap & (1 << mpt->unit)) {
332 			mpt->cfg_role |= MPT_ROLE_INITIATOR;
333 		}
334 		mpt->do_cfg_role = 1;
335 	}
336 	mpt->msi_enable = 0;
337 }
338 #else
339 static void
340 mpt_set_options(struct mpt_softc *mpt)
341 {
342 	int tval;
343 
344 	tval = 0;
345 	if (resource_int_value(device_get_name(mpt->dev),
346 	    device_get_unit(mpt->dev), "disable", &tval) == 0 && tval != 0) {
347 		mpt->disabled = 1;
348 	}
349 	tval = 0;
350 	if (resource_int_value(device_get_name(mpt->dev),
351 	    device_get_unit(mpt->dev), "debug", &tval) == 0 && tval != 0) {
352 		mpt->verbose = tval;
353 	}
354 	tval = -1;
355 	if (resource_int_value(device_get_name(mpt->dev),
356 	    device_get_unit(mpt->dev), "role", &tval) == 0 && tval >= 0 &&
357 	    tval <= 3) {
358 		mpt->cfg_role = tval;
359 		mpt->do_cfg_role = 1;
360 	}
361 
362 	tval = 0;
363 	mpt->msi_enable = 0;
364 	if (resource_int_value(device_get_name(mpt->dev),
365 	    device_get_unit(mpt->dev), "msi_enable", &tval) == 0 && tval == 1) {
366 		mpt->msi_enable = 1;
367 	}
368 }
369 #endif
370 
371 
372 static void
373 mpt_link_peer(struct mpt_softc *mpt)
374 {
375 	struct mpt_softc *mpt2;
376 
377 	if (mpt->unit == 0) {
378 		return;
379 	}
380 	/*
381 	 * XXX: depends on probe order
382 	 */
383 	mpt2 = (struct mpt_softc *)devclass_get_softc(mpt_devclass,mpt->unit-1);
384 
385 	if (mpt2 == NULL) {
386 		return;
387 	}
388 	if (pci_get_vendor(mpt2->dev) != pci_get_vendor(mpt->dev)) {
389 		return;
390 	}
391 	if (pci_get_device(mpt2->dev) != pci_get_device(mpt->dev)) {
392 		return;
393 	}
394 	mpt->mpt2 = mpt2;
395 	mpt2->mpt2 = mpt;
396 	if (mpt->verbose >= MPT_PRT_DEBUG) {
397 		mpt_prt(mpt, "linking with peer (mpt%d)\n",
398 		    device_get_unit(mpt2->dev));
399 	}
400 }
401 
402 static void
403 mpt_unlink_peer(struct mpt_softc *mpt)
404 {
405 	if (mpt->mpt2) {
406 		mpt->mpt2->mpt2 = NULL;
407 	}
408 }
409 
410 
411 static int
412 mpt_pci_attach(device_t dev)
413 {
414 	struct mpt_softc *mpt;
415 	int		  iqd;
416 	uint32_t	  data, cmd;
417 
418 	/* Allocate the softc structure */
419 	mpt  = (struct mpt_softc*)device_get_softc(dev);
420 	if (mpt == NULL) {
421 		device_printf(dev, "cannot allocate softc\n");
422 		return (ENOMEM);
423 	}
424 	memset(mpt, 0, sizeof(struct mpt_softc));
425 	switch ((pci_get_device(dev) & ~1)) {
426 	case PCI_PRODUCT_LSI_FC909:
427 	case PCI_PRODUCT_LSI_FC909A:
428 	case PCI_PRODUCT_LSI_FC919:
429 	case PCI_PRODUCT_LSI_FC929:
430 	case PCI_PRODUCT_LSI_FC919X:
431 	case PCI_PRODUCT_LSI_FC646:
432 	case PCI_PRODUCT_LSI_FC7X04X:
433 		mpt->is_fc = 1;
434 		break;
435 	case PCI_PRODUCT_LSI_SAS1064:
436 	case PCI_PRODUCT_LSI_SAS1064A:
437 	case PCI_PRODUCT_LSI_SAS1064E:
438 	case PCI_PRODUCT_LSI_SAS1066:
439 	case PCI_PRODUCT_LSI_SAS1066E:
440 	case PCI_PRODUCT_LSI_SAS1068:
441 	case PCI_PRODUCT_LSI_SAS1068E:
442 	case PCI_PRODUCT_LSI_SAS1078:
443 		mpt->is_sas = 1;
444 		break;
445 	default:
446 		mpt->is_spi = 1;
447 		break;
448 	}
449 	mpt->dev = dev;
450 	mpt->unit = device_get_unit(dev);
451 	mpt->raid_resync_rate = MPT_RAID_RESYNC_RATE_DEFAULT;
452 	mpt->raid_mwce_setting = MPT_RAID_MWCE_DEFAULT;
453 	mpt->raid_queue_depth = MPT_RAID_QUEUE_DEPTH_DEFAULT;
454 	mpt->verbose = MPT_PRT_NONE;
455 	mpt->role = MPT_ROLE_NONE;
456 	mpt_set_options(mpt);
457 	if (mpt->verbose == MPT_PRT_NONE) {
458 		mpt->verbose = MPT_PRT_WARN;
459 		/* Print INFO level (if any) if bootverbose is set */
460 		mpt->verbose += (bootverbose != 0)? 1 : 0;
461 	}
462 	/* Make sure memory access decoders are enabled */
463 	cmd = pci_read_config(dev, PCIR_COMMAND, 2);
464 	if ((cmd & PCIM_CMD_MEMEN) == 0) {
465 		device_printf(dev, "Memory accesses disabled");
466 		return (ENXIO);
467 	}
468 
469 	/*
470 	 * Make sure that SERR, PERR, WRITE INVALIDATE and BUSMASTER are set.
471 	 */
472 	cmd |=
473 	    PCIM_CMD_SERRESPEN | PCIM_CMD_PERRESPEN |
474 	    PCIM_CMD_BUSMASTEREN | PCIM_CMD_MWRICEN;
475 	pci_write_config(dev, PCIR_COMMAND, cmd, 2);
476 
477 	/*
478 	 * Make sure we've disabled the ROM.
479 	 */
480 	data = pci_read_config(dev, PCIR_BIOS, 4);
481 	data &= ~1;
482 	pci_write_config(dev, PCIR_BIOS, data, 4);
483 
484 	/*
485 	 * Is this part a dual?
486 	 * If so, link with our partner (around yet)
487 	 */
488 	if ((pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_FC929 ||
489 	    (pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_FC646 ||
490 	    (pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_FC7X04X ||
491 	    (pci_get_device(dev) & ~1) == PCI_PRODUCT_LSI_1030) {
492 		mpt_link_peer(mpt);
493 	}
494 
495 	/*
496 	 * Set up register access.  PIO mode is required for
497 	 * certain reset operations (but must be disabled for
498 	 * some cards otherwise).
499 	 */
500 	mpt->pci_pio_rid = PCIR_BAR(MPT_IO_BAR);
501 	mpt->pci_pio_reg = bus_alloc_resource(dev, SYS_RES_IOPORT,
502 			    &mpt->pci_pio_rid, 0, ~0, 0, RF_ACTIVE);
503 	if (mpt->pci_pio_reg == NULL) {
504 		device_printf(dev, "unable to map registers in PIO mode\n");
505 		goto bad;
506 	}
507 	mpt->pci_pio_st = rman_get_bustag(mpt->pci_pio_reg);
508 	mpt->pci_pio_sh = rman_get_bushandle(mpt->pci_pio_reg);
509 
510 	/* Allocate kernel virtual memory for the 9x9's Mem0 region */
511 	mpt->pci_mem_rid = PCIR_BAR(MPT_MEM_BAR);
512 	mpt->pci_reg = bus_alloc_resource(dev, SYS_RES_MEMORY,
513 			&mpt->pci_mem_rid, 0, ~0, 0, RF_ACTIVE);
514 	if (mpt->pci_reg == NULL) {
515 		device_printf(dev, "Unable to memory map registers.\n");
516 		if (mpt->is_sas) {
517 			device_printf(dev, "Giving Up.\n");
518 			goto bad;
519 		}
520 		device_printf(dev, "Falling back to PIO mode.\n");
521 		mpt->pci_st = mpt->pci_pio_st;
522 		mpt->pci_sh = mpt->pci_pio_sh;
523 	} else {
524 		mpt->pci_st = rman_get_bustag(mpt->pci_reg);
525 		mpt->pci_sh = rman_get_bushandle(mpt->pci_reg);
526 	}
527 
528 	/* Get a handle to the interrupt */
529 	iqd = 0;
530 	if (mpt->msi_enable && pci_msi_count(dev) == 1) {
531 		mpt->pci_msi_count = 1;
532 		if (pci_alloc_msi(dev, &mpt->pci_msi_count) == 0) {
533 			iqd = 1;
534 		} else {
535 			mpt->pci_msi_count = 0;
536 		}
537 	}
538 	mpt->pci_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &iqd,
539 	    RF_ACTIVE | RF_SHAREABLE);
540 	if (mpt->pci_irq == NULL) {
541 		device_printf(dev, "could not allocate interrupt\n");
542 		goto bad;
543 	}
544 
545 	MPT_LOCK_SETUP(mpt);
546 
547 	/* Disable interrupts at the part */
548 	mpt_disable_ints(mpt);
549 
550 	/* Register the interrupt handler */
551 	if (bus_setup_intr(dev, mpt->pci_irq, MPT_IFLAGS, mpt_pci_intr,
552 	    mpt, &mpt->ih)) {
553 		device_printf(dev, "could not setup interrupt\n");
554 		goto bad;
555 	}
556 
557 	/* Allocate dma memory */
558 /* XXX JGibbs -Should really be done based on IOCFacts. */
559 	if (mpt_dma_mem_alloc(mpt)) {
560 		mpt_prt(mpt, "Could not allocate DMA memory\n");
561 		goto bad;
562 	}
563 
564 	/*
565 	 * Save the PCI config register values
566  	 *
567 	 * Hard resets are known to screw up the BAR for diagnostic
568 	 * memory accesses (Mem1).
569 	 *
570 	 * Using Mem1 is known to make the chip stop responding to
571 	 * configuration space transfers, so we need to save it now
572 	 */
573 
574 	mpt_read_config_regs(mpt);
575 
576 	/*
577 	 * Disable PIO until we need it
578 	 */
579 	if (mpt->is_sas) {
580 		pci_disable_io(dev, SYS_RES_IOPORT);
581 	}
582 
583 	/* Initialize the hardware */
584 	if (mpt->disabled == 0) {
585 		MPT_LOCK(mpt);
586 		if (mpt_attach(mpt) != 0) {
587 			MPT_UNLOCK(mpt);
588 			goto bad;
589 		}
590 		MPT_UNLOCK(mpt);
591 	} else {
592 		mpt_prt(mpt, "device disabled at user request\n");
593 		goto bad;
594 	}
595 
596 	mpt->eh = EVENTHANDLER_REGISTER(shutdown_post_sync, mpt_pci_shutdown,
597 	    dev, SHUTDOWN_PRI_DEFAULT);
598 
599 	if (mpt->eh == NULL) {
600 		mpt_prt(mpt, "shutdown event registration failed\n");
601 		MPT_LOCK(mpt);
602 		(void) mpt_detach(mpt);
603 		MPT_UNLOCK(mpt);
604 		goto bad;
605 	}
606 	KASSERT(MPT_OWNED(mpt) == 0, ("leaving attach with device locked"));
607 	return (0);
608 
609 bad:
610 	mpt_dma_mem_free(mpt);
611 	mpt_free_bus_resources(mpt);
612 	mpt_unlink_peer(mpt);
613 
614 	MPT_LOCK_DESTROY(mpt);
615 
616 	/*
617 	 * but return zero to preserve unit numbering
618 	 */
619 	return (0);
620 }
621 
622 /*
623  * Free bus resources
624  */
625 static void
626 mpt_free_bus_resources(struct mpt_softc *mpt)
627 {
628 	if (mpt->ih) {
629 		bus_teardown_intr(mpt->dev, mpt->pci_irq, mpt->ih);
630 		mpt->ih = 0;
631 	}
632 
633 	if (mpt->pci_irq) {
634 		bus_release_resource(mpt->dev, SYS_RES_IRQ,
635 		    mpt->pci_msi_count ? 1 : 0, mpt->pci_irq);
636 		mpt->pci_irq = 0;
637 	}
638 
639 	if (mpt->pci_msi_count) {
640 		pci_release_msi(mpt->dev);
641 		mpt->pci_msi_count = 0;
642 	}
643 
644 	if (mpt->pci_pio_reg) {
645 		bus_release_resource(mpt->dev, SYS_RES_IOPORT, mpt->pci_pio_rid,
646 			mpt->pci_pio_reg);
647 		mpt->pci_pio_reg = 0;
648 	}
649 	if (mpt->pci_reg) {
650 		bus_release_resource(mpt->dev, SYS_RES_MEMORY, mpt->pci_mem_rid,
651 			mpt->pci_reg);
652 		mpt->pci_reg = 0;
653 	}
654 	MPT_LOCK_DESTROY(mpt);
655 }
656 
657 
658 /*
659  * Disconnect ourselves from the system.
660  */
661 static int
662 mpt_pci_detach(device_t dev)
663 {
664 	struct mpt_softc *mpt;
665 
666 	mpt  = (struct mpt_softc*)device_get_softc(dev);
667 
668 	if (mpt) {
669 		MPT_LOCK(mpt);
670 		mpt_disable_ints(mpt);
671 		mpt_detach(mpt);
672 		mpt_reset(mpt, /*reinit*/FALSE);
673 		mpt_dma_mem_free(mpt);
674 		mpt_free_bus_resources(mpt);
675 		mpt_raid_free_mem(mpt);
676 		if (mpt->eh != NULL) {
677                         EVENTHANDLER_DEREGISTER(shutdown_final, mpt->eh);
678 		}
679 		MPT_UNLOCK(mpt);
680 	}
681 	return(0);
682 }
683 
684 
685 /*
686  * Disable the hardware
687  */
688 static int
689 mpt_pci_shutdown(device_t dev)
690 {
691 	struct mpt_softc *mpt;
692 
693 	mpt = (struct mpt_softc *)device_get_softc(dev);
694 	if (mpt) {
695 		int r;
696 		MPT_LOCK(mpt);
697 		r = mpt_shutdown(mpt);
698 		MPT_UNLOCK(mpt);
699 		return (r);
700 	}
701 	return(0);
702 }
703 
704 static int
705 mpt_dma_mem_alloc(struct mpt_softc *mpt)
706 {
707 	int i, error, nsegs;
708 	uint8_t *vptr;
709 	uint32_t pptr, end;
710 	size_t len;
711 	struct mpt_map_info mi;
712 
713 	/* Check if we alreay have allocated the reply memory */
714 	if (mpt->reply_phys != 0) {
715 		return 0;
716 	}
717 
718 	len = sizeof (request_t) * MPT_MAX_REQUESTS(mpt);
719 #ifdef	RELENG_4
720 	mpt->request_pool = (request_t *)malloc(len, M_DEVBUF, M_WAITOK);
721 	if (mpt->request_pool == NULL) {
722 		mpt_prt(mpt, "cannot allocate request pool\n");
723 		return (1);
724 	}
725 	memset(mpt->request_pool, 0, len);
726 #else
727 	mpt->request_pool = (request_t *)malloc(len, M_DEVBUF, M_WAITOK|M_ZERO);
728 	if (mpt->request_pool == NULL) {
729 		mpt_prt(mpt, "cannot allocate request pool\n");
730 		return (1);
731 	}
732 #endif
733 
734 	/*
735 	 * Create a parent dma tag for this device.
736 	 *
737 	 * Align at byte boundaries,
738 	 * Limit to 32-bit addressing for request/reply queues.
739 	 */
740 	if (mpt_dma_tag_create(mpt, /*parent*/bus_get_dma_tag(mpt->dev),
741 	    /*alignment*/1, /*boundary*/0, /*lowaddr*/BUS_SPACE_MAXADDR,
742 	    /*highaddr*/BUS_SPACE_MAXADDR, /*filter*/NULL, /*filterarg*/NULL,
743 	    /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
744 	    /*nsegments*/BUS_SPACE_MAXSIZE_32BIT,
745 	    /*maxsegsz*/BUS_SPACE_UNRESTRICTED, /*flags*/0,
746 	    &mpt->parent_dmat) != 0) {
747 		mpt_prt(mpt, "cannot create parent dma tag\n");
748 		return (1);
749 	}
750 
751 	/* Create a child tag for reply buffers */
752 	if (mpt_dma_tag_create(mpt, mpt->parent_dmat, PAGE_SIZE, 0,
753 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
754 	    NULL, NULL, 2 * PAGE_SIZE, 1, BUS_SPACE_MAXSIZE_32BIT, 0,
755 	    &mpt->reply_dmat) != 0) {
756 		mpt_prt(mpt, "cannot create a dma tag for replies\n");
757 		return (1);
758 	}
759 
760 	/* Allocate some DMA accessable memory for replies */
761 	if (bus_dmamem_alloc(mpt->reply_dmat, (void **)&mpt->reply,
762 	    BUS_DMA_NOWAIT, &mpt->reply_dmap) != 0) {
763 		mpt_prt(mpt, "cannot allocate %lu bytes of reply memory\n",
764 		    (u_long) (2 * PAGE_SIZE));
765 		return (1);
766 	}
767 
768 	mi.mpt = mpt;
769 	mi.error = 0;
770 
771 	/* Load and lock it into "bus space" */
772 	bus_dmamap_load(mpt->reply_dmat, mpt->reply_dmap, mpt->reply,
773 	    2 * PAGE_SIZE, mpt_map_rquest, &mi, 0);
774 
775 	if (mi.error) {
776 		mpt_prt(mpt, "error %d loading dma map for DMA reply queue\n",
777 		    mi.error);
778 		return (1);
779 	}
780 	mpt->reply_phys = mi.phys;
781 
782 	/* Create a child tag for data buffers */
783 
784 	/*
785 	 * XXX: we should say that nsegs is 'unrestricted, but that
786 	 * XXX: tickles a horrible bug in the busdma code. Instead,
787 	 * XXX: we'll derive a reasonable segment limit from MAXPHYS
788 	 */
789 	nsegs = (MAXPHYS / PAGE_SIZE) + 1;
790 	if (mpt_dma_tag_create(mpt, mpt->parent_dmat, 1,
791 	    0, BUS_SPACE_MAXADDR, BUS_SPACE_MAXADDR,
792 	    NULL, NULL, MAXBSIZE, nsegs, BUS_SPACE_MAXSIZE_32BIT, 0,
793 	    &mpt->buffer_dmat) != 0) {
794 		mpt_prt(mpt, "cannot create a dma tag for data buffers\n");
795 		return (1);
796 	}
797 
798 	/* Create a child tag for request buffers */
799 	if (mpt_dma_tag_create(mpt, mpt->parent_dmat, PAGE_SIZE, 0,
800 	    BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR,
801 	    NULL, NULL, MPT_REQ_MEM_SIZE(mpt), 1, BUS_SPACE_MAXSIZE_32BIT, 0,
802 	    &mpt->request_dmat) != 0) {
803 		mpt_prt(mpt, "cannot create a dma tag for requests\n");
804 		return (1);
805 	}
806 
807 	/* Allocate some DMA accessable memory for requests */
808 	if (bus_dmamem_alloc(mpt->request_dmat, (void **)&mpt->request,
809 	    BUS_DMA_NOWAIT, &mpt->request_dmap) != 0) {
810 		mpt_prt(mpt, "cannot allocate %d bytes of request memory\n",
811 		    MPT_REQ_MEM_SIZE(mpt));
812 		return (1);
813 	}
814 
815 	mi.mpt = mpt;
816 	mi.error = 0;
817 
818 	/* Load and lock it into "bus space" */
819         bus_dmamap_load(mpt->request_dmat, mpt->request_dmap, mpt->request,
820 	    MPT_REQ_MEM_SIZE(mpt), mpt_map_rquest, &mi, 0);
821 
822 	if (mi.error) {
823 		mpt_prt(mpt, "error %d loading dma map for DMA request queue\n",
824 		    mi.error);
825 		return (1);
826 	}
827 	mpt->request_phys = mi.phys;
828 
829 	/*
830 	 * Now create per-request dma maps
831 	 */
832 	i = 0;
833 	pptr =  mpt->request_phys;
834 	vptr =  mpt->request;
835 	end = pptr + MPT_REQ_MEM_SIZE(mpt);
836 	while(pptr < end) {
837 		request_t *req = &mpt->request_pool[i];
838 		req->index = i++;
839 
840 		/* Store location of Request Data */
841 		req->req_pbuf = pptr;
842 		req->req_vbuf = vptr;
843 
844 		pptr += MPT_REQUEST_AREA;
845 		vptr += MPT_REQUEST_AREA;
846 
847 		req->sense_pbuf = (pptr - MPT_SENSE_SIZE);
848 		req->sense_vbuf = (vptr - MPT_SENSE_SIZE);
849 
850 		error = bus_dmamap_create(mpt->buffer_dmat, 0, &req->dmap);
851 		if (error) {
852 			mpt_prt(mpt, "error %d creating per-cmd DMA maps\n",
853 			    error);
854 			return (1);
855 		}
856 	}
857 
858 	return (0);
859 }
860 
861 
862 
863 /* Deallocate memory that was allocated by mpt_dma_mem_alloc
864  */
865 static void
866 mpt_dma_mem_free(struct mpt_softc *mpt)
867 {
868 	int i;
869 
870         /* Make sure we aren't double destroying */
871         if (mpt->reply_dmat == 0) {
872 		mpt_lprt(mpt, MPT_PRT_DEBUG, "already released dma memory\n");
873 		return;
874         }
875 
876 	for (i = 0; i < MPT_MAX_REQUESTS(mpt); i++) {
877 		bus_dmamap_destroy(mpt->buffer_dmat, mpt->request_pool[i].dmap);
878 	}
879 	bus_dmamap_unload(mpt->request_dmat, mpt->request_dmap);
880 	bus_dmamem_free(mpt->request_dmat, mpt->request, mpt->request_dmap);
881 	bus_dma_tag_destroy(mpt->request_dmat);
882 	bus_dma_tag_destroy(mpt->buffer_dmat);
883 	bus_dmamap_unload(mpt->reply_dmat, mpt->reply_dmap);
884 	bus_dmamem_free(mpt->reply_dmat, mpt->reply, mpt->reply_dmap);
885 	bus_dma_tag_destroy(mpt->reply_dmat);
886 	bus_dma_tag_destroy(mpt->parent_dmat);
887 	mpt->reply_dmat = 0;
888 	free(mpt->request_pool, M_DEVBUF);
889 	mpt->request_pool = 0;
890 
891 }
892 
893 
894 
895 /* Reads modifiable (via PCI transactions) config registers */
896 static void
897 mpt_read_config_regs(struct mpt_softc *mpt)
898 {
899 	mpt->pci_cfg.Command = pci_read_config(mpt->dev, PCIR_COMMAND, 2);
900 	mpt->pci_cfg.LatencyTimer_LineSize =
901 	    pci_read_config(mpt->dev, PCIR_CACHELNSZ, 2);
902 	mpt->pci_cfg.IO_BAR = pci_read_config(mpt->dev, PCIR_BAR(0), 4);
903 	mpt->pci_cfg.Mem0_BAR[0] = pci_read_config(mpt->dev, PCIR_BAR(1), 4);
904 	mpt->pci_cfg.Mem0_BAR[1] = pci_read_config(mpt->dev, PCIR_BAR(2), 4);
905 	mpt->pci_cfg.Mem1_BAR[0] = pci_read_config(mpt->dev, PCIR_BAR(3), 4);
906 	mpt->pci_cfg.Mem1_BAR[1] = pci_read_config(mpt->dev, PCIR_BAR(4), 4);
907 	mpt->pci_cfg.ROM_BAR = pci_read_config(mpt->dev, PCIR_BIOS, 4);
908 	mpt->pci_cfg.IntLine = pci_read_config(mpt->dev, PCIR_INTLINE, 1);
909 	mpt->pci_cfg.PMCSR = pci_read_config(mpt->dev, 0x44, 4);
910 }
911 
912 /* Sets modifiable config registers */
913 void
914 mpt_set_config_regs(struct mpt_softc *mpt)
915 {
916 	uint32_t val;
917 
918 #define MPT_CHECK(reg, offset, size)					\
919 	val = pci_read_config(mpt->dev, offset, size);			\
920 	if (mpt->pci_cfg.reg != val) {					\
921 		mpt_prt(mpt,						\
922 		    "Restoring " #reg " to 0x%X from 0x%X\n",		\
923 		    mpt->pci_cfg.reg, val);				\
924 	}
925 
926 	if (mpt->verbose >= MPT_PRT_DEBUG) {
927 		MPT_CHECK(Command, PCIR_COMMAND, 2);
928 		MPT_CHECK(LatencyTimer_LineSize, PCIR_CACHELNSZ, 2);
929 		MPT_CHECK(IO_BAR, PCIR_BAR(0), 4);
930 		MPT_CHECK(Mem0_BAR[0], PCIR_BAR(1), 4);
931 		MPT_CHECK(Mem0_BAR[1], PCIR_BAR(2), 4);
932 		MPT_CHECK(Mem1_BAR[0], PCIR_BAR(3), 4);
933 		MPT_CHECK(Mem1_BAR[1], PCIR_BAR(4), 4);
934 		MPT_CHECK(ROM_BAR, PCIR_BIOS, 4);
935 		MPT_CHECK(IntLine, PCIR_INTLINE, 1);
936 		MPT_CHECK(PMCSR, 0x44, 4);
937 	}
938 #undef MPT_CHECK
939 
940 	pci_write_config(mpt->dev, PCIR_COMMAND, mpt->pci_cfg.Command, 2);
941 	pci_write_config(mpt->dev, PCIR_CACHELNSZ,
942 	    mpt->pci_cfg.LatencyTimer_LineSize, 2);
943 	pci_write_config(mpt->dev, PCIR_BAR(0), mpt->pci_cfg.IO_BAR, 4);
944 	pci_write_config(mpt->dev, PCIR_BAR(1), mpt->pci_cfg.Mem0_BAR[0], 4);
945 	pci_write_config(mpt->dev, PCIR_BAR(2), mpt->pci_cfg.Mem0_BAR[1], 4);
946 	pci_write_config(mpt->dev, PCIR_BAR(3), mpt->pci_cfg.Mem1_BAR[0], 4);
947 	pci_write_config(mpt->dev, PCIR_BAR(4), mpt->pci_cfg.Mem1_BAR[1], 4);
948 	pci_write_config(mpt->dev, PCIR_BIOS, mpt->pci_cfg.ROM_BAR, 4);
949 	pci_write_config(mpt->dev, PCIR_INTLINE, mpt->pci_cfg.IntLine, 1);
950 	pci_write_config(mpt->dev, 0x44, mpt->pci_cfg.PMCSR, 4);
951 }
952 
953 static void
954 mpt_pci_intr(void *arg)
955 {
956 	struct mpt_softc *mpt;
957 
958 	mpt = (struct mpt_softc *)arg;
959 	MPT_LOCK(mpt);
960 	mpt_intr(mpt);
961 	MPT_UNLOCK(mpt);
962 }
963