mesh.c 29 KB

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