qed_spq.c 22 KB

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