request_manager.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /**********************************************************************
  2. * Author: Cavium, Inc.
  3. *
  4. * Contact: support@cavium.com
  5. * Please include "LiquidIO" in the subject.
  6. *
  7. * Copyright (c) 2003-2016 Cavium, Inc.
  8. *
  9. * This file is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, Version 2, as
  11. * published by the Free Software Foundation.
  12. *
  13. * This file is distributed in the hope that it will be useful, but
  14. * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
  16. * NONINFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. **********************************************************************/
  19. #include <linux/pci.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/vmalloc.h>
  22. #include "liquidio_common.h"
  23. #include "octeon_droq.h"
  24. #include "octeon_iq.h"
  25. #include "response_manager.h"
  26. #include "octeon_device.h"
  27. #include "octeon_main.h"
  28. #include "octeon_network.h"
  29. #include "cn66xx_device.h"
  30. #include "cn23xx_pf_device.h"
  31. #include "cn23xx_vf_device.h"
  32. struct iq_post_status {
  33. int status;
  34. int index;
  35. };
  36. static void check_db_timeout(struct work_struct *work);
  37. static void __check_db_timeout(struct octeon_device *oct, u64 iq_no);
  38. static void (*reqtype_free_fn[MAX_OCTEON_DEVICES][REQTYPE_LAST + 1]) (void *);
  39. /* Define this to return the request status comaptible to old code */
  40. /*#define OCTEON_USE_OLD_REQ_STATUS*/
  41. /* Return 0 on success, 1 on failure */
  42. int octeon_init_instr_queue(struct octeon_device *oct,
  43. union oct_txpciq txpciq,
  44. u32 num_descs)
  45. {
  46. struct octeon_instr_queue *iq;
  47. struct octeon_iq_config *conf = NULL;
  48. u32 iq_no = (u32)txpciq.s.q_no;
  49. u32 q_size;
  50. struct cavium_wq *db_wq;
  51. int numa_node = dev_to_node(&oct->pci_dev->dev);
  52. if (OCTEON_CN6XXX(oct))
  53. conf = &(CFG_GET_IQ_CFG(CHIP_CONF(oct, cn6xxx)));
  54. else if (OCTEON_CN23XX_PF(oct))
  55. conf = &(CFG_GET_IQ_CFG(CHIP_CONF(oct, cn23xx_pf)));
  56. else if (OCTEON_CN23XX_VF(oct))
  57. conf = &(CFG_GET_IQ_CFG(CHIP_CONF(oct, cn23xx_vf)));
  58. if (!conf) {
  59. dev_err(&oct->pci_dev->dev, "Unsupported Chip %x\n",
  60. oct->chip_id);
  61. return 1;
  62. }
  63. q_size = (u32)conf->instr_type * num_descs;
  64. iq = oct->instr_queue[iq_no];
  65. iq->oct_dev = oct;
  66. iq->base_addr = lio_dma_alloc(oct, q_size, &iq->base_addr_dma);
  67. if (!iq->base_addr) {
  68. dev_err(&oct->pci_dev->dev, "Cannot allocate memory for instr queue %d\n",
  69. iq_no);
  70. return 1;
  71. }
  72. iq->max_count = num_descs;
  73. /* Initialize a list to holds requests that have been posted to Octeon
  74. * but has yet to be fetched by octeon
  75. */
  76. iq->request_list = vzalloc_node(array_size(num_descs, sizeof(*iq->request_list)),
  77. numa_node);
  78. if (!iq->request_list)
  79. iq->request_list = vzalloc(array_size(num_descs, sizeof(*iq->request_list)));
  80. if (!iq->request_list) {
  81. lio_dma_free(oct, q_size, iq->base_addr, iq->base_addr_dma);
  82. dev_err(&oct->pci_dev->dev, "Alloc failed for IQ[%d] nr free list\n",
  83. iq_no);
  84. return 1;
  85. }
  86. dev_dbg(&oct->pci_dev->dev, "IQ[%d]: base: %p basedma: %pad count: %d\n",
  87. iq_no, iq->base_addr, &iq->base_addr_dma, iq->max_count);
  88. iq->txpciq.u64 = txpciq.u64;
  89. iq->fill_threshold = (u32)conf->db_min;
  90. iq->fill_cnt = 0;
  91. iq->host_write_index = 0;
  92. iq->octeon_read_index = 0;
  93. iq->flush_index = 0;
  94. iq->last_db_time = 0;
  95. iq->do_auto_flush = 1;
  96. iq->db_timeout = (u32)conf->db_timeout;
  97. atomic_set(&iq->instr_pending, 0);
  98. iq->pkts_processed = 0;
  99. /* Initialize the spinlock for this instruction queue */
  100. spin_lock_init(&iq->lock);
  101. if (iq_no == 0) {
  102. iq->allow_soft_cmds = true;
  103. spin_lock_init(&iq->post_lock);
  104. } else {
  105. iq->allow_soft_cmds = false;
  106. }
  107. spin_lock_init(&iq->iq_flush_running_lock);
  108. oct->io_qmask.iq |= BIT_ULL(iq_no);
  109. /* Set the 32B/64B mode for each input queue */
  110. oct->io_qmask.iq64B |= ((u64)(conf->instr_type == 64) << iq_no);
  111. iq->iqcmd_64B = (conf->instr_type == 64);
  112. oct->fn_list.setup_iq_regs(oct, iq_no);
  113. oct->check_db_wq[iq_no].wq = alloc_workqueue("check_iq_db",
  114. WQ_MEM_RECLAIM,
  115. 0);
  116. if (!oct->check_db_wq[iq_no].wq) {
  117. vfree(iq->request_list);
  118. iq->request_list = NULL;
  119. lio_dma_free(oct, q_size, iq->base_addr, iq->base_addr_dma);
  120. dev_err(&oct->pci_dev->dev, "check db wq create failed for iq %d\n",
  121. iq_no);
  122. return 1;
  123. }
  124. db_wq = &oct->check_db_wq[iq_no];
  125. INIT_DELAYED_WORK(&db_wq->wk.work, check_db_timeout);
  126. db_wq->wk.ctxptr = oct;
  127. db_wq->wk.ctxul = iq_no;
  128. queue_delayed_work(db_wq->wq, &db_wq->wk.work, msecs_to_jiffies(1));
  129. return 0;
  130. }
  131. int octeon_delete_instr_queue(struct octeon_device *oct, u32 iq_no)
  132. {
  133. u64 desc_size = 0, q_size;
  134. struct octeon_instr_queue *iq = oct->instr_queue[iq_no];
  135. cancel_delayed_work_sync(&oct->check_db_wq[iq_no].wk.work);
  136. destroy_workqueue(oct->check_db_wq[iq_no].wq);
  137. if (OCTEON_CN6XXX(oct))
  138. desc_size =
  139. CFG_GET_IQ_INSTR_TYPE(CHIP_CONF(oct, cn6xxx));
  140. else if (OCTEON_CN23XX_PF(oct))
  141. desc_size =
  142. CFG_GET_IQ_INSTR_TYPE(CHIP_CONF(oct, cn23xx_pf));
  143. else if (OCTEON_CN23XX_VF(oct))
  144. desc_size =
  145. CFG_GET_IQ_INSTR_TYPE(CHIP_CONF(oct, cn23xx_vf));
  146. vfree(iq->request_list);
  147. if (iq->base_addr) {
  148. q_size = iq->max_count * desc_size;
  149. lio_dma_free(oct, (u32)q_size, iq->base_addr,
  150. iq->base_addr_dma);
  151. oct->io_qmask.iq &= ~(1ULL << iq_no);
  152. vfree(oct->instr_queue[iq_no]);
  153. oct->instr_queue[iq_no] = NULL;
  154. oct->num_iqs--;
  155. return 0;
  156. }
  157. return 1;
  158. }
  159. EXPORT_SYMBOL_GPL(octeon_delete_instr_queue);
  160. /* Return 0 on success, 1 on failure */
  161. int octeon_setup_iq(struct octeon_device *oct,
  162. int ifidx,
  163. int q_index,
  164. union oct_txpciq txpciq,
  165. u32 num_descs,
  166. void *app_ctx)
  167. {
  168. u32 iq_no = (u32)txpciq.s.q_no;
  169. int numa_node = dev_to_node(&oct->pci_dev->dev);
  170. if (oct->instr_queue[iq_no]) {
  171. dev_dbg(&oct->pci_dev->dev, "IQ is in use. Cannot create the IQ: %d again\n",
  172. iq_no);
  173. oct->instr_queue[iq_no]->txpciq.u64 = txpciq.u64;
  174. oct->instr_queue[iq_no]->app_ctx = app_ctx;
  175. return 0;
  176. }
  177. oct->instr_queue[iq_no] =
  178. vzalloc_node(sizeof(struct octeon_instr_queue), numa_node);
  179. if (!oct->instr_queue[iq_no])
  180. oct->instr_queue[iq_no] =
  181. vzalloc(sizeof(struct octeon_instr_queue));
  182. if (!oct->instr_queue[iq_no])
  183. return 1;
  184. oct->instr_queue[iq_no]->q_index = q_index;
  185. oct->instr_queue[iq_no]->app_ctx = app_ctx;
  186. oct->instr_queue[iq_no]->ifidx = ifidx;
  187. if (octeon_init_instr_queue(oct, txpciq, num_descs)) {
  188. vfree(oct->instr_queue[iq_no]);
  189. oct->instr_queue[iq_no] = NULL;
  190. return 1;
  191. }
  192. oct->num_iqs++;
  193. if (oct->fn_list.enable_io_queues(oct)) {
  194. octeon_delete_instr_queue(oct, iq_no);
  195. return 1;
  196. }
  197. return 0;
  198. }
  199. int lio_wait_for_instr_fetch(struct octeon_device *oct)
  200. {
  201. int i, retry = 1000, pending, instr_cnt = 0;
  202. do {
  203. instr_cnt = 0;
  204. for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
  205. if (!(oct->io_qmask.iq & BIT_ULL(i)))
  206. continue;
  207. pending =
  208. atomic_read(&oct->instr_queue[i]->instr_pending);
  209. if (pending)
  210. __check_db_timeout(oct, i);
  211. instr_cnt += pending;
  212. }
  213. if (instr_cnt == 0)
  214. break;
  215. schedule_timeout_uninterruptible(1);
  216. } while (retry-- && instr_cnt);
  217. return instr_cnt;
  218. }
  219. EXPORT_SYMBOL_GPL(lio_wait_for_instr_fetch);
  220. static inline void
  221. ring_doorbell(struct octeon_device *oct, struct octeon_instr_queue *iq)
  222. {
  223. if (atomic_read(&oct->status) == OCT_DEV_RUNNING) {
  224. writel(iq->fill_cnt, iq->doorbell_reg);
  225. /* make sure doorbell write goes through */
  226. iq->fill_cnt = 0;
  227. iq->last_db_time = jiffies;
  228. return;
  229. }
  230. }
  231. void
  232. octeon_ring_doorbell_locked(struct octeon_device *oct, u32 iq_no)
  233. {
  234. struct octeon_instr_queue *iq;
  235. iq = oct->instr_queue[iq_no];
  236. spin_lock(&iq->post_lock);
  237. if (iq->fill_cnt)
  238. ring_doorbell(oct, iq);
  239. spin_unlock(&iq->post_lock);
  240. }
  241. EXPORT_SYMBOL_GPL(octeon_ring_doorbell_locked);
  242. static inline void __copy_cmd_into_iq(struct octeon_instr_queue *iq,
  243. u8 *cmd)
  244. {
  245. u8 *iqptr, cmdsize;
  246. cmdsize = ((iq->iqcmd_64B) ? 64 : 32);
  247. iqptr = iq->base_addr + (cmdsize * iq->host_write_index);
  248. memcpy(iqptr, cmd, cmdsize);
  249. }
  250. static inline struct iq_post_status
  251. __post_command2(struct octeon_instr_queue *iq, u8 *cmd)
  252. {
  253. struct iq_post_status st;
  254. st.status = IQ_SEND_OK;
  255. /* This ensures that the read index does not wrap around to the same
  256. * position if queue gets full before Octeon could fetch any instr.
  257. */
  258. if (atomic_read(&iq->instr_pending) >= (s32)(iq->max_count - 1)) {
  259. st.status = IQ_SEND_FAILED;
  260. st.index = -1;
  261. return st;
  262. }
  263. if (atomic_read(&iq->instr_pending) >= (s32)(iq->max_count - 2))
  264. st.status = IQ_SEND_STOP;
  265. __copy_cmd_into_iq(iq, cmd);
  266. /* "index" is returned, host_write_index is modified. */
  267. st.index = iq->host_write_index;
  268. iq->host_write_index = incr_index(iq->host_write_index, 1,
  269. iq->max_count);
  270. iq->fill_cnt++;
  271. /* Flush the command into memory. We need to be sure the data is in
  272. * memory before indicating that the instruction is pending.
  273. */
  274. wmb();
  275. atomic_inc(&iq->instr_pending);
  276. return st;
  277. }
  278. int
  279. octeon_register_reqtype_free_fn(struct octeon_device *oct, int reqtype,
  280. void (*fn)(void *))
  281. {
  282. if (reqtype > REQTYPE_LAST) {
  283. dev_err(&oct->pci_dev->dev, "%s: Invalid reqtype: %d\n",
  284. __func__, reqtype);
  285. return -EINVAL;
  286. }
  287. reqtype_free_fn[oct->octeon_id][reqtype] = fn;
  288. return 0;
  289. }
  290. EXPORT_SYMBOL_GPL(octeon_register_reqtype_free_fn);
  291. static inline void
  292. __add_to_request_list(struct octeon_instr_queue *iq,
  293. int idx, void *buf, int reqtype)
  294. {
  295. iq->request_list[idx].buf = buf;
  296. iq->request_list[idx].reqtype = reqtype;
  297. }
  298. /* Can only run in process context */
  299. int
  300. lio_process_iq_request_list(struct octeon_device *oct,
  301. struct octeon_instr_queue *iq, u32 napi_budget)
  302. {
  303. struct cavium_wq *cwq = &oct->dma_comp_wq;
  304. int reqtype;
  305. void *buf;
  306. u32 old = iq->flush_index;
  307. u32 inst_count = 0;
  308. unsigned int pkts_compl = 0, bytes_compl = 0;
  309. struct octeon_soft_command *sc;
  310. unsigned long flags;
  311. while (old != iq->octeon_read_index) {
  312. reqtype = iq->request_list[old].reqtype;
  313. buf = iq->request_list[old].buf;
  314. if (reqtype == REQTYPE_NONE)
  315. goto skip_this;
  316. octeon_update_tx_completion_counters(buf, reqtype, &pkts_compl,
  317. &bytes_compl);
  318. switch (reqtype) {
  319. case REQTYPE_NORESP_NET:
  320. case REQTYPE_NORESP_NET_SG:
  321. case REQTYPE_RESP_NET_SG:
  322. reqtype_free_fn[oct->octeon_id][reqtype](buf);
  323. break;
  324. case REQTYPE_RESP_NET:
  325. case REQTYPE_SOFT_COMMAND:
  326. sc = buf;
  327. /* We're expecting a response from Octeon.
  328. * It's up to lio_process_ordered_list() to
  329. * process sc. Add sc to the ordered soft
  330. * command response list because we expect
  331. * a response from Octeon.
  332. */
  333. spin_lock_irqsave(&oct->response_list
  334. [OCTEON_ORDERED_SC_LIST].lock, flags);
  335. atomic_inc(&oct->response_list
  336. [OCTEON_ORDERED_SC_LIST].pending_req_count);
  337. list_add_tail(&sc->node, &oct->response_list
  338. [OCTEON_ORDERED_SC_LIST].head);
  339. spin_unlock_irqrestore(&oct->response_list
  340. [OCTEON_ORDERED_SC_LIST].lock,
  341. flags);
  342. break;
  343. default:
  344. dev_err(&oct->pci_dev->dev,
  345. "%s Unknown reqtype: %d buf: %p at idx %d\n",
  346. __func__, reqtype, buf, old);
  347. }
  348. iq->request_list[old].buf = NULL;
  349. iq->request_list[old].reqtype = 0;
  350. skip_this:
  351. inst_count++;
  352. old = incr_index(old, 1, iq->max_count);
  353. if ((napi_budget) && (inst_count >= napi_budget))
  354. break;
  355. }
  356. if (bytes_compl)
  357. octeon_report_tx_completion_to_bql(iq->app_ctx, pkts_compl,
  358. bytes_compl);
  359. iq->flush_index = old;
  360. if (atomic_read(&oct->response_list
  361. [OCTEON_ORDERED_SC_LIST].pending_req_count))
  362. queue_work(cwq->wq, &cwq->wk.work.work);
  363. return inst_count;
  364. }
  365. EXPORT_SYMBOL_GPL(lio_process_iq_request_list);
  366. /* Can only be called from process context */
  367. int
  368. octeon_flush_iq(struct octeon_device *oct, struct octeon_instr_queue *iq,
  369. u32 napi_budget)
  370. {
  371. u32 inst_processed = 0;
  372. u32 tot_inst_processed = 0;
  373. int tx_done = 1;
  374. if (!spin_trylock(&iq->iq_flush_running_lock))
  375. return tx_done;
  376. spin_lock_bh(&iq->lock);
  377. iq->octeon_read_index = oct->fn_list.update_iq_read_idx(iq);
  378. do {
  379. /* Process any outstanding IQ packets. */
  380. if (iq->flush_index == iq->octeon_read_index)
  381. break;
  382. if (napi_budget)
  383. inst_processed =
  384. lio_process_iq_request_list(oct, iq,
  385. napi_budget -
  386. tot_inst_processed);
  387. else
  388. inst_processed =
  389. lio_process_iq_request_list(oct, iq, 0);
  390. if (inst_processed) {
  391. iq->pkts_processed += inst_processed;
  392. atomic_sub(inst_processed, &iq->instr_pending);
  393. iq->stats.instr_processed += inst_processed;
  394. }
  395. tot_inst_processed += inst_processed;
  396. } while (tot_inst_processed < napi_budget);
  397. if (napi_budget && (tot_inst_processed >= napi_budget))
  398. tx_done = 0;
  399. iq->last_db_time = jiffies;
  400. spin_unlock_bh(&iq->lock);
  401. spin_unlock(&iq->iq_flush_running_lock);
  402. return tx_done;
  403. }
  404. /* Process instruction queue after timeout.
  405. * This routine gets called from a workqueue or when removing the module.
  406. */
  407. static void __check_db_timeout(struct octeon_device *oct, u64 iq_no)
  408. {
  409. struct octeon_instr_queue *iq;
  410. u64 next_time;
  411. if (!oct)
  412. return;
  413. iq = oct->instr_queue[iq_no];
  414. if (!iq)
  415. return;
  416. /* return immediately, if no work pending */
  417. if (!atomic_read(&iq->instr_pending))
  418. return;
  419. /* If jiffies - last_db_time < db_timeout do nothing */
  420. next_time = iq->last_db_time + iq->db_timeout;
  421. if (!time_after(jiffies, (unsigned long)next_time))
  422. return;
  423. iq->last_db_time = jiffies;
  424. /* Flush the instruction queue */
  425. octeon_flush_iq(oct, iq, 0);
  426. lio_enable_irq(NULL, iq);
  427. }
  428. /* Called by the Poll thread at regular intervals to check the instruction
  429. * queue for commands to be posted and for commands that were fetched by Octeon.
  430. */
  431. static void check_db_timeout(struct work_struct *work)
  432. {
  433. struct cavium_wk *wk = (struct cavium_wk *)work;
  434. struct octeon_device *oct = (struct octeon_device *)wk->ctxptr;
  435. u64 iq_no = wk->ctxul;
  436. struct cavium_wq *db_wq = &oct->check_db_wq[iq_no];
  437. u32 delay = 10;
  438. __check_db_timeout(oct, iq_no);
  439. queue_delayed_work(db_wq->wq, &db_wq->wk.work, msecs_to_jiffies(delay));
  440. }
  441. int
  442. octeon_send_command(struct octeon_device *oct, u32 iq_no,
  443. u32 force_db, void *cmd, void *buf,
  444. u32 datasize, u32 reqtype)
  445. {
  446. int xmit_stopped;
  447. struct iq_post_status st;
  448. struct octeon_instr_queue *iq = oct->instr_queue[iq_no];
  449. /* Get the lock and prevent other tasks and tx interrupt handler from
  450. * running.
  451. */
  452. if (iq->allow_soft_cmds)
  453. spin_lock_bh(&iq->post_lock);
  454. st = __post_command2(iq, cmd);
  455. if (st.status != IQ_SEND_FAILED) {
  456. xmit_stopped = octeon_report_sent_bytes_to_bql(buf, reqtype);
  457. __add_to_request_list(iq, st.index, buf, reqtype);
  458. INCR_INSTRQUEUE_PKT_COUNT(oct, iq_no, bytes_sent, datasize);
  459. INCR_INSTRQUEUE_PKT_COUNT(oct, iq_no, instr_posted, 1);
  460. if (iq->fill_cnt >= MAX_OCTEON_FILL_COUNT || force_db ||
  461. xmit_stopped || st.status == IQ_SEND_STOP)
  462. ring_doorbell(oct, iq);
  463. } else {
  464. INCR_INSTRQUEUE_PKT_COUNT(oct, iq_no, instr_dropped, 1);
  465. }
  466. if (iq->allow_soft_cmds)
  467. spin_unlock_bh(&iq->post_lock);
  468. /* This is only done here to expedite packets being flushed
  469. * for cases where there are no IQ completion interrupts.
  470. */
  471. return st.status;
  472. }
  473. EXPORT_SYMBOL_GPL(octeon_send_command);
  474. void
  475. octeon_prepare_soft_command(struct octeon_device *oct,
  476. struct octeon_soft_command *sc,
  477. u8 opcode,
  478. u8 subcode,
  479. u32 irh_ossp,
  480. u64 ossp0,
  481. u64 ossp1)
  482. {
  483. struct octeon_config *oct_cfg;
  484. struct octeon_instr_ih2 *ih2;
  485. struct octeon_instr_ih3 *ih3;
  486. struct octeon_instr_pki_ih3 *pki_ih3;
  487. struct octeon_instr_irh *irh;
  488. struct octeon_instr_rdp *rdp;
  489. WARN_ON(opcode > 15);
  490. WARN_ON(subcode > 127);
  491. oct_cfg = octeon_get_conf(oct);
  492. if (OCTEON_CN23XX_PF(oct) || OCTEON_CN23XX_VF(oct)) {
  493. ih3 = (struct octeon_instr_ih3 *)&sc->cmd.cmd3.ih3;
  494. ih3->pkind = oct->instr_queue[sc->iq_no]->txpciq.s.pkind;
  495. pki_ih3 = (struct octeon_instr_pki_ih3 *)&sc->cmd.cmd3.pki_ih3;
  496. pki_ih3->w = 1;
  497. pki_ih3->raw = 1;
  498. pki_ih3->utag = 1;
  499. pki_ih3->uqpg =
  500. oct->instr_queue[sc->iq_no]->txpciq.s.use_qpg;
  501. pki_ih3->utt = 1;
  502. pki_ih3->tag = LIO_CONTROL;
  503. pki_ih3->tagtype = ATOMIC_TAG;
  504. pki_ih3->qpg =
  505. oct->instr_queue[sc->iq_no]->txpciq.s.ctrl_qpg;
  506. pki_ih3->pm = 0x7;
  507. pki_ih3->sl = 8;
  508. if (sc->datasize)
  509. ih3->dlengsz = sc->datasize;
  510. irh = (struct octeon_instr_irh *)&sc->cmd.cmd3.irh;
  511. irh->opcode = opcode;
  512. irh->subcode = subcode;
  513. /* opcode/subcode specific parameters (ossp) */
  514. irh->ossp = irh_ossp;
  515. sc->cmd.cmd3.ossp[0] = ossp0;
  516. sc->cmd.cmd3.ossp[1] = ossp1;
  517. if (sc->rdatasize) {
  518. rdp = (struct octeon_instr_rdp *)&sc->cmd.cmd3.rdp;
  519. rdp->pcie_port = oct->pcie_port;
  520. rdp->rlen = sc->rdatasize;
  521. irh->rflag = 1;
  522. /*PKI IH3*/
  523. /* pki_ih3 irh+ossp[0]+ossp[1]+rdp+rptr = 48 bytes */
  524. ih3->fsz = LIO_SOFTCMDRESP_IH3;
  525. } else {
  526. irh->rflag = 0;
  527. /*PKI IH3*/
  528. /* pki_h3 + irh + ossp[0] + ossp[1] = 32 bytes */
  529. ih3->fsz = LIO_PCICMD_O3;
  530. }
  531. } else {
  532. ih2 = (struct octeon_instr_ih2 *)&sc->cmd.cmd2.ih2;
  533. ih2->tagtype = ATOMIC_TAG;
  534. ih2->tag = LIO_CONTROL;
  535. ih2->raw = 1;
  536. ih2->grp = CFG_GET_CTRL_Q_GRP(oct_cfg);
  537. if (sc->datasize) {
  538. ih2->dlengsz = sc->datasize;
  539. ih2->rs = 1;
  540. }
  541. irh = (struct octeon_instr_irh *)&sc->cmd.cmd2.irh;
  542. irh->opcode = opcode;
  543. irh->subcode = subcode;
  544. /* opcode/subcode specific parameters (ossp) */
  545. irh->ossp = irh_ossp;
  546. sc->cmd.cmd2.ossp[0] = ossp0;
  547. sc->cmd.cmd2.ossp[1] = ossp1;
  548. if (sc->rdatasize) {
  549. rdp = (struct octeon_instr_rdp *)&sc->cmd.cmd2.rdp;
  550. rdp->pcie_port = oct->pcie_port;
  551. rdp->rlen = sc->rdatasize;
  552. irh->rflag = 1;
  553. /* irh+ossp[0]+ossp[1]+rdp+rptr = 40 bytes */
  554. ih2->fsz = LIO_SOFTCMDRESP_IH2;
  555. } else {
  556. irh->rflag = 0;
  557. /* irh + ossp[0] + ossp[1] = 24 bytes */
  558. ih2->fsz = LIO_PCICMD_O2;
  559. }
  560. }
  561. }
  562. EXPORT_SYMBOL_GPL(octeon_prepare_soft_command);
  563. int octeon_send_soft_command(struct octeon_device *oct,
  564. struct octeon_soft_command *sc)
  565. {
  566. struct octeon_instr_queue *iq;
  567. struct octeon_instr_ih2 *ih2;
  568. struct octeon_instr_ih3 *ih3;
  569. struct octeon_instr_irh *irh;
  570. u32 len;
  571. iq = oct->instr_queue[sc->iq_no];
  572. if (!iq->allow_soft_cmds) {
  573. dev_err(&oct->pci_dev->dev, "Soft commands are not allowed on Queue %d\n",
  574. sc->iq_no);
  575. INCR_INSTRQUEUE_PKT_COUNT(oct, sc->iq_no, instr_dropped, 1);
  576. return IQ_SEND_FAILED;
  577. }
  578. if (OCTEON_CN23XX_PF(oct) || OCTEON_CN23XX_VF(oct)) {
  579. ih3 = (struct octeon_instr_ih3 *)&sc->cmd.cmd3.ih3;
  580. if (ih3->dlengsz) {
  581. WARN_ON(!sc->dmadptr);
  582. sc->cmd.cmd3.dptr = sc->dmadptr;
  583. }
  584. irh = (struct octeon_instr_irh *)&sc->cmd.cmd3.irh;
  585. if (irh->rflag) {
  586. WARN_ON(!sc->dmarptr);
  587. WARN_ON(!sc->status_word);
  588. *sc->status_word = COMPLETION_WORD_INIT;
  589. sc->cmd.cmd3.rptr = sc->dmarptr;
  590. }
  591. len = (u32)ih3->dlengsz;
  592. } else {
  593. ih2 = (struct octeon_instr_ih2 *)&sc->cmd.cmd2.ih2;
  594. if (ih2->dlengsz) {
  595. WARN_ON(!sc->dmadptr);
  596. sc->cmd.cmd2.dptr = sc->dmadptr;
  597. }
  598. irh = (struct octeon_instr_irh *)&sc->cmd.cmd2.irh;
  599. if (irh->rflag) {
  600. WARN_ON(!sc->dmarptr);
  601. WARN_ON(!sc->status_word);
  602. *sc->status_word = COMPLETION_WORD_INIT;
  603. sc->cmd.cmd2.rptr = sc->dmarptr;
  604. }
  605. len = (u32)ih2->dlengsz;
  606. }
  607. sc->expiry_time = jiffies + msecs_to_jiffies(LIO_SC_MAX_TMO_MS);
  608. return (octeon_send_command(oct, sc->iq_no, 1, &sc->cmd, sc,
  609. len, REQTYPE_SOFT_COMMAND));
  610. }
  611. EXPORT_SYMBOL_GPL(octeon_send_soft_command);
  612. int octeon_setup_sc_buffer_pool(struct octeon_device *oct)
  613. {
  614. int i;
  615. u64 dma_addr;
  616. struct octeon_soft_command *sc;
  617. INIT_LIST_HEAD(&oct->sc_buf_pool.head);
  618. spin_lock_init(&oct->sc_buf_pool.lock);
  619. atomic_set(&oct->sc_buf_pool.alloc_buf_count, 0);
  620. for (i = 0; i < MAX_SOFT_COMMAND_BUFFERS; i++) {
  621. sc = (struct octeon_soft_command *)
  622. lio_dma_alloc(oct,
  623. SOFT_COMMAND_BUFFER_SIZE,
  624. (dma_addr_t *)&dma_addr);
  625. if (!sc) {
  626. octeon_free_sc_buffer_pool(oct);
  627. return 1;
  628. }
  629. sc->dma_addr = dma_addr;
  630. sc->size = SOFT_COMMAND_BUFFER_SIZE;
  631. list_add_tail(&sc->node, &oct->sc_buf_pool.head);
  632. }
  633. return 0;
  634. }
  635. EXPORT_SYMBOL_GPL(octeon_setup_sc_buffer_pool);
  636. int octeon_free_sc_done_list(struct octeon_device *oct)
  637. {
  638. struct octeon_response_list *done_sc_list, *zombie_sc_list;
  639. struct octeon_soft_command *sc;
  640. struct list_head *tmp, *tmp2;
  641. spinlock_t *sc_lists_lock; /* lock for response_list */
  642. done_sc_list = &oct->response_list[OCTEON_DONE_SC_LIST];
  643. zombie_sc_list = &oct->response_list[OCTEON_ZOMBIE_SC_LIST];
  644. if (!atomic_read(&done_sc_list->pending_req_count))
  645. return 0;
  646. sc_lists_lock = &oct->response_list[OCTEON_ORDERED_SC_LIST].lock;
  647. spin_lock_bh(sc_lists_lock);
  648. list_for_each_safe(tmp, tmp2, &done_sc_list->head) {
  649. sc = list_entry(tmp, struct octeon_soft_command, node);
  650. if (READ_ONCE(sc->caller_is_done)) {
  651. list_del(&sc->node);
  652. atomic_dec(&done_sc_list->pending_req_count);
  653. if (*sc->status_word == COMPLETION_WORD_INIT) {
  654. /* timeout; move sc to zombie list */
  655. list_add_tail(&sc->node, &zombie_sc_list->head);
  656. atomic_inc(&zombie_sc_list->pending_req_count);
  657. } else {
  658. octeon_free_soft_command(oct, sc);
  659. }
  660. }
  661. }
  662. spin_unlock_bh(sc_lists_lock);
  663. return 0;
  664. }
  665. EXPORT_SYMBOL_GPL(octeon_free_sc_done_list);
  666. int octeon_free_sc_zombie_list(struct octeon_device *oct)
  667. {
  668. struct octeon_response_list *zombie_sc_list;
  669. struct octeon_soft_command *sc;
  670. struct list_head *tmp, *tmp2;
  671. spinlock_t *sc_lists_lock; /* lock for response_list */
  672. zombie_sc_list = &oct->response_list[OCTEON_ZOMBIE_SC_LIST];
  673. sc_lists_lock = &oct->response_list[OCTEON_ORDERED_SC_LIST].lock;
  674. spin_lock_bh(sc_lists_lock);
  675. list_for_each_safe(tmp, tmp2, &zombie_sc_list->head) {
  676. list_del(tmp);
  677. atomic_dec(&zombie_sc_list->pending_req_count);
  678. sc = list_entry(tmp, struct octeon_soft_command, node);
  679. octeon_free_soft_command(oct, sc);
  680. }
  681. spin_unlock_bh(sc_lists_lock);
  682. return 0;
  683. }
  684. EXPORT_SYMBOL_GPL(octeon_free_sc_zombie_list);
  685. int octeon_free_sc_buffer_pool(struct octeon_device *oct)
  686. {
  687. struct list_head *tmp, *tmp2;
  688. struct octeon_soft_command *sc;
  689. octeon_free_sc_zombie_list(oct);
  690. spin_lock_bh(&oct->sc_buf_pool.lock);
  691. list_for_each_safe(tmp, tmp2, &oct->sc_buf_pool.head) {
  692. list_del(tmp);
  693. sc = (struct octeon_soft_command *)tmp;
  694. lio_dma_free(oct, sc->size, sc, sc->dma_addr);
  695. }
  696. INIT_LIST_HEAD(&oct->sc_buf_pool.head);
  697. spin_unlock_bh(&oct->sc_buf_pool.lock);
  698. return 0;
  699. }
  700. EXPORT_SYMBOL_GPL(octeon_free_sc_buffer_pool);
  701. struct octeon_soft_command *octeon_alloc_soft_command(struct octeon_device *oct,
  702. u32 datasize,
  703. u32 rdatasize,
  704. u32 ctxsize)
  705. {
  706. u64 dma_addr;
  707. u32 size;
  708. u32 offset = sizeof(struct octeon_soft_command);
  709. struct octeon_soft_command *sc = NULL;
  710. struct list_head *tmp;
  711. if (!rdatasize)
  712. rdatasize = 16;
  713. WARN_ON((offset + datasize + rdatasize + ctxsize) >
  714. SOFT_COMMAND_BUFFER_SIZE);
  715. spin_lock_bh(&oct->sc_buf_pool.lock);
  716. if (list_empty(&oct->sc_buf_pool.head)) {
  717. spin_unlock_bh(&oct->sc_buf_pool.lock);
  718. return NULL;
  719. }
  720. list_for_each(tmp, &oct->sc_buf_pool.head)
  721. break;
  722. list_del(tmp);
  723. atomic_inc(&oct->sc_buf_pool.alloc_buf_count);
  724. spin_unlock_bh(&oct->sc_buf_pool.lock);
  725. sc = (struct octeon_soft_command *)tmp;
  726. dma_addr = sc->dma_addr;
  727. size = sc->size;
  728. memset(sc, 0, sc->size);
  729. sc->dma_addr = dma_addr;
  730. sc->size = size;
  731. if (ctxsize) {
  732. sc->ctxptr = (u8 *)sc + offset;
  733. sc->ctxsize = ctxsize;
  734. }
  735. /* Start data at 128 byte boundary */
  736. offset = (offset + ctxsize + 127) & 0xffffff80;
  737. if (datasize) {
  738. sc->virtdptr = (u8 *)sc + offset;
  739. sc->dmadptr = dma_addr + offset;
  740. sc->datasize = datasize;
  741. }
  742. /* Start rdata at 128 byte boundary */
  743. offset = (offset + datasize + 127) & 0xffffff80;
  744. if (rdatasize) {
  745. WARN_ON(rdatasize < 16);
  746. sc->virtrptr = (u8 *)sc + offset;
  747. sc->dmarptr = dma_addr + offset;
  748. sc->rdatasize = rdatasize;
  749. sc->status_word = (u64 *)((u8 *)(sc->virtrptr) + rdatasize - 8);
  750. }
  751. return sc;
  752. }
  753. EXPORT_SYMBOL_GPL(octeon_alloc_soft_command);
  754. void octeon_free_soft_command(struct octeon_device *oct,
  755. struct octeon_soft_command *sc)
  756. {
  757. spin_lock_bh(&oct->sc_buf_pool.lock);
  758. list_add_tail(&sc->node, &oct->sc_buf_pool.head);
  759. atomic_dec(&oct->sc_buf_pool.alloc_buf_count);
  760. spin_unlock_bh(&oct->sc_buf_pool.lock);
  761. }
  762. EXPORT_SYMBOL_GPL(octeon_free_soft_command);