1 /* Intel PRO/1000 Linux driver
2  * Copyright(c) 1999 - 2015 Intel Corporation.
3  *
4  * This program is free software; you can redistribute it and/or modify it
5  * under the terms and conditions of the GNU General Public License,
6  * version 2, as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope it will be useful, but WITHOUT
9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
11  * more details.
12  *
13  * The full GNU General Public License is included in this distribution in
14  * the file called "COPYING".
15  *
16  * Contact Information:
17  * Linux NICS <linux.nics@intel.com>
18  * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
19  * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
20  */
21 
22 #include <linux/netdevice.h>
23 #include <linux/module.h>
24 #include <linux/pci.h>
25 
26 #include "e1000.h"
27 
28 /* This is the only thing that needs to be changed to adjust the
29  * maximum number of ports that the driver can manage.
30  */
31 #define E1000_MAX_NIC 32
32 
33 #define OPTION_UNSET   -1
34 #define OPTION_DISABLED 0
35 #define OPTION_ENABLED  1
36 
37 #define COPYBREAK_DEFAULT 256
38 unsigned int copybreak = COPYBREAK_DEFAULT;
39 module_param(copybreak, uint, 0644);
40 MODULE_PARM_DESC(copybreak,
41 		 "Maximum size of packet that is copied to a new buffer on receive");
42 
43 /* All parameters are treated the same, as an integer array of values.
44  * This macro just reduces the need to repeat the same declaration code
45  * over and over (plus this helps to avoid typo bugs).
46  */
47 #define E1000_PARAM_INIT { [0 ... E1000_MAX_NIC] = OPTION_UNSET }
48 #define E1000_PARAM(X, desc)					\
49 	static int X[E1000_MAX_NIC+1] = E1000_PARAM_INIT;	\
50 	static unsigned int num_##X;				\
51 	module_param_array_named(X, X, int, &num_##X, 0);	\
52 	MODULE_PARM_DESC(X, desc);
53 
54 /* Transmit Interrupt Delay in units of 1.024 microseconds
55  * Tx interrupt delay needs to typically be set to something non-zero
56  *
57  * Valid Range: 0-65535
58  */
59 E1000_PARAM(TxIntDelay, "Transmit Interrupt Delay");
60 #define DEFAULT_TIDV 8
61 #define MAX_TXDELAY 0xFFFF
62 #define MIN_TXDELAY 0
63 
64 /* Transmit Absolute Interrupt Delay in units of 1.024 microseconds
65  *
66  * Valid Range: 0-65535
67  */
68 E1000_PARAM(TxAbsIntDelay, "Transmit Absolute Interrupt Delay");
69 #define DEFAULT_TADV 32
70 #define MAX_TXABSDELAY 0xFFFF
71 #define MIN_TXABSDELAY 0
72 
73 /* Receive Interrupt Delay in units of 1.024 microseconds
74  * hardware will likely hang if you set this to anything but zero.
75  *
76  * Burst variant is used as default if device has FLAG2_DMA_BURST.
77  *
78  * Valid Range: 0-65535
79  */
80 E1000_PARAM(RxIntDelay, "Receive Interrupt Delay");
81 #define DEFAULT_RDTR	0
82 #define BURST_RDTR	0x20
83 #define MAX_RXDELAY 0xFFFF
84 #define MIN_RXDELAY 0
85 
86 /* Receive Absolute Interrupt Delay in units of 1.024 microseconds
87  *
88  * Burst variant is used as default if device has FLAG2_DMA_BURST.
89  *
90  * Valid Range: 0-65535
91  */
92 E1000_PARAM(RxAbsIntDelay, "Receive Absolute Interrupt Delay");
93 #define DEFAULT_RADV	8
94 #define BURST_RADV	0x20
95 #define MAX_RXABSDELAY 0xFFFF
96 #define MIN_RXABSDELAY 0
97 
98 /* Interrupt Throttle Rate (interrupts/sec)
99  *
100  * Valid Range: 100-100000 or one of: 0=off, 1=dynamic, 3=dynamic conservative
101  */
102 E1000_PARAM(InterruptThrottleRate, "Interrupt Throttling Rate");
103 #define DEFAULT_ITR 3
104 #define MAX_ITR 100000
105 #define MIN_ITR 100
106 
107 /* IntMode (Interrupt Mode)
108  *
109  * Valid Range: varies depending on kernel configuration & hardware support
110  *
111  * legacy=0, MSI=1, MSI-X=2
112  *
113  * When MSI/MSI-X support is enabled in kernel-
114  *   Default Value: 2 (MSI-X) when supported by hardware, 1 (MSI) otherwise
115  * When MSI/MSI-X support is not enabled in kernel-
116  *   Default Value: 0 (legacy)
117  *
118  * When a mode is specified that is not allowed/supported, it will be
119  * demoted to the most advanced interrupt mode available.
120  */
121 E1000_PARAM(IntMode, "Interrupt Mode");
122 #define MAX_INTMODE	2
123 #define MIN_INTMODE	0
124 
125 /* Enable Smart Power Down of the PHY
126  *
127  * Valid Range: 0, 1
128  *
129  * Default Value: 0 (disabled)
130  */
131 E1000_PARAM(SmartPowerDownEnable, "Enable PHY smart power down");
132 
133 /* Enable Kumeran Lock Loss workaround
134  *
135  * Valid Range: 0, 1
136  *
137  * Default Value: 1 (enabled)
138  */
139 E1000_PARAM(KumeranLockLoss, "Enable Kumeran lock loss workaround");
140 
141 /* Write Protect NVM
142  *
143  * Valid Range: 0, 1
144  *
145  * Default Value: 1 (enabled)
146  */
147 E1000_PARAM(WriteProtectNVM,
148 	    "Write-protect NVM [WARNING: disabling this can lead to corrupted NVM]");
149 
150 /* Enable CRC Stripping
151  *
152  * Valid Range: 0, 1
153  *
154  * Default Value: 1 (enabled)
155  */
156 E1000_PARAM(CrcStripping,
157 	    "Enable CRC Stripping, disable if your BMC needs the CRC");
158 
159 struct e1000_option {
160 	enum { enable_option, range_option, list_option } type;
161 	const char *name;
162 	const char *err;
163 	int def;
164 	union {
165 		/* range_option info */
166 		struct {
167 			int min;
168 			int max;
169 		} r;
170 		/* list_option info */
171 		struct {
172 			int nr;
173 			struct e1000_opt_list {
174 				int i;
175 				char *str;
176 			} *p;
177 		} l;
178 	} arg;
179 };
180 
181 static int e1000_validate_option(unsigned int *value,
182 				 const struct e1000_option *opt,
183 				 struct e1000_adapter *adapter)
184 {
185 	if (*value == OPTION_UNSET) {
186 		*value = opt->def;
187 		return 0;
188 	}
189 
190 	switch (opt->type) {
191 	case enable_option:
192 		switch (*value) {
193 		case OPTION_ENABLED:
194 			dev_info(&adapter->pdev->dev, "%s Enabled\n",
195 				 opt->name);
196 			return 0;
197 		case OPTION_DISABLED:
198 			dev_info(&adapter->pdev->dev, "%s Disabled\n",
199 				 opt->name);
200 			return 0;
201 		}
202 		break;
203 	case range_option:
204 		if (*value >= opt->arg.r.min && *value <= opt->arg.r.max) {
205 			dev_info(&adapter->pdev->dev, "%s set to %i\n",
206 				 opt->name, *value);
207 			return 0;
208 		}
209 		break;
210 	case list_option: {
211 		int i;
212 		struct e1000_opt_list *ent;
213 
214 		for (i = 0; i < opt->arg.l.nr; i++) {
215 			ent = &opt->arg.l.p[i];
216 			if (*value == ent->i) {
217 				if (ent->str[0] != '\0')
218 					dev_info(&adapter->pdev->dev, "%s\n",
219 						 ent->str);
220 				return 0;
221 			}
222 		}
223 	}
224 		break;
225 	default:
226 		BUG();
227 	}
228 
229 	dev_info(&adapter->pdev->dev, "Invalid %s value specified (%i) %s\n",
230 		 opt->name, *value, opt->err);
231 	*value = opt->def;
232 	return -1;
233 }
234 
235 /**
236  * e1000e_check_options - Range Checking for Command Line Parameters
237  * @adapter: board private structure
238  *
239  * This routine checks all command line parameters for valid user
240  * input.  If an invalid value is given, or if no user specified
241  * value exists, a default value is used.  The final value is stored
242  * in a variable in the adapter structure.
243  **/
244 void e1000e_check_options(struct e1000_adapter *adapter)
245 {
246 	struct e1000_hw *hw = &adapter->hw;
247 	int bd = adapter->bd_number;
248 
249 	if (bd >= E1000_MAX_NIC) {
250 		dev_notice(&adapter->pdev->dev,
251 			   "Warning: no configuration for board #%i\n", bd);
252 		dev_notice(&adapter->pdev->dev,
253 			   "Using defaults for all values\n");
254 	}
255 
256 	/* Transmit Interrupt Delay */
257 	{
258 		static const struct e1000_option opt = {
259 			.type = range_option,
260 			.name = "Transmit Interrupt Delay",
261 			.err  = "using default of "
262 				__MODULE_STRING(DEFAULT_TIDV),
263 			.def  = DEFAULT_TIDV,
264 			.arg  = { .r = { .min = MIN_TXDELAY,
265 					 .max = MAX_TXDELAY } }
266 		};
267 
268 		if (num_TxIntDelay > bd) {
269 			adapter->tx_int_delay = TxIntDelay[bd];
270 			e1000_validate_option(&adapter->tx_int_delay, &opt,
271 					      adapter);
272 		} else {
273 			adapter->tx_int_delay = opt.def;
274 		}
275 	}
276 	/* Transmit Absolute Interrupt Delay */
277 	{
278 		static const struct e1000_option opt = {
279 			.type = range_option,
280 			.name = "Transmit Absolute Interrupt Delay",
281 			.err  = "using default of "
282 				__MODULE_STRING(DEFAULT_TADV),
283 			.def  = DEFAULT_TADV,
284 			.arg  = { .r = { .min = MIN_TXABSDELAY,
285 					 .max = MAX_TXABSDELAY } }
286 		};
287 
288 		if (num_TxAbsIntDelay > bd) {
289 			adapter->tx_abs_int_delay = TxAbsIntDelay[bd];
290 			e1000_validate_option(&adapter->tx_abs_int_delay, &opt,
291 					      adapter);
292 		} else {
293 			adapter->tx_abs_int_delay = opt.def;
294 		}
295 	}
296 	/* Receive Interrupt Delay */
297 	{
298 		static struct e1000_option opt = {
299 			.type = range_option,
300 			.name = "Receive Interrupt Delay",
301 			.err  = "using default of "
302 				__MODULE_STRING(DEFAULT_RDTR),
303 			.def  = DEFAULT_RDTR,
304 			.arg  = { .r = { .min = MIN_RXDELAY,
305 					 .max = MAX_RXDELAY } }
306 		};
307 
308 		if (adapter->flags2 & FLAG2_DMA_BURST)
309 			opt.def = BURST_RDTR;
310 
311 		if (num_RxIntDelay > bd) {
312 			adapter->rx_int_delay = RxIntDelay[bd];
313 			e1000_validate_option(&adapter->rx_int_delay, &opt,
314 					      adapter);
315 		} else {
316 			adapter->rx_int_delay = opt.def;
317 		}
318 	}
319 	/* Receive Absolute Interrupt Delay */
320 	{
321 		static struct e1000_option opt = {
322 			.type = range_option,
323 			.name = "Receive Absolute Interrupt Delay",
324 			.err  = "using default of "
325 				__MODULE_STRING(DEFAULT_RADV),
326 			.def  = DEFAULT_RADV,
327 			.arg  = { .r = { .min = MIN_RXABSDELAY,
328 					 .max = MAX_RXABSDELAY } }
329 		};
330 
331 		if (adapter->flags2 & FLAG2_DMA_BURST)
332 			opt.def = BURST_RADV;
333 
334 		if (num_RxAbsIntDelay > bd) {
335 			adapter->rx_abs_int_delay = RxAbsIntDelay[bd];
336 			e1000_validate_option(&adapter->rx_abs_int_delay, &opt,
337 					      adapter);
338 		} else {
339 			adapter->rx_abs_int_delay = opt.def;
340 		}
341 	}
342 	/* Interrupt Throttling Rate */
343 	{
344 		static const struct e1000_option opt = {
345 			.type = range_option,
346 			.name = "Interrupt Throttling Rate (ints/sec)",
347 			.err  = "using default of "
348 				__MODULE_STRING(DEFAULT_ITR),
349 			.def  = DEFAULT_ITR,
350 			.arg  = { .r = { .min = MIN_ITR,
351 					 .max = MAX_ITR } }
352 		};
353 
354 		if (num_InterruptThrottleRate > bd) {
355 			adapter->itr = InterruptThrottleRate[bd];
356 
357 			/* Make sure a message is printed for non-special
358 			 * values. And in case of an invalid option, display
359 			 * warning, use default and go through itr/itr_setting
360 			 * adjustment logic below
361 			 */
362 			if ((adapter->itr > 4) &&
363 			    e1000_validate_option(&adapter->itr, &opt, adapter))
364 				adapter->itr = opt.def;
365 		} else {
366 			/* If no option specified, use default value and go
367 			 * through the logic below to adjust itr/itr_setting
368 			 */
369 			adapter->itr = opt.def;
370 
371 			/* Make sure a message is printed for non-special
372 			 * default values
373 			 */
374 			if (adapter->itr > 4)
375 				dev_info(&adapter->pdev->dev,
376 					 "%s set to default %d\n", opt.name,
377 					 adapter->itr);
378 		}
379 
380 		adapter->itr_setting = adapter->itr;
381 		switch (adapter->itr) {
382 		case 0:
383 			dev_info(&adapter->pdev->dev, "%s turned off\n",
384 				 opt.name);
385 			break;
386 		case 1:
387 			dev_info(&adapter->pdev->dev,
388 				 "%s set to dynamic mode\n", opt.name);
389 			adapter->itr = 20000;
390 			break;
391 		case 2:
392 			dev_info(&adapter->pdev->dev,
393 				 "%s Invalid mode - setting default\n",
394 				 opt.name);
395 			adapter->itr_setting = opt.def;
396 			/* fall-through */
397 		case 3:
398 			dev_info(&adapter->pdev->dev,
399 				 "%s set to dynamic conservative mode\n",
400 				 opt.name);
401 			adapter->itr = 20000;
402 			break;
403 		case 4:
404 			dev_info(&adapter->pdev->dev,
405 				 "%s set to simplified (2000-8000 ints) mode\n",
406 				 opt.name);
407 			break;
408 		default:
409 			/* Save the setting, because the dynamic bits
410 			 * change itr.
411 			 *
412 			 * Clear the lower two bits because
413 			 * they are used as control.
414 			 */
415 			adapter->itr_setting &= ~3;
416 			break;
417 		}
418 	}
419 	/* Interrupt Mode */
420 	{
421 		static struct e1000_option opt = {
422 			.type = range_option,
423 			.name = "Interrupt Mode",
424 #ifndef CONFIG_PCI_MSI
425 			.err  = "defaulting to 0 (legacy)",
426 			.def  = E1000E_INT_MODE_LEGACY,
427 			.arg  = { .r = { .min = 0,
428 					 .max = 0 } }
429 #endif
430 		};
431 
432 #ifdef CONFIG_PCI_MSI
433 		if (adapter->flags & FLAG_HAS_MSIX) {
434 			opt.err = kstrdup("defaulting to 2 (MSI-X)",
435 					  GFP_KERNEL);
436 			opt.def = E1000E_INT_MODE_MSIX;
437 			opt.arg.r.max = E1000E_INT_MODE_MSIX;
438 		} else {
439 			opt.err = kstrdup("defaulting to 1 (MSI)", GFP_KERNEL);
440 			opt.def = E1000E_INT_MODE_MSI;
441 			opt.arg.r.max = E1000E_INT_MODE_MSI;
442 		}
443 
444 		if (!opt.err) {
445 			dev_err(&adapter->pdev->dev,
446 				"Failed to allocate memory\n");
447 			return;
448 		}
449 #endif
450 
451 		if (num_IntMode > bd) {
452 			unsigned int int_mode = IntMode[bd];
453 
454 			e1000_validate_option(&int_mode, &opt, adapter);
455 			adapter->int_mode = int_mode;
456 		} else {
457 			adapter->int_mode = opt.def;
458 		}
459 
460 #ifdef CONFIG_PCI_MSI
461 		kfree(opt.err);
462 #endif
463 	}
464 	/* Smart Power Down */
465 	{
466 		static const struct e1000_option opt = {
467 			.type = enable_option,
468 			.name = "PHY Smart Power Down",
469 			.err  = "defaulting to Disabled",
470 			.def  = OPTION_DISABLED
471 		};
472 
473 		if (num_SmartPowerDownEnable > bd) {
474 			unsigned int spd = SmartPowerDownEnable[bd];
475 
476 			e1000_validate_option(&spd, &opt, adapter);
477 			if ((adapter->flags & FLAG_HAS_SMART_POWER_DOWN) && spd)
478 				adapter->flags |= FLAG_SMART_POWER_DOWN;
479 		}
480 	}
481 	/* CRC Stripping */
482 	{
483 		static const struct e1000_option opt = {
484 			.type = enable_option,
485 			.name = "CRC Stripping",
486 			.err  = "defaulting to Enabled",
487 			.def  = OPTION_ENABLED
488 		};
489 
490 		if (num_CrcStripping > bd) {
491 			unsigned int crc_stripping = CrcStripping[bd];
492 
493 			e1000_validate_option(&crc_stripping, &opt, adapter);
494 			if (crc_stripping == OPTION_ENABLED) {
495 				adapter->flags2 |= FLAG2_CRC_STRIPPING;
496 				adapter->flags2 |= FLAG2_DFLT_CRC_STRIPPING;
497 			}
498 		} else {
499 			adapter->flags2 |= FLAG2_CRC_STRIPPING;
500 			adapter->flags2 |= FLAG2_DFLT_CRC_STRIPPING;
501 		}
502 	}
503 	/* Kumeran Lock Loss Workaround */
504 	{
505 		static const struct e1000_option opt = {
506 			.type = enable_option,
507 			.name = "Kumeran Lock Loss Workaround",
508 			.err  = "defaulting to Enabled",
509 			.def  = OPTION_ENABLED
510 		};
511 		bool enabled = opt.def;
512 
513 		if (num_KumeranLockLoss > bd) {
514 			unsigned int kmrn_lock_loss = KumeranLockLoss[bd];
515 
516 			e1000_validate_option(&kmrn_lock_loss, &opt, adapter);
517 			enabled = kmrn_lock_loss;
518 		}
519 
520 		if (hw->mac.type == e1000_ich8lan)
521 			e1000e_set_kmrn_lock_loss_workaround_ich8lan(hw,
522 								     enabled);
523 	}
524 	/* Write-protect NVM */
525 	{
526 		static const struct e1000_option opt = {
527 			.type = enable_option,
528 			.name = "Write-protect NVM",
529 			.err  = "defaulting to Enabled",
530 			.def  = OPTION_ENABLED
531 		};
532 
533 		if (adapter->flags & FLAG_IS_ICH) {
534 			if (num_WriteProtectNVM > bd) {
535 				unsigned int write_protect_nvm =
536 				    WriteProtectNVM[bd];
537 				e1000_validate_option(&write_protect_nvm, &opt,
538 						      adapter);
539 				if (write_protect_nvm)
540 					adapter->flags |= FLAG_READ_ONLY_NVM;
541 			} else {
542 				if (opt.def)
543 					adapter->flags |= FLAG_READ_ONLY_NVM;
544 			}
545 		}
546 	}
547 }
548