Staging: netwave: delete the driver
[linux/fpc-iii.git] / drivers / net / wireless / libertas / mesh.c
blobe385af1f458351faa38601318c7880c508f181f6
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>
9 #include "mesh.h"
10 #include "decl.h"
11 #include "cmd.h"
14 /***************************************************************************
15 * Mesh sysfs support
18 /**
19 * Attributes exported through sysfs
22 /**
23 * @brief Get function for sysfs attribute anycast_mask
25 static ssize_t lbs_anycast_get(struct device *dev,
26 struct device_attribute *attr, char * buf)
28 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
29 struct cmd_ds_mesh_access mesh_access;
30 int ret;
32 memset(&mesh_access, 0, sizeof(mesh_access));
34 ret = lbs_mesh_access(priv, CMD_ACT_MESH_GET_ANYCAST, &mesh_access);
35 if (ret)
36 return ret;
38 return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
41 /**
42 * @brief Set function for sysfs attribute anycast_mask
44 static ssize_t lbs_anycast_set(struct device *dev,
45 struct device_attribute *attr, const char * buf, size_t count)
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;
52 memset(&mesh_access, 0, sizeof(mesh_access));
53 sscanf(buf, "%x", &datum);
54 mesh_access.data[0] = cpu_to_le32(datum);
56 ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_ANYCAST, &mesh_access);
57 if (ret)
58 return ret;
60 return strlen(buf);
63 /**
64 * @brief Get function for sysfs attribute prb_rsp_limit
66 static ssize_t lbs_prb_rsp_limit_get(struct device *dev,
67 struct device_attribute *attr, char *buf)
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;
74 memset(&mesh_access, 0, sizeof(mesh_access));
75 mesh_access.data[0] = cpu_to_le32(CMD_ACT_GET);
77 ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_GET_PRB_RSP_LIMIT,
78 &mesh_access);
79 if (ret)
80 return ret;
82 retry_limit = le32_to_cpu(mesh_access.data[1]);
83 return snprintf(buf, 10, "%d\n", retry_limit);
86 /**
87 * @brief Set function for sysfs attribute prb_rsp_limit
89 static ssize_t lbs_prb_rsp_limit_set(struct device *dev,
90 struct device_attribute *attr, const char *buf, size_t count)
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;
97 memset(&mesh_access, 0, sizeof(mesh_access));
98 mesh_access.data[0] = cpu_to_le32(CMD_ACT_SET);
100 if (!strict_strtoul(buf, 10, &retry_limit))
101 return -ENOTSUPP;
102 if (retry_limit > 15)
103 return -ENOTSUPP;
105 mesh_access.data[1] = cpu_to_le32(retry_limit);
107 ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_GET_PRB_RSP_LIMIT,
108 &mesh_access);
109 if (ret)
110 return ret;
112 return strlen(buf);
116 * Get function for sysfs attribute mesh
118 static ssize_t lbs_mesh_get(struct device *dev,
119 struct device_attribute *attr, char * buf)
121 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
122 return snprintf(buf, 5, "0x%X\n", !!priv->mesh_dev);
126 * Set function for sysfs attribute mesh
128 static ssize_t lbs_mesh_set(struct device *dev,
129 struct device_attribute *attr, const char * buf, size_t count)
131 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
132 int enable;
133 int ret, action = CMD_ACT_MESH_CONFIG_STOP;
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;
145 if (enable)
146 lbs_add_mesh(priv);
147 else
148 lbs_remove_mesh(priv);
150 return count;
154 * lbs_mesh attribute to be exported per ethX interface
155 * through sysfs (/sys/class/net/ethX/lbs_mesh)
157 static DEVICE_ATTR(lbs_mesh, 0644, lbs_mesh_get, lbs_mesh_set);
160 * anycast_mask attribute to be exported per mshX interface
161 * through sysfs (/sys/class/net/mshX/anycast_mask)
163 static DEVICE_ATTR(anycast_mask, 0644, lbs_anycast_get, lbs_anycast_set);
166 * prb_rsp_limit attribute to be exported per mshX interface
167 * through sysfs (/sys/class/net/mshX/prb_rsp_limit)
169 static DEVICE_ATTR(prb_rsp_limit, 0644, lbs_prb_rsp_limit_get,
170 lbs_prb_rsp_limit_set);
172 static struct attribute *lbs_mesh_sysfs_entries[] = {
173 &dev_attr_anycast_mask.attr,
174 &dev_attr_prb_rsp_limit.attr,
175 NULL,
178 static struct attribute_group lbs_mesh_attr_group = {
179 .attrs = lbs_mesh_sysfs_entries,
184 /***************************************************************************
185 * Initializing and starting, stopping mesh
189 * Check mesh FW version and appropriately send the mesh start
190 * command
192 int lbs_init_mesh(struct lbs_private *priv)
194 struct net_device *dev = priv->dev;
195 int ret = 0;
197 lbs_deb_enter(LBS_DEB_MESH);
199 priv->mesh_connect_status = LBS_DISCONNECTED;
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. */
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 */
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;
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.
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;
241 if (priv->mesh_tlv) {
242 sprintf(priv->mesh_ssid, "mesh");
243 priv->mesh_ssid_len = 4;
245 lbs_add_mesh(priv);
247 if (device_create_file(&dev->dev, &dev_attr_lbs_mesh))
248 lbs_pr_err("cannot register lbs_mesh attribute\n");
250 ret = 1;
253 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
254 return ret;
258 int lbs_deinit_mesh(struct lbs_private *priv)
260 struct net_device *dev = priv->dev;
261 int ret = 0;
263 lbs_deb_enter(LBS_DEB_MESH);
265 if (priv->mesh_tlv) {
266 device_remove_file(&dev->dev, &dev_attr_lbs_mesh);
267 ret = 1;
270 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
271 return ret;
276 * @brief This function closes the mshX interface
278 * @param dev A pointer to net_device structure
279 * @return 0
281 static int lbs_mesh_stop(struct net_device *dev)
283 struct lbs_private *priv = dev->ml_priv;
285 lbs_deb_enter(LBS_DEB_MESH);
286 spin_lock_irq(&priv->driver_lock);
288 priv->mesh_open = 0;
289 priv->mesh_connect_status = LBS_DISCONNECTED;
291 netif_stop_queue(dev);
292 netif_carrier_off(dev);
294 spin_unlock_irq(&priv->driver_lock);
296 schedule_work(&priv->mcast_work);
298 lbs_deb_leave(LBS_DEB_MESH);
299 return 0;
303 * @brief This function opens the mshX interface
305 * @param dev A pointer to net_device structure
306 * @return 0 or -EBUSY if monitor mode active
308 static int lbs_mesh_dev_open(struct net_device *dev)
310 struct lbs_private *priv = dev->ml_priv;
311 int ret = 0;
313 lbs_deb_enter(LBS_DEB_NET);
315 spin_lock_irq(&priv->driver_lock);
317 if (priv->monitormode) {
318 ret = -EBUSY;
319 goto out;
322 priv->mesh_open = 1;
323 priv->mesh_connect_status = LBS_CONNECTED;
324 netif_carrier_on(dev);
326 if (!priv->tx_pending_len)
327 netif_wake_queue(dev);
328 out:
330 spin_unlock_irq(&priv->driver_lock);
331 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
332 return ret;
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,
344 * @brief This function adds mshX interface
346 * @param priv A pointer to the struct lbs_private structure
347 * @return 0 if successful, -X otherwise
349 int lbs_add_mesh(struct lbs_private *priv)
351 struct net_device *mesh_dev = NULL;
352 int ret = 0;
354 lbs_deb_enter(LBS_DEB_MESH);
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;
363 mesh_dev->ml_priv = priv;
364 priv->mesh_dev = mesh_dev;
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);
370 SET_NETDEV_DEV(priv->mesh_dev, priv->dev->dev.parent);
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;
383 ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
384 if (ret)
385 goto err_unregister;
387 lbs_persist_config_init(mesh_dev);
389 /* Everything successful */
390 ret = 0;
391 goto done;
393 err_unregister:
394 unregister_netdev(mesh_dev);
396 err_free:
397 free_netdev(mesh_dev);
399 done:
400 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
401 return ret;
404 void lbs_remove_mesh(struct lbs_private *priv)
406 struct net_device *mesh_dev;
408 mesh_dev = priv->mesh_dev;
409 if (!mesh_dev)
410 return;
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);
425 /***************************************************************************
426 * Sending and receiving
428 struct net_device *lbs_mesh_set_dev(struct lbs_private *priv,
429 struct net_device *dev, struct rxpd *rxpd)
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;
440 return dev;
444 void lbs_mesh_set_txpd(struct lbs_private *priv,
445 struct net_device *dev, struct txpd *txpd)
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;
456 /***************************************************************************
457 * Mesh command handling
460 int lbs_cmd_bt_access(struct cmd_ds_command *cmd,
461 u16 cmd_action, void *pdata_buf)
463 struct cmd_ds_bt_access *bt_access = &cmd->params.bt;
464 lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
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);
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;
496 lbs_deb_leave(LBS_DEB_CMD);
497 return 0;
500 int lbs_cmd_fwt_access(struct cmd_ds_command *cmd,
501 u16 cmd_action, void *pdata_buf)
503 struct cmd_ds_fwt_access *fwt_access = &cmd->params.fwt;
504 lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
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;
511 if (pdata_buf)
512 memcpy(fwt_access, pdata_buf, sizeof(*fwt_access));
513 else
514 memset(fwt_access, 0, sizeof(*fwt_access));
516 fwt_access->action = cpu_to_le16(cmd_action);
518 lbs_deb_leave(LBS_DEB_CMD);
519 return 0;
522 int lbs_mesh_access(struct lbs_private *priv, uint16_t cmd_action,
523 struct cmd_ds_mesh_access *cmd)
525 int ret;
527 lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
529 cmd->hdr.command = cpu_to_le16(CMD_MESH_ACCESS);
530 cmd->hdr.size = cpu_to_le16(sizeof(*cmd));
531 cmd->hdr.result = 0;
533 cmd->action = cpu_to_le16(cmd_action);
535 ret = lbs_cmd_with_response(priv, CMD_MESH_ACCESS, cmd);
537 lbs_deb_leave(LBS_DEB_CMD);
538 return ret;
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)
545 int ret;
546 u16 command = CMD_MESH_CONFIG_OLD;
548 lbs_deb_enter(LBS_DEB_CMD);
551 * Command id is 0xac for v10 FW along with mesh interface
552 * id in bits 14-13-12.
554 if (priv->mesh_tlv == TLV_TYPE_MESH_ID)
555 command = CMD_MESH_CONFIG |
556 (MESH_IFACE_ID << MESH_IFACE_BIT_OFFSET);
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;
562 cmd->type = cpu_to_le16(type);
563 cmd->action = cpu_to_le16(action);
565 ret = lbs_cmd_with_response(priv, command, cmd);
567 lbs_deb_leave(LBS_DEB_CMD);
568 return ret;
571 int lbs_mesh_config_send(struct lbs_private *priv,
572 struct cmd_ds_mesh_config *cmd,
573 uint16_t action, uint16_t type)
575 int ret;
577 if (!(priv->fwcapinfo & FW_CAPINFO_PERSISTENT_CONFIG))
578 return -EOPNOTSUPP;
580 ret = __lbs_mesh_config_send(priv, cmd, action, type);
581 return ret;
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.
589 int lbs_mesh_config(struct lbs_private *priv, uint16_t action, uint16_t chan)
591 struct cmd_ds_mesh_config cmd;
592 struct mrvl_meshie *ie;
593 DECLARE_SSID_BUF(ssid);
595 memset(&cmd, 0, sizeof(cmd));
596 cmd.channel = cpu_to_le16(chan);
597 ie = (struct mrvl_meshie *)cmd.data;
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;
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));
626 return __lbs_mesh_config_send(priv, &cmd, action, priv->mesh_tlv);
631 /***************************************************************************
632 * Persistent configuration support
635 static int mesh_get_default_parameters(struct device *dev,
636 struct mrvl_mesh_defaults *defs)
638 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
639 struct cmd_ds_mesh_config cmd;
640 int ret;
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);
646 if (ret)
647 return -EOPNOTSUPP;
649 memcpy(defs, &cmd.data[0], sizeof(struct mrvl_mesh_defaults));
651 return 0;
655 * @brief Get function for sysfs attribute bootflag
657 static ssize_t bootflag_get(struct device *dev,
658 struct device_attribute *attr, char *buf)
660 struct mrvl_mesh_defaults defs;
661 int ret;
663 ret = mesh_get_default_parameters(dev, &defs);
665 if (ret)
666 return ret;
668 return snprintf(buf, 12, "%d\n", le32_to_cpu(defs.bootflag));
672 * @brief Set function for sysfs attribute bootflag
674 static ssize_t bootflag_set(struct device *dev, struct device_attribute *attr,
675 const char *buf, size_t count)
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;
682 memset(&cmd, 0, sizeof(cmd));
683 ret = sscanf(buf, "%d", &datum);
684 if ((ret != 1) || (datum > 1))
685 return -EINVAL;
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;
694 return strlen(buf);
698 * @brief Get function for sysfs attribute boottime
700 static ssize_t boottime_get(struct device *dev,
701 struct device_attribute *attr, char *buf)
703 struct mrvl_mesh_defaults defs;
704 int ret;
706 ret = mesh_get_default_parameters(dev, &defs);
708 if (ret)
709 return ret;
711 return snprintf(buf, 12, "%d\n", defs.boottime);
715 * @brief Set function for sysfs attribute boottime
717 static ssize_t boottime_set(struct device *dev,
718 struct device_attribute *attr, const char *buf, size_t count)
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;
725 memset(&cmd, 0, sizeof(cmd));
726 ret = sscanf(buf, "%d", &datum);
727 if ((ret != 1) || (datum > 255))
728 return -EINVAL;
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.
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;
746 return strlen(buf);
750 * @brief Get function for sysfs attribute channel
752 static ssize_t channel_get(struct device *dev,
753 struct device_attribute *attr, char *buf)
755 struct mrvl_mesh_defaults defs;
756 int ret;
758 ret = mesh_get_default_parameters(dev, &defs);
760 if (ret)
761 return ret;
763 return snprintf(buf, 12, "%d\n", le16_to_cpu(defs.channel));
767 * @brief Set function for sysfs attribute channel
769 static ssize_t channel_set(struct device *dev, struct device_attribute *attr,
770 const char *buf, size_t count)
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;
777 memset(&cmd, 0, sizeof(cmd));
778 ret = sscanf(buf, "%d", &datum);
779 if (ret != 1 || datum < 1 || datum > 11)
780 return -EINVAL;
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;
789 return strlen(buf);
793 * @brief Get function for sysfs attribute mesh_id
795 static ssize_t mesh_id_get(struct device *dev, struct device_attribute *attr,
796 char *buf)
798 struct mrvl_mesh_defaults defs;
799 int maxlen;
800 int ret;
802 ret = mesh_get_default_parameters(dev, &defs);
804 if (ret)
805 return ret;
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;
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;
816 defs.meshie.val.mesh_id[defs.meshie.val.mesh_id_len] = '\0';
818 return snprintf(buf, maxlen, "%s\n", defs.meshie.val.mesh_id);
822 * @brief Set function for sysfs attribute mesh_id
824 static ssize_t mesh_id_set(struct device *dev, struct device_attribute *attr,
825 const char *buf, size_t count)
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;
834 if (count < 2 || count > IEEE80211_MAX_SSID_LEN + 1)
835 return -EINVAL;
837 memset(&cmd, 0, sizeof(struct cmd_ds_mesh_config));
838 ie = (struct mrvl_meshie *) &cmd.data[0];
840 /* fetch all other Information Element parameters */
841 ret = mesh_get_default_parameters(dev, &defs);
843 cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie));
845 /* transfer IE elements */
846 memcpy(ie, &defs.meshie, sizeof(struct mrvl_meshie));
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;
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;
860 return strlen(buf);
864 * @brief Get function for sysfs attribute protocol_id
866 static ssize_t protocol_id_get(struct device *dev,
867 struct device_attribute *attr, char *buf)
869 struct mrvl_mesh_defaults defs;
870 int ret;
872 ret = mesh_get_default_parameters(dev, &defs);
874 if (ret)
875 return ret;
877 return snprintf(buf, 5, "%d\n", defs.meshie.val.active_protocol_id);
881 * @brief Set function for sysfs attribute protocol_id
883 static ssize_t protocol_id_set(struct device *dev,
884 struct device_attribute *attr, const char *buf, size_t count)
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;
893 memset(&cmd, 0, sizeof(cmd));
894 ret = sscanf(buf, "%d", &datum);
895 if ((ret != 1) || (datum > 255))
896 return -EINVAL;
898 /* fetch all other Information Element parameters */
899 ret = mesh_get_default_parameters(dev, &defs);
901 cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie));
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;
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;
914 return strlen(buf);
918 * @brief Get function for sysfs attribute metric_id
920 static ssize_t metric_id_get(struct device *dev,
921 struct device_attribute *attr, char *buf)
923 struct mrvl_mesh_defaults defs;
924 int ret;
926 ret = mesh_get_default_parameters(dev, &defs);
928 if (ret)
929 return ret;
931 return snprintf(buf, 5, "%d\n", defs.meshie.val.active_metric_id);
935 * @brief Set function for sysfs attribute metric_id
937 static ssize_t metric_id_set(struct device *dev, struct device_attribute *attr,
938 const char *buf, size_t count)
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;
947 memset(&cmd, 0, sizeof(cmd));
948 ret = sscanf(buf, "%d", &datum);
949 if ((ret != 1) || (datum > 255))
950 return -EINVAL;
952 /* fetch all other Information Element parameters */
953 ret = mesh_get_default_parameters(dev, &defs);
955 cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie));
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;
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;
968 return strlen(buf);
972 * @brief Get function for sysfs attribute capability
974 static ssize_t capability_get(struct device *dev,
975 struct device_attribute *attr, char *buf)
977 struct mrvl_mesh_defaults defs;
978 int ret;
980 ret = mesh_get_default_parameters(dev, &defs);
982 if (ret)
983 return ret;
985 return snprintf(buf, 5, "%d\n", defs.meshie.val.mesh_capability);
989 * @brief Set function for sysfs attribute capability
991 static ssize_t capability_set(struct device *dev, struct device_attribute *attr,
992 const char *buf, size_t count)
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;
1001 memset(&cmd, 0, sizeof(cmd));
1002 ret = sscanf(buf, "%d", &datum);
1003 if ((ret != 1) || (datum > 255))
1004 return -EINVAL;
1006 /* fetch all other Information Element parameters */
1007 ret = mesh_get_default_parameters(dev, &defs);
1009 cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie));
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;
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;
1022 return strlen(buf);
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);
1034 static struct attribute *boot_opts_attrs[] = {
1035 &dev_attr_bootflag.attr,
1036 &dev_attr_boottime.attr,
1037 &dev_attr_channel.attr,
1038 NULL
1041 static struct attribute_group boot_opts_group = {
1042 .name = "boot_options",
1043 .attrs = boot_opts_attrs,
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
1054 static struct attribute_group mesh_ie_group = {
1055 .name = "mesh_ie",
1056 .attrs = mesh_ie_attrs,
1059 void lbs_persist_config_init(struct net_device *dev)
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);
1066 void lbs_persist_config_remove(struct net_device *dev)
1068 sysfs_remove_group(&(dev->dev.kobj), &boot_opts_group);
1069 sysfs_remove_group(&(dev->dev.kobj), &mesh_ie_group);
1074 /***************************************************************************
1075 * Ethtool related
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"
1089 void lbs_mesh_ethtool_get_stats(struct net_device *dev,
1090 struct ethtool_stats *stats, uint64_t *data)
1092 struct lbs_private *priv = dev->ml_priv;
1093 struct cmd_ds_mesh_access mesh_access;
1094 int ret;
1096 lbs_deb_enter(LBS_DEB_ETHTOOL);
1098 /* Get Mesh Statistics */
1099 ret = lbs_mesh_access(priv, CMD_ACT_MESH_GET_STATS, &mesh_access);
1101 if (ret) {
1102 memset(data, 0, MESH_STATS_NUM*(sizeof(uint64_t)));
1103 return;
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]);
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;
1124 lbs_deb_enter(LBS_DEB_ETHTOOL);
1127 int lbs_mesh_ethtool_get_sset_count(struct net_device *dev, int sset)
1129 struct lbs_private *priv = dev->ml_priv;
1131 if (sset == ETH_SS_STATS && dev == priv->mesh_dev)
1132 return MESH_STATS_NUM;
1134 return -EOPNOTSUPP;
1137 void lbs_mesh_ethtool_get_strings(struct net_device *dev,
1138 uint32_t stringset, uint8_t *s)
1140 int i;
1142 lbs_deb_enter(LBS_DEB_ETHTOOL);
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);
1151 break;
1153 lbs_deb_enter(LBS_DEB_ETHTOOL);