mesh.c 30 KB

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