summaryrefslogtreecommitdiffstats
path: root/sys/dev/idt/idt_pci.c
blob: 07c061df2427b4bd1a849afa9e75977cfac882a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*-
 * Copyright (c) 2000, 2001 Richard Hodges and Matriplex, inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    This product includes software developed by Matriplex, inc.
 * 4. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************
 *
 * This driver is derived from the Nicstar driver by Mark Tinguely, and
 * some of the original driver still exists here.  Those portions are...
 *   Copyright (c) 1996, 1997, 1998, 1999 Mark Tinguely
 *   All rights reserved.
 */

#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/mutex.h>
#include <sys/socket.h>
#include <sys/socketvar.h>

#include <sys/bus.h>
#include <sys/conf.h>

#include <sys/module.h>
#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/rman.h>

#include <net/if.h>
#include <net/if_arp.h>

#include <netatm/port.h>
#include <netatm/queue.h>
#include <netatm/atm.h>
#include <netatm/atm_sys.h>
#include <netatm/atm_sap.h>
#include <netatm/atm_cm.h>
#include <netatm/atm_if.h>
#include <netatm/atm_stack.h>
#include <netatm/atm_pcb.h>
#include <netatm/atm_var.h>

#include <dev/pci/pcireg.h>
#include <dev/pci/pcivar.h>

#include <dev/idt/idtreg.h>
#include <dev/idt/idtvar.h>

#define	IDT_VID		0x111d
#define	IDT_NICSTAR_DID	0x0001

struct pci_type {
	u_int16_t	pci_vid;
	u_int16_t	pci_did;
	char *		pci_name;
} pci_devs[] = {
	{ IDT_VID, IDT_NICSTAR_DID, "IDT IDT77201/211 NICStAR ATM Adapter" },
	{ 0, 0, NULL }
};

uma_zone_t	idt_nif_zone;
uma_zone_t	idt_vcc_zone;

static int	idt_probe	(device_t);
static int	idt_attach	(device_t);
static int	idt_detach	(device_t);
static int	idt_shutdown	(device_t);
static void	idt_free	(device_t);
static int	idt_modevent	(module_t, int, void *);

static int
idt_probe(device_t dev)
{
	struct pci_type *t = pci_devs;

	while(t->pci_name != NULL) {
		if ((pci_get_vendor(dev) == t->pci_vid) &&
		    (pci_get_device(dev) == t->pci_did)) {
			device_set_desc(dev, t->pci_name);
			return(BUS_PROBE_DEFAULT);
		}
		t++;
	}

        return(ENXIO);
}

/******************************************************************************
 *
 *  Attach device
 *
 * Date first: 11/14/2000  last: 06/10/2001
 */

static int
idt_attach(device_t dev)
{
	struct idt_softc *sc;
	int error;

	sc = device_get_softc(dev);
	sc->dev = dev;
	error = 0;

	pci_enable_busmaster(dev);

	/* count = 2 (times 32 PCI clocks) */
	pci_write_config(dev, PCIR_LATTIMER, 0x20, 1);

	/* Map IDT registers */
	sc->mem_rid = 0x14;
	sc->mem_type = SYS_RES_MEMORY;
	sc->mem = bus_alloc_resource_any(dev, sc->mem_type, &sc->mem_rid,
					 RF_ACTIVE);
	if (sc->mem == NULL) {
		device_printf(dev, "could not map registers.\n");
		error = ENXIO;
		goto fail;
	}
	sc->bustag = rman_get_bustag(sc->mem);
	sc->bushandle = rman_get_bushandle(sc->mem);

	/* Map interrupt */
	sc->irq_rid = 0;
	sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid,
					 RF_ACTIVE | RF_SHAREABLE);
	if (sc->irq == NULL) {
		device_printf(dev, "could not map interrupt.\n");
		error = ENXIO;
		goto fail;
	}

	error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET, NULL, nicstar_intr,
			       sc, &sc->irq_ih);
	if (error) {
		device_printf(dev, "could not setup irq.\n");
		error = ENXIO;
		goto fail;
	}

	sc->virt_baseaddr = (vm_offset_t)rman_get_virtual(sc->mem);
	sc->cmd_reg = sc->virt_baseaddr + REGCMD;	/* old reg */
	sc->stat_reg = sc->virt_baseaddr + REGSTAT;	/* old reg */
	sc->reg_cmd = (u_long *)(sc->virt_baseaddr + REGCMD);
	sc->reg_stat = (u_long *)(sc->virt_baseaddr + REGSTAT);
	sc->reg_cfg = (u_long *)(sc->virt_baseaddr + REGCFG);
	sc->reg_data = (u_long *)(sc->virt_baseaddr + 0);
	sc->reg_tsqh = (u_long *)(sc->virt_baseaddr + REGTSQH);
	sc->reg_gp = (u_long *)(sc->virt_baseaddr + REGGP);
	sc->pci_rev = pci_get_revid(dev);
	sc->timer_wrap = 0;

	callout_handle_init(&sc->ch);

	phys_init(sc);		/* initialize the hardware */
	nicstar_init(sc);	/* allocate and initialize */
	
	error = idt_harp_init(sc);
	if (error)
		goto fail;

	return (0);
