1 /*
2  * Copyright (C) 1999 - 2010 Intel Corporation.
3  * Copyright (C) 2010 OKI SEMICONDUCTOR Co., LTD.
4  *
5  * This code was derived from the Intel e1000e Linux driver.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 2 of the License.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include "pch_gbe.h"
21 #include <linux/module.h>	/* for __MODULE_STRING */
22 
23 #define OPTION_UNSET   -1
24 #define OPTION_DISABLED 0
25 #define OPTION_ENABLED  1
26 
27 /**
28  * TxDescriptors - Transmit Descriptor Count
29  * @Valid Range:   PCH_GBE_MIN_TXD - PCH_GBE_MAX_TXD
30  * @Default Value: PCH_GBE_DEFAULT_TXD
31  */
32 static int TxDescriptors = OPTION_UNSET;
33 module_param(TxDescriptors, int, 0);
34 MODULE_PARM_DESC(TxDescriptors, "Number of transmit descriptors");
35 
36 /**
37  * RxDescriptors -Receive Descriptor Count
38  * @Valid Range:   PCH_GBE_MIN_RXD - PCH_GBE_MAX_RXD
39  * @Default Value: PCH_GBE_DEFAULT_RXD
40  */
41 static int RxDescriptors = OPTION_UNSET;
42 module_param(RxDescriptors, int, 0);
43 MODULE_PARM_DESC(RxDescriptors, "Number of receive descriptors");
44 
45 /**
46  * Speed - User Specified Speed Override
47  * @Valid Range: 0, 10, 100, 1000
48  *   - 0:    auto-negotiate at all supported speeds
49  *   - 10:   only link at 10 Mbps
50  *   - 100:  only link at 100 Mbps
51  *   - 1000: only link at 1000 Mbps
52  * @Default Value: 0
53  */
54 static int Speed = OPTION_UNSET;
55 module_param(Speed, int, 0);
56 MODULE_PARM_DESC(Speed, "Speed setting");
57 
58 /**
59  * Duplex - User Specified Duplex Override
60  * @Valid Range: 0-2
61  *   - 0:  auto-negotiate for duplex
62  *   - 1:  only link at half duplex
63  *   - 2:  only link at full duplex
64  * @Default Value: 0
65  */
66 static int Duplex = OPTION_UNSET;
67 module_param(Duplex, int, 0);
68 MODULE_PARM_DESC(Duplex, "Duplex setting");
69 
70 #define HALF_DUPLEX 1
71 #define FULL_DUPLEX 2
72 
73 /**
74  * AutoNeg - Auto-negotiation Advertisement Override
75  * @Valid Range: 0x01-0x0F, 0x20-0x2F
76  *
77  *       The AutoNeg value is a bit mask describing which speed and duplex
78  *       combinations should be advertised during auto-negotiation.
79  *       The supported speed and duplex modes are listed below
80  *
81  *       Bit           7     6     5      4      3     2     1      0
82  *       Speed (Mbps)  N/A   N/A   1000   N/A    100   100   10     10
83  *       Duplex                    Full          Full  Half  Full   Half
84  *
85  * @Default Value: 0x2F (copper)
86  */
87 static int AutoNeg = OPTION_UNSET;
88 module_param(AutoNeg, int, 0);
89 MODULE_PARM_DESC(AutoNeg, "Advertised auto-negotiation setting");
90 
91 #define PHY_ADVERTISE_10_HALF      0x0001
92 #define PHY_ADVERTISE_10_FULL      0x0002
93 #define PHY_ADVERTISE_100_HALF     0x0004
94 #define PHY_ADVERTISE_100_FULL     0x0008
95 #define PHY_ADVERTISE_1000_HALF    0x0010 /* Not used, just FYI */
96 #define PHY_ADVERTISE_1000_FULL    0x0020
97 #define PCH_AUTONEG_ADVERTISE_DEFAULT   0x2F
98 
99 /**
100  * FlowControl - User Specified Flow Control Override
101  * @Valid Range: 0-3
102  *    - 0:  No Flow Control
103  *    - 1:  Rx only, respond to PAUSE frames but do not generate them
104  *    - 2:  Tx only, generate PAUSE frames but ignore them on receive
105  *    - 3:  Full Flow Control Support
106  * @Default Value: Read flow control settings from the EEPROM
107  */
108 static int FlowControl = OPTION_UNSET;
109 module_param(FlowControl, int, 0);
110 MODULE_PARM_DESC(FlowControl, "Flow Control setting");
111 
112 /*
113  * XsumRX - Receive Checksum Offload Enable/Disable
114  * @Valid Range: 0, 1
115  *    - 0:  disables all checksum offload
116  *    - 1:  enables receive IP/TCP/UDP checksum offload
117  * @Default Value: PCH_GBE_DEFAULT_RX_CSUM
118  */
119 static int XsumRX = OPTION_UNSET;
120 module_param(XsumRX, int, 0);
121 MODULE_PARM_DESC(XsumRX, "Disable or enable Receive Checksum offload");
122 
123 #define PCH_GBE_DEFAULT_RX_CSUM             true	/* trueorfalse */
124 
125 /*
126  * XsumTX - Transmit Checksum Offload Enable/Disable
127  * @Valid Range: 0, 1
128  *    - 0:  disables all checksum offload
129  *    - 1:  enables transmit IP/TCP/UDP checksum offload
130  * @Default Value: PCH_GBE_DEFAULT_TX_CSUM
131  */
132 static int XsumTX = OPTION_UNSET;
133 module_param(XsumTX, int, 0);
134 MODULE_PARM_DESC(XsumTX, "Disable or enable Transmit Checksum offload");
135 
136 #define PCH_GBE_DEFAULT_TX_CSUM             true	/* trueorfalse */
137 
138 /**
139  * pch_gbe_option - Force the MAC's flow control settings
140  * @hw:	            Pointer to the HW structure
141  * Returns:
142  *	0:			Successful.
143  *	Negative value:		Failed.
144  */
145 struct pch_gbe_option {
146 	enum { enable_option, range_option, list_option } type;
147 	char *name;
148 	char *err;
149 	int  def;
150 	union {
151 		struct { /* range_option info */
152 			int min;
153 			int max;
154 		} r;
155 		struct { /* list_option info */
156 			int nr;
157 			const struct pch_gbe_opt_list { int i; char *str; } *p;
158 		} l;
159 	} arg;
160 };
161 
162 static const struct pch_gbe_opt_list speed_list[] = {
163 	{ 0, "" },
164 	{ SPEED_10, "" },
165 	{ SPEED_100, "" },
166 	{ SPEED_1000, "" }
167 };
168 
169 static const struct pch_gbe_opt_list dplx_list[] = {
170 	{ 0, "" },
171 	{ HALF_DUPLEX, "" },
172 	{ FULL_DUPLEX, "" }
173 };
174 
175 static const struct pch_gbe_opt_list an_list[] =
176 	#define AA "AutoNeg advertising "
177 	{{ 0x01, AA "10/HD" },
178 	 { 0x02, AA "10/FD" },
179 	 { 0x03, AA "10/FD, 10/HD" },
180 	 { 0x04, AA "100/HD" },
181 	 { 0x05, AA "100/HD, 10/HD" },
182 	 { 0x06, AA "100/HD, 10/FD" },
183 	 { 0x07, AA "100/HD, 10/FD, 10/HD" },
184 	 { 0x08, AA "100/FD" },
185 	 { 0x09, AA "100/FD, 10/HD" },
186 	 { 0x0a, AA "100/FD, 10/FD" },
187 	 { 0x0b, AA "100/FD, 10/FD, 10/HD" },
188 	 { 0x0c, AA "100/FD, 100/HD" },
189 	 { 0x0d, AA "100/FD, 100/HD, 10/HD" },
190 	 { 0x0e, AA "100/FD, 100/HD, 10/FD" },
191 	 { 0x0f, AA "100/FD, 100/HD, 10/FD, 10/HD" },
192 	 { 0x20, AA "1000/FD" },
193 	 { 0x21, AA "1000/FD, 10/HD" },
194 	 { 0x22, AA "1000/FD, 10/FD" },
195 	 { 0x23, AA "1000/FD, 10/FD, 10/HD" },
196 	 { 0x24, AA "1000/FD, 100/HD" },
197 	 { 0x25, AA "1000/FD, 100/HD, 10/HD" },
198 	 { 0x26, AA "1000/FD, 100/HD, 10/FD" },
199 	 { 0x27, AA "1000/FD, 100/HD, 10/FD, 10/HD" },
200 	 { 0x28, AA "1000/FD, 100/FD" },
201 	 { 0x29, AA "1000/FD, 100/FD, 10/HD" },
202 	 { 0x2a, AA "1000/FD, 100/FD, 10/FD" },
203 	 { 0x2b, AA "1000/FD, 100/FD, 10/FD, 10/HD" },
204 	 { 0x2c, AA "1000/FD, 100/FD, 100/HD" },
205 	 { 0x2d, AA "1000/FD, 100/FD, 100/HD, 10/HD" },
206 	 { 0x2e, AA "1000/FD, 100/FD, 100/HD, 10/FD" },
207 	 { 0x2f, AA "1000/FD, 100/FD, 100/HD, 10/FD, 10/HD" }
208 };
209 
210 static const struct pch_gbe_opt_list fc_list[] = {
211 	{ PCH_GBE_FC_NONE, "Flow Control Disabled" },
212 	{ PCH_GBE_FC_RX_PAUSE, "Flow Control Receive Only" },
213 	{ PCH_GBE_FC_TX_PAUSE, "Flow Control Transmit Only" },
214 	{ PCH_GBE_FC_FULL, "Flow Control Enabled" }
215 };
216 
217 /**
218  * pch_gbe_validate_option - Validate option
219  * @value:    value
220  * @opt:      option
221  * @adapter:  Board private structure
222  * Returns:
223  *	0:			Successful.
224  *	Negative value:		Failed.
225  */
226 static int pch_gbe_validate_option(int *value,
227 				    const struct pch_gbe_option *opt,
228 				    struct pch_gbe_adapter *adapter)
229 {
230 	if (*value == OPTION_UNSET) {
231 		*value = opt->def;
232 		return 0;
233 	}
234 
235 	switch (opt->type) {
236 	case enable_option:
237 		switch (*value) {
238 		case OPTION_ENABLED:
239 			netdev_dbg(adapter->netdev, "%s Enabled\n", opt->name);
240 			return 0;
241 		case OPTION_DISABLED:
242 			netdev_dbg(adapter->netdev, "%s Disabled\n", opt->name);
243 			return 0;
244 		}
245 		break;
246 	case range_option:
247 		if (*value >= opt->arg.r.min && *value <= opt->arg.r.max) {
248 			netdev_dbg(adapter->netdev, "%s set to %i\n",
249 				   opt->name, *value);
250 			return 0;
251 		}
252 		break;
253 	case list_option: {
254 		int i;
255 		const struct pch_gbe_opt_list *ent;
256 
257 		for (i = 0; i < opt->arg.l.nr; i++) {
258 			ent = &opt->arg.l.p[i];
259 			if (*value == ent->i) {
260 				if (ent->str[0] != '\0')
261 					netdev_dbg(adapter->netdev, "%s\n",
262 						   ent->str);
263 				return 0;
264 			}
265 		}
266 	}
267 		break;
268 	default:
269 		BUG();
270 	}
271 
272 	netdev_dbg(adapter->netdev, "Invalid %s value specified (%i) %s\n",
273 		   opt->name, *value, opt->err);
274 	*value = opt->def;
275 	return -1;
276 }
277 
278 /**
279  * pch_gbe_check_copper_options - Range Checking for Link Options, Copper Version
280  * @adapter:  Board private structure
281  */
282 static void pch_gbe_check_copper_options(struct pch_gbe_adapter *adapter)
283 {
284 	struct pch_gbe_hw *hw = &adapter->hw;
285 	int speed, dplx;
286 
287 	{ /* Speed */
288 		static const struct pch_gbe_option opt = {
289 			.type = list_option,
290 			.name = "Speed",
291 			.err  = "parameter ignored",
292 			.def  = 0,
293 			.arg  = { .l = { .nr = (int)ARRAY_SIZE(speed_list),
294 					 .p = speed_list } }
295 		};
296 		speed = Speed;
297 		pch_gbe_validate_option(&speed, &opt, adapter);
298 	}
299 	{ /* Duplex */
300 		static const struct pch_gbe_option opt = {
301 			.type = list_option,
302 			.name = "Duplex",
303 			.err  = "parameter ignored",
304 			.def  = 0,
305 			.arg  = { .l = { .nr = (int)ARRAY_SIZE(dplx_list),
306 					 .p = dplx_list } }
307 		};
308 		dplx = Duplex;
309 		pch_gbe_validate_option(&dplx, &opt, adapter);
310 	}
311 
312 	{ /* Autoneg */
313 		static const struct pch_gbe_option opt = {
314 			.type = list_option,
315 			.name = "AutoNeg",
316 			.err  = "parameter ignored",
317 			.def  = PCH_AUTONEG_ADVERTISE_DEFAULT,
318 			.arg  = { .l = { .nr = (int)ARRAY_SIZE(an_list),
319 					 .p = an_list} }
320 		};
321 		if (speed || dplx) {
322 			netdev_dbg(adapter->netdev,
323 				   "AutoNeg specified along with Speed or Duplex, AutoNeg parameter ignored\n");
324 			hw->phy.autoneg_advertised = opt.def;
325 		} else {
326 			int tmp = AutoNeg;
327 
328 			pch_gbe_validate_option(&tmp, &opt, adapter);
329 			hw->phy.autoneg_advertised = tmp;
330 		}
331 	}
332 
333 	switch (speed + dplx) {
334 	case 0:
335 		hw->mac.autoneg = hw->mac.fc_autoneg = 1;
336 		if ((speed || dplx))
337 			netdev_dbg(adapter->netdev,
338 				   "Speed and duplex autonegotiation enabled\n");
339 		hw->mac.link_speed = SPEED_10;
340 		hw->mac.link_duplex = DUPLEX_HALF;
341 		break;
342 	case HALF_DUPLEX:
343 		netdev_dbg(adapter->netdev,
344 			   "Half Duplex specified without Speed\n");
345 		netdev_dbg(adapter->netdev,
346 			   "Using Autonegotiation at Half Duplex only\n");
347 		hw->mac.autoneg = hw->mac.fc_autoneg = 1;
348 		hw->phy.autoneg_advertised = PHY_ADVERTISE_10_HALF |
349 						PHY_ADVERTISE_100_HALF;
350 		hw->mac.link_speed = SPEED_10;
351 		hw->mac.link_duplex = DUPLEX_HALF;
352 		break;
353 	case FULL_DUPLEX:
354 		netdev_dbg(adapter->netdev,
355 			   "Full Duplex specified without Speed\n");
356 		netdev_dbg(adapter->netdev,
357 			   "Using Autonegotiation at Full Duplex only\n");
358 		hw->mac.autoneg = hw->mac.fc_autoneg = 1;
359 		hw->phy.autoneg_advertised = PHY_ADVERTISE_10_FULL |
360 						PHY_ADVERTISE_100_FULL |
361 						PHY_ADVERTISE_1000_FULL;
362 		hw->mac.link_speed = SPEED_10;
363 		hw->mac.link_duplex = DUPLEX_FULL;
364 		break;
365 	case SPEED_10:
366 		netdev_dbg(adapter->netdev,
367 			   "10 Mbps Speed specified without Duplex\n");
368 		netdev_dbg(adapter->netdev,
369 			   "Using Autonegotiation at 10 Mbps only\n");
370 		hw->mac.autoneg = hw->mac.fc_autoneg = 1;
371 		hw->phy.autoneg_advertised = PHY_ADVERTISE_10_HALF |
372 						PHY_ADVERTISE_10_FULL;
373 		hw->mac.link_speed = SPEED_10;
374 		hw->mac.link_duplex = DUPLEX_HALF;
375 		break;
376 	case SPEED_10 + HALF_DUPLEX:
377 		netdev_dbg(adapter->netdev, "Forcing to 10 Mbps Half Duplex\n");
378 		hw->mac.autoneg = hw->mac.fc_autoneg = 0;
379 		hw->phy.autoneg_advertised = 0;
380 		hw->mac.link_speed = SPEED_10;
381 		hw->mac.link_duplex = DUPLEX_HALF;
382 		break;
383 	case SPEED_10 + FULL_DUPLEX:
384 		netdev_dbg(adapter->netdev, "Forcing to 10 Mbps Full Duplex\n");
385 		hw->mac.autoneg = hw->mac.fc_autoneg = 0;
386 		hw->phy.autoneg_advertised = 0;
387 		hw->mac.link_speed = SPEED_10;
388 		hw->mac.link_duplex = DUPLEX_FULL;
389 		break;
390 	case SPEED_100:
391 		netdev_dbg(adapter->netdev,
392 			   "100 Mbps Speed specified without Duplex\n");
393 		netdev_dbg(adapter->netdev,
394 			   "Using Autonegotiation at 100 Mbps only\n");
395 		hw->mac.autoneg = hw->mac.fc_autoneg = 1;
396 		hw->phy.autoneg_advertised = PHY_ADVERTISE_100_HALF |
397 						PHY_ADVERTISE_100_FULL;
398 		hw->mac.link_speed = SPEED_100;
399 		hw->mac.link_duplex = DUPLEX_HALF;
400 		break;
401 	case SPEED_100 + HALF_DUPLEX:
402 		netdev_dbg(adapter->netdev,
403 			   "Forcing to 100 Mbps Half Duplex\n");
404 		hw->mac.autoneg = hw->mac.fc_autoneg = 0;
405 		hw->phy.autoneg_advertised = 0;
406 		hw->mac.link_speed = SPEED_100;
407 		hw->mac.link_duplex = DUPLEX_HALF;
408 		break;
409 	case SPEED_100 + FULL_DUPLEX:
410 		netdev_dbg(adapter->netdev,
411 			   "Forcing to 100 Mbps Full Duplex\n");
412 		hw->mac.autoneg = hw->mac.fc_autoneg = 0;
413 		hw->phy.autoneg_advertised = 0;
414 		hw->mac.link_speed = SPEED_100;
415 		hw->mac.link_duplex = DUPLEX_FULL;
416 		break;
417 	case SPEED_1000:
418 		netdev_dbg(adapter->netdev,
419 			   "1000 Mbps Speed specified without Duplex\n");
420 		goto full_duplex_only;
421 	case SPEED_1000 + HALF_DUPLEX:
422 		netdev_dbg(adapter->netdev,
423 			   "Half Duplex is not supported at 1000 Mbps\n");
424 		/* fall through */
425 	case SPEED_1000 + FULL_DUPLEX:
426 full_duplex_only:
427 		netdev_dbg(adapter->netdev,
428 			   "Using Autonegotiation at 1000 Mbps Full Duplex only\n");
429 		hw->mac.autoneg = hw->mac.fc_autoneg = 1;
430 		hw->phy.autoneg_advertised = PHY_ADVERTISE_1000_FULL;
431 		hw->mac.link_speed = SPEED_1000;
432 		hw->mac.link_duplex = DUPLEX_FULL;
433 		break;
434 	default:
435 		BUG();
436 	}
437 }
438 
439 /**
440  * pch_gbe_check_options - Range Checking for Command Line Parameters
441  * @adapter:  Board private structure
442  */
443 void pch_gbe_check_options(struct pch_gbe_adapter *adapter)
444 {
445 	struct pch_gbe_hw *hw = &adapter->hw;
446 	struct net_device *dev = adapter->netdev;
447 	int val;
448 
449 	{ /* Transmit Descriptor Count */
450 		static const struct pch_gbe_option opt = {
451 			.type = range_option,
452 			.name = "Transmit Descriptors",
453 			.err  = "using default of "
454 				__MODULE_STRING(PCH_GBE_DEFAULT_TXD),
455 			.def  = PCH_GBE_DEFAULT_TXD,
456 			.arg  = { .r = { .min = PCH_GBE_MIN_TXD,
457 					 .max = PCH_GBE_MAX_TXD } }
458 		};
459 		struct pch_gbe_tx_ring *tx_ring = adapter->tx_ring;
460 		tx_ring->count = TxDescriptors;
461 		pch_gbe_validate_option(&tx_ring->count, &opt, adapter);
462 		tx_ring->count = roundup(tx_ring->count,
463 					PCH_GBE_TX_DESC_MULTIPLE);
464 	}
465 	{ /* Receive Descriptor Count */
466 		static const struct pch_gbe_option opt = {
467 			.type = range_option,
468 			.name = "Receive Descriptors",
469 			.err  = "using default of "
470 				__MODULE_STRING(PCH_GBE_DEFAULT_RXD),
471 			.def  = PCH_GBE_DEFAULT_RXD,
472 			.arg  = { .r = { .min = PCH_GBE_MIN_RXD,
473 					 .max = PCH_GBE_MAX_RXD } }
474 		};
475 		struct pch_gbe_rx_ring *rx_ring = adapter->rx_ring;
476 		rx_ring->count = RxDescriptors;
477 		pch_gbe_validate_option(&rx_ring->count, &opt, adapter);
478 		rx_ring->count = roundup(rx_ring->count,
479 				PCH_GBE_RX_DESC_MULTIPLE);
480 	}
481 	{ /* Checksum Offload Enable/Disable */
482 		static const struct pch_gbe_option opt = {
483 			.type = enable_option,
484 			.name = "Checksum Offload",
485 			.err  = "defaulting to Enabled",
486 			.def  = PCH_GBE_DEFAULT_RX_CSUM
487 		};
488 		val = XsumRX;
489 		pch_gbe_validate_option(&val, &opt, adapter);
490 		if (!val)
491 			dev->features &= ~NETIF_F_RXCSUM;
492 	}
493 	{ /* Checksum Offload Enable/Disable */
494 		static const struct pch_gbe_option opt = {
495 			.type = enable_option,
496 			.name = "Checksum Offload",
497 			.err  = "defaulting to Enabled",
498 			.def  = PCH_GBE_DEFAULT_TX_CSUM
499 		};
500 		val = XsumTX;
501 		pch_gbe_validate_option(&val, &opt, adapter);
502 		if (!val)
503 			dev->features &= ~NETIF_F_CSUM_MASK;
504 	}
505 	{ /* Flow Control */
506 		static const struct pch_gbe_option opt = {
507 			.type = list_option,
508 			.name = "Flow Control",
509 			.err  = "reading default settings from EEPROM",
510 			.def  = PCH_GBE_FC_DEFAULT,
511 			.arg  = { .l = { .nr = (int)ARRAY_SIZE(fc_list),
512 					 .p = fc_list } }
513 		};
514 		int tmp = FlowControl;
515 
516 		pch_gbe_validate_option(&tmp, &opt, adapter);
517 		hw->mac.fc = tmp;
518 	}
519 
520 	pch_gbe_check_copper_options(adapter);
521 }
522