qed_spq.c 22 KB

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