qed_hw.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  1. /* QLogic qed NIC Driver
  2. * Copyright (c) 2015-2017 QLogic Corporation
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and /or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #include <linux/types.h>
  33. #include <linux/io.h>
  34. #include <linux/delay.h>
  35. #include <linux/dma-mapping.h>
  36. #include <linux/errno.h>
  37. #include <linux/kernel.h>
  38. #include <linux/list.h>
  39. #include <linux/mutex.h>
  40. #include <linux/pci.h>
  41. #include <linux/slab.h>
  42. #include <linux/spinlock.h>
  43. #include <linux/string.h>
  44. #include <linux/qed/qed_chain.h>
  45. #include "qed.h"
  46. #include "qed_hsi.h"
  47. #include "qed_hw.h"
  48. #include "qed_reg_addr.h"
  49. #include "qed_sriov.h"
  50. #define QED_BAR_ACQUIRE_TIMEOUT 1000
  51. /* Invalid values */
  52. #define QED_BAR_INVALID_OFFSET (cpu_to_le32(-1))
  53. struct qed_ptt {
  54. struct list_head list_entry;
  55. unsigned int idx;
  56. struct pxp_ptt_entry pxp;
  57. u8 hwfn_id;
  58. };
  59. struct qed_ptt_pool {
  60. struct list_head free_list;
  61. spinlock_t lock; /* ptt synchronized access */
  62. struct qed_ptt ptts[PXP_EXTERNAL_BAR_PF_WINDOW_NUM];
  63. };
  64. int qed_ptt_pool_alloc(struct qed_hwfn *p_hwfn)
  65. {
  66. struct qed_ptt_pool *p_pool = kmalloc(sizeof(*p_pool), GFP_KERNEL);
  67. int i;
  68. if (!p_pool)
  69. return -ENOMEM;
  70. INIT_LIST_HEAD(&p_pool->free_list);
  71. for (i = 0; i < PXP_EXTERNAL_BAR_PF_WINDOW_NUM; i++) {
  72. p_pool->ptts[i].idx = i;
  73. p_pool->ptts[i].pxp.offset = QED_BAR_INVALID_OFFSET;
  74. p_pool->ptts[i].pxp.pretend.control = 0;
  75. p_pool->ptts[i].hwfn_id = p_hwfn->my_id;
  76. if (i >= RESERVED_PTT_MAX)
  77. list_add(&p_pool->ptts[i].list_entry,
  78. &p_pool->free_list);
  79. }
  80. p_hwfn->p_ptt_pool = p_pool;
  81. spin_lock_init(&p_pool->lock);
  82. return 0;
  83. }
  84. void qed_ptt_invalidate(struct qed_hwfn *p_hwfn)
  85. {
  86. struct qed_ptt *p_ptt;
  87. int i;
  88. for (i = 0; i < PXP_EXTERNAL_BAR_PF_WINDOW_NUM; i++) {
  89. p_ptt = &p_hwfn->p_ptt_pool->ptts[i];
  90. p_ptt->pxp.offset = QED_BAR_INVALID_OFFSET;
  91. }
  92. }
  93. void qed_ptt_pool_free(struct qed_hwfn *p_hwfn)
  94. {
  95. kfree(p_hwfn->p_ptt_pool);
  96. p_hwfn->p_ptt_pool = NULL;
  97. }
  98. struct qed_ptt *qed_ptt_acquire(struct qed_hwfn *p_hwfn)
  99. {
  100. struct qed_ptt *p_ptt;
  101. unsigned int i;
  102. /* Take the free PTT from the list */
  103. for (i = 0; i < QED_BAR_ACQUIRE_TIMEOUT; i++) {
  104. spin_lock_bh(&p_hwfn->p_ptt_pool->lock);
  105. if (!list_empty(&p_hwfn->p_ptt_pool->free_list)) {
  106. p_ptt = list_first_entry(&p_hwfn->p_ptt_pool->free_list,
  107. struct qed_ptt, list_entry);
  108. list_del(&p_ptt->list_entry);
  109. spin_unlock_bh(&p_hwfn->p_ptt_pool->lock);
  110. DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
  111. "allocated ptt %d\n", p_ptt->idx);
  112. return p_ptt;
  113. }
  114. spin_unlock_bh(&p_hwfn->p_ptt_pool->lock);
  115. usleep_range(1000, 2000);
  116. }
  117. DP_NOTICE(p_hwfn, "PTT acquire timeout - failed to allocate PTT\n");
  118. return NULL;
  119. }
  120. void qed_ptt_release(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
  121. {
  122. spin_lock_bh(&p_hwfn->p_ptt_pool->lock);
  123. list_add(&p_ptt->list_entry, &p_hwfn->p_ptt_pool->free_list);
  124. spin_unlock_bh(&p_hwfn->p_ptt_pool->lock);
  125. }
  126. u32 qed_ptt_get_hw_addr(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
  127. {
  128. /* The HW is using DWORDS and we need to translate it to Bytes */
  129. return le32_to_cpu(p_ptt->pxp.offset) << 2;
  130. }
  131. static u32 qed_ptt_config_addr(struct qed_ptt *p_ptt)
  132. {
  133. return PXP_PF_WINDOW_ADMIN_PER_PF_START +
  134. p_ptt->idx * sizeof(struct pxp_ptt_entry);
  135. }
  136. u32 qed_ptt_get_bar_addr(struct qed_ptt *p_ptt)
  137. {
  138. return PXP_EXTERNAL_BAR_PF_WINDOW_START +
  139. p_ptt->idx * PXP_EXTERNAL_BAR_PF_WINDOW_SINGLE_SIZE;
  140. }
  141. void qed_ptt_set_win(struct qed_hwfn *p_hwfn,
  142. struct qed_ptt *p_ptt, u32 new_hw_addr)
  143. {
  144. u32 prev_hw_addr;
  145. prev_hw_addr = qed_ptt_get_hw_addr(p_hwfn, p_ptt);
  146. if (new_hw_addr == prev_hw_addr)
  147. return;
  148. /* Update PTT entery in admin window */
  149. DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
  150. "Updating PTT entry %d to offset 0x%x\n",
  151. p_ptt->idx, new_hw_addr);
  152. /* The HW is using DWORDS and the address is in Bytes */
  153. p_ptt->pxp.offset = cpu_to_le32(new_hw_addr >> 2);
  154. REG_WR(p_hwfn,
  155. qed_ptt_config_addr(p_ptt) +
  156. offsetof(struct pxp_ptt_entry, offset),
  157. le32_to_cpu(p_ptt->pxp.offset));
  158. }
  159. static u32 qed_set_ptt(struct qed_hwfn *p_hwfn,
  160. struct qed_ptt *p_ptt, u32 hw_addr)
  161. {
  162. u32 win_hw_addr = qed_ptt_get_hw_addr(p_hwfn, p_ptt);
  163. u32 offset;
  164. offset = hw_addr - win_hw_addr;
  165. if (p_ptt->hwfn_id != p_hwfn->my_id)
  166. DP_NOTICE(p_hwfn,
  167. "ptt[%d] of hwfn[%02x] is used by hwfn[%02x]!\n",
  168. p_ptt->idx, p_ptt->hwfn_id, p_hwfn->my_id);
  169. /* Verify the address is within the window */
  170. if (hw_addr < win_hw_addr ||
  171. offset >= PXP_EXTERNAL_BAR_PF_WINDOW_SINGLE_SIZE) {
  172. qed_ptt_set_win(p_hwfn, p_ptt, hw_addr);
  173. offset = 0;
  174. }
  175. return qed_ptt_get_bar_addr(p_ptt) + offset;
  176. }
  177. struct qed_ptt *qed_get_reserved_ptt(struct qed_hwfn *p_hwfn,
  178. enum reserved_ptts ptt_idx)
  179. {
  180. if (ptt_idx >= RESERVED_PTT_MAX) {
  181. DP_NOTICE(p_hwfn,
  182. "Requested PTT %d is out of range\n", ptt_idx);
  183. return NULL;
  184. }
  185. return &p_hwfn->p_ptt_pool->ptts[ptt_idx];
  186. }
  187. void qed_wr(struct qed_hwfn *p_hwfn,
  188. struct qed_ptt *p_ptt,
  189. u32 hw_addr, u32 val)
  190. {
  191. u32 bar_addr = qed_set_ptt(p_hwfn, p_ptt, hw_addr);
  192. REG_WR(p_hwfn, bar_addr, val);
  193. DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
  194. "bar_addr 0x%x, hw_addr 0x%x, val 0x%x\n",
  195. bar_addr, hw_addr, val);
  196. }
  197. u32 qed_rd(struct qed_hwfn *p_hwfn,
  198. struct qed_ptt *p_ptt,
  199. u32 hw_addr)
  200. {
  201. u32 bar_addr = qed_set_ptt(p_hwfn, p_ptt, hw_addr);
  202. u32 val = REG_RD(p_hwfn, bar_addr);
  203. DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
  204. "bar_addr 0x%x, hw_addr 0x%x, val 0x%x\n",
  205. bar_addr, hw_addr, val);
  206. return val;
  207. }
  208. static void qed_memcpy_hw(struct qed_hwfn *p_hwfn,
  209. struct qed_ptt *p_ptt,
  210. void *addr, u32 hw_addr, size_t n, bool to_device)
  211. {
  212. u32 dw_count, *host_addr, hw_offset;
  213. size_t quota, done = 0;
  214. u32 __iomem *reg_addr;
  215. while (done < n) {
  216. quota = min_t(size_t, n - done,
  217. PXP_EXTERNAL_BAR_PF_WINDOW_SINGLE_SIZE);
  218. if (IS_PF(p_hwfn->cdev)) {
  219. qed_ptt_set_win(p_hwfn, p_ptt, hw_addr + done);
  220. hw_offset = qed_ptt_get_bar_addr(p_ptt);
  221. } else {
  222. hw_offset = hw_addr + done;
  223. }
  224. dw_count = quota / 4;
  225. host_addr = (u32 *)((u8 *)addr + done);
  226. reg_addr = (u32 __iomem *)REG_ADDR(p_hwfn, hw_offset);
  227. if (to_device)
  228. while (dw_count--)
  229. DIRECT_REG_WR(reg_addr++, *host_addr++);
  230. else
  231. while (dw_count--)
  232. *host_addr++ = DIRECT_REG_RD(reg_addr++);
  233. done += quota;
  234. }
  235. }
  236. void qed_memcpy_from(struct qed_hwfn *p_hwfn,
  237. struct qed_ptt *p_ptt, void *dest, u32 hw_addr, size_t n)
  238. {
  239. DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
  240. "hw_addr 0x%x, dest %p hw_addr 0x%x, size %lu\n",
  241. hw_addr, dest, hw_addr, (unsigned long)n);
  242. qed_memcpy_hw(p_hwfn, p_ptt, dest, hw_addr, n, false);
  243. }
  244. void qed_memcpy_to(struct qed_hwfn *p_hwfn,
  245. struct qed_ptt *p_ptt, u32 hw_addr, void *src, size_t n)
  246. {
  247. DP_VERBOSE(p_hwfn, NETIF_MSG_HW,
  248. "hw_addr 0x%x, hw_addr 0x%x, src %p size %lu\n",
  249. hw_addr, hw_addr, src, (unsigned long)n);
  250. qed_memcpy_hw(p_hwfn, p_ptt, src, hw_addr, n, true);
  251. }
  252. void qed_fid_pretend(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt, u16 fid)
  253. {
  254. u16 control = 0;
  255. SET_FIELD(control, PXP_PRETEND_CMD_IS_CONCRETE, 1);
  256. SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_FUNCTION, 1);
  257. /* Every pretend undos previous pretends, including
  258. * previous port pretend.
  259. */
  260. SET_FIELD(control, PXP_PRETEND_CMD_PORT, 0);
  261. SET_FIELD(control, PXP_PRETEND_CMD_USE_PORT, 0);
  262. SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_PORT, 1);
  263. if (!GET_FIELD(fid, PXP_CONCRETE_FID_VFVALID))
  264. fid = GET_FIELD(fid, PXP_CONCRETE_FID_PFID);
  265. p_ptt->pxp.pretend.control = cpu_to_le16(control);
  266. p_ptt->pxp.pretend.fid.concrete_fid.fid = cpu_to_le16(fid);
  267. REG_WR(p_hwfn,
  268. qed_ptt_config_addr(p_ptt) +
  269. offsetof(struct pxp_ptt_entry, pretend),
  270. *(u32 *)&p_ptt->pxp.pretend);
  271. }
  272. void qed_port_pretend(struct qed_hwfn *p_hwfn,
  273. struct qed_ptt *p_ptt, u8 port_id)
  274. {
  275. u16 control = 0;
  276. SET_FIELD(control, PXP_PRETEND_CMD_PORT, port_id);
  277. SET_FIELD(control, PXP_PRETEND_CMD_USE_PORT, 1);
  278. SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_PORT, 1);
  279. p_ptt->pxp.pretend.control = cpu_to_le16(control);
  280. REG_WR(p_hwfn,
  281. qed_ptt_config_addr(p_ptt) +
  282. offsetof(struct pxp_ptt_entry, pretend),
  283. *(u32 *)&p_ptt->pxp.pretend);
  284. }
  285. void qed_port_unpretend(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
  286. {
  287. u16 control = 0;
  288. SET_FIELD(control, PXP_PRETEND_CMD_PORT, 0);
  289. SET_FIELD(control, PXP_PRETEND_CMD_USE_PORT, 0);
  290. SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_PORT, 1);
  291. p_ptt->pxp.pretend.control = cpu_to_le16(control);
  292. REG_WR(p_hwfn,
  293. qed_ptt_config_addr(p_ptt) +
  294. offsetof(struct pxp_ptt_entry, pretend),
  295. *(u32 *)&p_ptt->pxp.pretend);
  296. }
  297. void qed_port_fid_pretend(struct qed_hwfn *p_hwfn,
  298. struct qed_ptt *p_ptt, u8 port_id, u16 fid)
  299. {
  300. u16 control = 0;
  301. SET_FIELD(control, PXP_PRETEND_CMD_PORT, port_id);
  302. SET_FIELD(control, PXP_PRETEND_CMD_USE_PORT, 1);
  303. SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_PORT, 1);
  304. SET_FIELD(control, PXP_PRETEND_CMD_IS_CONCRETE, 1);
  305. SET_FIELD(control, PXP_PRETEND_CMD_PRETEND_FUNCTION, 1);
  306. if (!GET_FIELD(fid, PXP_CONCRETE_FID_VFVALID))
  307. fid = GET_FIELD(fid, PXP_CONCRETE_FID_PFID);
  308. p_ptt->pxp.pretend.control = cpu_to_le16(control);
  309. p_ptt->pxp.pretend.fid.concrete_fid.fid = cpu_to_le16(fid);
  310. REG_WR(p_hwfn,
  311. qed_ptt_config_addr(p_ptt) +
  312. offsetof(struct pxp_ptt_entry, pretend),
  313. *(u32 *)&p_ptt->pxp.pretend);
  314. }
  315. u32 qed_vfid_to_concrete(struct qed_hwfn *p_hwfn, u8 vfid)
  316. {
  317. u32 concrete_fid = 0;
  318. SET_FIELD(concrete_fid, PXP_CONCRETE_FID_PFID, p_hwfn->rel_pf_id);
  319. SET_FIELD(concrete_fid, PXP_CONCRETE_FID_VFID, vfid);
  320. SET_FIELD(concrete_fid, PXP_CONCRETE_FID_VFVALID, 1);
  321. return concrete_fid;
  322. }
  323. /* DMAE */
  324. static void qed_dmae_opcode(struct qed_hwfn *p_hwfn,
  325. const u8 is_src_type_grc,
  326. const u8 is_dst_type_grc,
  327. struct qed_dmae_params *p_params)
  328. {
  329. u16 opcode_b = 0;
  330. u32 opcode = 0;
  331. /* Whether the source is the PCIe or the GRC.
  332. * 0- The source is the PCIe
  333. * 1- The source is the GRC.
  334. */
  335. opcode |= (is_src_type_grc ? DMAE_CMD_SRC_MASK_GRC
  336. : DMAE_CMD_SRC_MASK_PCIE) <<
  337. DMAE_CMD_SRC_SHIFT;
  338. opcode |= ((p_hwfn->rel_pf_id & DMAE_CMD_SRC_PF_ID_MASK) <<
  339. DMAE_CMD_SRC_PF_ID_SHIFT);
  340. /* The destination of the DMA can be: 0-None 1-PCIe 2-GRC 3-None */
  341. opcode |= (is_dst_type_grc ? DMAE_CMD_DST_MASK_GRC
  342. : DMAE_CMD_DST_MASK_PCIE) <<
  343. DMAE_CMD_DST_SHIFT;
  344. opcode |= ((p_hwfn->rel_pf_id & DMAE_CMD_DST_PF_ID_MASK) <<
  345. DMAE_CMD_DST_PF_ID_SHIFT);
  346. /* Whether to write a completion word to the completion destination:
  347. * 0-Do not write a completion word
  348. * 1-Write the completion word
  349. */
  350. opcode |= (DMAE_CMD_COMP_WORD_EN_MASK << DMAE_CMD_COMP_WORD_EN_SHIFT);
  351. opcode |= (DMAE_CMD_SRC_ADDR_RESET_MASK <<
  352. DMAE_CMD_SRC_ADDR_RESET_SHIFT);
  353. if (p_params->flags & QED_DMAE_FLAG_COMPLETION_DST)
  354. opcode |= (1 << DMAE_CMD_COMP_FUNC_SHIFT);
  355. opcode |= (DMAE_CMD_ENDIANITY << DMAE_CMD_ENDIANITY_MODE_SHIFT);
  356. opcode |= ((p_hwfn->port_id) << DMAE_CMD_PORT_ID_SHIFT);
  357. /* reset source address in next go */
  358. opcode |= (DMAE_CMD_SRC_ADDR_RESET_MASK <<
  359. DMAE_CMD_SRC_ADDR_RESET_SHIFT);
  360. /* reset dest address in next go */
  361. opcode |= (DMAE_CMD_DST_ADDR_RESET_MASK <<
  362. DMAE_CMD_DST_ADDR_RESET_SHIFT);
  363. /* SRC/DST VFID: all 1's - pf, otherwise VF id */
  364. if (p_params->flags & QED_DMAE_FLAG_VF_SRC) {
  365. opcode |= 1 << DMAE_CMD_SRC_VF_ID_VALID_SHIFT;
  366. opcode_b |= p_params->src_vfid << DMAE_CMD_SRC_VF_ID_SHIFT;
  367. } else {
  368. opcode_b |= DMAE_CMD_SRC_VF_ID_MASK <<
  369. DMAE_CMD_SRC_VF_ID_SHIFT;
  370. }
  371. if (p_params->flags & QED_DMAE_FLAG_VF_DST) {
  372. opcode |= 1 << DMAE_CMD_DST_VF_ID_VALID_SHIFT;
  373. opcode_b |= p_params->dst_vfid << DMAE_CMD_DST_VF_ID_SHIFT;
  374. } else {
  375. opcode_b |= DMAE_CMD_DST_VF_ID_MASK << DMAE_CMD_DST_VF_ID_SHIFT;
  376. }
  377. p_hwfn->dmae_info.p_dmae_cmd->opcode = cpu_to_le32(opcode);
  378. p_hwfn->dmae_info.p_dmae_cmd->opcode_b = cpu_to_le16(opcode_b);
  379. }
  380. u32 qed_dmae_idx_to_go_cmd(u8 idx)
  381. {
  382. /* All the DMAE 'go' registers form an array in internal memory */
  383. return DMAE_REG_GO_C0 + (idx << 2);
  384. }
  385. static int qed_dmae_post_command(struct qed_hwfn *p_hwfn,
  386. struct qed_ptt *p_ptt)
  387. {
  388. struct dmae_cmd *p_command = p_hwfn->dmae_info.p_dmae_cmd;
  389. u8 idx_cmd = p_hwfn->dmae_info.channel, i;
  390. int qed_status = 0;
  391. /* verify address is not NULL */
  392. if ((((!p_command->dst_addr_lo) && (!p_command->dst_addr_hi)) ||
  393. ((!p_command->src_addr_lo) && (!p_command->src_addr_hi)))) {
  394. DP_NOTICE(p_hwfn,
  395. "source or destination address 0 idx_cmd=%d\n"
  396. "opcode = [0x%08x,0x%04x] len=0x%x src=0x%x:%x dst=0x%x:%x\n",
  397. idx_cmd,
  398. le32_to_cpu(p_command->opcode),
  399. le16_to_cpu(p_command->opcode_b),
  400. le16_to_cpu(p_command->length_dw),
  401. le32_to_cpu(p_command->src_addr_hi),
  402. le32_to_cpu(p_command->src_addr_lo),
  403. le32_to_cpu(p_command->dst_addr_hi),
  404. le32_to_cpu(p_command->dst_addr_lo));
  405. return -EINVAL;
  406. }
  407. DP_VERBOSE(p_hwfn,
  408. NETIF_MSG_HW,
  409. "Posting DMAE command [idx %d]: opcode = [0x%08x,0x%04x] len=0x%x src=0x%x:%x dst=0x%x:%x\n",
  410. idx_cmd,
  411. le32_to_cpu(p_command->opcode),
  412. le16_to_cpu(p_command->opcode_b),
  413. le16_to_cpu(p_command->length_dw),
  414. le32_to_cpu(p_command->src_addr_hi),
  415. le32_to_cpu(p_command->src_addr_lo),
  416. le32_to_cpu(p_command->dst_addr_hi),
  417. le32_to_cpu(p_command->dst_addr_lo));
  418. /* Copy the command to DMAE - need to do it before every call
  419. * for source/dest address no reset.
  420. * The first 9 DWs are the command registers, the 10 DW is the
  421. * GO register, and the rest are result registers
  422. * (which are read only by the client).
  423. */
  424. for (i = 0; i < DMAE_CMD_SIZE; i++) {
  425. u32 data = (i < DMAE_CMD_SIZE_TO_FILL) ?
  426. *(((u32 *)p_command) + i) : 0;
  427. qed_wr(p_hwfn, p_ptt,
  428. DMAE_REG_CMD_MEM +
  429. (idx_cmd * DMAE_CMD_SIZE * sizeof(u32)) +
  430. (i * sizeof(u32)), data);
  431. }
  432. qed_wr(p_hwfn, p_ptt, qed_dmae_idx_to_go_cmd(idx_cmd), DMAE_GO_VALUE);
  433. return qed_status;
  434. }
  435. int qed_dmae_info_alloc(struct qed_hwfn *p_hwfn)
  436. {
  437. dma_addr_t *p_addr = &p_hwfn->dmae_info.completion_word_phys_addr;
  438. struct dmae_cmd **p_cmd = &p_hwfn->dmae_info.p_dmae_cmd;
  439. u32 **p_buff = &p_hwfn->dmae_info.p_intermediate_buffer;
  440. u32 **p_comp = &p_hwfn->dmae_info.p_completion_word;
  441. *p_comp = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
  442. sizeof(u32), p_addr, GFP_KERNEL);
  443. if (!*p_comp)
  444. goto err;
  445. p_addr = &p_hwfn->dmae_info.dmae_cmd_phys_addr;
  446. *p_cmd = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
  447. sizeof(struct dmae_cmd),
  448. p_addr, GFP_KERNEL);
  449. if (!*p_cmd)
  450. goto err;
  451. p_addr = &p_hwfn->dmae_info.intermediate_buffer_phys_addr;
  452. *p_buff = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
  453. sizeof(u32) * DMAE_MAX_RW_SIZE,
  454. p_addr, GFP_KERNEL);
  455. if (!*p_buff)
  456. goto err;
  457. p_hwfn->dmae_info.channel = p_hwfn->rel_pf_id;
  458. return 0;
  459. err:
  460. qed_dmae_info_free(p_hwfn);
  461. return -ENOMEM;
  462. }
  463. void qed_dmae_info_free(struct qed_hwfn *p_hwfn)
  464. {
  465. dma_addr_t p_phys;
  466. /* Just make sure no one is in the middle */
  467. mutex_lock(&p_hwfn->dmae_info.mutex);
  468. if (p_hwfn->dmae_info.p_completion_word) {
  469. p_phys = p_hwfn->dmae_info.completion_word_phys_addr;
  470. dma_free_coherent(&p_hwfn->cdev->pdev->dev,
  471. sizeof(u32),
  472. p_hwfn->dmae_info.p_completion_word, p_phys);
  473. p_hwfn->dmae_info.p_completion_word = NULL;
  474. }
  475. if (p_hwfn->dmae_info.p_dmae_cmd) {
  476. p_phys = p_hwfn->dmae_info.dmae_cmd_phys_addr;
  477. dma_free_coherent(&p_hwfn->cdev->pdev->dev,
  478. sizeof(struct dmae_cmd),
  479. p_hwfn->dmae_info.p_dmae_cmd, p_phys);
  480. p_hwfn->dmae_info.p_dmae_cmd = NULL;
  481. }
  482. if (p_hwfn->dmae_info.p_intermediate_buffer) {
  483. p_phys = p_hwfn->dmae_info.intermediate_buffer_phys_addr;
  484. dma_free_coherent(&p_hwfn->cdev->pdev->dev,
  485. sizeof(u32) * DMAE_MAX_RW_SIZE,
  486. p_hwfn->dmae_info.p_intermediate_buffer,
  487. p_phys);
  488. p_hwfn->dmae_info.p_intermediate_buffer = NULL;
  489. }
  490. mutex_unlock(&p_hwfn->dmae_info.mutex);
  491. }
  492. static int qed_dmae_operation_wait(struct qed_hwfn *p_hwfn)
  493. {
  494. u32 wait_cnt_limit = 10000, wait_cnt = 0;
  495. int qed_status = 0;
  496. barrier();
  497. while (*p_hwfn->dmae_info.p_completion_word != DMAE_COMPLETION_VAL) {
  498. udelay(DMAE_MIN_WAIT_TIME);
  499. if (++wait_cnt > wait_cnt_limit) {
  500. DP_NOTICE(p_hwfn->cdev,
  501. "Timed-out waiting for operation to complete. Completion word is 0x%08x expected 0x%08x.\n",
  502. *p_hwfn->dmae_info.p_completion_word,
  503. DMAE_COMPLETION_VAL);
  504. qed_status = -EBUSY;
  505. break;
  506. }
  507. /* to sync the completion_word since we are not
  508. * using the volatile keyword for p_completion_word
  509. */
  510. barrier();
  511. }
  512. if (qed_status == 0)
  513. *p_hwfn->dmae_info.p_completion_word = 0;
  514. return qed_status;
  515. }
  516. static int qed_dmae_execute_sub_operation(struct qed_hwfn *p_hwfn,
  517. struct qed_ptt *p_ptt,
  518. u64 src_addr,
  519. u64 dst_addr,
  520. u8 src_type,
  521. u8 dst_type,
  522. u32 length_dw)
  523. {
  524. dma_addr_t phys = p_hwfn->dmae_info.intermediate_buffer_phys_addr;
  525. struct dmae_cmd *cmd = p_hwfn->dmae_info.p_dmae_cmd;
  526. int qed_status = 0;
  527. switch (src_type) {
  528. case QED_DMAE_ADDRESS_GRC:
  529. case QED_DMAE_ADDRESS_HOST_PHYS:
  530. cmd->src_addr_hi = cpu_to_le32(upper_32_bits(src_addr));
  531. cmd->src_addr_lo = cpu_to_le32(lower_32_bits(src_addr));
  532. break;
  533. /* for virtual source addresses we use the intermediate buffer. */
  534. case QED_DMAE_ADDRESS_HOST_VIRT:
  535. cmd->src_addr_hi = cpu_to_le32(upper_32_bits(phys));
  536. cmd->src_addr_lo = cpu_to_le32(lower_32_bits(phys));
  537. memcpy(&p_hwfn->dmae_info.p_intermediate_buffer[0],
  538. (void *)(uintptr_t)src_addr,
  539. length_dw * sizeof(u32));
  540. break;
  541. default:
  542. return -EINVAL;
  543. }
  544. switch (dst_type) {
  545. case QED_DMAE_ADDRESS_GRC:
  546. case QED_DMAE_ADDRESS_HOST_PHYS:
  547. cmd->dst_addr_hi = cpu_to_le32(upper_32_bits(dst_addr));
  548. cmd->dst_addr_lo = cpu_to_le32(lower_32_bits(dst_addr));
  549. break;
  550. /* for virtual source addresses we use the intermediate buffer. */
  551. case QED_DMAE_ADDRESS_HOST_VIRT:
  552. cmd->dst_addr_hi = cpu_to_le32(upper_32_bits(phys));
  553. cmd->dst_addr_lo = cpu_to_le32(lower_32_bits(phys));
  554. break;
  555. default:
  556. return -EINVAL;
  557. }
  558. cmd->length_dw = cpu_to_le16((u16)length_dw);
  559. qed_dmae_post_command(p_hwfn, p_ptt);
  560. qed_status = qed_dmae_operation_wait(p_hwfn);
  561. if (qed_status) {
  562. DP_NOTICE(p_hwfn,
  563. "qed_dmae_host2grc: Wait Failed. source_addr 0x%llx, grc_addr 0x%llx, size_in_dwords 0x%x\n",
  564. src_addr, dst_addr, length_dw);
  565. return qed_status;
  566. }
  567. if (dst_type == QED_DMAE_ADDRESS_HOST_VIRT)
  568. memcpy((void *)(uintptr_t)(dst_addr),
  569. &p_hwfn->dmae_info.p_intermediate_buffer[0],
  570. length_dw * sizeof(u32));
  571. return 0;
  572. }
  573. static int qed_dmae_execute_command(struct qed_hwfn *p_hwfn,
  574. struct qed_ptt *p_ptt,
  575. u64 src_addr, u64 dst_addr,
  576. u8 src_type, u8 dst_type,
  577. u32 size_in_dwords,
  578. struct qed_dmae_params *p_params)
  579. {
  580. dma_addr_t phys = p_hwfn->dmae_info.completion_word_phys_addr;
  581. u16 length_cur = 0, i = 0, cnt_split = 0, length_mod = 0;
  582. struct dmae_cmd *cmd = p_hwfn->dmae_info.p_dmae_cmd;
  583. u64 src_addr_split = 0, dst_addr_split = 0;
  584. u16 length_limit = DMAE_MAX_RW_SIZE;
  585. int qed_status = 0;
  586. u32 offset = 0;
  587. qed_dmae_opcode(p_hwfn,
  588. (src_type == QED_DMAE_ADDRESS_GRC),
  589. (dst_type == QED_DMAE_ADDRESS_GRC),
  590. p_params);
  591. cmd->comp_addr_lo = cpu_to_le32(lower_32_bits(phys));
  592. cmd->comp_addr_hi = cpu_to_le32(upper_32_bits(phys));
  593. cmd->comp_val = cpu_to_le32(DMAE_COMPLETION_VAL);
  594. /* Check if the grc_addr is valid like < MAX_GRC_OFFSET */
  595. cnt_split = size_in_dwords / length_limit;
  596. length_mod = size_in_dwords % length_limit;
  597. src_addr_split = src_addr;
  598. dst_addr_split = dst_addr;
  599. for (i = 0; i <= cnt_split; i++) {
  600. offset = length_limit * i;
  601. if (!(p_params->flags & QED_DMAE_FLAG_RW_REPL_SRC)) {
  602. if (src_type == QED_DMAE_ADDRESS_GRC)
  603. src_addr_split = src_addr + offset;
  604. else
  605. src_addr_split = src_addr + (offset * 4);
  606. }
  607. if (dst_type == QED_DMAE_ADDRESS_GRC)
  608. dst_addr_split = dst_addr + offset;
  609. else
  610. dst_addr_split = dst_addr + (offset * 4);
  611. length_cur = (cnt_split == i) ? length_mod : length_limit;
  612. /* might be zero on last iteration */
  613. if (!length_cur)
  614. continue;
  615. qed_status = qed_dmae_execute_sub_operation(p_hwfn,
  616. p_ptt,
  617. src_addr_split,
  618. dst_addr_split,
  619. src_type,
  620. dst_type,
  621. length_cur);
  622. if (qed_status) {
  623. DP_NOTICE(p_hwfn,
  624. "qed_dmae_execute_sub_operation Failed with error 0x%x. source_addr 0x%llx, destination addr 0x%llx, size_in_dwords 0x%x\n",
  625. qed_status, src_addr, dst_addr, length_cur);
  626. break;
  627. }
  628. }
  629. return qed_status;
  630. }
  631. int qed_dmae_host2grc(struct qed_hwfn *p_hwfn,
  632. struct qed_ptt *p_ptt,
  633. u64 source_addr, u32 grc_addr, u32 size_in_dwords, u32 flags)
  634. {
  635. u32 grc_addr_in_dw = grc_addr / sizeof(u32);
  636. struct qed_dmae_params params;
  637. int rc;
  638. memset(&params, 0, sizeof(struct qed_dmae_params));
  639. params.flags = flags;
  640. mutex_lock(&p_hwfn->dmae_info.mutex);
  641. rc = qed_dmae_execute_command(p_hwfn, p_ptt, source_addr,
  642. grc_addr_in_dw,
  643. QED_DMAE_ADDRESS_HOST_VIRT,
  644. QED_DMAE_ADDRESS_GRC,
  645. size_in_dwords, &params);
  646. mutex_unlock(&p_hwfn->dmae_info.mutex);
  647. return rc;
  648. }
  649. int qed_dmae_grc2host(struct qed_hwfn *p_hwfn,
  650. struct qed_ptt *p_ptt,
  651. u32 grc_addr,
  652. dma_addr_t dest_addr, u32 size_in_dwords, u32 flags)
  653. {
  654. u32 grc_addr_in_dw = grc_addr / sizeof(u32);
  655. struct qed_dmae_params params;
  656. int rc;
  657. memset(&params, 0, sizeof(struct qed_dmae_params));
  658. params.flags = flags;
  659. mutex_lock(&p_hwfn->dmae_info.mutex);
  660. rc = qed_dmae_execute_command(p_hwfn, p_ptt, grc_addr_in_dw,
  661. dest_addr, QED_DMAE_ADDRESS_GRC,
  662. QED_DMAE_ADDRESS_HOST_VIRT,
  663. size_in_dwords, &params);
  664. mutex_unlock(&p_hwfn->dmae_info.mutex);
  665. return rc;
  666. }
  667. int qed_dmae_host2host(struct qed_hwfn *p_hwfn,
  668. struct qed_ptt *p_ptt,
  669. dma_addr_t source_addr,
  670. dma_addr_t dest_addr,
  671. u32 size_in_dwords, struct qed_dmae_params *p_params)
  672. {
  673. int rc;
  674. mutex_lock(&(p_hwfn->dmae_info.mutex));
  675. rc = qed_dmae_execute_command(p_hwfn, p_ptt, source_addr,
  676. dest_addr,
  677. QED_DMAE_ADDRESS_HOST_PHYS,
  678. QED_DMAE_ADDRESS_HOST_PHYS,
  679. size_in_dwords, p_params);
  680. mutex_unlock(&(p_hwfn->dmae_info.mutex));
  681. return rc;
  682. }
  683. int qed_dmae_sanity(struct qed_hwfn *p_hwfn,
  684. struct qed_ptt *p_ptt, const char *phase)
  685. {
  686. u32 size = PAGE_SIZE / 2, val;
  687. struct qed_dmae_params params;
  688. int rc = 0;
  689. dma_addr_t p_phys;
  690. void *p_virt;
  691. u32 *p_tmp;
  692. p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
  693. 2 * size, &p_phys, GFP_KERNEL);
  694. if (!p_virt) {
  695. DP_NOTICE(p_hwfn,
  696. "DMAE sanity [%s]: failed to allocate memory\n",
  697. phase);
  698. return -ENOMEM;
  699. }
  700. /* Fill the bottom half of the allocated memory with a known pattern */
  701. for (p_tmp = (u32 *)p_virt;
  702. p_tmp < (u32 *)((u8 *)p_virt + size); p_tmp++) {
  703. /* Save the address itself as the value */
  704. val = (u32)(uintptr_t)p_tmp;
  705. *p_tmp = val;
  706. }
  707. /* Zero the top half of the allocated memory */
  708. memset((u8 *)p_virt + size, 0, size);
  709. DP_VERBOSE(p_hwfn,
  710. QED_MSG_SP,
  711. "DMAE sanity [%s]: src_addr={phys 0x%llx, virt %p}, dst_addr={phys 0x%llx, virt %p}, size 0x%x\n",
  712. phase,
  713. (u64)p_phys,
  714. p_virt, (u64)(p_phys + size), (u8 *)p_virt + size, size);
  715. memset(&params, 0, sizeof(params));
  716. rc = qed_dmae_host2host(p_hwfn, p_ptt, p_phys, p_phys + size,
  717. size / 4 /* size_in_dwords */, &params);
  718. if (rc) {
  719. DP_NOTICE(p_hwfn,
  720. "DMAE sanity [%s]: qed_dmae_host2host() failed. rc = %d.\n",
  721. phase, rc);
  722. goto out;
  723. }
  724. /* Verify that the top half of the allocated memory has the pattern */
  725. for (p_tmp = (u32 *)((u8 *)p_virt + size);
  726. p_tmp < (u32 *)((u8 *)p_virt + (2 * size)); p_tmp++) {
  727. /* The corresponding address in the bottom half */
  728. val = (u32)(uintptr_t)p_tmp - size;
  729. if (*p_tmp != val) {
  730. DP_NOTICE(p_hwfn,
  731. "DMAE sanity [%s]: addr={phys 0x%llx, virt %p}, read_val 0x%08x, expected_val 0x%08x\n",
  732. phase,
  733. (u64)p_phys + ((u8 *)p_tmp - (u8 *)p_virt),
  734. p_tmp, *p_tmp, val);
  735. rc = -EINVAL;
  736. goto out;
  737. }
  738. }
  739. out:
  740. dma_free_coherent(&p_hwfn->cdev->pdev->dev, 2 * size, p_virt, p_phys);
  741. return rc;
  742. }