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 __devinitdata 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 __devinit atl1e_validate_option(int *value, struct atl1e_option *opt, struct atl1e_adapter *adapter)
120 {
121 	if (*value == OPTION_UNSET) {
122 		*value = opt->def;
123 		return 0;
124 	}
125 
126 	switch (opt->type) {
127 	case enable_option:
128 		switch (*value) {
129 		case OPTION_ENABLED:
130 			netdev_info(adapter->netdev,
131 				    "%s Enabled\n", opt->name);
132 			return 0;
133 		case OPTION_DISABLED:
134 			netdev_info(adapter->netdev,
135 				    "%s Disabled\n", opt->name);
136 			return 0;
137 		}
138 		break;
139 	case range_option:
140 		if (*value >= opt->arg.r.min && *value <= opt->arg.r.max) {
141 			netdev_info(adapter->netdev, "%s set to %i\n",
142 				    opt->name, *value);
143 			return 0;
144 		}
145 		break;
146 	case list_option:{
147 			int i;
148 			struct atl1e_opt_list *ent;
149 
150 			for (i = 0; i < opt->arg.l.nr; i++) {
151 				ent = &opt->arg.l.p[i];
152 				if (*value == ent->i) {
153 					if (ent->str[0] != '\0')
154 						netdev_info(adapter->netdev,
155 							    "%s\n", ent->str);
156 					return 0;
157 				}
158 			}
159 			break;
160 		}
161 	default:
162 		BUG();
163 	}
164 
165 	netdev_info(adapter->netdev, "Invalid %s specified (%i) %s\n",
166 		    opt->name, *value, opt->err);
167 	*value = opt->def;
168 	return -1;
169 }
170 
171 /**
172  * atl1e_check_options - Range Checking for Command Line Parameters
173  * @adapter: board private structure
174  *
175  * This routine checks all command line parameters for valid user
176  * input.  If an invalid value is given, or if no user specified
177  * value exists, a default value is used.  The final value is stored
178  * in a variable in the adapter structure.
179  */
180 void __devinit atl1e_check_options(struct atl1e_adapter *adapter)
181 {
182 	int bd = adapter->bd_number;
183 
184 	if (bd >= ATL1E_MAX_NIC) {
185 		netdev_notice(adapter->netdev,
186 			      "no configuration for board #%i\n", bd);
187 		netdev_notice(adapter->netdev,
188 			      "Using defaults for all values\n");
189 	}
190 
191 	{ 		/* Transmit Ring Size */
192 		struct atl1e_option opt = {
193 			.type = range_option,
194 			.name = "Transmit Ddescription Count",
195 			.err  = "using default of "
196 				__MODULE_STRING(ATL1E_DEFAULT_TX_DESC_CNT),
197 			.def  = ATL1E_DEFAULT_TX_DESC_CNT,
198 			.arg  = { .r = { .min = ATL1E_MIN_TX_DESC_CNT,
199 					 .max = ATL1E_MAX_TX_DESC_CNT} }
200 		};
201 		int val;
202 		if (num_tx_desc_cnt > bd) {
203 			val = tx_desc_cnt[bd];
204 			atl1e_validate_option(&val, &opt, adapter);
205 			adapter->tx_ring.count = (u16) val & 0xFFFC;
206 		} else
207 			adapter->tx_ring.count = (u16)opt.def;
208 	}
209 
210 	{ 		/* Receive Memory Block Count */
211 		struct atl1e_option opt = {
212 			.type = range_option,
213 			.name = "Memory size of rx buffer(KB)",
214 			.err  = "using default of "
215 				__MODULE_STRING(ATL1E_DEFAULT_RX_MEM_SIZE),
216 			.def  = ATL1E_DEFAULT_RX_MEM_SIZE,
217 			.arg  = { .r = { .min = ATL1E_MIN_RX_MEM_SIZE,
218 					 .max = ATL1E_MAX_RX_MEM_SIZE} }
219 		};
220 		int val;
221 		if (num_rx_mem_size > bd) {
222 			val = rx_mem_size[bd];
223 			atl1e_validate_option(&val, &opt, adapter);
224 			adapter->rx_ring.page_size = (u32)val * 1024;
225 		} else {
226 			adapter->rx_ring.page_size = (u32)opt.def * 1024;
227 		}
228 	}
229 
230 	{ 		/* Interrupt Moderate Timer */
231 		struct atl1e_option opt = {
232 			.type = range_option,
233 			.name = "Interrupt Moderate Timer",
234 			.err  = "using default of "
235 				__MODULE_STRING(INT_MOD_DEFAULT_CNT),
236 			.def  = INT_MOD_DEFAULT_CNT,
237 			.arg  = { .r = { .min = INT_MOD_MIN_CNT,
238 					 .max = INT_MOD_MAX_CNT} }
239 		} ;
240 		int val;
241 		if (num_int_mod_timer > bd) {
242 			val = int_mod_timer[bd];
243 			atl1e_validate_option(&val, &opt, adapter);
244 			adapter->hw.imt = (u16) val;
245 		} else
246 			adapter->hw.imt = (u16)(opt.def);
247 	}
248 
249 	{ 		/* MediaType */
250 		struct atl1e_option opt = {
251 			.type = range_option,
252 			.name = "Speed/Duplex Selection",
253 			.err  = "using default of "
254 				__MODULE_STRING(MEDIA_TYPE_AUTO_SENSOR),
255 			.def  = MEDIA_TYPE_AUTO_SENSOR,
256 			.arg  = { .r = { .min = MEDIA_TYPE_AUTO_SENSOR,
257 					 .max = MEDIA_TYPE_10M_HALF} }
258 		} ;
259 		int val;
260 		if (num_media_type > bd) {
261 			val = media_type[bd];
262 			atl1e_validate_option(&val, &opt, adapter);
263 			adapter->hw.media_type = (u16) val;
264 		} else
265 			adapter->hw.media_type = (u16)(opt.def);
266 
267 	}
268 }
269