xref: /illumos-gate/usr/src/uts/common/io/rge/rge_ndd.c (revision bea83d026ee1bd1b2a2419e1d0232f107a5d7d9b)
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 #include "rge.h"
29 
30 #define	RGE_DBG		RGE_DBG_NDD	/* debug flag for this code	*/
31 
32 /*
33  * Property names
34  */
35 static char transfer_speed_propname[] = "transfer-speed";
36 static char speed_propname[] = "speed";
37 static char duplex_propname[] = "full-duplex";
38 
39 /*
40  * Notes:
41  *	The first character of the <name> field encodes the read/write
42  *	status of the parameter:
43  *		'-' => read-only,
44  *		'+' => read/write,
45  *		'!' => invisible!
46  *
47  *	For writable parameters, we check for a driver property with the
48  *	same name; if found, and its value is in range, we initialise
49  *	the parameter from the property, overriding the default in the
50  *	table below.
51  *
52  *	A NULL in the <name> field terminates the array.
53  *
54  *	The <info> field is used here to provide the index of the
55  *	parameter to be initialised; thus it doesn't matter whether
56  *	this table is kept ordered or not.
57  *
58  *	The <info> field in the per-instance copy, on the other hand,
59  *	is used to count assignments so that we can tell when a magic
60  *	parameter has been set via ndd (see rge_param_set()).
61  */
62 static const nd_param_t nd_template_1000[] = {
63 /*	info		min	max	init	r/w+name		*/
64 
65 /* Our hardware capabilities */
66 { PARAM_AUTONEG_CAP,	    0,	  1,	1,	"-autoneg_cap"		},
67 { PARAM_PAUSE_CAP,	    0,	  1,	1,	"-pause_cap"		},
68 { PARAM_ASYM_PAUSE_CAP,	    0,	  1,	1,	"-asym_pause_cap"	},
69 { PARAM_1000FDX_CAP,	    0,	  1,	1,	"-1000fdx_cap"		},
70 { PARAM_1000HDX_CAP,	    0,	  1,	0,	"-1000hdx_cap"		},
71 { PARAM_100T4_CAP,	    0,	  1,	0,	"-100T4_cap"		},
72 { PARAM_100FDX_CAP,	    0,	  1,	1,	"-100fdx_cap"		},
73 { PARAM_100HDX_CAP,	    0,	  1,	1,	"-100hdx_cap"		},
74 { PARAM_10FDX_CAP,	    0,	  1,	1,	"-10fdx_cap"		},
75 { PARAM_10HDX_CAP,	    0,	  1,	1,	"-10hdx_cap"		},
76 
77 /* Our advertised capabilities */
78 { PARAM_ADV_AUTONEG_CAP,    0,	  1,	1,	"-adv_autoneg_cap"	},
79 { PARAM_ADV_PAUSE_CAP,	    0,	  1,	1,	"+adv_pause_cap"	},
80 { PARAM_ADV_ASYM_PAUSE_CAP, 0,	  1,	1,	"+adv_asym_pause_cap"	},
81 { PARAM_ADV_1000FDX_CAP,    0,	  1,	1,	"+adv_1000fdx_cap"	},
82 { PARAM_ADV_1000HDX_CAP,    0,	  1,	0,	"-adv_1000hdx_cap"	},
83 { PARAM_ADV_100T4_CAP,	    0,	  1,	0,	"-adv_100T4_cap"	},
84 { PARAM_ADV_100FDX_CAP,	    0,	  1,	1,	"+adv_100fdx_cap"	},
85 { PARAM_ADV_100HDX_CAP,	    0,	  1,	1,	"+adv_100hdx_cap"	},
86 { PARAM_ADV_10FDX_CAP,	    0,	  1,	1,	"+adv_10fdx_cap"	},
87 { PARAM_ADV_10HDX_CAP,	    0,	  1,	1,	"+adv_10hdx_cap"	},
88 
89 /* Current operating modes */
90 { PARAM_LINK_STATUS,	    0,	  1,	0,	"-link_status"		},
91 { PARAM_LINK_SPEED,	    0,    1000,	0,	"-link_speed"		},
92 { PARAM_LINK_DUPLEX,	    0,	  2,	0,	"-link_duplex"		},
93 
94 /* Loopback status */
95 { PARAM_LOOP_MODE,	    0,	  2,	0,	"-loop_mode"		},
96 
97 /* Terminator */
98 { PARAM_COUNT,		    0,	  0,	0,	NULL			}
99 };
100 
101 /* nd_template for RTL8101E */
102 static const nd_param_t nd_template_100[] = {
103 /*	info		min	max	init	r/w+name		*/
104 
105 /* Our hardware capabilities */
106 { PARAM_AUTONEG_CAP,	    0,	  1,	1,	"-autoneg_cap"		},
107 { PARAM_PAUSE_CAP,	    0,	  1,	1,	"-pause_cap"		},
108 { PARAM_ASYM_PAUSE_CAP,	    0,	  1,	1,	"-asym_pause_cap"	},
109 { PARAM_1000FDX_CAP,	    0,	  1,	0,	"-1000fdx_cap"		},
110 { PARAM_1000HDX_CAP,	    0,	  1,	0,	"-1000hdx_cap"		},
111 { PARAM_100T4_CAP,	    0,	  1,	0,	"-100T4_cap"		},
112 { PARAM_100FDX_CAP,	    0,	  1,	1,	"-100fdx_cap"		},
113 { PARAM_100HDX_CAP,	    0,	  1,	1,	"-100hdx_cap"		},
114 { PARAM_10FDX_CAP,	    0,	  1,	1,	"-10fdx_cap"		},
115 { PARAM_10HDX_CAP,	    0,	  1,	1,	"-10hdx_cap"		},
116 
117 /* Our advertised capabilities */
118 { PARAM_ADV_AUTONEG_CAP,    0,	  1,	1,	"-adv_autoneg_cap"	},
119 { PARAM_ADV_PAUSE_CAP,	    0,	  1,	1,	"+adv_pause_cap"	},
120 { PARAM_ADV_ASYM_PAUSE_CAP, 0,	  1,	1,	"+adv_asym_pause_cap"	},
121 { PARAM_ADV_1000FDX_CAP,    0,	  1,	0,	"-adv_1000fdx_cap"	},
122 { PARAM_ADV_1000HDX_CAP,    0,	  1,	0,	"-adv_1000hdx_cap"	},
123 { PARAM_ADV_100T4_CAP,	    0,	  1,	0,	"-adv_100T4_cap"	},
124 { PARAM_ADV_100FDX_CAP,	    0,	  1,	1,	"+adv_100fdx_cap"	},
125 { PARAM_ADV_100HDX_CAP,	    0,	  1,	1,	"+adv_100hdx_cap"	},
126 { PARAM_ADV_10FDX_CAP,	    0,	  1,	1,	"+adv_10fdx_cap"	},
127 { PARAM_ADV_10HDX_CAP,	    0,	  1,	1,	"+adv_10hdx_cap"	},
128 
129 /* Current operating modes */
130 { PARAM_LINK_STATUS,	    0,	  1,	0,	"-link_status"		},
131 { PARAM_LINK_SPEED,	    0,    1000,	0,	"-link_speed"		},
132 { PARAM_LINK_DUPLEX,	    0,	  2,	0,	"-link_duplex"		},
133 
134 /* Loopback status */
135 { PARAM_LOOP_MODE,	    0,	  2,	0,	"-loop_mode"		},
136 
137 /* Terminator */
138 { PARAM_COUNT,		    0,	  0,	0,	NULL			}
139 };
140 
141 /*  ============== NDD Support Functions ===============  */
142 
143 /*
144  * Extracts the value from the rge parameter array and prints
145  * the parameter value. cp points to the required parameter.
146  */
147 static int
148 rge_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *credp)
149 {
150 	nd_param_t *ndp;
151 
152 	_NOTE(ARGUNUSED(q, credp))
153 
154 	ndp = (nd_param_t *)cp;
155 	(void) mi_mpprintf(mp, "%d", ndp->ndp_val);
156 
157 	return (0);
158 }
159 
160 /*
161  * Validates the request to set a RGE parameter to a specific value.
162  * If the request is OK, the parameter is set.  Also the <info> field
163  * is incremented to show that the parameter was touched, even though
164  * it may have been set to the same value it already had.
165  */
166 static int
167 rge_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *credp)
168 {
169 	nd_param_t *ndp;
170 	long new_value;
171 	char *end;
172 
173 	_NOTE(ARGUNUSED(q, mp, credp))
174 
175 	ndp = (nd_param_t *)cp;
176 	new_value = mi_strtol(value, &end, 10);
177 	if (end == value)
178 		return (EINVAL);
179 	if (new_value < ndp->ndp_min || new_value > ndp->ndp_max)
180 		return (EINVAL);
181 
182 	ndp->ndp_val = new_value;
183 	ndp->ndp_info += 1;
184 	return (0);
185 }
186 
187 /*
188  * Initialise the per-instance parameter array from the global prototype,
189  * and register each element with the named dispatch handler using nd_load()
190  */
191 static int
192 rge_param_register(rge_t *rgep)
193 {
194 	const nd_param_t *tmplp;
195 	dev_info_t *dip;
196 	nd_param_t *ndp;
197 	caddr_t *nddpp;
198 	pfi_t setfn;
199 	char *nm;
200 	int pval;
201 
202 	dip = rgep->devinfo;
203 	nddpp = &rgep->nd_data_p;
204 	ASSERT(*nddpp == NULL);
205 
206 	if (rgep->chipid.mac_ver == MAC_VER_8101E)
207 		tmplp = nd_template_100;
208 	else
209 		tmplp = nd_template_1000;
210 
211 	for (; tmplp->ndp_name != NULL; ++tmplp) {
212 		/*
213 		 * Copy the template from nd_template[] into the
214 		 * proper slot in the per-instance parameters,
215 		 * then register the parameter with nd_load()
216 		 */
217 		ndp = &rgep->nd_params[tmplp->ndp_info];
218 		*ndp = *tmplp;
219 		nm = &ndp->ndp_name[0];
220 		setfn = rge_param_set;
221 
222 		switch (*nm) {
223 		default:
224 		case '!':
225 			continue;
226 
227 		case '+':
228 			break;
229 
230 		case '-':
231 			setfn = NULL;
232 			break;
233 		}
234 
235 		if (!nd_load(nddpp, ++nm, rge_param_get, setfn, (caddr_t)ndp))
236 			goto nd_fail;
237 
238 		/*
239 		 * If the parameter is writable, and there's a property
240 		 * with the same name, and its value is in range, we use
241 		 * it to initialise the parameter.  If it exists but is
242 		 * out of range, it's ignored.
243 		 */
244 		if (setfn && RGE_PROP_EXISTS(dip, nm)) {
245 			pval = RGE_PROP_GET_INT(dip, nm);
246 			if (pval >= ndp->ndp_min && pval <= ndp->ndp_max)
247 				ndp->ndp_val = pval;
248 		}
249 	}
250 
251 	RGE_DEBUG(("rge_param_register: OK"));
252 	return (DDI_SUCCESS);
253 
254 nd_fail:
255 	if (rgep->chipid.mac_ver == MAC_VER_8101E) {
256 		RGE_DEBUG(("rge_param_register: FAILED at index %d [info %d]",
257 		    tmplp-nd_template_100, tmplp->ndp_info));
258 	} else {
259 		RGE_DEBUG(("rge_param_register: FAILED at index %d [info %d]",
260 		    tmplp-nd_template_1000, tmplp->ndp_info));
261 	}
262 	nd_free(nddpp);
263 	return (DDI_FAILURE);
264 }
265 
266 int
267 rge_nd_init(rge_t *rgep)
268 {
269 	dev_info_t *dip;
270 	int duplex;
271 	int speed;
272 
273 	/*
274 	 * Register all the per-instance properties, initialising
275 	 * them from the table above or from driver properties set
276 	 * in the .conf file
277 	 */
278 	if (rge_param_register(rgep) != DDI_SUCCESS)
279 		return (-1);
280 
281 	/*
282 	 * The link speed may be forced to 10, 100 or 1000 Mbps using
283 	 * the property "transfer-speed". This may be done in OBP by
284 	 * using the command "apply transfer-speed=<speed> <device>".
285 	 * The speed may be 10, 100 or 1000 - any other value will be
286 	 * ignored.  Note that this does *enables* autonegotiation, but
287 	 * restricts it to the speed specified by the property.
288 	 */
289 	dip = rgep->devinfo;
290 	if (RGE_PROP_EXISTS(dip, transfer_speed_propname)) {
291 
292 		speed = RGE_PROP_GET_INT(dip, transfer_speed_propname);
293 		rge_log(rgep, "%s property is %d",
294 		    transfer_speed_propname, speed);
295 
296 		switch (speed) {
297 		case 1000:
298 			rgep->param_adv_autoneg = 1;
299 			rgep->param_adv_1000fdx = 1;
300 			rgep->param_adv_1000hdx = 1;
301 			rgep->param_adv_100fdx = 0;
302 			rgep->param_adv_100hdx = 0;
303 			rgep->param_adv_10fdx = 0;
304 			rgep->param_adv_10hdx = 0;
305 			break;
306 
307 		case 100:
308 			rgep->param_adv_autoneg = 1;
309 			rgep->param_adv_1000fdx = 0;
310 			rgep->param_adv_1000hdx = 0;
311 			rgep->param_adv_100fdx = 1;
312 			rgep->param_adv_100hdx = 1;
313 			rgep->param_adv_10fdx = 0;
314 			rgep->param_adv_10hdx = 0;
315 			break;
316 
317 		case 10:
318 			rgep->param_adv_autoneg = 1;
319 			rgep->param_adv_1000fdx = 0;
320 			rgep->param_adv_1000hdx = 0;
321 			rgep->param_adv_100fdx = 0;
322 			rgep->param_adv_100hdx = 0;
323 			rgep->param_adv_10fdx = 1;
324 			rgep->param_adv_10hdx = 1;
325 			break;
326 
327 		default:
328 			break;
329 		}
330 	}
331 
332 	/*
333 	 * Also check the "speed" and "full-duplex" properties.  Setting
334 	 * these properties will override all other settings and *disable*
335 	 * autonegotiation, so both should be specified if either one is.
336 	 * Otherwise, the unspecified parameter will be set to a default
337 	 * value (1000Mb/s, full-duplex).
338 	 */
339 	if (RGE_PROP_EXISTS(dip, speed_propname) ||
340 	    RGE_PROP_EXISTS(dip, duplex_propname)) {
341 
342 		rgep->param_adv_autoneg = 0;
343 		rgep->param_adv_1000fdx = 1;
344 		rgep->param_adv_1000hdx = 1;
345 		rgep->param_adv_100fdx = 1;
346 		rgep->param_adv_100hdx = 1;
347 		rgep->param_adv_10fdx = 1;
348 		rgep->param_adv_10hdx = 1;
349 
350 		speed = RGE_PROP_GET_INT(dip, speed_propname);
351 		duplex = RGE_PROP_GET_INT(dip, duplex_propname);
352 		rge_log(rgep, "%s property is %d",
353 		    speed_propname, speed);
354 		rge_log(rgep, "%s property is %d",
355 		    duplex_propname, duplex);
356 
357 		switch (speed) {
358 		case 1000:
359 		default:
360 			rgep->param_adv_100fdx = 0;
361 			rgep->param_adv_100hdx = 0;
362 			rgep->param_adv_10fdx = 0;
363 			rgep->param_adv_10hdx = 0;
364 			break;
365 
366 		case 100:
367 			rgep->param_adv_1000fdx = 0;
368 			rgep->param_adv_1000hdx = 0;
369 			rgep->param_adv_10fdx = 0;
370 			rgep->param_adv_10hdx = 0;
371 			break;
372 
373 		case 10:
374 			rgep->param_adv_1000fdx = 0;
375 			rgep->param_adv_1000hdx = 0;
376 			rgep->param_adv_100fdx = 0;
377 			rgep->param_adv_100hdx = 0;
378 			break;
379 		}
380 
381 		switch (duplex) {
382 		default:
383 		case 1:
384 			rgep->param_adv_1000hdx = 0;
385 			rgep->param_adv_100hdx = 0;
386 			rgep->param_adv_10hdx = 0;
387 			break;
388 
389 		case 0:
390 			rgep->param_adv_1000fdx = 0;
391 			rgep->param_adv_100fdx = 0;
392 			rgep->param_adv_10fdx = 0;
393 			break;
394 		}
395 	}
396 
397 	RGE_DEBUG(("rge_nd_init: autoneg %d"
398 	    "pause %d asym_pause %d "
399 	    "1000fdx %d 1000hdx %d "
400 	    "100fdx %d 100hdx %d "
401 	    "10fdx %d 10hdx %d ",
402 	    rgep->param_adv_autoneg,
403 	    rgep->param_adv_pause, rgep->param_adv_asym_pause,
404 	    rgep->param_adv_1000fdx, rgep->param_adv_1000hdx,
405 	    rgep->param_adv_100fdx, rgep->param_adv_100hdx,
406 	    rgep->param_adv_10fdx, rgep->param_adv_10hdx));
407 
408 	return (0);
409 }
410 
411 enum ioc_reply
412 rge_nd_ioctl(rge_t *rgep, queue_t *wq, mblk_t *mp, struct iocblk *iocp)
413 {
414 	nd_param_t *ndp;
415 	boolean_t ok;
416 	int info;
417 	int cmd;
418 
419 	RGE_TRACE(("rge_nd_ioctl($%p, $%p, $%p, $%p)",
420 	    (void *)rgep, (void *)wq, (void *)mp, (void *)iocp));
421 
422 	ASSERT(mutex_owned(rgep->genlock));
423 
424 	cmd = iocp->ioc_cmd;
425 	switch (cmd) {
426 	default:
427 		/* NOTREACHED */
428 		rge_error(rgep, "rge_nd_ioctl: invalid cmd 0x%x", cmd);
429 		return (IOC_INVAL);
430 
431 	case ND_GET:
432 		/*
433 		 * If nd_getset() returns B_FALSE, the command was
434 		 * not valid (e.g. unknown name), so we just tell the
435 		 * top-level ioctl code to send a NAK (with code EINVAL).
436 		 *
437 		 * Otherwise, nd_getset() will have built the reply to
438 		 * be sent (but not actually sent it), so we tell the
439 		 * caller to send the prepared reply.
440 		 */
441 		ok = nd_getset(wq, rgep->nd_data_p, mp);
442 		RGE_DEBUG(("rge_nd_ioctl: get %s", ok ? "OK" : "FAIL"));
443 		return (ok ? IOC_REPLY : IOC_INVAL);
444 
445 	case ND_SET:
446 		/*
447 		 * All adv_* parameters are locked (read-only) while
448 		 * the device is in any sort of loopback mode ...
449 		 */
450 		if (rgep->param_loop_mode != RGE_LOOP_NONE) {
451 			iocp->ioc_error = EBUSY;
452 			return (IOC_INVAL);
453 		}
454 
455 		/*
456 		 * Before calling nd_getset(), we save the <info> field
457 		 * of the 'autonegotiation' parameter so that we can tell
458 		 * whether it was assigned (even if its value doesn't
459 		 * actually change).
460 		 */
461 		ndp = &rgep->nd_params[PARAM_ADV_AUTONEG_CAP];
462 		info = ndp->ndp_info;
463 		ok = nd_getset(wq, rgep->nd_data_p, mp);
464 
465 		/*
466 		 * If nd_getset() returns B_FALSE, the command was
467 		 * not valid (e.g. unknown name), so we just tell
468 		 * the top-level ioctl code to send a NAK (with code
469 		 * EINVAL by default).
470 		 *
471 		 * Otherwise, nd_getset() will have built the reply to
472 		 * be sent - but that doesn't imply success!  In some
473 		 * cases, the reply it's built will have a non-zero
474 		 * error code in it (e.g. EPERM if not superuser).
475 		 * So, we also drop out in that case ...
476 		 */
477 		RGE_DEBUG(("rge_nd_ioctl: set %s err %d autoneg %d info %d/%d",
478 		    ok ? "OK" : "FAIL", iocp->ioc_error,
479 		    ndp->ndp_val, info, ndp->ndp_info));
480 		if (!ok)
481 			return (IOC_INVAL);
482 		if (iocp->ioc_error)
483 			return (IOC_REPLY);
484 
485 		return (IOC_RESTART_REPLY);
486 	}
487 }
488 
489 /* Free the Named Dispatch Table by calling nd_free */
490 void
491 rge_nd_cleanup(rge_t *rgep)
492 {
493 	nd_free(&rgep->nd_data_p);
494 }
495