tx.c 68 KB

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