request_manager.c 22 KB

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