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