rx.c 42 KB

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