tx.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2014 Intel Corporation. All rights reserved.
  4. * Copyright(c) 2013 - 2015 Intel Mobile Communications GmbH
  5. * Copyright(c) 2016 Intel Deutschland GmbH
  6. *
  7. * Portions of this file are derived from the ipw3945 project, as well
  8. * as portions of the ieee80211 subsystem header files.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms of version 2 of the GNU General Public License as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * this program; if not, write to the Free Software Foundation, Inc.,
  21. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  22. *
  23. * The full GNU General Public License is included in this distribution in the
  24. * file called LICENSE.
  25. *
  26. * Contact Information:
  27. * Intel Linux Wireless <linuxwifi@intel.com>
  28. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  29. *
  30. *****************************************************************************/
  31. #include <linux/etherdevice.h>
  32. #include <linux/ieee80211.h>
  33. #include <linux/slab.h>
  34. #include <linux/sched.h>
  35. #include <net/ip6_checksum.h>
  36. #include <net/tso.h>
  37. #include "iwl-debug.h"
  38. #include "iwl-csr.h"
  39. #include "iwl-prph.h"
  40. #include "iwl-io.h"
  41. #include "iwl-scd.h"
  42. #include "iwl-op-mode.h"
  43. #include "internal.h"
  44. /* FIXME: need to abstract out TX command (once we know what it looks like) */
  45. #include "dvm/commands.h"
  46. #define IWL_TX_CRC_SIZE 4
  47. #define IWL_TX_DELIMITER_SIZE 4
  48. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  49. * DMA services
  50. *
  51. * Theory of operation
  52. *
  53. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  54. * of buffer descriptors, each of which points to one or more data buffers for
  55. * the device to read from or fill. Driver and device exchange status of each
  56. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  57. * entries in each circular buffer, to protect against confusing empty and full
  58. * queue states.
  59. *
  60. * The device reads or writes the data in the queues via the device's several
  61. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  62. *
  63. * For Tx queue, there are low mark and high mark limits. If, after queuing
  64. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  65. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  66. * Tx queue resumed.
  67. *
  68. ***************************************************/
  69. static int iwl_queue_space(const struct iwl_queue *q)
  70. {
  71. unsigned int max;
  72. unsigned int used;
  73. /*
  74. * To avoid ambiguity between empty and completely full queues, there
  75. * should always be less than TFD_QUEUE_SIZE_MAX elements in the queue.
  76. * If q->n_window is smaller than TFD_QUEUE_SIZE_MAX, there is no need
  77. * to reserve any queue entries for this purpose.
  78. */
  79. if (q->n_window < TFD_QUEUE_SIZE_MAX)
  80. max = q->n_window;
  81. else
  82. max = TFD_QUEUE_SIZE_MAX - 1;
  83. /*
  84. * TFD_QUEUE_SIZE_MAX is a power of 2, so the following is equivalent to
  85. * modulo by TFD_QUEUE_SIZE_MAX and is well defined.
  86. */
  87. used = (q->write_ptr - q->read_ptr) & (TFD_QUEUE_SIZE_MAX - 1);
  88. if (WARN_ON(used > max))
  89. return 0;
  90. return max - used;
  91. }
  92. /*
  93. * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
  94. */
  95. static int iwl_queue_init(struct iwl_queue *q, int slots_num, u32 id)
  96. {
  97. q->n_window = slots_num;
  98. q->id = id;
  99. /* slots_num must be power-of-two size, otherwise
  100. * get_cmd_index is broken. */
  101. if (WARN_ON(!is_power_of_2(slots_num)))
  102. return -EINVAL;
  103. q->low_mark = q->n_window / 4;
  104. if (q->low_mark < 4)
  105. q->low_mark = 4;
  106. q->high_mark = q->n_window / 8;
  107. if (q->high_mark < 2)
  108. q->high_mark = 2;
  109. q->write_ptr = 0;
  110. q->read_ptr = 0;
  111. return 0;
  112. }
  113. static int iwl_pcie_alloc_dma_ptr(struct iwl_trans *trans,
  114. struct iwl_dma_ptr *ptr, size_t size)
  115. {
  116. if (WARN_ON(ptr->addr))
  117. return -EINVAL;
  118. ptr->addr = dma_alloc_coherent(trans->dev, size,
  119. &ptr->dma, GFP_KERNEL);
  120. if (!ptr->addr)
  121. return -ENOMEM;
  122. ptr->size = size;
  123. return 0;
  124. }
  125. static void iwl_pcie_free_dma_ptr(struct iwl_trans *trans,
  126. struct iwl_dma_ptr *ptr)
  127. {
  128. if (unlikely(!ptr->addr))
  129. return;
  130. dma_free_coherent(trans->dev, ptr->size, ptr->addr, ptr->dma);
  131. memset(ptr, 0, sizeof(*ptr));
  132. }
  133. static void iwl_pcie_txq_stuck_timer(unsigned long data)
  134. {
  135. struct iwl_txq *txq = (void *)data;
  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(txq->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, SCD_QUEUE_RDPTR(i)) &
  174. (TFD_QUEUE_SIZE_MAX - 1),
  175. iwl_read_prph(trans, SCD_QUEUE_WRPTR(i)));
  176. }
  177. iwl_force_nmi(trans);
  178. }
  179. /*
  180. * iwl_pcie_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
  181. */
  182. static void iwl_pcie_txq_update_byte_cnt_tbl(struct iwl_trans *trans,
  183. struct iwl_txq *txq, u16 byte_cnt)
  184. {
  185. struct iwlagn_scd_bc_tbl *scd_bc_tbl;
  186. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  187. int write_ptr = txq->q.write_ptr;
  188. int txq_id = txq->q.id;
  189. u8 sec_ctl = 0;
  190. u8 sta_id = 0;
  191. u16 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
  192. __le16 bc_ent;
  193. struct iwl_tx_cmd *tx_cmd =
  194. (void *) txq->entries[txq->q.write_ptr].cmd->payload;
  195. scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
  196. sta_id = tx_cmd->sta_id;
  197. sec_ctl = tx_cmd->sec_ctl;
  198. switch (sec_ctl & TX_CMD_SEC_MSK) {
  199. case TX_CMD_SEC_CCM:
  200. len += IEEE80211_CCMP_MIC_LEN;
  201. break;
  202. case TX_CMD_SEC_TKIP:
  203. len += IEEE80211_TKIP_ICV_LEN;
  204. break;
  205. case TX_CMD_SEC_WEP:
  206. len += IEEE80211_WEP_IV_LEN + IEEE80211_WEP_ICV_LEN;
  207. break;
  208. }
  209. if (trans_pcie->bc_table_dword)
  210. len = DIV_ROUND_UP(len, 4);
  211. if (WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX))
  212. return;
  213. bc_ent = cpu_to_le16(len | (sta_id << 12));
  214. scd_bc_tbl[txq_id].tfd_offset[write_ptr] = bc_ent;
  215. if (write_ptr < TFD_QUEUE_SIZE_BC_DUP)
  216. scd_bc_tbl[txq_id].
  217. tfd_offset[TFD_QUEUE_SIZE_MAX + write_ptr] = bc_ent;
  218. }
  219. static void iwl_pcie_txq_inval_byte_cnt_tbl(struct iwl_trans *trans,
  220. struct iwl_txq *txq)
  221. {
  222. struct iwl_trans_pcie *trans_pcie =
  223. IWL_TRANS_GET_PCIE_TRANS(trans);
  224. struct iwlagn_scd_bc_tbl *scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
  225. int txq_id = txq->q.id;
  226. int read_ptr = txq->q.read_ptr;
  227. u8 sta_id = 0;
  228. __le16 bc_ent;
  229. struct iwl_tx_cmd *tx_cmd =
  230. (void *)txq->entries[txq->q.read_ptr].cmd->payload;
  231. WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX);
  232. if (txq_id != trans_pcie->cmd_queue)
  233. sta_id = tx_cmd->sta_id;
  234. bc_ent = cpu_to_le16(1 | (sta_id << 12));
  235. scd_bc_tbl[txq_id].tfd_offset[read_ptr] = bc_ent;
  236. if (read_ptr < TFD_QUEUE_SIZE_BC_DUP)
  237. scd_bc_tbl[txq_id].
  238. tfd_offset[TFD_QUEUE_SIZE_MAX + read_ptr] = bc_ent;
  239. }
  240. /*
  241. * iwl_pcie_txq_inc_wr_ptr - Send new write index to hardware
  242. */
  243. static void iwl_pcie_txq_inc_wr_ptr(struct iwl_trans *trans,
  244. struct iwl_txq *txq)
  245. {
  246. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  247. u32 reg = 0;
  248. int txq_id = txq->q.id;
  249. lockdep_assert_held(&txq->lock);
  250. /*
  251. * explicitly wake up the NIC if:
  252. * 1. shadow registers aren't enabled
  253. * 2. NIC is woken up for CMD regardless of shadow outside this function
  254. * 3. there is a chance that the NIC is asleep
  255. */
  256. if (!trans->cfg->base_params->shadow_reg_enable &&
  257. txq_id != trans_pcie->cmd_queue &&
  258. test_bit(STATUS_TPOWER_PMI, &trans->status)) {
  259. /*
  260. * wake up nic if it's powered down ...
  261. * uCode will wake up, and interrupt us again, so next
  262. * time we'll skip this part.
  263. */
  264. reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
  265. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  266. IWL_DEBUG_INFO(trans, "Tx queue %d requesting wakeup, GP1 = 0x%x\n",
  267. txq_id, reg);
  268. iwl_set_bit(trans, CSR_GP_CNTRL,
  269. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  270. txq->need_update = true;
  271. return;
  272. }
  273. }
  274. /*
  275. * if not in power-save mode, uCode will never sleep when we're
  276. * trying to tx (during RFKILL, we're not trying to tx).
  277. */
  278. IWL_DEBUG_TX(trans, "Q:%d WR: 0x%x\n", txq_id, txq->q.write_ptr);
  279. if (!txq->block)
  280. iwl_write32(trans, HBUS_TARG_WRPTR,
  281. txq->q.write_ptr | (txq_id << 8));
  282. }
  283. void iwl_pcie_txq_check_wrptrs(struct iwl_trans *trans)
  284. {
  285. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  286. int i;
  287. for (i = 0; i < trans->cfg->base_params->num_of_queues; i++) {
  288. struct iwl_txq *txq = &trans_pcie->txq[i];
  289. spin_lock_bh(&txq->lock);
  290. if (trans_pcie->txq[i].need_update) {
  291. iwl_pcie_txq_inc_wr_ptr(trans, txq);
  292. trans_pcie->txq[i].need_update = false;
  293. }
  294. spin_unlock_bh(&txq->lock);
  295. }
  296. }
  297. static inline dma_addr_t iwl_pcie_tfd_tb_get_addr(struct iwl_tfd *tfd, u8 idx)
  298. {
  299. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  300. dma_addr_t addr = get_unaligned_le32(&tb->lo);
  301. if (sizeof(dma_addr_t) > sizeof(u32))
  302. addr |=
  303. ((dma_addr_t)(le16_to_cpu(tb->hi_n_len) & 0xF) << 16) << 16;
  304. return addr;
  305. }
  306. static inline void iwl_pcie_tfd_set_tb(struct iwl_tfd *tfd, u8 idx,
  307. dma_addr_t addr, u16 len)
  308. {
  309. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  310. u16 hi_n_len = len << 4;
  311. put_unaligned_le32(addr, &tb->lo);
  312. if (sizeof(dma_addr_t) > sizeof(u32))
  313. hi_n_len |= ((addr >> 16) >> 16) & 0xF;
  314. tb->hi_n_len = cpu_to_le16(hi_n_len);
  315. tfd->num_tbs = idx + 1;
  316. }
  317. static inline u8 iwl_pcie_tfd_get_num_tbs(struct iwl_tfd *tfd)
  318. {
  319. return tfd->num_tbs & 0x1f;
  320. }
  321. static void iwl_pcie_tfd_unmap(struct iwl_trans *trans,
  322. struct iwl_cmd_meta *meta,
  323. struct iwl_tfd *tfd)
  324. {
  325. int i;
  326. int num_tbs;
  327. /* Sanity check on number of chunks */
  328. num_tbs = iwl_pcie_tfd_get_num_tbs(tfd);
  329. if (num_tbs >= IWL_NUM_OF_TBS) {
  330. IWL_ERR(trans, "Too many chunks: %i\n", num_tbs);
  331. /* @todo issue fatal error, it is quite serious situation */
  332. return;
  333. }
  334. /* first TB is never freed - it's the scratchbuf data */
  335. for (i = 1; i < num_tbs; i++) {
  336. if (meta->flags & BIT(i + CMD_TB_BITMAP_POS))
  337. dma_unmap_page(trans->dev,
  338. iwl_pcie_tfd_tb_get_addr(tfd, i),
  339. iwl_pcie_tfd_tb_get_len(tfd, i),
  340. DMA_TO_DEVICE);
  341. else
  342. dma_unmap_single(trans->dev,
  343. iwl_pcie_tfd_tb_get_addr(tfd, i),
  344. iwl_pcie_tfd_tb_get_len(tfd, i),
  345. DMA_TO_DEVICE);
  346. }
  347. tfd->num_tbs = 0;
  348. }
  349. /*
  350. * iwl_pcie_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
  351. * @trans - transport private data
  352. * @txq - tx queue
  353. * @dma_dir - the direction of the DMA mapping
  354. *
  355. * Does NOT advance any TFD circular buffer read/write indexes
  356. * Does NOT free the TFD itself (which is within circular buffer)
  357. */
  358. static void iwl_pcie_txq_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq)
  359. {
  360. struct iwl_tfd *tfd_tmp = txq->tfds;
  361. /* rd_ptr is bounded by TFD_QUEUE_SIZE_MAX and
  362. * idx is bounded by n_window
  363. */
  364. int rd_ptr = txq->q.read_ptr;
  365. int idx = get_cmd_index(&txq->q, rd_ptr);
  366. lockdep_assert_held(&txq->lock);
  367. /* We have only q->n_window txq->entries, but we use
  368. * TFD_QUEUE_SIZE_MAX tfds
  369. */
  370. iwl_pcie_tfd_unmap(trans, &txq->entries[idx].meta, &tfd_tmp[rd_ptr]);
  371. /* free SKB */
  372. if (txq->entries) {
  373. struct sk_buff *skb;
  374. skb = txq->entries[idx].skb;
  375. /* Can be called from irqs-disabled context
  376. * If skb is not NULL, it means that the whole queue is being
  377. * freed and that the queue is not empty - free the skb
  378. */
  379. if (skb) {
  380. iwl_op_mode_free_skb(trans->op_mode, skb);
  381. txq->entries[idx].skb = NULL;
  382. }
  383. }
  384. }
  385. static int iwl_pcie_txq_build_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
  386. dma_addr_t addr, u16 len, bool reset)
  387. {
  388. struct iwl_queue *q;
  389. struct iwl_tfd *tfd, *tfd_tmp;
  390. u32 num_tbs;
  391. q = &txq->q;
  392. tfd_tmp = txq->tfds;
  393. tfd = &tfd_tmp[q->write_ptr];
  394. if (reset)
  395. memset(tfd, 0, sizeof(*tfd));
  396. num_tbs = iwl_pcie_tfd_get_num_tbs(tfd);
  397. /* Each TFD can point to a maximum 20 Tx buffers */
  398. if (num_tbs >= IWL_NUM_OF_TBS) {
  399. IWL_ERR(trans, "Error can not send more than %d chunks\n",
  400. IWL_NUM_OF_TBS);
  401. return -EINVAL;
  402. }
  403. if (WARN(addr & ~IWL_TX_DMA_MASK,
  404. "Unaligned address = %llx\n", (unsigned long long)addr))
  405. return -EINVAL;
  406. iwl_pcie_tfd_set_tb(tfd, num_tbs, addr, len);
  407. return num_tbs;
  408. }
  409. static int iwl_pcie_txq_alloc(struct iwl_trans *trans,
  410. struct iwl_txq *txq, int slots_num,
  411. u32 txq_id)
  412. {
  413. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  414. size_t tfd_sz = sizeof(struct iwl_tfd) * TFD_QUEUE_SIZE_MAX;
  415. size_t scratchbuf_sz;
  416. int i;
  417. if (WARN_ON(txq->entries || txq->tfds))
  418. return -EINVAL;
  419. setup_timer(&txq->stuck_timer, iwl_pcie_txq_stuck_timer,
  420. (unsigned long)txq);
  421. txq->trans_pcie = trans_pcie;
  422. txq->q.n_window = slots_num;
  423. txq->entries = kcalloc(slots_num,
  424. sizeof(struct iwl_pcie_txq_entry),
  425. GFP_KERNEL);
  426. if (!txq->entries)
  427. goto error;
  428. if (txq_id == trans_pcie->cmd_queue)
  429. for (i = 0; i < slots_num; i++) {
  430. txq->entries[i].cmd =
  431. kmalloc(sizeof(struct iwl_device_cmd),
  432. GFP_KERNEL);
  433. if (!txq->entries[i].cmd)
  434. goto error;
  435. }
  436. /* Circular buffer of transmit frame descriptors (TFDs),
  437. * shared with device */
  438. txq->tfds = dma_alloc_coherent(trans->dev, tfd_sz,
  439. &txq->q.dma_addr, GFP_KERNEL);
  440. if (!txq->tfds)
  441. goto error;
  442. BUILD_BUG_ON(IWL_HCMD_SCRATCHBUF_SIZE != sizeof(*txq->scratchbufs));
  443. BUILD_BUG_ON(offsetof(struct iwl_pcie_txq_scratch_buf, scratch) !=
  444. sizeof(struct iwl_cmd_header) +
  445. offsetof(struct iwl_tx_cmd, scratch));
  446. scratchbuf_sz = sizeof(*txq->scratchbufs) * slots_num;
  447. txq->scratchbufs = dma_alloc_coherent(trans->dev, scratchbuf_sz,
  448. &txq->scratchbufs_dma,
  449. GFP_KERNEL);
  450. if (!txq->scratchbufs)
  451. goto err_free_tfds;
  452. txq->q.id = txq_id;
  453. return 0;
  454. err_free_tfds:
  455. dma_free_coherent(trans->dev, tfd_sz, txq->tfds, txq->q.dma_addr);
  456. error:
  457. if (txq->entries && txq_id == trans_pcie->cmd_queue)
  458. for (i = 0; i < slots_num; i++)
  459. kfree(txq->entries[i].cmd);
  460. kfree(txq->entries);
  461. txq->entries = NULL;
  462. return -ENOMEM;
  463. }
  464. static int iwl_pcie_txq_init(struct iwl_trans *trans, struct iwl_txq *txq,
  465. int slots_num, u32 txq_id)
  466. {
  467. int ret;
  468. txq->need_update = false;
  469. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  470. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  471. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  472. /* Initialize queue's high/low-water marks, and head/tail indexes */
  473. ret = iwl_queue_init(&txq->q, slots_num, txq_id);
  474. if (ret)
  475. return ret;
  476. spin_lock_init(&txq->lock);
  477. __skb_queue_head_init(&txq->overflow_q);
  478. /*
  479. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  480. * given Tx queue, and enable the DMA channel used for that queue.
  481. * Circular buffer (TFD queue in DRAM) physical base address */
  482. iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(txq_id),
  483. txq->q.dma_addr >> 8);
  484. return 0;
  485. }
  486. static void iwl_pcie_free_tso_page(struct sk_buff *skb)
  487. {
  488. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  489. if (info->driver_data[IWL_TRANS_FIRST_DRIVER_DATA]) {
  490. struct page *page =
  491. info->driver_data[IWL_TRANS_FIRST_DRIVER_DATA];
  492. __free_page(page);
  493. info->driver_data[IWL_TRANS_FIRST_DRIVER_DATA] = NULL;
  494. }
  495. }
  496. /*
  497. * iwl_pcie_txq_unmap - Unmap any remaining DMA mappings and free skb's
  498. */
  499. static void iwl_pcie_txq_unmap(struct iwl_trans *trans, int txq_id)
  500. {
  501. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  502. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  503. struct iwl_queue *q = &txq->q;
  504. spin_lock_bh(&txq->lock);
  505. while (q->write_ptr != q->read_ptr) {
  506. IWL_DEBUG_TX_REPLY(trans, "Q %d Free %d\n",
  507. txq_id, q->read_ptr);
  508. if (txq_id != trans_pcie->cmd_queue) {
  509. struct sk_buff *skb = txq->entries[q->read_ptr].skb;
  510. if (WARN_ON_ONCE(!skb))
  511. continue;
  512. iwl_pcie_free_tso_page(skb);
  513. }
  514. iwl_pcie_txq_free_tfd(trans, txq);
  515. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr);
  516. }
  517. txq->active = false;
  518. while (!skb_queue_empty(&txq->overflow_q)) {
  519. struct sk_buff *skb = __skb_dequeue(&txq->overflow_q);
  520. iwl_op_mode_free_skb(trans->op_mode, skb);
  521. }
  522. spin_unlock_bh(&txq->lock);
  523. /* just in case - this queue may have been stopped */
  524. iwl_wake_queue(trans, txq);
  525. }
  526. /*
  527. * iwl_pcie_txq_free - Deallocate DMA queue.
  528. * @txq: Transmit queue to deallocate.
  529. *
  530. * Empty queue by removing and destroying all BD's.
  531. * Free all buffers.
  532. * 0-fill, but do not free "txq" descriptor structure.
  533. */
  534. static void iwl_pcie_txq_free(struct iwl_trans *trans, int txq_id)
  535. {
  536. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  537. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  538. struct device *dev = trans->dev;
  539. int i;
  540. if (WARN_ON(!txq))
  541. return;
  542. iwl_pcie_txq_unmap(trans, txq_id);
  543. /* De-alloc array of command/tx buffers */
  544. if (txq_id == trans_pcie->cmd_queue)
  545. for (i = 0; i < txq->q.n_window; i++) {
  546. kzfree(txq->entries[i].cmd);
  547. kzfree(txq->entries[i].free_buf);
  548. }
  549. /* De-alloc circular buffer of TFDs */
  550. if (txq->tfds) {
  551. dma_free_coherent(dev,
  552. sizeof(struct iwl_tfd) * TFD_QUEUE_SIZE_MAX,
  553. txq->tfds, txq->q.dma_addr);
  554. txq->q.dma_addr = 0;
  555. txq->tfds = NULL;
  556. dma_free_coherent(dev,
  557. sizeof(*txq->scratchbufs) * txq->q.n_window,
  558. txq->scratchbufs, txq->scratchbufs_dma);
  559. }
  560. kfree(txq->entries);
  561. txq->entries = NULL;
  562. del_timer_sync(&txq->stuck_timer);
  563. /* 0-fill queue descriptor structure */
  564. memset(txq, 0, sizeof(*txq));
  565. }
  566. void iwl_pcie_tx_start(struct iwl_trans *trans, u32 scd_base_addr)
  567. {
  568. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  569. int nq = trans->cfg->base_params->num_of_queues;
  570. int chan;
  571. u32 reg_val;
  572. int clear_dwords = (SCD_TRANS_TBL_OFFSET_QUEUE(nq) -
  573. SCD_CONTEXT_MEM_LOWER_BOUND) / sizeof(u32);
  574. /* make sure all queue are not stopped/used */
  575. memset(trans_pcie->queue_stopped, 0, sizeof(trans_pcie->queue_stopped));
  576. memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
  577. trans_pcie->scd_base_addr =
  578. iwl_read_prph(trans, SCD_SRAM_BASE_ADDR);
  579. WARN_ON(scd_base_addr != 0 &&
  580. scd_base_addr != trans_pcie->scd_base_addr);
  581. /* reset context data, TX status and translation data */
  582. iwl_trans_write_mem(trans, trans_pcie->scd_base_addr +
  583. SCD_CONTEXT_MEM_LOWER_BOUND,
  584. NULL, clear_dwords);
  585. iwl_write_prph(trans, SCD_DRAM_BASE_ADDR,
  586. trans_pcie->scd_bc_tbls.dma >> 10);
  587. /* The chain extension of the SCD doesn't work well. This feature is
  588. * enabled by default by the HW, so we need to disable it manually.
  589. */
  590. if (trans->cfg->base_params->scd_chain_ext_wa)
  591. iwl_write_prph(trans, SCD_CHAINEXT_EN, 0);
  592. iwl_trans_ac_txq_enable(trans, trans_pcie->cmd_queue,
  593. trans_pcie->cmd_fifo,
  594. trans_pcie->cmd_q_wdg_timeout);
  595. /* Activate all Tx DMA/FIFO channels */
  596. iwl_scd_activate_fifos(trans);
  597. /* Enable DMA channel */
  598. for (chan = 0; chan < FH_TCSR_CHNL_NUM; chan++)
  599. iwl_write_direct32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(chan),
  600. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  601. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE);
  602. /* Update FH chicken bits */
  603. reg_val = iwl_read_direct32(trans, FH_TX_CHICKEN_BITS_REG);
  604. iwl_write_direct32(trans, FH_TX_CHICKEN_BITS_REG,
  605. reg_val | FH_TX_CHICKEN_BITS_SCD_AUTO_RETRY_EN);
  606. /* Enable L1-Active */
  607. if (trans->cfg->device_family != IWL_DEVICE_FAMILY_8000)
  608. iwl_clear_bits_prph(trans, APMG_PCIDEV_STT_REG,
  609. APMG_PCIDEV_STT_VAL_L1_ACT_DIS);
  610. }
  611. void iwl_trans_pcie_tx_reset(struct iwl_trans *trans)
  612. {
  613. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  614. int txq_id;
  615. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  616. txq_id++) {
  617. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  618. iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(txq_id),
  619. txq->q.dma_addr >> 8);
  620. iwl_pcie_txq_unmap(trans, txq_id);
  621. txq->q.read_ptr = 0;
  622. txq->q.write_ptr = 0;
  623. }
  624. /* Tell NIC where to find the "keep warm" buffer */
  625. iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
  626. trans_pcie->kw.dma >> 4);
  627. /*
  628. * Send 0 as the scd_base_addr since the device may have be reset
  629. * while we were in WoWLAN in which case SCD_SRAM_BASE_ADDR will
  630. * contain garbage.
  631. */
  632. iwl_pcie_tx_start(trans, 0);
  633. }
  634. static void iwl_pcie_tx_stop_fh(struct iwl_trans *trans)
  635. {
  636. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  637. unsigned long flags;
  638. int ch, ret;
  639. u32 mask = 0;
  640. spin_lock(&trans_pcie->irq_lock);
  641. if (!iwl_trans_grab_nic_access(trans, &flags))
  642. goto out;
  643. /* Stop each Tx DMA channel */
  644. for (ch = 0; ch < FH_TCSR_CHNL_NUM; ch++) {
  645. iwl_write32(trans, FH_TCSR_CHNL_TX_CONFIG_REG(ch), 0x0);
  646. mask |= FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE(ch);
  647. }
  648. /* Wait for DMA channels to be idle */
  649. ret = iwl_poll_bit(trans, FH_TSSR_TX_STATUS_REG, mask, mask, 5000);
  650. if (ret < 0)
  651. IWL_ERR(trans,
  652. "Failing on timeout while stopping DMA channel %d [0x%08x]\n",
  653. ch, iwl_read32(trans, FH_TSSR_TX_STATUS_REG));
  654. iwl_trans_release_nic_access(trans, &flags);
  655. out:
  656. spin_unlock(&trans_pcie->irq_lock);
  657. }
  658. /*
  659. * iwl_pcie_tx_stop - Stop all Tx DMA channels
  660. */
  661. int iwl_pcie_tx_stop(struct iwl_trans *trans)
  662. {
  663. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  664. int txq_id;
  665. /* Turn off all Tx DMA fifos */
  666. iwl_scd_deactivate_fifos(trans);
  667. /* Turn off all Tx DMA channels */
  668. iwl_pcie_tx_stop_fh(trans);
  669. /*
  670. * This function can be called before the op_mode disabled the
  671. * queues. This happens when we have an rfkill interrupt.
  672. * Since we stop Tx altogether - mark the queues as stopped.
  673. */
  674. memset(trans_pcie->queue_stopped, 0, sizeof(trans_pcie->queue_stopped));
  675. memset(trans_pcie->queue_used, 0, sizeof(trans_pcie->queue_used));
  676. /* This can happen: start_hw, stop_device */
  677. if (!trans_pcie->txq)
  678. return 0;
  679. /* Unmap DMA from host system and free skb's */
  680. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  681. txq_id++)
  682. iwl_pcie_txq_unmap(trans, txq_id);
  683. return 0;
  684. }
  685. /*
  686. * iwl_trans_tx_free - Free TXQ Context
  687. *
  688. * Destroy all TX DMA queues and structures
  689. */
  690. void iwl_pcie_tx_free(struct iwl_trans *trans)
  691. {
  692. int txq_id;
  693. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  694. /* Tx queues */
  695. if (trans_pcie->txq) {
  696. for (txq_id = 0;
  697. txq_id < trans->cfg->base_params->num_of_queues; txq_id++)
  698. iwl_pcie_txq_free(trans, txq_id);
  699. }
  700. kfree(trans_pcie->txq);
  701. trans_pcie->txq = NULL;
  702. iwl_pcie_free_dma_ptr(trans, &trans_pcie->kw);
  703. iwl_pcie_free_dma_ptr(trans, &trans_pcie->scd_bc_tbls);
  704. }
  705. /*
  706. * iwl_pcie_tx_alloc - allocate TX context
  707. * Allocate all Tx DMA structures and initialize them
  708. */
  709. static int iwl_pcie_tx_alloc(struct iwl_trans *trans)
  710. {
  711. int ret;
  712. int txq_id, slots_num;
  713. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  714. u16 scd_bc_tbls_size = trans->cfg->base_params->num_of_queues *
  715. sizeof(struct iwlagn_scd_bc_tbl);
  716. /*It is not allowed to alloc twice, so warn when this happens.
  717. * We cannot rely on the previous allocation, so free and fail */
  718. if (WARN_ON(trans_pcie->txq)) {
  719. ret = -EINVAL;
  720. goto error;
  721. }
  722. ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->scd_bc_tbls,
  723. scd_bc_tbls_size);
  724. if (ret) {
  725. IWL_ERR(trans, "Scheduler BC Table allocation failed\n");
  726. goto error;
  727. }
  728. /* Alloc keep-warm buffer */
  729. ret = iwl_pcie_alloc_dma_ptr(trans, &trans_pcie->kw, IWL_KW_SIZE);
  730. if (ret) {
  731. IWL_ERR(trans, "Keep Warm allocation failed\n");
  732. goto error;
  733. }
  734. trans_pcie->txq = kcalloc(trans->cfg->base_params->num_of_queues,
  735. sizeof(struct iwl_txq), GFP_KERNEL);
  736. if (!trans_pcie->txq) {
  737. IWL_ERR(trans, "Not enough memory for txq\n");
  738. ret = -ENOMEM;
  739. goto error;
  740. }
  741. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  742. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  743. txq_id++) {
  744. slots_num = (txq_id == trans_pcie->cmd_queue) ?
  745. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  746. ret = iwl_pcie_txq_alloc(trans, &trans_pcie->txq[txq_id],
  747. slots_num, txq_id);
  748. if (ret) {
  749. IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
  750. goto error;
  751. }
  752. }
  753. return 0;
  754. error:
  755. iwl_pcie_tx_free(trans);
  756. return ret;
  757. }
  758. int iwl_pcie_tx_init(struct iwl_trans *trans)
  759. {
  760. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  761. int ret;
  762. int txq_id, slots_num;
  763. bool alloc = false;
  764. if (!trans_pcie->txq) {
  765. ret = iwl_pcie_tx_alloc(trans);
  766. if (ret)
  767. goto error;
  768. alloc = true;
  769. }
  770. spin_lock(&trans_pcie->irq_lock);
  771. /* Turn off all Tx DMA fifos */
  772. iwl_scd_deactivate_fifos(trans);
  773. /* Tell NIC where to find the "keep warm" buffer */
  774. iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
  775. trans_pcie->kw.dma >> 4);
  776. spin_unlock(&trans_pcie->irq_lock);
  777. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  778. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  779. txq_id++) {
  780. slots_num = (txq_id == trans_pcie->cmd_queue) ?
  781. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  782. ret = iwl_pcie_txq_init(trans, &trans_pcie->txq[txq_id],
  783. slots_num, txq_id);
  784. if (ret) {
  785. IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
  786. goto error;
  787. }
  788. }
  789. iwl_set_bits_prph(trans, SCD_GP_CTRL, SCD_GP_CTRL_AUTO_ACTIVE_MODE);
  790. if (trans->cfg->base_params->num_of_queues > 20)
  791. iwl_set_bits_prph(trans, SCD_GP_CTRL,
  792. SCD_GP_CTRL_ENABLE_31_QUEUES);
  793. return 0;
  794. error:
  795. /*Upon error, free only if we allocated something */
  796. if (alloc)
  797. iwl_pcie_tx_free(trans);
  798. return ret;
  799. }
  800. static inline void iwl_pcie_txq_progress(struct iwl_txq *txq)
  801. {
  802. lockdep_assert_held(&txq->lock);
  803. if (!txq->wd_timeout)
  804. return;
  805. /*
  806. * station is asleep and we send data - that must
  807. * be uAPSD or PS-Poll. Don't rearm the timer.
  808. */
  809. if (txq->frozen)
  810. return;
  811. /*
  812. * if empty delete timer, otherwise move timer forward
  813. * since we're making progress on this queue
  814. */
  815. if (txq->q.read_ptr == txq->q.write_ptr)
  816. del_timer(&txq->stuck_timer);
  817. else
  818. mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
  819. }
  820. /* Frees buffers until index _not_ inclusive */
  821. void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int txq_id, int ssn,
  822. struct sk_buff_head *skbs)
  823. {
  824. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  825. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  826. int tfd_num = ssn & (TFD_QUEUE_SIZE_MAX - 1);
  827. struct iwl_queue *q = &txq->q;
  828. int last_to_free;
  829. /* This function is not meant to release cmd queue*/
  830. if (WARN_ON(txq_id == trans_pcie->cmd_queue))
  831. return;
  832. spin_lock_bh(&txq->lock);
  833. if (!txq->active) {
  834. IWL_DEBUG_TX_QUEUES(trans, "Q %d inactive - ignoring idx %d\n",
  835. txq_id, ssn);
  836. goto out;
  837. }
  838. if (txq->q.read_ptr == tfd_num)
  839. goto out;
  840. IWL_DEBUG_TX_REPLY(trans, "[Q %d] %d -> %d (%d)\n",
  841. txq_id, txq->q.read_ptr, tfd_num, ssn);
  842. /*Since we free until index _not_ inclusive, the one before index is
  843. * the last we will free. This one must be used */
  844. last_to_free = iwl_queue_dec_wrap(tfd_num);
  845. if (!iwl_queue_used(q, last_to_free)) {
  846. IWL_ERR(trans,
  847. "%s: Read index for DMA queue txq id (%d), last_to_free %d is out of range [0-%d] %d %d.\n",
  848. __func__, txq_id, last_to_free, TFD_QUEUE_SIZE_MAX,
  849. q->write_ptr, q->read_ptr);
  850. goto out;
  851. }
  852. if (WARN_ON(!skb_queue_empty(skbs)))
  853. goto out;
  854. for (;
  855. q->read_ptr != tfd_num;
  856. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr)) {
  857. struct sk_buff *skb = txq->entries[txq->q.read_ptr].skb;
  858. if (WARN_ON_ONCE(!skb))
  859. continue;
  860. iwl_pcie_free_tso_page(skb);
  861. __skb_queue_tail(skbs, skb);
  862. txq->entries[txq->q.read_ptr].skb = NULL;
  863. iwl_pcie_txq_inval_byte_cnt_tbl(trans, txq);
  864. iwl_pcie_txq_free_tfd(trans, txq);
  865. }
  866. iwl_pcie_txq_progress(txq);
  867. if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
  868. test_bit(txq_id, trans_pcie->queue_stopped)) {
  869. struct sk_buff_head overflow_skbs;
  870. __skb_queue_head_init(&overflow_skbs);
  871. skb_queue_splice_init(&txq->overflow_q, &overflow_skbs);
  872. /*
  873. * This is tricky: we are in reclaim path which is non
  874. * re-entrant, so noone will try to take the access the
  875. * txq data from that path. We stopped tx, so we can't
  876. * have tx as well. Bottom line, we can unlock and re-lock
  877. * later.
  878. */
  879. spin_unlock_bh(&txq->lock);
  880. while (!skb_queue_empty(&overflow_skbs)) {
  881. struct sk_buff *skb = __skb_dequeue(&overflow_skbs);
  882. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  883. u8 dev_cmd_idx = IWL_TRANS_FIRST_DRIVER_DATA + 1;
  884. struct iwl_device_cmd *dev_cmd =
  885. info->driver_data[dev_cmd_idx];
  886. /*
  887. * Note that we can very well be overflowing again.
  888. * In that case, iwl_queue_space will be small again
  889. * and we won't wake mac80211's queue.
  890. */
  891. iwl_trans_pcie_tx(trans, skb, dev_cmd, txq_id);
  892. }
  893. spin_lock_bh(&txq->lock);
  894. if (iwl_queue_space(&txq->q) > txq->q.low_mark)
  895. iwl_wake_queue(trans, txq);
  896. }
  897. if (q->read_ptr == q->write_ptr) {
  898. IWL_DEBUG_RPM(trans, "Q %d - last tx reclaimed\n", q->id);
  899. iwl_trans_pcie_unref(trans);
  900. }
  901. out:
  902. spin_unlock_bh(&txq->lock);
  903. }
  904. static int iwl_pcie_set_cmd_in_flight(struct iwl_trans *trans,
  905. const struct iwl_host_cmd *cmd)
  906. {
  907. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  908. int ret;
  909. lockdep_assert_held(&trans_pcie->reg_lock);
  910. if (!(cmd->flags & CMD_SEND_IN_IDLE) &&
  911. !trans_pcie->ref_cmd_in_flight) {
  912. trans_pcie->ref_cmd_in_flight = true;
  913. IWL_DEBUG_RPM(trans, "set ref_cmd_in_flight - ref\n");
  914. iwl_trans_pcie_ref(trans);
  915. }
  916. /*
  917. * wake up the NIC to make sure that the firmware will see the host
  918. * command - we will let the NIC sleep once all the host commands
  919. * returned. This needs to be done only on NICs that have
  920. * apmg_wake_up_wa set.
  921. */
  922. if (trans->cfg->base_params->apmg_wake_up_wa &&
  923. !trans_pcie->cmd_hold_nic_awake) {
  924. __iwl_trans_pcie_set_bit(trans, CSR_GP_CNTRL,
  925. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  926. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  927. CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
  928. (CSR_GP_CNTRL_REG_FLAG_MAC_CLOCK_READY |
  929. CSR_GP_CNTRL_REG_FLAG_GOING_TO_SLEEP),
  930. 15000);
  931. if (ret < 0) {
  932. __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
  933. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  934. IWL_ERR(trans, "Failed to wake NIC for hcmd\n");
  935. return -EIO;
  936. }
  937. trans_pcie->cmd_hold_nic_awake = true;
  938. }
  939. return 0;
  940. }
  941. static int iwl_pcie_clear_cmd_in_flight(struct iwl_trans *trans)
  942. {
  943. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  944. lockdep_assert_held(&trans_pcie->reg_lock);
  945. if (trans_pcie->ref_cmd_in_flight) {
  946. trans_pcie->ref_cmd_in_flight = false;
  947. IWL_DEBUG_RPM(trans, "clear ref_cmd_in_flight - unref\n");
  948. iwl_trans_pcie_unref(trans);
  949. }
  950. if (trans->cfg->base_params->apmg_wake_up_wa) {
  951. if (WARN_ON(!trans_pcie->cmd_hold_nic_awake))
  952. return 0;
  953. trans_pcie->cmd_hold_nic_awake = false;
  954. __iwl_trans_pcie_clear_bit(trans, CSR_GP_CNTRL,
  955. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  956. }
  957. return 0;
  958. }
  959. /*
  960. * iwl_pcie_cmdq_reclaim - Reclaim TX command queue entries already Tx'd
  961. *
  962. * When FW advances 'R' index, all entries between old and new 'R' index
  963. * need to be reclaimed. As result, some free space forms. If there is
  964. * enough free space (> low mark), wake the stack that feeds us.
  965. */
  966. static void iwl_pcie_cmdq_reclaim(struct iwl_trans *trans, int txq_id, int idx)
  967. {
  968. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  969. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  970. struct iwl_queue *q = &txq->q;
  971. unsigned long flags;
  972. int nfreed = 0;
  973. lockdep_assert_held(&txq->lock);
  974. if ((idx >= TFD_QUEUE_SIZE_MAX) || (!iwl_queue_used(q, idx))) {
  975. IWL_ERR(trans,
  976. "%s: Read index for DMA queue txq id (%d), index %d is out of range [0-%d] %d %d.\n",
  977. __func__, txq_id, idx, TFD_QUEUE_SIZE_MAX,
  978. q->write_ptr, q->read_ptr);
  979. return;
  980. }
  981. for (idx = iwl_queue_inc_wrap(idx); q->read_ptr != idx;
  982. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr)) {
  983. if (nfreed++ > 0) {
  984. IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n",
  985. idx, q->write_ptr, q->read_ptr);
  986. iwl_force_nmi(trans);
  987. }
  988. }
  989. if (q->read_ptr == q->write_ptr) {
  990. spin_lock_irqsave(&trans_pcie->reg_lock, flags);
  991. iwl_pcie_clear_cmd_in_flight(trans);
  992. spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
  993. }
  994. iwl_pcie_txq_progress(txq);
  995. }
  996. static int iwl_pcie_txq_set_ratid_map(struct iwl_trans *trans, u16 ra_tid,
  997. u16 txq_id)
  998. {
  999. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1000. u32 tbl_dw_addr;
  1001. u32 tbl_dw;
  1002. u16 scd_q2ratid;
  1003. scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  1004. tbl_dw_addr = trans_pcie->scd_base_addr +
  1005. SCD_TRANS_TBL_OFFSET_QUEUE(txq_id);
  1006. tbl_dw = iwl_trans_read_mem32(trans, tbl_dw_addr);
  1007. if (txq_id & 0x1)
  1008. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  1009. else
  1010. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  1011. iwl_trans_write_mem32(trans, tbl_dw_addr, tbl_dw);
  1012. return 0;
  1013. }
  1014. /* Receiver address (actually, Rx station's index into station table),
  1015. * combined with Traffic ID (QOS priority), in format used by Tx Scheduler */
  1016. #define BUILD_RAxTID(sta_id, tid) (((sta_id) << 4) + (tid))
  1017. void iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int txq_id, u16 ssn,
  1018. const struct iwl_trans_txq_scd_cfg *cfg,
  1019. unsigned int wdg_timeout)
  1020. {
  1021. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1022. struct iwl_txq *txq = &trans_pcie->txq[txq_id];
  1023. int fifo = -1;
  1024. if (test_and_set_bit(txq_id, trans_pcie->queue_used))
  1025. WARN_ONCE(1, "queue %d already used - expect issues", txq_id);
  1026. txq->wd_timeout = msecs_to_jiffies(wdg_timeout);
  1027. if (cfg) {
  1028. fifo = cfg->fifo;
  1029. /* Disable the scheduler prior configuring the cmd queue */
  1030. if (txq_id == trans_pcie->cmd_queue &&
  1031. trans_pcie->scd_set_active)
  1032. iwl_scd_enable_set_active(trans, 0);
  1033. /* Stop this Tx queue before configuring it */
  1034. iwl_scd_txq_set_inactive(trans, txq_id);
  1035. /* Set this queue as a chain-building queue unless it is CMD */
  1036. if (txq_id != trans_pcie->cmd_queue)
  1037. iwl_scd_txq_set_chain(trans, txq_id);
  1038. if (cfg->aggregate) {
  1039. u16 ra_tid = BUILD_RAxTID(cfg->sta_id, cfg->tid);
  1040. /* Map receiver-address / traffic-ID to this queue */
  1041. iwl_pcie_txq_set_ratid_map(trans, ra_tid, txq_id);
  1042. /* enable aggregations for the queue */
  1043. iwl_scd_txq_enable_agg(trans, txq_id);
  1044. txq->ampdu = true;
  1045. } else {
  1046. /*
  1047. * disable aggregations for the queue, this will also
  1048. * make the ra_tid mapping configuration irrelevant
  1049. * since it is now a non-AGG queue.
  1050. */
  1051. iwl_scd_txq_disable_agg(trans, txq_id);
  1052. ssn = txq->q.read_ptr;
  1053. }
  1054. }
  1055. /* Place first TFD at index corresponding to start sequence number.
  1056. * Assumes that ssn_idx is valid (!= 0xFFF) */
  1057. txq->q.read_ptr = (ssn & 0xff);
  1058. txq->q.write_ptr = (ssn & 0xff);
  1059. iwl_write_direct32(trans, HBUS_TARG_WRPTR,
  1060. (ssn & 0xff) | (txq_id << 8));
  1061. if (cfg) {
  1062. u8 frame_limit = cfg->frame_limit;
  1063. iwl_write_prph(trans, SCD_QUEUE_RDPTR(txq_id), ssn);
  1064. /* Set up Tx window size and frame limit for this queue */
  1065. iwl_trans_write_mem32(trans, trans_pcie->scd_base_addr +
  1066. SCD_CONTEXT_QUEUE_OFFSET(txq_id), 0);
  1067. iwl_trans_write_mem32(trans,
  1068. trans_pcie->scd_base_addr +
  1069. SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
  1070. ((frame_limit << SCD_QUEUE_CTX_REG2_WIN_SIZE_POS) &
  1071. SCD_QUEUE_CTX_REG2_WIN_SIZE_MSK) |
  1072. ((frame_limit << SCD_QUEUE_CTX_REG2_FRAME_LIMIT_POS) &
  1073. SCD_QUEUE_CTX_REG2_FRAME_LIMIT_MSK));
  1074. /* Set up status area in SRAM, map to Tx DMA/FIFO, activate */
  1075. iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id),
  1076. (1 << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  1077. (cfg->fifo << SCD_QUEUE_STTS_REG_POS_TXF) |
  1078. (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
  1079. SCD_QUEUE_STTS_REG_MSK);
  1080. /* enable the scheduler for this queue (only) */
  1081. if (txq_id == trans_pcie->cmd_queue &&
  1082. trans_pcie->scd_set_active)
  1083. iwl_scd_enable_set_active(trans, BIT(txq_id));
  1084. IWL_DEBUG_TX_QUEUES(trans,
  1085. "Activate queue %d on FIFO %d WrPtr: %d\n",
  1086. txq_id, fifo, ssn & 0xff);
  1087. } else {
  1088. IWL_DEBUG_TX_QUEUES(trans,
  1089. "Activate queue %d WrPtr: %d\n",
  1090. txq_id, ssn & 0xff);
  1091. }
  1092. txq->active = true;
  1093. }
  1094. void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int txq_id,
  1095. bool configure_scd)
  1096. {
  1097. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1098. u32 stts_addr = trans_pcie->scd_base_addr +
  1099. SCD_TX_STTS_QUEUE_OFFSET(txq_id);
  1100. static const u32 zero_val[4] = {};
  1101. trans_pcie->txq[txq_id].frozen_expiry_remainder = 0;
  1102. trans_pcie->txq[txq_id].frozen = false;
  1103. /*
  1104. * Upon HW Rfkill - we stop the device, and then stop the queues
  1105. * in the op_mode. Just for the sake of the simplicity of the op_mode,
  1106. * allow the op_mode to call txq_disable after it already called
  1107. * stop_device.
  1108. */
  1109. if (!test_and_clear_bit(txq_id, trans_pcie->queue_used)) {
  1110. WARN_ONCE(test_bit(STATUS_DEVICE_ENABLED, &trans->status),
  1111. "queue %d not used", txq_id);
  1112. return;
  1113. }
  1114. if (configure_scd) {
  1115. iwl_scd_txq_set_inactive(trans, txq_id);
  1116. iwl_trans_write_mem(trans, stts_addr, (void *)zero_val,
  1117. ARRAY_SIZE(zero_val));
  1118. }
  1119. iwl_pcie_txq_unmap(trans, txq_id);
  1120. trans_pcie->txq[txq_id].ampdu = false;
  1121. IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", txq_id);
  1122. }
  1123. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  1124. /*
  1125. * iwl_pcie_enqueue_hcmd - enqueue a uCode command
  1126. * @priv: device private data point
  1127. * @cmd: a pointer to the ucode command structure
  1128. *
  1129. * The function returns < 0 values to indicate the operation
  1130. * failed. On success, it returns the index (>= 0) of command in the
  1131. * command queue.
  1132. */
  1133. static int iwl_pcie_enqueue_hcmd(struct iwl_trans *trans,
  1134. struct iwl_host_cmd *cmd)
  1135. {
  1136. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1137. struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  1138. struct iwl_queue *q = &txq->q;
  1139. struct iwl_device_cmd *out_cmd;
  1140. struct iwl_cmd_meta *out_meta;
  1141. unsigned long flags;
  1142. void *dup_buf = NULL;
  1143. dma_addr_t phys_addr;
  1144. int idx;
  1145. u16 copy_size, cmd_size, scratch_size;
  1146. bool had_nocopy = false;
  1147. u8 group_id = iwl_cmd_groupid(cmd->id);
  1148. int i, ret;
  1149. u32 cmd_pos;
  1150. const u8 *cmddata[IWL_MAX_CMD_TBS_PER_TFD];
  1151. u16 cmdlen[IWL_MAX_CMD_TBS_PER_TFD];
  1152. if (WARN(!trans_pcie->wide_cmd_header &&
  1153. group_id > IWL_ALWAYS_LONG_GROUP,
  1154. "unsupported wide command %#x\n", cmd->id))
  1155. return -EINVAL;
  1156. if (group_id != 0) {
  1157. copy_size = sizeof(struct iwl_cmd_header_wide);
  1158. cmd_size = sizeof(struct iwl_cmd_header_wide);
  1159. } else {
  1160. copy_size = sizeof(struct iwl_cmd_header);
  1161. cmd_size = sizeof(struct iwl_cmd_header);
  1162. }
  1163. /* need one for the header if the first is NOCOPY */
  1164. BUILD_BUG_ON(IWL_MAX_CMD_TBS_PER_TFD > IWL_NUM_OF_TBS - 1);
  1165. for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
  1166. cmddata[i] = cmd->data[i];
  1167. cmdlen[i] = cmd->len[i];
  1168. if (!cmd->len[i])
  1169. continue;
  1170. /* need at least IWL_HCMD_SCRATCHBUF_SIZE copied */
  1171. if (copy_size < IWL_HCMD_SCRATCHBUF_SIZE) {
  1172. int copy = IWL_HCMD_SCRATCHBUF_SIZE - copy_size;
  1173. if (copy > cmdlen[i])
  1174. copy = cmdlen[i];
  1175. cmdlen[i] -= copy;
  1176. cmddata[i] += copy;
  1177. copy_size += copy;
  1178. }
  1179. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
  1180. had_nocopy = true;
  1181. if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
  1182. idx = -EINVAL;
  1183. goto free_dup_buf;
  1184. }
  1185. } else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
  1186. /*
  1187. * This is also a chunk that isn't copied
  1188. * to the static buffer so set had_nocopy.
  1189. */
  1190. had_nocopy = true;
  1191. /* only allowed once */
  1192. if (WARN_ON(dup_buf)) {
  1193. idx = -EINVAL;
  1194. goto free_dup_buf;
  1195. }
  1196. dup_buf = kmemdup(cmddata[i], cmdlen[i],
  1197. GFP_ATOMIC);
  1198. if (!dup_buf)
  1199. return -ENOMEM;
  1200. } else {
  1201. /* NOCOPY must not be followed by normal! */
  1202. if (WARN_ON(had_nocopy)) {
  1203. idx = -EINVAL;
  1204. goto free_dup_buf;
  1205. }
  1206. copy_size += cmdlen[i];
  1207. }
  1208. cmd_size += cmd->len[i];
  1209. }
  1210. /*
  1211. * If any of the command structures end up being larger than
  1212. * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
  1213. * allocated into separate TFDs, then we will need to
  1214. * increase the size of the buffers.
  1215. */
  1216. if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
  1217. "Command %s (%#x) is too large (%d bytes)\n",
  1218. iwl_get_cmd_string(trans, cmd->id),
  1219. cmd->id, copy_size)) {
  1220. idx = -EINVAL;
  1221. goto free_dup_buf;
  1222. }
  1223. spin_lock_bh(&txq->lock);
  1224. if (iwl_queue_space(q) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
  1225. spin_unlock_bh(&txq->lock);
  1226. IWL_ERR(trans, "No space in command queue\n");
  1227. iwl_op_mode_cmd_queue_full(trans->op_mode);
  1228. idx = -ENOSPC;
  1229. goto free_dup_buf;
  1230. }
  1231. idx = get_cmd_index(q, q->write_ptr);
  1232. out_cmd = txq->entries[idx].cmd;
  1233. out_meta = &txq->entries[idx].meta;
  1234. memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
  1235. if (cmd->flags & CMD_WANT_SKB)
  1236. out_meta->source = cmd;
  1237. /* set up the header */
  1238. if (group_id != 0) {
  1239. out_cmd->hdr_wide.cmd = iwl_cmd_opcode(cmd->id);
  1240. out_cmd->hdr_wide.group_id = group_id;
  1241. out_cmd->hdr_wide.version = iwl_cmd_version(cmd->id);
  1242. out_cmd->hdr_wide.length =
  1243. cpu_to_le16(cmd_size -
  1244. sizeof(struct iwl_cmd_header_wide));
  1245. out_cmd->hdr_wide.reserved = 0;
  1246. out_cmd->hdr_wide.sequence =
  1247. cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->cmd_queue) |
  1248. INDEX_TO_SEQ(q->write_ptr));
  1249. cmd_pos = sizeof(struct iwl_cmd_header_wide);
  1250. copy_size = sizeof(struct iwl_cmd_header_wide);
  1251. } else {
  1252. out_cmd->hdr.cmd = iwl_cmd_opcode(cmd->id);
  1253. out_cmd->hdr.sequence =
  1254. cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->cmd_queue) |
  1255. INDEX_TO_SEQ(q->write_ptr));
  1256. out_cmd->hdr.group_id = 0;
  1257. cmd_pos = sizeof(struct iwl_cmd_header);
  1258. copy_size = sizeof(struct iwl_cmd_header);
  1259. }
  1260. /* and copy the data that needs to be copied */
  1261. for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
  1262. int copy;
  1263. if (!cmd->len[i])
  1264. continue;
  1265. /* copy everything if not nocopy/dup */
  1266. if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
  1267. IWL_HCMD_DFL_DUP))) {
  1268. copy = cmd->len[i];
  1269. memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
  1270. cmd_pos += copy;
  1271. copy_size += copy;
  1272. continue;
  1273. }
  1274. /*
  1275. * Otherwise we need at least IWL_HCMD_SCRATCHBUF_SIZE copied
  1276. * in total (for the scratchbuf handling), but copy up to what
  1277. * we can fit into the payload for debug dump purposes.
  1278. */
  1279. copy = min_t(int, TFD_MAX_PAYLOAD_SIZE - cmd_pos, cmd->len[i]);
  1280. memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
  1281. cmd_pos += copy;
  1282. /* However, treat copy_size the proper way, we need it below */
  1283. if (copy_size < IWL_HCMD_SCRATCHBUF_SIZE) {
  1284. copy = IWL_HCMD_SCRATCHBUF_SIZE - copy_size;
  1285. if (copy > cmd->len[i])
  1286. copy = cmd->len[i];
  1287. copy_size += copy;
  1288. }
  1289. }
  1290. IWL_DEBUG_HC(trans,
  1291. "Sending command %s (%.2x.%.2x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
  1292. iwl_get_cmd_string(trans, cmd->id),
  1293. group_id, out_cmd->hdr.cmd,
  1294. le16_to_cpu(out_cmd->hdr.sequence),
  1295. cmd_size, q->write_ptr, idx, trans_pcie->cmd_queue);
  1296. /* start the TFD with the scratchbuf */
  1297. scratch_size = min_t(int, copy_size, IWL_HCMD_SCRATCHBUF_SIZE);
  1298. memcpy(&txq->scratchbufs[q->write_ptr], &out_cmd->hdr, scratch_size);
  1299. iwl_pcie_txq_build_tfd(trans, txq,
  1300. iwl_pcie_get_scratchbuf_dma(txq, q->write_ptr),
  1301. scratch_size, true);
  1302. /* map first command fragment, if any remains */
  1303. if (copy_size > scratch_size) {
  1304. phys_addr = dma_map_single(trans->dev,
  1305. ((u8 *)&out_cmd->hdr) + scratch_size,
  1306. copy_size - scratch_size,
  1307. DMA_TO_DEVICE);
  1308. if (dma_mapping_error(trans->dev, phys_addr)) {
  1309. iwl_pcie_tfd_unmap(trans, out_meta,
  1310. &txq->tfds[q->write_ptr]);
  1311. idx = -ENOMEM;
  1312. goto out;
  1313. }
  1314. iwl_pcie_txq_build_tfd(trans, txq, phys_addr,
  1315. copy_size - scratch_size, false);
  1316. }
  1317. /* map the remaining (adjusted) nocopy/dup fragments */
  1318. for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
  1319. const void *data = cmddata[i];
  1320. if (!cmdlen[i])
  1321. continue;
  1322. if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
  1323. IWL_HCMD_DFL_DUP)))
  1324. continue;
  1325. if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
  1326. data = dup_buf;
  1327. phys_addr = dma_map_single(trans->dev, (void *)data,
  1328. cmdlen[i], DMA_TO_DEVICE);
  1329. if (dma_mapping_error(trans->dev, phys_addr)) {
  1330. iwl_pcie_tfd_unmap(trans, out_meta,
  1331. &txq->tfds[q->write_ptr]);
  1332. idx = -ENOMEM;
  1333. goto out;
  1334. }
  1335. iwl_pcie_txq_build_tfd(trans, txq, phys_addr, cmdlen[i], false);
  1336. }
  1337. BUILD_BUG_ON(IWL_NUM_OF_TBS + CMD_TB_BITMAP_POS >
  1338. sizeof(out_meta->flags) * BITS_PER_BYTE);
  1339. out_meta->flags = cmd->flags;
  1340. if (WARN_ON_ONCE(txq->entries[idx].free_buf))
  1341. kzfree(txq->entries[idx].free_buf);
  1342. txq->entries[idx].free_buf = dup_buf;
  1343. trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size, &out_cmd->hdr_wide);
  1344. /* start timer if queue currently empty */
  1345. if (q->read_ptr == q->write_ptr && txq->wd_timeout)
  1346. mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
  1347. spin_lock_irqsave(&trans_pcie->reg_lock, flags);
  1348. ret = iwl_pcie_set_cmd_in_flight(trans, cmd);
  1349. if (ret < 0) {
  1350. idx = ret;
  1351. spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
  1352. goto out;
  1353. }
  1354. /* Increment and update queue's write index */
  1355. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr);
  1356. iwl_pcie_txq_inc_wr_ptr(trans, txq);
  1357. spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
  1358. out:
  1359. spin_unlock_bh(&txq->lock);
  1360. free_dup_buf:
  1361. if (idx < 0)
  1362. kfree(dup_buf);
  1363. return idx;
  1364. }
  1365. /*
  1366. * iwl_pcie_hcmd_complete - Pull unused buffers off the queue and reclaim them
  1367. * @rxb: Rx buffer to reclaim
  1368. */
  1369. void iwl_pcie_hcmd_complete(struct iwl_trans *trans,
  1370. struct iwl_rx_cmd_buffer *rxb)
  1371. {
  1372. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1373. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  1374. u8 group_id = iwl_cmd_groupid(pkt->hdr.group_id);
  1375. u32 cmd_id;
  1376. int txq_id = SEQ_TO_QUEUE(sequence);
  1377. int index = SEQ_TO_INDEX(sequence);
  1378. int cmd_index;
  1379. struct iwl_device_cmd *cmd;
  1380. struct iwl_cmd_meta *meta;
  1381. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1382. struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  1383. /* If a Tx command is being handled and it isn't in the actual
  1384. * command queue then there a command routing bug has been introduced
  1385. * in the queue management code. */
  1386. if (WARN(txq_id != trans_pcie->cmd_queue,
  1387. "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
  1388. txq_id, trans_pcie->cmd_queue, sequence,
  1389. trans_pcie->txq[trans_pcie->cmd_queue].q.read_ptr,
  1390. trans_pcie->txq[trans_pcie->cmd_queue].q.write_ptr)) {
  1391. iwl_print_hex_error(trans, pkt, 32);
  1392. return;
  1393. }
  1394. spin_lock_bh(&txq->lock);
  1395. cmd_index = get_cmd_index(&txq->q, index);
  1396. cmd = txq->entries[cmd_index].cmd;
  1397. meta = &txq->entries[cmd_index].meta;
  1398. cmd_id = iwl_cmd_id(cmd->hdr.cmd, group_id, 0);
  1399. iwl_pcie_tfd_unmap(trans, meta, &txq->tfds[index]);
  1400. /* Input error checking is done when commands are added to queue. */
  1401. if (meta->flags & CMD_WANT_SKB) {
  1402. struct page *p = rxb_steal_page(rxb);
  1403. meta->source->resp_pkt = pkt;
  1404. meta->source->_rx_page_addr = (unsigned long)page_address(p);
  1405. meta->source->_rx_page_order = trans_pcie->rx_page_order;
  1406. }
  1407. if (meta->flags & CMD_WANT_ASYNC_CALLBACK)
  1408. iwl_op_mode_async_cb(trans->op_mode, cmd);
  1409. iwl_pcie_cmdq_reclaim(trans, txq_id, index);
  1410. if (!(meta->flags & CMD_ASYNC)) {
  1411. if (!test_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status)) {
  1412. IWL_WARN(trans,
  1413. "HCMD_ACTIVE already clear for command %s\n",
  1414. iwl_get_cmd_string(trans, cmd_id));
  1415. }
  1416. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  1417. IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
  1418. iwl_get_cmd_string(trans, cmd_id));
  1419. wake_up(&trans_pcie->wait_command_queue);
  1420. }
  1421. if (meta->flags & CMD_MAKE_TRANS_IDLE) {
  1422. IWL_DEBUG_INFO(trans, "complete %s - mark trans as idle\n",
  1423. iwl_get_cmd_string(trans, cmd->hdr.cmd));
  1424. set_bit(STATUS_TRANS_IDLE, &trans->status);
  1425. wake_up(&trans_pcie->d0i3_waitq);
  1426. }
  1427. if (meta->flags & CMD_WAKE_UP_TRANS) {
  1428. IWL_DEBUG_INFO(trans, "complete %s - clear trans idle flag\n",
  1429. iwl_get_cmd_string(trans, cmd->hdr.cmd));
  1430. clear_bit(STATUS_TRANS_IDLE, &trans->status);
  1431. wake_up(&trans_pcie->d0i3_waitq);
  1432. }
  1433. meta->flags = 0;
  1434. spin_unlock_bh(&txq->lock);
  1435. }
  1436. #define HOST_COMPLETE_TIMEOUT (2 * HZ)
  1437. static int iwl_pcie_send_hcmd_async(struct iwl_trans *trans,
  1438. struct iwl_host_cmd *cmd)
  1439. {
  1440. int ret;
  1441. /* An asynchronous command can not expect an SKB to be set. */
  1442. if (WARN_ON(cmd->flags & CMD_WANT_SKB))
  1443. return -EINVAL;
  1444. ret = iwl_pcie_enqueue_hcmd(trans, cmd);
  1445. if (ret < 0) {
  1446. IWL_ERR(trans,
  1447. "Error sending %s: enqueue_hcmd failed: %d\n",
  1448. iwl_get_cmd_string(trans, cmd->id), ret);
  1449. return ret;
  1450. }
  1451. return 0;
  1452. }
  1453. static int iwl_pcie_send_hcmd_sync(struct iwl_trans *trans,
  1454. struct iwl_host_cmd *cmd)
  1455. {
  1456. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1457. int cmd_idx;
  1458. int ret;
  1459. IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n",
  1460. iwl_get_cmd_string(trans, cmd->id));
  1461. if (WARN(test_and_set_bit(STATUS_SYNC_HCMD_ACTIVE,
  1462. &trans->status),
  1463. "Command %s: a command is already active!\n",
  1464. iwl_get_cmd_string(trans, cmd->id)))
  1465. return -EIO;
  1466. IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n",
  1467. iwl_get_cmd_string(trans, cmd->id));
  1468. cmd_idx = iwl_pcie_enqueue_hcmd(trans, cmd);
  1469. if (cmd_idx < 0) {
  1470. ret = cmd_idx;
  1471. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  1472. IWL_ERR(trans,
  1473. "Error sending %s: enqueue_hcmd failed: %d\n",
  1474. iwl_get_cmd_string(trans, cmd->id), ret);
  1475. return ret;
  1476. }
  1477. ret = wait_event_timeout(trans_pcie->wait_command_queue,
  1478. !test_bit(STATUS_SYNC_HCMD_ACTIVE,
  1479. &trans->status),
  1480. HOST_COMPLETE_TIMEOUT);
  1481. if (!ret) {
  1482. struct iwl_txq *txq = &trans_pcie->txq[trans_pcie->cmd_queue];
  1483. struct iwl_queue *q = &txq->q;
  1484. IWL_ERR(trans, "Error sending %s: time out after %dms.\n",
  1485. iwl_get_cmd_string(trans, cmd->id),
  1486. jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
  1487. IWL_ERR(trans, "Current CMD queue read_ptr %d write_ptr %d\n",
  1488. q->read_ptr, q->write_ptr);
  1489. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  1490. IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
  1491. iwl_get_cmd_string(trans, cmd->id));
  1492. ret = -ETIMEDOUT;
  1493. iwl_force_nmi(trans);
  1494. iwl_trans_fw_error(trans);
  1495. goto cancel;
  1496. }
  1497. if (test_bit(STATUS_FW_ERROR, &trans->status)) {
  1498. IWL_ERR(trans, "FW error in SYNC CMD %s\n",
  1499. iwl_get_cmd_string(trans, cmd->id));
  1500. dump_stack();
  1501. ret = -EIO;
  1502. goto cancel;
  1503. }
  1504. if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
  1505. test_bit(STATUS_RFKILL, &trans->status)) {
  1506. IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n");
  1507. ret = -ERFKILL;
  1508. goto cancel;
  1509. }
  1510. if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) {
  1511. IWL_ERR(trans, "Error: Response NULL in '%s'\n",
  1512. iwl_get_cmd_string(trans, cmd->id));
  1513. ret = -EIO;
  1514. goto cancel;
  1515. }
  1516. return 0;
  1517. cancel:
  1518. if (cmd->flags & CMD_WANT_SKB) {
  1519. /*
  1520. * Cancel the CMD_WANT_SKB flag for the cmd in the
  1521. * TX cmd queue. Otherwise in case the cmd comes
  1522. * in later, it will possibly set an invalid
  1523. * address (cmd->meta.source).
  1524. */
  1525. trans_pcie->txq[trans_pcie->cmd_queue].
  1526. entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB;
  1527. }
  1528. if (cmd->resp_pkt) {
  1529. iwl_free_resp(cmd);
  1530. cmd->resp_pkt = NULL;
  1531. }
  1532. return ret;
  1533. }
  1534. int iwl_trans_pcie_send_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  1535. {
  1536. if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
  1537. test_bit(STATUS_RFKILL, &trans->status)) {
  1538. IWL_DEBUG_RF_KILL(trans, "Dropping CMD 0x%x: RF KILL\n",
  1539. cmd->id);
  1540. return -ERFKILL;
  1541. }
  1542. if (cmd->flags & CMD_ASYNC)
  1543. return iwl_pcie_send_hcmd_async(trans, cmd);
  1544. /* We still can fail on RFKILL that can be asserted while we wait */
  1545. return iwl_pcie_send_hcmd_sync(trans, cmd);
  1546. }
  1547. static int iwl_fill_data_tbs(struct iwl_trans *trans, struct sk_buff *skb,
  1548. struct iwl_txq *txq, u8 hdr_len,
  1549. struct iwl_cmd_meta *out_meta,
  1550. struct iwl_device_cmd *dev_cmd, u16 tb1_len)
  1551. {
  1552. struct iwl_queue *q = &txq->q;
  1553. u16 tb2_len;
  1554. int i;
  1555. /*
  1556. * Set up TFD's third entry to point directly to remainder
  1557. * of skb's head, if any
  1558. */
  1559. tb2_len = skb_headlen(skb) - hdr_len;
  1560. if (tb2_len > 0) {
  1561. dma_addr_t tb2_phys = dma_map_single(trans->dev,
  1562. skb->data + hdr_len,
  1563. tb2_len, DMA_TO_DEVICE);
  1564. if (unlikely(dma_mapping_error(trans->dev, tb2_phys))) {
  1565. iwl_pcie_tfd_unmap(trans, out_meta,
  1566. &txq->tfds[q->write_ptr]);
  1567. return -EINVAL;
  1568. }
  1569. iwl_pcie_txq_build_tfd(trans, txq, tb2_phys, tb2_len, false);
  1570. }
  1571. /* set up the remaining entries to point to the data */
  1572. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1573. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1574. dma_addr_t tb_phys;
  1575. int tb_idx;
  1576. if (!skb_frag_size(frag))
  1577. continue;
  1578. tb_phys = skb_frag_dma_map(trans->dev, frag, 0,
  1579. skb_frag_size(frag), DMA_TO_DEVICE);
  1580. if (unlikely(dma_mapping_error(trans->dev, tb_phys))) {
  1581. iwl_pcie_tfd_unmap(trans, out_meta,
  1582. &txq->tfds[q->write_ptr]);
  1583. return -EINVAL;
  1584. }
  1585. tb_idx = iwl_pcie_txq_build_tfd(trans, txq, tb_phys,
  1586. skb_frag_size(frag), false);
  1587. out_meta->flags |= BIT(tb_idx + CMD_TB_BITMAP_POS);
  1588. }
  1589. trace_iwlwifi_dev_tx(trans->dev, skb,
  1590. &txq->tfds[txq->q.write_ptr],
  1591. sizeof(struct iwl_tfd),
  1592. &dev_cmd->hdr, IWL_HCMD_SCRATCHBUF_SIZE + tb1_len,
  1593. skb->data + hdr_len, tb2_len);
  1594. trace_iwlwifi_dev_tx_data(trans->dev, skb,
  1595. hdr_len, skb->len - hdr_len);
  1596. return 0;
  1597. }
  1598. #ifdef CONFIG_INET
  1599. static struct iwl_tso_hdr_page *
  1600. get_page_hdr(struct iwl_trans *trans, size_t len)
  1601. {
  1602. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1603. struct iwl_tso_hdr_page *p = this_cpu_ptr(trans_pcie->tso_hdr_page);
  1604. if (!p->page)
  1605. goto alloc;
  1606. /* enough room on this page */
  1607. if (p->pos + len < (u8 *)page_address(p->page) + PAGE_SIZE)
  1608. return p;
  1609. /* We don't have enough room on this page, get a new one. */
  1610. __free_page(p->page);
  1611. alloc:
  1612. p->page = alloc_page(GFP_ATOMIC);
  1613. if (!p->page)
  1614. return NULL;
  1615. p->pos = page_address(p->page);
  1616. return p;
  1617. }
  1618. static void iwl_compute_pseudo_hdr_csum(void *iph, struct tcphdr *tcph,
  1619. bool ipv6, unsigned int len)
  1620. {
  1621. if (ipv6) {
  1622. struct ipv6hdr *iphv6 = iph;
  1623. tcph->check = ~csum_ipv6_magic(&iphv6->saddr, &iphv6->daddr,
  1624. len + tcph->doff * 4,
  1625. IPPROTO_TCP, 0);
  1626. } else {
  1627. struct iphdr *iphv4 = iph;
  1628. ip_send_check(iphv4);
  1629. tcph->check = ~csum_tcpudp_magic(iphv4->saddr, iphv4->daddr,
  1630. len + tcph->doff * 4,
  1631. IPPROTO_TCP, 0);
  1632. }
  1633. }
  1634. static int iwl_fill_data_tbs_amsdu(struct iwl_trans *trans, struct sk_buff *skb,
  1635. struct iwl_txq *txq, u8 hdr_len,
  1636. struct iwl_cmd_meta *out_meta,
  1637. struct iwl_device_cmd *dev_cmd, u16 tb1_len)
  1638. {
  1639. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1640. struct iwl_trans_pcie *trans_pcie = txq->trans_pcie;
  1641. struct ieee80211_hdr *hdr = (void *)skb->data;
  1642. unsigned int snap_ip_tcp_hdrlen, ip_hdrlen, total_len, hdr_room;
  1643. unsigned int mss = skb_shinfo(skb)->gso_size;
  1644. struct iwl_queue *q = &txq->q;
  1645. u16 length, iv_len, amsdu_pad;
  1646. u8 *start_hdr;
  1647. struct iwl_tso_hdr_page *hdr_page;
  1648. int ret;
  1649. struct tso_t tso;
  1650. /* if the packet is protected, then it must be CCMP or GCMP */
  1651. BUILD_BUG_ON(IEEE80211_CCMP_HDR_LEN != IEEE80211_GCMP_HDR_LEN);
  1652. iv_len = ieee80211_has_protected(hdr->frame_control) ?
  1653. IEEE80211_CCMP_HDR_LEN : 0;
  1654. trace_iwlwifi_dev_tx(trans->dev, skb,
  1655. &txq->tfds[txq->q.write_ptr],
  1656. sizeof(struct iwl_tfd),
  1657. &dev_cmd->hdr, IWL_HCMD_SCRATCHBUF_SIZE + tb1_len,
  1658. NULL, 0);
  1659. ip_hdrlen = skb_transport_header(skb) - skb_network_header(skb);
  1660. snap_ip_tcp_hdrlen = 8 + ip_hdrlen + tcp_hdrlen(skb);
  1661. total_len = skb->len - snap_ip_tcp_hdrlen - hdr_len - iv_len;
  1662. amsdu_pad = 0;
  1663. /* total amount of header we may need for this A-MSDU */
  1664. hdr_room = DIV_ROUND_UP(total_len, mss) *
  1665. (3 + snap_ip_tcp_hdrlen + sizeof(struct ethhdr)) + iv_len;
  1666. /* Our device supports 9 segments at most, it will fit in 1 page */
  1667. hdr_page = get_page_hdr(trans, hdr_room);
  1668. if (!hdr_page)
  1669. return -ENOMEM;
  1670. get_page(hdr_page->page);
  1671. start_hdr = hdr_page->pos;
  1672. info->driver_data[IWL_TRANS_FIRST_DRIVER_DATA] = hdr_page->page;
  1673. memcpy(hdr_page->pos, skb->data + hdr_len, iv_len);
  1674. hdr_page->pos += iv_len;
  1675. /*
  1676. * Pull the ieee80211 header + IV to be able to use TSO core,
  1677. * we will restore it for the tx_status flow.
  1678. */
  1679. skb_pull(skb, hdr_len + iv_len);
  1680. tso_start(skb, &tso);
  1681. while (total_len) {
  1682. /* this is the data left for this subframe */
  1683. unsigned int data_left =
  1684. min_t(unsigned int, mss, total_len);
  1685. struct sk_buff *csum_skb = NULL;
  1686. unsigned int hdr_tb_len;
  1687. dma_addr_t hdr_tb_phys;
  1688. struct tcphdr *tcph;
  1689. u8 *iph;
  1690. total_len -= data_left;
  1691. memset(hdr_page->pos, 0, amsdu_pad);
  1692. hdr_page->pos += amsdu_pad;
  1693. amsdu_pad = (4 - (sizeof(struct ethhdr) + snap_ip_tcp_hdrlen +
  1694. data_left)) & 0x3;
  1695. ether_addr_copy(hdr_page->pos, ieee80211_get_DA(hdr));
  1696. hdr_page->pos += ETH_ALEN;
  1697. ether_addr_copy(hdr_page->pos, ieee80211_get_SA(hdr));
  1698. hdr_page->pos += ETH_ALEN;
  1699. length = snap_ip_tcp_hdrlen + data_left;
  1700. *((__be16 *)hdr_page->pos) = cpu_to_be16(length);
  1701. hdr_page->pos += sizeof(length);
  1702. /*
  1703. * This will copy the SNAP as well which will be considered
  1704. * as MAC header.
  1705. */
  1706. tso_build_hdr(skb, hdr_page->pos, &tso, data_left, !total_len);
  1707. iph = hdr_page->pos + 8;
  1708. tcph = (void *)(iph + ip_hdrlen);
  1709. /* For testing on current hardware only */
  1710. if (trans_pcie->sw_csum_tx) {
  1711. csum_skb = alloc_skb(data_left + tcp_hdrlen(skb),
  1712. GFP_ATOMIC);
  1713. if (!csum_skb) {
  1714. ret = -ENOMEM;
  1715. goto out_unmap;
  1716. }
  1717. iwl_compute_pseudo_hdr_csum(iph, tcph,
  1718. skb->protocol ==
  1719. htons(ETH_P_IPV6),
  1720. data_left);
  1721. memcpy(skb_put(csum_skb, tcp_hdrlen(skb)),
  1722. tcph, tcp_hdrlen(skb));
  1723. skb_set_transport_header(csum_skb, 0);
  1724. csum_skb->csum_start =
  1725. (unsigned char *)tcp_hdr(csum_skb) -
  1726. csum_skb->head;
  1727. }
  1728. hdr_page->pos += snap_ip_tcp_hdrlen;
  1729. hdr_tb_len = hdr_page->pos - start_hdr;
  1730. hdr_tb_phys = dma_map_single(trans->dev, start_hdr,
  1731. hdr_tb_len, DMA_TO_DEVICE);
  1732. if (unlikely(dma_mapping_error(trans->dev, hdr_tb_phys))) {
  1733. dev_kfree_skb(csum_skb);
  1734. ret = -EINVAL;
  1735. goto out_unmap;
  1736. }
  1737. iwl_pcie_txq_build_tfd(trans, txq, hdr_tb_phys,
  1738. hdr_tb_len, false);
  1739. trace_iwlwifi_dev_tx_tso_chunk(trans->dev, start_hdr,
  1740. hdr_tb_len);
  1741. /* prepare the start_hdr for the next subframe */
  1742. start_hdr = hdr_page->pos;
  1743. /* put the payload */
  1744. while (data_left) {
  1745. unsigned int size = min_t(unsigned int, tso.size,
  1746. data_left);
  1747. dma_addr_t tb_phys;
  1748. if (trans_pcie->sw_csum_tx)
  1749. memcpy(skb_put(csum_skb, size), tso.data, size);
  1750. tb_phys = dma_map_single(trans->dev, tso.data,
  1751. size, DMA_TO_DEVICE);
  1752. if (unlikely(dma_mapping_error(trans->dev, tb_phys))) {
  1753. dev_kfree_skb(csum_skb);
  1754. ret = -EINVAL;
  1755. goto out_unmap;
  1756. }
  1757. iwl_pcie_txq_build_tfd(trans, txq, tb_phys,
  1758. size, false);
  1759. trace_iwlwifi_dev_tx_tso_chunk(trans->dev, tso.data,
  1760. size);
  1761. data_left -= size;
  1762. tso_build_data(skb, &tso, size);
  1763. }
  1764. /* For testing on early hardware only */
  1765. if (trans_pcie->sw_csum_tx) {
  1766. __wsum csum;
  1767. csum = skb_checksum(csum_skb,
  1768. skb_checksum_start_offset(csum_skb),
  1769. csum_skb->len -
  1770. skb_checksum_start_offset(csum_skb),
  1771. 0);
  1772. dev_kfree_skb(csum_skb);
  1773. dma_sync_single_for_cpu(trans->dev, hdr_tb_phys,
  1774. hdr_tb_len, DMA_TO_DEVICE);
  1775. tcph->check = csum_fold(csum);
  1776. dma_sync_single_for_device(trans->dev, hdr_tb_phys,
  1777. hdr_tb_len, DMA_TO_DEVICE);
  1778. }
  1779. }
  1780. /* re -add the WiFi header and IV */
  1781. skb_push(skb, hdr_len + iv_len);
  1782. return 0;
  1783. out_unmap:
  1784. iwl_pcie_tfd_unmap(trans, out_meta, &txq->tfds[q->write_ptr]);
  1785. return ret;
  1786. }
  1787. #else /* CONFIG_INET */
  1788. static int iwl_fill_data_tbs_amsdu(struct iwl_trans *trans, struct sk_buff *skb,
  1789. struct iwl_txq *txq, u8 hdr_len,
  1790. struct iwl_cmd_meta *out_meta,
  1791. struct iwl_device_cmd *dev_cmd, u16 tb1_len)
  1792. {
  1793. /* No A-MSDU without CONFIG_INET */
  1794. WARN_ON(1);
  1795. return -1;
  1796. }
  1797. #endif /* CONFIG_INET */
  1798. int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
  1799. struct iwl_device_cmd *dev_cmd, int txq_id)
  1800. {
  1801. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1802. struct ieee80211_hdr *hdr;
  1803. struct iwl_tx_cmd *tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  1804. struct iwl_cmd_meta *out_meta;
  1805. struct iwl_txq *txq;
  1806. struct iwl_queue *q;
  1807. dma_addr_t tb0_phys, tb1_phys, scratch_phys;
  1808. void *tb1_addr;
  1809. u16 len, tb1_len;
  1810. bool wait_write_ptr;
  1811. __le16 fc;
  1812. u8 hdr_len;
  1813. u16 wifi_seq;
  1814. txq = &trans_pcie->txq[txq_id];
  1815. q = &txq->q;
  1816. if (WARN_ONCE(!test_bit(txq_id, trans_pcie->queue_used),
  1817. "TX on unused queue %d\n", txq_id))
  1818. return -EINVAL;
  1819. if (unlikely(trans_pcie->sw_csum_tx &&
  1820. skb->ip_summed == CHECKSUM_PARTIAL)) {
  1821. int offs = skb_checksum_start_offset(skb);
  1822. int csum_offs = offs + skb->csum_offset;
  1823. __wsum csum;
  1824. if (skb_ensure_writable(skb, csum_offs + sizeof(__sum16)))
  1825. return -1;
  1826. csum = skb_checksum(skb, offs, skb->len - offs, 0);
  1827. *(__sum16 *)(skb->data + csum_offs) = csum_fold(csum);
  1828. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1829. }
  1830. if (skb_is_nonlinear(skb) &&
  1831. skb_shinfo(skb)->nr_frags > IWL_PCIE_MAX_FRAGS &&
  1832. __skb_linearize(skb))
  1833. return -ENOMEM;
  1834. /* mac80211 always puts the full header into the SKB's head,
  1835. * so there's no need to check if it's readable there
  1836. */
  1837. hdr = (struct ieee80211_hdr *)skb->data;
  1838. fc = hdr->frame_control;
  1839. hdr_len = ieee80211_hdrlen(fc);
  1840. spin_lock(&txq->lock);
  1841. if (iwl_queue_space(q) < q->high_mark) {
  1842. iwl_stop_queue(trans, txq);
  1843. /* don't put the packet on the ring, if there is no room */
  1844. if (unlikely(iwl_queue_space(q) < 3)) {
  1845. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  1846. info->driver_data[IWL_TRANS_FIRST_DRIVER_DATA + 1] =
  1847. dev_cmd;
  1848. __skb_queue_tail(&txq->overflow_q, skb);
  1849. spin_unlock(&txq->lock);
  1850. return 0;
  1851. }
  1852. }
  1853. /* In AGG mode, the index in the ring must correspond to the WiFi
  1854. * sequence number. This is a HW requirements to help the SCD to parse
  1855. * the BA.
  1856. * Check here that the packets are in the right place on the ring.
  1857. */
  1858. wifi_seq = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
  1859. WARN_ONCE(txq->ampdu &&
  1860. (wifi_seq & 0xff) != q->write_ptr,
  1861. "Q: %d WiFi Seq %d tfdNum %d",
  1862. txq_id, wifi_seq, q->write_ptr);
  1863. /* Set up driver data for this TFD */
  1864. txq->entries[q->write_ptr].skb = skb;
  1865. txq->entries[q->write_ptr].cmd = dev_cmd;
  1866. dev_cmd->hdr.sequence =
  1867. cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  1868. INDEX_TO_SEQ(q->write_ptr)));
  1869. tb0_phys = iwl_pcie_get_scratchbuf_dma(txq, q->write_ptr);
  1870. scratch_phys = tb0_phys + sizeof(struct iwl_cmd_header) +
  1871. offsetof(struct iwl_tx_cmd, scratch);
  1872. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  1873. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  1874. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  1875. out_meta = &txq->entries[q->write_ptr].meta;
  1876. out_meta->flags = 0;
  1877. /*
  1878. * The second TB (tb1) points to the remainder of the TX command
  1879. * and the 802.11 header - dword aligned size
  1880. * (This calculation modifies the TX command, so do it before the
  1881. * setup of the first TB)
  1882. */
  1883. len = sizeof(struct iwl_tx_cmd) + sizeof(struct iwl_cmd_header) +
  1884. hdr_len - IWL_HCMD_SCRATCHBUF_SIZE;
  1885. tb1_len = ALIGN(len, 4);
  1886. /* Tell NIC about any 2-byte padding after MAC header */
  1887. if (tb1_len != len)
  1888. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  1889. /* The first TB points to the scratchbuf data - min_copy bytes */
  1890. memcpy(&txq->scratchbufs[q->write_ptr], &dev_cmd->hdr,
  1891. IWL_HCMD_SCRATCHBUF_SIZE);
  1892. iwl_pcie_txq_build_tfd(trans, txq, tb0_phys,
  1893. IWL_HCMD_SCRATCHBUF_SIZE, true);
  1894. /* there must be data left over for TB1 or this code must be changed */
  1895. BUILD_BUG_ON(sizeof(struct iwl_tx_cmd) < IWL_HCMD_SCRATCHBUF_SIZE);
  1896. /* map the data for TB1 */
  1897. tb1_addr = ((u8 *)&dev_cmd->hdr) + IWL_HCMD_SCRATCHBUF_SIZE;
  1898. tb1_phys = dma_map_single(trans->dev, tb1_addr, tb1_len, DMA_TO_DEVICE);
  1899. if (unlikely(dma_mapping_error(trans->dev, tb1_phys)))
  1900. goto out_err;
  1901. iwl_pcie_txq_build_tfd(trans, txq, tb1_phys, tb1_len, false);
  1902. if (ieee80211_is_data_qos(fc) &&
  1903. (*ieee80211_get_qos_ctl(hdr) & IEEE80211_QOS_CTL_A_MSDU_PRESENT)) {
  1904. if (unlikely(iwl_fill_data_tbs_amsdu(trans, skb, txq, hdr_len,
  1905. out_meta, dev_cmd,
  1906. tb1_len)))
  1907. goto out_err;
  1908. } else if (unlikely(iwl_fill_data_tbs(trans, skb, txq, hdr_len,
  1909. out_meta, dev_cmd, tb1_len))) {
  1910. goto out_err;
  1911. }
  1912. /* Set up entry for this TFD in Tx byte-count array */
  1913. iwl_pcie_txq_update_byte_cnt_tbl(trans, txq, le16_to_cpu(tx_cmd->len));
  1914. wait_write_ptr = ieee80211_has_morefrags(fc);
  1915. /* start timer if queue currently empty */
  1916. if (q->read_ptr == q->write_ptr) {
  1917. if (txq->wd_timeout) {
  1918. /*
  1919. * If the TXQ is active, then set the timer, if not,
  1920. * set the timer in remainder so that the timer will
  1921. * be armed with the right value when the station will
  1922. * wake up.
  1923. */
  1924. if (!txq->frozen)
  1925. mod_timer(&txq->stuck_timer,
  1926. jiffies + txq->wd_timeout);
  1927. else
  1928. txq->frozen_expiry_remainder = txq->wd_timeout;
  1929. }
  1930. IWL_DEBUG_RPM(trans, "Q: %d first tx - take ref\n", q->id);
  1931. iwl_trans_pcie_ref(trans);
  1932. }
  1933. /* Tell device the write index *just past* this latest filled TFD */
  1934. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr);
  1935. if (!wait_write_ptr)
  1936. iwl_pcie_txq_inc_wr_ptr(trans, txq);
  1937. /*
  1938. * At this point the frame is "transmitted" successfully
  1939. * and we will get a TX status notification eventually.
  1940. */
  1941. spin_unlock(&txq->lock);
  1942. return 0;
  1943. out_err:
  1944. spin_unlock(&txq->lock);
  1945. return -1;
  1946. }