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