tree-wide: fix comment/printk typos
[deliverable/linux.git] / drivers / net / wimax / i2400m / driver.c
CommitLineData
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()
0856ccf2 44 * i2400m->bus_setup()
024f7f31
IPG
45 * i2400m_bootrom_init()
46 * register_netdev()
0856ccf2 47 * wimax_dev_add()
024f7f31
IPG
48 * i2400m_dev_start()
49 * __i2400m_dev_start()
50 * i2400m_dev_bootstrap()
51 * i2400m_tx_setup()
52 * i2400m->bus_dev_start()
6a0f7ab8 53 * i2400m_firmware_check()
024f7f31 54 * i2400m_check_mac_addr()
024f7f31
IPG
55 *
56 * i2400m_release()
024f7f31
IPG
57 * i2400m_dev_stop()
58 * __i2400m_dev_stop()
59 * i2400m_dev_shutdown()
60 * i2400m->bus_dev_stop()
61 * i2400m_tx_release()
0856ccf2
IPG
62 * i2400m->bus_release()
63 * wimax_dev_rm()
024f7f31
IPG
64 * unregister_netdev()
65 */
66#include "i2400m.h"
fe442683 67#include <linux/etherdevice.h>
024f7f31
IPG
68#include <linux/wimax/i2400m.h>
69#include <linux/module.h>
70#include <linux/moduleparam.h>
7b43ca70 71#include <linux/suspend.h>
5a0e3ad6 72#include <linux/slab.h>
024f7f31
IPG
73
74#define D_SUBMODULE driver
75#include "debug-levels.h"
76
77
4c2b1a11
IPG
78static char i2400m_debug_params[128];
79module_param_string(debug, i2400m_debug_params, sizeof(i2400m_debug_params),
80 0644);
81MODULE_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.");
85
aba3792a
IPG
86static char i2400m_barkers_params[128];
87module_param_string(barkers, i2400m_barkers_params,
88 sizeof(i2400m_barkers_params), 0644);
89MODULE_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.");
94
b0fbcb2a
IPG
95static
96struct 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)
99{
100 struct i2400m_work *iw;
101
102 iw = kzalloc(sizeof(*iw) + pl_size, gfp_flags);
103 if (iw == NULL)
104 return NULL;
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);
109 return iw;
110}
111
112
024f7f31
IPG
113/*
114 * Schedule i2400m's specific work on the system's queue.
115 *
116 * Used for a few cases where we really need it; otherwise, identical
117 * to i2400m_queue_work().
118 *
119 * Returns < 0 errno code on error, 1 if ok.
120 *
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.
124 */
e3d32687 125static int i2400m_schedule_work(struct i2400m *i2400m,
b0fbcb2a
IPG
126 void (*fn)(struct work_struct *), gfp_t gfp_flags,
127 const void *pl, size_t pl_size)
024f7f31
IPG
128{
129 int result;
130 struct i2400m_work *iw;
131
024f7f31 132 result = -ENOMEM;
b0fbcb2a
IPG
133 iw = __i2400m_work_setup(i2400m, fn, gfp_flags, pl, pl_size);
134 if (iw != NULL) {
135 result = schedule_work(&iw->ws);
136 if (WARN_ON(result == 0))
137 result = -ENXIO;
138 }
024f7f31
IPG
139 return result;
140}
141
142
143/*
144 * WiMAX stack operation: relay a message from user space
145 *
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
151 *
152 * The WiMAX stack will call this function when a message was received
153 * from user space.
154 *
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.
159 *
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.
163 *
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.
169 *
170 * Note that asynchronous events from the device are processed and
171 * sent to user space in rx.c.
172 */
173static
174int 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)
178{
179 int result;
180 struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
181 struct device *dev = i2400m_dev(i2400m);
182 struct sk_buff *ack_skb;
183
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);
189 if (IS_ERR(ack_skb))
190 goto error_msg_to_dev;
024f7f31
IPG
191 result = wimax_msg_send(&i2400m->wimax_dev, ack_skb);
192error_msg_to_dev:
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,
195 genl_info, result);
196 return result;
197}
198
199
200/*
201 * Context to wait for a reset to finalize
202 */
203struct i2400m_reset_ctx {
204 struct completion completion;
205 int result;
206};
207
208
209/*
210 * WiMAX stack operation: reset a device
211 *
212 * @wimax_dev: device descriptor
213 *
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.
217 *
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.
221 *
222 * The bus-driver implementation of reset takes care of falling back
223 * to cold reset if warm fails.
224 */
225static
226int i2400m_op_reset(struct wimax_dev *wimax_dev)
227{
228 int result;
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),
233 .result = 0,
234 };
235
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);
c931ceeb 240 result = i2400m_reset(i2400m, I2400M_RT_WARM);
024f7f31
IPG
241 if (result < 0)
242 goto out;
243 result = wait_for_completion_timeout(&ctx.completion, 4*HZ);
244 if (result == 0)
245 result = -ETIMEDOUT;
246 else if (result > 0)
247 result = ctx.result;
248 /* if result < 0, pass it on */
249 mutex_lock(&i2400m->init_mutex);
250 i2400m->reset_ctx = NULL;
251 mutex_unlock(&i2400m->init_mutex);
252out:
253 d_fnend(4, dev, "(wimax_dev %p) = %d\n", wimax_dev, result);
254 return result;
255}
256
257
258/*
259 * Check the MAC address we got from boot mode is ok
260 *
261 * @i2400m: device descriptor
262 *
263 * Returns: 0 if ok, < 0 errno code on error.
264 */
265static
266int i2400m_check_mac_addr(struct i2400m *i2400m)
267{
268 int result;
269 struct device *dev = i2400m_dev(i2400m);
270 struct sk_buff *skb;
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 };
274
275 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
276 skb = i2400m_get_device_info(i2400m);
277 if (IS_ERR(skb)) {
278 result = PTR_ERR(skb);
279 dev_err(dev, "Cannot verify MAC address, error reading: %d\n",
280 result);
281 goto error;
282 }
b595076a 283 /* Extract MAC address */
024f7f31
IPG
284 ddi = (void *) skb->data;
285 BUILD_BUG_ON(ETH_ALEN != sizeof(ddi->mac_address));
4754b3de 286 d_printf(2, dev, "GET DEVICE INFO: mac addr %pM\n",
287 ddi->mac_address);
024f7f31
IPG
288 if (!memcmp(net_dev->perm_addr, ddi->mac_address,
289 sizeof(ddi->mac_address)))
290 goto ok;
291 dev_warn(dev, "warning: device reports a different MAC address "
292 "to that of boot mode's\n");
4754b3de 293 dev_warn(dev, "device reports %pM\n", ddi->mac_address);
294 dev_warn(dev, "boot mode reported %pM\n", net_dev->perm_addr);
024f7f31
IPG
295 if (!memcmp(zeromac, ddi->mac_address, sizeof(zeromac)))
296 dev_err(dev, "device reports an invalid MAC address, "
297 "not updating\n");
298 else {
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);
303 }
304ok:
305 result = 0;
306 kfree_skb(skb);
307error:
308 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
309 return result;
310}
311
312
313/**
314 * __i2400m_dev_start - Bring up driver communication with the device
315 *
316 * @i2400m: device descriptor
317 * @flags: boot mode flags
318 *
319 * Returns: 0 if ok, < 0 errno code on error.
320 *
321 * Uploads firmware and brings up all the resources needed to be able
322 * to communicate with the device.
323 *
e9a6b45b
IPG
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.
328 *
024f7f31
IPG
329 * TX needs to be setup before the bus-specific code (otherwise on
330 * shutdown, the bus-tx code could try to access it).
331 */
332static
333int __i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri flags)
334{
335 int result;
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);
ecddfd5e 339 int times = i2400m->bus_bm_retries;
024f7f31
IPG
340
341 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
342retry:
343 result = i2400m_dev_bootstrap(i2400m, flags);
344 if (result < 0) {
345 dev_err(dev, "cannot bootstrap device: %d\n", result);
346 goto error_bootstrap;
347 }
348 result = i2400m_tx_setup(i2400m);
349 if (result < 0)
350 goto error_tx_setup;
c747583d
IPG
351 result = i2400m_rx_setup(i2400m);
352 if (result < 0)
353 goto error_rx_setup;
024f7f31
IPG
354 i2400m->work_queue = create_singlethread_workqueue(wimax_dev->name);
355 if (i2400m->work_queue == NULL) {
356 result = -ENOMEM;
357 dev_err(dev, "cannot create workqueue\n");
358 goto error_create_workqueue;
359 }
097acbef
IPG
360 if (i2400m->bus_dev_start) {
361 result = i2400m->bus_dev_start(i2400m);
362 if (result < 0)
363 goto error_bus_dev_start;
364 }
c2315b4e
IPG
365 i2400m->ready = 1;
366 wmb(); /* see i2400m->ready's documentation */
a0beba21
IPG
367 /* process pending reports from the device */
368 queue_work(i2400m->work_queue, &i2400m->rx_report_ws);
6a0f7ab8
IPG
369 result = i2400m_firmware_check(i2400m); /* fw versions ok? */
370 if (result < 0)
371 goto error_fw_check;
024f7f31
IPG
372 /* At this point is ok to send commands to the device */
373 result = i2400m_check_mac_addr(i2400m);
374 if (result < 0)
375 goto error_check_mac_addr;
024f7f31
IPG
376 result = i2400m_dev_initialize(i2400m);
377 if (result < 0)
378 goto error_dev_initialize;
599e5953
CK
379
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(). */
384
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. */
388
024f7f31
IPG
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);
393 return result;
394
395error_dev_initialize:
396error_check_mac_addr:
49d72df3 397error_fw_check:
c2315b4e
IPG
398 i2400m->ready = 0;
399 wmb(); /* see i2400m->ready's documentation */
400 flush_workqueue(i2400m->work_queue);
097acbef
IPG
401 if (i2400m->bus_dev_stop)
402 i2400m->bus_dev_stop(i2400m);
024f7f31 403error_bus_dev_start:
e9a6b45b
IPG
404 destroy_workqueue(i2400m->work_queue);
405error_create_workqueue:
c747583d
IPG
406 i2400m_rx_release(i2400m);
407error_rx_setup:
024f7f31
IPG
408 i2400m_tx_release(i2400m);
409error_tx_setup:
410error_bootstrap:
0bcfc5ef 411 if (result == -EL3RST && times-- > 0) {
8b5b30ee 412 flags = I2400M_BRI_SOFT|I2400M_BRI_MAC_REINIT;
024f7f31
IPG
413 goto retry;
414 }
415 d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
416 net_dev, i2400m, result);
417 return result;
418}
419
420
421static
422int i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri bm_flags)
423{
c2315b4e 424 int result = 0;
024f7f31 425 mutex_lock(&i2400m->init_mutex); /* Well, start the device */
c2315b4e
IPG
426 if (i2400m->updown == 0) {
427 result = __i2400m_dev_start(i2400m, bm_flags);
428 if (result >= 0) {
429 i2400m->updown = 1;
f4e41345
CK
430 i2400m->alive = 1;
431 wmb();/* see i2400m->updown and i2400m->alive's doc */
c2315b4e
IPG
432 }
433 }
024f7f31
IPG
434 mutex_unlock(&i2400m->init_mutex);
435 return result;
436}
437
438
439/**
440 * i2400m_dev_stop - Tear down driver communication with the device
441 *
442 * @i2400m: device descriptor
443 *
444 * Returns: 0 if ok, < 0 errno code on error.
445 *
e9a6b45b
IPG
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.
024f7f31
IPG
449 */
450static
451void __i2400m_dev_stop(struct i2400m *i2400m)
452{
453 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
454 struct device *dev = i2400m_dev(i2400m);
455
456 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
457 wimax_state_change(wimax_dev, __WIMAX_ST_QUIESCING);
5eeae35b
IPG
458 i2400m_msg_to_dev_cancel_wait(i2400m, -EL3RST);
459 complete(&i2400m->msg_completion);
ac53aed9 460 i2400m_net_wake_stop(i2400m);
024f7f31 461 i2400m_dev_shutdown(i2400m);
c2315b4e
IPG
462 /*
463 * Make sure no report hooks are running *before* we stop the
464 * communication infrastructure with the device.
465 */
466 i2400m->ready = 0; /* nobody can queue work anymore */
467 wmb(); /* see i2400m->ready's documentation */
468 flush_workqueue(i2400m->work_queue);
469
097acbef
IPG
470 if (i2400m->bus_dev_stop)
471 i2400m->bus_dev_stop(i2400m);
e9a6b45b 472 destroy_workqueue(i2400m->work_queue);
c747583d 473 i2400m_rx_release(i2400m);
024f7f31
IPG
474 i2400m_tx_release(i2400m);
475 wimax_state_change(wimax_dev, WIMAX_ST_DOWN);
476 d_fnend(3, dev, "(i2400m %p) = 0\n", i2400m);
477}
478
479
480/*
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()).
484 */
485static
486void i2400m_dev_stop(struct i2400m *i2400m)
487{
488 mutex_lock(&i2400m->init_mutex);
489 if (i2400m->updown) {
490 __i2400m_dev_stop(i2400m);
491 i2400m->updown = 0;
f4e41345
CK
492 i2400m->alive = 0;
493 wmb(); /* see i2400m->updown and i2400m->alive's doc */
024f7f31
IPG
494 }
495 mutex_unlock(&i2400m->init_mutex);
496}
497
498
7b43ca70
IPG
499/*
500 * Listen to PM events to cache the firmware before suspend/hibernation
501 *
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.
505 *
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.
509 *
510 * Note: modus operandi stolen from the Orinoco driver; thx.
511 */
512static
513int i2400m_pm_notifier(struct notifier_block *notifier,
514 unsigned long pm_event,
515 void *unused)
516{
517 struct i2400m *i2400m =
518 container_of(notifier, struct i2400m, pm_notifier);
519 struct device *dev = i2400m_dev(i2400m);
520
521 d_fnstart(3, dev, "(i2400m %p pm_event %lx)\n", i2400m, pm_event);
522 switch (pm_event) {
523 case PM_HIBERNATION_PREPARE:
524 case PM_SUSPEND_PREPARE:
525 i2400m_fw_cache(i2400m);
526 break;
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);
533 break;
534
535 case PM_RESTORE_PREPARE:
536 default:
537 break;
538 }
539 d_fnend(3, dev, "(i2400m %p pm_event %lx) = void\n", i2400m, pm_event);
540 return NOTIFY_DONE;
541}
542
543
3725d8c9
IPG
544/*
545 * pre-reset is called before a device is going on reset
546 *
547 * This has to be followed by a call to i2400m_post_reset(), otherwise
548 * bad things might happen.
549 */
550int i2400m_pre_reset(struct i2400m *i2400m)
551{
552 int result;
553 struct device *dev = i2400m_dev(i2400m);
554
555 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
556 d_printf(1, dev, "pre-reset shut down\n");
557
558 result = 0;
559 mutex_lock(&i2400m->init_mutex);
560 if (i2400m->updown) {
561 netif_tx_disable(i2400m->wimax_dev.net_dev);
562 __i2400m_dev_stop(i2400m);
563 result = 0;
564 /* down't set updown to zero -- this way
565 * post_reset can restore properly */
566 }
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);
571 return result;
572}
573EXPORT_SYMBOL_GPL(i2400m_pre_reset);
574
575
576/*
577 * Restore device state after a reset
578 *
579 * Do the work needed after a device reset to bring it up to the same
580 * state as it was before the reset.
581 *
582 * NOTE: this requires i2400m->init_mutex taken
583 */
584int i2400m_post_reset(struct i2400m *i2400m)
585{
586 int result = 0;
587 struct device *dev = i2400m_dev(i2400m);
588
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);
593 if (result < 0) {
594 dev_err(dev, "bus-specific setup failed: %d\n",
595 result);
596 goto error_bus_setup;
597 }
598 }
599 mutex_lock(&i2400m->init_mutex);
600 if (i2400m->updown) {
601 result = __i2400m_dev_start(
602 i2400m, I2400M_BRI_SOFT | I2400M_BRI_MAC_REINIT);
603 if (result < 0)
604 goto error_dev_start;
605 }
606 mutex_unlock(&i2400m->init_mutex);
607 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
608 return result;
609
610error_dev_start:
611 if (i2400m->bus_release)
612 i2400m->bus_release(i2400m);
3725d8c9
IPG
613 /* even if the device was up, it could not be recovered, so we
614 * mark it as down. */
615 i2400m->updown = 0;
616 wmb(); /* see i2400m->updown's documentation */
617 mutex_unlock(&i2400m->init_mutex);
2354161d 618error_bus_setup:
3725d8c9
IPG
619 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
620 return result;
621}
622EXPORT_SYMBOL_GPL(i2400m_post_reset);
623
624
024f7f31
IPG
625/*
626 * The device has rebooted; fix up the device and the driver
627 *
628 * Tear down the driver communication with the device, reload the
629 * firmware and reinitialize the communication with the device.
630 *
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.
634 *
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.
638 *
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.
642 *
643 * This function is ran always in a thread context
3ef6129e
IPG
644 *
645 * This function gets passed, as payload to i2400m_work() a 'const
646 * char *' ptr with a "reason" why the reset happened (for messages).
024f7f31
IPG
647 */
648static
649void __i2400m_dev_reset_handle(struct work_struct *ws)
650{
651 int result;
652 struct i2400m_work *iw = container_of(ws, struct i2400m_work, ws);
3ef6129e 653 const char *reason;
024f7f31
IPG
654 struct i2400m *i2400m = iw->i2400m;
655 struct device *dev = i2400m_dev(i2400m);
024f7f31
IPG
656 struct i2400m_reset_ctx *ctx = i2400m->reset_ctx;
657
3ef6129e
IPG
658 if (WARN_ON(iw->pl_size != sizeof(reason)))
659 reason = "SW BUG: reason n/a";
660 else
661 memcpy(&reason, iw->pl, sizeof(reason));
662
663 d_fnstart(3, dev, "(ws %p i2400m %p reason %s)\n", ws, i2400m, reason);
664
f4e41345
CK
665 i2400m->boot_mode = 1;
666 wmb(); /* Make sure i2400m_msg_to_dev() sees boot_mode */
667
024f7f31
IPG
668 result = 0;
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. */
c2315b4e 673 dev_err(dev, "device rebooted somewhere else?\n");
0bcfc5ef 674 i2400m_msg_to_dev_cancel_wait(i2400m, -EL3RST);
024f7f31
IPG
675 complete(&i2400m->msg_completion);
676 goto out;
677 }
f4e41345 678
3ef6129e 679 dev_err(dev, "%s: reinitializing driver\n", reason);
f4e41345
CK
680 rmb();
681 if (i2400m->updown) {
682 __i2400m_dev_stop(i2400m);
c2315b4e
IPG
683 i2400m->updown = 0;
684 wmb(); /* see i2400m->updown's documentation */
c2315b4e 685 }
f4e41345
CK
686
687 if (i2400m->alive) {
688 result = __i2400m_dev_start(i2400m,
689 I2400M_BRI_SOFT | I2400M_BRI_MAC_REINIT);
690 if (result < 0) {
691 dev_err(dev, "%s: cannot start the device: %d\n",
692 reason, result);
693 result = -EUCLEAN;
694 if (atomic_read(&i2400m->bus_reset_retries)
695 >= I2400M_BUS_RESET_RETRIES) {
696 result = -ENODEV;
697 dev_err(dev, "tried too many times to "
698 "reset the device, giving up\n");
699 }
700 }
701 }
702
024f7f31
IPG
703 if (i2400m->reset_ctx) {
704 ctx->result = result;
705 complete(&ctx->completion);
706 }
707 mutex_unlock(&i2400m->init_mutex);
b9ee9501 708 if (result == -EUCLEAN) {
f4e41345
CK
709 /*
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
716 */
717 i2400m->boot_mode = 0;
718 wmb();
719
720 atomic_inc(&i2400m->bus_reset_retries);
b9ee9501 721 /* ops, need to clean up [w/ init_mutex not held] */
c931ceeb 722 result = i2400m_reset(i2400m, I2400M_RT_BUS);
b9ee9501
IPG
723 if (result >= 0)
724 result = -ENODEV;
f4e41345
CK
725 } else {
726 rmb();
727 if (i2400m->alive) {
728 /* great, we expect the device state up and
729 * dev_start() actually brings the device state up */
730 i2400m->updown = 1;
731 wmb();
732 atomic_set(&i2400m->bus_reset_retries, 0);
733 }
b9ee9501 734 }
024f7f31
IPG
735out:
736 i2400m_put(i2400m);
737 kfree(iw);
3ef6129e
IPG
738 d_fnend(3, dev, "(ws %p i2400m %p reason %s) = void\n",
739 ws, i2400m, reason);
024f7f31
IPG
740}
741
742
743/**
744 * i2400m_dev_reset_handle - Handle a device's reset in a thread context
745 *
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.
750 *
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.
754 */
3ef6129e 755int i2400m_dev_reset_handle(struct i2400m *i2400m, const char *reason)
024f7f31
IPG
756{
757 return i2400m_schedule_work(i2400m, __i2400m_dev_reset_handle,
3ef6129e 758 GFP_ATOMIC, &reason, sizeof(reason));
024f7f31
IPG
759}
760EXPORT_SYMBOL_GPL(i2400m_dev_reset_handle);
761
762
599e5953
CK
763 /*
764 * The actual work of error recovery.
765 *
766 * The current implementation of error recovery is to trigger a bus reset.
767 */
768static
769void __i2400m_error_recovery(struct work_struct *ws)
770{
771 struct i2400m_work *iw = container_of(ws, struct i2400m_work, ws);
772 struct i2400m *i2400m = iw->i2400m;
773
774 i2400m_reset(i2400m, I2400M_RT_BUS);
775
776 i2400m_put(i2400m);
777 kfree(iw);
778 return;
779}
780
781/*
782 * Schedule a work struct for error recovery.
783 *
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.
789 *
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).
793 *
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.
800 *
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.
805 */
806void i2400m_error_recovery(struct i2400m *i2400m)
807{
808 struct device *dev = i2400m_dev(i2400m);
809
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);
816 }
817 } else
818 atomic_dec(&i2400m->error_recovery);
819 return;
820}
821EXPORT_SYMBOL_GPL(i2400m_error_recovery);
822
2869da85
IPG
823/*
824 * Alloc the command and ack buffers for boot mode
a134fd6b
DB
825 *
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.
828 */
2869da85 829static
a134fd6b
DB
830int i2400m_bm_buf_alloc(struct i2400m *i2400m)
831{
832 int result;
833
834 result = -ENOMEM;
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;
841 return 0;
842
843error_bm_ack_buf_kzalloc:
844 kfree(i2400m->bm_cmd_buf);
845error_bm_cmd_kzalloc:
846 return result;
847}
a134fd6b 848
2869da85
IPG
849
850/*
851 * Free boot mode command and ack buffers.
a134fd6b 852 */
2869da85 853static
a134fd6b
DB
854void i2400m_bm_buf_free(struct i2400m *i2400m)
855{
856 kfree(i2400m->bm_ack_buf);
857 kfree(i2400m->bm_cmd_buf);
a134fd6b 858}
2869da85
IPG
859
860
af77dfa7
IPG
861/**
862 * i2400m_init - Initialize a 'struct i2400m' from all zeroes
863 *
864 * This is a bus-generic API call.
865 */
866void i2400m_init(struct i2400m *i2400m)
867{
868 wimax_dev_init(&i2400m->wimax_dev);
869
870 i2400m->boot_mode = 1;
871 i2400m->rx_reorder = 1;
872 init_waitqueue_head(&i2400m->state_wq);
873
874 spin_lock_init(&i2400m->tx_lock);
875 i2400m->tx_pl_min = UINT_MAX;
876 i2400m->tx_size_min = UINT_MAX;
877
878 spin_lock_init(&i2400m->rx_lock);
879 i2400m->rx_pl_min = UINT_MAX;
880 i2400m->rx_size_min = UINT_MAX;
a0beba21
IPG
881 INIT_LIST_HEAD(&i2400m->rx_reports);
882 INIT_WORK(&i2400m->rx_report_ws, i2400m_report_hook_work);
af77dfa7
IPG
883
884 mutex_init(&i2400m->msg_mutex);
885 init_completion(&i2400m->msg_completion);
886
887 mutex_init(&i2400m->init_mutex);
888 /* wake_tx_ws is initialized in i2400m_tx_setup() */
f4e41345
CK
889 atomic_set(&i2400m->bus_reset_retries, 0);
890
891 i2400m->alive = 0;
599e5953
CK
892
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);
af77dfa7
IPG
896}
897EXPORT_SYMBOL_GPL(i2400m_init);
898
899
c931ceeb
IPG
900int i2400m_reset(struct i2400m *i2400m, enum i2400m_reset_type rt)
901{
902 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
903
904 /*
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.
908 */
909 if (net_dev->reg_state == NETREG_REGISTERED) {
910 netif_tx_disable(net_dev);
911 netif_carrier_off(net_dev);
912 }
913 return i2400m->bus_reset(i2400m, rt);
914}
915EXPORT_SYMBOL_GPL(i2400m_reset);
916
917
024f7f31
IPG
918/**
919 * i2400m_setup - bus-generic setup function for the i2400m device
920 *
921 * @i2400m: device descriptor (bus-specific parts have been initialized)
922 *
923 * Returns: 0 if ok, < 0 errno code on error.
924 *
8f90f3ee
IPG
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.
024f7f31
IPG
928 */
929int i2400m_setup(struct i2400m *i2400m, enum i2400m_bri bm_flags)
930{
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;
935
936 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
937
938 snprintf(wimax_dev->name, sizeof(wimax_dev->name),
347707ba 939 "i2400m-%s:%s", dev->bus->name, dev_name(dev));
024f7f31 940
2869da85
IPG
941 result = i2400m_bm_buf_alloc(i2400m);
942 if (result < 0) {
943 dev_err(dev, "cannot allocate bootmode scratch buffers\n");
944 goto error_bm_buf_alloc;
945 }
946
0856ccf2
IPG
947 if (i2400m->bus_setup) {
948 result = i2400m->bus_setup(i2400m);
949 if (result < 0) {
950 dev_err(dev, "bus-specific setup failed: %d\n",
951 result);
952 goto error_bus_setup;
953 }
954 }
955
024f7f31
IPG
956 result = i2400m_bootrom_init(i2400m, bm_flags);
957 if (result < 0) {
958 dev_err(dev, "read mac addr: bootrom init "
959 "failed: %d\n", result);
960 goto error_bootrom_init;
961 }
962 result = i2400m_read_mac_addr(i2400m);
963 if (result < 0)
964 goto error_read_mac_addr;
fe442683 965 random_ether_addr(i2400m->src_mac_addr);
024f7f31 966
7b43ca70
IPG
967 i2400m->pm_notifier.notifier_call = i2400m_pm_notifier;
968 register_pm_notifier(&i2400m->pm_notifier);
969
024f7f31
IPG
970 result = register_netdev(net_dev); /* Okey dokey, bring it up */
971 if (result < 0) {
972 dev_err(dev, "cannot register i2400m network device: %d\n",
973 result);
974 goto error_register_netdev;
975 }
976 netif_carrier_off(net_dev);
977
024f7f31
IPG
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;
8f90f3ee 981
024f7f31
IPG
982 result = wimax_dev_add(&i2400m->wimax_dev, net_dev);
983 if (result < 0)
984 goto error_wimax_dev_add;
024f7f31
IPG
985
986 /* Now setup all that requires a registered net and wimax device. */
8987691a
IPG
987 result = sysfs_create_group(&net_dev->dev.kobj, &i2400m_dev_attr_group);
988 if (result < 0) {
989 dev_err(dev, "cannot setup i2400m's sysfs: %d\n", result);
990 goto error_sysfs_setup;
991 }
8f90f3ee 992
024f7f31
IPG
993 result = i2400m_debugfs_add(i2400m);
994 if (result < 0) {
995 dev_err(dev, "cannot setup i2400m's debugfs: %d\n", result);
996 goto error_debugfs_setup;
997 }
8f90f3ee
IPG
998
999 result = i2400m_dev_start(i2400m, bm_flags);
1000 if (result < 0)
1001 goto error_dev_start;
024f7f31
IPG
1002 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
1003 return result;
1004
8f90f3ee
IPG
1005error_dev_start:
1006 i2400m_debugfs_rm(i2400m);
024f7f31 1007error_debugfs_setup:
8987691a
IPG
1008 sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
1009 &i2400m_dev_attr_group);
1010error_sysfs_setup:
024f7f31
IPG
1011 wimax_dev_rm(&i2400m->wimax_dev);
1012error_wimax_dev_add:
024f7f31
IPG
1013 unregister_netdev(net_dev);
1014error_register_netdev:
7b43ca70 1015 unregister_pm_notifier(&i2400m->pm_notifier);
024f7f31
IPG
1016error_read_mac_addr:
1017error_bootrom_init:
0856ccf2
IPG
1018 if (i2400m->bus_release)
1019 i2400m->bus_release(i2400m);
1020error_bus_setup:
2869da85
IPG
1021 i2400m_bm_buf_free(i2400m);
1022error_bm_buf_alloc:
024f7f31
IPG
1023 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
1024 return result;
1025}
1026EXPORT_SYMBOL_GPL(i2400m_setup);
1027
1028
1029/**
1030 * i2400m_release - release the bus-generic driver resources
1031 *
1032 * Sends a disconnect message and undoes any setup done by i2400m_setup()
1033 */
1034void i2400m_release(struct i2400m *i2400m)
1035{
1036 struct device *dev = i2400m_dev(i2400m);
1037
1038 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
1039 netif_stop_queue(i2400m->wimax_dev.net_dev);
1040
8f90f3ee
IPG
1041 i2400m_dev_stop(i2400m);
1042
024f7f31 1043 i2400m_debugfs_rm(i2400m);
8987691a
IPG
1044 sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
1045 &i2400m_dev_attr_group);
024f7f31 1046 wimax_dev_rm(&i2400m->wimax_dev);
024f7f31 1047 unregister_netdev(i2400m->wimax_dev.net_dev);
7b43ca70 1048 unregister_pm_notifier(&i2400m->pm_notifier);
0856ccf2
IPG
1049 if (i2400m->bus_release)
1050 i2400m->bus_release(i2400m);
8f90f3ee 1051 i2400m_bm_buf_free(i2400m);
024f7f31
IPG
1052 d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
1053}
1054EXPORT_SYMBOL_GPL(i2400m_release);
1055
1056
1af7ad51
IPG
1057/*
1058 * Debug levels control; see debug.h
1059 */
1060struct 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),
4dc1bf07 1068 D_SUBMODULE_DEFINE(sysfs),
1af7ad51
IPG
1069 D_SUBMODULE_DEFINE(tx),
1070};
1071size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
1072
1073
024f7f31
IPG
1074static
1075int __init i2400m_driver_init(void)
1076{
4c2b1a11
IPG
1077 d_parse_params(D_LEVEL, D_LEVEL_SIZE, i2400m_debug_params,
1078 "i2400m.debug");
aba3792a 1079 return i2400m_barker_db_init(i2400m_barkers_params);
024f7f31
IPG
1080}
1081module_init(i2400m_driver_init);
1082
1083static
1084void __exit i2400m_driver_exit(void)
1085{
1086 /* for scheds i2400m_dev_reset_handle() */
1087 flush_scheduled_work();
aba3792a 1088 i2400m_barker_db_exit();
024f7f31
IPG
1089}
1090module_exit(i2400m_driver_exit);
1091
1092MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
1093MODULE_DESCRIPTION("Intel 2400M WiMAX networking bus-generic driver");
1094MODULE_LICENSE("GPL");
This page took 0.31404 seconds and 5 git commands to generate.