sdio.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677
  1. // SPDX-License-Identifier: ISC
  2. /*
  3. * Copyright (c) 2004-2011 Atheros Communications Inc.
  4. * Copyright (c) 2011-2012,2017 Qualcomm Atheros, Inc.
  5. * Copyright (c) 2016-2017 Erik Stromdahl <erik.stromdahl@gmail.com>
  6. * Copyright (c) 2022-2024 Qualcomm Innovation Center, Inc. All rights reserved.
  7. */
  8. #include <linux/module.h>
  9. #include <linux/mmc/card.h>
  10. #include <linux/mmc/mmc.h>
  11. #include <linux/mmc/host.h>
  12. #include <linux/mmc/sdio_func.h>
  13. #include <linux/mmc/sdio_ids.h>
  14. #include <linux/mmc/sdio.h>
  15. #include <linux/mmc/sd.h>
  16. #include <linux/bitfield.h>
  17. #include "core.h"
  18. #include "bmi.h"
  19. #include "debug.h"
  20. #include "hif.h"
  21. #include "htc.h"
  22. #include "mac.h"
  23. #include "targaddrs.h"
  24. #include "trace.h"
  25. #include "sdio.h"
  26. #include "coredump.h"
  27. void ath10k_sdio_fw_crashed_dump(struct ath10k *ar);
  28. #define ATH10K_SDIO_VSG_BUF_SIZE (64 * 1024)
  29. /* inlined helper functions */
  30. static inline int ath10k_sdio_calc_txrx_padded_len(struct ath10k_sdio *ar_sdio,
  31. size_t len)
  32. {
  33. return __ALIGN_MASK((len), ar_sdio->mbox_info.block_mask);
  34. }
  35. static inline enum ath10k_htc_ep_id pipe_id_to_eid(u8 pipe_id)
  36. {
  37. return (enum ath10k_htc_ep_id)pipe_id;
  38. }
  39. static inline void ath10k_sdio_mbox_free_rx_pkt(struct ath10k_sdio_rx_data *pkt)
  40. {
  41. dev_kfree_skb(pkt->skb);
  42. pkt->skb = NULL;
  43. pkt->alloc_len = 0;
  44. pkt->act_len = 0;
  45. pkt->trailer_only = false;
  46. }
  47. static inline int ath10k_sdio_mbox_alloc_rx_pkt(struct ath10k_sdio_rx_data *pkt,
  48. size_t act_len, size_t full_len,
  49. bool part_of_bundle,
  50. bool last_in_bundle)
  51. {
  52. pkt->skb = dev_alloc_skb(full_len);
  53. if (!pkt->skb)
  54. return -ENOMEM;
  55. pkt->act_len = act_len;
  56. pkt->alloc_len = full_len;
  57. pkt->part_of_bundle = part_of_bundle;
  58. pkt->last_in_bundle = last_in_bundle;
  59. pkt->trailer_only = false;
  60. return 0;
  61. }
  62. static inline bool is_trailer_only_msg(struct ath10k_sdio_rx_data *pkt)
  63. {
  64. bool trailer_only = false;
  65. struct ath10k_htc_hdr *htc_hdr =
  66. (struct ath10k_htc_hdr *)pkt->skb->data;
  67. u16 len = __le16_to_cpu(htc_hdr->len);
  68. if (len == htc_hdr->trailer_len)
  69. trailer_only = true;
  70. return trailer_only;
  71. }
  72. /* sdio/mmc functions */
  73. static inline void ath10k_sdio_set_cmd52_arg(u32 *arg, u8 write, u8 raw,
  74. unsigned int address,
  75. unsigned char val)
  76. {
  77. *arg = FIELD_PREP(BIT(31), write) |
  78. FIELD_PREP(BIT(27), raw) |
  79. FIELD_PREP(BIT(26), 1) |
  80. FIELD_PREP(GENMASK(25, 9), address) |
  81. FIELD_PREP(BIT(8), 1) |
  82. FIELD_PREP(GENMASK(7, 0), val);
  83. }
  84. static int ath10k_sdio_func0_cmd52_wr_byte(struct mmc_card *card,
  85. unsigned int address,
  86. unsigned char byte)
  87. {
  88. struct mmc_command io_cmd;
  89. memset(&io_cmd, 0, sizeof(io_cmd));
  90. ath10k_sdio_set_cmd52_arg(&io_cmd.arg, 1, 0, address, byte);
  91. io_cmd.opcode = SD_IO_RW_DIRECT;
  92. io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
  93. return mmc_wait_for_cmd(card->host, &io_cmd, 0);
  94. }
  95. static int ath10k_sdio_func0_cmd52_rd_byte(struct mmc_card *card,
  96. unsigned int address,
  97. unsigned char *byte)
  98. {
  99. struct mmc_command io_cmd;
  100. int ret;
  101. memset(&io_cmd, 0, sizeof(io_cmd));
  102. ath10k_sdio_set_cmd52_arg(&io_cmd.arg, 0, 0, address, 0);
  103. io_cmd.opcode = SD_IO_RW_DIRECT;
  104. io_cmd.flags = MMC_RSP_R5 | MMC_CMD_AC;
  105. ret = mmc_wait_for_cmd(card->host, &io_cmd, 0);
  106. if (!ret)
  107. *byte = io_cmd.resp[0];
  108. return ret;
  109. }
  110. static int ath10k_sdio_config(struct ath10k *ar)
  111. {
  112. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  113. struct sdio_func *func = ar_sdio->func;
  114. unsigned char byte, asyncintdelay = 2;
  115. int ret;
  116. ath10k_dbg(ar, ATH10K_DBG_BOOT, "sdio configuration\n");
  117. sdio_claim_host(func);
  118. byte = 0;
  119. ret = ath10k_sdio_func0_cmd52_rd_byte(func->card,
  120. SDIO_CCCR_DRIVE_STRENGTH,
  121. &byte);
  122. byte &= ~ATH10K_SDIO_DRIVE_DTSX_MASK;
  123. byte |= FIELD_PREP(ATH10K_SDIO_DRIVE_DTSX_MASK,
  124. ATH10K_SDIO_DRIVE_DTSX_TYPE_D);
  125. ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
  126. SDIO_CCCR_DRIVE_STRENGTH,
  127. byte);
  128. byte = 0;
  129. ret = ath10k_sdio_func0_cmd52_rd_byte(
  130. func->card,
  131. CCCR_SDIO_DRIVER_STRENGTH_ENABLE_ADDR,
  132. &byte);
  133. byte |= (CCCR_SDIO_DRIVER_STRENGTH_ENABLE_A |
  134. CCCR_SDIO_DRIVER_STRENGTH_ENABLE_C |
  135. CCCR_SDIO_DRIVER_STRENGTH_ENABLE_D);
  136. ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
  137. CCCR_SDIO_DRIVER_STRENGTH_ENABLE_ADDR,
  138. byte);
  139. if (ret) {
  140. ath10k_warn(ar, "failed to enable driver strength: %d\n", ret);
  141. goto out;
  142. }
  143. byte = 0;
  144. ret = ath10k_sdio_func0_cmd52_rd_byte(func->card,
  145. CCCR_SDIO_IRQ_MODE_REG_SDIO3,
  146. &byte);
  147. byte |= SDIO_IRQ_MODE_ASYNC_4BIT_IRQ_SDIO3;
  148. ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
  149. CCCR_SDIO_IRQ_MODE_REG_SDIO3,
  150. byte);
  151. if (ret) {
  152. ath10k_warn(ar, "failed to enable 4-bit async irq mode: %d\n",
  153. ret);
  154. goto out;
  155. }
  156. byte = 0;
  157. ret = ath10k_sdio_func0_cmd52_rd_byte(func->card,
  158. CCCR_SDIO_ASYNC_INT_DELAY_ADDRESS,
  159. &byte);
  160. byte &= ~CCCR_SDIO_ASYNC_INT_DELAY_MASK;
  161. byte |= FIELD_PREP(CCCR_SDIO_ASYNC_INT_DELAY_MASK, asyncintdelay);
  162. ret = ath10k_sdio_func0_cmd52_wr_byte(func->card,
  163. CCCR_SDIO_ASYNC_INT_DELAY_ADDRESS,
  164. byte);
  165. /* give us some time to enable, in ms */
  166. func->enable_timeout = 100;
  167. ret = sdio_set_block_size(func, ar_sdio->mbox_info.block_size);
  168. if (ret) {
  169. ath10k_warn(ar, "failed to set sdio block size to %d: %d\n",
  170. ar_sdio->mbox_info.block_size, ret);
  171. goto out;
  172. }
  173. out:
  174. sdio_release_host(func);
  175. return ret;
  176. }
  177. static int ath10k_sdio_write32(struct ath10k *ar, u32 addr, u32 val)
  178. {
  179. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  180. struct sdio_func *func = ar_sdio->func;
  181. int ret;
  182. sdio_claim_host(func);
  183. sdio_writel(func, val, addr, &ret);
  184. if (ret) {
  185. ath10k_warn(ar, "failed to write 0x%x to address 0x%x: %d\n",
  186. val, addr, ret);
  187. goto out;
  188. }
  189. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio write32 addr 0x%x val 0x%x\n",
  190. addr, val);
  191. out:
  192. sdio_release_host(func);
  193. return ret;
  194. }
  195. static int ath10k_sdio_writesb32(struct ath10k *ar, u32 addr, u32 val)
  196. {
  197. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  198. struct sdio_func *func = ar_sdio->func;
  199. __le32 *buf;
  200. int ret;
  201. buf = kzalloc(sizeof(*buf), GFP_KERNEL);
  202. if (!buf)
  203. return -ENOMEM;
  204. *buf = cpu_to_le32(val);
  205. sdio_claim_host(func);
  206. ret = sdio_writesb(func, addr, buf, sizeof(*buf));
  207. if (ret) {
  208. ath10k_warn(ar, "failed to write value 0x%x to fixed sb address 0x%x: %d\n",
  209. val, addr, ret);
  210. goto out;
  211. }
  212. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio writesb32 addr 0x%x val 0x%x\n",
  213. addr, val);
  214. out:
  215. sdio_release_host(func);
  216. kfree(buf);
  217. return ret;
  218. }
  219. static int ath10k_sdio_read32(struct ath10k *ar, u32 addr, u32 *val)
  220. {
  221. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  222. struct sdio_func *func = ar_sdio->func;
  223. int ret;
  224. sdio_claim_host(func);
  225. *val = sdio_readl(func, addr, &ret);
  226. if (ret) {
  227. ath10k_warn(ar, "failed to read from address 0x%x: %d\n",
  228. addr, ret);
  229. goto out;
  230. }
  231. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio read32 addr 0x%x val 0x%x\n",
  232. addr, *val);
  233. out:
  234. sdio_release_host(func);
  235. return ret;
  236. }
  237. static int ath10k_sdio_read(struct ath10k *ar, u32 addr, void *buf, size_t len)
  238. {
  239. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  240. struct sdio_func *func = ar_sdio->func;
  241. int ret;
  242. sdio_claim_host(func);
  243. ret = sdio_memcpy_fromio(func, buf, addr, len);
  244. if (ret) {
  245. ath10k_warn(ar, "failed to read from address 0x%x: %d\n",
  246. addr, ret);
  247. goto out;
  248. }
  249. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio read addr 0x%x buf 0x%p len %zu\n",
  250. addr, buf, len);
  251. ath10k_dbg_dump(ar, ATH10K_DBG_SDIO_DUMP, NULL, "sdio read ", buf, len);
  252. out:
  253. sdio_release_host(func);
  254. return ret;
  255. }
  256. static int ath10k_sdio_write(struct ath10k *ar, u32 addr, const void *buf, size_t len)
  257. {
  258. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  259. struct sdio_func *func = ar_sdio->func;
  260. int ret;
  261. sdio_claim_host(func);
  262. /* For some reason toio() doesn't have const for the buffer, need
  263. * an ugly hack to workaround that.
  264. */
  265. ret = sdio_memcpy_toio(func, addr, (void *)buf, len);
  266. if (ret) {
  267. ath10k_warn(ar, "failed to write to address 0x%x: %d\n",
  268. addr, ret);
  269. goto out;
  270. }
  271. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio write addr 0x%x buf 0x%p len %zu\n",
  272. addr, buf, len);
  273. ath10k_dbg_dump(ar, ATH10K_DBG_SDIO_DUMP, NULL, "sdio write ", buf, len);
  274. out:
  275. sdio_release_host(func);
  276. return ret;
  277. }
  278. static int ath10k_sdio_readsb(struct ath10k *ar, u32 addr, void *buf, size_t len)
  279. {
  280. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  281. struct sdio_func *func = ar_sdio->func;
  282. int ret;
  283. sdio_claim_host(func);
  284. len = round_down(len, ar_sdio->mbox_info.block_size);
  285. ret = sdio_readsb(func, buf, addr, len);
  286. if (ret) {
  287. ath10k_warn(ar, "failed to read from fixed (sb) address 0x%x: %d\n",
  288. addr, ret);
  289. goto out;
  290. }
  291. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio readsb addr 0x%x buf 0x%p len %zu\n",
  292. addr, buf, len);
  293. ath10k_dbg_dump(ar, ATH10K_DBG_SDIO_DUMP, NULL, "sdio readsb ", buf, len);
  294. out:
  295. sdio_release_host(func);
  296. return ret;
  297. }
  298. /* HIF mbox functions */
  299. static int ath10k_sdio_mbox_rx_process_packet(struct ath10k *ar,
  300. struct ath10k_sdio_rx_data *pkt,
  301. u32 *lookaheads,
  302. int *n_lookaheads)
  303. {
  304. struct ath10k_htc *htc = &ar->htc;
  305. struct sk_buff *skb = pkt->skb;
  306. struct ath10k_htc_hdr *htc_hdr = (struct ath10k_htc_hdr *)skb->data;
  307. bool trailer_present = htc_hdr->flags & ATH10K_HTC_FLAG_TRAILER_PRESENT;
  308. enum ath10k_htc_ep_id eid;
  309. u8 *trailer;
  310. int ret;
  311. if (trailer_present) {
  312. trailer = skb->data + skb->len - htc_hdr->trailer_len;
  313. eid = pipe_id_to_eid(htc_hdr->eid);
  314. ret = ath10k_htc_process_trailer(htc,
  315. trailer,
  316. htc_hdr->trailer_len,
  317. eid,
  318. lookaheads,
  319. n_lookaheads);
  320. if (ret)
  321. return ret;
  322. if (is_trailer_only_msg(pkt))
  323. pkt->trailer_only = true;
  324. skb_trim(skb, skb->len - htc_hdr->trailer_len);
  325. }
  326. skb_pull(skb, sizeof(*htc_hdr));
  327. return 0;
  328. }
  329. static int ath10k_sdio_mbox_rx_process_packets(struct ath10k *ar,
  330. u32 lookaheads[],
  331. int *n_lookahead)
  332. {
  333. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  334. struct ath10k_htc *htc = &ar->htc;
  335. struct ath10k_sdio_rx_data *pkt;
  336. struct ath10k_htc_ep *ep;
  337. struct ath10k_skb_rxcb *cb;
  338. enum ath10k_htc_ep_id id;
  339. int ret, i, *n_lookahead_local;
  340. u32 *lookaheads_local;
  341. int lookahead_idx = 0;
  342. for (i = 0; i < ar_sdio->n_rx_pkts; i++) {
  343. lookaheads_local = lookaheads;
  344. n_lookahead_local = n_lookahead;
  345. id = ((struct ath10k_htc_hdr *)
  346. &lookaheads[lookahead_idx++])->eid;
  347. if (id >= ATH10K_HTC_EP_COUNT) {
  348. ath10k_warn(ar, "invalid endpoint in look-ahead: %d\n",
  349. id);
  350. ret = -ENOMEM;
  351. goto out;
  352. }
  353. ep = &htc->endpoint[id];
  354. if (ep->service_id == 0) {
  355. ath10k_warn(ar, "ep %d is not connected\n", id);
  356. ret = -ENOMEM;
  357. goto out;
  358. }
  359. pkt = &ar_sdio->rx_pkts[i];
  360. if (pkt->part_of_bundle && !pkt->last_in_bundle) {
  361. /* Only read lookahead's from RX trailers
  362. * for the last packet in a bundle.
  363. */
  364. lookahead_idx--;
  365. lookaheads_local = NULL;
  366. n_lookahead_local = NULL;
  367. }
  368. ret = ath10k_sdio_mbox_rx_process_packet(ar,
  369. pkt,
  370. lookaheads_local,
  371. n_lookahead_local);
  372. if (ret)
  373. goto out;
  374. if (!pkt->trailer_only) {
  375. cb = ATH10K_SKB_RXCB(pkt->skb);
  376. cb->eid = id;
  377. skb_queue_tail(&ar_sdio->rx_head, pkt->skb);
  378. queue_work(ar->workqueue_aux,
  379. &ar_sdio->async_work_rx);
  380. } else {
  381. kfree_skb(pkt->skb);
  382. }
  383. /* The RX complete handler now owns the skb...*/
  384. pkt->skb = NULL;
  385. pkt->alloc_len = 0;
  386. }
  387. ret = 0;
  388. out:
  389. /* Free all packets that was not passed on to the RX completion
  390. * handler...
  391. */
  392. for (; i < ar_sdio->n_rx_pkts; i++)
  393. ath10k_sdio_mbox_free_rx_pkt(&ar_sdio->rx_pkts[i]);
  394. return ret;
  395. }
  396. static int ath10k_sdio_mbox_alloc_bundle(struct ath10k *ar,
  397. struct ath10k_sdio_rx_data *rx_pkts,
  398. struct ath10k_htc_hdr *htc_hdr,
  399. size_t full_len, size_t act_len,
  400. size_t *bndl_cnt)
  401. {
  402. int ret, i;
  403. u8 max_msgs = ar->htc.max_msgs_per_htc_bundle;
  404. *bndl_cnt = ath10k_htc_get_bundle_count(max_msgs, htc_hdr->flags);
  405. if (*bndl_cnt > max_msgs) {
  406. ath10k_warn(ar,
  407. "HTC bundle length %u exceeds maximum %u\n",
  408. le16_to_cpu(htc_hdr->len),
  409. max_msgs);
  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. int pkt_cnt = 0;
  438. if (n_lookaheads > ATH10K_SDIO_MAX_RX_MSGS) {
  439. ath10k_warn(ar, "the total number of pkts to be fetched (%u) exceeds maximum %u\n",
  440. n_lookaheads, ATH10K_SDIO_MAX_RX_MSGS);
  441. ret = -ENOMEM;
  442. goto err;
  443. }
  444. for (i = 0; i < n_lookaheads; i++) {
  445. htc_hdr = (struct ath10k_htc_hdr *)&lookaheads[i];
  446. last_in_bundle = false;
  447. if (le16_to_cpu(htc_hdr->len) > ATH10K_HTC_MBOX_MAX_PAYLOAD_LENGTH) {
  448. ath10k_warn(ar, "payload length %d exceeds max htc length: %zu\n",
  449. le16_to_cpu(htc_hdr->len),
  450. ATH10K_HTC_MBOX_MAX_PAYLOAD_LENGTH);
  451. ret = -ENOMEM;
  452. ath10k_core_start_recovery(ar);
  453. ath10k_warn(ar, "exceeds length, start recovery\n");
  454. goto err;
  455. }
  456. act_len = le16_to_cpu(htc_hdr->len) + sizeof(*htc_hdr);
  457. full_len = ath10k_sdio_calc_txrx_padded_len(ar_sdio, act_len);
  458. if (full_len > ATH10K_SDIO_MAX_BUFFER_SIZE) {
  459. ath10k_warn(ar, "rx buffer requested with invalid htc_hdr length (%d, 0x%x): %d\n",
  460. htc_hdr->eid, htc_hdr->flags,
  461. le16_to_cpu(htc_hdr->len));
  462. ret = -EINVAL;
  463. goto err;
  464. }
  465. if (ath10k_htc_get_bundle_count(
  466. ar->htc.max_msgs_per_htc_bundle, htc_hdr->flags)) {
  467. /* HTC header indicates that every packet to follow
  468. * has the same padded length so that it can be
  469. * optimally fetched as a full bundle.
  470. */
  471. size_t bndl_cnt;
  472. ret = ath10k_sdio_mbox_alloc_bundle(ar,
  473. &ar_sdio->rx_pkts[pkt_cnt],
  474. htc_hdr,
  475. full_len,
  476. act_len,
  477. &bndl_cnt);
  478. if (ret) {
  479. ath10k_warn(ar, "failed to allocate a bundle: %d\n",
  480. ret);
  481. goto err;
  482. }
  483. pkt_cnt += bndl_cnt;
  484. /* next buffer will be the last in the bundle */
  485. last_in_bundle = true;
  486. }
  487. /* Allocate skb for packet. If the packet had the
  488. * ATH10K_HTC_FLAG_BUNDLE_MASK flag set, all bundled
  489. * packet skb's have been allocated in the previous step.
  490. */
  491. if (htc_hdr->flags & ATH10K_HTC_FLAGS_RECV_1MORE_BLOCK)
  492. full_len += ATH10K_HIF_MBOX_BLOCK_SIZE;
  493. ret = ath10k_sdio_mbox_alloc_rx_pkt(&ar_sdio->rx_pkts[pkt_cnt],
  494. act_len,
  495. full_len,
  496. last_in_bundle,
  497. last_in_bundle);
  498. if (ret) {
  499. ath10k_warn(ar, "alloc_rx_pkt error %d\n", ret);
  500. goto err;
  501. }
  502. pkt_cnt++;
  503. }
  504. ar_sdio->n_rx_pkts = pkt_cnt;
  505. return 0;
  506. err:
  507. for (i = 0; i < ATH10K_SDIO_MAX_RX_MSGS; i++) {
  508. if (!ar_sdio->rx_pkts[i].alloc_len)
  509. break;
  510. ath10k_sdio_mbox_free_rx_pkt(&ar_sdio->rx_pkts[i]);
  511. }
  512. return ret;
  513. }
  514. static int ath10k_sdio_mbox_rx_fetch(struct ath10k *ar)
  515. {
  516. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  517. struct ath10k_sdio_rx_data *pkt = &ar_sdio->rx_pkts[0];
  518. struct sk_buff *skb = pkt->skb;
  519. struct ath10k_htc_hdr *htc_hdr;
  520. int ret;
  521. ret = ath10k_sdio_readsb(ar, ar_sdio->mbox_info.htc_addr,
  522. skb->data, pkt->alloc_len);
  523. if (ret)
  524. goto err;
  525. htc_hdr = (struct ath10k_htc_hdr *)skb->data;
  526. pkt->act_len = le16_to_cpu(htc_hdr->len) + sizeof(*htc_hdr);
  527. if (pkt->act_len > pkt->alloc_len) {
  528. ret = -EINVAL;
  529. goto err;
  530. }
  531. skb_put(skb, pkt->act_len);
  532. return 0;
  533. err:
  534. ar_sdio->n_rx_pkts = 0;
  535. ath10k_sdio_mbox_free_rx_pkt(pkt);
  536. return ret;
  537. }
  538. static int ath10k_sdio_mbox_rx_fetch_bundle(struct ath10k *ar)
  539. {
  540. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  541. struct ath10k_sdio_rx_data *pkt;
  542. struct ath10k_htc_hdr *htc_hdr;
  543. int ret, i;
  544. u32 pkt_offset, virt_pkt_len;
  545. virt_pkt_len = 0;
  546. for (i = 0; i < ar_sdio->n_rx_pkts; i++)
  547. virt_pkt_len += ar_sdio->rx_pkts[i].alloc_len;
  548. if (virt_pkt_len > ATH10K_SDIO_VSG_BUF_SIZE) {
  549. ath10k_warn(ar, "sdio vsg buffer size limit: %d\n", virt_pkt_len);
  550. ret = -E2BIG;
  551. goto err;
  552. }
  553. ret = ath10k_sdio_readsb(ar, ar_sdio->mbox_info.htc_addr,
  554. ar_sdio->vsg_buffer, virt_pkt_len);
  555. if (ret) {
  556. ath10k_warn(ar, "failed to read bundle packets: %d", ret);
  557. goto err;
  558. }
  559. pkt_offset = 0;
  560. for (i = 0; i < ar_sdio->n_rx_pkts; i++) {
  561. pkt = &ar_sdio->rx_pkts[i];
  562. htc_hdr = (struct ath10k_htc_hdr *)(ar_sdio->vsg_buffer + pkt_offset);
  563. pkt->act_len = le16_to_cpu(htc_hdr->len) + sizeof(*htc_hdr);
  564. if (pkt->act_len > pkt->alloc_len) {
  565. ret = -EINVAL;
  566. goto err;
  567. }
  568. skb_put_data(pkt->skb, htc_hdr, pkt->act_len);
  569. pkt_offset += pkt->alloc_len;
  570. }
  571. return 0;
  572. err:
  573. /* Free all packets that was not successfully fetched. */
  574. for (i = 0; i < ar_sdio->n_rx_pkts; i++)
  575. ath10k_sdio_mbox_free_rx_pkt(&ar_sdio->rx_pkts[i]);
  576. ar_sdio->n_rx_pkts = 0;
  577. return ret;
  578. }
  579. /* This is the timeout for mailbox processing done in the sdio irq
  580. * handler. The timeout is deliberately set quite high since SDIO dump logs
  581. * over serial port can/will add a substantial overhead to the processing
  582. * (if enabled).
  583. */
  584. #define SDIO_MBOX_PROCESSING_TIMEOUT_HZ (20 * HZ)
  585. static int ath10k_sdio_mbox_rxmsg_pending_handler(struct ath10k *ar,
  586. u32 msg_lookahead, bool *done)
  587. {
  588. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  589. u32 lookaheads[ATH10K_SDIO_MAX_RX_MSGS];
  590. int n_lookaheads = 1;
  591. unsigned long timeout;
  592. int ret;
  593. *done = true;
  594. /* Copy the lookahead obtained from the HTC register table into our
  595. * temp array as a start value.
  596. */
  597. lookaheads[0] = msg_lookahead;
  598. timeout = jiffies + SDIO_MBOX_PROCESSING_TIMEOUT_HZ;
  599. do {
  600. /* Try to allocate as many HTC RX packets indicated by
  601. * n_lookaheads.
  602. */
  603. ret = ath10k_sdio_mbox_rx_alloc(ar, lookaheads,
  604. n_lookaheads);
  605. if (ret)
  606. break;
  607. if (ar_sdio->n_rx_pkts >= 2)
  608. /* A recv bundle was detected, force IRQ status
  609. * re-check again.
  610. */
  611. *done = false;
  612. if (ar_sdio->n_rx_pkts > 1)
  613. ret = ath10k_sdio_mbox_rx_fetch_bundle(ar);
  614. else
  615. ret = ath10k_sdio_mbox_rx_fetch(ar);
  616. /* Process fetched packets. This will potentially update
  617. * n_lookaheads depending on if the packets contain lookahead
  618. * reports.
  619. */
  620. n_lookaheads = 0;
  621. ret = ath10k_sdio_mbox_rx_process_packets(ar,
  622. lookaheads,
  623. &n_lookaheads);
  624. if (!n_lookaheads || ret)
  625. break;
  626. /* For SYNCH processing, if we get here, we are running
  627. * through the loop again due to updated lookaheads. Set
  628. * flag that we should re-check IRQ status registers again
  629. * before leaving IRQ processing, this can net better
  630. * performance in high throughput situations.
  631. */
  632. *done = false;
  633. } while (time_before(jiffies, timeout));
  634. if (ret && (ret != -ECANCELED))
  635. ath10k_warn(ar, "failed to get pending recv messages: %d\n",
  636. ret);
  637. return ret;
  638. }
  639. static int ath10k_sdio_mbox_proc_dbg_intr(struct ath10k *ar)
  640. {
  641. u32 val;
  642. int ret;
  643. /* TODO: Add firmware crash handling */
  644. ath10k_warn(ar, "firmware crashed\n");
  645. /* read counter to clear the interrupt, the debug error interrupt is
  646. * counter 0.
  647. */
  648. ret = ath10k_sdio_read32(ar, MBOX_COUNT_DEC_ADDRESS, &val);
  649. if (ret)
  650. ath10k_warn(ar, "failed to clear debug interrupt: %d\n", ret);
  651. return ret;
  652. }
  653. static int ath10k_sdio_mbox_proc_counter_intr(struct ath10k *ar)
  654. {
  655. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  656. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  657. u8 counter_int_status;
  658. int ret;
  659. mutex_lock(&irq_data->mtx);
  660. counter_int_status = irq_data->irq_proc_reg->counter_int_status &
  661. irq_data->irq_en_reg->cntr_int_status_en;
  662. /* NOTE: other modules like GMBOX may use the counter interrupt for
  663. * credit flow control on other counters, we only need to check for
  664. * the debug assertion counter interrupt.
  665. */
  666. if (counter_int_status & ATH10K_SDIO_TARGET_DEBUG_INTR_MASK)
  667. ret = ath10k_sdio_mbox_proc_dbg_intr(ar);
  668. else
  669. ret = 0;
  670. mutex_unlock(&irq_data->mtx);
  671. return ret;
  672. }
  673. static int ath10k_sdio_mbox_proc_err_intr(struct ath10k *ar)
  674. {
  675. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  676. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  677. u8 error_int_status;
  678. int ret;
  679. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio error interrupt\n");
  680. error_int_status = irq_data->irq_proc_reg->error_int_status & 0x0F;
  681. if (!error_int_status) {
  682. ath10k_warn(ar, "invalid error interrupt status: 0x%x\n",
  683. error_int_status);
  684. return -EIO;
  685. }
  686. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  687. "sdio error_int_status 0x%x\n", error_int_status);
  688. if (FIELD_GET(MBOX_ERROR_INT_STATUS_WAKEUP_MASK,
  689. error_int_status))
  690. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio interrupt error wakeup\n");
  691. if (FIELD_GET(MBOX_ERROR_INT_STATUS_RX_UNDERFLOW_MASK,
  692. error_int_status))
  693. ath10k_warn(ar, "rx underflow interrupt error\n");
  694. if (FIELD_GET(MBOX_ERROR_INT_STATUS_TX_OVERFLOW_MASK,
  695. error_int_status))
  696. ath10k_warn(ar, "tx overflow interrupt error\n");
  697. /* Clear the interrupt */
  698. irq_data->irq_proc_reg->error_int_status &= ~error_int_status;
  699. /* set W1C value to clear the interrupt, this hits the register first */
  700. ret = ath10k_sdio_writesb32(ar, MBOX_ERROR_INT_STATUS_ADDRESS,
  701. error_int_status);
  702. if (ret) {
  703. ath10k_warn(ar, "unable to write to error int status address: %d\n",
  704. ret);
  705. return ret;
  706. }
  707. return 0;
  708. }
  709. static int ath10k_sdio_mbox_proc_cpu_intr(struct ath10k *ar)
  710. {
  711. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  712. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  713. u8 cpu_int_status;
  714. int ret;
  715. mutex_lock(&irq_data->mtx);
  716. cpu_int_status = irq_data->irq_proc_reg->cpu_int_status &
  717. irq_data->irq_en_reg->cpu_int_status_en;
  718. if (!cpu_int_status) {
  719. ath10k_warn(ar, "CPU interrupt status is zero\n");
  720. ret = -EIO;
  721. goto out;
  722. }
  723. /* Clear the interrupt */
  724. irq_data->irq_proc_reg->cpu_int_status &= ~cpu_int_status;
  725. /* Set up the register transfer buffer to hit the register 4 times,
  726. * this is done to make the access 4-byte aligned to mitigate issues
  727. * with host bus interconnects that restrict bus transfer lengths to
  728. * be a multiple of 4-bytes.
  729. *
  730. * Set W1C value to clear the interrupt, this hits the register first.
  731. */
  732. ret = ath10k_sdio_writesb32(ar, MBOX_CPU_INT_STATUS_ADDRESS,
  733. cpu_int_status);
  734. if (ret) {
  735. ath10k_warn(ar, "unable to write to cpu interrupt status address: %d\n",
  736. ret);
  737. goto out;
  738. }
  739. out:
  740. mutex_unlock(&irq_data->mtx);
  741. if (cpu_int_status & MBOX_CPU_STATUS_ENABLE_ASSERT_MASK)
  742. ath10k_sdio_fw_crashed_dump(ar);
  743. return ret;
  744. }
  745. static int ath10k_sdio_mbox_read_int_status(struct ath10k *ar,
  746. u8 *host_int_status,
  747. u32 *lookahead)
  748. {
  749. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  750. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  751. struct ath10k_sdio_irq_proc_regs *irq_proc_reg = irq_data->irq_proc_reg;
  752. struct ath10k_sdio_irq_enable_regs *irq_en_reg = irq_data->irq_en_reg;
  753. u8 htc_mbox = FIELD_PREP(ATH10K_HTC_MAILBOX_MASK, 1);
  754. int ret;
  755. mutex_lock(&irq_data->mtx);
  756. *lookahead = 0;
  757. *host_int_status = 0;
  758. /* int_status_en is supposed to be non zero, otherwise interrupts
  759. * shouldn't be enabled. There is however a short time frame during
  760. * initialization between the irq register and int_status_en init
  761. * where this can happen.
  762. * We silently ignore this condition.
  763. */
  764. if (!irq_en_reg->int_status_en) {
  765. ret = 0;
  766. goto out;
  767. }
  768. /* Read the first sizeof(struct ath10k_irq_proc_registers)
  769. * bytes of the HTC register table. This
  770. * will yield us the value of different int status
  771. * registers and the lookahead registers.
  772. */
  773. ret = ath10k_sdio_read(ar, MBOX_HOST_INT_STATUS_ADDRESS,
  774. irq_proc_reg, sizeof(*irq_proc_reg));
  775. if (ret) {
  776. ath10k_core_start_recovery(ar);
  777. ath10k_warn(ar, "read int status fail, start recovery\n");
  778. goto out;
  779. }
  780. /* Update only those registers that are enabled */
  781. *host_int_status = irq_proc_reg->host_int_status &
  782. irq_en_reg->int_status_en;
  783. /* Look at mbox status */
  784. if (!(*host_int_status & htc_mbox)) {
  785. *lookahead = 0;
  786. ret = 0;
  787. goto out;
  788. }
  789. /* Mask out pending mbox value, we use look ahead as
  790. * the real flag for mbox processing.
  791. */
  792. *host_int_status &= ~htc_mbox;
  793. if (irq_proc_reg->rx_lookahead_valid & htc_mbox) {
  794. *lookahead = le32_to_cpu(
  795. irq_proc_reg->rx_lookahead[ATH10K_HTC_MAILBOX]);
  796. if (!*lookahead)
  797. ath10k_warn(ar, "sdio mbox lookahead is zero\n");
  798. }
  799. out:
  800. mutex_unlock(&irq_data->mtx);
  801. return ret;
  802. }
  803. static int ath10k_sdio_mbox_proc_pending_irqs(struct ath10k *ar,
  804. bool *done)
  805. {
  806. u8 host_int_status;
  807. u32 lookahead;
  808. int ret;
  809. /* NOTE: HIF implementation guarantees that the context of this
  810. * call allows us to perform SYNCHRONOUS I/O, that is we can block,
  811. * sleep or call any API that can block or switch thread/task
  812. * contexts. This is a fully schedulable context.
  813. */
  814. ret = ath10k_sdio_mbox_read_int_status(ar,
  815. &host_int_status,
  816. &lookahead);
  817. if (ret) {
  818. *done = true;
  819. goto out;
  820. }
  821. if (!host_int_status && !lookahead) {
  822. ret = 0;
  823. *done = true;
  824. goto out;
  825. }
  826. if (lookahead) {
  827. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  828. "sdio pending mailbox msg lookahead 0x%08x\n",
  829. lookahead);
  830. ret = ath10k_sdio_mbox_rxmsg_pending_handler(ar,
  831. lookahead,
  832. done);
  833. if (ret)
  834. goto out;
  835. }
  836. /* now, handle the rest of the interrupts */
  837. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  838. "sdio host_int_status 0x%x\n", host_int_status);
  839. if (FIELD_GET(MBOX_HOST_INT_STATUS_CPU_MASK, host_int_status)) {
  840. /* CPU Interrupt */
  841. ret = ath10k_sdio_mbox_proc_cpu_intr(ar);
  842. if (ret)
  843. goto out;
  844. }
  845. if (FIELD_GET(MBOX_HOST_INT_STATUS_ERROR_MASK, host_int_status)) {
  846. /* Error Interrupt */
  847. ret = ath10k_sdio_mbox_proc_err_intr(ar);
  848. if (ret)
  849. goto out;
  850. }
  851. if (FIELD_GET(MBOX_HOST_INT_STATUS_COUNTER_MASK, host_int_status))
  852. /* Counter Interrupt */
  853. ret = ath10k_sdio_mbox_proc_counter_intr(ar);
  854. ret = 0;
  855. out:
  856. /* An optimization to bypass reading the IRQ status registers
  857. * unnecessarily which can re-wake the target, if upper layers
  858. * determine that we are in a low-throughput mode, we can rely on
  859. * taking another interrupt rather than re-checking the status
  860. * registers which can re-wake the target.
  861. *
  862. * NOTE : for host interfaces that makes use of detecting pending
  863. * mbox messages at hif can not use this optimization due to
  864. * possible side effects, SPI requires the host to drain all
  865. * messages from the mailbox before exiting the ISR routine.
  866. */
  867. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  868. "sdio pending irqs done %d status %d",
  869. *done, ret);
  870. return ret;
  871. }
  872. static void ath10k_sdio_set_mbox_info(struct ath10k *ar)
  873. {
  874. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  875. struct ath10k_mbox_info *mbox_info = &ar_sdio->mbox_info;
  876. u16 device = ar_sdio->func->device, dev_id_base, dev_id_chiprev;
  877. mbox_info->htc_addr = ATH10K_HIF_MBOX_BASE_ADDR;
  878. mbox_info->block_size = ATH10K_HIF_MBOX_BLOCK_SIZE;
  879. mbox_info->block_mask = ATH10K_HIF_MBOX_BLOCK_SIZE - 1;
  880. mbox_info->gmbox_addr = ATH10K_HIF_GMBOX_BASE_ADDR;
  881. mbox_info->gmbox_sz = ATH10K_HIF_GMBOX_WIDTH;
  882. mbox_info->ext_info[0].htc_ext_addr = ATH10K_HIF_MBOX0_EXT_BASE_ADDR;
  883. dev_id_base = (device & 0x0F00);
  884. dev_id_chiprev = (device & 0x00FF);
  885. switch (dev_id_base) {
  886. case (SDIO_DEVICE_ID_ATHEROS_AR6005 & 0x0F00):
  887. if (dev_id_chiprev < 4)
  888. mbox_info->ext_info[0].htc_ext_sz =
  889. ATH10K_HIF_MBOX0_EXT_WIDTH;
  890. else
  891. /* from QCA6174 2.0(0x504), the width has been extended
  892. * to 56K
  893. */
  894. mbox_info->ext_info[0].htc_ext_sz =
  895. ATH10K_HIF_MBOX0_EXT_WIDTH_ROME_2_0;
  896. break;
  897. case (SDIO_DEVICE_ID_ATHEROS_QCA9377 & 0x0F00):
  898. mbox_info->ext_info[0].htc_ext_sz =
  899. ATH10K_HIF_MBOX0_EXT_WIDTH_ROME_2_0;
  900. break;
  901. default:
  902. mbox_info->ext_info[0].htc_ext_sz =
  903. ATH10K_HIF_MBOX0_EXT_WIDTH;
  904. }
  905. mbox_info->ext_info[1].htc_ext_addr =
  906. mbox_info->ext_info[0].htc_ext_addr +
  907. mbox_info->ext_info[0].htc_ext_sz +
  908. ATH10K_HIF_MBOX_DUMMY_SPACE_SIZE;
  909. mbox_info->ext_info[1].htc_ext_sz = ATH10K_HIF_MBOX1_EXT_WIDTH;
  910. }
  911. /* BMI functions */
  912. static int ath10k_sdio_bmi_credits(struct ath10k *ar)
  913. {
  914. u32 addr, cmd_credits;
  915. unsigned long timeout;
  916. int ret;
  917. /* Read the counter register to get the command credits */
  918. addr = MBOX_COUNT_DEC_ADDRESS + ATH10K_HIF_MBOX_NUM_MAX * 4;
  919. timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
  920. cmd_credits = 0;
  921. while (time_before(jiffies, timeout) && !cmd_credits) {
  922. /* Hit the credit counter with a 4-byte access, the first byte
  923. * read will hit the counter and cause a decrement, while the
  924. * remaining 3 bytes has no effect. The rationale behind this
  925. * is to make all HIF accesses 4-byte aligned.
  926. */
  927. ret = ath10k_sdio_read32(ar, addr, &cmd_credits);
  928. if (ret) {
  929. ath10k_warn(ar,
  930. "unable to decrement the command credit count register: %d\n",
  931. ret);
  932. return ret;
  933. }
  934. /* The counter is only 8 bits.
  935. * Ignore anything in the upper 3 bytes
  936. */
  937. cmd_credits &= 0xFF;
  938. }
  939. if (!cmd_credits) {
  940. ath10k_warn(ar, "bmi communication timeout\n");
  941. return -ETIMEDOUT;
  942. }
  943. return 0;
  944. }
  945. static int ath10k_sdio_bmi_get_rx_lookahead(struct ath10k *ar)
  946. {
  947. unsigned long timeout;
  948. u32 rx_word;
  949. int ret;
  950. timeout = jiffies + BMI_COMMUNICATION_TIMEOUT_HZ;
  951. rx_word = 0;
  952. while ((time_before(jiffies, timeout)) && !rx_word) {
  953. ret = ath10k_sdio_read32(ar,
  954. MBOX_HOST_INT_STATUS_ADDRESS,
  955. &rx_word);
  956. if (ret) {
  957. ath10k_warn(ar, "unable to read RX_LOOKAHEAD_VALID: %d\n", ret);
  958. return ret;
  959. }
  960. /* all we really want is one bit */
  961. rx_word &= 1;
  962. }
  963. if (!rx_word) {
  964. ath10k_warn(ar, "bmi_recv_buf FIFO empty\n");
  965. return -EINVAL;
  966. }
  967. return ret;
  968. }
  969. static int ath10k_sdio_bmi_exchange_msg(struct ath10k *ar,
  970. void *req, u32 req_len,
  971. void *resp, u32 *resp_len)
  972. {
  973. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  974. u32 addr;
  975. int ret;
  976. if (req) {
  977. ret = ath10k_sdio_bmi_credits(ar);
  978. if (ret)
  979. return ret;
  980. addr = ar_sdio->mbox_info.htc_addr;
  981. memcpy(ar_sdio->bmi_buf, req, req_len);
  982. ret = ath10k_sdio_write(ar, addr, ar_sdio->bmi_buf, req_len);
  983. if (ret) {
  984. ath10k_warn(ar,
  985. "unable to send the bmi data to the device: %d\n",
  986. ret);
  987. return ret;
  988. }
  989. }
  990. if (!resp || !resp_len)
  991. /* No response expected */
  992. return 0;
  993. /* During normal bootup, small reads may be required.
  994. * Rather than issue an HIF Read and then wait as the Target
  995. * adds successive bytes to the FIFO, we wait here until
  996. * we know that response data is available.
  997. *
  998. * This allows us to cleanly timeout on an unexpected
  999. * Target failure rather than risk problems at the HIF level.
  1000. * In particular, this avoids SDIO timeouts and possibly garbage
  1001. * data on some host controllers. And on an interconnect
  1002. * such as Compact Flash (as well as some SDIO masters) which
  1003. * does not provide any indication on data timeout, it avoids
  1004. * a potential hang or garbage response.
  1005. *
  1006. * Synchronization is more difficult for reads larger than the
  1007. * size of the MBOX FIFO (128B), because the Target is unable
  1008. * to push the 129th byte of data until AFTER the Host posts an
  1009. * HIF Read and removes some FIFO data. So for large reads the
  1010. * Host proceeds to post an HIF Read BEFORE all the data is
  1011. * actually available to read. Fortunately, large BMI reads do
  1012. * not occur in practice -- they're supported for debug/development.
  1013. *
  1014. * So Host/Target BMI synchronization is divided into these cases:
  1015. * CASE 1: length < 4
  1016. * Should not happen
  1017. *
  1018. * CASE 2: 4 <= length <= 128
  1019. * Wait for first 4 bytes to be in FIFO
  1020. * If CONSERVATIVE_BMI_READ is enabled, also wait for
  1021. * a BMI command credit, which indicates that the ENTIRE
  1022. * response is available in the FIFO
  1023. *
  1024. * CASE 3: length > 128
  1025. * Wait for the first 4 bytes to be in FIFO
  1026. *
  1027. * For most uses, a small timeout should be sufficient and we will
  1028. * usually see a response quickly; but there may be some unusual
  1029. * (debug) cases of BMI_EXECUTE where we want an larger timeout.
  1030. * For now, we use an unbounded busy loop while waiting for
  1031. * BMI_EXECUTE.
  1032. *
  1033. * If BMI_EXECUTE ever needs to support longer-latency execution,
  1034. * especially in production, this code needs to be enhanced to sleep
  1035. * and yield. Also note that BMI_COMMUNICATION_TIMEOUT is currently
  1036. * a function of Host processor speed.
  1037. */
  1038. ret = ath10k_sdio_bmi_get_rx_lookahead(ar);
  1039. if (ret)
  1040. return ret;
  1041. /* We always read from the start of the mbox address */
  1042. addr = ar_sdio->mbox_info.htc_addr;
  1043. ret = ath10k_sdio_read(ar, addr, ar_sdio->bmi_buf, *resp_len);
  1044. if (ret) {
  1045. ath10k_warn(ar,
  1046. "unable to read the bmi data from the device: %d\n",
  1047. ret);
  1048. return ret;
  1049. }
  1050. memcpy(resp, ar_sdio->bmi_buf, *resp_len);
  1051. return 0;
  1052. }
  1053. /* sdio async handling functions */
  1054. static struct ath10k_sdio_bus_request
  1055. *ath10k_sdio_alloc_busreq(struct ath10k *ar)
  1056. {
  1057. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1058. struct ath10k_sdio_bus_request *bus_req;
  1059. spin_lock_bh(&ar_sdio->lock);
  1060. if (list_empty(&ar_sdio->bus_req_freeq)) {
  1061. bus_req = NULL;
  1062. goto out;
  1063. }
  1064. bus_req = list_first_entry(&ar_sdio->bus_req_freeq,
  1065. struct ath10k_sdio_bus_request, list);
  1066. list_del(&bus_req->list);
  1067. out:
  1068. spin_unlock_bh(&ar_sdio->lock);
  1069. return bus_req;
  1070. }
  1071. static void ath10k_sdio_free_bus_req(struct ath10k *ar,
  1072. struct ath10k_sdio_bus_request *bus_req)
  1073. {
  1074. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1075. memset(bus_req, 0, sizeof(*bus_req));
  1076. spin_lock_bh(&ar_sdio->lock);
  1077. list_add_tail(&bus_req->list, &ar_sdio->bus_req_freeq);
  1078. spin_unlock_bh(&ar_sdio->lock);
  1079. }
  1080. static void __ath10k_sdio_write_async(struct ath10k *ar,
  1081. struct ath10k_sdio_bus_request *req)
  1082. {
  1083. struct ath10k_htc_ep *ep;
  1084. struct sk_buff *skb;
  1085. int ret;
  1086. skb = req->skb;
  1087. ret = ath10k_sdio_write(ar, req->address, skb->data, skb->len);
  1088. if (ret)
  1089. ath10k_warn(ar, "failed to write skb to 0x%x asynchronously: %d",
  1090. req->address, ret);
  1091. if (req->htc_msg) {
  1092. ep = &ar->htc.endpoint[req->eid];
  1093. ath10k_htc_notify_tx_completion(ep, skb);
  1094. } else if (req->comp) {
  1095. complete(req->comp);
  1096. }
  1097. ath10k_sdio_free_bus_req(ar, req);
  1098. }
  1099. /* To improve throughput use workqueue to deliver packets to HTC layer,
  1100. * this way SDIO bus is utilised much better.
  1101. */
  1102. static void ath10k_rx_indication_async_work(struct work_struct *work)
  1103. {
  1104. struct ath10k_sdio *ar_sdio = container_of(work, struct ath10k_sdio,
  1105. async_work_rx);
  1106. struct ath10k *ar = ar_sdio->ar;
  1107. struct ath10k_htc_ep *ep;
  1108. struct ath10k_skb_rxcb *cb;
  1109. struct sk_buff *skb;
  1110. while (true) {
  1111. skb = skb_dequeue(&ar_sdio->rx_head);
  1112. if (!skb)
  1113. break;
  1114. cb = ATH10K_SKB_RXCB(skb);
  1115. ep = &ar->htc.endpoint[cb->eid];
  1116. ep->ep_ops.ep_rx_complete(ar, skb);
  1117. }
  1118. if (test_bit(ATH10K_FLAG_CORE_REGISTERED, &ar->dev_flags)) {
  1119. local_bh_disable();
  1120. napi_schedule(&ar->napi);
  1121. local_bh_enable();
  1122. }
  1123. }
  1124. static int ath10k_sdio_read_rtc_state(struct ath10k_sdio *ar_sdio, unsigned char *state)
  1125. {
  1126. struct ath10k *ar = ar_sdio->ar;
  1127. unsigned char rtc_state = 0;
  1128. int ret = 0;
  1129. rtc_state = sdio_f0_readb(ar_sdio->func, ATH10K_CIS_RTC_STATE_ADDR, &ret);
  1130. if (ret) {
  1131. ath10k_warn(ar, "failed to read rtc state: %d\n", ret);
  1132. return ret;
  1133. }
  1134. *state = rtc_state & 0x3;
  1135. return ret;
  1136. }
  1137. static int ath10k_sdio_set_mbox_sleep(struct ath10k *ar, bool enable_sleep)
  1138. {
  1139. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1140. u32 val;
  1141. int retry = ATH10K_CIS_READ_RETRY, ret = 0;
  1142. unsigned char rtc_state = 0;
  1143. sdio_claim_host(ar_sdio->func);
  1144. ret = ath10k_sdio_read32(ar, ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL, &val);
  1145. if (ret) {
  1146. ath10k_warn(ar, "failed to read fifo/chip control register: %d\n",
  1147. ret);
  1148. goto release;
  1149. }
  1150. if (enable_sleep) {
  1151. val &= ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL_DISABLE_SLEEP_OFF;
  1152. ar_sdio->mbox_state = SDIO_MBOX_SLEEP_STATE;
  1153. } else {
  1154. val |= ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL_DISABLE_SLEEP_ON;
  1155. ar_sdio->mbox_state = SDIO_MBOX_AWAKE_STATE;
  1156. }
  1157. ret = ath10k_sdio_write32(ar, ATH10K_FIFO_TIMEOUT_AND_CHIP_CONTROL, val);
  1158. if (ret) {
  1159. ath10k_warn(ar, "failed to write to FIFO_TIMEOUT_AND_CHIP_CONTROL: %d",
  1160. ret);
  1161. }
  1162. if (!enable_sleep) {
  1163. do {
  1164. udelay(ATH10K_CIS_READ_WAIT_4_RTC_CYCLE_IN_US);
  1165. ret = ath10k_sdio_read_rtc_state(ar_sdio, &rtc_state);
  1166. if (ret) {
  1167. ath10k_warn(ar, "failed to disable mbox sleep: %d", ret);
  1168. break;
  1169. }
  1170. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio read rtc state: %d\n",
  1171. rtc_state);
  1172. if (rtc_state == ATH10K_CIS_RTC_STATE_ON)
  1173. break;
  1174. udelay(ATH10K_CIS_XTAL_SETTLE_DURATION_IN_US);
  1175. retry--;
  1176. } while (retry > 0);
  1177. }
  1178. release:
  1179. sdio_release_host(ar_sdio->func);
  1180. return ret;
  1181. }
  1182. static void ath10k_sdio_sleep_timer_handler(struct timer_list *t)
  1183. {
  1184. struct ath10k_sdio *ar_sdio = from_timer(ar_sdio, t, sleep_timer);
  1185. ar_sdio->mbox_state = SDIO_MBOX_REQUEST_TO_SLEEP_STATE;
  1186. queue_work(ar_sdio->workqueue, &ar_sdio->wr_async_work);
  1187. }
  1188. static void ath10k_sdio_write_async_work(struct work_struct *work)
  1189. {
  1190. struct ath10k_sdio *ar_sdio = container_of(work, struct ath10k_sdio,
  1191. wr_async_work);
  1192. struct ath10k *ar = ar_sdio->ar;
  1193. struct ath10k_sdio_bus_request *req, *tmp_req;
  1194. struct ath10k_mbox_info *mbox_info = &ar_sdio->mbox_info;
  1195. spin_lock_bh(&ar_sdio->wr_async_lock);
  1196. list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
  1197. list_del(&req->list);
  1198. spin_unlock_bh(&ar_sdio->wr_async_lock);
  1199. if (req->address >= mbox_info->htc_addr &&
  1200. ar_sdio->mbox_state == SDIO_MBOX_SLEEP_STATE) {
  1201. ath10k_sdio_set_mbox_sleep(ar, false);
  1202. mod_timer(&ar_sdio->sleep_timer, jiffies +
  1203. msecs_to_jiffies(ATH10K_MIN_SLEEP_INACTIVITY_TIME_MS));
  1204. }
  1205. __ath10k_sdio_write_async(ar, req);
  1206. spin_lock_bh(&ar_sdio->wr_async_lock);
  1207. }
  1208. spin_unlock_bh(&ar_sdio->wr_async_lock);
  1209. if (ar_sdio->mbox_state == SDIO_MBOX_REQUEST_TO_SLEEP_STATE)
  1210. ath10k_sdio_set_mbox_sleep(ar, true);
  1211. }
  1212. static int ath10k_sdio_prep_async_req(struct ath10k *ar, u32 addr,
  1213. struct sk_buff *skb,
  1214. struct completion *comp,
  1215. bool htc_msg, enum ath10k_htc_ep_id eid)
  1216. {
  1217. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1218. struct ath10k_sdio_bus_request *bus_req;
  1219. /* Allocate a bus request for the message and queue it on the
  1220. * SDIO workqueue.
  1221. */
  1222. bus_req = ath10k_sdio_alloc_busreq(ar);
  1223. if (!bus_req) {
  1224. ath10k_warn(ar,
  1225. "unable to allocate bus request for async request\n");
  1226. return -ENOMEM;
  1227. }
  1228. bus_req->skb = skb;
  1229. bus_req->eid = eid;
  1230. bus_req->address = addr;
  1231. bus_req->htc_msg = htc_msg;
  1232. bus_req->comp = comp;
  1233. spin_lock_bh(&ar_sdio->wr_async_lock);
  1234. list_add_tail(&bus_req->list, &ar_sdio->wr_asyncq);
  1235. spin_unlock_bh(&ar_sdio->wr_async_lock);
  1236. return 0;
  1237. }
  1238. /* IRQ handler */
  1239. static void ath10k_sdio_irq_handler(struct sdio_func *func)
  1240. {
  1241. struct ath10k_sdio *ar_sdio = sdio_get_drvdata(func);
  1242. struct ath10k *ar = ar_sdio->ar;
  1243. unsigned long timeout;
  1244. bool done = false;
  1245. int ret;
  1246. /* Release the host during interrupts so we can pick it back up when
  1247. * we process commands.
  1248. */
  1249. sdio_release_host(ar_sdio->func);
  1250. timeout = jiffies + ATH10K_SDIO_HIF_COMMUNICATION_TIMEOUT_HZ;
  1251. do {
  1252. ret = ath10k_sdio_mbox_proc_pending_irqs(ar, &done);
  1253. if (ret)
  1254. break;
  1255. } while (time_before(jiffies, timeout) && !done);
  1256. ath10k_mac_tx_push_pending(ar);
  1257. sdio_claim_host(ar_sdio->func);
  1258. if (ret && ret != -ECANCELED)
  1259. ath10k_warn(ar, "failed to process pending SDIO interrupts: %d\n",
  1260. ret);
  1261. }
  1262. /* sdio HIF functions */
  1263. static int ath10k_sdio_disable_intrs(struct ath10k *ar)
  1264. {
  1265. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1266. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  1267. struct ath10k_sdio_irq_enable_regs *regs = irq_data->irq_en_reg;
  1268. int ret;
  1269. mutex_lock(&irq_data->mtx);
  1270. memset(regs, 0, sizeof(*regs));
  1271. ret = ath10k_sdio_write(ar, MBOX_INT_STATUS_ENABLE_ADDRESS,
  1272. &regs->int_status_en, sizeof(*regs));
  1273. if (ret)
  1274. ath10k_warn(ar, "unable to disable sdio interrupts: %d\n", ret);
  1275. mutex_unlock(&irq_data->mtx);
  1276. return ret;
  1277. }
  1278. static int ath10k_sdio_hif_power_up(struct ath10k *ar,
  1279. enum ath10k_firmware_mode fw_mode)
  1280. {
  1281. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1282. struct sdio_func *func = ar_sdio->func;
  1283. int ret;
  1284. if (!ar_sdio->is_disabled)
  1285. return 0;
  1286. ath10k_dbg(ar, ATH10K_DBG_BOOT, "sdio power on\n");
  1287. ret = ath10k_sdio_config(ar);
  1288. if (ret) {
  1289. ath10k_err(ar, "failed to config sdio: %d\n", ret);
  1290. return ret;
  1291. }
  1292. sdio_claim_host(func);
  1293. ret = sdio_enable_func(func);
  1294. if (ret) {
  1295. ath10k_warn(ar, "unable to enable sdio function: %d)\n", ret);
  1296. sdio_release_host(func);
  1297. return ret;
  1298. }
  1299. sdio_release_host(func);
  1300. /* Wait for hardware to initialise. It should take a lot less than
  1301. * 20 ms but let's be conservative here.
  1302. */
  1303. msleep(20);
  1304. ar_sdio->is_disabled = false;
  1305. ret = ath10k_sdio_disable_intrs(ar);
  1306. if (ret)
  1307. return ret;
  1308. return 0;
  1309. }
  1310. static void ath10k_sdio_hif_power_down(struct ath10k *ar)
  1311. {
  1312. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1313. int ret;
  1314. if (ar_sdio->is_disabled)
  1315. return;
  1316. ath10k_dbg(ar, ATH10K_DBG_BOOT, "sdio power off\n");
  1317. del_timer_sync(&ar_sdio->sleep_timer);
  1318. ath10k_sdio_set_mbox_sleep(ar, true);
  1319. /* Disable the card */
  1320. sdio_claim_host(ar_sdio->func);
  1321. ret = sdio_disable_func(ar_sdio->func);
  1322. if (ret) {
  1323. ath10k_warn(ar, "unable to disable sdio function: %d\n", ret);
  1324. sdio_release_host(ar_sdio->func);
  1325. return;
  1326. }
  1327. ret = mmc_hw_reset(ar_sdio->func->card);
  1328. if (ret)
  1329. ath10k_warn(ar, "unable to reset sdio: %d\n", ret);
  1330. sdio_release_host(ar_sdio->func);
  1331. ar_sdio->is_disabled = true;
  1332. }
  1333. static int ath10k_sdio_hif_tx_sg(struct ath10k *ar, u8 pipe_id,
  1334. struct ath10k_hif_sg_item *items, int n_items)
  1335. {
  1336. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1337. enum ath10k_htc_ep_id eid;
  1338. struct sk_buff *skb;
  1339. int ret, i;
  1340. eid = pipe_id_to_eid(pipe_id);
  1341. for (i = 0; i < n_items; i++) {
  1342. size_t padded_len;
  1343. u32 address;
  1344. skb = items[i].transfer_context;
  1345. padded_len = ath10k_sdio_calc_txrx_padded_len(ar_sdio,
  1346. skb->len);
  1347. skb_trim(skb, padded_len);
  1348. /* Write TX data to the end of the mbox address space */
  1349. address = ar_sdio->mbox_addr[eid] + ar_sdio->mbox_size[eid] -
  1350. skb->len;
  1351. ret = ath10k_sdio_prep_async_req(ar, address, skb,
  1352. NULL, true, eid);
  1353. if (ret)
  1354. return ret;
  1355. }
  1356. queue_work(ar_sdio->workqueue, &ar_sdio->wr_async_work);
  1357. return 0;
  1358. }
  1359. static int ath10k_sdio_enable_intrs(struct ath10k *ar)
  1360. {
  1361. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1362. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  1363. struct ath10k_sdio_irq_enable_regs *regs = irq_data->irq_en_reg;
  1364. int ret;
  1365. mutex_lock(&irq_data->mtx);
  1366. /* Enable all but CPU interrupts */
  1367. regs->int_status_en = FIELD_PREP(MBOX_INT_STATUS_ENABLE_ERROR_MASK, 1) |
  1368. FIELD_PREP(MBOX_INT_STATUS_ENABLE_CPU_MASK, 1) |
  1369. FIELD_PREP(MBOX_INT_STATUS_ENABLE_COUNTER_MASK, 1);
  1370. /* NOTE: There are some cases where HIF can do detection of
  1371. * pending mbox messages which is disabled now.
  1372. */
  1373. regs->int_status_en |=
  1374. FIELD_PREP(MBOX_INT_STATUS_ENABLE_MBOX_DATA_MASK, 1);
  1375. /* Set up the CPU Interrupt Status Register, enable CPU sourced interrupt #0
  1376. * #0 is used for report assertion from target
  1377. */
  1378. regs->cpu_int_status_en = FIELD_PREP(MBOX_CPU_STATUS_ENABLE_ASSERT_MASK, 1);
  1379. /* Set up the Error Interrupt status Register */
  1380. regs->err_int_status_en =
  1381. FIELD_PREP(MBOX_ERROR_STATUS_ENABLE_RX_UNDERFLOW_MASK, 1) |
  1382. FIELD_PREP(MBOX_ERROR_STATUS_ENABLE_TX_OVERFLOW_MASK, 1);
  1383. /* Enable Counter interrupt status register to get fatal errors for
  1384. * debugging.
  1385. */
  1386. regs->cntr_int_status_en =
  1387. FIELD_PREP(MBOX_COUNTER_INT_STATUS_ENABLE_BIT_MASK,
  1388. ATH10K_SDIO_TARGET_DEBUG_INTR_MASK);
  1389. ret = ath10k_sdio_write(ar, MBOX_INT_STATUS_ENABLE_ADDRESS,
  1390. &regs->int_status_en, sizeof(*regs));
  1391. if (ret)
  1392. ath10k_warn(ar,
  1393. "failed to update mbox interrupt status register : %d\n",
  1394. ret);
  1395. mutex_unlock(&irq_data->mtx);
  1396. return ret;
  1397. }
  1398. /* HIF diagnostics */
  1399. static int ath10k_sdio_hif_diag_read(struct ath10k *ar, u32 address, void *buf,
  1400. size_t buf_len)
  1401. {
  1402. int ret;
  1403. void *mem;
  1404. mem = kzalloc(buf_len, GFP_KERNEL);
  1405. if (!mem)
  1406. return -ENOMEM;
  1407. /* set window register to start read cycle */
  1408. ret = ath10k_sdio_write32(ar, MBOX_WINDOW_READ_ADDR_ADDRESS, address);
  1409. if (ret) {
  1410. ath10k_warn(ar, "failed to set mbox window read address: %d", ret);
  1411. goto out;
  1412. }
  1413. /* read the data */
  1414. ret = ath10k_sdio_read(ar, MBOX_WINDOW_DATA_ADDRESS, mem, buf_len);
  1415. if (ret) {
  1416. ath10k_warn(ar, "failed to read from mbox window data address: %d\n",
  1417. ret);
  1418. goto out;
  1419. }
  1420. memcpy(buf, mem, buf_len);
  1421. out:
  1422. kfree(mem);
  1423. return ret;
  1424. }
  1425. static int ath10k_sdio_diag_read32(struct ath10k *ar, u32 address,
  1426. u32 *value)
  1427. {
  1428. __le32 *val;
  1429. int ret;
  1430. val = kzalloc(sizeof(*val), GFP_KERNEL);
  1431. if (!val)
  1432. return -ENOMEM;
  1433. ret = ath10k_sdio_hif_diag_read(ar, address, val, sizeof(*val));
  1434. if (ret)
  1435. goto out;
  1436. *value = __le32_to_cpu(*val);
  1437. out:
  1438. kfree(val);
  1439. return ret;
  1440. }
  1441. static int ath10k_sdio_hif_diag_write_mem(struct ath10k *ar, u32 address,
  1442. const void *data, int nbytes)
  1443. {
  1444. int ret;
  1445. /* set write data */
  1446. ret = ath10k_sdio_write(ar, MBOX_WINDOW_DATA_ADDRESS, data, nbytes);
  1447. if (ret) {
  1448. ath10k_warn(ar,
  1449. "failed to write 0x%p to mbox window data address: %d\n",
  1450. data, ret);
  1451. return ret;
  1452. }
  1453. /* set window register, which starts the write cycle */
  1454. ret = ath10k_sdio_write32(ar, MBOX_WINDOW_WRITE_ADDR_ADDRESS, address);
  1455. if (ret) {
  1456. ath10k_warn(ar, "failed to set mbox window write address: %d", ret);
  1457. return ret;
  1458. }
  1459. return 0;
  1460. }
  1461. static int ath10k_sdio_hif_start_post(struct ath10k *ar)
  1462. {
  1463. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1464. u32 addr, val;
  1465. int ret = 0;
  1466. addr = host_interest_item_address(HI_ITEM(hi_acs_flags));
  1467. ret = ath10k_sdio_diag_read32(ar, addr, &val);
  1468. if (ret) {
  1469. ath10k_warn(ar, "unable to read hi_acs_flags : %d\n", ret);
  1470. return ret;
  1471. }
  1472. if (val & HI_ACS_FLAGS_SDIO_SWAP_MAILBOX_FW_ACK) {
  1473. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  1474. "sdio mailbox swap service enabled\n");
  1475. ar_sdio->swap_mbox = true;
  1476. } else {
  1477. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  1478. "sdio mailbox swap service disabled\n");
  1479. ar_sdio->swap_mbox = false;
  1480. }
  1481. ath10k_sdio_set_mbox_sleep(ar, true);
  1482. return 0;
  1483. }
  1484. static int ath10k_sdio_get_htt_tx_complete(struct ath10k *ar)
  1485. {
  1486. u32 addr, val;
  1487. int ret;
  1488. addr = host_interest_item_address(HI_ITEM(hi_acs_flags));
  1489. ret = ath10k_sdio_diag_read32(ar, addr, &val);
  1490. if (ret) {
  1491. ath10k_warn(ar,
  1492. "unable to read hi_acs_flags for htt tx comple : %d\n", ret);
  1493. return ret;
  1494. }
  1495. ret = (val & HI_ACS_FLAGS_SDIO_REDUCE_TX_COMPL_FW_ACK);
  1496. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio reduce tx complete fw%sack\n",
  1497. ret ? " " : " not ");
  1498. return ret;
  1499. }
  1500. /* HIF start/stop */
  1501. static int ath10k_sdio_hif_start(struct ath10k *ar)
  1502. {
  1503. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1504. int ret;
  1505. ath10k_core_napi_enable(ar);
  1506. /* Sleep 20 ms before HIF interrupts are disabled.
  1507. * This will give target plenty of time to process the BMI done
  1508. * request before interrupts are disabled.
  1509. */
  1510. msleep(20);
  1511. ret = ath10k_sdio_disable_intrs(ar);
  1512. if (ret)
  1513. return ret;
  1514. /* eid 0 always uses the lower part of the extended mailbox address
  1515. * space (ext_info[0].htc_ext_addr).
  1516. */
  1517. ar_sdio->mbox_addr[0] = ar_sdio->mbox_info.ext_info[0].htc_ext_addr;
  1518. ar_sdio->mbox_size[0] = ar_sdio->mbox_info.ext_info[0].htc_ext_sz;
  1519. sdio_claim_host(ar_sdio->func);
  1520. /* Register the isr */
  1521. ret = sdio_claim_irq(ar_sdio->func, ath10k_sdio_irq_handler);
  1522. if (ret) {
  1523. ath10k_warn(ar, "failed to claim sdio interrupt: %d\n", ret);
  1524. sdio_release_host(ar_sdio->func);
  1525. return ret;
  1526. }
  1527. sdio_release_host(ar_sdio->func);
  1528. ret = ath10k_sdio_enable_intrs(ar);
  1529. if (ret)
  1530. ath10k_warn(ar, "failed to enable sdio interrupts: %d\n", ret);
  1531. /* Enable sleep and then disable it again */
  1532. ret = ath10k_sdio_set_mbox_sleep(ar, true);
  1533. if (ret)
  1534. return ret;
  1535. /* Wait for 20ms for the written value to take effect */
  1536. msleep(20);
  1537. ret = ath10k_sdio_set_mbox_sleep(ar, false);
  1538. if (ret)
  1539. return ret;
  1540. return 0;
  1541. }
  1542. #define SDIO_IRQ_DISABLE_TIMEOUT_HZ (3 * HZ)
  1543. static void ath10k_sdio_irq_disable(struct ath10k *ar)
  1544. {
  1545. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1546. struct ath10k_sdio_irq_data *irq_data = &ar_sdio->irq_data;
  1547. struct ath10k_sdio_irq_enable_regs *regs = irq_data->irq_en_reg;
  1548. struct sk_buff *skb;
  1549. struct completion irqs_disabled_comp;
  1550. int ret;
  1551. skb = dev_alloc_skb(sizeof(*regs));
  1552. if (!skb)
  1553. return;
  1554. mutex_lock(&irq_data->mtx);
  1555. memset(regs, 0, sizeof(*regs)); /* disable all interrupts */
  1556. memcpy(skb->data, regs, sizeof(*regs));
  1557. skb_put(skb, sizeof(*regs));
  1558. mutex_unlock(&irq_data->mtx);
  1559. init_completion(&irqs_disabled_comp);
  1560. ret = ath10k_sdio_prep_async_req(ar, MBOX_INT_STATUS_ENABLE_ADDRESS,
  1561. skb, &irqs_disabled_comp, false, 0);
  1562. if (ret)
  1563. goto out;
  1564. queue_work(ar_sdio->workqueue, &ar_sdio->wr_async_work);
  1565. /* Wait for the completion of the IRQ disable request.
  1566. * If there is a timeout we will try to disable irq's anyway.
  1567. */
  1568. ret = wait_for_completion_timeout(&irqs_disabled_comp,
  1569. SDIO_IRQ_DISABLE_TIMEOUT_HZ);
  1570. if (!ret)
  1571. ath10k_warn(ar, "sdio irq disable request timed out\n");
  1572. sdio_claim_host(ar_sdio->func);
  1573. ret = sdio_release_irq(ar_sdio->func);
  1574. if (ret)
  1575. ath10k_warn(ar, "failed to release sdio interrupt: %d\n", ret);
  1576. sdio_release_host(ar_sdio->func);
  1577. out:
  1578. kfree_skb(skb);
  1579. }
  1580. static void ath10k_sdio_hif_stop(struct ath10k *ar)
  1581. {
  1582. struct ath10k_sdio_bus_request *req, *tmp_req;
  1583. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1584. struct sk_buff *skb;
  1585. ath10k_sdio_irq_disable(ar);
  1586. cancel_work_sync(&ar_sdio->async_work_rx);
  1587. while ((skb = skb_dequeue(&ar_sdio->rx_head)))
  1588. dev_kfree_skb_any(skb);
  1589. cancel_work_sync(&ar_sdio->wr_async_work);
  1590. spin_lock_bh(&ar_sdio->wr_async_lock);
  1591. /* Free all bus requests that have not been handled */
  1592. list_for_each_entry_safe(req, tmp_req, &ar_sdio->wr_asyncq, list) {
  1593. struct ath10k_htc_ep *ep;
  1594. list_del(&req->list);
  1595. if (req->htc_msg) {
  1596. ep = &ar->htc.endpoint[req->eid];
  1597. ath10k_htc_notify_tx_completion(ep, req->skb);
  1598. } else if (req->skb) {
  1599. kfree_skb(req->skb);
  1600. }
  1601. ath10k_sdio_free_bus_req(ar, req);
  1602. }
  1603. spin_unlock_bh(&ar_sdio->wr_async_lock);
  1604. ath10k_core_napi_sync_disable(ar);
  1605. }
  1606. #ifdef CONFIG_PM
  1607. static int ath10k_sdio_hif_suspend(struct ath10k *ar)
  1608. {
  1609. return 0;
  1610. }
  1611. static int ath10k_sdio_hif_resume(struct ath10k *ar)
  1612. {
  1613. switch (ar->state) {
  1614. case ATH10K_STATE_OFF:
  1615. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  1616. "sdio resume configuring sdio\n");
  1617. /* need to set sdio settings after power is cut from sdio */
  1618. ath10k_sdio_config(ar);
  1619. break;
  1620. case ATH10K_STATE_ON:
  1621. default:
  1622. break;
  1623. }
  1624. return 0;
  1625. }
  1626. #endif
  1627. static int ath10k_sdio_hif_map_service_to_pipe(struct ath10k *ar,
  1628. u16 service_id,
  1629. u8 *ul_pipe, u8 *dl_pipe)
  1630. {
  1631. struct ath10k_sdio *ar_sdio = ath10k_sdio_priv(ar);
  1632. struct ath10k_htc *htc = &ar->htc;
  1633. u32 htt_addr, wmi_addr, htt_mbox_size, wmi_mbox_size;
  1634. enum ath10k_htc_ep_id eid;
  1635. bool ep_found = false;
  1636. int i;
  1637. /* For sdio, we are interested in the mapping between eid
  1638. * and pipeid rather than service_id to pipe_id.
  1639. * First we find out which eid has been allocated to the
  1640. * service...
  1641. */
  1642. for (i = 0; i < ATH10K_HTC_EP_COUNT; i++) {
  1643. if (htc->endpoint[i].service_id == service_id) {
  1644. eid = htc->endpoint[i].eid;
  1645. ep_found = true;
  1646. break;
  1647. }
  1648. }
  1649. if (!ep_found)
  1650. return -EINVAL;
  1651. /* Then we create the simplest mapping possible between pipeid
  1652. * and eid
  1653. */
  1654. *ul_pipe = *dl_pipe = (u8)eid;
  1655. /* Normally, HTT will use the upper part of the extended
  1656. * mailbox address space (ext_info[1].htc_ext_addr) and WMI ctrl
  1657. * the lower part (ext_info[0].htc_ext_addr).
  1658. * If fw wants swapping of mailbox addresses, the opposite is true.
  1659. */
  1660. if (ar_sdio->swap_mbox) {
  1661. htt_addr = ar_sdio->mbox_info.ext_info[0].htc_ext_addr;
  1662. wmi_addr = ar_sdio->mbox_info.ext_info[1].htc_ext_addr;
  1663. htt_mbox_size = ar_sdio->mbox_info.ext_info[0].htc_ext_sz;
  1664. wmi_mbox_size = ar_sdio->mbox_info.ext_info[1].htc_ext_sz;
  1665. } else {
  1666. htt_addr = ar_sdio->mbox_info.ext_info[1].htc_ext_addr;
  1667. wmi_addr = ar_sdio->mbox_info.ext_info[0].htc_ext_addr;
  1668. htt_mbox_size = ar_sdio->mbox_info.ext_info[1].htc_ext_sz;
  1669. wmi_mbox_size = ar_sdio->mbox_info.ext_info[0].htc_ext_sz;
  1670. }
  1671. switch (service_id) {
  1672. case ATH10K_HTC_SVC_ID_RSVD_CTRL:
  1673. /* HTC ctrl ep mbox address has already been setup in
  1674. * ath10k_sdio_hif_start
  1675. */
  1676. break;
  1677. case ATH10K_HTC_SVC_ID_WMI_CONTROL:
  1678. ar_sdio->mbox_addr[eid] = wmi_addr;
  1679. ar_sdio->mbox_size[eid] = wmi_mbox_size;
  1680. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  1681. "sdio wmi ctrl mbox_addr 0x%x mbox_size %d\n",
  1682. ar_sdio->mbox_addr[eid], ar_sdio->mbox_size[eid]);
  1683. break;
  1684. case ATH10K_HTC_SVC_ID_HTT_DATA_MSG:
  1685. ar_sdio->mbox_addr[eid] = htt_addr;
  1686. ar_sdio->mbox_size[eid] = htt_mbox_size;
  1687. ath10k_dbg(ar, ATH10K_DBG_SDIO,
  1688. "sdio htt data mbox_addr 0x%x mbox_size %d\n",
  1689. ar_sdio->mbox_addr[eid], ar_sdio->mbox_size[eid]);
  1690. break;
  1691. default:
  1692. ath10k_warn(ar, "unsupported HTC service id: %d\n",
  1693. service_id);
  1694. return -EINVAL;
  1695. }
  1696. return 0;
  1697. }
  1698. static void ath10k_sdio_hif_get_default_pipe(struct ath10k *ar,
  1699. u8 *ul_pipe, u8 *dl_pipe)
  1700. {
  1701. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio hif get default pipe\n");
  1702. /* HTC ctrl ep (SVC id 1) always has eid (and pipe_id in our
  1703. * case) == 0
  1704. */
  1705. *ul_pipe = 0;
  1706. *dl_pipe = 0;
  1707. }
  1708. static const struct ath10k_hif_ops ath10k_sdio_hif_ops = {
  1709. .tx_sg = ath10k_sdio_hif_tx_sg,
  1710. .diag_read = ath10k_sdio_hif_diag_read,
  1711. .diag_write = ath10k_sdio_hif_diag_write_mem,
  1712. .exchange_bmi_msg = ath10k_sdio_bmi_exchange_msg,
  1713. .start = ath10k_sdio_hif_start,
  1714. .stop = ath10k_sdio_hif_stop,
  1715. .start_post = ath10k_sdio_hif_start_post,
  1716. .get_htt_tx_complete = ath10k_sdio_get_htt_tx_complete,
  1717. .map_service_to_pipe = ath10k_sdio_hif_map_service_to_pipe,
  1718. .get_default_pipe = ath10k_sdio_hif_get_default_pipe,
  1719. .power_up = ath10k_sdio_hif_power_up,
  1720. .power_down = ath10k_sdio_hif_power_down,
  1721. #ifdef CONFIG_PM
  1722. .suspend = ath10k_sdio_hif_suspend,
  1723. .resume = ath10k_sdio_hif_resume,
  1724. #endif
  1725. };
  1726. #ifdef CONFIG_PM_SLEEP
  1727. /* Empty handlers so that mmc subsystem doesn't remove us entirely during
  1728. * suspend. We instead follow cfg80211 suspend/resume handlers.
  1729. */
  1730. static int ath10k_sdio_pm_suspend(struct device *device)
  1731. {
  1732. struct sdio_func *func = dev_to_sdio_func(device);
  1733. struct ath10k_sdio *ar_sdio = sdio_get_drvdata(func);
  1734. struct ath10k *ar = ar_sdio->ar;
  1735. mmc_pm_flag_t pm_flag, pm_caps;
  1736. int ret;
  1737. if (!device_may_wakeup(ar->dev))
  1738. return 0;
  1739. ath10k_sdio_set_mbox_sleep(ar, true);
  1740. pm_flag = MMC_PM_KEEP_POWER;
  1741. ret = sdio_set_host_pm_flags(func, pm_flag);
  1742. if (ret) {
  1743. pm_caps = sdio_get_host_pm_caps(func);
  1744. ath10k_warn(ar, "failed to set sdio host pm flags (0x%x, 0x%x): %d\n",
  1745. pm_flag, pm_caps, ret);
  1746. return ret;
  1747. }
  1748. return ret;
  1749. }
  1750. static int ath10k_sdio_pm_resume(struct device *device)
  1751. {
  1752. return 0;
  1753. }
  1754. static SIMPLE_DEV_PM_OPS(ath10k_sdio_pm_ops, ath10k_sdio_pm_suspend,
  1755. ath10k_sdio_pm_resume);
  1756. #define ATH10K_SDIO_PM_OPS (&ath10k_sdio_pm_ops)
  1757. #else
  1758. #define ATH10K_SDIO_PM_OPS NULL
  1759. #endif /* CONFIG_PM_SLEEP */
  1760. static int ath10k_sdio_napi_poll(struct napi_struct *ctx, int budget)
  1761. {
  1762. struct ath10k *ar = container_of(ctx, struct ath10k, napi);
  1763. int done;
  1764. done = ath10k_htt_rx_hl_indication(ar, budget);
  1765. ath10k_dbg(ar, ATH10K_DBG_SDIO, "napi poll: done: %d, budget:%d\n", done, budget);
  1766. if (done < budget)
  1767. napi_complete_done(ctx, done);
  1768. return done;
  1769. }
  1770. static int ath10k_sdio_read_host_interest_value(struct ath10k *ar,
  1771. u32 item_offset,
  1772. u32 *val)
  1773. {
  1774. u32 addr;
  1775. int ret;
  1776. addr = host_interest_item_address(item_offset);
  1777. ret = ath10k_sdio_diag_read32(ar, addr, val);
  1778. if (ret)
  1779. ath10k_warn(ar, "unable to read host interest offset %d value\n",
  1780. item_offset);
  1781. return ret;
  1782. }
  1783. static int ath10k_sdio_read_mem(struct ath10k *ar, u32 address, void *buf,
  1784. u32 buf_len)
  1785. {
  1786. u32 val;
  1787. int i, ret;
  1788. for (i = 0; i < buf_len; i += 4) {
  1789. ret = ath10k_sdio_diag_read32(ar, address + i, &val);
  1790. if (ret) {
  1791. ath10k_warn(ar, "unable to read mem %d value\n", address + i);
  1792. break;
  1793. }
  1794. memcpy(buf + i, &val, 4);
  1795. }
  1796. return ret;
  1797. }
  1798. static bool ath10k_sdio_is_fast_dump_supported(struct ath10k *ar)
  1799. {
  1800. u32 param;
  1801. ath10k_sdio_read_host_interest_value(ar, HI_ITEM(hi_option_flag2), &param);
  1802. ath10k_dbg(ar, ATH10K_DBG_SDIO, "sdio hi_option_flag2 %x\n", param);
  1803. return !!(param & HI_OPTION_SDIO_CRASH_DUMP_ENHANCEMENT_FW);
  1804. }
  1805. static void ath10k_sdio_dump_registers(struct ath10k *ar,
  1806. struct ath10k_fw_crash_data *crash_data,
  1807. bool fast_dump)
  1808. {
  1809. u32 reg_dump_values[REG_DUMP_COUNT_QCA988X] = {};
  1810. int i, ret;
  1811. u32 reg_dump_area;
  1812. ret = ath10k_sdio_read_host_interest_value(ar, HI_ITEM(hi_failure_state),
  1813. &reg_dump_area);
  1814. if (ret) {
  1815. ath10k_warn(ar, "failed to read firmware dump area: %d\n", ret);
  1816. return;
  1817. }
  1818. if (fast_dump)
  1819. ret = ath10k_bmi_read_memory(ar, reg_dump_area, reg_dump_values,
  1820. sizeof(reg_dump_values));
  1821. else
  1822. ret = ath10k_sdio_read_mem(ar, reg_dump_area, reg_dump_values,
  1823. sizeof(reg_dump_values));
  1824. if (ret) {
  1825. ath10k_warn(ar, "failed to read firmware dump value: %d\n", ret);
  1826. return;
  1827. }
  1828. ath10k_err(ar, "firmware register dump:\n");
  1829. for (i = 0; i < ARRAY_SIZE(reg_dump_values); i += 4)
  1830. ath10k_err(ar, "[%02d]: 0x%08X 0x%08X 0x%08X 0x%08X\n",
  1831. i,
  1832. reg_dump_values[i],
  1833. reg_dump_values[i + 1],
  1834. reg_dump_values[i + 2],
  1835. reg_dump_values[i + 3]);
  1836. if (!crash_data)
  1837. return;
  1838. for (i = 0; i < ARRAY_SIZE(reg_dump_values); i++)
  1839. crash_data->registers[i] = __cpu_to_le32(reg_dump_values[i]);
  1840. }
  1841. static int ath10k_sdio_dump_memory_section(struct ath10k *ar,
  1842. const struct ath10k_mem_region *mem_region,
  1843. u8 *buf, size_t buf_len)
  1844. {
  1845. const struct ath10k_mem_section *cur_section, *next_section;
  1846. unsigned int count, section_size, skip_size;
  1847. int ret, i, j;
  1848. if (!mem_region || !buf)
  1849. return 0;
  1850. cur_section = &mem_region->section_table.sections[0];
  1851. if (mem_region->start > cur_section->start) {
  1852. ath10k_warn(ar, "incorrect memdump region 0x%x with section start address 0x%x.\n",
  1853. mem_region->start, cur_section->start);
  1854. return 0;
  1855. }
  1856. skip_size = cur_section->start - mem_region->start;
  1857. /* fill the gap between the first register section and register
  1858. * start address
  1859. */
  1860. for (i = 0; i < skip_size; i++) {
  1861. *buf = ATH10K_MAGIC_NOT_COPIED;
  1862. buf++;
  1863. }
  1864. count = 0;
  1865. i = 0;
  1866. for (; cur_section; cur_section = next_section) {
  1867. section_size = cur_section->end - cur_section->start;
  1868. if (section_size <= 0) {
  1869. ath10k_warn(ar, "incorrect ramdump format with start address 0x%x and stop address 0x%x\n",
  1870. cur_section->start,
  1871. cur_section->end);
  1872. break;
  1873. }
  1874. if (++i == mem_region->section_table.size) {
  1875. /* last section */
  1876. next_section = NULL;
  1877. skip_size = 0;
  1878. } else {
  1879. next_section = cur_section + 1;
  1880. if (cur_section->end > next_section->start) {
  1881. ath10k_warn(ar, "next ramdump section 0x%x is smaller than current end address 0x%x\n",
  1882. next_section->start,
  1883. cur_section->end);
  1884. break;
  1885. }
  1886. skip_size = next_section->start - cur_section->end;
  1887. }
  1888. if (buf_len < (skip_size + section_size)) {
  1889. ath10k_warn(ar, "ramdump buffer is too small: %zu\n", buf_len);
  1890. break;
  1891. }
  1892. buf_len -= skip_size + section_size;
  1893. /* read section to dest memory */
  1894. ret = ath10k_sdio_read_mem(ar, cur_section->start,
  1895. buf, section_size);
  1896. if (ret) {
  1897. ath10k_warn(ar, "failed to read ramdump from section 0x%x: %d\n",
  1898. cur_section->start, ret);
  1899. break;
  1900. }
  1901. buf += section_size;
  1902. count += section_size;
  1903. /* fill in the gap between this section and the next */
  1904. for (j = 0; j < skip_size; j++) {
  1905. *buf = ATH10K_MAGIC_NOT_COPIED;
  1906. buf++;
  1907. }
  1908. count += skip_size;
  1909. }
  1910. return count;
  1911. }
  1912. /* if an error happened returns < 0, otherwise the length */
  1913. static int ath10k_sdio_dump_memory_generic(struct ath10k *ar,
  1914. const struct ath10k_mem_region *current_region,
  1915. u8 *buf,
  1916. bool fast_dump)
  1917. {
  1918. int ret;
  1919. if (current_region->section_table.size > 0)
  1920. /* Copy each section individually. */
  1921. return ath10k_sdio_dump_memory_section(ar,
  1922. current_region,
  1923. buf,
  1924. current_region->len);
  1925. /* No individual memory sections defined so we can
  1926. * copy the entire memory region.
  1927. */
  1928. if (fast_dump)
  1929. ret = ath10k_bmi_read_memory(ar,
  1930. current_region->start,
  1931. buf,
  1932. current_region->len);
  1933. else
  1934. ret = ath10k_sdio_read_mem(ar,
  1935. current_region->start,
  1936. buf,
  1937. current_region->len);
  1938. if (ret) {
  1939. ath10k_warn(ar, "failed to copy ramdump region %s: %d\n",
  1940. current_region->name, ret);
  1941. return ret;
  1942. }
  1943. return current_region->len;
  1944. }
  1945. static void ath10k_sdio_dump_memory(struct ath10k *ar,
  1946. struct ath10k_fw_crash_data *crash_data,
  1947. bool fast_dump)
  1948. {
  1949. const struct ath10k_hw_mem_layout *mem_layout;
  1950. const struct ath10k_mem_region *current_region;
  1951. struct ath10k_dump_ram_data_hdr *hdr;
  1952. u32 count;
  1953. size_t buf_len;
  1954. int ret, i;
  1955. u8 *buf;
  1956. if (!crash_data)
  1957. return;
  1958. mem_layout = ath10k_coredump_get_mem_layout(ar);
  1959. if (!mem_layout)
  1960. return;
  1961. current_region = &mem_layout->region_table.regions[0];
  1962. buf = crash_data->ramdump_buf;
  1963. buf_len = crash_data->ramdump_buf_len;
  1964. memset(buf, 0, buf_len);
  1965. for (i = 0; i < mem_layout->region_table.size; i++) {
  1966. count = 0;
  1967. if (current_region->len > buf_len) {
  1968. ath10k_warn(ar, "memory region %s size %d is larger that remaining ramdump buffer size %zu\n",
  1969. current_region->name,
  1970. current_region->len,
  1971. buf_len);
  1972. break;
  1973. }
  1974. /* Reserve space for the header. */
  1975. hdr = (void *)buf;
  1976. buf += sizeof(*hdr);
  1977. buf_len -= sizeof(*hdr);
  1978. ret = ath10k_sdio_dump_memory_generic(ar, current_region, buf,
  1979. fast_dump);
  1980. if (ret >= 0)
  1981. count = ret;
  1982. hdr->region_type = cpu_to_le32(current_region->type);
  1983. hdr->start = cpu_to_le32(current_region->start);
  1984. hdr->length = cpu_to_le32(count);
  1985. if (count == 0)
  1986. /* Note: the header remains, just with zero length. */
  1987. break;
  1988. buf += count;
  1989. buf_len -= count;
  1990. current_region++;
  1991. }
  1992. }
  1993. void ath10k_sdio_fw_crashed_dump(struct ath10k *ar)
  1994. {
  1995. struct ath10k_fw_crash_data *crash_data;
  1996. char guid[UUID_STRING_LEN + 1];
  1997. bool fast_dump;
  1998. fast_dump = ath10k_sdio_is_fast_dump_supported(ar);
  1999. if (fast_dump)
  2000. ath10k_bmi_start(ar);
  2001. ar->stats.fw_crash_counter++;
  2002. ath10k_sdio_disable_intrs(ar);
  2003. crash_data = ath10k_coredump_new(ar);
  2004. if (crash_data)
  2005. scnprintf(guid, sizeof(guid), "%pUl", &crash_data->guid);
  2006. else
  2007. scnprintf(guid, sizeof(guid), "n/a");
  2008. ath10k_err(ar, "firmware crashed! (guid %s)\n", guid);
  2009. ath10k_print_driver_info(ar);
  2010. ath10k_sdio_dump_registers(ar, crash_data, fast_dump);
  2011. ath10k_sdio_dump_memory(ar, crash_data, fast_dump);
  2012. ath10k_sdio_enable_intrs(ar);
  2013. ath10k_core_start_recovery(ar);
  2014. }
  2015. static int ath10k_sdio_probe(struct sdio_func *func,
  2016. const struct sdio_device_id *id)
  2017. {
  2018. struct ath10k_sdio *ar_sdio;
  2019. struct ath10k *ar;
  2020. enum ath10k_hw_rev hw_rev;
  2021. u32 dev_id_base;
  2022. struct ath10k_bus_params bus_params = {};
  2023. int ret, i;
  2024. /* Assumption: All SDIO based chipsets (so far) are QCA6174 based.
  2025. * If there will be newer chipsets that does not use the hw reg
  2026. * setup as defined in qca6174_regs and qca6174_values, this
  2027. * assumption is no longer valid and hw_rev must be setup differently
  2028. * depending on chipset.
  2029. */
  2030. hw_rev = ATH10K_HW_QCA6174;
  2031. ar = ath10k_core_create(sizeof(*ar_sdio), &func->dev, ATH10K_BUS_SDIO,
  2032. hw_rev, &ath10k_sdio_hif_ops);
  2033. if (!ar) {
  2034. dev_err(&func->dev, "failed to allocate core\n");
  2035. return -ENOMEM;
  2036. }
  2037. netif_napi_add(ar->napi_dev, &ar->napi, ath10k_sdio_napi_poll);
  2038. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  2039. "sdio new func %d vendor 0x%x device 0x%x block 0x%x/0x%x\n",
  2040. func->num, func->vendor, func->device,
  2041. func->max_blksize, func->cur_blksize);
  2042. ar_sdio = ath10k_sdio_priv(ar);
  2043. ar_sdio->irq_data.irq_proc_reg =
  2044. devm_kzalloc(ar->dev, sizeof(struct ath10k_sdio_irq_proc_regs),
  2045. GFP_KERNEL);
  2046. if (!ar_sdio->irq_data.irq_proc_reg) {
  2047. ret = -ENOMEM;
  2048. goto err_core_destroy;
  2049. }
  2050. ar_sdio->vsg_buffer = devm_kmalloc(ar->dev, ATH10K_SDIO_VSG_BUF_SIZE, GFP_KERNEL);
  2051. if (!ar_sdio->vsg_buffer) {
  2052. ret = -ENOMEM;
  2053. goto err_core_destroy;
  2054. }
  2055. ar_sdio->irq_data.irq_en_reg =
  2056. devm_kzalloc(ar->dev, sizeof(struct ath10k_sdio_irq_enable_regs),
  2057. GFP_KERNEL);
  2058. if (!ar_sdio->irq_data.irq_en_reg) {
  2059. ret = -ENOMEM;
  2060. goto err_core_destroy;
  2061. }
  2062. ar_sdio->bmi_buf = devm_kzalloc(ar->dev, BMI_MAX_LARGE_CMDBUF_SIZE, GFP_KERNEL);
  2063. if (!ar_sdio->bmi_buf) {
  2064. ret = -ENOMEM;
  2065. goto err_core_destroy;
  2066. }
  2067. ar_sdio->func = func;
  2068. sdio_set_drvdata(func, ar_sdio);
  2069. ar_sdio->is_disabled = true;
  2070. ar_sdio->ar = ar;
  2071. spin_lock_init(&ar_sdio->lock);
  2072. spin_lock_init(&ar_sdio->wr_async_lock);
  2073. mutex_init(&ar_sdio->irq_data.mtx);
  2074. INIT_LIST_HEAD(&ar_sdio->bus_req_freeq);
  2075. INIT_LIST_HEAD(&ar_sdio->wr_asyncq);
  2076. INIT_WORK(&ar_sdio->wr_async_work, ath10k_sdio_write_async_work);
  2077. ar_sdio->workqueue = create_singlethread_workqueue("ath10k_sdio_wq");
  2078. if (!ar_sdio->workqueue) {
  2079. ret = -ENOMEM;
  2080. goto err_core_destroy;
  2081. }
  2082. for (i = 0; i < ATH10K_SDIO_BUS_REQUEST_MAX_NUM; i++)
  2083. ath10k_sdio_free_bus_req(ar, &ar_sdio->bus_req[i]);
  2084. skb_queue_head_init(&ar_sdio->rx_head);
  2085. INIT_WORK(&ar_sdio->async_work_rx, ath10k_rx_indication_async_work);
  2086. dev_id_base = (id->device & 0x0F00);
  2087. if (dev_id_base != (SDIO_DEVICE_ID_ATHEROS_AR6005 & 0x0F00) &&
  2088. dev_id_base != (SDIO_DEVICE_ID_ATHEROS_QCA9377 & 0x0F00)) {
  2089. ret = -ENODEV;
  2090. ath10k_err(ar, "unsupported device id %u (0x%x)\n",
  2091. dev_id_base, id->device);
  2092. goto err_free_wq;
  2093. }
  2094. ar->dev_id = QCA9377_1_0_DEVICE_ID;
  2095. ar->id.vendor = id->vendor;
  2096. ar->id.device = id->device;
  2097. ath10k_sdio_set_mbox_info(ar);
  2098. bus_params.dev_type = ATH10K_DEV_TYPE_HL;
  2099. /* TODO: don't know yet how to get chip_id with SDIO */
  2100. bus_params.chip_id = 0;
  2101. bus_params.hl_msdu_ids = true;
  2102. ar->hw->max_mtu = ETH_DATA_LEN;
  2103. ret = ath10k_core_register(ar, &bus_params);
  2104. if (ret) {
  2105. ath10k_err(ar, "failed to register driver core: %d\n", ret);
  2106. goto err_free_wq;
  2107. }
  2108. timer_setup(&ar_sdio->sleep_timer, ath10k_sdio_sleep_timer_handler, 0);
  2109. return 0;
  2110. err_free_wq:
  2111. destroy_workqueue(ar_sdio->workqueue);
  2112. err_core_destroy:
  2113. ath10k_core_destroy(ar);
  2114. return ret;
  2115. }
  2116. static void ath10k_sdio_remove(struct sdio_func *func)
  2117. {
  2118. struct ath10k_sdio *ar_sdio = sdio_get_drvdata(func);
  2119. struct ath10k *ar = ar_sdio->ar;
  2120. ath10k_dbg(ar, ATH10K_DBG_BOOT,
  2121. "sdio removed func %d vendor 0x%x device 0x%x\n",
  2122. func->num, func->vendor, func->device);
  2123. ath10k_core_unregister(ar);
  2124. netif_napi_del(&ar->napi);
  2125. destroy_workqueue(ar_sdio->workqueue);
  2126. ath10k_core_destroy(ar);
  2127. }
  2128. static const struct sdio_device_id ath10k_sdio_devices[] = {
  2129. {SDIO_DEVICE(SDIO_VENDOR_ID_ATHEROS, SDIO_DEVICE_ID_ATHEROS_AR6005)},
  2130. {SDIO_DEVICE(SDIO_VENDOR_ID_ATHEROS, SDIO_DEVICE_ID_ATHEROS_QCA9377)},
  2131. {},
  2132. };
  2133. MODULE_DEVICE_TABLE(sdio, ath10k_sdio_devices);
  2134. static struct sdio_driver ath10k_sdio_driver = {
  2135. .name = "ath10k_sdio",
  2136. .id_table = ath10k_sdio_devices,
  2137. .probe = ath10k_sdio_probe,
  2138. .remove = ath10k_sdio_remove,
  2139. .drv = {
  2140. .pm = ATH10K_SDIO_PM_OPS,
  2141. },
  2142. };
  2143. module_sdio_driver(ath10k_sdio_driver);
  2144. MODULE_AUTHOR("Qualcomm Atheros");
  2145. MODULE_DESCRIPTION("Driver support for Qualcomm Atheros 802.11ac WLAN SDIO devices");
  2146. MODULE_LICENSE("Dual BSD/GPL");