1 /*
2  * Copyright(c) 2007 Atheros Corporation. All rights reserved.
3  *
4  * Derived from Intel e1000 driver
5  * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the Free
9  * Software Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  * You should have received a copy of the GNU General Public License along with
18  * this program; if not, write to the Free Software Foundation, Inc., 59
19  * Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  */
21 
22 #include <linux/netdevice.h>
23 
24 #include "atl1e.h"
25 
26 /* This is the only thing that needs to be changed to adjust the
27  * maximum number of ports that the driver can manage.
28  */
29 
30 #define ATL1E_MAX_NIC 32
31 
32 #define OPTION_UNSET    -1
33 #define OPTION_DISABLED 0
34 #define OPTION_ENABLED  1
35 
36 /* All parameters are treated the same, as an integer array of values.
37  * This macro just reduces the need to repeat the same declaration code
38  * over and over (plus this helps to avoid typo bugs).
39  */
40 #define ATL1E_PARAM_INIT { [0 ... ATL1E_MAX_NIC] = OPTION_UNSET }
41 
42 #define ATL1E_PARAM(x, desc) \
43 	static int x[ATL1E_MAX_NIC + 1] = ATL1E_PARAM_INIT; \
44 	static unsigned int num_##x; \
45 	module_param_array_named(x, x, int, &num_##x, 0); \
46 	MODULE_PARM_DESC(x, desc);
47 
48 /* Transmit Memory count
49  *
50  * Valid Range: 64-2048
51  *
52  * Default Value: 128
53  */
54 #define ATL1E_MIN_TX_DESC_CNT		32
55 #define ATL1E_MAX_TX_DESC_CNT		1020
56 #define ATL1E_DEFAULT_TX_DESC_CNT	128
57 ATL1E_PARAM(tx_desc_cnt, "Transmit description count");
58 
59 /* Receive Memory Block Count
60  *
61  * Valid Range: 16-512
62  *
63  * Default Value: 128
64  */
65 #define ATL1E_MIN_RX_MEM_SIZE		8    /* 8KB   */
66 #define ATL1E_MAX_RX_MEM_SIZE		1024 /* 1MB   */
67 #define ATL1E_DEFAULT_RX_MEM_SIZE	256  /* 128KB */
68 ATL1E_PARAM(rx_mem_size, "memory size of rx buffer(KB)");
69 
70 /* User Specified MediaType Override
71  *
72  * Valid Range: 0-5
73  *  - 0    - auto-negotiate at all supported speeds
74  *  - 1    - only link at 100Mbps Full Duplex
75  *  - 2    - only link at 100Mbps Half Duplex
76  *  - 3    - only link at 10Mbps Full Duplex
77  *  - 4    - only link at 10Mbps Half Duplex
78  * Default Value: 0
79  */
80 
81 ATL1E_PARAM(media_type, "MediaType Select");
82 
83 /* Interrupt Moderate Timer in units of 2 us
84  *
85  * Valid Range: 10-65535
86  *
87  * Default Value: 45000(90ms)
88  */
89 #define INT_MOD_DEFAULT_CNT             100 /* 200us */
90 #define INT_MOD_MAX_CNT                 65000
91 #define INT_MOD_MIN_CNT                 50
92 ATL1E_PARAM(int_mod_timer, "Interrupt Moderator Timer");
93 
94 #define AUTONEG_ADV_DEFAULT  0x2F
95 #define AUTONEG_ADV_MASK     0x2F
96 #define FLOW_CONTROL_DEFAULT FLOW_CONTROL_FULL
97 
98 #define FLASH_VENDOR_DEFAULT    0
99 #define FLASH_VENDOR_MIN        0
100 #define FLASH_VENDOR_MAX        2
101 
102 struct atl1e_option {
103 	enum { enable_option, range_option, list_option } type;
104 	char *name;
105 	char *err;
106 	int  def;
107 	union {
108 		struct { /* range_option info */
109 			int min;
110 			int max;
111 		} r;
112 		struct { /* list_option info */
113 			int nr;
114 			struct atl1e_opt_list { int i; char *str; } *p;
115 		} l;
116 	} arg;
117 };
118 
119 static int atl1e_validate_option(int *value, struct atl1e_option *opt,
120 				 struct atl1e_adapter *adapter)
121 {
122 	if (*value == OPTION_UNSET) {
123 		*value = opt->def;
124 		return 0;
125 	}
126 
127 	switch (opt->type) {
128 	case enable_option:
129 		switch (*value) {
130 		case OPTION_ENABLED:
131 			netdev_info(adapter->netdev,
132 				    "%s Enabled\n", opt->name);
133 			return 0;
134 		case OPTION_DISABLED:
135 			netdev_info(adapter->netdev,
136 				    "%s Disabled\n", opt->name);
137 			return 0;
138 		}
139 		break;
140 	case range_option:
141 		if (*value >= opt->arg.r.min && *value <= opt->arg.r.max) {
142 			netdev_info(adapter->netdev, "%s set to %i\n",
143 				    opt->name, *value);
144 			return 0;
145 		}
146 		break;
147 	case list_option:{
148 			int i;
149 			struct atl1e_opt_list *ent;
150 
151 			for (i = 0; i < opt->arg.l.nr; i++) {
152 				ent = &opt->arg.l.p[i];
153 				if (*value == ent->i) {
154 					if (ent->str[0] != '\0')
155 						netdev_info(adapter->netdev,
156 							    "%s\n", ent->str);
157 					return 0;
158 				}
159 			}
160 			break;
161 		}
162 	default:
163 		BUG();
164 	}
165 
166 	netdev_info(adapter->netdev, "Invalid %s specified (%i) %s\n",
167 		    opt->name, *value, opt->err);
168 	*value = opt->def;
169 	return -1;
170 }
171 
172 /**
173  * atl1e_check_options - Range Checking for Command Line Parameters
174  * @adapter: board private structure
175  *
176  * This routine checks all command line parameters for valid user
177  * input.  If an invalid value is given, or if no user specified
178  * value exists, a default value is used.  The final value is stored
179  * in a variable in the adapter structure.
180  */
181 void atl1e_check_options(struct atl1e_adapter *adapter)
182 {
183 	int bd = adapter->bd_number;
184 
185 	if (bd >= ATL1E_MAX_NIC) {
186 		netdev_notice(adapter->netdev,
187 			      "no configuration for board #%i\n", bd);
188 		netdev_notice(adapter->netdev,
189 			      "Using defaults for all values\n");
190 	}
191 
192 	{ 		/* Transmit Ring Size */
193 		struct atl1e_option opt = {
194 			.type = range_option,
195 			.name = "Transmit Ddescription Count",
196 			.err  = "using default of "
197 				__MODULE_STRING(ATL1E_DEFAULT_TX_DESC_CNT),
198 			.def  = ATL1E_DEFAULT_TX_DESC_CNT,
199 			.arg  = { .r = { .min = ATL1E_MIN_TX_DESC_CNT,
200 					 .max = ATL1E_MAX_TX_DESC_CNT} }
201 		};
202 		int val;
203 		if (num_tx_desc_cnt > bd) {
204 			val = tx_desc_cnt[bd];
205 			atl1e_validate_option(&val, &opt, adapter);
206 			adapter->tx_ring.count = (u16) val & 0xFFFC;
207 		} else
208 			adapter->tx_ring.count = (u16)opt.def;
209 	}
210 
211 	{ 		/* Receive Memory Block Count */
212 		struct atl1e_option opt = {
213 			.type = range_option,
214 			.name = "Memory size of rx buffer(KB)",
215 			.err  = "using default of "
216 				__MODULE_STRING(ATL1E_DEFAULT_RX_MEM_SIZE),
217 			.def  = ATL1E_DEFAULT_RX_MEM_SIZE,
218 			.arg  = { .r = { .min = ATL1E_MIN_RX_MEM_SIZE,
219 					 .max = ATL1E_MAX_RX_MEM_SIZE} }
220 		};
221 		int val;
222 		if (num_rx_mem_size > bd) {
223 			val = rx_mem_size[bd];
224 			atl1e_validate_option(&val, &opt, adapter);
225 			adapter->rx_ring.page_size = (u32)val * 1024;
226 		} else {
227 			adapter->rx_ring.page_size = (u32)opt.def * 1024;
228 		}
229 	}
230 
231 	{ 		/* Interrupt Moderate Timer */
232 		struct atl1e_option opt = {
233 			.type = range_option,
234 			.name = "Interrupt Moderate Timer",
235 			.err  = "using default of "
236 				__MODULE_STRING(INT_MOD_DEFAULT_CNT),
237 			.def  = INT_MOD_DEFAULT_CNT,
238 			.arg  = { .r = { .min = INT_MOD_MIN_CNT,
239 					 .max = INT_MOD_MAX_CNT} }
240 		} ;
241 		int val;
242 		if (num_int_mod_timer > bd) {
243 			val = int_mod_timer[bd];
244 			atl1e_validate_option(&val, &opt, adapter);
245 			adapter->hw.imt = (u16) val;
246 		} else
247 			adapter->hw.imt = (u16)(opt.def);
248 	}
249 
250 	{ 		/* MediaType */
251 		struct atl1e_option opt = {
252 			.type = range_option,
253 			.name = "Speed/Duplex Selection",
254 			.err  = "using default of "
255 				__MODULE_STRING(MEDIA_TYPE_AUTO_SENSOR),
256 			.def  = MEDIA_TYPE_AUTO_SENSOR,
257 			.arg  = { .r = { .min = MEDIA_TYPE_AUTO_SENSOR,
258 					 .max = MEDIA_TYPE_10M_HALF} }
259 		} ;
260 		int val;
261 		if (num_media_type > bd) {
262 			val = media_type[bd];
263 			atl1e_validate_option(&val, &opt, adapter);
264 			adapter->hw.media_type = (u16) val;
265 		} else
266 			adapter->hw.media_type = (u16)(opt.def);
267 
268 	}
269 }
270