netdev.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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. wilc_for_each_vif(wilc, vif) {
  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. wilc_for_each_vif(wilc, vif) {
  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 void wilc_wake_tx_queues(struct wilc *wl)
  118. {
  119. int srcu_idx;
  120. struct wilc_vif *ifc;
  121. srcu_idx = srcu_read_lock(&wl->srcu);
  122. wilc_for_each_vif(wl, ifc) {
  123. if (ifc->mac_opened && netif_queue_stopped(ifc->ndev))
  124. netif_wake_queue(ifc->ndev);
  125. }
  126. srcu_read_unlock(&wl->srcu, srcu_idx);
  127. }
  128. static int wilc_txq_task(void *vp)
  129. {
  130. int ret;
  131. u32 txq_count;
  132. struct wilc *wl = vp;
  133. complete(&wl->txq_thread_started);
  134. while (1) {
  135. if (wait_for_completion_interruptible(&wl->txq_event))
  136. continue;
  137. if (wl->close) {
  138. complete(&wl->txq_thread_started);
  139. while (!kthread_should_stop())
  140. schedule();
  141. break;
  142. }
  143. do {
  144. ret = wilc_wlan_handle_txq(wl, &txq_count);
  145. if (txq_count < FLOW_CONTROL_LOWER_THRESHOLD) {
  146. wilc_wake_tx_queues(wl);
  147. }
  148. if (ret != WILC_VMM_ENTRY_FULL_RETRY)
  149. break;
  150. /* Back off TX task from sending packets for some time.
  151. * msleep_interruptible will allow RX task to run and
  152. * free buffers. TX task will be in TASK_INTERRUPTIBLE
  153. * state which will put the thread back to CPU running
  154. * queue when it's signaled even if the timeout isn't
  155. * elapsed. This gives faster chance for reserved SK
  156. * buffers to be free.
  157. */
  158. msleep_interruptible(TX_BACKOFF_WEIGHT_MS);
  159. } while (!wl->close);
  160. }
  161. return 0;
  162. }
  163. static int wilc_wlan_get_firmware(struct net_device *dev)
  164. {
  165. struct wilc_vif *vif = netdev_priv(dev);
  166. struct wilc *wilc = vif->wilc;
  167. int chip_id;
  168. const struct firmware *wilc_fw;
  169. int ret;
  170. chip_id = wilc_get_chipid(wilc, false);
  171. netdev_info(dev, "ChipID [%x] loading firmware [%s]\n", chip_id,
  172. WILC1000_FW(WILC1000_API_VER));
  173. ret = request_firmware(&wilc_fw, WILC1000_FW(WILC1000_API_VER),
  174. wilc->dev);
  175. if (ret != 0) {
  176. netdev_err(dev, "%s - firmware not available\n",
  177. WILC1000_FW(WILC1000_API_VER));
  178. return -EINVAL;
  179. }
  180. wilc->firmware = wilc_fw;
  181. return 0;
  182. }
  183. static int wilc_start_firmware(struct net_device *dev)
  184. {
  185. struct wilc_vif *vif = netdev_priv(dev);
  186. struct wilc *wilc = vif->wilc;
  187. int ret = 0;
  188. ret = wilc_wlan_start(wilc);
  189. if (ret)
  190. return ret;
  191. if (!wait_for_completion_timeout(&wilc->sync_event,
  192. msecs_to_jiffies(5000)))
  193. return -ETIME;
  194. return 0;
  195. }
  196. static int wilc1000_firmware_download(struct net_device *dev)
  197. {
  198. struct wilc_vif *vif = netdev_priv(dev);
  199. struct wilc *wilc = vif->wilc;
  200. int ret = 0;
  201. if (!wilc->firmware) {
  202. netdev_err(dev, "Firmware buffer is NULL\n");
  203. return -ENOBUFS;
  204. }
  205. ret = wilc_wlan_firmware_download(wilc, wilc->firmware->data,
  206. wilc->firmware->size);
  207. if (ret)
  208. return ret;
  209. release_firmware(wilc->firmware);
  210. wilc->firmware = NULL;
  211. netdev_dbg(dev, "Download Succeeded\n");
  212. return 0;
  213. }
  214. static int wilc_init_fw_config(struct net_device *dev, struct wilc_vif *vif)
  215. {
  216. struct wilc_priv *priv = &vif->priv;
  217. struct host_if_drv *hif_drv;
  218. u8 b;
  219. u16 hw;
  220. u32 w;
  221. netdev_dbg(dev, "Start configuring Firmware\n");
  222. hif_drv = (struct host_if_drv *)priv->hif_drv;
  223. netdev_dbg(dev, "Host = %p\n", hif_drv);
  224. w = vif->iftype;
  225. cpu_to_le32s(&w);
  226. if (!wilc_wlan_cfg_set(vif, 1, WID_SET_OPERATION_MODE, (u8 *)&w, 4,
  227. 0, 0))
  228. goto fail;
  229. b = WILC_FW_BSS_TYPE_INFRA;
  230. if (!wilc_wlan_cfg_set(vif, 0, WID_BSS_TYPE, &b, 1, 0, 0))
  231. goto fail;
  232. b = WILC_FW_TX_RATE_AUTO;
  233. if (!wilc_wlan_cfg_set(vif, 0, WID_CURRENT_TX_RATE, &b, 1, 0, 0))
  234. goto fail;
  235. b = WILC_FW_OPER_MODE_G_MIXED_11B_2;
  236. if (!wilc_wlan_cfg_set(vif, 0, WID_11G_OPERATING_MODE, &b, 1, 0, 0))
  237. goto fail;
  238. b = WILC_FW_PREAMBLE_AUTO;
  239. if (!wilc_wlan_cfg_set(vif, 0, WID_PREAMBLE, &b, 1, 0, 0))
  240. goto fail;
  241. b = WILC_FW_11N_PROT_AUTO;
  242. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_PROT_MECH, &b, 1, 0, 0))
  243. goto fail;
  244. b = WILC_FW_ACTIVE_SCAN;
  245. if (!wilc_wlan_cfg_set(vif, 0, WID_SCAN_TYPE, &b, 1, 0, 0))
  246. goto fail;
  247. b = WILC_FW_SITE_SURVEY_OFF;
  248. if (!wilc_wlan_cfg_set(vif, 0, WID_SITE_SURVEY, &b, 1, 0, 0))
  249. goto fail;
  250. hw = 0xffff;
  251. cpu_to_le16s(&hw);
  252. if (!wilc_wlan_cfg_set(vif, 0, WID_RTS_THRESHOLD, (u8 *)&hw, 2, 0, 0))
  253. goto fail;
  254. hw = 2346;
  255. cpu_to_le16s(&hw);
  256. if (!wilc_wlan_cfg_set(vif, 0, WID_FRAG_THRESHOLD, (u8 *)&hw, 2, 0, 0))
  257. goto fail;
  258. b = 0;
  259. if (!wilc_wlan_cfg_set(vif, 0, WID_BCAST_SSID, &b, 1, 0, 0))
  260. goto fail;
  261. b = 1;
  262. if (!wilc_wlan_cfg_set(vif, 0, WID_QOS_ENABLE, &b, 1, 0, 0))
  263. goto fail;
  264. b = WILC_FW_NO_POWERSAVE;
  265. if (!wilc_wlan_cfg_set(vif, 0, WID_POWER_MANAGEMENT, &b, 1, 0, 0))
  266. goto fail;
  267. b = WILC_FW_SEC_NO;
  268. if (!wilc_wlan_cfg_set(vif, 0, WID_11I_MODE, &b, 1, 0, 0))
  269. goto fail;
  270. b = WILC_FW_AUTH_OPEN_SYSTEM;
  271. if (!wilc_wlan_cfg_set(vif, 0, WID_AUTH_TYPE, &b, 1, 0, 0))
  272. goto fail;
  273. b = 3;
  274. if (!wilc_wlan_cfg_set(vif, 0, WID_LISTEN_INTERVAL, &b, 1, 0, 0))
  275. goto fail;
  276. b = 3;
  277. if (!wilc_wlan_cfg_set(vif, 0, WID_DTIM_PERIOD, &b, 1, 0, 0))
  278. goto fail;
  279. b = WILC_FW_ACK_POLICY_NORMAL;
  280. if (!wilc_wlan_cfg_set(vif, 0, WID_ACK_POLICY, &b, 1, 0, 0))
  281. goto fail;
  282. b = 0;
  283. if (!wilc_wlan_cfg_set(vif, 0, WID_USER_CONTROL_ON_TX_POWER, &b, 1,
  284. 0, 0))
  285. goto fail;
  286. b = 48;
  287. if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11A, &b, 1, 0, 0))
  288. goto fail;
  289. b = 28;
  290. if (!wilc_wlan_cfg_set(vif, 0, WID_TX_POWER_LEVEL_11B, &b, 1, 0, 0))
  291. goto fail;
  292. hw = 100;
  293. cpu_to_le16s(&hw);
  294. if (!wilc_wlan_cfg_set(vif, 0, WID_BEACON_INTERVAL, (u8 *)&hw, 2, 0, 0))
  295. goto fail;
  296. b = WILC_FW_REKEY_POLICY_DISABLE;
  297. if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_POLICY, &b, 1, 0, 0))
  298. goto fail;
  299. w = 84600;
  300. cpu_to_le32s(&w);
  301. if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PERIOD, (u8 *)&w, 4, 0, 0))
  302. goto fail;
  303. w = 500;
  304. cpu_to_le32s(&w);
  305. if (!wilc_wlan_cfg_set(vif, 0, WID_REKEY_PACKET_COUNT, (u8 *)&w, 4, 0,
  306. 0))
  307. goto fail;
  308. b = 1;
  309. if (!wilc_wlan_cfg_set(vif, 0, WID_SHORT_SLOT_ALLOWED, &b, 1, 0,
  310. 0))
  311. goto fail;
  312. b = WILC_FW_ERP_PROT_SELF_CTS;
  313. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ERP_PROT_TYPE, &b, 1, 0, 0))
  314. goto fail;
  315. b = 1;
  316. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_ENABLE, &b, 1, 0, 0))
  317. goto fail;
  318. b = WILC_FW_11N_OP_MODE_HT_MIXED;
  319. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OPERATING_MODE, &b, 1, 0, 0))
  320. goto fail;
  321. b = 1;
  322. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_TXOP_PROT_DISABLE, &b, 1, 0, 0))
  323. goto fail;
  324. b = WILC_FW_OBBS_NONHT_DETECT_PROTECT_REPORT;
  325. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_OBSS_NONHT_DETECTION, &b, 1,
  326. 0, 0))
  327. goto fail;
  328. b = WILC_FW_HT_PROT_RTS_CTS_NONHT;
  329. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_HT_PROT_TYPE, &b, 1, 0, 0))
  330. goto fail;
  331. b = 0;
  332. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_RIFS_PROT_ENABLE, &b, 1, 0,
  333. 0))
  334. goto fail;
  335. b = 7;
  336. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_CURRENT_TX_MCS, &b, 1, 0, 0))
  337. goto fail;
  338. b = 1;
  339. if (!wilc_wlan_cfg_set(vif, 0, WID_11N_IMMEDIATE_BA_ENABLED, &b, 1,
  340. 1, 0))
  341. goto fail;
  342. return 0;
  343. fail:
  344. return -EINVAL;
  345. }
  346. static void wlan_deinitialize_threads(struct net_device *dev)
  347. {
  348. struct wilc_vif *vif = netdev_priv(dev);
  349. struct wilc *wl = vif->wilc;
  350. wl->close = 1;
  351. complete(&wl->txq_event);
  352. if (wl->txq_thread) {
  353. kthread_stop(wl->txq_thread);
  354. wl->txq_thread = NULL;
  355. }
  356. }
  357. static void wilc_wlan_deinitialize(struct net_device *dev)
  358. {
  359. struct wilc_vif *vif = netdev_priv(dev);
  360. struct wilc *wl = vif->wilc;
  361. if (!wl) {
  362. netdev_err(dev, "wl is NULL\n");
  363. return;
  364. }
  365. if (wl->initialized) {
  366. netdev_info(dev, "Deinitializing wilc1000...\n");
  367. if (!wl->dev_irq_num &&
  368. wl->hif_func->disable_interrupt) {
  369. mutex_lock(&wl->hif_cs);
  370. wl->hif_func->disable_interrupt(wl);
  371. mutex_unlock(&wl->hif_cs);
  372. }
  373. complete(&wl->txq_event);
  374. wlan_deinitialize_threads(dev);
  375. deinit_irq(dev);
  376. wilc_wlan_stop(wl, vif);
  377. wilc_wlan_cleanup(dev);
  378. wl->initialized = false;
  379. netdev_dbg(dev, "wilc1000 deinitialization Done\n");
  380. } else {
  381. netdev_dbg(dev, "wilc1000 is not initialized\n");
  382. }
  383. }
  384. static int wlan_initialize_threads(struct net_device *dev)
  385. {
  386. struct wilc_vif *vif = netdev_priv(dev);
  387. struct wilc *wilc = vif->wilc;
  388. wilc->txq_thread = kthread_run(wilc_txq_task, (void *)wilc,
  389. "%s-tx", dev->name);
  390. if (IS_ERR(wilc->txq_thread)) {
  391. netdev_err(dev, "couldn't create TXQ thread\n");
  392. wilc->close = 1;
  393. return PTR_ERR(wilc->txq_thread);
  394. }
  395. wait_for_completion(&wilc->txq_thread_started);
  396. return 0;
  397. }
  398. static int wilc_wlan_initialize(struct net_device *dev, struct wilc_vif *vif)
  399. {
  400. int ret = 0;
  401. struct wilc *wl = vif->wilc;
  402. if (!wl->initialized) {
  403. wl->mac_status = WILC_MAC_STATUS_INIT;
  404. wl->close = 0;
  405. ret = wilc_wlan_init(dev);
  406. if (ret)
  407. return ret;
  408. ret = wlan_initialize_threads(dev);
  409. if (ret)
  410. goto fail_wilc_wlan;
  411. if (wl->dev_irq_num && init_irq(dev)) {
  412. ret = -EIO;
  413. goto fail_threads;
  414. }
  415. if (!wl->dev_irq_num &&
  416. wl->hif_func->enable_interrupt &&
  417. wl->hif_func->enable_interrupt(wl)) {
  418. ret = -EIO;
  419. goto fail_irq_init;
  420. }
  421. ret = wilc_wlan_get_firmware(dev);
  422. if (ret)
  423. goto fail_irq_enable;
  424. ret = wilc1000_firmware_download(dev);
  425. if (ret)
  426. goto fail_irq_enable;
  427. ret = wilc_start_firmware(dev);
  428. if (ret)
  429. goto fail_irq_enable;
  430. if (wilc_wlan_cfg_get(vif, 1, WID_FIRMWARE_VERSION, 1, 0)) {
  431. int size;
  432. char firmware_ver[WILC_MAX_FW_VERSION_STR_SIZE];
  433. size = wilc_wlan_cfg_get_val(wl, WID_FIRMWARE_VERSION,
  434. firmware_ver,
  435. sizeof(firmware_ver));
  436. firmware_ver[size] = '\0';
  437. netdev_dbg(dev, "Firmware Ver = %s\n", firmware_ver);
  438. }
  439. ret = wilc_init_fw_config(dev, vif);
  440. if (ret) {
  441. netdev_err(dev, "Failed to configure firmware\n");
  442. goto fail_fw_start;
  443. }
  444. wl->initialized = true;
  445. return 0;
  446. fail_fw_start:
  447. wilc_wlan_stop(wl, vif);
  448. fail_irq_enable:
  449. if (!wl->dev_irq_num &&
  450. wl->hif_func->disable_interrupt)
  451. wl->hif_func->disable_interrupt(wl);
  452. fail_irq_init:
  453. if (wl->dev_irq_num)
  454. deinit_irq(dev);
  455. fail_threads:
  456. wlan_deinitialize_threads(dev);
  457. fail_wilc_wlan:
  458. wilc_wlan_cleanup(dev);
  459. netdev_err(dev, "WLAN initialization FAILED\n");
  460. } else {
  461. netdev_dbg(dev, "wilc1000 already initialized\n");
  462. }
  463. return ret;
  464. }
  465. static int mac_init_fn(struct net_device *ndev)
  466. {
  467. netif_start_queue(ndev);
  468. netif_stop_queue(ndev);
  469. return 0;
  470. }
  471. static int wilc_mac_open(struct net_device *ndev)
  472. {
  473. struct wilc_vif *vif = netdev_priv(ndev);
  474. struct wilc *wl = vif->wilc;
  475. int ret = 0;
  476. struct mgmt_frame_regs mgmt_regs = {};
  477. if (!wl || !wl->dev) {
  478. netdev_err(ndev, "device not ready\n");
  479. return -ENODEV;
  480. }
  481. netdev_dbg(ndev, "MAC OPEN[%p]\n", ndev);
  482. ret = wilc_init_host_int(ndev);
  483. if (ret)
  484. return ret;
  485. ret = wilc_wlan_initialize(ndev, vif);
  486. if (ret) {
  487. wilc_deinit_host_int(ndev);
  488. return ret;
  489. }
  490. wilc_set_operation_mode(vif, wilc_get_vif_idx(vif), vif->iftype,
  491. vif->idx);
  492. netdev_dbg(ndev, "Mac address: %pM\n", ndev->dev_addr);
  493. ret = wilc_set_mac_address(vif, ndev->dev_addr);
  494. if (ret) {
  495. netdev_err(ndev, "Failed to enforce MAC address in chip");
  496. wilc_deinit_host_int(ndev);
  497. if (!wl->open_ifcs)
  498. wilc_wlan_deinitialize(ndev);
  499. return ret;
  500. }
  501. mgmt_regs.interface_stypes = vif->mgmt_reg_stypes;
  502. /* so we detect a change */
  503. vif->mgmt_reg_stypes = 0;
  504. wilc_update_mgmt_frame_registrations(vif->ndev->ieee80211_ptr->wiphy,
  505. vif->ndev->ieee80211_ptr,
  506. &mgmt_regs);
  507. netif_wake_queue(ndev);
  508. wl->open_ifcs++;
  509. vif->mac_opened = 1;
  510. return 0;
  511. }
  512. static struct net_device_stats *mac_stats(struct net_device *dev)
  513. {
  514. struct wilc_vif *vif = netdev_priv(dev);
  515. return &vif->netstats;
  516. }
  517. static int wilc_set_mac_addr(struct net_device *dev, void *p)
  518. {
  519. int result;
  520. struct wilc_vif *vif = netdev_priv(dev);
  521. struct wilc *wilc = vif->wilc;
  522. struct sockaddr *addr = (struct sockaddr *)p;
  523. unsigned char mac_addr[ETH_ALEN];
  524. struct wilc_vif *tmp_vif;
  525. int srcu_idx;
  526. if (!is_valid_ether_addr(addr->sa_data))
  527. return -EADDRNOTAVAIL;
  528. if (!vif->mac_opened) {
  529. eth_commit_mac_addr_change(dev, p);
  530. return 0;
  531. }
  532. /* Verify MAC Address is not already in use: */
  533. srcu_idx = srcu_read_lock(&wilc->srcu);
  534. wilc_for_each_vif(wilc, tmp_vif) {
  535. wilc_get_mac_address(tmp_vif, mac_addr);
  536. if (ether_addr_equal(addr->sa_data, mac_addr)) {
  537. if (vif != tmp_vif) {
  538. srcu_read_unlock(&wilc->srcu, srcu_idx);
  539. return -EADDRNOTAVAIL;
  540. }
  541. srcu_read_unlock(&wilc->srcu, srcu_idx);
  542. return 0;
  543. }
  544. }
  545. srcu_read_unlock(&wilc->srcu, srcu_idx);
  546. result = wilc_set_mac_address(vif, addr->sa_data);
  547. if (result)
  548. return result;
  549. eth_commit_mac_addr_change(dev, p);
  550. return result;
  551. }
  552. static void wilc_set_multicast_list(struct net_device *dev)
  553. {
  554. struct netdev_hw_addr *ha;
  555. struct wilc_vif *vif = netdev_priv(dev);
  556. int i;
  557. u8 *mc_list;
  558. u8 *cur_mc;
  559. if (dev->flags & IFF_PROMISC)
  560. return;
  561. if (dev->flags & IFF_ALLMULTI ||
  562. dev->mc.count > WILC_MULTICAST_TABLE_SIZE) {
  563. wilc_setup_multicast_filter(vif, 0, 0, NULL);
  564. return;
  565. }
  566. if (dev->mc.count == 0) {
  567. wilc_setup_multicast_filter(vif, 1, 0, NULL);
  568. return;
  569. }
  570. mc_list = kmalloc_array(dev->mc.count, ETH_ALEN, GFP_ATOMIC);
  571. if (!mc_list)
  572. return;
  573. cur_mc = mc_list;
  574. i = 0;
  575. netdev_for_each_mc_addr(ha, dev) {
  576. memcpy(cur_mc, ha->addr, ETH_ALEN);
  577. netdev_dbg(dev, "Entry[%d]: %pM\n", i, cur_mc);
  578. i++;
  579. cur_mc += ETH_ALEN;
  580. }
  581. if (wilc_setup_multicast_filter(vif, 1, dev->mc.count, mc_list))
  582. kfree(mc_list);
  583. }
  584. static void wilc_tx_complete(void *priv, int status)
  585. {
  586. struct tx_complete_data *pv_data = priv;
  587. dev_kfree_skb(pv_data->skb);
  588. kfree(pv_data);
  589. }
  590. netdev_tx_t wilc_mac_xmit(struct sk_buff *skb, struct net_device *ndev)
  591. {
  592. struct wilc_vif *vif = netdev_priv(ndev);
  593. struct wilc *wilc = vif->wilc;
  594. struct tx_complete_data *tx_data = NULL;
  595. int queue_count;
  596. if (skb->dev != ndev) {
  597. netdev_err(ndev, "Packet not destined to this device\n");
  598. dev_kfree_skb(skb);
  599. return NETDEV_TX_OK;
  600. }
  601. tx_data = kmalloc(sizeof(*tx_data), GFP_ATOMIC);
  602. if (!tx_data) {
  603. dev_kfree_skb(skb);
  604. netif_wake_queue(ndev);
  605. return NETDEV_TX_OK;
  606. }
  607. tx_data->buff = skb->data;
  608. tx_data->size = skb->len;
  609. tx_data->skb = skb;
  610. vif->netstats.tx_packets++;
  611. vif->netstats.tx_bytes += tx_data->size;
  612. queue_count = wilc_wlan_txq_add_net_pkt(ndev, tx_data,
  613. tx_data->buff, tx_data->size,
  614. wilc_tx_complete);
  615. if (queue_count > FLOW_CONTROL_UPPER_THRESHOLD) {
  616. int srcu_idx;
  617. struct wilc_vif *vif;
  618. srcu_idx = srcu_read_lock(&wilc->srcu);
  619. wilc_for_each_vif(wilc, vif) {
  620. if (vif->mac_opened)
  621. netif_stop_queue(vif->ndev);
  622. }
  623. srcu_read_unlock(&wilc->srcu, srcu_idx);
  624. }
  625. return NETDEV_TX_OK;
  626. }
  627. static int wilc_mac_close(struct net_device *ndev)
  628. {
  629. struct wilc_vif *vif = netdev_priv(ndev);
  630. struct wilc *wl = vif->wilc;
  631. netdev_dbg(ndev, "Mac close\n");
  632. if (wl->open_ifcs > 0)
  633. wl->open_ifcs--;
  634. else
  635. return 0;
  636. if (vif->ndev) {
  637. netif_stop_queue(vif->ndev);
  638. wilc_handle_disconnect(vif);
  639. wilc_deinit_host_int(vif->ndev);
  640. }
  641. if (wl->open_ifcs == 0) {
  642. netdev_dbg(ndev, "Deinitializing wilc1000\n");
  643. wl->close = 1;
  644. wilc_wlan_deinitialize(ndev);
  645. }
  646. vif->mac_opened = 0;
  647. return 0;
  648. }
  649. void wilc_frmw_to_host(struct wilc *wilc, u8 *buff, u32 size,
  650. u32 pkt_offset)
  651. {
  652. unsigned char *buff_to_send = NULL;
  653. struct net_device *wilc_netdev;
  654. unsigned int frame_len = 0;
  655. struct wilc_vif *vif;
  656. struct sk_buff *skb;
  657. int srcu_idx;
  658. int stats;
  659. if (!wilc)
  660. return;
  661. srcu_idx = srcu_read_lock(&wilc->srcu);
  662. wilc_netdev = get_if_handler(wilc, buff);
  663. if (!wilc_netdev)
  664. goto out;
  665. buff += pkt_offset;
  666. vif = netdev_priv(wilc_netdev);
  667. if (size > 0) {
  668. frame_len = size;
  669. buff_to_send = buff;
  670. skb = dev_alloc_skb(frame_len);
  671. if (!skb)
  672. goto out;
  673. skb->dev = wilc_netdev;
  674. skb_put_data(skb, buff_to_send, frame_len);
  675. skb->protocol = eth_type_trans(skb, wilc_netdev);
  676. vif->netstats.rx_packets++;
  677. vif->netstats.rx_bytes += frame_len;
  678. skb->ip_summed = CHECKSUM_UNNECESSARY;
  679. stats = netif_rx(skb);
  680. netdev_dbg(wilc_netdev, "netif_rx ret value is: %d\n", stats);
  681. }
  682. out:
  683. srcu_read_unlock(&wilc->srcu, srcu_idx);
  684. }
  685. void wilc_wfi_mgmt_rx(struct wilc *wilc, u8 *buff, u32 size, bool is_auth)
  686. {
  687. int srcu_idx;
  688. struct wilc_vif *vif;
  689. srcu_idx = srcu_read_lock(&wilc->srcu);
  690. wilc_for_each_vif(wilc, vif) {
  691. struct ieee80211_mgmt *mgmt = (struct ieee80211_mgmt *)buff;
  692. u16 type = le16_to_cpup((__le16 *)buff);
  693. u32 type_bit = BIT(type >> 4);
  694. u32 auth_bit = BIT(IEEE80211_STYPE_AUTH >> 4);
  695. if ((vif->mgmt_reg_stypes & auth_bit &&
  696. ieee80211_is_auth(mgmt->frame_control)) &&
  697. vif->iftype == WILC_STATION_MODE && is_auth) {
  698. wilc_wfi_mgmt_frame_rx(vif, buff, size);
  699. break;
  700. }
  701. if (vif->priv.p2p_listen_state &&
  702. vif->mgmt_reg_stypes & type_bit)
  703. wilc_wfi_p2p_rx(vif, buff, size);
  704. if (vif->monitor_flag)
  705. wilc_wfi_monitor_rx(wilc->monitor_dev, buff, size);
  706. }
  707. srcu_read_unlock(&wilc->srcu, srcu_idx);
  708. }
  709. static const struct net_device_ops wilc_netdev_ops = {
  710. .ndo_init = mac_init_fn,
  711. .ndo_open = wilc_mac_open,
  712. .ndo_stop = wilc_mac_close,
  713. .ndo_set_mac_address = wilc_set_mac_addr,
  714. .ndo_start_xmit = wilc_mac_xmit,
  715. .ndo_get_stats = mac_stats,
  716. .ndo_set_rx_mode = wilc_set_multicast_list,
  717. };
  718. void wilc_netdev_cleanup(struct wilc *wilc)
  719. {
  720. struct wilc_vif *vif, *vif_tmp;
  721. if (!wilc)
  722. return;
  723. if (wilc->firmware) {
  724. release_firmware(wilc->firmware);
  725. wilc->firmware = NULL;
  726. }
  727. list_for_each_entry_safe(vif, vif_tmp, &wilc->vif_list, list) {
  728. mutex_lock(&wilc->vif_mutex);
  729. list_del_rcu(&vif->list);
  730. wilc->vif_num--;
  731. mutex_unlock(&wilc->vif_mutex);
  732. synchronize_srcu(&wilc->srcu);
  733. if (vif->ndev)
  734. unregister_netdev(vif->ndev);
  735. }
  736. wilc_wfi_deinit_mon_interface(wilc, false);
  737. destroy_workqueue(wilc->hif_workqueue);
  738. wilc_wlan_cfg_deinit(wilc);
  739. wlan_deinit_locks(wilc);
  740. wiphy_unregister(wilc->wiphy);
  741. wiphy_free(wilc->wiphy);
  742. }
  743. EXPORT_SYMBOL_GPL(wilc_netdev_cleanup);
  744. static u8 wilc_get_available_idx(struct wilc *wl)
  745. {
  746. int idx = 0;
  747. struct wilc_vif *vif;
  748. int srcu_idx;
  749. srcu_idx = srcu_read_lock(&wl->srcu);
  750. wilc_for_each_vif(wl, vif) {
  751. if (vif->idx == 0)
  752. idx = 1;
  753. else
  754. idx = 0;
  755. }
  756. srcu_read_unlock(&wl->srcu, srcu_idx);
  757. return idx;
  758. }
  759. struct wilc_vif *wilc_netdev_ifc_init(struct wilc *wl, const char *name,
  760. int vif_type, enum nl80211_iftype type,
  761. bool rtnl_locked)
  762. {
  763. u8 mac_address[ETH_ALEN];
  764. struct net_device *ndev;
  765. struct wilc_vif *vif;
  766. int ret;
  767. ndev = alloc_etherdev(sizeof(*vif));
  768. if (!ndev)
  769. return ERR_PTR(-ENOMEM);
  770. vif = netdev_priv(ndev);
  771. ndev->ieee80211_ptr = &vif->priv.wdev;
  772. strcpy(ndev->name, name);
  773. vif->wilc = wl;
  774. vif->ndev = ndev;
  775. ndev->ml_priv = vif;
  776. ndev->netdev_ops = &wilc_netdev_ops;
  777. SET_NETDEV_DEV(ndev, wiphy_dev(wl->wiphy));
  778. vif->priv.wdev.wiphy = wl->wiphy;
  779. vif->priv.wdev.netdev = ndev;
  780. vif->priv.wdev.iftype = type;
  781. vif->priv.dev = ndev;
  782. ndev->needs_free_netdev = true;
  783. vif->iftype = vif_type;
  784. vif->idx = wilc_get_available_idx(wl);
  785. vif->mac_opened = 0;
  786. memcpy(mac_address, wl->nv_mac_address, ETH_ALEN);
  787. /* WILC firmware uses locally administered MAC address for the
  788. * second virtual interface (bit 1 of first byte set), but
  789. * since it is possibly not loaded/running yet, reproduce this behavior
  790. * in the driver during interface creation.
  791. */
  792. if (vif->idx)
  793. mac_address[0] |= 0x2;
  794. eth_hw_addr_set(vif->ndev, mac_address);
  795. mutex_lock(&wl->vif_mutex);
  796. list_add_tail_rcu(&vif->list, &wl->vif_list);
  797. wl->vif_num += 1;
  798. mutex_unlock(&wl->vif_mutex);
  799. synchronize_srcu(&wl->srcu);
  800. if (rtnl_locked)
  801. ret = cfg80211_register_netdevice(ndev);
  802. else
  803. ret = register_netdev(ndev);
  804. if (ret) {
  805. ret = -EFAULT;
  806. goto error_remove_vif;
  807. }
  808. return vif;
  809. error_remove_vif:
  810. mutex_lock(&wl->vif_mutex);
  811. list_del_rcu(&vif->list);
  812. wl->vif_num -= 1;
  813. mutex_unlock(&wl->vif_mutex);
  814. synchronize_srcu(&wl->srcu);
  815. free_netdev(ndev);
  816. return ERR_PTR(ret);
  817. }
  818. EXPORT_SYMBOL_GPL(wilc_netdev_ifc_init);
  819. MODULE_DESCRIPTION("Atmel WILC1000 core wireless driver");
  820. MODULE_LICENSE("GPL");
  821. MODULE_FIRMWARE(WILC1000_FW(WILC1000_API_VER));