fail:
	idt_free(dev);
	return (error);
}

/******************************************************************************
 *
 *  Detach device
 *
 * Date first: 11/14/2000  last: 11/14/2000
 */

static int
idt_detach(device_t dev)
{
	struct idt_softc *sc;
	int error;

	sc = device_get_softc(dev);
	error = 0;

        /*
         * De-Register this interface with ATM core services
         */
	error = atm_physif_deregister(&sc->iu_cmn);

	idt_device_stop(sc);	/* Stop the device */

	/*
	 * Lock out all device interupts.
	 */
	DEVICE_LOCK(&sc->iu_cmn);
	idt_free(dev);
	idt_release_mem(sc);
	DEVICE_UNLOCK(&sc->iu_cmn);

	return (error);
}

/******************************************************************************
 *
 *  Shutdown device
 *
 * Date first: 11/14/2000  last: 11/14/2000
 */

static int
idt_shutdown(device_t dev)
{

	struct idt_softc *sc;

	sc = device_get_softc(dev);

	idt_device_stop(sc);	/* Stop the device */

	return (0);
}

static void
idt_free (device_t dev)
{
	struct idt_softc *sc;

	sc = device_get_softc(dev);

	if (sc->irq_ih)
		bus_teardown_intr(dev, sc->irq, sc->irq_ih);
	if (sc->irq)
		bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq);
	if (sc->mem)
		bus_release_resource(dev, sc->mem_type, sc->mem_rid, sc->mem);

	return;
}

static int
idt_modevent (module_t mod, int type, void *data)
{
	int error;

	error = 0;

	switch (type) {
	case MOD_LOAD:
		idt_nif_zone = uma_zcreate("idt nif",
			sizeof(struct atm_nif),
			NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
		if (idt_nif_zone == NULL)
			panic("hfa_modevent:uma_zcreate nif");
		uma_zone_set_max(idt_nif_zone, 20);

		idt_vcc_zone = uma_zcreate("idt vcc",
			sizeof(Idt_vcc),
			NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
		if (idt_vcc_zone == NULL)
			panic("hfa_modevent: uma_zcreate vcc");
		uma_zone_set_max(idt_vcc_zone, 100);

		break;

	case MOD_UNLOAD:
		uma_zdestroy(idt_nif_zone);
		uma_zdestroy(idt_vcc_zone);

		break;
	default:
		break;
	}

	return (error);
}

static device_method_t idt_methods[] = {
	DEVMETHOD(device_probe,		idt_probe),
	DEVMETHOD(device_attach,	idt_attach),
	DEVMETHOD(device_detach,	idt_detach),
	DEVMETHOD(device_shutdown,	idt_shutdown),
	{0, 0}
};

static driver_t idt_driver = {
	"idt",
	idt_methods,
	sizeof(struct idt_softc)
};

static devclass_t idt_devclass;

DRIVER_MODULE(idt, pci, idt_driver, idt_devclass, idt_modevent, 0);
MODULE_VERSION(idt, 1);
OpenPOWER on IntegriCloud