xref: /titanic_52/usr/src/uts/sun4v/io/niumx/niumx.c (revision bd0f52d78d701efcad2c460df61b45677d041c35)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*
29  *	Niagara2 Network Interface Unit (NIU) Nexus Driver
30  */
31 
32 #include <sys/conf.h>
33 #include <sys/modctl.h>
34 #include <sys/ddi_impldefs.h>
35 #include <sys/ddi_subrdefs.h>
36 #include <sys/ddi.h>
37 #include <sys/sunndi.h>
38 #include <sys/sunddi.h>
39 #include <sys/open.h>
40 #include <sys/stat.h>
41 #include <sys/file.h>
42 #include <sys/machsystm.h>
43 #include <sys/hsvc.h>
44 #include <sys/sdt.h>
45 #include <sys/hypervisor_api.h>
46 #include "niumx_var.h"
47 
48 static int niumx_fm_init_child(dev_info_t *, dev_info_t *, int,
49 	ddi_iblock_cookie_t *);
50 static int niumx_intr_ops(dev_info_t *dip, dev_info_t *rdip,
51 	ddi_intr_op_t intr_op, ddi_intr_handle_impl_t *hdlp, void *result);
52 static int niumx_attach(dev_info_t *devi, ddi_attach_cmd_t cmd);
53 static int niumx_detach(dev_info_t *devi, ddi_detach_cmd_t cmd);
54 static int niumx_set_intr(dev_info_t *dip, dev_info_t *rdip,
55 	ddi_intr_handle_impl_t *hdlp, int valid);
56 static int niumx_add_intr(dev_info_t *dip, dev_info_t *rdip,
57 	ddi_intr_handle_impl_t *hdlp);
58 static int niumx_rem_intr(dev_info_t *dip, dev_info_t *rdip,
59 	ddi_intr_handle_impl_t *hdlp);
60 static uint_t niumx_intr_hdlr(void *arg);
61 static int niumx_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
62 	off_t offset, off_t len, caddr_t *addrp);
63 static int niumx_dma_allochdl(dev_info_t *dip, dev_info_t *rdip,
64 	ddi_dma_attr_t *attrp,
65 	int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep);
66 static int niumx_dma_freehdl(dev_info_t *dip, dev_info_t *rdip,
67 	ddi_dma_handle_t handlep);
68 static int niumx_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
69 	ddi_dma_handle_t handle, ddi_dma_req_t *dmareq,
70 	ddi_dma_cookie_t *cookiep, uint_t *ccountp);
71 static int niumx_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip,
72 	ddi_dma_handle_t handle);
73 static int niumx_ctlops(dev_info_t *dip, dev_info_t *rdip,
74 	ddi_ctl_enum_t op, void *arg, void *result);
75 
76 static struct bus_ops niumx_bus_ops = {
77 	BUSO_REV,
78 	niumx_map,
79 	0,
80 	0,
81 	0,
82 	i_ddi_map_fault,
83 	0,
84 	niumx_dma_allochdl,
85 	niumx_dma_freehdl,
86 	niumx_dma_bindhdl,
87 	niumx_dma_unbindhdl,
88 	0,
89 	0,
90 	0,
91 	niumx_ctlops,
92 	ddi_bus_prop_op,
93 	0,				/* (*bus_get_eventcookie)();    */
94 	0,				/* (*bus_add_eventcall)();	*/
95 	0,				/* (*bus_remove_eventcall)();   */
96 	0,				/* (*bus_post_event)();		*/
97 	0,				/* (*bus_intr_ctl)();		*/
98 	0,				/* (*bus_config)(); 		*/
99 	0,				/* (*bus_unconfig)(); 		*/
100 	niumx_fm_init_child,		/* (*bus_fm_init)(); 		*/
101 	0,				/* (*bus_fm_fini)(); 		*/
102 	0,				/* (*bus_enter)()		*/
103 	0,				/* (*bus_exit)()		*/
104 	0,				/* (*bus_power)()		*/
105 	niumx_intr_ops			/* (*bus_intr_op)(); 		*/
106 };
107 
108 static struct dev_ops niumx_ops = {
109 	DEVO_REV,		/* devo_rev */
110 	0,			/* refcnt  */
111 	ddi_no_info,		/* info */
112 	nulldev,		/* identify */
113 	0,			/* probe */
114 	niumx_attach,		/* attach */
115 	niumx_detach,		/* detach */
116 	nulldev,		/* reset */
117 	(struct cb_ops *)0,	/* driver operations */
118 	&niumx_bus_ops,		/* bus operations */
119 	0
120 };
121 
122 /* Module linkage information for the kernel. */
123 static struct modldrv modldrv = {
124 	&mod_driverops, /* Type of module */
125 	"NIU Nexus Driver %I%",
126 	&niumx_ops,	/* driver ops */
127 };
128 
129 static struct modlinkage modlinkage = {
130 	MODREV_1,
131 	(void *)&modldrv,
132 	NULL
133 };
134 
135 static void *niumx_state;
136 static niumx_ih_t niumx_ihtable[NIUMX_MAX_INTRS];
137 
138 /*
139  * forward function declarations:
140  */
141 static void niumx_removechild(dev_info_t *);
142 static int niumx_initchild(dev_info_t *child);
143 
144 int
145 _init(void)
146 {
147 	int e;
148 	uint64_t mjrnum;
149 	uint64_t mnrnum;
150 
151 	/*
152 	 * Check HV intr group api versioning.
153 	 * This driver uses the old interrupt routines which are supported
154 	 * in old firmware in the CORE API group and in newer firmware in
155 	 * the INTR API group.  Support for these calls will be dropped
156 	 * once the INTR API group major goes to 2.
157 	 */
158 	if ((hsvc_version(HSVC_GROUP_INTR, &mjrnum, &mnrnum) == 0) &&
159 	    (mjrnum > NIUMX_INTR_MAJOR_VER)) {
160 		cmn_err(CE_WARN, "niumx: unsupported intr api group: "
161 		    "maj:0x%lx, min:0x%lx", mjrnum, mnrnum);
162 		return (ENOTSUP);
163 	}
164 
165 	if ((e = ddi_soft_state_init(&niumx_state, sizeof (niumx_devstate_t),
166 	    1)) == 0 && (e = mod_install(&modlinkage)) != 0)
167 		ddi_soft_state_fini(&niumx_state);
168 	return (e);
169 }
170 
171 int
172 _fini(void)
173 {
174 	int e;
175 	if ((e = mod_remove(&modlinkage)) == 0)
176 		ddi_soft_state_fini(&niumx_state);
177 	return (e);
178 }
179 
180 int
181 _info(struct modinfo *modinfop)
182 {
183 	return (mod_info(&modlinkage, modinfop));
184 }
185 
186 
187 hrtime_t niumx_intr_timeout = 2ull * NANOSEC; /* 2 seconds in nanoseconds */
188 
189 void
190 niumx_intr_dist(void *arg)
191 {
192 	kmutex_t 	*lock_p = (kmutex_t *)arg;
193 	int		i = NIUMX_RSVD_INTRS;
194 	niumx_ih_t	*ih_p = niumx_ihtable + i;
195 
196 	DBG(DBG_A_INTX, NULL, "niumx_intr_dist entered\n");
197 	mutex_enter(lock_p);
198 	for (; i < NIUMX_MAX_INTRS; i++, ih_p++) {
199 		sysino_t sysino = ih_p->ih_sysino;
200 		cpuid_t	cpuid;
201 		int	intr_state, state;
202 		hrtime_t	start;
203 		dev_info_t	*dip = ih_p->ih_dip;
204 		if (!sysino ||	/* sequence is significant */
205 		    (hvio_intr_getvalid(sysino, &intr_state) != H_EOK) ||
206 		    (intr_state == HV_INTR_NOTVALID) ||
207 		    (cpuid = intr_dist_cpuid()) == ih_p->ih_cpuid)
208 			continue;
209 
210 		(void) hvio_intr_setvalid(sysino, HV_INTR_NOTVALID);
211 
212 		/* check for pending interrupts, busy wait if so */
213 		for (start = gethrtime(); !panicstr &&
214 		    (hvio_intr_getstate(sysino, &state) == H_EOK) &&
215 		    (state == HV_INTR_DELIVERED_STATE); /* */) {
216 			if (gethrtime() - start > niumx_intr_timeout) {
217 				cmn_err(CE_WARN, "%s%d: niumx_intr_dist: "
218 				    "pending interrupt (%x,%lx) timedout\n",
219 				    ddi_driver_name(dip), ddi_get_instance(dip),
220 				    ih_p->ih_inum, sysino);
221 				(void) hvio_intr_setstate(sysino,
222 					HV_INTR_IDLE_STATE);
223 				break;
224 			}
225 		}
226 		(void) hvio_intr_settarget(sysino, cpuid);
227 		(void) hvio_intr_setvalid(sysino, HV_INTR_VALID);
228 		ih_p->ih_cpuid = cpuid;
229 	}
230 	mutex_exit(lock_p);
231 }
232 
233 static int
234 niumx_attach(dev_info_t *dip, ddi_attach_cmd_t cmd)
235 {
236 	int instance = ddi_get_instance(dip);
237 	niumx_devstate_t *niumxds_p;	/* devstate pointer */
238 	niu_regspec_t	*reg_p;
239 	uint_t		reglen;
240 	int		ret = DDI_SUCCESS;
241 
242 	switch (cmd) {
243 	case DDI_ATTACH:
244 		if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, dip,
245 			DDI_PROP_DONTPASS, "reg", (int **)&reg_p, &reglen)
246 				!= DDI_PROP_SUCCESS) {
247 			DBG(DBG_ATTACH, dip, "reg lookup failed\n");
248 			ret = DDI_FAILURE;
249 			goto done;
250 		}
251 
252 		/*
253 		 * Allocate and get soft state structure.
254 		 */
255 		if (ddi_soft_state_zalloc(niumx_state, instance)
256 			!= DDI_SUCCESS) {
257 			ret = DDI_FAILURE;
258 			goto prop_free;
259 		}
260 		niumxds_p = (niumx_devstate_t *)ddi_get_soft_state(niumx_state,
261 							instance);
262 		niumxds_p->dip = dip;
263 		mutex_init(&niumxds_p->niumx_mutex, NULL, MUTEX_DRIVER, NULL);
264 
265 		DBG(DBG_ATTACH, dip, "soft state alloc'd instance = %d, "
266 			"niumxds_p = %p\n", instance, niumxds_p);
267 
268 		/* hv devhdl: low 28-bit of 1st "reg" entry's addr.hi */
269 		niumxds_p->niumx_dev_hdl = (devhandle_t)(reg_p->addr_high &
270 			NIUMX_DEVHDLE_MASK);
271 
272 		/* add interrupt redistribution callback */
273 		intr_dist_add(niumx_intr_dist, &niumxds_p->niumx_mutex);
274 
275 		niumxds_p->niumx_fm_cap = DDI_FM_EREPORT_CAPABLE;
276 
277 		ddi_fm_init(niumxds_p->dip, &niumxds_p->niumx_fm_cap,
278 			&niumxds_p->niumx_fm_ibc);
279 
280 		ret = DDI_SUCCESS;
281 		goto prop_free;
282 cleanup:
283 		mutex_destroy(&niumxds_p->niumx_mutex);
284 		ddi_soft_state_free(niumx_state, ddi_get_instance(dip));
285 prop_free:
286 		ddi_prop_free(reg_p);
287 done:
288 		return (ret);
289 
290 	case DDI_RESUME:
291 	default:
292 		break;
293 	}
294 	return (ret);
295 }
296 
297 static int
298 niumx_detach(dev_info_t *dip, ddi_detach_cmd_t cmd)
299 {
300 	niumx_devstate_t *niumxds_p;
301 
302 	switch (cmd) {
303 	case DDI_DETACH:
304 
305 		niumxds_p = (niumx_devstate_t *)
306 		    ddi_get_soft_state(niumx_state, ddi_get_instance(dip));
307 
308 		intr_dist_rem(niumx_intr_dist, &niumxds_p->niumx_mutex);
309 		ddi_fm_fini(dip);
310 		mutex_destroy(&niumxds_p->niumx_mutex);
311 		ddi_soft_state_free(niumx_state, ddi_get_instance(dip));
312 		return (DDI_SUCCESS);
313 
314 	case DDI_SUSPEND:
315 	default:
316 		break;
317 	}
318 	return (DDI_FAILURE);
319 }
320 
321 
322 /*
323  * Function used to initialize FMA for our children nodes. Called
324  * through pci busops when child node calls ddi_fm_init.
325  */
326 /*ARGSUSED*/
327 int
328 niumx_fm_init_child(dev_info_t *dip, dev_info_t *cdip, int cap,
329     ddi_iblock_cookie_t *ibc_p)
330 {
331 	niumx_devstate_t	*niumxds_p = DIP_TO_STATE(dip);
332 
333 	ASSERT(ibc_p != NULL);
334 	*ibc_p = niumxds_p->niumx_fm_ibc;
335 
336 	return (niumxds_p->niumx_fm_cap);
337 }
338 
339 
340 /*ARGSUSED*/
341 int
342 niumx_map(dev_info_t *dip, dev_info_t *rdip, ddi_map_req_t *mp,
343 	off_t offset, off_t len, caddr_t *vaddrp)
344 {
345 	struct regspec p_regspec;
346 	ddi_map_req_t p_mapreq;
347 	niu_regspec_t	*reg_p;
348 	int 	i, rn = mp->map_obj.rnumber, reglen, rnglen, rngnum, ret;
349 	niumx_ranges_t	*rng_p;
350 
351 	uint32_t	reg_begin, rng_begin;
352 
353 	DBG(DBG_MAP, dip, "%s%d: mapping %s%d reg %d\n", NAMEINST(dip),
354 		NAMEINST(rdip), rn);
355 
356 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
357 		"reg", (caddr_t)&reg_p, &reglen) != DDI_SUCCESS)
358 		return (DDI_FAILURE);
359 
360 	if (rn < 0 || (rn >= reglen / sizeof (niu_regspec_t))) {
361 		DBG(DBG_MAP, dip,  "rnumber out of range: %d\n", rn);
362 		kmem_free(reg_p, reglen);
363 		return (DDI_ME_RNUMBER_RANGE);
364 	}
365 
366 	/* build regspec up for parent */
367 	p_mapreq = *mp;		/* dup the whole structure */
368 	p_mapreq.map_type = DDI_MT_REGSPEC;
369 	p_mapreq.map_obj.rp = &p_regspec;
370 
371 	if (ddi_getlongprop(DDI_DEV_T_ANY, dip, DDI_PROP_DONTPASS, "ranges",
372 		(caddr_t)&rng_p, &rnglen) != DDI_SUCCESS) {
373 			DBG(DBG_MAP,  dip, "%s%d: no ranges property\n",
374 				ddi_driver_name(dip), ddi_get_instance(dip));
375 			kmem_free(reg_p, reglen);
376 			return (DDI_FAILURE);
377 	}
378 
379 	/* locate matching ranges record */
380 	rngnum = rnglen / sizeof (niumx_ranges_t);
381 	for (i = 0, reg_p += rn; i < rngnum; rng_p++, i++) {
382 		if (reg_p->addr_high == rng_p->child_hi)
383 			break;
384 	}
385 
386 	if (i >= rngnum) {
387 		DBG(DBG_MAP, dip, "ranges record for reg[%d] not found.\n", rn);
388 		ret = DDI_ME_REGSPEC_RANGE;
389 		goto err;
390 	}
391 
392 	/*
393 	 * validate request has matching bus type and within 4G
394 	 * limit by comparing addr.hi of "ranges" and child "reg".
395 	 */
396 
397 	ASSERT(reg_p->size_high == 0);
398 
399 	rng_begin = rng_p->child_lo;
400 	reg_begin = reg_p->addr_low;
401 	/* check to verify reg bounds are within rng bounds */
402 	if (reg_begin < rng_begin || (reg_begin + (reg_p->size_low - 1)) >
403 			(rng_begin + (rng_p->size_lo - 1))) {
404 		DBG(DBG_MAP, dip, "size out of range for reg[%d].\n", rn);
405 		ret = DDI_ME_REGSPEC_RANGE;
406 		goto err;
407 	}
408 
409 	p_regspec.regspec_bustype = rng_p->parent_hi;
410 	p_regspec.regspec_addr = reg_begin - rng_begin + rng_p->parent_lo;
411 	p_regspec.regspec_size = reg_p->size_low;
412 	DBG(DBG_MAP, dip, "regspec:bus,addr,size = (%x,%x,%x)\n",
413 		p_regspec.regspec_bustype, p_regspec.regspec_addr,
414 		p_regspec.regspec_size);
415 	ret = ddi_map(dip, &p_mapreq, 0, 0, vaddrp);
416 	DBG(DBG_MAP, dip, "niumx_map: ret %d.\n", ret);
417 err:
418 	kmem_free(rng_p - i, rnglen);
419 	kmem_free(reg_p - rn, reglen);
420 	return (ret);
421 }
422 
423 /*
424  * niumx_ctlops
425  */
426 int
427 niumx_ctlops(dev_info_t *dip, dev_info_t *rdip,
428 	ddi_ctl_enum_t ctlop, void *arg, void *result)
429 {
430 	niu_regspec_t *reg_p;
431 	int	reglen, totreg;
432 
433 	DBG(DBG_CTLOPS, dip, "niumx_ctlops ctlop=%d.\n", ctlop);
434 	if (rdip == (dev_info_t *)0)
435 		return (DDI_FAILURE);
436 
437 	switch (ctlop) {
438 	case DDI_CTLOPS_REPORTDEV:
439 		cmn_err(CE_NOTE, "device: %s@%s, %s%d\n",
440 		    ddi_node_name(rdip), ddi_get_name_addr(rdip),
441 		    NAMEINST(rdip));
442 		return (DDI_SUCCESS);
443 
444 	case DDI_CTLOPS_INITCHILD:
445 		return (niumx_initchild((dev_info_t *)arg));
446 
447 	case DDI_CTLOPS_UNINITCHILD:
448 		niumx_removechild((dev_info_t *)arg);
449 		return (DDI_SUCCESS);
450 
451 	case DDI_CTLOPS_REGSIZE:
452 	case DDI_CTLOPS_NREGS:
453 		/* fall through */
454 		break;
455 	default:
456 		DBG(DBG_CTLOPS, dip, "just pass to ddi_cltops.\n");
457 		return (ddi_ctlops(dip, rdip, ctlop, arg, result));
458 	}
459 
460 	/* REGSIZE/NREGS */
461 
462 	*(int *)result = 0;
463 
464 	if (ddi_getlongprop(DDI_DEV_T_NONE, rdip, DDI_PROP_DONTPASS |
465 		DDI_PROP_CANSLEEP, "reg", (caddr_t)&reg_p, &reglen)
466 			!= DDI_SUCCESS)
467 		return (DDI_FAILURE);
468 
469 	totreg = reglen / sizeof (niu_regspec_t);
470 	if (ctlop == DDI_CTLOPS_NREGS) {
471 		DBG(DBG_CTLOPS, (dev_info_t *)dip, "niumx_ctlops NREGS=%d.\n",
472 				totreg);
473 		*(int *)result = totreg;
474 	} else if (ctlop == DDI_CTLOPS_REGSIZE) {
475 		int	rn;
476 		rn = *(int *)arg;
477 		if (rn >= totreg) {
478 			kmem_free(reg_p, reglen);
479 			return (DDI_FAILURE);
480 		}
481 		*(off_t *)result = (reg_p + rn)->size_low;
482 		DBG(DBG_CTLOPS, (dev_info_t *)dip, "rn = %d, REGSIZE=%x.\n",
483 				rn, *(off_t *)result);
484 	}
485 
486 	kmem_free(reg_p, reglen);
487 	return (DDI_SUCCESS);
488 }
489 
490 static int
491 niumx_initchild(dev_info_t *child)
492 {
493 	char name[MAXNAMELEN];
494 	niu_regspec_t *r;
495 	uint_t n;
496 
497 	if (ddi_prop_lookup_int_array(DDI_DEV_T_ANY, child, DDI_PROP_DONTPASS,
498 	    "reg", (int **)&r, &n) != DDI_SUCCESS) {
499 		return (DDI_FAILURE);
500 	}
501 	(void) snprintf(name, MAXNAMELEN, "%x", (r[0].addr_high &
502 		NIUMX_FUNC_NUM_MASK));
503 	ddi_prop_free(r);
504 	ddi_set_name_addr(child, name);
505 	return (DDI_SUCCESS);
506 }
507 
508 static void
509 niumx_removechild(dev_info_t *dip)
510 {
511 	ddi_set_name_addr(dip, NULL);
512 	ddi_remove_minor_node(dip, NULL);
513 	impl_rem_dev_props(dip);
514 }
515 
516 
517 
518 /*
519  * bus dma alloc handle entry point:
520  */
521 /*ARGSUSED*/
522 int
523 niumx_dma_allochdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_attr_t *attrp,
524 	int (*waitfp)(caddr_t), caddr_t arg, ddi_dma_handle_t *handlep)
525 {
526 	ddi_dma_impl_t *mp;
527 	int sleep = (waitfp == DDI_DMA_SLEEP) ? KM_SLEEP : KM_NOSLEEP;
528 
529 	DBG(DBG_DMA_ALLOCH, dip, "rdip=%s%d\n", NAMEINST(rdip));
530 
531 	if (attrp->dma_attr_version != DMA_ATTR_V0) {
532 		DBG(DBG_DMA_ALLOCH, (dev_info_t *)dip, "DDI_DMA_BADATTR\n");
533 		return (DDI_DMA_BADATTR);
534 	}
535 
536 	/* Caution: we don't use zalloc to enhance performance! */
537 	if ((mp = kmem_alloc(sizeof (ddi_dma_impl_t), sleep)) == 0) {
538 		DBG(DBG_DMA_ALLOCH, dip, "can't alloc ddi_dma_impl_t\n");
539 		return (DDI_FAILURE);
540 	}
541 	mp->dmai_rdip = rdip;
542 	mp->dmai_pfnlst = NULL;
543 	mp->dmai_cookie = NULL;
544 	mp->dmai_fault = 0;
545 	mp->dmai_fault_check = NULL;
546 	mp->dmai_fault_notify = NULL;
547 
548 	mp->dmai_attr = *attrp; 	/* set requestors attr info */
549 
550 	DBG(DBG_DMA_ALLOCH, dip, "mp=%p\n", mp);
551 
552 	*handlep = (ddi_dma_handle_t)mp;
553 	return (DDI_SUCCESS);
554 }
555 
556 
557 /*
558  * bus dma free handle entry point:
559  */
560 /*ARGSUSED*/
561 int
562 niumx_dma_freehdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
563 {
564 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
565 
566 	if (mp->dmai_cookie)
567 		kmem_free(mp->dmai_cookie, sizeof (ddi_dma_cookie_t));
568 	kmem_free(mp, sizeof (ddi_dma_impl_t));
569 
570 	return (DDI_SUCCESS);
571 }
572 
573 
574 /*
575  * bus dma bind handle entry point:
576  *
577  *	check/enforce DMA type, setup pfn0 and some other key pieces
578  *	of this dma request.
579  * Note: this only works with DMA_OTYP_VADDR, and makes use of the known
580  *	fact that only contiguous memory blocks will be passed in.
581  *	Therefore only one cookie will ever be returned.
582  *
583  *	return values:
584  *		DDI_DMA_NOMAPPING - can't get valid pfn0, or bad dma type
585  *		DDI_DMA_NORESOURCES
586  *		DDI_SUCCESS
587  *
588  *	dma handle members affected (set on exit):
589  *	mp->dmai_object		- dmareq->dmar_object
590  *	mp->dmai_rflags		- dmareq->dmar_flags
591  *	mp->dmai_pfn0   	- 1st page pfn (if va/size pair and not shadow)
592  *	mp->dmai_roffset 	- initialized to starting page offset
593  *	mp->dmai_size		- # of total pages of entire object
594  *	mp->dmai_cookie		- new cookie alloc'd
595  */
596 /*ARGSUSED*/
597 int
598 niumx_dma_bindhdl(dev_info_t *dip, dev_info_t *rdip,
599 	ddi_dma_handle_t handle, ddi_dma_req_t *dmareq,
600 	ddi_dma_cookie_t *cookiep, uint_t *ccountp)
601 {
602 	int (*waitfp)(caddr_t) = dmareq->dmar_fp;
603 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
604 	ddi_dma_obj_t *dobj_p = &dmareq->dmar_object;
605 	uint32_t offset;
606 	pfn_t pfn0;
607 	int ret;
608 
609 	DBG(DBG_DMA_BINDH, dip, "rdip=%s%d mp=%p dmareq=%p\n", NAMEINST(rdip),
610 		mp, dmareq);
611 
612 	/* first check dma type */
613 	mp->dmai_rflags = dmareq->dmar_flags & DMP_DDIFLAGS | DMP_NOSYNC;
614 	switch (dobj_p->dmao_type) {
615 	case DMA_OTYP_VADDR: {
616 		caddr_t vaddr = dobj_p->dmao_obj.virt_obj.v_addr;
617 		struct as *as_p = dobj_p->dmao_obj.virt_obj.v_as;
618 		struct hat *hat_p = as_p ? as_p->a_hat : kas.a_hat;
619 		offset = (ulong_t)vaddr & NIUMX_PAGE_OFFSET;
620 		pfn0 = hat_getpfnum(hat_p, vaddr);
621 		}
622 		break;
623 
624 	case DMA_OTYP_BUFVADDR:
625 	case DMA_OTYP_PAGES:
626 	case DMA_OTYP_PADDR:
627 	default:
628 		cmn_err(CE_WARN, "%s%d requested unsupported dma type %x",
629 			NAMEINST(mp->dmai_rdip), dobj_p->dmao_type);
630 		ret = DDI_DMA_NOMAPPING;
631 		goto err;
632 	}
633 	if (pfn0 == PFN_INVALID) {
634 		cmn_err(CE_WARN, "%s%d: invalid pfn0 for DMA object %p",
635 			NAMEINST(dip), (void *)dobj_p);
636 		ret = DDI_DMA_NOMAPPING;
637 		goto err;
638 	}
639 	mp->dmai_object	 = *dobj_p;			/* whole object */
640 	mp->dmai_pfn0	 = (void *)pfn0;		/* cache pfn0   */
641 	mp->dmai_roffset = offset;			/* pg0 offset   */
642 	mp->dmai_mapping = mp->dmai_roffset | NIUMX_PTOB(pfn0);
643 	mp->dmai_size = mp->dmai_object.dmao_size;
644 
645 	DBG(DBG_DMA_BINDH, dip, "check pfn: mp=%p pfn0=%x\n",
646 		mp, mp->dmai_pfn0);
647 	if (!(mp->dmai_cookie = kmem_zalloc(sizeof (ddi_dma_cookie_t),
648 		waitfp == DDI_DMA_SLEEP ? KM_SLEEP : KM_NOSLEEP))) {
649 			ret = DDI_DMA_NORESOURCES;
650 			goto err;
651 		}
652 	mp->dmai_cookie->dmac_laddress = mp->dmai_mapping;
653 	mp->dmai_cookie->dmac_size = mp->dmai_size;
654 	*ccountp = 1;
655 	*cookiep = *mp->dmai_cookie;
656 	DBG(DBG_DMA_BINDH, dip, "cookie %" PRIx64 "+%x, count=%d\n",
657 		cookiep->dmac_address, cookiep->dmac_size, *ccountp);
658 	return (DDI_DMA_MAPPED);
659 
660 err:
661 	DBG(DBG_DMA_BINDH, (dev_info_t *)dip,
662 			"niumx_dma_bindhdl error ret=%d\n", ret);
663 	return (ret);
664 }
665 
666 /*
667  * bus dma unbind handle entry point:
668  */
669 /*ARGSUSED*/
670 int
671 niumx_dma_unbindhdl(dev_info_t *dip, dev_info_t *rdip, ddi_dma_handle_t handle)
672 {
673 	ddi_dma_impl_t *mp = (ddi_dma_impl_t *)handle;
674 
675 	DBG(DBG_DMA_UNBINDH, dip, "rdip=%s%d, mp=%p\n",
676 		ddi_driver_name(rdip), ddi_get_instance(rdip), handle);
677 	if (mp->dmai_cookie) {
678 		kmem_free(mp->dmai_cookie, sizeof (ddi_dma_cookie_t));
679 		mp->dmai_cookie = NULL;
680 	}
681 
682 	return (DDI_SUCCESS);
683 }
684 
685 /*ARGSUSED*/
686 int
687 niumx_intr_ops(dev_info_t *dip, dev_info_t *rdip, ddi_intr_op_t intr_op,
688     ddi_intr_handle_impl_t *hdlp, void *result)
689 {
690 
691 	int	ret = DDI_SUCCESS;
692 
693 	DBG(DBG_INTROPS, dip, "niumx_intr_ops: dip=%p rdip=%p intr_op=%x "
694 	    "handle=%p\n", dip, rdip, intr_op, hdlp);
695 
696 	switch (intr_op) {
697 
698 	case DDI_INTROP_SUPPORTED_TYPES:
699 		*(int *)result = DDI_INTR_TYPE_FIXED;
700 		break;
701 	case DDI_INTROP_GETCAP:
702 		*(int *)result =  DDI_INTR_FLAG_LEVEL;
703 		break;
704 	case DDI_INTROP_SETCAP:
705 		ret = DDI_ENOTSUP;
706 		break;
707 	case DDI_INTROP_ALLOC:
708 		/*  scratch1 = count,  # of intrs from DDI framework */
709 		*(int *)result = hdlp->ih_scratch1;
710 		break;
711 	case DDI_INTROP_FREE:
712 		/* Do we need to do anything here?  */
713 		break;
714 	case DDI_INTROP_GETPRI:
715 		*(int *)result = NIUMX_DEFAULT_PIL;
716 		break;
717 	case DDI_INTROP_SETPRI:
718 		ret = DDI_ENOTSUP;
719 		break;
720 	case DDI_INTROP_ADDISR:
721 		ret = niumx_add_intr(dip, rdip, hdlp);
722 		break;
723 	case DDI_INTROP_REMISR:
724 		ret = niumx_rem_intr(dip, rdip, hdlp);
725 		break;
726 	case DDI_INTROP_ENABLE:
727 		ret = niumx_set_intr(dip, rdip, hdlp, HV_INTR_VALID);
728 		break;
729 	case DDI_INTROP_DISABLE:
730 		ret = niumx_set_intr(dip, rdip, hdlp, HV_INTR_NOTVALID);
731 		break;
732 	case DDI_INTROP_SETMASK:
733 		ret = DDI_ENOTSUP;
734 		break;
735 	case DDI_INTROP_CLRMASK:
736 		ret = DDI_ENOTSUP;
737 		break;
738 	case DDI_INTROP_GETPENDING:
739 		ret = DDI_ENOTSUP;
740 		break;
741 	case DDI_INTROP_NINTRS:
742 	case DDI_INTROP_NAVAIL: {
743 		devino_t	*inos_p;
744 		int		inoslen;
745 		if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
746 			"interrupts", (caddr_t)&inos_p, &inoslen)
747 			!= DDI_SUCCESS) {
748 				ret = DDI_FAILURE;
749 				break;
750 			}
751 		*(int *)result = inoslen / sizeof (uint32_t);
752 		kmem_free(inos_p, inoslen);
753 		}
754 		break;
755 	default:
756 		ret = DDI_ENOTSUP;
757 		break;
758 	}
759 
760 	DBG(DBG_INTROPS, dip, "niumx_intr_ops: ret=%d\n", ret);
761 	return (ret);
762 }
763 
764 int
765 niumx_set_intr(dev_info_t *dip, dev_info_t *rdip,
766     ddi_intr_handle_impl_t *hdlp, int valid)
767 {
768 	niumx_ih_t	*ih_p;
769 	devino_t	*inos_p;
770 	int		inoslen, ret = DDI_SUCCESS;
771 	uint64_t	hvret;
772 
773 	ASSERT(hdlp->ih_inum < NIUMX_MAX_INTRS);
774 
775 	/* find the appropriate slot from the fixed table */
776 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
777 		"interrupts", (caddr_t)&inos_p, &inoslen) != DDI_SUCCESS) {
778 		ret = DDI_FAILURE;
779 		goto fail;
780 	}
781 	ih_p = niumx_ihtable + inos_p[hdlp->ih_inum];
782 	DBG(DBG_A_INTX, dip, "niumx_set_intr: rdip=%s%d, valid=%d %s (%x,%x)\n",
783 		NAMEINST(rdip), valid, valid ? "enabling" : "disabling",
784 		ih_p->ih_inum, ih_p->ih_sysino);
785 
786 	if (valid == HV_INTR_VALID)
787 		(void) hvio_intr_setstate(ih_p->ih_sysino, HV_INTR_IDLE_STATE);
788 	if ((hvret = hvio_intr_setvalid(ih_p->ih_sysino, valid))
789 		!= H_EOK) {
790 		DBG(DBG_A_INTX, dip, "hvio_intr_setvalid failed, ret 0x%x\n",
791 			hvret);
792 		ret = DDI_FAILURE;
793 	}
794 	kmem_free(inos_p, inoslen);
795 fail:
796 	return (ret);
797 }
798 
799 
800 
801 /*
802  * niumx_add_intr:
803  *
804  * This is the leaf/nexus/HV mapping, now read from "interrupts":
805  *
806  * we have a range of 64 to work with:
807  *   [0-15]  - reserved
808  *   [16]    - mac0
809  *   [17]    - MIF
810  *   [18]    - SYSERR
811  *   [19-26] - func0 Rx (qty. 8)
812  *   [27-34] - func0 Tx (qty. 8)
813  *   [35]    - mac1
814  *   [36-43] - func1 Rx (qty. 8)
815  *   [44-51] - func1 Tx (qty. 8)
816  */
817 int
818 niumx_add_intr(dev_info_t *dip, dev_info_t *rdip,
819     ddi_intr_handle_impl_t *hdlp)
820 {
821 	niumx_ih_t	*ih_p;
822 	int		inoslen, ret = DDI_SUCCESS;
823 	uint64_t	hvret;
824 	devino_t	*inos_p, ino; /* INO numbers, from "interrupts" prop */
825 	sysino_t	sysino;
826 
827 	/* get new ino */
828 	if (hdlp->ih_inum >= NIUMX_MAX_INTRS) {
829 		DBG(DBG_INTR, dip, "error: inum %d out of range\n",
830 			hdlp->ih_inum);
831 		ret = DDI_FAILURE;
832 		goto done;
833 	}
834 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
835 		"interrupts", (caddr_t)&inos_p, &inoslen) != DDI_SUCCESS) {
836 		ret = DDI_FAILURE;
837 		goto done;
838 	}
839 	ih_p = niumx_ihtable + inos_p[hdlp->ih_inum];
840 	ino = inos_p[hdlp->ih_inum];
841 	kmem_free(inos_p, inoslen);
842 	if ((hvret = hvio_intr_devino_to_sysino(DIP_TO_HANDLE(dip), ino,
843 		&sysino)) != H_EOK) {
844 		DBG(DBG_INTR, dip, "hvio_intr_devino_to_sysino failed, "
845 			"ret 0x%x\n", hvret);
846 		ret = DDI_FAILURE;
847 		goto done;
848 	}
849 	ih_p->ih_sysino = sysino;
850 	ih_p->ih_dip = dip;
851 	ih_p->ih_inum = hdlp->ih_inum;
852 	ih_p->ih_hdlr = hdlp->ih_cb_func;
853 	ih_p->ih_arg1 = hdlp->ih_cb_arg1;
854 	ih_p->ih_arg2 = hdlp->ih_cb_arg2;
855 
856 	DBG(DBG_A_INTX, dip, "niumx_add_intr: rdip=%s%d inum=0x%x "
857 		"handler=%p arg1=%p arg2=%p, new ih_p = %p\n", NAMEINST(rdip),
858 		hdlp->ih_inum, hdlp->ih_cb_func, hdlp->ih_cb_arg1,
859 		hdlp->ih_cb_arg2, ih_p);
860 
861 	if (hdlp->ih_pri == 0)
862 		hdlp->ih_pri = NIUMX_DEFAULT_PIL;
863 
864 	/* Save sysino value in hdlp */
865 	hdlp->ih_vector = ih_p->ih_sysino;
866 
867 	/* swap in our handler & arg */
868 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, (ddi_intr_handler_t *)niumx_intr_hdlr,
869 			(void *)ih_p, NULL);
870 
871 	DBG(DBG_A_INTX, dip, "for ino %x adding (%x,%x)\n", ino, ih_p->ih_inum,
872 			ih_p->ih_sysino);
873 	ret = i_ddi_add_ivintr(hdlp);
874 
875 	/* Restore orig. interrupt handler & args in handle. */
876 	DDI_INTR_ASSIGN_HDLR_N_ARGS(hdlp, ih_p->ih_hdlr, ih_p->ih_arg1,
877 		ih_p->ih_arg2);
878 
879 	if (ret != DDI_SUCCESS) {
880 		DBG(DBG_A_INTX, dip, "i_ddi_add_ivintr error ret=%x\n", ret);
881 		goto done;
882 	}
883 
884 	/* select cpu, saving it for removal */
885 	ih_p->ih_cpuid = intr_dist_cpuid();
886 
887 	if ((hvret = hvio_intr_settarget(ih_p->ih_sysino, ih_p->ih_cpuid))
888 		!= H_EOK) {
889 		DBG(DBG_A_INTX, dip, "hvio_intr_settarget failed, ret 0x%x\n",
890 			hvret);
891 		ret = DDI_FAILURE;
892 	}
893 done:
894 	DBG(DBG_A_INTX, dip, "done, ret = %d, ih_p 0x%p, hdlp 0x%p\n", ih_p,
895 		hdlp, ret);
896 	return (ret);
897 }
898 
899 /*
900  * niumx_rem_intr:
901  *
902  * This function is called to unregister interrupts.
903  */
904 int
905 niumx_rem_intr(dev_info_t *dip, dev_info_t *rdip,
906     ddi_intr_handle_impl_t *hdlp)
907 {
908 	niumx_ih_t	*ih_p;
909 	devino_t	*inos_p;
910 	int		inoslen, ret = DDI_SUCCESS, state;
911 	hrtime_t	start;
912 	sysino_t 	sysino;
913 
914 	ASSERT(hdlp->ih_inum < NIUMX_MAX_INTRS);
915 
916 	/* find the appropriate slot from the fixed table */
917 	if (ddi_getlongprop(DDI_DEV_T_ANY, rdip, DDI_PROP_DONTPASS,
918 		"interrupts", (caddr_t)&inos_p, &inoslen) != DDI_SUCCESS) {
919 		ret = DDI_FAILURE;
920 		goto fail1;
921 	}
922 	ih_p = niumx_ihtable + inos_p[hdlp->ih_inum];
923 	sysino = ih_p->ih_sysino;
924 	DBG(DBG_R_INTX, dip, "removing (%x,%x)\n", ih_p->ih_inum, sysino);
925 
926 	(void) hvio_intr_setvalid(sysino, HV_INTR_NOTVALID);
927 
928 	/* check for pending interrupts, busy wait if so */
929 	for (start = gethrtime(); !panicstr &&
930 	    (hvio_intr_getstate(sysino, &state) == H_EOK) &&
931 	    (state == HV_INTR_DELIVERED_STATE); /* */) {
932 		if (gethrtime() - start > niumx_intr_timeout) {
933 			cmn_err(CE_WARN, "%s%d: niumx_intr_dist: "
934 			    "pending interrupt (%x,%lx) timedout\n",
935 			    ddi_driver_name(dip), ddi_get_instance(dip),
936 			    ih_p->ih_inum, sysino);
937 			ret = DDI_FAILURE;
938 			goto fail2;
939 		}
940 	}
941 
942 	hdlp->ih_vector = (uint32_t)sysino;
943 	if (hdlp->ih_vector !=  NULL) i_ddi_rem_ivintr(hdlp);
944 
945 fail2:
946 	kmem_free(inos_p, inoslen);
947 fail1:
948 	return (ret);
949 }
950 
951 /*
952  * niumx_intr_hdlr (our interrupt handler)
953  */
954 uint_t
955 niumx_intr_hdlr(void *arg)
956 {
957 	niumx_ih_t *ih_p = (niumx_ih_t *)arg;
958 	uint_t		r;
959 
960 	DTRACE_PROBE4(interrupt__start, dev_info_t, ih_p->ih_dip, void *,
961 		ih_p->ih_hdlr, caddr_t, ih_p->ih_arg1, caddr_t, ih_p->ih_arg2);
962 
963 	r = (*ih_p->ih_hdlr)(ih_p->ih_arg1, ih_p->ih_arg2);
964 
965 	DTRACE_PROBE4(interrupt__complete, dev_info_t, ih_p->ih_dip, void *,
966 		ih_p->ih_hdlr, caddr_t, ih_p->ih_arg1, int, r);
967 
968 	(void) hvio_intr_setstate(ih_p->ih_sysino, HV_INTR_IDLE_STATE);
969 	return (r);
970 }
971 
972 #ifdef	DEBUG
973 uint64_t niumx_debug_flags = 0;
974 
975 static char *niumx_debug_sym [] = {	/* same sequence as niumx_debug_bit */
976 	/*  0 */ "attach",
977 	/*  1 */ "map",
978 	/*  2 */ "nex-ctlops",
979 	/*  3 */ "introps",
980 	/*  4 */ "intr-add",
981 	/*  5 */ "intr-rem",
982 	/*  6 */ "intr",
983 	/*  7 */ "dma-alloc",
984 	/*  8 */ "dma-bind",
985 	/*  9 */ "dma-unbind",
986 	/* 10 */ "chk-dma-mode"
987 };
988 
989 /*ARGSUSED*/
990 void
991 niumx_dbg(niumx_debug_bit_t bit, dev_info_t *dip, char *fmt, ...)
992 {
993 	va_list ap;
994 	char msgbuf[1024];
995 
996 	if (!(1ull << bit & niumx_debug_flags))
997 		return;
998 	va_start(ap, fmt);
999 	(void) vsprintf(msgbuf, fmt, ap);
1000 	va_end(ap);
1001 	cmn_err(CE_NOTE, "%s: %s", niumx_debug_sym[bit], msgbuf);
1002 }
1003 
1004 #endif	/* DEBUG */
1005