mesh.c 28 KB

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