blob: a0ae19966c0c919778214ae03327c4767778b72c [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>
Inaky Perez-Gonzalez7b43ca72009-09-14 14:10:16 -070069#include <linux/suspend.h>
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -080070
71#define D_SUBMODULE driver
72#include "debug-levels.h"
73
74
75int i2400m_idle_mode_disabled; /* 0 (idle mode enabled) by default */
76module_param_named(idle_mode_disabled, i2400m_idle_mode_disabled, int, 0644);
77MODULE_PARM_DESC(idle_mode_disabled,
78 "If true, the device will not enable idle mode negotiation "
79 "with the base station (when connected) to save power.");
80
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +000081int i2400m_rx_reorder_disabled; /* 0 (rx reorder enabled) by default */
82module_param_named(rx_reorder_disabled, i2400m_rx_reorder_disabled, int, 0644);
83MODULE_PARM_DESC(rx_reorder_disabled,
84 "If true, RX reordering will be disabled.");
85
Inaky Perez-Gonzalezfb101672009-05-07 10:27:42 -070086int i2400m_power_save_disabled; /* 0 (power saving enabled) by default */
87module_param_named(power_save_disabled, i2400m_power_save_disabled, int, 0644);
88MODULE_PARM_DESC(power_save_disabled,
89 "If true, the driver will not tell the device to enter "
90 "power saving mode when it reports it is ready for it. "
91 "False by default (so the device is told to do power "
92 "saving).");
93
Inaky Perez-Gonzalez4c2b1a12009-09-02 15:36:05 -070094static char i2400m_debug_params[128];
95module_param_string(debug, i2400m_debug_params, sizeof(i2400m_debug_params),
96 0644);
97MODULE_PARM_DESC(debug,
98 "String of space-separated NAME:VALUE pairs, where NAMEs "
99 "are the different debug submodules and VALUE are the "
100 "initial debug value to set.");
101
Inaky Perez-Gonzalezaba3792a2009-09-03 15:14:29 -0700102static char i2400m_barkers_params[128];
103module_param_string(barkers, i2400m_barkers_params,
104 sizeof(i2400m_barkers_params), 0644);
105MODULE_PARM_DESC(barkers,
106 "String of comma-separated 32-bit values; each is "
107 "recognized as the value the device sends as a reboot "
108 "signal; values are appended to a list--setting one value "
109 "as zero cleans the existing list and starts a new one.");
110
Inaky Perez-Gonzalezb0fbcb22009-09-14 13:29:32 -0700111static
112struct i2400m_work *__i2400m_work_setup(
113 struct i2400m *i2400m, void (*fn)(struct work_struct *),
114 gfp_t gfp_flags, const void *pl, size_t pl_size)
115{
116 struct i2400m_work *iw;
117
118 iw = kzalloc(sizeof(*iw) + pl_size, gfp_flags);
119 if (iw == NULL)
120 return NULL;
121 iw->i2400m = i2400m_get(i2400m);
122 iw->pl_size = pl_size;
123 memcpy(iw->pl, pl, pl_size);
124 INIT_WORK(&iw->ws, fn);
125 return iw;
126}
127
128
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800129/**
130 * i2400m_queue_work - schedule work on a i2400m's queue
131 *
132 * @i2400m: device descriptor
133 *
134 * @fn: function to run to execute work. It gets passed a 'struct
135 * work_struct' that is wrapped in a 'struct i2400m_work'. Once
136 * done, you have to (1) i2400m_put(i2400m_work->i2400m) and then
137 * (2) kfree(i2400m_work).
138 *
139 * @gfp_flags: GFP flags for memory allocation.
140 *
141 * @pl: pointer to a payload buffer that you want to pass to the _work
142 * function. Use this to pack (for example) a struct with extra
143 * arguments.
144 *
145 * @pl_size: size of the payload buffer.
146 *
147 * We do this quite often, so this just saves typing; allocate a
148 * wrapper for a i2400m, get a ref to it, pack arguments and launch
149 * the work.
150 *
151 * A usual workflow is:
152 *
153 * struct my_work_args {
154 * void *something;
155 * int whatever;
156 * };
157 * ...
158 *
159 * struct my_work_args my_args = {
160 * .something = FOO,
161 * .whaetever = BLAH
162 * };
163 * i2400m_queue_work(i2400m, 1, my_work_function, GFP_KERNEL,
164 * &args, sizeof(args))
165 *
166 * And now the work function can unpack the arguments and call the
167 * real function (or do the job itself):
168 *
169 * static
170 * void my_work_fn((struct work_struct *ws)
171 * {
172 * struct i2400m_work *iw =
173 * container_of(ws, struct i2400m_work, ws);
174 * struct my_work_args *my_args = (void *) iw->pl;
175 *
176 * my_work(iw->i2400m, my_args->something, my_args->whatevert);
177 * }
178 */
179int i2400m_queue_work(struct i2400m *i2400m,
180 void (*fn)(struct work_struct *), gfp_t gfp_flags,
181 const void *pl, size_t pl_size)
182{
183 int result;
184 struct i2400m_work *iw;
185
186 BUG_ON(i2400m->work_queue == NULL);
187 result = -ENOMEM;
Inaky Perez-Gonzalezb0fbcb22009-09-14 13:29:32 -0700188 iw = __i2400m_work_setup(i2400m, fn, gfp_flags, pl, pl_size);
189 if (iw != NULL) {
190 result = queue_work(i2400m->work_queue, &iw->ws);
191 if (WARN_ON(result == 0))
192 result = -ENXIO;
193 }
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800194 return result;
195}
196EXPORT_SYMBOL_GPL(i2400m_queue_work);
197
198
199/*
200 * Schedule i2400m's specific work on the system's queue.
201 *
202 * Used for a few cases where we really need it; otherwise, identical
203 * to i2400m_queue_work().
204 *
205 * Returns < 0 errno code on error, 1 if ok.
206 *
207 * If it returns zero, something really bad happened, as it means the
208 * works struct was already queued, but we have just allocated it, so
209 * it should not happen.
210 */
211int i2400m_schedule_work(struct i2400m *i2400m,
Inaky Perez-Gonzalezb0fbcb22009-09-14 13:29:32 -0700212 void (*fn)(struct work_struct *), gfp_t gfp_flags,
213 const void *pl, size_t pl_size)
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800214{
215 int result;
216 struct i2400m_work *iw;
217
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800218 result = -ENOMEM;
Inaky Perez-Gonzalezb0fbcb22009-09-14 13:29:32 -0700219 iw = __i2400m_work_setup(i2400m, fn, gfp_flags, pl, pl_size);
220 if (iw != NULL) {
221 result = schedule_work(&iw->ws);
222 if (WARN_ON(result == 0))
223 result = -ENXIO;
224 }
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800225 return result;
226}
227
228
229/*
230 * WiMAX stack operation: relay a message from user space
231 *
232 * @wimax_dev: device descriptor
233 * @pipe_name: named pipe the message is for
234 * @msg_buf: pointer to the message bytes
235 * @msg_len: length of the buffer
236 * @genl_info: passed by the generic netlink layer
237 *
238 * The WiMAX stack will call this function when a message was received
239 * from user space.
240 *
241 * For the i2400m, this is an L3L4 message, as specified in
242 * include/linux/wimax/i2400m.h, and thus prefixed with a 'struct
243 * i2400m_l3l4_hdr'. Driver (and device) expect the messages to be
244 * coded in Little Endian.
245 *
246 * This function just verifies that the header declaration and the
247 * payload are consistent and then deals with it, either forwarding it
248 * to the device or procesing it locally.
249 *
250 * In the i2400m, messages are basically commands that will carry an
251 * ack, so we use i2400m_msg_to_dev() and then deliver the ack back to
252 * user space. The rx.c code might intercept the response and use it
253 * to update the driver's state, but then it will pass it on so it can
254 * be relayed back to user space.
255 *
256 * Note that asynchronous events from the device are processed and
257 * sent to user space in rx.c.
258 */
259static
260int i2400m_op_msg_from_user(struct wimax_dev *wimax_dev,
261 const char *pipe_name,
262 const void *msg_buf, size_t msg_len,
263 const struct genl_info *genl_info)
264{
265 int result;
266 struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
267 struct device *dev = i2400m_dev(i2400m);
268 struct sk_buff *ack_skb;
269
270 d_fnstart(4, dev, "(wimax_dev %p [i2400m %p] msg_buf %p "
271 "msg_len %zu genl_info %p)\n", wimax_dev, i2400m,
272 msg_buf, msg_len, genl_info);
273 ack_skb = i2400m_msg_to_dev(i2400m, msg_buf, msg_len);
274 result = PTR_ERR(ack_skb);
275 if (IS_ERR(ack_skb))
276 goto error_msg_to_dev;
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800277 result = wimax_msg_send(&i2400m->wimax_dev, ack_skb);
278error_msg_to_dev:
279 d_fnend(4, dev, "(wimax_dev %p [i2400m %p] msg_buf %p msg_len %zu "
280 "genl_info %p) = %d\n", wimax_dev, i2400m, msg_buf, msg_len,
281 genl_info, result);
282 return result;
283}
284
285
286/*
287 * Context to wait for a reset to finalize
288 */
289struct i2400m_reset_ctx {
290 struct completion completion;
291 int result;
292};
293
294
295/*
296 * WiMAX stack operation: reset a device
297 *
298 * @wimax_dev: device descriptor
299 *
300 * See the documentation for wimax_reset() and wimax_dev->op_reset for
301 * the requirements of this function. The WiMAX stack guarantees
302 * serialization on calls to this function.
303 *
304 * Do a warm reset on the device; if it fails, resort to a cold reset
305 * and return -ENODEV. On successful warm reset, we need to block
306 * until it is complete.
307 *
308 * The bus-driver implementation of reset takes care of falling back
309 * to cold reset if warm fails.
310 */
311static
312int i2400m_op_reset(struct wimax_dev *wimax_dev)
313{
314 int result;
315 struct i2400m *i2400m = wimax_dev_to_i2400m(wimax_dev);
316 struct device *dev = i2400m_dev(i2400m);
317 struct i2400m_reset_ctx ctx = {
318 .completion = COMPLETION_INITIALIZER_ONSTACK(ctx.completion),
319 .result = 0,
320 };
321
322 d_fnstart(4, dev, "(wimax_dev %p)\n", wimax_dev);
323 mutex_lock(&i2400m->init_mutex);
324 i2400m->reset_ctx = &ctx;
325 mutex_unlock(&i2400m->init_mutex);
326 result = i2400m->bus_reset(i2400m, I2400M_RT_WARM);
327 if (result < 0)
328 goto out;
329 result = wait_for_completion_timeout(&ctx.completion, 4*HZ);
330 if (result == 0)
331 result = -ETIMEDOUT;
332 else if (result > 0)
333 result = ctx.result;
334 /* if result < 0, pass it on */
335 mutex_lock(&i2400m->init_mutex);
336 i2400m->reset_ctx = NULL;
337 mutex_unlock(&i2400m->init_mutex);
338out:
339 d_fnend(4, dev, "(wimax_dev %p) = %d\n", wimax_dev, result);
340 return result;
341}
342
343
344/*
345 * Check the MAC address we got from boot mode is ok
346 *
347 * @i2400m: device descriptor
348 *
349 * Returns: 0 if ok, < 0 errno code on error.
350 */
351static
352int i2400m_check_mac_addr(struct i2400m *i2400m)
353{
354 int result;
355 struct device *dev = i2400m_dev(i2400m);
356 struct sk_buff *skb;
357 const struct i2400m_tlv_detailed_device_info *ddi;
358 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
359 const unsigned char zeromac[ETH_ALEN] = { 0 };
360
361 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
362 skb = i2400m_get_device_info(i2400m);
363 if (IS_ERR(skb)) {
364 result = PTR_ERR(skb);
365 dev_err(dev, "Cannot verify MAC address, error reading: %d\n",
366 result);
367 goto error;
368 }
369 /* Extract MAC addresss */
370 ddi = (void *) skb->data;
371 BUILD_BUG_ON(ETH_ALEN != sizeof(ddi->mac_address));
372 d_printf(2, dev, "GET DEVICE INFO: mac addr "
373 "%02x:%02x:%02x:%02x:%02x:%02x\n",
374 ddi->mac_address[0], ddi->mac_address[1],
375 ddi->mac_address[2], ddi->mac_address[3],
376 ddi->mac_address[4], ddi->mac_address[5]);
377 if (!memcmp(net_dev->perm_addr, ddi->mac_address,
378 sizeof(ddi->mac_address)))
379 goto ok;
380 dev_warn(dev, "warning: device reports a different MAC address "
381 "to that of boot mode's\n");
382 dev_warn(dev, "device reports %02x:%02x:%02x:%02x:%02x:%02x\n",
383 ddi->mac_address[0], ddi->mac_address[1],
384 ddi->mac_address[2], ddi->mac_address[3],
385 ddi->mac_address[4], ddi->mac_address[5]);
386 dev_warn(dev, "boot mode reported %02x:%02x:%02x:%02x:%02x:%02x\n",
387 net_dev->perm_addr[0], net_dev->perm_addr[1],
388 net_dev->perm_addr[2], net_dev->perm_addr[3],
389 net_dev->perm_addr[4], net_dev->perm_addr[5]);
390 if (!memcmp(zeromac, ddi->mac_address, sizeof(zeromac)))
391 dev_err(dev, "device reports an invalid MAC address, "
392 "not updating\n");
393 else {
394 dev_warn(dev, "updating MAC address\n");
395 net_dev->addr_len = ETH_ALEN;
396 memcpy(net_dev->perm_addr, ddi->mac_address, ETH_ALEN);
397 memcpy(net_dev->dev_addr, ddi->mac_address, ETH_ALEN);
398 }
399ok:
400 result = 0;
401 kfree_skb(skb);
402error:
403 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
404 return result;
405}
406
407
408/**
409 * __i2400m_dev_start - Bring up driver communication with the device
410 *
411 * @i2400m: device descriptor
412 * @flags: boot mode flags
413 *
414 * Returns: 0 if ok, < 0 errno code on error.
415 *
416 * Uploads firmware and brings up all the resources needed to be able
417 * to communicate with the device.
418 *
Inaky Perez-Gonzaleze9a6b452009-05-08 13:02:41 -0700419 * The workqueue has to be setup early, at least before RX handling
420 * (it's only real user for now) so it can process reports as they
421 * arrive. We also want to destroy it if we retry, to make sure it is
422 * flushed...easier like this.
423 *
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800424 * TX needs to be setup before the bus-specific code (otherwise on
425 * shutdown, the bus-tx code could try to access it).
426 */
427static
428int __i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri flags)
429{
430 int result;
431 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
432 struct net_device *net_dev = wimax_dev->net_dev;
433 struct device *dev = i2400m_dev(i2400m);
Inaky Perez-Gonzalezecddfd52009-06-03 16:13:14 +0800434 int times = i2400m->bus_bm_retries;
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800435
436 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
437retry:
438 result = i2400m_dev_bootstrap(i2400m, flags);
439 if (result < 0) {
440 dev_err(dev, "cannot bootstrap device: %d\n", result);
441 goto error_bootstrap;
442 }
443 result = i2400m_tx_setup(i2400m);
444 if (result < 0)
445 goto error_tx_setup;
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000446 result = i2400m_rx_setup(i2400m);
447 if (result < 0)
448 goto error_rx_setup;
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800449 i2400m->work_queue = create_singlethread_workqueue(wimax_dev->name);
450 if (i2400m->work_queue == NULL) {
451 result = -ENOMEM;
452 dev_err(dev, "cannot create workqueue\n");
453 goto error_create_workqueue;
454 }
Inaky Perez-Gonzaleze9a6b452009-05-08 13:02:41 -0700455 result = i2400m->bus_dev_start(i2400m);
456 if (result < 0)
457 goto error_bus_dev_start;
Inaky Perez-Gonzalez6a0f7ab2009-02-28 23:42:49 +0000458 result = i2400m_firmware_check(i2400m); /* fw versions ok? */
459 if (result < 0)
460 goto error_fw_check;
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800461 /* At this point is ok to send commands to the device */
462 result = i2400m_check_mac_addr(i2400m);
463 if (result < 0)
464 goto error_check_mac_addr;
465 i2400m->ready = 1;
466 wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
467 result = i2400m_dev_initialize(i2400m);
468 if (result < 0)
469 goto error_dev_initialize;
470 /* At this point, reports will come for the device and set it
471 * to the right state if it is different than UNINITIALIZED */
472 d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
473 net_dev, i2400m, result);
474 return result;
475
476error_dev_initialize:
477error_check_mac_addr:
Inaky Perez-Gonzalez6a0f7ab2009-02-28 23:42:49 +0000478error_fw_check:
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800479 i2400m->bus_dev_stop(i2400m);
480error_bus_dev_start:
Inaky Perez-Gonzaleze9a6b452009-05-08 13:02:41 -0700481 destroy_workqueue(i2400m->work_queue);
482error_create_workqueue:
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000483 i2400m_rx_release(i2400m);
484error_rx_setup:
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800485 i2400m_tx_release(i2400m);
486error_tx_setup:
487error_bootstrap:
Cindy H Kao0bcfc5e2009-06-10 17:06:19 -0700488 if (result == -EL3RST && times-- > 0) {
Cindy H Kao8b5b30ee2009-06-10 16:52:10 -0700489 flags = I2400M_BRI_SOFT|I2400M_BRI_MAC_REINIT;
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800490 goto retry;
491 }
492 d_fnend(3, dev, "(net_dev %p [i2400m %p]) = %d\n",
493 net_dev, i2400m, result);
494 return result;
495}
496
497
498static
499int i2400m_dev_start(struct i2400m *i2400m, enum i2400m_bri bm_flags)
500{
501 int result;
502 mutex_lock(&i2400m->init_mutex); /* Well, start the device */
503 result = __i2400m_dev_start(i2400m, bm_flags);
504 if (result >= 0)
505 i2400m->updown = 1;
506 mutex_unlock(&i2400m->init_mutex);
507 return result;
508}
509
510
511/**
512 * i2400m_dev_stop - Tear down driver communication with the device
513 *
514 * @i2400m: device descriptor
515 *
516 * Returns: 0 if ok, < 0 errno code on error.
517 *
Inaky Perez-Gonzaleze9a6b452009-05-08 13:02:41 -0700518 * Releases all the resources allocated to communicate with the
519 * device. Note we cannot destroy the workqueue earlier as until RX is
520 * fully destroyed, it could still try to schedule jobs.
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800521 */
522static
523void __i2400m_dev_stop(struct i2400m *i2400m)
524{
525 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
526 struct device *dev = i2400m_dev(i2400m);
527
528 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
529 wimax_state_change(wimax_dev, __WIMAX_ST_QUIESCING);
Inaky Perez-Gonzalezac53aed2009-09-16 16:30:39 -0700530 i2400m_net_wake_stop(i2400m);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800531 i2400m_dev_shutdown(i2400m);
532 i2400m->ready = 0;
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800533 i2400m->bus_dev_stop(i2400m);
Inaky Perez-Gonzaleze9a6b452009-05-08 13:02:41 -0700534 destroy_workqueue(i2400m->work_queue);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000535 i2400m_rx_release(i2400m);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800536 i2400m_tx_release(i2400m);
537 wimax_state_change(wimax_dev, WIMAX_ST_DOWN);
538 d_fnend(3, dev, "(i2400m %p) = 0\n", i2400m);
539}
540
541
542/*
543 * Watch out -- we only need to stop if there is a need for it. The
544 * device could have reset itself and failed to come up again (see
545 * _i2400m_dev_reset_handle()).
546 */
547static
548void i2400m_dev_stop(struct i2400m *i2400m)
549{
550 mutex_lock(&i2400m->init_mutex);
551 if (i2400m->updown) {
552 __i2400m_dev_stop(i2400m);
553 i2400m->updown = 0;
554 }
555 mutex_unlock(&i2400m->init_mutex);
556}
557
558
559/*
Inaky Perez-Gonzalez7b43ca72009-09-14 14:10:16 -0700560 * Listen to PM events to cache the firmware before suspend/hibernation
561 *
562 * When the device comes out of suspend, it might go into reset and
563 * firmware has to be uploaded again. At resume, most of the times, we
564 * can't load firmware images from disk, so we need to cache it.
565 *
566 * i2400m_fw_cache() will allocate a kobject and attach the firmware
567 * to it; that way we don't have to worry too much about the fw loader
568 * hitting a race condition.
569 *
570 * Note: modus operandi stolen from the Orinoco driver; thx.
571 */
572static
573int i2400m_pm_notifier(struct notifier_block *notifier,
574 unsigned long pm_event,
575 void *unused)
576{
577 struct i2400m *i2400m =
578 container_of(notifier, struct i2400m, pm_notifier);
579 struct device *dev = i2400m_dev(i2400m);
580
581 d_fnstart(3, dev, "(i2400m %p pm_event %lx)\n", i2400m, pm_event);
582 switch (pm_event) {
583 case PM_HIBERNATION_PREPARE:
584 case PM_SUSPEND_PREPARE:
585 i2400m_fw_cache(i2400m);
586 break;
587 case PM_POST_RESTORE:
588 /* Restore from hibernation failed. We need to clean
589 * up in exactly the same way, so fall through. */
590 case PM_POST_HIBERNATION:
591 case PM_POST_SUSPEND:
592 i2400m_fw_uncache(i2400m);
593 break;
594
595 case PM_RESTORE_PREPARE:
596 default:
597 break;
598 }
599 d_fnend(3, dev, "(i2400m %p pm_event %lx) = void\n", i2400m, pm_event);
600 return NOTIFY_DONE;
601}
602
603
604/*
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800605 * The device has rebooted; fix up the device and the driver
606 *
607 * Tear down the driver communication with the device, reload the
608 * firmware and reinitialize the communication with the device.
609 *
610 * If someone calls a reset when the device's firmware is down, in
611 * theory we won't see it because we are not listening. However, just
612 * in case, leave the code to handle it.
613 *
614 * If there is a reset context, use it; this means someone is waiting
615 * for us to tell him when the reset operation is complete and the
616 * device is ready to rock again.
617 *
618 * NOTE: if we are in the process of bringing up or down the
619 * communication with the device [running i2400m_dev_start() or
620 * _stop()], don't do anything, let it fail and handle it.
621 *
622 * This function is ran always in a thread context
Inaky Perez-Gonzalez3ef61292009-09-14 14:05:19 -0700623 *
624 * This function gets passed, as payload to i2400m_work() a 'const
625 * char *' ptr with a "reason" why the reset happened (for messages).
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800626 */
627static
628void __i2400m_dev_reset_handle(struct work_struct *ws)
629{
630 int result;
631 struct i2400m_work *iw = container_of(ws, struct i2400m_work, ws);
Inaky Perez-Gonzalez3ef61292009-09-14 14:05:19 -0700632 const char *reason;
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800633 struct i2400m *i2400m = iw->i2400m;
634 struct device *dev = i2400m_dev(i2400m);
635 enum wimax_st wimax_state;
636 struct i2400m_reset_ctx *ctx = i2400m->reset_ctx;
637
Inaky Perez-Gonzalez3ef61292009-09-14 14:05:19 -0700638 if (WARN_ON(iw->pl_size != sizeof(reason)))
639 reason = "SW BUG: reason n/a";
640 else
641 memcpy(&reason, iw->pl, sizeof(reason));
642
643 d_fnstart(3, dev, "(ws %p i2400m %p reason %s)\n", ws, i2400m, reason);
644
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800645 result = 0;
646 if (mutex_trylock(&i2400m->init_mutex) == 0) {
647 /* We are still in i2400m_dev_start() [let it fail] or
648 * i2400m_dev_stop() [we are shutting down anyway, so
649 * ignore it] or we are resetting somewhere else. */
650 dev_err(dev, "device rebooted\n");
Cindy H Kao0bcfc5e2009-06-10 17:06:19 -0700651 i2400m_msg_to_dev_cancel_wait(i2400m, -EL3RST);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800652 complete(&i2400m->msg_completion);
653 goto out;
654 }
655 wimax_state = wimax_state_get(&i2400m->wimax_dev);
656 if (wimax_state < WIMAX_ST_UNINITIALIZED) {
Inaky Perez-Gonzalez3ef61292009-09-14 14:05:19 -0700657 dev_info(dev, "%s: it is down, ignoring\n", reason);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800658 goto out_unlock; /* ifconfig up/down wasn't called */
659 }
Inaky Perez-Gonzalez3ef61292009-09-14 14:05:19 -0700660 dev_err(dev, "%s: reinitializing driver\n", reason);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800661 __i2400m_dev_stop(i2400m);
662 i2400m->updown = 0;
663 result = __i2400m_dev_start(i2400m,
664 I2400M_BRI_SOFT | I2400M_BRI_MAC_REINIT);
665 if (result < 0) {
Inaky Perez-Gonzalez3ef61292009-09-14 14:05:19 -0700666 dev_err(dev, "%s: cannot start the device: %d\n",
667 reason, result);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800668 result = i2400m->bus_reset(i2400m, I2400M_RT_BUS);
669 if (result >= 0)
670 result = -ENODEV;
671 } else
672 i2400m->updown = 1;
673out_unlock:
674 if (i2400m->reset_ctx) {
675 ctx->result = result;
676 complete(&ctx->completion);
677 }
678 mutex_unlock(&i2400m->init_mutex);
679out:
680 i2400m_put(i2400m);
681 kfree(iw);
Inaky Perez-Gonzalez3ef61292009-09-14 14:05:19 -0700682 d_fnend(3, dev, "(ws %p i2400m %p reason %s) = void\n",
683 ws, i2400m, reason);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800684 return;
685}
686
687
688/**
689 * i2400m_dev_reset_handle - Handle a device's reset in a thread context
690 *
691 * Schedule a device reset handling out on a thread context, so it
692 * is safe to call from atomic context. We can't use the i2400m's
693 * queue as we are going to destroy it and reinitialize it as part of
694 * the driver bringup/bringup process.
695 *
696 * See __i2400m_dev_reset_handle() for details; that takes care of
697 * reinitializing the driver to handle the reset, calling into the
698 * bus-specific functions ops as needed.
699 */
Inaky Perez-Gonzalez3ef61292009-09-14 14:05:19 -0700700int i2400m_dev_reset_handle(struct i2400m *i2400m, const char *reason)
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800701{
Inaky Perez-Gonzalezb4013f92009-06-03 09:45:55 +0800702 i2400m->boot_mode = 1;
703 wmb(); /* Make sure i2400m_msg_to_dev() sees boot_mode */
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800704 return i2400m_schedule_work(i2400m, __i2400m_dev_reset_handle,
Inaky Perez-Gonzalez3ef61292009-09-14 14:05:19 -0700705 GFP_ATOMIC, &reason, sizeof(reason));
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800706}
707EXPORT_SYMBOL_GPL(i2400m_dev_reset_handle);
708
709
710/**
Dirk Brandewiea134fd62009-08-18 08:51:52 -0700711 * i2400m_bm_buf_alloc - Alloc the command and ack buffers for boot mode
712 *
713 * Get the buffers needed to deal with boot mode messages. These
714 * buffers need to be allocated before the sdio recieve irq is setup.
715 */
716int i2400m_bm_buf_alloc(struct i2400m *i2400m)
717{
718 int result;
719
720 result = -ENOMEM;
721 i2400m->bm_cmd_buf = kzalloc(I2400M_BM_CMD_BUF_SIZE, GFP_KERNEL);
722 if (i2400m->bm_cmd_buf == NULL)
723 goto error_bm_cmd_kzalloc;
724 i2400m->bm_ack_buf = kzalloc(I2400M_BM_ACK_BUF_SIZE, GFP_KERNEL);
725 if (i2400m->bm_ack_buf == NULL)
726 goto error_bm_ack_buf_kzalloc;
727 return 0;
728
729error_bm_ack_buf_kzalloc:
730 kfree(i2400m->bm_cmd_buf);
731error_bm_cmd_kzalloc:
732 return result;
733}
734EXPORT_SYMBOL_GPL(i2400m_bm_buf_alloc);
735
736/**
737 * i2400m_bm_buf_free - Free boot mode command and ack buffers.
738 *
739 * Free the command and ack buffers
740 *
741 */
742void i2400m_bm_buf_free(struct i2400m *i2400m)
743{
744 kfree(i2400m->bm_ack_buf);
745 kfree(i2400m->bm_cmd_buf);
746 return;
747}
748EXPORT_SYMBOL_GPL(i2400m_bm_buf_free
749);
750/**
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800751 * i2400m_setup - bus-generic setup function for the i2400m device
752 *
753 * @i2400m: device descriptor (bus-specific parts have been initialized)
754 *
755 * Returns: 0 if ok, < 0 errno code on error.
756 *
Inaky Perez-Gonzalez8f90f3e2009-09-16 17:53:57 -0700757 * Sets up basic device comunication infrastructure, boots the ROM to
758 * read the MAC address, registers with the WiMAX and network stacks
759 * and then brings up the device.
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800760 */
761int i2400m_setup(struct i2400m *i2400m, enum i2400m_bri bm_flags)
762{
763 int result = -ENODEV;
764 struct device *dev = i2400m_dev(i2400m);
765 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
766 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
767
768 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
769
770 snprintf(wimax_dev->name, sizeof(wimax_dev->name),
Kay Sievers347707b2009-02-28 23:42:51 +0000771 "i2400m-%s:%s", dev->bus->name, dev_name(dev));
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800772
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800773 result = i2400m_bootrom_init(i2400m, bm_flags);
774 if (result < 0) {
775 dev_err(dev, "read mac addr: bootrom init "
776 "failed: %d\n", result);
777 goto error_bootrom_init;
778 }
779 result = i2400m_read_mac_addr(i2400m);
780 if (result < 0)
781 goto error_read_mac_addr;
Inaky Perez-Gonzalezfe442682009-04-22 16:53:08 -0700782 random_ether_addr(i2400m->src_mac_addr);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800783
Inaky Perez-Gonzalez7b43ca72009-09-14 14:10:16 -0700784 i2400m->pm_notifier.notifier_call = i2400m_pm_notifier;
785 register_pm_notifier(&i2400m->pm_notifier);
786
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800787 result = register_netdev(net_dev); /* Okey dokey, bring it up */
788 if (result < 0) {
789 dev_err(dev, "cannot register i2400m network device: %d\n",
790 result);
791 goto error_register_netdev;
792 }
793 netif_carrier_off(net_dev);
794
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800795 i2400m->wimax_dev.op_msg_from_user = i2400m_op_msg_from_user;
796 i2400m->wimax_dev.op_rfkill_sw_toggle = i2400m_op_rfkill_sw_toggle;
797 i2400m->wimax_dev.op_reset = i2400m_op_reset;
Inaky Perez-Gonzalez8f90f3e2009-09-16 17:53:57 -0700798
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800799 result = wimax_dev_add(&i2400m->wimax_dev, net_dev);
800 if (result < 0)
801 goto error_wimax_dev_add;
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800802
803 /* Now setup all that requires a registered net and wimax device. */
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +0000804 result = sysfs_create_group(&net_dev->dev.kobj, &i2400m_dev_attr_group);
805 if (result < 0) {
806 dev_err(dev, "cannot setup i2400m's sysfs: %d\n", result);
807 goto error_sysfs_setup;
808 }
Inaky Perez-Gonzalez8f90f3e2009-09-16 17:53:57 -0700809
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800810 result = i2400m_debugfs_add(i2400m);
811 if (result < 0) {
812 dev_err(dev, "cannot setup i2400m's debugfs: %d\n", result);
813 goto error_debugfs_setup;
814 }
Inaky Perez-Gonzalez8f90f3e2009-09-16 17:53:57 -0700815
816 result = i2400m_dev_start(i2400m, bm_flags);
817 if (result < 0)
818 goto error_dev_start;
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800819 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
820 return result;
821
Inaky Perez-Gonzalez8f90f3e2009-09-16 17:53:57 -0700822error_dev_start:
823 i2400m_debugfs_rm(i2400m);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800824error_debugfs_setup:
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +0000825 sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
826 &i2400m_dev_attr_group);
827error_sysfs_setup:
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800828 wimax_dev_rm(&i2400m->wimax_dev);
829error_wimax_dev_add:
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800830 unregister_netdev(net_dev);
831error_register_netdev:
Inaky Perez-Gonzalez7b43ca72009-09-14 14:10:16 -0700832 unregister_pm_notifier(&i2400m->pm_notifier);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800833error_read_mac_addr:
834error_bootrom_init:
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800835 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
836 return result;
837}
838EXPORT_SYMBOL_GPL(i2400m_setup);
839
840
841/**
842 * i2400m_release - release the bus-generic driver resources
843 *
844 * Sends a disconnect message and undoes any setup done by i2400m_setup()
845 */
846void i2400m_release(struct i2400m *i2400m)
847{
848 struct device *dev = i2400m_dev(i2400m);
849
850 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
851 netif_stop_queue(i2400m->wimax_dev.net_dev);
852
Inaky Perez-Gonzalez8f90f3e2009-09-16 17:53:57 -0700853 i2400m_dev_stop(i2400m);
854
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800855 i2400m_debugfs_rm(i2400m);
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +0000856 sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
857 &i2400m_dev_attr_group);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800858 wimax_dev_rm(&i2400m->wimax_dev);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800859 unregister_netdev(i2400m->wimax_dev.net_dev);
Inaky Perez-Gonzalez7b43ca72009-09-14 14:10:16 -0700860 unregister_pm_notifier(&i2400m->pm_notifier);
Inaky Perez-Gonzalez8f90f3e2009-09-16 17:53:57 -0700861 i2400m_bm_buf_free(i2400m);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800862 d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
863}
864EXPORT_SYMBOL_GPL(i2400m_release);
865
866
Inaky Perez-Gonzalez1af7ad52009-01-29 17:18:31 -0800867/*
868 * Debug levels control; see debug.h
869 */
870struct d_level D_LEVEL[] = {
871 D_SUBMODULE_DEFINE(control),
872 D_SUBMODULE_DEFINE(driver),
873 D_SUBMODULE_DEFINE(debugfs),
874 D_SUBMODULE_DEFINE(fw),
875 D_SUBMODULE_DEFINE(netdev),
876 D_SUBMODULE_DEFINE(rfkill),
877 D_SUBMODULE_DEFINE(rx),
Inaky Perez-Gonzalez4dc1bf02009-09-02 15:31:48 -0700878 D_SUBMODULE_DEFINE(sysfs),
Inaky Perez-Gonzalez1af7ad52009-01-29 17:18:31 -0800879 D_SUBMODULE_DEFINE(tx),
880};
881size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
882
883
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800884static
885int __init i2400m_driver_init(void)
886{
Inaky Perez-Gonzalez4c2b1a12009-09-02 15:36:05 -0700887 d_parse_params(D_LEVEL, D_LEVEL_SIZE, i2400m_debug_params,
888 "i2400m.debug");
Inaky Perez-Gonzalezaba3792a2009-09-03 15:14:29 -0700889 return i2400m_barker_db_init(i2400m_barkers_params);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800890}
891module_init(i2400m_driver_init);
892
893static
894void __exit i2400m_driver_exit(void)
895{
896 /* for scheds i2400m_dev_reset_handle() */
897 flush_scheduled_work();
Inaky Perez-Gonzalezaba3792a2009-09-03 15:14:29 -0700898 i2400m_barker_db_exit();
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800899 return;
900}
901module_exit(i2400m_driver_exit);
902
903MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
904MODULE_DESCRIPTION("Intel 2400M WiMAX networking bus-generic driver");
905MODULE_LICENSE("GPL");