iwlwifi: add missing rcu_read_lock
[safe/jmp/linux-2.6] / drivers / net / wireless / libertas / mesh.c
1 #include <linux/delay.h>
2 #include <linux/etherdevice.h>
3 #include <linux/netdevice.h>
4 #include <linux/if_ether.h>
5 #include <linux/if_arp.h>
6 #include <linux/kthread.h>
7 #include <linux/kfifo.h>
8
9 #include "mesh.h"
10 #include "decl.h"
11 #include "cmd.h"
12
13
14 /***************************************************************************
15  * Mesh sysfs support
16  */
17
18 /**
19  * Attributes exported through sysfs
20  */
21
22 /**
23  * @brief Get function for sysfs attribute anycast_mask
24  */
25 static ssize_t lbs_anycast_get(struct device *dev,
26                 struct device_attribute *attr, char * buf)
27 {
28         struct lbs_private *priv = to_net_dev(dev)->ml_priv;
29         struct cmd_ds_mesh_access mesh_access;
30         int ret;
31
32         memset(&mesh_access, 0, sizeof(mesh_access));
33
34         ret = lbs_mesh_access(priv, CMD_ACT_MESH_GET_ANYCAST, &mesh_access);
35         if (ret)
36                 return ret;
37
38         return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
39 }
40
41 /**
42  * @brief Set function for sysfs attribute anycast_mask
43  */
44 static ssize_t lbs_anycast_set(struct device *dev,
45                 struct device_attribute *attr, const char * buf, size_t count)
46 {
47         struct lbs_private *priv = to_net_dev(dev)->ml_priv;
48         struct cmd_ds_mesh_access mesh_access;
49         uint32_t datum;
50         int ret;
51
52         memset(&mesh_access, 0, sizeof(mesh_access));
53         sscanf(buf, "%x", &datum);
54         mesh_access.data[0] = cpu_to_le32(datum);
55
56         ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_ANYCAST, &mesh_access);
57         if (ret)
58                 return ret;
59
60         return strlen(buf);
61 }
62
63 /**
64  * @brief Get function for sysfs attribute prb_rsp_limit
65  */
66 static ssize_t lbs_prb_rsp_limit_get(struct device *dev,
67                 struct device_attribute *attr, char *buf)
68 {
69         struct lbs_private *priv = to_net_dev(dev)->ml_priv;
70         struct cmd_ds_mesh_access mesh_access;
71         int ret;
72         u32 retry_limit;
73
74         memset(&mesh_access, 0, sizeof(mesh_access));
75         mesh_access.data[0] = cpu_to_le32(CMD_ACT_GET);
76
77         ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_GET_PRB_RSP_LIMIT,
78                         &mesh_access);
79         if (ret)
80                 return ret;
81
82         retry_limit = le32_to_cpu(mesh_access.data[1]);
83         return snprintf(buf, 10, "%d\n", retry_limit);
84 }
85
86 /**
87  * @brief Set function for sysfs attribute prb_rsp_limit
88  */
89 static ssize_t lbs_prb_rsp_limit_set(struct device *dev,
90                 struct device_attribute *attr, const char *buf, size_t count)
91 {
92         struct lbs_private *priv = to_net_dev(dev)->ml_priv;
93         struct cmd_ds_mesh_access mesh_access;
94         int ret;
95         unsigned long retry_limit;
96
97         memset(&mesh_access, 0, sizeof(mesh_access));
98         mesh_access.data[0] = cpu_to_le32(CMD_ACT_SET);
99
100         if (!strict_strtoul(buf, 10, &retry_limit))
101                 return -ENOTSUPP;
102         if (retry_limit > 15)
103                 return -ENOTSUPP;
104
105         mesh_access.data[1] = cpu_to_le32(retry_limit);
106
107         ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_GET_PRB_RSP_LIMIT,
108                         &mesh_access);
109         if (ret)
110                 return ret;
111
112         return strlen(buf);
113 }
114
115 /**
116  * Get function for sysfs attribute mesh
117  */
118 static ssize_t lbs_mesh_get(struct device *dev,
119                 struct device_attribute *attr, char * buf)
120 {
121         struct lbs_private *priv = to_net_dev(dev)->ml_priv;
122         return snprintf(buf, 5, "0x%X\n", !!priv->mesh_dev);
123 }
124
125 /**
126  *  Set function for sysfs attribute mesh
127  */
128 static ssize_t lbs_mesh_set(struct device *dev,
129                 struct device_attribute *attr, const char * buf, size_t count)
130 {
131         struct lbs_private *priv = to_net_dev(dev)->ml_priv;
132         int enable;
133         int ret, action = CMD_ACT_MESH_CONFIG_STOP;
134
135         sscanf(buf, "%x", &enable);
136         enable = !!enable;
137         if (enable == !!priv->mesh_dev)
138                 return count;
139         if (enable)
140                 action = CMD_ACT_MESH_CONFIG_START;
141         ret = lbs_mesh_config(priv, action, priv->channel);
142         if (ret)
143                 return ret;
144
145         if (enable)
146                 lbs_add_mesh(priv);
147         else
148                 lbs_remove_mesh(priv);
149
150         return count;
151 }
152
153 /**
154  * lbs_mesh attribute to be exported per ethX interface
155  * through sysfs (/sys/class/net/ethX/lbs_mesh)
156  */
157 static DEVICE_ATTR(lbs_mesh, 0644, lbs_mesh_get, lbs_mesh_set);
158
159 /**
160  * anycast_mask attribute to be exported per mshX interface
161  * through sysfs (/sys/class/net/mshX/anycast_mask)
162  */
163 static DEVICE_ATTR(anycast_mask, 0644, lbs_anycast_get, lbs_anycast_set);
164
165 /**
166  * prb_rsp_limit attribute to be exported per mshX interface
167  * through sysfs (/sys/class/net/mshX/prb_rsp_limit)
168  */
169 static DEVICE_ATTR(prb_rsp_limit, 0644, lbs_prb_rsp_limit_get,
170                 lbs_prb_rsp_limit_set);
171
172 static struct attribute *lbs_mesh_sysfs_entries[] = {
173         &dev_attr_anycast_mask.attr,
174         &dev_attr_prb_rsp_limit.attr,
175         NULL,
176 };
177
178 static struct attribute_group lbs_mesh_attr_group = {
179         .attrs = lbs_mesh_sysfs_entries,
180 };
181
182
183
184 /***************************************************************************
185  * Initializing and starting, stopping mesh
186  */
187
188 /*
189  * Check mesh FW version and appropriately send the mesh start
190  * command
191  */
192 int lbs_init_mesh(struct lbs_private *priv)
193 {
194         struct net_device *dev = priv->dev;
195         int ret = 0;
196
197         lbs_deb_enter(LBS_DEB_MESH);
198
199         priv->mesh_connect_status = LBS_DISCONNECTED;
200
201         /* Determine mesh_fw_ver from fwrelease and fwcapinfo */
202         /* 5.0.16p0 9.0.0.p0 is known to NOT support any mesh */
203         /* 5.110.22 have mesh command with 0xa3 command id */
204         /* 10.0.0.p0 FW brings in mesh config command with different id */
205         /* Check FW version MSB and initialize mesh_fw_ver */
206         if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5) {
207                 /* Enable mesh, if supported, and work out which TLV it uses.
208                    0x100 + 291 is an unofficial value used in 5.110.20.pXX
209                    0x100 + 37 is the official value used in 5.110.21.pXX
210                    but we check them in that order because 20.pXX doesn't
211                    give an error -- it just silently fails. */
212
213                 /* 5.110.20.pXX firmware will fail the command if the channel
214                    doesn't match the existing channel. But only if the TLV
215                    is correct. If the channel is wrong, _BOTH_ versions will
216                    give an error to 0x100+291, and allow 0x100+37 to succeed.
217                    It's just that 5.110.20.pXX will not have done anything
218                    useful */
219
220                 priv->mesh_tlv = TLV_TYPE_OLD_MESH_ID;
221                 if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
222                                     priv->channel)) {
223                         priv->mesh_tlv = TLV_TYPE_MESH_ID;
224                         if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
225                                             priv->channel))
226                                 priv->mesh_tlv = 0;
227                 }
228         } else
229         if ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
230                 (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK)) {
231                 /* 10.0.0.pXX new firmwares should succeed with TLV
232                  * 0x100+37; Do not invoke command with old TLV.
233                  */
234                 priv->mesh_tlv = TLV_TYPE_MESH_ID;
235                 if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
236                                     priv->channel))
237                         priv->mesh_tlv = 0;
238         }
239
240
241         if (priv->mesh_tlv) {
242                 sprintf(priv->mesh_ssid, "mesh");
243                 priv->mesh_ssid_len = 4;
244
245                 lbs_add_mesh(priv);
246
247                 if (device_create_file(&dev->dev, &dev_attr_lbs_mesh))
248                         lbs_pr_err("cannot register lbs_mesh attribute\n");
249
250                 ret = 1;
251         }
252
253         lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
254         return ret;
255 }
256
257
258 int lbs_deinit_mesh(struct lbs_private *priv)
259 {
260         struct net_device *dev = priv->dev;
261         int ret = 0;
262
263         lbs_deb_enter(LBS_DEB_MESH);
264
265         if (priv->mesh_tlv) {
266                 device_remove_file(&dev->dev, &dev_attr_lbs_mesh);
267                 ret = 1;
268         }
269
270         lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
271         return ret;
272 }
273
274
275 /**
276  *  @brief This function closes the mshX interface
277  *
278  *  @param dev     A pointer to net_device structure
279  *  @return        0
280  */
281 static int lbs_mesh_stop(struct net_device *dev)
282 {
283         struct lbs_private *priv = dev->ml_priv;
284
285         lbs_deb_enter(LBS_DEB_MESH);
286         spin_lock_irq(&priv->driver_lock);
287
288         priv->mesh_open = 0;
289         priv->mesh_connect_status = LBS_DISCONNECTED;
290
291         netif_stop_queue(dev);
292         netif_carrier_off(dev);
293
294         spin_unlock_irq(&priv->driver_lock);
295
296         schedule_work(&priv->mcast_work);
297
298         lbs_deb_leave(LBS_DEB_MESH);
299         return 0;
300 }
301
302 /**
303  *  @brief This function opens the mshX interface
304  *
305  *  @param dev     A pointer to net_device structure
306  *  @return        0 or -EBUSY if monitor mode active
307  */
308 static int lbs_mesh_dev_open(struct net_device *dev)
309 {
310         struct lbs_private *priv = dev->ml_priv;
311         int ret = 0;
312
313         lbs_deb_enter(LBS_DEB_NET);
314
315         spin_lock_irq(&priv->driver_lock);
316
317         if (priv->monitormode) {
318                 ret = -EBUSY;
319                 goto out;
320         }
321
322         priv->mesh_open = 1;
323         priv->mesh_connect_status = LBS_CONNECTED;
324         netif_carrier_on(dev);
325
326         if (!priv->tx_pending_len)
327                 netif_wake_queue(dev);
328  out:
329
330         spin_unlock_irq(&priv->driver_lock);
331         lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
332         return ret;
333 }
334
335 static const struct net_device_ops mesh_netdev_ops = {
336         .ndo_open               = lbs_mesh_dev_open,
337         .ndo_stop               = lbs_mesh_stop,
338         .ndo_start_xmit         = lbs_hard_start_xmit,
339         .ndo_set_mac_address    = lbs_set_mac_address,
340         .ndo_set_multicast_list = lbs_set_multicast_list,
341 };
342
343 /**
344  * @brief This function adds mshX interface
345  *
346  *  @param priv    A pointer to the struct lbs_private structure
347  *  @return        0 if successful, -X otherwise
348  */
349 int lbs_add_mesh(struct lbs_private *priv)
350 {
351         struct net_device *mesh_dev = NULL;
352         int ret = 0;
353
354         lbs_deb_enter(LBS_DEB_MESH);
355
356         /* Allocate a virtual mesh device */
357         mesh_dev = alloc_netdev(0, "msh%d", ether_setup);
358         if (!mesh_dev) {
359                 lbs_deb_mesh("init mshX device failed\n");
360                 ret = -ENOMEM;
361                 goto done;
362         }
363         mesh_dev->ml_priv = priv;
364         priv->mesh_dev = mesh_dev;
365
366         mesh_dev->netdev_ops = &mesh_netdev_ops;
367         mesh_dev->ethtool_ops = &lbs_ethtool_ops;
368         memcpy(mesh_dev->dev_addr, priv->dev->dev_addr, ETH_ALEN);
369
370         SET_NETDEV_DEV(priv->mesh_dev, priv->dev->dev.parent);
371
372 #ifdef  WIRELESS_EXT
373         mesh_dev->wireless_handlers = &mesh_handler_def;
374 #endif
375         mesh_dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
376         /* Register virtual mesh interface */
377         ret = register_netdev(mesh_dev);
378         if (ret) {
379                 lbs_pr_err("cannot register mshX virtual interface\n");
380                 goto err_free;
381         }
382
383         ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
384         if (ret)
385                 goto err_unregister;
386
387         lbs_persist_config_init(mesh_dev);
388
389         /* Everything successful */
390         ret = 0;
391         goto done;
392
393 err_unregister:
394         unregister_netdev(mesh_dev);
395
396 err_free:
397         free_netdev(mesh_dev);
398
399 done:
400         lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
401         return ret;
402 }
403
404 void lbs_remove_mesh(struct lbs_private *priv)
405 {
406         struct net_device *mesh_dev;
407
408         mesh_dev = priv->mesh_dev;
409         if (!mesh_dev)
410                 return;
411
412         lbs_deb_enter(LBS_DEB_MESH);
413         netif_stop_queue(mesh_dev);
414         netif_carrier_off(mesh_dev);
415         sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
416         lbs_persist_config_remove(mesh_dev);
417         unregister_netdev(mesh_dev);
418         priv->mesh_dev = NULL;
419         free_netdev(mesh_dev);
420         lbs_deb_leave(LBS_DEB_MESH);
421 }
422
423
424
425 /***************************************************************************
426  * Sending and receiving
427  */
428 struct net_device *lbs_mesh_set_dev(struct lbs_private *priv,
429         struct net_device *dev, struct rxpd *rxpd)
430 {
431         if (priv->mesh_dev) {
432                 if (priv->mesh_tlv == TLV_TYPE_OLD_MESH_ID) {
433                         if (rxpd->rx_control & RxPD_MESH_FRAME)
434                                 dev = priv->mesh_dev;
435                 } else if (priv->mesh_tlv == TLV_TYPE_MESH_ID) {
436                         if (rxpd->u.bss.bss_num == MESH_IFACE_ID)
437                                 dev = priv->mesh_dev;
438                 }
439         }
440         return dev;
441 }
442
443
444 void lbs_mesh_set_txpd(struct lbs_private *priv,
445         struct net_device *dev, struct txpd *txpd)
446 {
447         if (dev == priv->mesh_dev) {
448                 if (priv->mesh_tlv == TLV_TYPE_OLD_MESH_ID)
449                         txpd->tx_control |= cpu_to_le32(TxPD_MESH_FRAME);
450                 else if (priv->mesh_tlv == TLV_TYPE_MESH_ID)
451                         txpd->u.bss.bss_num = MESH_IFACE_ID;
452         }
453 }
454
455
456 /***************************************************************************
457  * Mesh command handling
458  */
459
460 int lbs_cmd_bt_access(struct cmd_ds_command *cmd,
461                                u16 cmd_action, void *pdata_buf)
462 {
463         struct cmd_ds_bt_access *bt_access = &cmd->params.bt;
464         lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
465
466         cmd->command = cpu_to_le16(CMD_BT_ACCESS);
467         cmd->size = cpu_to_le16(sizeof(struct cmd_ds_bt_access) +
468                 sizeof(struct cmd_header));
469         cmd->result = 0;
470         bt_access->action = cpu_to_le16(cmd_action);
471
472         switch (cmd_action) {
473         case CMD_ACT_BT_ACCESS_ADD:
474                 memcpy(bt_access->addr1, pdata_buf, 2 * ETH_ALEN);
475                 lbs_deb_hex(LBS_DEB_MESH, "BT_ADD: blinded MAC addr",
476                         bt_access->addr1, 6);
477                 break;
478         case CMD_ACT_BT_ACCESS_DEL:
479                 memcpy(bt_access->addr1, pdata_buf, 1 * ETH_ALEN);
480                 lbs_deb_hex(LBS_DEB_MESH, "BT_DEL: blinded MAC addr",
481                         bt_access->addr1, 6);
482                 break;
483         case CMD_ACT_BT_ACCESS_LIST:
484                 bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
485                 break;
486         case CMD_ACT_BT_ACCESS_RESET:
487                 break;
488         case CMD_ACT_BT_ACCESS_SET_INVERT:
489                 bt_access->id = cpu_to_le32(*(u32 *) pdata_buf);
490                 break;
491         case CMD_ACT_BT_ACCESS_GET_INVERT:
492                 break;
493         default:
494                 break;
495         }
496         lbs_deb_leave(LBS_DEB_CMD);
497         return 0;
498 }
499
500 int lbs_cmd_fwt_access(struct cmd_ds_command *cmd,
501                                u16 cmd_action, void *pdata_buf)
502 {
503         struct cmd_ds_fwt_access *fwt_access = &cmd->params.fwt;
504         lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
505
506         cmd->command = cpu_to_le16(CMD_FWT_ACCESS);
507         cmd->size = cpu_to_le16(sizeof(struct cmd_ds_fwt_access) +
508                 sizeof(struct cmd_header));
509         cmd->result = 0;
510
511         if (pdata_buf)
512                 memcpy(fwt_access, pdata_buf, sizeof(*fwt_access));
513         else
514                 memset(fwt_access, 0, sizeof(*fwt_access));
515
516         fwt_access->action = cpu_to_le16(cmd_action);
517
518         lbs_deb_leave(LBS_DEB_CMD);
519         return 0;
520 }
521
522 int lbs_mesh_access(struct lbs_private *priv, uint16_t cmd_action,
523                     struct cmd_ds_mesh_access *cmd)
524 {
525         int ret;
526
527         lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
528
529         cmd->hdr.command = cpu_to_le16(CMD_MESH_ACCESS);
530         cmd->hdr.size = cpu_to_le16(sizeof(*cmd));
531         cmd->hdr.result = 0;
532
533         cmd->action = cpu_to_le16(cmd_action);
534
535         ret = lbs_cmd_with_response(priv, CMD_MESH_ACCESS, cmd);
536
537         lbs_deb_leave(LBS_DEB_CMD);
538         return ret;
539 }
540
541 static int __lbs_mesh_config_send(struct lbs_private *priv,
542                                   struct cmd_ds_mesh_config *cmd,
543                                   uint16_t action, uint16_t type)
544 {
545         int ret;
546         u16 command = CMD_MESH_CONFIG_OLD;
547
548         lbs_deb_enter(LBS_DEB_CMD);
549
550         /*
551          * Command id is 0xac for v10 FW along with mesh interface
552          * id in bits 14-13-12.
553          */
554         if (priv->mesh_tlv == TLV_TYPE_MESH_ID)
555                 command = CMD_MESH_CONFIG |
556                           (MESH_IFACE_ID << MESH_IFACE_BIT_OFFSET);
557
558         cmd->hdr.command = cpu_to_le16(command);
559         cmd->hdr.size = cpu_to_le16(sizeof(struct cmd_ds_mesh_config));
560         cmd->hdr.result = 0;
561
562         cmd->type = cpu_to_le16(type);
563         cmd->action = cpu_to_le16(action);
564
565         ret = lbs_cmd_with_response(priv, command, cmd);
566
567         lbs_deb_leave(LBS_DEB_CMD);
568         return ret;
569 }
570
571 int lbs_mesh_config_send(struct lbs_private *priv,
572                          struct cmd_ds_mesh_config *cmd,
573                          uint16_t action, uint16_t type)
574 {
575         int ret;
576
577         if (!(priv->fwcapinfo & FW_CAPINFO_PERSISTENT_CONFIG))
578                 return -EOPNOTSUPP;
579
580         ret = __lbs_mesh_config_send(priv, cmd, action, type);
581         return ret;
582 }
583
584 /* This function is the CMD_MESH_CONFIG legacy function.  It only handles the
585  * START and STOP actions.  The extended actions supported by CMD_MESH_CONFIG
586  * are all handled by preparing a struct cmd_ds_mesh_config and passing it to
587  * lbs_mesh_config_send.
588  */
589 int lbs_mesh_config(struct lbs_private *priv, uint16_t action, uint16_t chan)
590 {
591         struct cmd_ds_mesh_config cmd;
592         struct mrvl_meshie *ie;
593         DECLARE_SSID_BUF(ssid);
594
595         memset(&cmd, 0, sizeof(cmd));
596         cmd.channel = cpu_to_le16(chan);
597         ie = (struct mrvl_meshie *)cmd.data;
598
599         switch (action) {
600         case CMD_ACT_MESH_CONFIG_START:
601                 ie->id = WLAN_EID_GENERIC;
602                 ie->val.oui[0] = 0x00;
603                 ie->val.oui[1] = 0x50;
604                 ie->val.oui[2] = 0x43;
605                 ie->val.type = MARVELL_MESH_IE_TYPE;
606                 ie->val.subtype = MARVELL_MESH_IE_SUBTYPE;
607                 ie->val.version = MARVELL_MESH_IE_VERSION;
608                 ie->val.active_protocol_id = MARVELL_MESH_PROTO_ID_HWMP;
609                 ie->val.active_metric_id = MARVELL_MESH_METRIC_ID;
610                 ie->val.mesh_capability = MARVELL_MESH_CAPABILITY;
611                 ie->val.mesh_id_len = priv->mesh_ssid_len;
612                 memcpy(ie->val.mesh_id, priv->mesh_ssid, priv->mesh_ssid_len);
613                 ie->len = sizeof(struct mrvl_meshie_val) -
614                         IEEE80211_MAX_SSID_LEN + priv->mesh_ssid_len;
615                 cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie_val));
616                 break;
617         case CMD_ACT_MESH_CONFIG_STOP:
618                 break;
619         default:
620                 return -1;
621         }
622         lbs_deb_cmd("mesh config action %d type %x channel %d SSID %s\n",
623                     action, priv->mesh_tlv, chan,
624                     print_ssid(ssid, priv->mesh_ssid, priv->mesh_ssid_len));
625
626         return __lbs_mesh_config_send(priv, &cmd, action, priv->mesh_tlv);
627 }
628
629
630
631 /***************************************************************************
632  * Persistent configuration support
633  */
634
635 static int mesh_get_default_parameters(struct device *dev,
636                                        struct mrvl_mesh_defaults *defs)
637 {
638         struct lbs_private *priv = to_net_dev(dev)->ml_priv;
639         struct cmd_ds_mesh_config cmd;
640         int ret;
641
642         memset(&cmd, 0, sizeof(struct cmd_ds_mesh_config));
643         ret = lbs_mesh_config_send(priv, &cmd, CMD_ACT_MESH_CONFIG_GET,
644                                    CMD_TYPE_MESH_GET_DEFAULTS);
645
646         if (ret)
647                 return -EOPNOTSUPP;
648
649         memcpy(defs, &cmd.data[0], sizeof(struct mrvl_mesh_defaults));
650
651         return 0;
652 }
653
654 /**
655  * @brief Get function for sysfs attribute bootflag
656  */
657 static ssize_t bootflag_get(struct device *dev,
658                             struct device_attribute *attr, char *buf)
659 {
660         struct mrvl_mesh_defaults defs;
661         int ret;
662
663         ret = mesh_get_default_parameters(dev, &defs);
664
665         if (ret)
666                 return ret;
667
668         return snprintf(buf, 12, "%d\n", le32_to_cpu(defs.bootflag));
669 }
670
671 /**
672  * @brief Set function for sysfs attribute bootflag
673  */
674 static ssize_t bootflag_set(struct device *dev, struct device_attribute *attr,
675                             const char *buf, size_t count)
676 {
677         struct lbs_private *priv = to_net_dev(dev)->ml_priv;
678         struct cmd_ds_mesh_config cmd;
679         uint32_t datum;
680         int ret;
681
682         memset(&cmd, 0, sizeof(cmd));
683         ret = sscanf(buf, "%d", &datum);
684         if ((ret != 1) || (datum > 1))
685                 return -EINVAL;
686
687         *((__le32 *)&cmd.data[0]) = cpu_to_le32(!!datum);
688         cmd.length = cpu_to_le16(sizeof(uint32_t));
689         ret = lbs_mesh_config_send(priv, &cmd, CMD_ACT_MESH_CONFIG_SET,
690                                    CMD_TYPE_MESH_SET_BOOTFLAG);
691         if (ret)
692                 return ret;
693
694         return strlen(buf);
695 }
696
697 /**
698  * @brief Get function for sysfs attribute boottime
699  */
700 static ssize_t boottime_get(struct device *dev,
701                             struct device_attribute *attr, char *buf)
702 {
703         struct mrvl_mesh_defaults defs;
704         int ret;
705
706         ret = mesh_get_default_parameters(dev, &defs);
707
708         if (ret)
709                 return ret;
710
711         return snprintf(buf, 12, "%d\n", defs.boottime);
712 }
713
714 /**
715  * @brief Set function for sysfs attribute boottime
716  */
717 static ssize_t boottime_set(struct device *dev,
718                 struct device_attribute *attr, const char *buf, size_t count)
719 {
720         struct lbs_private *priv = to_net_dev(dev)->ml_priv;
721         struct cmd_ds_mesh_config cmd;
722         uint32_t datum;
723         int ret;
724
725         memset(&cmd, 0, sizeof(cmd));
726         ret = sscanf(buf, "%d", &datum);
727         if ((ret != 1) || (datum > 255))
728                 return -EINVAL;
729
730         /* A too small boot time will result in the device booting into
731          * standalone (no-host) mode before the host can take control of it,
732          * so the change will be hard to revert.  This may be a desired
733          * feature (e.g to configure a very fast boot time for devices that
734          * will not be attached to a host), but dangerous.  So I'm enforcing a
735          * lower limit of 20 seconds:  remove and recompile the driver if this
736          * does not work for you.
737          */
738         datum = (datum < 20) ? 20 : datum;
739         cmd.data[0] = datum;
740         cmd.length = cpu_to_le16(sizeof(uint8_t));
741         ret = lbs_mesh_config_send(priv, &cmd, CMD_ACT_MESH_CONFIG_SET,
742                                    CMD_TYPE_MESH_SET_BOOTTIME);
743         if (ret)
744                 return ret;
745
746         return strlen(buf);
747 }
748
749 /**
750  * @brief Get function for sysfs attribute channel
751  */
752 static ssize_t channel_get(struct device *dev,
753                            struct device_attribute *attr, char *buf)
754 {
755         struct mrvl_mesh_defaults defs;
756         int ret;
757
758         ret = mesh_get_default_parameters(dev, &defs);
759
760         if (ret)
761                 return ret;
762
763         return snprintf(buf, 12, "%d\n", le16_to_cpu(defs.channel));
764 }
765
766 /**
767  * @brief Set function for sysfs attribute channel
768  */
769 static ssize_t channel_set(struct device *dev, struct device_attribute *attr,
770                            const char *buf, size_t count)
771 {
772         struct lbs_private *priv = to_net_dev(dev)->ml_priv;
773         struct cmd_ds_mesh_config cmd;
774         uint32_t datum;
775         int ret;
776
777         memset(&cmd, 0, sizeof(cmd));
778         ret = sscanf(buf, "%d", &datum);
779         if (ret != 1 || datum < 1 || datum > 11)
780                 return -EINVAL;
781
782         *((__le16 *)&cmd.data[0]) = cpu_to_le16(datum);
783         cmd.length = cpu_to_le16(sizeof(uint16_t));
784         ret = lbs_mesh_config_send(priv, &cmd, CMD_ACT_MESH_CONFIG_SET,
785                                    CMD_TYPE_MESH_SET_DEF_CHANNEL);
786         if (ret)
787                 return ret;
788
789         return strlen(buf);
790 }
791
792 /**
793  * @brief Get function for sysfs attribute mesh_id
794  */
795 static ssize_t mesh_id_get(struct device *dev, struct device_attribute *attr,
796                            char *buf)
797 {
798         struct mrvl_mesh_defaults defs;
799         int maxlen;
800         int ret;
801
802         ret = mesh_get_default_parameters(dev, &defs);
803
804         if (ret)
805                 return ret;
806
807         if (defs.meshie.val.mesh_id_len > IEEE80211_MAX_SSID_LEN) {
808                 lbs_pr_err("inconsistent mesh ID length");
809                 defs.meshie.val.mesh_id_len = IEEE80211_MAX_SSID_LEN;
810         }
811
812         /* SSID not null terminated: reserve room for \0 + \n */
813         maxlen = defs.meshie.val.mesh_id_len + 2;
814         maxlen = (PAGE_SIZE > maxlen) ? maxlen : PAGE_SIZE;
815
816         defs.meshie.val.mesh_id[defs.meshie.val.mesh_id_len] = '\0';
817
818         return snprintf(buf, maxlen, "%s\n", defs.meshie.val.mesh_id);
819 }
820
821 /**
822  * @brief Set function for sysfs attribute mesh_id
823  */
824 static ssize_t mesh_id_set(struct device *dev, struct device_attribute *attr,
825                            const char *buf, size_t count)
826 {
827         struct cmd_ds_mesh_config cmd;
828         struct mrvl_mesh_defaults defs;
829         struct mrvl_meshie *ie;
830         struct lbs_private *priv = to_net_dev(dev)->ml_priv;
831         int len;
832         int ret;
833
834         if (count < 2 || count > IEEE80211_MAX_SSID_LEN + 1)
835                 return -EINVAL;
836
837         memset(&cmd, 0, sizeof(struct cmd_ds_mesh_config));
838         ie = (struct mrvl_meshie *) &cmd.data[0];
839
840         /* fetch all other Information Element parameters */
841         ret = mesh_get_default_parameters(dev, &defs);
842
843         cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie));
844
845         /* transfer IE elements */
846         memcpy(ie, &defs.meshie, sizeof(struct mrvl_meshie));
847
848         len = count - 1;
849         memcpy(ie->val.mesh_id, buf, len);
850         /* SSID len */
851         ie->val.mesh_id_len = len;
852         /* IE len */
853         ie->len = sizeof(struct mrvl_meshie_val) - IEEE80211_MAX_SSID_LEN + len;
854
855         ret = lbs_mesh_config_send(priv, &cmd, CMD_ACT_MESH_CONFIG_SET,
856                                    CMD_TYPE_MESH_SET_MESH_IE);
857         if (ret)
858                 return ret;
859
860         return strlen(buf);
861 }
862
863 /**
864  * @brief Get function for sysfs attribute protocol_id
865  */
866 static ssize_t protocol_id_get(struct device *dev,
867                                struct device_attribute *attr, char *buf)
868 {
869         struct mrvl_mesh_defaults defs;
870         int ret;
871
872         ret = mesh_get_default_parameters(dev, &defs);
873
874         if (ret)
875                 return ret;
876
877         return snprintf(buf, 5, "%d\n", defs.meshie.val.active_protocol_id);
878 }
879
880 /**
881  * @brief Set function for sysfs attribute protocol_id
882  */
883 static ssize_t protocol_id_set(struct device *dev,
884                 struct device_attribute *attr, const char *buf, size_t count)
885 {
886         struct cmd_ds_mesh_config cmd;
887         struct mrvl_mesh_defaults defs;
888         struct mrvl_meshie *ie;
889         struct lbs_private *priv = to_net_dev(dev)->ml_priv;
890         uint32_t datum;
891         int ret;
892
893         memset(&cmd, 0, sizeof(cmd));
894         ret = sscanf(buf, "%d", &datum);
895         if ((ret != 1) || (datum > 255))
896                 return -EINVAL;
897
898         /* fetch all other Information Element parameters */
899         ret = mesh_get_default_parameters(dev, &defs);
900
901         cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie));
902
903         /* transfer IE elements */
904         ie = (struct mrvl_meshie *) &cmd.data[0];
905         memcpy(ie, &defs.meshie, sizeof(struct mrvl_meshie));
906         /* update protocol id */
907         ie->val.active_protocol_id = datum;
908
909         ret = lbs_mesh_config_send(priv, &cmd, CMD_ACT_MESH_CONFIG_SET,
910                                    CMD_TYPE_MESH_SET_MESH_IE);
911         if (ret)
912                 return ret;
913
914         return strlen(buf);
915 }
916
917 /**
918  * @brief Get function for sysfs attribute metric_id
919  */
920 static ssize_t metric_id_get(struct device *dev,
921                 struct device_attribute *attr, char *buf)
922 {
923         struct mrvl_mesh_defaults defs;
924         int ret;
925
926         ret = mesh_get_default_parameters(dev, &defs);
927
928         if (ret)
929                 return ret;
930
931         return snprintf(buf, 5, "%d\n", defs.meshie.val.active_metric_id);
932 }
933
934 /**
935  * @brief Set function for sysfs attribute metric_id
936  */
937 static ssize_t metric_id_set(struct device *dev, struct device_attribute *attr,
938                              const char *buf, size_t count)
939 {
940         struct cmd_ds_mesh_config cmd;
941         struct mrvl_mesh_defaults defs;
942         struct mrvl_meshie *ie;
943         struct lbs_private *priv = to_net_dev(dev)->ml_priv;
944         uint32_t datum;
945         int ret;
946
947         memset(&cmd, 0, sizeof(cmd));
948         ret = sscanf(buf, "%d", &datum);
949         if ((ret != 1) || (datum > 255))
950                 return -EINVAL;
951
952         /* fetch all other Information Element parameters */
953         ret = mesh_get_default_parameters(dev, &defs);
954
955         cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie));
956
957         /* transfer IE elements */
958         ie = (struct mrvl_meshie *) &cmd.data[0];
959         memcpy(ie, &defs.meshie, sizeof(struct mrvl_meshie));
960         /* update metric id */
961         ie->val.active_metric_id = datum;
962
963         ret = lbs_mesh_config_send(priv, &cmd, CMD_ACT_MESH_CONFIG_SET,
964                                    CMD_TYPE_MESH_SET_MESH_IE);
965         if (ret)
966                 return ret;
967
968         return strlen(buf);
969 }
970
971 /**
972  * @brief Get function for sysfs attribute capability
973  */
974 static ssize_t capability_get(struct device *dev,
975                 struct device_attribute *attr, char *buf)
976 {
977         struct mrvl_mesh_defaults defs;
978         int ret;
979
980         ret = mesh_get_default_parameters(dev, &defs);
981
982         if (ret)
983                 return ret;
984
985         return snprintf(buf, 5, "%d\n", defs.meshie.val.mesh_capability);
986 }
987
988 /**
989  * @brief Set function for sysfs attribute capability
990  */
991 static ssize_t capability_set(struct device *dev, struct device_attribute *attr,
992                               const char *buf, size_t count)
993 {
994         struct cmd_ds_mesh_config cmd;
995         struct mrvl_mesh_defaults defs;
996         struct mrvl_meshie *ie;
997         struct lbs_private *priv = to_net_dev(dev)->ml_priv;
998         uint32_t datum;
999         int ret;
1000
1001         memset(&cmd, 0, sizeof(cmd));
1002         ret = sscanf(buf, "%d", &datum);
1003         if ((ret != 1) || (datum > 255))
1004                 return -EINVAL;
1005
1006         /* fetch all other Information Element parameters */
1007         ret = mesh_get_default_parameters(dev, &defs);
1008
1009         cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie));
1010
1011         /* transfer IE elements */
1012         ie = (struct mrvl_meshie *) &cmd.data[0];
1013         memcpy(ie, &defs.meshie, sizeof(struct mrvl_meshie));
1014         /* update value */
1015         ie->val.mesh_capability = datum;
1016
1017         ret = lbs_mesh_config_send(priv, &cmd, CMD_ACT_MESH_CONFIG_SET,
1018                                    CMD_TYPE_MESH_SET_MESH_IE);
1019         if (ret)
1020                 return ret;
1021
1022         return strlen(buf);
1023 }
1024
1025
1026 static DEVICE_ATTR(bootflag, 0644, bootflag_get, bootflag_set);
1027 static DEVICE_ATTR(boottime, 0644, boottime_get, boottime_set);
1028 static DEVICE_ATTR(channel, 0644, channel_get, channel_set);
1029 static DEVICE_ATTR(mesh_id, 0644, mesh_id_get, mesh_id_set);
1030 static DEVICE_ATTR(protocol_id, 0644, protocol_id_get, protocol_id_set);
1031 static DEVICE_ATTR(metric_id, 0644, metric_id_get, metric_id_set);
1032 static DEVICE_ATTR(capability, 0644, capability_get, capability_set);
1033
1034 static struct attribute *boot_opts_attrs[] = {
1035         &dev_attr_bootflag.attr,
1036         &dev_attr_boottime.attr,
1037         &dev_attr_channel.attr,
1038         NULL
1039 };
1040
1041 static struct attribute_group boot_opts_group = {
1042         .name = "boot_options",
1043         .attrs = boot_opts_attrs,
1044 };
1045
1046 static struct attribute *mesh_ie_attrs[] = {
1047         &dev_attr_mesh_id.attr,
1048         &dev_attr_protocol_id.attr,
1049         &dev_attr_metric_id.attr,
1050         &dev_attr_capability.attr,
1051         NULL
1052 };
1053
1054 static struct attribute_group mesh_ie_group = {
1055         .name = "mesh_ie",
1056         .attrs = mesh_ie_attrs,
1057 };
1058
1059 void lbs_persist_config_init(struct net_device *dev)
1060 {
1061         int ret;
1062         ret = sysfs_create_group(&(dev->dev.kobj), &boot_opts_group);
1063         ret = sysfs_create_group(&(dev->dev.kobj), &mesh_ie_group);
1064 }
1065
1066 void lbs_persist_config_remove(struct net_device *dev)
1067 {
1068         sysfs_remove_group(&(dev->dev.kobj), &boot_opts_group);
1069         sysfs_remove_group(&(dev->dev.kobj), &mesh_ie_group);
1070 }
1071
1072
1073
1074 /***************************************************************************
1075  * Ethtool related
1076  */
1077
1078 static const char *mesh_stat_strings[] = {
1079                         "drop_duplicate_bcast",
1080                         "drop_ttl_zero",
1081                         "drop_no_fwd_route",
1082                         "drop_no_buffers",
1083                         "fwded_unicast_cnt",
1084                         "fwded_bcast_cnt",
1085                         "drop_blind_table",
1086                         "tx_failed_cnt"
1087 };
1088
1089 void lbs_mesh_ethtool_get_stats(struct net_device *dev,
1090         struct ethtool_stats *stats, uint64_t *data)
1091 {
1092         struct lbs_private *priv = dev->ml_priv;
1093         struct cmd_ds_mesh_access mesh_access;
1094         int ret;
1095
1096         lbs_deb_enter(LBS_DEB_ETHTOOL);
1097
1098         /* Get Mesh Statistics */
1099         ret = lbs_mesh_access(priv, CMD_ACT_MESH_GET_STATS, &mesh_access);
1100
1101         if (ret) {
1102                 memset(data, 0, MESH_STATS_NUM*(sizeof(uint64_t)));
1103                 return;
1104         }
1105
1106         priv->mstats.fwd_drop_rbt = le32_to_cpu(mesh_access.data[0]);
1107         priv->mstats.fwd_drop_ttl = le32_to_cpu(mesh_access.data[1]);
1108         priv->mstats.fwd_drop_noroute = le32_to_cpu(mesh_access.data[2]);
1109         priv->mstats.fwd_drop_nobuf = le32_to_cpu(mesh_access.data[3]);
1110         priv->mstats.fwd_unicast_cnt = le32_to_cpu(mesh_access.data[4]);
1111         priv->mstats.fwd_bcast_cnt = le32_to_cpu(mesh_access.data[5]);
1112         priv->mstats.drop_blind = le32_to_cpu(mesh_access.data[6]);
1113         priv->mstats.tx_failed_cnt = le32_to_cpu(mesh_access.data[7]);
1114
1115         data[0] = priv->mstats.fwd_drop_rbt;
1116         data[1] = priv->mstats.fwd_drop_ttl;
1117         data[2] = priv->mstats.fwd_drop_noroute;
1118         data[3] = priv->mstats.fwd_drop_nobuf;
1119         data[4] = priv->mstats.fwd_unicast_cnt;
1120         data[5] = priv->mstats.fwd_bcast_cnt;
1121         data[6] = priv->mstats.drop_blind;
1122         data[7] = priv->mstats.tx_failed_cnt;
1123
1124         lbs_deb_enter(LBS_DEB_ETHTOOL);
1125 }
1126
1127 int lbs_mesh_ethtool_get_sset_count(struct net_device *dev, int sset)
1128 {
1129         struct lbs_private *priv = dev->ml_priv;
1130
1131         if (sset == ETH_SS_STATS && dev == priv->mesh_dev)
1132                 return MESH_STATS_NUM;
1133
1134         return -EOPNOTSUPP;
1135 }
1136
1137 void lbs_mesh_ethtool_get_strings(struct net_device *dev,
1138         uint32_t stringset, uint8_t *s)
1139 {
1140         int i;
1141
1142         lbs_deb_enter(LBS_DEB_ETHTOOL);
1143
1144         switch (stringset) {
1145         case ETH_SS_STATS:
1146                 for (i = 0; i < MESH_STATS_NUM; i++) {
1147                         memcpy(s + i * ETH_GSTRING_LEN,
1148                                         mesh_stat_strings[i],
1149                                         ETH_GSTRING_LEN);
1150                 }
1151                 break;
1152         }
1153         lbs_deb_enter(LBS_DEB_ETHTOOL);
1154 }