qed_spq.c 22 KB

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