blob: 07d12be0cf818feaccc16953f27eaa495444689d [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);
530 i2400m_dev_shutdown(i2400m);
531 i2400m->ready = 0;
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800532 i2400m->bus_dev_stop(i2400m);
Inaky Perez-Gonzaleze9a6b452009-05-08 13:02:41 -0700533 destroy_workqueue(i2400m->work_queue);
Inaky Perez-Gonzalezc7475832009-02-28 23:42:54 +0000534 i2400m_rx_release(i2400m);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800535 i2400m_tx_release(i2400m);
536 wimax_state_change(wimax_dev, WIMAX_ST_DOWN);
537 d_fnend(3, dev, "(i2400m %p) = 0\n", i2400m);
538}
539
540
541/*
542 * Watch out -- we only need to stop if there is a need for it. The
543 * device could have reset itself and failed to come up again (see
544 * _i2400m_dev_reset_handle()).
545 */
546static
547void i2400m_dev_stop(struct i2400m *i2400m)
548{
549 mutex_lock(&i2400m->init_mutex);
550 if (i2400m->updown) {
551 __i2400m_dev_stop(i2400m);
552 i2400m->updown = 0;
553 }
554 mutex_unlock(&i2400m->init_mutex);
555}
556
557
558/*
Inaky Perez-Gonzalez7b43ca72009-09-14 14:10:16 -0700559 * Listen to PM events to cache the firmware before suspend/hibernation
560 *
561 * When the device comes out of suspend, it might go into reset and
562 * firmware has to be uploaded again. At resume, most of the times, we
563 * can't load firmware images from disk, so we need to cache it.
564 *
565 * i2400m_fw_cache() will allocate a kobject and attach the firmware
566 * to it; that way we don't have to worry too much about the fw loader
567 * hitting a race condition.
568 *
569 * Note: modus operandi stolen from the Orinoco driver; thx.
570 */
571static
572int i2400m_pm_notifier(struct notifier_block *notifier,
573 unsigned long pm_event,
574 void *unused)
575{
576 struct i2400m *i2400m =
577 container_of(notifier, struct i2400m, pm_notifier);
578 struct device *dev = i2400m_dev(i2400m);
579
580 d_fnstart(3, dev, "(i2400m %p pm_event %lx)\n", i2400m, pm_event);
581 switch (pm_event) {
582 case PM_HIBERNATION_PREPARE:
583 case PM_SUSPEND_PREPARE:
584 i2400m_fw_cache(i2400m);
585 break;
586 case PM_POST_RESTORE:
587 /* Restore from hibernation failed. We need to clean
588 * up in exactly the same way, so fall through. */
589 case PM_POST_HIBERNATION:
590 case PM_POST_SUSPEND:
591 i2400m_fw_uncache(i2400m);
592 break;
593
594 case PM_RESTORE_PREPARE:
595 default:
596 break;
597 }
598 d_fnend(3, dev, "(i2400m %p pm_event %lx) = void\n", i2400m, pm_event);
599 return NOTIFY_DONE;
600}
601
602
603/*
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800604 * The device has rebooted; fix up the device and the driver
605 *
606 * Tear down the driver communication with the device, reload the
607 * firmware and reinitialize the communication with the device.
608 *
609 * If someone calls a reset when the device's firmware is down, in
610 * theory we won't see it because we are not listening. However, just
611 * in case, leave the code to handle it.
612 *
613 * If there is a reset context, use it; this means someone is waiting
614 * for us to tell him when the reset operation is complete and the
615 * device is ready to rock again.
616 *
617 * NOTE: if we are in the process of bringing up or down the
618 * communication with the device [running i2400m_dev_start() or
619 * _stop()], don't do anything, let it fail and handle it.
620 *
621 * This function is ran always in a thread context
Inaky Perez-Gonzalez3ef61292009-09-14 14:05:19 -0700622 *
623 * This function gets passed, as payload to i2400m_work() a 'const
624 * char *' ptr with a "reason" why the reset happened (for messages).
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800625 */
626static
627void __i2400m_dev_reset_handle(struct work_struct *ws)
628{
629 int result;
630 struct i2400m_work *iw = container_of(ws, struct i2400m_work, ws);
Inaky Perez-Gonzalez3ef61292009-09-14 14:05:19 -0700631 const char *reason;
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800632 struct i2400m *i2400m = iw->i2400m;
633 struct device *dev = i2400m_dev(i2400m);
634 enum wimax_st wimax_state;
635 struct i2400m_reset_ctx *ctx = i2400m->reset_ctx;
636
Inaky Perez-Gonzalez3ef61292009-09-14 14:05:19 -0700637 if (WARN_ON(iw->pl_size != sizeof(reason)))
638 reason = "SW BUG: reason n/a";
639 else
640 memcpy(&reason, iw->pl, sizeof(reason));
641
642 d_fnstart(3, dev, "(ws %p i2400m %p reason %s)\n", ws, i2400m, reason);
643
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800644 result = 0;
645 if (mutex_trylock(&i2400m->init_mutex) == 0) {
646 /* We are still in i2400m_dev_start() [let it fail] or
647 * i2400m_dev_stop() [we are shutting down anyway, so
648 * ignore it] or we are resetting somewhere else. */
649 dev_err(dev, "device rebooted\n");
Cindy H Kao0bcfc5e2009-06-10 17:06:19 -0700650 i2400m_msg_to_dev_cancel_wait(i2400m, -EL3RST);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800651 complete(&i2400m->msg_completion);
652 goto out;
653 }
654 wimax_state = wimax_state_get(&i2400m->wimax_dev);
655 if (wimax_state < WIMAX_ST_UNINITIALIZED) {
Inaky Perez-Gonzalez3ef61292009-09-14 14:05:19 -0700656 dev_info(dev, "%s: it is down, ignoring\n", reason);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800657 goto out_unlock; /* ifconfig up/down wasn't called */
658 }
Inaky Perez-Gonzalez3ef61292009-09-14 14:05:19 -0700659 dev_err(dev, "%s: reinitializing driver\n", reason);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800660 __i2400m_dev_stop(i2400m);
661 i2400m->updown = 0;
662 result = __i2400m_dev_start(i2400m,
663 I2400M_BRI_SOFT | I2400M_BRI_MAC_REINIT);
664 if (result < 0) {
Inaky Perez-Gonzalez3ef61292009-09-14 14:05:19 -0700665 dev_err(dev, "%s: cannot start the device: %d\n",
666 reason, result);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800667 result = i2400m->bus_reset(i2400m, I2400M_RT_BUS);
668 if (result >= 0)
669 result = -ENODEV;
670 } else
671 i2400m->updown = 1;
672out_unlock:
673 if (i2400m->reset_ctx) {
674 ctx->result = result;
675 complete(&ctx->completion);
676 }
677 mutex_unlock(&i2400m->init_mutex);
678out:
679 i2400m_put(i2400m);
680 kfree(iw);
Inaky Perez-Gonzalez3ef61292009-09-14 14:05:19 -0700681 d_fnend(3, dev, "(ws %p i2400m %p reason %s) = void\n",
682 ws, i2400m, reason);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800683 return;
684}
685
686
687/**
688 * i2400m_dev_reset_handle - Handle a device's reset in a thread context
689 *
690 * Schedule a device reset handling out on a thread context, so it
691 * is safe to call from atomic context. We can't use the i2400m's
692 * queue as we are going to destroy it and reinitialize it as part of
693 * the driver bringup/bringup process.
694 *
695 * See __i2400m_dev_reset_handle() for details; that takes care of
696 * reinitializing the driver to handle the reset, calling into the
697 * bus-specific functions ops as needed.
698 */
Inaky Perez-Gonzalez3ef61292009-09-14 14:05:19 -0700699int i2400m_dev_reset_handle(struct i2400m *i2400m, const char *reason)
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800700{
Inaky Perez-Gonzalezb4013f92009-06-03 09:45:55 +0800701 i2400m->boot_mode = 1;
702 wmb(); /* Make sure i2400m_msg_to_dev() sees boot_mode */
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800703 return i2400m_schedule_work(i2400m, __i2400m_dev_reset_handle,
Inaky Perez-Gonzalez3ef61292009-09-14 14:05:19 -0700704 GFP_ATOMIC, &reason, sizeof(reason));
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800705}
706EXPORT_SYMBOL_GPL(i2400m_dev_reset_handle);
707
708
709/**
Dirk Brandewiea134fd62009-08-18 08:51:52 -0700710 * i2400m_bm_buf_alloc - Alloc the command and ack buffers for boot mode
711 *
712 * Get the buffers needed to deal with boot mode messages. These
713 * buffers need to be allocated before the sdio recieve irq is setup.
714 */
715int i2400m_bm_buf_alloc(struct i2400m *i2400m)
716{
717 int result;
718
719 result = -ENOMEM;
720 i2400m->bm_cmd_buf = kzalloc(I2400M_BM_CMD_BUF_SIZE, GFP_KERNEL);
721 if (i2400m->bm_cmd_buf == NULL)
722 goto error_bm_cmd_kzalloc;
723 i2400m->bm_ack_buf = kzalloc(I2400M_BM_ACK_BUF_SIZE, GFP_KERNEL);
724 if (i2400m->bm_ack_buf == NULL)
725 goto error_bm_ack_buf_kzalloc;
726 return 0;
727
728error_bm_ack_buf_kzalloc:
729 kfree(i2400m->bm_cmd_buf);
730error_bm_cmd_kzalloc:
731 return result;
732}
733EXPORT_SYMBOL_GPL(i2400m_bm_buf_alloc);
734
735/**
736 * i2400m_bm_buf_free - Free boot mode command and ack buffers.
737 *
738 * Free the command and ack buffers
739 *
740 */
741void i2400m_bm_buf_free(struct i2400m *i2400m)
742{
743 kfree(i2400m->bm_ack_buf);
744 kfree(i2400m->bm_cmd_buf);
745 return;
746}
747EXPORT_SYMBOL_GPL(i2400m_bm_buf_free
748);
749/**
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800750 * i2400m_setup - bus-generic setup function for the i2400m device
751 *
752 * @i2400m: device descriptor (bus-specific parts have been initialized)
753 *
754 * Returns: 0 if ok, < 0 errno code on error.
755 *
756 * Initializes the bus-generic parts of the i2400m driver; the
757 * bus-specific parts have been initialized, function pointers filled
758 * out by the bus-specific probe function.
759 *
760 * As well, this registers the WiMAX and net device nodes. Once this
761 * function returns, the device is operative and has to be ready to
762 * receive and send network traffic and WiMAX control operations.
763 */
764int i2400m_setup(struct i2400m *i2400m, enum i2400m_bri bm_flags)
765{
766 int result = -ENODEV;
767 struct device *dev = i2400m_dev(i2400m);
768 struct wimax_dev *wimax_dev = &i2400m->wimax_dev;
769 struct net_device *net_dev = i2400m->wimax_dev.net_dev;
770
771 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
772
773 snprintf(wimax_dev->name, sizeof(wimax_dev->name),
Kay Sievers347707b2009-02-28 23:42:51 +0000774 "i2400m-%s:%s", dev->bus->name, dev_name(dev));
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800775
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800776 result = i2400m_bootrom_init(i2400m, bm_flags);
777 if (result < 0) {
778 dev_err(dev, "read mac addr: bootrom init "
779 "failed: %d\n", result);
780 goto error_bootrom_init;
781 }
782 result = i2400m_read_mac_addr(i2400m);
783 if (result < 0)
784 goto error_read_mac_addr;
Inaky Perez-Gonzalezfe442682009-04-22 16:53:08 -0700785 random_ether_addr(i2400m->src_mac_addr);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800786
Inaky Perez-Gonzalez7b43ca72009-09-14 14:10:16 -0700787 i2400m->pm_notifier.notifier_call = i2400m_pm_notifier;
788 register_pm_notifier(&i2400m->pm_notifier);
789
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800790 result = register_netdev(net_dev); /* Okey dokey, bring it up */
791 if (result < 0) {
792 dev_err(dev, "cannot register i2400m network device: %d\n",
793 result);
794 goto error_register_netdev;
795 }
796 netif_carrier_off(net_dev);
797
798 result = i2400m_dev_start(i2400m, bm_flags);
799 if (result < 0)
800 goto error_dev_start;
801
802 i2400m->wimax_dev.op_msg_from_user = i2400m_op_msg_from_user;
803 i2400m->wimax_dev.op_rfkill_sw_toggle = i2400m_op_rfkill_sw_toggle;
804 i2400m->wimax_dev.op_reset = i2400m_op_reset;
805 result = wimax_dev_add(&i2400m->wimax_dev, net_dev);
806 if (result < 0)
807 goto error_wimax_dev_add;
808 /* User space needs to do some init stuff */
809 wimax_state_change(wimax_dev, WIMAX_ST_UNINITIALIZED);
810
811 /* Now setup all that requires a registered net and wimax device. */
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +0000812 result = sysfs_create_group(&net_dev->dev.kobj, &i2400m_dev_attr_group);
813 if (result < 0) {
814 dev_err(dev, "cannot setup i2400m's sysfs: %d\n", result);
815 goto error_sysfs_setup;
816 }
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800817 result = i2400m_debugfs_add(i2400m);
818 if (result < 0) {
819 dev_err(dev, "cannot setup i2400m's debugfs: %d\n", result);
820 goto error_debugfs_setup;
821 }
822 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
823 return result;
824
825error_debugfs_setup:
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +0000826 sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
827 &i2400m_dev_attr_group);
828error_sysfs_setup:
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800829 wimax_dev_rm(&i2400m->wimax_dev);
830error_wimax_dev_add:
831 i2400m_dev_stop(i2400m);
832error_dev_start:
833 unregister_netdev(net_dev);
834error_register_netdev:
Inaky Perez-Gonzalez7b43ca72009-09-14 14:10:16 -0700835 unregister_pm_notifier(&i2400m->pm_notifier);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800836error_read_mac_addr:
837error_bootrom_init:
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800838 d_fnend(3, dev, "(i2400m %p) = %d\n", i2400m, result);
839 return result;
840}
841EXPORT_SYMBOL_GPL(i2400m_setup);
842
843
844/**
845 * i2400m_release - release the bus-generic driver resources
846 *
847 * Sends a disconnect message and undoes any setup done by i2400m_setup()
848 */
849void i2400m_release(struct i2400m *i2400m)
850{
851 struct device *dev = i2400m_dev(i2400m);
852
853 d_fnstart(3, dev, "(i2400m %p)\n", i2400m);
854 netif_stop_queue(i2400m->wimax_dev.net_dev);
855
856 i2400m_debugfs_rm(i2400m);
Inaky Perez-Gonzalez89876912009-02-28 23:42:50 +0000857 sysfs_remove_group(&i2400m->wimax_dev.net_dev->dev.kobj,
858 &i2400m_dev_attr_group);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800859 wimax_dev_rm(&i2400m->wimax_dev);
860 i2400m_dev_stop(i2400m);
861 unregister_netdev(i2400m->wimax_dev.net_dev);
Inaky Perez-Gonzalez7b43ca72009-09-14 14:10:16 -0700862 unregister_pm_notifier(&i2400m->pm_notifier);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800863 kfree(i2400m->bm_ack_buf);
864 kfree(i2400m->bm_cmd_buf);
865 d_fnend(3, dev, "(i2400m %p) = void\n", i2400m);
866}
867EXPORT_SYMBOL_GPL(i2400m_release);
868
869
Inaky Perez-Gonzalez1af7ad52009-01-29 17:18:31 -0800870/*
871 * Debug levels control; see debug.h
872 */
873struct d_level D_LEVEL[] = {
874 D_SUBMODULE_DEFINE(control),
875 D_SUBMODULE_DEFINE(driver),
876 D_SUBMODULE_DEFINE(debugfs),
877 D_SUBMODULE_DEFINE(fw),
878 D_SUBMODULE_DEFINE(netdev),
879 D_SUBMODULE_DEFINE(rfkill),
880 D_SUBMODULE_DEFINE(rx),
Inaky Perez-Gonzalez4dc1bf02009-09-02 15:31:48 -0700881 D_SUBMODULE_DEFINE(sysfs),
Inaky Perez-Gonzalez1af7ad52009-01-29 17:18:31 -0800882 D_SUBMODULE_DEFINE(tx),
883};
884size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
885
886
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800887static
888int __init i2400m_driver_init(void)
889{
Inaky Perez-Gonzalez4c2b1a12009-09-02 15:36:05 -0700890 d_parse_params(D_LEVEL, D_LEVEL_SIZE, i2400m_debug_params,
891 "i2400m.debug");
Inaky Perez-Gonzalezaba3792a2009-09-03 15:14:29 -0700892 return i2400m_barker_db_init(i2400m_barkers_params);
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800893}
894module_init(i2400m_driver_init);
895
896static
897void __exit i2400m_driver_exit(void)
898{
899 /* for scheds i2400m_dev_reset_handle() */
900 flush_scheduled_work();
Inaky Perez-Gonzalezaba3792a2009-09-03 15:14:29 -0700901 i2400m_barker_db_exit();
Inaky Perez-Gonzalez024f7f32008-12-20 16:57:44 -0800902 return;
903}
904module_exit(i2400m_driver_exit);
905
906MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
907MODULE_DESCRIPTION("Intel 2400M WiMAX networking bus-generic driver");
908MODULE_LICENSE("GPL");