sdio.c 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113
  1. /*
  2. * Copyright (c) 2004-2011 Atheros Communications Inc.
  3. * Copyright (c) 2011-2012,2017 Qualcomm Atheros, Inc.
  4. * Copyright (c) 2016-2017 Erik Stromdahl <erik.stromdahl@gmail.com>
  5. *
  6. * Permission to use, copy, modify, and/or distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/mmc/card.h>
  20. #include <linux/mmc/mmc.h>
  21. #include <linux/mmc/host.h>
  22. #include <linux/mmc/sdio_func.h>
  23. #include <linux/mmc/sdio_ids.h>
  24. #include <linux/mmc/sdio.h>
  25. #include <linux/mmc/sd.h>
  26. #include <linux/bitfield.h>
  27. #include "core.h"
  28. #include "bmi.h"
  29. #include "debug.h"
  30. #include "hif.h"
  31. #include "htc.h"
  32. #include "mac.h"
  33. #include "targaddrs.h"
  34. #include "trace.h"
  35. #include "sdio.h"
  36. /* inlined helper functions */
  37. static inline int ath10k_sdio_calc_txrx_padded_len(struct ath10k_sdio *ar_sdio,
  38. size_t len)
  39. {
  40. return __ALIGN_MASK((len), ar_sdio->mbox_info.block_mask);
  41. }
  42. static inline enum ath10k_htc_ep_id pipe_id_to_eid(u8 pipe_id)
  43. {
  44. return (enum ath10k_htc_ep_id)pipe_id;
  45. }
  46. static inline void ath10k_sdio_mbox_free_rx_pkt(struct ath10k_sdio_rx_data *pkt)
  47. {
  48. dev_kfree_skb(pkt->skb);
  49. pkt->skb = NULL;
  50. pkt->alloc_len = 0;
  51. pkt->act_len = 0;
  52. pkt->trailer_only = false;
  53. }
  54. static inline int ath10k_sdio_mbox_alloc_rx_pkt(struct ath10k_sdio_rx_data *pkt,
  55. size_t act_len, size_t full_len,
  56. bool part_of_bundle,
  57. bool last_in_bundle)
  58. {
  59. pkt->skb = dev_alloc_skb(full_len);
  60. if (!pkt->skb)
  61. return -ENOMEM;
  62. pkt->act_len = act_len;
  63. pkt->alloc_len = full_len;
  64. pkt->part_of_bundle = part_of_bundle;
  65. pkt->last_in_bundle = last_in_bundle;
  66. pkt->trailer_only = false;
  67. return 0;
  68. }
  69. static inline bool is_trailer_only_msg(struct ath10k_sdio_rx_data *pkt)
  70. {
  71. bool trailer_only = false;
  72. struct ath10k_htc_hdr *htc_hdr =
  73. (struct ath10k_htc_hdr *)pkt->skb->data;
  74. u16 len = __le16_to_cpu(htc_hdr->len);
  75. if (len == htc_hdr->trailer_len)
  76. trailer_only = true;
  77. return trailer_only;
  78. }
  79. /* sdio/mmc functions */
  80. static inline void ath10k_sdio_set_cmd52_arg(u32 *arg, u8 write, u8 raw,
  81. unsigned int address,
  82. unsigned char val)
  83. {
  84. *arg = FIELD_PREP(BIT(31), write) |
  85. FIELD_PREP(BIT(27), raw) |
  86. FIELD_PREP(BIT(26), 1) |
  87. FIELD_PREP(GENMASK(25, 9), address) |
  88. FIELD_PREP(BIT(8), 1) |
  89. FIELD_PREP(GENMASK(7, 0), val);
  90. }
  91. static int ath10k_sdio_func0_cmd52_wr_byte(struct mmc_card *card,
  92. unsigned int address,
  93. unsigned char byte)
  94. {
  95. struct mmc_command io_cmd;
  96. memset(&io_cmd, 0, sizeof(io_cmd));
  97. ath10k_sdio_set_cmd52_arg(&io_cmd.arg, 1, 0, address, byte);
  98. io_cmd.opcode = SD_IO_RW_DIRECT;
  99. io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
  100. return mmc_wait_for_cmd(card->host, &io_cmd, 0);
  101. }
  102. static int ath10k_sdio_func0_cmd52_rd_byte(struct mmc_card *card,
  103. unsigned int address,
  104. unsigned char *byte)
  105. {
  106. struct mmc_command io_cmd;
  107. int ret;
  108. memset(&io_cmd, 0, sizeof(io_cmd));
  109. ath10k_sdio_set_cmd52_arg(&io_cmd.arg, 0, 0, address, 0);
  110. io_cmd.opcode = SD_IO_RW_DIRECT;
  111. io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
  112. ret = mmc_wait_for_cmd(card->host, &io_cmd, 0);
  113. if (!ret)
  114. *byte = io_cmd.resp[0];
  115. return ret;
  116. }
  117. static int ath10k_sdio_config(struct ath10k *ar)
  118. {
  119. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  120. struct sdio_func *func = ar_sdio->func;
  121. unsigned char byte, asyncintdelay = 2;
  122. int ret;
  123. ath10k_dbg(ar, ATH10K_DBG_BOOT, "sdio configuration\n");
  124. sdio_claim_host(func);
  125. byte = 0;
  126. ret = ath10k_sdio_func0_cmd52_rd_byte(func->card,
  127. SDIO_CCCR_DRIVE_STRENGTH,
  128. &byte);
  129. byte &= ~ATH10K_SDIO_DRIVE_DTSX_MASK;
  130. byte |= FIELD_PREP(ATH10K_SDIO_DRIVE_DTSX_MASK,
  131. ATH10K_SDIO_DRIVE_DTSX_TYPE_D);
  132. ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
  133. SDIO_CCCR_DRIVE_STRENGTH,
  134. byte);
  135. byte = 0;
  136. ret = ath10k_sdio_func0_cmd52_rd_byte(
  137. func->card,
  138. CCCR_SDIO_DRIVER_STRENGTH_ENABLE_ADDR,
  139. &byte);
  140. byte |= (CCCR_SDIO_DRIVER_STRENGTH_ENABLE_A |
  141. CCCR_SDIO_DRIVER_STRENGTH_ENABLE_C |
  142. CCCR_SDIO_DRIVER_STRENGTH_ENABLE_D);
  143. ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
  144. CCCR_SDIO_DRIVER_STRENGTH_ENABLE_ADDR,
  145. byte);
  146. if (ret) {
  147. ath10k_warn(ar, "failed to enable driver strength: %d\n", ret);
  148. goto out;
  149. }
  150. byte = 0;
  151. ret = ath10k_sdio_func0_cmd52_rd_byte(func->card,
  152. CCCR_SDIO_IRQ_MODE_REG_SDIO3,
  153. &byte);
  154. byte |= SDIO_IRQ_MODE_ASYNC_4BIT_IRQ_SDIO3;
  155. ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
  156. CCCR_SDIO_IRQ_MODE_REG_SDIO3,
  157. byte);
  158. if (ret) {
  159. ath10k_warn(ar, "failed to enable 4-bit async irq mode: %d\n",
  160. ret);
  161. goto out;
  162. }
  163. byte = 0;
  164. ret = ath10k_sdio_func0_cmd52_rd_byte(func->card,
  165. CCCR_SDIO_ASYNC_INT_DELAY_ADDRESS,
  166. &byte);
  167. byte &= ~CCCR_SDIO_ASYNC_INT_DELAY_MASK;
  168. byte |= FIELD_PREP(CCCR_SDIO_ASYNC_INT_DELAY_MASK, asyncintdelay);
  169. ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
  170. CCCR_SDIO_ASYNC_INT_DELAY_ADDRESS,
  171. byte);
  172. /* give us some time to enable, in ms */
  173. func->enable_timeout = 100;
  174. ret = sdio_set_block_size(func, ar_sdio->mbox_info.block_size);
  175. if (ret) {
  176. ath10k_warn(ar, "failed to set sdio block size to %d: %d\n",
  177. ar_sdio->mbox_info.block_size, ret);
  178. goto out;
  179. }
  180. out:
  181. sdio_release_host(func);
  182. return ret;
  183. }
  184. static int ath10k_sdio_write32(struct ath10k *ar, u32 addr, u32 val)
  185. {
  186. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  187. struct sdio_func *func = ar_sdio->func;
  188. int ret;
  189. sdio_claim_host(func);
  190. sdio_writel(func, val, addr, &ret);
  191. if (ret) {
  192. ath10k_warn(ar, "failed to write 0x%x to address 0x%x: %d\n",
  193. val, addr, ret);
  194. goto out;
  195. }
  196. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio write32 addr 0x%x val 0x%x\n",
  197. addr, val);
  198. out:
  199. sdio_release_host(func);
  200. return ret;
  201. }
  202. static int ath10k_sdio_writesb32(struct ath10k *ar, u32 addr, u32 val)
  203. {
  204. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  205. struct sdio_func *func = ar_sdio->func;
  206. __le32 *buf;
  207. int ret;
  208. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  209. if (!buf)
  210. return -ENOMEM;
  211. *buf = cpu_to_le32(val);
  212. sdio_claim_host(func);
  213. ret = sdio_writesb(func, addr, buf, sizeof(*buf));
  214. if (ret) {
  215. ath10k_warn(ar, "failed to write value 0x%x to fixed sb address 0x%x: %d\n",
  216. val, addr, ret);
  217. goto out;
  218. }
  219. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio writesb32 addr 0x%x val 0x%x\n",
  220. addr, val);
  221. out:
  222. sdio_release_host(func);
  223. kfree(buf);
  224. return ret;
  225. }
  226. static int ath10k_sdio_read32(struct ath10k *ar, u32 addr, u32 *val)
  227. {
  228. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  229. struct sdio_func *func = ar_sdio->func;
  230. int ret;
  231. sdio_claim_host(func);
  232. *val = sdio_readl(func, addr, &ret);
  233. if (ret) {
  234. ath10k_warn(ar, "failed to read from address 0x%x: %d\n",
  235. addr, ret);
  236. goto out;
  237. }
  238. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio read32 addr 0x%x val 0x%x\n",
  239. addr, *val);
  240. out:
  241. sdio_release_host(func);
  242. return ret;
  243. }
  244. static int ath10k_sdio_read(struct ath10k *ar, u32 addr, void *buf, size_t len)
  245. {
  246. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  247. struct sdio_func *func = ar_sdio->func;
  248. int ret;
  249. sdio_claim_host(func);
  250. ret = sdio_memcpy_fromio(func, buf, addr, len);
  251. if (ret) {
  252. ath10k_warn(ar, "failed to read from address 0x%x: %d\n",
  253. addr, ret);
  254. goto out;
  255. }
  256. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio read addr 0x%x buf 0x%p len %zu\n",
  257. addr, buf, len);
  258. ath10k_dbg_dump(ar, ATH10K_DBG_SDIO_DUMP, NULL, "sdio read ", buf, len);
  259. out:
  260. sdio_release_host(func);
  261. return ret;
  262. }
  263. static int ath10k_sdio_write(struct ath10k *ar, u32 addr, const void *buf, size_t len)
  264. {
  265. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  266. struct sdio_func *func = ar_sdio->func;
  267. int ret;
  268. sdio_claim_host(func);
  269. /* For some reason toio() doesn't have const for the buffer, need
  270. * an ugly hack to workaround that.
  271. */
  272. ret = sdio_memcpy_toio(func, addr, (void *)buf, len);
  273. if (ret) {
  274. ath10k_warn(ar, "failed to write to address 0x%x: %d\n",
  275. addr, ret);
  276. goto out;
  277. }
  278. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio write addr 0x%x buf 0x%p len %zu\n",
  279. addr, buf, len);
  280. ath10k_dbg_dump(ar, ATH10K_DBG_SDIO_DUMP, NULL, "sdio write ", buf, len);
  281. out:
  282. sdio_release_host(func);
  283. return ret;
  284. }
  285. static int ath10k_sdio_readsb(struct ath10k *ar, u32 addr, void *buf, size_t len)
  286. {
  287. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  288. struct sdio_func *func = ar_sdio->func;
  289. int ret;
  290. sdio_claim_host(func);
  291. len = round_down(len, ar_sdio->mbox_info.block_size);
  292. ret = sdio_readsb(func, buf, addr, len);
  293. if (ret) {
  294. ath10k_warn(ar, "failed to read from fixed (sb) address 0x%x: %d\n",
  295. addr, ret);
  296. goto out;
  297. }
  298. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio readsb addr 0x%x buf 0x%p len %zu\n",
  299. addr, buf, len);
  300. ath10k_dbg_dump(ar, ATH10K_DBG_SDIO_DUMP, NULL, "sdio readsb ", buf, len);
  301. out:
  302. sdio_release_host(func);
  303. return ret;
  304. }
  305. /* HIF mbox functions */
  306. static int ath10k_sdio_mbox_rx_process_packet(struct ath10k *ar,
  307. struct ath10k_sdio_rx_data *pkt,
  308. u32 *lookaheads,
  309. int *n_lookaheads)
  310. {
  311. struct ath10k_htc *htc = &ar->htc;
  312. struct sk_buff *skb = pkt->skb;
  313. struct ath10k_htc_hdr *htc_hdr = (struct ath10k_htc_hdr *)skb->data;
  314. bool trailer_present = htc_hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT;
  315. enum ath10k_htc_ep_id eid;
  316. u16 payload_len;
  317. u8 *trailer;
  318. int ret;
  319. payload_len = le16_to_cpu(htc_hdr->len);
  320. skb->len = payload_len + sizeof(struct ath10k_htc_hdr);
  321. if (trailer_present) {
  322. trailer = skb->data + sizeof(*htc_hdr) +
  323. payload_len - htc_hdr->trailer_len;
  324. eid = pipe_id_to_eid(htc_hdr->eid);
  325. ret = ath10k_htc_process_trailer(htc,
  326. trailer,
  327. htc_hdr->trailer_len,
  328. eid,
  329. lookaheads,
  330. n_lookaheads);
  331. if (ret)
  332. return ret;
  333. if (is_trailer_only_msg(pkt))
  334. pkt->trailer_only = true;
  335. skb_trim(skb, skb->len - htc_hdr->trailer_len);
  336. }
  337. skb_pull(skb, sizeof(*htc_hdr));
  338. return 0;
  339. }
  340. static int ath10k_sdio_mbox_rx_process_packets(struct ath10k *ar,
  341. u32 lookaheads[],
  342. int *n_lookahead)
  343. {
  344. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  345. struct ath10k_htc *htc = &ar->htc;
  346. struct ath10k_sdio_rx_data *pkt;
  347. struct ath10k_htc_ep *ep;
  348. enum ath10k_htc_ep_id id;
  349. int ret, i, *n_lookahead_local;
  350. u32 *lookaheads_local;
  351. int lookahead_idx = 0;
  352. for (i = 0; i < ar_sdio->n_rx_pkts; i++) {
  353. lookaheads_local = lookaheads;
  354. n_lookahead_local = n_lookahead;
  355. id = ((struct ath10k_htc_hdr *)
  356. &lookaheads[lookahead_idx++])->eid;
  357. if (id >= ATH10K_HTC_EP_COUNT) {
  358. ath10k_warn(ar, "invalid endpoint in look-ahead: %d\n",
  359. id);
  360. ret = -ENOMEM;
  361. goto out;
  362. }
  363. ep = &htc->endpoint[id];
  364. if (ep->service_id == 0) {
  365. ath10k_warn(ar, "ep %d is not connected\n", id);
  366. ret = -ENOMEM;
  367. goto out;
  368. }
  369. pkt = &ar_sdio->rx_pkts[i];
  370. if (pkt->part_of_bundle && !pkt->last_in_bundle) {
  371. /* Only read lookahead's from RX trailers
  372. * for the last packet in a bundle.
  373. */
  374. lookahead_idx--;
  375. lookaheads_local = NULL;
  376. n_lookahead_local = NULL;
  377. }
  378. ret = ath10k_sdio_mbox_rx_process_packet(ar,
  379. pkt,
  380. lookaheads_local,
  381. n_lookahead_local);
  382. if (ret)
  383. goto out;
  384. if (!pkt->trailer_only)
  385. ep->ep_ops.ep_rx_complete(ar_sdio->ar, pkt->skb);
  386. else
  387. kfree_skb(pkt->skb);
  388. /* The RX complete handler now owns the skb...*/
  389. pkt->skb = NULL;
  390. pkt->alloc_len = 0;
  391. }
  392. ret = 0;
  393. out:
  394. /* Free all packets that was not passed on to the RX completion
  395. * handler...
  396. */
  397. for (; i < ar_sdio->n_rx_pkts; i++)
  398. ath10k_sdio_mbox_free_rx_pkt(&ar_sdio->rx_pkts[i]);
  399. return ret;
  400. }
  401. static int ath10k_sdio_mbox_alloc_pkt_bundle(struct ath10k *ar,
  402. struct ath10k_sdio_rx_data *rx_pkts,
  403. struct ath10k_htc_hdr *htc_hdr,
  404. size_t full_len, size_t act_len,
  405. size_t *bndl_cnt)
  406. {
  407. int ret, i;
  408. *bndl_cnt = FIELD_GET(ATH10K_HTC_FLAG_BUNDLE_MASK, htc_hdr->flags);
  409. if (*bndl_cnt > HTC_HOST_MAX_MSG_PER_RX_BUNDLE) {
  410. ath10k_warn(ar,
  411. "HTC bundle length %u exceeds maximum %u\n",
  412. le16_to_cpu(htc_hdr->len),
  413. HTC_HOST_MAX_MSG_PER_RX_BUNDLE);
  414. return -ENOMEM;
  415. }
  416. /* Allocate bndl_cnt extra skb's for the bundle.
  417. * The package containing the
  418. * ATH10K_HTC_FLAG_BUNDLE_MASK flag is not included
  419. * in bndl_cnt. The skb for that packet will be
  420. * allocated separately.
  421. */
  422. for (i = 0; i < *bndl_cnt; i++) {
  423. ret = ath10k_sdio_mbox_alloc_rx_pkt(&rx_pkts[i],
  424. act_len,
  425. full_len,
  426. true,
  427. false);
  428. if (ret)
  429. return ret;
  430. }
  431. return 0;
  432. }
  433. static int ath10k_sdio_mbox_rx_alloc(struct ath10k *ar,
  434. u32 lookaheads[], int n_lookaheads)
  435. {
  436. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  437. struct ath10k_htc_hdr *htc_hdr;
  438. size_t full_len, act_len;
  439. bool last_in_bundle;
  440. int ret, i;
  441. if (n_lookaheads > ATH10K_SDIO_MAX_RX_MSGS) {
  442. ath10k_warn(ar,
  443. "the total number of pkgs to be fetched (%u) exceeds maximum %u\n",
  444. n_lookaheads,
  445. ATH10K_SDIO_MAX_RX_MSGS);
  446. ret = -ENOMEM;
  447. goto err;
  448. }
  449. for (i = 0; i < n_lookaheads; i++) {
  450. htc_hdr = (struct ath10k_htc_hdr *)&lookaheads[i];
  451. last_in_bundle = false;
  452. if (le16_to_cpu(htc_hdr->len) >
  453. ATH10K_HTC_MBOX_MAX_PAYLOAD_LENGTH) {
  454. ath10k_warn(ar,
  455. "payload length %d exceeds max htc length: %zu\n",
  456. le16_to_cpu(htc_hdr->len),
  457. ATH10K_HTC_MBOX_MAX_PAYLOAD_LENGTH);
  458. ret = -ENOMEM;
  459. goto err;
  460. }
  461. act_len = le16_to_cpu(htc_hdr->len) + sizeof(*htc_hdr);
  462. full_len = ath10k_sdio_calc_txrx_padded_len(ar_sdio, act_len);
  463. if (full_len > ATH10K_SDIO_MAX_BUFFER_SIZE) {
  464. ath10k_warn(ar,
  465. "rx buffer requested with invalid htc_hdr length (%d, 0x%x): %d\n",
  466. htc_hdr->eid, htc_hdr->flags,
  467. le16_to_cpu(htc_hdr->len));
  468. ret = -EINVAL;
  469. goto err;
  470. }
  471. if (htc_hdr->flags & ATH10K_HTC_FLAG_BUNDLE_MASK) {
  472. /* HTC header indicates that every packet to follow
  473. * has the same padded length so that it can be
  474. * optimally fetched as a full bundle.
  475. */
  476. size_t bndl_cnt;
  477. ret = ath10k_sdio_mbox_alloc_pkt_bundle(ar,
  478. &ar_sdio->rx_pkts[i],
  479. htc_hdr,
  480. full_len,
  481. act_len,
  482. &bndl_cnt);
  483. n_lookaheads += bndl_cnt;
  484. i += bndl_cnt;
  485. /*Next buffer will be the last in the bundle */
  486. last_in_bundle = true;
  487. }
  488. /* Allocate skb for packet. If the packet had the
  489. * ATH10K_HTC_FLAG_BUNDLE_MASK flag set, all bundled
  490. * packet skb's have been allocated in the previous step.
  491. */
  492. if (htc_hdr->flags & ATH10K_HTC_FLAGS_RECV_1MORE_BLOCK)
  493. full_len += ATH10K_HIF_MBOX_BLOCK_SIZE;
  494. ret = ath10k_sdio_mbox_alloc_rx_pkt(&ar_sdio->rx_pkts[i],
  495. act_len,
  496. full_len,
  497. last_in_bundle,
  498. last_in_bundle);
  499. }
  500. ar_sdio->n_rx_pkts = i;
  501. return 0;
  502. err:
  503. for (i = 0; i < ATH10K_SDIO_MAX_RX_MSGS; i++) {
  504. if (!ar_sdio->rx_pkts[i].alloc_len)
  505. break;
  506. ath10k_sdio_mbox_free_rx_pkt(&ar_sdio->rx_pkts[i]);
  507. }
  508. return ret;
  509. }
  510. static int ath10k_sdio_mbox_rx_packet(struct ath10k *ar,
  511. struct ath10k_sdio_rx_data *pkt)
  512. {
  513. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  514. struct sk_buff *skb = pkt->skb;
  515. int ret;
  516. ret = ath10k_sdio_readsb(ar, ar_sdio->mbox_info.htc_addr,
  517. skb->data, pkt->alloc_len);
  518. pkt->status = ret;
  519. if (!ret)
  520. skb_put(skb, pkt->act_len);
  521. return ret;
  522. }
  523. static int ath10k_sdio_mbox_rx_fetch(struct ath10k *ar)
  524. {
  525. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  526. int ret, i;
  527. for (i = 0; i < ar_sdio->n_rx_pkts; i++) {
  528. ret = ath10k_sdio_mbox_rx_packet(ar,
  529. &ar_sdio->rx_pkts[i]);
  530. if (ret)
  531. goto err;
  532. }
  533. return 0;
  534. err:
  535. /* Free all packets that was not successfully fetched. */
  536. for (; i < ar_sdio->n_rx_pkts; i++)
  537. ath10k_sdio_mbox_free_rx_pkt(&ar_sdio->rx_pkts[i]);
  538. return ret;
  539. }
  540. /* This is the timeout for mailbox processing done in the sdio irq
  541. * handler. The timeout is deliberately set quite high since SDIO dump logs
  542. * over serial port can/will add a substantial overhead to the processing
  543. * (if enabled).
  544. */
  545. #define SDIO_MBOX_PROCESSING_TIMEOUT_HZ (20 * HZ)
  546. static int ath10k_sdio_mbox_rxmsg_pending_handler(struct ath10k *ar,
  547. u32 msg_lookahead, bool *done)
  548. {
  549. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  550. u32 lookaheads[ATH10K_SDIO_MAX_RX_MSGS];
  551. int n_lookaheads = 1;
  552. unsigned long timeout;
  553. int ret;
  554. *done = true;
  555. /* Copy the lookahead obtained from the HTC register table into our
  556. * temp array as a start value.
  557. */
  558. lookaheads[0] = msg_lookahead;
  559. timeout = jiffies + SDIO_MBOX_PROCESSING_TIMEOUT_HZ;
  560. do {
  561. /* Try to allocate as many HTC RX packets indicated by
  562. * n_lookaheads.
  563. */
  564. ret = ath10k_sdio_mbox_rx_alloc(ar, lookaheads,
  565. n_lookaheads);
  566. if (ret)
  567. break;
  568. if (ar_sdio->n_rx_pkts >= 2)
  569. /* A recv bundle was detected, force IRQ status
  570. * re-check again.
  571. */
  572. *done = false;
  573. ret = ath10k_sdio_mbox_rx_fetch(ar);
  574. /* Process fetched packets. This will potentially update
  575. * n_lookaheads depending on if the packets contain lookahead
  576. * reports.
  577. */
  578. n_lookaheads = 0;
  579. ret = ath10k_sdio_mbox_rx_process_packets(ar,
  580. lookaheads,
  581. &n_lookaheads);
  582. if (!n_lookaheads || ret)
  583. break;
  584. /* For SYNCH processing, if we get here, we are running
  585. * through the loop again due to updated lookaheads. Set
  586. * flag that we should re-check IRQ status registers again
  587. * before leaving IRQ processing, this can net better
  588. * performance in high throughput situations.
  589. */
  590. *done = false;
  591. } while (time_before(jiffies, timeout));
  592. if (ret && (ret != -ECANCELED))
  593. ath10k_warn(ar, "failed to get pending recv messages: %d\n",
  594. ret);
  595. return ret;
  596. }
  597. static int ath10k_sdio_mbox_proc_dbg_intr(struct ath10k *ar)
  598. {
  599. u32 val;
  600. int ret;
  601. /* TODO: Add firmware crash handling */
  602. ath10k_warn(ar, "firmware crashed\n");
  603. /* read counter to clear the interrupt, the debug error interrupt is
  604. * counter 0.
  605. */
  606. ret = ath10k_sdio_read32(ar, MBOX_COUNT_DEC_ADDRESS, &val);
  607. if (ret)
  608. ath10k_warn(ar, "failed to clear debug interrupt: %d\n", ret);
  609. return ret;
  610. }
  611. static int ath10k_sdio_mbox_proc_counter_intr(struct ath10k *ar)
  612. {
  613. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  614. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  615. u8 counter_int_status;
  616. int ret;
  617. mutex_lock(&irq_data->mtx);
  618. counter_int_status = irq_data->irq_proc_reg->counter_int_status &
  619. irq_data->irq_en_reg->cntr_int_status_en;
  620. /* NOTE: other modules like GMBOX may use the counter interrupt for
  621. * credit flow control on other counters, we only need to check for
  622. * the debug assertion counter interrupt.
  623. */
  624. if (counter_int_status & ATH10K_SDIO_TARGET_DEBUG_INTR_MASK)
  625. ret = ath10k_sdio_mbox_proc_dbg_intr(ar);
  626. else
  627. ret = 0;
  628. mutex_unlock(&irq_data->mtx);
  629. return ret;
  630. }
  631. static int ath10k_sdio_mbox_proc_err_intr(struct ath10k *ar)
  632. {
  633. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  634. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  635. u8 error_int_status;
  636. int ret;
  637. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio error interrupt\n");
  638. error_int_status = irq_data->irq_proc_reg->error_int_status & 0x0F;
  639. if (!error_int_status) {
  640. ath10k_warn(ar, "invalid error interrupt status: 0x%x\n",
  641. error_int_status);
  642. return -EIO;
  643. }
  644. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  645. "sdio error_int_status 0x%x\n", error_int_status);
  646. if (FIELD_GET(MBOX_ERROR_INT_STATUS_WAKEUP_MASK,
  647. error_int_status))
  648. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio interrupt error wakeup\n");
  649. if (FIELD_GET(MBOX_ERROR_INT_STATUS_RX_UNDERFLOW_MASK,
  650. error_int_status))
  651. ath10k_warn(ar, "rx underflow interrupt error\n");
  652. if (FIELD_GET(MBOX_ERROR_INT_STATUS_TX_OVERFLOW_MASK,
  653. error_int_status))
  654. ath10k_warn(ar, "tx overflow interrupt error\n");
  655. /* Clear the interrupt */
  656. irq_data->irq_proc_reg->error_int_status &= ~error_int_status;
  657. /* set W1C value to clear the interrupt, this hits the register first */
  658. ret = ath10k_sdio_writesb32(ar, MBOX_ERROR_INT_STATUS_ADDRESS,
  659. error_int_status);
  660. if (ret) {
  661. ath10k_warn(ar, "unable to write to error int status address: %d\n",
  662. ret);
  663. return ret;
  664. }
  665. return 0;
  666. }
  667. static int ath10k_sdio_mbox_proc_cpu_intr(struct ath10k *ar)
  668. {
  669. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  670. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  671. u8 cpu_int_status;
  672. int ret;
  673. mutex_lock(&irq_data->mtx);
  674. cpu_int_status = irq_data->irq_proc_reg->cpu_int_status &
  675. irq_data->irq_en_reg->cpu_int_status_en;
  676. if (!cpu_int_status) {
  677. ath10k_warn(ar, "CPU interrupt status is zero\n");
  678. ret = -EIO;
  679. goto out;
  680. }
  681. /* Clear the interrupt */
  682. irq_data->irq_proc_reg->cpu_int_status &= ~cpu_int_status;
  683. /* Set up the register transfer buffer to hit the register 4 times,
  684. * this is done to make the access 4-byte aligned to mitigate issues
  685. * with host bus interconnects that restrict bus transfer lengths to
  686. * be a multiple of 4-bytes.
  687. *
  688. * Set W1C value to clear the interrupt, this hits the register first.
  689. */
  690. ret = ath10k_sdio_writesb32(ar, MBOX_CPU_INT_STATUS_ADDRESS,
  691. cpu_int_status);
  692. if (ret) {
  693. ath10k_warn(ar, "unable to write to cpu interrupt status address: %d\n",
  694. ret);
  695. goto out;
  696. }
  697. out:
  698. mutex_unlock(&irq_data->mtx);
  699. return ret;
  700. }
  701. static int ath10k_sdio_mbox_read_int_status(struct ath10k *ar,
  702. u8 *host_int_status,
  703. u32 *lookahead)
  704. {
  705. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  706. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  707. struct ath10k_sdio_irq_proc_regs *irq_proc_reg = irq_data->irq_proc_reg;
  708. struct ath10k_sdio_irq_enable_regs *irq_en_reg = irq_data->irq_en_reg;
  709. u8 htc_mbox = FIELD_PREP(ATH10K_HTC_MAILBOX_MASK, 1);
  710. int ret;
  711. mutex_lock(&irq_data->mtx);
  712. *lookahead = 0;
  713. *host_int_status = 0;
  714. /* int_status_en is supposed to be non zero, otherwise interrupts
  715. * shouldn't be enabled. There is however a short time frame during
  716. * initialization between the irq register and int_status_en init
  717. * where this can happen.
  718. * We silently ignore this condition.
  719. */
  720. if (!irq_en_reg->int_status_en) {
  721. ret = 0;
  722. goto out;
  723. }
  724. /* Read the first sizeof(struct ath10k_irq_proc_registers)
  725. * bytes of the HTC register table. This
  726. * will yield us the value of different int status
  727. * registers and the lookahead registers.
  728. */
  729. ret = ath10k_sdio_read(ar, MBOX_HOST_INT_STATUS_ADDRESS,
  730. irq_proc_reg, sizeof(*irq_proc_reg));
  731. if (ret)
  732. goto out;
  733. /* Update only those registers that are enabled */
  734. *host_int_status = irq_proc_reg->host_int_status &
  735. irq_en_reg->int_status_en;
  736. /* Look at mbox status */
  737. if (!(*host_int_status & htc_mbox)) {
  738. *lookahead = 0;
  739. ret = 0;
  740. goto out;
  741. }
  742. /* Mask out pending mbox value, we use look ahead as
  743. * the real flag for mbox processing.
  744. */
  745. *host_int_status &= ~htc_mbox;
  746. if (irq_proc_reg->rx_lookahead_valid & htc_mbox) {
  747. *lookahead = le32_to_cpu(
  748. irq_proc_reg->rx_lookahead[ATH10K_HTC_MAILBOX]);
  749. if (!*lookahead)
  750. ath10k_warn(ar, "sdio mbox lookahead is zero\n");
  751. }
  752. out:
  753. mutex_unlock(&irq_data->mtx);
  754. return ret;
  755. }
  756. static int ath10k_sdio_mbox_proc_pending_irqs(struct ath10k *ar,
  757. bool *done)
  758. {
  759. u8 host_int_status;
  760. u32 lookahead;
  761. int ret;
  762. /* NOTE: HIF implementation guarantees that the context of this
  763. * call allows us to perform SYNCHRONOUS I/O, that is we can block,
  764. * sleep or call any API that can block or switch thread/task
  765. * contexts. This is a fully schedulable context.
  766. */
  767. ret = ath10k_sdio_mbox_read_int_status(ar,
  768. &host_int_status,
  769. &lookahead);
  770. if (ret) {
  771. *done = true;
  772. goto out;
  773. }
  774. if (!host_int_status && !lookahead) {
  775. ret = 0;
  776. *done = true;
  777. goto out;
  778. }
  779. if (lookahead) {
  780. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  781. "sdio pending mailbox msg lookahead 0x%08x\n",
  782. lookahead);
  783. ret = ath10k_sdio_mbox_rxmsg_pending_handler(ar,
  784. lookahead,
  785. done);
  786. if (ret)
  787. goto out;
  788. }
  789. /* now, handle the rest of the interrupts */
  790. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  791. "sdio host_int_status 0x%x\n", host_int_status);
  792. if (FIELD_GET(MBOX_HOST_INT_STATUS_CPU_MASK, host_int_status)) {
  793. /* CPU Interrupt */
  794. ret = ath10k_sdio_mbox_proc_cpu_intr(ar);
  795. if (ret)
  796. goto out;
  797. }
  798. if (FIELD_GET(MBOX_HOST_INT_STATUS_ERROR_MASK, host_int_status)) {
  799. /* Error Interrupt */
  800. ret = ath10k_sdio_mbox_proc_err_intr(ar);
  801. if (ret)
  802. goto out;
  803. }
  804. if (FIELD_GET(MBOX_HOST_INT_STATUS_COUNTER_MASK, host_int_status))
  805. /* Counter Interrupt */
  806. ret = ath10k_sdio_mbox_proc_counter_intr(ar);
  807. ret = 0;
  808. out:
  809. /* An optimization to bypass reading the IRQ status registers
  810. * unecessarily which can re-wake the target, if upper layers
  811. * determine that we are in a low-throughput mode, we can rely on
  812. * taking another interrupt rather than re-checking the status
  813. * registers which can re-wake the target.
  814. *
  815. * NOTE : for host interfaces that makes use of detecting pending
  816. * mbox messages at hif can not use this optimization due to
  817. * possible side effects, SPI requires the host to drain all
  818. * messages from the mailbox before exiting the ISR routine.
  819. */
  820. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  821. "sdio pending irqs done %d status %d",
  822. *done, ret);
  823. return ret;
  824. }
  825. static void ath10k_sdio_set_mbox_info(struct ath10k *ar)
  826. {
  827. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  828. struct ath10k_mbox_info *mbox_info = &ar_sdio->mbox_info;
  829. u16 device = ar_sdio->func->device, dev_id_base, dev_id_chiprev;
  830. mbox_info->htc_addr = ATH10K_HIF_MBOX_BASE_ADDR;
  831. mbox_info->block_size = ATH10K_HIF_MBOX_BLOCK_SIZE;
  832. mbox_info->block_mask = ATH10K_HIF_MBOX_BLOCK_SIZE - 1;
  833. mbox_info->gmbox_addr = ATH10K_HIF_GMBOX_BASE_ADDR;
  834. mbox_info->gmbox_sz = ATH10K_HIF_GMBOX_WIDTH;
  835. mbox_info->ext_info[0].htc_ext_addr = ATH10K_HIF_MBOX0_EXT_BASE_ADDR;
  836. dev_id_base = FIELD_GET(QCA_MANUFACTURER_ID_BASE, device);
  837. dev_id_chiprev = FIELD_GET(QCA_MANUFACTURER_ID_REV_MASK, device);
  838. switch (dev_id_base) {
  839. case QCA_MANUFACTURER_ID_AR6005_BASE:
  840. if (dev_id_chiprev < 4)
  841. mbox_info->ext_info[0].htc_ext_sz =
  842. ATH10K_HIF_MBOX0_EXT_WIDTH;
  843. else
  844. /* from QCA6174 2.0(0x504), the width has been extended
  845. * to 56K
  846. */
  847. mbox_info->ext_info[0].htc_ext_sz =
  848. ATH10K_HIF_MBOX0_EXT_WIDTH_ROME_2_0;
  849. break;
  850. case QCA_MANUFACTURER_ID_QCA9377_BASE:
  851. mbox_info->ext_info[0].htc_ext_sz =
  852. ATH10K_HIF_MBOX0_EXT_WIDTH_ROME_2_0;
  853. break;
  854. default:
  855. mbox_info->ext_info[0].htc_ext_sz =
  856. ATH10K_HIF_MBOX0_EXT_WIDTH;
  857. }
  858. mbox_info->ext_info[1].htc_ext_addr =
  859. mbox_info->ext_info[0].htc_ext_addr +
  860. mbox_info->ext_info[0].htc_ext_sz +
  861. ATH10K_HIF_MBOX_DUMMY_SPACE_SIZE;
  862. mbox_info->ext_info[1].htc_ext_sz = ATH10K_HIF_MBOX1_EXT_WIDTH;
  863. }
  864. /* BMI functions */
  865. static int ath10k_sdio_bmi_credits(struct ath10k *ar)
  866. {
  867. u32 addr, cmd_credits;
  868. unsigned long timeout;
  869. int ret;
  870. /* Read the counter register to get the command credits */
  871. addr = MBOX_COUNT_DEC_ADDRESS + ATH10K_HIF_MBOX_NUM_MAX * 4;
  872. timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
  873. cmd_credits = 0;
  874. while (time_before(jiffies, timeout) && !cmd_credits) {
  875. /* Hit the credit counter with a 4-byte access, the first byte
  876. * read will hit the counter and cause a decrement, while the
  877. * remaining 3 bytes has no effect. The rationale behind this
  878. * is to make all HIF accesses 4-byte aligned.
  879. */
  880. ret = ath10k_sdio_read32(ar, addr, &cmd_credits);
  881. if (ret) {
  882. ath10k_warn(ar,
  883. "unable to decrement the command credit count register: %d\n",
  884. ret);
  885. return ret;
  886. }
  887. /* The counter is only 8 bits.
  888. * Ignore anything in the upper 3 bytes
  889. */
  890. cmd_credits &= 0xFF;
  891. }
  892. if (!cmd_credits) {
  893. ath10k_warn(ar, "bmi communication timeout\n");
  894. return -ETIMEDOUT;
  895. }
  896. return 0;
  897. }
  898. static int ath10k_sdio_bmi_get_rx_lookahead(struct ath10k *ar)
  899. {
  900. unsigned long timeout;
  901. u32 rx_word;
  902. int ret;
  903. timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
  904. rx_word = 0;
  905. while ((time_before(jiffies, timeout)) && !rx_word) {
  906. ret = ath10k_sdio_read32(ar,
  907. MBOX_HOST_INT_STATUS_ADDRESS,
  908. &rx_word);
  909. if (ret) {
  910. ath10k_warn(ar, "unable to read RX_LOOKAHEAD_VALID: %d\n", ret);
  911. return ret;
  912. }
  913. /* all we really want is one bit */
  914. rx_word &= 1;
  915. }
  916. if (!rx_word) {
  917. ath10k_warn(ar, "bmi_recv_buf FIFO empty\n");
  918. return -EINVAL;
  919. }
  920. return ret;
  921. }
  922. static int ath10k_sdio_bmi_exchange_msg(struct ath10k *ar,
  923. void *req, u32 req_len,
  924. void *resp, u32 *resp_len)
  925. {
  926. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  927. u32 addr;
  928. int ret;
  929. if (req) {
  930. ret = ath10k_sdio_bmi_credits(ar);
  931. if (ret)
  932. return ret;
  933. addr = ar_sdio->mbox_info.htc_addr;
  934. memcpy(ar_sdio->bmi_buf, req, req_len);
  935. ret = ath10k_sdio_write(ar, addr, ar_sdio->bmi_buf, req_len);
  936. if (ret) {
  937. ath10k_warn(ar,
  938. "unable to send the bmi data to the device: %d\n",
  939. ret);
  940. return ret;
  941. }
  942. }
  943. if (!resp || !resp_len)
  944. /* No response expected */
  945. return 0;
  946. /* During normal bootup, small reads may be required.
  947. * Rather than issue an HIF Read and then wait as the Target
  948. * adds successive bytes to the FIFO, we wait here until
  949. * we know that response data is available.
  950. *
  951. * This allows us to cleanly timeout on an unexpected
  952. * Target failure rather than risk problems at the HIF level.
  953. * In particular, this avoids SDIO timeouts and possibly garbage
  954. * data on some host controllers. And on an interconnect
  955. * such as Compact Flash (as well as some SDIO masters) which
  956. * does not provide any indication on data timeout, it avoids
  957. * a potential hang or garbage response.
  958. *
  959. * Synchronization is more difficult for reads larger than the
  960. * size of the MBOX FIFO (128B), because the Target is unable
  961. * to push the 129th byte of data until AFTER the Host posts an
  962. * HIF Read and removes some FIFO data. So for large reads the
  963. * Host proceeds to post an HIF Read BEFORE all the data is
  964. * actually available to read. Fortunately, large BMI reads do
  965. * not occur in practice -- they're supported for debug/development.
  966. *
  967. * So Host/Target BMI synchronization is divided into these cases:
  968. * CASE 1: length < 4
  969. * Should not happen
  970. *
  971. * CASE 2: 4 <= length <= 128
  972. * Wait for first 4 bytes to be in FIFO
  973. * If CONSERVATIVE_BMI_READ is enabled, also wait for
  974. * a BMI command credit, which indicates that the ENTIRE
  975. * response is available in the the FIFO
  976. *
  977. * CASE 3: length > 128
  978. * Wait for the first 4 bytes to be in FIFO
  979. *
  980. * For most uses, a small timeout should be sufficient and we will
  981. * usually see a response quickly; but there may be some unusual
  982. * (debug) cases of BMI_EXECUTE where we want an larger timeout.
  983. * For now, we use an unbounded busy loop while waiting for
  984. * BMI_EXECUTE.
  985. *
  986. * If BMI_EXECUTE ever needs to support longer-latency execution,
  987. * especially in production, this code needs to be enhanced to sleep
  988. * and yield. Also note that BMI_COMMUNICATION_TIMEOUT is currently
  989. * a function of Host processor speed.
  990. */
  991. ret = ath10k_sdio_bmi_get_rx_lookahead(ar);
  992. if (ret)
  993. return ret;
  994. /* We always read from the start of the mbox address */
  995. addr = ar_sdio->mbox_info.htc_addr;
  996. ret = ath10k_sdio_read(ar, addr, ar_sdio->bmi_buf, *resp_len);
  997. if (ret) {
  998. ath10k_warn(ar,
  999. "unable to read the bmi data from the device: %d\n",
  1000. ret);
  1001. return ret;
  1002. }
  1003. memcpy(resp, ar_sdio->bmi_buf, *resp_len);
  1004. return 0;
  1005. }
  1006. /* sdio async handling functions */
  1007. static struct ath10k_sdio_bus_request
  1008. *ath10k_sdio_alloc_busreq(struct ath10k *ar)
  1009. {
  1010. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1011. struct ath10k_sdio_bus_request *bus_req;
  1012. spin_lock_bh(&ar_sdio->lock);
  1013. if (list_empty(&ar_sdio->bus_req_freeq)) {
  1014. bus_req = NULL;
  1015. goto out;
  1016. }
  1017. bus_req = list_first_entry(&ar_sdio->bus_req_freeq,
  1018. struct ath10k_sdio_bus_request, list);
  1019. list_del(&bus_req->list);
  1020. out:
  1021. spin_unlock_bh(&ar_sdio->lock);
  1022. return bus_req;
  1023. }
  1024. static void ath10k_sdio_free_bus_req(struct ath10k *ar,
  1025. struct ath10k_sdio_bus_request *bus_req)
  1026. {
  1027. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1028. memset(bus_req, 0, sizeof(*bus_req));
  1029. spin_lock_bh(&ar_sdio->lock);
  1030. list_add_tail(&bus_req->list, &ar_sdio->bus_req_freeq);
  1031. spin_unlock_bh(&ar_sdio->lock);
  1032. }
  1033. static void __ath10k_sdio_write_async(struct ath10k *ar,
  1034. struct ath10k_sdio_bus_request *req)
  1035. {
  1036. struct ath10k_htc_ep *ep;
  1037. struct sk_buff *skb;
  1038. int ret;
  1039. skb = req->skb;
  1040. ret = ath10k_sdio_write(ar, req->address, skb->data, skb->len);
  1041. if (ret)
  1042. ath10k_warn(ar, "failed to write skb to 0x%x asynchronously: %d",
  1043. req->address, ret);
  1044. if (req->htc_msg) {
  1045. ep = &ar->htc.endpoint[req->eid];
  1046. ath10k_htc_notify_tx_completion(ep, skb);
  1047. } else if (req->comp) {
  1048. complete(req->comp);
  1049. }
  1050. ath10k_sdio_free_bus_req(ar, req);
  1051. }
  1052. static void ath10k_sdio_write_async_work(struct work_struct *work)
  1053. {
  1054. struct ath10k_sdio *ar_sdio = container_of(work, struct ath10k_sdio,
  1055. wr_async_work);
  1056. struct ath10k *ar = ar_sdio->ar;
  1057. struct ath10k_sdio_bus_request *req, *tmp_req;
  1058. spin_lock_bh(&ar_sdio->wr_async_lock);
  1059. list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
  1060. list_del(&req->list);
  1061. spin_unlock_bh(&ar_sdio->wr_async_lock);
  1062. __ath10k_sdio_write_async(ar, req);
  1063. spin_lock_bh(&ar_sdio->wr_async_lock);
  1064. }
  1065. spin_unlock_bh(&ar_sdio->wr_async_lock);
  1066. }
  1067. static int ath10k_sdio_prep_async_req(struct ath10k *ar, u32 addr,
  1068. struct sk_buff *skb,
  1069. struct completion *comp,
  1070. bool htc_msg, enum ath10k_htc_ep_id eid)
  1071. {
  1072. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1073. struct ath10k_sdio_bus_request *bus_req;
  1074. /* Allocate a bus request for the message and queue it on the
  1075. * SDIO workqueue.
  1076. */
  1077. bus_req = ath10k_sdio_alloc_busreq(ar);
  1078. if (!bus_req) {
  1079. ath10k_warn(ar,
  1080. "unable to allocate bus request for async request\n");
  1081. return -ENOMEM;
  1082. }
  1083. bus_req->skb = skb;
  1084. bus_req->eid = eid;
  1085. bus_req->address = addr;
  1086. bus_req->htc_msg = htc_msg;
  1087. bus_req->comp = comp;
  1088. spin_lock_bh(&ar_sdio->wr_async_lock);
  1089. list_add_tail(&bus_req->list, &ar_sdio->wr_asyncq);
  1090. spin_unlock_bh(&ar_sdio->wr_async_lock);
  1091. return 0;
  1092. }
  1093. /* IRQ handler */
  1094. static void ath10k_sdio_irq_handler(struct sdio_func *func)
  1095. {
  1096. struct ath10k_sdio *ar_sdio = sdio_get_drvdata(func);
  1097. struct ath10k *ar = ar_sdio->ar;
  1098. unsigned long timeout;
  1099. bool done = false;
  1100. int ret;
  1101. /* Release the host during interrupts so we can pick it back up when
  1102. * we process commands.
  1103. */
  1104. sdio_release_host(ar_sdio->func);
  1105. timeout = jiffies + ATH10K_SDIO_HIF_COMMUNICATION_TIMEOUT_HZ;
  1106. do {
  1107. ret = ath10k_sdio_mbox_proc_pending_irqs(ar, &done);
  1108. if (ret)
  1109. break;
  1110. } while (time_before(jiffies, timeout) && !done);
  1111. ath10k_mac_tx_push_pending(ar);
  1112. sdio_claim_host(ar_sdio->func);
  1113. if (ret && ret != -ECANCELED)
  1114. ath10k_warn(ar, "failed to process pending SDIO interrupts: %d\n",
  1115. ret);
  1116. }
  1117. /* sdio HIF functions */
  1118. static int ath10k_sdio_hif_disable_intrs(struct ath10k *ar)
  1119. {
  1120. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1121. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  1122. struct ath10k_sdio_irq_enable_regs *regs = irq_data->irq_en_reg;
  1123. int ret;
  1124. mutex_lock(&irq_data->mtx);
  1125. memset(regs, 0, sizeof(*regs));
  1126. ret = ath10k_sdio_write(ar, MBOX_INT_STATUS_ENABLE_ADDRESS,
  1127. &regs->int_status_en, sizeof(*regs));
  1128. if (ret)
  1129. ath10k_warn(ar, "unable to disable sdio interrupts: %d\n", ret);
  1130. mutex_unlock(&irq_data->mtx);
  1131. return ret;
  1132. }
  1133. static int ath10k_sdio_hif_power_up(struct ath10k *ar)
  1134. {
  1135. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1136. struct sdio_func *func = ar_sdio->func;
  1137. int ret;
  1138. if (!ar_sdio->is_disabled)
  1139. return 0;
  1140. ath10k_dbg(ar, ATH10K_DBG_BOOT, "sdio power on\n");
  1141. sdio_claim_host(func);
  1142. ret = sdio_enable_func(func);
  1143. if (ret) {
  1144. ath10k_warn(ar, "unable to enable sdio function: %d)\n", ret);
  1145. sdio_release_host(func);
  1146. return ret;
  1147. }
  1148. sdio_release_host(func);
  1149. /* Wait for hardware to initialise. It should take a lot less than
  1150. * 20 ms but let's be conservative here.
  1151. */
  1152. msleep(20);
  1153. ar_sdio->is_disabled = false;
  1154. ret = ath10k_sdio_hif_disable_intrs(ar);
  1155. if (ret)
  1156. return ret;
  1157. return 0;
  1158. }
  1159. static void ath10k_sdio_hif_power_down(struct ath10k *ar)
  1160. {
  1161. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1162. int ret;
  1163. if (ar_sdio->is_disabled)
  1164. return;
  1165. ath10k_dbg(ar, ATH10K_DBG_BOOT, "sdio power off\n");
  1166. /* Disable the card */
  1167. sdio_claim_host(ar_sdio->func);
  1168. ret = sdio_disable_func(ar_sdio->func);
  1169. sdio_release_host(ar_sdio->func);
  1170. if (ret)
  1171. ath10k_warn(ar, "unable to disable sdio function: %d\n", ret);
  1172. ar_sdio->is_disabled = true;
  1173. }
  1174. static int ath10k_sdio_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
  1175. struct ath10k_hif_sg_item *items, int n_items)
  1176. {
  1177. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1178. enum ath10k_htc_ep_id eid;
  1179. struct sk_buff *skb;
  1180. int ret, i;
  1181. eid = pipe_id_to_eid(pipe_id);
  1182. for (i = 0; i < n_items; i++) {
  1183. size_t padded_len;
  1184. u32 address;
  1185. skb = items[i].transfer_context;
  1186. padded_len = ath10k_sdio_calc_txrx_padded_len(ar_sdio,
  1187. skb->len);
  1188. skb_trim(skb, padded_len);
  1189. /* Write TX data to the end of the mbox address space */
  1190. address = ar_sdio->mbox_addr[eid] + ar_sdio->mbox_size[eid] -
  1191. skb->len;
  1192. ret = ath10k_sdio_prep_async_req(ar, address, skb,
  1193. NULL, true, eid);
  1194. if (ret)
  1195. return ret;
  1196. }
  1197. queue_work(ar_sdio->workqueue, &ar_sdio->wr_async_work);
  1198. return 0;
  1199. }
  1200. static int ath10k_sdio_hif_enable_intrs(struct ath10k *ar)
  1201. {
  1202. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1203. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  1204. struct ath10k_sdio_irq_enable_regs *regs = irq_data->irq_en_reg;
  1205. int ret;
  1206. mutex_lock(&irq_data->mtx);
  1207. /* Enable all but CPU interrupts */
  1208. regs->int_status_en = FIELD_PREP(MBOX_INT_STATUS_ENABLE_ERROR_MASK, 1) |
  1209. FIELD_PREP(MBOX_INT_STATUS_ENABLE_CPU_MASK, 1) |
  1210. FIELD_PREP(MBOX_INT_STATUS_ENABLE_COUNTER_MASK, 1);
  1211. /* NOTE: There are some cases where HIF can do detection of
  1212. * pending mbox messages which is disabled now.
  1213. */
  1214. regs->int_status_en |=
  1215. FIELD_PREP(MBOX_INT_STATUS_ENABLE_MBOX_DATA_MASK, 1);
  1216. /* Set up the CPU Interrupt status Register */
  1217. regs->cpu_int_status_en = 0;
  1218. /* Set up the Error Interrupt status Register */
  1219. regs->err_int_status_en =
  1220. FIELD_PREP(MBOX_ERROR_STATUS_ENABLE_RX_UNDERFLOW_MASK, 1) |
  1221. FIELD_PREP(MBOX_ERROR_STATUS_ENABLE_TX_OVERFLOW_MASK, 1);
  1222. /* Enable Counter interrupt status register to get fatal errors for
  1223. * debugging.
  1224. */
  1225. regs->cntr_int_status_en =
  1226. FIELD_PREP(MBOX_COUNTER_INT_STATUS_ENABLE_BIT_MASK,
  1227. ATH10K_SDIO_TARGET_DEBUG_INTR_MASK);
  1228. ret = ath10k_sdio_write(ar, MBOX_INT_STATUS_ENABLE_ADDRESS,
  1229. &regs->int_status_en, sizeof(*regs));
  1230. if (ret)
  1231. ath10k_warn(ar,
  1232. "failed to update mbox interrupt status register : %d\n",
  1233. ret);
  1234. mutex_unlock(&irq_data->mtx);
  1235. return ret;
  1236. }
  1237. static int ath10k_sdio_hif_set_mbox_sleep(struct ath10k *ar, bool enable_sleep)
  1238. {
  1239. u32 val;
  1240. int ret;
  1241. ret = ath10k_sdio_read32(ar, ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL, &val);
  1242. if (ret) {
  1243. ath10k_warn(ar, "failed to read fifo/chip control register: %d\n",
  1244. ret);
  1245. return ret;
  1246. }
  1247. if (enable_sleep)
  1248. val &= ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL_DISABLE_SLEEP_OFF;
  1249. else
  1250. val |= ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL_DISABLE_SLEEP_ON;
  1251. ret = ath10k_sdio_write32(ar, ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL, val);
  1252. if (ret) {
  1253. ath10k_warn(ar, "failed to write to FIFO_TIMEOUT_AND_CHIP_CONTROL: %d",
  1254. ret);
  1255. return ret;
  1256. }
  1257. return 0;
  1258. }
  1259. /* HIF diagnostics */
  1260. static int ath10k_sdio_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
  1261. size_t buf_len)
  1262. {
  1263. int ret;
  1264. /* set window register to start read cycle */
  1265. ret = ath10k_sdio_write32(ar, MBOX_WINDOW_READ_ADDR_ADDRESS, address);
  1266. if (ret) {
  1267. ath10k_warn(ar, "failed to set mbox window read address: %d", ret);
  1268. return ret;
  1269. }
  1270. /* read the data */
  1271. ret = ath10k_sdio_read(ar, MBOX_WINDOW_DATA_ADDRESS, buf, buf_len);
  1272. if (ret) {
  1273. ath10k_warn(ar, "failed to read from mbox window data address: %d\n",
  1274. ret);
  1275. return ret;
  1276. }
  1277. return 0;
  1278. }
  1279. static int ath10k_sdio_hif_diag_read32(struct ath10k *ar, u32 address,
  1280. u32 *value)
  1281. {
  1282. __le32 *val;
  1283. int ret;
  1284. val = kzalloc(sizeof(*val), GFP_KERNEL);
  1285. if (!val)
  1286. return -ENOMEM;
  1287. ret = ath10k_sdio_hif_diag_read(ar, address, val, sizeof(*val));
  1288. if (ret)
  1289. goto out;
  1290. *value = __le32_to_cpu(*val);
  1291. out:
  1292. kfree(val);
  1293. return ret;
  1294. }
  1295. static int ath10k_sdio_hif_diag_write_mem(struct ath10k *ar, u32 address,
  1296. const void *data, int nbytes)
  1297. {
  1298. int ret;
  1299. /* set write data */
  1300. ret = ath10k_sdio_write(ar, MBOX_WINDOW_DATA_ADDRESS, data, nbytes);
  1301. if (ret) {
  1302. ath10k_warn(ar,
  1303. "failed to write 0x%p to mbox window data address: %d\n",
  1304. data, ret);
  1305. return ret;
  1306. }
  1307. /* set window register, which starts the write cycle */
  1308. ret = ath10k_sdio_write32(ar, MBOX_WINDOW_WRITE_ADDR_ADDRESS, address);
  1309. if (ret) {
  1310. ath10k_warn(ar, "failed to set mbox window write address: %d", ret);
  1311. return ret;
  1312. }
  1313. return 0;
  1314. }
  1315. /* HIF start/stop */
  1316. static int ath10k_sdio_hif_start(struct ath10k *ar)
  1317. {
  1318. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1319. u32 addr, val;
  1320. int ret;
  1321. /* Sleep 20 ms before HIF interrupts are disabled.
  1322. * This will give target plenty of time to process the BMI done
  1323. * request before interrupts are disabled.
  1324. */
  1325. msleep(20);
  1326. ret = ath10k_sdio_hif_disable_intrs(ar);
  1327. if (ret)
  1328. return ret;
  1329. /* eid 0 always uses the lower part of the extended mailbox address
  1330. * space (ext_info[0].htc_ext_addr).
  1331. */
  1332. ar_sdio->mbox_addr[0] = ar_sdio->mbox_info.ext_info[0].htc_ext_addr;
  1333. ar_sdio->mbox_size[0] = ar_sdio->mbox_info.ext_info[0].htc_ext_sz;
  1334. sdio_claim_host(ar_sdio->func);
  1335. /* Register the isr */
  1336. ret = sdio_claim_irq(ar_sdio->func, ath10k_sdio_irq_handler);
  1337. if (ret) {
  1338. ath10k_warn(ar, "failed to claim sdio interrupt: %d\n", ret);
  1339. sdio_release_host(ar_sdio->func);
  1340. return ret;
  1341. }
  1342. sdio_release_host(ar_sdio->func);
  1343. ret = ath10k_sdio_hif_enable_intrs(ar);
  1344. if (ret)
  1345. ath10k_warn(ar, "failed to enable sdio interrupts: %d\n", ret);
  1346. addr = host_interest_item_address(HI_ITEM(hi_acs_flags));
  1347. ret = ath10k_sdio_hif_diag_read32(ar, addr, &val);
  1348. if (ret) {
  1349. ath10k_warn(ar, "unable to read hi_acs_flags address: %d\n", ret);
  1350. return ret;
  1351. }
  1352. if (val & HI_ACS_FLAGS_SDIO_SWAP_MAILBOX_FW_ACK) {
  1353. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  1354. "sdio mailbox swap service enabled\n");
  1355. ar_sdio->swap_mbox = true;
  1356. }
  1357. /* Enable sleep and then disable it again */
  1358. ret = ath10k_sdio_hif_set_mbox_sleep(ar, true);
  1359. if (ret)
  1360. return ret;
  1361. /* Wait for 20ms for the written value to take effect */
  1362. msleep(20);
  1363. ret = ath10k_sdio_hif_set_mbox_sleep(ar, false);
  1364. if (ret)
  1365. return ret;
  1366. return 0;
  1367. }
  1368. #define SDIO_IRQ_DISABLE_TIMEOUT_HZ (3 * HZ)
  1369. static void ath10k_sdio_irq_disable(struct ath10k *ar)
  1370. {
  1371. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1372. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  1373. struct ath10k_sdio_irq_enable_regs *regs = irq_data->irq_en_reg;
  1374. struct sk_buff *skb;
  1375. struct completion irqs_disabled_comp;
  1376. int ret;
  1377. skb = dev_alloc_skb(sizeof(*regs));
  1378. if (!skb)
  1379. return;
  1380. mutex_lock(&irq_data->mtx);
  1381. memset(regs, 0, sizeof(*regs)); /* disable all interrupts */
  1382. memcpy(skb->data, regs, sizeof(*regs));
  1383. skb_put(skb, sizeof(*regs));
  1384. mutex_unlock(&irq_data->mtx);
  1385. init_completion(&irqs_disabled_comp);
  1386. ret = ath10k_sdio_prep_async_req(ar, MBOX_INT_STATUS_ENABLE_ADDRESS,
  1387. skb, &irqs_disabled_comp, false, 0);
  1388. if (ret)
  1389. goto out;
  1390. queue_work(ar_sdio->workqueue, &ar_sdio->wr_async_work);
  1391. /* Wait for the completion of the IRQ disable request.
  1392. * If there is a timeout we will try to disable irq's anyway.
  1393. */
  1394. ret = wait_for_completion_timeout(&irqs_disabled_comp,
  1395. SDIO_IRQ_DISABLE_TIMEOUT_HZ);
  1396. if (!ret)
  1397. ath10k_warn(ar, "sdio irq disable request timed out\n");
  1398. sdio_claim_host(ar_sdio->func);
  1399. ret = sdio_release_irq(ar_sdio->func);
  1400. if (ret)
  1401. ath10k_warn(ar, "failed to release sdio interrupt: %d\n", ret);
  1402. sdio_release_host(ar_sdio->func);
  1403. out:
  1404. kfree_skb(skb);
  1405. }
  1406. static void ath10k_sdio_hif_stop(struct ath10k *ar)
  1407. {
  1408. struct ath10k_sdio_bus_request *req, *tmp_req;
  1409. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1410. ath10k_sdio_irq_disable(ar);
  1411. cancel_work_sync(&ar_sdio->wr_async_work);
  1412. spin_lock_bh(&ar_sdio->wr_async_lock);
  1413. /* Free all bus requests that have not been handled */
  1414. list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
  1415. struct ath10k_htc_ep *ep;
  1416. list_del(&req->list);
  1417. if (req->htc_msg) {
  1418. ep = &ar->htc.endpoint[req->eid];
  1419. ath10k_htc_notify_tx_completion(ep, req->skb);
  1420. } else if (req->skb) {
  1421. kfree_skb(req->skb);
  1422. }
  1423. ath10k_sdio_free_bus_req(ar, req);
  1424. }
  1425. spin_unlock_bh(&ar_sdio->wr_async_lock);
  1426. }
  1427. #ifdef CONFIG_PM
  1428. static int ath10k_sdio_hif_suspend(struct ath10k *ar)
  1429. {
  1430. return -EOPNOTSUPP;
  1431. }
  1432. static int ath10k_sdio_hif_resume(struct ath10k *ar)
  1433. {
  1434. switch (ar->state) {
  1435. case ATH10K_STATE_OFF:
  1436. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  1437. "sdio resume configuring sdio\n");
  1438. /* need to set sdio settings after power is cut from sdio */
  1439. ath10k_sdio_config(ar);
  1440. break;
  1441. case ATH10K_STATE_ON:
  1442. default:
  1443. break;
  1444. }
  1445. return 0;
  1446. }
  1447. #endif
  1448. static int ath10k_sdio_hif_map_service_to_pipe(struct ath10k *ar,
  1449. u16 service_id,
  1450. u8 *ul_pipe, u8 *dl_pipe)
  1451. {
  1452. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1453. struct ath10k_htc *htc = &ar->htc;
  1454. u32 htt_addr, wmi_addr, htt_mbox_size, wmi_mbox_size;
  1455. enum ath10k_htc_ep_id eid;
  1456. bool ep_found = false;
  1457. int i;
  1458. /* For sdio, we are interested in the mapping between eid
  1459. * and pipeid rather than service_id to pipe_id.
  1460. * First we find out which eid has been allocated to the
  1461. * service...
  1462. */
  1463. for (i = 0; i < ATH10K_HTC_EP_COUNT; i++) {
  1464. if (htc->endpoint[i].service_id == service_id) {
  1465. eid = htc->endpoint[i].eid;
  1466. ep_found = true;
  1467. break;
  1468. }
  1469. }
  1470. if (!ep_found)
  1471. return -EINVAL;
  1472. /* Then we create the simplest mapping possible between pipeid
  1473. * and eid
  1474. */
  1475. *ul_pipe = *dl_pipe = (u8)eid;
  1476. /* Normally, HTT will use the upper part of the extended
  1477. * mailbox address space (ext_info[1].htc_ext_addr) and WMI ctrl
  1478. * the lower part (ext_info[0].htc_ext_addr).
  1479. * If fw wants swapping of mailbox addresses, the opposite is true.
  1480. */
  1481. if (ar_sdio->swap_mbox) {
  1482. htt_addr = ar_sdio->mbox_info.ext_info[0].htc_ext_addr;
  1483. wmi_addr = ar_sdio->mbox_info.ext_info[1].htc_ext_addr;
  1484. htt_mbox_size = ar_sdio->mbox_info.ext_info[0].htc_ext_sz;
  1485. wmi_mbox_size = ar_sdio->mbox_info.ext_info[1].htc_ext_sz;
  1486. } else {
  1487. htt_addr = ar_sdio->mbox_info.ext_info[1].htc_ext_addr;
  1488. wmi_addr = ar_sdio->mbox_info.ext_info[0].htc_ext_addr;
  1489. htt_mbox_size = ar_sdio->mbox_info.ext_info[1].htc_ext_sz;
  1490. wmi_mbox_size = ar_sdio->mbox_info.ext_info[0].htc_ext_sz;
  1491. }
  1492. switch (service_id) {
  1493. case ATH10K_HTC_SVC_ID_RSVD_CTRL:
  1494. /* HTC ctrl ep mbox address has already been setup in
  1495. * ath10k_sdio_hif_start
  1496. */
  1497. break;
  1498. case ATH10K_HTC_SVC_ID_WMI_CONTROL:
  1499. ar_sdio->mbox_addr[eid] = wmi_addr;
  1500. ar_sdio->mbox_size[eid] = wmi_mbox_size;
  1501. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  1502. "sdio wmi ctrl mbox_addr 0x%x mbox_size %d\n",
  1503. ar_sdio->mbox_addr[eid], ar_sdio->mbox_size[eid]);
  1504. break;
  1505. case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
  1506. ar_sdio->mbox_addr[eid] = htt_addr;
  1507. ar_sdio->mbox_size[eid] = htt_mbox_size;
  1508. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  1509. "sdio htt data mbox_addr 0x%x mbox_size %d\n",
  1510. ar_sdio->mbox_addr[eid], ar_sdio->mbox_size[eid]);
  1511. break;
  1512. default:
  1513. ath10k_warn(ar, "unsupported HTC service id: %d\n",
  1514. service_id);
  1515. return -EINVAL;
  1516. }
  1517. return 0;
  1518. }
  1519. static void ath10k_sdio_hif_get_default_pipe(struct ath10k *ar,
  1520. u8 *ul_pipe, u8 *dl_pipe)
  1521. {
  1522. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio hif get default pipe\n");
  1523. /* HTC ctrl ep (SVC id 1) always has eid (and pipe_id in our
  1524. * case) == 0
  1525. */
  1526. *ul_pipe = 0;
  1527. *dl_pipe = 0;
  1528. }
  1529. /* This op is currently only used by htc_wait_target if the HTC ready
  1530. * message times out. It is not applicable for SDIO since there is nothing
  1531. * we can do if the HTC ready message does not arrive in time.
  1532. * TODO: Make this op non mandatory by introducing a NULL check in the
  1533. * hif op wrapper.
  1534. */
  1535. static void ath10k_sdio_hif_send_complete_check(struct ath10k *ar,
  1536. u8 pipe, int force)
  1537. {
  1538. }
  1539. static const struct ath10k_hif_ops ath10k_sdio_hif_ops = {
  1540. .tx_sg = ath10k_sdio_hif_tx_sg,
  1541. .diag_read = ath10k_sdio_hif_diag_read,
  1542. .diag_write = ath10k_sdio_hif_diag_write_mem,
  1543. .exchange_bmi_msg = ath10k_sdio_bmi_exchange_msg,
  1544. .start = ath10k_sdio_hif_start,
  1545. .stop = ath10k_sdio_hif_stop,
  1546. .map_service_to_pipe = ath10k_sdio_hif_map_service_to_pipe,
  1547. .get_default_pipe = ath10k_sdio_hif_get_default_pipe,
  1548. .send_complete_check = ath10k_sdio_hif_send_complete_check,
  1549. .power_up = ath10k_sdio_hif_power_up,
  1550. .power_down = ath10k_sdio_hif_power_down,
  1551. #ifdef CONFIG_PM
  1552. .suspend = ath10k_sdio_hif_suspend,
  1553. .resume = ath10k_sdio_hif_resume,
  1554. #endif
  1555. };
  1556. #ifdef CONFIG_PM_SLEEP
  1557. /* Empty handlers so that mmc subsystem doesn't remove us entirely during
  1558. * suspend. We instead follow cfg80211 suspend/resume handlers.
  1559. */
  1560. static int ath10k_sdio_pm_suspend(struct device *device)
  1561. {
  1562. return 0;
  1563. }
  1564. static int ath10k_sdio_pm_resume(struct device *device)
  1565. {
  1566. return 0;
  1567. }
  1568. static SIMPLE_DEV_PM_OPS(ath10k_sdio_pm_ops, ath10k_sdio_pm_suspend,
  1569. ath10k_sdio_pm_resume);
  1570. #define ATH10K_SDIO_PM_OPS (&ath10k_sdio_pm_ops)
  1571. #else
  1572. #define ATH10K_SDIO_PM_OPS NULL
  1573. #endif /* CONFIG_PM_SLEEP */
  1574. static int ath10k_sdio_probe(struct sdio_func *func,
  1575. const struct sdio_device_id *id)
  1576. {
  1577. struct ath10k_sdio *ar_sdio;
  1578. struct ath10k *ar;
  1579. enum ath10k_hw_rev hw_rev;
  1580. u32 chip_id, dev_id_base;
  1581. int ret, i;
  1582. /* Assumption: All SDIO based chipsets (so far) are QCA6174 based.
  1583. * If there will be newer chipsets that does not use the hw reg
  1584. * setup as defined in qca6174_regs and qca6174_values, this
  1585. * assumption is no longer valid and hw_rev must be setup differently
  1586. * depending on chipset.
  1587. */
  1588. hw_rev = ATH10K_HW_QCA6174;
  1589. ar = ath10k_core_create(sizeof(*ar_sdio), &func->dev, ATH10K_BUS_SDIO,
  1590. hw_rev, &ath10k_sdio_hif_ops);
  1591. if (!ar) {
  1592. dev_err(&func->dev, "failed to allocate core\n");
  1593. return -ENOMEM;
  1594. }
  1595. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  1596. "sdio new func %d vendor 0x%x device 0x%x block 0x%x/0x%x\n",
  1597. func->num, func->vendor, func->device,
  1598. func->max_blksize, func->cur_blksize);
  1599. ar_sdio = ath10k_sdio_priv(ar);
  1600. ar_sdio->irq_data.irq_proc_reg =
  1601. devm_kzalloc(ar->dev, sizeof(struct ath10k_sdio_irq_proc_regs),
  1602. GFP_KERNEL);
  1603. if (!ar_sdio->irq_data.irq_proc_reg) {
  1604. ret = -ENOMEM;
  1605. goto err_core_destroy;
  1606. }
  1607. ar_sdio->irq_data.irq_en_reg =
  1608. devm_kzalloc(ar->dev, sizeof(struct ath10k_sdio_irq_enable_regs),
  1609. GFP_KERNEL);
  1610. if (!ar_sdio->irq_data.irq_en_reg) {
  1611. ret = -ENOMEM;
  1612. goto err_core_destroy;
  1613. }
  1614. ar_sdio->bmi_buf = devm_kzalloc(ar->dev, BMI_MAX_CMDBUF_SIZE, GFP_KERNEL);
  1615. if (!ar_sdio->bmi_buf) {
  1616. ret = -ENOMEM;
  1617. goto err_core_destroy;
  1618. }
  1619. ar_sdio->func = func;
  1620. sdio_set_drvdata(func, ar_sdio);
  1621. ar_sdio->is_disabled = true;
  1622. ar_sdio->ar = ar;
  1623. spin_lock_init(&ar_sdio->lock);
  1624. spin_lock_init(&ar_sdio->wr_async_lock);
  1625. mutex_init(&ar_sdio->irq_data.mtx);
  1626. INIT_LIST_HEAD(&ar_sdio->bus_req_freeq);
  1627. INIT_LIST_HEAD(&ar_sdio->wr_asyncq);
  1628. INIT_WORK(&ar_sdio->wr_async_work, ath10k_sdio_write_async_work);
  1629. ar_sdio->workqueue = create_singlethread_workqueue("ath10k_sdio_wq");
  1630. if (!ar_sdio->workqueue) {
  1631. ret = -ENOMEM;
  1632. goto err_core_destroy;
  1633. }
  1634. for (i = 0; i < ATH10K_SDIO_BUS_REQUEST_MAX_NUM; i++)
  1635. ath10k_sdio_free_bus_req(ar, &ar_sdio->bus_req[i]);
  1636. dev_id_base = FIELD_GET(QCA_MANUFACTURER_ID_BASE, id->device);
  1637. switch (dev_id_base) {
  1638. case QCA_MANUFACTURER_ID_AR6005_BASE:
  1639. case QCA_MANUFACTURER_ID_QCA9377_BASE:
  1640. ar->dev_id = QCA9377_1_0_DEVICE_ID;
  1641. break;
  1642. default:
  1643. ret = -ENODEV;
  1644. ath10k_err(ar, "unsupported device id %u (0x%x)\n",
  1645. dev_id_base, id->device);
  1646. goto err_free_wq;
  1647. }
  1648. ar->id.vendor = id->vendor;
  1649. ar->id.device = id->device;
  1650. ath10k_sdio_set_mbox_info(ar);
  1651. ret = ath10k_sdio_config(ar);
  1652. if (ret) {
  1653. ath10k_err(ar, "failed to config sdio: %d\n", ret);
  1654. goto err_free_wq;
  1655. }
  1656. /* TODO: don't know yet how to get chip_id with SDIO */
  1657. chip_id = 0;
  1658. ret = ath10k_core_register(ar, chip_id);
  1659. if (ret) {
  1660. ath10k_err(ar, "failed to register driver core: %d\n", ret);
  1661. goto err_free_wq;
  1662. }
  1663. /* TODO: remove this once SDIO support is fully implemented */
  1664. ath10k_warn(ar, "WARNING: ath10k SDIO support is incomplete, don't expect anything to work!\n");
  1665. return 0;
  1666. err_free_wq:
  1667. destroy_workqueue(ar_sdio->workqueue);
  1668. err_core_destroy:
  1669. ath10k_core_destroy(ar);
  1670. return ret;
  1671. }
  1672. static void ath10k_sdio_remove(struct sdio_func *func)
  1673. {
  1674. struct ath10k_sdio *ar_sdio = sdio_get_drvdata(func);
  1675. struct ath10k *ar = ar_sdio->ar;
  1676. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  1677. "sdio removed func %d vendor 0x%x device 0x%x\n",
  1678. func->num, func->vendor, func->device);
  1679. (void)ath10k_sdio_hif_disable_intrs(ar);
  1680. cancel_work_sync(&ar_sdio->wr_async_work);
  1681. ath10k_core_unregister(ar);
  1682. ath10k_core_destroy(ar);
  1683. }
  1684. static const struct sdio_device_id ath10k_sdio_devices[] = {
  1685. {SDIO_DEVICE(QCA_MANUFACTURER_CODE,
  1686. (QCA_SDIO_ID_AR6005_BASE | 0xA))},
  1687. {SDIO_DEVICE(QCA_MANUFACTURER_CODE,
  1688. (QCA_SDIO_ID_QCA9377_BASE | 0x1))},
  1689. {},
  1690. };
  1691. MODULE_DEVICE_TABLE(sdio, ath10k_sdio_devices);
  1692. static struct sdio_driver ath10k_sdio_driver = {
  1693. .name = "ath10k_sdio",
  1694. .id_table = ath10k_sdio_devices,
  1695. .probe = ath10k_sdio_probe,
  1696. .remove = ath10k_sdio_remove,
  1697. .drv.pm = ATH10K_SDIO_PM_OPS,
  1698. };
  1699. static int __init ath10k_sdio_init(void)
  1700. {
  1701. int ret;
  1702. ret = sdio_register_driver(&ath10k_sdio_driver);
  1703. if (ret)
  1704. pr_err("sdio driver registration failed: %d\n", ret);
  1705. return ret;
  1706. }
  1707. static void __exit ath10k_sdio_exit(void)
  1708. {
  1709. sdio_unregister_driver(&ath10k_sdio_driver);
  1710. }
  1711. module_init(ath10k_sdio_init);
  1712. module_exit(ath10k_sdio_exit);
  1713. MODULE_AUTHOR("Qualcomm Atheros");
  1714. MODULE_DESCRIPTION("Driver support for Qualcomm Atheros 802.11ac WLAN SDIO devices");
  1715. MODULE_LICENSE("Dual BSD/GPL");