tx.c 47 KB

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