xref: /titanic_50/usr/src/uts/common/io/rge/rge_ndd.c (revision c77a61a72b5ecdc507d6cf104142edd371a16c84)
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 2006 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[] = {
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 
102 /*  ============== NDD Support Functions ===============  */
103 
104 /*
105  * Extracts the value from the rge parameter array and prints
106  * the parameter value. cp points to the required parameter.
107  */
108 static int
109 rge_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *credp)
110 {
111 	nd_param_t *ndp;
112 
113 	_NOTE(ARGUNUSED(q, credp))
114 
115 	ndp = (nd_param_t *)cp;
116 	(void) mi_mpprintf(mp, "%d", ndp->ndp_val);
117 
118 	return (0);
119 }
120 
121 /*
122  * Validates the request to set a RGE parameter to a specific value.
123  * If the request is OK, the parameter is set.  Also the <info> field
124  * is incremented to show that the parameter was touched, even though
125  * it may have been set to the same value it already had.
126  */
127 static int
128 rge_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *credp)
129 {
130 	nd_param_t *ndp;
131 	long new_value;
132 	char *end;
133 
134 	_NOTE(ARGUNUSED(q, mp, credp))
135 
136 	ndp = (nd_param_t *)cp;
137 	new_value = mi_strtol(value, &end, 10);
138 	if (end == value)
139 		return (EINVAL);
140 	if (new_value < ndp->ndp_min || new_value > ndp->ndp_max)
141 		return (EINVAL);
142 
143 	ndp->ndp_val = new_value;
144 	ndp->ndp_info += 1;
145 	return (0);
146 }
147 
148 /*
149  * Initialise the per-instance parameter array from the global prototype,
150  * and register each element with the named dispatch handler using nd_load()
151  */
152 static int
153 rge_param_register(rge_t *rgep)
154 {
155 	const nd_param_t *tmplp;
156 	dev_info_t *dip;
157 	nd_param_t *ndp;
158 	caddr_t *nddpp;
159 	pfi_t setfn;
160 	char *nm;
161 	int pval;
162 
163 	dip = rgep->devinfo;
164 	nddpp = &rgep->nd_data_p;
165 	ASSERT(*nddpp == NULL);
166 
167 	for (tmplp = nd_template; tmplp->ndp_name != NULL; ++tmplp) {
168 		/*
169 		 * Copy the template from nd_template[] into the
170 		 * proper slot in the per-instance parameters,
171 		 * then register the parameter with nd_load()
172 		 */
173 		ndp = &rgep->nd_params[tmplp->ndp_info];
174 		*ndp = *tmplp;
175 		nm = &ndp->ndp_name[0];
176 		setfn = rge_param_set;
177 
178 		switch (*nm) {
179 		default:
180 		case '!':
181 			continue;
182 
183 		case '+':
184 			break;
185 
186 		case '-':
187 			setfn = NULL;
188 			break;
189 		}
190 
191 		if (!nd_load(nddpp, ++nm, rge_param_get, setfn, (caddr_t)ndp))
192 			goto nd_fail;
193 
194 		/*
195 		 * If the parameter is writable, and there's a property
196 		 * with the same name, and its value is in range, we use
197 		 * it to initialise the parameter.  If it exists but is
198 		 * out of range, it's ignored.
199 		 */
200 		if (setfn && RGE_PROP_EXISTS(dip, nm)) {
201 			pval = RGE_PROP_GET_INT(dip, nm);
202 			if (pval >= ndp->ndp_min && pval <= ndp->ndp_max)
203 				ndp->ndp_val = pval;
204 		}
205 	}
206 
207 	RGE_DEBUG(("rge_param_register: OK"));
208 	return (DDI_SUCCESS);
209 
210 nd_fail:
211 	RGE_DEBUG(("rge_param_register: FAILED at index %d [info %d]",
212 		tmplp-nd_template, tmplp->ndp_info));
213 	nd_free(nddpp);
214 	return (DDI_FAILURE);
215 }
216 
217 int
218 rge_nd_init(rge_t *rgep)
219 {
220 	dev_info_t *dip;
221 	int duplex;
222 	int speed;
223 
224 	/*
225 	 * Register all the per-instance properties, initialising
226 	 * them from the table above or from driver properties set
227 	 * in the .conf file
228 	 */
229 	if (rge_param_register(rgep) != DDI_SUCCESS)
230 		return (-1);
231 
232 	/*
233 	 * The link speed may be forced to 10, 100 or 1000 Mbps using
234 	 * the property "transfer-speed". This may be done in OBP by
235 	 * using the command "apply transfer-speed=<speed> <device>".
236 	 * The speed may be 10, 100 or 1000 - any other value will be
237 	 * ignored.  Note that this does *enables* autonegotiation, but
238 	 * restricts it to the speed specified by the property.
239 	 */
240 	dip = rgep->devinfo;
241 	if (RGE_PROP_EXISTS(dip, transfer_speed_propname)) {
242 
243 		speed = RGE_PROP_GET_INT(dip, transfer_speed_propname);
244 		rge_log(rgep, "%s property is %d",
245 			transfer_speed_propname, speed);
246 
247 		switch (speed) {
248 		case 1000:
249 			rgep->param_adv_autoneg = 1;
250 			rgep->param_adv_1000fdx = 1;
251 			rgep->param_adv_1000hdx = 1;
252 			rgep->param_adv_100fdx = 0;
253 			rgep->param_adv_100hdx = 0;
254 			rgep->param_adv_10fdx = 0;
255 			rgep->param_adv_10hdx = 0;
256 			break;
257 
258 		case 100:
259 			rgep->param_adv_autoneg = 1;
260 			rgep->param_adv_1000fdx = 0;
261 			rgep->param_adv_1000hdx = 0;
262 			rgep->param_adv_100fdx = 1;
263 			rgep->param_adv_100hdx = 1;
264 			rgep->param_adv_10fdx = 0;
265 			rgep->param_adv_10hdx = 0;
266 			break;
267 
268 		case 10:
269 			rgep->param_adv_autoneg = 1;
270 			rgep->param_adv_1000fdx = 0;
271 			rgep->param_adv_1000hdx = 0;
272 			rgep->param_adv_100fdx = 0;
273 			rgep->param_adv_100hdx = 0;
274 			rgep->param_adv_10fdx = 1;
275 			rgep->param_adv_10hdx = 1;
276 			break;
277 
278 		default:
279 			break;
280 		}
281 	}
282 
283 	/*
284 	 * Also check the "speed" and "full-duplex" properties.  Setting
285 	 * these properties will override all other settings and *disable*
286 	 * autonegotiation, so both should be specified if either one is.
287 	 * Otherwise, the unspecified parameter will be set to a default
288 	 * value (1000Mb/s, full-duplex).
289 	 */
290 	if (RGE_PROP_EXISTS(dip, speed_propname) ||
291 	    RGE_PROP_EXISTS(dip, duplex_propname)) {
292 
293 		rgep->param_adv_autoneg = 0;
294 		rgep->param_adv_1000fdx = 1;
295 		rgep->param_adv_1000hdx = 1;
296 		rgep->param_adv_100fdx = 1;
297 		rgep->param_adv_100hdx = 1;
298 		rgep->param_adv_10fdx = 1;
299 		rgep->param_adv_10hdx = 1;
300 
301 		speed = RGE_PROP_GET_INT(dip, speed_propname);
302 		duplex = RGE_PROP_GET_INT(dip, duplex_propname);
303 		rge_log(rgep, "%s property is %d",
304 			speed_propname, speed);
305 		rge_log(rgep, "%s property is %d",
306 			duplex_propname, duplex);
307 
308 		switch (speed) {
309 		case 1000:
310 		default:
311 			rgep->param_adv_100fdx = 0;
312 			rgep->param_adv_100hdx = 0;
313 			rgep->param_adv_10fdx = 0;
314 			rgep->param_adv_10hdx = 0;
315 			break;
316 
317 		case 100:
318 			rgep->param_adv_1000fdx = 0;
319 			rgep->param_adv_1000hdx = 0;
320 			rgep->param_adv_10fdx = 0;
321 			rgep->param_adv_10hdx = 0;
322 			break;
323 
324 		case 10:
325 			rgep->param_adv_1000fdx = 0;
326 			rgep->param_adv_1000hdx = 0;
327 			rgep->param_adv_100fdx = 0;
328 			rgep->param_adv_100hdx = 0;
329 			break;
330 		}
331 
332 		switch (duplex) {
333 		default:
334 		case 1:
335 			rgep->param_adv_1000hdx = 0;
336 			rgep->param_adv_100hdx = 0;
337 			rgep->param_adv_10hdx = 0;
338 			break;
339 
340 		case 0:
341 			rgep->param_adv_1000fdx = 0;
342 			rgep->param_adv_100fdx = 0;
343 			rgep->param_adv_10fdx = 0;
344 			break;
345 		}
346 	}
347 
348 	RGE_DEBUG(("rge_nd_init: autoneg %d"
349 			"pause %d asym_pause %d "
350 			"1000fdx %d 1000hdx %d "
351 			"100fdx %d 100hdx %d "
352 			"10fdx %d 10hdx %d ",
353 		rgep->param_adv_autoneg,
354 		rgep->param_adv_pause, rgep->param_adv_asym_pause,
355 		rgep->param_adv_1000fdx, rgep->param_adv_1000hdx,
356 		rgep->param_adv_100fdx, rgep->param_adv_100hdx,
357 		rgep->param_adv_10fdx, rgep->param_adv_10hdx));
358 
359 	return (0);
360 }
361 
362 enum ioc_reply
363 rge_nd_ioctl(rge_t *rgep, queue_t *wq, mblk_t *mp, struct iocblk *iocp)
364 {
365 	nd_param_t *ndp;
366 	boolean_t ok;
367 	int info;
368 	int cmd;
369 
370 	RGE_TRACE(("rge_nd_ioctl($%p, $%p, $%p, $%p)",
371 		(void *)rgep, (void *)wq, (void *)mp, (void *)iocp));
372 
373 	ASSERT(mutex_owned(rgep->genlock));
374 
375 	cmd = iocp->ioc_cmd;
376 	switch (cmd) {
377 	default:
378 		/* NOTREACHED */
379 		rge_error(rgep, "rge_nd_ioctl: invalid cmd 0x%x", cmd);
380 		return (IOC_INVAL);
381 
382 	case ND_GET:
383 		/*
384 		 * If nd_getset() returns B_FALSE, the command was
385 		 * not valid (e.g. unknown name), so we just tell the
386 		 * top-level ioctl code to send a NAK (with code EINVAL).
387 		 *
388 		 * Otherwise, nd_getset() will have built the reply to
389 		 * be sent (but not actually sent it), so we tell the
390 		 * caller to send the prepared reply.
391 		 */
392 		ok = nd_getset(wq, rgep->nd_data_p, mp);
393 		RGE_DEBUG(("rge_nd_ioctl: get %s", ok ? "OK" : "FAIL"));
394 		return (ok ? IOC_REPLY : IOC_INVAL);
395 
396 	case ND_SET:
397 		/*
398 		 * All adv_* parameters are locked (read-only) while
399 		 * the device is in any sort of loopback mode ...
400 		 */
401 		if (rgep->param_loop_mode != RGE_LOOP_NONE) {
402 			iocp->ioc_error = EBUSY;
403 			return (IOC_INVAL);
404 		}
405 
406 		/*
407 		 * Before calling nd_getset(), we save the <info> field
408 		 * of the 'autonegotiation' parameter so that we can tell
409 		 * whether it was assigned (even if its value doesn't
410 		 * actually change).
411 		 */
412 		ndp = &rgep->nd_params[PARAM_ADV_AUTONEG_CAP];
413 		info = ndp->ndp_info;
414 		ok = nd_getset(wq, rgep->nd_data_p, mp);
415 
416 		/*
417 		 * If nd_getset() returns B_FALSE, the command was
418 		 * not valid (e.g. unknown name), so we just tell
419 		 * the top-level ioctl code to send a NAK (with code
420 		 * EINVAL by default).
421 		 *
422 		 * Otherwise, nd_getset() will have built the reply to
423 		 * be sent - but that doesn't imply success!  In some
424 		 * cases, the reply it's built will have a non-zero
425 		 * error code in it (e.g. EPERM if not superuser).
426 		 * So, we also drop out in that case ...
427 		 */
428 		RGE_DEBUG(("rge_nd_ioctl: set %s err %d autoneg %d info %d/%d",
429 			ok ? "OK" : "FAIL", iocp->ioc_error,
430 			ndp->ndp_val, info, ndp->ndp_info));
431 		if (!ok)
432 			return (IOC_INVAL);
433 		if (iocp->ioc_error)
434 			return (IOC_REPLY);
435 
436 		/*
437 		 * OK, a successful 'set'.  Prepare the messages explaining
438 		 * the link down/up cycle that will probably follow, then
439 		 * return IOC_RESTART_REPLY, telling the top-level ioctl
440 		 * code to update the PHY and restart the chip before
441 		 * sending our prepared reply
442 		 */
443 		if (ndp->ndp_val) {
444 			rgep->link_down_msg = " (autonegotiation enabled)";
445 			rgep->link_up_msg = " (autonegotiated)";
446 		} else {
447 			rgep->link_down_msg = " (autonegotiation disabled)";
448 			rgep->link_up_msg = " (forced)";
449 		}
450 		if (ndp->ndp_info == info)
451 			rgep->link_down_msg = " (advertised capabilities "
452 						"changed)";
453 
454 		return (IOC_RESTART_REPLY);
455 	}
456 }
457 
458 /* Free the Named Dispatch Table by calling nd_free */
459 void
460 rge_nd_cleanup(rge_t *rgep)
461 {
462 	nd_free(&rgep->nd_data_p);
463 }
464