tx.c 69 KB

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