desc.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789
  1. /*
  2. * Copyright (c) 2004-2008 Reyk Floeter <reyk@openbsd.org>
  3. * Copyright (c) 2006-2008 Nick Kossifidis <mickflemm@gmail.com>
  4. * Copyright (c) 2007-2008 Pavel Roskin <proski@gnu.org>
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. *
  18. */
  19. /******************************\
  20. Hardware Descriptor Functions
  21. \******************************/
  22. #include "ath5k.h"
  23. #include "reg.h"
  24. #include "debug.h"
  25. /**
  26. * DOC: Hardware descriptor functions
  27. *
  28. * Here we handle the processing of the low-level hw descriptors
  29. * that hw reads and writes via DMA for each TX and RX attempt (that means
  30. * we can also have descriptors for failed TX/RX tries). We have two kind of
  31. * descriptors for RX and TX, control descriptors tell the hw how to send or
  32. * receive a packet where to read/write it from/to etc and status descriptors
  33. * that contain information about how the packet was sent or received (errors
  34. * included).
  35. *
  36. * Descriptor format is not exactly the same for each MAC chip version so we
  37. * have function pointers on &struct ath5k_hw we initialize at runtime based on
  38. * the chip used.
  39. */
  40. /************************\
  41. * TX Control descriptors *
  42. \************************/
  43. /**
  44. * ath5k_hw_setup_2word_tx_desc() - Initialize a 2-word tx control descriptor
  45. * @ah: The &struct ath5k_hw
  46. * @desc: The &struct ath5k_desc
  47. * @pkt_len: Frame length in bytes
  48. * @hdr_len: Header length in bytes (only used on AR5210)
  49. * @padsize: Any padding we've added to the frame length
  50. * @type: One of enum ath5k_pkt_type
  51. * @tx_power: Tx power in 0.5dB steps
  52. * @tx_rate0: HW idx for transmission rate
  53. * @tx_tries0: Max number of retransmissions
  54. * @key_index: Index on key table to use for encryption
  55. * @antenna_mode: Which antenna to use (0 for auto)
  56. * @flags: One of AR5K_TXDESC_* flags (desc.h)
  57. * @rtscts_rate: HW idx for RTS/CTS transmission rate
  58. * @rtscts_duration: What to put on duration field on the header of RTS/CTS
  59. *
  60. * Internal function to initialize a 2-Word TX control descriptor
  61. * found on AR5210 and AR5211 MACs chips.
  62. *
  63. * Returns 0 on success or -EINVAL on false input
  64. */
  65. static int
  66. ath5k_hw_setup_2word_tx_desc(struct ath5k_hw *ah,
  67. struct ath5k_desc *desc,
  68. unsigned int pkt_len, unsigned int hdr_len,
  69. int padsize,
  70. enum ath5k_pkt_type type,
  71. unsigned int tx_power,
  72. unsigned int tx_rate0, unsigned int tx_tries0,
  73. unsigned int key_index,
  74. unsigned int antenna_mode,
  75. unsigned int flags,
  76. unsigned int rtscts_rate, unsigned int rtscts_duration)
  77. {
  78. u32 frame_type;
  79. struct ath5k_hw_2w_tx_ctl *tx_ctl;
  80. unsigned int frame_len;
  81. tx_ctl = &desc->ud.ds_tx5210.tx_ctl;
  82. /*
  83. * Validate input
  84. * - Zero retries don't make sense.
  85. * - A zero rate will put the HW into a mode where it continuously sends
  86. * noise on the channel, so it is important to avoid this.
  87. */
  88. if (unlikely(tx_tries0 == 0)) {
  89. ATH5K_ERR(ah, "zero retries\n");
  90. WARN_ON(1);
  91. return -EINVAL;
  92. }
  93. if (unlikely(tx_rate0 == 0)) {
  94. ATH5K_ERR(ah, "zero rate\n");
  95. WARN_ON(1);
  96. return -EINVAL;
  97. }
  98. /* Clear descriptor */
  99. memset(&desc->ud.ds_tx5210, 0, sizeof(struct ath5k_hw_5210_tx_desc));
  100. /* Setup control descriptor */
  101. /* Verify and set frame length */
  102. /* remove padding we might have added before */
  103. frame_len = pkt_len - padsize + FCS_LEN;
  104. if (frame_len & ~AR5K_2W_TX_DESC_CTL0_FRAME_LEN)
  105. return -EINVAL;
  106. tx_ctl->tx_control_0 = frame_len & AR5K_2W_TX_DESC_CTL0_FRAME_LEN;
  107. /* Verify and set buffer length */
  108. /* NB: beacon's BufLen must be a multiple of 4 bytes */
  109. if (type == AR5K_PKT_TYPE_BEACON)
  110. pkt_len = roundup(pkt_len, 4);
  111. if (pkt_len & ~AR5K_2W_TX_DESC_CTL1_BUF_LEN)
  112. return -EINVAL;
  113. tx_ctl->tx_control_1 = pkt_len & AR5K_2W_TX_DESC_CTL1_BUF_LEN;
  114. /*
  115. * Verify and set header length (only 5210)
  116. */
  117. if (ah->ah_version == AR5K_AR5210) {
  118. if (hdr_len & ~AR5K_2W_TX_DESC_CTL0_HEADER_LEN_5210)
  119. return -EINVAL;
  120. tx_ctl->tx_control_0 |=
  121. AR5K_REG_SM(hdr_len, AR5K_2W_TX_DESC_CTL0_HEADER_LEN_5210);
  122. }
  123. /*Differences between 5210-5211*/
  124. if (ah->ah_version == AR5K_AR5210) {
  125. switch (type) {
  126. case AR5K_PKT_TYPE_BEACON:
  127. case AR5K_PKT_TYPE_PROBE_RESP:
  128. frame_type = AR5K_AR5210_TX_DESC_FRAME_TYPE_NO_DELAY;
  129. break;
  130. case AR5K_PKT_TYPE_PIFS:
  131. frame_type = AR5K_AR5210_TX_DESC_FRAME_TYPE_PIFS;
  132. break;
  133. default:
  134. frame_type = type;
  135. break;
  136. }
  137. tx_ctl->tx_control_0 |=
  138. AR5K_REG_SM(frame_type, AR5K_2W_TX_DESC_CTL0_FRAME_TYPE_5210) |
  139. AR5K_REG_SM(tx_rate0, AR5K_2W_TX_DESC_CTL0_XMIT_RATE);
  140. } else {
  141. tx_ctl->tx_control_0 |=
  142. AR5K_REG_SM(tx_rate0, AR5K_2W_TX_DESC_CTL0_XMIT_RATE) |
  143. AR5K_REG_SM(antenna_mode,
  144. AR5K_2W_TX_DESC_CTL0_ANT_MODE_XMIT);
  145. tx_ctl->tx_control_1 |=
  146. AR5K_REG_SM(type, AR5K_2W_TX_DESC_CTL1_FRAME_TYPE_5211);
  147. }
  148. #define _TX_FLAGS(_c, _flag) \
  149. if (flags & AR5K_TXDESC_##_flag) { \
  150. tx_ctl->tx_control_##_c |= \
  151. AR5K_2W_TX_DESC_CTL##_c##_##_flag; \
  152. }
  153. #define _TX_FLAGS_5211(_c, _flag) \
  154. if (flags & AR5K_TXDESC_##_flag) { \
  155. tx_ctl->tx_control_##_c |= \
  156. AR5K_2W_TX_DESC_CTL##_c##_##_flag##_5211; \
  157. }
  158. _TX_FLAGS(0, CLRDMASK);
  159. _TX_FLAGS(0, INTREQ);
  160. _TX_FLAGS(0, RTSENA);
  161. if (ah->ah_version == AR5K_AR5211) {
  162. _TX_FLAGS_5211(0, VEOL);
  163. _TX_FLAGS_5211(1, NOACK);
  164. }
  165. #undef _TX_FLAGS
  166. #undef _TX_FLAGS_5211
  167. /*
  168. * WEP crap
  169. */
  170. if (key_index != AR5K_TXKEYIX_INVALID) {
  171. tx_ctl->tx_control_0 |=
  172. AR5K_2W_TX_DESC_CTL0_ENCRYPT_KEY_VALID;
  173. tx_ctl->tx_control_1 |=
  174. AR5K_REG_SM(key_index,
  175. AR5K_2W_TX_DESC_CTL1_ENC_KEY_IDX);
  176. }
  177. /*
  178. * RTS/CTS Duration [5210 ?]
  179. */
  180. if ((ah->ah_version == AR5K_AR5210) &&
  181. (flags & (AR5K_TXDESC_RTSENA | AR5K_TXDESC_CTSENA)))
  182. tx_ctl->tx_control_1 |= rtscts_duration &
  183. AR5K_2W_TX_DESC_CTL1_RTS_DURATION_5210;
  184. return 0;
  185. }
  186. /**
  187. * ath5k_hw_setup_4word_tx_desc() - Initialize a 4-word tx control descriptor
  188. * @ah: The &struct ath5k_hw
  189. * @desc: The &struct ath5k_desc
  190. * @pkt_len: Frame length in bytes
  191. * @hdr_len: Header length in bytes (only used on AR5210)
  192. * @padsize: Any padding we've added to the frame length
  193. * @type: One of enum ath5k_pkt_type
  194. * @tx_power: Tx power in 0.5dB steps
  195. * @tx_rate0: HW idx for transmission rate
  196. * @tx_tries0: Max number of retransmissions
  197. * @key_index: Index on key table to use for encryption
  198. * @antenna_mode: Which antenna to use (0 for auto)
  199. * @flags: One of AR5K_TXDESC_* flags (desc.h)
  200. * @rtscts_rate: HW idx for RTS/CTS transmission rate
  201. * @rtscts_duration: What to put on duration field on the header of RTS/CTS
  202. *
  203. * Internal function to initialize a 4-Word TX control descriptor
  204. * found on AR5212 and later MACs chips.
  205. *
  206. * Returns 0 on success or -EINVAL on false input
  207. */
  208. static int
  209. ath5k_hw_setup_4word_tx_desc(struct ath5k_hw *ah,
  210. struct ath5k_desc *desc,
  211. unsigned int pkt_len, unsigned int hdr_len,
  212. int padsize,
  213. enum ath5k_pkt_type type,
  214. unsigned int tx_power,
  215. unsigned int tx_rate0, unsigned int tx_tries0,
  216. unsigned int key_index,
  217. unsigned int antenna_mode,
  218. unsigned int flags,
  219. unsigned int rtscts_rate, unsigned int rtscts_duration)
  220. {
  221. struct ath5k_hw_4w_tx_ctl *tx_ctl;
  222. unsigned int frame_len;
  223. /*
  224. * Use local variables for these to reduce load/store access on
  225. * uncached memory
  226. */
  227. u32 txctl0 = 0, txctl1 = 0, txctl2 = 0, txctl3 = 0;
  228. tx_ctl = &desc->ud.ds_tx5212.tx_ctl;
  229. /*
  230. * Validate input
  231. * - Zero retries don't make sense.
  232. * - A zero rate will put the HW into a mode where it continuously sends
  233. * noise on the channel, so it is important to avoid this.
  234. */
  235. if (unlikely(tx_tries0 == 0)) {
  236. ATH5K_ERR(ah, "zero retries\n");
  237. WARN_ON(1);
  238. return -EINVAL;
  239. }
  240. if (unlikely(tx_rate0 == 0)) {
  241. ATH5K_ERR(ah, "zero rate\n");
  242. WARN_ON(1);
  243. return -EINVAL;
  244. }
  245. tx_power += ah->ah_txpower.txp_offset;
  246. if (tx_power > AR5K_TUNE_MAX_TXPOWER)
  247. tx_power = AR5K_TUNE_MAX_TXPOWER;
  248. /* Clear descriptor status area */
  249. memset(&desc->ud.ds_tx5212.tx_stat, 0,
  250. sizeof(desc->ud.ds_tx5212.tx_stat));
  251. /* Setup control descriptor */
  252. /* Verify and set frame length */
  253. /* remove padding we might have added before */
  254. frame_len = pkt_len - padsize + FCS_LEN;
  255. if (frame_len & ~AR5K_4W_TX_DESC_CTL0_FRAME_LEN)
  256. return -EINVAL;
  257. txctl0 = frame_len & AR5K_4W_TX_DESC_CTL0_FRAME_LEN;
  258. /* Verify and set buffer length */
  259. /* NB: beacon's BufLen must be a multiple of 4 bytes */
  260. if (type == AR5K_PKT_TYPE_BEACON)
  261. pkt_len = roundup(pkt_len, 4);
  262. if (pkt_len & ~AR5K_4W_TX_DESC_CTL1_BUF_LEN)
  263. return -EINVAL;
  264. txctl1 = pkt_len & AR5K_4W_TX_DESC_CTL1_BUF_LEN;
  265. txctl0 |= AR5K_REG_SM(tx_power, AR5K_4W_TX_DESC_CTL0_XMIT_POWER) |
  266. AR5K_REG_SM(antenna_mode, AR5K_4W_TX_DESC_CTL0_ANT_MODE_XMIT);
  267. txctl1 |= AR5K_REG_SM(type, AR5K_4W_TX_DESC_CTL1_FRAME_TYPE);
  268. txctl2 = AR5K_REG_SM(tx_tries0, AR5K_4W_TX_DESC_CTL2_XMIT_TRIES0);
  269. txctl3 = tx_rate0 & AR5K_4W_TX_DESC_CTL3_XMIT_RATE0;
  270. #define _TX_FLAGS(_c, _flag) \
  271. if (flags & AR5K_TXDESC_##_flag) { \
  272. txctl##_c |= AR5K_4W_TX_DESC_CTL##_c##_##_flag; \
  273. }
  274. _TX_FLAGS(0, CLRDMASK);
  275. _TX_FLAGS(0, VEOL);
  276. _TX_FLAGS(0, INTREQ);
  277. _TX_FLAGS(0, RTSENA);
  278. _TX_FLAGS(0, CTSENA);
  279. _TX_FLAGS(1, NOACK);
  280. #undef _TX_FLAGS
  281. /*
  282. * WEP crap
  283. */
  284. if (key_index != AR5K_TXKEYIX_INVALID) {
  285. txctl0 |= AR5K_4W_TX_DESC_CTL0_ENCRYPT_KEY_VALID;
  286. txctl1 |= AR5K_REG_SM(key_index,
  287. AR5K_4W_TX_DESC_CTL1_ENCRYPT_KEY_IDX);
  288. }
  289. /*
  290. * RTS/CTS
  291. */
  292. if (flags & (AR5K_TXDESC_RTSENA | AR5K_TXDESC_CTSENA)) {
  293. if ((flags & AR5K_TXDESC_RTSENA) &&
  294. (flags & AR5K_TXDESC_CTSENA))
  295. return -EINVAL;
  296. txctl2 |= rtscts_duration & AR5K_4W_TX_DESC_CTL2_RTS_DURATION;
  297. txctl3 |= AR5K_REG_SM(rtscts_rate,
  298. AR5K_4W_TX_DESC_CTL3_RTS_CTS_RATE);
  299. }
  300. tx_ctl->tx_control_0 = txctl0;
  301. tx_ctl->tx_control_1 = txctl1;
  302. tx_ctl->tx_control_2 = txctl2;
  303. tx_ctl->tx_control_3 = txctl3;
  304. return 0;
  305. }
  306. /**
  307. * ath5k_hw_setup_mrr_tx_desc() - Initialize an MRR tx control descriptor
  308. * @ah: The &struct ath5k_hw
  309. * @desc: The &struct ath5k_desc
  310. * @tx_rate1: HW idx for rate used on transmission series 1
  311. * @tx_tries1: Max number of retransmissions for transmission series 1
  312. * @tx_rate2: HW idx for rate used on transmission series 2
  313. * @tx_tries2: Max number of retransmissions for transmission series 2
  314. * @tx_rate3: HW idx for rate used on transmission series 3
  315. * @tx_tries3: Max number of retransmissions for transmission series 3
  316. *
  317. * Multi rate retry (MRR) tx control descriptors are available only on AR5212
  318. * MACs, they are part of the normal 4-word tx control descriptor (see above)
  319. * but we handle them through a separate function for better abstraction.
  320. *
  321. * Returns 0 on success or -EINVAL on invalid input
  322. */
  323. int
  324. ath5k_hw_setup_mrr_tx_desc(struct ath5k_hw *ah,
  325. struct ath5k_desc *desc,
  326. u_int tx_rate1, u_int tx_tries1,
  327. u_int tx_rate2, u_int tx_tries2,
  328. u_int tx_rate3, u_int tx_tries3)
  329. {
  330. struct ath5k_hw_4w_tx_ctl *tx_ctl;
  331. /* no mrr support for cards older than 5212 */
  332. if (ah->ah_version < AR5K_AR5212)
  333. return 0;
  334. /*
  335. * Rates can be 0 as long as the retry count is 0 too.
  336. * A zero rate and nonzero retry count will put the HW into a mode where
  337. * it continuously sends noise on the channel, so it is important to
  338. * avoid this.
  339. */
  340. if (unlikely((tx_rate1 == 0 && tx_tries1 != 0) ||
  341. (tx_rate2 == 0 && tx_tries2 != 0) ||
  342. (tx_rate3 == 0 && tx_tries3 != 0))) {
  343. ATH5K_ERR(ah, "zero rate\n");
  344. WARN_ON(1);
  345. return -EINVAL;
  346. }
  347. if (ah->ah_version == AR5K_AR5212) {
  348. tx_ctl = &desc->ud.ds_tx5212.tx_ctl;
  349. #define _XTX_TRIES(_n) \
  350. if (tx_tries##_n) { \
  351. tx_ctl->tx_control_2 |= \
  352. AR5K_REG_SM(tx_tries##_n, \
  353. AR5K_4W_TX_DESC_CTL2_XMIT_TRIES##_n); \
  354. tx_ctl->tx_control_3 |= \
  355. AR5K_REG_SM(tx_rate##_n, \
  356. AR5K_4W_TX_DESC_CTL3_XMIT_RATE##_n); \
  357. }
  358. _XTX_TRIES(1);
  359. _XTX_TRIES(2);
  360. _XTX_TRIES(3);
  361. #undef _XTX_TRIES
  362. return 1;
  363. }
  364. return 0;
  365. }
  366. /***********************\
  367. * TX Status descriptors *
  368. \***********************/
  369. /**
  370. * ath5k_hw_proc_2word_tx_status() - Process a tx status descriptor on 5210/1
  371. * @ah: The &struct ath5k_hw
  372. * @desc: The &struct ath5k_desc
  373. * @ts: The &struct ath5k_tx_status
  374. */
  375. static int
  376. ath5k_hw_proc_2word_tx_status(struct ath5k_hw *ah,
  377. struct ath5k_desc *desc,
  378. struct ath5k_tx_status *ts)
  379. {
  380. struct ath5k_hw_2w_tx_ctl *tx_ctl;
  381. struct ath5k_hw_tx_status *tx_status;
  382. tx_ctl = &desc->ud.ds_tx5210.tx_ctl;
  383. tx_status = &desc->ud.ds_tx5210.tx_stat;
  384. /* No frame has been send or error */
  385. if (unlikely((tx_status->tx_status_1 & AR5K_DESC_TX_STATUS1_DONE) == 0))
  386. return -EINPROGRESS;
  387. /*
  388. * Get descriptor status
  389. */
  390. ts->ts_tstamp = AR5K_REG_MS(tx_status->tx_status_0,
  391. AR5K_DESC_TX_STATUS0_SEND_TIMESTAMP);
  392. ts->ts_shortretry = AR5K_REG_MS(tx_status->tx_status_0,
  393. AR5K_DESC_TX_STATUS0_SHORT_RETRY_COUNT);
  394. ts->ts_final_retry = AR5K_REG_MS(tx_status->tx_status_0,
  395. AR5K_DESC_TX_STATUS0_LONG_RETRY_COUNT);
  396. /*TODO: ts->ts_virtcol + test*/
  397. ts->ts_seqnum = AR5K_REG_MS(tx_status->tx_status_1,
  398. AR5K_DESC_TX_STATUS1_SEQ_NUM);
  399. ts->ts_rssi = AR5K_REG_MS(tx_status->tx_status_1,
  400. AR5K_DESC_TX_STATUS1_ACK_SIG_STRENGTH);
  401. ts->ts_antenna = 1;
  402. ts->ts_status = 0;
  403. ts->ts_final_idx = 0;
  404. if (!(tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FRAME_XMIT_OK)) {
  405. if (tx_status->tx_status_0 &
  406. AR5K_DESC_TX_STATUS0_EXCESSIVE_RETRIES)
  407. ts->ts_status |= AR5K_TXERR_XRETRY;
  408. if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FIFO_UNDERRUN)
  409. ts->ts_status |= AR5K_TXERR_FIFO;
  410. if (tx_status->tx_status_0 & AR5K_DESC_TX_STATUS0_FILTERED)
  411. ts->ts_status |= AR5K_TXERR_FILT;
  412. }
  413. return 0;
  414. }
  415. /**
  416. * ath5k_hw_proc_4word_tx_status() - Process a tx status descriptor on 5212
  417. * @ah: The &struct ath5k_hw
  418. * @desc: The &struct ath5k_desc
  419. * @ts: The &struct ath5k_tx_status
  420. */
  421. static int
  422. ath5k_hw_proc_4word_tx_status(struct ath5k_hw *ah,
  423. struct ath5k_desc *desc,
  424. struct ath5k_tx_status *ts)
  425. {
  426. struct ath5k_hw_4w_tx_ctl *tx_ctl;
  427. struct ath5k_hw_tx_status *tx_status;
  428. u32 txstat0, txstat1;
  429. tx_ctl = &desc->ud.ds_tx5212.tx_ctl;
  430. tx_status = &desc->ud.ds_tx5212.tx_stat;
  431. txstat1 = ACCESS_ONCE(tx_status->tx_status_1);
  432. /* No frame has been send or error */
  433. if (unlikely(!(txstat1 & AR5K_DESC_TX_STATUS1_DONE)))
  434. return -EINPROGRESS;
  435. txstat0 = ACCESS_ONCE(tx_status->tx_status_0);
  436. /*
  437. * Get descriptor status
  438. */
  439. ts->ts_tstamp = AR5K_REG_MS(txstat0,
  440. AR5K_DESC_TX_STATUS0_SEND_TIMESTAMP);
  441. ts->ts_shortretry = AR5K_REG_MS(txstat0,
  442. AR5K_DESC_TX_STATUS0_SHORT_RETRY_COUNT);
  443. ts->ts_final_retry = AR5K_REG_MS(txstat0,
  444. AR5K_DESC_TX_STATUS0_LONG_RETRY_COUNT);
  445. ts->ts_seqnum = AR5K_REG_MS(txstat1,
  446. AR5K_DESC_TX_STATUS1_SEQ_NUM);
  447. ts->ts_rssi = AR5K_REG_MS(txstat1,
  448. AR5K_DESC_TX_STATUS1_ACK_SIG_STRENGTH);
  449. ts->ts_antenna = (txstat1 &
  450. AR5K_DESC_TX_STATUS1_XMIT_ANTENNA_5212) ? 2 : 1;
  451. ts->ts_status = 0;
  452. ts->ts_final_idx = AR5K_REG_MS(txstat1,
  453. AR5K_DESC_TX_STATUS1_FINAL_TS_IX_5212);
  454. /* TX error */
  455. if (!(txstat0 & AR5K_DESC_TX_STATUS0_FRAME_XMIT_OK)) {
  456. if (txstat0 & AR5K_DESC_TX_STATUS0_EXCESSIVE_RETRIES)
  457. ts->ts_status |= AR5K_TXERR_XRETRY;
  458. if (txstat0 & AR5K_DESC_TX_STATUS0_FIFO_UNDERRUN)
  459. ts->ts_status |= AR5K_TXERR_FIFO;
  460. if (txstat0 & AR5K_DESC_TX_STATUS0_FILTERED)
  461. ts->ts_status |= AR5K_TXERR_FILT;
  462. }
  463. return 0;
  464. }
  465. /****************\
  466. * RX Descriptors *
  467. \****************/
  468. /**
  469. * ath5k_hw_setup_rx_desc() - Initialize an rx control descriptor
  470. * @ah: The &struct ath5k_hw
  471. * @desc: The &struct ath5k_desc
  472. * @size: RX buffer length in bytes
  473. * @flags: One of AR5K_RXDESC_* flags
  474. */
  475. int
  476. ath5k_hw_setup_rx_desc(struct ath5k_hw *ah,
  477. struct ath5k_desc *desc,
  478. u32 size, unsigned int flags)
  479. {
  480. struct ath5k_hw_rx_ctl *rx_ctl;
  481. rx_ctl = &desc->ud.ds_rx.rx_ctl;
  482. /*
  483. * Clear the descriptor
  484. * If we don't clean the status descriptor,
  485. * while scanning we get too many results,
  486. * most of them virtual, after some secs
  487. * of scanning system hangs. M.F.
  488. */
  489. memset(&desc->ud.ds_rx, 0, sizeof(struct ath5k_hw_all_rx_desc));
  490. if (unlikely(size & ~AR5K_DESC_RX_CTL1_BUF_LEN))
  491. return -EINVAL;
  492. /* Setup descriptor */
  493. rx_ctl->rx_control_1 = size & AR5K_DESC_RX_CTL1_BUF_LEN;
  494. if (flags & AR5K_RXDESC_INTREQ)
  495. rx_ctl->rx_control_1 |= AR5K_DESC_RX_CTL1_INTREQ;
  496. return 0;
  497. }
  498. /**
  499. * ath5k_hw_proc_5210_rx_status() - Process the rx status descriptor on 5210/1
  500. * @ah: The &struct ath5k_hw
  501. * @desc: The &struct ath5k_desc
  502. * @rs: The &struct ath5k_rx_status
  503. *
  504. * Internal function used to process an RX status descriptor
  505. * on AR5210/5211 MAC.
  506. *
  507. * Returns 0 on success or -EINPROGRESS in case we haven't received the who;e
  508. * frame yet.
  509. */
  510. static int
  511. ath5k_hw_proc_5210_rx_status(struct ath5k_hw *ah,
  512. struct ath5k_desc *desc,
  513. struct ath5k_rx_status *rs)
  514. {
  515. struct ath5k_hw_rx_status *rx_status;
  516. rx_status = &desc->ud.ds_rx.rx_stat;
  517. /* No frame received / not ready */
  518. if (unlikely(!(rx_status->rx_status_1 &
  519. AR5K_5210_RX_DESC_STATUS1_DONE)))
  520. return -EINPROGRESS;
  521. memset(rs, 0, sizeof(struct ath5k_rx_status));
  522. /*
  523. * Frame receive status
  524. */
  525. rs->rs_datalen = rx_status->rx_status_0 &
  526. AR5K_5210_RX_DESC_STATUS0_DATA_LEN;
  527. rs->rs_rssi = AR5K_REG_MS(rx_status->rx_status_0,
  528. AR5K_5210_RX_DESC_STATUS0_RECEIVE_SIGNAL);
  529. rs->rs_rate = AR5K_REG_MS(rx_status->rx_status_0,
  530. AR5K_5210_RX_DESC_STATUS0_RECEIVE_RATE);
  531. rs->rs_more = !!(rx_status->rx_status_0 &
  532. AR5K_5210_RX_DESC_STATUS0_MORE);
  533. /* TODO: this timestamp is 13 bit, later on we assume 15 bit!
  534. * also the HAL code for 5210 says the timestamp is bits [10..22] of the
  535. * TSF, and extends the timestamp here to 15 bit.
  536. * we need to check on 5210...
  537. */
  538. rs->rs_tstamp = AR5K_REG_MS(rx_status->rx_status_1,
  539. AR5K_5210_RX_DESC_STATUS1_RECEIVE_TIMESTAMP);
  540. if (ah->ah_version == AR5K_AR5211)
  541. rs->rs_antenna = AR5K_REG_MS(rx_status->rx_status_0,
  542. AR5K_5210_RX_DESC_STATUS0_RECEIVE_ANT_5211);
  543. else
  544. rs->rs_antenna = (rx_status->rx_status_0 &
  545. AR5K_5210_RX_DESC_STATUS0_RECEIVE_ANT_5210)
  546. ? 2 : 1;
  547. /*
  548. * Key table status
  549. */
  550. if (rx_status->rx_status_1 & AR5K_5210_RX_DESC_STATUS1_KEY_INDEX_VALID)
  551. rs->rs_keyix = AR5K_REG_MS(rx_status->rx_status_1,
  552. AR5K_5210_RX_DESC_STATUS1_KEY_INDEX);
  553. else
  554. rs->rs_keyix = AR5K_RXKEYIX_INVALID;
  555. /*
  556. * Receive/descriptor errors
  557. */
  558. if (!(rx_status->rx_status_1 &
  559. AR5K_5210_RX_DESC_STATUS1_FRAME_RECEIVE_OK)) {
  560. if (rx_status->rx_status_1 &
  561. AR5K_5210_RX_DESC_STATUS1_CRC_ERROR)
  562. rs->rs_status |= AR5K_RXERR_CRC;
  563. /* only on 5210 */
  564. if ((ah->ah_version == AR5K_AR5210) &&
  565. (rx_status->rx_status_1 &
  566. AR5K_5210_RX_DESC_STATUS1_FIFO_OVERRUN_5210))
  567. rs->rs_status |= AR5K_RXERR_FIFO;
  568. if (rx_status->rx_status_1 &
  569. AR5K_5210_RX_DESC_STATUS1_PHY_ERROR) {
  570. rs->rs_status |= AR5K_RXERR_PHY;
  571. rs->rs_phyerr = AR5K_REG_MS(rx_status->rx_status_1,
  572. AR5K_5210_RX_DESC_STATUS1_PHY_ERROR);
  573. }
  574. if (rx_status->rx_status_1 &
  575. AR5K_5210_RX_DESC_STATUS1_DECRYPT_CRC_ERROR)
  576. rs->rs_status |= AR5K_RXERR_DECRYPT;
  577. }
  578. return 0;
  579. }
  580. /**
  581. * ath5k_hw_proc_5212_rx_status() - Process the rx status descriptor on 5212
  582. * @ah: The &struct ath5k_hw
  583. * @desc: The &struct ath5k_desc
  584. * @rs: The &struct ath5k_rx_status
  585. *
  586. * Internal function used to process an RX status descriptor
  587. * on AR5212 and later MAC.
  588. *
  589. * Returns 0 on success or -EINPROGRESS in case we haven't received the who;e
  590. * frame yet.
  591. */
  592. static int
  593. ath5k_hw_proc_5212_rx_status(struct ath5k_hw *ah,
  594. struct ath5k_desc *desc,
  595. struct ath5k_rx_status *rs)
  596. {
  597. struct ath5k_hw_rx_status *rx_status;
  598. u32 rxstat0, rxstat1;
  599. rx_status = &desc->ud.ds_rx.rx_stat;
  600. rxstat1 = ACCESS_ONCE(rx_status->rx_status_1);
  601. /* No frame received / not ready */
  602. if (unlikely(!(rxstat1 & AR5K_5212_RX_DESC_STATUS1_DONE)))
  603. return -EINPROGRESS;
  604. memset(rs, 0, sizeof(struct ath5k_rx_status));
  605. rxstat0 = ACCESS_ONCE(rx_status->rx_status_0);
  606. /*
  607. * Frame receive status
  608. */
  609. rs->rs_datalen = rxstat0 & AR5K_5212_RX_DESC_STATUS0_DATA_LEN;
  610. rs->rs_rssi = AR5K_REG_MS(rxstat0,
  611. AR5K_5212_RX_DESC_STATUS0_RECEIVE_SIGNAL);
  612. rs->rs_rate = AR5K_REG_MS(rxstat0,
  613. AR5K_5212_RX_DESC_STATUS0_RECEIVE_RATE);
  614. rs->rs_antenna = AR5K_REG_MS(rxstat0,
  615. AR5K_5212_RX_DESC_STATUS0_RECEIVE_ANTENNA);
  616. rs->rs_more = !!(rxstat0 & AR5K_5212_RX_DESC_STATUS0_MORE);
  617. rs->rs_tstamp = AR5K_REG_MS(rxstat1,
  618. AR5K_5212_RX_DESC_STATUS1_RECEIVE_TIMESTAMP);
  619. /*
  620. * Key table status
  621. */
  622. if (rxstat1 & AR5K_5212_RX_DESC_STATUS1_KEY_INDEX_VALID)
  623. rs->rs_keyix = AR5K_REG_MS(rxstat1,
  624. AR5K_5212_RX_DESC_STATUS1_KEY_INDEX);
  625. else
  626. rs->rs_keyix = AR5K_RXKEYIX_INVALID;
  627. /*
  628. * Receive/descriptor errors
  629. */
  630. if (!(rxstat1 & AR5K_5212_RX_DESC_STATUS1_FRAME_RECEIVE_OK)) {
  631. if (rxstat1 & AR5K_5212_RX_DESC_STATUS1_CRC_ERROR)
  632. rs->rs_status |= AR5K_RXERR_CRC;
  633. if (rxstat1 & AR5K_5212_RX_DESC_STATUS1_PHY_ERROR) {
  634. rs->rs_status |= AR5K_RXERR_PHY;
  635. rs->rs_phyerr = AR5K_REG_MS(rxstat1,
  636. AR5K_5212_RX_DESC_STATUS1_PHY_ERROR_CODE);
  637. if (!ah->ah_capabilities.cap_has_phyerr_counters)
  638. ath5k_ani_phy_error_report(ah, rs->rs_phyerr);
  639. }
  640. if (rxstat1 & AR5K_5212_RX_DESC_STATUS1_DECRYPT_CRC_ERROR)
  641. rs->rs_status |= AR5K_RXERR_DECRYPT;
  642. if (rxstat1 & AR5K_5212_RX_DESC_STATUS1_MIC_ERROR)
  643. rs->rs_status |= AR5K_RXERR_MIC;
  644. }
  645. return 0;
  646. }
  647. /********\
  648. * Attach *
  649. \********/
  650. /**
  651. * ath5k_hw_init_desc_functions() - Init function pointers inside ah
  652. * @ah: The &struct ath5k_hw
  653. *
  654. * Maps the internal descriptor functions to the function pointers on ah, used
  655. * from above. This is used as an abstraction layer to handle the various chips
  656. * the same way.
  657. */
  658. int
  659. ath5k_hw_init_desc_functions(struct ath5k_hw *ah)
  660. {
  661. if (ah->ah_version == AR5K_AR5212) {
  662. ah->ah_setup_tx_desc = ath5k_hw_setup_4word_tx_desc;
  663. ah->ah_proc_tx_desc = ath5k_hw_proc_4word_tx_status;
  664. ah->ah_proc_rx_desc = ath5k_hw_proc_5212_rx_status;
  665. } else if (ah->ah_version <= AR5K_AR5211) {
  666. ah->ah_setup_tx_desc = ath5k_hw_setup_2word_tx_desc;
  667. ah->ah_proc_tx_desc = ath5k_hw_proc_2word_tx_status;
  668. ah->ah_proc_rx_desc = ath5k_hw_proc_5210_rx_status;
  669. } else
  670. return -ENOTSUPP;
  671. return 0;
  672. }