request_manager.c 22 KB

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