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