request_manager.c 22 KB

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