tx.c 49 KB

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