]>
Commit | Line | Data |
---|---|---|
024f7f31 IPG |
1 | /* |
2 | * Intel Wireless WiMAX Connection 2400m | |
3 | * Generic probe/disconnect, reset and message passing | |
4 | * | |
5 | * | |
6 | * Copyright (C) 2007-2008 Intel Corporation <linux-wimax@intel.com> | |
7 | * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU General Public License version | |
11 | * 2 as published by the Free Software Foundation. | |
12 | * | |
13 | * This program is distributed in the hope that it will be useful, | |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | * GNU General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License | |
19 | * along with this program; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
21 | * 02110-1301, USA. | |
22 | * | |
23 | * | |
24 | * See i2400m.h for driver documentation. This contains helpers for | |
25 | * the driver model glue [_setup()/_release()], handling device resets | |
26 | * [_dev_reset_handle()], and the backends for the WiMAX stack ops | |
27 | * reset [_op_reset()] and message from user [_op_msg_from_user()]. | |
28 | * | |
29 | * ROADMAP: | |
30 | * | |
31 | * i2400m_op_msg_from_user() | |
32 | * i2400m_msg_to_dev() | |
33 | * wimax_msg_to_user_send() | |
34 | * | |
35 | * i2400m_op_reset() | |
36 | * i240m->bus_reset() | |
37 | * | |
38 | * i2400m_dev_reset_handle() | |
39 | * __i2400m_dev_reset_handle() | |
40 | * __i2400m_dev_stop() | |
41 | * __i2400m_dev_start() | |
42 | * | |
43 | * i2400m_setup() | |
44 | * i2400m_bootrom_init() | |
45 | * register_netdev() | |
46 | * i2400m_dev_start() | |
47 | * __i2400m_dev_start() | |
48 | * i2400m_dev_bootstrap() | |
49 | * i2400m_tx_setup() | |
50 | * i2400m->bus_dev_start() | |
6a0f7ab8 | 51 | * i2400m_firmware_check() |
024f7f31 IPG |
52 | * i2400m_check_mac_addr() |
53 | * wimax_dev_add() | |
54 | * | |
55 | * i2400m_release() | |
56 | * wimax_dev_rm() | |
57 | * i2400m_dev_stop() | |
58 | * __i2400m_dev_stop() | |
59 | * i2400m_dev_shutdown() | |
60 | * i2400m->bus_dev_stop() | |
61 | * i2400m_tx_release() | |
62 | * unregister_netdev() | |
63 | */ | |
64 | #include "i2400m.h" | |
fe442683 | 65 | #include <linux/etherdevice.h> |
024f7f31 IPG |
66 | #include <linux/wimax/i2400m.h> |
67 | #include <linux/module.h> | |
68 | #include <linux/moduleparam.h> | |
69 | ||
70 | #define D_SUBMODULE driver | |
71 | #include "debug-levels.h" | |
72 | ||
73 | ||
74 | int i2400m_idle_mode_disabled; /* 0 (idle mode enabled) by default */ | |
75 | module_param_named(idle_mode_disabled, i2400m_idle_mode_disabled, int, 0644); | |
76 | MODULE_PARM_DESC(idle_mode_disabled, | |
77 | "If true, the device will not enable idle mode negotiation " | |
78 | "with the base station (when connected) to save power."); | |
79 | ||
c747583d IPG |
80 | int i2400m_rx_reorder_disabled; /* 0 (rx reorder enabled) by default */ |
81 | module_param_named(rx_reorder_disabled, i2400m_rx_reorder_disabled, int, 0644); | |
82 | MODULE_PARM_DESC(rx_reorder_disabled, | |
83 | "If true, RX reordering will be disabled."); | |
84 | ||
fb101674 IPG |
85 | int i2400m_power_save_disabled; /* 0 (power saving enabled) by default */ |
86 | module_param_named(power_save_disabled, i2400m_power_save_disabled, int, 0644); | |
87 | MODULE_PARM_DESC(power_save_disabled, | |
88 | "If true, the driver will not tell the device to enter " | |
89 | "power saving mode when it reports it is ready for it. " | |
90 | "False by default (so the device is told to do power " | |
91 | "saving)."); | |
92 | ||
4c2b1a11 IPG |
93 | static char i2400m_debug_params[128]; |
94 | module_param_string(debug, i2400m_debug_params, sizeof(i2400m_debug_params), | |
95 | 0644); | |
96 | MODULE_PARM_DESC(debug, | |
97 | "String of space-separated NAME:VALUE pairs, where NAMEs " | |
98 | "are the different debug submodules and VALUE are the " | |
99 | "initial debug value to set."); | |
100 | ||
aba3792a IPG |
101 | static char i2400m_barkers_params[128]; |
102 | module_param_string(barkers, i2400m_barkers_params, | |
103 | sizeof(i2400m_barkers_params), 0644); | |
104 | MODULE_PARM_DESC(barkers, | |
105 | "String of comma-separated 32-bit values; each is " | |
106 | "recognized as the value the device sends as a reboot " | |
107 | "signal; values are appended to a list--setting one value " | |
108 | "as zero cleans the existing list and starts a new one."); | |
109 | ||
b0fbcb2a IPG |
110 | static |
111 | struct i2400m_work *__i2400m_work_setup( | |
112 | struct i2400m *i2400m, void (*fn)(struct work_struct *), | |
113 | gfp_t gfp_flags, const void *pl, size_t pl_size) | |
114 | { | |
115 | struct i2400m_work *iw; | |
116 | ||
117 | iw = kzalloc(sizeof(*iw) + pl_size, gfp_flags); | |
118 | if (iw == NULL) | |
119 | return NULL; | |
120 | iw->i2400m = i2400m_get(i2400m); | |
121 | iw->pl_size = pl_size; | |
122 | memcpy(iw->pl, pl, pl_size); | |
123 | INIT_WORK(&iw->ws, fn); | |
124 | return iw; | |
125 | } | |
126 | ||
127 | ||
024f7f31 IPG |
128 | /** |
129 | * i2400m_queue_work - schedule work on a i2400m's queue | |
130 | * | |
131 | * @i2400m: device descriptor | |
132 | * | |
133 | * @fn: function to run to execute work. It gets passed a 'struct | |
134 | * work_struct' that is wrapped in a 'struct i2400m_work'. Once | |
135 | * done, you have to (1) i2400m_put(i2400m_work->i2400m) and then | |
136 | * (2) kfree(i2400m_work). | |
137 | * | |
138 | * @gfp_flags: GFP flags for memory allocation. | |
139 | * | |
140 | * @pl: pointer to a payload buffer that you want to pass to the _work | |
141 | * function. Use this to pack (for example) a struct with extra | |
142 | * arguments. | |
143 | * | |
144 | * @pl_size: size of the payload buffer. | |
145 | * | |
146 | * We do this quite often, so this just saves typing; allocate a | |
147 | * wrapper for a i2400m, get a ref to it, pack arguments and launch | |
148 | * the work. | |
149 | * | |
150 | * A usual workflow is: | |
151 | * | |
152 | * struct my_work_args { | |
153 | * void *something; | |
154 | * int whatever; | |
155 | * }; | |
156 | * ... | |
157 | * | |
158 | * struct my_work_args my_args = { | |
159 | * .something = FOO, | |
160 | * .whaetever = BLAH | |
161 | * }; | |
162 | * i2400m_queue_work(i2400m, 1, my_work_function, GFP_KERNEL, | |
163 | * &args, sizeof(args)) | |
164 | * | |
165 | * And now the work function can unpack the arguments and call the | |
166 | * real function (or do the job itself): | |
167 | * | |
168 | * static | |
169 | * void my_work_fn((struct work_struct *ws) | |
170 | * { | |
171 | * struct i2400m_work *iw = | |
172 | * container_of(ws, struct i2400m_work, ws); | |
173 | * struct my_work_args *my_args = (void *) iw->pl; | |
174 | * | |
175 | * my_work(iw->i2400m, my_args->something, my_args->whatevert); | |
176 | * } | |
177 | */ | |
178 | int i2400m_queue_work(struct i2400m *i2400m, | |
179 | void (*fn)(struct work_struct *), gfp_t gfp_flags, | |
180 | const void *pl, size_t pl_size) | |
181 | { | |
182 | int result; | |
183 | struct i2400m_work *iw; | |
184 | ||
185 | BUG_ON(i2400m->work_queue == NULL); | |
186 | result = -ENOMEM; | |
b0fbcb2a IPG |
187 | iw = __i2400m_work_setup(i2400m, fn, gfp_flags, pl, pl_size); |
188 | if (iw != NULL) { | |
189 | result = queue_work(i2400m->work_queue, &iw->ws); | |
190 | if (WARN_ON(result == 0)) | |
191 | result = -ENXIO; | |
192 | } | |
024f7f31 IPG |
193 | return result; |
194 | } | |
195 | EXPORT_SYMBOL_GPL(i2400m_queue_work); | |
196 | ||
197 | ||
198 | /* | |
199 | * Schedule i2400m's specific work on the system's queue. | |
200 | * | |
201 | * Used for a few cases where we really need it; otherwise, identical | |
202 | * to i2400m_queue_work(). | |
203 | * | |
204 | * Returns < 0 errno code on error, 1 if ok. | |
205 | * | |
206 | * If it returns zero, something really bad happened, as it means the | |
207 | * works struct was already queued, but we have just allocated it, so | |
208 | * it should not happen. | |
209 | */ | |
210 | int i2400m_schedule_work(struct i2400m *i2400m, | |
b0fbcb2a IPG |
211 | void (*fn)(struct work_struct *), gfp_t gfp_flags, |
212 | const void *pl, size_t pl_size) | |
024f7f31 IPG |
213 | { |
214 | int result; | |
215 | struct i2400m_work *iw; | |
216 | ||
024f7f31 | 217 | result = -ENOMEM; |
b0fbcb2a IPG |
218 | iw = __i2400m_work_setup(i2400m, fn, gfp_flags, pl, pl_size); |
219 | if (iw != NULL) { | |
220 | result = schedule_work(&iw->ws); | |
221 | if (WARN_ON(result == 0)) | |
222 | result = -ENXIO; | |
223 | } | |
024f7f31 IPG |
224 | return result; |
225 | } | |
226 | ||
227 | ||
228 | /* | |
229 | * WiMAX stack operation: relay a message from user space | |
230 | * | |
231 | * @wimax_dev: device descriptor | |
232 | * @pipe_name: named pipe the message is for | |
233 | * @msg_buf: pointer to the message bytes | |
234 | * @msg_len: length of the buffer | |
235 | * @genl_info: passed by the generic netlink layer | |
236 | * | |
237 | * The WiMAX stack will call this function when a message was received | |
238 | * from user space. | |
239 | * | |
240 | * For the i2400m, this is an L3L4 message, as specified in | |
241 | * include/linux/wimax/i2400m.h, and thus prefixed with a 'struct | |
242 | * i2400m_l3l4_hdr'. Driver (and device) expect the messages to be | |
243 | * coded in Little Endian. | |
244 | * | |
245 | * This function just verifies that the header declaration and the | |
246 | * payload are consistent and then deals with it, either forwarding it | |
247 | * to the device or procesing it locally. | |
248 | * | |
249 | * In the i2400m, messages are basically commands that will carry an | |
250 | * ack, so we use i2400m_msg_to_dev() and then deliver the ack back to | |
251 | * user space. The rx.c code might intercept the response and use it | |
252 | * to update the driver's state, but then it will pass it on so it can | |
253 | * be relayed back to user space. | |
254 | * | |
255 | * Note that asynchronous events from the device are processed and | |
256 | * sent to user space in rx.c. | |
257 | */ | |
258 | static | |
259 | int i2400m_op_msg_from_user(struct wimax_dev *wimax_dev, | |
260 | const char *pipe_name, | |
261 | const void *msg_buf, size_t msg_len, | |
262 | const struct genl_info *genl_info) | |
263 | { | |
264 | int result; | |
265 | struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev); | |
266 | struct device *dev = i2400m_dev(i2400m); | |
267 | struct sk_buff *ack_skb; | |
268 | ||
269 | d_fnstart(4, dev, "(wimax_dev %p [i2400m %p] msg_buf %p " | |
270 | "msg_len %zu genl_info %p)\n", wimax_dev, i2400m, | |
271 | msg_buf, msg_len, genl_info); | |
272 | ack_skb = i2400m_msg_to_dev(i2400m, msg_buf, msg_len); | |
273 | result = PTR_ERR(ack_skb); | |
274 | if (IS_ERR(ack_skb)) | |
275 | goto error_msg_to_dev; | |
024f7f31 IPG |
276 | result = wimax_msg_send(&i2400m->wimax_dev, ack_skb); |
277 | error_msg_to_dev: | |
278 | d_fnend(4, dev, "(wimax_dev %p [i2400m %p] msg_buf %p msg_len %zu " | |
279 | "genl_info %p) = %d\n", wimax_dev, i2400m, msg_buf, msg_len, | |
280 | genl_info, result); | |
281 | return result; | |
282 | } | |
283 | ||
284 | ||
285 | /* | |
286 | * Context to wait for a reset to finalize | |
287 | */ | |
288 | struct i2400m_reset_ctx { | |
289 | struct completion completion; | |
290 | int result; | |
291 | }; | |
292 | ||
293 | ||
294 | /* | |
295 | * WiMAX stack operation: reset a device | |
296 | * | |
297 | * @wimax_dev: device descriptor | |
298 | * | |
299 | * See the documentation for wimax_reset() and wimax_dev->op_reset for | |
300 | * the requirements of this function. The WiMAX stack guarantees | |
301 | * serialization on calls to this function. | |
302 | * | |
303 | * Do a warm reset on the device; if it fails, resort to a cold reset | |
304 | * and return -ENODEV. On successful warm reset, we need to block | |
305 | * until it is complete. | |
306 | * | |
307 | * The bus-driver implementation of reset takes care of falling back | |
308 | * to cold reset if warm fails. | |
309 | */ | |
310 | static | |
311 | int i2400m_op_reset(struct wimax_dev *wimax_dev) | |
312 | { | |
313 | int result; | |
314 | struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev); | |
315 | struct device *dev = i2400m_dev(i2400m); | |
316 | struct i2400m_reset_ctx ctx = { | |
317 | .completion = COMPLETION_INITIALIZER_ONSTACK(ctx.completion), | |
318 | .result = 0, | |
319 | }; | |
320 | ||
321 | d_fnstart(4, dev, "(wimax_dev %p)\n", wimax_dev); | |
322 | mutex_lock(&i2400m->init_mutex); | |
323 | i2400m->reset_ctx = &ctx; | |
324 | mutex_unlock(&i2400m->init_mutex); | |
325 | result = i2400m->bus_reset(i2400m, I2400M_RT_WARM); | |
326 | if (result < 0) | |
327 | goto out; | |
328 | result = wait_for_completion_timeout(&ctx.completion, 4*HZ); | |
329 | if (result == 0) | |
330 | result = -ETIMEDOUT; | |
331 | else if (result > 0) | |
332 | result = ctx.result; | |
333 | /* if result < 0, pass it on */ | |
334 | mutex_lock(&i2400m->init_mutex); | |
335 | i2400m->reset_ctx = NULL; | |
336 | mutex_unlock(&i2400m->init_mutex); | |
337 | out: | |
338 | d_fnend(4, dev, "(wimax_dev %p) = %d\n", wimax_dev, result); | |
339 | return result; | |
340 | } | |
341 | ||
342 | ||
343 | /* | |
344 | * Check the MAC address we got from boot mode is ok | |
345 | * | |
346 | * @i2400m: device descriptor | |
347 | * | |
348 | * Returns: 0 if ok, < 0 errno code on error. | |
349 | */ | |
350 | static | |
351 | int i2400m_check_mac_addr(struct i2400m *i2400m) | |
352 | { | |
353 | int result; | |
354 | struct device *dev = i2400m_dev(i2400m); | |
355 | struct sk_buff *skb; | |
356 | const struct i2400m_tlv_detailed_device_info *ddi; | |
357 | struct net_device *net_dev = i2400m->wimax_dev.net_dev; | |
358 | const unsigned char zeromac[ETH_ALEN] = { 0 }; | |
359 | ||
360 | d_fnstart(3, dev, "(i2400m %p)\n", i2400m); | |
361 | skb = i2400m_get_device_info(i2400m); | |
362 | if (IS_ERR(skb)) { | |
363 | result = PTR_ERR(skb); | |
364 | dev_err(dev, "Cannot verify MAC address, error reading: %d\n", | |
365 | result); | |
366 | goto error; | |
367 | } | |
368 | /* Extract MAC addresss */ | |
369 | ddi = (void *) skb->data; | |
370 | BUILD_BUG_ON(ETH_ALEN != sizeof(ddi->mac_address)); | |
371 | d_printf(2, dev, "GET DEVICE INFO: mac addr " | |
372 | "%02x:%02x:%02x:%02x:%02x:%02x\n", | |
373 | ddi->mac_address[0], ddi->mac_address[1], | |
374 | ddi->mac_address[2], ddi->mac_address[3], | |
375 | ddi->mac_address[4], ddi->mac_address[5]); | |
376 | if (!memcmp(net_dev->perm_addr, ddi->mac_address, | |
377 | sizeof(ddi->mac_address))) | |
378 | goto ok; | |
379 | dev_warn(dev, "warning: device reports a different MAC address " | |
380 | "to that of boot mode's\n"); | |
381 | dev_warn(dev, "device reports %02x:%02x:%02x:%02x:%02x:%02x\n", | |
382 | ddi->mac_address[0], ddi->mac_address[1], | |
383 | ddi->mac_address[2], ddi->mac_address[3], | |
384 | ddi->mac_address[4], ddi->mac_address[5]); | |
385 | dev_warn(dev, "boot mode reported %02x:%02x:%02x:%02x:%02x:%02x\n", | |
386 | net_dev->perm_addr[0], net_dev->perm_addr[1], | |
387 | net_dev->perm_addr[2], net_dev->perm_addr[3], | |
388 | net_dev->perm_addr[4], net_dev->perm_addr[5]); | |
389 | if (!memcmp(zeromac, ddi->mac_address, sizeof(zeromac))) | |
390 | dev_err(dev, "device reports an invalid MAC address, " | |
391 | "not updating\n"); | |
392 | else { | |
393 | dev_warn(dev, "updating MAC address\n"); | |
394 | net_dev->addr_len = ETH_ALEN; | |
395 | memcpy(net_dev->perm_addr, ddi->mac_address, ETH_ALEN); | |
396 | memcpy(net_dev->dev_addr, ddi->mac_address, ETH_ALEN); | |
397 | } | |
398 | ok: | |
399 | result = 0; | |
400 | kfree_skb(skb); | |
401 | error: | |
402 | d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result); | |
403 | return result; | |
404 | } | |
405 | ||
406 | ||
407 | /** | |
408 | * __i2400m_dev_start - Bring up driver communication with the device | |
409 | * | |
410 | * @i2400m: device descriptor | |
411 | * @flags: boot mode flags | |
412 | * | |
413 | * Returns: 0 if ok, < 0 errno code on error. | |
414 | * | |
415 | * Uploads firmware and brings up all the resources needed to be able | |
416 | * to communicate with the device. | |
417 | * | |
e9a6b45b IPG |
418 | * The workqueue has to be setup early, at least before RX handling |
419 | * (it's only real user for now) so it can process reports as they | |
420 | * arrive. We also want to destroy it if we retry, to make sure it is | |
421 | * flushed...easier like this. | |
422 | * | |
024f7f31 IPG |
423 | * TX needs to be setup before the bus-specific code (otherwise on |
424 | * shutdown, the bus-tx code could try to access it). | |
425 | */ | |
426 | static | |
427 | int __i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri flags) | |
428 | { | |
429 | int result; | |
430 | struct wimax_dev *wimax_dev = &i2400m->wimax_dev; | |
431 | struct net_device *net_dev = wimax_dev->net_dev; | |
432 | struct device *dev = i2400m_dev(i2400m); | |
ecddfd5e | 433 | int times = i2400m->bus_bm_retries; |
024f7f31 IPG |
434 | |
435 | d_fnstart(3, dev, "(i2400m %p)\n", i2400m); | |
436 | retry: | |
437 | result = i2400m_dev_bootstrap(i2400m, flags); | |
438 | if (result < 0) { | |
439 | dev_err(dev, "cannot bootstrap device: %d\n", result); | |
440 | goto error_bootstrap; | |
441 | } | |
442 | result = i2400m_tx_setup(i2400m); | |
443 | if (result < 0) | |
444 | goto error_tx_setup; | |
c747583d IPG |
445 | result = i2400m_rx_setup(i2400m); |
446 | if (result < 0) | |
447 | goto error_rx_setup; | |
024f7f31 IPG |
448 | i2400m->work_queue = create_singlethread_workqueue(wimax_dev->name); |
449 | if (i2400m->work_queue == NULL) { | |
450 | result = -ENOMEM; | |
451 | dev_err(dev, "cannot create workqueue\n"); | |
452 | goto error_create_workqueue; | |
453 | } | |
e9a6b45b IPG |
454 | result = i2400m->bus_dev_start(i2400m); |
455 | if (result < 0) | |
456 | goto error_bus_dev_start; | |
6a0f7ab8 IPG |
457 | result = i2400m_firmware_check(i2400m); /* fw versions ok? */ |
458 | if (result < 0) | |
459 | goto error_fw_check; | |
024f7f31 IPG |
460 | /* At this point is ok to send commands to the device */ |
461 | result = i2400m_check_mac_addr(i2400m); | |
462 | if (result < 0) | |
463 | goto error_check_mac_addr; | |
464 | i2400m->ready = 1; | |
465 | wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED); | |
466 | result = i2400m_dev_initialize(i2400m); | |
467 | if (result < 0) | |
468 | goto error_dev_initialize; | |
469 | /* At this point, reports will come for the device and set it | |
470 | * to the right state if it is different than UNINITIALIZED */ | |
471 | d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n", | |
472 | net_dev, i2400m, result); | |
473 | return result; | |
474 | ||
475 | error_dev_initialize: | |
476 | error_check_mac_addr: | |
6a0f7ab8 | 477 | error_fw_check: |
024f7f31 IPG |
478 | i2400m->bus_dev_stop(i2400m); |
479 | error_bus_dev_start: | |
e9a6b45b IPG |
480 | destroy_workqueue(i2400m->work_queue); |
481 | error_create_workqueue: | |
c747583d IPG |
482 | i2400m_rx_release(i2400m); |
483 | error_rx_setup: | |
024f7f31 IPG |
484 | i2400m_tx_release(i2400m); |
485 | error_tx_setup: | |
486 | error_bootstrap: | |
0bcfc5ef | 487 | if (result == -EL3RST && times-- > 0) { |
8b5b30ee | 488 | flags = I2400M_BRI_SOFT|I2400M_BRI_MAC_REINIT; |
024f7f31 IPG |
489 | goto retry; |
490 | } | |
491 | d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n", | |
492 | net_dev, i2400m, result); | |
493 | return result; | |
494 | } | |
495 | ||
496 | ||
497 | static | |
498 | int i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri bm_flags) | |
499 | { | |
500 | int result; | |
501 | mutex_lock(&i2400m->init_mutex); /* Well, start the device */ | |
502 | result = __i2400m_dev_start(i2400m, bm_flags); | |
503 | if (result >= 0) | |
504 | i2400m->updown = 1; | |
505 | mutex_unlock(&i2400m->init_mutex); | |
506 | return result; | |
507 | } | |
508 | ||
509 | ||
510 | /** | |
511 | * i2400m_dev_stop - Tear down driver communication with the device | |
512 | * | |
513 | * @i2400m: device descriptor | |
514 | * | |
515 | * Returns: 0 if ok, < 0 errno code on error. | |
516 | * | |
e9a6b45b IPG |
517 | * Releases all the resources allocated to communicate with the |
518 | * device. Note we cannot destroy the workqueue earlier as until RX is | |
519 | * fully destroyed, it could still try to schedule jobs. | |
024f7f31 IPG |
520 | */ |
521 | static | |
522 | void __i2400m_dev_stop(struct i2400m *i2400m) | |
523 | { | |
524 | struct wimax_dev *wimax_dev = &i2400m->wimax_dev; | |
525 | struct device *dev = i2400m_dev(i2400m); | |
526 | ||
527 | d_fnstart(3, dev, "(i2400m %p)\n", i2400m); | |
528 | wimax_state_change(wimax_dev, __WIMAX_ST_QUIESCING); | |
529 | i2400m_dev_shutdown(i2400m); | |
530 | i2400m->ready = 0; | |
024f7f31 | 531 | i2400m->bus_dev_stop(i2400m); |
e9a6b45b | 532 | destroy_workqueue(i2400m->work_queue); |
c747583d | 533 | i2400m_rx_release(i2400m); |
024f7f31 IPG |
534 | i2400m_tx_release(i2400m); |
535 | wimax_state_change(wimax_dev, WIMAX_ST_DOWN); | |
536 | d_fnend(3, dev, "(i2400m %p) = 0\n", i2400m); | |
537 | } | |
538 | ||
539 | ||
540 | /* | |
541 | * Watch out -- we only need to stop if there is a need for it. The | |
542 | * device could have reset itself and failed to come up again (see | |
543 | * _i2400m_dev_reset_handle()). | |
544 | */ | |
545 | static | |
546 | void i2400m_dev_stop(struct i2400m *i2400m) | |
547 | { | |
548 | mutex_lock(&i2400m->init_mutex); | |
549 | if (i2400m->updown) { | |
550 | __i2400m_dev_stop(i2400m); | |
551 | i2400m->updown = 0; | |
552 | } | |
553 | mutex_unlock(&i2400m->init_mutex); | |
554 | } | |
555 | ||
556 | ||
557 | /* | |
558 | * The device has rebooted; fix up the device and the driver | |
559 | * | |
560 | * Tear down the driver communication with the device, reload the | |
561 | * firmware and reinitialize the communication with the device. | |
562 | * | |
563 | * If someone calls a reset when the device's firmware is down, in | |
564 | * theory we won't see it because we are not listening. However, just | |
565 | * in case, leave the code to handle it. | |
566 | * | |
567 | * If there is a reset context, use it; this means someone is waiting | |
568 | * for us to tell him when the reset operation is complete and the | |
569 | * device is ready to rock again. | |
570 | * | |
571 | * NOTE: if we are in the process of bringing up or down the | |
572 | * communication with the device [running i2400m_dev_start() or | |
573 | * _stop()], don't do anything, let it fail and handle it. | |
574 | * | |
575 | * This function is ran always in a thread context | |
3ef6129e IPG |
576 | * |
577 | * This function gets passed, as payload to i2400m_work() a 'const | |
578 | * char *' ptr with a "reason" why the reset happened (for messages). | |
024f7f31 IPG |
579 | */ |
580 | static | |
581 | void __i2400m_dev_reset_handle(struct work_struct *ws) | |
582 | { | |
583 | int result; | |
584 | struct i2400m_work *iw = container_of(ws, struct i2400m_work, ws); | |
3ef6129e | 585 | const char *reason; |
024f7f31 IPG |
586 | struct i2400m *i2400m = iw->i2400m; |
587 | struct device *dev = i2400m_dev(i2400m); | |
588 | enum wimax_st wimax_state; | |
589 | struct i2400m_reset_ctx *ctx = i2400m->reset_ctx; | |
590 | ||
3ef6129e IPG |
591 | if (WARN_ON(iw->pl_size != sizeof(reason))) |
592 | reason = "SW BUG: reason n/a"; | |
593 | else | |
594 | memcpy(&reason, iw->pl, sizeof(reason)); | |
595 | ||
596 | d_fnstart(3, dev, "(ws %p i2400m %p reason %s)\n", ws, i2400m, reason); | |
597 | ||
024f7f31 IPG |
598 | result = 0; |
599 | if (mutex_trylock(&i2400m->init_mutex) == 0) { | |
600 | /* We are still in i2400m_dev_start() [let it fail] or | |
601 | * i2400m_dev_stop() [we are shutting down anyway, so | |
602 | * ignore it] or we are resetting somewhere else. */ | |
603 | dev_err(dev, "device rebooted\n"); | |
0bcfc5ef | 604 | i2400m_msg_to_dev_cancel_wait(i2400m, -EL3RST); |
024f7f31 IPG |
605 | complete(&i2400m->msg_completion); |
606 | goto out; | |
607 | } | |
608 | wimax_state = wimax_state_get(&i2400m->wimax_dev); | |
609 | if (wimax_state < WIMAX_ST_UNINITIALIZED) { | |
3ef6129e | 610 | dev_info(dev, "%s: it is down, ignoring\n", reason); |
024f7f31 IPG |
611 | goto out_unlock; /* ifconfig up/down wasn't called */ |
612 | } | |
3ef6129e | 613 | dev_err(dev, "%s: reinitializing driver\n", reason); |
024f7f31 IPG |
614 | __i2400m_dev_stop(i2400m); |
615 | i2400m->updown = 0; | |
616 | result = __i2400m_dev_start(i2400m, | |
617 | I2400M_BRI_SOFT | I2400M_BRI_MAC_REINIT); | |
618 | if (result < 0) { | |
3ef6129e IPG |
619 | dev_err(dev, "%s: cannot start the device: %d\n", |
620 | reason, result); | |
024f7f31 IPG |
621 | result = i2400m->bus_reset(i2400m, I2400M_RT_BUS); |
622 | if (result >= 0) | |
623 | result = -ENODEV; | |
624 | } else | |
625 | i2400m->updown = 1; | |
626 | out_unlock: | |
627 | if (i2400m->reset_ctx) { | |
628 | ctx->result = result; | |
629 | complete(&ctx->completion); | |
630 | } | |
631 | mutex_unlock(&i2400m->init_mutex); | |
632 | out: | |
633 | i2400m_put(i2400m); | |
634 | kfree(iw); | |
3ef6129e IPG |
635 | d_fnend(3, dev, "(ws %p i2400m %p reason %s) = void\n", |
636 | ws, i2400m, reason); | |
024f7f31 IPG |
637 | return; |
638 | } | |
639 | ||
640 | ||
641 | /** | |
642 | * i2400m_dev_reset_handle - Handle a device's reset in a thread context | |
643 | * | |
644 | * Schedule a device reset handling out on a thread context, so it | |
645 | * is safe to call from atomic context. We can't use the i2400m's | |
646 | * queue as we are going to destroy it and reinitialize it as part of | |
647 | * the driver bringup/bringup process. | |
648 | * | |
649 | * See __i2400m_dev_reset_handle() for details; that takes care of | |
650 | * reinitializing the driver to handle the reset, calling into the | |
651 | * bus-specific functions ops as needed. | |
652 | */ | |
3ef6129e | 653 | int i2400m_dev_reset_handle(struct i2400m *i2400m, const char *reason) |
024f7f31 | 654 | { |
b4013f91 IPG |
655 | i2400m->boot_mode = 1; |
656 | wmb(); /* Make sure i2400m_msg_to_dev() sees boot_mode */ | |
024f7f31 | 657 | return i2400m_schedule_work(i2400m, __i2400m_dev_reset_handle, |
3ef6129e | 658 | GFP_ATOMIC, &reason, sizeof(reason)); |
024f7f31 IPG |
659 | } |
660 | EXPORT_SYMBOL_GPL(i2400m_dev_reset_handle); | |
661 | ||
662 | ||
a134fd6b DB |
663 | /** |
664 | * i2400m_bm_buf_alloc - Alloc the command and ack buffers for boot mode | |
665 | * | |
666 | * Get the buffers needed to deal with boot mode messages. These | |
667 | * buffers need to be allocated before the sdio recieve irq is setup. | |
668 | */ | |
669 | int i2400m_bm_buf_alloc(struct i2400m *i2400m) | |
670 | { | |
671 | int result; | |
672 | ||
673 | result = -ENOMEM; | |
674 | i2400m->bm_cmd_buf = kzalloc(I2400M_BM_CMD_BUF_SIZE, GFP_KERNEL); | |
675 | if (i2400m->bm_cmd_buf == NULL) | |
676 | goto error_bm_cmd_kzalloc; | |
677 | i2400m->bm_ack_buf = kzalloc(I2400M_BM_ACK_BUF_SIZE, GFP_KERNEL); | |
678 | if (i2400m->bm_ack_buf == NULL) | |
679 | goto error_bm_ack_buf_kzalloc; | |
680 | return 0; | |
681 | ||
682 | error_bm_ack_buf_kzalloc: | |
683 | kfree(i2400m->bm_cmd_buf); | |
684 | error_bm_cmd_kzalloc: | |
685 | return result; | |
686 | } | |
687 | EXPORT_SYMBOL_GPL(i2400m_bm_buf_alloc); | |
688 | ||
689 | /** | |
690 | * i2400m_bm_buf_free - Free boot mode command and ack buffers. | |
691 | * | |
692 | * Free the command and ack buffers | |
693 | * | |
694 | */ | |
695 | void i2400m_bm_buf_free(struct i2400m *i2400m) | |
696 | { | |
697 | kfree(i2400m->bm_ack_buf); | |
698 | kfree(i2400m->bm_cmd_buf); | |
699 | return; | |
700 | } | |
701 | EXPORT_SYMBOL_GPL(i2400m_bm_buf_free | |
702 | ); | |
024f7f31 IPG |
703 | /** |
704 | * i2400m_setup - bus-generic setup function for the i2400m device | |
705 | * | |
706 | * @i2400m: device descriptor (bus-specific parts have been initialized) | |
707 | * | |
708 | * Returns: 0 if ok, < 0 errno code on error. | |
709 | * | |
710 | * Initializes the bus-generic parts of the i2400m driver; the | |
711 | * bus-specific parts have been initialized, function pointers filled | |
712 | * out by the bus-specific probe function. | |
713 | * | |
714 | * As well, this registers the WiMAX and net device nodes. Once this | |
715 | * function returns, the device is operative and has to be ready to | |
716 | * receive and send network traffic and WiMAX control operations. | |
717 | */ | |
718 | int i2400m_setup(struct i2400m *i2400m, enum i2400m_bri bm_flags) | |
719 | { | |
720 | int result = -ENODEV; | |
721 | struct device *dev = i2400m_dev(i2400m); | |
722 | struct wimax_dev *wimax_dev = &i2400m->wimax_dev; | |
723 | struct net_device *net_dev = i2400m->wimax_dev.net_dev; | |
724 | ||
725 | d_fnstart(3, dev, "(i2400m %p)\n", i2400m); | |
726 | ||
727 | snprintf(wimax_dev->name, sizeof(wimax_dev->name), | |
347707ba | 728 | "i2400m-%s:%s", dev->bus->name, dev_name(dev)); |
024f7f31 | 729 | |
024f7f31 IPG |
730 | result = i2400m_bootrom_init(i2400m, bm_flags); |
731 | if (result < 0) { | |
732 | dev_err(dev, "read mac addr: bootrom init " | |
733 | "failed: %d\n", result); | |
734 | goto error_bootrom_init; | |
735 | } | |
736 | result = i2400m_read_mac_addr(i2400m); | |
737 | if (result < 0) | |
738 | goto error_read_mac_addr; | |
fe442683 | 739 | random_ether_addr(i2400m->src_mac_addr); |
024f7f31 IPG |
740 | |
741 | result = register_netdev(net_dev); /* Okey dokey, bring it up */ | |
742 | if (result < 0) { | |
743 | dev_err(dev, "cannot register i2400m network device: %d\n", | |
744 | result); | |
745 | goto error_register_netdev; | |
746 | } | |
747 | netif_carrier_off(net_dev); | |
748 | ||
749 | result = i2400m_dev_start(i2400m, bm_flags); | |
750 | if (result < 0) | |
751 | goto error_dev_start; | |
752 | ||
753 | i2400m->wimax_dev.op_msg_from_user = i2400m_op_msg_from_user; | |
754 | i2400m->wimax_dev.op_rfkill_sw_toggle = i2400m_op_rfkill_sw_toggle; | |
755 | i2400m->wimax_dev.op_reset = i2400m_op_reset; | |
756 | result = wimax_dev_add(&i2400m->wimax_dev, net_dev); | |
757 | if (result < 0) | |
758 | goto error_wimax_dev_add; | |
759 | /* User space needs to do some init stuff */ | |
760 | wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED); | |
761 | ||
762 | /* Now setup all that requires a registered net and wimax device. */ | |
8987691a IPG |
763 | result = sysfs_create_group(&net_dev->dev.kobj, &i2400m_dev_attr_group); |
764 | if (result < 0) { | |
765 | dev_err(dev, "cannot setup i2400m's sysfs: %d\n", result); | |
766 | goto error_sysfs_setup; | |
767 | } | |
024f7f31 IPG |
768 | result = i2400m_debugfs_add(i2400m); |
769 | if (result < 0) { | |
770 | dev_err(dev, "cannot setup i2400m's debugfs: %d\n", result); | |
771 | goto error_debugfs_setup; | |
772 | } | |
773 | d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result); | |
774 | return result; | |
775 | ||
776 | error_debugfs_setup: | |
8987691a IPG |
777 | sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj, |
778 | &i2400m_dev_attr_group); | |
779 | error_sysfs_setup: | |
024f7f31 IPG |
780 | wimax_dev_rm(&i2400m->wimax_dev); |
781 | error_wimax_dev_add: | |
782 | i2400m_dev_stop(i2400m); | |
783 | error_dev_start: | |
784 | unregister_netdev(net_dev); | |
785 | error_register_netdev: | |
786 | error_read_mac_addr: | |
787 | error_bootrom_init: | |
024f7f31 IPG |
788 | d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result); |
789 | return result; | |
790 | } | |
791 | EXPORT_SYMBOL_GPL(i2400m_setup); | |
792 | ||
793 | ||
794 | /** | |
795 | * i2400m_release - release the bus-generic driver resources | |
796 | * | |
797 | * Sends a disconnect message and undoes any setup done by i2400m_setup() | |
798 | */ | |
799 | void i2400m_release(struct i2400m *i2400m) | |
800 | { | |
801 | struct device *dev = i2400m_dev(i2400m); | |
802 | ||
803 | d_fnstart(3, dev, "(i2400m %p)\n", i2400m); | |
804 | netif_stop_queue(i2400m->wimax_dev.net_dev); | |
805 | ||
806 | i2400m_debugfs_rm(i2400m); | |
8987691a IPG |
807 | sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj, |
808 | &i2400m_dev_attr_group); | |
024f7f31 IPG |
809 | wimax_dev_rm(&i2400m->wimax_dev); |
810 | i2400m_dev_stop(i2400m); | |
811 | unregister_netdev(i2400m->wimax_dev.net_dev); | |
812 | kfree(i2400m->bm_ack_buf); | |
813 | kfree(i2400m->bm_cmd_buf); | |
814 | d_fnend(3, dev, "(i2400m %p) = void\n", i2400m); | |
815 | } | |
816 | EXPORT_SYMBOL_GPL(i2400m_release); | |
817 | ||
818 | ||
1af7ad51 IPG |
819 | /* |
820 | * Debug levels control; see debug.h | |
821 | */ | |
822 | struct d_level D_LEVEL[] = { | |
823 | D_SUBMODULE_DEFINE(control), | |
824 | D_SUBMODULE_DEFINE(driver), | |
825 | D_SUBMODULE_DEFINE(debugfs), | |
826 | D_SUBMODULE_DEFINE(fw), | |
827 | D_SUBMODULE_DEFINE(netdev), | |
828 | D_SUBMODULE_DEFINE(rfkill), | |
829 | D_SUBMODULE_DEFINE(rx), | |
4dc1bf07 | 830 | D_SUBMODULE_DEFINE(sysfs), |
1af7ad51 IPG |
831 | D_SUBMODULE_DEFINE(tx), |
832 | }; | |
833 | size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL); | |
834 | ||
835 | ||
024f7f31 IPG |
836 | static |
837 | int __init i2400m_driver_init(void) | |
838 | { | |
4c2b1a11 IPG |
839 | d_parse_params(D_LEVEL, D_LEVEL_SIZE, i2400m_debug_params, |
840 | "i2400m.debug"); | |
aba3792a | 841 | return i2400m_barker_db_init(i2400m_barkers_params); |
024f7f31 IPG |
842 | } |
843 | module_init(i2400m_driver_init); | |
844 | ||
845 | static | |
846 | void __exit i2400m_driver_exit(void) | |
847 | { | |
848 | /* for scheds i2400m_dev_reset_handle() */ | |
849 | flush_scheduled_work(); | |
aba3792a | 850 | i2400m_barker_db_exit(); |
024f7f31 IPG |
851 | return; |
852 | } | |
853 | module_exit(i2400m_driver_exit); | |
854 | ||
855 | MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>"); | |
856 | MODULE_DESCRIPTION("Intel 2400M WiMAX networking bus-generic driver"); | |
857 | MODULE_LICENSE("GPL"); |