Linux 4.4.145
[linux/fpc-iii.git] / drivers / net / wireless / libertas / mesh.c
blobd0c881dd584677f18c551657d351b6bee435da5f
1 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
3 #include <linux/delay.h>
4 #include <linux/etherdevice.h>
5 #include <linux/hardirq.h>
6 #include <linux/netdevice.h>
7 #include <linux/if_ether.h>
8 #include <linux/if_arp.h>
9 #include <linux/kthread.h>
10 #include <linux/kfifo.h>
11 #include <net/cfg80211.h>
13 #include "mesh.h"
14 #include "decl.h"
15 #include "cmd.h"
18 static int lbs_add_mesh(struct lbs_private *priv);
20 /***************************************************************************
21 * Mesh command handling
24 static int lbs_mesh_access(struct lbs_private *priv, uint16_t cmd_action,
25 struct cmd_ds_mesh_access *cmd)
27 int ret;
29 lbs_deb_enter_args(LBS_DEB_CMD, "action %d", cmd_action);
31 cmd->hdr.command = cpu_to_le16(CMD_MESH_ACCESS);
32 cmd->hdr.size = cpu_to_le16(sizeof(*cmd));
33 cmd->hdr.result = 0;
35 cmd->action = cpu_to_le16(cmd_action);
37 ret = lbs_cmd_with_response(priv, CMD_MESH_ACCESS, cmd);
39 lbs_deb_leave(LBS_DEB_CMD);
40 return ret;
43 static int __lbs_mesh_config_send(struct lbs_private *priv,
44 struct cmd_ds_mesh_config *cmd,
45 uint16_t action, uint16_t type)
47 int ret;
48 u16 command = CMD_MESH_CONFIG_OLD;
50 lbs_deb_enter(LBS_DEB_CMD);
53 * Command id is 0xac for v10 FW along with mesh interface
54 * id in bits 14-13-12.
56 if (priv->mesh_tlv == TLV_TYPE_MESH_ID)
57 command = CMD_MESH_CONFIG |
58 (MESH_IFACE_ID << MESH_IFACE_BIT_OFFSET);
60 cmd->hdr.command = cpu_to_le16(command);
61 cmd->hdr.size = cpu_to_le16(sizeof(struct cmd_ds_mesh_config));
62 cmd->hdr.result = 0;
64 cmd->type = cpu_to_le16(type);
65 cmd->action = cpu_to_le16(action);
67 ret = lbs_cmd_with_response(priv, command, cmd);
69 lbs_deb_leave(LBS_DEB_CMD);
70 return ret;
73 static int lbs_mesh_config_send(struct lbs_private *priv,
74 struct cmd_ds_mesh_config *cmd,
75 uint16_t action, uint16_t type)
77 int ret;
79 if (!(priv->fwcapinfo & FW_CAPINFO_PERSISTENT_CONFIG))
80 return -EOPNOTSUPP;
82 ret = __lbs_mesh_config_send(priv, cmd, action, type);
83 return ret;
86 /* This function is the CMD_MESH_CONFIG legacy function. It only handles the
87 * START and STOP actions. The extended actions supported by CMD_MESH_CONFIG
88 * are all handled by preparing a struct cmd_ds_mesh_config and passing it to
89 * lbs_mesh_config_send.
91 static int lbs_mesh_config(struct lbs_private *priv, uint16_t action,
92 uint16_t chan)
94 struct cmd_ds_mesh_config cmd;
95 struct mrvl_meshie *ie;
97 memset(&cmd, 0, sizeof(cmd));
98 cmd.channel = cpu_to_le16(chan);
99 ie = (struct mrvl_meshie *)cmd.data;
101 switch (action) {
102 case CMD_ACT_MESH_CONFIG_START:
103 ie->id = WLAN_EID_VENDOR_SPECIFIC;
104 ie->val.oui[0] = 0x00;
105 ie->val.oui[1] = 0x50;
106 ie->val.oui[2] = 0x43;
107 ie->val.type = MARVELL_MESH_IE_TYPE;
108 ie->val.subtype = MARVELL_MESH_IE_SUBTYPE;
109 ie->val.version = MARVELL_MESH_IE_VERSION;
110 ie->val.active_protocol_id = MARVELL_MESH_PROTO_ID_HWMP;
111 ie->val.active_metric_id = MARVELL_MESH_METRIC_ID;
112 ie->val.mesh_capability = MARVELL_MESH_CAPABILITY;
113 ie->val.mesh_id_len = priv->mesh_ssid_len;
114 memcpy(ie->val.mesh_id, priv->mesh_ssid, priv->mesh_ssid_len);
115 ie->len = sizeof(struct mrvl_meshie_val) -
116 IEEE80211_MAX_SSID_LEN + priv->mesh_ssid_len;
117 cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie_val));
118 break;
119 case CMD_ACT_MESH_CONFIG_STOP:
120 break;
121 default:
122 return -1;
124 lbs_deb_cmd("mesh config action %d type %x channel %d SSID %*pE\n",
125 action, priv->mesh_tlv, chan, priv->mesh_ssid_len,
126 priv->mesh_ssid);
128 return __lbs_mesh_config_send(priv, &cmd, action, priv->mesh_tlv);
131 int lbs_mesh_set_channel(struct lbs_private *priv, u8 channel)
133 priv->mesh_channel = channel;
134 return lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START, channel);
137 static uint16_t lbs_mesh_get_channel(struct lbs_private *priv)
139 return priv->mesh_channel ?: 1;
142 /***************************************************************************
143 * Mesh sysfs support
147 * Attributes exported through sysfs
151 * lbs_anycast_get - Get function for sysfs attribute anycast_mask
152 * @dev: the &struct device
153 * @attr: device attributes
154 * @buf: buffer where data will be returned
156 static ssize_t lbs_anycast_get(struct device *dev,
157 struct device_attribute *attr, char * buf)
159 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
160 struct cmd_ds_mesh_access mesh_access;
161 int ret;
163 memset(&mesh_access, 0, sizeof(mesh_access));
165 ret = lbs_mesh_access(priv, CMD_ACT_MESH_GET_ANYCAST, &mesh_access);
166 if (ret)
167 return ret;
169 return snprintf(buf, 12, "0x%X\n", le32_to_cpu(mesh_access.data[0]));
173 * lbs_anycast_set - Set function for sysfs attribute anycast_mask
174 * @dev: the &struct device
175 * @attr: device attributes
176 * @buf: buffer that contains new attribute value
177 * @count: size of buffer
179 static ssize_t lbs_anycast_set(struct device *dev,
180 struct device_attribute *attr, const char * buf, size_t count)
182 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
183 struct cmd_ds_mesh_access mesh_access;
184 uint32_t datum;
185 int ret;
187 memset(&mesh_access, 0, sizeof(mesh_access));
188 sscanf(buf, "%x", &datum);
189 mesh_access.data[0] = cpu_to_le32(datum);
191 ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_ANYCAST, &mesh_access);
192 if (ret)
193 return ret;
195 return strlen(buf);
199 * lbs_prb_rsp_limit_get - Get function for sysfs attribute prb_rsp_limit
200 * @dev: the &struct device
201 * @attr: device attributes
202 * @buf: buffer where data will be returned
204 static ssize_t lbs_prb_rsp_limit_get(struct device *dev,
205 struct device_attribute *attr, char *buf)
207 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
208 struct cmd_ds_mesh_access mesh_access;
209 int ret;
210 u32 retry_limit;
212 memset(&mesh_access, 0, sizeof(mesh_access));
213 mesh_access.data[0] = cpu_to_le32(CMD_ACT_GET);
215 ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_GET_PRB_RSP_LIMIT,
216 &mesh_access);
217 if (ret)
218 return ret;
220 retry_limit = le32_to_cpu(mesh_access.data[1]);
221 return snprintf(buf, 10, "%d\n", retry_limit);
225 * lbs_prb_rsp_limit_set - Set function for sysfs attribute prb_rsp_limit
226 * @dev: the &struct device
227 * @attr: device attributes
228 * @buf: buffer that contains new attribute value
229 * @count: size of buffer
231 static ssize_t lbs_prb_rsp_limit_set(struct device *dev,
232 struct device_attribute *attr, const char *buf, size_t count)
234 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
235 struct cmd_ds_mesh_access mesh_access;
236 int ret;
237 unsigned long retry_limit;
239 memset(&mesh_access, 0, sizeof(mesh_access));
240 mesh_access.data[0] = cpu_to_le32(CMD_ACT_SET);
242 if (!kstrtoul(buf, 10, &retry_limit))
243 return -ENOTSUPP;
244 if (retry_limit > 15)
245 return -ENOTSUPP;
247 mesh_access.data[1] = cpu_to_le32(retry_limit);
249 ret = lbs_mesh_access(priv, CMD_ACT_MESH_SET_GET_PRB_RSP_LIMIT,
250 &mesh_access);
251 if (ret)
252 return ret;
254 return strlen(buf);
258 * lbs_mesh_get - Get function for sysfs attribute mesh
259 * @dev: the &struct device
260 * @attr: device attributes
261 * @buf: buffer where data will be returned
263 static ssize_t lbs_mesh_get(struct device *dev,
264 struct device_attribute *attr, char * buf)
266 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
267 return snprintf(buf, 5, "0x%X\n", !!priv->mesh_dev);
271 * lbs_mesh_set - Set function for sysfs attribute mesh
272 * @dev: the &struct device
273 * @attr: device attributes
274 * @buf: buffer that contains new attribute value
275 * @count: size of buffer
277 static ssize_t lbs_mesh_set(struct device *dev,
278 struct device_attribute *attr, const char * buf, size_t count)
280 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
281 int enable;
283 sscanf(buf, "%x", &enable);
284 enable = !!enable;
285 if (enable == !!priv->mesh_dev)
286 return count;
288 if (enable)
289 lbs_add_mesh(priv);
290 else
291 lbs_remove_mesh(priv);
293 return count;
297 * lbs_mesh attribute to be exported per ethX interface
298 * through sysfs (/sys/class/net/ethX/lbs_mesh)
300 static DEVICE_ATTR(lbs_mesh, 0644, lbs_mesh_get, lbs_mesh_set);
303 * anycast_mask attribute to be exported per mshX interface
304 * through sysfs (/sys/class/net/mshX/anycast_mask)
306 static DEVICE_ATTR(anycast_mask, 0644, lbs_anycast_get, lbs_anycast_set);
309 * prb_rsp_limit attribute to be exported per mshX interface
310 * through sysfs (/sys/class/net/mshX/prb_rsp_limit)
312 static DEVICE_ATTR(prb_rsp_limit, 0644, lbs_prb_rsp_limit_get,
313 lbs_prb_rsp_limit_set);
315 static struct attribute *lbs_mesh_sysfs_entries[] = {
316 &dev_attr_anycast_mask.attr,
317 &dev_attr_prb_rsp_limit.attr,
318 NULL,
321 static const struct attribute_group lbs_mesh_attr_group = {
322 .attrs = lbs_mesh_sysfs_entries,
326 /***************************************************************************
327 * Persistent configuration support
330 static int mesh_get_default_parameters(struct device *dev,
331 struct mrvl_mesh_defaults *defs)
333 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
334 struct cmd_ds_mesh_config cmd;
335 int ret;
337 memset(&cmd, 0, sizeof(struct cmd_ds_mesh_config));
338 ret = lbs_mesh_config_send(priv, &cmd, CMD_ACT_MESH_CONFIG_GET,
339 CMD_TYPE_MESH_GET_DEFAULTS);
341 if (ret)
342 return -EOPNOTSUPP;
344 memcpy(defs, &cmd.data[0], sizeof(struct mrvl_mesh_defaults));
346 return 0;
350 * bootflag_get - Get function for sysfs attribute bootflag
351 * @dev: the &struct device
352 * @attr: device attributes
353 * @buf: buffer where data will be returned
355 static ssize_t bootflag_get(struct device *dev,
356 struct device_attribute *attr, char *buf)
358 struct mrvl_mesh_defaults defs;
359 int ret;
361 ret = mesh_get_default_parameters(dev, &defs);
363 if (ret)
364 return ret;
366 return snprintf(buf, 12, "%d\n", le32_to_cpu(defs.bootflag));
370 * bootflag_set - Set function for sysfs attribute bootflag
371 * @dev: the &struct device
372 * @attr: device attributes
373 * @buf: buffer that contains new attribute value
374 * @count: size of buffer
376 static ssize_t bootflag_set(struct device *dev, struct device_attribute *attr,
377 const char *buf, size_t count)
379 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
380 struct cmd_ds_mesh_config cmd;
381 uint32_t datum;
382 int ret;
384 memset(&cmd, 0, sizeof(cmd));
385 ret = sscanf(buf, "%d", &datum);
386 if ((ret != 1) || (datum > 1))
387 return -EINVAL;
389 *((__le32 *)&cmd.data[0]) = cpu_to_le32(!!datum);
390 cmd.length = cpu_to_le16(sizeof(uint32_t));
391 ret = lbs_mesh_config_send(priv, &cmd, CMD_ACT_MESH_CONFIG_SET,
392 CMD_TYPE_MESH_SET_BOOTFLAG);
393 if (ret)
394 return ret;
396 return strlen(buf);
400 * boottime_get - Get function for sysfs attribute boottime
401 * @dev: the &struct device
402 * @attr: device attributes
403 * @buf: buffer where data will be returned
405 static ssize_t boottime_get(struct device *dev,
406 struct device_attribute *attr, char *buf)
408 struct mrvl_mesh_defaults defs;
409 int ret;
411 ret = mesh_get_default_parameters(dev, &defs);
413 if (ret)
414 return ret;
416 return snprintf(buf, 12, "%d\n", defs.boottime);
420 * boottime_set - Set function for sysfs attribute boottime
421 * @dev: the &struct device
422 * @attr: device attributes
423 * @buf: buffer that contains new attribute value
424 * @count: size of buffer
426 static ssize_t boottime_set(struct device *dev,
427 struct device_attribute *attr, const char *buf, size_t count)
429 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
430 struct cmd_ds_mesh_config cmd;
431 uint32_t datum;
432 int ret;
434 memset(&cmd, 0, sizeof(cmd));
435 ret = sscanf(buf, "%d", &datum);
436 if ((ret != 1) || (datum > 255))
437 return -EINVAL;
439 /* A too small boot time will result in the device booting into
440 * standalone (no-host) mode before the host can take control of it,
441 * so the change will be hard to revert. This may be a desired
442 * feature (e.g to configure a very fast boot time for devices that
443 * will not be attached to a host), but dangerous. So I'm enforcing a
444 * lower limit of 20 seconds: remove and recompile the driver if this
445 * does not work for you.
447 datum = (datum < 20) ? 20 : datum;
448 cmd.data[0] = datum;
449 cmd.length = cpu_to_le16(sizeof(uint8_t));
450 ret = lbs_mesh_config_send(priv, &cmd, CMD_ACT_MESH_CONFIG_SET,
451 CMD_TYPE_MESH_SET_BOOTTIME);
452 if (ret)
453 return ret;
455 return strlen(buf);
459 * channel_get - Get function for sysfs attribute channel
460 * @dev: the &struct device
461 * @attr: device attributes
462 * @buf: buffer where data will be returned
464 static ssize_t channel_get(struct device *dev,
465 struct device_attribute *attr, char *buf)
467 struct mrvl_mesh_defaults defs;
468 int ret;
470 ret = mesh_get_default_parameters(dev, &defs);
472 if (ret)
473 return ret;
475 return snprintf(buf, 12, "%d\n", le16_to_cpu(defs.channel));
479 * channel_set - Set function for sysfs attribute channel
480 * @dev: the &struct device
481 * @attr: device attributes
482 * @buf: buffer that contains new attribute value
483 * @count: size of buffer
485 static ssize_t channel_set(struct device *dev, struct device_attribute *attr,
486 const char *buf, size_t count)
488 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
489 struct cmd_ds_mesh_config cmd;
490 uint32_t datum;
491 int ret;
493 memset(&cmd, 0, sizeof(cmd));
494 ret = sscanf(buf, "%d", &datum);
495 if (ret != 1 || datum < 1 || datum > 11)
496 return -EINVAL;
498 *((__le16 *)&cmd.data[0]) = cpu_to_le16(datum);
499 cmd.length = cpu_to_le16(sizeof(uint16_t));
500 ret = lbs_mesh_config_send(priv, &cmd, CMD_ACT_MESH_CONFIG_SET,
501 CMD_TYPE_MESH_SET_DEF_CHANNEL);
502 if (ret)
503 return ret;
505 return strlen(buf);
509 * mesh_id_get - Get function for sysfs attribute mesh_id
510 * @dev: the &struct device
511 * @attr: device attributes
512 * @buf: buffer where data will be returned
514 static ssize_t mesh_id_get(struct device *dev, struct device_attribute *attr,
515 char *buf)
517 struct mrvl_mesh_defaults defs;
518 int ret;
520 ret = mesh_get_default_parameters(dev, &defs);
522 if (ret)
523 return ret;
525 if (defs.meshie.val.mesh_id_len > IEEE80211_MAX_SSID_LEN) {
526 dev_err(dev, "inconsistent mesh ID length\n");
527 defs.meshie.val.mesh_id_len = IEEE80211_MAX_SSID_LEN;
530 memcpy(buf, defs.meshie.val.mesh_id, defs.meshie.val.mesh_id_len);
531 buf[defs.meshie.val.mesh_id_len] = '\n';
532 buf[defs.meshie.val.mesh_id_len + 1] = '\0';
534 return defs.meshie.val.mesh_id_len + 1;
538 * mesh_id_set - Set function for sysfs attribute mesh_id
539 * @dev: the &struct device
540 * @attr: device attributes
541 * @buf: buffer that contains new attribute value
542 * @count: size of buffer
544 static ssize_t mesh_id_set(struct device *dev, struct device_attribute *attr,
545 const char *buf, size_t count)
547 struct cmd_ds_mesh_config cmd;
548 struct mrvl_mesh_defaults defs;
549 struct mrvl_meshie *ie;
550 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
551 int len;
552 int ret;
554 if (count < 2 || count > IEEE80211_MAX_SSID_LEN + 1)
555 return -EINVAL;
557 memset(&cmd, 0, sizeof(struct cmd_ds_mesh_config));
558 ie = (struct mrvl_meshie *) &cmd.data[0];
560 /* fetch all other Information Element parameters */
561 ret = mesh_get_default_parameters(dev, &defs);
563 cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie));
565 /* transfer IE elements */
566 memcpy(ie, &defs.meshie, sizeof(struct mrvl_meshie));
568 len = count - 1;
569 memcpy(ie->val.mesh_id, buf, len);
570 /* SSID len */
571 ie->val.mesh_id_len = len;
572 /* IE len */
573 ie->len = sizeof(struct mrvl_meshie_val) - IEEE80211_MAX_SSID_LEN + len;
575 ret = lbs_mesh_config_send(priv, &cmd, CMD_ACT_MESH_CONFIG_SET,
576 CMD_TYPE_MESH_SET_MESH_IE);
577 if (ret)
578 return ret;
580 return strlen(buf);
584 * protocol_id_get - Get function for sysfs attribute protocol_id
585 * @dev: the &struct device
586 * @attr: device attributes
587 * @buf: buffer where data will be returned
589 static ssize_t protocol_id_get(struct device *dev,
590 struct device_attribute *attr, char *buf)
592 struct mrvl_mesh_defaults defs;
593 int ret;
595 ret = mesh_get_default_parameters(dev, &defs);
597 if (ret)
598 return ret;
600 return snprintf(buf, 5, "%d\n", defs.meshie.val.active_protocol_id);
604 * protocol_id_set - Set function for sysfs attribute protocol_id
605 * @dev: the &struct device
606 * @attr: device attributes
607 * @buf: buffer that contains new attribute value
608 * @count: size of buffer
610 static ssize_t protocol_id_set(struct device *dev,
611 struct device_attribute *attr, const char *buf, size_t count)
613 struct cmd_ds_mesh_config cmd;
614 struct mrvl_mesh_defaults defs;
615 struct mrvl_meshie *ie;
616 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
617 uint32_t datum;
618 int ret;
620 memset(&cmd, 0, sizeof(cmd));
621 ret = sscanf(buf, "%d", &datum);
622 if ((ret != 1) || (datum > 255))
623 return -EINVAL;
625 /* fetch all other Information Element parameters */
626 ret = mesh_get_default_parameters(dev, &defs);
628 cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie));
630 /* transfer IE elements */
631 ie = (struct mrvl_meshie *) &cmd.data[0];
632 memcpy(ie, &defs.meshie, sizeof(struct mrvl_meshie));
633 /* update protocol id */
634 ie->val.active_protocol_id = datum;
636 ret = lbs_mesh_config_send(priv, &cmd, CMD_ACT_MESH_CONFIG_SET,
637 CMD_TYPE_MESH_SET_MESH_IE);
638 if (ret)
639 return ret;
641 return strlen(buf);
645 * metric_id_get - Get function for sysfs attribute metric_id
646 * @dev: the &struct device
647 * @attr: device attributes
648 * @buf: buffer where data will be returned
650 static ssize_t metric_id_get(struct device *dev,
651 struct device_attribute *attr, char *buf)
653 struct mrvl_mesh_defaults defs;
654 int ret;
656 ret = mesh_get_default_parameters(dev, &defs);
658 if (ret)
659 return ret;
661 return snprintf(buf, 5, "%d\n", defs.meshie.val.active_metric_id);
665 * metric_id_set - Set function for sysfs attribute metric_id
666 * @dev: the &struct device
667 * @attr: device attributes
668 * @buf: buffer that contains new attribute value
669 * @count: size of buffer
671 static ssize_t metric_id_set(struct device *dev, struct device_attribute *attr,
672 const char *buf, size_t count)
674 struct cmd_ds_mesh_config cmd;
675 struct mrvl_mesh_defaults defs;
676 struct mrvl_meshie *ie;
677 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
678 uint32_t datum;
679 int ret;
681 memset(&cmd, 0, sizeof(cmd));
682 ret = sscanf(buf, "%d", &datum);
683 if ((ret != 1) || (datum > 255))
684 return -EINVAL;
686 /* fetch all other Information Element parameters */
687 ret = mesh_get_default_parameters(dev, &defs);
689 cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie));
691 /* transfer IE elements */
692 ie = (struct mrvl_meshie *) &cmd.data[0];
693 memcpy(ie, &defs.meshie, sizeof(struct mrvl_meshie));
694 /* update metric id */
695 ie->val.active_metric_id = datum;
697 ret = lbs_mesh_config_send(priv, &cmd, CMD_ACT_MESH_CONFIG_SET,
698 CMD_TYPE_MESH_SET_MESH_IE);
699 if (ret)
700 return ret;
702 return strlen(buf);
706 * capability_get - Get function for sysfs attribute capability
707 * @dev: the &struct device
708 * @attr: device attributes
709 * @buf: buffer where data will be returned
711 static ssize_t capability_get(struct device *dev,
712 struct device_attribute *attr, char *buf)
714 struct mrvl_mesh_defaults defs;
715 int ret;
717 ret = mesh_get_default_parameters(dev, &defs);
719 if (ret)
720 return ret;
722 return snprintf(buf, 5, "%d\n", defs.meshie.val.mesh_capability);
726 * capability_set - Set function for sysfs attribute capability
727 * @dev: the &struct device
728 * @attr: device attributes
729 * @buf: buffer that contains new attribute value
730 * @count: size of buffer
732 static ssize_t capability_set(struct device *dev, struct device_attribute *attr,
733 const char *buf, size_t count)
735 struct cmd_ds_mesh_config cmd;
736 struct mrvl_mesh_defaults defs;
737 struct mrvl_meshie *ie;
738 struct lbs_private *priv = to_net_dev(dev)->ml_priv;
739 uint32_t datum;
740 int ret;
742 memset(&cmd, 0, sizeof(cmd));
743 ret = sscanf(buf, "%d", &datum);
744 if ((ret != 1) || (datum > 255))
745 return -EINVAL;
747 /* fetch all other Information Element parameters */
748 ret = mesh_get_default_parameters(dev, &defs);
750 cmd.length = cpu_to_le16(sizeof(struct mrvl_meshie));
752 /* transfer IE elements */
753 ie = (struct mrvl_meshie *) &cmd.data[0];
754 memcpy(ie, &defs.meshie, sizeof(struct mrvl_meshie));
755 /* update value */
756 ie->val.mesh_capability = datum;
758 ret = lbs_mesh_config_send(priv, &cmd, CMD_ACT_MESH_CONFIG_SET,
759 CMD_TYPE_MESH_SET_MESH_IE);
760 if (ret)
761 return ret;
763 return strlen(buf);
767 static DEVICE_ATTR(bootflag, 0644, bootflag_get, bootflag_set);
768 static DEVICE_ATTR(boottime, 0644, boottime_get, boottime_set);
769 static DEVICE_ATTR(channel, 0644, channel_get, channel_set);
770 static DEVICE_ATTR(mesh_id, 0644, mesh_id_get, mesh_id_set);
771 static DEVICE_ATTR(protocol_id, 0644, protocol_id_get, protocol_id_set);
772 static DEVICE_ATTR(metric_id, 0644, metric_id_get, metric_id_set);
773 static DEVICE_ATTR(capability, 0644, capability_get, capability_set);
775 static struct attribute *boot_opts_attrs[] = {
776 &dev_attr_bootflag.attr,
777 &dev_attr_boottime.attr,
778 &dev_attr_channel.attr,
779 NULL
782 static const struct attribute_group boot_opts_group = {
783 .name = "boot_options",
784 .attrs = boot_opts_attrs,
787 static struct attribute *mesh_ie_attrs[] = {
788 &dev_attr_mesh_id.attr,
789 &dev_attr_protocol_id.attr,
790 &dev_attr_metric_id.attr,
791 &dev_attr_capability.attr,
792 NULL
795 static const struct attribute_group mesh_ie_group = {
796 .name = "mesh_ie",
797 .attrs = mesh_ie_attrs,
800 static void lbs_persist_config_init(struct net_device *dev)
802 int ret;
803 ret = sysfs_create_group(&(dev->dev.kobj), &boot_opts_group);
804 ret = sysfs_create_group(&(dev->dev.kobj), &mesh_ie_group);
807 static void lbs_persist_config_remove(struct net_device *dev)
809 sysfs_remove_group(&(dev->dev.kobj), &boot_opts_group);
810 sysfs_remove_group(&(dev->dev.kobj), &mesh_ie_group);
814 /***************************************************************************
815 * Initializing and starting, stopping mesh
819 * Check mesh FW version and appropriately send the mesh start
820 * command
822 int lbs_init_mesh(struct lbs_private *priv)
824 int ret = 0;
826 lbs_deb_enter(LBS_DEB_MESH);
828 /* Determine mesh_fw_ver from fwrelease and fwcapinfo */
829 /* 5.0.16p0 9.0.0.p0 is known to NOT support any mesh */
830 /* 5.110.22 have mesh command with 0xa3 command id */
831 /* 10.0.0.p0 FW brings in mesh config command with different id */
832 /* Check FW version MSB and initialize mesh_fw_ver */
833 if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5) {
834 /* Enable mesh, if supported, and work out which TLV it uses.
835 0x100 + 291 is an unofficial value used in 5.110.20.pXX
836 0x100 + 37 is the official value used in 5.110.21.pXX
837 but we check them in that order because 20.pXX doesn't
838 give an error -- it just silently fails. */
840 /* 5.110.20.pXX firmware will fail the command if the channel
841 doesn't match the existing channel. But only if the TLV
842 is correct. If the channel is wrong, _BOTH_ versions will
843 give an error to 0x100+291, and allow 0x100+37 to succeed.
844 It's just that 5.110.20.pXX will not have done anything
845 useful */
847 priv->mesh_tlv = TLV_TYPE_OLD_MESH_ID;
848 if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START, 1)) {
849 priv->mesh_tlv = TLV_TYPE_MESH_ID;
850 if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START, 1))
851 priv->mesh_tlv = 0;
853 } else
854 if ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
855 (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK)) {
856 /* 10.0.0.pXX new firmwares should succeed with TLV
857 * 0x100+37; Do not invoke command with old TLV.
859 priv->mesh_tlv = TLV_TYPE_MESH_ID;
860 if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START, 1))
861 priv->mesh_tlv = 0;
864 /* Stop meshing until interface is brought up */
865 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP, 1);
867 if (priv->mesh_tlv) {
868 sprintf(priv->mesh_ssid, "mesh");
869 priv->mesh_ssid_len = 4;
870 ret = 1;
873 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
874 return ret;
877 void lbs_start_mesh(struct lbs_private *priv)
879 lbs_add_mesh(priv);
881 if (device_create_file(&priv->dev->dev, &dev_attr_lbs_mesh))
882 netdev_err(priv->dev, "cannot register lbs_mesh attribute\n");
885 int lbs_deinit_mesh(struct lbs_private *priv)
887 struct net_device *dev = priv->dev;
888 int ret = 0;
890 lbs_deb_enter(LBS_DEB_MESH);
892 if (priv->mesh_tlv) {
893 device_remove_file(&dev->dev, &dev_attr_lbs_mesh);
894 ret = 1;
897 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
898 return ret;
903 * lbs_mesh_stop - close the mshX interface
905 * @dev: A pointer to &net_device structure
906 * returns: 0
908 static int lbs_mesh_stop(struct net_device *dev)
910 struct lbs_private *priv = dev->ml_priv;
912 lbs_deb_enter(LBS_DEB_MESH);
913 lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_STOP,
914 lbs_mesh_get_channel(priv));
916 spin_lock_irq(&priv->driver_lock);
918 netif_stop_queue(dev);
919 netif_carrier_off(dev);
921 spin_unlock_irq(&priv->driver_lock);
923 lbs_update_mcast(priv);
924 if (!lbs_iface_active(priv))
925 lbs_stop_iface(priv);
927 lbs_deb_leave(LBS_DEB_MESH);
928 return 0;
932 * lbs_mesh_dev_open - open the mshX interface
934 * @dev: A pointer to &net_device structure
935 * returns: 0 or -EBUSY if monitor mode active
937 static int lbs_mesh_dev_open(struct net_device *dev)
939 struct lbs_private *priv = dev->ml_priv;
940 int ret = 0;
942 lbs_deb_enter(LBS_DEB_NET);
943 if (!priv->iface_running) {
944 ret = lbs_start_iface(priv);
945 if (ret)
946 goto out;
949 spin_lock_irq(&priv->driver_lock);
951 if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR) {
952 ret = -EBUSY;
953 spin_unlock_irq(&priv->driver_lock);
954 goto out;
957 netif_carrier_on(dev);
959 if (!priv->tx_pending_len)
960 netif_wake_queue(dev);
962 spin_unlock_irq(&priv->driver_lock);
964 ret = lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
965 lbs_mesh_get_channel(priv));
967 out:
968 lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
969 return ret;
972 static const struct net_device_ops mesh_netdev_ops = {
973 .ndo_open = lbs_mesh_dev_open,
974 .ndo_stop = lbs_mesh_stop,
975 .ndo_start_xmit = lbs_hard_start_xmit,
976 .ndo_set_mac_address = lbs_set_mac_address,
977 .ndo_set_rx_mode = lbs_set_multicast_list,
981 * lbs_add_mesh - add mshX interface
983 * @priv: A pointer to the &struct lbs_private structure
984 * returns: 0 if successful, -X otherwise
986 static int lbs_add_mesh(struct lbs_private *priv)
988 struct net_device *mesh_dev = NULL;
989 struct wireless_dev *mesh_wdev;
990 int ret = 0;
992 lbs_deb_enter(LBS_DEB_MESH);
994 /* Allocate a virtual mesh device */
995 mesh_wdev = kzalloc(sizeof(struct wireless_dev), GFP_KERNEL);
996 if (!mesh_wdev) {
997 lbs_deb_mesh("init mshX wireless device failed\n");
998 ret = -ENOMEM;
999 goto done;
1002 mesh_dev = alloc_netdev(0, "msh%d", NET_NAME_UNKNOWN, ether_setup);
1003 if (!mesh_dev) {
1004 lbs_deb_mesh("init mshX device failed\n");
1005 ret = -ENOMEM;
1006 goto err_free_wdev;
1009 mesh_wdev->iftype = NL80211_IFTYPE_MESH_POINT;
1010 mesh_wdev->wiphy = priv->wdev->wiphy;
1011 mesh_wdev->netdev = mesh_dev;
1013 mesh_dev->ml_priv = priv;
1014 mesh_dev->ieee80211_ptr = mesh_wdev;
1015 priv->mesh_dev = mesh_dev;
1017 mesh_dev->netdev_ops = &mesh_netdev_ops;
1018 mesh_dev->ethtool_ops = &lbs_ethtool_ops;
1019 eth_hw_addr_inherit(mesh_dev, priv->dev);
1021 SET_NETDEV_DEV(priv->mesh_dev, priv->dev->dev.parent);
1023 mesh_dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1024 /* Register virtual mesh interface */
1025 ret = register_netdev(mesh_dev);
1026 if (ret) {
1027 pr_err("cannot register mshX virtual interface\n");
1028 goto err_free_netdev;
1031 ret = sysfs_create_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
1032 if (ret)
1033 goto err_unregister;
1035 lbs_persist_config_init(mesh_dev);
1037 /* Everything successful */
1038 ret = 0;
1039 goto done;
1041 err_unregister:
1042 unregister_netdev(mesh_dev);
1044 err_free_netdev:
1045 free_netdev(mesh_dev);
1047 err_free_wdev:
1048 kfree(mesh_wdev);
1050 done:
1051 lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
1052 return ret;
1055 void lbs_remove_mesh(struct lbs_private *priv)
1057 struct net_device *mesh_dev;
1059 mesh_dev = priv->mesh_dev;
1060 if (!mesh_dev)
1061 return;
1063 lbs_deb_enter(LBS_DEB_MESH);
1064 netif_stop_queue(mesh_dev);
1065 netif_carrier_off(mesh_dev);
1066 sysfs_remove_group(&(mesh_dev->dev.kobj), &lbs_mesh_attr_group);
1067 lbs_persist_config_remove(mesh_dev);
1068 unregister_netdev(mesh_dev);
1069 priv->mesh_dev = NULL;
1070 kfree(mesh_dev->ieee80211_ptr);
1071 free_netdev(mesh_dev);
1072 lbs_deb_leave(LBS_DEB_MESH);
1076 /***************************************************************************
1077 * Sending and receiving
1079 struct net_device *lbs_mesh_set_dev(struct lbs_private *priv,
1080 struct net_device *dev, struct rxpd *rxpd)
1082 if (priv->mesh_dev) {
1083 if (priv->mesh_tlv == TLV_TYPE_OLD_MESH_ID) {
1084 if (rxpd->rx_control & RxPD_MESH_FRAME)
1085 dev = priv->mesh_dev;
1086 } else if (priv->mesh_tlv == TLV_TYPE_MESH_ID) {
1087 if (rxpd->u.bss.bss_num == MESH_IFACE_ID)
1088 dev = priv->mesh_dev;
1091 return dev;
1095 void lbs_mesh_set_txpd(struct lbs_private *priv,
1096 struct net_device *dev, struct txpd *txpd)
1098 if (dev == priv->mesh_dev) {
1099 if (priv->mesh_tlv == TLV_TYPE_OLD_MESH_ID)
1100 txpd->tx_control |= cpu_to_le32(TxPD_MESH_FRAME);
1101 else if (priv->mesh_tlv == TLV_TYPE_MESH_ID)
1102 txpd->u.bss.bss_num = MESH_IFACE_ID;
1107 /***************************************************************************
1108 * Ethtool related
1111 static const char * const mesh_stat_strings[] = {
1112 "drop_duplicate_bcast",
1113 "drop_ttl_zero",
1114 "drop_no_fwd_route",
1115 "drop_no_buffers",
1116 "fwded_unicast_cnt",
1117 "fwded_bcast_cnt",
1118 "drop_blind_table",
1119 "tx_failed_cnt"
1122 void lbs_mesh_ethtool_get_stats(struct net_device *dev,
1123 struct ethtool_stats *stats, uint64_t *data)
1125 struct lbs_private *priv = dev->ml_priv;
1126 struct cmd_ds_mesh_access mesh_access;
1127 int ret;
1129 lbs_deb_enter(LBS_DEB_ETHTOOL);
1131 /* Get Mesh Statistics */
1132 ret = lbs_mesh_access(priv, CMD_ACT_MESH_GET_STATS, &mesh_access);
1134 if (ret) {
1135 memset(data, 0, MESH_STATS_NUM*(sizeof(uint64_t)));
1136 return;
1139 priv->mstats.fwd_drop_rbt = le32_to_cpu(mesh_access.data[0]);
1140 priv->mstats.fwd_drop_ttl = le32_to_cpu(mesh_access.data[1]);
1141 priv->mstats.fwd_drop_noroute = le32_to_cpu(mesh_access.data[2]);
1142 priv->mstats.fwd_drop_nobuf = le32_to_cpu(mesh_access.data[3]);
1143 priv->mstats.fwd_unicast_cnt = le32_to_cpu(mesh_access.data[4]);
1144 priv->mstats.fwd_bcast_cnt = le32_to_cpu(mesh_access.data[5]);
1145 priv->mstats.drop_blind = le32_to_cpu(mesh_access.data[6]);
1146 priv->mstats.tx_failed_cnt = le32_to_cpu(mesh_access.data[7]);
1148 data[0] = priv->mstats.fwd_drop_rbt;
1149 data[1] = priv->mstats.fwd_drop_ttl;
1150 data[2] = priv->mstats.fwd_drop_noroute;
1151 data[3] = priv->mstats.fwd_drop_nobuf;
1152 data[4] = priv->mstats.fwd_unicast_cnt;
1153 data[5] = priv->mstats.fwd_bcast_cnt;
1154 data[6] = priv->mstats.drop_blind;
1155 data[7] = priv->mstats.tx_failed_cnt;
1157 lbs_deb_enter(LBS_DEB_ETHTOOL);
1160 int lbs_mesh_ethtool_get_sset_count(struct net_device *dev, int sset)
1162 struct lbs_private *priv = dev->ml_priv;
1164 if (sset == ETH_SS_STATS && dev == priv->mesh_dev)
1165 return MESH_STATS_NUM;
1167 return -EOPNOTSUPP;
1170 void lbs_mesh_ethtool_get_strings(struct net_device *dev,
1171 uint32_t stringset, uint8_t *s)
1173 int i;
1175 lbs_deb_enter(LBS_DEB_ETHTOOL);
1177 switch (stringset) {
1178 case ETH_SS_STATS:
1179 for (i = 0; i < MESH_STATS_NUM; i++) {
1180 memcpy(s + i * ETH_GSTRING_LEN,
1181 mesh_stat_strings[i],
1182 ETH_GSTRING_LEN);
1184 break;
1186 lbs_deb_enter(LBS_DEB_ETHTOOL);