qed_spq.c 24 KB

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