Merge branch 'for-linus' of git://git.kernel.dk/linux-2.6-block
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / net / wimax / i2400m / driver.c
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->bus_setup()
45 * i2400m_bootrom_init()
46 * register_netdev()
47 * wimax_dev_add()
48 * i2400m_dev_start()
49 * __i2400m_dev_start()
50 * i2400m_dev_bootstrap()
51 * i2400m_tx_setup()
52 * i2400m->bus_dev_start()
53 * i2400m_firmware_check()
54 * i2400m_check_mac_addr()
55 *
56 * i2400m_release()
57 * i2400m_dev_stop()
58 * __i2400m_dev_stop()
59 * i2400m_dev_shutdown()
60 * i2400m->bus_dev_stop()
61 * i2400m_tx_release()
62 * i2400m->bus_release()
63 * wimax_dev_rm()
64 * unregister_netdev()
65 */
66 #include "i2400m.h"
67 #include <linux/etherdevice.h>
68 #include <linux/wimax/i2400m.h>
69 #include <linux/module.h>
70 #include <linux/moduleparam.h>
71 #include <linux/suspend.h>
72
73 #define D_SUBMODULE driver
74 #include "debug-levels.h"
75
76
77 int i2400m_idle_mode_disabled; /* 0 (idle mode enabled) by default */
78 module_param_named(idle_mode_disabled, i2400m_idle_mode_disabled, int, 0644);
79 MODULE_PARM_DESC(idle_mode_disabled,
80 "If true, the device will not enable idle mode negotiation "
81 "with the base station (when connected) to save power.");
82
83 int i2400m_rx_reorder_disabled; /* 0 (rx reorder enabled) by default */
84 module_param_named(rx_reorder_disabled, i2400m_rx_reorder_disabled, int, 0644);
85 MODULE_PARM_DESC(rx_reorder_disabled,
86 "If true, RX reordering will be disabled.");
87
88 int i2400m_power_save_disabled; /* 0 (power saving enabled) by default */
89 module_param_named(power_save_disabled, i2400m_power_save_disabled, int, 0644);
90 MODULE_PARM_DESC(power_save_disabled,
91 "If true, the driver will not tell the device to enter "
92 "power saving mode when it reports it is ready for it. "
93 "False by default (so the device is told to do power "
94 "saving).");
95
96 static char i2400m_debug_params[128];
97 module_param_string(debug, i2400m_debug_params, sizeof(i2400m_debug_params),
98 0644);
99 MODULE_PARM_DESC(debug,
100 "String of space-separated NAME:VALUE pairs, where NAMEs "
101 "are the different debug submodules and VALUE are the "
102 "initial debug value to set.");
103
104 static char i2400m_barkers_params[128];
105 module_param_string(barkers, i2400m_barkers_params,
106 sizeof(i2400m_barkers_params), 0644);
107 MODULE_PARM_DESC(barkers,
108 "String of comma-separated 32-bit values; each is "
109 "recognized as the value the device sends as a reboot "
110 "signal; values are appended to a list--setting one value "
111 "as zero cleans the existing list and starts a new one.");
112
113 static
114 struct i2400m_work *__i2400m_work_setup(
115 struct i2400m *i2400m, void (*fn)(struct work_struct *),
116 gfp_t gfp_flags, const void *pl, size_t pl_size)
117 {
118 struct i2400m_work *iw;
119
120 iw = kzalloc(sizeof(*iw) + pl_size, gfp_flags);
121 if (iw == NULL)
122 return NULL;
123 iw->i2400m = i2400m_get(i2400m);
124 iw->pl_size = pl_size;
125 memcpy(iw->pl, pl, pl_size);
126 INIT_WORK(&iw->ws, fn);
127 return iw;
128 }
129
130
131 /*
132 * Schedule i2400m's specific work on the system's queue.
133 *
134 * Used for a few cases where we really need it; otherwise, identical
135 * to i2400m_queue_work().
136 *
137 * Returns < 0 errno code on error, 1 if ok.
138 *
139 * If it returns zero, something really bad happened, as it means the
140 * works struct was already queued, but we have just allocated it, so
141 * it should not happen.
142 */
143 int i2400m_schedule_work(struct i2400m *i2400m,
144 void (*fn)(struct work_struct *), gfp_t gfp_flags,
145 const void *pl, size_t pl_size)
146 {
147 int result;
148 struct i2400m_work *iw;
149
150 result = -ENOMEM;
151 iw = __i2400m_work_setup(i2400m, fn, gfp_flags, pl, pl_size);
152 if (iw != NULL) {
153 result = schedule_work(&iw->ws);
154 if (WARN_ON(result == 0))
155 result = -ENXIO;
156 }
157 return result;
158 }
159
160
161 /*
162 * WiMAX stack operation: relay a message from user space
163 *
164 * @wimax_dev: device descriptor
165 * @pipe_name: named pipe the message is for
166 * @msg_buf: pointer to the message bytes
167 * @msg_len: length of the buffer
168 * @genl_info: passed by the generic netlink layer
169 *
170 * The WiMAX stack will call this function when a message was received
171 * from user space.
172 *
173 * For the i2400m, this is an L3L4 message, as specified in
174 * include/linux/wimax/i2400m.h, and thus prefixed with a 'struct
175 * i2400m_l3l4_hdr'. Driver (and device) expect the messages to be
176 * coded in Little Endian.
177 *
178 * This function just verifies that the header declaration and the
179 * payload are consistent and then deals with it, either forwarding it
180 * to the device or procesing it locally.
181 *
182 * In the i2400m, messages are basically commands that will carry an
183 * ack, so we use i2400m_msg_to_dev() and then deliver the ack back to
184 * user space. The rx.c code might intercept the response and use it
185 * to update the driver's state, but then it will pass it on so it can
186 * be relayed back to user space.
187 *
188 * Note that asynchronous events from the device are processed and
189 * sent to user space in rx.c.
190 */
191 static
192 int i2400m_op_msg_from_user(struct wimax_dev *wimax_dev,
193 const char *pipe_name,
194 const void *msg_buf, size_t msg_len,
195 const struct genl_info *genl_info)
196 {
197 int result;
198 struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
199 struct device *dev = i2400m_dev(i2400m);
200 struct sk_buff *ack_skb;
201
202 d_fnstart(4, dev, "(wimax_dev %p [i2400m %p] msg_buf %p "
203 "msg_len %zu genl_info %p)\n", wimax_dev, i2400m,
204 msg_buf, msg_len, genl_info);
205 ack_skb = i2400m_msg_to_dev(i2400m, msg_buf, msg_len);
206 result = PTR_ERR(ack_skb);
207 if (IS_ERR(ack_skb))
208 goto error_msg_to_dev;
209 result = wimax_msg_send(&i2400m->wimax_dev, ack_skb);
210 error_msg_to_dev:
211 d_fnend(4, dev, "(wimax_dev %p [i2400m %p] msg_buf %p msg_len %zu "
212 "genl_info %p) = %d\n", wimax_dev, i2400m, msg_buf, msg_len,
213 genl_info, result);
214 return result;
215 }
216
217
218 /*
219 * Context to wait for a reset to finalize
220 */
221 struct i2400m_reset_ctx {
222 struct completion completion;
223 int result;
224 };
225
226
227 /*
228 * WiMAX stack operation: reset a device
229 *
230 * @wimax_dev: device descriptor
231 *
232 * See the documentation for wimax_reset() and wimax_dev->op_reset for
233 * the requirements of this function. The WiMAX stack guarantees
234 * serialization on calls to this function.
235 *
236 * Do a warm reset on the device; if it fails, resort to a cold reset
237 * and return -ENODEV. On successful warm reset, we need to block
238 * until it is complete.
239 *
240 * The bus-driver implementation of reset takes care of falling back
241 * to cold reset if warm fails.
242 */
243 static
244 int i2400m_op_reset(struct wimax_dev *wimax_dev)
245 {
246 int result;
247 struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
248 struct device *dev = i2400m_dev(i2400m);
249 struct i2400m_reset_ctx ctx = {
250 .completion = COMPLETION_INITIALIZER_ONSTACK(ctx.completion),
251 .result = 0,
252 };
253
254 d_fnstart(4, dev, "(wimax_dev %p)\n", wimax_dev);
255 mutex_lock(&i2400m->init_mutex);
256 i2400m->reset_ctx = &ctx;
257 mutex_unlock(&i2400m->init_mutex);
258 result = i2400m_reset(i2400m, I2400M_RT_WARM);
259 if (result < 0)
260 goto out;
261 result = wait_for_completion_timeout(&ctx.completion, 4*HZ);
262 if (result == 0)
263 result = -ETIMEDOUT;
264 else if (result > 0)
265 result = ctx.result;
266 /* if result < 0, pass it on */
267 mutex_lock(&i2400m->init_mutex);
268 i2400m->reset_ctx = NULL;
269 mutex_unlock(&i2400m->init_mutex);
270 out:
271 d_fnend(4, dev, "(wimax_dev %p) = %d\n", wimax_dev, result);
272 return result;
273 }
274
275
276 /*
277 * Check the MAC address we got from boot mode is ok
278 *
279 * @i2400m: device descriptor
280 *
281 * Returns: 0 if ok, < 0 errno code on error.
282 */
283 static
284 int i2400m_check_mac_addr(struct i2400m *i2400m)
285 {
286 int result;
287 struct device *dev = i2400m_dev(i2400m);
288 struct sk_buff *skb;
289 const struct i2400m_tlv_detailed_device_info *ddi;
290 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
291 const unsigned char zeromac[ETH_ALEN] = { 0 };
292
293 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
294 skb = i2400m_get_device_info(i2400m);
295 if (IS_ERR(skb)) {
296 result = PTR_ERR(skb);
297 dev_err(dev, "Cannot verify MAC address, error reading: %d\n",
298 result);
299 goto error;
300 }
301 /* Extract MAC addresss */
302 ddi = (void *) skb->data;
303 BUILD_BUG_ON(ETH_ALEN != sizeof(ddi->mac_address));
304 d_printf(2, dev, "GET DEVICE INFO: mac addr %pM\n",
305 ddi->mac_address);
306 if (!memcmp(net_dev->perm_addr, ddi->mac_address,
307 sizeof(ddi->mac_address)))
308 goto ok;
309 dev_warn(dev, "warning: device reports a different MAC address "
310 "to that of boot mode's\n");
311 dev_warn(dev, "device reports %pM\n", ddi->mac_address);
312 dev_warn(dev, "boot mode reported %pM\n", net_dev->perm_addr);
313 if (!memcmp(zeromac, ddi->mac_address, sizeof(zeromac)))
314 dev_err(dev, "device reports an invalid MAC address, "
315 "not updating\n");
316 else {
317 dev_warn(dev, "updating MAC address\n");
318 net_dev->addr_len = ETH_ALEN;
319 memcpy(net_dev->perm_addr, ddi->mac_address, ETH_ALEN);
320 memcpy(net_dev->dev_addr, ddi->mac_address, ETH_ALEN);
321 }
322 ok:
323 result = 0;
324 kfree_skb(skb);
325 error:
326 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
327 return result;
328 }
329
330
331 /**
332 * __i2400m_dev_start - Bring up driver communication with the device
333 *
334 * @i2400m: device descriptor
335 * @flags: boot mode flags
336 *
337 * Returns: 0 if ok, < 0 errno code on error.
338 *
339 * Uploads firmware and brings up all the resources needed to be able
340 * to communicate with the device.
341 *
342 * The workqueue has to be setup early, at least before RX handling
343 * (it's only real user for now) so it can process reports as they
344 * arrive. We also want to destroy it if we retry, to make sure it is
345 * flushed...easier like this.
346 *
347 * TX needs to be setup before the bus-specific code (otherwise on
348 * shutdown, the bus-tx code could try to access it).
349 */
350 static
351 int __i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri flags)
352 {
353 int result;
354 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
355 struct net_device *net_dev = wimax_dev->net_dev;
356 struct device *dev = i2400m_dev(i2400m);
357 int times = i2400m->bus_bm_retries;
358
359 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
360 retry:
361 result = i2400m_dev_bootstrap(i2400m, flags);
362 if (result < 0) {
363 dev_err(dev, "cannot bootstrap device: %d\n", result);
364 goto error_bootstrap;
365 }
366 result = i2400m_tx_setup(i2400m);
367 if (result < 0)
368 goto error_tx_setup;
369 result = i2400m_rx_setup(i2400m);
370 if (result < 0)
371 goto error_rx_setup;
372 i2400m->work_queue = create_singlethread_workqueue(wimax_dev->name);
373 if (i2400m->work_queue == NULL) {
374 result = -ENOMEM;
375 dev_err(dev, "cannot create workqueue\n");
376 goto error_create_workqueue;
377 }
378 if (i2400m->bus_dev_start) {
379 result = i2400m->bus_dev_start(i2400m);
380 if (result < 0)
381 goto error_bus_dev_start;
382 }
383 i2400m->ready = 1;
384 wmb(); /* see i2400m->ready's documentation */
385 /* process pending reports from the device */
386 queue_work(i2400m->work_queue, &i2400m->rx_report_ws);
387 result = i2400m_firmware_check(i2400m); /* fw versions ok? */
388 if (result < 0)
389 goto error_fw_check;
390 /* At this point is ok to send commands to the device */
391 result = i2400m_check_mac_addr(i2400m);
392 if (result < 0)
393 goto error_check_mac_addr;
394 result = i2400m_dev_initialize(i2400m);
395 if (result < 0)
396 goto error_dev_initialize;
397 /* At this point, reports will come for the device and set it
398 * to the right state if it is different than UNINITIALIZED */
399 d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
400 net_dev, i2400m, result);
401 return result;
402
403 error_dev_initialize:
404 error_check_mac_addr:
405 i2400m->ready = 0;
406 wmb(); /* see i2400m->ready's documentation */
407 flush_workqueue(i2400m->work_queue);
408 error_fw_check:
409 if (i2400m->bus_dev_stop)
410 i2400m->bus_dev_stop(i2400m);
411 error_bus_dev_start:
412 destroy_workqueue(i2400m->work_queue);
413 error_create_workqueue:
414 i2400m_rx_release(i2400m);
415 error_rx_setup:
416 i2400m_tx_release(i2400m);
417 error_tx_setup:
418 error_bootstrap:
419 if (result == -EL3RST && times-- > 0) {
420 flags = I2400M_BRI_SOFT|I2400M_BRI_MAC_REINIT;
421 goto retry;
422 }
423 d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
424 net_dev, i2400m, result);
425 return result;
426 }
427
428
429 static
430 int i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri bm_flags)
431 {
432 int result = 0;
433 mutex_lock(&i2400m->init_mutex); /* Well, start the device */
434 if (i2400m->updown == 0) {
435 result = __i2400m_dev_start(i2400m, bm_flags);
436 if (result >= 0) {
437 i2400m->updown = 1;
438 wmb(); /* see i2400m->updown's documentation */
439 }
440 }
441 mutex_unlock(&i2400m->init_mutex);
442 return result;
443 }
444
445
446 /**
447 * i2400m_dev_stop - Tear down driver communication with the device
448 *
449 * @i2400m: device descriptor
450 *
451 * Returns: 0 if ok, < 0 errno code on error.
452 *
453 * Releases all the resources allocated to communicate with the
454 * device. Note we cannot destroy the workqueue earlier as until RX is
455 * fully destroyed, it could still try to schedule jobs.
456 */
457 static
458 void __i2400m_dev_stop(struct i2400m *i2400m)
459 {
460 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
461 struct device *dev = i2400m_dev(i2400m);
462
463 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
464 wimax_state_change(wimax_dev, __WIMAX_ST_QUIESCING);
465 i2400m_msg_to_dev_cancel_wait(i2400m, -EL3RST);
466 complete(&i2400m->msg_completion);
467 i2400m_net_wake_stop(i2400m);
468 i2400m_dev_shutdown(i2400m);
469 /*
470 * Make sure no report hooks are running *before* we stop the
471 * communication infrastructure with the device.
472 */
473 i2400m->ready = 0; /* nobody can queue work anymore */
474 wmb(); /* see i2400m->ready's documentation */
475 flush_workqueue(i2400m->work_queue);
476
477 if (i2400m->bus_dev_stop)
478 i2400m->bus_dev_stop(i2400m);
479 destroy_workqueue(i2400m->work_queue);
480 i2400m_rx_release(i2400m);
481 i2400m_tx_release(i2400m);
482 wimax_state_change(wimax_dev, WIMAX_ST_DOWN);
483 d_fnend(3, dev, "(i2400m %p) = 0\n", i2400m);
484 }
485
486
487 /*
488 * Watch out -- we only need to stop if there is a need for it. The
489 * device could have reset itself and failed to come up again (see
490 * _i2400m_dev_reset_handle()).
491 */
492 static
493 void i2400m_dev_stop(struct i2400m *i2400m)
494 {
495 mutex_lock(&i2400m->init_mutex);
496 if (i2400m->updown) {
497 __i2400m_dev_stop(i2400m);
498 i2400m->updown = 0;
499 wmb(); /* see i2400m->updown's documentation */
500 }
501 mutex_unlock(&i2400m->init_mutex);
502 }
503
504
505 /*
506 * Listen to PM events to cache the firmware before suspend/hibernation
507 *
508 * When the device comes out of suspend, it might go into reset and
509 * firmware has to be uploaded again. At resume, most of the times, we
510 * can't load firmware images from disk, so we need to cache it.
511 *
512 * i2400m_fw_cache() will allocate a kobject and attach the firmware
513 * to it; that way we don't have to worry too much about the fw loader
514 * hitting a race condition.
515 *
516 * Note: modus operandi stolen from the Orinoco driver; thx.
517 */
518 static
519 int i2400m_pm_notifier(struct notifier_block *notifier,
520 unsigned long pm_event,
521 void *unused)
522 {
523 struct i2400m *i2400m =
524 container_of(notifier, struct i2400m, pm_notifier);
525 struct device *dev = i2400m_dev(i2400m);
526
527 d_fnstart(3, dev, "(i2400m %p pm_event %lx)\n", i2400m, pm_event);
528 switch (pm_event) {
529 case PM_HIBERNATION_PREPARE:
530 case PM_SUSPEND_PREPARE:
531 i2400m_fw_cache(i2400m);
532 break;
533 case PM_POST_RESTORE:
534 /* Restore from hibernation failed. We need to clean
535 * up in exactly the same way, so fall through. */
536 case PM_POST_HIBERNATION:
537 case PM_POST_SUSPEND:
538 i2400m_fw_uncache(i2400m);
539 break;
540
541 case PM_RESTORE_PREPARE:
542 default:
543 break;
544 }
545 d_fnend(3, dev, "(i2400m %p pm_event %lx) = void\n", i2400m, pm_event);
546 return NOTIFY_DONE;
547 }
548
549
550 /*
551 * pre-reset is called before a device is going on reset
552 *
553 * This has to be followed by a call to i2400m_post_reset(), otherwise
554 * bad things might happen.
555 */
556 int i2400m_pre_reset(struct i2400m *i2400m)
557 {
558 int result;
559 struct device *dev = i2400m_dev(i2400m);
560
561 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
562 d_printf(1, dev, "pre-reset shut down\n");
563
564 result = 0;
565 mutex_lock(&i2400m->init_mutex);
566 if (i2400m->updown) {
567 netif_tx_disable(i2400m->wimax_dev.net_dev);
568 __i2400m_dev_stop(i2400m);
569 result = 0;
570 /* down't set updown to zero -- this way
571 * post_reset can restore properly */
572 }
573 mutex_unlock(&i2400m->init_mutex);
574 if (i2400m->bus_release)
575 i2400m->bus_release(i2400m);
576 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
577 return result;
578 }
579 EXPORT_SYMBOL_GPL(i2400m_pre_reset);
580
581
582 /*
583 * Restore device state after a reset
584 *
585 * Do the work needed after a device reset to bring it up to the same
586 * state as it was before the reset.
587 *
588 * NOTE: this requires i2400m->init_mutex taken
589 */
590 int i2400m_post_reset(struct i2400m *i2400m)
591 {
592 int result = 0;
593 struct device *dev = i2400m_dev(i2400m);
594
595 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
596 d_printf(1, dev, "post-reset start\n");
597 if (i2400m->bus_setup) {
598 result = i2400m->bus_setup(i2400m);
599 if (result < 0) {
600 dev_err(dev, "bus-specific setup failed: %d\n",
601 result);
602 goto error_bus_setup;
603 }
604 }
605 mutex_lock(&i2400m->init_mutex);
606 if (i2400m->updown) {
607 result = __i2400m_dev_start(
608 i2400m, I2400M_BRI_SOFT | I2400M_BRI_MAC_REINIT);
609 if (result < 0)
610 goto error_dev_start;
611 }
612 mutex_unlock(&i2400m->init_mutex);
613 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
614 return result;
615
616 error_dev_start:
617 if (i2400m->bus_release)
618 i2400m->bus_release(i2400m);
619 error_bus_setup:
620 /* even if the device was up, it could not be recovered, so we
621 * mark it as down. */
622 i2400m->updown = 0;
623 wmb(); /* see i2400m->updown's documentation */
624 mutex_unlock(&i2400m->init_mutex);
625 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
626 return result;
627 }
628 EXPORT_SYMBOL_GPL(i2400m_post_reset);
629
630
631 /*
632 * The device has rebooted; fix up the device and the driver
633 *
634 * Tear down the driver communication with the device, reload the
635 * firmware and reinitialize the communication with the device.
636 *
637 * If someone calls a reset when the device's firmware is down, in
638 * theory we won't see it because we are not listening. However, just
639 * in case, leave the code to handle it.
640 *
641 * If there is a reset context, use it; this means someone is waiting
642 * for us to tell him when the reset operation is complete and the
643 * device is ready to rock again.
644 *
645 * NOTE: if we are in the process of bringing up or down the
646 * communication with the device [running i2400m_dev_start() or
647 * _stop()], don't do anything, let it fail and handle it.
648 *
649 * This function is ran always in a thread context
650 *
651 * This function gets passed, as payload to i2400m_work() a 'const
652 * char *' ptr with a "reason" why the reset happened (for messages).
653 */
654 static
655 void __i2400m_dev_reset_handle(struct work_struct *ws)
656 {
657 int result;
658 struct i2400m_work *iw = container_of(ws, struct i2400m_work, ws);
659 const char *reason;
660 struct i2400m *i2400m = iw->i2400m;
661 struct device *dev = i2400m_dev(i2400m);
662 struct i2400m_reset_ctx *ctx = i2400m->reset_ctx;
663
664 if (WARN_ON(iw->pl_size != sizeof(reason)))
665 reason = "SW BUG: reason n/a";
666 else
667 memcpy(&reason, iw->pl, sizeof(reason));
668
669 d_fnstart(3, dev, "(ws %p i2400m %p reason %s)\n", ws, i2400m, reason);
670
671 result = 0;
672 if (mutex_trylock(&i2400m->init_mutex) == 0) {
673 /* We are still in i2400m_dev_start() [let it fail] or
674 * i2400m_dev_stop() [we are shutting down anyway, so
675 * ignore it] or we are resetting somewhere else. */
676 dev_err(dev, "device rebooted somewhere else?\n");
677 i2400m_msg_to_dev_cancel_wait(i2400m, -EL3RST);
678 complete(&i2400m->msg_completion);
679 goto out;
680 }
681 if (i2400m->updown == 0) {
682 dev_info(dev, "%s: device is down, doing nothing\n", reason);
683 goto out_unlock;
684 }
685 dev_err(dev, "%s: reinitializing driver\n", reason);
686 __i2400m_dev_stop(i2400m);
687 result = __i2400m_dev_start(i2400m,
688 I2400M_BRI_SOFT | I2400M_BRI_MAC_REINIT);
689 if (result < 0) {
690 i2400m->updown = 0;
691 wmb(); /* see i2400m->updown's documentation */
692 dev_err(dev, "%s: cannot start the device: %d\n",
693 reason, result);
694 result = -EUCLEAN;
695 }
696 out_unlock:
697 if (i2400m->reset_ctx) {
698 ctx->result = result;
699 complete(&ctx->completion);
700 }
701 mutex_unlock(&i2400m->init_mutex);
702 if (result == -EUCLEAN) {
703 /* ops, need to clean up [w/ init_mutex not held] */
704 result = i2400m_reset(i2400m, I2400M_RT_BUS);
705 if (result >= 0)
706 result = -ENODEV;
707 }
708 out:
709 i2400m_put(i2400m);
710 kfree(iw);
711 d_fnend(3, dev, "(ws %p i2400m %p reason %s) = void\n",
712 ws, i2400m, reason);
713 return;
714 }
715
716
717 /**
718 * i2400m_dev_reset_handle - Handle a device's reset in a thread context
719 *
720 * Schedule a device reset handling out on a thread context, so it
721 * is safe to call from atomic context. We can't use the i2400m's
722 * queue as we are going to destroy it and reinitialize it as part of
723 * the driver bringup/bringup process.
724 *
725 * See __i2400m_dev_reset_handle() for details; that takes care of
726 * reinitializing the driver to handle the reset, calling into the
727 * bus-specific functions ops as needed.
728 */
729 int i2400m_dev_reset_handle(struct i2400m *i2400m, const char *reason)
730 {
731 i2400m->boot_mode = 1;
732 wmb(); /* Make sure i2400m_msg_to_dev() sees boot_mode */
733 return i2400m_schedule_work(i2400m, __i2400m_dev_reset_handle,
734 GFP_ATOMIC, &reason, sizeof(reason));
735 }
736 EXPORT_SYMBOL_GPL(i2400m_dev_reset_handle);
737
738
739 /*
740 * Alloc the command and ack buffers for boot mode
741 *
742 * Get the buffers needed to deal with boot mode messages. These
743 * buffers need to be allocated before the sdio recieve irq is setup.
744 */
745 static
746 int i2400m_bm_buf_alloc(struct i2400m *i2400m)
747 {
748 int result;
749
750 result = -ENOMEM;
751 i2400m->bm_cmd_buf = kzalloc(I2400M_BM_CMD_BUF_SIZE, GFP_KERNEL);
752 if (i2400m->bm_cmd_buf == NULL)
753 goto error_bm_cmd_kzalloc;
754 i2400m->bm_ack_buf = kzalloc(I2400M_BM_ACK_BUF_SIZE, GFP_KERNEL);
755 if (i2400m->bm_ack_buf == NULL)
756 goto error_bm_ack_buf_kzalloc;
757 return 0;
758
759 error_bm_ack_buf_kzalloc:
760 kfree(i2400m->bm_cmd_buf);
761 error_bm_cmd_kzalloc:
762 return result;
763 }
764
765
766 /*
767 * Free boot mode command and ack buffers.
768 */
769 static
770 void i2400m_bm_buf_free(struct i2400m *i2400m)
771 {
772 kfree(i2400m->bm_ack_buf);
773 kfree(i2400m->bm_cmd_buf);
774 }
775
776
777 /**
778 * i2400m_init - Initialize a 'struct i2400m' from all zeroes
779 *
780 * This is a bus-generic API call.
781 */
782 void i2400m_init(struct i2400m *i2400m)
783 {
784 wimax_dev_init(&i2400m->wimax_dev);
785
786 i2400m->boot_mode = 1;
787 i2400m->rx_reorder = 1;
788 init_waitqueue_head(&i2400m->state_wq);
789
790 spin_lock_init(&i2400m->tx_lock);
791 i2400m->tx_pl_min = UINT_MAX;
792 i2400m->tx_size_min = UINT_MAX;
793
794 spin_lock_init(&i2400m->rx_lock);
795 i2400m->rx_pl_min = UINT_MAX;
796 i2400m->rx_size_min = UINT_MAX;
797 INIT_LIST_HEAD(&i2400m->rx_reports);
798 INIT_WORK(&i2400m->rx_report_ws, i2400m_report_hook_work);
799
800 mutex_init(&i2400m->msg_mutex);
801 init_completion(&i2400m->msg_completion);
802
803 mutex_init(&i2400m->init_mutex);
804 /* wake_tx_ws is initialized in i2400m_tx_setup() */
805 }
806 EXPORT_SYMBOL_GPL(i2400m_init);
807
808
809 int i2400m_reset(struct i2400m *i2400m, enum i2400m_reset_type rt)
810 {
811 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
812
813 /*
814 * Make sure we stop TXs and down the carrier before
815 * resetting; this is needed to avoid things like
816 * i2400m_wake_tx() scheduling stuff in parallel.
817 */
818 if (net_dev->reg_state == NETREG_REGISTERED) {
819 netif_tx_disable(net_dev);
820 netif_carrier_off(net_dev);
821 }
822 return i2400m->bus_reset(i2400m, rt);
823 }
824 EXPORT_SYMBOL_GPL(i2400m_reset);
825
826
827 /**
828 * i2400m_setup - bus-generic setup function for the i2400m device
829 *
830 * @i2400m: device descriptor (bus-specific parts have been initialized)
831 *
832 * Returns: 0 if ok, < 0 errno code on error.
833 *
834 * Sets up basic device comunication infrastructure, boots the ROM to
835 * read the MAC address, registers with the WiMAX and network stacks
836 * and then brings up the device.
837 */
838 int i2400m_setup(struct i2400m *i2400m, enum i2400m_bri bm_flags)
839 {
840 int result = -ENODEV;
841 struct device *dev = i2400m_dev(i2400m);
842 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
843 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
844
845 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
846
847 snprintf(wimax_dev->name, sizeof(wimax_dev->name),
848 "i2400m-%s:%s", dev->bus->name, dev_name(dev));
849
850 result = i2400m_bm_buf_alloc(i2400m);
851 if (result < 0) {
852 dev_err(dev, "cannot allocate bootmode scratch buffers\n");
853 goto error_bm_buf_alloc;
854 }
855
856 if (i2400m->bus_setup) {
857 result = i2400m->bus_setup(i2400m);
858 if (result < 0) {
859 dev_err(dev, "bus-specific setup failed: %d\n",
860 result);
861 goto error_bus_setup;
862 }
863 }
864
865 result = i2400m_bootrom_init(i2400m, bm_flags);
866 if (result < 0) {
867 dev_err(dev, "read mac addr: bootrom init "
868 "failed: %d\n", result);
869 goto error_bootrom_init;
870 }
871 result = i2400m_read_mac_addr(i2400m);
872 if (result < 0)
873 goto error_read_mac_addr;
874 random_ether_addr(i2400m->src_mac_addr);
875
876 i2400m->pm_notifier.notifier_call = i2400m_pm_notifier;
877 register_pm_notifier(&i2400m->pm_notifier);
878
879 result = register_netdev(net_dev); /* Okey dokey, bring it up */
880 if (result < 0) {
881 dev_err(dev, "cannot register i2400m network device: %d\n",
882 result);
883 goto error_register_netdev;
884 }
885 netif_carrier_off(net_dev);
886
887 i2400m->wimax_dev.op_msg_from_user = i2400m_op_msg_from_user;
888 i2400m->wimax_dev.op_rfkill_sw_toggle = i2400m_op_rfkill_sw_toggle;
889 i2400m->wimax_dev.op_reset = i2400m_op_reset;
890
891 result = wimax_dev_add(&i2400m->wimax_dev, net_dev);
892 if (result < 0)
893 goto error_wimax_dev_add;
894
895 /* Now setup all that requires a registered net and wimax device. */
896 result = sysfs_create_group(&net_dev->dev.kobj, &i2400m_dev_attr_group);
897 if (result < 0) {
898 dev_err(dev, "cannot setup i2400m's sysfs: %d\n", result);
899 goto error_sysfs_setup;
900 }
901
902 result = i2400m_debugfs_add(i2400m);
903 if (result < 0) {
904 dev_err(dev, "cannot setup i2400m's debugfs: %d\n", result);
905 goto error_debugfs_setup;
906 }
907
908 result = i2400m_dev_start(i2400m, bm_flags);
909 if (result < 0)
910 goto error_dev_start;
911 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
912 return result;
913
914 error_dev_start:
915 i2400m_debugfs_rm(i2400m);
916 error_debugfs_setup:
917 sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
918 &i2400m_dev_attr_group);
919 error_sysfs_setup:
920 wimax_dev_rm(&i2400m->wimax_dev);
921 error_wimax_dev_add:
922 unregister_netdev(net_dev);
923 error_register_netdev:
924 unregister_pm_notifier(&i2400m->pm_notifier);
925 error_read_mac_addr:
926 error_bootrom_init:
927 if (i2400m->bus_release)
928 i2400m->bus_release(i2400m);
929 error_bus_setup:
930 i2400m_bm_buf_free(i2400m);
931 error_bm_buf_alloc:
932 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
933 return result;
934 }
935 EXPORT_SYMBOL_GPL(i2400m_setup);
936
937
938 /**
939 * i2400m_release - release the bus-generic driver resources
940 *
941 * Sends a disconnect message and undoes any setup done by i2400m_setup()
942 */
943 void i2400m_release(struct i2400m *i2400m)
944 {
945 struct device *dev = i2400m_dev(i2400m);
946
947 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
948 netif_stop_queue(i2400m->wimax_dev.net_dev);
949
950 i2400m_dev_stop(i2400m);
951
952 i2400m_debugfs_rm(i2400m);
953 sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
954 &i2400m_dev_attr_group);
955 wimax_dev_rm(&i2400m->wimax_dev);
956 unregister_netdev(i2400m->wimax_dev.net_dev);
957 unregister_pm_notifier(&i2400m->pm_notifier);
958 if (i2400m->bus_release)
959 i2400m->bus_release(i2400m);
960 i2400m_bm_buf_free(i2400m);
961 d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
962 }
963 EXPORT_SYMBOL_GPL(i2400m_release);
964
965
966 /*
967 * Debug levels control; see debug.h
968 */
969 struct d_level D_LEVEL[] = {
970 D_SUBMODULE_DEFINE(control),
971 D_SUBMODULE_DEFINE(driver),
972 D_SUBMODULE_DEFINE(debugfs),
973 D_SUBMODULE_DEFINE(fw),
974 D_SUBMODULE_DEFINE(netdev),
975 D_SUBMODULE_DEFINE(rfkill),
976 D_SUBMODULE_DEFINE(rx),
977 D_SUBMODULE_DEFINE(sysfs),
978 D_SUBMODULE_DEFINE(tx),
979 };
980 size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
981
982
983 static
984 int __init i2400m_driver_init(void)
985 {
986 d_parse_params(D_LEVEL, D_LEVEL_SIZE, i2400m_debug_params,
987 "i2400m.debug");
988 return i2400m_barker_db_init(i2400m_barkers_params);
989 }
990 module_init(i2400m_driver_init);
991
992 static
993 void __exit i2400m_driver_exit(void)
994 {
995 /* for scheds i2400m_dev_reset_handle() */
996 flush_scheduled_work();
997 i2400m_barker_db_exit();
998 return;
999 }
1000 module_exit(i2400m_driver_exit);
1001
1002 MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
1003 MODULE_DESCRIPTION("Intel 2400M WiMAX networking bus-generic driver");
1004 MODULE_LICENSE("GPL");