iwl-tx.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2003 - 2008 Intel Corporation. All rights reserved.
  4. *
  5. * Portions of this file are derived from the ipw3945 project, as well
  6. * as portions of the ieee80211 subsystem header files.
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of version 2 of the GNU General Public License as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
  20. *
  21. * The full GNU General Public License is included in this distribution in the
  22. * file called LICENSE.
  23. *
  24. * Contact Information:
  25. * James P. Ketrenos <ipw2100-admin@linux.intel.com>
  26. * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  27. *
  28. *****************************************************************************/
  29. #include <linux/etherdevice.h>
  30. #include <net/mac80211.h>
  31. #include "iwl-eeprom.h"
  32. #include "iwl-dev.h"
  33. #include "iwl-core.h"
  34. #include "iwl-sta.h"
  35. #include "iwl-io.h"
  36. #include "iwl-helpers.h"
  37. static const u16 default_tid_to_tx_fifo[] = {
  38. IWL_TX_FIFO_AC1,
  39. IWL_TX_FIFO_AC0,
  40. IWL_TX_FIFO_AC0,
  41. IWL_TX_FIFO_AC1,
  42. IWL_TX_FIFO_AC2,
  43. IWL_TX_FIFO_AC2,
  44. IWL_TX_FIFO_AC3,
  45. IWL_TX_FIFO_AC3,
  46. IWL_TX_FIFO_NONE,
  47. IWL_TX_FIFO_NONE,
  48. IWL_TX_FIFO_NONE,
  49. IWL_TX_FIFO_NONE,
  50. IWL_TX_FIFO_NONE,
  51. IWL_TX_FIFO_NONE,
  52. IWL_TX_FIFO_NONE,
  53. IWL_TX_FIFO_NONE,
  54. IWL_TX_FIFO_AC3
  55. };
  56. /**
  57. * iwl_hw_txq_free_tfd - Free all chunks referenced by TFD [txq->q.read_ptr]
  58. *
  59. * Does NOT advance any TFD circular buffer read/write indexes
  60. * Does NOT free the TFD itself (which is within circular buffer)
  61. */
  62. int iwl_hw_txq_free_tfd(struct iwl_priv *priv, struct iwl_tx_queue *txq)
  63. {
  64. struct iwl_tfd_frame *bd_tmp = (struct iwl_tfd_frame *)&txq->bd[0];
  65. struct iwl_tfd_frame *bd = &bd_tmp[txq->q.read_ptr];
  66. struct pci_dev *dev = priv->pci_dev;
  67. int i;
  68. int counter = 0;
  69. int index, is_odd;
  70. /* Host command buffers stay mapped in memory, nothing to clean */
  71. if (txq->q.id == IWL_CMD_QUEUE_NUM)
  72. return 0;
  73. /* Sanity check on number of chunks */
  74. counter = IWL_GET_BITS(*bd, num_tbs);
  75. if (counter > MAX_NUM_OF_TBS) {
  76. IWL_ERROR("Too many chunks: %i\n", counter);
  77. /* @todo issue fatal error, it is quite serious situation */
  78. return 0;
  79. }
  80. /* Unmap chunks, if any.
  81. * TFD info for odd chunks is different format than for even chunks. */
  82. for (i = 0; i < counter; i++) {
  83. index = i / 2;
  84. is_odd = i & 0x1;
  85. if (is_odd)
  86. pci_unmap_single(
  87. dev,
  88. IWL_GET_BITS(bd->pa[index], tb2_addr_lo16) |
  89. (IWL_GET_BITS(bd->pa[index],
  90. tb2_addr_hi20) << 16),
  91. IWL_GET_BITS(bd->pa[index], tb2_len),
  92. PCI_DMA_TODEVICE);
  93. else if (i > 0)
  94. pci_unmap_single(dev,
  95. le32_to_cpu(bd->pa[index].tb1_addr),
  96. IWL_GET_BITS(bd->pa[index], tb1_len),
  97. PCI_DMA_TODEVICE);
  98. /* Free SKB, if any, for this chunk */
  99. if (txq->txb[txq->q.read_ptr].skb[i]) {
  100. struct sk_buff *skb = txq->txb[txq->q.read_ptr].skb[i];
  101. dev_kfree_skb(skb);
  102. txq->txb[txq->q.read_ptr].skb[i] = NULL;
  103. }
  104. }
  105. return 0;
  106. }
  107. EXPORT_SYMBOL(iwl_hw_txq_free_tfd);
  108. int iwl_hw_txq_attach_buf_to_tfd(struct iwl_priv *priv, void *ptr,
  109. dma_addr_t addr, u16 len)
  110. {
  111. int index, is_odd;
  112. struct iwl_tfd_frame *tfd = ptr;
  113. u32 num_tbs = IWL_GET_BITS(*tfd, num_tbs);
  114. /* Each TFD can point to a maximum 20 Tx buffers */
  115. if ((num_tbs >= MAX_NUM_OF_TBS) || (num_tbs < 0)) {
  116. IWL_ERROR("Error can not send more than %d chunks\n",
  117. MAX_NUM_OF_TBS);
  118. return -EINVAL;
  119. }
  120. index = num_tbs / 2;
  121. is_odd = num_tbs & 0x1;
  122. if (!is_odd) {
  123. tfd->pa[index].tb1_addr = cpu_to_le32(addr);
  124. IWL_SET_BITS(tfd->pa[index], tb1_addr_hi,
  125. iwl_get_dma_hi_address(addr));
  126. IWL_SET_BITS(tfd->pa[index], tb1_len, len);
  127. } else {
  128. IWL_SET_BITS(tfd->pa[index], tb2_addr_lo16,
  129. (u32) (addr & 0xffff));
  130. IWL_SET_BITS(tfd->pa[index], tb2_addr_hi20, addr >> 16);
  131. IWL_SET_BITS(tfd->pa[index], tb2_len, len);
  132. }
  133. IWL_SET_BITS(*tfd, num_tbs, num_tbs + 1);
  134. return 0;
  135. }
  136. EXPORT_SYMBOL(iwl_hw_txq_attach_buf_to_tfd);
  137. /**
  138. * iwl_txq_update_write_ptr - Send new write index to hardware
  139. */
  140. int iwl_txq_update_write_ptr(struct iwl_priv *priv, struct iwl_tx_queue *txq)
  141. {
  142. u32 reg = 0;
  143. int ret = 0;
  144. int txq_id = txq->q.id;
  145. if (txq->need_update == 0)
  146. return ret;
  147. /* if we're trying to save power */
  148. if (test_bit(STATUS_POWER_PMI, &priv->status)) {
  149. /* wake up nic if it's powered down ...
  150. * uCode will wake up, and interrupt us again, so next
  151. * time we'll skip this part. */
  152. reg = iwl_read32(priv, CSR_UCODE_DRV_GP1);
  153. if (reg & CSR_UCODE_DRV_GP1_BIT_MAC_SLEEP) {
  154. IWL_DEBUG_INFO("Requesting wakeup, GP1 = 0x%x\n", reg);
  155. iwl_set_bit(priv, CSR_GP_CNTRL,
  156. CSR_GP_CNTRL_REG_FLAG_MAC_ACCESS_REQ);
  157. return ret;
  158. }
  159. /* restore this queue's parameters in nic hardware. */
  160. ret = iwl_grab_nic_access(priv);
  161. if (ret)
  162. return ret;
  163. iwl_write_direct32(priv, HBUS_TARG_WRPTR,
  164. txq->q.write_ptr | (txq_id << 8));
  165. iwl_release_nic_access(priv);
  166. /* else not in power-save mode, uCode will never sleep when we're
  167. * trying to tx (during RFKILL, we're not trying to tx). */
  168. } else
  169. iwl_write32(priv, HBUS_TARG_WRPTR,
  170. txq->q.write_ptr | (txq_id << 8));
  171. txq->need_update = 0;
  172. return ret;
  173. }
  174. EXPORT_SYMBOL(iwl_txq_update_write_ptr);
  175. /**
  176. * iwl_tx_queue_free - Deallocate DMA queue.
  177. * @txq: Transmit queue to deallocate.
  178. *
  179. * Empty queue by removing and destroying all BD's.
  180. * Free all buffers.
  181. * 0-fill, but do not free "txq" descriptor structure.
  182. */
  183. static void iwl_tx_queue_free(struct iwl_priv *priv, int txq_id)
  184. {
  185. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  186. struct iwl_queue *q = &txq->q;
  187. struct pci_dev *dev = priv->pci_dev;
  188. int i, slots_num, len;
  189. if (q->n_bd == 0)
  190. return;
  191. /* first, empty all BD's */
  192. for (; q->write_ptr != q->read_ptr;
  193. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd))
  194. iwl_hw_txq_free_tfd(priv, txq);
  195. len = sizeof(struct iwl_cmd) * q->n_window;
  196. if (q->id == IWL_CMD_QUEUE_NUM)
  197. len += IWL_MAX_SCAN_SIZE;
  198. /* De-alloc array of command/tx buffers */
  199. slots_num = (txq_id == IWL_CMD_QUEUE_NUM) ?
  200. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  201. for (i = 0; i < slots_num; i++)
  202. kfree(txq->cmd[i]);
  203. if (txq_id == IWL_CMD_QUEUE_NUM)
  204. kfree(txq->cmd[slots_num]);
  205. /* De-alloc circular buffer of TFDs */
  206. if (txq->q.n_bd)
  207. pci_free_consistent(dev, sizeof(struct iwl_tfd_frame) *
  208. txq->q.n_bd, txq->bd, txq->q.dma_addr);
  209. /* De-alloc array of per-TFD driver data */
  210. kfree(txq->txb);
  211. txq->txb = NULL;
  212. /* 0-fill queue descriptor structure */
  213. memset(txq, 0, sizeof(*txq));
  214. }
  215. /*************** DMA-QUEUE-GENERAL-FUNCTIONS *****
  216. * DMA services
  217. *
  218. * Theory of operation
  219. *
  220. * A Tx or Rx queue resides in host DRAM, and is comprised of a circular buffer
  221. * of buffer descriptors, each of which points to one or more data buffers for
  222. * the device to read from or fill. Driver and device exchange status of each
  223. * queue via "read" and "write" pointers. Driver keeps minimum of 2 empty
  224. * entries in each circular buffer, to protect against confusing empty and full
  225. * queue states.
  226. *
  227. * The device reads or writes the data in the queues via the device's several
  228. * DMA/FIFO channels. Each queue is mapped to a single DMA channel.
  229. *
  230. * For Tx queue, there are low mark and high mark limits. If, after queuing
  231. * the packet for Tx, free space become < low mark, Tx queue stopped. When
  232. * reclaiming packets (on 'tx done IRQ), if free space become > high mark,
  233. * Tx queue resumed.
  234. *
  235. * See more detailed info in iwl-4965-hw.h.
  236. ***************************************************/
  237. int iwl_queue_space(const struct iwl_queue *q)
  238. {
  239. int s = q->read_ptr - q->write_ptr;
  240. if (q->read_ptr > q->write_ptr)
  241. s -= q->n_bd;
  242. if (s <= 0)
  243. s += q->n_window;
  244. /* keep some reserve to not confuse empty and full situations */
  245. s -= 2;
  246. if (s < 0)
  247. s = 0;
  248. return s;
  249. }
  250. EXPORT_SYMBOL(iwl_queue_space);
  251. /**
  252. * iwl_queue_init - Initialize queue's high/low-water and read/write indexes
  253. */
  254. static int iwl_queue_init(struct iwl_priv *priv, struct iwl_queue *q,
  255. int count, int slots_num, u32 id)
  256. {
  257. q->n_bd = count;
  258. q->n_window = slots_num;
  259. q->id = id;
  260. /* count must be power-of-two size, otherwise iwl_queue_inc_wrap
  261. * and iwl_queue_dec_wrap are broken. */
  262. BUG_ON(!is_power_of_2(count));
  263. /* slots_num must be power-of-two size, otherwise
  264. * get_cmd_index is broken. */
  265. BUG_ON(!is_power_of_2(slots_num));
  266. q->low_mark = q->n_window / 4;
  267. if (q->low_mark < 4)
  268. q->low_mark = 4;
  269. q->high_mark = q->n_window / 8;
  270. if (q->high_mark < 2)
  271. q->high_mark = 2;
  272. q->write_ptr = q->read_ptr = 0;
  273. return 0;
  274. }
  275. /**
  276. * iwl_tx_queue_alloc - Alloc driver data and TFD CB for one Tx/cmd queue
  277. */
  278. static int iwl_tx_queue_alloc(struct iwl_priv *priv,
  279. struct iwl_tx_queue *txq, u32 id)
  280. {
  281. struct pci_dev *dev = priv->pci_dev;
  282. /* Driver private data, only for Tx (not command) queues,
  283. * not shared with device. */
  284. if (id != IWL_CMD_QUEUE_NUM) {
  285. txq->txb = kmalloc(sizeof(txq->txb[0]) *
  286. TFD_QUEUE_SIZE_MAX, GFP_KERNEL);
  287. if (!txq->txb) {
  288. IWL_ERROR("kmalloc for auxiliary BD "
  289. "structures failed\n");
  290. goto error;
  291. }
  292. } else
  293. txq->txb = NULL;
  294. /* Circular buffer of transmit frame descriptors (TFDs),
  295. * shared with device */
  296. txq->bd = pci_alloc_consistent(dev,
  297. sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX,
  298. &txq->q.dma_addr);
  299. if (!txq->bd) {
  300. IWL_ERROR("pci_alloc_consistent(%zd) failed\n",
  301. sizeof(txq->bd[0]) * TFD_QUEUE_SIZE_MAX);
  302. goto error;
  303. }
  304. txq->q.id = id;
  305. return 0;
  306. error:
  307. kfree(txq->txb);
  308. txq->txb = NULL;
  309. return -ENOMEM;
  310. }
  311. /*
  312. * Tell nic where to find circular buffer of Tx Frame Descriptors for
  313. * given Tx queue, and enable the DMA channel used for that queue.
  314. *
  315. * 4965 supports up to 16 Tx queues in DRAM, mapped to up to 8 Tx DMA
  316. * channels supported in hardware.
  317. */
  318. static int iwl_hw_tx_queue_init(struct iwl_priv *priv,
  319. struct iwl_tx_queue *txq)
  320. {
  321. int rc;
  322. unsigned long flags;
  323. int txq_id = txq->q.id;
  324. spin_lock_irqsave(&priv->lock, flags);
  325. rc = iwl_grab_nic_access(priv);
  326. if (rc) {
  327. spin_unlock_irqrestore(&priv->lock, flags);
  328. return rc;
  329. }
  330. /* Circular buffer (TFD queue in DRAM) physical base address */
  331. iwl_write_direct32(priv, FH_MEM_CBBC_QUEUE(txq_id),
  332. txq->q.dma_addr >> 8);
  333. /* Enable DMA channel, using same id as for TFD queue */
  334. iwl_write_direct32(
  335. priv, FH_TCSR_CHNL_TX_CONFIG_REG(txq_id),
  336. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CHNL_ENABLE |
  337. FH_TCSR_TX_CONFIG_REG_VAL_DMA_CREDIT_ENABLE_VAL);
  338. iwl_release_nic_access(priv);
  339. spin_unlock_irqrestore(&priv->lock, flags);
  340. return 0;
  341. }
  342. /**
  343. * iwl_tx_queue_init - Allocate and initialize one tx/cmd queue
  344. */
  345. static int iwl_tx_queue_init(struct iwl_priv *priv,
  346. struct iwl_tx_queue *txq,
  347. int slots_num, u32 txq_id)
  348. {
  349. int i, len;
  350. int rc = 0;
  351. /*
  352. * Alloc buffer array for commands (Tx or other types of commands).
  353. * For the command queue (#4), allocate command space + one big
  354. * command for scan, since scan command is very huge; the system will
  355. * not have two scans at the same time, so only one is needed.
  356. * For normal Tx queues (all other queues), no super-size command
  357. * space is needed.
  358. */
  359. len = sizeof(struct iwl_cmd);
  360. for (i = 0; i <= slots_num; i++) {
  361. if (i == slots_num) {
  362. if (txq_id == IWL_CMD_QUEUE_NUM)
  363. len += IWL_MAX_SCAN_SIZE;
  364. else
  365. continue;
  366. }
  367. txq->cmd[i] = kmalloc(len, GFP_KERNEL | GFP_DMA);
  368. if (!txq->cmd[i])
  369. return -ENOMEM;
  370. }
  371. /* Alloc driver data array and TFD circular buffer */
  372. rc = iwl_tx_queue_alloc(priv, txq, txq_id);
  373. if (rc) {
  374. for (i = 0; i < slots_num; i++)
  375. kfree(txq->cmd[i]);
  376. return -ENOMEM;
  377. }
  378. txq->need_update = 0;
  379. /* TFD_QUEUE_SIZE_MAX must be power-of-two size, otherwise
  380. * iwl_queue_inc_wrap and iwl_queue_dec_wrap are broken. */
  381. BUILD_BUG_ON(TFD_QUEUE_SIZE_MAX & (TFD_QUEUE_SIZE_MAX - 1));
  382. /* Initialize queue's high/low-water marks, and head/tail indexes */
  383. iwl_queue_init(priv, &txq->q, TFD_QUEUE_SIZE_MAX, slots_num, txq_id);
  384. /* Tell device where to find queue */
  385. iwl_hw_tx_queue_init(priv, txq);
  386. return 0;
  387. }
  388. /**
  389. * iwl_hw_txq_ctx_free - Free TXQ Context
  390. *
  391. * Destroy all TX DMA queues and structures
  392. */
  393. void iwl_hw_txq_ctx_free(struct iwl_priv *priv)
  394. {
  395. int txq_id;
  396. /* Tx queues */
  397. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  398. iwl_tx_queue_free(priv, txq_id);
  399. /* Keep-warm buffer */
  400. iwl_kw_free(priv);
  401. }
  402. EXPORT_SYMBOL(iwl_hw_txq_ctx_free);
  403. /**
  404. * iwl_txq_ctx_reset - Reset TX queue context
  405. * Destroys all DMA structures and initialise them again
  406. *
  407. * @param priv
  408. * @return error code
  409. */
  410. int iwl_txq_ctx_reset(struct iwl_priv *priv)
  411. {
  412. int ret = 0;
  413. int txq_id, slots_num;
  414. unsigned long flags;
  415. iwl_kw_free(priv);
  416. /* Free all tx/cmd queues and keep-warm buffer */
  417. iwl_hw_txq_ctx_free(priv);
  418. /* Alloc keep-warm buffer */
  419. ret = iwl_kw_alloc(priv);
  420. if (ret) {
  421. IWL_ERROR("Keep Warm allocation failed");
  422. goto error_kw;
  423. }
  424. spin_lock_irqsave(&priv->lock, flags);
  425. ret = iwl_grab_nic_access(priv);
  426. if (unlikely(ret)) {
  427. spin_unlock_irqrestore(&priv->lock, flags);
  428. goto error_reset;
  429. }
  430. /* Turn off all Tx DMA fifos */
  431. priv->cfg->ops->lib->txq_set_sched(priv, 0);
  432. iwl_release_nic_access(priv);
  433. spin_unlock_irqrestore(&priv->lock, flags);
  434. /* Tell nic where to find the keep-warm buffer */
  435. ret = iwl_kw_init(priv);
  436. if (ret) {
  437. IWL_ERROR("kw_init failed\n");
  438. goto error_reset;
  439. }
  440. /* Alloc and init all Tx queues, including the command queue (#4) */
  441. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  442. slots_num = (txq_id == IWL_CMD_QUEUE_NUM) ?
  443. TFD_CMD_SLOTS : TFD_TX_CMD_SLOTS;
  444. ret = iwl_tx_queue_init(priv, &priv->txq[txq_id], slots_num,
  445. txq_id);
  446. if (ret) {
  447. IWL_ERROR("Tx %d queue init failed\n", txq_id);
  448. goto error;
  449. }
  450. }
  451. return ret;
  452. error:
  453. iwl_hw_txq_ctx_free(priv);
  454. error_reset:
  455. iwl_kw_free(priv);
  456. error_kw:
  457. return ret;
  458. }
  459. /**
  460. * iwl_txq_ctx_stop - Stop all Tx DMA channels, free Tx queue memory
  461. */
  462. void iwl_txq_ctx_stop(struct iwl_priv *priv)
  463. {
  464. int txq_id;
  465. unsigned long flags;
  466. /* Turn off all Tx DMA fifos */
  467. spin_lock_irqsave(&priv->lock, flags);
  468. if (iwl_grab_nic_access(priv)) {
  469. spin_unlock_irqrestore(&priv->lock, flags);
  470. return;
  471. }
  472. priv->cfg->ops->lib->txq_set_sched(priv, 0);
  473. /* Stop each Tx DMA channel, and wait for it to be idle */
  474. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++) {
  475. iwl_write_direct32(priv,
  476. FH_TCSR_CHNL_TX_CONFIG_REG(txq_id), 0x0);
  477. iwl_poll_direct_bit(priv, FH_TSSR_TX_STATUS_REG,
  478. FH_TSSR_TX_STATUS_REG_MSK_CHNL_IDLE
  479. (txq_id), 200);
  480. }
  481. iwl_release_nic_access(priv);
  482. spin_unlock_irqrestore(&priv->lock, flags);
  483. /* Deallocate memory for all Tx queues */
  484. iwl_hw_txq_ctx_free(priv);
  485. }
  486. EXPORT_SYMBOL(iwl_txq_ctx_stop);
  487. /*
  488. * handle build REPLY_TX command notification.
  489. */
  490. static void iwl_tx_cmd_build_basic(struct iwl_priv *priv,
  491. struct iwl_tx_cmd *tx_cmd,
  492. struct ieee80211_tx_info *info,
  493. struct ieee80211_hdr *hdr,
  494. int is_unicast, u8 std_id)
  495. {
  496. __le16 fc = hdr->frame_control;
  497. __le32 tx_flags = tx_cmd->tx_flags;
  498. tx_cmd->stop_time.life_time = TX_CMD_LIFE_TIME_INFINITE;
  499. if (!(info->flags & IEEE80211_TX_CTL_NO_ACK)) {
  500. tx_flags |= TX_CMD_FLG_ACK_MSK;
  501. if (ieee80211_is_mgmt(fc))
  502. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  503. if (ieee80211_is_probe_resp(fc) &&
  504. !(le16_to_cpu(hdr->seq_ctrl) & 0xf))
  505. tx_flags |= TX_CMD_FLG_TSF_MSK;
  506. } else {
  507. tx_flags &= (~TX_CMD_FLG_ACK_MSK);
  508. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  509. }
  510. if (ieee80211_is_back_req(fc))
  511. tx_flags |= TX_CMD_FLG_ACK_MSK | TX_CMD_FLG_IMM_BA_RSP_MASK;
  512. tx_cmd->sta_id = std_id;
  513. if (ieee80211_has_morefrags(fc))
  514. tx_flags |= TX_CMD_FLG_MORE_FRAG_MSK;
  515. if (ieee80211_is_data_qos(fc)) {
  516. u8 *qc = ieee80211_get_qos_ctl(hdr);
  517. tx_cmd->tid_tspec = qc[0] & 0xf;
  518. tx_flags &= ~TX_CMD_FLG_SEQ_CTL_MSK;
  519. } else {
  520. tx_flags |= TX_CMD_FLG_SEQ_CTL_MSK;
  521. }
  522. priv->cfg->ops->utils->rts_tx_cmd_flag(info, &tx_flags);
  523. if ((tx_flags & TX_CMD_FLG_RTS_MSK) || (tx_flags & TX_CMD_FLG_CTS_MSK))
  524. tx_flags |= TX_CMD_FLG_FULL_TXOP_PROT_MSK;
  525. tx_flags &= ~(TX_CMD_FLG_ANT_SEL_MSK);
  526. if (ieee80211_is_mgmt(fc)) {
  527. if (ieee80211_is_assoc_req(fc) || ieee80211_is_reassoc_req(fc))
  528. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(3);
  529. else
  530. tx_cmd->timeout.pm_frame_timeout = cpu_to_le16(2);
  531. } else {
  532. tx_cmd->timeout.pm_frame_timeout = 0;
  533. }
  534. tx_cmd->driver_txop = 0;
  535. tx_cmd->tx_flags = tx_flags;
  536. tx_cmd->next_frame_len = 0;
  537. }
  538. #define RTS_HCCA_RETRY_LIMIT 3
  539. #define RTS_DFAULT_RETRY_LIMIT 60
  540. static void iwl_tx_cmd_build_rate(struct iwl_priv *priv,
  541. struct iwl_tx_cmd *tx_cmd,
  542. struct ieee80211_tx_info *info,
  543. __le16 fc, int sta_id,
  544. int is_hcca)
  545. {
  546. u8 rts_retry_limit = 0;
  547. u8 data_retry_limit = 0;
  548. u8 rate_plcp;
  549. u16 rate_flags = 0;
  550. int rate_idx;
  551. rate_idx = min(ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xffff,
  552. IWL_RATE_COUNT - 1);
  553. rate_plcp = iwl_rates[rate_idx].plcp;
  554. rts_retry_limit = (is_hcca) ?
  555. RTS_HCCA_RETRY_LIMIT : RTS_DFAULT_RETRY_LIMIT;
  556. if ((rate_idx >= IWL_FIRST_CCK_RATE) && (rate_idx <= IWL_LAST_CCK_RATE))
  557. rate_flags |= RATE_MCS_CCK_MSK;
  558. if (ieee80211_is_probe_resp(fc)) {
  559. data_retry_limit = 3;
  560. if (data_retry_limit < rts_retry_limit)
  561. rts_retry_limit = data_retry_limit;
  562. } else
  563. data_retry_limit = IWL_DEFAULT_TX_RETRY;
  564. if (priv->data_retry_limit != -1)
  565. data_retry_limit = priv->data_retry_limit;
  566. if (ieee80211_is_data(fc)) {
  567. tx_cmd->initial_rate_index = 0;
  568. tx_cmd->tx_flags |= TX_CMD_FLG_STA_RATE_MSK;
  569. } else {
  570. switch (fc & cpu_to_le16(IEEE80211_FCTL_STYPE)) {
  571. case cpu_to_le16(IEEE80211_STYPE_AUTH):
  572. case cpu_to_le16(IEEE80211_STYPE_DEAUTH):
  573. case cpu_to_le16(IEEE80211_STYPE_ASSOC_REQ):
  574. case cpu_to_le16(IEEE80211_STYPE_REASSOC_REQ):
  575. if (tx_cmd->tx_flags & TX_CMD_FLG_RTS_MSK) {
  576. tx_cmd->tx_flags &= ~TX_CMD_FLG_RTS_MSK;
  577. tx_cmd->tx_flags |= TX_CMD_FLG_CTS_MSK;
  578. }
  579. break;
  580. default:
  581. break;
  582. }
  583. /* Alternate between antenna A and B for successive frames */
  584. if (priv->use_ant_b_for_management_frame) {
  585. priv->use_ant_b_for_management_frame = 0;
  586. rate_flags |= RATE_MCS_ANT_B_MSK;
  587. } else {
  588. priv->use_ant_b_for_management_frame = 1;
  589. rate_flags |= RATE_MCS_ANT_A_MSK;
  590. }
  591. }
  592. tx_cmd->rts_retry_limit = rts_retry_limit;
  593. tx_cmd->data_retry_limit = data_retry_limit;
  594. tx_cmd->rate_n_flags = iwl_hw_set_rate_n_flags(rate_plcp, rate_flags);
  595. }
  596. static void iwl_tx_cmd_build_hwcrypto(struct iwl_priv *priv,
  597. struct ieee80211_tx_info *info,
  598. struct iwl_tx_cmd *tx_cmd,
  599. struct sk_buff *skb_frag,
  600. int sta_id)
  601. {
  602. struct ieee80211_key_conf *keyconf = info->control.hw_key;
  603. switch (keyconf->alg) {
  604. case ALG_CCMP:
  605. tx_cmd->sec_ctl = TX_CMD_SEC_CCM;
  606. memcpy(tx_cmd->key, keyconf->key, keyconf->keylen);
  607. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  608. tx_cmd->tx_flags |= TX_CMD_FLG_AGG_CCMP_MSK;
  609. IWL_DEBUG_TX("tx_cmd with aes hwcrypto\n");
  610. break;
  611. case ALG_TKIP:
  612. tx_cmd->sec_ctl = TX_CMD_SEC_TKIP;
  613. ieee80211_get_tkip_key(keyconf, skb_frag,
  614. IEEE80211_TKIP_P2_KEY, tx_cmd->key);
  615. IWL_DEBUG_TX("tx_cmd with tkip hwcrypto\n");
  616. break;
  617. case ALG_WEP:
  618. tx_cmd->sec_ctl |= (TX_CMD_SEC_WEP |
  619. (keyconf->keyidx & TX_CMD_SEC_MSK) << TX_CMD_SEC_SHIFT);
  620. if (keyconf->keylen == WEP_KEY_LEN_128)
  621. tx_cmd->sec_ctl |= TX_CMD_SEC_KEY128;
  622. memcpy(&tx_cmd->key[3], keyconf->key, keyconf->keylen);
  623. IWL_DEBUG_TX("Configuring packet for WEP encryption "
  624. "with key %d\n", keyconf->keyidx);
  625. break;
  626. default:
  627. printk(KERN_ERR "Unknown encode alg %d\n", keyconf->alg);
  628. break;
  629. }
  630. }
  631. static void iwl_update_tx_stats(struct iwl_priv *priv, u16 fc, u16 len)
  632. {
  633. /* 0 - mgmt, 1 - cnt, 2 - data */
  634. int idx = (fc & IEEE80211_FCTL_FTYPE) >> 2;
  635. priv->tx_stats[idx].cnt++;
  636. priv->tx_stats[idx].bytes += len;
  637. }
  638. /*
  639. * start REPLY_TX command process
  640. */
  641. int iwl_tx_skb(struct iwl_priv *priv, struct sk_buff *skb)
  642. {
  643. struct ieee80211_hdr *hdr = (struct ieee80211_hdr *)skb->data;
  644. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  645. struct iwl_tfd_frame *tfd;
  646. struct iwl_tx_queue *txq;
  647. struct iwl_queue *q;
  648. struct iwl_cmd *out_cmd;
  649. struct iwl_tx_cmd *tx_cmd;
  650. int swq_id, txq_id;
  651. dma_addr_t phys_addr;
  652. dma_addr_t txcmd_phys;
  653. dma_addr_t scratch_phys;
  654. u16 len, idx, len_org;
  655. u16 seq_number = 0;
  656. __le16 fc;
  657. u8 hdr_len, unicast;
  658. u8 sta_id;
  659. u8 wait_write_ptr = 0;
  660. u8 tid = 0;
  661. u8 *qc = NULL;
  662. unsigned long flags;
  663. int ret;
  664. spin_lock_irqsave(&priv->lock, flags);
  665. if (iwl_is_rfkill(priv)) {
  666. IWL_DEBUG_DROP("Dropping - RF KILL\n");
  667. goto drop_unlock;
  668. }
  669. if (!priv->vif) {
  670. IWL_DEBUG_DROP("Dropping - !priv->vif\n");
  671. goto drop_unlock;
  672. }
  673. if ((ieee80211_get_tx_rate(priv->hw, info)->hw_value & 0xFF) ==
  674. IWL_INVALID_RATE) {
  675. IWL_ERROR("ERROR: No TX rate available.\n");
  676. goto drop_unlock;
  677. }
  678. unicast = !is_multicast_ether_addr(hdr->addr1);
  679. fc = hdr->frame_control;
  680. #ifdef CONFIG_IWLWIFI_DEBUG
  681. if (ieee80211_is_auth(fc))
  682. IWL_DEBUG_TX("Sending AUTH frame\n");
  683. else if (ieee80211_is_assoc_req(fc))
  684. IWL_DEBUG_TX("Sending ASSOC frame\n");
  685. else if (ieee80211_is_reassoc_req(fc))
  686. IWL_DEBUG_TX("Sending REASSOC frame\n");
  687. #endif
  688. /* drop all data frame if we are not associated */
  689. if (ieee80211_is_data(fc) &&
  690. (!iwl_is_associated(priv) ||
  691. ((priv->iw_mode == IEEE80211_IF_TYPE_STA) && !priv->assoc_id) ||
  692. !priv->assoc_station_added)) {
  693. IWL_DEBUG_DROP("Dropping - !iwl_is_associated\n");
  694. goto drop_unlock;
  695. }
  696. spin_unlock_irqrestore(&priv->lock, flags);
  697. hdr_len = ieee80211_get_hdrlen(le16_to_cpu(fc));
  698. /* Find (or create) index into station table for destination station */
  699. sta_id = iwl_get_sta_id(priv, hdr);
  700. if (sta_id == IWL_INVALID_STATION) {
  701. DECLARE_MAC_BUF(mac);
  702. IWL_DEBUG_DROP("Dropping - INVALID STATION: %s\n",
  703. print_mac(mac, hdr->addr1));
  704. goto drop;
  705. }
  706. IWL_DEBUG_TX("station Id %d\n", sta_id);
  707. swq_id = skb_get_queue_mapping(skb);
  708. txq_id = swq_id;
  709. if (ieee80211_is_data_qos(fc)) {
  710. qc = ieee80211_get_qos_ctl(hdr);
  711. tid = qc[0] & 0xf;
  712. seq_number = priv->stations[sta_id].tid[tid].seq_number;
  713. seq_number &= IEEE80211_SCTL_SEQ;
  714. hdr->seq_ctrl = hdr->seq_ctrl &
  715. __constant_cpu_to_le16(IEEE80211_SCTL_FRAG);
  716. hdr->seq_ctrl |= cpu_to_le16(seq_number);
  717. seq_number += 0x10;
  718. /* aggregation is on for this <sta,tid> */
  719. if (info->flags & IEEE80211_TX_CTL_AMPDU)
  720. txq_id = priv->stations[sta_id].tid[tid].agg.txq_id;
  721. priv->stations[sta_id].tid[tid].tfds_in_queue++;
  722. }
  723. /* Descriptor for chosen Tx queue */
  724. txq = &priv->txq[txq_id];
  725. q = &txq->q;
  726. spin_lock_irqsave(&priv->lock, flags);
  727. /* Set up first empty TFD within this queue's circular TFD buffer */
  728. tfd = &txq->bd[q->write_ptr];
  729. memset(tfd, 0, sizeof(*tfd));
  730. idx = get_cmd_index(q, q->write_ptr, 0);
  731. /* Set up driver data for this TFD */
  732. memset(&(txq->txb[q->write_ptr]), 0, sizeof(struct iwl_tx_info));
  733. txq->txb[q->write_ptr].skb[0] = skb;
  734. /* Set up first empty entry in queue's array of Tx/cmd buffers */
  735. out_cmd = txq->cmd[idx];
  736. tx_cmd = &out_cmd->cmd.tx;
  737. memset(&out_cmd->hdr, 0, sizeof(out_cmd->hdr));
  738. memset(tx_cmd, 0, sizeof(struct iwl_tx_cmd));
  739. /*
  740. * Set up the Tx-command (not MAC!) header.
  741. * Store the chosen Tx queue and TFD index within the sequence field;
  742. * after Tx, uCode's Tx response will return this value so driver can
  743. * locate the frame within the tx queue and do post-tx processing.
  744. */
  745. out_cmd->hdr.cmd = REPLY_TX;
  746. out_cmd->hdr.sequence = cpu_to_le16((u16)(QUEUE_TO_SEQ(txq_id) |
  747. INDEX_TO_SEQ(q->write_ptr)));
  748. /* Copy MAC header from skb into command buffer */
  749. memcpy(tx_cmd->hdr, hdr, hdr_len);
  750. /*
  751. * Use the first empty entry in this queue's command buffer array
  752. * to contain the Tx command and MAC header concatenated together
  753. * (payload data will be in another buffer).
  754. * Size of this varies, due to varying MAC header length.
  755. * If end is not dword aligned, we'll have 2 extra bytes at the end
  756. * of the MAC header (device reads on dword boundaries).
  757. * We'll tell device about this padding later.
  758. */
  759. len = sizeof(struct iwl_tx_cmd) +
  760. sizeof(struct iwl_cmd_header) + hdr_len;
  761. len_org = len;
  762. len = (len + 3) & ~3;
  763. if (len_org != len)
  764. len_org = 1;
  765. else
  766. len_org = 0;
  767. /* Physical address of this Tx command's header (not MAC header!),
  768. * within command buffer array. */
  769. txcmd_phys = pci_map_single(priv->pci_dev, out_cmd,
  770. sizeof(struct iwl_cmd), PCI_DMA_TODEVICE);
  771. txcmd_phys += offsetof(struct iwl_cmd, hdr);
  772. /* Add buffer containing Tx command and MAC(!) header to TFD's
  773. * first entry */
  774. iwl_hw_txq_attach_buf_to_tfd(priv, tfd, txcmd_phys, len);
  775. if (info->control.hw_key)
  776. iwl_tx_cmd_build_hwcrypto(priv, info, tx_cmd, skb, sta_id);
  777. /* Set up TFD's 2nd entry to point directly to remainder of skb,
  778. * if any (802.11 null frames have no payload). */
  779. len = skb->len - hdr_len;
  780. if (len) {
  781. phys_addr = pci_map_single(priv->pci_dev, skb->data + hdr_len,
  782. len, PCI_DMA_TODEVICE);
  783. iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, len);
  784. }
  785. /* Tell NIC about any 2-byte padding after MAC header */
  786. if (len_org)
  787. tx_cmd->tx_flags |= TX_CMD_FLG_MH_PAD_MSK;
  788. /* Total # bytes to be transmitted */
  789. len = (u16)skb->len;
  790. tx_cmd->len = cpu_to_le16(len);
  791. /* TODO need this for burst mode later on */
  792. iwl_tx_cmd_build_basic(priv, tx_cmd, info, hdr, unicast, sta_id);
  793. /* set is_hcca to 0; it probably will never be implemented */
  794. iwl_tx_cmd_build_rate(priv, tx_cmd, info, fc, sta_id, 0);
  795. iwl_update_tx_stats(priv, le16_to_cpu(fc), len);
  796. scratch_phys = txcmd_phys + sizeof(struct iwl_cmd_header) +
  797. offsetof(struct iwl_tx_cmd, scratch);
  798. tx_cmd->dram_lsb_ptr = cpu_to_le32(scratch_phys);
  799. tx_cmd->dram_msb_ptr = iwl_get_dma_hi_address(scratch_phys);
  800. if (!ieee80211_has_morefrags(hdr->frame_control)) {
  801. txq->need_update = 1;
  802. if (qc)
  803. priv->stations[sta_id].tid[tid].seq_number = seq_number;
  804. } else {
  805. wait_write_ptr = 1;
  806. txq->need_update = 0;
  807. }
  808. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd, sizeof(*tx_cmd));
  809. iwl_print_hex_dump(priv, IWL_DL_TX, (u8 *)tx_cmd->hdr, hdr_len);
  810. /* Set up entry for this TFD in Tx byte-count array */
  811. priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, len);
  812. /* Tell device the write index *just past* this latest filled TFD */
  813. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  814. ret = iwl_txq_update_write_ptr(priv, txq);
  815. spin_unlock_irqrestore(&priv->lock, flags);
  816. if (ret)
  817. return ret;
  818. if ((iwl_queue_space(q) < q->high_mark) && priv->mac80211_registered) {
  819. if (wait_write_ptr) {
  820. spin_lock_irqsave(&priv->lock, flags);
  821. txq->need_update = 1;
  822. iwl_txq_update_write_ptr(priv, txq);
  823. spin_unlock_irqrestore(&priv->lock, flags);
  824. } else {
  825. ieee80211_stop_queue(priv->hw, swq_id);
  826. }
  827. }
  828. return 0;
  829. drop_unlock:
  830. spin_unlock_irqrestore(&priv->lock, flags);
  831. drop:
  832. return -1;
  833. }
  834. EXPORT_SYMBOL(iwl_tx_skb);
  835. /*************** HOST COMMAND QUEUE FUNCTIONS *****/
  836. /**
  837. * iwl_enqueue_hcmd - enqueue a uCode command
  838. * @priv: device private data point
  839. * @cmd: a point to the ucode command structure
  840. *
  841. * The function returns < 0 values to indicate the operation is
  842. * failed. On success, it turns the index (> 0) of command in the
  843. * command queue.
  844. */
  845. int iwl_enqueue_hcmd(struct iwl_priv *priv, struct iwl_host_cmd *cmd)
  846. {
  847. struct iwl_tx_queue *txq = &priv->txq[IWL_CMD_QUEUE_NUM];
  848. struct iwl_queue *q = &txq->q;
  849. struct iwl_tfd_frame *tfd;
  850. struct iwl_cmd *out_cmd;
  851. dma_addr_t phys_addr;
  852. unsigned long flags;
  853. int len, ret;
  854. u32 idx;
  855. u16 fix_size;
  856. cmd->len = priv->cfg->ops->utils->get_hcmd_size(cmd->id, cmd->len);
  857. fix_size = (u16)(cmd->len + sizeof(out_cmd->hdr));
  858. /* If any of the command structures end up being larger than
  859. * the TFD_MAX_PAYLOAD_SIZE, and it sent as a 'small' command then
  860. * we will need to increase the size of the TFD entries */
  861. BUG_ON((fix_size > TFD_MAX_PAYLOAD_SIZE) &&
  862. !(cmd->meta.flags & CMD_SIZE_HUGE));
  863. if (iwl_is_rfkill(priv)) {
  864. IWL_DEBUG_INFO("Not sending command - RF KILL");
  865. return -EIO;
  866. }
  867. if (iwl_queue_space(q) < ((cmd->meta.flags & CMD_ASYNC) ? 2 : 1)) {
  868. IWL_ERROR("No space for Tx\n");
  869. return -ENOSPC;
  870. }
  871. spin_lock_irqsave(&priv->hcmd_lock, flags);
  872. tfd = &txq->bd[q->write_ptr];
  873. memset(tfd, 0, sizeof(*tfd));
  874. idx = get_cmd_index(q, q->write_ptr, cmd->meta.flags & CMD_SIZE_HUGE);
  875. out_cmd = txq->cmd[idx];
  876. out_cmd->hdr.cmd = cmd->id;
  877. memcpy(&out_cmd->meta, &cmd->meta, sizeof(cmd->meta));
  878. memcpy(&out_cmd->cmd.payload, cmd->data, cmd->len);
  879. /* At this point, the out_cmd now has all of the incoming cmd
  880. * information */
  881. out_cmd->hdr.flags = 0;
  882. out_cmd->hdr.sequence = cpu_to_le16(QUEUE_TO_SEQ(IWL_CMD_QUEUE_NUM) |
  883. INDEX_TO_SEQ(q->write_ptr));
  884. if (out_cmd->meta.flags & CMD_SIZE_HUGE)
  885. out_cmd->hdr.sequence |= cpu_to_le16(SEQ_HUGE_FRAME);
  886. len = (idx == TFD_CMD_SLOTS) ?
  887. IWL_MAX_SCAN_SIZE : sizeof(struct iwl_cmd);
  888. phys_addr = pci_map_single(priv->pci_dev, out_cmd, len,
  889. PCI_DMA_TODEVICE);
  890. phys_addr += offsetof(struct iwl_cmd, hdr);
  891. iwl_hw_txq_attach_buf_to_tfd(priv, tfd, phys_addr, fix_size);
  892. IWL_DEBUG_HC("Sending command %s (#%x), seq: 0x%04X, "
  893. "%d bytes at %d[%d]:%d\n",
  894. get_cmd_string(out_cmd->hdr.cmd),
  895. out_cmd->hdr.cmd, le16_to_cpu(out_cmd->hdr.sequence),
  896. fix_size, q->write_ptr, idx, IWL_CMD_QUEUE_NUM);
  897. txq->need_update = 1;
  898. /* Set up entry in queue's byte count circular buffer */
  899. priv->cfg->ops->lib->txq_update_byte_cnt_tbl(priv, txq, 0);
  900. /* Increment and update queue's write index */
  901. q->write_ptr = iwl_queue_inc_wrap(q->write_ptr, q->n_bd);
  902. ret = iwl_txq_update_write_ptr(priv, txq);
  903. spin_unlock_irqrestore(&priv->hcmd_lock, flags);
  904. return ret ? ret : idx;
  905. }
  906. int iwl_tx_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
  907. {
  908. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  909. struct iwl_queue *q = &txq->q;
  910. struct iwl_tx_info *tx_info;
  911. int nfreed = 0;
  912. if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
  913. IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
  914. "is out of range [0-%d] %d %d.\n", txq_id,
  915. index, q->n_bd, q->write_ptr, q->read_ptr);
  916. return 0;
  917. }
  918. for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
  919. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  920. tx_info = &txq->txb[txq->q.read_ptr];
  921. ieee80211_tx_status_irqsafe(priv->hw, tx_info->skb[0]);
  922. tx_info->skb[0] = NULL;
  923. if (priv->cfg->ops->lib->txq_inval_byte_cnt_tbl)
  924. priv->cfg->ops->lib->txq_inval_byte_cnt_tbl(priv, txq);
  925. iwl_hw_txq_free_tfd(priv, txq);
  926. nfreed++;
  927. }
  928. return nfreed;
  929. }
  930. EXPORT_SYMBOL(iwl_tx_queue_reclaim);
  931. /**
  932. * iwl_hcmd_queue_reclaim - Reclaim TX command queue entries already Tx'd
  933. *
  934. * When FW advances 'R' index, all entries between old and new 'R' index
  935. * need to be reclaimed. As result, some free space forms. If there is
  936. * enough free space (> low mark), wake the stack that feeds us.
  937. */
  938. static void iwl_hcmd_queue_reclaim(struct iwl_priv *priv, int txq_id, int index)
  939. {
  940. struct iwl_tx_queue *txq = &priv->txq[txq_id];
  941. struct iwl_queue *q = &txq->q;
  942. struct iwl_tfd_frame *bd = &txq->bd[index];
  943. dma_addr_t dma_addr;
  944. int is_odd, buf_len;
  945. int nfreed = 0;
  946. if ((index >= q->n_bd) || (iwl_queue_used(q, index) == 0)) {
  947. IWL_ERROR("Read index for DMA queue txq id (%d), index %d, "
  948. "is out of range [0-%d] %d %d.\n", txq_id,
  949. index, q->n_bd, q->write_ptr, q->read_ptr);
  950. return;
  951. }
  952. for (index = iwl_queue_inc_wrap(index, q->n_bd); q->read_ptr != index;
  953. q->read_ptr = iwl_queue_inc_wrap(q->read_ptr, q->n_bd)) {
  954. if (nfreed > 1) {
  955. IWL_ERROR("HCMD skipped: index (%d) %d %d\n", index,
  956. q->write_ptr, q->read_ptr);
  957. queue_work(priv->workqueue, &priv->restart);
  958. }
  959. is_odd = (index/2) & 0x1;
  960. if (is_odd) {
  961. dma_addr = IWL_GET_BITS(bd->pa[index], tb2_addr_lo16) |
  962. (IWL_GET_BITS(bd->pa[index],
  963. tb2_addr_hi20) << 16);
  964. buf_len = IWL_GET_BITS(bd->pa[index], tb2_len);
  965. } else {
  966. dma_addr = le32_to_cpu(bd->pa[index].tb1_addr);
  967. buf_len = IWL_GET_BITS(bd->pa[index], tb1_len);
  968. }
  969. pci_unmap_single(priv->pci_dev, dma_addr, buf_len,
  970. PCI_DMA_TODEVICE);
  971. nfreed++;
  972. }
  973. }
  974. /**
  975. * iwl_tx_cmd_complete - Pull unused buffers off the queue and reclaim them
  976. * @rxb: Rx buffer to reclaim
  977. *
  978. * If an Rx buffer has an async callback associated with it the callback
  979. * will be executed. The attached skb (if present) will only be freed
  980. * if the callback returns 1
  981. */
  982. void iwl_tx_cmd_complete(struct iwl_priv *priv, struct iwl_rx_mem_buffer *rxb)
  983. {
  984. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  985. u16 sequence = le16_to_cpu(pkt->hdr.sequence);
  986. int txq_id = SEQ_TO_QUEUE(sequence);
  987. int index = SEQ_TO_INDEX(sequence);
  988. int huge = sequence & SEQ_HUGE_FRAME;
  989. int cmd_index;
  990. struct iwl_cmd *cmd;
  991. /* If a Tx command is being handled and it isn't in the actual
  992. * command queue then there a command routing bug has been introduced
  993. * in the queue management code. */
  994. if (txq_id != IWL_CMD_QUEUE_NUM)
  995. IWL_ERROR("Error wrong command queue %d command id 0x%X\n",
  996. txq_id, pkt->hdr.cmd);
  997. BUG_ON(txq_id != IWL_CMD_QUEUE_NUM);
  998. cmd_index = get_cmd_index(&priv->txq[IWL_CMD_QUEUE_NUM].q, index, huge);
  999. cmd = priv->txq[IWL_CMD_QUEUE_NUM].cmd[cmd_index];
  1000. /* Input error checking is done when commands are added to queue. */
  1001. if (cmd->meta.flags & CMD_WANT_SKB) {
  1002. cmd->meta.source->u.skb = rxb->skb;
  1003. rxb->skb = NULL;
  1004. } else if (cmd->meta.u.callback &&
  1005. !cmd->meta.u.callback(priv, cmd, rxb->skb))
  1006. rxb->skb = NULL;
  1007. iwl_hcmd_queue_reclaim(priv, txq_id, index);
  1008. if (!(cmd->meta.flags & CMD_ASYNC)) {
  1009. clear_bit(STATUS_HCMD_ACTIVE, &priv->status);
  1010. wake_up_interruptible(&priv->wait_command_queue);
  1011. }
  1012. }
  1013. EXPORT_SYMBOL(iwl_tx_cmd_complete);
  1014. /*
  1015. * Find first available (lowest unused) Tx Queue, mark it "active".
  1016. * Called only when finding queue for aggregation.
  1017. * Should never return anything < 7, because they should already
  1018. * be in use as EDCA AC (0-3), Command (4), HCCA (5, 6).
  1019. */
  1020. static int iwl_txq_ctx_activate_free(struct iwl_priv *priv)
  1021. {
  1022. int txq_id;
  1023. for (txq_id = 0; txq_id < priv->hw_params.max_txq_num; txq_id++)
  1024. if (!test_and_set_bit(txq_id, &priv->txq_ctx_active_msk))
  1025. return txq_id;
  1026. return -1;
  1027. }
  1028. int iwl_tx_agg_start(struct iwl_priv *priv, const u8 *ra, u16 tid, u16 *ssn)
  1029. {
  1030. int sta_id;
  1031. int tx_fifo;
  1032. int txq_id;
  1033. int ret;
  1034. unsigned long flags;
  1035. struct iwl_tid_data *tid_data;
  1036. DECLARE_MAC_BUF(mac);
  1037. if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
  1038. tx_fifo = default_tid_to_tx_fifo[tid];
  1039. else
  1040. return -EINVAL;
  1041. IWL_WARNING("%s on ra = %s tid = %d\n",
  1042. __func__, print_mac(mac, ra), tid);
  1043. sta_id = iwl_find_station(priv, ra);
  1044. if (sta_id == IWL_INVALID_STATION)
  1045. return -ENXIO;
  1046. if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_OFF) {
  1047. IWL_ERROR("Start AGG when state is not IWL_AGG_OFF !\n");
  1048. return -ENXIO;
  1049. }
  1050. txq_id = iwl_txq_ctx_activate_free(priv);
  1051. if (txq_id == -1)
  1052. return -ENXIO;
  1053. spin_lock_irqsave(&priv->sta_lock, flags);
  1054. tid_data = &priv->stations[sta_id].tid[tid];
  1055. *ssn = SEQ_TO_SN(tid_data->seq_number);
  1056. tid_data->agg.txq_id = txq_id;
  1057. spin_unlock_irqrestore(&priv->sta_lock, flags);
  1058. ret = priv->cfg->ops->lib->txq_agg_enable(priv, txq_id, tx_fifo,
  1059. sta_id, tid, *ssn);
  1060. if (ret)
  1061. return ret;
  1062. if (tid_data->tfds_in_queue == 0) {
  1063. printk(KERN_ERR "HW queue is empty\n");
  1064. tid_data->agg.state = IWL_AGG_ON;
  1065. ieee80211_start_tx_ba_cb_irqsafe(priv->hw, ra, tid);
  1066. } else {
  1067. IWL_DEBUG_HT("HW queue is NOT empty: %d packets in HW queue\n",
  1068. tid_data->tfds_in_queue);
  1069. tid_data->agg.state = IWL_EMPTYING_HW_QUEUE_ADDBA;
  1070. }
  1071. return ret;
  1072. }
  1073. EXPORT_SYMBOL(iwl_tx_agg_start);
  1074. int iwl_tx_agg_stop(struct iwl_priv *priv , const u8 *ra, u16 tid)
  1075. {
  1076. int tx_fifo_id, txq_id, sta_id, ssn = -1;
  1077. struct iwl_tid_data *tid_data;
  1078. int ret, write_ptr, read_ptr;
  1079. unsigned long flags;
  1080. DECLARE_MAC_BUF(mac);
  1081. if (!ra) {
  1082. IWL_ERROR("ra = NULL\n");
  1083. return -EINVAL;
  1084. }
  1085. if (likely(tid < ARRAY_SIZE(default_tid_to_tx_fifo)))
  1086. tx_fifo_id = default_tid_to_tx_fifo[tid];
  1087. else
  1088. return -EINVAL;
  1089. sta_id = iwl_find_station(priv, ra);
  1090. if (sta_id == IWL_INVALID_STATION)
  1091. return -ENXIO;
  1092. if (priv->stations[sta_id].tid[tid].agg.state != IWL_AGG_ON)
  1093. IWL_WARNING("Stopping AGG while state not IWL_AGG_ON\n");
  1094. tid_data = &priv->stations[sta_id].tid[tid];
  1095. ssn = (tid_data->seq_number & IEEE80211_SCTL_SEQ) >> 4;
  1096. txq_id = tid_data->agg.txq_id;
  1097. write_ptr = priv->txq[txq_id].q.write_ptr;
  1098. read_ptr = priv->txq[txq_id].q.read_ptr;
  1099. /* The queue is not empty */
  1100. if (write_ptr != read_ptr) {
  1101. IWL_DEBUG_HT("Stopping a non empty AGG HW QUEUE\n");
  1102. priv->stations[sta_id].tid[tid].agg.state =
  1103. IWL_EMPTYING_HW_QUEUE_DELBA;
  1104. return 0;
  1105. }
  1106. IWL_DEBUG_HT("HW queue is empty\n");
  1107. priv->stations[sta_id].tid[tid].agg.state = IWL_AGG_OFF;
  1108. spin_lock_irqsave(&priv->lock, flags);
  1109. ret = priv->cfg->ops->lib->txq_agg_disable(priv, txq_id, ssn,
  1110. tx_fifo_id);
  1111. spin_unlock_irqrestore(&priv->lock, flags);
  1112. if (ret)
  1113. return ret;
  1114. ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, ra, tid);
  1115. return 0;
  1116. }
  1117. EXPORT_SYMBOL(iwl_tx_agg_stop);
  1118. int iwl_txq_check_empty(struct iwl_priv *priv, int sta_id, u8 tid, int txq_id)
  1119. {
  1120. struct iwl_queue *q = &priv->txq[txq_id].q;
  1121. u8 *addr = priv->stations[sta_id].sta.sta.addr;
  1122. struct iwl_tid_data *tid_data = &priv->stations[sta_id].tid[tid];
  1123. switch (priv->stations[sta_id].tid[tid].agg.state) {
  1124. case IWL_EMPTYING_HW_QUEUE_DELBA:
  1125. /* We are reclaiming the last packet of the */
  1126. /* aggregated HW queue */
  1127. if (txq_id == tid_data->agg.txq_id &&
  1128. q->read_ptr == q->write_ptr) {
  1129. u16 ssn = SEQ_TO_SN(tid_data->seq_number);
  1130. int tx_fifo = default_tid_to_tx_fifo[tid];
  1131. IWL_DEBUG_HT("HW queue empty: continue DELBA flow\n");
  1132. priv->cfg->ops->lib->txq_agg_disable(priv, txq_id,
  1133. ssn, tx_fifo);
  1134. tid_data->agg.state = IWL_AGG_OFF;
  1135. ieee80211_stop_tx_ba_cb_irqsafe(priv->hw, addr, tid);
  1136. }
  1137. break;
  1138. case IWL_EMPTYING_HW_QUEUE_ADDBA:
  1139. /* We are reclaiming the last packet of the queue */
  1140. if (tid_data->tfds_in_queue == 0) {
  1141. IWL_DEBUG_HT("HW queue empty: continue ADDBA flow\n");
  1142. tid_data->agg.state = IWL_AGG_ON;
  1143. ieee80211_start_tx_ba_cb_irqsafe(priv->hw, addr, tid);
  1144. }
  1145. break;
  1146. }
  1147. return 0;
  1148. }
  1149. EXPORT_SYMBOL(iwl_txq_check_empty);
  1150. /**
  1151. * iwl_tx_status_reply_compressed_ba - Update tx status from block-ack
  1152. *
  1153. * Go through block-ack's bitmap of ACK'd frames, update driver's record of
  1154. * ACK vs. not. This gets sent to mac80211, then to rate scaling algo.
  1155. */
  1156. static int iwl_tx_status_reply_compressed_ba(struct iwl_priv *priv,
  1157. struct iwl_ht_agg *agg,
  1158. struct iwl_compressed_ba_resp *ba_resp)
  1159. {
  1160. int i, sh, ack;
  1161. u16 seq_ctl = le16_to_cpu(ba_resp->seq_ctl);
  1162. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  1163. u64 bitmap;
  1164. int successes = 0;
  1165. struct ieee80211_tx_info *info;
  1166. if (unlikely(!agg->wait_for_ba)) {
  1167. IWL_ERROR("Received BA when not expected\n");
  1168. return -EINVAL;
  1169. }
  1170. /* Mark that the expected block-ack response arrived */
  1171. agg->wait_for_ba = 0;
  1172. IWL_DEBUG_TX_REPLY("BA %d %d\n", agg->start_idx, ba_resp->seq_ctl);
  1173. /* Calculate shift to align block-ack bits with our Tx window bits */
  1174. sh = agg->start_idx - SEQ_TO_INDEX(seq_ctl>>4);
  1175. if (sh < 0) /* tbw something is wrong with indices */
  1176. sh += 0x100;
  1177. /* don't use 64-bit values for now */
  1178. bitmap = le64_to_cpu(ba_resp->bitmap) >> sh;
  1179. if (agg->frame_count > (64 - sh)) {
  1180. IWL_DEBUG_TX_REPLY("more frames than bitmap size");
  1181. return -1;
  1182. }
  1183. /* check for success or failure according to the
  1184. * transmitted bitmap and block-ack bitmap */
  1185. bitmap &= agg->bitmap;
  1186. /* For each frame attempted in aggregation,
  1187. * update driver's record of tx frame's status. */
  1188. for (i = 0; i < agg->frame_count ; i++) {
  1189. ack = bitmap & (1ULL << i);
  1190. successes += !!ack;
  1191. IWL_DEBUG_TX_REPLY("%s ON i=%d idx=%d raw=%d\n",
  1192. ack? "ACK":"NACK", i, (agg->start_idx + i) & 0xff,
  1193. agg->start_idx + i);
  1194. }
  1195. info = IEEE80211_SKB_CB(priv->txq[scd_flow].txb[agg->start_idx].skb[0]);
  1196. memset(&info->status, 0, sizeof(info->status));
  1197. info->flags = IEEE80211_TX_STAT_ACK;
  1198. info->flags |= IEEE80211_TX_STAT_AMPDU;
  1199. info->status.ampdu_ack_map = successes;
  1200. info->status.ampdu_ack_len = agg->frame_count;
  1201. iwl_hwrate_to_tx_control(priv, agg->rate_n_flags, info);
  1202. IWL_DEBUG_TX_REPLY("Bitmap %llx\n", (unsigned long long)bitmap);
  1203. return 0;
  1204. }
  1205. /**
  1206. * iwl_rx_reply_compressed_ba - Handler for REPLY_COMPRESSED_BA
  1207. *
  1208. * Handles block-acknowledge notification from device, which reports success
  1209. * of frames sent via aggregation.
  1210. */
  1211. void iwl_rx_reply_compressed_ba(struct iwl_priv *priv,
  1212. struct iwl_rx_mem_buffer *rxb)
  1213. {
  1214. struct iwl_rx_packet *pkt = (struct iwl_rx_packet *)rxb->skb->data;
  1215. struct iwl_compressed_ba_resp *ba_resp = &pkt->u.compressed_ba;
  1216. int index;
  1217. struct iwl_tx_queue *txq = NULL;
  1218. struct iwl_ht_agg *agg;
  1219. DECLARE_MAC_BUF(mac);
  1220. /* "flow" corresponds to Tx queue */
  1221. u16 scd_flow = le16_to_cpu(ba_resp->scd_flow);
  1222. /* "ssn" is start of block-ack Tx window, corresponds to index
  1223. * (in Tx queue's circular buffer) of first TFD/frame in window */
  1224. u16 ba_resp_scd_ssn = le16_to_cpu(ba_resp->scd_ssn);
  1225. if (scd_flow >= priv->hw_params.max_txq_num) {
  1226. IWL_ERROR("BUG_ON scd_flow is bigger than number of queues");
  1227. return;
  1228. }
  1229. txq = &priv->txq[scd_flow];
  1230. agg = &priv->stations[ba_resp->sta_id].tid[ba_resp->tid].agg;
  1231. /* Find index just before block-ack window */
  1232. index = iwl_queue_dec_wrap(ba_resp_scd_ssn & 0xff, txq->q.n_bd);
  1233. /* TODO: Need to get this copy more safely - now good for debug */
  1234. IWL_DEBUG_TX_REPLY("REPLY_COMPRESSED_BA [%d]Received from %s, "
  1235. "sta_id = %d\n",
  1236. agg->wait_for_ba,
  1237. print_mac(mac, (u8 *) &ba_resp->sta_addr_lo32),
  1238. ba_resp->sta_id);
  1239. IWL_DEBUG_TX_REPLY("TID = %d, SeqCtl = %d, bitmap = 0x%llx, scd_flow = "
  1240. "%d, scd_ssn = %d\n",
  1241. ba_resp->tid,
  1242. ba_resp->seq_ctl,
  1243. (unsigned long long)le64_to_cpu(ba_resp->bitmap),
  1244. ba_resp->scd_flow,
  1245. ba_resp->scd_ssn);
  1246. IWL_DEBUG_TX_REPLY("DAT start_idx = %d, bitmap = 0x%llx \n",
  1247. agg->start_idx,
  1248. (unsigned long long)agg->bitmap);
  1249. /* Update driver's record of ACK vs. not for each frame in window */
  1250. iwl_tx_status_reply_compressed_ba(priv, agg, ba_resp);
  1251. /* Release all TFDs before the SSN, i.e. all TFDs in front of
  1252. * block-ack window (we assume that they've been successfully
  1253. * transmitted ... if not, it's too late anyway). */
  1254. if (txq->q.read_ptr != (ba_resp_scd_ssn & 0xff)) {
  1255. /* calculate mac80211 ampdu sw queue to wake */
  1256. int ampdu_q =
  1257. scd_flow - priv->hw_params.first_ampdu_q + priv->hw->queues;
  1258. int freed = iwl_tx_queue_reclaim(priv, scd_flow, index);
  1259. priv->stations[ba_resp->sta_id].
  1260. tid[ba_resp->tid].tfds_in_queue -= freed;
  1261. if (iwl_queue_space(&txq->q) > txq->q.low_mark &&
  1262. priv->mac80211_registered &&
  1263. agg->state != IWL_EMPTYING_HW_QUEUE_DELBA)
  1264. ieee80211_wake_queue(priv->hw, ampdu_q);
  1265. iwl_txq_check_empty(priv, ba_resp->sta_id,
  1266. ba_resp->tid, scd_flow);
  1267. }
  1268. }
  1269. EXPORT_SYMBOL(iwl_rx_reply_compressed_ba);
  1270. #ifdef CONFIG_IWLWIFI_DEBUG
  1271. #define TX_STATUS_ENTRY(x) case TX_STATUS_FAIL_ ## x: return #x
  1272. const char *iwl_get_tx_fail_reason(u32 status)
  1273. {
  1274. switch (status & TX_STATUS_MSK) {
  1275. case TX_STATUS_SUCCESS:
  1276. return "SUCCESS";
  1277. TX_STATUS_ENTRY(SHORT_LIMIT);
  1278. TX_STATUS_ENTRY(LONG_LIMIT);
  1279. TX_STATUS_ENTRY(FIFO_UNDERRUN);
  1280. TX_STATUS_ENTRY(MGMNT_ABORT);
  1281. TX_STATUS_ENTRY(NEXT_FRAG);
  1282. TX_STATUS_ENTRY(LIFE_EXPIRE);
  1283. TX_STATUS_ENTRY(DEST_PS);
  1284. TX_STATUS_ENTRY(ABORTED);
  1285. TX_STATUS_ENTRY(BT_RETRY);
  1286. TX_STATUS_ENTRY(STA_INVALID);
  1287. TX_STATUS_ENTRY(FRAG_DROPPED);
  1288. TX_STATUS_ENTRY(TID_DISABLE);
  1289. TX_STATUS_ENTRY(FRAME_FLUSHED);
  1290. TX_STATUS_ENTRY(INSUFFICIENT_CF_POLL);
  1291. TX_STATUS_ENTRY(TX_LOCKED);
  1292. TX_STATUS_ENTRY(NO_BEACON_ON_RADAR);
  1293. }
  1294. return "UNKNOWN";
  1295. }
  1296. EXPORT_SYMBOL(iwl_get_tx_fail_reason);
  1297. #endif /* CONFIG_IWLWIFI_DEBUG */