netdev.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2012 - 2018 Microchip Technology Inc., and its subsidiaries.
  4. * All rights reserved.
  5. */
  6. #include <linux/irq.h>
  7. #include <linux/kthread.h>
  8. #include <linux/firmware.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/inetdevice.h>
  11. #include "cfg80211.h"
  12. #include "wlan_cfg.h"
  13. #define WILC_MULTICAST_TABLE_SIZE 8
  14. #define WILC_MAX_FW_VERSION_STR_SIZE 50
  15. /* latest API version supported */
  16. #define WILC1000_API_VER 1
  17. #define WILC1000_FW_PREFIX "atmel/wilc1000_wifi_firmware-"
  18. #define __WILC1000_FW(api) WILC1000_FW_PREFIX #api ".bin"
  19. #define WILC1000_FW(api) __WILC1000_FW(api)
  20. static irqreturn_t isr_uh_routine(int irq, void *user_data)
  21. {
  22. struct wilc *wilc = user_data;
  23. if (wilc->close) {
  24. pr_err("Can't handle UH interrupt\n");
  25. return IRQ_HANDLED;
  26. }
  27. return IRQ_WAKE_THREAD;
  28. }
  29. static irqreturn_t isr_bh_routine(int irq, void *userdata)
  30. {
  31. struct wilc *wilc = userdata;
  32. if (wilc->close) {
  33. pr_err("Can't handle BH interrupt\n");
  34. return IRQ_HANDLED;
  35. }
  36. wilc_handle_isr(wilc);
  37. return IRQ_HANDLED;
  38. }
  39. static int init_irq(struct net_device *dev)
  40. {
  41. struct wilc_vif *vif = netdev_priv(dev);
  42. struct wilc *wl = vif->wilc;
  43. int ret;
  44. ret = request_threaded_irq(wl->dev_irq_num, isr_uh_routine,
  45. isr_bh_routine,
  46. IRQF_TRIGGER_FALLING | IRQF_ONESHOT,
  47. dev->name, wl);
  48. if (ret) {
  49. netdev_err(dev, "Failed to request IRQ [%d]\n", ret);
  50. return ret;
  51. }
  52. netdev_dbg(dev, "IRQ request succeeded IRQ-NUM= %d\n", wl->dev_irq_num);
  53. return 0;
  54. }
  55. static void deinit_irq(struct net_device *dev)
  56. {
  57. struct wilc_vif *vif = netdev_priv(dev);
  58. struct wilc *wilc = vif->wilc;
  59. /* Deinitialize IRQ */
  60. if (wilc->dev_irq_num)
  61. free_irq(wilc->dev_irq_num, wilc);
  62. }
  63. void wilc_mac_indicate(struct wilc *wilc)
  64. {
  65. s8 status;
  66. wilc_wlan_cfg_get_val(wilc, WID_STATUS, &status, 1);
  67. if (wilc->mac_status == WILC_MAC_STATUS_INIT) {
  68. wilc->mac_status = status;
  69. complete(&wilc->sync_event);
  70. } else {
  71. wilc->mac_status = status;
  72. }
  73. }
  74. static struct net_device *get_if_handler(struct wilc *wilc, u8 *mac_header)
  75. {
  76. struct net_device *ndev = NULL;
  77. struct wilc_vif *vif;
  78. struct ieee80211_hdr *h = (struct ieee80211_hdr *)mac_header;
  79. list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
  80. if (vif->iftype == WILC_STATION_MODE)
  81. if (ether_addr_equal_unaligned(h->addr2, vif->bssid)) {
  82. ndev = vif->ndev;
  83. goto out;
  84. }
  85. if (vif->iftype == WILC_AP_MODE)
  86. if (ether_addr_equal_unaligned(h->addr1, vif->bssid)) {
  87. ndev = vif->ndev;
  88. goto out;
  89. }
  90. }
  91. out:
  92. return ndev;
  93. }
  94. void wilc_wlan_set_bssid(struct net_device *wilc_netdev, const u8 *bssid,
  95. u8 mode)
  96. {
  97. struct wilc_vif *vif = netdev_priv(wilc_netdev);
  98. if (bssid)
  99. ether_addr_copy(vif->bssid, bssid);
  100. else
  101. eth_zero_addr(vif->bssid);
  102. vif->iftype = mode;
  103. }
  104. int wilc_wlan_get_num_conn_ifcs(struct wilc *wilc)
  105. {
  106. int srcu_idx;
  107. u8 ret_val = 0;
  108. struct wilc_vif *vif;
  109. srcu_idx = srcu_read_lock(&wilc->srcu);
  110. list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
  111. if (!is_zero_ether_addr(vif->bssid))
  112. ret_val++;
  113. }
  114. srcu_read_unlock(&wilc->srcu, srcu_idx);
  115. return ret_val;
  116. }
  117. static int wilc_txq_task(void *vp)
  118. {
  119. int ret;
  120. u32 txq_count;
  121. struct wilc *wl = vp;
  122. complete(&wl->txq_thread_started);
  123. while (1) {
  124. wait_for_completion(&wl->txq_event);
  125. if (wl->close) {
  126. complete(&wl->txq_thread_started);
  127. while (!kthread_should_stop())
  128. schedule();
  129. break;
  130. }
  131. do {
  132. ret = wilc_wlan_handle_txq(wl, &txq_count);
  133. if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD) {
  134. int srcu_idx;
  135. struct wilc_vif *ifc;
  136. srcu_idx = srcu_read_lock(&wl->srcu);
  137. list_for_each_entry_rcu(ifc, &wl->vif_list,
  138. list) {
  139. if (ifc->mac_opened && ifc->ndev)
  140. netif_wake_queue(ifc->ndev);
  141. }
  142. srcu_read_unlock(&wl->srcu, srcu_idx);
  143. }
  144. } while (ret == WILC_VMM_ENTRY_FULL_RETRY && !wl->close);
  145. }
  146. return 0;
  147. }
  148. static int wilc_wlan_get_firmware(struct net_device *dev)
  149. {
  150. struct wilc_vif *vif = netdev_priv(dev);
  151. struct wilc *wilc = vif->wilc;
  152. int chip_id;
  153. const struct firmware *wilc_fw;
  154. int ret;
  155. chip_id = wilc_get_chipid(wilc, false);
  156. netdev_info(dev, "ChipID [%x] loading firmware [%s]\n", chip_id,
  157. WILC1000_FW(WILC1000_API_VER));
  158. ret = request_firmware(&wilc_fw, WILC1000_FW(WILC1000_API_VER),
  159. wilc->dev);
  160. if (ret != 0) {
  161. netdev_err(dev, "%s - firmware not available\n",
  162. WILC1000_FW(WILC1000_API_VER));
  163. return -EINVAL;
  164. }
  165. wilc->firmware = wilc_fw;
  166. return 0;
  167. }
  168. static int wilc_start_firmware(struct net_device *dev)
  169. {
  170. struct wilc_vif *vif = netdev_priv(dev);
  171. struct wilc *wilc = vif->wilc;
  172. int ret = 0;
  173. ret = wilc_wlan_start(wilc);
  174. if (ret)
  175. return ret;
  176. if (!wait_for_completion_timeout(&wilc->sync_event,
  177. msecs_to_jiffies(5000)))
  178. return -ETIME;
  179. return 0;
  180. }
  181. static int wilc1000_firmware_download(struct net_device *dev)
  182. {
  183. struct wilc_vif *vif = netdev_priv(dev);
  184. struct wilc *wilc = vif->wilc;
  185. int ret = 0;
  186. if (!wilc->firmware) {
  187. netdev_err(dev, "Firmware buffer is NULL\n");
  188. return -ENOBUFS;
  189. }
  190. ret = wilc_wlan_firmware_download(wilc, wilc->firmware->data,
  191. wilc->firmware->size);
  192. if (ret)
  193. return ret;
  194. release_firmware(wilc->firmware);
  195. wilc->firmware = NULL;
  196. netdev_dbg(dev, "Download Succeeded\n");
  197. return 0;
  198. }
  199. static int wilc_init_fw_config(struct net_device *dev, struct wilc_vif *vif)
  200. {
  201. struct wilc_priv *priv = &vif->priv;
  202. struct host_if_drv *hif_drv;
  203. u8 b;
  204. u16 hw;
  205. u32 w;
  206. netdev_dbg(dev, "Start configuring Firmware\n");
  207. hif_drv = (struct host_if_drv *)priv->hif_drv;
  208. netdev_dbg(dev, "Host = %p\n", hif_drv);
  209. w = vif->iftype;
  210. cpu_to_le32s(&w);
  211. if (!wilc_wlan_cfg_set(vif, 1, WID_SET_OPERATION_MODE, (u8 *)&w, 4,
  212. 0, 0))
  213. goto fail;
  214. b = WILC_FW_BSS_TYPE_INFRA;
  215. if (!wilc_wlan_cfg_set(vif, 0, WID_BSS_TYPE, &b, 1, 0, 0))
  216. goto fail;
  217. b = WILC_FW_TX_RATE_AUTO;
  218. if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_TX_RATE, &b, 1, 0, 0))
  219. goto fail;
  220. b = WILC_FW_OPER_MODE_G_MIXED_11B_2;
  221. if (!wilc_wlan_cfg_set(vif, 0, WID_11G_OPERATING_MODE, &b, 1, 0, 0))
  222. goto fail;
  223. b = WILC_FW_PREAMBLE_SHORT;
  224. if (!wilc_wlan_cfg_set(vif, 0, WID_PREAMBLE, &b, 1, 0, 0))
  225. goto fail;
  226. b = WILC_FW_11N_PROT_AUTO;
  227. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_PROT_MECH, &b, 1, 0, 0))
  228. goto fail;
  229. b = WILC_FW_ACTIVE_SCAN;
  230. if (!wilc_wlan_cfg_set(vif, 0, WID_SCAN_TYPE, &b, 1, 0, 0))
  231. goto fail;
  232. b = WILC_FW_SITE_SURVEY_OFF;
  233. if (!wilc_wlan_cfg_set(vif, 0, WID_SITE_SURVEY, &b, 1, 0, 0))
  234. goto fail;
  235. hw = 0xffff;
  236. cpu_to_le16s(&hw);
  237. if (!wilc_wlan_cfg_set(vif, 0, WID_RTS_THRESHOLD, (u8 *)&hw, 2, 0, 0))
  238. goto fail;
  239. hw = 2346;
  240. cpu_to_le16s(&hw);
  241. if (!wilc_wlan_cfg_set(vif, 0, WID_FRAG_THRESHOLD, (u8 *)&hw, 2, 0, 0))
  242. goto fail;
  243. b = 0;
  244. if (!wilc_wlan_cfg_set(vif, 0, WID_BCAST_SSID, &b, 1, 0, 0))
  245. goto fail;
  246. b = 1;
  247. if (!wilc_wlan_cfg_set(vif, 0, WID_QOS_ENABLE, &b, 1, 0, 0))
  248. goto fail;
  249. b = WILC_FW_NO_POWERSAVE;
  250. if (!wilc_wlan_cfg_set(vif, 0, WID_POWER_MANAGEMENT, &b, 1, 0, 0))
  251. goto fail;
  252. b = WILC_FW_SEC_NO;
  253. if (!wilc_wlan_cfg_set(vif, 0, WID_11I_MODE, &b, 1, 0, 0))
  254. goto fail;
  255. b = WILC_FW_AUTH_OPEN_SYSTEM;
  256. if (!wilc_wlan_cfg_set(vif, 0, WID_AUTH_TYPE, &b, 1, 0, 0))
  257. goto fail;
  258. b = 3;
  259. if (!wilc_wlan_cfg_set(vif, 0, WID_LISTEN_INTERVAL, &b, 1, 0, 0))
  260. goto fail;
  261. b = 3;
  262. if (!wilc_wlan_cfg_set(vif, 0, WID_DTIM_PERIOD, &b, 1, 0, 0))
  263. goto fail;
  264. b = WILC_FW_ACK_POLICY_NORMAL;
  265. if (!wilc_wlan_cfg_set(vif, 0, WID_ACK_POLICY, &b, 1, 0, 0))
  266. goto fail;
  267. b = 0;
  268. if (!wilc_wlan_cfg_set(vif, 0, WID_USER_CONTROL_ON_TX_POWER, &b, 1,
  269. 0, 0))
  270. goto fail;
  271. b = 48;
  272. if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11A, &b, 1, 0, 0))
  273. goto fail;
  274. b = 28;
  275. if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11B, &b, 1, 0, 0))
  276. goto fail;
  277. hw = 100;
  278. cpu_to_le16s(&hw);
  279. if (!wilc_wlan_cfg_set(vif, 0, WID_BEACON_INTERVAL, (u8 *)&hw, 2, 0, 0))
  280. goto fail;
  281. b = WILC_FW_REKEY_POLICY_DISABLE;
  282. if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_POLICY, &b, 1, 0, 0))
  283. goto fail;
  284. w = 84600;
  285. cpu_to_le32s(&w);
  286. if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PERIOD, (u8 *)&w, 4, 0, 0))
  287. goto fail;
  288. w = 500;
  289. cpu_to_le32s(&w);
  290. if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PACKET_COUNT, (u8 *)&w, 4, 0,
  291. 0))
  292. goto fail;
  293. b = 1;
  294. if (!wilc_wlan_cfg_set(vif, 0, WID_SHORT_SLOT_ALLOWED, &b, 1, 0,
  295. 0))
  296. goto fail;
  297. b = WILC_FW_ERP_PROT_SELF_CTS;
  298. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ERP_PROT_TYPE, &b, 1, 0, 0))
  299. goto fail;
  300. b = 1;
  301. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ENABLE, &b, 1, 0, 0))
  302. goto fail;
  303. b = WILC_FW_11N_OP_MODE_HT_MIXED;
  304. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OPERATING_MODE, &b, 1, 0, 0))
  305. goto fail;
  306. b = 1;
  307. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_TXOP_PROT_DISABLE, &b, 1, 0, 0))
  308. goto fail;
  309. b = WILC_FW_OBBS_NONHT_DETECT_PROTECT_REPORT;
  310. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OBSS_NONHT_DETECTION, &b, 1,
  311. 0, 0))
  312. goto fail;
  313. b = WILC_FW_HT_PROT_RTS_CTS_NONHT;
  314. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_HT_PROT_TYPE, &b, 1, 0, 0))
  315. goto fail;
  316. b = 0;
  317. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_RIFS_PROT_ENABLE, &b, 1, 0,
  318. 0))
  319. goto fail;
  320. b = 7;
  321. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_CURRENT_TX_MCS, &b, 1, 0, 0))
  322. goto fail;
  323. b = 1;
  324. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_IMMEDIATE_BA_ENABLED, &b, 1,
  325. 1, 1))
  326. goto fail;
  327. return 0;
  328. fail:
  329. return -EINVAL;
  330. }
  331. static void wlan_deinitialize_threads(struct net_device *dev)
  332. {
  333. struct wilc_vif *vif = netdev_priv(dev);
  334. struct wilc *wl = vif->wilc;
  335. wl->close = 1;
  336. complete(&wl->txq_event);
  337. if (wl->txq_thread) {
  338. kthread_stop(wl->txq_thread);
  339. wl->txq_thread = NULL;
  340. }
  341. }
  342. static void wilc_wlan_deinitialize(struct net_device *dev)
  343. {
  344. struct wilc_vif *vif = netdev_priv(dev);
  345. struct wilc *wl = vif->wilc;
  346. if (!wl) {
  347. netdev_err(dev, "wl is NULL\n");
  348. return;
  349. }
  350. if (wl->initialized) {
  351. netdev_info(dev, "Deinitializing wilc1000...\n");
  352. if (!wl->dev_irq_num &&
  353. wl->hif_func->disable_interrupt) {
  354. mutex_lock(&wl->hif_cs);
  355. wl->hif_func->disable_interrupt(wl);
  356. mutex_unlock(&wl->hif_cs);
  357. }
  358. complete(&wl->txq_event);
  359. wlan_deinitialize_threads(dev);
  360. deinit_irq(dev);
  361. wilc_wlan_stop(wl, vif);
  362. wilc_wlan_cleanup(dev);
  363. wl->initialized = false;
  364. netdev_dbg(dev, "wilc1000 deinitialization Done\n");
  365. } else {
  366. netdev_dbg(dev, "wilc1000 is not initialized\n");
  367. }
  368. }
  369. static int wlan_initialize_threads(struct net_device *dev)
  370. {
  371. struct wilc_vif *vif = netdev_priv(dev);
  372. struct wilc *wilc = vif->wilc;
  373. wilc->txq_thread = kthread_run(wilc_txq_task, (void *)wilc,
  374. "%s-tx", dev->name);
  375. if (IS_ERR(wilc->txq_thread)) {
  376. netdev_err(dev, "couldn't create TXQ thread\n");
  377. wilc->close = 1;
  378. return PTR_ERR(wilc->txq_thread);
  379. }
  380. wait_for_completion(&wilc->txq_thread_started);
  381. return 0;
  382. }
  383. static int wilc_wlan_initialize(struct net_device *dev, struct wilc_vif *vif)
  384. {
  385. int ret = 0;
  386. struct wilc *wl = vif->wilc;
  387. if (!wl->initialized) {
  388. wl->mac_status = WILC_MAC_STATUS_INIT;
  389. wl->close = 0;
  390. ret = wilc_wlan_init(dev);
  391. if (ret)
  392. return ret;
  393. ret = wlan_initialize_threads(dev);
  394. if (ret)
  395. goto fail_wilc_wlan;
  396. if (wl->dev_irq_num && init_irq(dev)) {
  397. ret = -EIO;
  398. goto fail_threads;
  399. }
  400. if (!wl->dev_irq_num &&
  401. wl->hif_func->enable_interrupt &&
  402. wl->hif_func->enable_interrupt(wl)) {
  403. ret = -EIO;
  404. goto fail_irq_init;
  405. }
  406. ret = wilc_wlan_get_firmware(dev);
  407. if (ret)
  408. goto fail_irq_enable;
  409. ret = wilc1000_firmware_download(dev);
  410. if (ret)
  411. goto fail_irq_enable;
  412. ret = wilc_start_firmware(dev);
  413. if (ret)
  414. goto fail_irq_enable;
  415. if (wilc_wlan_cfg_get(vif, 1, WID_FIRMWARE_VERSION, 1, 0)) {
  416. int size;
  417. char firmware_ver[WILC_MAX_FW_VERSION_STR_SIZE];
  418. size = wilc_wlan_cfg_get_val(wl, WID_FIRMWARE_VERSION,
  419. firmware_ver,
  420. sizeof(firmware_ver));
  421. firmware_ver[size] = '\0';
  422. netdev_dbg(dev, "Firmware Ver = %s\n", firmware_ver);
  423. }
  424. ret = wilc_init_fw_config(dev, vif);
  425. if (ret) {
  426. netdev_err(dev, "Failed to configure firmware\n");
  427. goto fail_fw_start;
  428. }
  429. wl->initialized = true;
  430. return 0;
  431. fail_fw_start:
  432. wilc_wlan_stop(wl, vif);
  433. fail_irq_enable:
  434. if (!wl->dev_irq_num &&
  435. wl->hif_func->disable_interrupt)
  436. wl->hif_func->disable_interrupt(wl);
  437. fail_irq_init:
  438. if (wl->dev_irq_num)
  439. deinit_irq(dev);
  440. fail_threads:
  441. wlan_deinitialize_threads(dev);
  442. fail_wilc_wlan:
  443. wilc_wlan_cleanup(dev);
  444. netdev_err(dev, "WLAN initialization FAILED\n");
  445. } else {
  446. netdev_dbg(dev, "wilc1000 already initialized\n");
  447. }
  448. return ret;
  449. }
  450. static int mac_init_fn(struct net_device *ndev)
  451. {
  452. netif_start_queue(ndev);
  453. netif_stop_queue(ndev);
  454. return 0;
  455. }
  456. static int wilc_mac_open(struct net_device *ndev)
  457. {
  458. struct wilc_vif *vif = netdev_priv(ndev);
  459. struct wilc *wl = vif->wilc;
  460. int ret = 0;
  461. struct mgmt_frame_regs mgmt_regs = {};
  462. u8 addr[ETH_ALEN] __aligned(2);
  463. if (!wl || !wl->dev) {
  464. netdev_err(ndev, "device not ready\n");
  465. return -ENODEV;
  466. }
  467. netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev);
  468. ret = wilc_init_host_int(ndev);
  469. if (ret)
  470. return ret;
  471. ret = wilc_wlan_initialize(ndev, vif);
  472. if (ret) {
  473. wilc_deinit_host_int(ndev);
  474. return ret;
  475. }
  476. wilc_set_operation_mode(vif, wilc_get_vif_idx(vif), vif->iftype,
  477. vif->idx);
  478. if (is_valid_ether_addr(ndev->dev_addr)) {
  479. ether_addr_copy(addr, ndev->dev_addr);
  480. wilc_set_mac_address(vif, addr);
  481. } else {
  482. wilc_get_mac_address(vif, addr);
  483. eth_hw_addr_set(ndev, addr);
  484. }
  485. netdev_dbg(ndev, "Mac address: %pM\n", ndev->dev_addr);
  486. if (!is_valid_ether_addr(ndev->dev_addr)) {
  487. netdev_err(ndev, "Wrong MAC address\n");
  488. wilc_deinit_host_int(ndev);
  489. wilc_wlan_deinitialize(ndev);
  490. return -EINVAL;
  491. }
  492. mgmt_regs.interface_stypes = vif->mgmt_reg_stypes;
  493. /* so we detect a change */
  494. vif->mgmt_reg_stypes = 0;
  495. wilc_update_mgmt_frame_registrations(vif->ndev->ieee80211_ptr->wiphy,
  496. vif->ndev->ieee80211_ptr,
  497. &mgmt_regs);
  498. netif_wake_queue(ndev);
  499. wl->open_ifcs++;
  500. vif->mac_opened = 1;
  501. return 0;
  502. }
  503. static struct net_device_stats *mac_stats(struct net_device *dev)
  504. {
  505. struct wilc_vif *vif = netdev_priv(dev);
  506. return &vif->netstats;
  507. }
  508. static int wilc_set_mac_addr(struct net_device *dev, void *p)
  509. {
  510. int result;
  511. struct wilc_vif *vif = netdev_priv(dev);
  512. struct wilc *wilc = vif->wilc;
  513. struct sockaddr *addr = (struct sockaddr *)p;
  514. unsigned char mac_addr[ETH_ALEN];
  515. struct wilc_vif *tmp_vif;
  516. int srcu_idx;
  517. if (!is_valid_ether_addr(addr->sa_data))
  518. return -EADDRNOTAVAIL;
  519. if (!vif->mac_opened) {
  520. eth_commit_mac_addr_change(dev, p);
  521. return 0;
  522. }
  523. /* Verify MAC Address is not already in use: */
  524. srcu_idx = srcu_read_lock(&wilc->srcu);
  525. list_for_each_entry_rcu(tmp_vif, &wilc->vif_list, list) {
  526. wilc_get_mac_address(tmp_vif, mac_addr);
  527. if (ether_addr_equal(addr->sa_data, mac_addr)) {
  528. if (vif != tmp_vif) {
  529. srcu_read_unlock(&wilc->srcu, srcu_idx);
  530. return -EADDRNOTAVAIL;
  531. }
  532. srcu_read_unlock(&wilc->srcu, srcu_idx);
  533. return 0;
  534. }
  535. }
  536. srcu_read_unlock(&wilc->srcu, srcu_idx);
  537. result = wilc_set_mac_address(vif, (u8 *)addr->sa_data);
  538. if (result)
  539. return result;
  540. eth_commit_mac_addr_change(dev, p);
  541. return result;
  542. }
  543. static void wilc_set_multicast_list(struct net_device *dev)
  544. {
  545. struct netdev_hw_addr *ha;
  546. struct wilc_vif *vif = netdev_priv(dev);
  547. int i;
  548. u8 *mc_list;
  549. u8 *cur_mc;
  550. if (dev->flags & IFF_PROMISC)
  551. return;
  552. if (dev->flags & IFF_ALLMULTI ||
  553. dev->mc.count > WILC_MULTICAST_TABLE_SIZE) {
  554. wilc_setup_multicast_filter(vif, 0, 0, NULL);
  555. return;
  556. }
  557. if (dev->mc.count == 0) {
  558. wilc_setup_multicast_filter(vif, 1, 0, NULL);
  559. return;
  560. }
  561. mc_list = kmalloc_array(dev->mc.count, ETH_ALEN, GFP_ATOMIC);
  562. if (!mc_list)
  563. return;
  564. cur_mc = mc_list;
  565. i = 0;
  566. netdev_for_each_mc_addr(ha, dev) {
  567. memcpy(cur_mc, ha->addr, ETH_ALEN);
  568. netdev_dbg(dev, "Entry[%d]: %pM\n", i, cur_mc);
  569. i++;
  570. cur_mc += ETH_ALEN;
  571. }
  572. if (wilc_setup_multicast_filter(vif, 1, dev->mc.count, mc_list))
  573. kfree(mc_list);
  574. }
  575. static void wilc_tx_complete(void *priv, int status)
  576. {
  577. struct tx_complete_data *pv_data = priv;
  578. dev_kfree_skb(pv_data->skb);
  579. kfree(pv_data);
  580. }
  581. netdev_tx_t wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev)
  582. {
  583. struct wilc_vif *vif = netdev_priv(ndev);
  584. struct wilc *wilc = vif->wilc;
  585. struct tx_complete_data *tx_data = NULL;
  586. int queue_count;
  587. if (skb->dev != ndev) {
  588. netdev_err(ndev, "Packet not destined to this device\n");
  589. dev_kfree_skb(skb);
  590. return NETDEV_TX_OK;
  591. }
  592. tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
  593. if (!tx_data) {
  594. dev_kfree_skb(skb);
  595. netif_wake_queue(ndev);
  596. return NETDEV_TX_OK;
  597. }
  598. tx_data->buff = skb->data;
  599. tx_data->size = skb->len;
  600. tx_data->skb = skb;
  601. vif->netstats.tx_packets++;
  602. vif->netstats.tx_bytes += tx_data->size;
  603. queue_count = wilc_wlan_txq_add_net_pkt(ndev, tx_data,
  604. tx_data->buff, tx_data->size,
  605. wilc_tx_complete);
  606. if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) {
  607. int srcu_idx;
  608. struct wilc_vif *vif;
  609. srcu_idx = srcu_read_lock(&wilc->srcu);
  610. list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
  611. if (vif->mac_opened)
  612. netif_stop_queue(vif->ndev);
  613. }
  614. srcu_read_unlock(&wilc->srcu, srcu_idx);
  615. }
  616. return NETDEV_TX_OK;
  617. }
  618. static int wilc_mac_close(struct net_device *ndev)
  619. {
  620. struct wilc_vif *vif = netdev_priv(ndev);
  621. struct wilc *wl = vif->wilc;
  622. netdev_dbg(ndev, "Mac close\n");
  623. if (wl->open_ifcs > 0)
  624. wl->open_ifcs--;
  625. else
  626. return 0;
  627. if (vif->ndev) {
  628. netif_stop_queue(vif->ndev);
  629. wilc_handle_disconnect(vif);
  630. wilc_deinit_host_int(vif->ndev);
  631. }
  632. if (wl->open_ifcs == 0) {
  633. netdev_dbg(ndev, "Deinitializing wilc1000\n");
  634. wl->close = 1;
  635. wilc_wlan_deinitialize(ndev);
  636. }
  637. vif->mac_opened = 0;
  638. return 0;
  639. }
  640. void wilc_frmw_to_host(struct wilc *wilc, u8 *buff, u32 size,
  641. u32 pkt_offset)
  642. {
  643. unsigned int frame_len = 0;
  644. int stats;
  645. unsigned char *buff_to_send = NULL;
  646. struct sk_buff *skb;
  647. struct net_device *wilc_netdev;
  648. struct wilc_vif *vif;
  649. if (!wilc)
  650. return;
  651. wilc_netdev = get_if_handler(wilc, buff);
  652. if (!wilc_netdev)
  653. return;
  654. buff += pkt_offset;
  655. vif = netdev_priv(wilc_netdev);
  656. if (size > 0) {
  657. frame_len = size;
  658. buff_to_send = buff;
  659. skb = dev_alloc_skb(frame_len);
  660. if (!skb)
  661. return;
  662. skb->dev = wilc_netdev;
  663. skb_put_data(skb, buff_to_send, frame_len);
  664. skb->protocol = eth_type_trans(skb, wilc_netdev);
  665. vif->netstats.rx_packets++;
  666. vif->netstats.rx_bytes += frame_len;
  667. skb->ip_summed = CHECKSUM_UNNECESSARY;
  668. stats = netif_rx(skb);
  669. netdev_dbg(wilc_netdev, "netif_rx ret value is: %d\n", stats);
  670. }
  671. }
  672. void wilc_wfi_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size, bool is_auth)
  673. {
  674. int srcu_idx;
  675. struct wilc_vif *vif;
  676. srcu_idx = srcu_read_lock(&wilc->srcu);
  677. list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
  678. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buff;
  679. u16 type = le16_to_cpup((__le16 *)buff);
  680. u32 type_bit = BIT(type >> 4);
  681. u32 auth_bit = BIT(IEEE80211_STYPE_AUTH >> 4);
  682. if ((vif->mgmt_reg_stypes & auth_bit &&
  683. ieee80211_is_auth(mgmt->frame_control)) &&
  684. vif->iftype == WILC_STATION_MODE && is_auth) {
  685. wilc_wfi_mgmt_frame_rx(vif, buff, size);
  686. break;
  687. }
  688. if (vif->priv.p2p_listen_state &&
  689. vif->mgmt_reg_stypes & type_bit)
  690. wilc_wfi_p2p_rx(vif, buff, size);
  691. if (vif->monitor_flag)
  692. wilc_wfi_monitor_rx(wilc->monitor_dev, buff, size);
  693. }
  694. srcu_read_unlock(&wilc->srcu, srcu_idx);
  695. }
  696. static const struct net_device_ops wilc_netdev_ops = {
  697. .ndo_init = mac_init_fn,
  698. .ndo_open = wilc_mac_open,
  699. .ndo_stop = wilc_mac_close,
  700. .ndo_set_mac_address = wilc_set_mac_addr,
  701. .ndo_start_xmit = wilc_mac_xmit,
  702. .ndo_get_stats = mac_stats,
  703. .ndo_set_rx_mode = wilc_set_multicast_list,
  704. };
  705. void wilc_netdev_cleanup(struct wilc *wilc)
  706. {
  707. struct wilc_vif *vif;
  708. int srcu_idx, ifc_cnt = 0;
  709. if (!wilc)
  710. return;
  711. if (wilc->firmware) {
  712. release_firmware(wilc->firmware);
  713. wilc->firmware = NULL;
  714. }
  715. srcu_idx = srcu_read_lock(&wilc->srcu);
  716. list_for_each_entry_rcu(vif, &wilc->vif_list, list) {
  717. if (vif->ndev)
  718. unregister_netdev(vif->ndev);
  719. }
  720. srcu_read_unlock(&wilc->srcu, srcu_idx);
  721. wilc_wfi_deinit_mon_interface(wilc, false);
  722. destroy_workqueue(wilc->hif_workqueue);
  723. while (ifc_cnt < WILC_NUM_CONCURRENT_IFC) {
  724. mutex_lock(&wilc->vif_mutex);
  725. if (wilc->vif_num <= 0) {
  726. mutex_unlock(&wilc->vif_mutex);
  727. break;
  728. }
  729. vif = wilc_get_wl_to_vif(wilc);
  730. if (!IS_ERR(vif))
  731. list_del_rcu(&vif->list);
  732. wilc->vif_num--;
  733. mutex_unlock(&wilc->vif_mutex);
  734. synchronize_srcu(&wilc->srcu);
  735. ifc_cnt++;
  736. }
  737. wilc_wlan_cfg_deinit(wilc);
  738. wlan_deinit_locks(wilc);
  739. wiphy_unregister(wilc->wiphy);
  740. wiphy_free(wilc->wiphy);
  741. }
  742. EXPORT_SYMBOL_GPL(wilc_netdev_cleanup);
  743. static u8 wilc_get_available_idx(struct wilc *wl)
  744. {
  745. int idx = 0;
  746. struct wilc_vif *vif;
  747. int srcu_idx;
  748. srcu_idx = srcu_read_lock(&wl->srcu);
  749. list_for_each_entry_rcu(vif, &wl->vif_list, list) {
  750. if (vif->idx == 0)
  751. idx = 1;
  752. else
  753. idx = 0;
  754. }
  755. srcu_read_unlock(&wl->srcu, srcu_idx);
  756. return idx;
  757. }
  758. struct wilc_vif *wilc_netdev_ifc_init(struct wilc *wl, const char *name,
  759. int vif_type, enum nl80211_iftype type,
  760. bool rtnl_locked)
  761. {
  762. struct net_device *ndev;
  763. struct wilc_vif *vif;
  764. int ret;
  765. ndev = alloc_etherdev(sizeof(*vif));
  766. if (!ndev)
  767. return ERR_PTR(-ENOMEM);
  768. vif = netdev_priv(ndev);
  769. ndev->ieee80211_ptr = &vif->priv.wdev;
  770. strcpy(ndev->name, name);
  771. vif->wilc = wl;
  772. vif->ndev = ndev;
  773. ndev->ml_priv = vif;
  774. ndev->netdev_ops = &wilc_netdev_ops;
  775. SET_NETDEV_DEV(ndev, wiphy_dev(wl->wiphy));
  776. vif->priv.wdev.wiphy = wl->wiphy;
  777. vif->priv.wdev.netdev = ndev;
  778. vif->priv.wdev.iftype = type;
  779. vif->priv.dev = ndev;
  780. if (rtnl_locked)
  781. ret = cfg80211_register_netdevice(ndev);
  782. else
  783. ret = register_netdev(ndev);
  784. if (ret) {
  785. ret = -EFAULT;
  786. goto error;
  787. }
  788. wl->hif_workqueue = alloc_ordered_workqueue("%s-wq", WQ_MEM_RECLAIM,
  789. ndev->name);
  790. if (!wl->hif_workqueue) {
  791. ret = -ENOMEM;
  792. goto unregister_netdev;
  793. }
  794. ndev->needs_free_netdev = true;
  795. vif->iftype = vif_type;
  796. vif->idx = wilc_get_available_idx(wl);
  797. vif->mac_opened = 0;
  798. mutex_lock(&wl->vif_mutex);
  799. list_add_tail_rcu(&vif->list, &wl->vif_list);
  800. wl->vif_num += 1;
  801. mutex_unlock(&wl->vif_mutex);
  802. synchronize_srcu(&wl->srcu);
  803. return vif;
  804. unregister_netdev:
  805. if (rtnl_locked)
  806. cfg80211_unregister_netdevice(ndev);
  807. else
  808. unregister_netdev(ndev);
  809. error:
  810. free_netdev(ndev);
  811. return ERR_PTR(ret);
  812. }
  813. MODULE_LICENSE("GPL");
  814. MODULE_FIRMWARE(WILC1000_FW(WILC1000_API_VER));