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