qed_spq.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. /* QLogic qed NIC Driver
  2. * Copyright (c) 2015 QLogic Corporation
  3. *
  4. * This software is available under the terms of the GNU General Public License
  5. * (GPL) Version 2, available from the file COPYING in the main directory of
  6. * this source tree.
  7. */
  8. #include <linux/types.h>
  9. #include <asm/byteorder.h>
  10. #include <linux/io.h>
  11. #include <linux/delay.h>
  12. #include <linux/dma-mapping.h>
  13. #include <linux/errno.h>
  14. #include <linux/kernel.h>
  15. #include <linux/list.h>
  16. #include <linux/pci.h>
  17. #include <linux/slab.h>
  18. #include <linux/spinlock.h>
  19. #include <linux/string.h>
  20. #include "qed.h"
  21. #include "qed_cxt.h"
  22. #include "qed_dev_api.h"
  23. #include "qed_hsi.h"
  24. #include "qed_hw.h"
  25. #include "qed_int.h"
  26. #include "qed_mcp.h"
  27. #include "qed_reg_addr.h"
  28. #include "qed_sp.h"
  29. #include "qed_sriov.h"
  30. /***************************************************************************
  31. * Structures & Definitions
  32. ***************************************************************************/
  33. #define SPQ_HIGH_PRI_RESERVE_DEFAULT (1)
  34. #define SPQ_BLOCK_SLEEP_LENGTH (1000)
  35. /***************************************************************************
  36. * Blocking Imp. (BLOCK/EBLOCK mode)
  37. ***************************************************************************/
  38. static void qed_spq_blocking_cb(struct qed_hwfn *p_hwfn,
  39. void *cookie,
  40. union event_ring_data *data,
  41. u8 fw_return_code)
  42. {
  43. struct qed_spq_comp_done *comp_done;
  44. comp_done = (struct qed_spq_comp_done *)cookie;
  45. comp_done->done = 0x1;
  46. comp_done->fw_return_code = fw_return_code;
  47. /* make update visible to waiting thread */
  48. smp_wmb();
  49. }
  50. static int qed_spq_block(struct qed_hwfn *p_hwfn,
  51. struct qed_spq_entry *p_ent,
  52. u8 *p_fw_ret)
  53. {
  54. int sleep_count = SPQ_BLOCK_SLEEP_LENGTH;
  55. struct qed_spq_comp_done *comp_done;
  56. int rc;
  57. comp_done = (struct qed_spq_comp_done *)p_ent->comp_cb.cookie;
  58. while (sleep_count) {
  59. /* validate we receive completion update */
  60. smp_rmb();
  61. if (comp_done->done == 1) {
  62. if (p_fw_ret)
  63. *p_fw_ret = comp_done->fw_return_code;
  64. return 0;
  65. }
  66. usleep_range(5000, 10000);
  67. sleep_count--;
  68. }
  69. DP_INFO(p_hwfn, "Ramrod is stuck, requesting MCP drain\n");
  70. rc = qed_mcp_drain(p_hwfn, p_hwfn->p_main_ptt);
  71. if (rc != 0)
  72. DP_NOTICE(p_hwfn, "MCP drain failed\n");
  73. /* Retry after drain */
  74. sleep_count = SPQ_BLOCK_SLEEP_LENGTH;
  75. while (sleep_count) {
  76. /* validate we receive completion update */
  77. smp_rmb();
  78. if (comp_done->done == 1) {
  79. if (p_fw_ret)
  80. *p_fw_ret = comp_done->fw_return_code;
  81. return 0;
  82. }
  83. usleep_range(5000, 10000);
  84. sleep_count--;
  85. }
  86. if (comp_done->done == 1) {
  87. if (p_fw_ret)
  88. *p_fw_ret = comp_done->fw_return_code;
  89. return 0;
  90. }
  91. DP_NOTICE(p_hwfn, "Ramrod is stuck, MCP drain failed\n");
  92. return -EBUSY;
  93. }
  94. /***************************************************************************
  95. * SPQ entries inner API
  96. ***************************************************************************/
  97. static int
  98. qed_spq_fill_entry(struct qed_hwfn *p_hwfn,
  99. struct qed_spq_entry *p_ent)
  100. {
  101. p_ent->flags = 0;
  102. switch (p_ent->comp_mode) {
  103. case QED_SPQ_MODE_EBLOCK:
  104. case QED_SPQ_MODE_BLOCK:
  105. p_ent->comp_cb.function = qed_spq_blocking_cb;
  106. break;
  107. case QED_SPQ_MODE_CB:
  108. break;
  109. default:
  110. DP_NOTICE(p_hwfn, "Unknown SPQE completion mode %d\n",
  111. p_ent->comp_mode);
  112. return -EINVAL;
  113. }
  114. DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
  115. "Ramrod header: [CID 0x%08x CMD 0x%02x protocol 0x%02x] Data pointer: [%08x:%08x] Completion Mode: %s\n",
  116. p_ent->elem.hdr.cid,
  117. p_ent->elem.hdr.cmd_id,
  118. p_ent->elem.hdr.protocol_id,
  119. p_ent->elem.data_ptr.hi,
  120. p_ent->elem.data_ptr.lo,
  121. D_TRINE(p_ent->comp_mode, QED_SPQ_MODE_EBLOCK,
  122. QED_SPQ_MODE_BLOCK, "MODE_EBLOCK", "MODE_BLOCK",
  123. "MODE_CB"));
  124. return 0;
  125. }
  126. /***************************************************************************
  127. * HSI access
  128. ***************************************************************************/
  129. static void qed_spq_hw_initialize(struct qed_hwfn *p_hwfn,
  130. struct qed_spq *p_spq)
  131. {
  132. u16 pq;
  133. struct qed_cxt_info cxt_info;
  134. struct core_conn_context *p_cxt;
  135. union qed_qm_pq_params pq_params;
  136. int rc;
  137. cxt_info.iid = p_spq->cid;
  138. rc = qed_cxt_get_cid_info(p_hwfn, &cxt_info);
  139. if (rc < 0) {
  140. DP_NOTICE(p_hwfn, "Cannot find context info for cid=%d\n",
  141. p_spq->cid);
  142. return;
  143. }
  144. p_cxt = cxt_info.p_cxt;
  145. SET_FIELD(p_cxt->xstorm_ag_context.flags10,
  146. XSTORM_CORE_CONN_AG_CTX_DQ_CF_EN, 1);
  147. SET_FIELD(p_cxt->xstorm_ag_context.flags1,
  148. XSTORM_CORE_CONN_AG_CTX_DQ_CF_ACTIVE, 1);
  149. SET_FIELD(p_cxt->xstorm_ag_context.flags9,
  150. XSTORM_CORE_CONN_AG_CTX_CONSOLID_PROD_CF_EN, 1);
  151. /* QM physical queue */
  152. memset(&pq_params, 0, sizeof(pq_params));
  153. pq_params.core.tc = LB_TC;
  154. pq = qed_get_qm_pq(p_hwfn, PROTOCOLID_CORE, &pq_params);
  155. p_cxt->xstorm_ag_context.physical_q0 = cpu_to_le16(pq);
  156. p_cxt->xstorm_st_context.spq_base_lo =
  157. DMA_LO_LE(p_spq->chain.p_phys_addr);
  158. p_cxt->xstorm_st_context.spq_base_hi =
  159. DMA_HI_LE(p_spq->chain.p_phys_addr);
  160. DMA_REGPAIR_LE(p_cxt->xstorm_st_context.consolid_base_addr,
  161. p_hwfn->p_consq->chain.p_phys_addr);
  162. }
  163. static int qed_spq_hw_post(struct qed_hwfn *p_hwfn,
  164. struct qed_spq *p_spq,
  165. struct qed_spq_entry *p_ent)
  166. {
  167. struct qed_chain *p_chain = &p_hwfn->p_spq->chain;
  168. u16 echo = qed_chain_get_prod_idx(p_chain);
  169. struct slow_path_element *elem;
  170. struct core_db_data db;
  171. p_ent->elem.hdr.echo = cpu_to_le16(echo);
  172. elem = qed_chain_produce(p_chain);
  173. if (!elem) {
  174. DP_NOTICE(p_hwfn, "Failed to produce from SPQ chain\n");
  175. return -EINVAL;
  176. }
  177. *elem = p_ent->elem; /* struct assignment */
  178. /* send a doorbell on the slow hwfn session */
  179. memset(&db, 0, sizeof(db));
  180. SET_FIELD(db.params, CORE_DB_DATA_DEST, DB_DEST_XCM);
  181. SET_FIELD(db.params, CORE_DB_DATA_AGG_CMD, DB_AGG_CMD_SET);
  182. SET_FIELD(db.params, CORE_DB_DATA_AGG_VAL_SEL,
  183. DQ_XCM_CORE_SPQ_PROD_CMD);
  184. db.agg_flags = DQ_XCM_CORE_DQ_CF_CMD;
  185. db.spq_prod = cpu_to_le16(qed_chain_get_prod_idx(p_chain));
  186. /* make sure the SPQE is updated before the doorbell */
  187. wmb();
  188. DOORBELL(p_hwfn, qed_db_addr(p_spq->cid, DQ_DEMS_LEGACY), *(u32 *)&db);
  189. /* make sure doorbell is rang */
  190. wmb();
  191. DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
  192. "Doorbelled [0x%08x, CID 0x%08x] with Flags: %02x agg_params: %02x, prod: %04x\n",
  193. qed_db_addr(p_spq->cid, DQ_DEMS_LEGACY),
  194. p_spq->cid, db.params, db.agg_flags,
  195. qed_chain_get_prod_idx(p_chain));
  196. return 0;
  197. }
  198. /***************************************************************************
  199. * Asynchronous events
  200. ***************************************************************************/
  201. static int
  202. qed_async_event_completion(struct qed_hwfn *p_hwfn,
  203. struct event_ring_entry *p_eqe)
  204. {
  205. switch (p_eqe->protocol_id) {
  206. case PROTOCOLID_COMMON:
  207. return qed_sriov_eqe_event(p_hwfn,
  208. p_eqe->opcode,
  209. p_eqe->echo, &p_eqe->data);
  210. default:
  211. DP_NOTICE(p_hwfn,
  212. "Unknown Async completion for protocol: %d\n",
  213. p_eqe->protocol_id);
  214. return -EINVAL;
  215. }
  216. }
  217. /***************************************************************************
  218. * EQ API
  219. ***************************************************************************/
  220. void qed_eq_prod_update(struct qed_hwfn *p_hwfn,
  221. u16 prod)
  222. {
  223. u32 addr = GTT_BAR0_MAP_REG_USDM_RAM +
  224. USTORM_EQE_CONS_OFFSET(p_hwfn->rel_pf_id);
  225. REG_WR16(p_hwfn, addr, prod);
  226. /* keep prod updates ordered */
  227. mmiowb();
  228. }
  229. int qed_eq_completion(struct qed_hwfn *p_hwfn,
  230. void *cookie)
  231. {
  232. struct qed_eq *p_eq = cookie;
  233. struct qed_chain *p_chain = &p_eq->chain;
  234. int rc = 0;
  235. /* take a snapshot of the FW consumer */
  236. u16 fw_cons_idx = le16_to_cpu(*p_eq->p_fw_cons);
  237. DP_VERBOSE(p_hwfn, QED_MSG_SPQ, "fw_cons_idx %x\n", fw_cons_idx);
  238. /* Need to guarantee the fw_cons index we use points to a usuable
  239. * element (to comply with our chain), so our macros would comply
  240. */
  241. if ((fw_cons_idx & qed_chain_get_usable_per_page(p_chain)) ==
  242. qed_chain_get_usable_per_page(p_chain))
  243. fw_cons_idx += qed_chain_get_unusable_per_page(p_chain);
  244. /* Complete current segment of eq entries */
  245. while (fw_cons_idx != qed_chain_get_cons_idx(p_chain)) {
  246. struct event_ring_entry *p_eqe = qed_chain_consume(p_chain);
  247. if (!p_eqe) {
  248. rc = -EINVAL;
  249. break;
  250. }
  251. DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
  252. "op %x prot %x res0 %x echo %x fwret %x flags %x\n",
  253. p_eqe->opcode,
  254. p_eqe->protocol_id,
  255. p_eqe->reserved0,
  256. le16_to_cpu(p_eqe->echo),
  257. p_eqe->fw_return_code,
  258. p_eqe->flags);
  259. if (GET_FIELD(p_eqe->flags, EVENT_RING_ENTRY_ASYNC)) {
  260. if (qed_async_event_completion(p_hwfn, p_eqe))
  261. rc = -EINVAL;
  262. } else if (qed_spq_completion(p_hwfn,
  263. p_eqe->echo,
  264. p_eqe->fw_return_code,
  265. &p_eqe->data)) {
  266. rc = -EINVAL;
  267. }
  268. qed_chain_recycle_consumed(p_chain);
  269. }
  270. qed_eq_prod_update(p_hwfn, qed_chain_get_prod_idx(p_chain));
  271. return rc;
  272. }
  273. struct qed_eq *qed_eq_alloc(struct qed_hwfn *p_hwfn,
  274. u16 num_elem)
  275. {
  276. struct qed_eq *p_eq;
  277. /* Allocate EQ struct */
  278. p_eq = kzalloc(sizeof(*p_eq), GFP_KERNEL);
  279. if (!p_eq) {
  280. DP_NOTICE(p_hwfn, "Failed to allocate `struct qed_eq'\n");
  281. return NULL;
  282. }
  283. /* Allocate and initialize EQ chain*/
  284. if (qed_chain_alloc(p_hwfn->cdev,
  285. QED_CHAIN_USE_TO_PRODUCE,
  286. QED_CHAIN_MODE_PBL,
  287. QED_CHAIN_CNT_TYPE_U16,
  288. num_elem,
  289. sizeof(union event_ring_element),
  290. &p_eq->chain)) {
  291. DP_NOTICE(p_hwfn, "Failed to allocate eq chain\n");
  292. goto eq_allocate_fail;
  293. }
  294. /* register EQ completion on the SP SB */
  295. qed_int_register_cb(p_hwfn,
  296. qed_eq_completion,
  297. p_eq,
  298. &p_eq->eq_sb_index,
  299. &p_eq->p_fw_cons);
  300. return p_eq;
  301. eq_allocate_fail:
  302. qed_eq_free(p_hwfn, p_eq);
  303. return NULL;
  304. }
  305. void qed_eq_setup(struct qed_hwfn *p_hwfn,
  306. struct qed_eq *p_eq)
  307. {
  308. qed_chain_reset(&p_eq->chain);
  309. }
  310. void qed_eq_free(struct qed_hwfn *p_hwfn,
  311. struct qed_eq *p_eq)
  312. {
  313. if (!p_eq)
  314. return;
  315. qed_chain_free(p_hwfn->cdev, &p_eq->chain);
  316. kfree(p_eq);
  317. }
  318. /***************************************************************************
  319. * CQE API - manipulate EQ functionality
  320. ***************************************************************************/
  321. static int qed_cqe_completion(
  322. struct qed_hwfn *p_hwfn,
  323. struct eth_slow_path_rx_cqe *cqe,
  324. enum protocol_type protocol)
  325. {
  326. if (IS_VF(p_hwfn->cdev))
  327. return 0;
  328. /* @@@tmp - it's possible we'll eventually want to handle some
  329. * actual commands that can arrive here, but for now this is only
  330. * used to complete the ramrod using the echo value on the cqe
  331. */
  332. return qed_spq_completion(p_hwfn, cqe->echo, 0, NULL);
  333. }
  334. int qed_eth_cqe_completion(struct qed_hwfn *p_hwfn,
  335. struct eth_slow_path_rx_cqe *cqe)
  336. {
  337. int rc;
  338. rc = qed_cqe_completion(p_hwfn, cqe, PROTOCOLID_ETH);
  339. if (rc)
  340. DP_NOTICE(p_hwfn,
  341. "Failed to handle RXQ CQE [cmd 0x%02x]\n",
  342. cqe->ramrod_cmd_id);
  343. return rc;
  344. }
  345. /***************************************************************************
  346. * Slow hwfn Queue (spq)
  347. ***************************************************************************/
  348. void qed_spq_setup(struct qed_hwfn *p_hwfn)
  349. {
  350. struct qed_spq *p_spq = p_hwfn->p_spq;
  351. struct qed_spq_entry *p_virt = NULL;
  352. dma_addr_t p_phys = 0;
  353. u32 i, capacity;
  354. INIT_LIST_HEAD(&p_spq->pending);
  355. INIT_LIST_HEAD(&p_spq->completion_pending);
  356. INIT_LIST_HEAD(&p_spq->free_pool);
  357. INIT_LIST_HEAD(&p_spq->unlimited_pending);
  358. spin_lock_init(&p_spq->lock);
  359. /* SPQ empty pool */
  360. p_phys = p_spq->p_phys + offsetof(struct qed_spq_entry, ramrod);
  361. p_virt = p_spq->p_virt;
  362. capacity = qed_chain_get_capacity(&p_spq->chain);
  363. for (i = 0; i < capacity; i++) {
  364. DMA_REGPAIR_LE(p_virt->elem.data_ptr, p_phys);
  365. list_add_tail(&p_virt->list, &p_spq->free_pool);
  366. p_virt++;
  367. p_phys += sizeof(struct qed_spq_entry);
  368. }
  369. /* Statistics */
  370. p_spq->normal_count = 0;
  371. p_spq->comp_count = 0;
  372. p_spq->comp_sent_count = 0;
  373. p_spq->unlimited_pending_count = 0;
  374. bitmap_zero(p_spq->p_comp_bitmap, SPQ_RING_SIZE);
  375. p_spq->comp_bitmap_idx = 0;
  376. /* SPQ cid, cannot fail */
  377. qed_cxt_acquire_cid(p_hwfn, PROTOCOLID_CORE, &p_spq->cid);
  378. qed_spq_hw_initialize(p_hwfn, p_spq);
  379. /* reset the chain itself */
  380. qed_chain_reset(&p_spq->chain);
  381. }
  382. int qed_spq_alloc(struct qed_hwfn *p_hwfn)
  383. {
  384. struct qed_spq_entry *p_virt = NULL;
  385. struct qed_spq *p_spq = NULL;
  386. dma_addr_t p_phys = 0;
  387. u32 capacity;
  388. /* SPQ struct */
  389. p_spq =
  390. kzalloc(sizeof(struct qed_spq), GFP_KERNEL);
  391. if (!p_spq) {
  392. DP_NOTICE(p_hwfn, "Failed to allocate `struct qed_spq'\n");
  393. return -ENOMEM;
  394. }
  395. /* SPQ ring */
  396. if (qed_chain_alloc(p_hwfn->cdev,
  397. QED_CHAIN_USE_TO_PRODUCE,
  398. QED_CHAIN_MODE_SINGLE,
  399. QED_CHAIN_CNT_TYPE_U16,
  400. 0, /* N/A when the mode is SINGLE */
  401. sizeof(struct slow_path_element),
  402. &p_spq->chain)) {
  403. DP_NOTICE(p_hwfn, "Failed to allocate spq chain\n");
  404. goto spq_allocate_fail;
  405. }
  406. /* allocate and fill the SPQ elements (incl. ramrod data list) */
  407. capacity = qed_chain_get_capacity(&p_spq->chain);
  408. p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
  409. capacity *
  410. sizeof(struct qed_spq_entry),
  411. &p_phys, GFP_KERNEL);
  412. if (!p_virt)
  413. goto spq_allocate_fail;
  414. p_spq->p_virt = p_virt;
  415. p_spq->p_phys = p_phys;
  416. p_hwfn->p_spq = p_spq;
  417. return 0;
  418. spq_allocate_fail:
  419. qed_chain_free(p_hwfn->cdev, &p_spq->chain);
  420. kfree(p_spq);
  421. return -ENOMEM;
  422. }
  423. void qed_spq_free(struct qed_hwfn *p_hwfn)
  424. {
  425. struct qed_spq *p_spq = p_hwfn->p_spq;
  426. u32 capacity;
  427. if (!p_spq)
  428. return;
  429. if (p_spq->p_virt) {
  430. capacity = qed_chain_get_capacity(&p_spq->chain);
  431. dma_free_coherent(&p_hwfn->cdev->pdev->dev,
  432. capacity *
  433. sizeof(struct qed_spq_entry),
  434. p_spq->p_virt, p_spq->p_phys);
  435. }
  436. qed_chain_free(p_hwfn->cdev, &p_spq->chain);
  437. ;
  438. kfree(p_spq);
  439. }
  440. int
  441. qed_spq_get_entry(struct qed_hwfn *p_hwfn,
  442. struct qed_spq_entry **pp_ent)
  443. {
  444. struct qed_spq *p_spq = p_hwfn->p_spq;
  445. struct qed_spq_entry *p_ent = NULL;
  446. int rc = 0;
  447. spin_lock_bh(&p_spq->lock);
  448. if (list_empty(&p_spq->free_pool)) {
  449. p_ent = kzalloc(sizeof(*p_ent), GFP_ATOMIC);
  450. if (!p_ent) {
  451. rc = -ENOMEM;
  452. goto out_unlock;
  453. }
  454. p_ent->queue = &p_spq->unlimited_pending;
  455. } else {
  456. p_ent = list_first_entry(&p_spq->free_pool,
  457. struct qed_spq_entry,
  458. list);
  459. list_del(&p_ent->list);
  460. p_ent->queue = &p_spq->pending;
  461. }
  462. *pp_ent = p_ent;
  463. out_unlock:
  464. spin_unlock_bh(&p_spq->lock);
  465. return rc;
  466. }
  467. /* Locked variant; Should be called while the SPQ lock is taken */
  468. static void __qed_spq_return_entry(struct qed_hwfn *p_hwfn,
  469. struct qed_spq_entry *p_ent)
  470. {
  471. list_add_tail(&p_ent->list, &p_hwfn->p_spq->free_pool);
  472. }
  473. void qed_spq_return_entry(struct qed_hwfn *p_hwfn,
  474. struct qed_spq_entry *p_ent)
  475. {
  476. spin_lock_bh(&p_hwfn->p_spq->lock);
  477. __qed_spq_return_entry(p_hwfn, p_ent);
  478. spin_unlock_bh(&p_hwfn->p_spq->lock);
  479. }
  480. /**
  481. * @brief qed_spq_add_entry - adds a new entry to the pending
  482. * list. Should be used while lock is being held.
  483. *
  484. * Addes an entry to the pending list is there is room (en empty
  485. * element is available in the free_pool), or else places the
  486. * entry in the unlimited_pending pool.
  487. *
  488. * @param p_hwfn
  489. * @param p_ent
  490. * @param priority
  491. *
  492. * @return int
  493. */
  494. static int
  495. qed_spq_add_entry(struct qed_hwfn *p_hwfn,
  496. struct qed_spq_entry *p_ent,
  497. enum spq_priority priority)
  498. {
  499. struct qed_spq *p_spq = p_hwfn->p_spq;
  500. if (p_ent->queue == &p_spq->unlimited_pending) {
  501. if (list_empty(&p_spq->free_pool)) {
  502. list_add_tail(&p_ent->list, &p_spq->unlimited_pending);
  503. p_spq->unlimited_pending_count++;
  504. return 0;
  505. } else {
  506. struct qed_spq_entry *p_en2;
  507. p_en2 = list_first_entry(&p_spq->free_pool,
  508. struct qed_spq_entry,
  509. list);
  510. list_del(&p_en2->list);
  511. /* Copy the ring element physical pointer to the new
  512. * entry, since we are about to override the entire ring
  513. * entry and don't want to lose the pointer.
  514. */
  515. p_ent->elem.data_ptr = p_en2->elem.data_ptr;
  516. *p_en2 = *p_ent;
  517. /* EBLOCK responsible to free the allocated p_ent */
  518. if (p_ent->comp_mode != QED_SPQ_MODE_EBLOCK)
  519. kfree(p_ent);
  520. p_ent = p_en2;
  521. }
  522. }
  523. /* entry is to be placed in 'pending' queue */
  524. switch (priority) {
  525. case QED_SPQ_PRIORITY_NORMAL:
  526. list_add_tail(&p_ent->list, &p_spq->pending);
  527. p_spq->normal_count++;
  528. break;
  529. case QED_SPQ_PRIORITY_HIGH:
  530. list_add(&p_ent->list, &p_spq->pending);
  531. p_spq->high_count++;
  532. break;
  533. default:
  534. return -EINVAL;
  535. }
  536. return 0;
  537. }
  538. /***************************************************************************
  539. * Accessor
  540. ***************************************************************************/
  541. u32 qed_spq_get_cid(struct qed_hwfn *p_hwfn)
  542. {
  543. if (!p_hwfn->p_spq)
  544. return 0xffffffff; /* illegal */
  545. return p_hwfn->p_spq->cid;
  546. }
  547. /***************************************************************************
  548. * Posting new Ramrods
  549. ***************************************************************************/
  550. static int qed_spq_post_list(struct qed_hwfn *p_hwfn,
  551. struct list_head *head,
  552. u32 keep_reserve)
  553. {
  554. struct qed_spq *p_spq = p_hwfn->p_spq;
  555. int rc;
  556. while (qed_chain_get_elem_left(&p_spq->chain) > keep_reserve &&
  557. !list_empty(head)) {
  558. struct qed_spq_entry *p_ent =
  559. list_first_entry(head, struct qed_spq_entry, list);
  560. list_del(&p_ent->list);
  561. list_add_tail(&p_ent->list, &p_spq->completion_pending);
  562. p_spq->comp_sent_count++;
  563. rc = qed_spq_hw_post(p_hwfn, p_spq, p_ent);
  564. if (rc) {
  565. list_del(&p_ent->list);
  566. __qed_spq_return_entry(p_hwfn, p_ent);
  567. return rc;
  568. }
  569. }
  570. return 0;
  571. }
  572. static int qed_spq_pend_post(struct qed_hwfn *p_hwfn)
  573. {
  574. struct qed_spq *p_spq = p_hwfn->p_spq;
  575. struct qed_spq_entry *p_ent = NULL;
  576. while (!list_empty(&p_spq->free_pool)) {
  577. if (list_empty(&p_spq->unlimited_pending))
  578. break;
  579. p_ent = list_first_entry(&p_spq->unlimited_pending,
  580. struct qed_spq_entry,
  581. list);
  582. if (!p_ent)
  583. return -EINVAL;
  584. list_del(&p_ent->list);
  585. qed_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
  586. }
  587. return qed_spq_post_list(p_hwfn, &p_spq->pending,
  588. SPQ_HIGH_PRI_RESERVE_DEFAULT);
  589. }
  590. int qed_spq_post(struct qed_hwfn *p_hwfn,
  591. struct qed_spq_entry *p_ent,
  592. u8 *fw_return_code)
  593. {
  594. int rc = 0;
  595. struct qed_spq *p_spq = p_hwfn ? p_hwfn->p_spq : NULL;
  596. bool b_ret_ent = true;
  597. if (!p_hwfn)
  598. return -EINVAL;
  599. if (!p_ent) {
  600. DP_NOTICE(p_hwfn, "Got a NULL pointer\n");
  601. return -EINVAL;
  602. }
  603. /* Complete the entry */
  604. rc = qed_spq_fill_entry(p_hwfn, p_ent);
  605. spin_lock_bh(&p_spq->lock);
  606. /* Check return value after LOCK is taken for cleaner error flow */
  607. if (rc)
  608. goto spq_post_fail;
  609. /* Add the request to the pending queue */
  610. rc = qed_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
  611. if (rc)
  612. goto spq_post_fail;
  613. rc = qed_spq_pend_post(p_hwfn);
  614. if (rc) {
  615. /* Since it's possible that pending failed for a different
  616. * entry [although unlikely], the failed entry was already
  617. * dealt with; No need to return it here.
  618. */
  619. b_ret_ent = false;
  620. goto spq_post_fail;
  621. }
  622. spin_unlock_bh(&p_spq->lock);
  623. if (p_ent->comp_mode == QED_SPQ_MODE_EBLOCK) {
  624. /* For entries in QED BLOCK mode, the completion code cannot
  625. * perform the necessary cleanup - if it did, we couldn't
  626. * access p_ent here to see whether it's successful or not.
  627. * Thus, after gaining the answer perform the cleanup here.
  628. */
  629. rc = qed_spq_block(p_hwfn, p_ent, fw_return_code);
  630. if (p_ent->queue == &p_spq->unlimited_pending) {
  631. /* This is an allocated p_ent which does not need to
  632. * return to pool.
  633. */
  634. kfree(p_ent);
  635. return rc;
  636. }
  637. if (rc)
  638. goto spq_post_fail2;
  639. /* return to pool */
  640. qed_spq_return_entry(p_hwfn, p_ent);
  641. }
  642. return rc;
  643. spq_post_fail2:
  644. spin_lock_bh(&p_spq->lock);
  645. list_del(&p_ent->list);
  646. qed_chain_return_produced(&p_spq->chain);
  647. spq_post_fail:
  648. /* return to the free pool */
  649. if (b_ret_ent)
  650. __qed_spq_return_entry(p_hwfn, p_ent);
  651. spin_unlock_bh(&p_spq->lock);
  652. return rc;
  653. }
  654. int qed_spq_completion(struct qed_hwfn *p_hwfn,
  655. __le16 echo,
  656. u8 fw_return_code,
  657. union event_ring_data *p_data)
  658. {
  659. struct qed_spq *p_spq;
  660. struct qed_spq_entry *p_ent = NULL;
  661. struct qed_spq_entry *tmp;
  662. struct qed_spq_entry *found = NULL;
  663. int rc;
  664. if (!p_hwfn)
  665. return -EINVAL;
  666. p_spq = p_hwfn->p_spq;
  667. if (!p_spq)
  668. return -EINVAL;
  669. spin_lock_bh(&p_spq->lock);
  670. list_for_each_entry_safe(p_ent, tmp, &p_spq->completion_pending,
  671. list) {
  672. if (p_ent->elem.hdr.echo == echo) {
  673. u16 pos = le16_to_cpu(echo) % SPQ_RING_SIZE;
  674. list_del(&p_ent->list);
  675. /* Avoid overriding of SPQ entries when getting
  676. * out-of-order completions, by marking the completions
  677. * in a bitmap and increasing the chain consumer only
  678. * for the first successive completed entries.
  679. */
  680. __set_bit(pos, p_spq->p_comp_bitmap);
  681. while (test_bit(p_spq->comp_bitmap_idx,
  682. p_spq->p_comp_bitmap)) {
  683. __clear_bit(p_spq->comp_bitmap_idx,
  684. p_spq->p_comp_bitmap);
  685. p_spq->comp_bitmap_idx++;
  686. qed_chain_return_produced(&p_spq->chain);
  687. }
  688. p_spq->comp_count++;
  689. found = p_ent;
  690. break;
  691. }
  692. /* This is relatively uncommon - depends on scenarios
  693. * which have mutliple per-PF sent ramrods.
  694. */
  695. DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
  696. "Got completion for echo %04x - doesn't match echo %04x in completion pending list\n",
  697. le16_to_cpu(echo),
  698. le16_to_cpu(p_ent->elem.hdr.echo));
  699. }
  700. /* Release lock before callback, as callback may post
  701. * an additional ramrod.
  702. */
  703. spin_unlock_bh(&p_spq->lock);
  704. if (!found) {
  705. DP_NOTICE(p_hwfn,
  706. "Failed to find an entry this EQE completes\n");
  707. return -EEXIST;
  708. }
  709. DP_VERBOSE(p_hwfn, QED_MSG_SPQ, "Complete: func %p cookie %p)\n",
  710. p_ent->comp_cb.function, p_ent->comp_cb.cookie);
  711. if (found->comp_cb.function)
  712. found->comp_cb.function(p_hwfn, found->comp_cb.cookie, p_data,
  713. fw_return_code);
  714. if ((found->comp_mode != QED_SPQ_MODE_EBLOCK) ||
  715. (found->queue == &p_spq->unlimited_pending))
  716. /* EBLOCK is responsible for returning its own entry into the
  717. * free list, unless it originally added the entry into the
  718. * unlimited pending list.
  719. */
  720. qed_spq_return_entry(p_hwfn, found);
  721. /* Attempt to post pending requests */
  722. spin_lock_bh(&p_spq->lock);
  723. rc = qed_spq_pend_post(p_hwfn);
  724. spin_unlock_bh(&p_spq->lock);
  725. return rc;
  726. }
  727. struct qed_consq *qed_consq_alloc(struct qed_hwfn *p_hwfn)
  728. {
  729. struct qed_consq *p_consq;
  730. /* Allocate ConsQ struct */
  731. p_consq = kzalloc(sizeof(*p_consq), GFP_KERNEL);
  732. if (!p_consq) {
  733. DP_NOTICE(p_hwfn, "Failed to allocate `struct qed_consq'\n");
  734. return NULL;
  735. }
  736. /* Allocate and initialize EQ chain*/
  737. if (qed_chain_alloc(p_hwfn->cdev,
  738. QED_CHAIN_USE_TO_PRODUCE,
  739. QED_CHAIN_MODE_PBL,
  740. QED_CHAIN_CNT_TYPE_U16,
  741. QED_CHAIN_PAGE_SIZE / 0x80,
  742. 0x80, &p_consq->chain)) {
  743. DP_NOTICE(p_hwfn, "Failed to allocate consq chain");
  744. goto consq_allocate_fail;
  745. }
  746. return p_consq;
  747. consq_allocate_fail:
  748. qed_consq_free(p_hwfn, p_consq);
  749. return NULL;
  750. }
  751. void qed_consq_setup(struct qed_hwfn *p_hwfn,
  752. struct qed_consq *p_consq)
  753. {
  754. qed_chain_reset(&p_consq->chain);
  755. }
  756. void qed_consq_free(struct qed_hwfn *p_hwfn,
  757. struct qed_consq *p_consq)
  758. {
  759. if (!p_consq)
  760. return;
  761. qed_chain_free(p_hwfn->cdev, &p_consq->chain);
  762. kfree(p_consq);
  763. }