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