iwl_trans.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519
  1. /* Intel 7265D/3165 PCIe transport: see iwl_trans.h.
  2. *
  3. * Register sequences follow Linux iwlwifi (pcie/trans.c, pcie/rx.c,
  4. * pcie/tx.c) for the 7000 device family: no MSI-X, no ICT, legacy single
  5. * RX queue (256 x 4 KB RBDs), gen1 128-byte TFDs, byte-count table in
  6. * dwords. Everything is polled; CSR_INT_MASK stays zero and PCI INTx is
  7. * disabled, so the card never raises an interrupt.
  8. *
  9. * The NIC is kept awake (MAC_ACCESS_REQ held) the whole time it is up: the
  10. * firmware is configured without power save, and holding the request is
  11. * what Linux does while host commands are in flight (apmg_wake_up_wa). */
  12. #include "iwl_trans.h"
  13. #include "mm.h"
  14. #include "paging.h"
  15. #include "pci.h"
  16. #include "process.h"
  17. #include "ramfs.h"
  18. #include "serial.h"
  19. /* Per-family transport parameters (Linux: iwl_cfg_trans_params plus the
  20. * op_mode's iwl_trans_config). The 7000 family runs the MVM firmware with
  21. * DQA queues and wide command headers; the 6000 family runs the DVM
  22. * firmware with the four AC queues, the command queue on 4 and 4-byte
  23. * headers. */
  24. struct iwl_family {
  25. const char *fw_path;
  26. unsigned char dvm; /* DVM firmware (6000) vs MVM (7000/8000) */
  27. unsigned char tlv; /* TLV .ucode vs the v2 fixed header */
  28. unsigned char cmd_q, cmd_fifo, nq;
  29. unsigned char wide_hdr; /* mvm: trans->wide_cmd_header */
  30. unsigned char bc_dword; /* byte-count table in dwords */
  31. unsigned char scd_set_active; /* mvm: trans_cfg.scd_set_active */
  32. unsigned char chain_ext_wa; /* base_params->scd_chain_ext_wa */
  33. unsigned char shadow_reg; /* base_params->shadow_reg_enable */
  34. unsigned char wake_me; /* tell ME we leave (7000 family) */
  35. unsigned char secure8000; /* 8000: multi-CPU secure section load */
  36. unsigned char apmg; /* APMG clock/power block present */
  37. unsigned char nvm_ext; /* 8000 ext NVM: MAC from MAO/WFMP */
  38. unsigned char q[4], q_slots[4]; /* data/mgmt queues to allocate */
  39. };
  40. static const struct iwl_family fam_7000 = {
  41. "firmware/iwlwifi-7265D-29.ucode", 0, 1, IWL_Q_CMD, 7, 31,
  42. 1, 1, 1, 0, 1, 1, 0, 1, 0,
  43. { IWL_Q_AUX, IWL_Q_BSS, 0xff, 0xff }, { 8, 32, 0, 0 },
  44. };
  45. static const struct iwl_family fam_6000 = {
  46. "firmware/iwlwifi-6000-4.ucode", 1, 0, IWL_DVM_Q_CMD, 7, 20,
  47. 0, 0, 0, 1, 0, 0, 0, 1, 0,
  48. { IWL_DVM_Q_VO, IWL_DVM_Q_VI, IWL_DVM_Q_BE, IWL_DVM_Q_BK },
  49. { 16, 4, 32, 4 },
  50. };
  51. static const struct iwl_family fam_8000 = {
  52. "firmware/iwlwifi-8265-36.ucode", 0, 1, IWL_Q_CMD, 7, 31,
  53. 1, 1, 1, 0, 1, 0, 1, 0, 1,
  54. { IWL_Q_AUX, IWL_Q_BSS, 0xff, 0xff }, { 8, 32, 0, 0 },
  55. };
  56. /* ---- CSR / HBUS / FH / PRPH registers (iwl-csr.h, iwl-fh.h, iwl-prph.h) */
  57. #define CSR_HW_IF_CONFIG_REG 0x000
  58. #define CSR_INT_COALESCING 0x004
  59. #define CSR_INT 0x008
  60. #define CSR_INT_MASK 0x00c
  61. #define CSR_FH_INT_STATUS 0x010
  62. #define CSR_RESET 0x020
  63. #define CSR_GP_CNTRL 0x024
  64. #define CSR_HW_REV 0x028
  65. #define CSR_GIO_REG 0x03c
  66. #define CSR_UCODE_DRV_GP1_CLR 0x05c
  67. #define CSR_MBOX_SET_REG 0x088
  68. #define CSR_MAC_SHADOW_REG_CTRL 0x0a8
  69. #define CSR_GIO_CHICKEN_BITS 0x100
  70. #define CSR_DBG_HPET_MEM_REG 0x240
  71. #define CSR_DBG_LINK_PWR_MGMT_REG 0x250
  72. #define HW_IF_NIC_READY 0x00400000u
  73. #define HW_IF_PREPARE 0x08000000u
  74. #define HW_IF_HAP_WAKE_L1A 0x00080000u
  75. #define INT_FH_RX (1u << 31)
  76. #define INT_HW_ERR (1u << 29)
  77. #define INT_FH_TX (1u << 27)
  78. #define INT_SW_ERR (1u << 25)
  79. #define INT_SW_RX (1u << 3)
  80. #define INT_ALIVE (1u << 0)
  81. #define RESET_SW_RESET 0x00000080u
  82. #define RESET_MASTER_DISABLED 0x00000100u
  83. #define RESET_STOP_MASTER 0x00000200u
  84. #define GP_MAC_CLOCK_READY 0x00000001u
  85. #define GP_INIT_DONE 0x00000004u
  86. #define GP_MAC_ACCESS_REQ 0x00000008u
  87. #define GP_GOING_TO_SLEEP 0x00000010u
  88. #define GP_HW_RF_KILL_SW 0x08000000u
  89. #define UCODE_SW_BIT_RFKILL 0x00000002u
  90. #define UCODE_CMD_BLOCKED 0x00000004u
  91. #define MBOX_OS_ALIVE (1u << 5)
  92. #define GIO_L0S_DISABLED 0x00000002u
  93. #define GIO_CHICKEN_L1A_NO_L0S_RX 0x00800000u
  94. #define GIO_CHICKEN_DIS_L0S_TIMER 0x20000000u
  95. #define LINK_PWR_MGMT_DISABLED 0x80000000u
  96. #define HBUS_TARG_MEM_WADDR 0x410
  97. #define HBUS_TARG_MEM_WDAT 0x418
  98. #define HBUS_TARG_PRPH_WADDR 0x444
  99. #define HBUS_TARG_PRPH_RADDR 0x448
  100. #define HBUS_TARG_PRPH_WDAT 0x44c
  101. #define HBUS_TARG_PRPH_RDAT 0x450
  102. #define HBUS_TARG_WRPTR 0x460
  103. #define FH_KW_MEM_ADDR_REG 0x197c
  104. #define FH_RSCSR_STTS_WPTR 0x1bc0
  105. #define FH_RSCSR_RBDCB_BASE 0x1bc4
  106. #define FH_RSCSR_RBDCB_WPTR 0x1bc8
  107. #define FH_RSCSR_RDPTR 0x1bcc
  108. #define FH_RCSR_CONFIG 0x1c00
  109. #define FH_RCSR_RBDCB_WPTR 0x1c08
  110. #define FH_RCSR_FLUSH_RB_REQ 0x1c10
  111. #define FH_TFDIB_CTRL0(c) (0x1900 + 8 * (c))
  112. #define FH_TFDIB_CTRL1(c) (0x1904 + 8 * (c))
  113. #define FH_TCSR_CONFIG(c) (0x1d00 + 0x20 * (c))
  114. #define FH_TCSR_BUF_STS(c) (0x1d08 + 0x20 * (c))
  115. #define FH_SRVC_CHNL 9
  116. #define FH_SRVC_SRAM_ADDR 0x19c8
  117. #define FH_TX_CHICKEN_BITS 0x1e98
  118. #define FH_TCSR_DMA_ENABLE 0x80000000u
  119. #define FH_TCSR_CREDIT_ENABLE 0x00000008u
  120. #define FH_TCSR_HOST_ENDTFD 0x00100000u
  121. #define FH_MEM_TB_MAX_LENGTH 0x20000u
  122. #define APMG_CLK_EN_REG 0x3004
  123. #define APMG_PS_CTRL_REG 0x300c
  124. #define APMG_PCIDEV_STT_REG 0x3010
  125. #define APMG_RTC_INT_STT_REG 0x301c
  126. #define APMG_CLK_DMA_RQT 0x00000200u
  127. #define APMG_PS_PWR_SRC_MSK 0x03000000u
  128. #define APMG_PCIDEV_L1_ACT_DIS 0x00000800u
  129. #define APMG_PCIDEV_WAKE_ME 0x00004000u
  130. #define APMG_RTC_INT_RFKILL 0x10000000u
  131. #define LMPM_CHICK 0xa01ff8
  132. #define LMPM_EXTENDED_ADDR 0x1u
  133. #define FW_MEM_EXT_START 0x40000u
  134. #define FW_MEM_EXT_END 0x57fffu
  135. #define FH_UCODE_LOAD_STATUS 0x1af0u /* direct CSR */
  136. #define RELEASE_CPU_RESET 0x300cu /* PRPH */
  137. #define RELEASE_CPU_RESET_BIT (1u << 24)
  138. #define WFPM_GP2 0xa030b4u /* PRPH */
  139. #define SEC_CPU1_CPU2 0xffffccccu
  140. #define SEC_PAGING 0xaaaabbbbu
  141. #define SCD_BASE 0xa02c00
  142. #define SCD_SRAM_BASE_ADDR (SCD_BASE + 0x0)
  143. #define SCD_DRAM_BASE_ADDR (SCD_BASE + 0x8)
  144. #define SCD_TXFACT (SCD_BASE + 0x10)
  145. #define SCD_QUEUECHAIN_SEL (SCD_BASE + 0xe8)
  146. #define SCD_CHAINEXT_EN (SCD_BASE + 0x244)
  147. #define SCD_AGGR_SEL (SCD_BASE + 0x248)
  148. #define SCD_GP_CTRL (SCD_BASE + 0x1a8)
  149. #define SCD_EN_CTRL (SCD_BASE + 0x254)
  150. #define SCD_GP_AUTO_ACTIVE (1u << 18)
  151. #define SCD_GP_31_QUEUES (1u << 0)
  152. #define SCD_CONTEXT_MEM 0x600
  153. #define SCD_TRANS_TBL_MEM 0x7e0
  154. #define SCD_STTS_ACTIVE 3
  155. #define SCD_STTS_TXF 0
  156. #define SCD_STTS_WSL 4
  157. #define SCD_STTS_ACT_EN 19
  158. #define SCD_STTS_MSK 0x017f0000u
  159. static unsigned int scd_queue_rdptr(unsigned int q)
  160. {
  161. return q < 20 ? SCD_BASE + 0x68 + q * 4 : SCD_BASE + 0x2b4 + q * 4;
  162. }
  163. static unsigned int scd_queue_status(unsigned int q)
  164. {
  165. return q < 20 ? SCD_BASE + 0x10c + q * 4 : SCD_BASE + 0x334 + (q - 20) * 4;
  166. }
  167. static unsigned int fh_cbbc(unsigned int q)
  168. {
  169. if (q < 16)
  170. return 0x19d0 + 4 * q;
  171. if (q < 20)
  172. return 0x1bf0 + 4 * (q - 16);
  173. return 0x1b20 + 4 * (q - 20);
  174. }
  175. /* ---- device state ------------------------------------------------------ */
  176. #define RX_RING 256
  177. #define TFD_SIZE 128
  178. #define TFD_RING 256
  179. #define BC_ENTRIES 320 /* 256 + 64 duplicate */
  180. #define CMD_SLOTS 32
  181. #define CMD_BUF 4096 /* one page per command slot */
  182. #define DATA_SLOTS 32
  183. #define DATA_BUF 2048
  184. #define FIRST_TB 20 /* IWL_FIRST_TB_SIZE */
  185. struct txq {
  186. unsigned char *tfds; /* 256 x 128-byte TFDs */
  187. unsigned char *bufs; /* slots x bufsize */
  188. unsigned int nslots, bufsize;
  189. unsigned int wptr, rptr; /* 0..255 */
  190. int active;
  191. };
  192. struct iwl_state {
  193. volatile unsigned char *mmio;
  194. unsigned int bar, bus, dev, func, devid, hw_rev;
  195. const char *chip;
  196. unsigned int *rbd; /* RX descriptors (page) */
  197. unsigned char *rb[RX_RING]; /* RX buffers (4 KB pages) */
  198. volatile unsigned short *rb_stts;
  199. unsigned int rx_read;
  200. unsigned short *bc; /* byte-count tables, IWL_NUM_Q x 320 */
  201. unsigned char *kw; /* keep-warm page */
  202. unsigned char *fwbuf; /* 128 KB firmware chunk bounce buffer */
  203. struct txq q[IWL_NUM_Q];
  204. unsigned int scd_base;
  205. iwl_rx_handler handler;
  206. /* synchronous command bookkeeping */
  207. int waiting; /* a command is waiting for its response */
  208. unsigned short wait_seq;
  209. void *resp;
  210. unsigned int resp_max;
  211. int resp_len, resp_done;
  212. unsigned char wait_notif;
  213. int notif_seen;
  214. int fatal;
  215. struct iwl_fw_info fw;
  216. const unsigned char *fw_data;
  217. unsigned int fw_size;
  218. const struct iwl_family *fam;
  219. unsigned int sub; /* IWL_SUB_* within the family */
  220. void (*nic_config)(void); /* op-mode NIC config (DVM), may be 0 */
  221. /* v2 (pre-TLV) image: [IWL_IMG_*] instruction and data sections */
  222. const unsigned char *img_inst[2], *img_data[2];
  223. unsigned int img_inst_len[2], img_data_len[2];
  224. };
  225. static struct iwl_state *S; /* page-allocated on probe */
  226. typedef char iwl_state_fits_page[sizeof(struct iwl_state) <= 4096 ? 1 : -1];
  227. void iwl_log(const char *s)
  228. {
  229. serial_puts(SERIAL_COM1, "iwl: ");
  230. serial_puts(SERIAL_COM1, s);
  231. serial_puts(SERIAL_COM1, "\n");
  232. }
  233. void iwl_loghex(const char *s, unsigned int v)
  234. {
  235. serial_puts(SERIAL_COM1, "iwl: ");
  236. serial_puts(SERIAL_COM1, s);
  237. serial_puts(SERIAL_COM1, " ");
  238. serial_puthex(SERIAL_COM1, v);
  239. serial_puts(SERIAL_COM1, "\n");
  240. }
  241. /* Byte helpers: the kernel has no libc, and `rep` string ops keep gcc from
  242. * turning a loop back into a memcpy/memset call. */
  243. void iwl_memcpy(void *dst, const void *src, unsigned int n)
  244. {
  245. __asm__ volatile ("cld; rep movsb"
  246. : "+D"(dst), "+S"(src), "+c"(n) : : "memory");
  247. }
  248. void iwl_memzero(void *dst, unsigned int n)
  249. {
  250. __asm__ volatile ("cld; rep stosb"
  251. : "+D"(dst), "+c"(n) : "a"(0) : "memory");
  252. }
  253. /* ~1 us per port-0x80 write on every chipset; works with interrupts off
  254. * (some network syscalls run with IF=0, which freezes get_ticks_ms()). */
  255. void iwl_udelay(unsigned int us)
  256. {
  257. while (us--)
  258. port_outb(0x80, 0);
  259. }
  260. unsigned int iwl_rd(unsigned int reg)
  261. {
  262. return *(volatile unsigned int *)(S->mmio + reg);
  263. }
  264. void iwl_wr(unsigned int reg, unsigned int val)
  265. {
  266. *(volatile unsigned int *)(S->mmio + reg) = val;
  267. }
  268. static void iwl_set(unsigned int reg, unsigned int bits)
  269. {
  270. iwl_wr(reg, iwl_rd(reg) | bits);
  271. }
  272. static void iwl_clr(unsigned int reg, unsigned int bits)
  273. {
  274. iwl_wr(reg, iwl_rd(reg) & ~bits);
  275. }
  276. /* Poll until (reg & mask) == (bits & mask); timeout in microseconds. */
  277. static int iwl_poll(unsigned int reg, unsigned int bits, unsigned int mask,
  278. unsigned int us)
  279. {
  280. unsigned int t;
  281. for (t = 0; t < us; t += 10) {
  282. if ((iwl_rd(reg) & mask) == (bits & mask))
  283. return 0;
  284. iwl_udelay(10);
  285. }
  286. return -1;
  287. }
  288. unsigned int iwl_rd_prph(unsigned int addr)
  289. {
  290. iwl_wr(HBUS_TARG_PRPH_RADDR, (addr & 0x000fffffu) | (3u << 24));
  291. return iwl_rd(HBUS_TARG_PRPH_RDAT);
  292. }
  293. void iwl_wr_prph(unsigned int addr, unsigned int val)
  294. {
  295. iwl_wr(HBUS_TARG_PRPH_WADDR, (addr & 0x000fffffu) | (3u << 24));
  296. iwl_wr(HBUS_TARG_PRPH_WDAT, val);
  297. }
  298. static void prph_set(unsigned int addr, unsigned int bits)
  299. {
  300. iwl_wr_prph(addr, iwl_rd_prph(addr) | bits);
  301. }
  302. static void prph_clr(unsigned int addr, unsigned int bits)
  303. {
  304. iwl_wr_prph(addr, iwl_rd_prph(addr) & ~bits);
  305. }
  306. unsigned int iwl_mem_rd32(unsigned int addr)
  307. {
  308. iwl_wr(0x40c /* HBUS_TARG_MEM_RADDR */, addr);
  309. return iwl_rd(0x41c /* HBUS_TARG_MEM_RDAT */);
  310. }
  311. static void mem_wr32(unsigned int addr, unsigned int val)
  312. {
  313. iwl_wr(HBUS_TARG_MEM_WADDR, addr);
  314. iwl_wr(HBUS_TARG_MEM_WDAT, val);
  315. }
  316. static void mem_zero(unsigned int addr, unsigned int dwords)
  317. {
  318. iwl_wr(HBUS_TARG_MEM_WADDR, addr);
  319. while (dwords--)
  320. iwl_wr(HBUS_TARG_MEM_WDAT, 0);
  321. }
  322. /* Wake the MAC and keep it awake (MAC_ACCESS_REQ stays set). */
  323. static int grab_nic(void)
  324. {
  325. iwl_set(CSR_GP_CNTRL, GP_MAC_ACCESS_REQ);
  326. if (iwl_poll(CSR_GP_CNTRL, GP_MAC_CLOCK_READY,
  327. GP_MAC_CLOCK_READY | GP_GOING_TO_SLEEP, 15000) < 0) {
  328. iwl_loghex("nic access timeout, GP_CNTRL", iwl_rd(CSR_GP_CNTRL));
  329. return -1;
  330. }
  331. return 0;
  332. }
  333. /* ---- probe --------------------------------------------------------------- */
  334. static const char *chip_for(unsigned int devid,
  335. const struct iwl_family **fam, unsigned int *sub)
  336. {
  337. *fam = &fam_7000;
  338. *sub = IWL_SUB_7265;
  339. switch (devid) {
  340. case 0x095a: case 0x095b: return "Intel Wireless 7265";
  341. case 0x3165: case 0x3166: return "Intel Wireless 3165";
  342. case 0x24fd:
  343. *fam = &fam_8000;
  344. *sub = IWL_SUB_8265;
  345. return "Intel Wireless 8265";
  346. }
  347. *fam = &fam_6000;
  348. switch (devid) {
  349. case 0x4239: case 0x422c:
  350. *sub = IWL_SUB_6000I;
  351. return "Intel Centrino Advanced-N 6200";
  352. case 0x4238: case 0x422b:
  353. *sub = IWL_SUB_6000;
  354. return "Intel Centrino Ultimate-N 6300";
  355. }
  356. return 0;
  357. }
  358. /* The firmware reported SW_ERR/HW_ERR: it is dead until reloaded. */
  359. int iwl_fatal(void)
  360. {
  361. return S && S->fatal;
  362. }
  363. int iwl_is_dvm(void)
  364. {
  365. return S && S->fam->dvm;
  366. }
  367. unsigned int iwl_subfamily(void)
  368. {
  369. return S ? S->sub : 0;
  370. }
  371. void iwl_set_nic_config(void (*fn)(void))
  372. {
  373. if (S)
  374. S->nic_config = fn;
  375. }
  376. int iwl_present(void)
  377. {
  378. return S != 0;
  379. }
  380. const char *iwl_chip_name(void)
  381. {
  382. return S ? S->chip : "none";
  383. }
  384. unsigned int iwl_hw_rev(void)
  385. {
  386. return S ? S->hw_rev : 0;
  387. }
  388. const struct iwl_fw_info *iwl_fw(void)
  389. {
  390. return S ? &S->fw : 0;
  391. }
  392. const unsigned char *iwl_fw_data(unsigned int *size)
  393. {
  394. if (!S)
  395. return 0;
  396. *size = S->fw_size;
  397. return S->fw_data;
  398. }
  399. /* Put the function in D0 (PCI PM capability), as pci_enable_device()
  400. * does; firmware can leave an unused WiFi card in D3hot. */
  401. static void pci_d0(unsigned int bus, unsigned int dev, unsigned int func)
  402. {
  403. unsigned int cap, n;
  404. if (!((pci_cfg_read32(bus, dev, func, 0x04) >> 16) & 0x10))
  405. return;
  406. cap = pci_cfg_read32(bus, dev, func, 0x34) & 0xfcu;
  407. for (n = 0; cap && n < 48; n++) {
  408. unsigned int hdr = pci_cfg_read32(bus, dev, func, cap);
  409. if ((hdr & 0xff) == 0x01) {
  410. unsigned int pmcsr = pci_cfg_read32(bus, dev, func, cap + 4);
  411. if (pmcsr & 3u) {
  412. pci_cfg_write32(bus, dev, func, cap + 4,
  413. pmcsr & ~3u);
  414. iwl_udelay(10000);
  415. iwl_log("PCI D3 -> D0");
  416. }
  417. return;
  418. }
  419. cap = (hdr >> 8) & 0xfcu;
  420. }
  421. }
  422. int iwl_probe(void)
  423. {
  424. unsigned int bus, dev, func;
  425. if (S)
  426. return 0;
  427. for (bus = 0; bus < 256; bus++) {
  428. for (dev = 0; dev < 32; dev++) {
  429. unsigned int id = pci_cfg_read32(bus, dev, 0, 0);
  430. unsigned int nfunc;
  431. if (id == 0xffffffffu || id == 0)
  432. continue;
  433. nfunc = (pci_cfg_read32(bus, dev, 0, 0x0c) & 0x800000u) ? 8 : 1;
  434. for (func = 0; func < nfunc; func++) {
  435. unsigned int idf = pci_cfg_read32(bus, dev, func, 0);
  436. unsigned int bar, cmd, page, sub;
  437. const struct iwl_family *fam;
  438. const char *name;
  439. if ((idf & 0xffffu) != 0x8086)
  440. continue;
  441. name = chip_for(idf >> 16, &fam, &sub);
  442. if (!name)
  443. continue;
  444. bar = pci_cfg_read32(bus, dev, func, 0x10);
  445. if (bar & 1u)
  446. continue;
  447. bar &= 0xfffffff0u;
  448. if (!bar) {
  449. iwl_log("BAR0 unassigned");
  450. continue;
  451. }
  452. page = mm_alloc_page();
  453. if (!page)
  454. return -1;
  455. S = (struct iwl_state *)page;
  456. iwl_memzero(S, 4096);
  457. S->bar = bar;
  458. S->bus = bus;
  459. S->dev = dev;
  460. S->func = func;
  461. S->devid = idf >> 16;
  462. S->chip = name;
  463. S->fam = fam;
  464. S->sub = sub;
  465. pci_d0(bus, dev, func);
  466. /* Memory + bus master, INTx off (polled). */
  467. cmd = pci_cfg_read32(bus, dev, func, 0x04);
  468. pci_cfg_write32(bus, dev, func, 0x04,
  469. (cmd & 0xffffu) | 0x0006u | 0x0400u);
  470. /* PCI_CFG_RETRY_TIMEOUT (0x41) = 0, like Linux. */
  471. pci_cfg_write8(bus, dev, func, 0x41, 0);
  472. /* The 8 KB CSR window (the ioremap window is
  473. * shared by every address space). */
  474. S->mmio = ioremap(bar, 0x2000, IOREMAP_MMIO);
  475. if (!S->mmio)
  476. return -1;
  477. iwl_wr(CSR_INT_MASK, 0);
  478. S->hw_rev = iwl_rd(CSR_HW_REV);
  479. serial_puts(SERIAL_COM1, "iwl: ");
  480. serial_puts(SERIAL_COM1, name);
  481. serial_puts(SERIAL_COM1, " dev ");
  482. serial_puthex(SERIAL_COM1, S->devid);
  483. serial_puts(SERIAL_COM1, " hw_rev ");
  484. serial_puthex(SERIAL_COM1, S->hw_rev);
  485. serial_puts(SERIAL_COM1, " bar ");
  486. serial_puthex(SERIAL_COM1, bar);
  487. serial_puts(SERIAL_COM1, "\n");
  488. if (S->hw_rev == 0xffffffffu) {
  489. iwl_log("HW_REV reads ~0: PCI issue");
  490. S = 0;
  491. return -1;
  492. }
  493. /* 0x095a/b: only rev-D silicon runs the 7265D
  494. * firmware (Linux: CSR_HW_REV_TYPE_7265D). */
  495. if (!fam->dvm && !fam->secure8000 &&
  496. S->devid != 0x3165 && S->devid != 0x3166 &&
  497. (S->hw_rev & 0xfff0u) != 0x210u)
  498. iwl_log("warning: not a 7265D; the 7265D firmware may fail");
  499. return 0;
  500. }
  501. }
  502. }
  503. return -1;
  504. }
  505. /* ---- firmware file ------------------------------------------------------- */
  506. #define TLV_INST 1
  507. #define TLV_FLAGS 18
  508. #define TLV_SEC_RT 19
  509. #define TLV_SEC_INIT 20
  510. #define TLV_DEF_CALIB 22
  511. #define TLV_PHY_SKU 23
  512. #define TLV_API_CHANGES 29
  513. #define TLV_ENABLED_CAPA 30
  514. static unsigned int le32(const unsigned char *p)
  515. {
  516. return p[0] | (p[1] << 8) | (p[2] << 16) | ((unsigned int)p[3] << 24);
  517. }
  518. /* Pre-TLV image (iwl-drv.c iwl_parse_v1_v2_firmware(), API >= 3 = v2):
  519. * ver, build, inst, data, init, init_data, boot sizes, then the sections
  520. * in that order. */
  521. static int fw_parse_v2(const unsigned char *d, unsigned int size)
  522. {
  523. unsigned int inst, data, init, initd, boot, o = 28;
  524. if (size < 28)
  525. return -1;
  526. S->fw.ver = le32(d);
  527. inst = le32(d + 8);
  528. data = le32(d + 12);
  529. init = le32(d + 16);
  530. initd = le32(d + 20);
  531. boot = le32(d + 24);
  532. if (((S->fw.ver >> 8) & 0xff) < 3 ||
  533. o + inst + data + init + initd + boot != size || !inst || !init) {
  534. iwl_log("firmware is not a v2 .ucode image");
  535. return -1;
  536. }
  537. S->img_inst[IWL_IMG_RT] = d + o;
  538. S->img_inst_len[IWL_IMG_RT] = inst;
  539. o += inst;
  540. S->img_data[IWL_IMG_RT] = d + o;
  541. S->img_data_len[IWL_IMG_RT] = data;
  542. o += data;
  543. S->img_inst[IWL_IMG_INIT] = d + o;
  544. S->img_inst_len[IWL_IMG_INIT] = init;
  545. o += init;
  546. S->img_data[IWL_IMG_INIT] = d + o;
  547. S->img_data_len[IWL_IMG_INIT] = initd;
  548. {
  549. static const char hx[] = "0123456789abcdef";
  550. unsigned int i, v = S->fw.ver;
  551. char *h = S->fw.human;
  552. /* "6000 v9.221.4.1", like Linux's "%u.%u.%u.%u" */
  553. h[0] = 'v';
  554. for (i = 0; i < 4; i++) {
  555. unsigned int b = (v >> (24 - 8 * i)) & 0xff;
  556. *++h = hx[b >> 4 & 0xf];
  557. *++h = hx[b & 0xf];
  558. if (i < 3)
  559. *++h = '.';
  560. }
  561. *++h = 0;
  562. }
  563. return 0;
  564. }
  565. int iwl_fw_parse(void)
  566. {
  567. const unsigned char *d;
  568. unsigned int size, off, i;
  569. if (!S)
  570. return -1;
  571. if (S->fw.present)
  572. return 0;
  573. d = ramfs_lookup(S->fam->fw_path, &size);
  574. if (!d) {
  575. iwl_log("firmware missing from /ramfs:");
  576. iwl_log(S->fam->fw_path);
  577. return -1;
  578. }
  579. if (!S->fam->tlv) {
  580. if (fw_parse_v2(d, size) < 0)
  581. return -1;
  582. S->fw_data = d;
  583. S->fw_size = size;
  584. S->fw.present = 1;
  585. iwl_log(S->fw.human);
  586. return 0;
  587. }
  588. if (size < 88 || le32(d) != 0 || le32(d + 4) != 0x0a4c5749u) {
  589. iwl_log("firmware is not a TLV image");
  590. return -1;
  591. }
  592. S->fw_data = d;
  593. S->fw_size = size;
  594. for (i = 0; i < sizeof(S->fw.human) - 1 && d[8 + i]; i++)
  595. S->fw.human[i] = (char)d[8 + i];
  596. S->fw.ver = le32(d + 72);
  597. off = 88;
  598. while (off + 8 <= size) {
  599. unsigned int t = le32(d + off), l = le32(d + off + 4);
  600. const unsigned char *p = d + off + 8;
  601. if (off + 8 + l > size)
  602. break;
  603. if (t == TLV_API_CHANGES && l >= 8 && le32(p) < 4)
  604. S->fw.api[le32(p)] = le32(p + 4);
  605. else if (t == TLV_ENABLED_CAPA && l >= 8 && le32(p) < 4)
  606. S->fw.capa[le32(p)] = le32(p + 4);
  607. else if (t == TLV_PHY_SKU && l >= 4)
  608. S->fw.phy_sku = le32(p);
  609. else if (t == 31 && l >= 4) /* N_SCAN_CHANNELS */
  610. S->fw.n_scan_ch = le32(p);
  611. else if (t == TLV_DEF_CALIB && l >= 12) {
  612. unsigned int type = le32(p);
  613. /* enum iwl_ucode_type: 0 REGULAR, 1 INIT */
  614. if (type < 2) {
  615. S->fw.flow_trigger[type] = le32(p + 4);
  616. S->fw.event_trigger[type] = le32(p + 8);
  617. }
  618. }
  619. off += 8 + ((l + 3) & ~3u);
  620. }
  621. S->fw.present = 1;
  622. iwl_log(S->fw.human);
  623. return 0;
  624. }
  625. /* 8000 paging (Linux iwl_fill_paging_mem()): in the image's section list
  626. * the PAGING separator is followed by the CPU2 paging CSS block and then
  627. * the paging image. Returns 0 and both sections when the image has them. */
  628. int iwl_fw_paging(int img, const unsigned char **css, unsigned int *css_len,
  629. const unsigned char **pg, unsigned int *pg_len)
  630. {
  631. unsigned int want = img == IWL_IMG_INIT ? TLV_SEC_INIT : TLV_SEC_RT;
  632. const unsigned char *d = S->fw_data;
  633. unsigned int off = 88, seen = 0;
  634. while (S->fam->secure8000 && off + 8 <= S->fw_size) {
  635. unsigned int t = le32(d + off), l = le32(d + off + 4);
  636. const unsigned char *p = d + off + 8;
  637. if (t == want && l > 4) {
  638. if (seen == 1) {
  639. *css = p + 4;
  640. *css_len = l - 4;
  641. } else if (seen == 2) {
  642. *pg = p + 4;
  643. *pg_len = l - 4;
  644. return 0;
  645. }
  646. if (seen || le32(p) == SEC_PAGING)
  647. seen++;
  648. }
  649. off += 8 + ((l + 3) & ~3u);
  650. }
  651. return -1;
  652. }
  653. /* ---- hardware bring-up ------------------------------------------------- */
  654. int iwl_rfkill(void)
  655. {
  656. return S && !(iwl_rd(CSR_GP_CNTRL) & GP_HW_RF_KILL_SW);
  657. }
  658. static int set_hw_ready(void)
  659. {
  660. iwl_set(CSR_HW_IF_CONFIG_REG, HW_IF_NIC_READY);
  661. if (iwl_poll(CSR_HW_IF_CONFIG_REG, HW_IF_NIC_READY, HW_IF_NIC_READY, 50) < 0)
  662. return -1;
  663. iwl_set(CSR_MBOX_SET_REG, MBOX_OS_ALIVE);
  664. return 0;
  665. }
  666. static int prepare_card(void)
  667. {
  668. int iter, t;
  669. if (set_hw_ready() == 0)
  670. return 0;
  671. iwl_set(CSR_DBG_LINK_PWR_MGMT_REG, LINK_PWR_MGMT_DISABLED);
  672. iwl_udelay(1000);
  673. for (iter = 0; iter < 10; iter++) {
  674. iwl_set(CSR_HW_IF_CONFIG_REG, HW_IF_PREPARE);
  675. for (t = 0; t < 150000; t += 200) {
  676. if (set_hw_ready() == 0)
  677. return 0;
  678. iwl_udelay(200);
  679. }
  680. iwl_udelay(25000);
  681. }
  682. iwl_log("card not ready (owned by ME/AMT?)");
  683. return -1;
  684. }
  685. static int apm_init(void)
  686. {
  687. /* Linux sets the L0S-exit-timer workaround only for families below
  688. * 8000; the 8265's CSR does not use it. */
  689. if (!S->fam->secure8000)
  690. iwl_set(CSR_GIO_CHICKEN_BITS, GIO_CHICKEN_DIS_L0S_TIMER);
  691. iwl_set(CSR_GIO_CHICKEN_BITS, GIO_CHICKEN_L1A_NO_L0S_RX);
  692. iwl_set(CSR_DBG_HPET_MEM_REG, 0xffff0000u);
  693. iwl_set(CSR_HW_IF_CONFIG_REG, HW_IF_HAP_WAKE_L1A);
  694. iwl_set(CSR_GIO_REG, GIO_L0S_DISABLED);
  695. /* iwl_finish_nic_init(): INIT_DONE, then wait for the MAC clock. */
  696. iwl_set(CSR_GP_CNTRL, GP_INIT_DONE);
  697. if (iwl_poll(CSR_GP_CNTRL, GP_MAC_CLOCK_READY, GP_MAC_CLOCK_READY,
  698. 25000) < 0) {
  699. iwl_log("MAC clock not ready");
  700. return -1;
  701. }
  702. if (grab_nic() < 0)
  703. return -1;
  704. if (S->fam->apmg) {
  705. iwl_wr_prph(APMG_CLK_EN_REG, APMG_CLK_DMA_RQT);
  706. iwl_udelay(20);
  707. prph_set(APMG_PCIDEV_STT_REG, APMG_PCIDEV_L1_ACT_DIS);
  708. iwl_wr_prph(APMG_RTC_INT_STT_REG, APMG_RTC_INT_RFKILL);
  709. }
  710. return 0;
  711. }
  712. static void stop_master(void)
  713. {
  714. iwl_set(CSR_RESET, RESET_STOP_MASTER);
  715. if (iwl_poll(CSR_RESET, RESET_MASTER_DISABLED, RESET_MASTER_DISABLED,
  716. 100) < 0)
  717. iwl_log("master disable timed out");
  718. }
  719. int iwl_start_hw(void)
  720. {
  721. if (!S)
  722. return -1;
  723. if (prepare_card() < 0)
  724. return -1;
  725. iwl_set(CSR_RESET, RESET_SW_RESET);
  726. iwl_udelay(6000);
  727. if (prepare_card() < 0)
  728. return -1;
  729. if (apm_init() < 0)
  730. return -1;
  731. iwl_wr(CSR_INT, 0xffffffffu);
  732. iwl_wr(CSR_FH_INT_STATUS, 0xffffffffu);
  733. return 0;
  734. }
  735. static void rx_stop(void)
  736. {
  737. iwl_wr(FH_RCSR_CONFIG, 0);
  738. iwl_poll(0x1c44 /* FH_MEM_RSSR_RX_STATUS_REG */, 0x01000000u,
  739. 0x01000000u, 1000);
  740. }
  741. void iwl_stop_device(void)
  742. {
  743. unsigned int c;
  744. if (!S || !S->mmio)
  745. return;
  746. iwl_wr(CSR_INT_MASK, 0);
  747. if (iwl_rd(CSR_GP_CNTRL) & GP_MAC_CLOCK_READY) {
  748. /* Stop the scheduler, every TX DMA channel and RX DMA. */
  749. iwl_wr_prph(SCD_TXFACT, 0);
  750. for (c = 0; c < 8; c++)
  751. iwl_wr(FH_TCSR_CONFIG(c), 0);
  752. rx_stop();
  753. if (S->fam->apmg) {
  754. iwl_wr_prph(APMG_CLK_EN_REG, APMG_CLK_DMA_RQT);
  755. /* Inform ME that we are leaving (7000 family). */
  756. if (S->fam->wake_me) {
  757. prph_set(APMG_PCIDEV_STT_REG,
  758. APMG_PCIDEV_WAKE_ME);
  759. iwl_udelay(5000);
  760. }
  761. }
  762. }
  763. stop_master();
  764. iwl_set(CSR_RESET, RESET_SW_RESET);
  765. iwl_udelay(6000);
  766. iwl_clr(CSR_GP_CNTRL, GP_INIT_DONE | GP_MAC_ACCESS_REQ);
  767. iwl_wr(CSR_INT, 0xffffffffu);
  768. iwl_wr(CSR_FH_INT_STATUS, 0xffffffffu);
  769. for (c = 0; c < IWL_NUM_Q; c++)
  770. S->q[c].active = 0;
  771. }
  772. /* ---- ring allocation ---------------------------------------------------- */
  773. static void *alloc_zero(unsigned int pages)
  774. {
  775. unsigned int p = mm_alloc_pages(pages);
  776. if (p)
  777. iwl_memzero((void *)p, pages * 4096);
  778. return (void *)p;
  779. }
  780. static int alloc_txq(int qn, unsigned int slots, unsigned int bufsize)
  781. {
  782. struct txq *q = &S->q[qn];
  783. if (!q->tfds) {
  784. q->tfds = alloc_zero(TFD_RING * TFD_SIZE / 4096);
  785. q->bufs = alloc_zero((slots * bufsize + 4095) / 4096);
  786. if (!q->tfds || !q->bufs)
  787. return -1;
  788. q->nslots = slots;
  789. q->bufsize = bufsize;
  790. }
  791. iwl_memzero(q->tfds, TFD_RING * TFD_SIZE);
  792. q->wptr = q->rptr = 0;
  793. q->active = 0;
  794. return 0;
  795. }
  796. static int alloc_rings(void)
  797. {
  798. unsigned int i;
  799. if (!S->rbd) {
  800. S->rbd = alloc_zero(1);
  801. S->rb_stts = alloc_zero(1);
  802. S->bc = alloc_zero((IWL_NUM_Q * BC_ENTRIES * 2 + 4095) / 4096);
  803. S->kw = alloc_zero(1);
  804. S->fwbuf = alloc_zero(FH_MEM_TB_MAX_LENGTH / 4096);
  805. if (!S->rbd || !S->rb_stts || !S->bc || !S->kw || !S->fwbuf)
  806. return -1;
  807. for (i = 0; i < RX_RING; i++) {
  808. S->rb[i] = (unsigned char *)mm_alloc_page();
  809. if (!S->rb[i])
  810. return -1;
  811. }
  812. }
  813. if (alloc_txq(S->fam->cmd_q, CMD_SLOTS, CMD_BUF) < 0)
  814. return -1;
  815. for (i = 0; i < 4; i++)
  816. if (S->fam->q[i] != 0xff &&
  817. alloc_txq(S->fam->q[i], S->fam->q_slots[i], DATA_BUF) < 0)
  818. return -1;
  819. return 0;
  820. }
  821. static void rx_init(void)
  822. {
  823. unsigned int i;
  824. for (i = 0; i < RX_RING; i++) {
  825. *(volatile unsigned int *)S->rb[i] = 0x55550000u; /* FRAME_INVALID */
  826. S->rbd[i] = (unsigned int)S->rb[i] >> 8;
  827. }
  828. iwl_memzero((void *)S->rb_stts, 16);
  829. S->rx_read = 0;
  830. iwl_wr(FH_RCSR_CONFIG, 0);
  831. iwl_wr(FH_RCSR_RBDCB_WPTR, 0);
  832. iwl_wr(FH_RCSR_FLUSH_RB_REQ, 0);
  833. iwl_wr(FH_RSCSR_RDPTR, 0);
  834. iwl_wr(FH_RSCSR_RBDCB_WPTR, 0);
  835. iwl_wr(FH_RSCSR_RBDCB_BASE, (unsigned int)S->rbd >> 8);
  836. iwl_wr(FH_RSCSR_STTS_WPTR, (unsigned int)S->rb_stts >> 4);
  837. /* enable | ignore RXF empty | irq to host | 4K RBs | RB timeout 0x11 |
  838. * 256 RBDs (log 8). */
  839. iwl_wr(FH_RCSR_CONFIG, 0x80000000u | 0x4u | 0x1000u | 0 |
  840. (0x11u << 4) | (8u << 20));
  841. *(volatile unsigned char *)(S->mmio + CSR_INT_COALESCING) = 0x40;
  842. /* Hand every buffer but the last 8 to the hardware (write pointer
  843. * moves in multiples of 8). */
  844. iwl_wr(FH_RSCSR_RBDCB_WPTR, (RX_RING - 8) & ~7u);
  845. }
  846. static void tx_init(void)
  847. {
  848. unsigned int q;
  849. iwl_wr_prph(SCD_TXFACT, 0);
  850. iwl_wr(FH_KW_MEM_ADDR_REG, (unsigned int)S->kw >> 4);
  851. for (q = 0; q < IWL_NUM_Q; q++)
  852. if (S->q[q].tfds)
  853. iwl_wr(fh_cbbc(q), (unsigned int)S->q[q].tfds >> 8);
  854. prph_set(SCD_GP_CTRL, SCD_GP_AUTO_ACTIVE |
  855. (S->fam->nq > 20 ? SCD_GP_31_QUEUES : 0));
  856. }
  857. /* ---- firmware load ------------------------------------------------------ */
  858. static int load_chunk(unsigned int dst, const unsigned char *src,
  859. unsigned int n)
  860. {
  861. int ext = dst >= FW_MEM_EXT_START && dst <= FW_MEM_EXT_END;
  862. unsigned int t;
  863. iwl_memcpy(S->fwbuf, src, n);
  864. if (ext)
  865. prph_set(LMPM_CHICK, LMPM_EXTENDED_ADDR);
  866. iwl_wr(CSR_INT, INT_FH_TX);
  867. iwl_wr(CSR_FH_INT_STATUS, 0xffffffffu);
  868. iwl_wr(FH_TCSR_CONFIG(FH_SRVC_CHNL), 0);
  869. iwl_wr(FH_SRVC_SRAM_ADDR, dst);
  870. iwl_wr(FH_TFDIB_CTRL0(FH_SRVC_CHNL), (unsigned int)S->fwbuf);
  871. iwl_wr(FH_TFDIB_CTRL1(FH_SRVC_CHNL), n);
  872. iwl_wr(FH_TCSR_BUF_STS(FH_SRVC_CHNL), (1u << 20) | (1u << 12) | 3u);
  873. iwl_wr(FH_TCSR_CONFIG(FH_SRVC_CHNL),
  874. FH_TCSR_DMA_ENABLE | FH_TCSR_HOST_ENDTFD);
  875. for (t = 0; t < 5000000; t += 10) {
  876. if (iwl_rd(CSR_INT) & INT_FH_TX)
  877. break;
  878. iwl_udelay(10);
  879. }
  880. iwl_wr(CSR_INT, INT_FH_TX);
  881. iwl_wr(CSR_FH_INT_STATUS, 0xffffffffu);
  882. if (ext)
  883. prph_clr(LMPM_CHICK, LMPM_EXTENDED_ADDR);
  884. if (t >= 5000000) {
  885. iwl_loghex("firmware chunk timeout at", dst);
  886. return -1;
  887. }
  888. return 0;
  889. }
  890. static int load_section(unsigned int dst, const unsigned char *p,
  891. unsigned int len)
  892. {
  893. unsigned int o;
  894. for (o = 0; o < len; o += FH_MEM_TB_MAX_LENGTH) {
  895. unsigned int n = len - o;
  896. if (n > FH_MEM_TB_MAX_LENGTH)
  897. n = FH_MEM_TB_MAX_LENGTH;
  898. if (load_chunk(dst + o, p + o, n) < 0)
  899. return -1;
  900. }
  901. return 0;
  902. }
  903. /* 8000-family firmware: the TLV section list is split into CPU1 and CPU2
  904. * lists by SEPARATOR_SECTION entries, each loaded section must be
  905. * acknowledged in FH_UCODE_LOAD_STATUS, and the CPU reset is released
  906. * through RELEASE_CPU_RESET (Linux iwl_pcie_load_given_ucode_8000()). */
  907. #define SEC_MAX 24
  908. struct fw_sec {
  909. unsigned int off, len;
  910. const unsigned char *data;
  911. };
  912. static int load_sections_8000(int img)
  913. {
  914. unsigned int want = img == IWL_IMG_INIT ? TLV_SEC_INIT : TLV_SEC_RT;
  915. const unsigned char *d = S->fw_data;
  916. unsigned int off = 88, n = 0, i, val, sec_num;
  917. struct fw_sec sec[SEC_MAX];
  918. while (off + 8 <= S->fw_size && n < SEC_MAX) {
  919. unsigned int t = le32(d + off), l = le32(d + off + 4);
  920. const unsigned char *p = d + off + 8;
  921. if (t == want && l > 4) {
  922. sec[n].off = le32(p);
  923. sec[n].len = l - 4;
  924. sec[n].data = p + 4;
  925. n++;
  926. }
  927. off += 8 + ((l + 3) & ~3u);
  928. }
  929. if (!n)
  930. return -1;
  931. iwl_wr_prph(WFPM_GP2, 0x01010101);
  932. prph_set(RELEASE_CPU_RESET, RELEASE_CPU_RESET_BIT);
  933. sec_num = 1;
  934. for (i = 0; i < n; i++) {
  935. if (sec[i].off == SEC_CPU1_CPU2 || sec[i].off == SEC_PAGING)
  936. break;
  937. if (load_section(sec[i].off, sec[i].data, sec[i].len) < 0)
  938. return -1;
  939. val = iwl_rd(FH_UCODE_LOAD_STATUS);
  940. iwl_wr(FH_UCODE_LOAD_STATUS, val | sec_num);
  941. sec_num = (sec_num << 1) | 1;
  942. }
  943. iwl_wr(FH_UCODE_LOAD_STATUS, 0xffffu);
  944. if (i < n && sec[i].off == SEC_CPU1_CPU2) {
  945. sec_num = 1;
  946. for (i = i + 1; i < n; i++) {
  947. if (sec[i].off == SEC_PAGING ||
  948. sec[i].off == SEC_CPU1_CPU2)
  949. break;
  950. if (load_section(sec[i].off, sec[i].data, sec[i].len) < 0)
  951. return -1;
  952. val = iwl_rd(FH_UCODE_LOAD_STATUS);
  953. iwl_wr(FH_UCODE_LOAD_STATUS,
  954. val | (sec_num << 16));
  955. sec_num = (sec_num << 1) | 1;
  956. }
  957. }
  958. iwl_wr(FH_UCODE_LOAD_STATUS, 0xffffffffu);
  959. return 0;
  960. }
  961. static int load_sections(int img)
  962. {
  963. unsigned int want = img == IWL_IMG_INIT ? TLV_SEC_INIT : TLV_SEC_RT;
  964. const unsigned char *d = S->fw_data;
  965. unsigned int off = 88, nsec = 0;
  966. if (S->fam->secure8000)
  967. return load_sections_8000(img);
  968. if (!S->fam->tlv) {
  969. /* IWLAGN_RTC_INST_LOWER_BOUND / IWLAGN_RTC_DATA_LOWER_BOUND */
  970. if (load_section(0x000000, S->img_inst[img],
  971. S->img_inst_len[img]) < 0 ||
  972. load_section(0x800000, S->img_data[img],
  973. S->img_data_len[img]) < 0)
  974. return -1;
  975. return 0;
  976. }
  977. while (off + 8 <= S->fw_size) {
  978. unsigned int t = le32(d + off), l = le32(d + off + 4);
  979. const unsigned char *p = d + off + 8;
  980. if (t == want && l > 4) {
  981. unsigned int dst = le32(p), len = l - 4, o;
  982. /* CPU1_CPU2 / paging separators end the CPU1 list
  983. * (single-CPU image: nothing follows that we load). */
  984. if (dst == 0xffffccccu || dst == 0xaaaabbbbu)
  985. break;
  986. for (o = 0; o < len; o += FH_MEM_TB_MAX_LENGTH) {
  987. unsigned int n = len - o;
  988. if (n > FH_MEM_TB_MAX_LENGTH)
  989. n = FH_MEM_TB_MAX_LENGTH;
  990. if (load_chunk(dst + o, p + 4 + o, n) < 0)
  991. return -1;
  992. }
  993. nsec++;
  994. }
  995. off += 8 + ((l + 3) & ~3u);
  996. }
  997. return nsec ? 0 : -1;
  998. }
  999. int iwl_load_fw(int img)
  1000. {
  1001. if (!S || !S->fw.present)
  1002. return -1;
  1003. if (prepare_card() < 0)
  1004. return -1;
  1005. iwl_wr(CSR_INT, 0xffffffffu);
  1006. iwl_wr(CSR_UCODE_DRV_GP1_CLR, UCODE_SW_BIT_RFKILL);
  1007. iwl_wr(CSR_UCODE_DRV_GP1_CLR, UCODE_CMD_BLOCKED);
  1008. iwl_wr(CSR_INT, 0xffffffffu);
  1009. /* iwl_pcie_nic_init(): APM, power source VMAIN, NIC config, rings. */
  1010. if (apm_init() < 0)
  1011. return -1;
  1012. if (S->fam->apmg)
  1013. iwl_wr_prph(APMG_PS_CTRL_REG,
  1014. iwl_rd_prph(APMG_PS_CTRL_REG) &
  1015. ~APMG_PS_PWR_SRC_MSK);
  1016. if (alloc_rings() < 0) {
  1017. iwl_log("out of memory for rings");
  1018. return -1;
  1019. }
  1020. S->fatal = 0;
  1021. S->waiting = 0;
  1022. /* iwl_op_mode_nic_config(): radio SKU bits (DVM) before the rings. */
  1023. if (S->nic_config)
  1024. S->nic_config();
  1025. if (S->fam->secure8000) {
  1026. /* iwl_mvm_nic_config(): MAC step (the 8000 CSR_HW_REV keeps the
  1027. * step in bits 0-1, no dash) and the radio type/step/dash from
  1028. * the PHY SKU; RADIO_SI/MAC_SI stay clear on 8000. */
  1029. unsigned int k = S->fw.phy_sku;
  1030. iwl_wr(CSR_HW_IF_CONFIG_REG,
  1031. (iwl_rd(CSR_HW_IF_CONFIG_REG) & ~0xff2fu) |
  1032. (S->hw_rev & 3) << 2 | (k & 3) << 10 |
  1033. ((k >> 2) & 3) << 14 | ((k >> 4) & 3) << 12);
  1034. }
  1035. rx_init();
  1036. tx_init();
  1037. if (S->fam->shadow_reg)
  1038. iwl_wr(CSR_MAC_SHADOW_REG_CTRL,
  1039. iwl_rd(CSR_MAC_SHADOW_REG_CTRL) | 0x800fffffu);
  1040. iwl_wr(CSR_UCODE_DRV_GP1_CLR, UCODE_SW_BIT_RFKILL);
  1041. if (load_sections(img) < 0) {
  1042. iwl_log("firmware section load failed");
  1043. return -1;
  1044. }
  1045. iwl_wr(CSR_INT, 0xffffffffu);
  1046. if (!S->fam->secure8000)
  1047. iwl_wr(CSR_RESET, 0); /* release the CPU */
  1048. return 0;
  1049. }
  1050. /* After ALIVE: scheduler context, byte-count tables, cmd queue on FIFO 7,
  1051. * TX DMA channels (iwl_pcie_tx_start()). */
  1052. /* iwl_trans_pcie_txq_enable() with a cfg (the transport programs the
  1053. * scheduler: FIFO, window, frame limit), as iwl_trans_ac_txq_enable()
  1054. * does for the command queue and for DVM's AC queues. */
  1055. static void scd_enable_queue(unsigned int qn, unsigned int fifo)
  1056. {
  1057. struct txq *q = &S->q[qn];
  1058. int cmdq = qn == S->fam->cmd_q;
  1059. if (cmdq && S->fam->scd_set_active)
  1060. iwl_wr_prph(SCD_EN_CTRL, 0);
  1061. iwl_wr_prph(scd_queue_status(qn), 1u << SCD_STTS_ACT_EN);
  1062. if (!cmdq)
  1063. prph_set(SCD_QUEUECHAIN_SEL, 1u << qn);
  1064. prph_clr(SCD_AGGR_SEL, 1u << qn);
  1065. q->wptr = q->rptr = 0;
  1066. iwl_wr(HBUS_TARG_WRPTR, 0 | (qn << 8));
  1067. iwl_wr_prph(scd_queue_rdptr(qn), 0);
  1068. mem_wr32(S->scd_base + SCD_CONTEXT_MEM + qn * 8, 0);
  1069. mem_wr32(S->scd_base + SCD_CONTEXT_MEM + qn * 8 + 4,
  1070. (64u & 0x7f) | ((64u << 16) & 0x7f0000u));
  1071. iwl_wr_prph(scd_queue_status(qn),
  1072. (1u << SCD_STTS_ACTIVE) | (fifo << SCD_STTS_TXF) |
  1073. (1u << SCD_STTS_WSL) | SCD_STTS_MSK);
  1074. if (cmdq && S->fam->scd_set_active)
  1075. iwl_wr_prph(SCD_EN_CTRL, 1u << qn);
  1076. q->active = 1;
  1077. }
  1078. void iwl_txq_enable_fifo(int qn, unsigned int fifo)
  1079. {
  1080. if (S && qn >= 0 && qn < IWL_NUM_Q && S->q[qn].tfds)
  1081. scd_enable_queue((unsigned int)qn, fifo);
  1082. }
  1083. void iwl_fw_alive(unsigned int scd_base)
  1084. {
  1085. unsigned int c;
  1086. S->scd_base = iwl_rd_prph(SCD_SRAM_BASE_ADDR);
  1087. if (scd_base && scd_base != S->scd_base)
  1088. iwl_loghex("scd base mismatch, alive says", scd_base);
  1089. mem_zero(S->scd_base + SCD_CONTEXT_MEM,
  1090. (((SCD_TRANS_TBL_MEM + S->fam->nq * 2) & 0xfffcu) -
  1091. SCD_CONTEXT_MEM) / 4);
  1092. iwl_wr_prph(SCD_DRAM_BASE_ADDR, (unsigned int)S->bc >> 10);
  1093. /* The SCD chain extension misbehaves on the 6000 family. */
  1094. if (S->fam->chain_ext_wa)
  1095. iwl_wr_prph(SCD_CHAINEXT_EN, 0);
  1096. scd_enable_queue(S->fam->cmd_q, S->fam->cmd_fifo);
  1097. iwl_wr_prph(SCD_TXFACT, 0xffu); /* FIFOs 0..7 */
  1098. for (c = 0; c < 8; c++)
  1099. iwl_wr(FH_TCSR_CONFIG(c),
  1100. FH_TCSR_DMA_ENABLE | FH_TCSR_CREDIT_ENABLE);
  1101. iwl_wr(FH_TX_CHICKEN_BITS, iwl_rd(FH_TX_CHICKEN_BITS) | 0x2u);
  1102. if (S->fam->apmg)
  1103. prph_clr(APMG_PCIDEV_STT_REG, APMG_PCIDEV_L1_ACT_DIS);
  1104. }
  1105. /* DQA data/mgmt queue (iwl_trans_pcie_txq_enable() with cfg == NULL: the
  1106. * firmware's SCD_QUEUE_CFG command configures the scheduler itself). */
  1107. void iwl_txq_enable(int qn, unsigned short ssn)
  1108. {
  1109. struct txq *q = &S->q[qn];
  1110. /* SCD write-pointer quirk: never move by a multiple of 0x40. */
  1111. if (!((ssn - q->wptr) & 0x3f) && ssn != q->wptr)
  1112. ssn++;
  1113. q->rptr = q->wptr = ssn & 0xff;
  1114. iwl_wr(HBUS_TARG_WRPTR, (ssn & 0xff) | ((unsigned int)qn << 8));
  1115. q->active = 1;
  1116. }
  1117. unsigned int iwl_txq_wptr(int q)
  1118. {
  1119. return S->q[q].wptr;
  1120. }
  1121. /* ---- TX ---------------------------------------------------------------- */
  1122. static void tfd_set(unsigned char *tfd, unsigned int tb, unsigned int addr,
  1123. unsigned int len)
  1124. {
  1125. unsigned char *e = tfd + 4 + tb * 6;
  1126. e[0] = addr; e[1] = addr >> 8; e[2] = addr >> 16; e[3] = addr >> 24;
  1127. e[4] = (len << 4) & 0xf0;
  1128. e[5] = len >> 4;
  1129. tfd[3] = (unsigned char)(tb + 1);
  1130. }
  1131. static void bc_set(int qn, unsigned int idx, unsigned int bytes,
  1132. unsigned int sta)
  1133. {
  1134. unsigned short *t = S->bc + qn * BC_ENTRIES;
  1135. unsigned short v;
  1136. /* + CRC + delimiter; in dwords with bc_table_dword (7000), else
  1137. * bytes (iwl_txq_gen1_update_byte_cnt_tbl()). */
  1138. bytes += 8;
  1139. if (S->fam->bc_dword)
  1140. bytes = (bytes + 3) / 4;
  1141. v = (unsigned short)(bytes | (sta << 12));
  1142. t[idx] = v;
  1143. if (idx < BC_ENTRIES - TFD_RING)
  1144. t[TFD_RING + idx] = v;
  1145. }
  1146. static int txq_space(struct txq *q)
  1147. {
  1148. return ((q->wptr - q->rptr) & 0xff) < q->nslots - 1;
  1149. }
  1150. static unsigned char *slot_buf(struct txq *q, unsigned int idx)
  1151. {
  1152. return q->bufs + (idx & (q->nslots - 1)) * q->bufsize;
  1153. }
  1154. static void ring_doorbell(int qn)
  1155. {
  1156. struct txq *q = &S->q[qn];
  1157. q->wptr = (q->wptr + 1) & 0xff;
  1158. iwl_wr(HBUS_TARG_WRPTR, q->wptr | ((unsigned int)qn << 8));
  1159. }
  1160. static int enqueue_cmd(unsigned int id, const void *data, unsigned int len,
  1161. unsigned short *seq_out)
  1162. {
  1163. unsigned int cq = S->fam->cmd_q;
  1164. struct txq *q = &S->q[cq];
  1165. /* mvm sets wide_cmd_header: legacy (group 0) commands are sent as
  1166. * group 1 with the 8-byte header (iwl_trans_send_cmd(), DEF_ID).
  1167. * DVM uses the plain 4-byte iwl_cmd_header. */
  1168. unsigned char group = (id >> 8) & 0xff ? (id >> 8) & 0xff : 1;
  1169. unsigned char *buf, *tfd;
  1170. unsigned int hdr = S->fam->wide_hdr ? 8 : 4, total = hdr + len, idx;
  1171. unsigned short seq;
  1172. if (!q->active || total > CMD_BUF)
  1173. return -1;
  1174. if (!txq_space(q)) {
  1175. /* Command responses reclaim; every one we sent was synchronous,
  1176. * so the ring can simply be considered drained. */
  1177. q->rptr = q->wptr;
  1178. }
  1179. idx = q->wptr;
  1180. buf = slot_buf(q, idx);
  1181. seq = (unsigned short)((cq << 8) | idx);
  1182. buf[0] = id & 0xff;
  1183. buf[1] = S->fam->wide_hdr ? group : 0;
  1184. buf[2] = seq & 0xff;
  1185. buf[3] = seq >> 8;
  1186. if (S->fam->wide_hdr) {
  1187. buf[4] = len & 0xff;
  1188. buf[5] = len >> 8;
  1189. buf[6] = 0;
  1190. buf[7] = (id >> 16) & 0xff;
  1191. }
  1192. iwl_memcpy(buf + hdr, data, len);
  1193. tfd = q->tfds + idx * TFD_SIZE;
  1194. iwl_memzero(tfd, TFD_SIZE);
  1195. if (total <= FIRST_TB) {
  1196. tfd_set(tfd, 0, (unsigned int)buf, total);
  1197. } else {
  1198. tfd_set(tfd, 0, (unsigned int)buf, FIRST_TB);
  1199. tfd_set(tfd, 1, (unsigned int)buf + FIRST_TB, total - FIRST_TB);
  1200. }
  1201. *seq_out = seq;
  1202. ring_doorbell(cq);
  1203. return 0;
  1204. }
  1205. int iwl_tx(int qn, const void *txcmd, unsigned int txlen,
  1206. const void *frame, unsigned int flen, unsigned char sta_id,
  1207. unsigned int mic_len)
  1208. {
  1209. struct txq *q = &S->q[qn];
  1210. unsigned char *buf, *tfd;
  1211. unsigned int idx, total, t;
  1212. if (!q->active || 4 + txlen + flen > q->bufsize)
  1213. return -1;
  1214. for (t = 0; !txq_space(q); t++) {
  1215. if (t > 2000 || iwl_rx_poll() < 0)
  1216. return -1;
  1217. iwl_udelay(50);
  1218. }
  1219. idx = q->wptr;
  1220. buf = slot_buf(q, idx);
  1221. /* struct iwl_device_tx_cmd: 4-byte header (TX_CMD, seq) + tx_cmd. */
  1222. buf[0] = 0x1c; /* TX_CMD */
  1223. buf[1] = 0;
  1224. buf[2] = idx;
  1225. buf[3] = (unsigned char)qn;
  1226. iwl_memcpy(buf + 4, txcmd, txlen);
  1227. iwl_memcpy(buf + 4 + txlen, frame, flen);
  1228. /* dram_lsb_ptr/msb: where the firmware writes the tx_cmd scratch
  1229. * (offset 8 of struct iwl_tx_cmd), as iwl_pcie_tx() does. */
  1230. if (txlen >= 56) {
  1231. unsigned int scratch = (unsigned int)buf + 4 + 8;
  1232. buf[4 + 44] = scratch;
  1233. buf[4 + 45] = scratch >> 8;
  1234. buf[4 + 46] = scratch >> 16;
  1235. buf[4 + 47] = scratch >> 24;
  1236. buf[4 + 48] = 0;
  1237. }
  1238. total = 4 + txlen + flen;
  1239. tfd = q->tfds + idx * TFD_SIZE;
  1240. iwl_memzero(tfd, TFD_SIZE);
  1241. tfd_set(tfd, 0, (unsigned int)buf, FIRST_TB);
  1242. tfd_set(tfd, 1, (unsigned int)buf + FIRST_TB, total - FIRST_TB);
  1243. /* Byte count: the over-the-air MPDU (tx_cmd->len) plus the MIC the
  1244. * firmware appends for a hardware-encrypted frame. */
  1245. bc_set(qn, idx, flen + mic_len, sta_id);
  1246. ring_doorbell(qn);
  1247. return 0;
  1248. }
  1249. void iwl_tx_reclaim(int qn, unsigned int idx)
  1250. {
  1251. if (qn >= 0 && qn < IWL_NUM_Q)
  1252. S->q[qn].rptr = (idx + 1) & 0xff;
  1253. }
  1254. /* ---- RX ---------------------------------------------------------------- */
  1255. void iwl_set_rx_handler(iwl_rx_handler h)
  1256. {
  1257. S->handler = h;
  1258. }
  1259. static void handle_pkt(const struct iwl_pkt *pkt)
  1260. {
  1261. if (S->waiting && !(pkt->seq & 0x8000u) && pkt->seq == S->wait_seq) {
  1262. unsigned int n = iwl_pkt_payload_len(pkt);
  1263. if (n > S->resp_max)
  1264. n = S->resp_max;
  1265. if (S->resp && n)
  1266. iwl_memcpy(S->resp, pkt->data, n);
  1267. S->resp_len = (int)iwl_pkt_payload_len(pkt);
  1268. S->resp_done = 1;
  1269. return;
  1270. }
  1271. if (S->wait_notif && pkt->group == 0 && pkt->cmd == S->wait_notif)
  1272. S->notif_seen = 1;
  1273. if (S->handler)
  1274. S->handler(pkt);
  1275. }
  1276. int iwl_rx_poll(void)
  1277. {
  1278. unsigned int r, n = 0, ints;
  1279. if (!S || !S->rbd)
  1280. return 0;
  1281. ints = iwl_rd(CSR_INT);
  1282. if (ints & (INT_SW_ERR | INT_HW_ERR)) {
  1283. if (!S->fatal) {
  1284. iwl_loghex("firmware/hardware error, CSR_INT", ints);
  1285. iwl_loghex("GP_CNTRL", iwl_rd(CSR_GP_CNTRL));
  1286. }
  1287. S->fatal = 1;
  1288. iwl_wr(CSR_INT, ints);
  1289. return -1;
  1290. }
  1291. if (ints)
  1292. iwl_wr(CSR_INT, ints);
  1293. if (iwl_rd(CSR_FH_INT_STATUS))
  1294. iwl_wr(CSR_FH_INT_STATUS, 0xffffffffu);
  1295. r = S->rb_stts[0] & 0x0fff;
  1296. r &= RX_RING - 1;
  1297. while (S->rx_read != r) {
  1298. unsigned char *page = S->rb[S->rx_read];
  1299. unsigned int off = 0;
  1300. while (off + 12 < 4096) {
  1301. const struct iwl_pkt *pkt =
  1302. (const struct iwl_pkt *)(page + off);
  1303. unsigned int len;
  1304. if (pkt->len_n_flags == 0x55550000u)
  1305. break;
  1306. len = (pkt->len_n_flags & 0x3fffu) + 4;
  1307. if (len < 8 || off + len > 4096)
  1308. break;
  1309. handle_pkt(pkt);
  1310. off += (len + 63) & ~63u;
  1311. }
  1312. *(volatile unsigned int *)page = 0x55550000u;
  1313. S->rx_read = (S->rx_read + 1) & (RX_RING - 1);
  1314. n++;
  1315. /* Give the hardware the slot 8 behind us (multiple of 8). */
  1316. if ((S->rx_read & 7) == 0)
  1317. iwl_wr(FH_RSCSR_RBDCB_WPTR,
  1318. (S->rx_read + RX_RING - 8) & (RX_RING - 1));
  1319. }
  1320. return S->fatal ? -1 : (int)n;
  1321. }
  1322. int iwl_wait_notif(unsigned char cmd, unsigned int ms)
  1323. {
  1324. unsigned int t;
  1325. S->wait_notif = cmd;
  1326. S->notif_seen = 0;
  1327. for (t = 0; t < ms * 10; t++) {
  1328. if (iwl_rx_poll() < 0)
  1329. break;
  1330. if (S->notif_seen)
  1331. break;
  1332. iwl_udelay(100);
  1333. }
  1334. S->wait_notif = 0;
  1335. return S->notif_seen ? 0 : -1;
  1336. }
  1337. int iwl_send_cmd(unsigned int id, const void *data, unsigned int len,
  1338. void *resp, unsigned int resp_max)
  1339. {
  1340. unsigned short seq;
  1341. unsigned int t;
  1342. if (!S || S->fatal)
  1343. return -1;
  1344. S->resp = resp;
  1345. S->resp_max = resp_max;
  1346. S->resp_done = 0;
  1347. S->resp_len = -1;
  1348. if (enqueue_cmd(id, data, len, &seq) < 0)
  1349. return -1;
  1350. S->wait_seq = seq;
  1351. S->waiting = 1;
  1352. for (t = 0; t < 20000 && !S->resp_done; t++) { /* 2 s */
  1353. if (iwl_rx_poll() < 0)
  1354. break;
  1355. if (!S->resp_done)
  1356. iwl_udelay(100);
  1357. }
  1358. S->waiting = 0;
  1359. S->q[S->fam->cmd_q].rptr = S->q[S->fam->cmd_q].wptr;
  1360. if (!S->resp_done) {
  1361. iwl_loghex("command timeout, id", id);
  1362. return -1;
  1363. }
  1364. return S->resp_len;
  1365. }
  1366. int iwl_send_cmd_async(unsigned int id, const void *data, unsigned int len)
  1367. {
  1368. unsigned short seq;
  1369. if (!S || S->fatal)
  1370. return -1;
  1371. return enqueue_cmd(id, data, len, &seq);
  1372. }