tx.c 69 KB

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