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