mesh.c 30 KB

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