tx.c 67 KB

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