rtw_io.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /******************************************************************************
  2. *
  3. * Copyright(c) 2007 - 2017 Realtek Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of version 2 of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. *****************************************************************************/
  15. /*
  16. The purpose of rtw_io.c
  17. a. provides the API
  18. b. provides the protocol engine
  19. c. provides the software interface between caller and the hardware interface
  20. Compiler Flag Option:
  21. 1. CONFIG_SDIO_HCI:
  22. a. USE_SYNC_IRP: Only sync operations are provided.
  23. b. USE_ASYNC_IRP:Both sync/async operations are provided.
  24. 2. CONFIG_USB_HCI:
  25. a. USE_ASYNC_IRP: Both sync/async operations are provided.
  26. 3. CONFIG_CFIO_HCI:
  27. b. USE_SYNC_IRP: Only sync operations are provided.
  28. Only sync read/rtw_write_mem operations are provided.
  29. jackson@realtek.com.tw
  30. */
  31. #define _RTW_IO_C_
  32. #include <drv_types.h>
  33. #include <hal_data.h>
  34. #if defined(PLATFORM_LINUX) && defined (PLATFORM_WINDOWS)
  35. #error "Shall be Linux or Windows, but not both!\n"
  36. #endif
  37. #if defined(CONFIG_SDIO_HCI) || defined(CONFIG_PLATFORM_RTL8197D)
  38. #define rtw_le16_to_cpu(val) val
  39. #define rtw_le32_to_cpu(val) val
  40. #define rtw_cpu_to_le16(val) val
  41. #define rtw_cpu_to_le32(val) val
  42. #else
  43. #define rtw_le16_to_cpu(val) le16_to_cpu(val)
  44. #define rtw_le32_to_cpu(val) le32_to_cpu(val)
  45. #define rtw_cpu_to_le16(val) cpu_to_le16(val)
  46. #define rtw_cpu_to_le32(val) cpu_to_le32(val)
  47. #endif
  48. u8 _rtw_read8(_adapter *adapter, u32 addr)
  49. {
  50. u8 r_val;
  51. /* struct io_queue *pio_queue = (struct io_queue *)adapter->pio_queue; */
  52. struct io_priv *pio_priv = &adapter->iopriv;
  53. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  54. u8(*_read8)(struct intf_hdl *pintfhdl, u32 addr);
  55. _read8 = pintfhdl->io_ops._read8;
  56. r_val = _read8(pintfhdl, addr);
  57. return r_val;
  58. }
  59. u16 _rtw_read16(_adapter *adapter, u32 addr)
  60. {
  61. u16 r_val;
  62. /* struct io_queue *pio_queue = (struct io_queue *)adapter->pio_queue; */
  63. struct io_priv *pio_priv = &adapter->iopriv;
  64. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  65. u16(*_read16)(struct intf_hdl *pintfhdl, u32 addr);
  66. _read16 = pintfhdl->io_ops._read16;
  67. r_val = _read16(pintfhdl, addr);
  68. return rtw_le16_to_cpu(r_val);
  69. }
  70. u32 _rtw_read32(_adapter *adapter, u32 addr)
  71. {
  72. u32 r_val;
  73. /* struct io_queue *pio_queue = (struct io_queue *)adapter->pio_queue; */
  74. struct io_priv *pio_priv = &adapter->iopriv;
  75. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  76. u32(*_read32)(struct intf_hdl *pintfhdl, u32 addr);
  77. _read32 = pintfhdl->io_ops._read32;
  78. r_val = _read32(pintfhdl, addr);
  79. return rtw_le32_to_cpu(r_val);
  80. }
  81. int _rtw_write8(_adapter *adapter, u32 addr, u8 val)
  82. {
  83. /* struct io_queue *pio_queue = (struct io_queue *)adapter->pio_queue; */
  84. struct io_priv *pio_priv = &adapter->iopriv;
  85. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  86. int (*_write8)(struct intf_hdl *pintfhdl, u32 addr, u8 val);
  87. int ret;
  88. _write8 = pintfhdl->io_ops._write8;
  89. ret = _write8(pintfhdl, addr, val);
  90. return RTW_STATUS_CODE(ret);
  91. }
  92. int _rtw_write16(_adapter *adapter, u32 addr, u16 val)
  93. {
  94. /* struct io_queue *pio_queue = (struct io_queue *)adapter->pio_queue; */
  95. struct io_priv *pio_priv = &adapter->iopriv;
  96. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  97. int (*_write16)(struct intf_hdl *pintfhdl, u32 addr, u16 val);
  98. int ret;
  99. _write16 = pintfhdl->io_ops._write16;
  100. val = rtw_cpu_to_le16(val);
  101. ret = _write16(pintfhdl, addr, val);
  102. return RTW_STATUS_CODE(ret);
  103. }
  104. int _rtw_write32(_adapter *adapter, u32 addr, u32 val)
  105. {
  106. /* struct io_queue *pio_queue = (struct io_queue *)adapter->pio_queue; */
  107. struct io_priv *pio_priv = &adapter->iopriv;
  108. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  109. int (*_write32)(struct intf_hdl *pintfhdl, u32 addr, u32 val);
  110. int ret;
  111. _write32 = pintfhdl->io_ops._write32;
  112. val = rtw_cpu_to_le32(val);
  113. ret = _write32(pintfhdl, addr, val);
  114. return RTW_STATUS_CODE(ret);
  115. }
  116. int _rtw_writeN(_adapter *adapter, u32 addr , u32 length , u8 *pdata)
  117. {
  118. /* struct io_queue *pio_queue = (struct io_queue *)adapter->pio_queue; */
  119. struct io_priv *pio_priv = &adapter->iopriv;
  120. struct intf_hdl *pintfhdl = (struct intf_hdl *)(&(pio_priv->intf));
  121. int (*_writeN)(struct intf_hdl *pintfhdl, u32 addr, u32 length, u8 *pdata);
  122. int ret;
  123. _writeN = pintfhdl->io_ops._writeN;
  124. ret = _writeN(pintfhdl, addr, length, pdata);
  125. return RTW_STATUS_CODE(ret);
  126. }
  127. #ifdef CONFIG_SDIO_HCI
  128. u8 _rtw_sd_f0_read8(_adapter *adapter, u32 addr)
  129. {
  130. u8 r_val = 0x00;
  131. struct io_priv *pio_priv = &adapter->iopriv;
  132. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  133. u8(*_sd_f0_read8)(struct intf_hdl *pintfhdl, u32 addr);
  134. _sd_f0_read8 = pintfhdl->io_ops._sd_f0_read8;
  135. if (_sd_f0_read8)
  136. r_val = _sd_f0_read8(pintfhdl, addr);
  137. else
  138. RTW_WARN(FUNC_ADPT_FMT" _sd_f0_read8 callback is NULL\n", FUNC_ADPT_ARG(adapter));
  139. return r_val;
  140. }
  141. #ifdef CONFIG_SDIO_INDIRECT_ACCESS
  142. u8 _rtw_sd_iread8(_adapter *adapter, u32 addr)
  143. {
  144. u8 r_val = 0x00;
  145. struct io_priv *pio_priv = &adapter->iopriv;
  146. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  147. u8(*_sd_iread8)(struct intf_hdl *pintfhdl, u32 addr);
  148. _sd_iread8 = pintfhdl->io_ops._sd_iread8;
  149. if (_sd_iread8)
  150. r_val = _sd_iread8(pintfhdl, addr);
  151. else
  152. RTW_ERR(FUNC_ADPT_FMT" _sd_iread8 callback is NULL\n", FUNC_ADPT_ARG(adapter));
  153. return r_val;
  154. }
  155. u16 _rtw_sd_iread16(_adapter *adapter, u32 addr)
  156. {
  157. u16 r_val = 0x00;
  158. struct io_priv *pio_priv = &adapter->iopriv;
  159. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  160. u16(*_sd_iread16)(struct intf_hdl *pintfhdl, u32 addr);
  161. _sd_iread16 = pintfhdl->io_ops._sd_iread16;
  162. if (_sd_iread16)
  163. r_val = _sd_iread16(pintfhdl, addr);
  164. else
  165. RTW_ERR(FUNC_ADPT_FMT" _sd_iread16 callback is NULL\n", FUNC_ADPT_ARG(adapter));
  166. return r_val;
  167. }
  168. u32 _rtw_sd_iread32(_adapter *adapter, u32 addr)
  169. {
  170. u32 r_val = 0x00;
  171. struct io_priv *pio_priv = &adapter->iopriv;
  172. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  173. u32(*_sd_iread32)(struct intf_hdl *pintfhdl, u32 addr);
  174. _sd_iread32 = pintfhdl->io_ops._sd_iread32;
  175. if (_sd_iread32)
  176. r_val = _sd_iread32(pintfhdl, addr);
  177. else
  178. RTW_ERR(FUNC_ADPT_FMT" _sd_iread32 callback is NULL\n", FUNC_ADPT_ARG(adapter));
  179. return r_val;
  180. }
  181. int _rtw_sd_iwrite8(_adapter *adapter, u32 addr, u8 val)
  182. {
  183. struct io_priv *pio_priv = &adapter->iopriv;
  184. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  185. int (*_sd_iwrite8)(struct intf_hdl *pintfhdl, u32 addr, u8 val);
  186. int ret = -1;
  187. _sd_iwrite8 = pintfhdl->io_ops._sd_iwrite8;
  188. if (_sd_iwrite8)
  189. ret = _sd_iwrite8(pintfhdl, addr, val);
  190. else
  191. RTW_ERR(FUNC_ADPT_FMT" _sd_iwrite8 callback is NULL\n", FUNC_ADPT_ARG(adapter));
  192. return RTW_STATUS_CODE(ret);
  193. }
  194. int _rtw_sd_iwrite16(_adapter *adapter, u32 addr, u16 val)
  195. {
  196. struct io_priv *pio_priv = &adapter->iopriv;
  197. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  198. int (*_sd_iwrite16)(struct intf_hdl *pintfhdl, u32 addr, u16 val);
  199. int ret = -1;
  200. _sd_iwrite16 = pintfhdl->io_ops._sd_iwrite16;
  201. if (_sd_iwrite16)
  202. ret = _sd_iwrite16(pintfhdl, addr, val);
  203. else
  204. RTW_ERR(FUNC_ADPT_FMT" _sd_iwrite16 callback is NULL\n", FUNC_ADPT_ARG(adapter));
  205. return RTW_STATUS_CODE(ret);
  206. }
  207. int _rtw_sd_iwrite32(_adapter *adapter, u32 addr, u32 val)
  208. {
  209. struct io_priv *pio_priv = &adapter->iopriv;
  210. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  211. int (*_sd_iwrite32)(struct intf_hdl *pintfhdl, u32 addr, u32 val);
  212. int ret = -1;
  213. _sd_iwrite32 = pintfhdl->io_ops._sd_iwrite32;
  214. if (_sd_iwrite32)
  215. ret = _sd_iwrite32(pintfhdl, addr, val);
  216. else
  217. RTW_ERR(FUNC_ADPT_FMT" _sd_iwrite32 callback is NULL\n", FUNC_ADPT_ARG(adapter));
  218. return RTW_STATUS_CODE(ret);
  219. }
  220. #endif /* CONFIG_SDIO_INDIRECT_ACCESS */
  221. #endif /* CONFIG_SDIO_HCI */
  222. int _rtw_write8_async(_adapter *adapter, u32 addr, u8 val)
  223. {
  224. /* struct io_queue *pio_queue = (struct io_queue *)adapter->pio_queue; */
  225. struct io_priv *pio_priv = &adapter->iopriv;
  226. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  227. int (*_write8_async)(struct intf_hdl *pintfhdl, u32 addr, u8 val);
  228. int ret;
  229. _write8_async = pintfhdl->io_ops._write8_async;
  230. ret = _write8_async(pintfhdl, addr, val);
  231. return RTW_STATUS_CODE(ret);
  232. }
  233. int _rtw_write16_async(_adapter *adapter, u32 addr, u16 val)
  234. {
  235. /* struct io_queue *pio_queue = (struct io_queue *)adapter->pio_queue; */
  236. struct io_priv *pio_priv = &adapter->iopriv;
  237. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  238. int (*_write16_async)(struct intf_hdl *pintfhdl, u32 addr, u16 val);
  239. int ret;
  240. _write16_async = pintfhdl->io_ops._write16_async;
  241. val = rtw_cpu_to_le16(val);
  242. ret = _write16_async(pintfhdl, addr, val);
  243. return RTW_STATUS_CODE(ret);
  244. }
  245. int _rtw_write32_async(_adapter *adapter, u32 addr, u32 val)
  246. {
  247. /* struct io_queue *pio_queue = (struct io_queue *)adapter->pio_queue; */
  248. struct io_priv *pio_priv = &adapter->iopriv;
  249. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  250. int (*_write32_async)(struct intf_hdl *pintfhdl, u32 addr, u32 val);
  251. int ret;
  252. _write32_async = pintfhdl->io_ops._write32_async;
  253. val = rtw_cpu_to_le32(val);
  254. ret = _write32_async(pintfhdl, addr, val);
  255. return RTW_STATUS_CODE(ret);
  256. }
  257. void _rtw_read_mem(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
  258. {
  259. void (*_read_mem)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
  260. /* struct io_queue *pio_queue = (struct io_queue *)adapter->pio_queue; */
  261. struct io_priv *pio_priv = &adapter->iopriv;
  262. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  263. if (RTW_CANNOT_RUN(adapter)) {
  264. return;
  265. }
  266. _read_mem = pintfhdl->io_ops._read_mem;
  267. _read_mem(pintfhdl, addr, cnt, pmem);
  268. }
  269. void _rtw_write_mem(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
  270. {
  271. void (*_write_mem)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
  272. /* struct io_queue *pio_queue = (struct io_queue *)adapter->pio_queue; */
  273. struct io_priv *pio_priv = &adapter->iopriv;
  274. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  275. _write_mem = pintfhdl->io_ops._write_mem;
  276. _write_mem(pintfhdl, addr, cnt, pmem);
  277. }
  278. void _rtw_read_port(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
  279. {
  280. u32(*_read_port)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
  281. /* struct io_queue *pio_queue = (struct io_queue *)adapter->pio_queue; */
  282. struct io_priv *pio_priv = &adapter->iopriv;
  283. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  284. if (RTW_CANNOT_RUN(adapter)) {
  285. return;
  286. }
  287. _read_port = pintfhdl->io_ops._read_port;
  288. _read_port(pintfhdl, addr, cnt, pmem);
  289. }
  290. void _rtw_read_port_cancel(_adapter *adapter)
  291. {
  292. void (*_read_port_cancel)(struct intf_hdl *pintfhdl);
  293. struct io_priv *pio_priv = &adapter->iopriv;
  294. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  295. _read_port_cancel = pintfhdl->io_ops._read_port_cancel;
  296. RTW_DISABLE_FUNC(adapter, DF_RX_BIT);
  297. if (_read_port_cancel)
  298. _read_port_cancel(pintfhdl);
  299. }
  300. u32 _rtw_write_port(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem)
  301. {
  302. u32(*_write_port)(struct intf_hdl *pintfhdl, u32 addr, u32 cnt, u8 *pmem);
  303. /* struct io_queue *pio_queue = (struct io_queue *)adapter->pio_queue; */
  304. struct io_priv *pio_priv = &adapter->iopriv;
  305. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  306. u32 ret = _SUCCESS;
  307. _write_port = pintfhdl->io_ops._write_port;
  308. ret = _write_port(pintfhdl, addr, cnt, pmem);
  309. return ret;
  310. }
  311. u32 _rtw_write_port_and_wait(_adapter *adapter, u32 addr, u32 cnt, u8 *pmem, int timeout_ms)
  312. {
  313. int ret = _SUCCESS;
  314. struct xmit_buf *pxmitbuf = (struct xmit_buf *)pmem;
  315. struct submit_ctx sctx;
  316. rtw_sctx_init(&sctx, timeout_ms);
  317. pxmitbuf->sctx = &sctx;
  318. ret = _rtw_write_port(adapter, addr, cnt, pmem);
  319. if (ret == _SUCCESS) {
  320. ret = rtw_sctx_wait(&sctx, __func__);
  321. if (ret != _SUCCESS)
  322. pxmitbuf->sctx = NULL;
  323. }
  324. return ret;
  325. }
  326. void _rtw_write_port_cancel(_adapter *adapter)
  327. {
  328. void (*_write_port_cancel)(struct intf_hdl *pintfhdl);
  329. struct io_priv *pio_priv = &adapter->iopriv;
  330. struct intf_hdl *pintfhdl = &(pio_priv->intf);
  331. _write_port_cancel = pintfhdl->io_ops._write_port_cancel;
  332. RTW_DISABLE_FUNC(adapter, DF_TX_BIT);
  333. if (_write_port_cancel)
  334. _write_port_cancel(pintfhdl);
  335. }
  336. int rtw_init_io_priv(_adapter *padapter, void (*set_intf_ops)(_adapter *padapter, struct _io_ops *pops))
  337. {
  338. struct io_priv *piopriv = &padapter->iopriv;
  339. struct intf_hdl *pintf = &piopriv->intf;
  340. if (set_intf_ops == NULL)
  341. return _FAIL;
  342. piopriv->padapter = padapter;
  343. pintf->padapter = padapter;
  344. pintf->pintf_dev = adapter_to_dvobj(padapter);
  345. set_intf_ops(padapter, &pintf->io_ops);
  346. return _SUCCESS;
  347. }
  348. /*
  349. * Increase and check if the continual_io_error of this @param dvobjprive is larger than MAX_CONTINUAL_IO_ERR
  350. * @return _TRUE:
  351. * @return _FALSE:
  352. */
  353. int rtw_inc_and_chk_continual_io_error(struct dvobj_priv *dvobj)
  354. {
  355. int ret = _FALSE;
  356. int value;
  357. value = ATOMIC_INC_RETURN(&dvobj->continual_io_error);
  358. if (value > MAX_CONTINUAL_IO_ERR) {
  359. RTW_INFO("[dvobj:%p][ERROR] continual_io_error:%d > %d\n", dvobj, value, MAX_CONTINUAL_IO_ERR);
  360. ret = _TRUE;
  361. } else {
  362. /* RTW_INFO("[dvobj:%p] continual_io_error:%d\n", dvobj, value); */
  363. }
  364. return ret;
  365. }
  366. /*
  367. * Set the continual_io_error of this @param dvobjprive to 0
  368. */
  369. void rtw_reset_continual_io_error(struct dvobj_priv *dvobj)
  370. {
  371. ATOMIC_SET(&dvobj->continual_io_error, 0);
  372. }
  373. #ifdef DBG_IO
  374. #define RTW_IO_SNIFF_TYPE_RANGE 0 /* specific address range is accessed */
  375. #define RTW_IO_SNIFF_TYPE_EN 1 /* part or all sniffed range is enabled */
  376. #define RTW_IO_SNIFF_TYPE_DIS 2 /* part or all sniffed range is disabled */
  377. struct rtw_io_sniff_ent {
  378. u8 chip;
  379. u8 hci;
  380. u32 addr;
  381. u8 type;
  382. union {
  383. u32 end_addr;
  384. u32 mask;
  385. } u;
  386. char *tag;
  387. };
  388. const char *rtw_io_sniff_ent_get_tag(const struct rtw_io_sniff_ent *ent)
  389. {
  390. return ent->tag;
  391. }
  392. #define RTW_IO_SNIFF_RANGE_ENT(_chip, _hci, _addr, _end_addr, _tag) \
  393. {.chip = _chip, .hci = _hci, .addr = _addr, .u.end_addr = _end_addr, .tag = _tag, .type = RTW_IO_SNIFF_TYPE_RANGE,}
  394. #define RTW_IO_SNIFF_EN_ENT(_chip, _hci, _addr, _mask, _tag) \
  395. {.chip = _chip, .hci = _hci, .addr = _addr, .u.mask = _mask, .tag = _tag, .type = RTW_IO_SNIFF_TYPE_EN,}
  396. #define RTW_IO_SNIFF_DIS_ENT(_chip, _hci, _addr, _mask, _tag) \
  397. {.chip = _chip, .hci = _hci, .addr = _addr, .u.mask = _mask, .tag = _tag, .type = RTW_IO_SNIFF_TYPE_DIS,}
  398. const struct rtw_io_sniff_ent read_sniff[] = {
  399. #ifdef DBG_IO_HCI_EN_CHK
  400. RTW_IO_SNIFF_EN_ENT(MAX_CHIP_TYPE, RTW_SDIO, 0x02, 0x1FC, "SDIO 0x02[8:2] not all 0"),
  401. RTW_IO_SNIFF_EN_ENT(MAX_CHIP_TYPE, RTW_USB, 0x02, 0x1E0, "USB 0x02[8:5] not all 0"),
  402. RTW_IO_SNIFF_EN_ENT(MAX_CHIP_TYPE, RTW_PCIE, 0x02, 0x01C, "PCI 0x02[4:2] not all 0"),
  403. #endif
  404. #ifdef DBG_IO_SNIFF_EXAMPLE
  405. RTW_IO_SNIFF_RANGE_ENT(MAX_CHIP_TYPE, 0, 0x522, 0x522, "read TXPAUSE"),
  406. RTW_IO_SNIFF_DIS_ENT(MAX_CHIP_TYPE, 0, 0x02, 0x3, "0x02[1:0] not all 1"),
  407. #endif
  408. };
  409. const int read_sniff_num = sizeof(read_sniff) / sizeof(struct rtw_io_sniff_ent);
  410. const struct rtw_io_sniff_ent write_sniff[] = {
  411. #ifdef DBG_IO_HCI_EN_CHK
  412. RTW_IO_SNIFF_EN_ENT(MAX_CHIP_TYPE, RTW_SDIO, 0x02, 0x1FC, "SDIO 0x02[8:2] not all 0"),
  413. RTW_IO_SNIFF_EN_ENT(MAX_CHIP_TYPE, RTW_USB, 0x02, 0x1E0, "USB 0x02[8:5] not all 0"),
  414. RTW_IO_SNIFF_EN_ENT(MAX_CHIP_TYPE, RTW_PCIE, 0x02, 0x01C, "PCI 0x02[4:2] not all 0"),
  415. #endif
  416. #ifdef DBG_IO_SNIFF_EXAMPLE
  417. RTW_IO_SNIFF_RANGE_ENT(MAX_CHIP_TYPE, 0, 0x522, 0x522, "write TXPAUSE"),
  418. RTW_IO_SNIFF_DIS_ENT(MAX_CHIP_TYPE, 0, 0x02, 0x3, "0x02[1:0] not all 1"),
  419. #endif
  420. };
  421. const int write_sniff_num = sizeof(write_sniff) / sizeof(struct rtw_io_sniff_ent);
  422. static bool match_io_sniff_ranges(_adapter *adapter
  423. , const struct rtw_io_sniff_ent *sniff, int i, u32 addr, u16 len)
  424. {
  425. /* check if IO range after sniff end address */
  426. if (addr > sniff->u.end_addr)
  427. return 0;
  428. return 1;
  429. }
  430. static bool match_io_sniff_en(_adapter *adapter
  431. , const struct rtw_io_sniff_ent *sniff, int i, u32 addr, u8 len, u32 val)
  432. {
  433. u8 sniff_len;
  434. u8 shift;
  435. u32 mask;
  436. bool ret = 0;
  437. /* check if IO range after sniff end address */
  438. sniff_len = 4;
  439. while (!(sniff->u.mask & (0xFF << ((sniff_len - 1) * 8)))) {
  440. sniff_len--;
  441. if (sniff_len == 0)
  442. goto exit;
  443. }
  444. if (sniff->addr + sniff_len <= addr)
  445. goto exit;
  446. if (sniff->addr > addr) {
  447. shift = (sniff->addr - addr) * 8;
  448. mask = sniff->u.mask << shift;
  449. } else if (sniff->addr < addr) {
  450. shift = (addr - sniff->addr) * 8;
  451. mask = sniff->u.mask >> shift;
  452. } else {
  453. shift = 0;
  454. mask = sniff->u.mask;
  455. }
  456. if (sniff->type == RTW_IO_SNIFF_TYPE_DIS) {
  457. if (len == 4)
  458. mask &= 0xFFFFFFFF;
  459. else if (len == 3)
  460. mask &= 0x00FFFFFF;
  461. else if (len == 2)
  462. mask &= 0x0000FFFF;
  463. else if (len == 1)
  464. mask &= 0x000000FF;
  465. else
  466. mask &= 0x00000000;
  467. }
  468. if ((sniff->type == RTW_IO_SNIFF_TYPE_EN && (mask & val))
  469. || (sniff->type == RTW_IO_SNIFF_TYPE_DIS && (mask & val) != mask)
  470. ) {
  471. ret = 1;
  472. if (0)
  473. RTW_INFO(FUNC_ADPT_FMT" addr:0x%x len:%u val:0x%x i:%d sniff_len:%u shift:%u mask:0x%x\n"
  474. , FUNC_ADPT_ARG(adapter), addr, len, val, i, sniff_len, shift, mask);
  475. }
  476. exit:
  477. return ret;
  478. }
  479. static bool match_io_sniff(_adapter *adapter
  480. , const struct rtw_io_sniff_ent *sniff, int i, u32 addr, u8 len, u32 val)
  481. {
  482. bool ret = 0;
  483. if (sniff->chip != MAX_CHIP_TYPE
  484. && sniff->chip != rtw_get_chip_type(adapter))
  485. goto exit;
  486. if (sniff->hci
  487. && !(sniff->hci & rtw_get_intf_type(adapter)))
  488. goto exit;
  489. if (sniff->addr >= addr + len) /* IO range below sniff start address */
  490. goto exit;
  491. switch (sniff->type) {
  492. case RTW_IO_SNIFF_TYPE_RANGE:
  493. ret = match_io_sniff_ranges(adapter, sniff, i, addr, len);
  494. break;
  495. case RTW_IO_SNIFF_TYPE_EN:
  496. case RTW_IO_SNIFF_TYPE_DIS:
  497. if (len == 1 || len == 2 || len == 4)
  498. ret = match_io_sniff_en(adapter, sniff, i, addr, len, val);
  499. break;
  500. default:
  501. rtw_warn_on(1);
  502. break;
  503. }
  504. exit:
  505. return ret;
  506. }
  507. const struct rtw_io_sniff_ent *match_read_sniff(_adapter *adapter
  508. , u32 addr, u16 len, u32 val)
  509. {
  510. int i;
  511. bool ret = 0;
  512. for (i = 0; i < read_sniff_num; i++) {
  513. ret = match_io_sniff(adapter, &read_sniff[i], i, addr, len, val);
  514. if (ret)
  515. goto exit;
  516. }
  517. exit:
  518. return ret ? &read_sniff[i] : NULL;
  519. }
  520. const struct rtw_io_sniff_ent *match_write_sniff(_adapter *adapter
  521. , u32 addr, u16 len, u32 val)
  522. {
  523. int i;
  524. bool ret = 0;
  525. for (i = 0; i < write_sniff_num; i++) {
  526. ret = match_io_sniff(adapter, &write_sniff[i], i, addr, len, val);
  527. if (ret)
  528. goto exit;
  529. }
  530. exit:
  531. return ret ? &write_sniff[i] : NULL;
  532. }
  533. struct rf_sniff_ent {
  534. u8 path;
  535. u16 reg;
  536. u32 mask;
  537. };
  538. struct rf_sniff_ent rf_read_sniff_ranges[] = {
  539. /* example for all path addr 0x55 with all RF Reg mask */
  540. /* {MAX_RF_PATH, 0x55, bRFRegOffsetMask}, */
  541. };
  542. struct rf_sniff_ent rf_write_sniff_ranges[] = {
  543. /* example for all path addr 0x55 with all RF Reg mask */
  544. /* {MAX_RF_PATH, 0x55, bRFRegOffsetMask}, */
  545. };
  546. int rf_read_sniff_num = sizeof(rf_read_sniff_ranges) / sizeof(struct rf_sniff_ent);
  547. int rf_write_sniff_num = sizeof(rf_write_sniff_ranges) / sizeof(struct rf_sniff_ent);
  548. bool match_rf_read_sniff_ranges(_adapter *adapter, u8 path, u32 addr, u32 mask)
  549. {
  550. int i;
  551. for (i = 0; i < rf_read_sniff_num; i++) {
  552. if (rf_read_sniff_ranges[i].path == MAX_RF_PATH || rf_read_sniff_ranges[i].path == path)
  553. if (addr == rf_read_sniff_ranges[i].reg && (mask & rf_read_sniff_ranges[i].mask))
  554. return _TRUE;
  555. }
  556. return _FALSE;
  557. }
  558. bool match_rf_write_sniff_ranges(_adapter *adapter, u8 path, u32 addr, u32 mask)
  559. {
  560. int i;
  561. for (i = 0; i < rf_write_sniff_num; i++) {
  562. if (rf_write_sniff_ranges[i].path == MAX_RF_PATH || rf_write_sniff_ranges[i].path == path)
  563. if (addr == rf_write_sniff_ranges[i].reg && (mask & rf_write_sniff_ranges[i].mask))
  564. return _TRUE;
  565. }
  566. return _FALSE;
  567. }
  568. u8 dbg_rtw_read8(_adapter *adapter, u32 addr, const char *caller, const int line)
  569. {
  570. u8 val = _rtw_read8(adapter, addr);
  571. const struct rtw_io_sniff_ent *ent = match_read_sniff(adapter, addr, 1, val);
  572. if (ent) {
  573. RTW_INFO("DBG_IO %s:%d rtw_read8(0x%04x) return 0x%02x %s\n"
  574. , caller, line, addr, val, rtw_io_sniff_ent_get_tag(ent));
  575. }
  576. return val;
  577. }
  578. u16 dbg_rtw_read16(_adapter *adapter, u32 addr, const char *caller, const int line)
  579. {
  580. u16 val = _rtw_read16(adapter, addr);
  581. const struct rtw_io_sniff_ent *ent = match_read_sniff(adapter, addr, 2, val);
  582. if (ent) {
  583. RTW_INFO("DBG_IO %s:%d rtw_read16(0x%04x) return 0x%04x %s\n"
  584. , caller, line, addr, val, rtw_io_sniff_ent_get_tag(ent));
  585. }
  586. return val;
  587. }
  588. u32 dbg_rtw_read32(_adapter *adapter, u32 addr, const char *caller, const int line)
  589. {
  590. u32 val = _rtw_read32(adapter, addr);
  591. const struct rtw_io_sniff_ent *ent = match_read_sniff(adapter, addr, 4, val);
  592. if (ent) {
  593. RTW_INFO("DBG_IO %s:%d rtw_read32(0x%04x) return 0x%08x %s\n"
  594. , caller, line, addr, val, rtw_io_sniff_ent_get_tag(ent));
  595. }
  596. return val;
  597. }
  598. int dbg_rtw_write8(_adapter *adapter, u32 addr, u8 val, const char *caller, const int line)
  599. {
  600. const struct rtw_io_sniff_ent *ent = match_write_sniff(adapter, addr, 1, val);
  601. if (ent) {
  602. RTW_INFO("DBG_IO %s:%d rtw_write8(0x%04x, 0x%02x) %s\n"
  603. , caller, line, addr, val, rtw_io_sniff_ent_get_tag(ent));
  604. }
  605. return _rtw_write8(adapter, addr, val);
  606. }
  607. int dbg_rtw_write16(_adapter *adapter, u32 addr, u16 val, const char *caller, const int line)
  608. {
  609. const struct rtw_io_sniff_ent *ent = match_write_sniff(adapter, addr, 2, val);
  610. if (ent) {
  611. RTW_INFO("DBG_IO %s:%d rtw_write16(0x%04x, 0x%04x) %s\n"
  612. , caller, line, addr, val, rtw_io_sniff_ent_get_tag(ent));
  613. }
  614. return _rtw_write16(adapter, addr, val);
  615. }
  616. int dbg_rtw_write32(_adapter *adapter, u32 addr, u32 val, const char *caller, const int line)
  617. {
  618. const struct rtw_io_sniff_ent *ent = match_write_sniff(adapter, addr, 4, val);
  619. if (ent) {
  620. RTW_INFO("DBG_IO %s:%d rtw_write32(0x%04x, 0x%08x) %s\n"
  621. , caller, line, addr, val, rtw_io_sniff_ent_get_tag(ent));
  622. }
  623. return _rtw_write32(adapter, addr, val);
  624. }
  625. int dbg_rtw_writeN(_adapter *adapter, u32 addr , u32 length , u8 *data, const char *caller, const int line)
  626. {
  627. const struct rtw_io_sniff_ent *ent = match_write_sniff(adapter, addr, length, 0);
  628. if (ent) {
  629. RTW_INFO("DBG_IO %s:%d rtw_writeN(0x%04x, %u) %s\n"
  630. , caller, line, addr, length, rtw_io_sniff_ent_get_tag(ent));
  631. }
  632. return _rtw_writeN(adapter, addr, length, data);
  633. }
  634. #ifdef CONFIG_SDIO_HCI
  635. u8 dbg_rtw_sd_f0_read8(_adapter *adapter, u32 addr, const char *caller, const int line)
  636. {
  637. u8 val = _rtw_sd_f0_read8(adapter, addr);
  638. #if 0
  639. const struct rtw_io_sniff_ent *ent = match_read_sniff(adapter, addr, 1, val);
  640. if (ent) {
  641. RTW_INFO("DBG_IO %s:%d rtw_sd_f0_read8(0x%04x) return 0x%02x %s\n"
  642. , caller, line, addr, val, rtw_io_sniff_ent_get_tag(ent));
  643. }
  644. #endif
  645. return val;
  646. }
  647. #ifdef CONFIG_SDIO_INDIRECT_ACCESS
  648. u8 dbg_rtw_sd_iread8(_adapter *adapter, u32 addr, const char *caller, const int line)
  649. {
  650. u8 val = rtw_sd_iread8(adapter, addr);
  651. const struct rtw_io_sniff_ent *ent = match_read_sniff(adapter, addr, 1, val);
  652. if (ent) {
  653. RTW_INFO("DBG_IO %s:%d rtw_sd_iread8(0x%04x) return 0x%02x %s\n"
  654. , caller, line, addr, val, rtw_io_sniff_ent_get_tag(ent));
  655. }
  656. return val;
  657. }
  658. u16 dbg_rtw_sd_iread16(_adapter *adapter, u32 addr, const char *caller, const int line)
  659. {
  660. u16 val = _rtw_sd_iread16(adapter, addr);
  661. const struct rtw_io_sniff_ent *ent = match_read_sniff(adapter, addr, 2, val);
  662. if (ent) {
  663. RTW_INFO("DBG_IO %s:%d rtw_sd_iread16(0x%04x) return 0x%04x %s\n"
  664. , caller, line, addr, val, rtw_io_sniff_ent_get_tag(ent));
  665. }
  666. return val;
  667. }
  668. u32 dbg_rtw_sd_iread32(_adapter *adapter, u32 addr, const char *caller, const int line)
  669. {
  670. u32 val = _rtw_sd_iread32(adapter, addr);
  671. const struct rtw_io_sniff_ent *ent = match_read_sniff(adapter, addr, 4, val);
  672. if (ent) {
  673. RTW_INFO("DBG_IO %s:%d rtw_sd_iread32(0x%04x) return 0x%08x %s\n"
  674. , caller, line, addr, val, rtw_io_sniff_ent_get_tag(ent));
  675. }
  676. return val;
  677. }
  678. int dbg_rtw_sd_iwrite8(_adapter *adapter, u32 addr, u8 val, const char *caller, const int line)
  679. {
  680. const struct rtw_io_sniff_ent *ent = match_write_sniff(adapter, addr, 1, val);
  681. if (ent) {
  682. RTW_INFO("DBG_IO %s:%d rtw_sd_iwrite8(0x%04x, 0x%02x) %s\n"
  683. , caller, line, addr, val, rtw_io_sniff_ent_get_tag(ent));
  684. }
  685. return _rtw_sd_iwrite8(adapter, addr, val);
  686. }
  687. int dbg_rtw_sd_iwrite16(_adapter *adapter, u32 addr, u16 val, const char *caller, const int line)
  688. {
  689. const struct rtw_io_sniff_ent *ent = match_write_sniff(adapter, addr, 2, val);
  690. if (ent) {
  691. RTW_INFO("DBG_IO %s:%d rtw_sd_iwrite16(0x%04x, 0x%04x) %s\n"
  692. , caller, line, addr, val, rtw_io_sniff_ent_get_tag(ent));
  693. }
  694. return _rtw_sd_iwrite16(adapter, addr, val);
  695. }
  696. int dbg_rtw_sd_iwrite32(_adapter *adapter, u32 addr, u32 val, const char *caller, const int line)
  697. {
  698. const struct rtw_io_sniff_ent *ent = match_write_sniff(adapter, addr, 4, val);
  699. if (ent) {
  700. RTW_INFO("DBG_IO %s:%d rtw_sd_iwrite32(0x%04x, 0x%08x) %s\n"
  701. , caller, line, addr, val, rtw_io_sniff_ent_get_tag(ent));
  702. }
  703. return _rtw_sd_iwrite32(adapter, addr, val);
  704. }
  705. #endif /* CONFIG_SDIO_INDIRECT_ACCESS */
  706. #endif /* CONFIG_SDIO_HCI */
  707. #endif