mesh.c 28 KB

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