tx.c 53 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
  4. * Copyright(c) 2013 - 2014 Intel Mobile Communications GmbH
  5. *
  6. * Portions of this file are derived from the ipw3945 project, as well
  7. * as portions of the ieee80211 subsystem header files.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of version 2 of the GNU General Public License as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful, but WITHOUT
  14. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  15. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along with
  19. * this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  21. *
  22. * The full GNU General Public License is included in this distribution in the
  23. * file called LICENSE.
  24. *
  25. * Contact Information:
  26. * Intel Linux Wireless <ilw@linux.intel.com>
  27. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  28. *
  29. *****************************************************************************/
  30. #include <linux/etherdevice.h>
  31. #include <linux/slab.h>
  32. #include <linux/sched.h>
  33. #include "iwl-debug.h"
  34. #include "iwl-csr.h"
  35. #include "iwl-prph.h"
  36. #include "iwl-io.h"
  37. #include "iwl-scd.h"
  38. #include "iwl-op-mode.h"
  39. #include "internal.h"
  40. /* FIXME: need to abstract out TX command (once we know what it looks like) */
  41. #include "dvm/commands.h"
  42. #define IWL_TX_CRC_SIZE 4
  43. #define IWL_TX_DELIMITER_SIZE 4
  44. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  45. * DMA services
  46. *
  47. * Theory of operation
  48. *
  49. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  50. * of buffer descriptors, each of which points to one or more data buffers for
  51. * the device to read from or fill. Driver and device exchange status of each
  52. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  53. * entries in each circular buffer, to protect against confusing empty and full
  54. * queue states.
  55. *
  56. * The device reads or writes the data in the queues via the device's several
  57. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  58. *
  59. * For Tx queue, there are low mark and high mark limits. If, after queuing
  60. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  61. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  62. * Tx queue resumed.
  63. *
  64. ***************************************************/
  65. static int iwl_queue_space(const struct iwl_queue *q)
  66. {
  67. unsigned int max;
  68. unsigned int used;
  69. /*
  70. * To avoid ambiguity between empty and completely full queues, there
  71. * should always be less than TFD_QUEUE_SIZE_MAX elements in the queue.
  72. * If q->n_window is smaller than TFD_QUEUE_SIZE_MAX, there is no need
  73. * to reserve any queue entries for this purpose.
  74. */
  75. if (q->n_window < TFD_QUEUE_SIZE_MAX)
  76. max = q->n_window;
  77. else
  78. max = TFD_QUEUE_SIZE_MAX - 1;
  79. /*
  80. * TFD_QUEUE_SIZE_MAX is a power of 2, so the following is equivalent to
  81. * modulo by TFD_QUEUE_SIZE_MAX and is well defined.
  82. */
  83. used = (q->write_ptr - q->read_ptr) & (TFD_QUEUE_SIZE_MAX - 1);
  84. if (WARN_ON(used > max))
  85. return 0;
  86. return max - used;
  87. }
  88. /*
  89. * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
  90. */
  91. static int iwl_queue_init(struct iwl_queue *q, int slots_num, u32 id)
  92. {
  93. q->n_window = slots_num;
  94. q->id = id;
  95. /* slots_num must be power-of-two size, otherwise
  96. * get_cmd_index is broken. */
  97. if (WARN_ON(!is_power_of_2(slots_num)))
  98. return -EINVAL;
  99. q->low_mark = q->n_window / 4;
  100. if (q->low_mark < 4)
  101. q->low_mark = 4;
  102. q->high_mark = q->n_window / 8;
  103. if (q->high_mark < 2)
  104. q->high_mark = 2;
  105. q->write_ptr = 0;
  106. q->read_ptr = 0;
  107. return 0;
  108. }
  109. static int iwl_pcie_alloc_dma_ptr(struct iwl_trans *trans,
  110. struct iwl_dma_ptr *ptr, size_t size)
  111. {
  112. if (WARN_ON(ptr->addr))
  113. return -EINVAL;
  114. ptr->addr = dma_alloc_coherent(trans->dev, size,
  115. &ptr->dma, GFP_KERNEL);
  116. if (!ptr->addr)
  117. return -ENOMEM;
  118. ptr->size = size;
  119. return 0;
  120. }
  121. static void iwl_pcie_free_dma_ptr(struct iwl_trans *trans,
  122. struct iwl_dma_ptr *ptr)
  123. {
  124. if (unlikely(!ptr->addr))
  125. return;
  126. dma_free_coherent(trans->dev, ptr->size, ptr->addr, ptr->dma);
  127. memset(ptr, 0, sizeof(*ptr));
  128. }
  129. static void iwl_pcie_txq_stuck_timer(unsigned long data)
  130. {
  131. struct iwl_txq *txq = (void *)data;
  132. struct iwl_trans_pcie *trans_pcie = txq->trans_pcie;
  133. struct iwl_trans *trans = iwl_trans_pcie_get_trans(trans_pcie);
  134. u32 scd_sram_addr = trans_pcie->scd_base_addr +
  135. SCD_TX_STTS_QUEUE_OFFSET(txq->q.id);
  136. u8 buf[16];
  137. int i;
  138. spin_lock(&txq->lock);
  139. /* check if triggered erroneously */
  140. if (txq->q.read_ptr == txq->q.write_ptr) {
  141. spin_unlock(&txq->lock);
  142. return;
  143. }
  144. spin_unlock(&txq->lock);
  145. IWL_ERR(trans, "Queue %d stuck for %u ms.\n", txq->q.id,
  146. jiffies_to_msecs(txq->wd_timeout));
  147. IWL_ERR(trans, "Current SW read_ptr %d write_ptr %d\n",
  148. txq->q.read_ptr, txq->q.write_ptr);
  149. iwl_trans_read_mem_bytes(trans, scd_sram_addr, buf, sizeof(buf));
  150. iwl_print_hex_error(trans, buf, sizeof(buf));
  151. for (i = 0; i < FH_TCSR_CHNL_NUM; i++)
  152. IWL_ERR(trans, "FH TRBs(%d) = 0x%08x\n", i,
  153. iwl_read_direct32(trans, FH_TX_TRB_REG(i)));
  154. for (i = 0; i < trans->cfg->base_params->num_of_queues; i++) {
  155. u32 status = iwl_read_prph(trans, SCD_QUEUE_STATUS_BITS(i));
  156. u8 fifo = (status >> SCD_QUEUE_STTS_REG_POS_TXF) & 0x7;
  157. bool active = !!(status & BIT(SCD_QUEUE_STTS_REG_POS_ACTIVE));
  158. u32 tbl_dw =
  159. iwl_trans_read_mem32(trans,
  160. trans_pcie->scd_base_addr +
  161. SCD_TRANS_TBL_OFFSET_QUEUE(i));
  162. if (i & 0x1)
  163. tbl_dw = (tbl_dw & 0xFFFF0000) >> 16;
  164. else
  165. tbl_dw = tbl_dw & 0x0000FFFF;
  166. IWL_ERR(trans,
  167. "Q %d is %sactive and mapped to fifo %d ra_tid 0x%04x [%d,%d]\n",
  168. i, active ? "" : "in", fifo, tbl_dw,
  169. iwl_read_prph(trans, SCD_QUEUE_RDPTR(i)) &
  170. (TFD_QUEUE_SIZE_MAX - 1),
  171. iwl_read_prph(trans, SCD_QUEUE_WRPTR(i)));
  172. }
  173. iwl_force_nmi(trans);
  174. }
  175. /*
  176. * iwl_pcie_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
  177. */
  178. static void iwl_pcie_txq_update_byte_cnt_tbl(struct iwl_trans *trans,
  179. struct iwl_txq *txq, u16 byte_cnt)
  180. {
  181. struct iwlagn_scd_bc_tbl *scd_bc_tbl;
  182. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  183. int write_ptr = txq->q.write_ptr;
  184. int txq_id = txq->q.id;
  185. u8 sec_ctl = 0;
  186. u8 sta_id = 0;
  187. u16 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
  188. __le16 bc_ent;
  189. struct iwl_tx_cmd *tx_cmd =
  190. (void *) txq->entries[txq->q.write_ptr].cmd->payload;
  191. scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
  192. WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX);
  193. sta_id = tx_cmd->sta_id;
  194. sec_ctl = tx_cmd->sec_ctl;
  195. switch (sec_ctl & TX_CMD_SEC_MSK) {
  196. case TX_CMD_SEC_CCM:
  197. len += IEEE80211_CCMP_MIC_LEN;
  198. break;
  199. case TX_CMD_SEC_TKIP:
  200. len += IEEE80211_TKIP_ICV_LEN;
  201. break;
  202. case TX_CMD_SEC_WEP:
  203. len += IEEE80211_WEP_IV_LEN + IEEE80211_WEP_ICV_LEN;
  204. break;
  205. }
  206. if (trans_pcie->bc_table_dword)
  207. len = DIV_ROUND_UP(len, 4);
  208. bc_ent = cpu_to_le16(len | (sta_id << 12));
  209. scd_bc_tbl[txq_id].tfd_offset[write_ptr] = bc_ent;
  210. if (write_ptr < TFD_QUEUE_SIZE_BC_DUP)
  211. scd_bc_tbl[txq_id].
  212. tfd_offset[TFD_QUEUE_SIZE_MAX + write_ptr] = bc_ent;
  213. }
  214. static void iwl_pcie_txq_inval_byte_cnt_tbl(struct iwl_trans *trans,
  215. struct iwl_txq *txq)
  216. {
  217. struct iwl_trans_pcie *trans_pcie =
  218. IWL_TRANS_GET_PCIE_TRANS(trans);
  219. struct iwlagn_scd_bc_tbl *scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
  220. int txq_id = txq->q.id;
  221. int read_ptr = txq->q.read_ptr;
  222. u8 sta_id = 0;
  223. __le16 bc_ent;
  224. struct iwl_tx_cmd *tx_cmd =
  225. (void *)txq->entries[txq->q.read_ptr].cmd->payload;
  226. WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX);
  227. if (txq_id != trans_pcie->cmd_queue)
  228. sta_id = tx_cmd->sta_id;
  229. bc_ent = cpu_to_le16(1 | (sta_id << 12));
  230. scd_bc_tbl[txq_id].tfd_offset[read_ptr] = bc_ent;
  231. if (read_ptr < TFD_QUEUE_SIZE_BC_DUP)
  232. scd_bc_tbl[txq_id].
  233. tfd_offset[TFD_QUEUE_SIZE_MAX + read_ptr] = bc_ent;
  234. }
  235. /*
  236. * iwl_pcie_txq_inc_wr_ptr - Send new write index to hardware
  237. */
  238. static void iwl_pcie_txq_inc_wr_ptr(struct iwl_trans *trans,
  239. struct iwl_txq *txq)
  240. {
  241. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  242. u32 reg = 0;
  243. int txq_id = txq->q.id;
  244. lockdep_assert_held(&txq->lock);
  245. /*
  246. * explicitly wake up the NIC if:
  247. * 1. shadow registers aren't enabled
  248. * 2. NIC is woken up for CMD regardless of shadow outside this function
  249. * 3. there is a chance that the NIC is asleep
  250. */
  251. if (!trans->cfg->base_params->shadow_reg_enable &&
  252. txq_id != trans_pcie->cmd_queue &&
  253. test_bit(STATUS_TPOWER_PMI, &trans->status)) {
  254. /*
  255. * wake up nic if it's powered down ...
  256. * uCode will wake up, and interrupt us again, so next
  257. * time we'll skip this part.
  258. */
  259. reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
  260. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  261. IWL_DEBUG_INFO(trans, "Tx queue %d requesting wakeup, GP1 = 0x%x\n",
  262. txq_id, reg);
  263. iwl_set_bit(trans, CSR_GP_CNTRL,
  264. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  265. txq->need_update = true;
  266. return;
  267. }
  268. }
  269. /*
  270. * if not in power-save mode, uCode will never sleep when we're
  271. * trying to tx (during RFKILL, we're not trying to tx).
  272. */
  273. IWL_DEBUG_TX(trans, "Q:%d WR: 0x%x\n", txq_id, txq->q.write_ptr);
  274. iwl_write32(trans, HBUS_TARG_WRPTR, txq->q.write_ptr | (txq_id << 8));
  275. }
  276. void iwl_pcie_txq_check_wrptrs(struct iwl_trans *trans)
  277. {
  278. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  279. int i;
  280. for (i = 0; i < trans->cfg->base_params->num_of_queues; i++) {
  281. struct iwl_txq *txq = &trans_pcie->txq[i];
  282. spin_lock_bh(&txq->lock);
  283. if (trans_pcie->txq[i].need_update) {
  284. iwl_pcie_txq_inc_wr_ptr(trans, txq);
  285. trans_pcie->txq[i].need_update = false;
  286. }
  287. spin_unlock_bh(&txq->lock);
  288. }
  289. }
  290. static inline dma_addr_t iwl_pcie_tfd_tb_get_addr(struct iwl_tfd *tfd, u8 idx)
  291. {
  292. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  293. dma_addr_t addr = get_unaligned_le32(&tb->lo);
  294. if (sizeof(dma_addr_t) > sizeof(u32))
  295. addr |=
  296. ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;
  297. return addr;
  298. }
  299. static inline void iwl_pcie_tfd_set_tb(struct iwl_tfd *tfd, u8 idx,
  300. dma_addr_t addr, u16 len)
  301. {
  302. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  303. u16 hi_n_len = len << 4;
  304. put_unaligned_le32(addr, &tb->lo);
  305. if (sizeof(dma_addr_t) > sizeof(u32))
  306. hi_n_len |= ((addr >> 16) >> 16) & 0xF;
  307. tb->hi_n_len = cpu_to_le16(hi_n_len);
  308. tfd->num_tbs = idx + 1;
  309. }
  310. static inline u8 iwl_pcie_tfd_get_num_tbs(struct iwl_tfd *tfd)
  311. {
  312. return tfd->num_tbs & 0x1f;
  313. }
  314. static void iwl_pcie_tfd_unmap(struct iwl_trans *trans,
  315. struct iwl_cmd_meta *meta,
  316. struct iwl_tfd *tfd)
  317. {
  318. int i;
  319. int num_tbs;
  320. /* Sanity check on number of chunks */
  321. num_tbs = iwl_pcie_tfd_get_num_tbs(tfd);
  322. if (num_tbs >= IWL_NUM_OF_TBS) {
  323. IWL_ERR(trans, "Too many chunks: %i\n", num_tbs);
  324. /* @todo issue fatal error, it is quite serious situation */
  325. return;
  326. }
  327. /* first TB is never freed - it's the scratchbuf data */
  328. for (i = 1; i < num_tbs; i++)
  329. dma_unmap_single(trans->dev, iwl_pcie_tfd_tb_get_addr(tfd, i),
  330. iwl_pcie_tfd_tb_get_len(tfd, i),
  331. DMA_TO_DEVICE);
  332. tfd->num_tbs = 0;
  333. }
  334. /*
  335. * iwl_pcie_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
  336. * @trans - transport private data
  337. * @txq - tx queue
  338. * @dma_dir - the direction of the DMA mapping
  339. *
  340. * Does NOT advance any TFD circular buffer read/write indexes
  341. * Does NOT free the TFD itself (which is within circular buffer)
  342. */
  343. static void iwl_pcie_txq_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq)
  344. {
  345. struct iwl_tfd *tfd_tmp = txq->tfds;
  346. /* rd_ptr is bounded by TFD_QUEUE_SIZE_MAX and
  347. * idx is bounded by n_window
  348. */
  349. int rd_ptr = txq->q.read_ptr;
  350. int idx = get_cmd_index(&txq->q, rd_ptr);
  351. lockdep_assert_held(&txq->lock);
  352. /* We have only q->n_window txq->entries, but we use
  353. * TFD_QUEUE_SIZE_MAX tfds
  354. */
  355. iwl_pcie_tfd_unmap(trans, &txq->entries[idx].meta, &tfd_tmp[rd_ptr]);
  356. /* free SKB */
  357. if (txq->entries) {
  358. struct sk_buff *skb;
  359. skb = txq->entries[idx].skb;
  360. /* Can be called from irqs-disabled context
  361. * If skb is not NULL, it means that the whole queue is being
  362. * freed and that the queue is not empty - free the skb
  363. */
  364. if (skb) {
  365. iwl_op_mode_free_skb(trans->op_mode, skb);
  366. txq->entries[idx].skb = NULL;
  367. }
  368. }
  369. }
  370. static int iwl_pcie_txq_build_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
  371. dma_addr_t addr, u16 len, bool reset)
  372. {
  373. struct iwl_queue *q;
  374. struct iwl_tfd *tfd, *tfd_tmp;
  375. u32 num_tbs;
  376. q = &txq->q;
  377. tfd_tmp = txq->tfds;
  378. tfd = &tfd_tmp[q->write_ptr];
  379. if (reset)
  380. memset(tfd, 0, sizeof(*tfd));
  381. num_tbs = iwl_pcie_tfd_get_num_tbs(tfd);
  382. /* Each TFD can point to a maximum 20 Tx buffers */
  383. if (num_tbs >= IWL_NUM_OF_TBS) {
  384. IWL_ERR(trans, "Error can not send more than %d chunks\n",
  385. IWL_NUM_OF_TBS);
  386. return -EINVAL;
  387. }
  388. if (WARN(addr & ~IWL_TX_DMA_MASK,
  389. "Unaligned address = %llx\n", (unsigned long long)addr))
  390. return -EINVAL;
  391. iwl_pcie_tfd_set_tb(tfd, num_tbs, addr, len);
  392. return 0;
  393. }
  394. static int iwl_pcie_txq_alloc(struct iwl_trans *trans,
  395. struct iwl_txq *txq, int slots_num,
  396. u32 txq_id)
  397. {
  398. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  399. size_t tfd_sz = sizeof(struct iwl_tfd) * TFD_QUEUE_SIZE_MAX;
  400. size_t scratchbuf_sz;
  401. int i;
  402. if (WARN_ON(txq->entries || txq->tfds))
  403. return -EINVAL;
  404. setup_timer(&txq->stuck_timer, iwl_pcie_txq_stuck_timer,
  405. (unsigned long)txq);
  406. txq->trans_pcie = trans_pcie;
  407. txq->q.n_window = slots_num;
  408. txq->entries = kcalloc(slots_num,
  409. sizeof(struct iwl_pcie_txq_entry),
  410. GFP_KERNEL);
  411. if (!txq->entries)
  412. goto error;
  413. if (txq_id == trans_pcie->cmd_queue)
  414. for (i = 0; i < slots_num; i++) {
  415. txq->entries[i].cmd =
  416. kmalloc(sizeof(struct iwl_device_cmd),
  417. GFP_KERNEL);
  418. if (!txq->entries[i].cmd)
  419. goto error;
  420. }
  421. /* Circular buffer of transmit frame descriptors (TFDs),
  422. * shared with device */
  423. txq->tfds = dma_alloc_coherent(trans->dev, tfd_sz,
  424. &txq->q.dma_addr, GFP_KERNEL);
  425. if (!txq->tfds)
  426. goto error;
  427. BUILD_BUG_ON(IWL_HCMD_SCRATCHBUF_SIZE != sizeof(*txq->scratchbufs));
  428. BUILD_BUG_ON(offsetof(struct iwl_pcie_txq_scratch_buf, scratch) !=
  429. sizeof(struct iwl_cmd_header) +
  430. offsetof(struct iwl_tx_cmd, scratch));
  431. scratchbuf_sz = sizeof(*txq->scratchbufs) * slots_num;
  432. txq->scratchbufs = dma_alloc_coherent(trans->dev, scratchbuf_sz,
  433. &txq->scratchbufs_dma,
  434. GFP_KERNEL);
  435. if (!txq->scratchbufs)
  436. goto err_free_tfds;
  437. txq->q.id = txq_id;
  438. return 0;
  439. err_free_tfds:
  440. dma_free_coherent(trans->dev, tfd_sz, txq->tfds, txq->q.dma_addr);
  441. error:
  442. if (txq->entries && txq_id == trans_pcie->cmd_queue)
  443. for (i = 0; i < slots_num; i++)
  444. kfree(txq->entries[i].cmd);
  445. kfree(txq->entries);
  446. txq->entries = NULL;
  447. return -ENOMEM;
  448. }
  449. static int iwl_pcie_txq_init(struct iwl_trans *trans, struct iwl_txq *txq,
  450. int slots_num, u32 txq_id)
  451. {
  452. int ret;
  453. txq->need_update = false;
  454. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  455. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  456. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  457. /* Initialize queue's high/low-water marks, and head/tail indexes */
  458. ret = iwl_queue_init(&txq->q, slots_num, txq_id);
  459. if (ret)
  460. return ret;
  461. spin_lock_init(&txq->lock);
  462. /*
  463. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  464. * given Tx queue, and enable the DMA channel used for that queue.
  465. * Circular buffer (TFD queue in DRAM) physical base address */
  466. iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(txq_id),
  467. txq->q.dma_addr >> 8);
  468. return 0;
  469. }
  470. /*
  471. * iwl_pcie_txq_unmap - Unmap any remaining DMA mappings and free skb's
  472. */
  473. static void iwl_pcie_txq_unmap(struct iwl_trans *trans, int txq_id)
  474. {
  475. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  476. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  477. struct iwl_queue *q = &txq->q;
  478. spin_lock_bh(&txq->lock);
  479. while (q->write_ptr != q->read_ptr) {
  480. IWL_DEBUG_TX_REPLY(trans, "Q %d Free %d\n",
  481. txq_id, q->read_ptr);
  482. iwl_pcie_txq_free_tfd(trans, txq);
  483. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr);
  484. }
  485. txq->active = false;
  486. spin_unlock_bh(&txq->lock);
  487. /* just in case - this queue may have been stopped */
  488. iwl_wake_queue(trans, txq);
  489. }
  490. /*
  491. * iwl_pcie_txq_free - Deallocate DMA queue.
  492. * @txq: Transmit queue to deallocate.
  493. *
  494. * Empty queue by removing and destroying all BD's.
  495. * Free all buffers.
  496. * 0-fill, but do not free "txq" descriptor structure.
  497. */
  498. static void iwl_pcie_txq_free(struct iwl_trans *trans, int txq_id)
  499. {
  500. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  501. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  502. struct device *dev = trans->dev;
  503. int i;
  504. if (WARN_ON(!txq))
  505. return;
  506. iwl_pcie_txq_unmap(trans, txq_id);
  507. /* De-alloc array of command/tx buffers */
  508. if (txq_id == trans_pcie->cmd_queue)
  509. for (i = 0; i < txq->q.n_window; i++) {
  510. kzfree(txq->entries[i].cmd);
  511. kzfree(txq->entries[i].free_buf);
  512. }
  513. /* De-alloc circular buffer of TFDs */
  514. if (txq->tfds) {
  515. dma_free_coherent(dev,
  516. sizeof(struct iwl_tfd) * TFD_QUEUE_SIZE_MAX,
  517. txq->tfds, txq->q.dma_addr);
  518. txq->q.dma_addr = 0;
  519. txq->tfds = NULL;
  520. dma_free_coherent(dev,
  521. sizeof(*txq->scratchbufs) * txq->q.n_window,
  522. txq->scratchbufs, txq->scratchbufs_dma);
  523. }
  524. kfree(txq->entries);
  525. txq->entries = NULL;
  526. del_timer_sync(&txq->stuck_timer);
  527. /* 0-fill queue descriptor structure */
  528. memset(txq, 0, sizeof(*txq));
  529. }
  530. void iwl_pcie_tx_start(struct iwl_trans *trans, u32 scd_base_addr)
  531. {
  532. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  533. int nq = trans->cfg->base_params->num_of_queues;
  534. int chan;
  535. u32 reg_val;
  536. int clear_dwords = (SCD_TRANS_TBL_OFFSET_QUEUE(nq) -
  537. SCD_CONTEXT_MEM_LOWER_BOUND) / sizeof(u32);
  538. /* make sure all queue are not stopped/used */
  539. memset(trans_pcie->queue_stopped, 0, sizeof(trans_pcie->queue_stopped));
  540. memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
  541. trans_pcie->scd_base_addr =
  542. iwl_read_prph(trans, SCD_SRAM_BASE_ADDR);
  543. WARN_ON(scd_base_addr != 0 &&
  544. scd_base_addr != trans_pcie->scd_base_addr);
  545. /* reset context data, TX status and translation data */
  546. iwl_trans_write_mem(trans, trans_pcie->scd_base_addr +
  547. SCD_CONTEXT_MEM_LOWER_BOUND,
  548. NULL, clear_dwords);
  549. iwl_write_prph(trans, SCD_DRAM_BASE_ADDR,
  550. trans_pcie->scd_bc_tbls.dma >> 10);
  551. /* The chain extension of the SCD doesn't work well. This feature is
  552. * enabled by default by the HW, so we need to disable it manually.
  553. */
  554. if (trans->cfg->base_params->scd_chain_ext_wa)
  555. iwl_write_prph(trans, SCD_CHAINEXT_EN, 0);
  556. iwl_trans_ac_txq_enable(trans, trans_pcie->cmd_queue,
  557. trans_pcie->cmd_fifo,
  558. trans_pcie->cmd_q_wdg_timeout);
  559. /* Activate all Tx DMA/FIFO channels */
  560. iwl_scd_activate_fifos(trans);
  561. /* Enable DMA channel */
  562. for (chan = 0; chan < FH_TCSR_CHNL_NUM; chan++)
  563. iwl_write_direct32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
  564. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  565. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  566. /* Update FH chicken bits */
  567. reg_val = iwl_read_direct32(trans, FH_TX_CHICKEN_BITS_REG);
  568. iwl_write_direct32(trans, FH_TX_CHICKEN_BITS_REG,
  569. reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  570. /* Enable L1-Active */
  571. if (trans->cfg->device_family != IWL_DEVICE_FAMILY_8000)
  572. iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
  573. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  574. }
  575. void iwl_trans_pcie_tx_reset(struct iwl_trans *trans)
  576. {
  577. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  578. int txq_id;
  579. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  580. txq_id++) {
  581. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  582. iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(txq_id),
  583. txq->q.dma_addr >> 8);
  584. iwl_pcie_txq_unmap(trans, txq_id);
  585. txq->q.read_ptr = 0;
  586. txq->q.write_ptr = 0;
  587. }
  588. /* Tell NIC where to find the "keep warm" buffer */
  589. iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
  590. trans_pcie->kw.dma >> 4);
  591. /*
  592. * Send 0 as the scd_base_addr since the device may have be reset
  593. * while we were in WoWLAN in which case SCD_SRAM_BASE_ADDR will
  594. * contain garbage.
  595. */
  596. iwl_pcie_tx_start(trans, 0);
  597. }
  598. static void iwl_pcie_tx_stop_fh(struct iwl_trans *trans)
  599. {
  600. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  601. unsigned long flags;
  602. int ch, ret;
  603. u32 mask = 0;
  604. spin_lock(&trans_pcie->irq_lock);
  605. if (!iwl_trans_grab_nic_access(trans, false, &flags))
  606. goto out;
  607. /* Stop each Tx DMA channel */
  608. for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
  609. iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  610. mask |= FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch);
  611. }
  612. /* Wait for DMA channels to be idle */
  613. ret = iwl_poll_bit(trans, FH_TSSR_TX_STATUS_REG, mask, mask, 5000);
  614. if (ret < 0)
  615. IWL_ERR(trans,
  616. "Failing on timeout while stopping DMA channel %d [0x%08x]\n",
  617. ch, iwl_read32(trans, FH_TSSR_TX_STATUS_REG));
  618. iwl_trans_release_nic_access(trans, &flags);
  619. out:
  620. spin_unlock(&trans_pcie->irq_lock);
  621. }
  622. /*
  623. * iwl_pcie_tx_stop - Stop all Tx DMA channels
  624. */
  625. int iwl_pcie_tx_stop(struct iwl_trans *trans)
  626. {
  627. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  628. int txq_id;
  629. /* Turn off all Tx DMA fifos */
  630. iwl_scd_deactivate_fifos(trans);
  631. /* Turn off all Tx DMA channels */
  632. iwl_pcie_tx_stop_fh(trans);
  633. /*
  634. * This function can be called before the op_mode disabled the
  635. * queues. This happens when we have an rfkill interrupt.
  636. * Since we stop Tx altogether - mark the queues as stopped.
  637. */
  638. memset(trans_pcie->queue_stopped, 0, sizeof(trans_pcie->queue_stopped));
  639. memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
  640. /* This can happen: start_hw, stop_device */
  641. if (!trans_pcie->txq)
  642. return 0;
  643. /* Unmap DMA from host system and free skb's */
  644. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  645. txq_id++)
  646. iwl_pcie_txq_unmap(trans, txq_id);
  647. return 0;
  648. }
  649. /*
  650. * iwl_trans_tx_free - Free TXQ Context
  651. *
  652. * Destroy all TX DMA queues and structures
  653. */
  654. void iwl_pcie_tx_free(struct iwl_trans *trans)
  655. {
  656. int txq_id;
  657. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  658. /* Tx queues */
  659. if (trans_pcie->txq) {
  660. for (txq_id = 0;
  661. txq_id < trans->cfg->base_params->num_of_queues; txq_id++)
  662. iwl_pcie_txq_free(trans, txq_id);
  663. }
  664. kfree(trans_pcie->txq);
  665. trans_pcie->txq = NULL;
  666. iwl_pcie_free_dma_ptr(trans, &trans_pcie->kw);
  667. iwl_pcie_free_dma_ptr(trans, &trans_pcie->scd_bc_tbls);
  668. }
  669. /*
  670. * iwl_pcie_tx_alloc - allocate TX context
  671. * Allocate all Tx DMA structures and initialize them
  672. */
  673. static int iwl_pcie_tx_alloc(struct iwl_trans *trans)
  674. {
  675. int ret;
  676. int txq_id, slots_num;
  677. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  678. u16 scd_bc_tbls_size = trans->cfg->base_params->num_of_queues *
  679. sizeof(struct iwlagn_scd_bc_tbl);
  680. /*It is not allowed to alloc twice, so warn when this happens.
  681. * We cannot rely on the previous allocation, so free and fail */
  682. if (WARN_ON(trans_pcie->txq)) {
  683. ret = -EINVAL;
  684. goto error;
  685. }
  686. ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->scd_bc_tbls,
  687. scd_bc_tbls_size);
  688. if (ret) {
  689. IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
  690. goto error;
  691. }
  692. /* Alloc keep-warm buffer */
  693. ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
  694. if (ret) {
  695. IWL_ERR(trans, "Keep Warm allocation failed\n");
  696. goto error;
  697. }
  698. trans_pcie->txq = kcalloc(trans->cfg->base_params->num_of_queues,
  699. sizeof(struct iwl_txq), GFP_KERNEL);
  700. if (!trans_pcie->txq) {
  701. IWL_ERR(trans, "Not enough memory for txq\n");
  702. ret = -ENOMEM;
  703. goto error;
  704. }
  705. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  706. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  707. txq_id++) {
  708. slots_num = (txq_id == trans_pcie->cmd_queue) ?
  709. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  710. ret = iwl_pcie_txq_alloc(trans, &trans_pcie->txq[txq_id],
  711. slots_num, txq_id);
  712. if (ret) {
  713. IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
  714. goto error;
  715. }
  716. }
  717. return 0;
  718. error:
  719. iwl_pcie_tx_free(trans);
  720. return ret;
  721. }
  722. int iwl_pcie_tx_init(struct iwl_trans *trans)
  723. {
  724. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  725. int ret;
  726. int txq_id, slots_num;
  727. bool alloc = false;
  728. if (!trans_pcie->txq) {
  729. ret = iwl_pcie_tx_alloc(trans);
  730. if (ret)
  731. goto error;
  732. alloc = true;
  733. }
  734. spin_lock(&trans_pcie->irq_lock);
  735. /* Turn off all Tx DMA fifos */
  736. iwl_scd_deactivate_fifos(trans);
  737. /* Tell NIC where to find the "keep warm" buffer */
  738. iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
  739. trans_pcie->kw.dma >> 4);
  740. spin_unlock(&trans_pcie->irq_lock);
  741. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  742. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  743. txq_id++) {
  744. slots_num = (txq_id == trans_pcie->cmd_queue) ?
  745. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  746. ret = iwl_pcie_txq_init(trans, &trans_pcie->txq[txq_id],
  747. slots_num, txq_id);
  748. if (ret) {
  749. IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
  750. goto error;
  751. }
  752. }
  753. if (trans->cfg->base_params->num_of_queues > 20)
  754. iwl_set_bits_prph(trans, SCD_GP_CTRL,
  755. SCD_GP_CTRL_ENABLE_31_QUEUES);
  756. return 0;
  757. error:
  758. /*Upon error, free only if we allocated something */
  759. if (alloc)
  760. iwl_pcie_tx_free(trans);
  761. return ret;
  762. }
  763. static inline void iwl_pcie_txq_progress(struct iwl_txq *txq)
  764. {
  765. lockdep_assert_held(&txq->lock);
  766. if (!txq->wd_timeout)
  767. return;
  768. /*
  769. * station is asleep and we send data - that must
  770. * be uAPSD or PS-Poll. Don't rearm the timer.
  771. */
  772. if (txq->frozen)
  773. return;
  774. /*
  775. * if empty delete timer, otherwise move timer forward
  776. * since we're making progress on this queue
  777. */
  778. if (txq->q.read_ptr == txq->q.write_ptr)
  779. del_timer(&txq->stuck_timer);
  780. else
  781. mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
  782. }
  783. /* Frees buffers until index _not_ inclusive */
  784. void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int txq_id, int ssn,
  785. struct sk_buff_head *skbs)
  786. {
  787. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  788. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  789. int tfd_num = ssn & (TFD_QUEUE_SIZE_MAX - 1);
  790. struct iwl_queue *q = &txq->q;
  791. int last_to_free;
  792. /* This function is not meant to release cmd queue*/
  793. if (WARN_ON(txq_id == trans_pcie->cmd_queue))
  794. return;
  795. spin_lock_bh(&txq->lock);
  796. if (!txq->active) {
  797. IWL_DEBUG_TX_QUEUES(trans, "Q %d inactive - ignoring idx %d\n",
  798. txq_id, ssn);
  799. goto out;
  800. }
  801. if (txq->q.read_ptr == tfd_num)
  802. goto out;
  803. IWL_DEBUG_TX_REPLY(trans, "[Q %d] %d -> %d (%d)\n",
  804. txq_id, txq->q.read_ptr, tfd_num, ssn);
  805. /*Since we free until index _not_ inclusive, the one before index is
  806. * the last we will free. This one must be used */
  807. last_to_free = iwl_queue_dec_wrap(tfd_num);
  808. if (!iwl_queue_used(q, last_to_free)) {
  809. IWL_ERR(trans,
  810. "%s: Read index for DMA queue txq id (%d), last_to_free %d is out of range [0-%d] %d %d.\n",
  811. __func__, txq_id, last_to_free, TFD_QUEUE_SIZE_MAX,
  812. q->write_ptr, q->read_ptr);
  813. goto out;
  814. }
  815. if (WARN_ON(!skb_queue_empty(skbs)))
  816. goto out;
  817. for (;
  818. q->read_ptr != tfd_num;
  819. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr)) {
  820. if (WARN_ON_ONCE(txq->entries[txq->q.read_ptr].skb == NULL))
  821. continue;
  822. __skb_queue_tail(skbs, txq->entries[txq->q.read_ptr].skb);
  823. txq->entries[txq->q.read_ptr].skb = NULL;
  824. iwl_pcie_txq_inval_byte_cnt_tbl(trans, txq);
  825. iwl_pcie_txq_free_tfd(trans, txq);
  826. }
  827. iwl_pcie_txq_progress(txq);
  828. if (iwl_queue_space(&txq->q) > txq->q.low_mark)
  829. iwl_wake_queue(trans, txq);
  830. if (q->read_ptr == q->write_ptr) {
  831. IWL_DEBUG_RPM(trans, "Q %d - last tx reclaimed\n", q->id);
  832. iwl_trans_pcie_unref(trans);
  833. }
  834. out:
  835. spin_unlock_bh(&txq->lock);
  836. }
  837. static int iwl_pcie_set_cmd_in_flight(struct iwl_trans *trans,
  838. const struct iwl_host_cmd *cmd)
  839. {
  840. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  841. int ret;
  842. lockdep_assert_held(&trans_pcie->reg_lock);
  843. if (!(cmd->flags & CMD_SEND_IN_IDLE) &&
  844. !trans_pcie->ref_cmd_in_flight) {
  845. trans_pcie->ref_cmd_in_flight = true;
  846. IWL_DEBUG_RPM(trans, "set ref_cmd_in_flight - ref\n");
  847. iwl_trans_pcie_ref(trans);
  848. }
  849. /*
  850. * wake up the NIC to make sure that the firmware will see the host
  851. * command - we will let the NIC sleep once all the host commands
  852. * returned. This needs to be done only on NICs that have
  853. * apmg_wake_up_wa set.
  854. */
  855. if (trans->cfg->base_params->apmg_wake_up_wa &&
  856. !trans_pcie->cmd_hold_nic_awake) {
  857. __iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL,
  858. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  859. if (trans->cfg->device_family == IWL_DEVICE_FAMILY_8000)
  860. udelay(2);
  861. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  862. CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
  863. (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
  864. CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP),
  865. 15000);
  866. if (ret < 0) {
  867. __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
  868. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  869. IWL_ERR(trans, "Failed to wake NIC for hcmd\n");
  870. return -EIO;
  871. }
  872. trans_pcie->cmd_hold_nic_awake = true;
  873. }
  874. return 0;
  875. }
  876. static int iwl_pcie_clear_cmd_in_flight(struct iwl_trans *trans)
  877. {
  878. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  879. lockdep_assert_held(&trans_pcie->reg_lock);
  880. if (trans_pcie->ref_cmd_in_flight) {
  881. trans_pcie->ref_cmd_in_flight = false;
  882. IWL_DEBUG_RPM(trans, "clear ref_cmd_in_flight - unref\n");
  883. iwl_trans_pcie_unref(trans);
  884. }
  885. if (trans->cfg->base_params->apmg_wake_up_wa) {
  886. if (WARN_ON(!trans_pcie->cmd_hold_nic_awake))
  887. return 0;
  888. trans_pcie->cmd_hold_nic_awake = false;
  889. __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
  890. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  891. }
  892. return 0;
  893. }
  894. /*
  895. * iwl_pcie_cmdq_reclaim - Reclaim TX command queue entries already Tx'd
  896. *
  897. * When FW advances 'R' index, all entries between old and new 'R' index
  898. * need to be reclaimed. As result, some free space forms. If there is
  899. * enough free space (> low mark), wake the stack that feeds us.
  900. */
  901. static void iwl_pcie_cmdq_reclaim(struct iwl_trans *trans, int txq_id, int idx)
  902. {
  903. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  904. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  905. struct iwl_queue *q = &txq->q;
  906. unsigned long flags;
  907. int nfreed = 0;
  908. lockdep_assert_held(&txq->lock);
  909. if ((idx >= TFD_QUEUE_SIZE_MAX) || (!iwl_queue_used(q, idx))) {
  910. IWL_ERR(trans,
  911. "%s: Read index for DMA queue txq id (%d), index %d is out of range [0-%d] %d %d.\n",
  912. __func__, txq_id, idx, TFD_QUEUE_SIZE_MAX,
  913. q->write_ptr, q->read_ptr);
  914. return;
  915. }
  916. for (idx = iwl_queue_inc_wrap(idx); q->read_ptr != idx;
  917. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr)) {
  918. if (nfreed++ > 0) {
  919. IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n",
  920. idx, q->write_ptr, q->read_ptr);
  921. iwl_force_nmi(trans);
  922. }
  923. }
  924. if (q->read_ptr == q->write_ptr) {
  925. spin_lock_irqsave(&trans_pcie->reg_lock, flags);
  926. iwl_pcie_clear_cmd_in_flight(trans);
  927. spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
  928. }
  929. iwl_pcie_txq_progress(txq);
  930. }
  931. static int iwl_pcie_txq_set_ratid_map(struct iwl_trans *trans, u16 ra_tid,
  932. u16 txq_id)
  933. {
  934. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  935. u32 tbl_dw_addr;
  936. u32 tbl_dw;
  937. u16 scd_q2ratid;
  938. scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  939. tbl_dw_addr = trans_pcie->scd_base_addr +
  940. SCD_TRANS_TBL_OFFSET_QUEUE(txq_id);
  941. tbl_dw = iwl_trans_read_mem32(trans, tbl_dw_addr);
  942. if (txq_id & 0x1)
  943. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  944. else
  945. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  946. iwl_trans_write_mem32(trans, tbl_dw_addr, tbl_dw);
  947. return 0;
  948. }
  949. /* Receiver address (actually, Rx station's index into station table),
  950. * combined with Traffic ID (QOS priority), in format used by Tx Scheduler */
  951. #define BUILD_RAxTID(sta_id, tid) (((sta_id) << 4) + (tid))
  952. void iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int txq_id, u16 ssn,
  953. const struct iwl_trans_txq_scd_cfg *cfg,
  954. unsigned int wdg_timeout)
  955. {
  956. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  957. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  958. int fifo = -1;
  959. if (test_and_set_bit(txq_id, trans_pcie->queue_used))
  960. WARN_ONCE(1, "queue %d already used - expect issues", txq_id);
  961. txq->wd_timeout = msecs_to_jiffies(wdg_timeout);
  962. if (cfg) {
  963. fifo = cfg->fifo;
  964. /* Disable the scheduler prior configuring the cmd queue */
  965. if (txq_id == trans_pcie->cmd_queue &&
  966. trans_pcie->scd_set_active)
  967. iwl_scd_enable_set_active(trans, 0);
  968. /* Stop this Tx queue before configuring it */
  969. iwl_scd_txq_set_inactive(trans, txq_id);
  970. /* Set this queue as a chain-building queue unless it is CMD */
  971. if (txq_id != trans_pcie->cmd_queue)
  972. iwl_scd_txq_set_chain(trans, txq_id);
  973. if (cfg->aggregate) {
  974. u16 ra_tid = BUILD_RAxTID(cfg->sta_id, cfg->tid);
  975. /* Map receiver-address / traffic-ID to this queue */
  976. iwl_pcie_txq_set_ratid_map(trans, ra_tid, txq_id);
  977. /* enable aggregations for the queue */
  978. iwl_scd_txq_enable_agg(trans, txq_id);
  979. txq->ampdu = true;
  980. } else {
  981. /*
  982. * disable aggregations for the queue, this will also
  983. * make the ra_tid mapping configuration irrelevant
  984. * since it is now a non-AGG queue.
  985. */
  986. iwl_scd_txq_disable_agg(trans, txq_id);
  987. ssn = txq->q.read_ptr;
  988. }
  989. }
  990. /* Place first TFD at index corresponding to start sequence number.
  991. * Assumes that ssn_idx is valid (!= 0xFFF) */
  992. txq->q.read_ptr = (ssn & 0xff);
  993. txq->q.write_ptr = (ssn & 0xff);
  994. iwl_write_direct32(trans, HBUS_TARG_WRPTR,
  995. (ssn & 0xff) | (txq_id << 8));
  996. if (cfg) {
  997. u8 frame_limit = cfg->frame_limit;
  998. iwl_write_prph(trans, SCD_QUEUE_RDPTR(txq_id), ssn);
  999. /* Set up Tx window size and frame limit for this queue */
  1000. iwl_trans_write_mem32(trans, trans_pcie->scd_base_addr +
  1001. SCD_CONTEXT_QUEUE_OFFSET(txq_id), 0);
  1002. iwl_trans_write_mem32(trans,
  1003. trans_pcie->scd_base_addr +
  1004. SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
  1005. ((frame_limit << SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  1006. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  1007. ((frame_limit << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  1008. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  1009. /* Set up status area in SRAM, map to Tx DMA/FIFO, activate */
  1010. iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id),
  1011. (1 << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  1012. (cfg->fifo << SCD_QUEUE_STTS_REG_POS_TXF) |
  1013. (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
  1014. SCD_QUEUE_STTS_REG_MSK);
  1015. /* enable the scheduler for this queue (only) */
  1016. if (txq_id == trans_pcie->cmd_queue &&
  1017. trans_pcie->scd_set_active)
  1018. iwl_scd_enable_set_active(trans, BIT(txq_id));
  1019. IWL_DEBUG_TX_QUEUES(trans,
  1020. "Activate queue %d on FIFO %d WrPtr: %d\n",
  1021. txq_id, fifo, ssn & 0xff);
  1022. } else {
  1023. IWL_DEBUG_TX_QUEUES(trans,
  1024. "Activate queue %d WrPtr: %d\n",
  1025. txq_id, ssn & 0xff);
  1026. }
  1027. txq->active = true;
  1028. }
  1029. void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int txq_id,
  1030. bool configure_scd)
  1031. {
  1032. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1033. u32 stts_addr = trans_pcie->scd_base_addr +
  1034. SCD_TX_STTS_QUEUE_OFFSET(txq_id);
  1035. static const u32 zero_val[4] = {};
  1036. trans_pcie->txq[txq_id].frozen_expiry_remainder = 0;
  1037. trans_pcie->txq[txq_id].frozen = false;
  1038. /*
  1039. * Upon HW Rfkill - we stop the device, and then stop the queues
  1040. * in the op_mode. Just for the sake of the simplicity of the op_mode,
  1041. * allow the op_mode to call txq_disable after it already called
  1042. * stop_device.
  1043. */
  1044. if (!test_and_clear_bit(txq_id, trans_pcie->queue_used)) {
  1045. WARN_ONCE(test_bit(STATUS_DEVICE_ENABLED, &trans->status),
  1046. "queue %d not used", txq_id);
  1047. return;
  1048. }
  1049. if (configure_scd) {
  1050. iwl_scd_txq_set_inactive(trans, txq_id);
  1051. iwl_trans_write_mem(trans, stts_addr, (void *)zero_val,
  1052. ARRAY_SIZE(zero_val));
  1053. }
  1054. iwl_pcie_txq_unmap(trans, txq_id);
  1055. trans_pcie->txq[txq_id].ampdu = false;
  1056. IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", txq_id);
  1057. }
  1058. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  1059. /*
  1060. * iwl_pcie_enqueue_hcmd - enqueue a uCode command
  1061. * @priv: device private data point
  1062. * @cmd: a pointer to the ucode command structure
  1063. *
  1064. * The function returns < 0 values to indicate the operation
  1065. * failed. On success, it returns the index (>= 0) of command in the
  1066. * command queue.
  1067. */
  1068. static int iwl_pcie_enqueue_hcmd(struct iwl_trans *trans,
  1069. struct iwl_host_cmd *cmd)
  1070. {
  1071. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1072. struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  1073. struct iwl_queue *q = &txq->q;
  1074. struct iwl_device_cmd *out_cmd;
  1075. struct iwl_cmd_meta *out_meta;
  1076. unsigned long flags;
  1077. void *dup_buf = NULL;
  1078. dma_addr_t phys_addr;
  1079. int idx;
  1080. u16 copy_size, cmd_size, scratch_size;
  1081. bool had_nocopy = false;
  1082. int i, ret;
  1083. u32 cmd_pos;
  1084. const u8 *cmddata[IWL_MAX_CMD_TBS_PER_TFD];
  1085. u16 cmdlen[IWL_MAX_CMD_TBS_PER_TFD];
  1086. copy_size = sizeof(out_cmd->hdr);
  1087. cmd_size = sizeof(out_cmd->hdr);
  1088. /* need one for the header if the first is NOCOPY */
  1089. BUILD_BUG_ON(IWL_MAX_CMD_TBS_PER_TFD > IWL_NUM_OF_TBS - 1);
  1090. for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
  1091. cmddata[i] = cmd->data[i];
  1092. cmdlen[i] = cmd->len[i];
  1093. if (!cmd->len[i])
  1094. continue;
  1095. /* need at least IWL_HCMD_SCRATCHBUF_SIZE copied */
  1096. if (copy_size < IWL_HCMD_SCRATCHBUF_SIZE) {
  1097. int copy = IWL_HCMD_SCRATCHBUF_SIZE - copy_size;
  1098. if (copy > cmdlen[i])
  1099. copy = cmdlen[i];
  1100. cmdlen[i] -= copy;
  1101. cmddata[i] += copy;
  1102. copy_size += copy;
  1103. }
  1104. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
  1105. had_nocopy = true;
  1106. if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
  1107. idx = -EINVAL;
  1108. goto free_dup_buf;
  1109. }
  1110. } else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
  1111. /*
  1112. * This is also a chunk that isn't copied
  1113. * to the static buffer so set had_nocopy.
  1114. */
  1115. had_nocopy = true;
  1116. /* only allowed once */
  1117. if (WARN_ON(dup_buf)) {
  1118. idx = -EINVAL;
  1119. goto free_dup_buf;
  1120. }
  1121. dup_buf = kmemdup(cmddata[i], cmdlen[i],
  1122. GFP_ATOMIC);
  1123. if (!dup_buf)
  1124. return -ENOMEM;
  1125. } else {
  1126. /* NOCOPY must not be followed by normal! */
  1127. if (WARN_ON(had_nocopy)) {
  1128. idx = -EINVAL;
  1129. goto free_dup_buf;
  1130. }
  1131. copy_size += cmdlen[i];
  1132. }
  1133. cmd_size += cmd->len[i];
  1134. }
  1135. /*
  1136. * If any of the command structures end up being larger than
  1137. * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
  1138. * allocated into separate TFDs, then we will need to
  1139. * increase the size of the buffers.
  1140. */
  1141. if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
  1142. "Command %s (%#x) is too large (%d bytes)\n",
  1143. get_cmd_string(trans_pcie, cmd->id), cmd->id, copy_size)) {
  1144. idx = -EINVAL;
  1145. goto free_dup_buf;
  1146. }
  1147. spin_lock_bh(&txq->lock);
  1148. if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
  1149. spin_unlock_bh(&txq->lock);
  1150. IWL_ERR(trans, "No space in command queue\n");
  1151. iwl_op_mode_cmd_queue_full(trans->op_mode);
  1152. idx = -ENOSPC;
  1153. goto free_dup_buf;
  1154. }
  1155. idx = get_cmd_index(q, q->write_ptr);
  1156. out_cmd = txq->entries[idx].cmd;
  1157. out_meta = &txq->entries[idx].meta;
  1158. memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
  1159. if (cmd->flags & CMD_WANT_SKB)
  1160. out_meta->source = cmd;
  1161. /* set up the header */
  1162. out_cmd->hdr.cmd = cmd->id;
  1163. out_cmd->hdr.flags = 0;
  1164. out_cmd->hdr.sequence =
  1165. cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->cmd_queue) |
  1166. INDEX_TO_SEQ(q->write_ptr));
  1167. /* and copy the data that needs to be copied */
  1168. cmd_pos = offsetof(struct iwl_device_cmd, payload);
  1169. copy_size = sizeof(out_cmd->hdr);
  1170. for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
  1171. int copy;
  1172. if (!cmd->len[i])
  1173. continue;
  1174. /* copy everything if not nocopy/dup */
  1175. if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
  1176. IWL_HCMD_DFL_DUP))) {
  1177. copy = cmd->len[i];
  1178. memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
  1179. cmd_pos += copy;
  1180. copy_size += copy;
  1181. continue;
  1182. }
  1183. /*
  1184. * Otherwise we need at least IWL_HCMD_SCRATCHBUF_SIZE copied
  1185. * in total (for the scratchbuf handling), but copy up to what
  1186. * we can fit into the payload for debug dump purposes.
  1187. */
  1188. copy = min_t(int, TFD_MAX_PAYLOAD_SIZE - cmd_pos, cmd->len[i]);
  1189. memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
  1190. cmd_pos += copy;
  1191. /* However, treat copy_size the proper way, we need it below */
  1192. if (copy_size < IWL_HCMD_SCRATCHBUF_SIZE) {
  1193. copy = IWL_HCMD_SCRATCHBUF_SIZE - copy_size;
  1194. if (copy > cmd->len[i])
  1195. copy = cmd->len[i];
  1196. copy_size += copy;
  1197. }
  1198. }
  1199. IWL_DEBUG_HC(trans,
  1200. "Sending command %s (#%x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
  1201. get_cmd_string(trans_pcie, out_cmd->hdr.cmd),
  1202. out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
  1203. cmd_size, q->write_ptr, idx, trans_pcie->cmd_queue);
  1204. /* start the TFD with the scratchbuf */
  1205. scratch_size = min_t(int, copy_size, IWL_HCMD_SCRATCHBUF_SIZE);
  1206. memcpy(&txq->scratchbufs[q->write_ptr], &out_cmd->hdr, scratch_size);
  1207. iwl_pcie_txq_build_tfd(trans, txq,
  1208. iwl_pcie_get_scratchbuf_dma(txq, q->write_ptr),
  1209. scratch_size, true);
  1210. /* map first command fragment, if any remains */
  1211. if (copy_size > scratch_size) {
  1212. phys_addr = dma_map_single(trans->dev,
  1213. ((u8 *)&out_cmd->hdr) + scratch_size,
  1214. copy_size - scratch_size,
  1215. DMA_TO_DEVICE);
  1216. if (dma_mapping_error(trans->dev, phys_addr)) {
  1217. iwl_pcie_tfd_unmap(trans, out_meta,
  1218. &txq->tfds[q->write_ptr]);
  1219. idx = -ENOMEM;
  1220. goto out;
  1221. }
  1222. iwl_pcie_txq_build_tfd(trans, txq, phys_addr,
  1223. copy_size - scratch_size, false);
  1224. }
  1225. /* map the remaining (adjusted) nocopy/dup fragments */
  1226. for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
  1227. const void *data = cmddata[i];
  1228. if (!cmdlen[i])
  1229. continue;
  1230. if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
  1231. IWL_HCMD_DFL_DUP)))
  1232. continue;
  1233. if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
  1234. data = dup_buf;
  1235. phys_addr = dma_map_single(trans->dev, (void *)data,
  1236. cmdlen[i], DMA_TO_DEVICE);
  1237. if (dma_mapping_error(trans->dev, phys_addr)) {
  1238. iwl_pcie_tfd_unmap(trans, out_meta,
  1239. &txq->tfds[q->write_ptr]);
  1240. idx = -ENOMEM;
  1241. goto out;
  1242. }
  1243. iwl_pcie_txq_build_tfd(trans, txq, phys_addr, cmdlen[i], false);
  1244. }
  1245. out_meta->flags = cmd->flags;
  1246. if (WARN_ON_ONCE(txq->entries[idx].free_buf))
  1247. kzfree(txq->entries[idx].free_buf);
  1248. txq->entries[idx].free_buf = dup_buf;
  1249. trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size, &out_cmd->hdr);
  1250. /* start timer if queue currently empty */
  1251. if (q->read_ptr == q->write_ptr && txq->wd_timeout)
  1252. mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
  1253. spin_lock_irqsave(&trans_pcie->reg_lock, flags);
  1254. ret = iwl_pcie_set_cmd_in_flight(trans, cmd);
  1255. if (ret < 0) {
  1256. idx = ret;
  1257. spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
  1258. goto out;
  1259. }
  1260. /* Increment and update queue's write index */
  1261. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr);
  1262. iwl_pcie_txq_inc_wr_ptr(trans, txq);
  1263. spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
  1264. out:
  1265. spin_unlock_bh(&txq->lock);
  1266. free_dup_buf:
  1267. if (idx < 0)
  1268. kfree(dup_buf);
  1269. return idx;
  1270. }
  1271. /*
  1272. * iwl_pcie_hcmd_complete - Pull unused buffers off the queue and reclaim them
  1273. * @rxb: Rx buffer to reclaim
  1274. * @handler_status: return value of the handler of the command
  1275. * (put in setup_rx_handlers)
  1276. *
  1277. * If an Rx buffer has an async callback associated with it the callback
  1278. * will be executed. The attached skb (if present) will only be freed
  1279. * if the callback returns 1
  1280. */
  1281. void iwl_pcie_hcmd_complete(struct iwl_trans *trans,
  1282. struct iwl_rx_cmd_buffer *rxb, int handler_status)
  1283. {
  1284. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1285. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  1286. int txq_id = SEQ_TO_QUEUE(sequence);
  1287. int index = SEQ_TO_INDEX(sequence);
  1288. int cmd_index;
  1289. struct iwl_device_cmd *cmd;
  1290. struct iwl_cmd_meta *meta;
  1291. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1292. struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  1293. /* If a Tx command is being handled and it isn't in the actual
  1294. * command queue then there a command routing bug has been introduced
  1295. * in the queue management code. */
  1296. if (WARN(txq_id != trans_pcie->cmd_queue,
  1297. "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
  1298. txq_id, trans_pcie->cmd_queue, sequence,
  1299. trans_pcie->txq[trans_pcie->cmd_queue].q.read_ptr,
  1300. trans_pcie->txq[trans_pcie->cmd_queue].q.write_ptr)) {
  1301. iwl_print_hex_error(trans, pkt, 32);
  1302. return;
  1303. }
  1304. spin_lock_bh(&txq->lock);
  1305. cmd_index = get_cmd_index(&txq->q, index);
  1306. cmd = txq->entries[cmd_index].cmd;
  1307. meta = &txq->entries[cmd_index].meta;
  1308. iwl_pcie_tfd_unmap(trans, meta, &txq->tfds[index]);
  1309. /* Input error checking is done when commands are added to queue. */
  1310. if (meta->flags & CMD_WANT_SKB) {
  1311. struct page *p = rxb_steal_page(rxb);
  1312. meta->source->resp_pkt = pkt;
  1313. meta->source->_rx_page_addr = (unsigned long)page_address(p);
  1314. meta->source->_rx_page_order = trans_pcie->rx_page_order;
  1315. meta->source->handler_status = handler_status;
  1316. }
  1317. iwl_pcie_cmdq_reclaim(trans, txq_id, index);
  1318. if (!(meta->flags & CMD_ASYNC)) {
  1319. if (!test_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status)) {
  1320. IWL_WARN(trans,
  1321. "HCMD_ACTIVE already clear for command %s\n",
  1322. get_cmd_string(trans_pcie, cmd->hdr.cmd));
  1323. }
  1324. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  1325. IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
  1326. get_cmd_string(trans_pcie, cmd->hdr.cmd));
  1327. wake_up(&trans_pcie->wait_command_queue);
  1328. }
  1329. meta->flags = 0;
  1330. spin_unlock_bh(&txq->lock);
  1331. }
  1332. #define HOST_COMPLETE_TIMEOUT (2 * HZ)
  1333. static int iwl_pcie_send_hcmd_async(struct iwl_trans *trans,
  1334. struct iwl_host_cmd *cmd)
  1335. {
  1336. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1337. int ret;
  1338. /* An asynchronous command can not expect an SKB to be set. */
  1339. if (WARN_ON(cmd->flags & CMD_WANT_SKB))
  1340. return -EINVAL;
  1341. ret = iwl_pcie_enqueue_hcmd(trans, cmd);
  1342. if (ret < 0) {
  1343. IWL_ERR(trans,
  1344. "Error sending %s: enqueue_hcmd failed: %d\n",
  1345. get_cmd_string(trans_pcie, cmd->id), ret);
  1346. return ret;
  1347. }
  1348. return 0;
  1349. }
  1350. static int iwl_pcie_send_hcmd_sync(struct iwl_trans *trans,
  1351. struct iwl_host_cmd *cmd)
  1352. {
  1353. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1354. int cmd_idx;
  1355. int ret;
  1356. IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n",
  1357. get_cmd_string(trans_pcie, cmd->id));
  1358. if (WARN(test_and_set_bit(STATUS_SYNC_HCMD_ACTIVE,
  1359. &trans->status),
  1360. "Command %s: a command is already active!\n",
  1361. get_cmd_string(trans_pcie, cmd->id)))
  1362. return -EIO;
  1363. IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n",
  1364. get_cmd_string(trans_pcie, cmd->id));
  1365. cmd_idx = iwl_pcie_enqueue_hcmd(trans, cmd);
  1366. if (cmd_idx < 0) {
  1367. ret = cmd_idx;
  1368. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  1369. IWL_ERR(trans,
  1370. "Error sending %s: enqueue_hcmd failed: %d\n",
  1371. get_cmd_string(trans_pcie, cmd->id), ret);
  1372. return ret;
  1373. }
  1374. ret = wait_event_timeout(trans_pcie->wait_command_queue,
  1375. !test_bit(STATUS_SYNC_HCMD_ACTIVE,
  1376. &trans->status),
  1377. HOST_COMPLETE_TIMEOUT);
  1378. if (!ret) {
  1379. struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  1380. struct iwl_queue *q = &txq->q;
  1381. IWL_ERR(trans, "Error sending %s: time out after %dms.\n",
  1382. get_cmd_string(trans_pcie, cmd->id),
  1383. jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
  1384. IWL_ERR(trans, "Current CMD queue read_ptr %d write_ptr %d\n",
  1385. q->read_ptr, q->write_ptr);
  1386. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  1387. IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
  1388. get_cmd_string(trans_pcie, cmd->id));
  1389. ret = -ETIMEDOUT;
  1390. iwl_force_nmi(trans);
  1391. iwl_trans_fw_error(trans);
  1392. goto cancel;
  1393. }
  1394. if (test_bit(STATUS_FW_ERROR, &trans->status)) {
  1395. IWL_ERR(trans, "FW error in SYNC CMD %s\n",
  1396. get_cmd_string(trans_pcie, cmd->id));
  1397. dump_stack();
  1398. ret = -EIO;
  1399. goto cancel;
  1400. }
  1401. if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
  1402. test_bit(STATUS_RFKILL, &trans->status)) {
  1403. IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n");
  1404. ret = -ERFKILL;
  1405. goto cancel;
  1406. }
  1407. if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) {
  1408. IWL_ERR(trans, "Error: Response NULL in '%s'\n",
  1409. get_cmd_string(trans_pcie, cmd->id));
  1410. ret = -EIO;
  1411. goto cancel;
  1412. }
  1413. return 0;
  1414. cancel:
  1415. if (cmd->flags & CMD_WANT_SKB) {
  1416. /*
  1417. * Cancel the CMD_WANT_SKB flag for the cmd in the
  1418. * TX cmd queue. Otherwise in case the cmd comes
  1419. * in later, it will possibly set an invalid
  1420. * address (cmd->meta.source).
  1421. */
  1422. trans_pcie->txq[trans_pcie->cmd_queue].
  1423. entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB;
  1424. }
  1425. if (cmd->resp_pkt) {
  1426. iwl_free_resp(cmd);
  1427. cmd->resp_pkt = NULL;
  1428. }
  1429. return ret;
  1430. }
  1431. int iwl_trans_pcie_send_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  1432. {
  1433. if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
  1434. test_bit(STATUS_RFKILL, &trans->status)) {
  1435. IWL_DEBUG_RF_KILL(trans, "Dropping CMD 0x%x: RF KILL\n",
  1436. cmd->id);
  1437. return -ERFKILL;
  1438. }
  1439. if (cmd->flags & CMD_ASYNC)
  1440. return iwl_pcie_send_hcmd_async(trans, cmd);
  1441. /* We still can fail on RFKILL that can be asserted while we wait */
  1442. return iwl_pcie_send_hcmd_sync(trans, cmd);
  1443. }
  1444. int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
  1445. struct iwl_device_cmd *dev_cmd, int txq_id)
  1446. {
  1447. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1448. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  1449. struct iwl_tx_cmd *tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  1450. struct iwl_cmd_meta *out_meta;
  1451. struct iwl_txq *txq;
  1452. struct iwl_queue *q;
  1453. dma_addr_t tb0_phys, tb1_phys, scratch_phys;
  1454. void *tb1_addr;
  1455. u16 len, tb1_len, tb2_len;
  1456. bool wait_write_ptr;
  1457. __le16 fc = hdr->frame_control;
  1458. u8 hdr_len = ieee80211_hdrlen(fc);
  1459. u16 wifi_seq;
  1460. txq = &trans_pcie->txq[txq_id];
  1461. q = &txq->q;
  1462. if (WARN_ONCE(!test_bit(txq_id, trans_pcie->queue_used),
  1463. "TX on unused queue %d\n", txq_id))
  1464. return -EINVAL;
  1465. spin_lock(&txq->lock);
  1466. /* In AGG mode, the index in the ring must correspond to the WiFi
  1467. * sequence number. This is a HW requirements to help the SCD to parse
  1468. * the BA.
  1469. * Check here that the packets are in the right place on the ring.
  1470. */
  1471. wifi_seq = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
  1472. WARN_ONCE(txq->ampdu &&
  1473. (wifi_seq & 0xff) != q->write_ptr,
  1474. "Q: %d WiFi Seq %d tfdNum %d",
  1475. txq_id, wifi_seq, q->write_ptr);
  1476. /* Set up driver data for this TFD */
  1477. txq->entries[q->write_ptr].skb = skb;
  1478. txq->entries[q->write_ptr].cmd = dev_cmd;
  1479. dev_cmd->hdr.sequence =
  1480. cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  1481. INDEX_TO_SEQ(q->write_ptr)));
  1482. tb0_phys = iwl_pcie_get_scratchbuf_dma(txq, q->write_ptr);
  1483. scratch_phys = tb0_phys + sizeof(struct iwl_cmd_header) +
  1484. offsetof(struct iwl_tx_cmd, scratch);
  1485. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  1486. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  1487. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  1488. out_meta = &txq->entries[q->write_ptr].meta;
  1489. /*
  1490. * The second TB (tb1) points to the remainder of the TX command
  1491. * and the 802.11 header - dword aligned size
  1492. * (This calculation modifies the TX command, so do it before the
  1493. * setup of the first TB)
  1494. */
  1495. len = sizeof(struct iwl_tx_cmd) + sizeof(struct iwl_cmd_header) +
  1496. hdr_len - IWL_HCMD_SCRATCHBUF_SIZE;
  1497. tb1_len = ALIGN(len, 4);
  1498. /* Tell NIC about any 2-byte padding after MAC header */
  1499. if (tb1_len != len)
  1500. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  1501. /* The first TB points to the scratchbuf data - min_copy bytes */
  1502. memcpy(&txq->scratchbufs[q->write_ptr], &dev_cmd->hdr,
  1503. IWL_HCMD_SCRATCHBUF_SIZE);
  1504. iwl_pcie_txq_build_tfd(trans, txq, tb0_phys,
  1505. IWL_HCMD_SCRATCHBUF_SIZE, true);
  1506. /* there must be data left over for TB1 or this code must be changed */
  1507. BUILD_BUG_ON(sizeof(struct iwl_tx_cmd) < IWL_HCMD_SCRATCHBUF_SIZE);
  1508. /* map the data for TB1 */
  1509. tb1_addr = ((u8 *)&dev_cmd->hdr) + IWL_HCMD_SCRATCHBUF_SIZE;
  1510. tb1_phys = dma_map_single(trans->dev, tb1_addr, tb1_len, DMA_TO_DEVICE);
  1511. if (unlikely(dma_mapping_error(trans->dev, tb1_phys)))
  1512. goto out_err;
  1513. iwl_pcie_txq_build_tfd(trans, txq, tb1_phys, tb1_len, false);
  1514. /*
  1515. * Set up TFD's third entry to point directly to remainder
  1516. * of skb, if any (802.11 null frames have no payload).
  1517. */
  1518. tb2_len = skb->len - hdr_len;
  1519. if (tb2_len > 0) {
  1520. dma_addr_t tb2_phys = dma_map_single(trans->dev,
  1521. skb->data + hdr_len,
  1522. tb2_len, DMA_TO_DEVICE);
  1523. if (unlikely(dma_mapping_error(trans->dev, tb2_phys))) {
  1524. iwl_pcie_tfd_unmap(trans, out_meta,
  1525. &txq->tfds[q->write_ptr]);
  1526. goto out_err;
  1527. }
  1528. iwl_pcie_txq_build_tfd(trans, txq, tb2_phys, tb2_len, false);
  1529. }
  1530. /* Set up entry for this TFD in Tx byte-count array */
  1531. iwl_pcie_txq_update_byte_cnt_tbl(trans, txq, le16_to_cpu(tx_cmd->len));
  1532. trace_iwlwifi_dev_tx(trans->dev, skb,
  1533. &txq->tfds[txq->q.write_ptr],
  1534. sizeof(struct iwl_tfd),
  1535. &dev_cmd->hdr, IWL_HCMD_SCRATCHBUF_SIZE + tb1_len,
  1536. skb->data + hdr_len, tb2_len);
  1537. trace_iwlwifi_dev_tx_data(trans->dev, skb,
  1538. skb->data + hdr_len, tb2_len);
  1539. wait_write_ptr = ieee80211_has_morefrags(fc);
  1540. /* start timer if queue currently empty */
  1541. if (q->read_ptr == q->write_ptr) {
  1542. if (txq->wd_timeout)
  1543. mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
  1544. IWL_DEBUG_RPM(trans, "Q: %d first tx - take ref\n", q->id);
  1545. iwl_trans_pcie_ref(trans);
  1546. }
  1547. /* Tell device the write index *just past* this latest filled TFD */
  1548. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr);
  1549. if (!wait_write_ptr)
  1550. iwl_pcie_txq_inc_wr_ptr(trans, txq);
  1551. /*
  1552. * At this point the frame is "transmitted" successfully
  1553. * and we will get a TX status notification eventually.
  1554. */
  1555. if (iwl_queue_space(q) < q->high_mark) {
  1556. if (wait_write_ptr)
  1557. iwl_pcie_txq_inc_wr_ptr(trans, txq);
  1558. else
  1559. iwl_stop_queue(trans, txq);
  1560. }
  1561. spin_unlock(&txq->lock);
  1562. return 0;
  1563. out_err:
  1564. spin_unlock(&txq->lock);
  1565. return -1;
  1566. }