nilfs2: fix imperfect completion wait in nilfs_wait_on_logs
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / wimax / stack.c
blob813e1eaea29bb2ef71b2e6b4292248837fcef93a
1 /*
2 * Linux WiMAX
3 * Initialization, addition and removal of wimax devices
6 * Copyright (C) 2005-2006 Intel Corporation <linux-wimax@intel.com>
7 * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
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.
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.
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.
24 * This implements:
26 * - basic life cycle of 'struct wimax_dev' [wimax_dev_*()]; on
27 * addition/registration initialize all subfields and allocate
28 * generic netlink resources for user space communication. On
29 * removal/unregistration, undo all that.
31 * - device state machine [wimax_state_change()] and support to send
32 * reports to user space when the state changes
33 * [wimax_gnl_re_state_change*()].
35 * See include/net/wimax.h for rationales and design.
37 * ROADMAP
39 * [__]wimax_state_change() Called by drivers to update device's state
40 * wimax_gnl_re_state_change_alloc()
41 * wimax_gnl_re_state_change_send()
43 * wimax_dev_init() Init a device
44 * wimax_dev_add() Register
45 * wimax_rfkill_add()
46 * wimax_gnl_add() Register all the generic netlink resources.
47 * wimax_id_table_add()
48 * wimax_dev_rm() Unregister
49 * wimax_id_table_rm()
50 * wimax_gnl_rm()
51 * wimax_rfkill_rm()
53 #include <linux/device.h>
54 #include <net/genetlink.h>
55 #include <linux/netdevice.h>
56 #include <linux/wimax.h>
57 #include "wimax-internal.h"
60 #define D_SUBMODULE stack
61 #include "debug-levels.h"
63 static char wimax_debug_params[128];
64 module_param_string(debug, wimax_debug_params, sizeof(wimax_debug_params),
65 0644);
66 MODULE_PARM_DESC(debug,
67 "String of space-separated NAME:VALUE pairs, where NAMEs "
68 "are the different debug submodules and VALUE are the "
69 "initial debug value to set.");
72 * Authoritative source for the RE_STATE_CHANGE attribute policy
74 * We don't really use it here, but /me likes to keep the definition
75 * close to where the data is generated.
78 static const struct nla_policy wimax_gnl_re_status_change[WIMAX_GNL_ATTR_MAX + 1] = {
79 [WIMAX_GNL_STCH_STATE_OLD] = { .type = NLA_U8 },
80 [WIMAX_GNL_STCH_STATE_NEW] = { .type = NLA_U8 },
86 * Allocate a Report State Change message
88 * @header: save it, you need it for _send()
90 * Creates and fills a basic state change message; different code
91 * paths can then add more attributes to the message as needed.
93 * Use wimax_gnl_re_state_change_send() to send the returned skb.
95 * Returns: skb with the genl message if ok, IS_ERR() ptr on error
96 * with an errno code.
98 static
99 struct sk_buff *wimax_gnl_re_state_change_alloc(
100 struct wimax_dev *wimax_dev,
101 enum wimax_st new_state, enum wimax_st old_state,
102 void **header)
104 int result;
105 struct device *dev = wimax_dev_to_dev(wimax_dev);
106 void *data;
107 struct sk_buff *report_skb;
109 d_fnstart(3, dev, "(wimax_dev %p new_state %u old_state %u)\n",
110 wimax_dev, new_state, old_state);
111 result = -ENOMEM;
112 report_skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
113 if (report_skb == NULL) {
114 dev_err(dev, "RE_STCH: can't create message\n");
115 goto error_new;
117 data = genlmsg_put(report_skb, 0, wimax_gnl_mcg.id, &wimax_gnl_family,
118 0, WIMAX_GNL_RE_STATE_CHANGE);
119 if (data == NULL) {
120 dev_err(dev, "RE_STCH: can't put data into message\n");
121 goto error_put;
123 *header = data;
125 result = nla_put_u8(report_skb, WIMAX_GNL_STCH_STATE_OLD, old_state);
126 if (result < 0) {
127 dev_err(dev, "RE_STCH: Error adding OLD attr: %d\n", result);
128 goto error_put;
130 result = nla_put_u8(report_skb, WIMAX_GNL_STCH_STATE_NEW, new_state);
131 if (result < 0) {
132 dev_err(dev, "RE_STCH: Error adding NEW attr: %d\n", result);
133 goto error_put;
135 result = nla_put_u32(report_skb, WIMAX_GNL_STCH_IFIDX,
136 wimax_dev->net_dev->ifindex);
137 if (result < 0) {
138 dev_err(dev, "RE_STCH: Error adding IFINDEX attribute\n");
139 goto error_put;
141 d_fnend(3, dev, "(wimax_dev %p new_state %u old_state %u) = %p\n",
142 wimax_dev, new_state, old_state, report_skb);
143 return report_skb;
145 error_put:
146 nlmsg_free(report_skb);
147 error_new:
148 d_fnend(3, dev, "(wimax_dev %p new_state %u old_state %u) = %d\n",
149 wimax_dev, new_state, old_state, result);
150 return ERR_PTR(result);
155 * Send a Report State Change message (as created with _alloc).
157 * @report_skb: as returned by wimax_gnl_re_state_change_alloc()
158 * @header: as returned by wimax_gnl_re_state_change_alloc()
160 * Returns: 0 if ok, < 0 errno code on error.
162 * If the message is NULL, pretend it didn't happen.
164 static
165 int wimax_gnl_re_state_change_send(
166 struct wimax_dev *wimax_dev, struct sk_buff *report_skb,
167 void *header)
169 int result = 0;
170 struct device *dev = wimax_dev_to_dev(wimax_dev);
171 d_fnstart(3, dev, "(wimax_dev %p report_skb %p)\n",
172 wimax_dev, report_skb);
173 if (report_skb == NULL) {
174 result = -ENOMEM;
175 goto out;
177 genlmsg_end(report_skb, header);
178 genlmsg_multicast(report_skb, 0, wimax_gnl_mcg.id, GFP_KERNEL);
179 out:
180 d_fnend(3, dev, "(wimax_dev %p report_skb %p) = %d\n",
181 wimax_dev, report_skb, result);
182 return result;
186 static
187 void __check_new_state(enum wimax_st old_state, enum wimax_st new_state,
188 unsigned allowed_states_bm)
190 if (WARN_ON(((1 << new_state) & allowed_states_bm) == 0)) {
191 printk(KERN_ERR "SW BUG! Forbidden state change %u -> %u\n",
192 old_state, new_state);
198 * Set the current state of a WiMAX device [unlocking version of
199 * wimax_state_change().
201 void __wimax_state_change(struct wimax_dev *wimax_dev, enum wimax_st new_state)
203 struct device *dev = wimax_dev_to_dev(wimax_dev);
204 enum wimax_st old_state = wimax_dev->state;
205 struct sk_buff *stch_skb;
206 void *header;
208 d_fnstart(3, dev, "(wimax_dev %p new_state %u [old %u])\n",
209 wimax_dev, new_state, old_state);
211 if (WARN_ON(new_state >= __WIMAX_ST_INVALID)) {
212 dev_err(dev, "SW BUG: requesting invalid state %u\n",
213 new_state);
214 goto out;
216 if (old_state == new_state)
217 goto out;
218 header = NULL; /* gcc complains? can't grok why */
219 stch_skb = wimax_gnl_re_state_change_alloc(
220 wimax_dev, new_state, old_state, &header);
222 /* Verify the state transition and do exit-from-state actions */
223 switch (old_state) {
224 case __WIMAX_ST_NULL:
225 __check_new_state(old_state, new_state,
226 1 << WIMAX_ST_DOWN);
227 break;
228 case WIMAX_ST_DOWN:
229 __check_new_state(old_state, new_state,
230 1 << __WIMAX_ST_QUIESCING
231 | 1 << WIMAX_ST_UNINITIALIZED
232 | 1 << WIMAX_ST_RADIO_OFF);
233 break;
234 case __WIMAX_ST_QUIESCING:
235 __check_new_state(old_state, new_state, 1 << WIMAX_ST_DOWN);
236 break;
237 case WIMAX_ST_UNINITIALIZED:
238 __check_new_state(old_state, new_state,
239 1 << __WIMAX_ST_QUIESCING
240 | 1 << WIMAX_ST_RADIO_OFF);
241 break;
242 case WIMAX_ST_RADIO_OFF:
243 __check_new_state(old_state, new_state,
244 1 << __WIMAX_ST_QUIESCING
245 | 1 << WIMAX_ST_READY);
246 break;
247 case WIMAX_ST_READY:
248 __check_new_state(old_state, new_state,
249 1 << __WIMAX_ST_QUIESCING
250 | 1 << WIMAX_ST_RADIO_OFF
251 | 1 << WIMAX_ST_SCANNING
252 | 1 << WIMAX_ST_CONNECTING
253 | 1 << WIMAX_ST_CONNECTED);
254 break;
255 case WIMAX_ST_SCANNING:
256 __check_new_state(old_state, new_state,
257 1 << __WIMAX_ST_QUIESCING
258 | 1 << WIMAX_ST_RADIO_OFF
259 | 1 << WIMAX_ST_READY
260 | 1 << WIMAX_ST_CONNECTING
261 | 1 << WIMAX_ST_CONNECTED);
262 break;
263 case WIMAX_ST_CONNECTING:
264 __check_new_state(old_state, new_state,
265 1 << __WIMAX_ST_QUIESCING
266 | 1 << WIMAX_ST_RADIO_OFF
267 | 1 << WIMAX_ST_READY
268 | 1 << WIMAX_ST_SCANNING
269 | 1 << WIMAX_ST_CONNECTED);
270 break;
271 case WIMAX_ST_CONNECTED:
272 __check_new_state(old_state, new_state,
273 1 << __WIMAX_ST_QUIESCING
274 | 1 << WIMAX_ST_RADIO_OFF
275 | 1 << WIMAX_ST_READY);
276 netif_tx_disable(wimax_dev->net_dev);
277 netif_carrier_off(wimax_dev->net_dev);
278 break;
279 case __WIMAX_ST_INVALID:
280 default:
281 dev_err(dev, "SW BUG: wimax_dev %p is in unknown state %u\n",
282 wimax_dev, wimax_dev->state);
283 WARN_ON(1);
284 goto out;
287 /* Execute the actions of entry to the new state */
288 switch (new_state) {
289 case __WIMAX_ST_NULL:
290 dev_err(dev, "SW BUG: wimax_dev %p entering NULL state "
291 "from %u\n", wimax_dev, wimax_dev->state);
292 WARN_ON(1); /* Nobody can enter this state */
293 break;
294 case WIMAX_ST_DOWN:
295 break;
296 case __WIMAX_ST_QUIESCING:
297 break;
298 case WIMAX_ST_UNINITIALIZED:
299 break;
300 case WIMAX_ST_RADIO_OFF:
301 break;
302 case WIMAX_ST_READY:
303 break;
304 case WIMAX_ST_SCANNING:
305 break;
306 case WIMAX_ST_CONNECTING:
307 break;
308 case WIMAX_ST_CONNECTED:
309 netif_carrier_on(wimax_dev->net_dev);
310 netif_wake_queue(wimax_dev->net_dev);
311 break;
312 case __WIMAX_ST_INVALID:
313 default:
314 BUG();
316 __wimax_state_set(wimax_dev, new_state);
317 if (stch_skb)
318 wimax_gnl_re_state_change_send(wimax_dev, stch_skb, header);
319 out:
320 d_fnend(3, dev, "(wimax_dev %p new_state %u [old %u]) = void\n",
321 wimax_dev, new_state, old_state);
322 return;
327 * wimax_state_change - Set the current state of a WiMAX device
329 * @wimax_dev: WiMAX device descriptor (properly referenced)
330 * @new_state: New state to switch to
332 * This implements the state changes for the wimax devices. It will
334 * - verify that the state transition is legal (for now it'll just
335 * print a warning if not) according to the table in
336 * linux/wimax.h's documentation for 'enum wimax_st'.
338 * - perform the actions needed for leaving the current state and
339 * whichever are needed for entering the new state.
341 * - issue a report to user space indicating the new state (and an
342 * optional payload with information about the new state).
344 * NOTE: @wimax_dev must be locked
346 void wimax_state_change(struct wimax_dev *wimax_dev, enum wimax_st new_state)
349 * A driver cannot take the wimax_dev out of the
350 * __WIMAX_ST_NULL state unless by calling wimax_dev_add(). If
351 * the wimax_dev's state is still NULL, we ignore any request
352 * to change its state because it means it hasn't been yet
353 * registered.
355 * There is no need to complain about it, as routines that
356 * call this might be shared from different code paths that
357 * are called before or after wimax_dev_add() has done its
358 * job.
360 mutex_lock(&wimax_dev->mutex);
361 if (wimax_dev->state > __WIMAX_ST_NULL)
362 __wimax_state_change(wimax_dev, new_state);
363 mutex_unlock(&wimax_dev->mutex);
364 return;
366 EXPORT_SYMBOL_GPL(wimax_state_change);
370 * wimax_state_get() - Return the current state of a WiMAX device
372 * @wimax_dev: WiMAX device descriptor
374 * Returns: Current state of the device according to its driver.
376 enum wimax_st wimax_state_get(struct wimax_dev *wimax_dev)
378 enum wimax_st state;
379 mutex_lock(&wimax_dev->mutex);
380 state = wimax_dev->state;
381 mutex_unlock(&wimax_dev->mutex);
382 return state;
384 EXPORT_SYMBOL_GPL(wimax_state_get);
388 * wimax_dev_init - initialize a newly allocated instance
390 * @wimax_dev: WiMAX device descriptor to initialize.
392 * Initializes fields of a freshly allocated @wimax_dev instance. This
393 * function assumes that after allocation, the memory occupied by
394 * @wimax_dev was zeroed.
396 void wimax_dev_init(struct wimax_dev *wimax_dev)
398 INIT_LIST_HEAD(&wimax_dev->id_table_node);
399 __wimax_state_set(wimax_dev, __WIMAX_ST_NULL);
400 mutex_init(&wimax_dev->mutex);
401 mutex_init(&wimax_dev->mutex_reset);
403 EXPORT_SYMBOL_GPL(wimax_dev_init);
406 * This extern is declared here because it's easier to keep track --
407 * both declarations are a list of the same
409 extern struct genl_ops
410 wimax_gnl_msg_from_user,
411 wimax_gnl_reset,
412 wimax_gnl_rfkill,
413 wimax_gnl_state_get;
415 static
416 struct genl_ops *wimax_gnl_ops[] = {
417 &wimax_gnl_msg_from_user,
418 &wimax_gnl_reset,
419 &wimax_gnl_rfkill,
420 &wimax_gnl_state_get,
424 static
425 size_t wimax_addr_scnprint(char *addr_str, size_t addr_str_size,
426 unsigned char *addr, size_t addr_len)
428 unsigned cnt, total;
429 for (total = cnt = 0; cnt < addr_len; cnt++)
430 total += scnprintf(addr_str + total, addr_str_size - total,
431 "%02x%c", addr[cnt],
432 cnt == addr_len - 1 ? '\0' : ':');
433 return total;
438 * wimax_dev_add - Register a new WiMAX device
440 * @wimax_dev: WiMAX device descriptor (as embedded in your @net_dev's
441 * priv data). You must have called wimax_dev_init() on it before.
443 * @net_dev: net device the @wimax_dev is associated with. The
444 * function expects SET_NETDEV_DEV() and register_netdev() were
445 * already called on it.
447 * Registers the new WiMAX device, sets up the user-kernel control
448 * interface (generic netlink) and common WiMAX infrastructure.
450 * Note that the parts that will allow interaction with user space are
451 * setup at the very end, when the rest is in place, as once that
452 * happens, the driver might get user space control requests via
453 * netlink or from debugfs that might translate into calls into
454 * wimax_dev->op_*().
456 int wimax_dev_add(struct wimax_dev *wimax_dev, struct net_device *net_dev)
458 int result;
459 struct device *dev = net_dev->dev.parent;
460 char addr_str[32];
462 d_fnstart(3, dev, "(wimax_dev %p net_dev %p)\n", wimax_dev, net_dev);
464 /* Do the RFKILL setup before locking, as RFKILL will call
465 * into our functions. */
466 wimax_dev->net_dev = net_dev;
467 result = wimax_rfkill_add(wimax_dev);
468 if (result < 0)
469 goto error_rfkill_add;
471 /* Set up user-space interaction */
472 mutex_lock(&wimax_dev->mutex);
473 wimax_id_table_add(wimax_dev);
474 result = wimax_debugfs_add(wimax_dev);
475 if (result < 0) {
476 dev_err(dev, "cannot initialize debugfs: %d\n",
477 result);
478 goto error_debugfs_add;
481 __wimax_state_set(wimax_dev, WIMAX_ST_DOWN);
482 mutex_unlock(&wimax_dev->mutex);
484 wimax_addr_scnprint(addr_str, sizeof(addr_str),
485 net_dev->dev_addr, net_dev->addr_len);
486 dev_err(dev, "WiMAX interface %s (%s) ready\n",
487 net_dev->name, addr_str);
488 d_fnend(3, dev, "(wimax_dev %p net_dev %p) = 0\n", wimax_dev, net_dev);
489 return 0;
491 error_debugfs_add:
492 wimax_id_table_rm(wimax_dev);
493 mutex_unlock(&wimax_dev->mutex);
494 wimax_rfkill_rm(wimax_dev);
495 error_rfkill_add:
496 d_fnend(3, dev, "(wimax_dev %p net_dev %p) = %d\n",
497 wimax_dev, net_dev, result);
498 return result;
500 EXPORT_SYMBOL_GPL(wimax_dev_add);
504 * wimax_dev_rm - Unregister an existing WiMAX device
506 * @wimax_dev: WiMAX device descriptor
508 * Unregisters a WiMAX device previously registered for use with
509 * wimax_add_rm().
511 * IMPORTANT! Must call before calling unregister_netdev().
513 * After this function returns, you will not get any more user space
514 * control requests (via netlink or debugfs) and thus to wimax_dev->ops.
516 * Reentrancy control is ensured by setting the state to
517 * %__WIMAX_ST_QUIESCING. rfkill operations coming through
518 * wimax_*rfkill*() will be stopped by the quiescing state; ops coming
519 * from the rfkill subsystem will be stopped by the support being
520 * removed by wimax_rfkill_rm().
522 void wimax_dev_rm(struct wimax_dev *wimax_dev)
524 d_fnstart(3, NULL, "(wimax_dev %p)\n", wimax_dev);
526 mutex_lock(&wimax_dev->mutex);
527 __wimax_state_change(wimax_dev, __WIMAX_ST_QUIESCING);
528 wimax_debugfs_rm(wimax_dev);
529 wimax_id_table_rm(wimax_dev);
530 __wimax_state_change(wimax_dev, WIMAX_ST_DOWN);
531 mutex_unlock(&wimax_dev->mutex);
532 wimax_rfkill_rm(wimax_dev);
533 d_fnend(3, NULL, "(wimax_dev %p) = void\n", wimax_dev);
535 EXPORT_SYMBOL_GPL(wimax_dev_rm);
538 /* Debug framework control of debug levels */
539 struct d_level D_LEVEL[] = {
540 D_SUBMODULE_DEFINE(debugfs),
541 D_SUBMODULE_DEFINE(id_table),
542 D_SUBMODULE_DEFINE(op_msg),
543 D_SUBMODULE_DEFINE(op_reset),
544 D_SUBMODULE_DEFINE(op_rfkill),
545 D_SUBMODULE_DEFINE(op_state_get),
546 D_SUBMODULE_DEFINE(stack),
548 size_t D_LEVEL_SIZE = ARRAY_SIZE(D_LEVEL);
551 struct genl_family wimax_gnl_family = {
552 .id = GENL_ID_GENERATE,
553 .name = "WiMAX",
554 .version = WIMAX_GNL_VERSION,
555 .hdrsize = 0,
556 .maxattr = WIMAX_GNL_ATTR_MAX,
559 struct genl_multicast_group wimax_gnl_mcg = {
560 .name = "msg",
565 /* Shutdown the wimax stack */
566 static
567 int __init wimax_subsys_init(void)
569 int result, cnt;
571 d_fnstart(4, NULL, "()\n");
572 d_parse_params(D_LEVEL, D_LEVEL_SIZE, wimax_debug_params,
573 "wimax.debug");
575 snprintf(wimax_gnl_family.name, sizeof(wimax_gnl_family.name),
576 "WiMAX");
577 result = genl_register_family(&wimax_gnl_family);
578 if (unlikely(result < 0)) {
579 printk(KERN_ERR "cannot register generic netlink family: %d\n",
580 result);
581 goto error_register_family;
584 for (cnt = 0; cnt < ARRAY_SIZE(wimax_gnl_ops); cnt++) {
585 result = genl_register_ops(&wimax_gnl_family,
586 wimax_gnl_ops[cnt]);
587 d_printf(4, NULL, "registering generic netlink op code "
588 "%u: %d\n", wimax_gnl_ops[cnt]->cmd, result);
589 if (unlikely(result < 0)) {
590 printk(KERN_ERR "cannot register generic netlink op "
591 "code %u: %d\n",
592 wimax_gnl_ops[cnt]->cmd, result);
593 goto error_register_ops;
597 result = genl_register_mc_group(&wimax_gnl_family, &wimax_gnl_mcg);
598 if (result < 0)
599 goto error_mc_group;
600 d_fnend(4, NULL, "() = 0\n");
601 return 0;
603 error_mc_group:
604 error_register_ops:
605 for (cnt--; cnt >= 0; cnt--)
606 genl_unregister_ops(&wimax_gnl_family,
607 wimax_gnl_ops[cnt]);
608 genl_unregister_family(&wimax_gnl_family);
609 error_register_family:
610 d_fnend(4, NULL, "() = %d\n", result);
611 return result;
614 module_init(wimax_subsys_init);
617 /* Shutdown the wimax stack */
618 static
619 void __exit wimax_subsys_exit(void)
621 int cnt;
622 wimax_id_table_release();
623 genl_unregister_mc_group(&wimax_gnl_family, &wimax_gnl_mcg);
624 for (cnt = ARRAY_SIZE(wimax_gnl_ops) - 1; cnt >= 0; cnt--)
625 genl_unregister_ops(&wimax_gnl_family,
626 wimax_gnl_ops[cnt]);
627 genl_unregister_family(&wimax_gnl_family);
629 module_exit(wimax_subsys_exit);
631 MODULE_AUTHOR("Intel Corporation <linux-wimax@intel.com>");
632 MODULE_DESCRIPTION("Linux WiMAX stack");
633 MODULE_LICENSE("GPL");