rt2800pci.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293
  1. /*
  2. Copyright (C) 2009 Ivo van Doorn <IvDoorn@gmail.com>
  3. Copyright (C) 2009 Alban Browaeys <prahal@yahoo.com>
  4. Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
  5. Copyright (C) 2009 Luis Correia <luis.f.correia@gmail.com>
  6. Copyright (C) 2009 Mattias Nissler <mattias.nissler@gmx.de>
  7. Copyright (C) 2009 Mark Asselstine <asselsm@gmail.com>
  8. Copyright (C) 2009 Xose Vazquez Perez <xose.vazquez@gmail.com>
  9. Copyright (C) 2009 Bart Zolnierkiewicz <bzolnier@gmail.com>
  10. <http://rt2x00.serialmonkey.com>
  11. This program is free software; you can redistribute it and/or modify
  12. it under the terms of the GNU General Public License as published by
  13. the Free Software Foundation; either version 2 of the License, or
  14. (at your option) any later version.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the
  21. Free Software Foundation, Inc.,
  22. 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. /*
  25. Module: rt2800pci
  26. Abstract: rt2800pci device specific routines.
  27. Supported chipsets: RT2800E & RT2800ED.
  28. */
  29. #include <linux/crc-ccitt.h>
  30. #include <linux/delay.h>
  31. #include <linux/etherdevice.h>
  32. #include <linux/init.h>
  33. #include <linux/kernel.h>
  34. #include <linux/module.h>
  35. #include <linux/pci.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/eeprom_93cx6.h>
  38. #include "rt2x00.h"
  39. #include "rt2x00pci.h"
  40. #include "rt2x00soc.h"
  41. #include "rt2800lib.h"
  42. #include "rt2800.h"
  43. #include "rt2800pci.h"
  44. /*
  45. * Allow hardware encryption to be disabled.
  46. */
  47. static int modparam_nohwcrypt = 1;
  48. module_param_named(nohwcrypt, modparam_nohwcrypt, bool, S_IRUGO);
  49. MODULE_PARM_DESC(nohwcrypt, "Disable hardware encryption.");
  50. static void rt2800pci_mcu_status(struct rt2x00_dev *rt2x00dev, const u8 token)
  51. {
  52. unsigned int i;
  53. u32 reg;
  54. for (i = 0; i < 200; i++) {
  55. rt2800_register_read(rt2x00dev, H2M_MAILBOX_CID, &reg);
  56. if ((rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD0) == token) ||
  57. (rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD1) == token) ||
  58. (rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD2) == token) ||
  59. (rt2x00_get_field32(reg, H2M_MAILBOX_CID_CMD3) == token))
  60. break;
  61. udelay(REGISTER_BUSY_DELAY);
  62. }
  63. if (i == 200)
  64. ERROR(rt2x00dev, "MCU request failed, no response from hardware\n");
  65. rt2800_register_write(rt2x00dev, H2M_MAILBOX_STATUS, ~0);
  66. rt2800_register_write(rt2x00dev, H2M_MAILBOX_CID, ~0);
  67. }
  68. #ifdef CONFIG_RT2800PCI_SOC
  69. static void rt2800pci_read_eeprom_soc(struct rt2x00_dev *rt2x00dev)
  70. {
  71. u32 *base_addr = (u32 *) KSEG1ADDR(0x1F040000); /* XXX for RT3052 */
  72. memcpy_fromio(rt2x00dev->eeprom, base_addr, EEPROM_SIZE);
  73. }
  74. #else
  75. static inline void rt2800pci_read_eeprom_soc(struct rt2x00_dev *rt2x00dev)
  76. {
  77. }
  78. #endif /* CONFIG_RT2800PCI_SOC */
  79. #ifdef CONFIG_RT2800PCI_PCI
  80. static void rt2800pci_eepromregister_read(struct eeprom_93cx6 *eeprom)
  81. {
  82. struct rt2x00_dev *rt2x00dev = eeprom->data;
  83. u32 reg;
  84. rt2800_register_read(rt2x00dev, E2PROM_CSR, &reg);
  85. eeprom->reg_data_in = !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_IN);
  86. eeprom->reg_data_out = !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_OUT);
  87. eeprom->reg_data_clock =
  88. !!rt2x00_get_field32(reg, E2PROM_CSR_DATA_CLOCK);
  89. eeprom->reg_chip_select =
  90. !!rt2x00_get_field32(reg, E2PROM_CSR_CHIP_SELECT);
  91. }
  92. static void rt2800pci_eepromregister_write(struct eeprom_93cx6 *eeprom)
  93. {
  94. struct rt2x00_dev *rt2x00dev = eeprom->data;
  95. u32 reg = 0;
  96. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_IN, !!eeprom->reg_data_in);
  97. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_OUT, !!eeprom->reg_data_out);
  98. rt2x00_set_field32(&reg, E2PROM_CSR_DATA_CLOCK,
  99. !!eeprom->reg_data_clock);
  100. rt2x00_set_field32(&reg, E2PROM_CSR_CHIP_SELECT,
  101. !!eeprom->reg_chip_select);
  102. rt2800_register_write(rt2x00dev, E2PROM_CSR, reg);
  103. }
  104. static void rt2800pci_read_eeprom_pci(struct rt2x00_dev *rt2x00dev)
  105. {
  106. struct eeprom_93cx6 eeprom;
  107. u32 reg;
  108. rt2800_register_read(rt2x00dev, E2PROM_CSR, &reg);
  109. eeprom.data = rt2x00dev;
  110. eeprom.register_read = rt2800pci_eepromregister_read;
  111. eeprom.register_write = rt2800pci_eepromregister_write;
  112. eeprom.width = !rt2x00_get_field32(reg, E2PROM_CSR_TYPE) ?
  113. PCI_EEPROM_WIDTH_93C46 : PCI_EEPROM_WIDTH_93C66;
  114. eeprom.reg_data_in = 0;
  115. eeprom.reg_data_out = 0;
  116. eeprom.reg_data_clock = 0;
  117. eeprom.reg_chip_select = 0;
  118. eeprom_93cx6_multiread(&eeprom, EEPROM_BASE, rt2x00dev->eeprom,
  119. EEPROM_SIZE / sizeof(u16));
  120. }
  121. static int rt2800pci_efuse_detect(struct rt2x00_dev *rt2x00dev)
  122. {
  123. return rt2800_efuse_detect(rt2x00dev);
  124. }
  125. static inline void rt2800pci_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
  126. {
  127. rt2800_read_eeprom_efuse(rt2x00dev);
  128. }
  129. #else
  130. static inline void rt2800pci_read_eeprom_pci(struct rt2x00_dev *rt2x00dev)
  131. {
  132. }
  133. static inline int rt2800pci_efuse_detect(struct rt2x00_dev *rt2x00dev)
  134. {
  135. return 0;
  136. }
  137. static inline void rt2800pci_read_eeprom_efuse(struct rt2x00_dev *rt2x00dev)
  138. {
  139. }
  140. #endif /* CONFIG_RT2800PCI_PCI */
  141. /*
  142. * Firmware functions
  143. */
  144. static char *rt2800pci_get_firmware_name(struct rt2x00_dev *rt2x00dev)
  145. {
  146. return FIRMWARE_RT2860;
  147. }
  148. static int rt2800pci_check_firmware(struct rt2x00_dev *rt2x00dev,
  149. const u8 *data, const size_t len)
  150. {
  151. u16 fw_crc;
  152. u16 crc;
  153. /*
  154. * Only support 8kb firmware files.
  155. */
  156. if (len != 8192)
  157. return FW_BAD_LENGTH;
  158. /*
  159. * The last 2 bytes in the firmware array are the crc checksum itself,
  160. * this means that we should never pass those 2 bytes to the crc
  161. * algorithm.
  162. */
  163. fw_crc = (data[len - 2] << 8 | data[len - 1]);
  164. /*
  165. * Use the crc ccitt algorithm.
  166. * This will return the same value as the legacy driver which
  167. * used bit ordering reversion on the both the firmware bytes
  168. * before input input as well as on the final output.
  169. * Obviously using crc ccitt directly is much more efficient.
  170. */
  171. crc = crc_ccitt(~0, data, len - 2);
  172. /*
  173. * There is a small difference between the crc-itu-t + bitrev and
  174. * the crc-ccitt crc calculation. In the latter method the 2 bytes
  175. * will be swapped, use swab16 to convert the crc to the correct
  176. * value.
  177. */
  178. crc = swab16(crc);
  179. return (fw_crc == crc) ? FW_OK : FW_BAD_CRC;
  180. }
  181. static int rt2800pci_load_firmware(struct rt2x00_dev *rt2x00dev,
  182. const u8 *data, const size_t len)
  183. {
  184. unsigned int i;
  185. u32 reg;
  186. /*
  187. * Wait for stable hardware.
  188. */
  189. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  190. rt2800_register_read(rt2x00dev, MAC_CSR0, &reg);
  191. if (reg && reg != ~0)
  192. break;
  193. msleep(1);
  194. }
  195. if (i == REGISTER_BUSY_COUNT) {
  196. ERROR(rt2x00dev, "Unstable hardware.\n");
  197. return -EBUSY;
  198. }
  199. rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0x00000002);
  200. rt2800_register_write(rt2x00dev, AUTOWAKEUP_CFG, 0x00000000);
  201. /*
  202. * Disable DMA, will be reenabled later when enabling
  203. * the radio.
  204. */
  205. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  206. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  207. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
  208. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  209. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
  210. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  211. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  212. /*
  213. * enable Host program ram write selection
  214. */
  215. reg = 0;
  216. rt2x00_set_field32(&reg, PBF_SYS_CTRL_HOST_RAM_WRITE, 1);
  217. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, reg);
  218. /*
  219. * Write firmware to device.
  220. */
  221. rt2800_register_multiwrite(rt2x00dev, FIRMWARE_IMAGE_BASE,
  222. data, len);
  223. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000);
  224. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00001);
  225. /*
  226. * Wait for device to stabilize.
  227. */
  228. for (i = 0; i < REGISTER_BUSY_COUNT; i++) {
  229. rt2800_register_read(rt2x00dev, PBF_SYS_CTRL, &reg);
  230. if (rt2x00_get_field32(reg, PBF_SYS_CTRL_READY))
  231. break;
  232. msleep(1);
  233. }
  234. if (i == REGISTER_BUSY_COUNT) {
  235. ERROR(rt2x00dev, "PBF system register not ready.\n");
  236. return -EBUSY;
  237. }
  238. /*
  239. * Disable interrupts
  240. */
  241. rt2x00dev->ops->lib->set_device_state(rt2x00dev, STATE_RADIO_IRQ_OFF);
  242. /*
  243. * Initialize BBP R/W access agent
  244. */
  245. rt2800_register_write(rt2x00dev, H2M_BBP_AGENT, 0);
  246. rt2800_register_write(rt2x00dev, H2M_MAILBOX_CSR, 0);
  247. return 0;
  248. }
  249. /*
  250. * Initialization functions.
  251. */
  252. static bool rt2800pci_get_entry_state(struct queue_entry *entry)
  253. {
  254. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  255. u32 word;
  256. if (entry->queue->qid == QID_RX) {
  257. rt2x00_desc_read(entry_priv->desc, 1, &word);
  258. return (!rt2x00_get_field32(word, RXD_W1_DMA_DONE));
  259. } else {
  260. rt2x00_desc_read(entry_priv->desc, 1, &word);
  261. return (!rt2x00_get_field32(word, TXD_W1_DMA_DONE));
  262. }
  263. }
  264. static void rt2800pci_clear_entry(struct queue_entry *entry)
  265. {
  266. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  267. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  268. u32 word;
  269. if (entry->queue->qid == QID_RX) {
  270. rt2x00_desc_read(entry_priv->desc, 0, &word);
  271. rt2x00_set_field32(&word, RXD_W0_SDP0, skbdesc->skb_dma);
  272. rt2x00_desc_write(entry_priv->desc, 0, word);
  273. rt2x00_desc_read(entry_priv->desc, 1, &word);
  274. rt2x00_set_field32(&word, RXD_W1_DMA_DONE, 0);
  275. rt2x00_desc_write(entry_priv->desc, 1, word);
  276. } else {
  277. rt2x00_desc_read(entry_priv->desc, 1, &word);
  278. rt2x00_set_field32(&word, TXD_W1_DMA_DONE, 1);
  279. rt2x00_desc_write(entry_priv->desc, 1, word);
  280. }
  281. }
  282. static int rt2800pci_init_queues(struct rt2x00_dev *rt2x00dev)
  283. {
  284. struct queue_entry_priv_pci *entry_priv;
  285. u32 reg;
  286. rt2800_register_read(rt2x00dev, WPDMA_RST_IDX, &reg);
  287. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX0, 1);
  288. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX1, 1);
  289. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX2, 1);
  290. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX3, 1);
  291. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX4, 1);
  292. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX5, 1);
  293. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DRX_IDX0, 1);
  294. rt2800_register_write(rt2x00dev, WPDMA_RST_IDX, reg);
  295. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e1f);
  296. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e00);
  297. /*
  298. * Initialize registers.
  299. */
  300. entry_priv = rt2x00dev->tx[0].entries[0].priv_data;
  301. rt2800_register_write(rt2x00dev, TX_BASE_PTR0, entry_priv->desc_dma);
  302. rt2800_register_write(rt2x00dev, TX_MAX_CNT0, rt2x00dev->tx[0].limit);
  303. rt2800_register_write(rt2x00dev, TX_CTX_IDX0, 0);
  304. rt2800_register_write(rt2x00dev, TX_DTX_IDX0, 0);
  305. entry_priv = rt2x00dev->tx[1].entries[0].priv_data;
  306. rt2800_register_write(rt2x00dev, TX_BASE_PTR1, entry_priv->desc_dma);
  307. rt2800_register_write(rt2x00dev, TX_MAX_CNT1, rt2x00dev->tx[1].limit);
  308. rt2800_register_write(rt2x00dev, TX_CTX_IDX1, 0);
  309. rt2800_register_write(rt2x00dev, TX_DTX_IDX1, 0);
  310. entry_priv = rt2x00dev->tx[2].entries[0].priv_data;
  311. rt2800_register_write(rt2x00dev, TX_BASE_PTR2, entry_priv->desc_dma);
  312. rt2800_register_write(rt2x00dev, TX_MAX_CNT2, rt2x00dev->tx[2].limit);
  313. rt2800_register_write(rt2x00dev, TX_CTX_IDX2, 0);
  314. rt2800_register_write(rt2x00dev, TX_DTX_IDX2, 0);
  315. entry_priv = rt2x00dev->tx[3].entries[0].priv_data;
  316. rt2800_register_write(rt2x00dev, TX_BASE_PTR3, entry_priv->desc_dma);
  317. rt2800_register_write(rt2x00dev, TX_MAX_CNT3, rt2x00dev->tx[3].limit);
  318. rt2800_register_write(rt2x00dev, TX_CTX_IDX3, 0);
  319. rt2800_register_write(rt2x00dev, TX_DTX_IDX3, 0);
  320. entry_priv = rt2x00dev->rx->entries[0].priv_data;
  321. rt2800_register_write(rt2x00dev, RX_BASE_PTR, entry_priv->desc_dma);
  322. rt2800_register_write(rt2x00dev, RX_MAX_CNT, rt2x00dev->rx[0].limit);
  323. rt2800_register_write(rt2x00dev, RX_CRX_IDX, rt2x00dev->rx[0].limit - 1);
  324. rt2800_register_write(rt2x00dev, RX_DRX_IDX, 0);
  325. /*
  326. * Enable global DMA configuration
  327. */
  328. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  329. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  330. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  331. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  332. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  333. rt2800_register_write(rt2x00dev, DELAY_INT_CFG, 0);
  334. return 0;
  335. }
  336. /*
  337. * Device state switch handlers.
  338. */
  339. static void rt2800pci_toggle_rx(struct rt2x00_dev *rt2x00dev,
  340. enum dev_state state)
  341. {
  342. u32 reg;
  343. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  344. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX,
  345. (state == STATE_RADIO_RX_ON) ||
  346. (state == STATE_RADIO_RX_ON_LINK));
  347. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  348. }
  349. static void rt2800pci_toggle_irq(struct rt2x00_dev *rt2x00dev,
  350. enum dev_state state)
  351. {
  352. int mask = (state == STATE_RADIO_IRQ_ON);
  353. u32 reg;
  354. /*
  355. * When interrupts are being enabled, the interrupt registers
  356. * should clear the register to assure a clean state.
  357. */
  358. if (state == STATE_RADIO_IRQ_ON) {
  359. rt2800_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  360. rt2800_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  361. }
  362. rt2800_register_read(rt2x00dev, INT_MASK_CSR, &reg);
  363. rt2x00_set_field32(&reg, INT_MASK_CSR_RXDELAYINT, mask);
  364. rt2x00_set_field32(&reg, INT_MASK_CSR_TXDELAYINT, mask);
  365. rt2x00_set_field32(&reg, INT_MASK_CSR_RX_DONE, mask);
  366. rt2x00_set_field32(&reg, INT_MASK_CSR_AC0_DMA_DONE, mask);
  367. rt2x00_set_field32(&reg, INT_MASK_CSR_AC1_DMA_DONE, mask);
  368. rt2x00_set_field32(&reg, INT_MASK_CSR_AC2_DMA_DONE, mask);
  369. rt2x00_set_field32(&reg, INT_MASK_CSR_AC3_DMA_DONE, mask);
  370. rt2x00_set_field32(&reg, INT_MASK_CSR_HCCA_DMA_DONE, mask);
  371. rt2x00_set_field32(&reg, INT_MASK_CSR_MGMT_DMA_DONE, mask);
  372. rt2x00_set_field32(&reg, INT_MASK_CSR_MCU_COMMAND, mask);
  373. rt2x00_set_field32(&reg, INT_MASK_CSR_RXTX_COHERENT, mask);
  374. rt2x00_set_field32(&reg, INT_MASK_CSR_TBTT, mask);
  375. rt2x00_set_field32(&reg, INT_MASK_CSR_PRE_TBTT, mask);
  376. rt2x00_set_field32(&reg, INT_MASK_CSR_TX_FIFO_STATUS, mask);
  377. rt2x00_set_field32(&reg, INT_MASK_CSR_AUTO_WAKEUP, mask);
  378. rt2x00_set_field32(&reg, INT_MASK_CSR_GPTIMER, mask);
  379. rt2x00_set_field32(&reg, INT_MASK_CSR_RX_COHERENT, mask);
  380. rt2x00_set_field32(&reg, INT_MASK_CSR_TX_COHERENT, mask);
  381. rt2800_register_write(rt2x00dev, INT_MASK_CSR, reg);
  382. }
  383. static int rt2800pci_enable_radio(struct rt2x00_dev *rt2x00dev)
  384. {
  385. u32 reg;
  386. u16 word;
  387. /*
  388. * Initialize all registers.
  389. */
  390. if (unlikely(rt2800_wait_wpdma_ready(rt2x00dev) ||
  391. rt2800pci_init_queues(rt2x00dev) ||
  392. rt2800_init_registers(rt2x00dev) ||
  393. rt2800_wait_wpdma_ready(rt2x00dev) ||
  394. rt2800_init_bbp(rt2x00dev) ||
  395. rt2800_init_rfcsr(rt2x00dev)))
  396. return -EIO;
  397. /*
  398. * Send signal to firmware during boot time.
  399. */
  400. rt2800_mcu_request(rt2x00dev, MCU_BOOT_SIGNAL, 0xff, 0, 0);
  401. /*
  402. * Enable RX.
  403. */
  404. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  405. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
  406. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 0);
  407. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  408. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  409. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 1);
  410. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 1);
  411. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_WP_DMA_BURST_SIZE, 2);
  412. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  413. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  414. rt2800_register_read(rt2x00dev, MAC_SYS_CTRL, &reg);
  415. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_TX, 1);
  416. rt2x00_set_field32(&reg, MAC_SYS_CTRL_ENABLE_RX, 1);
  417. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, reg);
  418. /*
  419. * Initialize LED control
  420. */
  421. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED1, &word);
  422. rt2800_mcu_request(rt2x00dev, MCU_LED_1, 0xff,
  423. word & 0xff, (word >> 8) & 0xff);
  424. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED2, &word);
  425. rt2800_mcu_request(rt2x00dev, MCU_LED_2, 0xff,
  426. word & 0xff, (word >> 8) & 0xff);
  427. rt2x00_eeprom_read(rt2x00dev, EEPROM_LED3, &word);
  428. rt2800_mcu_request(rt2x00dev, MCU_LED_3, 0xff,
  429. word & 0xff, (word >> 8) & 0xff);
  430. return 0;
  431. }
  432. static void rt2800pci_disable_radio(struct rt2x00_dev *rt2x00dev)
  433. {
  434. u32 reg;
  435. rt2800_register_read(rt2x00dev, WPDMA_GLO_CFG, &reg);
  436. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_TX_DMA, 0);
  437. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_DMA_BUSY, 0);
  438. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_ENABLE_RX_DMA, 0);
  439. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_RX_DMA_BUSY, 0);
  440. rt2x00_set_field32(&reg, WPDMA_GLO_CFG_TX_WRITEBACK_DONE, 1);
  441. rt2800_register_write(rt2x00dev, WPDMA_GLO_CFG, reg);
  442. rt2800_register_write(rt2x00dev, MAC_SYS_CTRL, 0);
  443. rt2800_register_write(rt2x00dev, PWR_PIN_CFG, 0);
  444. rt2800_register_write(rt2x00dev, TX_PIN_CFG, 0);
  445. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00001280);
  446. rt2800_register_read(rt2x00dev, WPDMA_RST_IDX, &reg);
  447. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX0, 1);
  448. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX1, 1);
  449. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX2, 1);
  450. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX3, 1);
  451. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX4, 1);
  452. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX5, 1);
  453. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DRX_IDX0, 1);
  454. rt2800_register_write(rt2x00dev, WPDMA_RST_IDX, reg);
  455. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e1f);
  456. rt2800_register_write(rt2x00dev, PBF_SYS_CTRL, 0x00000e00);
  457. /* Wait for DMA, ignore error */
  458. rt2800_wait_wpdma_ready(rt2x00dev);
  459. }
  460. static int rt2800pci_set_state(struct rt2x00_dev *rt2x00dev,
  461. enum dev_state state)
  462. {
  463. /*
  464. * Always put the device to sleep (even when we intend to wakeup!)
  465. * if the device is booting and wasn't asleep it will return
  466. * failure when attempting to wakeup.
  467. */
  468. rt2800_mcu_request(rt2x00dev, MCU_SLEEP, 0xff, 0, 2);
  469. if (state == STATE_AWAKE) {
  470. rt2800_mcu_request(rt2x00dev, MCU_WAKEUP, TOKEN_WAKUP, 0, 0);
  471. rt2800pci_mcu_status(rt2x00dev, TOKEN_WAKUP);
  472. }
  473. return 0;
  474. }
  475. static int rt2800pci_set_device_state(struct rt2x00_dev *rt2x00dev,
  476. enum dev_state state)
  477. {
  478. int retval = 0;
  479. switch (state) {
  480. case STATE_RADIO_ON:
  481. /*
  482. * Before the radio can be enabled, the device first has
  483. * to be woken up. After that it needs a bit of time
  484. * to be fully awake and then the radio can be enabled.
  485. */
  486. rt2800pci_set_state(rt2x00dev, STATE_AWAKE);
  487. msleep(1);
  488. retval = rt2800pci_enable_radio(rt2x00dev);
  489. break;
  490. case STATE_RADIO_OFF:
  491. /*
  492. * After the radio has been disabled, the device should
  493. * be put to sleep for powersaving.
  494. */
  495. rt2800pci_disable_radio(rt2x00dev);
  496. rt2800pci_set_state(rt2x00dev, STATE_SLEEP);
  497. break;
  498. case STATE_RADIO_RX_ON:
  499. case STATE_RADIO_RX_ON_LINK:
  500. case STATE_RADIO_RX_OFF:
  501. case STATE_RADIO_RX_OFF_LINK:
  502. rt2800pci_toggle_rx(rt2x00dev, state);
  503. break;
  504. case STATE_RADIO_IRQ_ON:
  505. case STATE_RADIO_IRQ_OFF:
  506. rt2800pci_toggle_irq(rt2x00dev, state);
  507. break;
  508. case STATE_DEEP_SLEEP:
  509. case STATE_SLEEP:
  510. case STATE_STANDBY:
  511. case STATE_AWAKE:
  512. retval = rt2800pci_set_state(rt2x00dev, state);
  513. break;
  514. default:
  515. retval = -ENOTSUPP;
  516. break;
  517. }
  518. if (unlikely(retval))
  519. ERROR(rt2x00dev, "Device failed to enter state %d (%d).\n",
  520. state, retval);
  521. return retval;
  522. }
  523. /*
  524. * TX descriptor initialization
  525. */
  526. static void rt2800pci_write_tx_desc(struct rt2x00_dev *rt2x00dev,
  527. struct sk_buff *skb,
  528. struct txentry_desc *txdesc)
  529. {
  530. struct skb_frame_desc *skbdesc = get_skb_frame_desc(skb);
  531. __le32 *txd = skbdesc->desc;
  532. __le32 *txwi = (__le32 *)(skb->data - rt2x00dev->ops->extra_tx_headroom);
  533. u32 word;
  534. /*
  535. * Initialize TX Info descriptor
  536. */
  537. rt2x00_desc_read(txwi, 0, &word);
  538. rt2x00_set_field32(&word, TXWI_W0_FRAG,
  539. test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
  540. rt2x00_set_field32(&word, TXWI_W0_MIMO_PS, 0);
  541. rt2x00_set_field32(&word, TXWI_W0_CF_ACK, 0);
  542. rt2x00_set_field32(&word, TXWI_W0_TS,
  543. test_bit(ENTRY_TXD_REQ_TIMESTAMP, &txdesc->flags));
  544. rt2x00_set_field32(&word, TXWI_W0_AMPDU,
  545. test_bit(ENTRY_TXD_HT_AMPDU, &txdesc->flags));
  546. rt2x00_set_field32(&word, TXWI_W0_MPDU_DENSITY, txdesc->mpdu_density);
  547. rt2x00_set_field32(&word, TXWI_W0_TX_OP, txdesc->ifs);
  548. rt2x00_set_field32(&word, TXWI_W0_MCS, txdesc->mcs);
  549. rt2x00_set_field32(&word, TXWI_W0_BW,
  550. test_bit(ENTRY_TXD_HT_BW_40, &txdesc->flags));
  551. rt2x00_set_field32(&word, TXWI_W0_SHORT_GI,
  552. test_bit(ENTRY_TXD_HT_SHORT_GI, &txdesc->flags));
  553. rt2x00_set_field32(&word, TXWI_W0_STBC, txdesc->stbc);
  554. rt2x00_set_field32(&word, TXWI_W0_PHYMODE, txdesc->rate_mode);
  555. rt2x00_desc_write(txwi, 0, word);
  556. rt2x00_desc_read(txwi, 1, &word);
  557. rt2x00_set_field32(&word, TXWI_W1_ACK,
  558. test_bit(ENTRY_TXD_ACK, &txdesc->flags));
  559. rt2x00_set_field32(&word, TXWI_W1_NSEQ,
  560. test_bit(ENTRY_TXD_GENERATE_SEQ, &txdesc->flags));
  561. rt2x00_set_field32(&word, TXWI_W1_BW_WIN_SIZE, txdesc->ba_size);
  562. rt2x00_set_field32(&word, TXWI_W1_WIRELESS_CLI_ID,
  563. test_bit(ENTRY_TXD_ENCRYPT, &txdesc->flags) ?
  564. txdesc->key_idx : 0xff);
  565. rt2x00_set_field32(&word, TXWI_W1_MPDU_TOTAL_BYTE_COUNT,
  566. skb->len - txdesc->l2pad);
  567. rt2x00_set_field32(&word, TXWI_W1_PACKETID,
  568. skbdesc->entry->queue->qid + 1);
  569. rt2x00_desc_write(txwi, 1, word);
  570. /*
  571. * Always write 0 to IV/EIV fields, hardware will insert the IV
  572. * from the IVEIV register when TXD_W3_WIV is set to 0.
  573. * When TXD_W3_WIV is set to 1 it will use the IV data
  574. * from the descriptor. The TXWI_W1_WIRELESS_CLI_ID indicates which
  575. * crypto entry in the registers should be used to encrypt the frame.
  576. */
  577. _rt2x00_desc_write(txwi, 2, 0 /* skbdesc->iv[0] */);
  578. _rt2x00_desc_write(txwi, 3, 0 /* skbdesc->iv[1] */);
  579. /*
  580. * The buffers pointed by SD_PTR0/SD_LEN0 and SD_PTR1/SD_LEN1
  581. * must contains a TXWI structure + 802.11 header + padding + 802.11
  582. * data. We choose to have SD_PTR0/SD_LEN0 only contains TXWI and
  583. * SD_PTR1/SD_LEN1 contains 802.11 header + padding + 802.11
  584. * data. It means that LAST_SEC0 is always 0.
  585. */
  586. /*
  587. * Initialize TX descriptor
  588. */
  589. rt2x00_desc_read(txd, 0, &word);
  590. rt2x00_set_field32(&word, TXD_W0_SD_PTR0, skbdesc->skb_dma);
  591. rt2x00_desc_write(txd, 0, word);
  592. rt2x00_desc_read(txd, 1, &word);
  593. rt2x00_set_field32(&word, TXD_W1_SD_LEN1, skb->len);
  594. rt2x00_set_field32(&word, TXD_W1_LAST_SEC1,
  595. !test_bit(ENTRY_TXD_MORE_FRAG, &txdesc->flags));
  596. rt2x00_set_field32(&word, TXD_W1_BURST,
  597. test_bit(ENTRY_TXD_BURST, &txdesc->flags));
  598. rt2x00_set_field32(&word, TXD_W1_SD_LEN0,
  599. rt2x00dev->ops->extra_tx_headroom);
  600. rt2x00_set_field32(&word, TXD_W1_LAST_SEC0, 0);
  601. rt2x00_set_field32(&word, TXD_W1_DMA_DONE, 0);
  602. rt2x00_desc_write(txd, 1, word);
  603. rt2x00_desc_read(txd, 2, &word);
  604. rt2x00_set_field32(&word, TXD_W2_SD_PTR1,
  605. skbdesc->skb_dma + rt2x00dev->ops->extra_tx_headroom);
  606. rt2x00_desc_write(txd, 2, word);
  607. rt2x00_desc_read(txd, 3, &word);
  608. rt2x00_set_field32(&word, TXD_W3_WIV,
  609. !test_bit(ENTRY_TXD_ENCRYPT_IV, &txdesc->flags));
  610. rt2x00_set_field32(&word, TXD_W3_QSEL, 2);
  611. rt2x00_desc_write(txd, 3, word);
  612. }
  613. /*
  614. * TX data initialization
  615. */
  616. static void rt2800pci_write_beacon(struct queue_entry *entry)
  617. {
  618. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  619. struct skb_frame_desc *skbdesc = get_skb_frame_desc(entry->skb);
  620. unsigned int beacon_base;
  621. u32 reg;
  622. /*
  623. * Disable beaconing while we are reloading the beacon data,
  624. * otherwise we might be sending out invalid data.
  625. */
  626. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  627. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 0);
  628. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  629. /*
  630. * Write entire beacon with descriptor to register.
  631. */
  632. beacon_base = HW_BEACON_OFFSET(entry->entry_idx);
  633. rt2800_register_multiwrite(rt2x00dev,
  634. beacon_base,
  635. skbdesc->desc, skbdesc->desc_len);
  636. rt2800_register_multiwrite(rt2x00dev,
  637. beacon_base + skbdesc->desc_len,
  638. entry->skb->data, entry->skb->len);
  639. /*
  640. * Clean up beacon skb.
  641. */
  642. dev_kfree_skb_any(entry->skb);
  643. entry->skb = NULL;
  644. }
  645. static void rt2800pci_kick_tx_queue(struct rt2x00_dev *rt2x00dev,
  646. const enum data_queue_qid queue_idx)
  647. {
  648. struct data_queue *queue;
  649. unsigned int idx, qidx = 0;
  650. u32 reg;
  651. if (queue_idx == QID_BEACON) {
  652. rt2800_register_read(rt2x00dev, BCN_TIME_CFG, &reg);
  653. if (!rt2x00_get_field32(reg, BCN_TIME_CFG_BEACON_GEN)) {
  654. rt2x00_set_field32(&reg, BCN_TIME_CFG_TSF_TICKING, 1);
  655. rt2x00_set_field32(&reg, BCN_TIME_CFG_TBTT_ENABLE, 1);
  656. rt2x00_set_field32(&reg, BCN_TIME_CFG_BEACON_GEN, 1);
  657. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, reg);
  658. }
  659. return;
  660. }
  661. if (queue_idx > QID_HCCA && queue_idx != QID_MGMT)
  662. return;
  663. queue = rt2x00queue_get_queue(rt2x00dev, queue_idx);
  664. idx = queue->index[Q_INDEX];
  665. if (queue_idx == QID_MGMT)
  666. qidx = 5;
  667. else
  668. qidx = queue_idx;
  669. rt2800_register_write(rt2x00dev, TX_CTX_IDX(qidx), idx);
  670. }
  671. static void rt2800pci_kill_tx_queue(struct rt2x00_dev *rt2x00dev,
  672. const enum data_queue_qid qid)
  673. {
  674. u32 reg;
  675. if (qid == QID_BEACON) {
  676. rt2800_register_write(rt2x00dev, BCN_TIME_CFG, 0);
  677. return;
  678. }
  679. rt2800_register_read(rt2x00dev, WPDMA_RST_IDX, &reg);
  680. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX0, (qid == QID_AC_BE));
  681. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX1, (qid == QID_AC_BK));
  682. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX2, (qid == QID_AC_VI));
  683. rt2x00_set_field32(&reg, WPDMA_RST_IDX_DTX_IDX3, (qid == QID_AC_VO));
  684. rt2800_register_write(rt2x00dev, WPDMA_RST_IDX, reg);
  685. }
  686. /*
  687. * RX control handlers
  688. */
  689. static void rt2800pci_fill_rxdone(struct queue_entry *entry,
  690. struct rxdone_entry_desc *rxdesc)
  691. {
  692. struct rt2x00_dev *rt2x00dev = entry->queue->rt2x00dev;
  693. struct queue_entry_priv_pci *entry_priv = entry->priv_data;
  694. __le32 *rxd = entry_priv->desc;
  695. __le32 *rxwi = (__le32 *)entry->skb->data;
  696. u32 rxd3;
  697. u32 rxwi0;
  698. u32 rxwi1;
  699. u32 rxwi2;
  700. u32 rxwi3;
  701. rt2x00_desc_read(rxd, 3, &rxd3);
  702. rt2x00_desc_read(rxwi, 0, &rxwi0);
  703. rt2x00_desc_read(rxwi, 1, &rxwi1);
  704. rt2x00_desc_read(rxwi, 2, &rxwi2);
  705. rt2x00_desc_read(rxwi, 3, &rxwi3);
  706. if (rt2x00_get_field32(rxd3, RXD_W3_CRC_ERROR))
  707. rxdesc->flags |= RX_FLAG_FAILED_FCS_CRC;
  708. if (test_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags)) {
  709. /*
  710. * Unfortunately we don't know the cipher type used during
  711. * decryption. This prevents us from correct providing
  712. * correct statistics through debugfs.
  713. */
  714. rxdesc->cipher = rt2x00_get_field32(rxwi0, RXWI_W0_UDF);
  715. rxdesc->cipher_status =
  716. rt2x00_get_field32(rxd3, RXD_W3_CIPHER_ERROR);
  717. }
  718. if (rt2x00_get_field32(rxd3, RXD_W3_DECRYPTED)) {
  719. /*
  720. * Hardware has stripped IV/EIV data from 802.11 frame during
  721. * decryption. Unfortunately the descriptor doesn't contain
  722. * any fields with the EIV/IV data either, so they can't
  723. * be restored by rt2x00lib.
  724. */
  725. rxdesc->flags |= RX_FLAG_IV_STRIPPED;
  726. if (rxdesc->cipher_status == RX_CRYPTO_SUCCESS)
  727. rxdesc->flags |= RX_FLAG_DECRYPTED;
  728. else if (rxdesc->cipher_status == RX_CRYPTO_FAIL_MIC)
  729. rxdesc->flags |= RX_FLAG_MMIC_ERROR;
  730. }
  731. if (rt2x00_get_field32(rxd3, RXD_W3_MY_BSS))
  732. rxdesc->dev_flags |= RXDONE_MY_BSS;
  733. if (rt2x00_get_field32(rxd3, RXD_W3_L2PAD))
  734. rxdesc->dev_flags |= RXDONE_L2PAD;
  735. if (rt2x00_get_field32(rxwi1, RXWI_W1_SHORT_GI))
  736. rxdesc->flags |= RX_FLAG_SHORT_GI;
  737. if (rt2x00_get_field32(rxwi1, RXWI_W1_BW))
  738. rxdesc->flags |= RX_FLAG_40MHZ;
  739. /*
  740. * Detect RX rate, always use MCS as signal type.
  741. */
  742. rxdesc->dev_flags |= RXDONE_SIGNAL_MCS;
  743. rxdesc->rate_mode = rt2x00_get_field32(rxwi1, RXWI_W1_PHYMODE);
  744. rxdesc->signal = rt2x00_get_field32(rxwi1, RXWI_W1_MCS);
  745. /*
  746. * Mask of 0x8 bit to remove the short preamble flag.
  747. */
  748. if (rxdesc->rate_mode == RATE_MODE_CCK)
  749. rxdesc->signal &= ~0x8;
  750. rxdesc->rssi =
  751. (rt2x00_get_field32(rxwi2, RXWI_W2_RSSI0) +
  752. rt2x00_get_field32(rxwi2, RXWI_W2_RSSI1)) / 2;
  753. rxdesc->noise =
  754. (rt2x00_get_field32(rxwi3, RXWI_W3_SNR0) +
  755. rt2x00_get_field32(rxwi3, RXWI_W3_SNR1)) / 2;
  756. rxdesc->size = rt2x00_get_field32(rxwi0, RXWI_W0_MPDU_TOTAL_BYTE_COUNT);
  757. /*
  758. * Set RX IDX in register to inform hardware that we have handled
  759. * this entry and it is available for reuse again.
  760. */
  761. rt2800_register_write(rt2x00dev, RX_CRX_IDX, entry->entry_idx);
  762. /*
  763. * Remove TXWI descriptor from start of buffer.
  764. */
  765. skb_pull(entry->skb, RXWI_DESC_SIZE);
  766. }
  767. /*
  768. * Interrupt functions.
  769. */
  770. static void rt2800pci_txdone(struct rt2x00_dev *rt2x00dev)
  771. {
  772. struct data_queue *queue;
  773. struct queue_entry *entry;
  774. __le32 *txwi;
  775. struct txdone_entry_desc txdesc;
  776. u32 word;
  777. u32 reg;
  778. u32 old_reg;
  779. int wcid, ack, pid, tx_wcid, tx_ack, tx_pid;
  780. u16 mcs, real_mcs;
  781. /*
  782. * During each loop we will compare the freshly read
  783. * TX_STA_FIFO register value with the value read from
  784. * the previous loop. If the 2 values are equal then
  785. * we should stop processing because the chance it
  786. * quite big that the device has been unplugged and
  787. * we risk going into an endless loop.
  788. */
  789. old_reg = 0;
  790. while (1) {
  791. rt2800_register_read(rt2x00dev, TX_STA_FIFO, &reg);
  792. if (!rt2x00_get_field32(reg, TX_STA_FIFO_VALID))
  793. break;
  794. if (old_reg == reg)
  795. break;
  796. old_reg = reg;
  797. wcid = rt2x00_get_field32(reg, TX_STA_FIFO_WCID);
  798. ack = rt2x00_get_field32(reg, TX_STA_FIFO_TX_ACK_REQUIRED);
  799. pid = rt2x00_get_field32(reg, TX_STA_FIFO_PID_TYPE);
  800. /*
  801. * Skip this entry when it contains an invalid
  802. * queue identication number.
  803. */
  804. if (pid <= 0 || pid > QID_RX)
  805. continue;
  806. queue = rt2x00queue_get_queue(rt2x00dev, pid - 1);
  807. if (unlikely(!queue))
  808. continue;
  809. /*
  810. * Inside each queue, we process each entry in a chronological
  811. * order. We first check that the queue is not empty.
  812. */
  813. if (rt2x00queue_empty(queue))
  814. continue;
  815. entry = rt2x00queue_get_entry(queue, Q_INDEX_DONE);
  816. /* Check if we got a match by looking at WCID/ACK/PID
  817. * fields */
  818. txwi = (__le32 *)(entry->skb->data -
  819. rt2x00dev->ops->extra_tx_headroom);
  820. rt2x00_desc_read(txwi, 1, &word);
  821. tx_wcid = rt2x00_get_field32(word, TXWI_W1_WIRELESS_CLI_ID);
  822. tx_ack = rt2x00_get_field32(word, TXWI_W1_ACK);
  823. tx_pid = rt2x00_get_field32(word, TXWI_W1_PACKETID);
  824. if ((wcid != tx_wcid) || (ack != tx_ack) || (pid != tx_pid))
  825. WARNING(rt2x00dev, "invalid TX_STA_FIFO content\n");
  826. /*
  827. * Obtain the status about this packet.
  828. */
  829. txdesc.flags = 0;
  830. rt2x00_desc_read(txwi, 0, &word);
  831. mcs = rt2x00_get_field32(word, TXWI_W0_MCS);
  832. real_mcs = rt2x00_get_field32(reg, TX_STA_FIFO_MCS);
  833. /*
  834. * Ralink has a retry mechanism using a global fallback
  835. * table. We setup this fallback table to try the immediate
  836. * lower rate for all rates. In the TX_STA_FIFO, the MCS field
  837. * always contains the MCS used for the last transmission, be
  838. * it successful or not.
  839. */
  840. if (rt2x00_get_field32(reg, TX_STA_FIFO_TX_SUCCESS)) {
  841. /*
  842. * Transmission succeeded. The number of retries is
  843. * mcs - real_mcs
  844. */
  845. __set_bit(TXDONE_SUCCESS, &txdesc.flags);
  846. txdesc.retry = ((mcs > real_mcs) ? mcs - real_mcs : 0);
  847. } else {
  848. /*
  849. * Transmission failed. The number of retries is
  850. * always 7 in this case (for a total number of 8
  851. * frames sent).
  852. */
  853. __set_bit(TXDONE_FAILURE, &txdesc.flags);
  854. txdesc.retry = 7;
  855. }
  856. __set_bit(TXDONE_FALLBACK, &txdesc.flags);
  857. rt2x00lib_txdone(entry, &txdesc);
  858. }
  859. }
  860. static irqreturn_t rt2800pci_interrupt(int irq, void *dev_instance)
  861. {
  862. struct rt2x00_dev *rt2x00dev = dev_instance;
  863. u32 reg;
  864. /* Read status and ACK all interrupts */
  865. rt2800_register_read(rt2x00dev, INT_SOURCE_CSR, &reg);
  866. rt2800_register_write(rt2x00dev, INT_SOURCE_CSR, reg);
  867. if (!reg)
  868. return IRQ_NONE;
  869. if (!test_bit(DEVICE_STATE_ENABLED_RADIO, &rt2x00dev->flags))
  870. return IRQ_HANDLED;
  871. /*
  872. * 1 - Rx ring done interrupt.
  873. */
  874. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_RX_DONE))
  875. rt2x00pci_rxdone(rt2x00dev);
  876. if (rt2x00_get_field32(reg, INT_SOURCE_CSR_TX_FIFO_STATUS))
  877. rt2800pci_txdone(rt2x00dev);
  878. return IRQ_HANDLED;
  879. }
  880. /*
  881. * Device probe functions.
  882. */
  883. static int rt2800pci_validate_eeprom(struct rt2x00_dev *rt2x00dev)
  884. {
  885. /*
  886. * Read EEPROM into buffer
  887. */
  888. if (rt2x00_is_soc(rt2x00dev))
  889. rt2800pci_read_eeprom_soc(rt2x00dev);
  890. else if (rt2800pci_efuse_detect(rt2x00dev))
  891. rt2800pci_read_eeprom_efuse(rt2x00dev);
  892. else
  893. rt2800pci_read_eeprom_pci(rt2x00dev);
  894. return rt2800_validate_eeprom(rt2x00dev);
  895. }
  896. static const struct rt2800_ops rt2800pci_rt2800_ops = {
  897. .register_read = rt2x00pci_register_read,
  898. .register_read_lock = rt2x00pci_register_read, /* same for PCI */
  899. .register_write = rt2x00pci_register_write,
  900. .register_write_lock = rt2x00pci_register_write, /* same for PCI */
  901. .register_multiread = rt2x00pci_register_multiread,
  902. .register_multiwrite = rt2x00pci_register_multiwrite,
  903. .regbusy_read = rt2x00pci_regbusy_read,
  904. };
  905. static int rt2800pci_probe_hw(struct rt2x00_dev *rt2x00dev)
  906. {
  907. int retval;
  908. rt2x00dev->priv = (void *)&rt2800pci_rt2800_ops;
  909. /*
  910. * Allocate eeprom data.
  911. */
  912. retval = rt2800pci_validate_eeprom(rt2x00dev);
  913. if (retval)
  914. return retval;
  915. retval = rt2800_init_eeprom(rt2x00dev);
  916. if (retval)
  917. return retval;
  918. /*
  919. * Initialize hw specifications.
  920. */
  921. retval = rt2800_probe_hw_mode(rt2x00dev);
  922. if (retval)
  923. return retval;
  924. /*
  925. * This device has multiple filters for control frames
  926. * and has a separate filter for PS Poll frames.
  927. */
  928. __set_bit(DRIVER_SUPPORT_CONTROL_FILTERS, &rt2x00dev->flags);
  929. __set_bit(DRIVER_SUPPORT_CONTROL_FILTER_PSPOLL, &rt2x00dev->flags);
  930. /*
  931. * This device requires firmware.
  932. */
  933. if (!rt2x00_is_soc(rt2x00dev))
  934. __set_bit(DRIVER_REQUIRE_FIRMWARE, &rt2x00dev->flags);
  935. __set_bit(DRIVER_REQUIRE_DMA, &rt2x00dev->flags);
  936. __set_bit(DRIVER_REQUIRE_L2PAD, &rt2x00dev->flags);
  937. if (!modparam_nohwcrypt)
  938. __set_bit(CONFIG_SUPPORT_HW_CRYPTO, &rt2x00dev->flags);
  939. /*
  940. * Set the rssi offset.
  941. */
  942. rt2x00dev->rssi_offset = DEFAULT_RSSI_OFFSET;
  943. return 0;
  944. }
  945. static const struct rt2x00lib_ops rt2800pci_rt2x00_ops = {
  946. .irq_handler = rt2800pci_interrupt,
  947. .probe_hw = rt2800pci_probe_hw,
  948. .get_firmware_name = rt2800pci_get_firmware_name,
  949. .check_firmware = rt2800pci_check_firmware,
  950. .load_firmware = rt2800pci_load_firmware,
  951. .initialize = rt2x00pci_initialize,
  952. .uninitialize = rt2x00pci_uninitialize,
  953. .get_entry_state = rt2800pci_get_entry_state,
  954. .clear_entry = rt2800pci_clear_entry,
  955. .set_device_state = rt2800pci_set_device_state,
  956. .rfkill_poll = rt2800_rfkill_poll,
  957. .link_stats = rt2800_link_stats,
  958. .reset_tuner = rt2800_reset_tuner,
  959. .link_tuner = rt2800_link_tuner,
  960. .write_tx_desc = rt2800pci_write_tx_desc,
  961. .write_tx_data = rt2x00pci_write_tx_data,
  962. .write_beacon = rt2800pci_write_beacon,
  963. .kick_tx_queue = rt2800pci_kick_tx_queue,
  964. .kill_tx_queue = rt2800pci_kill_tx_queue,
  965. .fill_rxdone = rt2800pci_fill_rxdone,
  966. .config_shared_key = rt2800_config_shared_key,
  967. .config_pairwise_key = rt2800_config_pairwise_key,
  968. .config_filter = rt2800_config_filter,
  969. .config_intf = rt2800_config_intf,
  970. .config_erp = rt2800_config_erp,
  971. .config_ant = rt2800_config_ant,
  972. .config = rt2800_config,
  973. };
  974. static const struct data_queue_desc rt2800pci_queue_rx = {
  975. .entry_num = RX_ENTRIES,
  976. .data_size = AGGREGATION_SIZE,
  977. .desc_size = RXD_DESC_SIZE,
  978. .priv_size = sizeof(struct queue_entry_priv_pci),
  979. };
  980. static const struct data_queue_desc rt2800pci_queue_tx = {
  981. .entry_num = TX_ENTRIES,
  982. .data_size = AGGREGATION_SIZE,
  983. .desc_size = TXD_DESC_SIZE,
  984. .priv_size = sizeof(struct queue_entry_priv_pci),
  985. };
  986. static const struct data_queue_desc rt2800pci_queue_bcn = {
  987. .entry_num = 8 * BEACON_ENTRIES,
  988. .data_size = 0, /* No DMA required for beacons */
  989. .desc_size = TXWI_DESC_SIZE,
  990. .priv_size = sizeof(struct queue_entry_priv_pci),
  991. };
  992. static const struct rt2x00_ops rt2800pci_ops = {
  993. .name = KBUILD_MODNAME,
  994. .max_sta_intf = 1,
  995. .max_ap_intf = 8,
  996. .eeprom_size = EEPROM_SIZE,
  997. .rf_size = RF_SIZE,
  998. .tx_queues = NUM_TX_QUEUES,
  999. .extra_tx_headroom = TXWI_DESC_SIZE,
  1000. .rx = &rt2800pci_queue_rx,
  1001. .tx = &rt2800pci_queue_tx,
  1002. .bcn = &rt2800pci_queue_bcn,
  1003. .lib = &rt2800pci_rt2x00_ops,
  1004. .hw = &rt2800_mac80211_ops,
  1005. #ifdef CONFIG_RT2X00_LIB_DEBUGFS
  1006. .debugfs = &rt2800_rt2x00debug,
  1007. #endif /* CONFIG_RT2X00_LIB_DEBUGFS */
  1008. };
  1009. /*
  1010. * RT2800pci module information.
  1011. */
  1012. #ifdef CONFIG_RT2800PCI_PCI
  1013. static DEFINE_PCI_DEVICE_TABLE(rt2800pci_device_table) = {
  1014. { PCI_DEVICE(0x1814, 0x0601), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1015. { PCI_DEVICE(0x1814, 0x0681), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1016. { PCI_DEVICE(0x1814, 0x0701), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1017. { PCI_DEVICE(0x1814, 0x0781), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1018. { PCI_DEVICE(0x1432, 0x7708), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1019. { PCI_DEVICE(0x1432, 0x7727), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1020. { PCI_DEVICE(0x1432, 0x7728), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1021. { PCI_DEVICE(0x1432, 0x7738), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1022. { PCI_DEVICE(0x1432, 0x7748), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1023. { PCI_DEVICE(0x1432, 0x7758), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1024. { PCI_DEVICE(0x1432, 0x7768), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1025. { PCI_DEVICE(0x1a3b, 0x1059), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1026. #ifdef CONFIG_RT2800PCI_RT30XX
  1027. { PCI_DEVICE(0x1814, 0x3090), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1028. { PCI_DEVICE(0x1814, 0x3091), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1029. { PCI_DEVICE(0x1814, 0x3092), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1030. { PCI_DEVICE(0x1462, 0x891a), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1031. #endif
  1032. #ifdef CONFIG_RT2800PCI_RT35XX
  1033. { PCI_DEVICE(0x1814, 0x3060), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1034. { PCI_DEVICE(0x1814, 0x3062), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1035. { PCI_DEVICE(0x1814, 0x3562), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1036. { PCI_DEVICE(0x1814, 0x3592), PCI_DEVICE_DATA(&rt2800pci_ops) },
  1037. #endif
  1038. { 0, }
  1039. };
  1040. #endif /* CONFIG_RT2800PCI_PCI */
  1041. MODULE_AUTHOR(DRV_PROJECT);
  1042. MODULE_VERSION(DRV_VERSION);
  1043. MODULE_DESCRIPTION("Ralink RT2800 PCI & PCMCIA Wireless LAN driver.");
  1044. MODULE_SUPPORTED_DEVICE("Ralink RT2860 PCI & PCMCIA chipset based cards");
  1045. #ifdef CONFIG_RT2800PCI_PCI
  1046. MODULE_FIRMWARE(FIRMWARE_RT2860);
  1047. MODULE_DEVICE_TABLE(pci, rt2800pci_device_table);
  1048. #endif /* CONFIG_RT2800PCI_PCI */
  1049. MODULE_LICENSE("GPL");
  1050. #ifdef CONFIG_RT2800PCI_SOC
  1051. static int rt2800soc_probe(struct platform_device *pdev)
  1052. {
  1053. return rt2x00soc_probe(pdev, &rt2800pci_ops);
  1054. }
  1055. static struct platform_driver rt2800soc_driver = {
  1056. .driver = {
  1057. .name = "rt2800_wmac",
  1058. .owner = THIS_MODULE,
  1059. .mod_name = KBUILD_MODNAME,
  1060. },
  1061. .probe = rt2800soc_probe,
  1062. .remove = __devexit_p(rt2x00soc_remove),
  1063. .suspend = rt2x00soc_suspend,
  1064. .resume = rt2x00soc_resume,
  1065. };
  1066. #endif /* CONFIG_RT2800PCI_SOC */
  1067. #ifdef CONFIG_RT2800PCI_PCI
  1068. static struct pci_driver rt2800pci_driver = {
  1069. .name = KBUILD_MODNAME,
  1070. .id_table = rt2800pci_device_table,
  1071. .probe = rt2x00pci_probe,
  1072. .remove = __devexit_p(rt2x00pci_remove),
  1073. .suspend = rt2x00pci_suspend,
  1074. .resume = rt2x00pci_resume,
  1075. };
  1076. #endif /* CONFIG_RT2800PCI_PCI */
  1077. static int __init rt2800pci_init(void)
  1078. {
  1079. int ret = 0;
  1080. #ifdef CONFIG_RT2800PCI_SOC
  1081. ret = platform_driver_register(&rt2800soc_driver);
  1082. if (ret)
  1083. return ret;
  1084. #endif
  1085. #ifdef CONFIG_RT2800PCI_PCI
  1086. ret = pci_register_driver(&rt2800pci_driver);
  1087. if (ret) {
  1088. #ifdef CONFIG_RT2800PCI_SOC
  1089. platform_driver_unregister(&rt2800soc_driver);
  1090. #endif
  1091. return ret;
  1092. }
  1093. #endif
  1094. return ret;
  1095. }
  1096. static void __exit rt2800pci_exit(void)
  1097. {
  1098. #ifdef CONFIG_RT2800PCI_PCI
  1099. pci_unregister_driver(&rt2800pci_driver);
  1100. #endif
  1101. #ifdef CONFIG_RT2800PCI_SOC
  1102. platform_driver_unregister(&rt2800soc_driver);
  1103. #endif
  1104. }
  1105. module_init(rt2800pci_init);
  1106. module_exit(rt2800pci_exit);