tx.c 49 KB

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