2 * Intel Wireless WiMAX Connection 2400m
3 * Generic probe/disconnect, reset and message passing
6 * Copyright (C) 2007-2008 Intel Corporation <linux-wimax@intel.com>
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
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.
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.
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
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()].
31 * i2400m_op_msg_from_user()
33 * wimax_msg_to_user_send()
38 * i2400m_dev_reset_handle()
39 * __i2400m_dev_reset_handle()
41 * __i2400m_dev_start()
45 * i2400m_bootrom_init()
49 * __i2400m_dev_start()
50 * i2400m_dev_bootstrap()
52 * i2400m->bus_dev_start()
53 * i2400m_firmware_check()
54 * i2400m_check_mac_addr()
59 * i2400m_dev_shutdown()
60 * i2400m->bus_dev_stop()
62 * i2400m->bus_release()
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 #include <linux/slab.h>
74 #define D_SUBMODULE driver
75 #include "debug-levels.h"
78 static char i2400m_debug_params
[128];
79 module_param_string(debug
, i2400m_debug_params
, sizeof(i2400m_debug_params
),
81 MODULE_PARM_DESC(debug
,
82 "String of space-separated NAME:VALUE pairs, where NAMEs "
83 "are the different debug submodules and VALUE are the "
84 "initial debug value to set.");
86 static char i2400m_barkers_params
[128];
87 module_param_string(barkers
, i2400m_barkers_params
,
88 sizeof(i2400m_barkers_params
), 0644);
89 MODULE_PARM_DESC(barkers
,
90 "String of comma-separated 32-bit values; each is "
91 "recognized as the value the device sends as a reboot "
92 "signal; values are appended to a list--setting one value "
93 "as zero cleans the existing list and starts a new one.");
96 struct i2400m_work
*__i2400m_work_setup(
97 struct i2400m
*i2400m
, void (*fn
)(struct work_struct
*),
98 gfp_t gfp_flags
, const void *pl
, size_t pl_size
)
100 struct i2400m_work
*iw
;
102 iw
= kzalloc(sizeof(*iw
) + pl_size
, gfp_flags
);
105 iw
->i2400m
= i2400m_get(i2400m
);
106 iw
->pl_size
= pl_size
;
107 memcpy(iw
->pl
, pl
, pl_size
);
108 INIT_WORK(&iw
->ws
, fn
);
114 * Schedule i2400m's specific work on the system's queue.
116 * Used for a few cases where we really need it; otherwise, identical
117 * to i2400m_queue_work().
119 * Returns < 0 errno code on error, 1 if ok.
121 * If it returns zero, something really bad happened, as it means the
122 * works struct was already queued, but we have just allocated it, so
123 * it should not happen.
125 int i2400m_schedule_work(struct i2400m
*i2400m
,
126 void (*fn
)(struct work_struct
*), gfp_t gfp_flags
,
127 const void *pl
, size_t pl_size
)
130 struct i2400m_work
*iw
;
133 iw
= __i2400m_work_setup(i2400m
, fn
, gfp_flags
, pl
, pl_size
);
135 result
= schedule_work(&iw
->ws
);
136 if (WARN_ON(result
== 0))
144 * WiMAX stack operation: relay a message from user space
146 * @wimax_dev: device descriptor
147 * @pipe_name: named pipe the message is for
148 * @msg_buf: pointer to the message bytes
149 * @msg_len: length of the buffer
150 * @genl_info: passed by the generic netlink layer
152 * The WiMAX stack will call this function when a message was received
155 * For the i2400m, this is an L3L4 message, as specified in
156 * include/linux/wimax/i2400m.h, and thus prefixed with a 'struct
157 * i2400m_l3l4_hdr'. Driver (and device) expect the messages to be
158 * coded in Little Endian.
160 * This function just verifies that the header declaration and the
161 * payload are consistent and then deals with it, either forwarding it
162 * to the device or procesing it locally.
164 * In the i2400m, messages are basically commands that will carry an
165 * ack, so we use i2400m_msg_to_dev() and then deliver the ack back to
166 * user space. The rx.c code might intercept the response and use it
167 * to update the driver's state, but then it will pass it on so it can
168 * be relayed back to user space.
170 * Note that asynchronous events from the device are processed and
171 * sent to user space in rx.c.
174 int i2400m_op_msg_from_user(struct wimax_dev
*wimax_dev
,
175 const char *pipe_name
,
176 const void *msg_buf
, size_t msg_len
,
177 const struct genl_info
*genl_info
)
180 struct i2400m
*i2400m
= wimax_dev_to_i2400m(wimax_dev
);
181 struct device
*dev
= i2400m_dev(i2400m
);
182 struct sk_buff
*ack_skb
;
184 d_fnstart(4, dev
, "(wimax_dev %p [i2400m %p] msg_buf %p "
185 "msg_len %zu genl_info %p)\n", wimax_dev
, i2400m
,
186 msg_buf
, msg_len
, genl_info
);
187 ack_skb
= i2400m_msg_to_dev(i2400m
, msg_buf
, msg_len
);
188 result
= PTR_ERR(ack_skb
);
190 goto error_msg_to_dev
;
191 result
= wimax_msg_send(&i2400m
->wimax_dev
, ack_skb
);
193 d_fnend(4, dev
, "(wimax_dev %p [i2400m %p] msg_buf %p msg_len %zu "
194 "genl_info %p) = %d\n", wimax_dev
, i2400m
, msg_buf
, msg_len
,
201 * Context to wait for a reset to finalize
203 struct i2400m_reset_ctx
{
204 struct completion completion
;
210 * WiMAX stack operation: reset a device
212 * @wimax_dev: device descriptor
214 * See the documentation for wimax_reset() and wimax_dev->op_reset for
215 * the requirements of this function. The WiMAX stack guarantees
216 * serialization on calls to this function.
218 * Do a warm reset on the device; if it fails, resort to a cold reset
219 * and return -ENODEV. On successful warm reset, we need to block
220 * until it is complete.
222 * The bus-driver implementation of reset takes care of falling back
223 * to cold reset if warm fails.
226 int i2400m_op_reset(struct wimax_dev
*wimax_dev
)
229 struct i2400m
*i2400m
= wimax_dev_to_i2400m(wimax_dev
);
230 struct device
*dev
= i2400m_dev(i2400m
);
231 struct i2400m_reset_ctx ctx
= {
232 .completion
= COMPLETION_INITIALIZER_ONSTACK(ctx
.completion
),
236 d_fnstart(4, dev
, "(wimax_dev %p)\n", wimax_dev
);
237 mutex_lock(&i2400m
->init_mutex
);
238 i2400m
->reset_ctx
= &ctx
;
239 mutex_unlock(&i2400m
->init_mutex
);
240 result
= i2400m_reset(i2400m
, I2400M_RT_WARM
);
243 result
= wait_for_completion_timeout(&ctx
.completion
, 4*HZ
);
248 /* if result < 0, pass it on */
249 mutex_lock(&i2400m
->init_mutex
);
250 i2400m
->reset_ctx
= NULL
;
251 mutex_unlock(&i2400m
->init_mutex
);
253 d_fnend(4, dev
, "(wimax_dev %p) = %d\n", wimax_dev
, result
);
259 * Check the MAC address we got from boot mode is ok
261 * @i2400m: device descriptor
263 * Returns: 0 if ok, < 0 errno code on error.
266 int i2400m_check_mac_addr(struct i2400m
*i2400m
)
269 struct device
*dev
= i2400m_dev(i2400m
);
271 const struct i2400m_tlv_detailed_device_info
*ddi
;
272 struct net_device
*net_dev
= i2400m
->wimax_dev
.net_dev
;
273 const unsigned char zeromac
[ETH_ALEN
] = { 0 };
275 d_fnstart(3, dev
, "(i2400m %p)\n", i2400m
);
276 skb
= i2400m_get_device_info(i2400m
);
278 result
= PTR_ERR(skb
);
279 dev_err(dev
, "Cannot verify MAC address, error reading: %d\n",
283 /* Extract MAC addresss */
284 ddi
= (void *) skb
->data
;
285 BUILD_BUG_ON(ETH_ALEN
!= sizeof(ddi
->mac_address
));
286 d_printf(2, dev
, "GET DEVICE INFO: mac addr %pM\n",
288 if (!memcmp(net_dev
->perm_addr
, ddi
->mac_address
,
289 sizeof(ddi
->mac_address
)))
291 dev_warn(dev
, "warning: device reports a different MAC address "
292 "to that of boot mode's\n");
293 dev_warn(dev
, "device reports %pM\n", ddi
->mac_address
);
294 dev_warn(dev
, "boot mode reported %pM\n", net_dev
->perm_addr
);
295 if (!memcmp(zeromac
, ddi
->mac_address
, sizeof(zeromac
)))
296 dev_err(dev
, "device reports an invalid MAC address, "
299 dev_warn(dev
, "updating MAC address\n");
300 net_dev
->addr_len
= ETH_ALEN
;
301 memcpy(net_dev
->perm_addr
, ddi
->mac_address
, ETH_ALEN
);
302 memcpy(net_dev
->dev_addr
, ddi
->mac_address
, ETH_ALEN
);
308 d_fnend(3, dev
, "(i2400m %p) = %d\n", i2400m
, result
);
314 * __i2400m_dev_start - Bring up driver communication with the device
316 * @i2400m: device descriptor
317 * @flags: boot mode flags
319 * Returns: 0 if ok, < 0 errno code on error.
321 * Uploads firmware and brings up all the resources needed to be able
322 * to communicate with the device.
324 * The workqueue has to be setup early, at least before RX handling
325 * (it's only real user for now) so it can process reports as they
326 * arrive. We also want to destroy it if we retry, to make sure it is
327 * flushed...easier like this.
329 * TX needs to be setup before the bus-specific code (otherwise on
330 * shutdown, the bus-tx code could try to access it).
333 int __i2400m_dev_start(struct i2400m
*i2400m
, enum i2400m_bri flags
)
336 struct wimax_dev
*wimax_dev
= &i2400m
->wimax_dev
;
337 struct net_device
*net_dev
= wimax_dev
->net_dev
;
338 struct device
*dev
= i2400m_dev(i2400m
);
339 int times
= i2400m
->bus_bm_retries
;
341 d_fnstart(3, dev
, "(i2400m %p)\n", i2400m
);
343 result
= i2400m_dev_bootstrap(i2400m
, flags
);
345 dev_err(dev
, "cannot bootstrap device: %d\n", result
);
346 goto error_bootstrap
;
348 result
= i2400m_tx_setup(i2400m
);
351 result
= i2400m_rx_setup(i2400m
);
354 i2400m
->work_queue
= create_singlethread_workqueue(wimax_dev
->name
);
355 if (i2400m
->work_queue
== NULL
) {
357 dev_err(dev
, "cannot create workqueue\n");
358 goto error_create_workqueue
;
360 if (i2400m
->bus_dev_start
) {
361 result
= i2400m
->bus_dev_start(i2400m
);
363 goto error_bus_dev_start
;
366 wmb(); /* see i2400m->ready's documentation */
367 /* process pending reports from the device */
368 queue_work(i2400m
->work_queue
, &i2400m
->rx_report_ws
);
369 result
= i2400m_firmware_check(i2400m
); /* fw versions ok? */
372 /* At this point is ok to send commands to the device */
373 result
= i2400m_check_mac_addr(i2400m
);
375 goto error_check_mac_addr
;
376 result
= i2400m_dev_initialize(i2400m
);
378 goto error_dev_initialize
;
380 /* We don't want any additional unwanted error recovery triggered
381 * from any other context so if anything went wrong before we come
382 * here, let's keep i2400m->error_recovery untouched and leave it to
383 * dev_reset_handle(). See dev_reset_handle(). */
385 atomic_dec(&i2400m
->error_recovery
);
386 /* Every thing works so far, ok, now we are ready to
387 * take error recovery if it's required. */
389 /* At this point, reports will come for the device and set it
390 * to the right state if it is different than UNINITIALIZED */
391 d_fnend(3, dev
, "(net_dev %p [i2400m %p]) = %d\n",
392 net_dev
, i2400m
, result
);
395 error_dev_initialize
:
396 error_check_mac_addr
:
399 wmb(); /* see i2400m->ready's documentation */
400 flush_workqueue(i2400m
->work_queue
);
401 if (i2400m
->bus_dev_stop
)
402 i2400m
->bus_dev_stop(i2400m
);
404 destroy_workqueue(i2400m
->work_queue
);
405 error_create_workqueue
:
406 i2400m_rx_release(i2400m
);
408 i2400m_tx_release(i2400m
);
411 if (result
== -EL3RST
&& times
-- > 0) {
412 flags
= I2400M_BRI_SOFT
|I2400M_BRI_MAC_REINIT
;
415 d_fnend(3, dev
, "(net_dev %p [i2400m %p]) = %d\n",
416 net_dev
, i2400m
, result
);
422 int i2400m_dev_start(struct i2400m
*i2400m
, enum i2400m_bri bm_flags
)
425 mutex_lock(&i2400m
->init_mutex
); /* Well, start the device */
426 if (i2400m
->updown
== 0) {
427 result
= __i2400m_dev_start(i2400m
, bm_flags
);
431 wmb();/* see i2400m->updown and i2400m->alive's doc */
434 mutex_unlock(&i2400m
->init_mutex
);
440 * i2400m_dev_stop - Tear down driver communication with the device
442 * @i2400m: device descriptor
444 * Returns: 0 if ok, < 0 errno code on error.
446 * Releases all the resources allocated to communicate with the
447 * device. Note we cannot destroy the workqueue earlier as until RX is
448 * fully destroyed, it could still try to schedule jobs.
451 void __i2400m_dev_stop(struct i2400m
*i2400m
)
453 struct wimax_dev
*wimax_dev
= &i2400m
->wimax_dev
;
454 struct device
*dev
= i2400m_dev(i2400m
);
456 d_fnstart(3, dev
, "(i2400m %p)\n", i2400m
);
457 wimax_state_change(wimax_dev
, __WIMAX_ST_QUIESCING
);
458 i2400m_msg_to_dev_cancel_wait(i2400m
, -EL3RST
);
459 complete(&i2400m
->msg_completion
);
460 i2400m_net_wake_stop(i2400m
);
461 i2400m_dev_shutdown(i2400m
);
463 * Make sure no report hooks are running *before* we stop the
464 * communication infrastructure with the device.
466 i2400m
->ready
= 0; /* nobody can queue work anymore */
467 wmb(); /* see i2400m->ready's documentation */
468 flush_workqueue(i2400m
->work_queue
);
470 if (i2400m
->bus_dev_stop
)
471 i2400m
->bus_dev_stop(i2400m
);
472 destroy_workqueue(i2400m
->work_queue
);
473 i2400m_rx_release(i2400m
);
474 i2400m_tx_release(i2400m
);
475 wimax_state_change(wimax_dev
, WIMAX_ST_DOWN
);
476 d_fnend(3, dev
, "(i2400m %p) = 0\n", i2400m
);
481 * Watch out -- we only need to stop if there is a need for it. The
482 * device could have reset itself and failed to come up again (see
483 * _i2400m_dev_reset_handle()).
486 void i2400m_dev_stop(struct i2400m
*i2400m
)
488 mutex_lock(&i2400m
->init_mutex
);
489 if (i2400m
->updown
) {
490 __i2400m_dev_stop(i2400m
);
493 wmb(); /* see i2400m->updown and i2400m->alive's doc */
495 mutex_unlock(&i2400m
->init_mutex
);
500 * Listen to PM events to cache the firmware before suspend/hibernation
502 * When the device comes out of suspend, it might go into reset and
503 * firmware has to be uploaded again. At resume, most of the times, we
504 * can't load firmware images from disk, so we need to cache it.
506 * i2400m_fw_cache() will allocate a kobject and attach the firmware
507 * to it; that way we don't have to worry too much about the fw loader
508 * hitting a race condition.
510 * Note: modus operandi stolen from the Orinoco driver; thx.
513 int i2400m_pm_notifier(struct notifier_block
*notifier
,
514 unsigned long pm_event
,
517 struct i2400m
*i2400m
=
518 container_of(notifier
, struct i2400m
, pm_notifier
);
519 struct device
*dev
= i2400m_dev(i2400m
);
521 d_fnstart(3, dev
, "(i2400m %p pm_event %lx)\n", i2400m
, pm_event
);
523 case PM_HIBERNATION_PREPARE
:
524 case PM_SUSPEND_PREPARE
:
525 i2400m_fw_cache(i2400m
);
527 case PM_POST_RESTORE
:
528 /* Restore from hibernation failed. We need to clean
529 * up in exactly the same way, so fall through. */
530 case PM_POST_HIBERNATION
:
531 case PM_POST_SUSPEND
:
532 i2400m_fw_uncache(i2400m
);
535 case PM_RESTORE_PREPARE
:
539 d_fnend(3, dev
, "(i2400m %p pm_event %lx) = void\n", i2400m
, pm_event
);
545 * pre-reset is called before a device is going on reset
547 * This has to be followed by a call to i2400m_post_reset(), otherwise
548 * bad things might happen.
550 int i2400m_pre_reset(struct i2400m
*i2400m
)
553 struct device
*dev
= i2400m_dev(i2400m
);
555 d_fnstart(3, dev
, "(i2400m %p)\n", i2400m
);
556 d_printf(1, dev
, "pre-reset shut down\n");
559 mutex_lock(&i2400m
->init_mutex
);
560 if (i2400m
->updown
) {
561 netif_tx_disable(i2400m
->wimax_dev
.net_dev
);
562 __i2400m_dev_stop(i2400m
);
564 /* down't set updown to zero -- this way
565 * post_reset can restore properly */
567 mutex_unlock(&i2400m
->init_mutex
);
568 if (i2400m
->bus_release
)
569 i2400m
->bus_release(i2400m
);
570 d_fnend(3, dev
, "(i2400m %p) = %d\n", i2400m
, result
);
573 EXPORT_SYMBOL_GPL(i2400m_pre_reset
);
577 * Restore device state after a reset
579 * Do the work needed after a device reset to bring it up to the same
580 * state as it was before the reset.
582 * NOTE: this requires i2400m->init_mutex taken
584 int i2400m_post_reset(struct i2400m
*i2400m
)
587 struct device
*dev
= i2400m_dev(i2400m
);
589 d_fnstart(3, dev
, "(i2400m %p)\n", i2400m
);
590 d_printf(1, dev
, "post-reset start\n");
591 if (i2400m
->bus_setup
) {
592 result
= i2400m
->bus_setup(i2400m
);
594 dev_err(dev
, "bus-specific setup failed: %d\n",
596 goto error_bus_setup
;
599 mutex_lock(&i2400m
->init_mutex
);
600 if (i2400m
->updown
) {
601 result
= __i2400m_dev_start(
602 i2400m
, I2400M_BRI_SOFT
| I2400M_BRI_MAC_REINIT
);
604 goto error_dev_start
;
606 mutex_unlock(&i2400m
->init_mutex
);
607 d_fnend(3, dev
, "(i2400m %p) = %d\n", i2400m
, result
);
611 if (i2400m
->bus_release
)
612 i2400m
->bus_release(i2400m
);
613 /* even if the device was up, it could not be recovered, so we
614 * mark it as down. */
616 wmb(); /* see i2400m->updown's documentation */
617 mutex_unlock(&i2400m
->init_mutex
);
619 d_fnend(3, dev
, "(i2400m %p) = %d\n", i2400m
, result
);
622 EXPORT_SYMBOL_GPL(i2400m_post_reset
);
626 * The device has rebooted; fix up the device and the driver
628 * Tear down the driver communication with the device, reload the
629 * firmware and reinitialize the communication with the device.
631 * If someone calls a reset when the device's firmware is down, in
632 * theory we won't see it because we are not listening. However, just
633 * in case, leave the code to handle it.
635 * If there is a reset context, use it; this means someone is waiting
636 * for us to tell him when the reset operation is complete and the
637 * device is ready to rock again.
639 * NOTE: if we are in the process of bringing up or down the
640 * communication with the device [running i2400m_dev_start() or
641 * _stop()], don't do anything, let it fail and handle it.
643 * This function is ran always in a thread context
645 * This function gets passed, as payload to i2400m_work() a 'const
646 * char *' ptr with a "reason" why the reset happened (for messages).
649 void __i2400m_dev_reset_handle(struct work_struct
*ws
)
652 struct i2400m_work
*iw
= container_of(ws
, struct i2400m_work
, ws
);
654 struct i2400m
*i2400m
= iw
->i2400m
;
655 struct device
*dev
= i2400m_dev(i2400m
);
656 struct i2400m_reset_ctx
*ctx
= i2400m
->reset_ctx
;
658 if (WARN_ON(iw
->pl_size
!= sizeof(reason
)))
659 reason
= "SW BUG: reason n/a";
661 memcpy(&reason
, iw
->pl
, sizeof(reason
));
663 d_fnstart(3, dev
, "(ws %p i2400m %p reason %s)\n", ws
, i2400m
, reason
);
665 i2400m
->boot_mode
= 1;
666 wmb(); /* Make sure i2400m_msg_to_dev() sees boot_mode */
669 if (mutex_trylock(&i2400m
->init_mutex
) == 0) {
670 /* We are still in i2400m_dev_start() [let it fail] or
671 * i2400m_dev_stop() [we are shutting down anyway, so
672 * ignore it] or we are resetting somewhere else. */
673 dev_err(dev
, "device rebooted somewhere else?\n");
674 i2400m_msg_to_dev_cancel_wait(i2400m
, -EL3RST
);
675 complete(&i2400m
->msg_completion
);
679 dev_err(dev
, "%s: reinitializing driver\n", reason
);
681 if (i2400m
->updown
) {
682 __i2400m_dev_stop(i2400m
);
684 wmb(); /* see i2400m->updown's documentation */
688 result
= __i2400m_dev_start(i2400m
,
689 I2400M_BRI_SOFT
| I2400M_BRI_MAC_REINIT
);
691 dev_err(dev
, "%s: cannot start the device: %d\n",
694 if (atomic_read(&i2400m
->bus_reset_retries
)
695 >= I2400M_BUS_RESET_RETRIES
) {
697 dev_err(dev
, "tried too many times to "
698 "reset the device, giving up\n");
703 if (i2400m
->reset_ctx
) {
704 ctx
->result
= result
;
705 complete(&ctx
->completion
);
707 mutex_unlock(&i2400m
->init_mutex
);
708 if (result
== -EUCLEAN
) {
710 * We come here because the reset during operational mode
711 * wasn't successully done and need to proceed to a bus
712 * reset. For the dev_reset_handle() to be able to handle
713 * the reset event later properly, we restore boot_mode back
714 * to the state before previous reset. ie: just like we are
715 * issuing the bus reset for the first time
717 i2400m
->boot_mode
= 0;
720 atomic_inc(&i2400m
->bus_reset_retries
);
721 /* ops, need to clean up [w/ init_mutex not held] */
722 result
= i2400m_reset(i2400m
, I2400M_RT_BUS
);
728 /* great, we expect the device state up and
729 * dev_start() actually brings the device state up */
732 atomic_set(&i2400m
->bus_reset_retries
, 0);
738 d_fnend(3, dev
, "(ws %p i2400m %p reason %s) = void\n",
744 * i2400m_dev_reset_handle - Handle a device's reset in a thread context
746 * Schedule a device reset handling out on a thread context, so it
747 * is safe to call from atomic context. We can't use the i2400m's
748 * queue as we are going to destroy it and reinitialize it as part of
749 * the driver bringup/bringup process.
751 * See __i2400m_dev_reset_handle() for details; that takes care of
752 * reinitializing the driver to handle the reset, calling into the
753 * bus-specific functions ops as needed.
755 int i2400m_dev_reset_handle(struct i2400m
*i2400m
, const char *reason
)
757 return i2400m_schedule_work(i2400m
, __i2400m_dev_reset_handle
,
758 GFP_ATOMIC
, &reason
, sizeof(reason
));
760 EXPORT_SYMBOL_GPL(i2400m_dev_reset_handle
);
764 * The actual work of error recovery.
766 * The current implementation of error recovery is to trigger a bus reset.
769 void __i2400m_error_recovery(struct work_struct
*ws
)
771 struct i2400m_work
*iw
= container_of(ws
, struct i2400m_work
, ws
);
772 struct i2400m
*i2400m
= iw
->i2400m
;
774 i2400m_reset(i2400m
, I2400M_RT_BUS
);
782 * Schedule a work struct for error recovery.
784 * The intention of error recovery is to bring back the device to some
785 * known state whenever TX sees -110 (-ETIMEOUT) on copying the data to
786 * the device. The TX failure could mean a device bus stuck, so the current
787 * error recovery implementation is to trigger a bus reset to the device
788 * and hopefully it can bring back the device.
790 * The actual work of error recovery has to be in a thread context because
791 * it is kicked off in the TX thread (i2400ms->tx_workqueue) which is to be
792 * destroyed by the error recovery mechanism (currently a bus reset).
794 * Also, there may be already a queue of TX works that all hit
795 * the -ETIMEOUT error condition because the device is stuck already.
796 * Since bus reset is used as the error recovery mechanism and we don't
797 * want consecutive bus resets simply because the multiple TX works
798 * in the queue all hit the same device erratum, the flag "error_recovery"
799 * is introduced for preventing unwanted consecutive bus resets.
801 * Error recovery shall only be invoked again if previous one was completed.
802 * The flag error_recovery is set when error recovery mechanism is scheduled,
803 * and is checked when we need to schedule another error recovery. If it is
804 * in place already, then we shouldn't schedule another one.
806 void i2400m_error_recovery(struct i2400m
*i2400m
)
808 struct device
*dev
= i2400m_dev(i2400m
);
810 if (atomic_add_return(1, &i2400m
->error_recovery
) == 1) {
811 if (i2400m_schedule_work(i2400m
, __i2400m_error_recovery
,
812 GFP_ATOMIC
, NULL
, 0) < 0) {
813 dev_err(dev
, "run out of memory for "
814 "scheduling an error recovery ?\n");
815 atomic_dec(&i2400m
->error_recovery
);
818 atomic_dec(&i2400m
->error_recovery
);
821 EXPORT_SYMBOL_GPL(i2400m_error_recovery
);
824 * Alloc the command and ack buffers for boot mode
826 * Get the buffers needed to deal with boot mode messages. These
827 * buffers need to be allocated before the sdio recieve irq is setup.
830 int i2400m_bm_buf_alloc(struct i2400m
*i2400m
)
835 i2400m
->bm_cmd_buf
= kzalloc(I2400M_BM_CMD_BUF_SIZE
, GFP_KERNEL
);
836 if (i2400m
->bm_cmd_buf
== NULL
)
837 goto error_bm_cmd_kzalloc
;
838 i2400m
->bm_ack_buf
= kzalloc(I2400M_BM_ACK_BUF_SIZE
, GFP_KERNEL
);
839 if (i2400m
->bm_ack_buf
== NULL
)
840 goto error_bm_ack_buf_kzalloc
;
843 error_bm_ack_buf_kzalloc
:
844 kfree(i2400m
->bm_cmd_buf
);
845 error_bm_cmd_kzalloc
:
851 * Free boot mode command and ack buffers.
854 void i2400m_bm_buf_free(struct i2400m
*i2400m
)
856 kfree(i2400m
->bm_ack_buf
);
857 kfree(i2400m
->bm_cmd_buf
);
862 * i2400m_init - Initialize a 'struct i2400m' from all zeroes
864 * This is a bus-generic API call.
866 void i2400m_init(struct i2400m
*i2400m
)
868 wimax_dev_init(&i2400m
->wimax_dev
);
870 i2400m
->boot_mode
= 1;
871 i2400m
->rx_reorder
= 1;
872 init_waitqueue_head(&i2400m
->state_wq
);
874 spin_lock_init(&i2400m
->tx_lock
);
875 i2400m
->tx_pl_min
= UINT_MAX
;
876 i2400m
->tx_size_min
= UINT_MAX
;
878 spin_lock_init(&i2400m
->rx_lock
);
879 i2400m
->rx_pl_min
= UINT_MAX
;
880 i2400m
->rx_size_min
= UINT_MAX
;
881 INIT_LIST_HEAD(&i2400m
->rx_reports
);
882 INIT_WORK(&i2400m
->rx_report_ws
, i2400m_report_hook_work
);
884 mutex_init(&i2400m
->msg_mutex
);
885 init_completion(&i2400m
->msg_completion
);
887 mutex_init(&i2400m
->init_mutex
);
888 /* wake_tx_ws is initialized in i2400m_tx_setup() */
889 atomic_set(&i2400m
->bus_reset_retries
, 0);
893 /* initialize error_recovery to 1 for denoting we
894 * are not yet ready to take any error recovery */
895 atomic_set(&i2400m
->error_recovery
, 1);
897 EXPORT_SYMBOL_GPL(i2400m_init
);
900 int i2400m_reset(struct i2400m
*i2400m
, enum i2400m_reset_type rt
)
902 struct net_device
*net_dev
= i2400m
->wimax_dev
.net_dev
;
905 * Make sure we stop TXs and down the carrier before
906 * resetting; this is needed to avoid things like
907 * i2400m_wake_tx() scheduling stuff in parallel.
909 if (net_dev
->reg_state
== NETREG_REGISTERED
) {
910 netif_tx_disable(net_dev
);
911 netif_carrier_off(net_dev
);
913 return i2400m
->bus_reset(i2400m
, rt
);
915 EXPORT_SYMBOL_GPL(i2400m_reset
);
919 * i2400m_setup - bus-generic setup function for the i2400m device
921 * @i2400m: device descriptor (bus-specific parts have been initialized)
923 * Returns: 0 if ok, < 0 errno code on error.
925 * Sets up basic device comunication infrastructure, boots the ROM to
926 * read the MAC address, registers with the WiMAX and network stacks
927 * and then brings up the device.
929 int i2400m_setup(struct i2400m
*i2400m
, enum i2400m_bri bm_flags
)
931 int result
= -ENODEV
;
932 struct device
*dev
= i2400m_dev(i2400m
);
933 struct wimax_dev
*wimax_dev
= &i2400m
->wimax_dev
;
934 struct net_device
*net_dev
= i2400m
->wimax_dev
.net_dev
;
936 d_fnstart(3, dev
, "(i2400m %p)\n", i2400m
);
938 snprintf(wimax_dev
->name
, sizeof(wimax_dev
->name
),
939 "i2400m-%s:%s", dev
->bus
->name
, dev_name(dev
));
941 result
= i2400m_bm_buf_alloc(i2400m
);
943 dev_err(dev
, "cannot allocate bootmode scratch buffers\n");
944 goto error_bm_buf_alloc
;
947 if (i2400m
->bus_setup
) {
948 result
= i2400m
->bus_setup(i2400m
);
950 dev_err(dev
, "bus-specific setup failed: %d\n",
952 goto error_bus_setup
;
956 result
= i2400m_bootrom_init(i2400m
, bm_flags
);
958 dev_err(dev
, "read mac addr: bootrom init "
959 "failed: %d\n", result
);
960 goto error_bootrom_init
;
962 result
= i2400m_read_mac_addr(i2400m
);
964 goto error_read_mac_addr
;
965 random_ether_addr(i2400m
->src_mac_addr
);
967 i2400m
->pm_notifier
.notifier_call
= i2400m_pm_notifier
;
968 register_pm_notifier(&i2400m
->pm_notifier
);
970 result
= register_netdev(net_dev
); /* Okey dokey, bring it up */
972 dev_err(dev
, "cannot register i2400m network device: %d\n",
974 goto error_register_netdev
;
976 netif_carrier_off(net_dev
);
978 i2400m
->wimax_dev
.op_msg_from_user
= i2400m_op_msg_from_user
;
979 i2400m
->wimax_dev
.op_rfkill_sw_toggle
= i2400m_op_rfkill_sw_toggle
;
980 i2400m
->wimax_dev
.op_reset
= i2400m_op_reset
;
982 result
= wimax_dev_add(&i2400m
->wimax_dev
, net_dev
);
984 goto error_wimax_dev_add
;
986 /* Now setup all that requires a registered net and wimax device. */
987 result
= sysfs_create_group(&net_dev
->dev
.kobj
, &i2400m_dev_attr_group
);
989 dev_err(dev
, "cannot setup i2400m's sysfs: %d\n", result
);
990 goto error_sysfs_setup
;
993 result
= i2400m_debugfs_add(i2400m
);
995 dev_err(dev
, "cannot setup i2400m's debugfs: %d\n", result
);
996 goto error_debugfs_setup
;
999 result
= i2400m_dev_start(i2400m
, bm_flags
);
1001 goto error_dev_start
;
1002 d_fnend(3, dev
, "(i2400m %p) = %d\n", i2400m
, result
);
1006 i2400m_debugfs_rm(i2400m
);
1007 error_debugfs_setup
:
1008 sysfs_remove_group(&i2400m
->wimax_dev
.net_dev
->dev
.kobj
,
1009 &i2400m_dev_attr_group
);
1011 wimax_dev_rm(&i2400m
->wimax_dev
);
1012 error_wimax_dev_add
:
1013 unregister_netdev(net_dev
);
1014 error_register_netdev
:
1015 unregister_pm_notifier(&i2400m
->pm_notifier
);
1016 error_read_mac_addr
:
1018 if (i2400m
->bus_release
)
1019 i2400m
->bus_release(i2400m
);
1021 i2400m_bm_buf_free(i2400m
);
1023 d_fnend(3, dev
, "(i2400m %p) = %d\n", i2400m
, result
);
1026 EXPORT_SYMBOL_GPL(i2400m_setup
);
1030 * i2400m_release - release the bus-generic driver resources
1032 * Sends a disconnect message and undoes any setup done by i2400m_setup()
1034 void i2400m_release(struct i2400m
*i2400m
)
1036 struct device
*dev
= i2400m_dev(i2400m
);
1038 d_fnstart(3, dev
, "(i2400m %p)\n", i2400m
);
1039 netif_stop_queue(i2400m
->wimax_dev
.net_dev
);
1041 i2400m_dev_stop(i2400m
);
1043 i2400m_debugfs_rm(i2400m
);
1044 sysfs_remove_group(&i2400m
->wimax_dev
.net_dev
->dev
.kobj
,
1045 &i2400m_dev_attr_group
);
1046 wimax_dev_rm(&i2400m
->wimax_dev
);
1047 unregister_netdev(i2400m
->wimax_dev
.net_dev
);
1048 unregister_pm_notifier(&i2400m
->pm_notifier
);
1049 if (i2400m
->bus_release
)
1050 i2400m
->bus_release(i2400m
);
1051 i2400m_bm_buf_free(i2400m
);
1052 d_fnend(3, dev
, "(i2400m %p) = void\n", i2400m
);
1054 EXPORT_SYMBOL_GPL(i2400m_release
);
1058 * Debug levels control; see debug.h
1060 struct d_level D_LEVEL
[] = {
1061 D_SUBMODULE_DEFINE(control
),
1062 D_SUBMODULE_DEFINE(driver
),
1063 D_SUBMODULE_DEFINE(debugfs
),
1064 D_SUBMODULE_DEFINE(fw
),
1065 D_SUBMODULE_DEFINE(netdev
),
1066 D_SUBMODULE_DEFINE(rfkill
),
1067 D_SUBMODULE_DEFINE(rx
),
1068 D_SUBMODULE_DEFINE(sysfs
),
1069 D_SUBMODULE_DEFINE(tx
),
1071 size_t D_LEVEL_SIZE
= ARRAY_SIZE(D_LEVEL
);
1075 int __init
i2400m_driver_init(void)
1077 d_parse_params(D_LEVEL
, D_LEVEL_SIZE
, i2400m_debug_params
,
1079 return i2400m_barker_db_init(i2400m_barkers_params
);
1081 module_init(i2400m_driver_init
);
1084 void __exit
i2400m_driver_exit(void)
1086 /* for scheds i2400m_dev_reset_handle() */
1087 flush_scheduled_work();
1088 i2400m_barker_db_exit();
1090 module_exit(i2400m_driver_exit
);
1092 MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
1093 MODULE_DESCRIPTION("Intel 2400M WiMAX networking bus-generic driver");
1094 MODULE_LICENSE("GPL");