1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 1999 - 2008 Intel Corporation. */
3 
4 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
5 
6 #include "ixgb.h"
7 
8 /* This is the only thing that needs to be changed to adjust the
9  * maximum number of ports that the driver can manage.
10  */
11 
12 #define IXGB_MAX_NIC 8
13 
14 #define OPTION_UNSET	-1
15 #define OPTION_DISABLED 0
16 #define OPTION_ENABLED  1
17 
18 /* All parameters are treated the same, as an integer array of values.
19  * This macro just reduces the need to repeat the same declaration code
20  * over and over (plus this helps to avoid typo bugs).
21  */
22 
23 #define IXGB_PARAM_INIT { [0 ... IXGB_MAX_NIC] = OPTION_UNSET }
24 #define IXGB_PARAM(X, desc)					\
25 	static int X[IXGB_MAX_NIC+1]		\
26 		= IXGB_PARAM_INIT;				\
27 	static unsigned int num_##X = 0;			\
28 	module_param_array_named(X, X, int, &num_##X, 0);	\
29 	MODULE_PARM_DESC(X, desc);
30 
31 /* Transmit Descriptor Count
32  *
33  * Valid Range: 64-4096
34  *
35  * Default Value: 256
36  */
37 
38 IXGB_PARAM(TxDescriptors, "Number of transmit descriptors");
39 
40 /* Receive Descriptor Count
41  *
42  * Valid Range: 64-4096
43  *
44  * Default Value: 1024
45  */
46 
47 IXGB_PARAM(RxDescriptors, "Number of receive descriptors");
48 
49 /* User Specified Flow Control Override
50  *
51  * Valid Range: 0-3
52  *  - 0 - No Flow Control
53  *  - 1 - Rx only, respond to PAUSE frames but do not generate them
54  *  - 2 - Tx only, generate PAUSE frames but ignore them on receive
55  *  - 3 - Full Flow Control Support
56  *
57  * Default Value: 2 - Tx only (silicon bug avoidance)
58  */
59 
60 IXGB_PARAM(FlowControl, "Flow Control setting");
61 
62 /* XsumRX - Receive Checksum Offload Enable/Disable
63  *
64  * Valid Range: 0, 1
65  *  - 0 - disables all checksum offload
66  *  - 1 - enables receive IP/TCP/UDP checksum offload
67  *        on 82597 based NICs
68  *
69  * Default Value: 1
70  */
71 
72 IXGB_PARAM(XsumRX, "Disable or enable Receive Checksum offload");
73 
74 /* Transmit Interrupt Delay in units of 0.8192 microseconds
75  *
76  * Valid Range: 0-65535
77  *
78  * Default Value: 32
79  */
80 
81 IXGB_PARAM(TxIntDelay, "Transmit Interrupt Delay");
82 
83 /* Receive Interrupt Delay in units of 0.8192 microseconds
84  *
85  * Valid Range: 0-65535
86  *
87  * Default Value: 72
88  */
89 
90 IXGB_PARAM(RxIntDelay, "Receive Interrupt Delay");
91 
92 /* Receive Flow control high threshold (when we send a pause frame)
93  * (FCRTH)
94  *
95  * Valid Range: 1,536 - 262,136 (0x600 - 0x3FFF8, 8 byte granularity)
96  *
97  * Default Value: 196,608 (0x30000)
98  */
99 
100 IXGB_PARAM(RxFCHighThresh, "Receive Flow Control High Threshold");
101 
102 /* Receive Flow control low threshold (when we send a resume frame)
103  * (FCRTL)
104  *
105  * Valid Range: 64 - 262,136 (0x40 - 0x3FFF8, 8 byte granularity)
106  *              must be less than high threshold by at least 8 bytes
107  *
108  * Default Value:  163,840 (0x28000)
109  */
110 
111 IXGB_PARAM(RxFCLowThresh, "Receive Flow Control Low Threshold");
112 
113 /* Flow control request timeout (how long to pause the link partner's tx)
114  * (PAP 15:0)
115  *
116  * Valid Range: 1 - 65535
117  *
118  * Default Value:  65535 (0xffff) (we'll send an xon if we recover)
119  */
120 
121 IXGB_PARAM(FCReqTimeout, "Flow Control Request Timeout");
122 
123 /* Interrupt Delay Enable
124  *
125  * Valid Range: 0, 1
126  *
127  *  - 0 - disables transmit interrupt delay
128  *  - 1 - enables transmmit interrupt delay
129  *
130  * Default Value: 1
131  */
132 
133 IXGB_PARAM(IntDelayEnable, "Transmit Interrupt Delay Enable");
134 
135 
136 #define DEFAULT_TIDV	   		     32
137 #define MAX_TIDV			 0xFFFF
138 #define MIN_TIDV			      0
139 
140 #define DEFAULT_RDTR		   	     72
141 #define MAX_RDTR			 0xFFFF
142 #define MIN_RDTR			      0
143 
144 #define DEFAULT_FCRTL	  		0x28000
145 #define DEFAULT_FCRTH			0x30000
146 #define MIN_FCRTL			      0
147 #define MAX_FCRTL			0x3FFE8
148 #define MIN_FCRTH			      8
149 #define MAX_FCRTH			0x3FFF0
150 
151 #define MIN_FCPAUSE			      1
152 #define MAX_FCPAUSE			 0xffff
153 #define DEFAULT_FCPAUSE		  	 0xFFFF /* this may be too long */
154 
155 struct ixgb_option {
156 	enum { enable_option, range_option, list_option } type;
157 	const char *name;
158 	const char *err;
159 	int def;
160 	union {
161 		struct {	/* range_option info */
162 			int min;
163 			int max;
164 		} r;
165 		struct {	/* list_option info */
166 			int nr;
167 			const struct ixgb_opt_list {
168 				int i;
169 				const char *str;
170 			} *p;
171 		} l;
172 	} arg;
173 };
174 
175 static int
ixgb_validate_option(unsigned int * value,const struct ixgb_option * opt)176 ixgb_validate_option(unsigned int *value, const struct ixgb_option *opt)
177 {
178 	if (*value == OPTION_UNSET) {
179 		*value = opt->def;
180 		return 0;
181 	}
182 
183 	switch (opt->type) {
184 	case enable_option:
185 		switch (*value) {
186 		case OPTION_ENABLED:
187 			pr_info("%s Enabled\n", opt->name);
188 			return 0;
189 		case OPTION_DISABLED:
190 			pr_info("%s Disabled\n", opt->name);
191 			return 0;
192 		}
193 		break;
194 	case range_option:
195 		if (*value >= opt->arg.r.min && *value <= opt->arg.r.max) {
196 			pr_info("%s set to %i\n", opt->name, *value);
197 			return 0;
198 		}
199 		break;
200 	case list_option: {
201 		int i;
202 		const struct ixgb_opt_list *ent;
203 
204 		for (i = 0; i < opt->arg.l.nr; i++) {
205 			ent = &opt->arg.l.p[i];
206 			if (*value == ent->i) {
207 				if (ent->str[0] != '\0')
208 					pr_info("%s\n", ent->str);
209 				return 0;
210 			}
211 		}
212 	}
213 		break;
214 	default:
215 		BUG();
216 	}
217 
218 	pr_info("Invalid %s specified (%i) %s\n", opt->name, *value, opt->err);
219 	*value = opt->def;
220 	return -1;
221 }
222 
223 /**
224  * ixgb_check_options - Range Checking for Command Line Parameters
225  * @adapter: board private structure
226  *
227  * This routine checks all command line parameters for valid user
228  * input.  If an invalid value is given, or if no user specified
229  * value exists, a default value is used.  The final value is stored
230  * in a variable in the adapter structure.
231  **/
232 
233 void
ixgb_check_options(struct ixgb_adapter * adapter)234 ixgb_check_options(struct ixgb_adapter *adapter)
235 {
236 	int bd = adapter->bd_number;
237 	if (bd >= IXGB_MAX_NIC) {
238 		pr_notice("Warning: no configuration for board #%i\n", bd);
239 		pr_notice("Using defaults for all values\n");
240 	}
241 
242 	{ /* Transmit Descriptor Count */
243 		static const struct ixgb_option opt = {
244 			.type = range_option,
245 			.name = "Transmit Descriptors",
246 			.err  = "using default of " __MODULE_STRING(DEFAULT_TXD),
247 			.def  = DEFAULT_TXD,
248 			.arg  = { .r = { .min = MIN_TXD,
249 					 .max = MAX_TXD}}
250 		};
251 		struct ixgb_desc_ring *tx_ring = &adapter->tx_ring;
252 
253 		if (num_TxDescriptors > bd) {
254 			tx_ring->count = TxDescriptors[bd];
255 			ixgb_validate_option(&tx_ring->count, &opt);
256 		} else {
257 			tx_ring->count = opt.def;
258 		}
259 		tx_ring->count = ALIGN(tx_ring->count, IXGB_REQ_TX_DESCRIPTOR_MULTIPLE);
260 	}
261 	{ /* Receive Descriptor Count */
262 		static const struct ixgb_option opt = {
263 			.type = range_option,
264 			.name = "Receive Descriptors",
265 			.err  = "using default of " __MODULE_STRING(DEFAULT_RXD),
266 			.def  = DEFAULT_RXD,
267 			.arg  = { .r = { .min = MIN_RXD,
268 					 .max = MAX_RXD}}
269 		};
270 		struct ixgb_desc_ring *rx_ring = &adapter->rx_ring;
271 
272 		if (num_RxDescriptors > bd) {
273 			rx_ring->count = RxDescriptors[bd];
274 			ixgb_validate_option(&rx_ring->count, &opt);
275 		} else {
276 			rx_ring->count = opt.def;
277 		}
278 		rx_ring->count = ALIGN(rx_ring->count, IXGB_REQ_RX_DESCRIPTOR_MULTIPLE);
279 	}
280 	{ /* Receive Checksum Offload Enable */
281 		static const struct ixgb_option opt = {
282 			.type = enable_option,
283 			.name = "Receive Checksum Offload",
284 			.err  = "defaulting to Enabled",
285 			.def  = OPTION_ENABLED
286 		};
287 
288 		if (num_XsumRX > bd) {
289 			unsigned int rx_csum = XsumRX[bd];
290 			ixgb_validate_option(&rx_csum, &opt);
291 			adapter->rx_csum = rx_csum;
292 		} else {
293 			adapter->rx_csum = opt.def;
294 		}
295 	}
296 	{ /* Flow Control */
297 
298 		static const struct ixgb_opt_list fc_list[] = {
299 		       { ixgb_fc_none, "Flow Control Disabled" },
300 		       { ixgb_fc_rx_pause, "Flow Control Receive Only" },
301 		       { ixgb_fc_tx_pause, "Flow Control Transmit Only" },
302 		       { ixgb_fc_full, "Flow Control Enabled" },
303 		       { ixgb_fc_default, "Flow Control Hardware Default" }
304 		};
305 
306 		static const struct ixgb_option opt = {
307 			.type = list_option,
308 			.name = "Flow Control",
309 			.err  = "reading default settings from EEPROM",
310 			.def  = ixgb_fc_tx_pause,
311 			.arg  = { .l = { .nr = ARRAY_SIZE(fc_list),
312 					 .p = fc_list }}
313 		};
314 
315 		if (num_FlowControl > bd) {
316 			unsigned int fc = FlowControl[bd];
317 			ixgb_validate_option(&fc, &opt);
318 			adapter->hw.fc.type = fc;
319 		} else {
320 			adapter->hw.fc.type = opt.def;
321 		}
322 	}
323 	{ /* Receive Flow Control High Threshold */
324 		static const struct ixgb_option opt = {
325 			.type = range_option,
326 			.name = "Rx Flow Control High Threshold",
327 			.err  = "using default of " __MODULE_STRING(DEFAULT_FCRTH),
328 			.def  = DEFAULT_FCRTH,
329 			.arg  = { .r = { .min = MIN_FCRTH,
330 					 .max = MAX_FCRTH}}
331 		};
332 
333 		if (num_RxFCHighThresh > bd) {
334 			adapter->hw.fc.high_water = RxFCHighThresh[bd];
335 			ixgb_validate_option(&adapter->hw.fc.high_water, &opt);
336 		} else {
337 			adapter->hw.fc.high_water = opt.def;
338 		}
339 		if (!(adapter->hw.fc.type & ixgb_fc_tx_pause) )
340 			pr_info("Ignoring RxFCHighThresh when no RxFC\n");
341 	}
342 	{ /* Receive Flow Control Low Threshold */
343 		static const struct ixgb_option opt = {
344 			.type = range_option,
345 			.name = "Rx Flow Control Low Threshold",
346 			.err  = "using default of " __MODULE_STRING(DEFAULT_FCRTL),
347 			.def  = DEFAULT_FCRTL,
348 			.arg  = { .r = { .min = MIN_FCRTL,
349 					 .max = MAX_FCRTL}}
350 		};
351 
352 		if (num_RxFCLowThresh > bd) {
353 			adapter->hw.fc.low_water = RxFCLowThresh[bd];
354 			ixgb_validate_option(&adapter->hw.fc.low_water, &opt);
355 		} else {
356 			adapter->hw.fc.low_water = opt.def;
357 		}
358 		if (!(adapter->hw.fc.type & ixgb_fc_tx_pause) )
359 			pr_info("Ignoring RxFCLowThresh when no RxFC\n");
360 	}
361 	{ /* Flow Control Pause Time Request*/
362 		static const struct ixgb_option opt = {
363 			.type = range_option,
364 			.name = "Flow Control Pause Time Request",
365 			.err  = "using default of "__MODULE_STRING(DEFAULT_FCPAUSE),
366 			.def  = DEFAULT_FCPAUSE,
367 			.arg = { .r = { .min = MIN_FCPAUSE,
368 					.max = MAX_FCPAUSE}}
369 		};
370 
371 		if (num_FCReqTimeout > bd) {
372 			unsigned int pause_time = FCReqTimeout[bd];
373 			ixgb_validate_option(&pause_time, &opt);
374 			adapter->hw.fc.pause_time = pause_time;
375 		} else {
376 			adapter->hw.fc.pause_time = opt.def;
377 		}
378 		if (!(adapter->hw.fc.type & ixgb_fc_tx_pause) )
379 			pr_info("Ignoring FCReqTimeout when no RxFC\n");
380 	}
381 	/* high low and spacing check for rx flow control thresholds */
382 	if (adapter->hw.fc.type & ixgb_fc_tx_pause) {
383 		/* high must be greater than low */
384 		if (adapter->hw.fc.high_water < (adapter->hw.fc.low_water + 8)) {
385 			/* set defaults */
386 			pr_info("RxFCHighThresh must be >= (RxFCLowThresh + 8), Using Defaults\n");
387 			adapter->hw.fc.high_water = DEFAULT_FCRTH;
388 			adapter->hw.fc.low_water  = DEFAULT_FCRTL;
389 		}
390 	}
391 	{ /* Receive Interrupt Delay */
392 		static const struct ixgb_option opt = {
393 			.type = range_option,
394 			.name = "Receive Interrupt Delay",
395 			.err  = "using default of " __MODULE_STRING(DEFAULT_RDTR),
396 			.def  = DEFAULT_RDTR,
397 			.arg  = { .r = { .min = MIN_RDTR,
398 					 .max = MAX_RDTR}}
399 		};
400 
401 		if (num_RxIntDelay > bd) {
402 			adapter->rx_int_delay = RxIntDelay[bd];
403 			ixgb_validate_option(&adapter->rx_int_delay, &opt);
404 		} else {
405 			adapter->rx_int_delay = opt.def;
406 		}
407 	}
408 	{ /* Transmit Interrupt Delay */
409 		static const struct ixgb_option opt = {
410 			.type = range_option,
411 			.name = "Transmit Interrupt Delay",
412 			.err  = "using default of " __MODULE_STRING(DEFAULT_TIDV),
413 			.def  = DEFAULT_TIDV,
414 			.arg  = { .r = { .min = MIN_TIDV,
415 					 .max = MAX_TIDV}}
416 		};
417 
418 		if (num_TxIntDelay > bd) {
419 			adapter->tx_int_delay = TxIntDelay[bd];
420 			ixgb_validate_option(&adapter->tx_int_delay, &opt);
421 		} else {
422 			adapter->tx_int_delay = opt.def;
423 		}
424 	}
425 
426 	{ /* Transmit Interrupt Delay Enable */
427 		static const struct ixgb_option opt = {
428 			.type = enable_option,
429 			.name = "Tx Interrupt Delay Enable",
430 			.err  = "defaulting to Enabled",
431 			.def  = OPTION_ENABLED
432 		};
433 
434 		if (num_IntDelayEnable > bd) {
435 			unsigned int ide = IntDelayEnable[bd];
436 			ixgb_validate_option(&ide, &opt);
437 			adapter->tx_int_delay_enable = ide;
438 		} else {
439 			adapter->tx_int_delay_enable = opt.def;
440 		}
441 	}
442 }
443