sdio.c 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671
  1. /*
  2. * Marvell Wireless LAN device driver: SDIO specific handling
  3. *
  4. * Copyright (C) 2011-2014, Marvell International Ltd.
  5. *
  6. * This software file (the "File") is distributed by Marvell International
  7. * Ltd. under the terms of the GNU General Public License Version 2, June 1991
  8. * (the "License"). You may use, redistribute and/or modify this File in
  9. * accordance with the terms and conditions of the License, a copy of which
  10. * is available by writing to the Free Software Foundation, Inc.,
  11. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA or on the
  12. * worldwide web at http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt.
  13. *
  14. * THE FILE IS DISTRIBUTED AS-IS, WITHOUT WARRANTY OF ANY KIND, AND THE
  15. * IMPLIED WARRANTIES OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE
  16. * ARE EXPRESSLY DISCLAIMED. The License provides additional details about
  17. * this warranty disclaimer.
  18. */
  19. #include <linux/firmware.h>
  20. #include "decl.h"
  21. #include "ioctl.h"
  22. #include "util.h"
  23. #include "fw.h"
  24. #include "main.h"
  25. #include "wmm.h"
  26. #include "11n.h"
  27. #include "sdio.h"
  28. #define SDIO_VERSION "1.0"
  29. /* The mwifiex_sdio_remove() callback function is called when
  30. * user removes this module from kernel space or ejects
  31. * the card from the slot. The driver handles these 2 cases
  32. * differently.
  33. * If the user is removing the module, the few commands (FUNC_SHUTDOWN,
  34. * HS_CANCEL etc.) are sent to the firmware.
  35. * If the card is removed, there is no need to send these command.
  36. *
  37. * The variable 'user_rmmod' is used to distinguish these two
  38. * scenarios. This flag is initialized as FALSE in case the card
  39. * is removed, and will be set to TRUE for module removal when
  40. * module_exit function is called.
  41. */
  42. static u8 user_rmmod;
  43. static struct mwifiex_if_ops sdio_ops;
  44. static unsigned long iface_work_flags;
  45. static struct semaphore add_remove_card_sem;
  46. static struct memory_type_mapping generic_mem_type_map[] = {
  47. {"DUMP", NULL, 0, 0xDD},
  48. };
  49. static struct memory_type_mapping mem_type_mapping_tbl[] = {
  50. {"ITCM", NULL, 0, 0xF0},
  51. {"DTCM", NULL, 0, 0xF1},
  52. {"SQRAM", NULL, 0, 0xF2},
  53. {"APU", NULL, 0, 0xF3},
  54. {"CIU", NULL, 0, 0xF4},
  55. {"ICU", NULL, 0, 0xF5},
  56. {"MAC", NULL, 0, 0xF6},
  57. {"EXT7", NULL, 0, 0xF7},
  58. {"EXT8", NULL, 0, 0xF8},
  59. {"EXT9", NULL, 0, 0xF9},
  60. {"EXT10", NULL, 0, 0xFA},
  61. {"EXT11", NULL, 0, 0xFB},
  62. {"EXT12", NULL, 0, 0xFC},
  63. {"EXT13", NULL, 0, 0xFD},
  64. {"EXTLAST", NULL, 0, 0xFE},
  65. };
  66. /*
  67. * SDIO probe.
  68. *
  69. * This function probes an mwifiex device and registers it. It allocates
  70. * the card structure, enables SDIO function number and initiates the
  71. * device registration and initialization procedure by adding a logical
  72. * interface.
  73. */
  74. static int
  75. mwifiex_sdio_probe(struct sdio_func *func, const struct sdio_device_id *id)
  76. {
  77. int ret;
  78. struct sdio_mmc_card *card = NULL;
  79. pr_debug("info: vendor=0x%4.04X device=0x%4.04X class=%d function=%d\n",
  80. func->vendor, func->device, func->class, func->num);
  81. card = kzalloc(sizeof(struct sdio_mmc_card), GFP_KERNEL);
  82. if (!card)
  83. return -ENOMEM;
  84. card->func = func;
  85. card->device_id = id;
  86. func->card->quirks |= MMC_QUIRK_BLKSZ_FOR_BYTE_MODE;
  87. if (id->driver_data) {
  88. struct mwifiex_sdio_device *data = (void *)id->driver_data;
  89. card->firmware = data->firmware;
  90. card->reg = data->reg;
  91. card->max_ports = data->max_ports;
  92. card->mp_agg_pkt_limit = data->mp_agg_pkt_limit;
  93. card->supports_sdio_new_mode = data->supports_sdio_new_mode;
  94. card->has_control_mask = data->has_control_mask;
  95. card->tx_buf_size = data->tx_buf_size;
  96. card->mp_tx_agg_buf_size = data->mp_tx_agg_buf_size;
  97. card->mp_rx_agg_buf_size = data->mp_rx_agg_buf_size;
  98. card->can_dump_fw = data->can_dump_fw;
  99. card->fw_dump_enh = data->fw_dump_enh;
  100. card->can_auto_tdls = data->can_auto_tdls;
  101. card->can_ext_scan = data->can_ext_scan;
  102. }
  103. sdio_claim_host(func);
  104. ret = sdio_enable_func(func);
  105. sdio_release_host(func);
  106. if (ret) {
  107. pr_err("%s: failed to enable function\n", __func__);
  108. kfree(card);
  109. return -EIO;
  110. }
  111. if (mwifiex_add_card(card, &add_remove_card_sem, &sdio_ops,
  112. MWIFIEX_SDIO)) {
  113. pr_err("%s: add card failed\n", __func__);
  114. kfree(card);
  115. sdio_claim_host(func);
  116. ret = sdio_disable_func(func);
  117. sdio_release_host(func);
  118. ret = -1;
  119. }
  120. return ret;
  121. }
  122. /*
  123. * SDIO resume.
  124. *
  125. * Kernel needs to suspend all functions separately. Therefore all
  126. * registered functions must have drivers with suspend and resume
  127. * methods. Failing that the kernel simply removes the whole card.
  128. *
  129. * If already not resumed, this function turns on the traffic and
  130. * sends a host sleep cancel request to the firmware.
  131. */
  132. static int mwifiex_sdio_resume(struct device *dev)
  133. {
  134. struct sdio_func *func = dev_to_sdio_func(dev);
  135. struct sdio_mmc_card *card;
  136. struct mwifiex_adapter *adapter;
  137. mmc_pm_flag_t pm_flag = 0;
  138. if (func) {
  139. pm_flag = sdio_get_host_pm_caps(func);
  140. card = sdio_get_drvdata(func);
  141. if (!card || !card->adapter) {
  142. pr_err("resume: invalid card or adapter\n");
  143. return 0;
  144. }
  145. } else {
  146. pr_err("resume: sdio_func is not specified\n");
  147. return 0;
  148. }
  149. adapter = card->adapter;
  150. if (!adapter->is_suspended) {
  151. mwifiex_dbg(adapter, WARN,
  152. "device already resumed\n");
  153. return 0;
  154. }
  155. adapter->is_suspended = false;
  156. /* Disable Host Sleep */
  157. mwifiex_cancel_hs(mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_STA),
  158. MWIFIEX_ASYNC_CMD);
  159. return 0;
  160. }
  161. /*
  162. * SDIO remove.
  163. *
  164. * This function removes the interface and frees up the card structure.
  165. */
  166. static void
  167. mwifiex_sdio_remove(struct sdio_func *func)
  168. {
  169. struct sdio_mmc_card *card;
  170. struct mwifiex_adapter *adapter;
  171. struct mwifiex_private *priv;
  172. card = sdio_get_drvdata(func);
  173. if (!card)
  174. return;
  175. adapter = card->adapter;
  176. if (!adapter || !adapter->priv_num)
  177. return;
  178. mwifiex_dbg(adapter, INFO, "info: SDIO func num=%d\n", func->num);
  179. if (user_rmmod) {
  180. if (adapter->is_suspended)
  181. mwifiex_sdio_resume(adapter->dev);
  182. mwifiex_deauthenticate_all(adapter);
  183. priv = mwifiex_get_priv(adapter, MWIFIEX_BSS_ROLE_ANY);
  184. mwifiex_disable_auto_ds(priv);
  185. mwifiex_init_shutdown_fw(priv, MWIFIEX_FUNC_SHUTDOWN);
  186. }
  187. mwifiex_remove_card(card->adapter, &add_remove_card_sem);
  188. }
  189. /*
  190. * SDIO suspend.
  191. *
  192. * Kernel needs to suspend all functions separately. Therefore all
  193. * registered functions must have drivers with suspend and resume
  194. * methods. Failing that the kernel simply removes the whole card.
  195. *
  196. * If already not suspended, this function allocates and sends a host
  197. * sleep activate request to the firmware and turns off the traffic.
  198. */
  199. static int mwifiex_sdio_suspend(struct device *dev)
  200. {
  201. struct sdio_func *func = dev_to_sdio_func(dev);
  202. struct sdio_mmc_card *card;
  203. struct mwifiex_adapter *adapter;
  204. mmc_pm_flag_t pm_flag = 0;
  205. int ret = 0;
  206. if (func) {
  207. pm_flag = sdio_get_host_pm_caps(func);
  208. pr_debug("cmd: %s: suspend: PM flag = 0x%x\n",
  209. sdio_func_id(func), pm_flag);
  210. if (!(pm_flag & MMC_PM_KEEP_POWER)) {
  211. pr_err("%s: cannot remain alive while host is"
  212. " suspended\n", sdio_func_id(func));
  213. return -ENOSYS;
  214. }
  215. card = sdio_get_drvdata(func);
  216. if (!card || !card->adapter) {
  217. pr_err("suspend: invalid card or adapter\n");
  218. return 0;
  219. }
  220. } else {
  221. pr_err("suspend: sdio_func is not specified\n");
  222. return 0;
  223. }
  224. adapter = card->adapter;
  225. /* Enable the Host Sleep */
  226. if (!mwifiex_enable_hs(adapter)) {
  227. mwifiex_dbg(adapter, ERROR,
  228. "cmd: failed to suspend\n");
  229. adapter->hs_enabling = false;
  230. return -EFAULT;
  231. }
  232. mwifiex_dbg(adapter, INFO,
  233. "cmd: suspend with MMC_PM_KEEP_POWER\n");
  234. ret = sdio_set_host_pm_flags(func, MMC_PM_KEEP_POWER);
  235. /* Indicate device suspended */
  236. adapter->is_suspended = true;
  237. adapter->hs_enabling = false;
  238. return ret;
  239. }
  240. /* Device ID for SD8786 */
  241. #define SDIO_DEVICE_ID_MARVELL_8786 (0x9116)
  242. /* Device ID for SD8787 */
  243. #define SDIO_DEVICE_ID_MARVELL_8787 (0x9119)
  244. /* Device ID for SD8797 */
  245. #define SDIO_DEVICE_ID_MARVELL_8797 (0x9129)
  246. /* Device ID for SD8897 */
  247. #define SDIO_DEVICE_ID_MARVELL_8897 (0x912d)
  248. /* Device ID for SD8887 */
  249. #define SDIO_DEVICE_ID_MARVELL_8887 (0x9135)
  250. /* Device ID for SD8801 */
  251. #define SDIO_DEVICE_ID_MARVELL_8801 (0x9139)
  252. /* Device ID for SD8997 */
  253. #define SDIO_DEVICE_ID_MARVELL_8997 (0x9141)
  254. /* WLAN IDs */
  255. static const struct sdio_device_id mwifiex_ids[] = {
  256. {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8786),
  257. .driver_data = (unsigned long) &mwifiex_sdio_sd8786},
  258. {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8787),
  259. .driver_data = (unsigned long) &mwifiex_sdio_sd8787},
  260. {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8797),
  261. .driver_data = (unsigned long) &mwifiex_sdio_sd8797},
  262. {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8897),
  263. .driver_data = (unsigned long) &mwifiex_sdio_sd8897},
  264. {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8887),
  265. .driver_data = (unsigned long)&mwifiex_sdio_sd8887},
  266. {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8801),
  267. .driver_data = (unsigned long)&mwifiex_sdio_sd8801},
  268. {SDIO_DEVICE(SDIO_VENDOR_ID_MARVELL, SDIO_DEVICE_ID_MARVELL_8997),
  269. .driver_data = (unsigned long)&mwifiex_sdio_sd8997},
  270. {},
  271. };
  272. MODULE_DEVICE_TABLE(sdio, mwifiex_ids);
  273. static const struct dev_pm_ops mwifiex_sdio_pm_ops = {
  274. .suspend = mwifiex_sdio_suspend,
  275. .resume = mwifiex_sdio_resume,
  276. };
  277. static struct sdio_driver mwifiex_sdio = {
  278. .name = "mwifiex_sdio",
  279. .id_table = mwifiex_ids,
  280. .probe = mwifiex_sdio_probe,
  281. .remove = mwifiex_sdio_remove,
  282. .drv = {
  283. .owner = THIS_MODULE,
  284. .pm = &mwifiex_sdio_pm_ops,
  285. }
  286. };
  287. /* Write data into SDIO card register. Caller claims SDIO device. */
  288. static int
  289. mwifiex_write_reg_locked(struct sdio_func *func, u32 reg, u8 data)
  290. {
  291. int ret = -1;
  292. sdio_writeb(func, data, reg, &ret);
  293. return ret;
  294. }
  295. /*
  296. * This function writes data into SDIO card register.
  297. */
  298. static int
  299. mwifiex_write_reg(struct mwifiex_adapter *adapter, u32 reg, u8 data)
  300. {
  301. struct sdio_mmc_card *card = adapter->card;
  302. int ret;
  303. sdio_claim_host(card->func);
  304. ret = mwifiex_write_reg_locked(card->func, reg, data);
  305. sdio_release_host(card->func);
  306. return ret;
  307. }
  308. /*
  309. * This function reads data from SDIO card register.
  310. */
  311. static int
  312. mwifiex_read_reg(struct mwifiex_adapter *adapter, u32 reg, u8 *data)
  313. {
  314. struct sdio_mmc_card *card = adapter->card;
  315. int ret = -1;
  316. u8 val;
  317. sdio_claim_host(card->func);
  318. val = sdio_readb(card->func, reg, &ret);
  319. sdio_release_host(card->func);
  320. *data = val;
  321. return ret;
  322. }
  323. /*
  324. * This function writes multiple data into SDIO card memory.
  325. *
  326. * This does not work in suspended mode.
  327. */
  328. static int
  329. mwifiex_write_data_sync(struct mwifiex_adapter *adapter,
  330. u8 *buffer, u32 pkt_len, u32 port)
  331. {
  332. struct sdio_mmc_card *card = adapter->card;
  333. int ret;
  334. u8 blk_mode =
  335. (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE : BLOCK_MODE;
  336. u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
  337. u32 blk_cnt =
  338. (blk_mode ==
  339. BLOCK_MODE) ? (pkt_len /
  340. MWIFIEX_SDIO_BLOCK_SIZE) : pkt_len;
  341. u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
  342. if (adapter->is_suspended) {
  343. mwifiex_dbg(adapter, ERROR,
  344. "%s: not allowed while suspended\n", __func__);
  345. return -1;
  346. }
  347. sdio_claim_host(card->func);
  348. ret = sdio_writesb(card->func, ioport, buffer, blk_cnt * blk_size);
  349. sdio_release_host(card->func);
  350. return ret;
  351. }
  352. /*
  353. * This function reads multiple data from SDIO card memory.
  354. */
  355. static int mwifiex_read_data_sync(struct mwifiex_adapter *adapter, u8 *buffer,
  356. u32 len, u32 port, u8 claim)
  357. {
  358. struct sdio_mmc_card *card = adapter->card;
  359. int ret;
  360. u8 blk_mode = (port & MWIFIEX_SDIO_BYTE_MODE_MASK) ? BYTE_MODE
  361. : BLOCK_MODE;
  362. u32 blk_size = (blk_mode == BLOCK_MODE) ? MWIFIEX_SDIO_BLOCK_SIZE : 1;
  363. u32 blk_cnt = (blk_mode == BLOCK_MODE) ? (len / MWIFIEX_SDIO_BLOCK_SIZE)
  364. : len;
  365. u32 ioport = (port & MWIFIEX_SDIO_IO_PORT_MASK);
  366. if (claim)
  367. sdio_claim_host(card->func);
  368. ret = sdio_readsb(card->func, buffer, ioport, blk_cnt * blk_size);
  369. if (claim)
  370. sdio_release_host(card->func);
  371. return ret;
  372. }
  373. /*
  374. * This function wakes up the card.
  375. *
  376. * A host power up command is written to the card configuration
  377. * register to wake up the card.
  378. */
  379. static int mwifiex_pm_wakeup_card(struct mwifiex_adapter *adapter)
  380. {
  381. mwifiex_dbg(adapter, EVENT,
  382. "event: wakeup device...\n");
  383. return mwifiex_write_reg(adapter, CONFIGURATION_REG, HOST_POWER_UP);
  384. }
  385. /*
  386. * This function is called after the card has woken up.
  387. *
  388. * The card configuration register is reset.
  389. */
  390. static int mwifiex_pm_wakeup_card_complete(struct mwifiex_adapter *adapter)
  391. {
  392. mwifiex_dbg(adapter, EVENT,
  393. "cmd: wakeup device completed\n");
  394. return mwifiex_write_reg(adapter, CONFIGURATION_REG, 0);
  395. }
  396. /*
  397. * This function is used to initialize IO ports for the
  398. * chipsets supporting SDIO new mode eg SD8897.
  399. */
  400. static int mwifiex_init_sdio_new_mode(struct mwifiex_adapter *adapter)
  401. {
  402. u8 reg;
  403. struct sdio_mmc_card *card = adapter->card;
  404. adapter->ioport = MEM_PORT;
  405. /* enable sdio new mode */
  406. if (mwifiex_read_reg(adapter, card->reg->card_cfg_2_1_reg, &reg))
  407. return -1;
  408. if (mwifiex_write_reg(adapter, card->reg->card_cfg_2_1_reg,
  409. reg | CMD53_NEW_MODE))
  410. return -1;
  411. /* Configure cmd port and enable reading rx length from the register */
  412. if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_0, &reg))
  413. return -1;
  414. if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_0,
  415. reg | CMD_PORT_RD_LEN_EN))
  416. return -1;
  417. /* Enable Dnld/Upld ready auto reset for cmd port after cmd53 is
  418. * completed
  419. */
  420. if (mwifiex_read_reg(adapter, card->reg->cmd_cfg_1, &reg))
  421. return -1;
  422. if (mwifiex_write_reg(adapter, card->reg->cmd_cfg_1,
  423. reg | CMD_PORT_AUTO_EN))
  424. return -1;
  425. return 0;
  426. }
  427. /* This function initializes the IO ports.
  428. *
  429. * The following operations are performed -
  430. * - Read the IO ports (0, 1 and 2)
  431. * - Set host interrupt Reset-To-Read to clear
  432. * - Set auto re-enable interrupt
  433. */
  434. static int mwifiex_init_sdio_ioport(struct mwifiex_adapter *adapter)
  435. {
  436. u8 reg;
  437. struct sdio_mmc_card *card = adapter->card;
  438. adapter->ioport = 0;
  439. if (card->supports_sdio_new_mode) {
  440. if (mwifiex_init_sdio_new_mode(adapter))
  441. return -1;
  442. goto cont;
  443. }
  444. /* Read the IO port */
  445. if (!mwifiex_read_reg(adapter, card->reg->io_port_0_reg, &reg))
  446. adapter->ioport |= (reg & 0xff);
  447. else
  448. return -1;
  449. if (!mwifiex_read_reg(adapter, card->reg->io_port_1_reg, &reg))
  450. adapter->ioport |= ((reg & 0xff) << 8);
  451. else
  452. return -1;
  453. if (!mwifiex_read_reg(adapter, card->reg->io_port_2_reg, &reg))
  454. adapter->ioport |= ((reg & 0xff) << 16);
  455. else
  456. return -1;
  457. cont:
  458. mwifiex_dbg(adapter, INFO,
  459. "info: SDIO FUNC1 IO port: %#x\n", adapter->ioport);
  460. /* Set Host interrupt reset to read to clear */
  461. if (!mwifiex_read_reg(adapter, card->reg->host_int_rsr_reg, &reg))
  462. mwifiex_write_reg(adapter, card->reg->host_int_rsr_reg,
  463. reg | card->reg->sdio_int_mask);
  464. else
  465. return -1;
  466. /* Dnld/Upld ready set to auto reset */
  467. if (!mwifiex_read_reg(adapter, card->reg->card_misc_cfg_reg, &reg))
  468. mwifiex_write_reg(adapter, card->reg->card_misc_cfg_reg,
  469. reg | AUTO_RE_ENABLE_INT);
  470. else
  471. return -1;
  472. return 0;
  473. }
  474. /*
  475. * This function sends data to the card.
  476. */
  477. static int mwifiex_write_data_to_card(struct mwifiex_adapter *adapter,
  478. u8 *payload, u32 pkt_len, u32 port)
  479. {
  480. u32 i = 0;
  481. int ret;
  482. do {
  483. ret = mwifiex_write_data_sync(adapter, payload, pkt_len, port);
  484. if (ret) {
  485. i++;
  486. mwifiex_dbg(adapter, ERROR,
  487. "host_to_card, write iomem\t"
  488. "(%d) failed: %d\n", i, ret);
  489. if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
  490. mwifiex_dbg(adapter, ERROR,
  491. "write CFG reg failed\n");
  492. ret = -1;
  493. if (i > MAX_WRITE_IOMEM_RETRY)
  494. return ret;
  495. }
  496. } while (ret == -1);
  497. return ret;
  498. }
  499. /*
  500. * This function gets the read port.
  501. *
  502. * If control port bit is set in MP read bitmap, the control port
  503. * is returned, otherwise the current read port is returned and
  504. * the value is increased (provided it does not reach the maximum
  505. * limit, in which case it is reset to 1)
  506. */
  507. static int mwifiex_get_rd_port(struct mwifiex_adapter *adapter, u8 *port)
  508. {
  509. struct sdio_mmc_card *card = adapter->card;
  510. const struct mwifiex_sdio_card_reg *reg = card->reg;
  511. u32 rd_bitmap = card->mp_rd_bitmap;
  512. mwifiex_dbg(adapter, DATA,
  513. "data: mp_rd_bitmap=0x%08x\n", rd_bitmap);
  514. if (card->supports_sdio_new_mode) {
  515. if (!(rd_bitmap & reg->data_port_mask))
  516. return -1;
  517. } else {
  518. if (!(rd_bitmap & (CTRL_PORT_MASK | reg->data_port_mask)))
  519. return -1;
  520. }
  521. if ((card->has_control_mask) &&
  522. (card->mp_rd_bitmap & CTRL_PORT_MASK)) {
  523. card->mp_rd_bitmap &= (u32) (~CTRL_PORT_MASK);
  524. *port = CTRL_PORT;
  525. mwifiex_dbg(adapter, DATA,
  526. "data: port=%d mp_rd_bitmap=0x%08x\n",
  527. *port, card->mp_rd_bitmap);
  528. return 0;
  529. }
  530. if (!(card->mp_rd_bitmap & (1 << card->curr_rd_port)))
  531. return -1;
  532. /* We are now handling the SDIO data ports */
  533. card->mp_rd_bitmap &= (u32)(~(1 << card->curr_rd_port));
  534. *port = card->curr_rd_port;
  535. if (++card->curr_rd_port == card->max_ports)
  536. card->curr_rd_port = reg->start_rd_port;
  537. mwifiex_dbg(adapter, DATA,
  538. "data: port=%d mp_rd_bitmap=0x%08x -> 0x%08x\n",
  539. *port, rd_bitmap, card->mp_rd_bitmap);
  540. return 0;
  541. }
  542. /*
  543. * This function gets the write port for data.
  544. *
  545. * The current write port is returned if available and the value is
  546. * increased (provided it does not reach the maximum limit, in which
  547. * case it is reset to 1)
  548. */
  549. static int mwifiex_get_wr_port_data(struct mwifiex_adapter *adapter, u32 *port)
  550. {
  551. struct sdio_mmc_card *card = adapter->card;
  552. const struct mwifiex_sdio_card_reg *reg = card->reg;
  553. u32 wr_bitmap = card->mp_wr_bitmap;
  554. mwifiex_dbg(adapter, DATA,
  555. "data: mp_wr_bitmap=0x%08x\n", wr_bitmap);
  556. if (!(wr_bitmap & card->mp_data_port_mask)) {
  557. adapter->data_sent = true;
  558. return -EBUSY;
  559. }
  560. if (card->mp_wr_bitmap & (1 << card->curr_wr_port)) {
  561. card->mp_wr_bitmap &= (u32) (~(1 << card->curr_wr_port));
  562. *port = card->curr_wr_port;
  563. if (++card->curr_wr_port == card->mp_end_port)
  564. card->curr_wr_port = reg->start_wr_port;
  565. } else {
  566. adapter->data_sent = true;
  567. return -EBUSY;
  568. }
  569. if ((card->has_control_mask) && (*port == CTRL_PORT)) {
  570. mwifiex_dbg(adapter, ERROR,
  571. "invalid data port=%d cur port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
  572. *port, card->curr_wr_port, wr_bitmap,
  573. card->mp_wr_bitmap);
  574. return -1;
  575. }
  576. mwifiex_dbg(adapter, DATA,
  577. "data: port=%d mp_wr_bitmap=0x%08x -> 0x%08x\n",
  578. *port, wr_bitmap, card->mp_wr_bitmap);
  579. return 0;
  580. }
  581. /*
  582. * This function polls the card status.
  583. */
  584. static int
  585. mwifiex_sdio_poll_card_status(struct mwifiex_adapter *adapter, u8 bits)
  586. {
  587. struct sdio_mmc_card *card = adapter->card;
  588. u32 tries;
  589. u8 cs;
  590. for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
  591. if (mwifiex_read_reg(adapter, card->reg->poll_reg, &cs))
  592. break;
  593. else if ((cs & bits) == bits)
  594. return 0;
  595. usleep_range(10, 20);
  596. }
  597. mwifiex_dbg(adapter, ERROR,
  598. "poll card status failed, tries = %d\n", tries);
  599. return -1;
  600. }
  601. /*
  602. * This function reads the firmware status.
  603. */
  604. static int
  605. mwifiex_sdio_read_fw_status(struct mwifiex_adapter *adapter, u16 *dat)
  606. {
  607. struct sdio_mmc_card *card = adapter->card;
  608. const struct mwifiex_sdio_card_reg *reg = card->reg;
  609. u8 fws0, fws1;
  610. if (mwifiex_read_reg(adapter, reg->status_reg_0, &fws0))
  611. return -1;
  612. if (mwifiex_read_reg(adapter, reg->status_reg_1, &fws1))
  613. return -1;
  614. *dat = (u16) ((fws1 << 8) | fws0);
  615. return 0;
  616. }
  617. /*
  618. * This function disables the host interrupt.
  619. *
  620. * The host interrupt mask is read, the disable bit is reset and
  621. * written back to the card host interrupt mask register.
  622. */
  623. static void mwifiex_sdio_disable_host_int(struct mwifiex_adapter *adapter)
  624. {
  625. struct sdio_mmc_card *card = adapter->card;
  626. struct sdio_func *func = card->func;
  627. sdio_claim_host(func);
  628. mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg, 0);
  629. sdio_release_irq(func);
  630. sdio_release_host(func);
  631. }
  632. /*
  633. * This function reads the interrupt status from card.
  634. */
  635. static void mwifiex_interrupt_status(struct mwifiex_adapter *adapter)
  636. {
  637. struct sdio_mmc_card *card = adapter->card;
  638. u8 sdio_ireg;
  639. unsigned long flags;
  640. if (mwifiex_read_data_sync(adapter, card->mp_regs,
  641. card->reg->max_mp_regs,
  642. REG_PORT | MWIFIEX_SDIO_BYTE_MODE_MASK, 0)) {
  643. mwifiex_dbg(adapter, ERROR, "read mp_regs failed\n");
  644. return;
  645. }
  646. sdio_ireg = card->mp_regs[card->reg->host_int_status_reg];
  647. if (sdio_ireg) {
  648. /*
  649. * DN_LD_HOST_INT_STATUS and/or UP_LD_HOST_INT_STATUS
  650. * For SDIO new mode CMD port interrupts
  651. * DN_LD_CMD_PORT_HOST_INT_STATUS and/or
  652. * UP_LD_CMD_PORT_HOST_INT_STATUS
  653. * Clear the interrupt status register
  654. */
  655. mwifiex_dbg(adapter, INTR,
  656. "int: sdio_ireg = %#x\n", sdio_ireg);
  657. spin_lock_irqsave(&adapter->int_lock, flags);
  658. adapter->int_status |= sdio_ireg;
  659. spin_unlock_irqrestore(&adapter->int_lock, flags);
  660. }
  661. }
  662. /*
  663. * SDIO interrupt handler.
  664. *
  665. * This function reads the interrupt status from firmware and handles
  666. * the interrupt in current thread (ksdioirqd) right away.
  667. */
  668. static void
  669. mwifiex_sdio_interrupt(struct sdio_func *func)
  670. {
  671. struct mwifiex_adapter *adapter;
  672. struct sdio_mmc_card *card;
  673. card = sdio_get_drvdata(func);
  674. if (!card || !card->adapter) {
  675. pr_debug("int: func=%p card=%p adapter=%p\n",
  676. func, card, card ? card->adapter : NULL);
  677. return;
  678. }
  679. adapter = card->adapter;
  680. if (!adapter->pps_uapsd_mode && adapter->ps_state == PS_STATE_SLEEP)
  681. adapter->ps_state = PS_STATE_AWAKE;
  682. mwifiex_interrupt_status(adapter);
  683. mwifiex_main_process(adapter);
  684. }
  685. /*
  686. * This function enables the host interrupt.
  687. *
  688. * The host interrupt enable mask is written to the card
  689. * host interrupt mask register.
  690. */
  691. static int mwifiex_sdio_enable_host_int(struct mwifiex_adapter *adapter)
  692. {
  693. struct sdio_mmc_card *card = adapter->card;
  694. struct sdio_func *func = card->func;
  695. int ret;
  696. sdio_claim_host(func);
  697. /* Request the SDIO IRQ */
  698. ret = sdio_claim_irq(func, mwifiex_sdio_interrupt);
  699. if (ret) {
  700. mwifiex_dbg(adapter, ERROR,
  701. "claim irq failed: ret=%d\n", ret);
  702. goto out;
  703. }
  704. /* Simply write the mask to the register */
  705. ret = mwifiex_write_reg_locked(func, card->reg->host_int_mask_reg,
  706. card->reg->host_int_enable);
  707. if (ret) {
  708. mwifiex_dbg(adapter, ERROR,
  709. "enable host interrupt failed\n");
  710. sdio_release_irq(func);
  711. }
  712. out:
  713. sdio_release_host(func);
  714. return ret;
  715. }
  716. /*
  717. * This function sends a data buffer to the card.
  718. */
  719. static int mwifiex_sdio_card_to_host(struct mwifiex_adapter *adapter,
  720. u32 *type, u8 *buffer,
  721. u32 npayload, u32 ioport)
  722. {
  723. int ret;
  724. u32 nb;
  725. if (!buffer) {
  726. mwifiex_dbg(adapter, ERROR,
  727. "%s: buffer is NULL\n", __func__);
  728. return -1;
  729. }
  730. ret = mwifiex_read_data_sync(adapter, buffer, npayload, ioport, 1);
  731. if (ret) {
  732. mwifiex_dbg(adapter, ERROR,
  733. "%s: read iomem failed: %d\n", __func__,
  734. ret);
  735. return -1;
  736. }
  737. nb = le16_to_cpu(*(__le16 *) (buffer));
  738. if (nb > npayload) {
  739. mwifiex_dbg(adapter, ERROR,
  740. "%s: invalid packet, nb=%d npayload=%d\n",
  741. __func__, nb, npayload);
  742. return -1;
  743. }
  744. *type = le16_to_cpu(*(__le16 *) (buffer + 2));
  745. return ret;
  746. }
  747. /*
  748. * This function downloads the firmware to the card.
  749. *
  750. * Firmware is downloaded to the card in blocks. Every block download
  751. * is tested for CRC errors, and retried a number of times before
  752. * returning failure.
  753. */
  754. static int mwifiex_prog_fw_w_helper(struct mwifiex_adapter *adapter,
  755. struct mwifiex_fw_image *fw)
  756. {
  757. struct sdio_mmc_card *card = adapter->card;
  758. const struct mwifiex_sdio_card_reg *reg = card->reg;
  759. int ret;
  760. u8 *firmware = fw->fw_buf;
  761. u32 firmware_len = fw->fw_len;
  762. u32 offset = 0;
  763. u8 base0, base1;
  764. u8 *fwbuf;
  765. u16 len = 0;
  766. u32 txlen, tx_blocks = 0, tries;
  767. u32 i = 0;
  768. if (!firmware_len) {
  769. mwifiex_dbg(adapter, ERROR,
  770. "firmware image not found! Terminating download\n");
  771. return -1;
  772. }
  773. mwifiex_dbg(adapter, INFO,
  774. "info: downloading FW image (%d bytes)\n",
  775. firmware_len);
  776. /* Assume that the allocated buffer is 8-byte aligned */
  777. fwbuf = kzalloc(MWIFIEX_UPLD_SIZE, GFP_KERNEL);
  778. if (!fwbuf)
  779. return -ENOMEM;
  780. sdio_claim_host(card->func);
  781. /* Perform firmware data transfer */
  782. do {
  783. /* The host polls for the DN_LD_CARD_RDY and CARD_IO_READY
  784. bits */
  785. ret = mwifiex_sdio_poll_card_status(adapter, CARD_IO_READY |
  786. DN_LD_CARD_RDY);
  787. if (ret) {
  788. mwifiex_dbg(adapter, ERROR,
  789. "FW download with helper:\t"
  790. "poll status timeout @ %d\n", offset);
  791. goto done;
  792. }
  793. /* More data? */
  794. if (offset >= firmware_len)
  795. break;
  796. for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
  797. ret = mwifiex_read_reg(adapter, reg->base_0_reg,
  798. &base0);
  799. if (ret) {
  800. mwifiex_dbg(adapter, ERROR,
  801. "dev BASE0 register read failed:\t"
  802. "base0=%#04X(%d). Terminating dnld\n",
  803. base0, base0);
  804. goto done;
  805. }
  806. ret = mwifiex_read_reg(adapter, reg->base_1_reg,
  807. &base1);
  808. if (ret) {
  809. mwifiex_dbg(adapter, ERROR,
  810. "dev BASE1 register read failed:\t"
  811. "base1=%#04X(%d). Terminating dnld\n",
  812. base1, base1);
  813. goto done;
  814. }
  815. len = (u16) (((base1 & 0xff) << 8) | (base0 & 0xff));
  816. if (len)
  817. break;
  818. usleep_range(10, 20);
  819. }
  820. if (!len) {
  821. break;
  822. } else if (len > MWIFIEX_UPLD_SIZE) {
  823. mwifiex_dbg(adapter, ERROR,
  824. "FW dnld failed @ %d, invalid length %d\n",
  825. offset, len);
  826. ret = -1;
  827. goto done;
  828. }
  829. txlen = len;
  830. if (len & BIT(0)) {
  831. i++;
  832. if (i > MAX_WRITE_IOMEM_RETRY) {
  833. mwifiex_dbg(adapter, ERROR,
  834. "FW dnld failed @ %d, over max retry\n",
  835. offset);
  836. ret = -1;
  837. goto done;
  838. }
  839. mwifiex_dbg(adapter, ERROR,
  840. "CRC indicated by the helper:\t"
  841. "len = 0x%04X, txlen = %d\n", len, txlen);
  842. len &= ~BIT(0);
  843. /* Setting this to 0 to resend from same offset */
  844. txlen = 0;
  845. } else {
  846. i = 0;
  847. /* Set blocksize to transfer - checking for last
  848. block */
  849. if (firmware_len - offset < txlen)
  850. txlen = firmware_len - offset;
  851. tx_blocks = (txlen + MWIFIEX_SDIO_BLOCK_SIZE - 1)
  852. / MWIFIEX_SDIO_BLOCK_SIZE;
  853. /* Copy payload to buffer */
  854. memmove(fwbuf, &firmware[offset], txlen);
  855. }
  856. ret = mwifiex_write_data_sync(adapter, fwbuf, tx_blocks *
  857. MWIFIEX_SDIO_BLOCK_SIZE,
  858. adapter->ioport);
  859. if (ret) {
  860. mwifiex_dbg(adapter, ERROR,
  861. "FW download, write iomem (%d) failed @ %d\n",
  862. i, offset);
  863. if (mwifiex_write_reg(adapter, CONFIGURATION_REG, 0x04))
  864. mwifiex_dbg(adapter, ERROR,
  865. "write CFG reg failed\n");
  866. ret = -1;
  867. goto done;
  868. }
  869. offset += txlen;
  870. } while (true);
  871. sdio_release_host(card->func);
  872. mwifiex_dbg(adapter, MSG,
  873. "info: FW download over, size %d bytes\n", offset);
  874. ret = 0;
  875. done:
  876. kfree(fwbuf);
  877. return ret;
  878. }
  879. /*
  880. * This function checks the firmware status in card.
  881. *
  882. * The winner interface is also determined by this function.
  883. */
  884. static int mwifiex_check_fw_status(struct mwifiex_adapter *adapter,
  885. u32 poll_num)
  886. {
  887. struct sdio_mmc_card *card = adapter->card;
  888. int ret = 0;
  889. u16 firmware_stat;
  890. u32 tries;
  891. u8 winner_status;
  892. /* Wait for firmware initialization event */
  893. for (tries = 0; tries < poll_num; tries++) {
  894. ret = mwifiex_sdio_read_fw_status(adapter, &firmware_stat);
  895. if (ret)
  896. continue;
  897. if (firmware_stat == FIRMWARE_READY_SDIO) {
  898. ret = 0;
  899. break;
  900. } else {
  901. msleep(100);
  902. ret = -1;
  903. }
  904. }
  905. if (ret) {
  906. if (mwifiex_read_reg
  907. (adapter, card->reg->status_reg_0, &winner_status))
  908. winner_status = 0;
  909. if (winner_status)
  910. adapter->winner = 0;
  911. else
  912. adapter->winner = 1;
  913. }
  914. return ret;
  915. }
  916. /*
  917. * This function decode sdio aggreation pkt.
  918. *
  919. * Based on the the data block size and pkt_len,
  920. * skb data will be decoded to few packets.
  921. */
  922. static void mwifiex_deaggr_sdio_pkt(struct mwifiex_adapter *adapter,
  923. struct sk_buff *skb)
  924. {
  925. u32 total_pkt_len, pkt_len;
  926. struct sk_buff *skb_deaggr;
  927. u32 pkt_type;
  928. u16 blk_size;
  929. u8 blk_num;
  930. u8 *data;
  931. data = skb->data;
  932. total_pkt_len = skb->len;
  933. while (total_pkt_len >= (SDIO_HEADER_OFFSET + INTF_HEADER_LEN)) {
  934. if (total_pkt_len < adapter->sdio_rx_block_size)
  935. break;
  936. blk_num = *(data + BLOCK_NUMBER_OFFSET);
  937. blk_size = adapter->sdio_rx_block_size * blk_num;
  938. if (blk_size > total_pkt_len) {
  939. mwifiex_dbg(adapter, ERROR,
  940. "%s: error in blk_size,\t"
  941. "blk_num=%d, blk_size=%d, total_pkt_len=%d\n",
  942. __func__, blk_num, blk_size, total_pkt_len);
  943. break;
  944. }
  945. pkt_len = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET));
  946. pkt_type = le16_to_cpu(*(__le16 *)(data + SDIO_HEADER_OFFSET +
  947. 2));
  948. if ((pkt_len + SDIO_HEADER_OFFSET) > blk_size) {
  949. mwifiex_dbg(adapter, ERROR,
  950. "%s: error in pkt_len,\t"
  951. "pkt_len=%d, blk_size=%d\n",
  952. __func__, pkt_len, blk_size);
  953. break;
  954. }
  955. skb_deaggr = mwifiex_alloc_dma_align_buf(pkt_len,
  956. GFP_KERNEL | GFP_DMA);
  957. if (!skb_deaggr)
  958. break;
  959. skb_put(skb_deaggr, pkt_len);
  960. memcpy(skb_deaggr->data, data + SDIO_HEADER_OFFSET, pkt_len);
  961. skb_pull(skb_deaggr, INTF_HEADER_LEN);
  962. mwifiex_handle_rx_packet(adapter, skb_deaggr);
  963. data += blk_size;
  964. total_pkt_len -= blk_size;
  965. }
  966. }
  967. /*
  968. * This function decodes a received packet.
  969. *
  970. * Based on the type, the packet is treated as either a data, or
  971. * a command response, or an event, and the correct handler
  972. * function is invoked.
  973. */
  974. static int mwifiex_decode_rx_packet(struct mwifiex_adapter *adapter,
  975. struct sk_buff *skb, u32 upld_typ)
  976. {
  977. u8 *cmd_buf;
  978. __le16 *curr_ptr = (__le16 *)skb->data;
  979. u16 pkt_len = le16_to_cpu(*curr_ptr);
  980. struct mwifiex_rxinfo *rx_info;
  981. if (upld_typ != MWIFIEX_TYPE_AGGR_DATA) {
  982. skb_trim(skb, pkt_len);
  983. skb_pull(skb, INTF_HEADER_LEN);
  984. }
  985. switch (upld_typ) {
  986. case MWIFIEX_TYPE_AGGR_DATA:
  987. mwifiex_dbg(adapter, INFO,
  988. "info: --- Rx: Aggr Data packet ---\n");
  989. rx_info = MWIFIEX_SKB_RXCB(skb);
  990. rx_info->buf_type = MWIFIEX_TYPE_AGGR_DATA;
  991. if (adapter->rx_work_enabled) {
  992. skb_queue_tail(&adapter->rx_data_q, skb);
  993. atomic_inc(&adapter->rx_pending);
  994. adapter->data_received = true;
  995. } else {
  996. mwifiex_deaggr_sdio_pkt(adapter, skb);
  997. dev_kfree_skb_any(skb);
  998. }
  999. break;
  1000. case MWIFIEX_TYPE_DATA:
  1001. mwifiex_dbg(adapter, DATA,
  1002. "info: --- Rx: Data packet ---\n");
  1003. if (adapter->rx_work_enabled) {
  1004. skb_queue_tail(&adapter->rx_data_q, skb);
  1005. adapter->data_received = true;
  1006. atomic_inc(&adapter->rx_pending);
  1007. } else {
  1008. mwifiex_handle_rx_packet(adapter, skb);
  1009. }
  1010. break;
  1011. case MWIFIEX_TYPE_CMD:
  1012. mwifiex_dbg(adapter, CMD,
  1013. "info: --- Rx: Cmd Response ---\n");
  1014. /* take care of curr_cmd = NULL case */
  1015. if (!adapter->curr_cmd) {
  1016. cmd_buf = adapter->upld_buf;
  1017. if (adapter->ps_state == PS_STATE_SLEEP_CFM)
  1018. mwifiex_process_sleep_confirm_resp(adapter,
  1019. skb->data,
  1020. skb->len);
  1021. memcpy(cmd_buf, skb->data,
  1022. min_t(u32, MWIFIEX_SIZE_OF_CMD_BUFFER,
  1023. skb->len));
  1024. dev_kfree_skb_any(skb);
  1025. } else {
  1026. adapter->cmd_resp_received = true;
  1027. adapter->curr_cmd->resp_skb = skb;
  1028. }
  1029. break;
  1030. case MWIFIEX_TYPE_EVENT:
  1031. mwifiex_dbg(adapter, EVENT,
  1032. "info: --- Rx: Event ---\n");
  1033. adapter->event_cause = le32_to_cpu(*(__le32 *) skb->data);
  1034. if ((skb->len > 0) && (skb->len < MAX_EVENT_SIZE))
  1035. memcpy(adapter->event_body,
  1036. skb->data + MWIFIEX_EVENT_HEADER_LEN,
  1037. skb->len);
  1038. /* event cause has been saved to adapter->event_cause */
  1039. adapter->event_received = true;
  1040. adapter->event_skb = skb;
  1041. break;
  1042. default:
  1043. mwifiex_dbg(adapter, ERROR,
  1044. "unknown upload type %#x\n", upld_typ);
  1045. dev_kfree_skb_any(skb);
  1046. break;
  1047. }
  1048. return 0;
  1049. }
  1050. /*
  1051. * This function transfers received packets from card to driver, performing
  1052. * aggregation if required.
  1053. *
  1054. * For data received on control port, or if aggregation is disabled, the
  1055. * received buffers are uploaded as separate packets. However, if aggregation
  1056. * is enabled and required, the buffers are copied onto an aggregation buffer,
  1057. * provided there is space left, processed and finally uploaded.
  1058. */
  1059. static int mwifiex_sdio_card_to_host_mp_aggr(struct mwifiex_adapter *adapter,
  1060. u16 rx_len, u8 port)
  1061. {
  1062. struct sdio_mmc_card *card = adapter->card;
  1063. s32 f_do_rx_aggr = 0;
  1064. s32 f_do_rx_cur = 0;
  1065. s32 f_aggr_cur = 0;
  1066. s32 f_post_aggr_cur = 0;
  1067. struct sk_buff *skb_deaggr;
  1068. struct sk_buff *skb = NULL;
  1069. u32 pkt_len, pkt_type, mport, pind;
  1070. u8 *curr_ptr;
  1071. if ((card->has_control_mask) && (port == CTRL_PORT)) {
  1072. /* Read the command Resp without aggr */
  1073. mwifiex_dbg(adapter, CMD,
  1074. "info: %s: no aggregation for cmd\t"
  1075. "response\n", __func__);
  1076. f_do_rx_cur = 1;
  1077. goto rx_curr_single;
  1078. }
  1079. if (!card->mpa_rx.enabled) {
  1080. mwifiex_dbg(adapter, WARN,
  1081. "info: %s: rx aggregation disabled\n",
  1082. __func__);
  1083. f_do_rx_cur = 1;
  1084. goto rx_curr_single;
  1085. }
  1086. if ((!card->has_control_mask && (card->mp_rd_bitmap &
  1087. card->reg->data_port_mask)) ||
  1088. (card->has_control_mask && (card->mp_rd_bitmap &
  1089. (~((u32) CTRL_PORT_MASK))))) {
  1090. /* Some more data RX pending */
  1091. mwifiex_dbg(adapter, INFO,
  1092. "info: %s: not last packet\n", __func__);
  1093. if (MP_RX_AGGR_IN_PROGRESS(card)) {
  1094. if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len)) {
  1095. f_aggr_cur = 1;
  1096. } else {
  1097. /* No room in Aggr buf, do rx aggr now */
  1098. f_do_rx_aggr = 1;
  1099. f_post_aggr_cur = 1;
  1100. }
  1101. } else {
  1102. /* Rx aggr not in progress */
  1103. f_aggr_cur = 1;
  1104. }
  1105. } else {
  1106. /* No more data RX pending */
  1107. mwifiex_dbg(adapter, INFO,
  1108. "info: %s: last packet\n", __func__);
  1109. if (MP_RX_AGGR_IN_PROGRESS(card)) {
  1110. f_do_rx_aggr = 1;
  1111. if (MP_RX_AGGR_BUF_HAS_ROOM(card, rx_len))
  1112. f_aggr_cur = 1;
  1113. else
  1114. /* No room in Aggr buf, do rx aggr now */
  1115. f_do_rx_cur = 1;
  1116. } else {
  1117. f_do_rx_cur = 1;
  1118. }
  1119. }
  1120. if (f_aggr_cur) {
  1121. mwifiex_dbg(adapter, INFO,
  1122. "info: current packet aggregation\n");
  1123. /* Curr pkt can be aggregated */
  1124. mp_rx_aggr_setup(card, rx_len, port);
  1125. if (MP_RX_AGGR_PKT_LIMIT_REACHED(card) ||
  1126. mp_rx_aggr_port_limit_reached(card)) {
  1127. mwifiex_dbg(adapter, INFO,
  1128. "info: %s: aggregated packet\t"
  1129. "limit reached\n", __func__);
  1130. /* No more pkts allowed in Aggr buf, rx it */
  1131. f_do_rx_aggr = 1;
  1132. }
  1133. }
  1134. if (f_do_rx_aggr) {
  1135. /* do aggr RX now */
  1136. mwifiex_dbg(adapter, DATA,
  1137. "info: do_rx_aggr: num of packets: %d\n",
  1138. card->mpa_rx.pkt_cnt);
  1139. if (card->supports_sdio_new_mode) {
  1140. int i;
  1141. u32 port_count;
  1142. for (i = 0, port_count = 0; i < card->max_ports; i++)
  1143. if (card->mpa_rx.ports & BIT(i))
  1144. port_count++;
  1145. /* Reading data from "start_port + 0" to "start_port +
  1146. * port_count -1", so decrease the count by 1
  1147. */
  1148. port_count--;
  1149. mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
  1150. (port_count << 8)) + card->mpa_rx.start_port;
  1151. } else {
  1152. mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
  1153. (card->mpa_rx.ports << 4)) +
  1154. card->mpa_rx.start_port;
  1155. }
  1156. if (mwifiex_read_data_sync(adapter, card->mpa_rx.buf,
  1157. card->mpa_rx.buf_len, mport, 1))
  1158. goto error;
  1159. curr_ptr = card->mpa_rx.buf;
  1160. for (pind = 0; pind < card->mpa_rx.pkt_cnt; pind++) {
  1161. u32 *len_arr = card->mpa_rx.len_arr;
  1162. /* get curr PKT len & type */
  1163. pkt_len = le16_to_cpu(*(__le16 *) &curr_ptr[0]);
  1164. pkt_type = le16_to_cpu(*(__le16 *) &curr_ptr[2]);
  1165. /* copy pkt to deaggr buf */
  1166. skb_deaggr = mwifiex_alloc_dma_align_buf(len_arr[pind],
  1167. GFP_KERNEL |
  1168. GFP_DMA);
  1169. if (!skb_deaggr) {
  1170. mwifiex_dbg(adapter, ERROR, "skb allocation failure\t"
  1171. "drop pkt len=%d type=%d\n",
  1172. pkt_len, pkt_type);
  1173. curr_ptr += len_arr[pind];
  1174. continue;
  1175. }
  1176. skb_put(skb_deaggr, len_arr[pind]);
  1177. if ((pkt_type == MWIFIEX_TYPE_DATA ||
  1178. (pkt_type == MWIFIEX_TYPE_AGGR_DATA &&
  1179. adapter->sdio_rx_aggr_enable)) &&
  1180. (pkt_len <= len_arr[pind])) {
  1181. memcpy(skb_deaggr->data, curr_ptr, pkt_len);
  1182. skb_trim(skb_deaggr, pkt_len);
  1183. /* Process de-aggr packet */
  1184. mwifiex_decode_rx_packet(adapter, skb_deaggr,
  1185. pkt_type);
  1186. } else {
  1187. mwifiex_dbg(adapter, ERROR,
  1188. "drop wrong aggr pkt:\t"
  1189. "sdio_single_port_rx_aggr=%d\t"
  1190. "type=%d len=%d max_len=%d\n",
  1191. adapter->sdio_rx_aggr_enable,
  1192. pkt_type, pkt_len, len_arr[pind]);
  1193. dev_kfree_skb_any(skb_deaggr);
  1194. }
  1195. curr_ptr += len_arr[pind];
  1196. }
  1197. MP_RX_AGGR_BUF_RESET(card);
  1198. }
  1199. rx_curr_single:
  1200. if (f_do_rx_cur) {
  1201. mwifiex_dbg(adapter, INFO, "info: RX: port: %d, rx_len: %d\n",
  1202. port, rx_len);
  1203. skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL | GFP_DMA);
  1204. if (!skb) {
  1205. mwifiex_dbg(adapter, ERROR,
  1206. "single skb allocated fail,\t"
  1207. "drop pkt port=%d len=%d\n", port, rx_len);
  1208. if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
  1209. card->mpa_rx.buf, rx_len,
  1210. adapter->ioport + port))
  1211. goto error;
  1212. return 0;
  1213. }
  1214. skb_put(skb, rx_len);
  1215. if (mwifiex_sdio_card_to_host(adapter, &pkt_type,
  1216. skb->data, skb->len,
  1217. adapter->ioport + port))
  1218. goto error;
  1219. if (!adapter->sdio_rx_aggr_enable &&
  1220. pkt_type == MWIFIEX_TYPE_AGGR_DATA) {
  1221. mwifiex_dbg(adapter, ERROR, "drop wrong pkt type %d\t"
  1222. "current SDIO RX Aggr not enabled\n",
  1223. pkt_type);
  1224. dev_kfree_skb_any(skb);
  1225. return 0;
  1226. }
  1227. mwifiex_decode_rx_packet(adapter, skb, pkt_type);
  1228. }
  1229. if (f_post_aggr_cur) {
  1230. mwifiex_dbg(adapter, INFO,
  1231. "info: current packet aggregation\n");
  1232. /* Curr pkt can be aggregated */
  1233. mp_rx_aggr_setup(card, rx_len, port);
  1234. }
  1235. return 0;
  1236. error:
  1237. if (MP_RX_AGGR_IN_PROGRESS(card))
  1238. MP_RX_AGGR_BUF_RESET(card);
  1239. if (f_do_rx_cur && skb)
  1240. /* Single transfer pending. Free curr buff also */
  1241. dev_kfree_skb_any(skb);
  1242. return -1;
  1243. }
  1244. /*
  1245. * This function checks the current interrupt status.
  1246. *
  1247. * The following interrupts are checked and handled by this function -
  1248. * - Data sent
  1249. * - Command sent
  1250. * - Packets received
  1251. *
  1252. * Since the firmware does not generate download ready interrupt if the
  1253. * port updated is command port only, command sent interrupt checking
  1254. * should be done manually, and for every SDIO interrupt.
  1255. *
  1256. * In case of Rx packets received, the packets are uploaded from card to
  1257. * host and processed accordingly.
  1258. */
  1259. static int mwifiex_process_int_status(struct mwifiex_adapter *adapter)
  1260. {
  1261. struct sdio_mmc_card *card = adapter->card;
  1262. const struct mwifiex_sdio_card_reg *reg = card->reg;
  1263. int ret = 0;
  1264. u8 sdio_ireg;
  1265. struct sk_buff *skb;
  1266. u8 port = CTRL_PORT;
  1267. u32 len_reg_l, len_reg_u;
  1268. u32 rx_blocks;
  1269. u16 rx_len;
  1270. unsigned long flags;
  1271. u32 bitmap;
  1272. u8 cr;
  1273. spin_lock_irqsave(&adapter->int_lock, flags);
  1274. sdio_ireg = adapter->int_status;
  1275. adapter->int_status = 0;
  1276. spin_unlock_irqrestore(&adapter->int_lock, flags);
  1277. if (!sdio_ireg)
  1278. return ret;
  1279. /* Following interrupt is only for SDIO new mode */
  1280. if (sdio_ireg & DN_LD_CMD_PORT_HOST_INT_STATUS && adapter->cmd_sent)
  1281. adapter->cmd_sent = false;
  1282. /* Following interrupt is only for SDIO new mode */
  1283. if (sdio_ireg & UP_LD_CMD_PORT_HOST_INT_STATUS) {
  1284. u32 pkt_type;
  1285. /* read the len of control packet */
  1286. rx_len = card->mp_regs[reg->cmd_rd_len_1] << 8;
  1287. rx_len |= (u16)card->mp_regs[reg->cmd_rd_len_0];
  1288. rx_blocks = DIV_ROUND_UP(rx_len, MWIFIEX_SDIO_BLOCK_SIZE);
  1289. if (rx_len <= INTF_HEADER_LEN ||
  1290. (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
  1291. MWIFIEX_RX_DATA_BUF_SIZE)
  1292. return -1;
  1293. rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
  1294. mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n", rx_len);
  1295. skb = mwifiex_alloc_dma_align_buf(rx_len, GFP_KERNEL | GFP_DMA);
  1296. if (!skb)
  1297. return -1;
  1298. skb_put(skb, rx_len);
  1299. if (mwifiex_sdio_card_to_host(adapter, &pkt_type, skb->data,
  1300. skb->len, adapter->ioport |
  1301. CMD_PORT_SLCT)) {
  1302. mwifiex_dbg(adapter, ERROR,
  1303. "%s: failed to card_to_host", __func__);
  1304. dev_kfree_skb_any(skb);
  1305. goto term_cmd;
  1306. }
  1307. if ((pkt_type != MWIFIEX_TYPE_CMD) &&
  1308. (pkt_type != MWIFIEX_TYPE_EVENT))
  1309. mwifiex_dbg(adapter, ERROR,
  1310. "%s:Received wrong packet on cmd port",
  1311. __func__);
  1312. mwifiex_decode_rx_packet(adapter, skb, pkt_type);
  1313. }
  1314. if (sdio_ireg & DN_LD_HOST_INT_STATUS) {
  1315. bitmap = (u32) card->mp_regs[reg->wr_bitmap_l];
  1316. bitmap |= ((u32) card->mp_regs[reg->wr_bitmap_u]) << 8;
  1317. if (card->supports_sdio_new_mode) {
  1318. bitmap |=
  1319. ((u32) card->mp_regs[reg->wr_bitmap_1l]) << 16;
  1320. bitmap |=
  1321. ((u32) card->mp_regs[reg->wr_bitmap_1u]) << 24;
  1322. }
  1323. card->mp_wr_bitmap = bitmap;
  1324. mwifiex_dbg(adapter, INTR,
  1325. "int: DNLD: wr_bitmap=0x%x\n",
  1326. card->mp_wr_bitmap);
  1327. if (adapter->data_sent &&
  1328. (card->mp_wr_bitmap & card->mp_data_port_mask)) {
  1329. mwifiex_dbg(adapter, INTR,
  1330. "info: <--- Tx DONE Interrupt --->\n");
  1331. adapter->data_sent = false;
  1332. }
  1333. }
  1334. /* As firmware will not generate download ready interrupt if the port
  1335. updated is command port only, cmd_sent should be done for any SDIO
  1336. interrupt. */
  1337. if (card->has_control_mask && adapter->cmd_sent) {
  1338. /* Check if firmware has attach buffer at command port and
  1339. update just that in wr_bit_map. */
  1340. card->mp_wr_bitmap |=
  1341. (u32) card->mp_regs[reg->wr_bitmap_l] & CTRL_PORT_MASK;
  1342. if (card->mp_wr_bitmap & CTRL_PORT_MASK)
  1343. adapter->cmd_sent = false;
  1344. }
  1345. mwifiex_dbg(adapter, INTR, "info: cmd_sent=%d data_sent=%d\n",
  1346. adapter->cmd_sent, adapter->data_sent);
  1347. if (sdio_ireg & UP_LD_HOST_INT_STATUS) {
  1348. bitmap = (u32) card->mp_regs[reg->rd_bitmap_l];
  1349. bitmap |= ((u32) card->mp_regs[reg->rd_bitmap_u]) << 8;
  1350. if (card->supports_sdio_new_mode) {
  1351. bitmap |=
  1352. ((u32) card->mp_regs[reg->rd_bitmap_1l]) << 16;
  1353. bitmap |=
  1354. ((u32) card->mp_regs[reg->rd_bitmap_1u]) << 24;
  1355. }
  1356. card->mp_rd_bitmap = bitmap;
  1357. mwifiex_dbg(adapter, INTR,
  1358. "int: UPLD: rd_bitmap=0x%x\n",
  1359. card->mp_rd_bitmap);
  1360. while (true) {
  1361. ret = mwifiex_get_rd_port(adapter, &port);
  1362. if (ret) {
  1363. mwifiex_dbg(adapter, INFO,
  1364. "info: no more rd_port available\n");
  1365. break;
  1366. }
  1367. len_reg_l = reg->rd_len_p0_l + (port << 1);
  1368. len_reg_u = reg->rd_len_p0_u + (port << 1);
  1369. rx_len = ((u16) card->mp_regs[len_reg_u]) << 8;
  1370. rx_len |= (u16) card->mp_regs[len_reg_l];
  1371. mwifiex_dbg(adapter, INFO,
  1372. "info: RX: port=%d rx_len=%u\n",
  1373. port, rx_len);
  1374. rx_blocks =
  1375. (rx_len + MWIFIEX_SDIO_BLOCK_SIZE -
  1376. 1) / MWIFIEX_SDIO_BLOCK_SIZE;
  1377. if (rx_len <= INTF_HEADER_LEN ||
  1378. (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE) >
  1379. card->mpa_rx.buf_size) {
  1380. mwifiex_dbg(adapter, ERROR,
  1381. "invalid rx_len=%d\n",
  1382. rx_len);
  1383. return -1;
  1384. }
  1385. rx_len = (u16) (rx_blocks * MWIFIEX_SDIO_BLOCK_SIZE);
  1386. mwifiex_dbg(adapter, INFO, "info: rx_len = %d\n",
  1387. rx_len);
  1388. if (mwifiex_sdio_card_to_host_mp_aggr(adapter, rx_len,
  1389. port)) {
  1390. mwifiex_dbg(adapter, ERROR,
  1391. "card_to_host_mpa failed: int status=%#x\n",
  1392. sdio_ireg);
  1393. goto term_cmd;
  1394. }
  1395. }
  1396. }
  1397. return 0;
  1398. term_cmd:
  1399. /* terminate cmd */
  1400. if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
  1401. mwifiex_dbg(adapter, ERROR, "read CFG reg failed\n");
  1402. else
  1403. mwifiex_dbg(adapter, INFO,
  1404. "info: CFG reg val = %d\n", cr);
  1405. if (mwifiex_write_reg(adapter, CONFIGURATION_REG, (cr | 0x04)))
  1406. mwifiex_dbg(adapter, ERROR,
  1407. "write CFG reg failed\n");
  1408. else
  1409. mwifiex_dbg(adapter, INFO, "info: write success\n");
  1410. if (mwifiex_read_reg(adapter, CONFIGURATION_REG, &cr))
  1411. mwifiex_dbg(adapter, ERROR,
  1412. "read CFG reg failed\n");
  1413. else
  1414. mwifiex_dbg(adapter, INFO,
  1415. "info: CFG reg val =%x\n", cr);
  1416. return -1;
  1417. }
  1418. /*
  1419. * This function aggregates transmission buffers in driver and downloads
  1420. * the aggregated packet to card.
  1421. *
  1422. * The individual packets are aggregated by copying into an aggregation
  1423. * buffer and then downloaded to the card. Previous unsent packets in the
  1424. * aggregation buffer are pre-copied first before new packets are added.
  1425. * Aggregation is done till there is space left in the aggregation buffer,
  1426. * or till new packets are available.
  1427. *
  1428. * The function will only download the packet to the card when aggregation
  1429. * stops, otherwise it will just aggregate the packet in aggregation buffer
  1430. * and return.
  1431. */
  1432. static int mwifiex_host_to_card_mp_aggr(struct mwifiex_adapter *adapter,
  1433. u8 *payload, u32 pkt_len, u32 port,
  1434. u32 next_pkt_len)
  1435. {
  1436. struct sdio_mmc_card *card = adapter->card;
  1437. int ret = 0;
  1438. s32 f_send_aggr_buf = 0;
  1439. s32 f_send_cur_buf = 0;
  1440. s32 f_precopy_cur_buf = 0;
  1441. s32 f_postcopy_cur_buf = 0;
  1442. u32 mport;
  1443. if (!card->mpa_tx.enabled ||
  1444. (card->has_control_mask && (port == CTRL_PORT)) ||
  1445. (card->supports_sdio_new_mode && (port == CMD_PORT_SLCT))) {
  1446. mwifiex_dbg(adapter, WARN,
  1447. "info: %s: tx aggregation disabled\n",
  1448. __func__);
  1449. f_send_cur_buf = 1;
  1450. goto tx_curr_single;
  1451. }
  1452. if (next_pkt_len) {
  1453. /* More pkt in TX queue */
  1454. mwifiex_dbg(adapter, INFO,
  1455. "info: %s: more packets in queue.\n",
  1456. __func__);
  1457. if (MP_TX_AGGR_IN_PROGRESS(card)) {
  1458. if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len)) {
  1459. f_precopy_cur_buf = 1;
  1460. if (!(card->mp_wr_bitmap &
  1461. (1 << card->curr_wr_port)) ||
  1462. !MP_TX_AGGR_BUF_HAS_ROOM(
  1463. card, pkt_len + next_pkt_len))
  1464. f_send_aggr_buf = 1;
  1465. } else {
  1466. /* No room in Aggr buf, send it */
  1467. f_send_aggr_buf = 1;
  1468. if (!(card->mp_wr_bitmap &
  1469. (1 << card->curr_wr_port)))
  1470. f_send_cur_buf = 1;
  1471. else
  1472. f_postcopy_cur_buf = 1;
  1473. }
  1474. } else {
  1475. if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len) &&
  1476. (card->mp_wr_bitmap & (1 << card->curr_wr_port)))
  1477. f_precopy_cur_buf = 1;
  1478. else
  1479. f_send_cur_buf = 1;
  1480. }
  1481. } else {
  1482. /* Last pkt in TX queue */
  1483. mwifiex_dbg(adapter, INFO,
  1484. "info: %s: Last packet in Tx Queue.\n",
  1485. __func__);
  1486. if (MP_TX_AGGR_IN_PROGRESS(card)) {
  1487. /* some packs in Aggr buf already */
  1488. f_send_aggr_buf = 1;
  1489. if (MP_TX_AGGR_BUF_HAS_ROOM(card, pkt_len))
  1490. f_precopy_cur_buf = 1;
  1491. else
  1492. /* No room in Aggr buf, send it */
  1493. f_send_cur_buf = 1;
  1494. } else {
  1495. f_send_cur_buf = 1;
  1496. }
  1497. }
  1498. if (f_precopy_cur_buf) {
  1499. mwifiex_dbg(adapter, DATA,
  1500. "data: %s: precopy current buffer\n",
  1501. __func__);
  1502. MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
  1503. if (MP_TX_AGGR_PKT_LIMIT_REACHED(card) ||
  1504. mp_tx_aggr_port_limit_reached(card))
  1505. /* No more pkts allowed in Aggr buf, send it */
  1506. f_send_aggr_buf = 1;
  1507. }
  1508. if (f_send_aggr_buf) {
  1509. mwifiex_dbg(adapter, DATA,
  1510. "data: %s: send aggr buffer: %d %d\n",
  1511. __func__, card->mpa_tx.start_port,
  1512. card->mpa_tx.ports);
  1513. if (card->supports_sdio_new_mode) {
  1514. u32 port_count;
  1515. int i;
  1516. for (i = 0, port_count = 0; i < card->max_ports; i++)
  1517. if (card->mpa_tx.ports & BIT(i))
  1518. port_count++;
  1519. /* Writing data from "start_port + 0" to "start_port +
  1520. * port_count -1", so decrease the count by 1
  1521. */
  1522. port_count--;
  1523. mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
  1524. (port_count << 8)) + card->mpa_tx.start_port;
  1525. } else {
  1526. mport = (adapter->ioport | SDIO_MPA_ADDR_BASE |
  1527. (card->mpa_tx.ports << 4)) +
  1528. card->mpa_tx.start_port;
  1529. }
  1530. ret = mwifiex_write_data_to_card(adapter, card->mpa_tx.buf,
  1531. card->mpa_tx.buf_len, mport);
  1532. MP_TX_AGGR_BUF_RESET(card);
  1533. }
  1534. tx_curr_single:
  1535. if (f_send_cur_buf) {
  1536. mwifiex_dbg(adapter, DATA,
  1537. "data: %s: send current buffer %d\n",
  1538. __func__, port);
  1539. ret = mwifiex_write_data_to_card(adapter, payload, pkt_len,
  1540. adapter->ioport + port);
  1541. }
  1542. if (f_postcopy_cur_buf) {
  1543. mwifiex_dbg(adapter, DATA,
  1544. "data: %s: postcopy current buffer\n",
  1545. __func__);
  1546. MP_TX_AGGR_BUF_PUT(card, payload, pkt_len, port);
  1547. }
  1548. return ret;
  1549. }
  1550. /*
  1551. * This function downloads data from driver to card.
  1552. *
  1553. * Both commands and data packets are transferred to the card by this
  1554. * function.
  1555. *
  1556. * This function adds the SDIO specific header to the front of the buffer
  1557. * before transferring. The header contains the length of the packet and
  1558. * the type. The firmware handles the packets based upon this set type.
  1559. */
  1560. static int mwifiex_sdio_host_to_card(struct mwifiex_adapter *adapter,
  1561. u8 type, struct sk_buff *skb,
  1562. struct mwifiex_tx_param *tx_param)
  1563. {
  1564. struct sdio_mmc_card *card = adapter->card;
  1565. int ret;
  1566. u32 buf_block_len;
  1567. u32 blk_size;
  1568. u32 port = CTRL_PORT;
  1569. u8 *payload = (u8 *)skb->data;
  1570. u32 pkt_len = skb->len;
  1571. /* Allocate buffer and copy payload */
  1572. blk_size = MWIFIEX_SDIO_BLOCK_SIZE;
  1573. buf_block_len = (pkt_len + blk_size - 1) / blk_size;
  1574. *(__le16 *)&payload[0] = cpu_to_le16((u16)pkt_len);
  1575. *(__le16 *)&payload[2] = cpu_to_le16(type);
  1576. /*
  1577. * This is SDIO specific header
  1578. * u16 length,
  1579. * u16 type (MWIFIEX_TYPE_DATA = 0, MWIFIEX_TYPE_CMD = 1,
  1580. * MWIFIEX_TYPE_EVENT = 3)
  1581. */
  1582. if (type == MWIFIEX_TYPE_DATA) {
  1583. ret = mwifiex_get_wr_port_data(adapter, &port);
  1584. if (ret) {
  1585. mwifiex_dbg(adapter, ERROR,
  1586. "%s: no wr_port available\n",
  1587. __func__);
  1588. return ret;
  1589. }
  1590. } else {
  1591. adapter->cmd_sent = true;
  1592. /* Type must be MWIFIEX_TYPE_CMD */
  1593. if (pkt_len <= INTF_HEADER_LEN ||
  1594. pkt_len > MWIFIEX_UPLD_SIZE)
  1595. mwifiex_dbg(adapter, ERROR,
  1596. "%s: payload=%p, nb=%d\n",
  1597. __func__, payload, pkt_len);
  1598. if (card->supports_sdio_new_mode)
  1599. port = CMD_PORT_SLCT;
  1600. }
  1601. /* Transfer data to card */
  1602. pkt_len = buf_block_len * blk_size;
  1603. if (tx_param)
  1604. ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
  1605. port, tx_param->next_pkt_len
  1606. );
  1607. else
  1608. ret = mwifiex_host_to_card_mp_aggr(adapter, payload, pkt_len,
  1609. port, 0);
  1610. if (ret) {
  1611. if (type == MWIFIEX_TYPE_CMD)
  1612. adapter->cmd_sent = false;
  1613. if (type == MWIFIEX_TYPE_DATA) {
  1614. adapter->data_sent = false;
  1615. /* restore curr_wr_port in error cases */
  1616. card->curr_wr_port = port;
  1617. card->mp_wr_bitmap |= (u32)(1 << card->curr_wr_port);
  1618. }
  1619. } else {
  1620. if (type == MWIFIEX_TYPE_DATA) {
  1621. if (!(card->mp_wr_bitmap & (1 << card->curr_wr_port)))
  1622. adapter->data_sent = true;
  1623. else
  1624. adapter->data_sent = false;
  1625. }
  1626. }
  1627. return ret;
  1628. }
  1629. /*
  1630. * This function allocates the MPA Tx and Rx buffers.
  1631. */
  1632. static int mwifiex_alloc_sdio_mpa_buffers(struct mwifiex_adapter *adapter,
  1633. u32 mpa_tx_buf_size, u32 mpa_rx_buf_size)
  1634. {
  1635. struct sdio_mmc_card *card = adapter->card;
  1636. u32 rx_buf_size;
  1637. int ret = 0;
  1638. card->mpa_tx.buf = kzalloc(mpa_tx_buf_size, GFP_KERNEL);
  1639. if (!card->mpa_tx.buf) {
  1640. ret = -1;
  1641. goto error;
  1642. }
  1643. card->mpa_tx.buf_size = mpa_tx_buf_size;
  1644. rx_buf_size = max_t(u32, mpa_rx_buf_size,
  1645. (u32)SDIO_MAX_AGGR_BUF_SIZE);
  1646. card->mpa_rx.buf = kzalloc(rx_buf_size, GFP_KERNEL);
  1647. if (!card->mpa_rx.buf) {
  1648. ret = -1;
  1649. goto error;
  1650. }
  1651. card->mpa_rx.buf_size = rx_buf_size;
  1652. error:
  1653. if (ret) {
  1654. kfree(card->mpa_tx.buf);
  1655. kfree(card->mpa_rx.buf);
  1656. }
  1657. return ret;
  1658. }
  1659. /*
  1660. * This function unregisters the SDIO device.
  1661. *
  1662. * The SDIO IRQ is released, the function is disabled and driver
  1663. * data is set to null.
  1664. */
  1665. static void
  1666. mwifiex_unregister_dev(struct mwifiex_adapter *adapter)
  1667. {
  1668. struct sdio_mmc_card *card = adapter->card;
  1669. if (adapter->card) {
  1670. sdio_claim_host(card->func);
  1671. sdio_disable_func(card->func);
  1672. sdio_release_host(card->func);
  1673. }
  1674. }
  1675. /*
  1676. * This function registers the SDIO device.
  1677. *
  1678. * SDIO IRQ is claimed, block size is set and driver data is initialized.
  1679. */
  1680. static int mwifiex_register_dev(struct mwifiex_adapter *adapter)
  1681. {
  1682. int ret;
  1683. struct sdio_mmc_card *card = adapter->card;
  1684. struct sdio_func *func = card->func;
  1685. /* save adapter pointer in card */
  1686. card->adapter = adapter;
  1687. adapter->tx_buf_size = card->tx_buf_size;
  1688. sdio_claim_host(func);
  1689. /* Set block size */
  1690. ret = sdio_set_block_size(card->func, MWIFIEX_SDIO_BLOCK_SIZE);
  1691. sdio_release_host(func);
  1692. if (ret) {
  1693. mwifiex_dbg(adapter, ERROR,
  1694. "cannot set SDIO block size\n");
  1695. return ret;
  1696. }
  1697. adapter->dev = &func->dev;
  1698. strcpy(adapter->fw_name, card->firmware);
  1699. if (card->fw_dump_enh) {
  1700. adapter->mem_type_mapping_tbl = generic_mem_type_map;
  1701. adapter->num_mem_types = 1;
  1702. } else {
  1703. adapter->mem_type_mapping_tbl = mem_type_mapping_tbl;
  1704. adapter->num_mem_types = ARRAY_SIZE(mem_type_mapping_tbl);
  1705. }
  1706. return 0;
  1707. }
  1708. /*
  1709. * This function initializes the SDIO driver.
  1710. *
  1711. * The following initializations steps are followed -
  1712. * - Read the Host interrupt status register to acknowledge
  1713. * the first interrupt got from bootloader
  1714. * - Disable host interrupt mask register
  1715. * - Get SDIO port
  1716. * - Initialize SDIO variables in card
  1717. * - Allocate MP registers
  1718. * - Allocate MPA Tx and Rx buffers
  1719. */
  1720. static int mwifiex_init_sdio(struct mwifiex_adapter *adapter)
  1721. {
  1722. struct sdio_mmc_card *card = adapter->card;
  1723. const struct mwifiex_sdio_card_reg *reg = card->reg;
  1724. int ret;
  1725. u8 sdio_ireg;
  1726. sdio_set_drvdata(card->func, card);
  1727. /*
  1728. * Read the host_int_status_reg for ACK the first interrupt got
  1729. * from the bootloader. If we don't do this we get a interrupt
  1730. * as soon as we register the irq.
  1731. */
  1732. mwifiex_read_reg(adapter, card->reg->host_int_status_reg, &sdio_ireg);
  1733. /* Get SDIO ioport */
  1734. mwifiex_init_sdio_ioport(adapter);
  1735. /* Initialize SDIO variables in card */
  1736. card->mp_rd_bitmap = 0;
  1737. card->mp_wr_bitmap = 0;
  1738. card->curr_rd_port = reg->start_rd_port;
  1739. card->curr_wr_port = reg->start_wr_port;
  1740. card->mp_data_port_mask = reg->data_port_mask;
  1741. card->mpa_tx.buf_len = 0;
  1742. card->mpa_tx.pkt_cnt = 0;
  1743. card->mpa_tx.start_port = 0;
  1744. card->mpa_tx.enabled = 1;
  1745. card->mpa_tx.pkt_aggr_limit = card->mp_agg_pkt_limit;
  1746. card->mpa_rx.buf_len = 0;
  1747. card->mpa_rx.pkt_cnt = 0;
  1748. card->mpa_rx.start_port = 0;
  1749. card->mpa_rx.enabled = 1;
  1750. card->mpa_rx.pkt_aggr_limit = card->mp_agg_pkt_limit;
  1751. /* Allocate buffers for SDIO MP-A */
  1752. card->mp_regs = kzalloc(reg->max_mp_regs, GFP_KERNEL);
  1753. if (!card->mp_regs)
  1754. return -ENOMEM;
  1755. /* Allocate skb pointer buffers */
  1756. card->mpa_rx.skb_arr = kzalloc((sizeof(void *)) *
  1757. card->mp_agg_pkt_limit, GFP_KERNEL);
  1758. card->mpa_rx.len_arr = kzalloc(sizeof(*card->mpa_rx.len_arr) *
  1759. card->mp_agg_pkt_limit, GFP_KERNEL);
  1760. ret = mwifiex_alloc_sdio_mpa_buffers(adapter,
  1761. card->mp_tx_agg_buf_size,
  1762. card->mp_rx_agg_buf_size);
  1763. if (ret) {
  1764. mwifiex_dbg(adapter, ERROR,
  1765. "failed to alloc sdio mp-a buffers\n");
  1766. kfree(card->mp_regs);
  1767. return -1;
  1768. }
  1769. adapter->auto_tdls = card->can_auto_tdls;
  1770. adapter->ext_scan = card->can_ext_scan;
  1771. return ret;
  1772. }
  1773. /*
  1774. * This function resets the MPA Tx and Rx buffers.
  1775. */
  1776. static void mwifiex_cleanup_mpa_buf(struct mwifiex_adapter *adapter)
  1777. {
  1778. struct sdio_mmc_card *card = adapter->card;
  1779. MP_TX_AGGR_BUF_RESET(card);
  1780. MP_RX_AGGR_BUF_RESET(card);
  1781. }
  1782. /*
  1783. * This function cleans up the allocated card buffers.
  1784. *
  1785. * The following are freed by this function -
  1786. * - MP registers
  1787. * - MPA Tx buffer
  1788. * - MPA Rx buffer
  1789. */
  1790. static void mwifiex_cleanup_sdio(struct mwifiex_adapter *adapter)
  1791. {
  1792. struct sdio_mmc_card *card = adapter->card;
  1793. kfree(card->mp_regs);
  1794. kfree(card->mpa_rx.skb_arr);
  1795. kfree(card->mpa_rx.len_arr);
  1796. kfree(card->mpa_tx.buf);
  1797. kfree(card->mpa_rx.buf);
  1798. sdio_set_drvdata(card->func, NULL);
  1799. kfree(card);
  1800. }
  1801. /*
  1802. * This function updates the MP end port in card.
  1803. */
  1804. static void
  1805. mwifiex_update_mp_end_port(struct mwifiex_adapter *adapter, u16 port)
  1806. {
  1807. struct sdio_mmc_card *card = adapter->card;
  1808. const struct mwifiex_sdio_card_reg *reg = card->reg;
  1809. int i;
  1810. card->mp_end_port = port;
  1811. card->mp_data_port_mask = reg->data_port_mask;
  1812. if (reg->start_wr_port) {
  1813. for (i = 1; i <= card->max_ports - card->mp_end_port; i++)
  1814. card->mp_data_port_mask &=
  1815. ~(1 << (card->max_ports - i));
  1816. }
  1817. card->curr_wr_port = reg->start_wr_port;
  1818. mwifiex_dbg(adapter, CMD,
  1819. "cmd: mp_end_port %d, data port mask 0x%x\n",
  1820. port, card->mp_data_port_mask);
  1821. }
  1822. static void mwifiex_recreate_adapter(struct sdio_mmc_card *card)
  1823. {
  1824. struct sdio_func *func = card->func;
  1825. const struct sdio_device_id *device_id = card->device_id;
  1826. /* TODO mmc_hw_reset does not require destroying and re-probing the
  1827. * whole adapter. Hence there was no need to for this rube-goldberg
  1828. * design to reload the fw from an external workqueue. If we don't
  1829. * destroy the adapter we could reload the fw from
  1830. * mwifiex_main_work_queue directly.
  1831. * The real difficulty with fw reset is to restore all the user
  1832. * settings applied through ioctl. By destroying and recreating the
  1833. * adapter, we take the easy way out, since we rely on user space to
  1834. * restore them. We assume that user space will treat the new
  1835. * incarnation of the adapter(interfaces) as if they had been just
  1836. * discovered and initializes them from scratch.
  1837. */
  1838. mwifiex_sdio_remove(func);
  1839. /* power cycle the adapter */
  1840. sdio_claim_host(func);
  1841. mmc_hw_reset(func->card->host);
  1842. sdio_release_host(func);
  1843. mwifiex_sdio_probe(func, device_id);
  1844. }
  1845. static struct mwifiex_adapter *save_adapter;
  1846. static void mwifiex_sdio_card_reset_work(struct mwifiex_adapter *adapter)
  1847. {
  1848. struct sdio_mmc_card *card = adapter->card;
  1849. /* TODO card pointer is unprotected. If the adapter is removed
  1850. * physically, sdio core might trigger mwifiex_sdio_remove, before this
  1851. * workqueue is run, which will destroy the adapter struct. When this
  1852. * workqueue eventually exceutes it will dereference an invalid adapter
  1853. * pointer
  1854. */
  1855. mwifiex_recreate_adapter(card);
  1856. }
  1857. /* This function read/write firmware */
  1858. static enum
  1859. rdwr_status mwifiex_sdio_rdwr_firmware(struct mwifiex_adapter *adapter,
  1860. u8 doneflag)
  1861. {
  1862. struct sdio_mmc_card *card = adapter->card;
  1863. int ret, tries;
  1864. u8 ctrl_data = 0;
  1865. sdio_writeb(card->func, card->reg->fw_dump_host_ready,
  1866. card->reg->fw_dump_ctrl, &ret);
  1867. if (ret) {
  1868. mwifiex_dbg(adapter, ERROR, "SDIO Write ERR\n");
  1869. return RDWR_STATUS_FAILURE;
  1870. }
  1871. for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
  1872. ctrl_data = sdio_readb(card->func, card->reg->fw_dump_ctrl,
  1873. &ret);
  1874. if (ret) {
  1875. mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
  1876. return RDWR_STATUS_FAILURE;
  1877. }
  1878. if (ctrl_data == FW_DUMP_DONE)
  1879. break;
  1880. if (doneflag && ctrl_data == doneflag)
  1881. return RDWR_STATUS_DONE;
  1882. if (ctrl_data != card->reg->fw_dump_host_ready) {
  1883. mwifiex_dbg(adapter, WARN,
  1884. "The ctrl reg was changed, re-try again\n");
  1885. sdio_writeb(card->func, card->reg->fw_dump_host_ready,
  1886. card->reg->fw_dump_ctrl, &ret);
  1887. if (ret) {
  1888. mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
  1889. return RDWR_STATUS_FAILURE;
  1890. }
  1891. }
  1892. usleep_range(100, 200);
  1893. }
  1894. if (ctrl_data == card->reg->fw_dump_host_ready) {
  1895. mwifiex_dbg(adapter, ERROR,
  1896. "Fail to pull ctrl_data\n");
  1897. return RDWR_STATUS_FAILURE;
  1898. }
  1899. return RDWR_STATUS_SUCCESS;
  1900. }
  1901. /* This function dump firmware memory to file */
  1902. static void mwifiex_sdio_fw_dump(struct mwifiex_adapter *adapter)
  1903. {
  1904. struct sdio_mmc_card *card = adapter->card;
  1905. int ret = 0;
  1906. unsigned int reg, reg_start, reg_end;
  1907. u8 *dbg_ptr, *end_ptr, dump_num, idx, i, read_reg, doneflag = 0;
  1908. enum rdwr_status stat;
  1909. u32 memory_size;
  1910. if (!card->can_dump_fw)
  1911. return;
  1912. for (idx = 0; idx < ARRAY_SIZE(mem_type_mapping_tbl); idx++) {
  1913. struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
  1914. if (entry->mem_ptr) {
  1915. vfree(entry->mem_ptr);
  1916. entry->mem_ptr = NULL;
  1917. }
  1918. entry->mem_size = 0;
  1919. }
  1920. mwifiex_pm_wakeup_card(adapter);
  1921. sdio_claim_host(card->func);
  1922. mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
  1923. stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
  1924. if (stat == RDWR_STATUS_FAILURE)
  1925. goto done;
  1926. reg = card->reg->fw_dump_start;
  1927. /* Read the number of the memories which will dump */
  1928. dump_num = sdio_readb(card->func, reg, &ret);
  1929. if (ret) {
  1930. mwifiex_dbg(adapter, ERROR, "SDIO read memory length err\n");
  1931. goto done;
  1932. }
  1933. /* Read the length of every memory which will dump */
  1934. for (idx = 0; idx < dump_num; idx++) {
  1935. struct memory_type_mapping *entry = &mem_type_mapping_tbl[idx];
  1936. stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
  1937. if (stat == RDWR_STATUS_FAILURE)
  1938. goto done;
  1939. memory_size = 0;
  1940. reg = card->reg->fw_dump_start;
  1941. for (i = 0; i < 4; i++) {
  1942. read_reg = sdio_readb(card->func, reg, &ret);
  1943. if (ret) {
  1944. mwifiex_dbg(adapter, ERROR, "SDIO read err\n");
  1945. goto done;
  1946. }
  1947. memory_size |= (read_reg << i*8);
  1948. reg++;
  1949. }
  1950. if (memory_size == 0) {
  1951. mwifiex_dbg(adapter, DUMP, "Firmware dump Finished!\n");
  1952. ret = mwifiex_write_reg(adapter,
  1953. card->reg->fw_dump_ctrl,
  1954. FW_DUMP_READ_DONE);
  1955. if (ret) {
  1956. mwifiex_dbg(adapter, ERROR, "SDIO write err\n");
  1957. return;
  1958. }
  1959. break;
  1960. }
  1961. mwifiex_dbg(adapter, DUMP,
  1962. "%s_SIZE=0x%x\n", entry->mem_name, memory_size);
  1963. entry->mem_ptr = vmalloc(memory_size + 1);
  1964. entry->mem_size = memory_size;
  1965. if (!entry->mem_ptr) {
  1966. mwifiex_dbg(adapter, ERROR, "Vmalloc %s failed\n",
  1967. entry->mem_name);
  1968. goto done;
  1969. }
  1970. dbg_ptr = entry->mem_ptr;
  1971. end_ptr = dbg_ptr + memory_size;
  1972. doneflag = entry->done_flag;
  1973. mwifiex_dbg(adapter, DUMP,
  1974. "Start %s output, please wait...\n",
  1975. entry->mem_name);
  1976. do {
  1977. stat = mwifiex_sdio_rdwr_firmware(adapter, doneflag);
  1978. if (stat == RDWR_STATUS_FAILURE)
  1979. goto done;
  1980. reg_start = card->reg->fw_dump_start;
  1981. reg_end = card->reg->fw_dump_end;
  1982. for (reg = reg_start; reg <= reg_end; reg++) {
  1983. *dbg_ptr = sdio_readb(card->func, reg, &ret);
  1984. if (ret) {
  1985. mwifiex_dbg(adapter, ERROR,
  1986. "SDIO read err\n");
  1987. goto done;
  1988. }
  1989. if (dbg_ptr < end_ptr)
  1990. dbg_ptr++;
  1991. else
  1992. mwifiex_dbg(adapter, ERROR,
  1993. "Allocated buf not enough\n");
  1994. }
  1995. if (stat != RDWR_STATUS_DONE)
  1996. continue;
  1997. mwifiex_dbg(adapter, DUMP, "%s done: size=0x%tx\n",
  1998. entry->mem_name, dbg_ptr - entry->mem_ptr);
  1999. break;
  2000. } while (1);
  2001. }
  2002. mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
  2003. done:
  2004. sdio_release_host(card->func);
  2005. }
  2006. static void mwifiex_sdio_generic_fw_dump(struct mwifiex_adapter *adapter)
  2007. {
  2008. struct sdio_mmc_card *card = adapter->card;
  2009. struct memory_type_mapping *entry = &generic_mem_type_map[0];
  2010. unsigned int reg, reg_start, reg_end;
  2011. u8 start_flag = 0, done_flag = 0;
  2012. u8 *dbg_ptr, *end_ptr;
  2013. enum rdwr_status stat;
  2014. int ret = -1, tries;
  2015. if (!card->fw_dump_enh)
  2016. return;
  2017. if (entry->mem_ptr) {
  2018. vfree(entry->mem_ptr);
  2019. entry->mem_ptr = NULL;
  2020. }
  2021. entry->mem_size = 0;
  2022. mwifiex_pm_wakeup_card(adapter);
  2023. sdio_claim_host(card->func);
  2024. mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump start ==\n");
  2025. stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
  2026. if (stat == RDWR_STATUS_FAILURE)
  2027. goto done;
  2028. reg_start = card->reg->fw_dump_start;
  2029. reg_end = card->reg->fw_dump_end;
  2030. for (reg = reg_start; reg <= reg_end; reg++) {
  2031. for (tries = 0; tries < MAX_POLL_TRIES; tries++) {
  2032. start_flag = sdio_readb(card->func, reg, &ret);
  2033. if (ret) {
  2034. mwifiex_dbg(adapter, ERROR,
  2035. "SDIO read err\n");
  2036. goto done;
  2037. }
  2038. if (start_flag == 0)
  2039. break;
  2040. if (tries == MAX_POLL_TRIES) {
  2041. mwifiex_dbg(adapter, ERROR,
  2042. "FW not ready to dump\n");
  2043. ret = -1;
  2044. goto done;
  2045. }
  2046. }
  2047. usleep_range(100, 200);
  2048. }
  2049. entry->mem_ptr = vmalloc(0xf0000 + 1);
  2050. if (!entry->mem_ptr) {
  2051. ret = -1;
  2052. goto done;
  2053. }
  2054. dbg_ptr = entry->mem_ptr;
  2055. entry->mem_size = 0xf0000;
  2056. end_ptr = dbg_ptr + entry->mem_size;
  2057. done_flag = entry->done_flag;
  2058. mwifiex_dbg(adapter, DUMP,
  2059. "Start %s output, please wait...\n", entry->mem_name);
  2060. while (true) {
  2061. stat = mwifiex_sdio_rdwr_firmware(adapter, done_flag);
  2062. if (stat == RDWR_STATUS_FAILURE)
  2063. goto done;
  2064. for (reg = reg_start; reg <= reg_end; reg++) {
  2065. *dbg_ptr = sdio_readb(card->func, reg, &ret);
  2066. if (ret) {
  2067. mwifiex_dbg(adapter, ERROR,
  2068. "SDIO read err\n");
  2069. goto done;
  2070. }
  2071. dbg_ptr++;
  2072. if (dbg_ptr >= end_ptr) {
  2073. u8 *tmp_ptr;
  2074. tmp_ptr = vmalloc(entry->mem_size + 0x4000 + 1);
  2075. if (!tmp_ptr)
  2076. goto done;
  2077. memcpy(tmp_ptr, entry->mem_ptr,
  2078. entry->mem_size);
  2079. vfree(entry->mem_ptr);
  2080. entry->mem_ptr = tmp_ptr;
  2081. tmp_ptr = NULL;
  2082. dbg_ptr = entry->mem_ptr + entry->mem_size;
  2083. entry->mem_size += 0x4000;
  2084. end_ptr = entry->mem_ptr + entry->mem_size;
  2085. }
  2086. }
  2087. if (stat == RDWR_STATUS_DONE) {
  2088. entry->mem_size = dbg_ptr - entry->mem_ptr;
  2089. mwifiex_dbg(adapter, DUMP, "dump %s done size=0x%x\n",
  2090. entry->mem_name, entry->mem_size);
  2091. ret = 0;
  2092. break;
  2093. }
  2094. }
  2095. mwifiex_dbg(adapter, MSG, "== mwifiex firmware dump end ==\n");
  2096. done:
  2097. if (ret) {
  2098. mwifiex_dbg(adapter, ERROR, "firmware dump failed\n");
  2099. if (entry->mem_ptr) {
  2100. vfree(entry->mem_ptr);
  2101. entry->mem_ptr = NULL;
  2102. }
  2103. entry->mem_size = 0;
  2104. }
  2105. sdio_release_host(card->func);
  2106. }
  2107. static void mwifiex_sdio_device_dump_work(struct mwifiex_adapter *adapter)
  2108. {
  2109. struct sdio_mmc_card *card = adapter->card;
  2110. mwifiex_drv_info_dump(adapter);
  2111. if (card->fw_dump_enh)
  2112. mwifiex_sdio_generic_fw_dump(adapter);
  2113. else
  2114. mwifiex_sdio_fw_dump(adapter);
  2115. mwifiex_upload_device_dump(adapter);
  2116. }
  2117. static void mwifiex_sdio_work(struct work_struct *work)
  2118. {
  2119. if (test_and_clear_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP,
  2120. &iface_work_flags))
  2121. mwifiex_sdio_device_dump_work(save_adapter);
  2122. if (test_and_clear_bit(MWIFIEX_IFACE_WORK_CARD_RESET,
  2123. &iface_work_flags))
  2124. mwifiex_sdio_card_reset_work(save_adapter);
  2125. }
  2126. static DECLARE_WORK(sdio_work, mwifiex_sdio_work);
  2127. /* This function resets the card */
  2128. static void mwifiex_sdio_card_reset(struct mwifiex_adapter *adapter)
  2129. {
  2130. save_adapter = adapter;
  2131. if (test_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags))
  2132. return;
  2133. set_bit(MWIFIEX_IFACE_WORK_CARD_RESET, &iface_work_flags);
  2134. schedule_work(&sdio_work);
  2135. }
  2136. /* This function dumps FW information */
  2137. static void mwifiex_sdio_device_dump(struct mwifiex_adapter *adapter)
  2138. {
  2139. save_adapter = adapter;
  2140. if (test_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags))
  2141. return;
  2142. set_bit(MWIFIEX_IFACE_WORK_DEVICE_DUMP, &iface_work_flags);
  2143. schedule_work(&sdio_work);
  2144. }
  2145. /* Function to dump SDIO function registers and SDIO scratch registers in case
  2146. * of FW crash
  2147. */
  2148. static int
  2149. mwifiex_sdio_reg_dump(struct mwifiex_adapter *adapter, char *drv_buf)
  2150. {
  2151. char *p = drv_buf;
  2152. struct sdio_mmc_card *cardp = adapter->card;
  2153. int ret = 0;
  2154. u8 count, func, data, index = 0, size = 0;
  2155. u8 reg, reg_start, reg_end;
  2156. char buf[256], *ptr;
  2157. if (!p)
  2158. return 0;
  2159. mwifiex_dbg(adapter, MSG, "SDIO register dump start\n");
  2160. mwifiex_pm_wakeup_card(adapter);
  2161. sdio_claim_host(cardp->func);
  2162. for (count = 0; count < 5; count++) {
  2163. memset(buf, 0, sizeof(buf));
  2164. ptr = buf;
  2165. switch (count) {
  2166. case 0:
  2167. /* Read the registers of SDIO function0 */
  2168. func = count;
  2169. reg_start = 0;
  2170. reg_end = 9;
  2171. break;
  2172. case 1:
  2173. /* Read the registers of SDIO function1 */
  2174. func = count;
  2175. reg_start = cardp->reg->func1_dump_reg_start;
  2176. reg_end = cardp->reg->func1_dump_reg_end;
  2177. break;
  2178. case 2:
  2179. index = 0;
  2180. func = 1;
  2181. reg_start = cardp->reg->func1_spec_reg_table[index++];
  2182. size = cardp->reg->func1_spec_reg_num;
  2183. reg_end = cardp->reg->func1_spec_reg_table[size-1];
  2184. break;
  2185. default:
  2186. /* Read the scratch registers of SDIO function1 */
  2187. if (count == 4)
  2188. mdelay(100);
  2189. func = 1;
  2190. reg_start = cardp->reg->func1_scratch_reg;
  2191. reg_end = reg_start + MWIFIEX_SDIO_SCRATCH_SIZE;
  2192. }
  2193. if (count != 2)
  2194. ptr += sprintf(ptr, "SDIO Func%d (%#x-%#x): ",
  2195. func, reg_start, reg_end);
  2196. else
  2197. ptr += sprintf(ptr, "SDIO Func%d: ", func);
  2198. for (reg = reg_start; reg <= reg_end;) {
  2199. if (func == 0)
  2200. data = sdio_f0_readb(cardp->func, reg, &ret);
  2201. else
  2202. data = sdio_readb(cardp->func, reg, &ret);
  2203. if (count == 2)
  2204. ptr += sprintf(ptr, "(%#x) ", reg);
  2205. if (!ret) {
  2206. ptr += sprintf(ptr, "%02x ", data);
  2207. } else {
  2208. ptr += sprintf(ptr, "ERR");
  2209. break;
  2210. }
  2211. if (count == 2 && reg < reg_end)
  2212. reg = cardp->reg->func1_spec_reg_table[index++];
  2213. else
  2214. reg++;
  2215. }
  2216. mwifiex_dbg(adapter, MSG, "%s\n", buf);
  2217. p += sprintf(p, "%s\n", buf);
  2218. }
  2219. sdio_release_host(cardp->func);
  2220. mwifiex_dbg(adapter, MSG, "SDIO register dump end\n");
  2221. return p - drv_buf;
  2222. }
  2223. static struct mwifiex_if_ops sdio_ops = {
  2224. .init_if = mwifiex_init_sdio,
  2225. .cleanup_if = mwifiex_cleanup_sdio,
  2226. .check_fw_status = mwifiex_check_fw_status,
  2227. .prog_fw = mwifiex_prog_fw_w_helper,
  2228. .register_dev = mwifiex_register_dev,
  2229. .unregister_dev = mwifiex_unregister_dev,
  2230. .enable_int = mwifiex_sdio_enable_host_int,
  2231. .disable_int = mwifiex_sdio_disable_host_int,
  2232. .process_int_status = mwifiex_process_int_status,
  2233. .host_to_card = mwifiex_sdio_host_to_card,
  2234. .wakeup = mwifiex_pm_wakeup_card,
  2235. .wakeup_complete = mwifiex_pm_wakeup_card_complete,
  2236. /* SDIO specific */
  2237. .update_mp_end_port = mwifiex_update_mp_end_port,
  2238. .cleanup_mpa_buf = mwifiex_cleanup_mpa_buf,
  2239. .cmdrsp_complete = mwifiex_sdio_cmdrsp_complete,
  2240. .event_complete = mwifiex_sdio_event_complete,
  2241. .card_reset = mwifiex_sdio_card_reset,
  2242. .reg_dump = mwifiex_sdio_reg_dump,
  2243. .device_dump = mwifiex_sdio_device_dump,
  2244. .deaggr_pkt = mwifiex_deaggr_sdio_pkt,
  2245. };
  2246. /*
  2247. * This function initializes the SDIO driver.
  2248. *
  2249. * This initiates the semaphore and registers the device with
  2250. * SDIO bus.
  2251. */
  2252. static int
  2253. mwifiex_sdio_init_module(void)
  2254. {
  2255. sema_init(&add_remove_card_sem, 1);
  2256. /* Clear the flag in case user removes the card. */
  2257. user_rmmod = 0;
  2258. return sdio_register_driver(&mwifiex_sdio);
  2259. }
  2260. /*
  2261. * This function cleans up the SDIO driver.
  2262. *
  2263. * The following major steps are followed for cleanup -
  2264. * - Resume the device if its suspended
  2265. * - Disconnect the device if connected
  2266. * - Shutdown the firmware
  2267. * - Unregister the device from SDIO bus.
  2268. */
  2269. static void
  2270. mwifiex_sdio_cleanup_module(void)
  2271. {
  2272. if (!down_interruptible(&add_remove_card_sem))
  2273. up(&add_remove_card_sem);
  2274. /* Set the flag as user is removing this module. */
  2275. user_rmmod = 1;
  2276. cancel_work_sync(&sdio_work);
  2277. sdio_unregister_driver(&mwifiex_sdio);
  2278. }
  2279. module_init(mwifiex_sdio_init_module);
  2280. module_exit(mwifiex_sdio_cleanup_module);
  2281. MODULE_AUTHOR("Marvell International Ltd.");
  2282. MODULE_DESCRIPTION("Marvell WiFi-Ex SDIO Driver version " SDIO_VERSION);
  2283. MODULE_VERSION(SDIO_VERSION);
  2284. MODULE_LICENSE("GPL v2");
  2285. MODULE_FIRMWARE(SD8786_DEFAULT_FW_NAME);
  2286. MODULE_FIRMWARE(SD8787_DEFAULT_FW_NAME);
  2287. MODULE_FIRMWARE(SD8797_DEFAULT_FW_NAME);
  2288. MODULE_FIRMWARE(SD8897_DEFAULT_FW_NAME);
  2289. MODULE_FIRMWARE(SD8887_DEFAULT_FW_NAME);
  2290. MODULE_FIRMWARE(SD8997_DEFAULT_FW_NAME);