tx.c 47 KB

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