mesh.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152
  1. #include <linux/delay.h>
  2. #include <linux/etherdevice.h>
  3. #include <linux/netdevice.h>
  4. #include <linux/if_ether.h>
  5. #include <linux/if_arp.h>
  6. #include <linux/kthread.h>
  7. #include <linux/kfifo.h>
  8. #include <net/cfg80211.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. priv->mesh_connect_status = LBS_DISCONNECTED;
  163. /* Determine mesh_fw_ver from fwrelease and fwcapinfo */
  164. /* 5.0.16p0 9.0.0.p0 is known to NOT support any mesh */
  165. /* 5.110.22 have mesh command with 0xa3 command id */
  166. /* 10.0.0.p0 FW brings in mesh config command with different id */
  167. /* Check FW version MSB and initialize mesh_fw_ver */
  168. if (MRVL_FW_MAJOR_REV(priv->fwrelease) == MRVL_FW_V5) {
  169. /* Enable mesh, if supported, and work out which TLV it uses.
  170. 0x100 + 291 is an unofficial value used in 5.110.20.pXX
  171. 0x100 + 37 is the official value used in 5.110.21.pXX
  172. but we check them in that order because 20.pXX doesn't
  173. give an error -- it just silently fails. */
  174. /* 5.110.20.pXX firmware will fail the command if the channel
  175. doesn't match the existing channel. But only if the TLV
  176. is correct. If the channel is wrong, _BOTH_ versions will
  177. give an error to 0x100+291, and allow 0x100+37 to succeed.
  178. It's just that 5.110.20.pXX will not have done anything
  179. useful */
  180. priv->mesh_tlv = TLV_TYPE_OLD_MESH_ID;
  181. if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
  182. priv->channel)) {
  183. priv->mesh_tlv = TLV_TYPE_MESH_ID;
  184. if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
  185. priv->channel))
  186. priv->mesh_tlv = 0;
  187. }
  188. } else
  189. if ((MRVL_FW_MAJOR_REV(priv->fwrelease) >= MRVL_FW_V10) &&
  190. (priv->fwcapinfo & MESH_CAPINFO_ENABLE_MASK)) {
  191. /* 10.0.0.pXX new firmwares should succeed with TLV
  192. * 0x100+37; Do not invoke command with old TLV.
  193. */
  194. priv->mesh_tlv = TLV_TYPE_MESH_ID;
  195. if (lbs_mesh_config(priv, CMD_ACT_MESH_CONFIG_START,
  196. priv->channel))
  197. priv->mesh_tlv = 0;
  198. }
  199. if (priv->mesh_tlv) {
  200. sprintf(priv->mesh_ssid, "mesh");
  201. priv->mesh_ssid_len = 4;
  202. lbs_add_mesh(priv);
  203. if (device_create_file(&dev->dev, &dev_attr_lbs_mesh))
  204. lbs_pr_err("cannot register lbs_mesh attribute\n");
  205. ret = 1;
  206. }
  207. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  208. return ret;
  209. }
  210. int lbs_deinit_mesh(struct lbs_private *priv)
  211. {
  212. struct net_device *dev = priv->dev;
  213. int ret = 0;
  214. lbs_deb_enter(LBS_DEB_MESH);
  215. if (priv->mesh_tlv) {
  216. device_remove_file(&dev->dev, &dev_attr_lbs_mesh);
  217. ret = 1;
  218. }
  219. lbs_deb_leave_args(LBS_DEB_MESH, "ret %d", ret);
  220. return ret;
  221. }
  222. /**
  223. * @brief This function closes the mshX interface
  224. *
  225. * @param dev A pointer to net_device structure
  226. * @return 0
  227. */
  228. static int lbs_mesh_stop(struct net_device *dev)
  229. {
  230. struct lbs_private *priv = dev->ml_priv;
  231. lbs_deb_enter(LBS_DEB_MESH);
  232. spin_lock_irq(&priv->driver_lock);
  233. priv->mesh_open = 0;
  234. priv->mesh_connect_status = LBS_DISCONNECTED;
  235. netif_stop_queue(dev);
  236. netif_carrier_off(dev);
  237. spin_unlock_irq(&priv->driver_lock);
  238. schedule_work(&priv->mcast_work);
  239. lbs_deb_leave(LBS_DEB_MESH);
  240. return 0;
  241. }
  242. /**
  243. * @brief This function opens the mshX interface
  244. *
  245. * @param dev A pointer to net_device structure
  246. * @return 0 or -EBUSY if monitor mode active
  247. */
  248. static int lbs_mesh_dev_open(struct net_device *dev)
  249. {
  250. struct lbs_private *priv = dev->ml_priv;
  251. int ret = 0;
  252. lbs_deb_enter(LBS_DEB_NET);
  253. spin_lock_irq(&priv->driver_lock);
  254. if (priv->wdev->iftype == NL80211_IFTYPE_MONITOR) {
  255. ret = -EBUSY;
  256. goto out;
  257. }
  258. priv->mesh_open = 1;
  259. priv->mesh_connect_status = LBS_CONNECTED;
  260. netif_carrier_on(dev);
  261. if (!priv->tx_pending_len)
  262. netif_wake_queue(dev);
  263. out:
  264. spin_unlock_irq(&priv->driver_lock);
  265. lbs_deb_leave_args(LBS_DEB_NET, "ret %d", ret);
  266. return ret;
  267. }
  268. static const struct net_device_ops mesh_netdev_ops = {
  269. .ndo_open = lbs_mesh_dev_open,
  270. .ndo_stop = lbs_mesh_stop,
  271. .ndo_start_xmit = lbs_hard_start_xmit,
  272. .ndo_set_mac_address = lbs_set_mac_address,
  273. .ndo_set_multicast_list = lbs_set_multicast_list,
  274. };
  275. /**
  276. * @brief This function adds mshX interface
  277. *
  278. * @param priv A pointer to the struct lbs_private structure
  279. * @return 0 if successful, -X otherwise
  280. */
  281. int lbs_add_mesh(struct lbs_private *priv)
  282. {
  283. struct net_device *mesh_dev = NULL;
  284. int ret = 0;
  285. lbs_deb_enter(LBS_DEB_MESH);
  286. /* Allocate a virtual mesh device */
  287. mesh_dev = alloc_netdev(0, "msh%d", ether_setup);
  288. if (!mesh_dev) {
  289. lbs_deb_mesh("init mshX device failed\n");
  290. ret = -ENOMEM;
  291. goto done;
  292. }
  293. mesh_dev->ml_priv = priv;
  294. priv->mesh_dev = mesh_dev;
  295. mesh_dev->netdev_ops = &mesh_netdev_ops;
  296. mesh_dev->ethtool_ops = &lbs_ethtool_ops;
  297. memcpy(mesh_dev->dev_addr, priv->dev->dev_addr, ETH_ALEN);
  298. SET_NETDEV_DEV(priv->mesh_dev, priv->dev->dev.parent);
  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. }