mesh.c 29 KB

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