tx.c 68 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468
  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. #include "fw/api/tx.h"
  46. #define IWL_TX_CRC_SIZE 4
  47. #define IWL_TX_DELIMITER_SIZE 4
  48. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  49. * DMA services
  50. *
  51. * Theory of operation
  52. *
  53. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  54. * of buffer descriptors, each of which points to one or more data buffers for
  55. * the device to read from or fill. Driver and device exchange status of each
  56. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  57. * entries in each circular buffer, to protect against confusing empty and full
  58. * queue states.
  59. *
  60. * The device reads or writes the data in the queues via the device's several
  61. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  62. *
  63. * For Tx queue, there are low mark and high mark limits. If, after queuing
  64. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  65. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  66. * Tx queue resumed.
  67. *
  68. ***************************************************/
  69. int iwl_queue_space(const struct iwl_txq *q)
  70. {
  71. unsigned int max;
  72. unsigned int used;
  73. /*
  74. * To avoid ambiguity between empty and completely full queues, there
  75. * should always be less than TFD_QUEUE_SIZE_MAX elements in the queue.
  76. * If q->n_window is smaller than TFD_QUEUE_SIZE_MAX, there is no need
  77. * to reserve any queue entries for this purpose.
  78. */
  79. if (q->n_window < TFD_QUEUE_SIZE_MAX)
  80. max = q->n_window;
  81. else
  82. max = TFD_QUEUE_SIZE_MAX - 1;
  83. /*
  84. * TFD_QUEUE_SIZE_MAX is a power of 2, so the following is equivalent to
  85. * modulo by TFD_QUEUE_SIZE_MAX and is well defined.
  86. */
  87. used = (q->write_ptr - q->read_ptr) & (TFD_QUEUE_SIZE_MAX - 1);
  88. if (WARN_ON(used > max))
  89. return 0;
  90. return max - used;
  91. }
  92. /*
  93. * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
  94. */
  95. static int iwl_queue_init(struct iwl_txq *q, int slots_num)
  96. {
  97. q->n_window = slots_num;
  98. /* slots_num must be power-of-two size, otherwise
  99. * iwl_pcie_get_cmd_index is broken. */
  100. if (WARN_ON(!is_power_of_2(slots_num)))
  101. return -EINVAL;
  102. q->low_mark = q->n_window / 4;
  103. if (q->low_mark < 4)
  104. q->low_mark = 4;
  105. q->high_mark = q->n_window / 8;
  106. if (q->high_mark < 2)
  107. q->high_mark = 2;
  108. q->write_ptr = 0;
  109. q->read_ptr = 0;
  110. return 0;
  111. }
  112. int iwl_pcie_alloc_dma_ptr(struct iwl_trans *trans,
  113. struct iwl_dma_ptr *ptr, size_t size)
  114. {
  115. if (WARN_ON(ptr->addr))
  116. return -EINVAL;
  117. ptr->addr = dma_alloc_coherent(trans->dev, size,
  118. &ptr->dma, GFP_KERNEL);
  119. if (!ptr->addr)
  120. return -ENOMEM;
  121. ptr->size = size;
  122. return 0;
  123. }
  124. void iwl_pcie_free_dma_ptr(struct iwl_trans *trans, struct iwl_dma_ptr *ptr)
  125. {
  126. if (unlikely(!ptr->addr))
  127. return;
  128. dma_free_coherent(trans->dev, ptr->size, ptr->addr, ptr->dma);
  129. memset(ptr, 0, sizeof(*ptr));
  130. }
  131. static void iwl_pcie_txq_stuck_timer(struct timer_list *t)
  132. {
  133. struct iwl_txq *txq = from_timer(txq, t, stuck_timer);
  134. struct iwl_trans_pcie *trans_pcie = txq->trans_pcie;
  135. struct iwl_trans *trans = iwl_trans_pcie_get_trans(trans_pcie);
  136. spin_lock(&txq->lock);
  137. /* check if triggered erroneously */
  138. if (txq->read_ptr == txq->write_ptr) {
  139. spin_unlock(&txq->lock);
  140. return;
  141. }
  142. spin_unlock(&txq->lock);
  143. iwl_trans_pcie_log_scd_error(trans, txq);
  144. iwl_force_nmi(trans);
  145. }
  146. /*
  147. * iwl_pcie_txq_update_byte_cnt_tbl - Set up entry in Tx byte-count array
  148. */
  149. static void iwl_pcie_txq_update_byte_cnt_tbl(struct iwl_trans *trans,
  150. struct iwl_txq *txq, u16 byte_cnt,
  151. int num_tbs)
  152. {
  153. struct iwlagn_scd_bc_tbl *scd_bc_tbl;
  154. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  155. int write_ptr = txq->write_ptr;
  156. int txq_id = txq->id;
  157. u8 sec_ctl = 0;
  158. u16 len = byte_cnt + IWL_TX_CRC_SIZE + IWL_TX_DELIMITER_SIZE;
  159. __le16 bc_ent;
  160. struct iwl_tx_cmd *tx_cmd =
  161. (void *)txq->entries[txq->write_ptr].cmd->payload;
  162. u8 sta_id = tx_cmd->sta_id;
  163. scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
  164. sec_ctl = tx_cmd->sec_ctl;
  165. switch (sec_ctl & TX_CMD_SEC_MSK) {
  166. case TX_CMD_SEC_CCM:
  167. len += IEEE80211_CCMP_MIC_LEN;
  168. break;
  169. case TX_CMD_SEC_TKIP:
  170. len += IEEE80211_TKIP_ICV_LEN;
  171. break;
  172. case TX_CMD_SEC_WEP:
  173. len += IEEE80211_WEP_IV_LEN + IEEE80211_WEP_ICV_LEN;
  174. break;
  175. }
  176. if (trans_pcie->bc_table_dword)
  177. len = DIV_ROUND_UP(len, 4);
  178. if (WARN_ON(len > 0xFFF || write_ptr >= TFD_QUEUE_SIZE_MAX))
  179. return;
  180. bc_ent = cpu_to_le16(len | (sta_id << 12));
  181. scd_bc_tbl[txq_id].tfd_offset[write_ptr] = bc_ent;
  182. if (write_ptr < TFD_QUEUE_SIZE_BC_DUP)
  183. scd_bc_tbl[txq_id].
  184. tfd_offset[TFD_QUEUE_SIZE_MAX + write_ptr] = bc_ent;
  185. }
  186. static void iwl_pcie_txq_inval_byte_cnt_tbl(struct iwl_trans *trans,
  187. struct iwl_txq *txq)
  188. {
  189. struct iwl_trans_pcie *trans_pcie =
  190. IWL_TRANS_GET_PCIE_TRANS(trans);
  191. struct iwlagn_scd_bc_tbl *scd_bc_tbl = trans_pcie->scd_bc_tbls.addr;
  192. int txq_id = txq->id;
  193. int read_ptr = txq->read_ptr;
  194. u8 sta_id = 0;
  195. __le16 bc_ent;
  196. struct iwl_tx_cmd *tx_cmd =
  197. (void *)txq->entries[read_ptr].cmd->payload;
  198. WARN_ON(read_ptr >= TFD_QUEUE_SIZE_MAX);
  199. if (txq_id != trans_pcie->cmd_queue)
  200. sta_id = tx_cmd->sta_id;
  201. bc_ent = cpu_to_le16(1 | (sta_id << 12));
  202. scd_bc_tbl[txq_id].tfd_offset[read_ptr] = bc_ent;
  203. if (read_ptr < TFD_QUEUE_SIZE_BC_DUP)
  204. scd_bc_tbl[txq_id].
  205. tfd_offset[TFD_QUEUE_SIZE_MAX + read_ptr] = bc_ent;
  206. }
  207. /*
  208. * iwl_pcie_txq_inc_wr_ptr - Send new write index to hardware
  209. */
  210. static void iwl_pcie_txq_inc_wr_ptr(struct iwl_trans *trans,
  211. struct iwl_txq *txq)
  212. {
  213. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  214. u32 reg = 0;
  215. int txq_id = txq->id;
  216. lockdep_assert_held(&txq->lock);
  217. /*
  218. * explicitly wake up the NIC if:
  219. * 1. shadow registers aren't enabled
  220. * 2. NIC is woken up for CMD regardless of shadow outside this function
  221. * 3. there is a chance that the NIC is asleep
  222. */
  223. if (!trans->cfg->base_params->shadow_reg_enable &&
  224. txq_id != trans_pcie->cmd_queue &&
  225. test_bit(STATUS_TPOWER_PMI, &trans->status)) {
  226. /*
  227. * wake up nic if it's powered down ...
  228. * uCode will wake up, and interrupt us again, so next
  229. * time we'll skip this part.
  230. */
  231. reg = iwl_read32(trans, CSR_UCODE_DRV_GP1);
  232. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  233. IWL_DEBUG_INFO(trans, "Tx queue %d requesting wakeup, GP1 = 0x%x\n",
  234. txq_id, reg);
  235. iwl_set_bit(trans, CSR_GP_CNTRL,
  236. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  237. txq->need_update = true;
  238. return;
  239. }
  240. }
  241. /*
  242. * if not in power-save mode, uCode will never sleep when we're
  243. * trying to tx (during RFKILL, we're not trying to tx).
  244. */
  245. IWL_DEBUG_TX(trans, "Q:%d WR: 0x%x\n", txq_id, txq->write_ptr);
  246. if (!txq->block)
  247. iwl_write32(trans, HBUS_TARG_WRPTR,
  248. txq->write_ptr | (txq_id << 8));
  249. }
  250. void iwl_pcie_txq_check_wrptrs(struct iwl_trans *trans)
  251. {
  252. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  253. int i;
  254. for (i = 0; i < trans->cfg->base_params->num_of_queues; i++) {
  255. struct iwl_txq *txq = trans_pcie->txq[i];
  256. if (!test_bit(i, trans_pcie->queue_used))
  257. continue;
  258. spin_lock_bh(&txq->lock);
  259. if (txq->need_update) {
  260. iwl_pcie_txq_inc_wr_ptr(trans, txq);
  261. txq->need_update = false;
  262. }
  263. spin_unlock_bh(&txq->lock);
  264. }
  265. }
  266. static inline dma_addr_t iwl_pcie_tfd_tb_get_addr(struct iwl_trans *trans,
  267. void *_tfd, u8 idx)
  268. {
  269. if (trans->cfg->use_tfh) {
  270. struct iwl_tfh_tfd *tfd = _tfd;
  271. struct iwl_tfh_tb *tb = &tfd->tbs[idx];
  272. return (dma_addr_t)(le64_to_cpu(tb->addr));
  273. } else {
  274. struct iwl_tfd *tfd = _tfd;
  275. struct iwl_tfd_tb *tb = &tfd->tbs[idx];
  276. dma_addr_t addr = get_unaligned_le32(&tb->lo);
  277. dma_addr_t hi_len;
  278. if (sizeof(dma_addr_t) <= sizeof(u32))
  279. return addr;
  280. hi_len = le16_to_cpu(tb->hi_n_len) & 0xF;
  281. /*
  282. * shift by 16 twice to avoid warnings on 32-bit
  283. * (where this code never runs anyway due to the
  284. * if statement above)
  285. */
  286. return addr | ((hi_len << 16) << 16);
  287. }
  288. }
  289. static inline void iwl_pcie_tfd_set_tb(struct iwl_trans *trans, void *tfd,
  290. u8 idx, dma_addr_t addr, u16 len)
  291. {
  292. struct iwl_tfd *tfd_fh = (void *)tfd;
  293. struct iwl_tfd_tb *tb = &tfd_fh->tbs[idx];
  294. u16 hi_n_len = len << 4;
  295. put_unaligned_le32(addr, &tb->lo);
  296. hi_n_len |= iwl_get_dma_hi_addr(addr);
  297. tb->hi_n_len = cpu_to_le16(hi_n_len);
  298. tfd_fh->num_tbs = idx + 1;
  299. }
  300. static inline u8 iwl_pcie_tfd_get_num_tbs(struct iwl_trans *trans, void *_tfd)
  301. {
  302. if (trans->cfg->use_tfh) {
  303. struct iwl_tfh_tfd *tfd = _tfd;
  304. return le16_to_cpu(tfd->num_tbs) & 0x1f;
  305. } else {
  306. struct iwl_tfd *tfd = _tfd;
  307. return tfd->num_tbs & 0x1f;
  308. }
  309. }
  310. static void iwl_pcie_tfd_unmap(struct iwl_trans *trans,
  311. struct iwl_cmd_meta *meta,
  312. struct iwl_txq *txq, int index)
  313. {
  314. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  315. int i, num_tbs;
  316. void *tfd = iwl_pcie_get_tfd(trans, txq, index);
  317. /* Sanity check on number of chunks */
  318. num_tbs = iwl_pcie_tfd_get_num_tbs(trans, tfd);
  319. if (num_tbs > trans_pcie->max_tbs) {
  320. IWL_ERR(trans, "Too many chunks: %i\n", num_tbs);
  321. /* @todo issue fatal error, it is quite serious situation */
  322. return;
  323. }
  324. /* first TB is never freed - it's the bidirectional DMA data */
  325. for (i = 1; i < num_tbs; i++) {
  326. if (meta->tbs & BIT(i))
  327. dma_unmap_page(trans->dev,
  328. iwl_pcie_tfd_tb_get_addr(trans, tfd, i),
  329. iwl_pcie_tfd_tb_get_len(trans, tfd, i),
  330. DMA_TO_DEVICE);
  331. else
  332. dma_unmap_single(trans->dev,
  333. iwl_pcie_tfd_tb_get_addr(trans, tfd,
  334. i),
  335. iwl_pcie_tfd_tb_get_len(trans, tfd,
  336. i),
  337. DMA_TO_DEVICE);
  338. }
  339. if (trans->cfg->use_tfh) {
  340. struct iwl_tfh_tfd *tfd_fh = (void *)tfd;
  341. tfd_fh->num_tbs = 0;
  342. } else {
  343. struct iwl_tfd *tfd_fh = (void *)tfd;
  344. tfd_fh->num_tbs = 0;
  345. }
  346. }
  347. /*
  348. * iwl_pcie_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
  349. * @trans - transport private data
  350. * @txq - tx queue
  351. * @dma_dir - the direction of the DMA mapping
  352. *
  353. * Does NOT advance any TFD circular buffer read/write indexes
  354. * Does NOT free the TFD itself (which is within circular buffer)
  355. */
  356. void iwl_pcie_txq_free_tfd(struct iwl_trans *trans, struct iwl_txq *txq)
  357. {
  358. /* rd_ptr is bounded by TFD_QUEUE_SIZE_MAX and
  359. * idx is bounded by n_window
  360. */
  361. int rd_ptr = txq->read_ptr;
  362. int idx = iwl_pcie_get_cmd_index(txq, rd_ptr);
  363. lockdep_assert_held(&txq->lock);
  364. /* We have only q->n_window txq->entries, but we use
  365. * TFD_QUEUE_SIZE_MAX tfds
  366. */
  367. iwl_pcie_tfd_unmap(trans, &txq->entries[idx].meta, txq, rd_ptr);
  368. /* free SKB */
  369. if (txq->entries) {
  370. struct sk_buff *skb;
  371. skb = txq->entries[idx].skb;
  372. /* Can be called from irqs-disabled context
  373. * If skb is not NULL, it means that the whole queue is being
  374. * freed and that the queue is not empty - free the skb
  375. */
  376. if (skb) {
  377. iwl_op_mode_free_skb(trans->op_mode, skb);
  378. txq->entries[idx].skb = NULL;
  379. }
  380. }
  381. }
  382. static int iwl_pcie_txq_build_tfd(struct iwl_trans *trans, struct iwl_txq *txq,
  383. dma_addr_t addr, u16 len, bool reset)
  384. {
  385. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  386. void *tfd;
  387. u32 num_tbs;
  388. tfd = txq->tfds + trans_pcie->tfd_size * txq->write_ptr;
  389. if (reset)
  390. memset(tfd, 0, trans_pcie->tfd_size);
  391. num_tbs = iwl_pcie_tfd_get_num_tbs(trans, tfd);
  392. /* Each TFD can point to a maximum max_tbs Tx buffers */
  393. if (num_tbs >= trans_pcie->max_tbs) {
  394. IWL_ERR(trans, "Error can not send more than %d chunks\n",
  395. trans_pcie->max_tbs);
  396. return -EINVAL;
  397. }
  398. if (WARN(addr & ~IWL_TX_DMA_MASK,
  399. "Unaligned address = %llx\n", (unsigned long long)addr))
  400. return -EINVAL;
  401. iwl_pcie_tfd_set_tb(trans, tfd, num_tbs, addr, len);
  402. return num_tbs;
  403. }
  404. int iwl_pcie_txq_alloc(struct iwl_trans *trans, struct iwl_txq *txq,
  405. int slots_num, bool cmd_queue)
  406. {
  407. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  408. size_t tfd_sz = trans_pcie->tfd_size * TFD_QUEUE_SIZE_MAX;
  409. size_t tb0_buf_sz;
  410. int i;
  411. if (WARN_ON(txq->entries || txq->tfds))
  412. return -EINVAL;
  413. timer_setup(&txq->stuck_timer, iwl_pcie_txq_stuck_timer, 0);
  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. 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 ? trans_pcie->tx_cmd_queue_size :
  776. TFD_TX_CMD_SLOTS;
  777. trans_pcie->txq[txq_id] = &trans_pcie->txq_memory[txq_id];
  778. ret = iwl_pcie_txq_alloc(trans, trans_pcie->txq[txq_id],
  779. slots_num, cmd_queue);
  780. if (ret) {
  781. IWL_ERR(trans, "Tx %d queue alloc failed\n", txq_id);
  782. goto error;
  783. }
  784. trans_pcie->txq[txq_id]->id = txq_id;
  785. }
  786. return 0;
  787. error:
  788. iwl_pcie_tx_free(trans);
  789. return ret;
  790. }
  791. void iwl_pcie_set_tx_cmd_queue_size(struct iwl_trans *trans)
  792. {
  793. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  794. int queue_size = TFD_CMD_SLOTS;
  795. if (trans->cfg->tx_cmd_queue_size)
  796. queue_size = trans->cfg->tx_cmd_queue_size;
  797. if (WARN_ON(!(is_power_of_2(queue_size) &&
  798. TFD_QUEUE_CB_SIZE(queue_size) > 0)))
  799. trans_pcie->tx_cmd_queue_size = TFD_CMD_SLOTS;
  800. else
  801. trans_pcie->tx_cmd_queue_size = queue_size;
  802. }
  803. int iwl_pcie_tx_init(struct iwl_trans *trans)
  804. {
  805. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  806. int ret;
  807. int txq_id, slots_num;
  808. bool alloc = false;
  809. iwl_pcie_set_tx_cmd_queue_size(trans);
  810. if (!trans_pcie->txq_memory) {
  811. ret = iwl_pcie_tx_alloc(trans);
  812. if (ret)
  813. goto error;
  814. alloc = true;
  815. }
  816. spin_lock(&trans_pcie->irq_lock);
  817. /* Turn off all Tx DMA fifos */
  818. iwl_scd_deactivate_fifos(trans);
  819. /* Tell NIC where to find the "keep warm" buffer */
  820. iwl_write_direct32(trans, FH_KW_MEM_ADDR_REG,
  821. trans_pcie->kw.dma >> 4);
  822. spin_unlock(&trans_pcie->irq_lock);
  823. /* Alloc and init all Tx queues, including the command queue (#4/#9) */
  824. for (txq_id = 0; txq_id < trans->cfg->base_params->num_of_queues;
  825. txq_id++) {
  826. bool cmd_queue = (txq_id == trans_pcie->cmd_queue);
  827. slots_num = cmd_queue ? trans_pcie->tx_cmd_queue_size :
  828. TFD_TX_CMD_SLOTS;
  829. ret = iwl_pcie_txq_init(trans, trans_pcie->txq[txq_id],
  830. slots_num, cmd_queue);
  831. if (ret) {
  832. IWL_ERR(trans, "Tx %d queue init failed\n", txq_id);
  833. goto error;
  834. }
  835. /*
  836. * Tell nic where to find circular buffer of TFDs for a
  837. * given Tx queue, and enable the DMA channel used for that
  838. * queue.
  839. * Circular buffer (TFD queue in DRAM) physical base address
  840. */
  841. iwl_write_direct32(trans, FH_MEM_CBBC_QUEUE(trans, txq_id),
  842. trans_pcie->txq[txq_id]->dma_addr >> 8);
  843. }
  844. iwl_set_bits_prph(trans, SCD_GP_CTRL, SCD_GP_CTRL_AUTO_ACTIVE_MODE);
  845. if (trans->cfg->base_params->num_of_queues > 20)
  846. iwl_set_bits_prph(trans, SCD_GP_CTRL,
  847. SCD_GP_CTRL_ENABLE_31_QUEUES);
  848. return 0;
  849. error:
  850. /*Upon error, free only if we allocated something */
  851. if (alloc)
  852. iwl_pcie_tx_free(trans);
  853. return ret;
  854. }
  855. static inline void iwl_pcie_txq_progress(struct iwl_txq *txq)
  856. {
  857. lockdep_assert_held(&txq->lock);
  858. if (!txq->wd_timeout)
  859. return;
  860. /*
  861. * station is asleep and we send data - that must
  862. * be uAPSD or PS-Poll. Don't rearm the timer.
  863. */
  864. if (txq->frozen)
  865. return;
  866. /*
  867. * if empty delete timer, otherwise move timer forward
  868. * since we're making progress on this queue
  869. */
  870. if (txq->read_ptr == txq->write_ptr)
  871. del_timer(&txq->stuck_timer);
  872. else
  873. mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
  874. }
  875. /* Frees buffers until index _not_ inclusive */
  876. void iwl_trans_pcie_reclaim(struct iwl_trans *trans, int txq_id, int ssn,
  877. struct sk_buff_head *skbs)
  878. {
  879. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  880. struct iwl_txq *txq = trans_pcie->txq[txq_id];
  881. int tfd_num = ssn & (TFD_QUEUE_SIZE_MAX - 1);
  882. int last_to_free;
  883. /* This function is not meant to release cmd queue*/
  884. if (WARN_ON(txq_id == trans_pcie->cmd_queue))
  885. return;
  886. spin_lock_bh(&txq->lock);
  887. if (!test_bit(txq_id, trans_pcie->queue_used)) {
  888. IWL_DEBUG_TX_QUEUES(trans, "Q %d inactive - ignoring idx %d\n",
  889. txq_id, ssn);
  890. goto out;
  891. }
  892. if (txq->read_ptr == tfd_num)
  893. goto out;
  894. IWL_DEBUG_TX_REPLY(trans, "[Q %d] %d -> %d (%d)\n",
  895. txq_id, txq->read_ptr, tfd_num, ssn);
  896. /*Since we free until index _not_ inclusive, the one before index is
  897. * the last we will free. This one must be used */
  898. last_to_free = iwl_queue_dec_wrap(tfd_num);
  899. if (!iwl_queue_used(txq, last_to_free)) {
  900. IWL_ERR(trans,
  901. "%s: Read index for DMA queue txq id (%d), last_to_free %d is out of range [0-%d] %d %d.\n",
  902. __func__, txq_id, last_to_free, TFD_QUEUE_SIZE_MAX,
  903. txq->write_ptr, txq->read_ptr);
  904. goto out;
  905. }
  906. if (WARN_ON(!skb_queue_empty(skbs)))
  907. goto out;
  908. for (;
  909. txq->read_ptr != tfd_num;
  910. txq->read_ptr = iwl_queue_inc_wrap(txq->read_ptr)) {
  911. int idx = iwl_pcie_get_cmd_index(txq, txq->read_ptr);
  912. struct sk_buff *skb = txq->entries[idx].skb;
  913. if (WARN_ON_ONCE(!skb))
  914. continue;
  915. iwl_pcie_free_tso_page(trans_pcie, skb);
  916. __skb_queue_tail(skbs, skb);
  917. txq->entries[idx].skb = NULL;
  918. if (!trans->cfg->use_tfh)
  919. iwl_pcie_txq_inval_byte_cnt_tbl(trans, txq);
  920. iwl_pcie_txq_free_tfd(trans, txq);
  921. }
  922. iwl_pcie_txq_progress(txq);
  923. if (iwl_queue_space(txq) > txq->low_mark &&
  924. test_bit(txq_id, trans_pcie->queue_stopped)) {
  925. struct sk_buff_head overflow_skbs;
  926. __skb_queue_head_init(&overflow_skbs);
  927. skb_queue_splice_init(&txq->overflow_q, &overflow_skbs);
  928. /*
  929. * This is tricky: we are in reclaim path which is non
  930. * re-entrant, so noone will try to take the access the
  931. * txq data from that path. We stopped tx, so we can't
  932. * have tx as well. Bottom line, we can unlock and re-lock
  933. * later.
  934. */
  935. spin_unlock_bh(&txq->lock);
  936. while (!skb_queue_empty(&overflow_skbs)) {
  937. struct sk_buff *skb = __skb_dequeue(&overflow_skbs);
  938. struct iwl_device_cmd *dev_cmd_ptr;
  939. dev_cmd_ptr = *(void **)((u8 *)skb->cb +
  940. trans_pcie->dev_cmd_offs);
  941. /*
  942. * Note that we can very well be overflowing again.
  943. * In that case, iwl_queue_space will be small again
  944. * and we won't wake mac80211's queue.
  945. */
  946. iwl_trans_pcie_tx(trans, skb, dev_cmd_ptr, txq_id);
  947. }
  948. spin_lock_bh(&txq->lock);
  949. if (iwl_queue_space(txq) > txq->low_mark)
  950. iwl_wake_queue(trans, txq);
  951. }
  952. if (txq->read_ptr == txq->write_ptr) {
  953. IWL_DEBUG_RPM(trans, "Q %d - last tx reclaimed\n", txq->id);
  954. iwl_trans_unref(trans);
  955. }
  956. out:
  957. spin_unlock_bh(&txq->lock);
  958. }
  959. static int iwl_pcie_set_cmd_in_flight(struct iwl_trans *trans,
  960. const struct iwl_host_cmd *cmd)
  961. {
  962. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  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 (trans->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. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  981. ret = iwl_poll_bit(trans, CSR_GP_CNTRL,
  982. CSR_GP_CNTRL_REG_VAL_MAC_ACCESS_EN,
  983. (CSR_GP_CNTRL_REG_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. CSR_GP_CNTRL_REG_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. lockdep_assert_held(&txq->lock);
  1010. if ((idx >= TFD_QUEUE_SIZE_MAX) || (!iwl_queue_used(txq, idx))) {
  1011. IWL_ERR(trans,
  1012. "%s: Read index for DMA queue txq id (%d), index %d is out of range [0-%d] %d %d.\n",
  1013. __func__, txq_id, idx, TFD_QUEUE_SIZE_MAX,
  1014. txq->write_ptr, txq->read_ptr);
  1015. return;
  1016. }
  1017. for (idx = iwl_queue_inc_wrap(idx); txq->read_ptr != idx;
  1018. txq->read_ptr = iwl_queue_inc_wrap(txq->read_ptr)) {
  1019. if (nfreed++ > 0) {
  1020. IWL_ERR(trans, "HCMD skipped: index (%d) %d %d\n",
  1021. idx, txq->write_ptr, txq->read_ptr);
  1022. iwl_force_nmi(trans);
  1023. }
  1024. }
  1025. if (txq->read_ptr == txq->write_ptr) {
  1026. spin_lock_irqsave(&trans_pcie->reg_lock, flags);
  1027. iwl_pcie_clear_cmd_in_flight(trans);
  1028. spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
  1029. }
  1030. iwl_pcie_txq_progress(txq);
  1031. }
  1032. static int iwl_pcie_txq_set_ratid_map(struct iwl_trans *trans, u16 ra_tid,
  1033. u16 txq_id)
  1034. {
  1035. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1036. u32 tbl_dw_addr;
  1037. u32 tbl_dw;
  1038. u16 scd_q2ratid;
  1039. scd_q2ratid = ra_tid & SCD_QUEUE_RA_TID_MAP_RATID_MSK;
  1040. tbl_dw_addr = trans_pcie->scd_base_addr +
  1041. SCD_TRANS_TBL_OFFSET_QUEUE(txq_id);
  1042. tbl_dw = iwl_trans_read_mem32(trans, tbl_dw_addr);
  1043. if (txq_id & 0x1)
  1044. tbl_dw = (scd_q2ratid << 16) | (tbl_dw & 0x0000FFFF);
  1045. else
  1046. tbl_dw = scd_q2ratid | (tbl_dw & 0xFFFF0000);
  1047. iwl_trans_write_mem32(trans, tbl_dw_addr, tbl_dw);
  1048. return 0;
  1049. }
  1050. /* Receiver address (actually, Rx station's index into station table),
  1051. * combined with Traffic ID (QOS priority), in format used by Tx Scheduler */
  1052. #define BUILD_RAxTID(sta_id, tid) (((sta_id) << 4) + (tid))
  1053. bool iwl_trans_pcie_txq_enable(struct iwl_trans *trans, int txq_id, u16 ssn,
  1054. const struct iwl_trans_txq_scd_cfg *cfg,
  1055. unsigned int wdg_timeout)
  1056. {
  1057. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1058. struct iwl_txq *txq = trans_pcie->txq[txq_id];
  1059. int fifo = -1;
  1060. bool scd_bug = false;
  1061. if (test_and_set_bit(txq_id, trans_pcie->queue_used))
  1062. WARN_ONCE(1, "queue %d already used - expect issues", txq_id);
  1063. txq->wd_timeout = msecs_to_jiffies(wdg_timeout);
  1064. if (cfg) {
  1065. fifo = cfg->fifo;
  1066. /* Disable the scheduler prior configuring the cmd queue */
  1067. if (txq_id == trans_pcie->cmd_queue &&
  1068. trans_pcie->scd_set_active)
  1069. iwl_scd_enable_set_active(trans, 0);
  1070. /* Stop this Tx queue before configuring it */
  1071. iwl_scd_txq_set_inactive(trans, txq_id);
  1072. /* Set this queue as a chain-building queue unless it is CMD */
  1073. if (txq_id != trans_pcie->cmd_queue)
  1074. iwl_scd_txq_set_chain(trans, txq_id);
  1075. if (cfg->aggregate) {
  1076. u16 ra_tid = BUILD_RAxTID(cfg->sta_id, cfg->tid);
  1077. /* Map receiver-address / traffic-ID to this queue */
  1078. iwl_pcie_txq_set_ratid_map(trans, ra_tid, txq_id);
  1079. /* enable aggregations for the queue */
  1080. iwl_scd_txq_enable_agg(trans, txq_id);
  1081. txq->ampdu = true;
  1082. } else {
  1083. /*
  1084. * disable aggregations for the queue, this will also
  1085. * make the ra_tid mapping configuration irrelevant
  1086. * since it is now a non-AGG queue.
  1087. */
  1088. iwl_scd_txq_disable_agg(trans, txq_id);
  1089. ssn = txq->read_ptr;
  1090. }
  1091. } else {
  1092. /*
  1093. * If we need to move the SCD write pointer by steps of
  1094. * 0x40, 0x80 or 0xc0, it gets stuck. Avoids this and let
  1095. * the op_mode know by returning true later.
  1096. * Do this only in case cfg is NULL since this trick can
  1097. * be done only if we have DQA enabled which is true for mvm
  1098. * only. And mvm never sets a cfg pointer.
  1099. * This is really ugly, but this is the easiest way out for
  1100. * this sad hardware issue.
  1101. * This bug has been fixed on devices 9000 and up.
  1102. */
  1103. scd_bug = !trans->cfg->mq_rx_supported &&
  1104. !((ssn - txq->write_ptr) & 0x3f) &&
  1105. (ssn != txq->write_ptr);
  1106. if (scd_bug)
  1107. ssn++;
  1108. }
  1109. /* Place first TFD at index corresponding to start sequence number.
  1110. * Assumes that ssn_idx is valid (!= 0xFFF) */
  1111. txq->read_ptr = (ssn & 0xff);
  1112. txq->write_ptr = (ssn & 0xff);
  1113. iwl_write_direct32(trans, HBUS_TARG_WRPTR,
  1114. (ssn & 0xff) | (txq_id << 8));
  1115. if (cfg) {
  1116. u8 frame_limit = cfg->frame_limit;
  1117. iwl_write_prph(trans, SCD_QUEUE_RDPTR(txq_id), ssn);
  1118. /* Set up Tx window size and frame limit for this queue */
  1119. iwl_trans_write_mem32(trans, trans_pcie->scd_base_addr +
  1120. SCD_CONTEXT_QUEUE_OFFSET(txq_id), 0);
  1121. iwl_trans_write_mem32(trans,
  1122. trans_pcie->scd_base_addr +
  1123. SCD_CONTEXT_QUEUE_OFFSET(txq_id) + sizeof(u32),
  1124. SCD_QUEUE_CTX_REG2_VAL(WIN_SIZE, frame_limit) |
  1125. SCD_QUEUE_CTX_REG2_VAL(FRAME_LIMIT, frame_limit));
  1126. /* Set up status area in SRAM, map to Tx DMA/FIFO, activate */
  1127. iwl_write_prph(trans, SCD_QUEUE_STATUS_BITS(txq_id),
  1128. (1 << SCD_QUEUE_STTS_REG_POS_ACTIVE) |
  1129. (cfg->fifo << SCD_QUEUE_STTS_REG_POS_TXF) |
  1130. (1 << SCD_QUEUE_STTS_REG_POS_WSL) |
  1131. SCD_QUEUE_STTS_REG_MSK);
  1132. /* enable the scheduler for this queue (only) */
  1133. if (txq_id == trans_pcie->cmd_queue &&
  1134. trans_pcie->scd_set_active)
  1135. iwl_scd_enable_set_active(trans, BIT(txq_id));
  1136. IWL_DEBUG_TX_QUEUES(trans,
  1137. "Activate queue %d on FIFO %d WrPtr: %d\n",
  1138. txq_id, fifo, ssn & 0xff);
  1139. } else {
  1140. IWL_DEBUG_TX_QUEUES(trans,
  1141. "Activate queue %d WrPtr: %d\n",
  1142. txq_id, ssn & 0xff);
  1143. }
  1144. return scd_bug;
  1145. }
  1146. void iwl_trans_pcie_txq_set_shared_mode(struct iwl_trans *trans, u32 txq_id,
  1147. bool shared_mode)
  1148. {
  1149. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1150. struct iwl_txq *txq = trans_pcie->txq[txq_id];
  1151. txq->ampdu = !shared_mode;
  1152. }
  1153. void iwl_trans_pcie_txq_disable(struct iwl_trans *trans, int txq_id,
  1154. bool configure_scd)
  1155. {
  1156. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1157. u32 stts_addr = trans_pcie->scd_base_addr +
  1158. SCD_TX_STTS_QUEUE_OFFSET(txq_id);
  1159. static const u32 zero_val[4] = {};
  1160. trans_pcie->txq[txq_id]->frozen_expiry_remainder = 0;
  1161. trans_pcie->txq[txq_id]->frozen = false;
  1162. /*
  1163. * Upon HW Rfkill - we stop the device, and then stop the queues
  1164. * in the op_mode. Just for the sake of the simplicity of the op_mode,
  1165. * allow the op_mode to call txq_disable after it already called
  1166. * stop_device.
  1167. */
  1168. if (!test_and_clear_bit(txq_id, trans_pcie->queue_used)) {
  1169. WARN_ONCE(test_bit(STATUS_DEVICE_ENABLED, &trans->status),
  1170. "queue %d not used", txq_id);
  1171. return;
  1172. }
  1173. if (configure_scd) {
  1174. iwl_scd_txq_set_inactive(trans, txq_id);
  1175. iwl_trans_write_mem(trans, stts_addr, (void *)zero_val,
  1176. ARRAY_SIZE(zero_val));
  1177. }
  1178. iwl_pcie_txq_unmap(trans, txq_id);
  1179. trans_pcie->txq[txq_id]->ampdu = false;
  1180. IWL_DEBUG_TX_QUEUES(trans, "Deactivate queue %d\n", txq_id);
  1181. }
  1182. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  1183. /*
  1184. * iwl_pcie_enqueue_hcmd - enqueue a uCode command
  1185. * @priv: device private data point
  1186. * @cmd: a pointer to the ucode command structure
  1187. *
  1188. * The function returns < 0 values to indicate the operation
  1189. * failed. On success, it returns the index (>= 0) of command in the
  1190. * command queue.
  1191. */
  1192. static int iwl_pcie_enqueue_hcmd(struct iwl_trans *trans,
  1193. struct iwl_host_cmd *cmd)
  1194. {
  1195. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1196. struct iwl_txq *txq = trans_pcie->txq[trans_pcie->cmd_queue];
  1197. struct iwl_device_cmd *out_cmd;
  1198. struct iwl_cmd_meta *out_meta;
  1199. unsigned long flags;
  1200. void *dup_buf = NULL;
  1201. dma_addr_t phys_addr;
  1202. int idx;
  1203. u16 copy_size, cmd_size, tb0_size;
  1204. bool had_nocopy = false;
  1205. u8 group_id = iwl_cmd_groupid(cmd->id);
  1206. int i, ret;
  1207. u32 cmd_pos;
  1208. const u8 *cmddata[IWL_MAX_CMD_TBS_PER_TFD];
  1209. u16 cmdlen[IWL_MAX_CMD_TBS_PER_TFD];
  1210. if (WARN(!trans->wide_cmd_header &&
  1211. group_id > IWL_ALWAYS_LONG_GROUP,
  1212. "unsupported wide command %#x\n", cmd->id))
  1213. return -EINVAL;
  1214. if (group_id != 0) {
  1215. copy_size = sizeof(struct iwl_cmd_header_wide);
  1216. cmd_size = sizeof(struct iwl_cmd_header_wide);
  1217. } else {
  1218. copy_size = sizeof(struct iwl_cmd_header);
  1219. cmd_size = sizeof(struct iwl_cmd_header);
  1220. }
  1221. /* need one for the header if the first is NOCOPY */
  1222. BUILD_BUG_ON(IWL_MAX_CMD_TBS_PER_TFD > IWL_NUM_OF_TBS - 1);
  1223. for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
  1224. cmddata[i] = cmd->data[i];
  1225. cmdlen[i] = cmd->len[i];
  1226. if (!cmd->len[i])
  1227. continue;
  1228. /* need at least IWL_FIRST_TB_SIZE copied */
  1229. if (copy_size < IWL_FIRST_TB_SIZE) {
  1230. int copy = IWL_FIRST_TB_SIZE - copy_size;
  1231. if (copy > cmdlen[i])
  1232. copy = cmdlen[i];
  1233. cmdlen[i] -= copy;
  1234. cmddata[i] += copy;
  1235. copy_size += copy;
  1236. }
  1237. if (cmd->dataflags[i] & IWL_HCMD_DFL_NOCOPY) {
  1238. had_nocopy = true;
  1239. if (WARN_ON(cmd->dataflags[i] & IWL_HCMD_DFL_DUP)) {
  1240. idx = -EINVAL;
  1241. goto free_dup_buf;
  1242. }
  1243. } else if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP) {
  1244. /*
  1245. * This is also a chunk that isn't copied
  1246. * to the static buffer so set had_nocopy.
  1247. */
  1248. had_nocopy = true;
  1249. /* only allowed once */
  1250. if (WARN_ON(dup_buf)) {
  1251. idx = -EINVAL;
  1252. goto free_dup_buf;
  1253. }
  1254. dup_buf = kmemdup(cmddata[i], cmdlen[i],
  1255. GFP_ATOMIC);
  1256. if (!dup_buf)
  1257. return -ENOMEM;
  1258. } else {
  1259. /* NOCOPY must not be followed by normal! */
  1260. if (WARN_ON(had_nocopy)) {
  1261. idx = -EINVAL;
  1262. goto free_dup_buf;
  1263. }
  1264. copy_size += cmdlen[i];
  1265. }
  1266. cmd_size += cmd->len[i];
  1267. }
  1268. /*
  1269. * If any of the command structures end up being larger than
  1270. * the TFD_MAX_PAYLOAD_SIZE and they aren't dynamically
  1271. * allocated into separate TFDs, then we will need to
  1272. * increase the size of the buffers.
  1273. */
  1274. if (WARN(copy_size > TFD_MAX_PAYLOAD_SIZE,
  1275. "Command %s (%#x) is too large (%d bytes)\n",
  1276. iwl_get_cmd_string(trans, cmd->id),
  1277. cmd->id, copy_size)) {
  1278. idx = -EINVAL;
  1279. goto free_dup_buf;
  1280. }
  1281. spin_lock_bh(&txq->lock);
  1282. if (iwl_queue_space(txq) < ((cmd->flags & CMD_ASYNC) ? 2 : 1)) {
  1283. spin_unlock_bh(&txq->lock);
  1284. IWL_ERR(trans, "No space in command queue\n");
  1285. iwl_op_mode_cmd_queue_full(trans->op_mode);
  1286. idx = -ENOSPC;
  1287. goto free_dup_buf;
  1288. }
  1289. idx = iwl_pcie_get_cmd_index(txq, txq->write_ptr);
  1290. out_cmd = txq->entries[idx].cmd;
  1291. out_meta = &txq->entries[idx].meta;
  1292. memset(out_meta, 0, sizeof(*out_meta)); /* re-initialize to NULL */
  1293. if (cmd->flags & CMD_WANT_SKB)
  1294. out_meta->source = cmd;
  1295. /* set up the header */
  1296. if (group_id != 0) {
  1297. out_cmd->hdr_wide.cmd = iwl_cmd_opcode(cmd->id);
  1298. out_cmd->hdr_wide.group_id = group_id;
  1299. out_cmd->hdr_wide.version = iwl_cmd_version(cmd->id);
  1300. out_cmd->hdr_wide.length =
  1301. cpu_to_le16(cmd_size -
  1302. sizeof(struct iwl_cmd_header_wide));
  1303. out_cmd->hdr_wide.reserved = 0;
  1304. out_cmd->hdr_wide.sequence =
  1305. cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->cmd_queue) |
  1306. INDEX_TO_SEQ(txq->write_ptr));
  1307. cmd_pos = sizeof(struct iwl_cmd_header_wide);
  1308. copy_size = sizeof(struct iwl_cmd_header_wide);
  1309. } else {
  1310. out_cmd->hdr.cmd = iwl_cmd_opcode(cmd->id);
  1311. out_cmd->hdr.sequence =
  1312. cpu_to_le16(QUEUE_TO_SEQ(trans_pcie->cmd_queue) |
  1313. INDEX_TO_SEQ(txq->write_ptr));
  1314. out_cmd->hdr.group_id = 0;
  1315. cmd_pos = sizeof(struct iwl_cmd_header);
  1316. copy_size = sizeof(struct iwl_cmd_header);
  1317. }
  1318. /* and copy the data that needs to be copied */
  1319. for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
  1320. int copy;
  1321. if (!cmd->len[i])
  1322. continue;
  1323. /* copy everything if not nocopy/dup */
  1324. if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
  1325. IWL_HCMD_DFL_DUP))) {
  1326. copy = cmd->len[i];
  1327. memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
  1328. cmd_pos += copy;
  1329. copy_size += copy;
  1330. continue;
  1331. }
  1332. /*
  1333. * Otherwise we need at least IWL_FIRST_TB_SIZE copied
  1334. * in total (for bi-directional DMA), but copy up to what
  1335. * we can fit into the payload for debug dump purposes.
  1336. */
  1337. copy = min_t(int, TFD_MAX_PAYLOAD_SIZE - cmd_pos, cmd->len[i]);
  1338. memcpy((u8 *)out_cmd + cmd_pos, cmd->data[i], copy);
  1339. cmd_pos += copy;
  1340. /* However, treat copy_size the proper way, we need it below */
  1341. if (copy_size < IWL_FIRST_TB_SIZE) {
  1342. copy = IWL_FIRST_TB_SIZE - copy_size;
  1343. if (copy > cmd->len[i])
  1344. copy = cmd->len[i];
  1345. copy_size += copy;
  1346. }
  1347. }
  1348. IWL_DEBUG_HC(trans,
  1349. "Sending command %s (%.2x.%.2x), seq: 0x%04X, %d bytes at %d[%d]:%d\n",
  1350. iwl_get_cmd_string(trans, cmd->id),
  1351. group_id, out_cmd->hdr.cmd,
  1352. le16_to_cpu(out_cmd->hdr.sequence),
  1353. cmd_size, txq->write_ptr, idx, trans_pcie->cmd_queue);
  1354. /* start the TFD with the minimum copy bytes */
  1355. tb0_size = min_t(int, copy_size, IWL_FIRST_TB_SIZE);
  1356. memcpy(&txq->first_tb_bufs[idx], &out_cmd->hdr, tb0_size);
  1357. iwl_pcie_txq_build_tfd(trans, txq,
  1358. iwl_pcie_get_first_tb_dma(txq, idx),
  1359. tb0_size, true);
  1360. /* map first command fragment, if any remains */
  1361. if (copy_size > tb0_size) {
  1362. phys_addr = dma_map_single(trans->dev,
  1363. ((u8 *)&out_cmd->hdr) + tb0_size,
  1364. copy_size - tb0_size,
  1365. DMA_TO_DEVICE);
  1366. if (dma_mapping_error(trans->dev, phys_addr)) {
  1367. iwl_pcie_tfd_unmap(trans, out_meta, txq,
  1368. txq->write_ptr);
  1369. idx = -ENOMEM;
  1370. goto out;
  1371. }
  1372. iwl_pcie_txq_build_tfd(trans, txq, phys_addr,
  1373. copy_size - tb0_size, false);
  1374. }
  1375. /* map the remaining (adjusted) nocopy/dup fragments */
  1376. for (i = 0; i < IWL_MAX_CMD_TBS_PER_TFD; i++) {
  1377. const void *data = cmddata[i];
  1378. if (!cmdlen[i])
  1379. continue;
  1380. if (!(cmd->dataflags[i] & (IWL_HCMD_DFL_NOCOPY |
  1381. IWL_HCMD_DFL_DUP)))
  1382. continue;
  1383. if (cmd->dataflags[i] & IWL_HCMD_DFL_DUP)
  1384. data = dup_buf;
  1385. phys_addr = dma_map_single(trans->dev, (void *)data,
  1386. cmdlen[i], DMA_TO_DEVICE);
  1387. if (dma_mapping_error(trans->dev, phys_addr)) {
  1388. iwl_pcie_tfd_unmap(trans, out_meta, txq,
  1389. txq->write_ptr);
  1390. idx = -ENOMEM;
  1391. goto out;
  1392. }
  1393. iwl_pcie_txq_build_tfd(trans, txq, phys_addr, cmdlen[i], false);
  1394. }
  1395. BUILD_BUG_ON(IWL_TFH_NUM_TBS > sizeof(out_meta->tbs) * BITS_PER_BYTE);
  1396. out_meta->flags = cmd->flags;
  1397. if (WARN_ON_ONCE(txq->entries[idx].free_buf))
  1398. kzfree(txq->entries[idx].free_buf);
  1399. txq->entries[idx].free_buf = dup_buf;
  1400. trace_iwlwifi_dev_hcmd(trans->dev, cmd, cmd_size, &out_cmd->hdr_wide);
  1401. /* start timer if queue currently empty */
  1402. if (txq->read_ptr == txq->write_ptr && txq->wd_timeout)
  1403. mod_timer(&txq->stuck_timer, jiffies + txq->wd_timeout);
  1404. spin_lock_irqsave(&trans_pcie->reg_lock, flags);
  1405. ret = iwl_pcie_set_cmd_in_flight(trans, cmd);
  1406. if (ret < 0) {
  1407. idx = ret;
  1408. spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
  1409. goto out;
  1410. }
  1411. /* Increment and update queue's write index */
  1412. txq->write_ptr = iwl_queue_inc_wrap(txq->write_ptr);
  1413. iwl_pcie_txq_inc_wr_ptr(trans, txq);
  1414. spin_unlock_irqrestore(&trans_pcie->reg_lock, flags);
  1415. out:
  1416. spin_unlock_bh(&txq->lock);
  1417. free_dup_buf:
  1418. if (idx < 0)
  1419. kfree(dup_buf);
  1420. return idx;
  1421. }
  1422. /*
  1423. * iwl_pcie_hcmd_complete - Pull unused buffers off the queue and reclaim them
  1424. * @rxb: Rx buffer to reclaim
  1425. */
  1426. void iwl_pcie_hcmd_complete(struct iwl_trans *trans,
  1427. struct iwl_rx_cmd_buffer *rxb)
  1428. {
  1429. struct iwl_rx_packet *pkt = rxb_addr(rxb);
  1430. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  1431. u8 group_id;
  1432. u32 cmd_id;
  1433. int txq_id = SEQ_TO_QUEUE(sequence);
  1434. int index = SEQ_TO_INDEX(sequence);
  1435. int cmd_index;
  1436. struct iwl_device_cmd *cmd;
  1437. struct iwl_cmd_meta *meta;
  1438. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1439. struct iwl_txq *txq = trans_pcie->txq[trans_pcie->cmd_queue];
  1440. /* If a Tx command is being handled and it isn't in the actual
  1441. * command queue then there a command routing bug has been introduced
  1442. * in the queue management code. */
  1443. if (WARN(txq_id != trans_pcie->cmd_queue,
  1444. "wrong command queue %d (should be %d), sequence 0x%X readp=%d writep=%d\n",
  1445. txq_id, trans_pcie->cmd_queue, sequence, txq->read_ptr,
  1446. txq->write_ptr)) {
  1447. iwl_print_hex_error(trans, pkt, 32);
  1448. return;
  1449. }
  1450. spin_lock_bh(&txq->lock);
  1451. cmd_index = iwl_pcie_get_cmd_index(txq, index);
  1452. cmd = txq->entries[cmd_index].cmd;
  1453. meta = &txq->entries[cmd_index].meta;
  1454. group_id = cmd->hdr.group_id;
  1455. cmd_id = iwl_cmd_id(cmd->hdr.cmd, group_id, 0);
  1456. iwl_pcie_tfd_unmap(trans, meta, txq, index);
  1457. /* Input error checking is done when commands are added to queue. */
  1458. if (meta->flags & CMD_WANT_SKB) {
  1459. struct page *p = rxb_steal_page(rxb);
  1460. meta->source->resp_pkt = pkt;
  1461. meta->source->_rx_page_addr = (unsigned long)page_address(p);
  1462. meta->source->_rx_page_order = trans_pcie->rx_page_order;
  1463. }
  1464. if (meta->flags & CMD_WANT_ASYNC_CALLBACK)
  1465. iwl_op_mode_async_cb(trans->op_mode, cmd);
  1466. iwl_pcie_cmdq_reclaim(trans, txq_id, index);
  1467. if (!(meta->flags & CMD_ASYNC)) {
  1468. if (!test_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status)) {
  1469. IWL_WARN(trans,
  1470. "HCMD_ACTIVE already clear for command %s\n",
  1471. iwl_get_cmd_string(trans, cmd_id));
  1472. }
  1473. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  1474. IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
  1475. iwl_get_cmd_string(trans, cmd_id));
  1476. wake_up(&trans_pcie->wait_command_queue);
  1477. }
  1478. if (meta->flags & CMD_MAKE_TRANS_IDLE) {
  1479. IWL_DEBUG_INFO(trans, "complete %s - mark trans as idle\n",
  1480. iwl_get_cmd_string(trans, cmd->hdr.cmd));
  1481. set_bit(STATUS_TRANS_IDLE, &trans->status);
  1482. wake_up(&trans_pcie->d0i3_waitq);
  1483. }
  1484. if (meta->flags & CMD_WAKE_UP_TRANS) {
  1485. IWL_DEBUG_INFO(trans, "complete %s - clear trans idle flag\n",
  1486. iwl_get_cmd_string(trans, cmd->hdr.cmd));
  1487. clear_bit(STATUS_TRANS_IDLE, &trans->status);
  1488. wake_up(&trans_pcie->d0i3_waitq);
  1489. }
  1490. meta->flags = 0;
  1491. spin_unlock_bh(&txq->lock);
  1492. }
  1493. #define HOST_COMPLETE_TIMEOUT (2 * HZ)
  1494. static int iwl_pcie_send_hcmd_async(struct iwl_trans *trans,
  1495. struct iwl_host_cmd *cmd)
  1496. {
  1497. int ret;
  1498. /* An asynchronous command can not expect an SKB to be set. */
  1499. if (WARN_ON(cmd->flags & CMD_WANT_SKB))
  1500. return -EINVAL;
  1501. ret = iwl_pcie_enqueue_hcmd(trans, cmd);
  1502. if (ret < 0) {
  1503. IWL_ERR(trans,
  1504. "Error sending %s: enqueue_hcmd failed: %d\n",
  1505. iwl_get_cmd_string(trans, cmd->id), ret);
  1506. return ret;
  1507. }
  1508. return 0;
  1509. }
  1510. static int iwl_pcie_send_hcmd_sync(struct iwl_trans *trans,
  1511. struct iwl_host_cmd *cmd)
  1512. {
  1513. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1514. struct iwl_txq *txq = trans_pcie->txq[trans_pcie->cmd_queue];
  1515. int cmd_idx;
  1516. int ret;
  1517. IWL_DEBUG_INFO(trans, "Attempting to send sync command %s\n",
  1518. iwl_get_cmd_string(trans, cmd->id));
  1519. if (WARN(test_and_set_bit(STATUS_SYNC_HCMD_ACTIVE,
  1520. &trans->status),
  1521. "Command %s: a command is already active!\n",
  1522. iwl_get_cmd_string(trans, cmd->id)))
  1523. return -EIO;
  1524. IWL_DEBUG_INFO(trans, "Setting HCMD_ACTIVE for command %s\n",
  1525. iwl_get_cmd_string(trans, cmd->id));
  1526. if (pm_runtime_suspended(&trans_pcie->pci_dev->dev)) {
  1527. ret = wait_event_timeout(trans_pcie->d0i3_waitq,
  1528. pm_runtime_active(&trans_pcie->pci_dev->dev),
  1529. msecs_to_jiffies(IWL_TRANS_IDLE_TIMEOUT));
  1530. if (!ret) {
  1531. IWL_ERR(trans, "Timeout exiting D0i3 before hcmd\n");
  1532. return -ETIMEDOUT;
  1533. }
  1534. }
  1535. cmd_idx = iwl_pcie_enqueue_hcmd(trans, cmd);
  1536. if (cmd_idx < 0) {
  1537. ret = cmd_idx;
  1538. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  1539. IWL_ERR(trans,
  1540. "Error sending %s: enqueue_hcmd failed: %d\n",
  1541. iwl_get_cmd_string(trans, cmd->id), ret);
  1542. return ret;
  1543. }
  1544. ret = wait_event_timeout(trans_pcie->wait_command_queue,
  1545. !test_bit(STATUS_SYNC_HCMD_ACTIVE,
  1546. &trans->status),
  1547. HOST_COMPLETE_TIMEOUT);
  1548. if (!ret) {
  1549. IWL_ERR(trans, "Error sending %s: time out after %dms.\n",
  1550. iwl_get_cmd_string(trans, cmd->id),
  1551. jiffies_to_msecs(HOST_COMPLETE_TIMEOUT));
  1552. IWL_ERR(trans, "Current CMD queue read_ptr %d write_ptr %d\n",
  1553. txq->read_ptr, txq->write_ptr);
  1554. clear_bit(STATUS_SYNC_HCMD_ACTIVE, &trans->status);
  1555. IWL_DEBUG_INFO(trans, "Clearing HCMD_ACTIVE for command %s\n",
  1556. iwl_get_cmd_string(trans, cmd->id));
  1557. ret = -ETIMEDOUT;
  1558. iwl_force_nmi(trans);
  1559. iwl_trans_fw_error(trans);
  1560. goto cancel;
  1561. }
  1562. if (test_bit(STATUS_FW_ERROR, &trans->status)) {
  1563. iwl_trans_dump_regs(trans);
  1564. IWL_ERR(trans, "FW error in SYNC CMD %s\n",
  1565. iwl_get_cmd_string(trans, cmd->id));
  1566. dump_stack();
  1567. ret = -EIO;
  1568. goto cancel;
  1569. }
  1570. if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
  1571. test_bit(STATUS_RFKILL_OPMODE, &trans->status)) {
  1572. IWL_DEBUG_RF_KILL(trans, "RFKILL in SYNC CMD... no rsp\n");
  1573. ret = -ERFKILL;
  1574. goto cancel;
  1575. }
  1576. if ((cmd->flags & CMD_WANT_SKB) && !cmd->resp_pkt) {
  1577. IWL_ERR(trans, "Error: Response NULL in '%s'\n",
  1578. iwl_get_cmd_string(trans, cmd->id));
  1579. ret = -EIO;
  1580. goto cancel;
  1581. }
  1582. return 0;
  1583. cancel:
  1584. if (cmd->flags & CMD_WANT_SKB) {
  1585. /*
  1586. * Cancel the CMD_WANT_SKB flag for the cmd in the
  1587. * TX cmd queue. Otherwise in case the cmd comes
  1588. * in later, it will possibly set an invalid
  1589. * address (cmd->meta.source).
  1590. */
  1591. txq->entries[cmd_idx].meta.flags &= ~CMD_WANT_SKB;
  1592. }
  1593. if (cmd->resp_pkt) {
  1594. iwl_free_resp(cmd);
  1595. cmd->resp_pkt = NULL;
  1596. }
  1597. return ret;
  1598. }
  1599. int iwl_trans_pcie_send_hcmd(struct iwl_trans *trans, struct iwl_host_cmd *cmd)
  1600. {
  1601. if (!(cmd->flags & CMD_SEND_IN_RFKILL) &&
  1602. test_bit(STATUS_RFKILL_OPMODE, &trans->status)) {
  1603. IWL_DEBUG_RF_KILL(trans, "Dropping CMD 0x%x: RF KILL\n",
  1604. cmd->id);
  1605. return -ERFKILL;
  1606. }
  1607. if (cmd->flags & CMD_ASYNC)
  1608. return iwl_pcie_send_hcmd_async(trans, cmd);
  1609. /* We still can fail on RFKILL that can be asserted while we wait */
  1610. return iwl_pcie_send_hcmd_sync(trans, cmd);
  1611. }
  1612. static int iwl_fill_data_tbs(struct iwl_trans *trans, struct sk_buff *skb,
  1613. struct iwl_txq *txq, u8 hdr_len,
  1614. struct iwl_cmd_meta *out_meta,
  1615. struct iwl_device_cmd *dev_cmd, u16 tb1_len)
  1616. {
  1617. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1618. u16 tb2_len;
  1619. int i;
  1620. /*
  1621. * Set up TFD's third entry to point directly to remainder
  1622. * of skb's head, if any
  1623. */
  1624. tb2_len = skb_headlen(skb) - hdr_len;
  1625. if (tb2_len > 0) {
  1626. dma_addr_t tb2_phys = dma_map_single(trans->dev,
  1627. skb->data + hdr_len,
  1628. tb2_len, DMA_TO_DEVICE);
  1629. if (unlikely(dma_mapping_error(trans->dev, tb2_phys))) {
  1630. iwl_pcie_tfd_unmap(trans, out_meta, txq,
  1631. txq->write_ptr);
  1632. return -EINVAL;
  1633. }
  1634. iwl_pcie_txq_build_tfd(trans, txq, tb2_phys, tb2_len, false);
  1635. }
  1636. /* set up the remaining entries to point to the data */
  1637. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  1638. const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
  1639. dma_addr_t tb_phys;
  1640. int tb_idx;
  1641. if (!skb_frag_size(frag))
  1642. continue;
  1643. tb_phys = skb_frag_dma_map(trans->dev, frag, 0,
  1644. skb_frag_size(frag), DMA_TO_DEVICE);
  1645. if (unlikely(dma_mapping_error(trans->dev, tb_phys))) {
  1646. iwl_pcie_tfd_unmap(trans, out_meta, txq,
  1647. txq->write_ptr);
  1648. return -EINVAL;
  1649. }
  1650. tb_idx = iwl_pcie_txq_build_tfd(trans, txq, tb_phys,
  1651. skb_frag_size(frag), false);
  1652. out_meta->tbs |= BIT(tb_idx);
  1653. }
  1654. trace_iwlwifi_dev_tx(trans->dev, skb,
  1655. iwl_pcie_get_tfd(trans, txq, txq->write_ptr),
  1656. trans_pcie->tfd_size,
  1657. &dev_cmd->hdr, IWL_FIRST_TB_SIZE + tb1_len,
  1658. hdr_len);
  1659. trace_iwlwifi_dev_tx_data(trans->dev, skb, hdr_len);
  1660. return 0;
  1661. }
  1662. #ifdef CONFIG_INET
  1663. struct iwl_tso_hdr_page *get_page_hdr(struct iwl_trans *trans, size_t len)
  1664. {
  1665. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1666. struct iwl_tso_hdr_page *p = this_cpu_ptr(trans_pcie->tso_hdr_page);
  1667. if (!p->page)
  1668. goto alloc;
  1669. /* enough room on this page */
  1670. if (p->pos + len < (u8 *)page_address(p->page) + PAGE_SIZE)
  1671. return p;
  1672. /* We don't have enough room on this page, get a new one. */
  1673. __free_page(p->page);
  1674. alloc:
  1675. p->page = alloc_page(GFP_ATOMIC);
  1676. if (!p->page)
  1677. return NULL;
  1678. p->pos = page_address(p->page);
  1679. return p;
  1680. }
  1681. static void iwl_compute_pseudo_hdr_csum(void *iph, struct tcphdr *tcph,
  1682. bool ipv6, unsigned int len)
  1683. {
  1684. if (ipv6) {
  1685. struct ipv6hdr *iphv6 = iph;
  1686. tcph->check = ~csum_ipv6_magic(&iphv6->saddr, &iphv6->daddr,
  1687. len + tcph->doff * 4,
  1688. IPPROTO_TCP, 0);
  1689. } else {
  1690. struct iphdr *iphv4 = iph;
  1691. ip_send_check(iphv4);
  1692. tcph->check = ~csum_tcpudp_magic(iphv4->saddr, iphv4->daddr,
  1693. len + tcph->doff * 4,
  1694. IPPROTO_TCP, 0);
  1695. }
  1696. }
  1697. static int iwl_fill_data_tbs_amsdu(struct iwl_trans *trans, struct sk_buff *skb,
  1698. struct iwl_txq *txq, u8 hdr_len,
  1699. struct iwl_cmd_meta *out_meta,
  1700. struct iwl_device_cmd *dev_cmd, u16 tb1_len)
  1701. {
  1702. struct iwl_tx_cmd *tx_cmd = (void *)dev_cmd->payload;
  1703. struct iwl_trans_pcie *trans_pcie = txq->trans_pcie;
  1704. struct ieee80211_hdr *hdr = (void *)skb->data;
  1705. unsigned int snap_ip_tcp_hdrlen, ip_hdrlen, total_len, hdr_room;
  1706. unsigned int mss = skb_shinfo(skb)->gso_size;
  1707. u16 length, iv_len, amsdu_pad;
  1708. u8 *start_hdr;
  1709. struct iwl_tso_hdr_page *hdr_page;
  1710. struct page **page_ptr;
  1711. int ret;
  1712. struct tso_t tso;
  1713. /* if the packet is protected, then it must be CCMP or GCMP */
  1714. BUILD_BUG_ON(IEEE80211_CCMP_HDR_LEN != IEEE80211_GCMP_HDR_LEN);
  1715. iv_len = ieee80211_has_protected(hdr->frame_control) ?
  1716. IEEE80211_CCMP_HDR_LEN : 0;
  1717. trace_iwlwifi_dev_tx(trans->dev, skb,
  1718. iwl_pcie_get_tfd(trans, txq, txq->write_ptr),
  1719. trans_pcie->tfd_size,
  1720. &dev_cmd->hdr, IWL_FIRST_TB_SIZE + tb1_len, 0);
  1721. ip_hdrlen = skb_transport_header(skb) - skb_network_header(skb);
  1722. snap_ip_tcp_hdrlen = 8 + ip_hdrlen + tcp_hdrlen(skb);
  1723. total_len = skb->len - snap_ip_tcp_hdrlen - hdr_len - iv_len;
  1724. amsdu_pad = 0;
  1725. /* total amount of header we may need for this A-MSDU */
  1726. hdr_room = DIV_ROUND_UP(total_len, mss) *
  1727. (3 + snap_ip_tcp_hdrlen + sizeof(struct ethhdr)) + iv_len;
  1728. /* Our device supports 9 segments at most, it will fit in 1 page */
  1729. hdr_page = get_page_hdr(trans, hdr_room);
  1730. if (!hdr_page)
  1731. return -ENOMEM;
  1732. get_page(hdr_page->page);
  1733. start_hdr = hdr_page->pos;
  1734. page_ptr = (void *)((u8 *)skb->cb + trans_pcie->page_offs);
  1735. *page_ptr = hdr_page->page;
  1736. memcpy(hdr_page->pos, skb->data + hdr_len, iv_len);
  1737. hdr_page->pos += iv_len;
  1738. /*
  1739. * Pull the ieee80211 header + IV to be able to use TSO core,
  1740. * we will restore it for the tx_status flow.
  1741. */
  1742. skb_pull(skb, hdr_len + iv_len);
  1743. /*
  1744. * Remove the length of all the headers that we don't actually
  1745. * have in the MPDU by themselves, but that we duplicate into
  1746. * all the different MSDUs inside the A-MSDU.
  1747. */
  1748. le16_add_cpu(&tx_cmd->len, -snap_ip_tcp_hdrlen);
  1749. tso_start(skb, &tso);
  1750. while (total_len) {
  1751. /* this is the data left for this subframe */
  1752. unsigned int data_left =
  1753. min_t(unsigned int, mss, total_len);
  1754. struct sk_buff *csum_skb = NULL;
  1755. unsigned int hdr_tb_len;
  1756. dma_addr_t hdr_tb_phys;
  1757. struct tcphdr *tcph;
  1758. u8 *iph, *subf_hdrs_start = hdr_page->pos;
  1759. total_len -= data_left;
  1760. memset(hdr_page->pos, 0, amsdu_pad);
  1761. hdr_page->pos += amsdu_pad;
  1762. amsdu_pad = (4 - (sizeof(struct ethhdr) + snap_ip_tcp_hdrlen +
  1763. data_left)) & 0x3;
  1764. ether_addr_copy(hdr_page->pos, ieee80211_get_DA(hdr));
  1765. hdr_page->pos += ETH_ALEN;
  1766. ether_addr_copy(hdr_page->pos, ieee80211_get_SA(hdr));
  1767. hdr_page->pos += ETH_ALEN;
  1768. length = snap_ip_tcp_hdrlen + data_left;
  1769. *((__be16 *)hdr_page->pos) = cpu_to_be16(length);
  1770. hdr_page->pos += sizeof(length);
  1771. /*
  1772. * This will copy the SNAP as well which will be considered
  1773. * as MAC header.
  1774. */
  1775. tso_build_hdr(skb, hdr_page->pos, &tso, data_left, !total_len);
  1776. iph = hdr_page->pos + 8;
  1777. tcph = (void *)(iph + ip_hdrlen);
  1778. /* For testing on current hardware only */
  1779. if (trans_pcie->sw_csum_tx) {
  1780. csum_skb = alloc_skb(data_left + tcp_hdrlen(skb),
  1781. GFP_ATOMIC);
  1782. if (!csum_skb) {
  1783. ret = -ENOMEM;
  1784. goto out_unmap;
  1785. }
  1786. iwl_compute_pseudo_hdr_csum(iph, tcph,
  1787. skb->protocol ==
  1788. htons(ETH_P_IPV6),
  1789. data_left);
  1790. skb_put_data(csum_skb, tcph, tcp_hdrlen(skb));
  1791. skb_reset_transport_header(csum_skb);
  1792. csum_skb->csum_start =
  1793. (unsigned char *)tcp_hdr(csum_skb) -
  1794. csum_skb->head;
  1795. }
  1796. hdr_page->pos += snap_ip_tcp_hdrlen;
  1797. hdr_tb_len = hdr_page->pos - start_hdr;
  1798. hdr_tb_phys = dma_map_single(trans->dev, start_hdr,
  1799. hdr_tb_len, DMA_TO_DEVICE);
  1800. if (unlikely(dma_mapping_error(trans->dev, hdr_tb_phys))) {
  1801. dev_kfree_skb(csum_skb);
  1802. ret = -EINVAL;
  1803. goto out_unmap;
  1804. }
  1805. iwl_pcie_txq_build_tfd(trans, txq, hdr_tb_phys,
  1806. hdr_tb_len, false);
  1807. trace_iwlwifi_dev_tx_tso_chunk(trans->dev, start_hdr,
  1808. hdr_tb_len);
  1809. /* add this subframe's headers' length to the tx_cmd */
  1810. le16_add_cpu(&tx_cmd->len, hdr_page->pos - subf_hdrs_start);
  1811. /* prepare the start_hdr for the next subframe */
  1812. start_hdr = hdr_page->pos;
  1813. /* put the payload */
  1814. while (data_left) {
  1815. unsigned int size = min_t(unsigned int, tso.size,
  1816. data_left);
  1817. dma_addr_t tb_phys;
  1818. if (trans_pcie->sw_csum_tx)
  1819. skb_put_data(csum_skb, tso.data, size);
  1820. tb_phys = dma_map_single(trans->dev, tso.data,
  1821. size, DMA_TO_DEVICE);
  1822. if (unlikely(dma_mapping_error(trans->dev, tb_phys))) {
  1823. dev_kfree_skb(csum_skb);
  1824. ret = -EINVAL;
  1825. goto out_unmap;
  1826. }
  1827. iwl_pcie_txq_build_tfd(trans, txq, tb_phys,
  1828. size, false);
  1829. trace_iwlwifi_dev_tx_tso_chunk(trans->dev, tso.data,
  1830. size);
  1831. data_left -= size;
  1832. tso_build_data(skb, &tso, size);
  1833. }
  1834. /* For testing on early hardware only */
  1835. if (trans_pcie->sw_csum_tx) {
  1836. __wsum csum;
  1837. csum = skb_checksum(csum_skb,
  1838. skb_checksum_start_offset(csum_skb),
  1839. csum_skb->len -
  1840. skb_checksum_start_offset(csum_skb),
  1841. 0);
  1842. dev_kfree_skb(csum_skb);
  1843. dma_sync_single_for_cpu(trans->dev, hdr_tb_phys,
  1844. hdr_tb_len, DMA_TO_DEVICE);
  1845. tcph->check = csum_fold(csum);
  1846. dma_sync_single_for_device(trans->dev, hdr_tb_phys,
  1847. hdr_tb_len, DMA_TO_DEVICE);
  1848. }
  1849. }
  1850. /* re -add the WiFi header and IV */
  1851. skb_push(skb, hdr_len + iv_len);
  1852. return 0;
  1853. out_unmap:
  1854. iwl_pcie_tfd_unmap(trans, out_meta, txq, txq->write_ptr);
  1855. return ret;
  1856. }
  1857. #else /* CONFIG_INET */
  1858. static int iwl_fill_data_tbs_amsdu(struct iwl_trans *trans, struct sk_buff *skb,
  1859. struct iwl_txq *txq, u8 hdr_len,
  1860. struct iwl_cmd_meta *out_meta,
  1861. struct iwl_device_cmd *dev_cmd, u16 tb1_len)
  1862. {
  1863. /* No A-MSDU without CONFIG_INET */
  1864. WARN_ON(1);
  1865. return -1;
  1866. }
  1867. #endif /* CONFIG_INET */
  1868. int iwl_trans_pcie_tx(struct iwl_trans *trans, struct sk_buff *skb,
  1869. struct iwl_device_cmd *dev_cmd, int txq_id)
  1870. {
  1871. struct iwl_trans_pcie *trans_pcie = IWL_TRANS_GET_PCIE_TRANS(trans);
  1872. struct ieee80211_hdr *hdr;
  1873. struct iwl_tx_cmd *tx_cmd = (struct iwl_tx_cmd *)dev_cmd->payload;
  1874. struct iwl_cmd_meta *out_meta;
  1875. struct iwl_txq *txq;
  1876. dma_addr_t tb0_phys, tb1_phys, scratch_phys;
  1877. void *tb1_addr;
  1878. void *tfd;
  1879. u16 len, tb1_len;
  1880. bool wait_write_ptr;
  1881. __le16 fc;
  1882. u8 hdr_len;
  1883. u16 wifi_seq;
  1884. bool amsdu;
  1885. txq = trans_pcie->txq[txq_id];
  1886. if (WARN_ONCE(!test_bit(txq_id, trans_pcie->queue_used),
  1887. "TX on unused queue %d\n", txq_id))
  1888. return -EINVAL;
  1889. if (unlikely(trans_pcie->sw_csum_tx &&
  1890. skb->ip_summed == CHECKSUM_PARTIAL)) {
  1891. int offs = skb_checksum_start_offset(skb);
  1892. int csum_offs = offs + skb->csum_offset;
  1893. __wsum csum;
  1894. if (skb_ensure_writable(skb, csum_offs + sizeof(__sum16)))
  1895. return -1;
  1896. csum = skb_checksum(skb, offs, skb->len - offs, 0);
  1897. *(__sum16 *)(skb->data + csum_offs) = csum_fold(csum);
  1898. skb->ip_summed = CHECKSUM_UNNECESSARY;
  1899. }
  1900. if (skb_is_nonlinear(skb) &&
  1901. skb_shinfo(skb)->nr_frags > IWL_PCIE_MAX_FRAGS(trans_pcie) &&
  1902. __skb_linearize(skb))
  1903. return -ENOMEM;
  1904. /* mac80211 always puts the full header into the SKB's head,
  1905. * so there's no need to check if it's readable there
  1906. */
  1907. hdr = (struct ieee80211_hdr *)skb->data;
  1908. fc = hdr->frame_control;
  1909. hdr_len = ieee80211_hdrlen(fc);
  1910. spin_lock(&txq->lock);
  1911. if (iwl_queue_space(txq) < txq->high_mark) {
  1912. iwl_stop_queue(trans, txq);
  1913. /* don't put the packet on the ring, if there is no room */
  1914. if (unlikely(iwl_queue_space(txq) < 3)) {
  1915. struct iwl_device_cmd **dev_cmd_ptr;
  1916. dev_cmd_ptr = (void *)((u8 *)skb->cb +
  1917. trans_pcie->dev_cmd_offs);
  1918. *dev_cmd_ptr = dev_cmd;
  1919. __skb_queue_tail(&txq->overflow_q, skb);
  1920. spin_unlock(&txq->lock);
  1921. return 0;
  1922. }
  1923. }
  1924. /* In AGG mode, the index in the ring must correspond to the WiFi
  1925. * sequence number. This is a HW requirements to help the SCD to parse
  1926. * the BA.
  1927. * Check here that the packets are in the right place on the ring.
  1928. */
  1929. wifi_seq = IEEE80211_SEQ_TO_SN(le16_to_cpu(hdr->seq_ctrl));
  1930. WARN_ONCE(txq->ampdu &&
  1931. (wifi_seq & 0xff) != txq->write_ptr,
  1932. "Q: %d WiFi Seq %d tfdNum %d",
  1933. txq_id, wifi_seq, txq->write_ptr);
  1934. /* Set up driver data for this TFD */
  1935. txq->entries[txq->write_ptr].skb = skb;
  1936. txq->entries[txq->write_ptr].cmd = dev_cmd;
  1937. dev_cmd->hdr.sequence =
  1938. cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  1939. INDEX_TO_SEQ(txq->write_ptr)));
  1940. tb0_phys = iwl_pcie_get_first_tb_dma(txq, txq->write_ptr);
  1941. scratch_phys = tb0_phys + sizeof(struct iwl_cmd_header) +
  1942. offsetof(struct iwl_tx_cmd, scratch);
  1943. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  1944. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_addr(scratch_phys);
  1945. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  1946. out_meta = &txq->entries[txq->write_ptr].meta;
  1947. out_meta->flags = 0;
  1948. /*
  1949. * The second TB (tb1) points to the remainder of the TX command
  1950. * and the 802.11 header - dword aligned size
  1951. * (This calculation modifies the TX command, so do it before the
  1952. * setup of the first TB)
  1953. */
  1954. len = sizeof(struct iwl_tx_cmd) + sizeof(struct iwl_cmd_header) +
  1955. hdr_len - IWL_FIRST_TB_SIZE;
  1956. /* do not align A-MSDU to dword as the subframe header aligns it */
  1957. amsdu = ieee80211_is_data_qos(fc) &&
  1958. (*ieee80211_get_qos_ctl(hdr) &
  1959. IEEE80211_QOS_CTL_A_MSDU_PRESENT);
  1960. if (trans_pcie->sw_csum_tx || !amsdu) {
  1961. tb1_len = ALIGN(len, 4);
  1962. /* Tell NIC about any 2-byte padding after MAC header */
  1963. if (tb1_len != len)
  1964. tx_cmd->tx_flags |= cpu_to_le32(TX_CMD_FLG_MH_PAD);
  1965. } else {
  1966. tb1_len = len;
  1967. }
  1968. /*
  1969. * The first TB points to bi-directional DMA data, we'll
  1970. * memcpy the data into it later.
  1971. */
  1972. iwl_pcie_txq_build_tfd(trans, txq, tb0_phys,
  1973. IWL_FIRST_TB_SIZE, true);
  1974. /* there must be data left over for TB1 or this code must be changed */
  1975. BUILD_BUG_ON(sizeof(struct iwl_tx_cmd) < IWL_FIRST_TB_SIZE);
  1976. /* map the data for TB1 */
  1977. tb1_addr = ((u8 *)&dev_cmd->hdr) + IWL_FIRST_TB_SIZE;
  1978. tb1_phys = dma_map_single(trans->dev, tb1_addr, tb1_len, DMA_TO_DEVICE);
  1979. if (unlikely(dma_mapping_error(trans->dev, tb1_phys)))
  1980. goto out_err;
  1981. iwl_pcie_txq_build_tfd(trans, txq, tb1_phys, tb1_len, false);
  1982. if (amsdu) {
  1983. if (unlikely(iwl_fill_data_tbs_amsdu(trans, skb, txq, hdr_len,
  1984. out_meta, dev_cmd,
  1985. tb1_len)))
  1986. goto out_err;
  1987. } else if (unlikely(iwl_fill_data_tbs(trans, skb, txq, hdr_len,
  1988. out_meta, dev_cmd, tb1_len))) {
  1989. goto out_err;
  1990. }
  1991. /* building the A-MSDU might have changed this data, so memcpy it now */
  1992. memcpy(&txq->first_tb_bufs[txq->write_ptr], &dev_cmd->hdr,
  1993. IWL_FIRST_TB_SIZE);
  1994. tfd = iwl_pcie_get_tfd(trans, txq, txq->write_ptr);
  1995. /* Set up entry for this TFD in Tx byte-count array */
  1996. iwl_pcie_txq_update_byte_cnt_tbl(trans, txq, le16_to_cpu(tx_cmd->len),
  1997. iwl_pcie_tfd_get_num_tbs(trans, tfd));
  1998. wait_write_ptr = ieee80211_has_morefrags(fc);
  1999. /* start timer if queue currently empty */
  2000. if (txq->read_ptr == txq->write_ptr) {
  2001. if (txq->wd_timeout) {
  2002. /*
  2003. * If the TXQ is active, then set the timer, if not,
  2004. * set the timer in remainder so that the timer will
  2005. * be armed with the right value when the station will
  2006. * wake up.
  2007. */
  2008. if (!txq->frozen)
  2009. mod_timer(&txq->stuck_timer,
  2010. jiffies + txq->wd_timeout);
  2011. else
  2012. txq->frozen_expiry_remainder = txq->wd_timeout;
  2013. }
  2014. IWL_DEBUG_RPM(trans, "Q: %d first tx - take ref\n", txq->id);
  2015. iwl_trans_ref(trans);
  2016. }
  2017. /* Tell device the write index *just past* this latest filled TFD */
  2018. txq->write_ptr = iwl_queue_inc_wrap(txq->write_ptr);
  2019. if (!wait_write_ptr)
  2020. iwl_pcie_txq_inc_wr_ptr(trans, txq);
  2021. /*
  2022. * At this point the frame is "transmitted" successfully
  2023. * and we will get a TX status notification eventually.
  2024. */
  2025. spin_unlock(&txq->lock);
  2026. return 0;
  2027. out_err:
  2028. spin_unlock(&txq->lock);
  2029. return -1;
  2030. }