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