tx.c 55 KB

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