sdio.c 54 KB

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