wimax: allow specifying debug levels as command line option
[safe/jmp/linux-2.6] / 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_bootrom_init()
45  *   register_netdev()
46  *   i2400m_dev_start()
47  *     __i2400m_dev_start()
48  *       i2400m_dev_bootstrap()
49  *       i2400m_tx_setup()
50  *       i2400m->bus_dev_start()
51  *       i2400m_firmware_check()
52  *       i2400m_check_mac_addr()
53  *   wimax_dev_add()
54  *
55  * i2400m_release()
56  *   wimax_dev_rm()
57  *   i2400m_dev_stop()
58  *     __i2400m_dev_stop()
59  *       i2400m_dev_shutdown()
60  *       i2400m->bus_dev_stop()
61  *       i2400m_tx_release()
62  *   unregister_netdev()
63  */
64 #include "i2400m.h"
65 #include <linux/etherdevice.h>
66 #include <linux/wimax/i2400m.h>
67 #include <linux/module.h>
68 #include <linux/moduleparam.h>
69
70 #define D_SUBMODULE driver
71 #include "debug-levels.h"
72
73
74 int i2400m_idle_mode_disabled;  /* 0 (idle mode enabled) by default */
75 module_param_named(idle_mode_disabled, i2400m_idle_mode_disabled, int, 0644);
76 MODULE_PARM_DESC(idle_mode_disabled,
77                  "If true, the device will not enable idle mode negotiation "
78                  "with the base station (when connected) to save power.");
79
80 int i2400m_rx_reorder_disabled; /* 0 (rx reorder enabled) by default */
81 module_param_named(rx_reorder_disabled, i2400m_rx_reorder_disabled, int, 0644);
82 MODULE_PARM_DESC(rx_reorder_disabled,
83                  "If true, RX reordering will be disabled.");
84
85 int i2400m_power_save_disabled; /* 0 (power saving enabled) by default */
86 module_param_named(power_save_disabled, i2400m_power_save_disabled, int, 0644);
87 MODULE_PARM_DESC(power_save_disabled,
88                  "If true, the driver will not tell the device to enter "
89                  "power saving mode when it reports it is ready for it. "
90                  "False by default (so the device is told to do power "
91                  "saving).");
92
93 static char i2400m_debug_params[128];
94 module_param_string(debug, i2400m_debug_params, sizeof(i2400m_debug_params),
95                     0644);
96 MODULE_PARM_DESC(debug,
97                  "String of space-separated NAME:VALUE pairs, where NAMEs "
98                  "are the different debug submodules and VALUE are the "
99                  "initial debug value to set.");
100
101 /**
102  * i2400m_queue_work - schedule work on a i2400m's queue
103  *
104  * @i2400m: device descriptor
105  *
106  * @fn: function to run to execute work. It gets passed a 'struct
107  *     work_struct' that is wrapped in a 'struct i2400m_work'. Once
108  *     done, you have to (1) i2400m_put(i2400m_work->i2400m) and then
109  *     (2) kfree(i2400m_work).
110  *
111  * @gfp_flags: GFP flags for memory allocation.
112  *
113  * @pl: pointer to a payload buffer that you want to pass to the _work
114  *     function. Use this to pack (for example) a struct with extra
115  *     arguments.
116  *
117  * @pl_size: size of the payload buffer.
118  *
119  * We do this quite often, so this just saves typing; allocate a
120  * wrapper for a i2400m, get a ref to it, pack arguments and launch
121  * the work.
122  *
123  * A usual workflow is:
124  *
125  * struct my_work_args {
126  *         void *something;
127  *         int whatever;
128  * };
129  * ...
130  *
131  * struct my_work_args my_args = {
132  *         .something = FOO,
133  *         .whaetever = BLAH
134  * };
135  * i2400m_queue_work(i2400m, 1, my_work_function, GFP_KERNEL,
136  *                   &args, sizeof(args))
137  *
138  * And now the work function can unpack the arguments and call the
139  * real function (or do the job itself):
140  *
141  * static
142  * void my_work_fn((struct work_struct *ws)
143  * {
144  *         struct i2400m_work *iw =
145  *                 container_of(ws, struct i2400m_work, ws);
146  *         struct my_work_args *my_args = (void *) iw->pl;
147  *
148  *         my_work(iw->i2400m, my_args->something, my_args->whatevert);
149  * }
150  */
151 int i2400m_queue_work(struct i2400m *i2400m,
152                       void (*fn)(struct work_struct *), gfp_t gfp_flags,
153                       const void *pl, size_t pl_size)
154 {
155         int result;
156         struct i2400m_work *iw;
157
158         BUG_ON(i2400m->work_queue == NULL);
159         result = -ENOMEM;
160         iw = kzalloc(sizeof(*iw) + pl_size, gfp_flags);
161         if (iw == NULL)
162                 goto error_kzalloc;
163         iw->i2400m = i2400m_get(i2400m);
164         memcpy(iw->pl, pl, pl_size);
165         INIT_WORK(&iw->ws, fn);
166         result = queue_work(i2400m->work_queue, &iw->ws);
167 error_kzalloc:
168         return result;
169 }
170 EXPORT_SYMBOL_GPL(i2400m_queue_work);
171
172
173 /*
174  * Schedule i2400m's specific work on the system's queue.
175  *
176  * Used for a few cases where we really need it; otherwise, identical
177  * to i2400m_queue_work().
178  *
179  * Returns < 0 errno code on error, 1 if ok.
180  *
181  * If it returns zero, something really bad happened, as it means the
182  * works struct was already queued, but we have just allocated it, so
183  * it should not happen.
184  */
185 int i2400m_schedule_work(struct i2400m *i2400m,
186                          void (*fn)(struct work_struct *), gfp_t gfp_flags)
187 {
188         int result;
189         struct i2400m_work *iw;
190
191         result = -ENOMEM;
192         iw = kzalloc(sizeof(*iw), gfp_flags);
193         if (iw == NULL)
194                 goto error_kzalloc;
195         iw->i2400m = i2400m_get(i2400m);
196         INIT_WORK(&iw->ws, fn);
197         result = schedule_work(&iw->ws);
198         if (result == 0)
199                 result = -ENXIO;
200 error_kzalloc:
201         return result;
202 }
203
204
205 /*
206  * WiMAX stack operation: relay a message from user space
207  *
208  * @wimax_dev: device descriptor
209  * @pipe_name: named pipe the message is for
210  * @msg_buf: pointer to the message bytes
211  * @msg_len: length of the buffer
212  * @genl_info: passed by the generic netlink layer
213  *
214  * The WiMAX stack will call this function when a message was received
215  * from user space.
216  *
217  * For the i2400m, this is an L3L4 message, as specified in
218  * include/linux/wimax/i2400m.h, and thus prefixed with a 'struct
219  * i2400m_l3l4_hdr'. Driver (and device) expect the messages to be
220  * coded in Little Endian.
221  *
222  * This function just verifies that the header declaration and the
223  * payload are consistent and then deals with it, either forwarding it
224  * to the device or procesing it locally.
225  *
226  * In the i2400m, messages are basically commands that will carry an
227  * ack, so we use i2400m_msg_to_dev() and then deliver the ack back to
228  * user space. The rx.c code might intercept the response and use it
229  * to update the driver's state, but then it will pass it on so it can
230  * be relayed back to user space.
231  *
232  * Note that asynchronous events from the device are processed and
233  * sent to user space in rx.c.
234  */
235 static
236 int i2400m_op_msg_from_user(struct wimax_dev *wimax_dev,
237                             const char *pipe_name,
238                             const void *msg_buf, size_t msg_len,
239                             const struct genl_info *genl_info)
240 {
241         int result;
242         struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
243         struct device *dev = i2400m_dev(i2400m);
244         struct sk_buff *ack_skb;
245
246         d_fnstart(4, dev, "(wimax_dev %p [i2400m %p] msg_buf %p "
247                   "msg_len %zu genl_info %p)\n", wimax_dev, i2400m,
248                   msg_buf, msg_len, genl_info);
249         ack_skb = i2400m_msg_to_dev(i2400m, msg_buf, msg_len);
250         result = PTR_ERR(ack_skb);
251         if (IS_ERR(ack_skb))
252                 goto error_msg_to_dev;
253         result = wimax_msg_send(&i2400m->wimax_dev, ack_skb);
254 error_msg_to_dev:
255         d_fnend(4, dev, "(wimax_dev %p [i2400m %p] msg_buf %p msg_len %zu "
256                 "genl_info %p) = %d\n", wimax_dev, i2400m, msg_buf, msg_len,
257                 genl_info, result);
258         return result;
259 }
260
261
262 /*
263  * Context to wait for a reset to finalize
264  */
265 struct i2400m_reset_ctx {
266         struct completion completion;
267         int result;
268 };
269
270
271 /*
272  * WiMAX stack operation: reset a device
273  *
274  * @wimax_dev: device descriptor
275  *
276  * See the documentation for wimax_reset() and wimax_dev->op_reset for
277  * the requirements of this function. The WiMAX stack guarantees
278  * serialization on calls to this function.
279  *
280  * Do a warm reset on the device; if it fails, resort to a cold reset
281  * and return -ENODEV. On successful warm reset, we need to block
282  * until it is complete.
283  *
284  * The bus-driver implementation of reset takes care of falling back
285  * to cold reset if warm fails.
286  */
287 static
288 int i2400m_op_reset(struct wimax_dev *wimax_dev)
289 {
290         int result;
291         struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
292         struct device *dev = i2400m_dev(i2400m);
293         struct i2400m_reset_ctx ctx = {
294                 .completion = COMPLETION_INITIALIZER_ONSTACK(ctx.completion),
295                 .result = 0,
296         };
297
298         d_fnstart(4, dev, "(wimax_dev %p)\n", wimax_dev);
299         mutex_lock(&i2400m->init_mutex);
300         i2400m->reset_ctx = &ctx;
301         mutex_unlock(&i2400m->init_mutex);
302         result = i2400m->bus_reset(i2400m, I2400M_RT_WARM);
303         if (result < 0)
304                 goto out;
305         result = wait_for_completion_timeout(&ctx.completion, 4*HZ);
306         if (result == 0)
307                 result = -ETIMEDOUT;
308         else if (result > 0)
309                 result = ctx.result;
310         /* if result < 0, pass it on */
311         mutex_lock(&i2400m->init_mutex);
312         i2400m->reset_ctx = NULL;
313         mutex_unlock(&i2400m->init_mutex);
314 out:
315         d_fnend(4, dev, "(wimax_dev %p) = %d\n", wimax_dev, result);
316         return result;
317 }
318
319
320 /*
321  * Check the MAC address we got from boot mode is ok
322  *
323  * @i2400m: device descriptor
324  *
325  * Returns: 0 if ok, < 0 errno code on error.
326  */
327 static
328 int i2400m_check_mac_addr(struct i2400m *i2400m)
329 {
330         int result;
331         struct device *dev = i2400m_dev(i2400m);
332         struct sk_buff *skb;
333         const struct i2400m_tlv_detailed_device_info *ddi;
334         struct net_device *net_dev = i2400m->wimax_dev.net_dev;
335         const unsigned char zeromac[ETH_ALEN] = { 0 };
336
337         d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
338         skb = i2400m_get_device_info(i2400m);
339         if (IS_ERR(skb)) {
340                 result = PTR_ERR(skb);
341                 dev_err(dev, "Cannot verify MAC address, error reading: %d\n",
342                         result);
343                 goto error;
344         }
345         /* Extract MAC addresss */
346         ddi = (void *) skb->data;
347         BUILD_BUG_ON(ETH_ALEN != sizeof(ddi->mac_address));
348         d_printf(2, dev, "GET DEVICE INFO: mac addr "
349                  "%02x:%02x:%02x:%02x:%02x:%02x\n",
350                  ddi->mac_address[0], ddi->mac_address[1],
351                  ddi->mac_address[2], ddi->mac_address[3],
352                  ddi->mac_address[4], ddi->mac_address[5]);
353         if (!memcmp(net_dev->perm_addr, ddi->mac_address,
354                    sizeof(ddi->mac_address)))
355                 goto ok;
356         dev_warn(dev, "warning: device reports a different MAC address "
357                  "to that of boot mode's\n");
358         dev_warn(dev, "device reports     %02x:%02x:%02x:%02x:%02x:%02x\n",
359                  ddi->mac_address[0], ddi->mac_address[1],
360                  ddi->mac_address[2], ddi->mac_address[3],
361                  ddi->mac_address[4], ddi->mac_address[5]);
362         dev_warn(dev, "boot mode reported %02x:%02x:%02x:%02x:%02x:%02x\n",
363                  net_dev->perm_addr[0], net_dev->perm_addr[1],
364                  net_dev->perm_addr[2], net_dev->perm_addr[3],
365                  net_dev->perm_addr[4], net_dev->perm_addr[5]);
366         if (!memcmp(zeromac, ddi->mac_address, sizeof(zeromac)))
367                 dev_err(dev, "device reports an invalid MAC address, "
368                         "not updating\n");
369         else {
370                 dev_warn(dev, "updating MAC address\n");
371                 net_dev->addr_len = ETH_ALEN;
372                 memcpy(net_dev->perm_addr, ddi->mac_address, ETH_ALEN);
373                 memcpy(net_dev->dev_addr, ddi->mac_address, ETH_ALEN);
374         }
375 ok:
376         result = 0;
377         kfree_skb(skb);
378 error:
379         d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
380         return result;
381 }
382
383
384 /**
385  * __i2400m_dev_start - Bring up driver communication with the device
386  *
387  * @i2400m: device descriptor
388  * @flags: boot mode flags
389  *
390  * Returns: 0 if ok, < 0 errno code on error.
391  *
392  * Uploads firmware and brings up all the resources needed to be able
393  * to communicate with the device.
394  *
395  * The workqueue has to be setup early, at least before RX handling
396  * (it's only real user for now) so it can process reports as they
397  * arrive. We also want to destroy it if we retry, to make sure it is
398  * flushed...easier like this.
399  *
400  * TX needs to be setup before the bus-specific code (otherwise on
401  * shutdown, the bus-tx code could try to access it).
402  */
403 static
404 int __i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri flags)
405 {
406         int result;
407         struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
408         struct net_device *net_dev = wimax_dev->net_dev;
409         struct device *dev = i2400m_dev(i2400m);
410         int times = i2400m->bus_bm_retries;
411
412         d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
413 retry:
414         result = i2400m_dev_bootstrap(i2400m, flags);
415         if (result < 0) {
416                 dev_err(dev, "cannot bootstrap device: %d\n", result);
417                 goto error_bootstrap;
418         }
419         result = i2400m_tx_setup(i2400m);
420         if (result < 0)
421                 goto error_tx_setup;
422         result = i2400m_rx_setup(i2400m);
423         if (result < 0)
424                 goto error_rx_setup;
425         i2400m->work_queue = create_singlethread_workqueue(wimax_dev->name);
426         if (i2400m->work_queue == NULL) {
427                 result = -ENOMEM;
428                 dev_err(dev, "cannot create workqueue\n");
429                 goto error_create_workqueue;
430         }
431         result = i2400m->bus_dev_start(i2400m);
432         if (result < 0)
433                 goto error_bus_dev_start;
434         result = i2400m_firmware_check(i2400m); /* fw versions ok? */
435         if (result < 0)
436                 goto error_fw_check;
437         /* At this point is ok to send commands to the device */
438         result = i2400m_check_mac_addr(i2400m);
439         if (result < 0)
440                 goto error_check_mac_addr;
441         i2400m->ready = 1;
442         wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
443         result = i2400m_dev_initialize(i2400m);
444         if (result < 0)
445                 goto error_dev_initialize;
446         /* At this point, reports will come for the device and set it
447          * to the right state if it is different than UNINITIALIZED */
448         d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
449                 net_dev, i2400m, result);
450         return result;
451
452 error_dev_initialize:
453 error_check_mac_addr:
454 error_fw_check:
455         i2400m->bus_dev_stop(i2400m);
456 error_bus_dev_start:
457         destroy_workqueue(i2400m->work_queue);
458 error_create_workqueue:
459         i2400m_rx_release(i2400m);
460 error_rx_setup:
461         i2400m_tx_release(i2400m);
462 error_tx_setup:
463 error_bootstrap:
464         if (result == -EL3RST && times-- > 0) {
465                 flags = I2400M_BRI_SOFT|I2400M_BRI_MAC_REINIT;
466                 goto retry;
467         }
468         d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
469                 net_dev, i2400m, result);
470         return result;
471 }
472
473
474 static
475 int i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri bm_flags)
476 {
477         int result;
478         mutex_lock(&i2400m->init_mutex);        /* Well, start the device */
479         result = __i2400m_dev_start(i2400m, bm_flags);
480         if (result >= 0)
481                 i2400m->updown = 1;
482         mutex_unlock(&i2400m->init_mutex);
483         return result;
484 }
485
486
487 /**
488  * i2400m_dev_stop - Tear down driver communication with the device
489  *
490  * @i2400m: device descriptor
491  *
492  * Returns: 0 if ok, < 0 errno code on error.
493  *
494  * Releases all the resources allocated to communicate with the
495  * device. Note we cannot destroy the workqueue earlier as until RX is
496  * fully destroyed, it could still try to schedule jobs.
497  */
498 static
499 void __i2400m_dev_stop(struct i2400m *i2400m)
500 {
501         struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
502         struct device *dev = i2400m_dev(i2400m);
503
504         d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
505         wimax_state_change(wimax_dev, __WIMAX_ST_QUIESCING);
506         i2400m_dev_shutdown(i2400m);
507         i2400m->ready = 0;
508         i2400m->bus_dev_stop(i2400m);
509         destroy_workqueue(i2400m->work_queue);
510         i2400m_rx_release(i2400m);
511         i2400m_tx_release(i2400m);
512         wimax_state_change(wimax_dev, WIMAX_ST_DOWN);
513         d_fnend(3, dev, "(i2400m %p) = 0\n", i2400m);
514 }
515
516
517 /*
518  * Watch out -- we only need to stop if there is a need for it. The
519  * device could have reset itself and failed to come up again (see
520  * _i2400m_dev_reset_handle()).
521  */
522 static
523 void i2400m_dev_stop(struct i2400m *i2400m)
524 {
525         mutex_lock(&i2400m->init_mutex);
526         if (i2400m->updown) {
527                 __i2400m_dev_stop(i2400m);
528                 i2400m->updown = 0;
529         }
530         mutex_unlock(&i2400m->init_mutex);
531 }
532
533
534 /*
535  * The device has rebooted; fix up the device and the driver
536  *
537  * Tear down the driver communication with the device, reload the
538  * firmware and reinitialize the communication with the device.
539  *
540  * If someone calls a reset when the device's firmware is down, in
541  * theory we won't see it because we are not listening. However, just
542  * in case, leave the code to handle it.
543  *
544  * If there is a reset context, use it; this means someone is waiting
545  * for us to tell him when the reset operation is complete and the
546  * device is ready to rock again.
547  *
548  * NOTE: if we are in the process of bringing up or down the
549  *       communication with the device [running i2400m_dev_start() or
550  *       _stop()], don't do anything, let it fail and handle it.
551  *
552  * This function is ran always in a thread context
553  */
554 static
555 void __i2400m_dev_reset_handle(struct work_struct *ws)
556 {
557         int result;
558         struct i2400m_work *iw = container_of(ws, struct i2400m_work, ws);
559         struct i2400m *i2400m = iw->i2400m;
560         struct device *dev = i2400m_dev(i2400m);
561         enum wimax_st wimax_state;
562         struct i2400m_reset_ctx *ctx = i2400m->reset_ctx;
563
564         d_fnstart(3, dev, "(ws %p i2400m %p)\n", ws, i2400m);
565         result = 0;
566         if (mutex_trylock(&i2400m->init_mutex) == 0) {
567                 /* We are still in i2400m_dev_start() [let it fail] or
568                  * i2400m_dev_stop() [we are shutting down anyway, so
569                  * ignore it] or we are resetting somewhere else. */
570                 dev_err(dev, "device rebooted\n");
571                 i2400m_msg_to_dev_cancel_wait(i2400m, -EL3RST);
572                 complete(&i2400m->msg_completion);
573                 goto out;
574         }
575         wimax_state = wimax_state_get(&i2400m->wimax_dev);
576         if (wimax_state < WIMAX_ST_UNINITIALIZED) {
577                 dev_info(dev, "device rebooted: it is down, ignoring\n");
578                 goto out_unlock;        /* ifconfig up/down wasn't called */
579         }
580         dev_err(dev, "device rebooted: reinitializing driver\n");
581         __i2400m_dev_stop(i2400m);
582         i2400m->updown = 0;
583         result = __i2400m_dev_start(i2400m,
584                                     I2400M_BRI_SOFT | I2400M_BRI_MAC_REINIT);
585         if (result < 0) {
586                 dev_err(dev, "device reboot: cannot start the device: %d\n",
587                         result);
588                 result = i2400m->bus_reset(i2400m, I2400M_RT_BUS);
589                 if (result >= 0)
590                         result = -ENODEV;
591         } else
592                 i2400m->updown = 1;
593 out_unlock:
594         if (i2400m->reset_ctx) {
595                 ctx->result = result;
596                 complete(&ctx->completion);
597         }
598         mutex_unlock(&i2400m->init_mutex);
599 out:
600         i2400m_put(i2400m);
601         kfree(iw);
602         d_fnend(3, dev, "(ws %p i2400m %p) = void\n", ws, i2400m);
603         return;
604 }
605
606
607 /**
608  * i2400m_dev_reset_handle - Handle a device's reset in a thread context
609  *
610  * Schedule a device reset handling out on a thread context, so it
611  * is safe to call from atomic context. We can't use the i2400m's
612  * queue as we are going to destroy it and reinitialize it as part of
613  * the driver bringup/bringup process.
614  *
615  * See __i2400m_dev_reset_handle() for details; that takes care of
616  * reinitializing the driver to handle the reset, calling into the
617  * bus-specific functions ops as needed.
618  */
619 int i2400m_dev_reset_handle(struct i2400m *i2400m)
620 {
621         i2400m->boot_mode = 1;
622         wmb();          /* Make sure i2400m_msg_to_dev() sees boot_mode */
623         return i2400m_schedule_work(i2400m, __i2400m_dev_reset_handle,
624                                     GFP_ATOMIC);
625 }
626 EXPORT_SYMBOL_GPL(i2400m_dev_reset_handle);
627
628
629 /**
630  * i2400m_bm_buf_alloc - Alloc the command and ack buffers for boot mode
631  *
632  * Get the buffers needed to deal with boot mode messages.  These
633  * buffers need to be allocated before the sdio recieve irq is setup.
634  */
635 int i2400m_bm_buf_alloc(struct i2400m *i2400m)
636 {
637         int result;
638
639         result = -ENOMEM;
640         i2400m->bm_cmd_buf = kzalloc(I2400M_BM_CMD_BUF_SIZE, GFP_KERNEL);
641         if (i2400m->bm_cmd_buf == NULL)
642                 goto error_bm_cmd_kzalloc;
643         i2400m->bm_ack_buf = kzalloc(I2400M_BM_ACK_BUF_SIZE, GFP_KERNEL);
644         if (i2400m->bm_ack_buf == NULL)
645                 goto error_bm_ack_buf_kzalloc;
646         return 0;
647
648 error_bm_ack_buf_kzalloc:
649         kfree(i2400m->bm_cmd_buf);
650 error_bm_cmd_kzalloc:
651         return result;
652 }
653 EXPORT_SYMBOL_GPL(i2400m_bm_buf_alloc);
654
655 /**
656  * i2400m_bm_buf_free - Free boot mode command and ack buffers.
657  *
658  * Free the command and ack buffers
659  *
660  */
661 void i2400m_bm_buf_free(struct i2400m *i2400m)
662 {
663         kfree(i2400m->bm_ack_buf);
664         kfree(i2400m->bm_cmd_buf);
665         return;
666 }
667 EXPORT_SYMBOL_GPL(i2400m_bm_buf_free
668 );
669 /**
670  * i2400m_setup - bus-generic setup function for the i2400m device
671  *
672  * @i2400m: device descriptor (bus-specific parts have been initialized)
673  *
674  * Returns: 0 if ok, < 0 errno code on error.
675  *
676  * Initializes the bus-generic parts of the i2400m driver; the
677  * bus-specific parts have been initialized, function pointers filled
678  * out by the bus-specific probe function.
679  *
680  * As well, this registers the WiMAX and net device nodes. Once this
681  * function returns, the device is operative and has to be ready to
682  * receive and send network traffic and WiMAX control operations.
683  */
684 int i2400m_setup(struct i2400m *i2400m, enum i2400m_bri bm_flags)
685 {
686         int result = -ENODEV;
687         struct device *dev = i2400m_dev(i2400m);
688         struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
689         struct net_device *net_dev = i2400m->wimax_dev.net_dev;
690
691         d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
692
693         snprintf(wimax_dev->name, sizeof(wimax_dev->name),
694                  "i2400m-%s:%s", dev->bus->name, dev_name(dev));
695
696         result = i2400m_bootrom_init(i2400m, bm_flags);
697         if (result < 0) {
698                 dev_err(dev, "read mac addr: bootrom init "
699                         "failed: %d\n", result);
700                 goto error_bootrom_init;
701         }
702         result = i2400m_read_mac_addr(i2400m);
703         if (result < 0)
704                 goto error_read_mac_addr;
705         random_ether_addr(i2400m->src_mac_addr);
706
707         result = register_netdev(net_dev);      /* Okey dokey, bring it up */
708         if (result < 0) {
709                 dev_err(dev, "cannot register i2400m network device: %d\n",
710                         result);
711                 goto error_register_netdev;
712         }
713         netif_carrier_off(net_dev);
714
715         result = i2400m_dev_start(i2400m, bm_flags);
716         if (result < 0)
717                 goto error_dev_start;
718
719         i2400m->wimax_dev.op_msg_from_user = i2400m_op_msg_from_user;
720         i2400m->wimax_dev.op_rfkill_sw_toggle = i2400m_op_rfkill_sw_toggle;
721         i2400m->wimax_dev.op_reset = i2400m_op_reset;
722         result = wimax_dev_add(&i2400m->wimax_dev, net_dev);
723         if (result < 0)
724                 goto error_wimax_dev_add;
725         /* User space needs to do some init stuff */
726         wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
727
728         /* Now setup all that requires a registered net and wimax device. */
729         result = sysfs_create_group(&net_dev->dev.kobj, &i2400m_dev_attr_group);
730         if (result < 0) {
731                 dev_err(dev, "cannot setup i2400m's sysfs: %d\n", result);
732                 goto error_sysfs_setup;
733         }
734         result = i2400m_debugfs_add(i2400m);
735         if (result < 0) {
736                 dev_err(dev, "cannot setup i2400m's debugfs: %d\n", result);
737                 goto error_debugfs_setup;
738         }
739         d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
740         return result;
741
742 error_debugfs_setup:
743         sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
744                            &i2400m_dev_attr_group);
745 error_sysfs_setup:
746         wimax_dev_rm(&i2400m->wimax_dev);
747 error_wimax_dev_add:
748         i2400m_dev_stop(i2400m);
749 error_dev_start:
750         unregister_netdev(net_dev);
751 error_register_netdev:
752 error_read_mac_addr:
753 error_bootrom_init:
754         d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
755         return result;
756 }
757 EXPORT_SYMBOL_GPL(i2400m_setup);
758
759
760 /**
761  * i2400m_release - release the bus-generic driver resources
762  *
763  * Sends a disconnect message and undoes any setup done by i2400m_setup()
764  */
765 void i2400m_release(struct i2400m *i2400m)
766 {
767         struct device *dev = i2400m_dev(i2400m);
768
769         d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
770         netif_stop_queue(i2400m->wimax_dev.net_dev);
771
772         i2400m_debugfs_rm(i2400m);
773         sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
774                            &i2400m_dev_attr_group);
775         wimax_dev_rm(&i2400m->wimax_dev);
776         i2400m_dev_stop(i2400m);
777         unregister_netdev(i2400m->wimax_dev.net_dev);
778         kfree(i2400m->bm_ack_buf);
779         kfree(i2400m->bm_cmd_buf);
780         d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
781 }
782 EXPORT_SYMBOL_GPL(i2400m_release);
783
784
785 /*
786  * Debug levels control; see debug.h
787  */
788 struct d_level D_LEVEL[] = {
789         D_SUBMODULE_DEFINE(control),
790         D_SUBMODULE_DEFINE(driver),
791         D_SUBMODULE_DEFINE(debugfs),
792         D_SUBMODULE_DEFINE(fw),
793         D_SUBMODULE_DEFINE(netdev),
794         D_SUBMODULE_DEFINE(rfkill),
795         D_SUBMODULE_DEFINE(rx),
796         D_SUBMODULE_DEFINE(sysfs),
797         D_SUBMODULE_DEFINE(tx),
798 };
799 size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
800
801
802 static
803 int __init i2400m_driver_init(void)
804 {
805         d_parse_params(D_LEVEL, D_LEVEL_SIZE, i2400m_debug_params,
806                        "i2400m.debug");
807         return 0;
808 }
809 module_init(i2400m_driver_init);
810
811 static
812 void __exit i2400m_driver_exit(void)
813 {
814         /* for scheds i2400m_dev_reset_handle() */
815         flush_scheduled_work();
816         return;
817 }
818 module_exit(i2400m_driver_exit);
819
820 MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
821 MODULE_DESCRIPTION("Intel 2400M WiMAX networking bus-generic driver");
822 MODULE_LICENSE("GPL");