mesh.c 29 KB

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