rx.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  1. /*
  2. * Intel Wireless Multicomm 3200 WiFi driver
  3. *
  4. * Copyright (C) 2009 Intel Corporation. All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in
  14. * the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Intel Corporation nor the names of its
  17. * contributors may be used to endorse or promote products derived
  18. * from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. *
  33. * Intel Corporation <ilw@linux.intel.com>
  34. * Samuel Ortiz <samuel.ortiz@intel.com>
  35. * Zhu Yi <yi.zhu@intel.com>
  36. *
  37. */
  38. #include <linux/kernel.h>
  39. #include <linux/netdevice.h>
  40. #include <linux/sched.h>
  41. #include <linux/etherdevice.h>
  42. #include <linux/wireless.h>
  43. #include <linux/ieee80211.h>
  44. #include <linux/if_arp.h>
  45. #include <linux/list.h>
  46. #include <linux/slab.h>
  47. #include <net/iw_handler.h>
  48. #include "iwm.h"
  49. #include "debug.h"
  50. #include "hal.h"
  51. #include "umac.h"
  52. #include "lmac.h"
  53. #include "commands.h"
  54. #include "rx.h"
  55. #include "cfg80211.h"
  56. #include "eeprom.h"
  57. static int iwm_rx_check_udma_hdr(struct iwm_udma_in_hdr *hdr)
  58. {
  59. if ((le32_to_cpu(hdr->cmd) == UMAC_PAD_TERMINAL) ||
  60. (le32_to_cpu(hdr->size) == UMAC_PAD_TERMINAL))
  61. return -EINVAL;
  62. return 0;
  63. }
  64. static inline int iwm_rx_resp_size(struct iwm_udma_in_hdr *hdr)
  65. {
  66. return ALIGN(le32_to_cpu(hdr->size) + sizeof(struct iwm_udma_in_hdr),
  67. 16);
  68. }
  69. /*
  70. * Notification handlers:
  71. *
  72. * For every possible notification we can receive from the
  73. * target, we have a handler.
  74. * When we get a target notification, and there is no one
  75. * waiting for it, it's just processed through the rx code
  76. * path:
  77. *
  78. * iwm_rx_handle()
  79. * -> iwm_rx_handle_umac()
  80. * -> iwm_rx_handle_wifi()
  81. * -> iwm_rx_handle_resp()
  82. * -> iwm_ntf_*()
  83. *
  84. * OR
  85. *
  86. * -> iwm_rx_handle_non_wifi()
  87. *
  88. * If there are processes waiting for this notification, then
  89. * iwm_rx_handle_wifi() just wakes those processes up and they
  90. * grab the pending notification.
  91. */
  92. static int iwm_ntf_error(struct iwm_priv *iwm, u8 *buf,
  93. unsigned long buf_size, struct iwm_wifi_cmd *cmd)
  94. {
  95. struct iwm_umac_notif_error *error;
  96. struct iwm_fw_error_hdr *fw_err;
  97. error = (struct iwm_umac_notif_error *)buf;
  98. fw_err = &error->err;
  99. memcpy(iwm->last_fw_err, fw_err, sizeof(struct iwm_fw_error_hdr));
  100. IWM_ERR(iwm, "%cMAC FW ERROR:\n",
  101. (le32_to_cpu(fw_err->category) == UMAC_SYS_ERR_CAT_LMAC) ? 'L' : 'U');
  102. IWM_ERR(iwm, "\tCategory: %d\n", le32_to_cpu(fw_err->category));
  103. IWM_ERR(iwm, "\tStatus: 0x%x\n", le32_to_cpu(fw_err->status));
  104. IWM_ERR(iwm, "\tPC: 0x%x\n", le32_to_cpu(fw_err->pc));
  105. IWM_ERR(iwm, "\tblink1: %d\n", le32_to_cpu(fw_err->blink1));
  106. IWM_ERR(iwm, "\tblink2: %d\n", le32_to_cpu(fw_err->blink2));
  107. IWM_ERR(iwm, "\tilink1: %d\n", le32_to_cpu(fw_err->ilink1));
  108. IWM_ERR(iwm, "\tilink2: %d\n", le32_to_cpu(fw_err->ilink2));
  109. IWM_ERR(iwm, "\tData1: 0x%x\n", le32_to_cpu(fw_err->data1));
  110. IWM_ERR(iwm, "\tData2: 0x%x\n", le32_to_cpu(fw_err->data2));
  111. IWM_ERR(iwm, "\tLine number: %d\n", le32_to_cpu(fw_err->line_num));
  112. IWM_ERR(iwm, "\tUMAC status: 0x%x\n", le32_to_cpu(fw_err->umac_status));
  113. IWM_ERR(iwm, "\tLMAC status: 0x%x\n", le32_to_cpu(fw_err->lmac_status));
  114. IWM_ERR(iwm, "\tSDIO status: 0x%x\n", le32_to_cpu(fw_err->sdio_status));
  115. iwm_resetting(iwm);
  116. return 0;
  117. }
  118. static int iwm_ntf_umac_alive(struct iwm_priv *iwm, u8 *buf,
  119. unsigned long buf_size, struct iwm_wifi_cmd *cmd)
  120. {
  121. struct iwm_umac_notif_alive *alive_resp =
  122. (struct iwm_umac_notif_alive *)(buf);
  123. u16 status = le16_to_cpu(alive_resp->status);
  124. if (status == UMAC_NTFY_ALIVE_STATUS_ERR) {
  125. IWM_ERR(iwm, "Receive error UMAC_ALIVE\n");
  126. return -EIO;
  127. }
  128. iwm_tx_credit_init_pools(iwm, alive_resp);
  129. return 0;
  130. }
  131. static int iwm_ntf_init_complete(struct iwm_priv *iwm, u8 *buf,
  132. unsigned long buf_size,
  133. struct iwm_wifi_cmd *cmd)
  134. {
  135. struct wiphy *wiphy = iwm_to_wiphy(iwm);
  136. struct iwm_umac_notif_init_complete *init_complete =
  137. (struct iwm_umac_notif_init_complete *)(buf);
  138. u16 status = le16_to_cpu(init_complete->status);
  139. bool blocked = (status == UMAC_NTFY_INIT_COMPLETE_STATUS_ERR);
  140. if (blocked)
  141. IWM_DBG_NTF(iwm, DBG, "Hardware rf kill is on (radio off)\n");
  142. else
  143. IWM_DBG_NTF(iwm, DBG, "Hardware rf kill is off (radio on)\n");
  144. wiphy_rfkill_set_hw_state(wiphy, blocked);
  145. return 0;
  146. }
  147. static int iwm_ntf_tx_credit_update(struct iwm_priv *iwm, u8 *buf,
  148. unsigned long buf_size,
  149. struct iwm_wifi_cmd *cmd)
  150. {
  151. int pool_nr, total_freed_pages;
  152. unsigned long pool_map;
  153. int i, id;
  154. struct iwm_umac_notif_page_dealloc *dealloc =
  155. (struct iwm_umac_notif_page_dealloc *)buf;
  156. pool_nr = GET_VAL32(dealloc->changes, UMAC_DEALLOC_NTFY_CHANGES_CNT);
  157. pool_map = GET_VAL32(dealloc->changes, UMAC_DEALLOC_NTFY_CHANGES_MSK);
  158. IWM_DBG_TX(iwm, DBG, "UMAC dealloc notification: pool nr %d, "
  159. "update map 0x%lx\n", pool_nr, pool_map);
  160. spin_lock(&iwm->tx_credit.lock);
  161. for (i = 0; i < pool_nr; i++) {
  162. id = GET_VAL32(dealloc->grp_info[i],
  163. UMAC_DEALLOC_NTFY_GROUP_NUM);
  164. if (test_bit(id, &pool_map)) {
  165. total_freed_pages = GET_VAL32(dealloc->grp_info[i],
  166. UMAC_DEALLOC_NTFY_PAGE_CNT);
  167. iwm_tx_credit_inc(iwm, id, total_freed_pages);
  168. }
  169. }
  170. spin_unlock(&iwm->tx_credit.lock);
  171. return 0;
  172. }
  173. static int iwm_ntf_umac_reset(struct iwm_priv *iwm, u8 *buf,
  174. unsigned long buf_size, struct iwm_wifi_cmd *cmd)
  175. {
  176. IWM_DBG_NTF(iwm, DBG, "UMAC RESET done\n");
  177. return 0;
  178. }
  179. static int iwm_ntf_lmac_version(struct iwm_priv *iwm, u8 *buf,
  180. unsigned long buf_size,
  181. struct iwm_wifi_cmd *cmd)
  182. {
  183. IWM_DBG_NTF(iwm, INFO, "LMAC Version: %x.%x\n", buf[9], buf[8]);
  184. return 0;
  185. }
  186. static int iwm_ntf_tx(struct iwm_priv *iwm, u8 *buf,
  187. unsigned long buf_size, struct iwm_wifi_cmd *cmd)
  188. {
  189. struct iwm_lmac_tx_resp *tx_resp;
  190. struct iwm_umac_wifi_in_hdr *hdr;
  191. tx_resp = (struct iwm_lmac_tx_resp *)
  192. (buf + sizeof(struct iwm_umac_wifi_in_hdr));
  193. hdr = (struct iwm_umac_wifi_in_hdr *)buf;
  194. IWM_DBG_TX(iwm, DBG, "REPLY_TX, buf size: %lu\n", buf_size);
  195. IWM_DBG_TX(iwm, DBG, "Seqnum: %d\n",
  196. le16_to_cpu(hdr->sw_hdr.cmd.seq_num));
  197. IWM_DBG_TX(iwm, DBG, "\tFrame cnt: %d\n", tx_resp->frame_cnt);
  198. IWM_DBG_TX(iwm, DBG, "\tRetry cnt: %d\n",
  199. le16_to_cpu(tx_resp->retry_cnt));
  200. IWM_DBG_TX(iwm, DBG, "\tSeq ctl: %d\n", le16_to_cpu(tx_resp->seq_ctl));
  201. IWM_DBG_TX(iwm, DBG, "\tByte cnt: %d\n",
  202. le16_to_cpu(tx_resp->byte_cnt));
  203. IWM_DBG_TX(iwm, DBG, "\tStatus: 0x%x\n", le32_to_cpu(tx_resp->status));
  204. return 0;
  205. }
  206. static int iwm_ntf_calib_res(struct iwm_priv *iwm, u8 *buf,
  207. unsigned long buf_size, struct iwm_wifi_cmd *cmd)
  208. {
  209. u8 opcode;
  210. u8 *calib_buf;
  211. struct iwm_lmac_calib_hdr *hdr = (struct iwm_lmac_calib_hdr *)
  212. (buf + sizeof(struct iwm_umac_wifi_in_hdr));
  213. opcode = hdr->opcode;
  214. BUG_ON(opcode >= CALIBRATION_CMD_NUM ||
  215. opcode < PHY_CALIBRATE_OPCODES_NUM);
  216. IWM_DBG_NTF(iwm, DBG, "Store calibration result for opcode: %d\n",
  217. opcode);
  218. buf_size -= sizeof(struct iwm_umac_wifi_in_hdr);
  219. calib_buf = iwm->calib_res[opcode].buf;
  220. if (!calib_buf || (iwm->calib_res[opcode].size < buf_size)) {
  221. kfree(calib_buf);
  222. calib_buf = kzalloc(buf_size, GFP_KERNEL);
  223. if (!calib_buf) {
  224. IWM_ERR(iwm, "Memory allocation failed: calib_res\n");
  225. return -ENOMEM;
  226. }
  227. iwm->calib_res[opcode].buf = calib_buf;
  228. iwm->calib_res[opcode].size = buf_size;
  229. }
  230. memcpy(calib_buf, hdr, buf_size);
  231. set_bit(opcode - PHY_CALIBRATE_OPCODES_NUM, &iwm->calib_done_map);
  232. return 0;
  233. }
  234. static int iwm_ntf_calib_complete(struct iwm_priv *iwm, u8 *buf,
  235. unsigned long buf_size,
  236. struct iwm_wifi_cmd *cmd)
  237. {
  238. IWM_DBG_NTF(iwm, DBG, "Calibration completed\n");
  239. return 0;
  240. }
  241. static int iwm_ntf_calib_cfg(struct iwm_priv *iwm, u8 *buf,
  242. unsigned long buf_size, struct iwm_wifi_cmd *cmd)
  243. {
  244. struct iwm_lmac_cal_cfg_resp *cal_resp;
  245. cal_resp = (struct iwm_lmac_cal_cfg_resp *)
  246. (buf + sizeof(struct iwm_umac_wifi_in_hdr));
  247. IWM_DBG_NTF(iwm, DBG, "Calibration CFG command status: %d\n",
  248. le32_to_cpu(cal_resp->status));
  249. return 0;
  250. }
  251. static int iwm_ntf_wifi_status(struct iwm_priv *iwm, u8 *buf,
  252. unsigned long buf_size, struct iwm_wifi_cmd *cmd)
  253. {
  254. struct iwm_umac_notif_wifi_status *status =
  255. (struct iwm_umac_notif_wifi_status *)buf;
  256. iwm->core_enabled |= le16_to_cpu(status->status);
  257. return 0;
  258. }
  259. static struct iwm_rx_ticket_node *
  260. iwm_rx_ticket_node_alloc(struct iwm_priv *iwm, struct iwm_rx_ticket *ticket)
  261. {
  262. struct iwm_rx_ticket_node *ticket_node;
  263. ticket_node = kzalloc(sizeof(struct iwm_rx_ticket_node), GFP_KERNEL);
  264. if (!ticket_node) {
  265. IWM_ERR(iwm, "Couldn't allocate ticket node\n");
  266. return ERR_PTR(-ENOMEM);
  267. }
  268. ticket_node->ticket = kmemdup(ticket, sizeof(struct iwm_rx_ticket),
  269. GFP_KERNEL);
  270. if (!ticket_node->ticket) {
  271. IWM_ERR(iwm, "Couldn't allocate RX ticket\n");
  272. kfree(ticket_node);
  273. return ERR_PTR(-ENOMEM);
  274. }
  275. INIT_LIST_HEAD(&ticket_node->node);
  276. return ticket_node;
  277. }
  278. static void iwm_rx_ticket_node_free(struct iwm_rx_ticket_node *ticket_node)
  279. {
  280. kfree(ticket_node->ticket);
  281. kfree(ticket_node);
  282. }
  283. static struct iwm_rx_packet *iwm_rx_packet_get(struct iwm_priv *iwm, u16 id)
  284. {
  285. u8 id_hash = IWM_RX_ID_GET_HASH(id);
  286. struct iwm_rx_packet *packet;
  287. spin_lock(&iwm->packet_lock[id_hash]);
  288. list_for_each_entry(packet, &iwm->rx_packets[id_hash], node)
  289. if (packet->id == id) {
  290. list_del(&packet->node);
  291. spin_unlock(&iwm->packet_lock[id_hash]);
  292. return packet;
  293. }
  294. spin_unlock(&iwm->packet_lock[id_hash]);
  295. return NULL;
  296. }
  297. static struct iwm_rx_packet *iwm_rx_packet_alloc(struct iwm_priv *iwm, u8 *buf,
  298. u32 size, u16 id)
  299. {
  300. struct iwm_rx_packet *packet;
  301. packet = kzalloc(sizeof(struct iwm_rx_packet), GFP_KERNEL);
  302. if (!packet) {
  303. IWM_ERR(iwm, "Couldn't allocate packet\n");
  304. return ERR_PTR(-ENOMEM);
  305. }
  306. packet->skb = dev_alloc_skb(size);
  307. if (!packet->skb) {
  308. IWM_ERR(iwm, "Couldn't allocate packet SKB\n");
  309. kfree(packet);
  310. return ERR_PTR(-ENOMEM);
  311. }
  312. packet->pkt_size = size;
  313. skb_put(packet->skb, size);
  314. memcpy(packet->skb->data, buf, size);
  315. INIT_LIST_HEAD(&packet->node);
  316. packet->id = id;
  317. return packet;
  318. }
  319. void iwm_rx_free(struct iwm_priv *iwm)
  320. {
  321. struct iwm_rx_ticket_node *ticket, *nt;
  322. struct iwm_rx_packet *packet, *np;
  323. int i;
  324. spin_lock(&iwm->ticket_lock);
  325. list_for_each_entry_safe(ticket, nt, &iwm->rx_tickets, node) {
  326. list_del(&ticket->node);
  327. iwm_rx_ticket_node_free(ticket);
  328. }
  329. spin_unlock(&iwm->ticket_lock);
  330. for (i = 0; i < IWM_RX_ID_HASH; i++) {
  331. spin_lock(&iwm->packet_lock[i]);
  332. list_for_each_entry_safe(packet, np, &iwm->rx_packets[i],
  333. node) {
  334. list_del(&packet->node);
  335. kfree_skb(packet->skb);
  336. kfree(packet);
  337. }
  338. spin_unlock(&iwm->packet_lock[i]);
  339. }
  340. }
  341. static int iwm_ntf_rx_ticket(struct iwm_priv *iwm, u8 *buf,
  342. unsigned long buf_size, struct iwm_wifi_cmd *cmd)
  343. {
  344. struct iwm_umac_notif_rx_ticket *ntf_rx_ticket =
  345. (struct iwm_umac_notif_rx_ticket *)buf;
  346. struct iwm_rx_ticket *ticket =
  347. (struct iwm_rx_ticket *)ntf_rx_ticket->tickets;
  348. int i, schedule_rx = 0;
  349. for (i = 0; i < ntf_rx_ticket->num_tickets; i++) {
  350. struct iwm_rx_ticket_node *ticket_node;
  351. switch (le16_to_cpu(ticket->action)) {
  352. case IWM_RX_TICKET_RELEASE:
  353. case IWM_RX_TICKET_DROP:
  354. /* We can push the packet to the stack */
  355. ticket_node = iwm_rx_ticket_node_alloc(iwm, ticket);
  356. if (IS_ERR(ticket_node))
  357. return PTR_ERR(ticket_node);
  358. IWM_DBG_RX(iwm, DBG, "TICKET %s(%d)\n",
  359. __le16_to_cpu(ticket->action) ==
  360. IWM_RX_TICKET_RELEASE ?
  361. "RELEASE" : "DROP",
  362. ticket->id);
  363. spin_lock(&iwm->ticket_lock);
  364. list_add_tail(&ticket_node->node, &iwm->rx_tickets);
  365. spin_unlock(&iwm->ticket_lock);
  366. /*
  367. * We received an Rx ticket, most likely there's
  368. * a packet pending for it, it's not worth going
  369. * through the packet hash list to double check.
  370. * Let's just fire the rx worker..
  371. */
  372. schedule_rx = 1;
  373. break;
  374. default:
  375. IWM_ERR(iwm, "Invalid RX ticket action: 0x%x\n",
  376. ticket->action);
  377. }
  378. ticket++;
  379. }
  380. if (schedule_rx)
  381. queue_work(iwm->rx_wq, &iwm->rx_worker);
  382. return 0;
  383. }
  384. static int iwm_ntf_rx_packet(struct iwm_priv *iwm, u8 *buf,
  385. unsigned long buf_size, struct iwm_wifi_cmd *cmd)
  386. {
  387. struct iwm_umac_wifi_in_hdr *wifi_hdr;
  388. struct iwm_rx_packet *packet;
  389. u16 id, buf_offset;
  390. u32 packet_size;
  391. u8 id_hash;
  392. IWM_DBG_RX(iwm, DBG, "\n");
  393. wifi_hdr = (struct iwm_umac_wifi_in_hdr *)buf;
  394. id = le16_to_cpu(wifi_hdr->sw_hdr.cmd.seq_num);
  395. buf_offset = sizeof(struct iwm_umac_wifi_in_hdr);
  396. packet_size = buf_size - sizeof(struct iwm_umac_wifi_in_hdr);
  397. IWM_DBG_RX(iwm, DBG, "CMD:0x%x, seqnum: %d, packet size: %d\n",
  398. wifi_hdr->sw_hdr.cmd.cmd, id, packet_size);
  399. IWM_DBG_RX(iwm, DBG, "Packet id: %d\n", id);
  400. IWM_HEXDUMP(iwm, DBG, RX, "PACKET: ", buf + buf_offset, packet_size);
  401. packet = iwm_rx_packet_alloc(iwm, buf + buf_offset, packet_size, id);
  402. if (IS_ERR(packet))
  403. return PTR_ERR(packet);
  404. id_hash = IWM_RX_ID_GET_HASH(id);
  405. spin_lock(&iwm->packet_lock[id_hash]);
  406. list_add_tail(&packet->node, &iwm->rx_packets[id_hash]);
  407. spin_unlock(&iwm->packet_lock[id_hash]);
  408. /* We might (unlikely) have received the packet _after_ the ticket */
  409. queue_work(iwm->rx_wq, &iwm->rx_worker);
  410. return 0;
  411. }
  412. /* MLME handlers */
  413. static int iwm_mlme_assoc_start(struct iwm_priv *iwm, u8 *buf,
  414. unsigned long buf_size,
  415. struct iwm_wifi_cmd *cmd)
  416. {
  417. struct iwm_umac_notif_assoc_start *start;
  418. start = (struct iwm_umac_notif_assoc_start *)buf;
  419. IWM_DBG_MLME(iwm, INFO, "Association with %pM Started, reason: %d\n",
  420. start->bssid, le32_to_cpu(start->roam_reason));
  421. wake_up_interruptible(&iwm->mlme_queue);
  422. return 0;
  423. }
  424. static u8 iwm_is_open_wep_profile(struct iwm_priv *iwm)
  425. {
  426. if ((iwm->umac_profile->sec.ucast_cipher == UMAC_CIPHER_TYPE_WEP_40 ||
  427. iwm->umac_profile->sec.ucast_cipher == UMAC_CIPHER_TYPE_WEP_104) &&
  428. (iwm->umac_profile->sec.ucast_cipher ==
  429. iwm->umac_profile->sec.mcast_cipher) &&
  430. (iwm->umac_profile->sec.auth_type == UMAC_AUTH_TYPE_OPEN))
  431. return 1;
  432. return 0;
  433. }
  434. static int iwm_mlme_assoc_complete(struct iwm_priv *iwm, u8 *buf,
  435. unsigned long buf_size,
  436. struct iwm_wifi_cmd *cmd)
  437. {
  438. struct wiphy *wiphy = iwm_to_wiphy(iwm);
  439. struct ieee80211_channel *chan;
  440. struct iwm_umac_notif_assoc_complete *complete =
  441. (struct iwm_umac_notif_assoc_complete *)buf;
  442. IWM_DBG_MLME(iwm, INFO, "Association with %pM completed, status: %d\n",
  443. complete->bssid, complete->status);
  444. switch (le32_to_cpu(complete->status)) {
  445. case UMAC_ASSOC_COMPLETE_SUCCESS:
  446. chan = ieee80211_get_channel(wiphy,
  447. ieee80211_channel_to_frequency(complete->channel,
  448. complete->band == UMAC_BAND_2GHZ ?
  449. IEEE80211_BAND_2GHZ :
  450. IEEE80211_BAND_5GHZ));
  451. if (!chan || chan->flags & IEEE80211_CHAN_DISABLED) {
  452. /* Associated to a unallowed channel, disassociate. */
  453. __iwm_invalidate_mlme_profile(iwm);
  454. IWM_WARN(iwm, "Couldn't associate with %pM due to "
  455. "channel %d is disabled. Check your local "
  456. "regulatory setting.\n",
  457. complete->bssid, complete->channel);
  458. goto failure;
  459. }
  460. set_bit(IWM_STATUS_ASSOCIATED, &iwm->status);
  461. memcpy(iwm->bssid, complete->bssid, ETH_ALEN);
  462. iwm->channel = complete->channel;
  463. /* Internal roaming state, avoid notifying SME. */
  464. if (!test_and_clear_bit(IWM_STATUS_SME_CONNECTING, &iwm->status)
  465. && iwm->conf.mode == UMAC_MODE_BSS) {
  466. cancel_delayed_work(&iwm->disconnect);
  467. cfg80211_roamed(iwm_to_ndev(iwm), NULL,
  468. complete->bssid,
  469. iwm->req_ie, iwm->req_ie_len,
  470. iwm->resp_ie, iwm->resp_ie_len,
  471. GFP_KERNEL);
  472. break;
  473. }
  474. iwm_link_on(iwm);
  475. if (iwm->conf.mode == UMAC_MODE_IBSS)
  476. goto ibss;
  477. if (!test_bit(IWM_STATUS_RESETTING, &iwm->status))
  478. cfg80211_connect_result(iwm_to_ndev(iwm),
  479. complete->bssid,
  480. iwm->req_ie, iwm->req_ie_len,
  481. iwm->resp_ie, iwm->resp_ie_len,
  482. WLAN_STATUS_SUCCESS,
  483. GFP_KERNEL);
  484. else
  485. cfg80211_roamed(iwm_to_ndev(iwm), NULL,
  486. complete->bssid,
  487. iwm->req_ie, iwm->req_ie_len,
  488. iwm->resp_ie, iwm->resp_ie_len,
  489. GFP_KERNEL);
  490. break;
  491. case UMAC_ASSOC_COMPLETE_FAILURE:
  492. failure:
  493. clear_bit(IWM_STATUS_ASSOCIATED, &iwm->status);
  494. memset(iwm->bssid, 0, ETH_ALEN);
  495. iwm->channel = 0;
  496. /* Internal roaming state, avoid notifying SME. */
  497. if (!test_and_clear_bit(IWM_STATUS_SME_CONNECTING, &iwm->status)
  498. && iwm->conf.mode == UMAC_MODE_BSS) {
  499. cancel_delayed_work(&iwm->disconnect);
  500. break;
  501. }
  502. iwm_link_off(iwm);
  503. if (iwm->conf.mode == UMAC_MODE_IBSS)
  504. goto ibss;
  505. if (!test_bit(IWM_STATUS_RESETTING, &iwm->status))
  506. if (!iwm_is_open_wep_profile(iwm)) {
  507. cfg80211_connect_result(iwm_to_ndev(iwm),
  508. complete->bssid,
  509. NULL, 0, NULL, 0,
  510. WLAN_STATUS_UNSPECIFIED_FAILURE,
  511. GFP_KERNEL);
  512. } else {
  513. /* Let's try shared WEP auth */
  514. IWM_ERR(iwm, "Trying WEP shared auth\n");
  515. schedule_work(&iwm->auth_retry_worker);
  516. }
  517. else
  518. cfg80211_disconnected(iwm_to_ndev(iwm), 0, NULL, 0,
  519. GFP_KERNEL);
  520. break;
  521. default:
  522. break;
  523. }
  524. clear_bit(IWM_STATUS_RESETTING, &iwm->status);
  525. return 0;
  526. ibss:
  527. cfg80211_ibss_joined(iwm_to_ndev(iwm), iwm->bssid, GFP_KERNEL);
  528. clear_bit(IWM_STATUS_RESETTING, &iwm->status);
  529. return 0;
  530. }
  531. static int iwm_mlme_profile_invalidate(struct iwm_priv *iwm, u8 *buf,
  532. unsigned long buf_size,
  533. struct iwm_wifi_cmd *cmd)
  534. {
  535. struct iwm_umac_notif_profile_invalidate *invalid;
  536. u32 reason;
  537. invalid = (struct iwm_umac_notif_profile_invalidate *)buf;
  538. reason = le32_to_cpu(invalid->reason);
  539. IWM_DBG_MLME(iwm, INFO, "Profile Invalidated. Reason: %d\n", reason);
  540. if (reason != UMAC_PROFILE_INVALID_REQUEST &&
  541. test_bit(IWM_STATUS_SME_CONNECTING, &iwm->status))
  542. cfg80211_connect_result(iwm_to_ndev(iwm), NULL, NULL, 0, NULL,
  543. 0, WLAN_STATUS_UNSPECIFIED_FAILURE,
  544. GFP_KERNEL);
  545. clear_bit(IWM_STATUS_SME_CONNECTING, &iwm->status);
  546. clear_bit(IWM_STATUS_ASSOCIATED, &iwm->status);
  547. iwm->umac_profile_active = false;
  548. memset(iwm->bssid, 0, ETH_ALEN);
  549. iwm->channel = 0;
  550. iwm_link_off(iwm);
  551. wake_up_interruptible(&iwm->mlme_queue);
  552. return 0;
  553. }
  554. #define IWM_DISCONNECT_INTERVAL (5 * HZ)
  555. static int iwm_mlme_connection_terminated(struct iwm_priv *iwm, u8 *buf,
  556. unsigned long buf_size,
  557. struct iwm_wifi_cmd *cmd)
  558. {
  559. IWM_DBG_MLME(iwm, DBG, "Connection terminated\n");
  560. schedule_delayed_work(&iwm->disconnect, IWM_DISCONNECT_INTERVAL);
  561. return 0;
  562. }
  563. static int iwm_mlme_scan_complete(struct iwm_priv *iwm, u8 *buf,
  564. unsigned long buf_size,
  565. struct iwm_wifi_cmd *cmd)
  566. {
  567. int ret;
  568. struct iwm_umac_notif_scan_complete *scan_complete =
  569. (struct iwm_umac_notif_scan_complete *)buf;
  570. u32 result = le32_to_cpu(scan_complete->result);
  571. IWM_DBG_MLME(iwm, INFO, "type:0x%x result:0x%x seq:%d\n",
  572. le32_to_cpu(scan_complete->type),
  573. le32_to_cpu(scan_complete->result),
  574. scan_complete->seq_num);
  575. if (!test_and_clear_bit(IWM_STATUS_SCANNING, &iwm->status)) {
  576. IWM_ERR(iwm, "Scan complete while device not scanning\n");
  577. return -EIO;
  578. }
  579. if (!iwm->scan_request)
  580. return 0;
  581. ret = iwm_cfg80211_inform_bss(iwm);
  582. cfg80211_scan_done(iwm->scan_request,
  583. (result & UMAC_SCAN_RESULT_ABORTED) ? 1 : !!ret);
  584. iwm->scan_request = NULL;
  585. return ret;
  586. }
  587. static int iwm_mlme_update_sta_table(struct iwm_priv *iwm, u8 *buf,
  588. unsigned long buf_size,
  589. struct iwm_wifi_cmd *cmd)
  590. {
  591. struct iwm_umac_notif_sta_info *umac_sta =
  592. (struct iwm_umac_notif_sta_info *)buf;
  593. struct iwm_sta_info *sta;
  594. int i;
  595. switch (le32_to_cpu(umac_sta->opcode)) {
  596. case UMAC_OPCODE_ADD_MODIFY:
  597. sta = &iwm->sta_table[GET_VAL8(umac_sta->sta_id, LMAC_STA_ID)];
  598. IWM_DBG_MLME(iwm, INFO, "%s STA: ID = %d, Color = %d, "
  599. "addr = %pM, qos = %d\n",
  600. sta->valid ? "Modify" : "Add",
  601. GET_VAL8(umac_sta->sta_id, LMAC_STA_ID),
  602. GET_VAL8(umac_sta->sta_id, LMAC_STA_COLOR),
  603. umac_sta->mac_addr,
  604. umac_sta->flags & UMAC_STA_FLAG_QOS);
  605. sta->valid = true;
  606. sta->qos = umac_sta->flags & UMAC_STA_FLAG_QOS;
  607. sta->color = GET_VAL8(umac_sta->sta_id, LMAC_STA_COLOR);
  608. memcpy(sta->addr, umac_sta->mac_addr, ETH_ALEN);
  609. break;
  610. case UMAC_OPCODE_REMOVE:
  611. IWM_DBG_MLME(iwm, INFO, "Remove STA: ID = %d, Color = %d, "
  612. "addr = %pM\n",
  613. GET_VAL8(umac_sta->sta_id, LMAC_STA_ID),
  614. GET_VAL8(umac_sta->sta_id, LMAC_STA_COLOR),
  615. umac_sta->mac_addr);
  616. sta = &iwm->sta_table[GET_VAL8(umac_sta->sta_id, LMAC_STA_ID)];
  617. if (!memcmp(sta->addr, umac_sta->mac_addr, ETH_ALEN))
  618. sta->valid = false;
  619. break;
  620. case UMAC_OPCODE_CLEAR_ALL:
  621. for (i = 0; i < IWM_STA_TABLE_NUM; i++)
  622. iwm->sta_table[i].valid = false;
  623. break;
  624. default:
  625. break;
  626. }
  627. return 0;
  628. }
  629. static int iwm_mlme_medium_lost(struct iwm_priv *iwm, u8 *buf,
  630. unsigned long buf_size,
  631. struct iwm_wifi_cmd *cmd)
  632. {
  633. struct wiphy *wiphy = iwm_to_wiphy(iwm);
  634. IWM_DBG_NTF(iwm, DBG, "WiFi/WiMax coexistence radio is OFF\n");
  635. wiphy_rfkill_set_hw_state(wiphy, true);
  636. return 0;
  637. }
  638. static int iwm_mlme_update_bss_table(struct iwm_priv *iwm, u8 *buf,
  639. unsigned long buf_size,
  640. struct iwm_wifi_cmd *cmd)
  641. {
  642. struct wiphy *wiphy = iwm_to_wiphy(iwm);
  643. struct ieee80211_mgmt *mgmt;
  644. struct iwm_umac_notif_bss_info *umac_bss =
  645. (struct iwm_umac_notif_bss_info *)buf;
  646. struct ieee80211_channel *channel;
  647. struct ieee80211_supported_band *band;
  648. struct iwm_bss_info *bss;
  649. s32 signal;
  650. int freq;
  651. u16 frame_len = le16_to_cpu(umac_bss->frame_len);
  652. size_t bss_len = sizeof(struct iwm_umac_notif_bss_info) + frame_len;
  653. mgmt = (struct ieee80211_mgmt *)(umac_bss->frame_buf);
  654. IWM_DBG_MLME(iwm, DBG, "New BSS info entry: %pM\n", mgmt->bssid);
  655. IWM_DBG_MLME(iwm, DBG, "\tType: 0x%x\n", le32_to_cpu(umac_bss->type));
  656. IWM_DBG_MLME(iwm, DBG, "\tTimestamp: %d\n",
  657. le32_to_cpu(umac_bss->timestamp));
  658. IWM_DBG_MLME(iwm, DBG, "\tTable Index: %d\n",
  659. le16_to_cpu(umac_bss->table_idx));
  660. IWM_DBG_MLME(iwm, DBG, "\tBand: %d\n", umac_bss->band);
  661. IWM_DBG_MLME(iwm, DBG, "\tChannel: %d\n", umac_bss->channel);
  662. IWM_DBG_MLME(iwm, DBG, "\tRSSI: %d\n", umac_bss->rssi);
  663. IWM_DBG_MLME(iwm, DBG, "\tFrame Length: %d\n", frame_len);
  664. list_for_each_entry(bss, &iwm->bss_list, node)
  665. if (bss->bss->table_idx == umac_bss->table_idx)
  666. break;
  667. if (&bss->node != &iwm->bss_list) {
  668. /* Remove the old BSS entry, we will add it back later. */
  669. list_del(&bss->node);
  670. kfree(bss->bss);
  671. } else {
  672. /* New BSS entry */
  673. bss = kzalloc(sizeof(struct iwm_bss_info), GFP_KERNEL);
  674. if (!bss) {
  675. IWM_ERR(iwm, "Couldn't allocate bss_info\n");
  676. return -ENOMEM;
  677. }
  678. }
  679. bss->bss = kzalloc(bss_len, GFP_KERNEL);
  680. if (!bss->bss) {
  681. kfree(bss);
  682. IWM_ERR(iwm, "Couldn't allocate bss\n");
  683. return -ENOMEM;
  684. }
  685. INIT_LIST_HEAD(&bss->node);
  686. memcpy(bss->bss, umac_bss, bss_len);
  687. if (umac_bss->band == UMAC_BAND_2GHZ)
  688. band = wiphy->bands[IEEE80211_BAND_2GHZ];
  689. else if (umac_bss->band == UMAC_BAND_5GHZ)
  690. band = wiphy->bands[IEEE80211_BAND_5GHZ];
  691. else {
  692. IWM_ERR(iwm, "Invalid band: %d\n", umac_bss->band);
  693. goto err;
  694. }
  695. freq = ieee80211_channel_to_frequency(umac_bss->channel, band->band);
  696. channel = ieee80211_get_channel(wiphy, freq);
  697. signal = umac_bss->rssi * 100;
  698. bss->cfg_bss = cfg80211_inform_bss_frame(wiphy, channel,
  699. mgmt, frame_len,
  700. signal, GFP_KERNEL);
  701. if (!bss->cfg_bss)
  702. goto err;
  703. list_add_tail(&bss->node, &iwm->bss_list);
  704. return 0;
  705. err:
  706. kfree(bss->bss);
  707. kfree(bss);
  708. return -EINVAL;
  709. }
  710. static int iwm_mlme_remove_bss(struct iwm_priv *iwm, u8 *buf,
  711. unsigned long buf_size, struct iwm_wifi_cmd *cmd)
  712. {
  713. struct iwm_umac_notif_bss_removed *bss_rm =
  714. (struct iwm_umac_notif_bss_removed *)buf;
  715. struct iwm_bss_info *bss, *next;
  716. u16 table_idx;
  717. int i;
  718. for (i = 0; i < le32_to_cpu(bss_rm->count); i++) {
  719. table_idx = le16_to_cpu(bss_rm->entries[i]) &
  720. IWM_BSS_REMOVE_INDEX_MSK;
  721. list_for_each_entry_safe(bss, next, &iwm->bss_list, node)
  722. if (bss->bss->table_idx == cpu_to_le16(table_idx)) {
  723. struct ieee80211_mgmt *mgmt;
  724. mgmt = (struct ieee80211_mgmt *)
  725. (bss->bss->frame_buf);
  726. IWM_DBG_MLME(iwm, ERR, "BSS removed: %pM\n",
  727. mgmt->bssid);
  728. list_del(&bss->node);
  729. kfree(bss->bss);
  730. kfree(bss);
  731. }
  732. }
  733. return 0;
  734. }
  735. static int iwm_mlme_mgt_frame(struct iwm_priv *iwm, u8 *buf,
  736. unsigned long buf_size, struct iwm_wifi_cmd *cmd)
  737. {
  738. struct iwm_umac_notif_mgt_frame *mgt_frame =
  739. (struct iwm_umac_notif_mgt_frame *)buf;
  740. struct ieee80211_mgmt *mgt = (struct ieee80211_mgmt *)mgt_frame->frame;
  741. IWM_HEXDUMP(iwm, DBG, MLME, "MGT: ", mgt_frame->frame,
  742. le16_to_cpu(mgt_frame->len));
  743. if (ieee80211_is_assoc_req(mgt->frame_control)) {
  744. iwm->req_ie_len = le16_to_cpu(mgt_frame->len)
  745. - offsetof(struct ieee80211_mgmt,
  746. u.assoc_req.variable);
  747. kfree(iwm->req_ie);
  748. iwm->req_ie = kmemdup(mgt->u.assoc_req.variable,
  749. iwm->req_ie_len, GFP_KERNEL);
  750. } else if (ieee80211_is_reassoc_req(mgt->frame_control)) {
  751. iwm->req_ie_len = le16_to_cpu(mgt_frame->len)
  752. - offsetof(struct ieee80211_mgmt,
  753. u.reassoc_req.variable);
  754. kfree(iwm->req_ie);
  755. iwm->req_ie = kmemdup(mgt->u.reassoc_req.variable,
  756. iwm->req_ie_len, GFP_KERNEL);
  757. } else if (ieee80211_is_assoc_resp(mgt->frame_control)) {
  758. iwm->resp_ie_len = le16_to_cpu(mgt_frame->len)
  759. - offsetof(struct ieee80211_mgmt,
  760. u.assoc_resp.variable);
  761. kfree(iwm->resp_ie);
  762. iwm->resp_ie = kmemdup(mgt->u.assoc_resp.variable,
  763. iwm->resp_ie_len, GFP_KERNEL);
  764. } else if (ieee80211_is_reassoc_resp(mgt->frame_control)) {
  765. iwm->resp_ie_len = le16_to_cpu(mgt_frame->len)
  766. - offsetof(struct ieee80211_mgmt,
  767. u.reassoc_resp.variable);
  768. kfree(iwm->resp_ie);
  769. iwm->resp_ie = kmemdup(mgt->u.reassoc_resp.variable,
  770. iwm->resp_ie_len, GFP_KERNEL);
  771. } else {
  772. IWM_ERR(iwm, "Unsupported management frame: 0x%x",
  773. le16_to_cpu(mgt->frame_control));
  774. return 0;
  775. }
  776. return 0;
  777. }
  778. static int iwm_ntf_mlme(struct iwm_priv *iwm, u8 *buf,
  779. unsigned long buf_size, struct iwm_wifi_cmd *cmd)
  780. {
  781. struct iwm_umac_notif_wifi_if *notif =
  782. (struct iwm_umac_notif_wifi_if *)buf;
  783. switch (notif->status) {
  784. case WIFI_IF_NTFY_ASSOC_START:
  785. return iwm_mlme_assoc_start(iwm, buf, buf_size, cmd);
  786. case WIFI_IF_NTFY_ASSOC_COMPLETE:
  787. return iwm_mlme_assoc_complete(iwm, buf, buf_size, cmd);
  788. case WIFI_IF_NTFY_PROFILE_INVALIDATE_COMPLETE:
  789. return iwm_mlme_profile_invalidate(iwm, buf, buf_size, cmd);
  790. case WIFI_IF_NTFY_CONNECTION_TERMINATED:
  791. return iwm_mlme_connection_terminated(iwm, buf, buf_size, cmd);
  792. case WIFI_IF_NTFY_SCAN_COMPLETE:
  793. return iwm_mlme_scan_complete(iwm, buf, buf_size, cmd);
  794. case WIFI_IF_NTFY_STA_TABLE_CHANGE:
  795. return iwm_mlme_update_sta_table(iwm, buf, buf_size, cmd);
  796. case WIFI_IF_NTFY_EXTENDED_IE_REQUIRED:
  797. IWM_DBG_MLME(iwm, DBG, "Extended IE required\n");
  798. break;
  799. case WIFI_IF_NTFY_RADIO_PREEMPTION:
  800. return iwm_mlme_medium_lost(iwm, buf, buf_size, cmd);
  801. case WIFI_IF_NTFY_BSS_TRK_TABLE_CHANGED:
  802. return iwm_mlme_update_bss_table(iwm, buf, buf_size, cmd);
  803. case WIFI_IF_NTFY_BSS_TRK_ENTRIES_REMOVED:
  804. return iwm_mlme_remove_bss(iwm, buf, buf_size, cmd);
  805. break;
  806. case WIFI_IF_NTFY_MGMT_FRAME:
  807. return iwm_mlme_mgt_frame(iwm, buf, buf_size, cmd);
  808. case WIFI_DBG_IF_NTFY_SCAN_SUPER_JOB_START:
  809. case WIFI_DBG_IF_NTFY_SCAN_SUPER_JOB_COMPLETE:
  810. case WIFI_DBG_IF_NTFY_SCAN_CHANNEL_START:
  811. case WIFI_DBG_IF_NTFY_SCAN_CHANNEL_RESULT:
  812. case WIFI_DBG_IF_NTFY_SCAN_MINI_JOB_START:
  813. case WIFI_DBG_IF_NTFY_SCAN_MINI_JOB_COMPLETE:
  814. case WIFI_DBG_IF_NTFY_CNCT_ATC_START:
  815. case WIFI_DBG_IF_NTFY_COEX_NOTIFICATION:
  816. case WIFI_DBG_IF_NTFY_COEX_HANDLE_ENVELOP:
  817. case WIFI_DBG_IF_NTFY_COEX_HANDLE_RELEASE_ENVELOP:
  818. IWM_DBG_MLME(iwm, DBG, "MLME debug notification: 0x%x\n",
  819. notif->status);
  820. break;
  821. default:
  822. IWM_ERR(iwm, "Unhandled notification: 0x%x\n", notif->status);
  823. break;
  824. }
  825. return 0;
  826. }
  827. #define IWM_STATS_UPDATE_INTERVAL (2 * HZ)
  828. static int iwm_ntf_statistics(struct iwm_priv *iwm, u8 *buf,
  829. unsigned long buf_size, struct iwm_wifi_cmd *cmd)
  830. {
  831. struct iwm_umac_notif_stats *stats = (struct iwm_umac_notif_stats *)buf;
  832. struct iw_statistics *wstats = &iwm->wstats;
  833. u16 max_rate = 0;
  834. int i;
  835. IWM_DBG_MLME(iwm, DBG, "Statistics notification received\n");
  836. if (test_bit(IWM_STATUS_ASSOCIATED, &iwm->status)) {
  837. for (i = 0; i < UMAC_NTF_RATE_SAMPLE_NR; i++) {
  838. max_rate = max_t(u16, max_rate,
  839. max(le16_to_cpu(stats->tx_rate[i]),
  840. le16_to_cpu(stats->rx_rate[i])));
  841. }
  842. /* UMAC passes rate info multiplies by 2 */
  843. iwm->rate = max_rate >> 1;
  844. }
  845. iwm->txpower = le32_to_cpu(stats->tx_power);
  846. wstats->status = 0;
  847. wstats->discard.nwid = le32_to_cpu(stats->rx_drop_other_bssid);
  848. wstats->discard.code = le32_to_cpu(stats->rx_drop_decode);
  849. wstats->discard.fragment = le32_to_cpu(stats->rx_drop_reassembly);
  850. wstats->discard.retries = le32_to_cpu(stats->tx_drop_max_retry);
  851. wstats->miss.beacon = le32_to_cpu(stats->missed_beacons);
  852. /* according to cfg80211 */
  853. if (stats->rssi_dbm < -110)
  854. wstats->qual.qual = 0;
  855. else if (stats->rssi_dbm > -40)
  856. wstats->qual.qual = 70;
  857. else
  858. wstats->qual.qual = stats->rssi_dbm + 110;
  859. wstats->qual.level = stats->rssi_dbm;
  860. wstats->qual.noise = stats->noise_dbm;
  861. wstats->qual.updated = IW_QUAL_ALL_UPDATED | IW_QUAL_DBM;
  862. schedule_delayed_work(&iwm->stats_request, IWM_STATS_UPDATE_INTERVAL);
  863. mod_timer(&iwm->watchdog, round_jiffies(jiffies + IWM_WATCHDOG_PERIOD));
  864. return 0;
  865. }
  866. static int iwm_ntf_eeprom_proxy(struct iwm_priv *iwm, u8 *buf,
  867. unsigned long buf_size,
  868. struct iwm_wifi_cmd *cmd)
  869. {
  870. struct iwm_umac_cmd_eeprom_proxy *eeprom_proxy =
  871. (struct iwm_umac_cmd_eeprom_proxy *)
  872. (buf + sizeof(struct iwm_umac_wifi_in_hdr));
  873. struct iwm_umac_cmd_eeprom_proxy_hdr *hdr = &eeprom_proxy->hdr;
  874. u32 hdr_offset = le32_to_cpu(hdr->offset);
  875. u32 hdr_len = le32_to_cpu(hdr->len);
  876. u32 hdr_type = le32_to_cpu(hdr->type);
  877. IWM_DBG_NTF(iwm, DBG, "type: 0x%x, len: %d, offset: 0x%x\n",
  878. hdr_type, hdr_len, hdr_offset);
  879. if ((hdr_offset + hdr_len) > IWM_EEPROM_LEN)
  880. return -EINVAL;
  881. switch (hdr_type) {
  882. case IWM_UMAC_CMD_EEPROM_TYPE_READ:
  883. memcpy(iwm->eeprom + hdr_offset, eeprom_proxy->buf, hdr_len);
  884. break;
  885. case IWM_UMAC_CMD_EEPROM_TYPE_WRITE:
  886. default:
  887. return -ENOTSUPP;
  888. }
  889. return 0;
  890. }
  891. static int iwm_ntf_channel_info_list(struct iwm_priv *iwm, u8 *buf,
  892. unsigned long buf_size,
  893. struct iwm_wifi_cmd *cmd)
  894. {
  895. struct iwm_umac_cmd_get_channel_list *ch_list =
  896. (struct iwm_umac_cmd_get_channel_list *)
  897. (buf + sizeof(struct iwm_umac_wifi_in_hdr));
  898. struct wiphy *wiphy = iwm_to_wiphy(iwm);
  899. struct ieee80211_supported_band *band;
  900. int i;
  901. band = wiphy->bands[IEEE80211_BAND_2GHZ];
  902. for (i = 0; i < band->n_channels; i++) {
  903. unsigned long ch_mask_0 =
  904. le32_to_cpu(ch_list->ch[0].channels_mask);
  905. unsigned long ch_mask_2 =
  906. le32_to_cpu(ch_list->ch[2].channels_mask);
  907. if (!test_bit(i, &ch_mask_0))
  908. band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
  909. if (!test_bit(i, &ch_mask_2))
  910. band->channels[i].flags |= IEEE80211_CHAN_NO_IBSS;
  911. }
  912. band = wiphy->bands[IEEE80211_BAND_5GHZ];
  913. for (i = 0; i < min(band->n_channels, 32); i++) {
  914. unsigned long ch_mask_1 =
  915. le32_to_cpu(ch_list->ch[1].channels_mask);
  916. unsigned long ch_mask_3 =
  917. le32_to_cpu(ch_list->ch[3].channels_mask);
  918. if (!test_bit(i, &ch_mask_1))
  919. band->channels[i].flags |= IEEE80211_CHAN_DISABLED;
  920. if (!test_bit(i, &ch_mask_3))
  921. band->channels[i].flags |= IEEE80211_CHAN_NO_IBSS;
  922. }
  923. return 0;
  924. }
  925. static int iwm_ntf_stop_resume_tx(struct iwm_priv *iwm, u8 *buf,
  926. unsigned long buf_size,
  927. struct iwm_wifi_cmd *cmd)
  928. {
  929. struct iwm_umac_notif_stop_resume_tx *stp_res_tx =
  930. (struct iwm_umac_notif_stop_resume_tx *)buf;
  931. struct iwm_sta_info *sta_info;
  932. struct iwm_tid_info *tid_info;
  933. u8 sta_id = STA_ID_N_COLOR_ID(stp_res_tx->sta_id);
  934. u16 tid_msk = le16_to_cpu(stp_res_tx->stop_resume_tid_msk);
  935. int bit, ret = 0;
  936. bool stop = false;
  937. IWM_DBG_NTF(iwm, DBG, "stop/resume notification:\n"
  938. "\tflags: 0x%x\n"
  939. "\tSTA id: %d\n"
  940. "\tTID bitmask: 0x%x\n",
  941. stp_res_tx->flags, stp_res_tx->sta_id,
  942. stp_res_tx->stop_resume_tid_msk);
  943. if (stp_res_tx->flags & UMAC_STOP_TX_FLAG)
  944. stop = true;
  945. sta_info = &iwm->sta_table[sta_id];
  946. if (!sta_info->valid) {
  947. IWM_ERR(iwm, "Stoping an invalid STA: %d %d\n",
  948. sta_id, stp_res_tx->sta_id);
  949. return -EINVAL;
  950. }
  951. for_each_set_bit(bit, (unsigned long *)&tid_msk, IWM_UMAC_TID_NR) {
  952. tid_info = &sta_info->tid_info[bit];
  953. mutex_lock(&tid_info->mutex);
  954. tid_info->stopped = stop;
  955. mutex_unlock(&tid_info->mutex);
  956. if (!stop) {
  957. struct iwm_tx_queue *txq;
  958. int queue = iwm_tid_to_queue(bit);
  959. if (queue < 0)
  960. continue;
  961. txq = &iwm->txq[queue];
  962. /*
  963. * If we resume, we have to move our SKBs
  964. * back to the tx queue and queue some work.
  965. */
  966. spin_lock_bh(&txq->lock);
  967. skb_queue_splice_init(&txq->queue, &txq->stopped_queue);
  968. spin_unlock_bh(&txq->lock);
  969. queue_work(txq->wq, &txq->worker);
  970. }
  971. }
  972. /* We send an ACK only for the stop case */
  973. if (stop)
  974. ret = iwm_send_umac_stop_resume_tx(iwm, stp_res_tx);
  975. return ret;
  976. }
  977. static int iwm_ntf_wifi_if_wrapper(struct iwm_priv *iwm, u8 *buf,
  978. unsigned long buf_size,
  979. struct iwm_wifi_cmd *cmd)
  980. {
  981. struct iwm_umac_wifi_if *hdr;
  982. if (cmd == NULL) {
  983. IWM_ERR(iwm, "Couldn't find expected wifi command\n");
  984. return -EINVAL;
  985. }
  986. hdr = (struct iwm_umac_wifi_if *)cmd->buf.payload;
  987. IWM_DBG_NTF(iwm, DBG, "WIFI_IF_WRAPPER cmd is delivered to UMAC: "
  988. "oid is 0x%x\n", hdr->oid);
  989. set_bit(hdr->oid, &iwm->wifi_ntfy[0]);
  990. wake_up_interruptible(&iwm->wifi_ntfy_queue);
  991. switch (hdr->oid) {
  992. case UMAC_WIFI_IF_CMD_SET_PROFILE:
  993. iwm->umac_profile_active = true;
  994. break;
  995. default:
  996. break;
  997. }
  998. return 0;
  999. }
  1000. #define CT_KILL_DELAY (30 * HZ)
  1001. static int iwm_ntf_card_state(struct iwm_priv *iwm, u8 *buf,
  1002. unsigned long buf_size, struct iwm_wifi_cmd *cmd)
  1003. {
  1004. struct wiphy *wiphy = iwm_to_wiphy(iwm);
  1005. struct iwm_lmac_card_state *state = (struct iwm_lmac_card_state *)
  1006. (buf + sizeof(struct iwm_umac_wifi_in_hdr));
  1007. u32 flags = le32_to_cpu(state->flags);
  1008. IWM_INFO(iwm, "HW RF Kill %s, CT Kill %s\n",
  1009. flags & IWM_CARD_STATE_HW_DISABLED ? "ON" : "OFF",
  1010. flags & IWM_CARD_STATE_CTKILL_DISABLED ? "ON" : "OFF");
  1011. if (flags & IWM_CARD_STATE_CTKILL_DISABLED) {
  1012. /*
  1013. * We got a CTKILL event: We bring the interface down in
  1014. * oder to cool the device down, and try to bring it up
  1015. * 30 seconds later. If it's still too hot, we'll go through
  1016. * this code path again.
  1017. */
  1018. cancel_delayed_work_sync(&iwm->ct_kill_delay);
  1019. schedule_delayed_work(&iwm->ct_kill_delay, CT_KILL_DELAY);
  1020. }
  1021. wiphy_rfkill_set_hw_state(wiphy, flags &
  1022. (IWM_CARD_STATE_HW_DISABLED |
  1023. IWM_CARD_STATE_CTKILL_DISABLED));
  1024. return 0;
  1025. }
  1026. static int iwm_rx_handle_wifi(struct iwm_priv *iwm, u8 *buf,
  1027. unsigned long buf_size)
  1028. {
  1029. struct iwm_umac_wifi_in_hdr *wifi_hdr;
  1030. struct iwm_wifi_cmd *cmd;
  1031. u8 source, cmd_id;
  1032. u16 seq_num;
  1033. u32 count;
  1034. wifi_hdr = (struct iwm_umac_wifi_in_hdr *)buf;
  1035. cmd_id = wifi_hdr->sw_hdr.cmd.cmd;
  1036. source = GET_VAL32(wifi_hdr->hw_hdr.cmd, UMAC_HDI_IN_CMD_SOURCE);
  1037. if (source >= IWM_SRC_NUM) {
  1038. IWM_CRIT(iwm, "invalid source %d\n", source);
  1039. return -EINVAL;
  1040. }
  1041. if (cmd_id == REPLY_RX_MPDU_CMD)
  1042. trace_iwm_rx_packet(iwm, buf, buf_size);
  1043. else if ((cmd_id == UMAC_NOTIFY_OPCODE_RX_TICKET) &&
  1044. (source == UMAC_HDI_IN_SOURCE_FW))
  1045. trace_iwm_rx_ticket(iwm, buf, buf_size);
  1046. else
  1047. trace_iwm_rx_wifi_cmd(iwm, wifi_hdr);
  1048. count = GET_VAL32(wifi_hdr->sw_hdr.meta_data, UMAC_FW_CMD_BYTE_COUNT);
  1049. count += sizeof(struct iwm_umac_wifi_in_hdr) -
  1050. sizeof(struct iwm_dev_cmd_hdr);
  1051. if (count > buf_size) {
  1052. IWM_CRIT(iwm, "count %d, buf size:%ld\n", count, buf_size);
  1053. return -EINVAL;
  1054. }
  1055. seq_num = le16_to_cpu(wifi_hdr->sw_hdr.cmd.seq_num);
  1056. IWM_DBG_RX(iwm, DBG, "CMD:0x%x, source: 0x%x, seqnum: %d\n",
  1057. cmd_id, source, seq_num);
  1058. /*
  1059. * If this is a response to a previously sent command, there must
  1060. * be a pending command for this sequence number.
  1061. */
  1062. cmd = iwm_get_pending_wifi_cmd(iwm, seq_num);
  1063. /* Notify the caller only for sync commands. */
  1064. switch (source) {
  1065. case UMAC_HDI_IN_SOURCE_FHRX:
  1066. if (iwm->lmac_handlers[cmd_id] &&
  1067. test_bit(cmd_id, &iwm->lmac_handler_map[0]))
  1068. return iwm_notif_send(iwm, cmd, cmd_id, source,
  1069. buf, count);
  1070. break;
  1071. case UMAC_HDI_IN_SOURCE_FW:
  1072. if (iwm->umac_handlers[cmd_id] &&
  1073. test_bit(cmd_id, &iwm->umac_handler_map[0]))
  1074. return iwm_notif_send(iwm, cmd, cmd_id, source,
  1075. buf, count);
  1076. break;
  1077. case UMAC_HDI_IN_SOURCE_UDMA:
  1078. break;
  1079. }
  1080. return iwm_rx_handle_resp(iwm, buf, count, cmd);
  1081. }
  1082. int iwm_rx_handle_resp(struct iwm_priv *iwm, u8 *buf, unsigned long buf_size,
  1083. struct iwm_wifi_cmd *cmd)
  1084. {
  1085. u8 source, cmd_id;
  1086. struct iwm_umac_wifi_in_hdr *wifi_hdr;
  1087. int ret = 0;
  1088. wifi_hdr = (struct iwm_umac_wifi_in_hdr *)buf;
  1089. cmd_id = wifi_hdr->sw_hdr.cmd.cmd;
  1090. source = GET_VAL32(wifi_hdr->hw_hdr.cmd, UMAC_HDI_IN_CMD_SOURCE);
  1091. IWM_DBG_RX(iwm, DBG, "CMD:0x%x, source: 0x%x\n", cmd_id, source);
  1092. switch (source) {
  1093. case UMAC_HDI_IN_SOURCE_FHRX:
  1094. if (iwm->lmac_handlers[cmd_id])
  1095. ret = iwm->lmac_handlers[cmd_id]
  1096. (iwm, buf, buf_size, cmd);
  1097. break;
  1098. case UMAC_HDI_IN_SOURCE_FW:
  1099. if (iwm->umac_handlers[cmd_id])
  1100. ret = iwm->umac_handlers[cmd_id]
  1101. (iwm, buf, buf_size, cmd);
  1102. break;
  1103. case UMAC_HDI_IN_SOURCE_UDMA:
  1104. ret = -EINVAL;
  1105. break;
  1106. }
  1107. kfree(cmd);
  1108. return ret;
  1109. }
  1110. static int iwm_rx_handle_nonwifi(struct iwm_priv *iwm, u8 *buf,
  1111. unsigned long buf_size)
  1112. {
  1113. u8 seq_num;
  1114. struct iwm_udma_in_hdr *hdr = (struct iwm_udma_in_hdr *)buf;
  1115. struct iwm_nonwifi_cmd *cmd;
  1116. trace_iwm_rx_nonwifi_cmd(iwm, buf, buf_size);
  1117. seq_num = GET_VAL32(hdr->cmd, UDMA_HDI_IN_CMD_NON_WIFI_HW_SEQ_NUM);
  1118. /*
  1119. * We received a non wifi answer.
  1120. * Let's check if there's a pending command for it, and if so
  1121. * replace the command payload with the buffer, and then wake the
  1122. * callers up.
  1123. * That means we only support synchronised non wifi command response
  1124. * schemes.
  1125. */
  1126. list_for_each_entry(cmd, &iwm->nonwifi_pending_cmd, pending)
  1127. if (cmd->seq_num == seq_num) {
  1128. cmd->resp_received = true;
  1129. cmd->buf.len = buf_size;
  1130. memcpy(cmd->buf.hdr, buf, buf_size);
  1131. wake_up_interruptible(&iwm->nonwifi_queue);
  1132. }
  1133. return 0;
  1134. }
  1135. static int iwm_rx_handle_umac(struct iwm_priv *iwm, u8 *buf,
  1136. unsigned long buf_size)
  1137. {
  1138. int ret = 0;
  1139. u8 op_code;
  1140. unsigned long buf_offset = 0;
  1141. struct iwm_udma_in_hdr *hdr;
  1142. /*
  1143. * To allow for a more efficient bus usage, UMAC
  1144. * messages are encapsulated into UDMA ones. This
  1145. * way we can have several UMAC messages in one bus
  1146. * transfer.
  1147. * A UDMA frame size is always aligned on 16 bytes,
  1148. * and a UDMA frame must not start with a UMAC_PAD_TERMINAL
  1149. * word. This is how we parse a bus frame into several
  1150. * UDMA ones.
  1151. */
  1152. while (buf_offset < buf_size) {
  1153. hdr = (struct iwm_udma_in_hdr *)(buf + buf_offset);
  1154. if (iwm_rx_check_udma_hdr(hdr) < 0) {
  1155. IWM_DBG_RX(iwm, DBG, "End of frame\n");
  1156. break;
  1157. }
  1158. op_code = GET_VAL32(hdr->cmd, UMAC_HDI_IN_CMD_OPCODE);
  1159. IWM_DBG_RX(iwm, DBG, "Op code: 0x%x\n", op_code);
  1160. if (op_code == UMAC_HDI_IN_OPCODE_WIFI) {
  1161. ret |= iwm_rx_handle_wifi(iwm, buf + buf_offset,
  1162. buf_size - buf_offset);
  1163. } else if (op_code < UMAC_HDI_IN_OPCODE_NONWIFI_MAX) {
  1164. if (GET_VAL32(hdr->cmd,
  1165. UDMA_HDI_IN_CMD_NON_WIFI_HW_SIG) !=
  1166. UDMA_HDI_IN_CMD_NON_WIFI_HW_SIG) {
  1167. IWM_ERR(iwm, "Incorrect hw signature\n");
  1168. return -EINVAL;
  1169. }
  1170. ret |= iwm_rx_handle_nonwifi(iwm, buf + buf_offset,
  1171. buf_size - buf_offset);
  1172. } else {
  1173. IWM_ERR(iwm, "Invalid RX opcode: 0x%x\n", op_code);
  1174. ret |= -EINVAL;
  1175. }
  1176. buf_offset += iwm_rx_resp_size(hdr);
  1177. }
  1178. return ret;
  1179. }
  1180. int iwm_rx_handle(struct iwm_priv *iwm, u8 *buf, unsigned long buf_size)
  1181. {
  1182. struct iwm_udma_in_hdr *hdr;
  1183. hdr = (struct iwm_udma_in_hdr *)buf;
  1184. switch (le32_to_cpu(hdr->cmd)) {
  1185. case UMAC_REBOOT_BARKER:
  1186. if (test_bit(IWM_STATUS_READY, &iwm->status)) {
  1187. IWM_ERR(iwm, "Unexpected BARKER\n");
  1188. schedule_work(&iwm->reset_worker);
  1189. return 0;
  1190. }
  1191. return iwm_notif_send(iwm, NULL, IWM_BARKER_REBOOT_NOTIFICATION,
  1192. IWM_SRC_UDMA, buf, buf_size);
  1193. case UMAC_ACK_BARKER:
  1194. return iwm_notif_send(iwm, NULL, IWM_ACK_BARKER_NOTIFICATION,
  1195. IWM_SRC_UDMA, NULL, 0);
  1196. default:
  1197. IWM_DBG_RX(iwm, DBG, "Received cmd: 0x%x\n", hdr->cmd);
  1198. return iwm_rx_handle_umac(iwm, buf, buf_size);
  1199. }
  1200. return 0;
  1201. }
  1202. static const iwm_handler iwm_umac_handlers[] =
  1203. {
  1204. [UMAC_NOTIFY_OPCODE_ERROR] = iwm_ntf_error,
  1205. [UMAC_NOTIFY_OPCODE_ALIVE] = iwm_ntf_umac_alive,
  1206. [UMAC_NOTIFY_OPCODE_INIT_COMPLETE] = iwm_ntf_init_complete,
  1207. [UMAC_NOTIFY_OPCODE_WIFI_CORE_STATUS] = iwm_ntf_wifi_status,
  1208. [UMAC_NOTIFY_OPCODE_WIFI_IF_WRAPPER] = iwm_ntf_mlme,
  1209. [UMAC_NOTIFY_OPCODE_PAGE_DEALLOC] = iwm_ntf_tx_credit_update,
  1210. [UMAC_NOTIFY_OPCODE_RX_TICKET] = iwm_ntf_rx_ticket,
  1211. [UMAC_CMD_OPCODE_RESET] = iwm_ntf_umac_reset,
  1212. [UMAC_NOTIFY_OPCODE_STATS] = iwm_ntf_statistics,
  1213. [UMAC_CMD_OPCODE_EEPROM_PROXY] = iwm_ntf_eeprom_proxy,
  1214. [UMAC_CMD_OPCODE_GET_CHAN_INFO_LIST] = iwm_ntf_channel_info_list,
  1215. [UMAC_CMD_OPCODE_STOP_RESUME_STA_TX] = iwm_ntf_stop_resume_tx,
  1216. [REPLY_RX_MPDU_CMD] = iwm_ntf_rx_packet,
  1217. [UMAC_CMD_OPCODE_WIFI_IF_WRAPPER] = iwm_ntf_wifi_if_wrapper,
  1218. };
  1219. static const iwm_handler iwm_lmac_handlers[] =
  1220. {
  1221. [REPLY_TX] = iwm_ntf_tx,
  1222. [REPLY_ALIVE] = iwm_ntf_lmac_version,
  1223. [CALIBRATION_RES_NOTIFICATION] = iwm_ntf_calib_res,
  1224. [CALIBRATION_COMPLETE_NOTIFICATION] = iwm_ntf_calib_complete,
  1225. [CALIBRATION_CFG_CMD] = iwm_ntf_calib_cfg,
  1226. [REPLY_RX_MPDU_CMD] = iwm_ntf_rx_packet,
  1227. [CARD_STATE_NOTIFICATION] = iwm_ntf_card_state,
  1228. };
  1229. void iwm_rx_setup_handlers(struct iwm_priv *iwm)
  1230. {
  1231. iwm->umac_handlers = (iwm_handler *) iwm_umac_handlers;
  1232. iwm->lmac_handlers = (iwm_handler *) iwm_lmac_handlers;
  1233. }
  1234. static void iwm_remove_iv(struct sk_buff *skb, u32 hdr_total_len)
  1235. {
  1236. struct ieee80211_hdr *hdr;
  1237. unsigned int hdr_len;
  1238. hdr = (struct ieee80211_hdr *)skb->data;
  1239. if (!ieee80211_has_protected(hdr->frame_control))
  1240. return;
  1241. hdr_len = ieee80211_hdrlen(hdr->frame_control);
  1242. if (hdr_total_len <= hdr_len)
  1243. return;
  1244. memmove(skb->data + (hdr_total_len - hdr_len), skb->data, hdr_len);
  1245. skb_pull(skb, (hdr_total_len - hdr_len));
  1246. }
  1247. static void iwm_rx_adjust_packet(struct iwm_priv *iwm,
  1248. struct iwm_rx_packet *packet,
  1249. struct iwm_rx_ticket_node *ticket_node)
  1250. {
  1251. u32 payload_offset = 0, payload_len;
  1252. struct iwm_rx_ticket *ticket = ticket_node->ticket;
  1253. struct iwm_rx_mpdu_hdr *mpdu_hdr;
  1254. struct ieee80211_hdr *hdr;
  1255. mpdu_hdr = (struct iwm_rx_mpdu_hdr *)packet->skb->data;
  1256. payload_offset += sizeof(struct iwm_rx_mpdu_hdr);
  1257. /* Padding is 0 or 2 bytes */
  1258. payload_len = le16_to_cpu(mpdu_hdr->len) +
  1259. (le16_to_cpu(ticket->flags) & IWM_RX_TICKET_PAD_SIZE_MSK);
  1260. payload_len -= ticket->tail_len;
  1261. IWM_DBG_RX(iwm, DBG, "Packet adjusted, len:%d, offset:%d, "
  1262. "ticket offset:%d ticket tail len:%d\n",
  1263. payload_len, payload_offset, ticket->payload_offset,
  1264. ticket->tail_len);
  1265. IWM_HEXDUMP(iwm, DBG, RX, "RAW: ", packet->skb->data, packet->skb->len);
  1266. skb_pull(packet->skb, payload_offset);
  1267. skb_trim(packet->skb, payload_len);
  1268. iwm_remove_iv(packet->skb, ticket->payload_offset);
  1269. hdr = (struct ieee80211_hdr *) packet->skb->data;
  1270. if (ieee80211_is_data_qos(hdr->frame_control)) {
  1271. /* UMAC handed QOS_DATA frame with 2 padding bytes appended
  1272. * to the qos_ctl field in IEEE 802.11 headers. */
  1273. memmove(packet->skb->data + IEEE80211_QOS_CTL_LEN + 2,
  1274. packet->skb->data,
  1275. ieee80211_hdrlen(hdr->frame_control) -
  1276. IEEE80211_QOS_CTL_LEN);
  1277. hdr = (struct ieee80211_hdr *) skb_pull(packet->skb,
  1278. IEEE80211_QOS_CTL_LEN + 2);
  1279. hdr->frame_control &= ~cpu_to_le16(IEEE80211_STYPE_QOS_DATA);
  1280. }
  1281. IWM_HEXDUMP(iwm, DBG, RX, "ADJUSTED: ",
  1282. packet->skb->data, packet->skb->len);
  1283. }
  1284. static void classify8023(struct sk_buff *skb)
  1285. {
  1286. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *) skb->data;
  1287. if (ieee80211_is_data_qos(hdr->frame_control)) {
  1288. u8 *qc = ieee80211_get_qos_ctl(hdr);
  1289. /* frame has qos control */
  1290. skb->priority = *qc & IEEE80211_QOS_CTL_TID_MASK;
  1291. } else {
  1292. skb->priority = 0;
  1293. }
  1294. }
  1295. static void iwm_rx_process_amsdu(struct iwm_priv *iwm, struct sk_buff *skb)
  1296. {
  1297. struct wireless_dev *wdev = iwm_to_wdev(iwm);
  1298. struct net_device *ndev = iwm_to_ndev(iwm);
  1299. struct sk_buff_head list;
  1300. struct sk_buff *frame;
  1301. IWM_HEXDUMP(iwm, DBG, RX, "A-MSDU: ", skb->data, skb->len);
  1302. __skb_queue_head_init(&list);
  1303. ieee80211_amsdu_to_8023s(skb, &list, ndev->dev_addr, wdev->iftype, 0,
  1304. true);
  1305. while ((frame = __skb_dequeue(&list))) {
  1306. ndev->stats.rx_packets++;
  1307. ndev->stats.rx_bytes += frame->len;
  1308. frame->protocol = eth_type_trans(frame, ndev);
  1309. frame->ip_summed = CHECKSUM_NONE;
  1310. memset(frame->cb, 0, sizeof(frame->cb));
  1311. if (netif_rx_ni(frame) == NET_RX_DROP) {
  1312. IWM_ERR(iwm, "Packet dropped\n");
  1313. ndev->stats.rx_dropped++;
  1314. }
  1315. }
  1316. }
  1317. static void iwm_rx_process_packet(struct iwm_priv *iwm,
  1318. struct iwm_rx_packet *packet,
  1319. struct iwm_rx_ticket_node *ticket_node)
  1320. {
  1321. int ret;
  1322. struct sk_buff *skb = packet->skb;
  1323. struct wireless_dev *wdev = iwm_to_wdev(iwm);
  1324. struct net_device *ndev = iwm_to_ndev(iwm);
  1325. IWM_DBG_RX(iwm, DBG, "Processing packet ID %d\n", packet->id);
  1326. switch (le16_to_cpu(ticket_node->ticket->action)) {
  1327. case IWM_RX_TICKET_RELEASE:
  1328. IWM_DBG_RX(iwm, DBG, "RELEASE packet\n");
  1329. iwm_rx_adjust_packet(iwm, packet, ticket_node);
  1330. skb->dev = iwm_to_ndev(iwm);
  1331. classify8023(skb);
  1332. if (le16_to_cpu(ticket_node->ticket->flags) &
  1333. IWM_RX_TICKET_AMSDU_MSK) {
  1334. iwm_rx_process_amsdu(iwm, skb);
  1335. break;
  1336. }
  1337. ret = ieee80211_data_to_8023(skb, ndev->dev_addr, wdev->iftype);
  1338. if (ret < 0) {
  1339. IWM_DBG_RX(iwm, DBG, "Couldn't convert 802.11 header - "
  1340. "%d\n", ret);
  1341. kfree_skb(packet->skb);
  1342. break;
  1343. }
  1344. IWM_HEXDUMP(iwm, DBG, RX, "802.3: ", skb->data, skb->len);
  1345. ndev->stats.rx_packets++;
  1346. ndev->stats.rx_bytes += skb->len;
  1347. skb->protocol = eth_type_trans(skb, ndev);
  1348. skb->ip_summed = CHECKSUM_NONE;
  1349. memset(skb->cb, 0, sizeof(skb->cb));
  1350. if (netif_rx_ni(skb) == NET_RX_DROP) {
  1351. IWM_ERR(iwm, "Packet dropped\n");
  1352. ndev->stats.rx_dropped++;
  1353. }
  1354. break;
  1355. case IWM_RX_TICKET_DROP:
  1356. IWM_DBG_RX(iwm, DBG, "DROP packet: 0x%x\n",
  1357. le16_to_cpu(ticket_node->ticket->flags));
  1358. kfree_skb(packet->skb);
  1359. break;
  1360. default:
  1361. IWM_ERR(iwm, "Unknown ticket action: %d\n",
  1362. le16_to_cpu(ticket_node->ticket->action));
  1363. kfree_skb(packet->skb);
  1364. }
  1365. kfree(packet);
  1366. iwm_rx_ticket_node_free(ticket_node);
  1367. }
  1368. /*
  1369. * Rx data processing:
  1370. *
  1371. * We're receiving Rx packet from the LMAC, and Rx ticket from
  1372. * the UMAC.
  1373. * To forward a target data packet upstream (i.e. to the
  1374. * kernel network stack), we must have received an Rx ticket
  1375. * that tells us we're allowed to release this packet (ticket
  1376. * action is IWM_RX_TICKET_RELEASE). The Rx ticket also indicates,
  1377. * among other things, where valid data actually starts in the Rx
  1378. * packet.
  1379. */
  1380. void iwm_rx_worker(struct work_struct *work)
  1381. {
  1382. struct iwm_priv *iwm;
  1383. struct iwm_rx_ticket_node *ticket, *next;
  1384. iwm = container_of(work, struct iwm_priv, rx_worker);
  1385. /*
  1386. * We go through the tickets list and if there is a pending
  1387. * packet for it, we push it upstream.
  1388. * We stop whenever a ticket is missing its packet, as we're
  1389. * supposed to send the packets in order.
  1390. */
  1391. spin_lock(&iwm->ticket_lock);
  1392. list_for_each_entry_safe(ticket, next, &iwm->rx_tickets, node) {
  1393. struct iwm_rx_packet *packet =
  1394. iwm_rx_packet_get(iwm, le16_to_cpu(ticket->ticket->id));
  1395. if (!packet) {
  1396. IWM_DBG_RX(iwm, DBG, "Skip rx_work: Wait for ticket %d "
  1397. "to be handled first\n",
  1398. le16_to_cpu(ticket->ticket->id));
  1399. break;
  1400. }
  1401. list_del(&ticket->node);
  1402. iwm_rx_process_packet(iwm, packet, ticket);
  1403. }
  1404. spin_unlock(&iwm->ticket_lock);
  1405. }