1 /**********************************************************************
4 * Contact: support@cavium.com
5 * Please include "LiquidIO" in the subject.
7 * Copyright (c) 2003-2016 Cavium, Inc.
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.
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
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"
33 struct iq_post_status {
38 static void check_db_timeout(struct work_struct *work);
39 static void __check_db_timeout(struct octeon_device *oct, u64 iq_no);
41 static void (*reqtype_free_fn[MAX_OCTEON_DEVICES][REQTYPE_LAST + 1]) (void *);
43 /* Define this to return the request status comaptible to old code */
44 /*#define OCTEON_USE_OLD_REQ_STATUS*/
46 /* Return 0 on success, 1 on failure */
47 int octeon_init_instr_queue(struct octeon_device *oct,
48 union oct_txpciq txpciq,
51 struct octeon_instr_queue *iq;
52 struct octeon_iq_config *conf = NULL;
53 u32 iq_no = (u32)txpciq.s.q_no;
55 struct cavium_wq *db_wq;
56 int numa_node = dev_to_node(&oct->pci_dev->dev);
58 if (OCTEON_CN6XXX(oct))
59 conf = &(CFG_GET_IQ_CFG(CHIP_CONF(oct, cn6xxx)));
60 else if (OCTEON_CN23XX_PF(oct))
61 conf = &(CFG_GET_IQ_CFG(CHIP_CONF(oct, cn23xx_pf)));
62 else if (OCTEON_CN23XX_VF(oct))
63 conf = &(CFG_GET_IQ_CFG(CHIP_CONF(oct, cn23xx_vf)));
66 dev_err(&oct->pci_dev->dev, "Unsupported Chip %x\n",
71 q_size = (u32)conf->instr_type * num_descs;
73 iq = oct->instr_queue[iq_no];
77 iq->base_addr = lio_dma_alloc(oct, q_size, &iq->base_addr_dma);
79 dev_err(&oct->pci_dev->dev, "Cannot allocate memory for instr queue %d\n",
84 iq->max_count = num_descs;
86 /* Initialize a list to holds requests that have been posted to Octeon
87 * but has yet to be fetched by octeon
89 iq->request_list = vzalloc_node(array_size(num_descs, sizeof(*iq->request_list)),
91 if (!iq->request_list)
92 iq->request_list = vzalloc(array_size(num_descs, sizeof(*iq->request_list)));
93 if (!iq->request_list) {
94 lio_dma_free(oct, q_size, iq->base_addr, iq->base_addr_dma);
95 dev_err(&oct->pci_dev->dev, "Alloc failed for IQ[%d] nr free list\n",
100 dev_dbg(&oct->pci_dev->dev, "IQ[%d]: base: %p basedma: %pad count: %d\n",
101 iq_no, iq->base_addr, &iq->base_addr_dma, iq->max_count);
103 iq->txpciq.u64 = txpciq.u64;
104 iq->fill_threshold = (u32)conf->db_min;
106 iq->host_write_index = 0;
107 iq->octeon_read_index = 0;
109 iq->last_db_time = 0;
110 iq->do_auto_flush = 1;
111 iq->db_timeout = (u32)conf->db_timeout;
112 atomic_set(&iq->instr_pending, 0);
113 iq->pkts_processed = 0;
115 /* Initialize the spinlock for this instruction queue */
116 spin_lock_init(&iq->lock);
118 iq->allow_soft_cmds = true;
119 spin_lock_init(&iq->post_lock);
121 iq->allow_soft_cmds = false;
124 spin_lock_init(&iq->iq_flush_running_lock);
126 oct->io_qmask.iq |= BIT_ULL(iq_no);
128 /* Set the 32B/64B mode for each input queue */
129 oct->io_qmask.iq64B |= ((conf->instr_type == 64) << iq_no);
130 iq->iqcmd_64B = (conf->instr_type == 64);
132 oct->fn_list.setup_iq_regs(oct, iq_no);
134 oct->check_db_wq[iq_no].wq = alloc_workqueue("check_iq_db",
137 if (!oct->check_db_wq[iq_no].wq) {
138 vfree(iq->request_list);
139 iq->request_list = NULL;
140 lio_dma_free(oct, q_size, iq->base_addr, iq->base_addr_dma);
141 dev_err(&oct->pci_dev->dev, "check db wq create failed for iq %d\n",
146 db_wq = &oct->check_db_wq[iq_no];
148 INIT_DELAYED_WORK(&db_wq->wk.work, check_db_timeout);
149 db_wq->wk.ctxptr = oct;
150 db_wq->wk.ctxul = iq_no;
151 queue_delayed_work(db_wq->wq, &db_wq->wk.work, msecs_to_jiffies(1));
156 int octeon_delete_instr_queue(struct octeon_device *oct, u32 iq_no)
158 u64 desc_size = 0, q_size;
159 struct octeon_instr_queue *iq = oct->instr_queue[iq_no];
161 cancel_delayed_work_sync(&oct->check_db_wq[iq_no].wk.work);
162 destroy_workqueue(oct->check_db_wq[iq_no].wq);
164 if (OCTEON_CN6XXX(oct))
166 CFG_GET_IQ_INSTR_TYPE(CHIP_CONF(oct, cn6xxx));
167 else if (OCTEON_CN23XX_PF(oct))
169 CFG_GET_IQ_INSTR_TYPE(CHIP_CONF(oct, cn23xx_pf));
170 else if (OCTEON_CN23XX_VF(oct))
172 CFG_GET_IQ_INSTR_TYPE(CHIP_CONF(oct, cn23xx_vf));
174 vfree(iq->request_list);
177 q_size = iq->max_count * desc_size;
178 lio_dma_free(oct, (u32)q_size, iq->base_addr,
180 oct->io_qmask.iq &= ~(1ULL << iq_no);
181 vfree(oct->instr_queue[iq_no]);
182 oct->instr_queue[iq_no] = NULL;
188 EXPORT_SYMBOL_GPL(octeon_delete_instr_queue);
190 /* Return 0 on success, 1 on failure */
191 int octeon_setup_iq(struct octeon_device *oct,
194 union oct_txpciq txpciq,
198 u32 iq_no = (u32)txpciq.s.q_no;
199 int numa_node = dev_to_node(&oct->pci_dev->dev);
201 if (oct->instr_queue[iq_no]) {
202 dev_dbg(&oct->pci_dev->dev, "IQ is in use. Cannot create the IQ: %d again\n",
204 oct->instr_queue[iq_no]->txpciq.u64 = txpciq.u64;
205 oct->instr_queue[iq_no]->app_ctx = app_ctx;
208 oct->instr_queue[iq_no] =
209 vzalloc_node(sizeof(struct octeon_instr_queue), numa_node);
210 if (!oct->instr_queue[iq_no])
211 oct->instr_queue[iq_no] =
212 vzalloc(sizeof(struct octeon_instr_queue));
213 if (!oct->instr_queue[iq_no])
217 oct->instr_queue[iq_no]->q_index = q_index;
218 oct->instr_queue[iq_no]->app_ctx = app_ctx;
219 oct->instr_queue[iq_no]->ifidx = ifidx;
221 if (octeon_init_instr_queue(oct, txpciq, num_descs)) {
222 vfree(oct->instr_queue[iq_no]);
223 oct->instr_queue[iq_no] = NULL;
228 if (oct->fn_list.enable_io_queues(oct)) {
229 octeon_delete_instr_queue(oct, iq_no);
236 int lio_wait_for_instr_fetch(struct octeon_device *oct)
238 int i, retry = 1000, pending, instr_cnt = 0;
243 for (i = 0; i < MAX_OCTEON_INSTR_QUEUES(oct); i++) {
244 if (!(oct->io_qmask.iq & BIT_ULL(i)))
247 atomic_read(&oct->instr_queue[i]->instr_pending);
249 __check_db_timeout(oct, i);
250 instr_cnt += pending;
256 schedule_timeout_uninterruptible(1);
258 } while (retry-- && instr_cnt);
262 EXPORT_SYMBOL_GPL(lio_wait_for_instr_fetch);
265 ring_doorbell(struct octeon_device *oct, struct octeon_instr_queue *iq)
267 if (atomic_read(&oct->status) == OCT_DEV_RUNNING) {
268 writel(iq->fill_cnt, iq->doorbell_reg);
269 /* make sure doorbell write goes through */
271 iq->last_db_time = jiffies;
277 octeon_ring_doorbell_locked(struct octeon_device *oct, u32 iq_no)
279 struct octeon_instr_queue *iq;
281 iq = oct->instr_queue[iq_no];
282 spin_lock(&iq->post_lock);
284 ring_doorbell(oct, iq);
285 spin_unlock(&iq->post_lock);
287 EXPORT_SYMBOL_GPL(octeon_ring_doorbell_locked);
289 static inline void __copy_cmd_into_iq(struct octeon_instr_queue *iq,
294 cmdsize = ((iq->iqcmd_64B) ? 64 : 32);
295 iqptr = iq->base_addr + (cmdsize * iq->host_write_index);
297 memcpy(iqptr, cmd, cmdsize);
300 static inline struct iq_post_status
301 __post_command2(struct octeon_instr_queue *iq, u8 *cmd)
303 struct iq_post_status st;
305 st.status = IQ_SEND_OK;
307 /* This ensures that the read index does not wrap around to the same
308 * position if queue gets full before Octeon could fetch any instr.
310 if (atomic_read(&iq->instr_pending) >= (s32)(iq->max_count - 1)) {
311 st.status = IQ_SEND_FAILED;
316 if (atomic_read(&iq->instr_pending) >= (s32)(iq->max_count - 2))
317 st.status = IQ_SEND_STOP;
319 __copy_cmd_into_iq(iq, cmd);
321 /* "index" is returned, host_write_index is modified. */
322 st.index = iq->host_write_index;
323 iq->host_write_index = incr_index(iq->host_write_index, 1,
327 /* Flush the command into memory. We need to be sure the data is in
328 * memory before indicating that the instruction is pending.
332 atomic_inc(&iq->instr_pending);
338 octeon_register_reqtype_free_fn(struct octeon_device *oct, int reqtype,
341 if (reqtype > REQTYPE_LAST) {
342 dev_err(&oct->pci_dev->dev, "%s: Invalid reqtype: %d\n",
347 reqtype_free_fn[oct->octeon_id][reqtype] = fn;
351 EXPORT_SYMBOL_GPL(octeon_register_reqtype_free_fn);
354 __add_to_request_list(struct octeon_instr_queue *iq,
355 int idx, void *buf, int reqtype)
357 iq->request_list[idx].buf = buf;
358 iq->request_list[idx].reqtype = reqtype;
361 /* Can only run in process context */
363 lio_process_iq_request_list(struct octeon_device *oct,
364 struct octeon_instr_queue *iq, u32 napi_budget)
366 struct cavium_wq *cwq = &oct->dma_comp_wq;
369 u32 old = iq->flush_index;
371 unsigned int pkts_compl = 0, bytes_compl = 0;
372 struct octeon_soft_command *sc;
375 while (old != iq->octeon_read_index) {
376 reqtype = iq->request_list[old].reqtype;
377 buf = iq->request_list[old].buf;
379 if (reqtype == REQTYPE_NONE)
382 octeon_update_tx_completion_counters(buf, reqtype, &pkts_compl,
386 case REQTYPE_NORESP_NET:
387 case REQTYPE_NORESP_NET_SG:
388 case REQTYPE_RESP_NET_SG:
389 reqtype_free_fn[oct->octeon_id][reqtype](buf);
391 case REQTYPE_RESP_NET:
392 case REQTYPE_SOFT_COMMAND:
394 /* We're expecting a response from Octeon.
395 * It's up to lio_process_ordered_list() to
396 * process sc. Add sc to the ordered soft
397 * command response list because we expect
398 * a response from Octeon.
400 spin_lock_irqsave(&oct->response_list
401 [OCTEON_ORDERED_SC_LIST].lock, flags);
402 atomic_inc(&oct->response_list
403 [OCTEON_ORDERED_SC_LIST].pending_req_count);
404 list_add_tail(&sc->node, &oct->response_list
405 [OCTEON_ORDERED_SC_LIST].head);
406 spin_unlock_irqrestore(&oct->response_list
407 [OCTEON_ORDERED_SC_LIST].lock,
411 dev_err(&oct->pci_dev->dev,
412 "%s Unknown reqtype: %d buf: %p at idx %d\n",
413 __func__, reqtype, buf, old);
416 iq->request_list[old].buf = NULL;
417 iq->request_list[old].reqtype = 0;
421 old = incr_index(old, 1, iq->max_count);
423 if ((napi_budget) && (inst_count >= napi_budget))
427 octeon_report_tx_completion_to_bql(iq->app_ctx, pkts_compl,
429 iq->flush_index = old;
431 if (atomic_read(&oct->response_list
432 [OCTEON_ORDERED_SC_LIST].pending_req_count))
433 queue_work(cwq->wq, &cwq->wk.work.work);
437 EXPORT_SYMBOL_GPL(lio_process_iq_request_list);
439 /* Can only be called from process context */
441 octeon_flush_iq(struct octeon_device *oct, struct octeon_instr_queue *iq,
444 u32 inst_processed = 0;
445 u32 tot_inst_processed = 0;
448 if (!spin_trylock(&iq->iq_flush_running_lock))
451 spin_lock_bh(&iq->lock);
453 iq->octeon_read_index = oct->fn_list.update_iq_read_idx(iq);
456 /* Process any outstanding IQ packets. */
457 if (iq->flush_index == iq->octeon_read_index)
462 lio_process_iq_request_list(oct, iq,
467 lio_process_iq_request_list(oct, iq, 0);
469 if (inst_processed) {
470 iq->pkts_processed += inst_processed;
471 atomic_sub(inst_processed, &iq->instr_pending);
472 iq->stats.instr_processed += inst_processed;
475 tot_inst_processed += inst_processed;
476 } while (tot_inst_processed < napi_budget);
478 if (napi_budget && (tot_inst_processed >= napi_budget))
481 iq->last_db_time = jiffies;
483 spin_unlock_bh(&iq->lock);
485 spin_unlock(&iq->iq_flush_running_lock);
490 /* Process instruction queue after timeout.
491 * This routine gets called from a workqueue or when removing the module.
493 static void __check_db_timeout(struct octeon_device *oct, u64 iq_no)
495 struct octeon_instr_queue *iq;
501 iq = oct->instr_queue[iq_no];
505 /* return immediately, if no work pending */
506 if (!atomic_read(&iq->instr_pending))
508 /* If jiffies - last_db_time < db_timeout do nothing */
509 next_time = iq->last_db_time + iq->db_timeout;
510 if (!time_after(jiffies, (unsigned long)next_time))
512 iq->last_db_time = jiffies;
514 /* Flush the instruction queue */
515 octeon_flush_iq(oct, iq, 0);
517 lio_enable_irq(NULL, iq);
520 /* Called by the Poll thread at regular intervals to check the instruction
521 * queue for commands to be posted and for commands that were fetched by Octeon.
523 static void check_db_timeout(struct work_struct *work)
525 struct cavium_wk *wk = (struct cavium_wk *)work;
526 struct octeon_device *oct = (struct octeon_device *)wk->ctxptr;
527 u64 iq_no = wk->ctxul;
528 struct cavium_wq *db_wq = &oct->check_db_wq[iq_no];
531 __check_db_timeout(oct, iq_no);
532 queue_delayed_work(db_wq->wq, &db_wq->wk.work, msecs_to_jiffies(delay));
536 octeon_send_command(struct octeon_device *oct, u32 iq_no,
537 u32 force_db, void *cmd, void *buf,
538 u32 datasize, u32 reqtype)
541 struct iq_post_status st;
542 struct octeon_instr_queue *iq = oct->instr_queue[iq_no];
544 /* Get the lock and prevent other tasks and tx interrupt handler from
547 if (iq->allow_soft_cmds)
548 spin_lock_bh(&iq->post_lock);
550 st = __post_command2(iq, cmd);
552 if (st.status != IQ_SEND_FAILED) {
553 xmit_stopped = octeon_report_sent_bytes_to_bql(buf, reqtype);
554 __add_to_request_list(iq, st.index, buf, reqtype);
555 INCR_INSTRQUEUE_PKT_COUNT(oct, iq_no, bytes_sent, datasize);
556 INCR_INSTRQUEUE_PKT_COUNT(oct, iq_no, instr_posted, 1);
558 if (iq->fill_cnt >= MAX_OCTEON_FILL_COUNT || force_db ||
559 xmit_stopped || st.status == IQ_SEND_STOP)
560 ring_doorbell(oct, iq);
562 INCR_INSTRQUEUE_PKT_COUNT(oct, iq_no, instr_dropped, 1);
565 if (iq->allow_soft_cmds)
566 spin_unlock_bh(&iq->post_lock);
568 /* This is only done here to expedite packets being flushed
569 * for cases where there are no IQ completion interrupts.
574 EXPORT_SYMBOL_GPL(octeon_send_command);
577 octeon_prepare_soft_command(struct octeon_device *oct,
578 struct octeon_soft_command *sc,
585 struct octeon_config *oct_cfg;
586 struct octeon_instr_ih2 *ih2;
587 struct octeon_instr_ih3 *ih3;
588 struct octeon_instr_pki_ih3 *pki_ih3;
589 struct octeon_instr_irh *irh;
590 struct octeon_instr_rdp *rdp;
592 WARN_ON(opcode > 15);
593 WARN_ON(subcode > 127);
595 oct_cfg = octeon_get_conf(oct);
597 if (OCTEON_CN23XX_PF(oct) || OCTEON_CN23XX_VF(oct)) {
598 ih3 = (struct octeon_instr_ih3 *)&sc->cmd.cmd3.ih3;
600 ih3->pkind = oct->instr_queue[sc->iq_no]->txpciq.s.pkind;
602 pki_ih3 = (struct octeon_instr_pki_ih3 *)&sc->cmd.cmd3.pki_ih3;
608 oct->instr_queue[sc->iq_no]->txpciq.s.use_qpg;
610 pki_ih3->tag = LIO_CONTROL;
611 pki_ih3->tagtype = ATOMIC_TAG;
613 oct->instr_queue[sc->iq_no]->txpciq.s.ctrl_qpg;
619 ih3->dlengsz = sc->datasize;
621 irh = (struct octeon_instr_irh *)&sc->cmd.cmd3.irh;
622 irh->opcode = opcode;
623 irh->subcode = subcode;
625 /* opcode/subcode specific parameters (ossp) */
626 irh->ossp = irh_ossp;
627 sc->cmd.cmd3.ossp[0] = ossp0;
628 sc->cmd.cmd3.ossp[1] = ossp1;
631 rdp = (struct octeon_instr_rdp *)&sc->cmd.cmd3.rdp;
632 rdp->pcie_port = oct->pcie_port;
633 rdp->rlen = sc->rdatasize;
637 /* pki_ih3 irh+ossp[0]+ossp[1]+rdp+rptr = 48 bytes */
638 ih3->fsz = LIO_SOFTCMDRESP_IH3;
642 /* pki_h3 + irh + ossp[0] + ossp[1] = 32 bytes */
643 ih3->fsz = LIO_PCICMD_O3;
647 ih2 = (struct octeon_instr_ih2 *)&sc->cmd.cmd2.ih2;
648 ih2->tagtype = ATOMIC_TAG;
649 ih2->tag = LIO_CONTROL;
651 ih2->grp = CFG_GET_CTRL_Q_GRP(oct_cfg);
654 ih2->dlengsz = sc->datasize;
658 irh = (struct octeon_instr_irh *)&sc->cmd.cmd2.irh;
659 irh->opcode = opcode;
660 irh->subcode = subcode;
662 /* opcode/subcode specific parameters (ossp) */
663 irh->ossp = irh_ossp;
664 sc->cmd.cmd2.ossp[0] = ossp0;
665 sc->cmd.cmd2.ossp[1] = ossp1;
668 rdp = (struct octeon_instr_rdp *)&sc->cmd.cmd2.rdp;
669 rdp->pcie_port = oct->pcie_port;
670 rdp->rlen = sc->rdatasize;
673 /* irh+ossp[0]+ossp[1]+rdp+rptr = 40 bytes */
674 ih2->fsz = LIO_SOFTCMDRESP_IH2;
677 /* irh + ossp[0] + ossp[1] = 24 bytes */
678 ih2->fsz = LIO_PCICMD_O2;
682 EXPORT_SYMBOL_GPL(octeon_prepare_soft_command);
684 int octeon_send_soft_command(struct octeon_device *oct,
685 struct octeon_soft_command *sc)
687 struct octeon_instr_queue *iq;
688 struct octeon_instr_ih2 *ih2;
689 struct octeon_instr_ih3 *ih3;
690 struct octeon_instr_irh *irh;
693 iq = oct->instr_queue[sc->iq_no];
694 if (!iq->allow_soft_cmds) {
695 dev_err(&oct->pci_dev->dev, "Soft commands are not allowed on Queue %d\n",
697 INCR_INSTRQUEUE_PKT_COUNT(oct, sc->iq_no, instr_dropped, 1);
698 return IQ_SEND_FAILED;
701 if (OCTEON_CN23XX_PF(oct) || OCTEON_CN23XX_VF(oct)) {
702 ih3 = (struct octeon_instr_ih3 *)&sc->cmd.cmd3.ih3;
704 WARN_ON(!sc->dmadptr);
705 sc->cmd.cmd3.dptr = sc->dmadptr;
707 irh = (struct octeon_instr_irh *)&sc->cmd.cmd3.irh;
709 WARN_ON(!sc->dmarptr);
710 WARN_ON(!sc->status_word);
711 *sc->status_word = COMPLETION_WORD_INIT;
712 sc->cmd.cmd3.rptr = sc->dmarptr;
714 len = (u32)ih3->dlengsz;
716 ih2 = (struct octeon_instr_ih2 *)&sc->cmd.cmd2.ih2;
718 WARN_ON(!sc->dmadptr);
719 sc->cmd.cmd2.dptr = sc->dmadptr;
721 irh = (struct octeon_instr_irh *)&sc->cmd.cmd2.irh;
723 WARN_ON(!sc->dmarptr);
724 WARN_ON(!sc->status_word);
725 *sc->status_word = COMPLETION_WORD_INIT;
726 sc->cmd.cmd2.rptr = sc->dmarptr;
728 len = (u32)ih2->dlengsz;
731 sc->expiry_time = jiffies + msecs_to_jiffies(LIO_SC_MAX_TMO_MS);
733 return (octeon_send_command(oct, sc->iq_no, 1, &sc->cmd, sc,
734 len, REQTYPE_SOFT_COMMAND));
736 EXPORT_SYMBOL_GPL(octeon_send_soft_command);
738 int octeon_setup_sc_buffer_pool(struct octeon_device *oct)
742 struct octeon_soft_command *sc;
744 INIT_LIST_HEAD(&oct->sc_buf_pool.head);
745 spin_lock_init(&oct->sc_buf_pool.lock);
746 atomic_set(&oct->sc_buf_pool.alloc_buf_count, 0);
748 for (i = 0; i < MAX_SOFT_COMMAND_BUFFERS; i++) {
749 sc = (struct octeon_soft_command *)
751 SOFT_COMMAND_BUFFER_SIZE,
752 (dma_addr_t *)&dma_addr);
754 octeon_free_sc_buffer_pool(oct);
758 sc->dma_addr = dma_addr;
759 sc->size = SOFT_COMMAND_BUFFER_SIZE;
761 list_add_tail(&sc->node, &oct->sc_buf_pool.head);
766 EXPORT_SYMBOL_GPL(octeon_setup_sc_buffer_pool);
768 int octeon_free_sc_done_list(struct octeon_device *oct)
770 struct octeon_response_list *done_sc_list, *zombie_sc_list;
771 struct octeon_soft_command *sc;
772 struct list_head *tmp, *tmp2;
773 spinlock_t *sc_lists_lock; /* lock for response_list */
775 done_sc_list = &oct->response_list[OCTEON_DONE_SC_LIST];
776 zombie_sc_list = &oct->response_list[OCTEON_ZOMBIE_SC_LIST];
778 if (!atomic_read(&done_sc_list->pending_req_count))
781 sc_lists_lock = &oct->response_list[OCTEON_ORDERED_SC_LIST].lock;
783 spin_lock_bh(sc_lists_lock);
785 list_for_each_safe(tmp, tmp2, &done_sc_list->head) {
786 sc = list_entry(tmp, struct octeon_soft_command, node);
788 if (READ_ONCE(sc->caller_is_done)) {
790 atomic_dec(&done_sc_list->pending_req_count);
792 if (*sc->status_word == COMPLETION_WORD_INIT) {
793 /* timeout; move sc to zombie list */
794 list_add_tail(&sc->node, &zombie_sc_list->head);
795 atomic_inc(&zombie_sc_list->pending_req_count);
797 octeon_free_soft_command(oct, sc);
802 spin_unlock_bh(sc_lists_lock);
806 EXPORT_SYMBOL_GPL(octeon_free_sc_done_list);
808 int octeon_free_sc_zombie_list(struct octeon_device *oct)
810 struct octeon_response_list *zombie_sc_list;
811 struct octeon_soft_command *sc;
812 struct list_head *tmp, *tmp2;
813 spinlock_t *sc_lists_lock; /* lock for response_list */
815 zombie_sc_list = &oct->response_list[OCTEON_ZOMBIE_SC_LIST];
816 sc_lists_lock = &oct->response_list[OCTEON_ORDERED_SC_LIST].lock;
818 spin_lock_bh(sc_lists_lock);
820 list_for_each_safe(tmp, tmp2, &zombie_sc_list->head) {
822 atomic_dec(&zombie_sc_list->pending_req_count);
823 sc = list_entry(tmp, struct octeon_soft_command, node);
824 octeon_free_soft_command(oct, sc);
827 spin_unlock_bh(sc_lists_lock);
831 EXPORT_SYMBOL_GPL(octeon_free_sc_zombie_list);
833 int octeon_free_sc_buffer_pool(struct octeon_device *oct)
835 struct list_head *tmp, *tmp2;
836 struct octeon_soft_command *sc;
838 octeon_free_sc_zombie_list(oct);
840 spin_lock_bh(&oct->sc_buf_pool.lock);
842 list_for_each_safe(tmp, tmp2, &oct->sc_buf_pool.head) {
845 sc = (struct octeon_soft_command *)tmp;
847 lio_dma_free(oct, sc->size, sc, sc->dma_addr);
850 INIT_LIST_HEAD(&oct->sc_buf_pool.head);
852 spin_unlock_bh(&oct->sc_buf_pool.lock);
856 EXPORT_SYMBOL_GPL(octeon_free_sc_buffer_pool);
858 struct octeon_soft_command *octeon_alloc_soft_command(struct octeon_device *oct,
865 u32 offset = sizeof(struct octeon_soft_command);
866 struct octeon_soft_command *sc = NULL;
867 struct list_head *tmp;
872 WARN_ON((offset + datasize + rdatasize + ctxsize) >
873 SOFT_COMMAND_BUFFER_SIZE);
875 spin_lock_bh(&oct->sc_buf_pool.lock);
877 if (list_empty(&oct->sc_buf_pool.head)) {
878 spin_unlock_bh(&oct->sc_buf_pool.lock);
882 list_for_each(tmp, &oct->sc_buf_pool.head)
887 atomic_inc(&oct->sc_buf_pool.alloc_buf_count);
889 spin_unlock_bh(&oct->sc_buf_pool.lock);
891 sc = (struct octeon_soft_command *)tmp;
893 dma_addr = sc->dma_addr;
896 memset(sc, 0, sc->size);
898 sc->dma_addr = dma_addr;
902 sc->ctxptr = (u8 *)sc + offset;
903 sc->ctxsize = ctxsize;
906 /* Start data at 128 byte boundary */
907 offset = (offset + ctxsize + 127) & 0xffffff80;
910 sc->virtdptr = (u8 *)sc + offset;
911 sc->dmadptr = dma_addr + offset;
912 sc->datasize = datasize;
915 /* Start rdata at 128 byte boundary */
916 offset = (offset + datasize + 127) & 0xffffff80;
919 WARN_ON(rdatasize < 16);
920 sc->virtrptr = (u8 *)sc + offset;
921 sc->dmarptr = dma_addr + offset;
922 sc->rdatasize = rdatasize;
923 sc->status_word = (u64 *)((u8 *)(sc->virtrptr) + rdatasize - 8);
928 EXPORT_SYMBOL_GPL(octeon_alloc_soft_command);
930 void octeon_free_soft_command(struct octeon_device *oct,
931 struct octeon_soft_command *sc)
933 spin_lock_bh(&oct->sc_buf_pool.lock);
935 list_add_tail(&sc->node, &oct->sc_buf_pool.head);
937 atomic_dec(&oct->sc_buf_pool.alloc_buf_count);
939 spin_unlock_bh(&oct->sc_buf_pool.lock);
941 EXPORT_SYMBOL_GPL(octeon_free_soft_command);