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