• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* QLogic qed NIC Driver
2  * Copyright (c) 2015-2017  QLogic Corporation
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and /or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32 
33 #include <linux/types.h>
34 #include <asm/byteorder.h>
35 #include <linux/io.h>
36 #include <linux/delay.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/errno.h>
39 #include <linux/kernel.h>
40 #include <linux/list.h>
41 #include <linux/pci.h>
42 #include <linux/slab.h>
43 #include <linux/spinlock.h>
44 #include <linux/string.h>
45 #include "qed.h"
46 #include "qed_cxt.h"
47 #include "qed_dev_api.h"
48 #include "qed_hsi.h"
49 #include "qed_hw.h"
50 #include "qed_int.h"
51 #include "qed_iscsi.h"
52 #include "qed_mcp.h"
53 #include "qed_ooo.h"
54 #include "qed_reg_addr.h"
55 #include "qed_sp.h"
56 #include "qed_sriov.h"
57 #include "qed_rdma.h"
58 
59 /***************************************************************************
60 * Structures & Definitions
61 ***************************************************************************/
62 
63 #define SPQ_HIGH_PRI_RESERVE_DEFAULT    (1)
64 
65 #define SPQ_BLOCK_DELAY_MAX_ITER        (10)
66 #define SPQ_BLOCK_DELAY_US              (10)
67 #define SPQ_BLOCK_SLEEP_MAX_ITER        (1000)
68 #define SPQ_BLOCK_SLEEP_MS              (5)
69 
70 /***************************************************************************
71 * Blocking Imp. (BLOCK/EBLOCK mode)
72 ***************************************************************************/
qed_spq_blocking_cb(struct qed_hwfn * p_hwfn,void * cookie,union event_ring_data * data,u8 fw_return_code)73 static void qed_spq_blocking_cb(struct qed_hwfn *p_hwfn,
74 				void *cookie,
75 				union event_ring_data *data, u8 fw_return_code)
76 {
77 	struct qed_spq_comp_done *comp_done;
78 
79 	comp_done = (struct qed_spq_comp_done *)cookie;
80 
81 	comp_done->fw_return_code = fw_return_code;
82 
83 	/* Make sure completion done is visible on waiting thread */
84 	smp_store_release(&comp_done->done, 0x1);
85 }
86 
__qed_spq_block(struct qed_hwfn * p_hwfn,struct qed_spq_entry * p_ent,u8 * p_fw_ret,bool sleep_between_iter)87 static int __qed_spq_block(struct qed_hwfn *p_hwfn,
88 			   struct qed_spq_entry *p_ent,
89 			   u8 *p_fw_ret, bool sleep_between_iter)
90 {
91 	struct qed_spq_comp_done *comp_done;
92 	u32 iter_cnt;
93 
94 	comp_done = (struct qed_spq_comp_done *)p_ent->comp_cb.cookie;
95 	iter_cnt = sleep_between_iter ? SPQ_BLOCK_SLEEP_MAX_ITER
96 				      : SPQ_BLOCK_DELAY_MAX_ITER;
97 
98 	while (iter_cnt--) {
99 		/* Validate we receive completion update */
100 		if (smp_load_acquire(&comp_done->done) == 1) { /* ^^^ */
101 			if (p_fw_ret)
102 				*p_fw_ret = comp_done->fw_return_code;
103 			return 0;
104 		}
105 
106 		if (sleep_between_iter)
107 			msleep(SPQ_BLOCK_SLEEP_MS);
108 		else
109 			udelay(SPQ_BLOCK_DELAY_US);
110 	}
111 
112 	return -EBUSY;
113 }
114 
qed_spq_block(struct qed_hwfn * p_hwfn,struct qed_spq_entry * p_ent,u8 * p_fw_ret,bool skip_quick_poll)115 static int qed_spq_block(struct qed_hwfn *p_hwfn,
116 			 struct qed_spq_entry *p_ent,
117 			 u8 *p_fw_ret, bool skip_quick_poll)
118 {
119 	struct qed_spq_comp_done *comp_done;
120 	struct qed_ptt *p_ptt;
121 	int rc;
122 
123 	/* A relatively short polling period w/o sleeping, to allow the FW to
124 	 * complete the ramrod and thus possibly to avoid the following sleeps.
125 	 */
126 	if (!skip_quick_poll) {
127 		rc = __qed_spq_block(p_hwfn, p_ent, p_fw_ret, false);
128 		if (!rc)
129 			return 0;
130 	}
131 
132 	/* Move to polling with a sleeping period between iterations */
133 	rc = __qed_spq_block(p_hwfn, p_ent, p_fw_ret, true);
134 	if (!rc)
135 		return 0;
136 
137 	p_ptt = qed_ptt_acquire(p_hwfn);
138 	if (!p_ptt) {
139 		DP_NOTICE(p_hwfn, "ptt, failed to acquire\n");
140 		return -EAGAIN;
141 	}
142 
143 	DP_INFO(p_hwfn, "Ramrod is stuck, requesting MCP drain\n");
144 	rc = qed_mcp_drain(p_hwfn, p_ptt);
145 	qed_ptt_release(p_hwfn, p_ptt);
146 	if (rc) {
147 		DP_NOTICE(p_hwfn, "MCP drain failed\n");
148 		goto err;
149 	}
150 
151 	/* Retry after drain */
152 	rc = __qed_spq_block(p_hwfn, p_ent, p_fw_ret, true);
153 	if (!rc)
154 		return 0;
155 
156 	comp_done = (struct qed_spq_comp_done *)p_ent->comp_cb.cookie;
157 	if (comp_done->done == 1) {
158 		if (p_fw_ret)
159 			*p_fw_ret = comp_done->fw_return_code;
160 		return 0;
161 	}
162 err:
163 	DP_NOTICE(p_hwfn,
164 		  "Ramrod is stuck [CID %08x cmd %02x protocol %02x echo %04x]\n",
165 		  le32_to_cpu(p_ent->elem.hdr.cid),
166 		  p_ent->elem.hdr.cmd_id,
167 		  p_ent->elem.hdr.protocol_id,
168 		  le16_to_cpu(p_ent->elem.hdr.echo));
169 
170 	return -EBUSY;
171 }
172 
173 /***************************************************************************
174 * SPQ entries inner API
175 ***************************************************************************/
qed_spq_fill_entry(struct qed_hwfn * p_hwfn,struct qed_spq_entry * p_ent)176 static int qed_spq_fill_entry(struct qed_hwfn *p_hwfn,
177 			      struct qed_spq_entry *p_ent)
178 {
179 	p_ent->flags = 0;
180 
181 	switch (p_ent->comp_mode) {
182 	case QED_SPQ_MODE_EBLOCK:
183 	case QED_SPQ_MODE_BLOCK:
184 		p_ent->comp_cb.function = qed_spq_blocking_cb;
185 		break;
186 	case QED_SPQ_MODE_CB:
187 		break;
188 	default:
189 		DP_NOTICE(p_hwfn, "Unknown SPQE completion mode %d\n",
190 			  p_ent->comp_mode);
191 		return -EINVAL;
192 	}
193 
194 	DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
195 		   "Ramrod header: [CID 0x%08x CMD 0x%02x protocol 0x%02x] Data pointer: [%08x:%08x] Completion Mode: %s\n",
196 		   p_ent->elem.hdr.cid,
197 		   p_ent->elem.hdr.cmd_id,
198 		   p_ent->elem.hdr.protocol_id,
199 		   p_ent->elem.data_ptr.hi,
200 		   p_ent->elem.data_ptr.lo,
201 		   D_TRINE(p_ent->comp_mode, QED_SPQ_MODE_EBLOCK,
202 			   QED_SPQ_MODE_BLOCK, "MODE_EBLOCK", "MODE_BLOCK",
203 			   "MODE_CB"));
204 
205 	return 0;
206 }
207 
208 /***************************************************************************
209 * HSI access
210 ***************************************************************************/
qed_spq_hw_initialize(struct qed_hwfn * p_hwfn,struct qed_spq * p_spq)211 static void qed_spq_hw_initialize(struct qed_hwfn *p_hwfn,
212 				  struct qed_spq *p_spq)
213 {
214 	struct e4_core_conn_context *p_cxt;
215 	struct qed_cxt_info cxt_info;
216 	u16 physical_q;
217 	int rc;
218 
219 	cxt_info.iid = p_spq->cid;
220 
221 	rc = qed_cxt_get_cid_info(p_hwfn, &cxt_info);
222 
223 	if (rc < 0) {
224 		DP_NOTICE(p_hwfn, "Cannot find context info for cid=%d\n",
225 			  p_spq->cid);
226 		return;
227 	}
228 
229 	p_cxt = cxt_info.p_cxt;
230 
231 	SET_FIELD(p_cxt->xstorm_ag_context.flags10,
232 		  E4_XSTORM_CORE_CONN_AG_CTX_DQ_CF_EN, 1);
233 	SET_FIELD(p_cxt->xstorm_ag_context.flags1,
234 		  E4_XSTORM_CORE_CONN_AG_CTX_DQ_CF_ACTIVE, 1);
235 	SET_FIELD(p_cxt->xstorm_ag_context.flags9,
236 		  E4_XSTORM_CORE_CONN_AG_CTX_CONSOLID_PROD_CF_EN, 1);
237 
238 	/* QM physical queue */
239 	physical_q = qed_get_cm_pq_idx(p_hwfn, PQ_FLAGS_LB);
240 	p_cxt->xstorm_ag_context.physical_q0 = cpu_to_le16(physical_q);
241 
242 	p_cxt->xstorm_st_context.spq_base_lo =
243 		DMA_LO_LE(p_spq->chain.p_phys_addr);
244 	p_cxt->xstorm_st_context.spq_base_hi =
245 		DMA_HI_LE(p_spq->chain.p_phys_addr);
246 
247 	DMA_REGPAIR_LE(p_cxt->xstorm_st_context.consolid_base_addr,
248 		       p_hwfn->p_consq->chain.p_phys_addr);
249 }
250 
qed_spq_hw_post(struct qed_hwfn * p_hwfn,struct qed_spq * p_spq,struct qed_spq_entry * p_ent)251 static int qed_spq_hw_post(struct qed_hwfn *p_hwfn,
252 			   struct qed_spq *p_spq, struct qed_spq_entry *p_ent)
253 {
254 	struct qed_chain *p_chain = &p_hwfn->p_spq->chain;
255 	u16 echo = qed_chain_get_prod_idx(p_chain);
256 	struct slow_path_element	*elem;
257 	struct core_db_data		db;
258 
259 	p_ent->elem.hdr.echo	= cpu_to_le16(echo);
260 	elem = qed_chain_produce(p_chain);
261 	if (!elem) {
262 		DP_NOTICE(p_hwfn, "Failed to produce from SPQ chain\n");
263 		return -EINVAL;
264 	}
265 
266 	*elem = p_ent->elem; /* struct assignment */
267 
268 	/* send a doorbell on the slow hwfn session */
269 	memset(&db, 0, sizeof(db));
270 	SET_FIELD(db.params, CORE_DB_DATA_DEST, DB_DEST_XCM);
271 	SET_FIELD(db.params, CORE_DB_DATA_AGG_CMD, DB_AGG_CMD_SET);
272 	SET_FIELD(db.params, CORE_DB_DATA_AGG_VAL_SEL,
273 		  DQ_XCM_CORE_SPQ_PROD_CMD);
274 	db.agg_flags = DQ_XCM_CORE_DQ_CF_CMD;
275 	db.spq_prod = cpu_to_le16(qed_chain_get_prod_idx(p_chain));
276 
277 	/* make sure the SPQE is updated before the doorbell */
278 	wmb();
279 
280 	DOORBELL(p_hwfn, qed_db_addr(p_spq->cid, DQ_DEMS_LEGACY), *(u32 *)&db);
281 
282 	/* make sure doorbell is rang */
283 	wmb();
284 
285 	DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
286 		   "Doorbelled [0x%08x, CID 0x%08x] with Flags: %02x agg_params: %02x, prod: %04x\n",
287 		   qed_db_addr(p_spq->cid, DQ_DEMS_LEGACY),
288 		   p_spq->cid, db.params, db.agg_flags,
289 		   qed_chain_get_prod_idx(p_chain));
290 
291 	return 0;
292 }
293 
294 /***************************************************************************
295 * Asynchronous events
296 ***************************************************************************/
297 static int
qed_async_event_completion(struct qed_hwfn * p_hwfn,struct event_ring_entry * p_eqe)298 qed_async_event_completion(struct qed_hwfn *p_hwfn,
299 			   struct event_ring_entry *p_eqe)
300 {
301 	qed_spq_async_comp_cb cb;
302 
303 	if (!p_hwfn->p_spq || (p_eqe->protocol_id >= MAX_PROTOCOL_TYPE))
304 		return -EINVAL;
305 
306 	cb = p_hwfn->p_spq->async_comp_cb[p_eqe->protocol_id];
307 	if (cb) {
308 		return cb(p_hwfn, p_eqe->opcode, p_eqe->echo,
309 			  &p_eqe->data, p_eqe->fw_return_code);
310 	} else {
311 		DP_NOTICE(p_hwfn,
312 			  "Unknown Async completion for protocol: %d\n",
313 			  p_eqe->protocol_id);
314 		return -EINVAL;
315 	}
316 }
317 
318 int
qed_spq_register_async_cb(struct qed_hwfn * p_hwfn,enum protocol_type protocol_id,qed_spq_async_comp_cb cb)319 qed_spq_register_async_cb(struct qed_hwfn *p_hwfn,
320 			  enum protocol_type protocol_id,
321 			  qed_spq_async_comp_cb cb)
322 {
323 	if (!p_hwfn->p_spq || (protocol_id >= MAX_PROTOCOL_TYPE))
324 		return -EINVAL;
325 
326 	p_hwfn->p_spq->async_comp_cb[protocol_id] = cb;
327 	return 0;
328 }
329 
330 void
qed_spq_unregister_async_cb(struct qed_hwfn * p_hwfn,enum protocol_type protocol_id)331 qed_spq_unregister_async_cb(struct qed_hwfn *p_hwfn,
332 			    enum protocol_type protocol_id)
333 {
334 	if (!p_hwfn->p_spq || (protocol_id >= MAX_PROTOCOL_TYPE))
335 		return;
336 
337 	p_hwfn->p_spq->async_comp_cb[protocol_id] = NULL;
338 }
339 
340 /***************************************************************************
341 * EQ API
342 ***************************************************************************/
qed_eq_prod_update(struct qed_hwfn * p_hwfn,u16 prod)343 void qed_eq_prod_update(struct qed_hwfn *p_hwfn, u16 prod)
344 {
345 	u32 addr = GTT_BAR0_MAP_REG_USDM_RAM +
346 		   USTORM_EQE_CONS_OFFSET(p_hwfn->rel_pf_id);
347 
348 	REG_WR16(p_hwfn, addr, prod);
349 
350 	/* keep prod updates ordered */
351 	mmiowb();
352 }
353 
qed_eq_completion(struct qed_hwfn * p_hwfn,void * cookie)354 int qed_eq_completion(struct qed_hwfn *p_hwfn, void *cookie)
355 {
356 	struct qed_eq *p_eq = cookie;
357 	struct qed_chain *p_chain = &p_eq->chain;
358 	int rc = 0;
359 
360 	/* take a snapshot of the FW consumer */
361 	u16 fw_cons_idx = le16_to_cpu(*p_eq->p_fw_cons);
362 
363 	DP_VERBOSE(p_hwfn, QED_MSG_SPQ, "fw_cons_idx %x\n", fw_cons_idx);
364 
365 	/* Need to guarantee the fw_cons index we use points to a usuable
366 	 * element (to comply with our chain), so our macros would comply
367 	 */
368 	if ((fw_cons_idx & qed_chain_get_usable_per_page(p_chain)) ==
369 	    qed_chain_get_usable_per_page(p_chain))
370 		fw_cons_idx += qed_chain_get_unusable_per_page(p_chain);
371 
372 	/* Complete current segment of eq entries */
373 	while (fw_cons_idx != qed_chain_get_cons_idx(p_chain)) {
374 		struct event_ring_entry *p_eqe = qed_chain_consume(p_chain);
375 
376 		if (!p_eqe) {
377 			rc = -EINVAL;
378 			break;
379 		}
380 
381 		DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
382 			   "op %x prot %x res0 %x echo %x fwret %x flags %x\n",
383 			   p_eqe->opcode,
384 			   p_eqe->protocol_id,
385 			   p_eqe->reserved0,
386 			   le16_to_cpu(p_eqe->echo),
387 			   p_eqe->fw_return_code,
388 			   p_eqe->flags);
389 
390 		if (GET_FIELD(p_eqe->flags, EVENT_RING_ENTRY_ASYNC)) {
391 			if (qed_async_event_completion(p_hwfn, p_eqe))
392 				rc = -EINVAL;
393 		} else if (qed_spq_completion(p_hwfn,
394 					      p_eqe->echo,
395 					      p_eqe->fw_return_code,
396 					      &p_eqe->data)) {
397 			rc = -EINVAL;
398 		}
399 
400 		qed_chain_recycle_consumed(p_chain);
401 	}
402 
403 	qed_eq_prod_update(p_hwfn, qed_chain_get_prod_idx(p_chain));
404 
405 	/* Attempt to post pending requests */
406 	spin_lock_bh(&p_hwfn->p_spq->lock);
407 	rc = qed_spq_pend_post(p_hwfn);
408 	spin_unlock_bh(&p_hwfn->p_spq->lock);
409 
410 	return rc;
411 }
412 
qed_eq_alloc(struct qed_hwfn * p_hwfn,u16 num_elem)413 int qed_eq_alloc(struct qed_hwfn *p_hwfn, u16 num_elem)
414 {
415 	struct qed_eq *p_eq;
416 
417 	/* Allocate EQ struct */
418 	p_eq = kzalloc(sizeof(*p_eq), GFP_KERNEL);
419 	if (!p_eq)
420 		return -ENOMEM;
421 
422 	/* Allocate and initialize EQ chain*/
423 	if (qed_chain_alloc(p_hwfn->cdev,
424 			    QED_CHAIN_USE_TO_PRODUCE,
425 			    QED_CHAIN_MODE_PBL,
426 			    QED_CHAIN_CNT_TYPE_U16,
427 			    num_elem,
428 			    sizeof(union event_ring_element),
429 			    &p_eq->chain, NULL))
430 		goto eq_allocate_fail;
431 
432 	/* register EQ completion on the SP SB */
433 	qed_int_register_cb(p_hwfn, qed_eq_completion,
434 			    p_eq, &p_eq->eq_sb_index, &p_eq->p_fw_cons);
435 
436 	p_hwfn->p_eq = p_eq;
437 	return 0;
438 
439 eq_allocate_fail:
440 	kfree(p_eq);
441 	return -ENOMEM;
442 }
443 
qed_eq_setup(struct qed_hwfn * p_hwfn)444 void qed_eq_setup(struct qed_hwfn *p_hwfn)
445 {
446 	qed_chain_reset(&p_hwfn->p_eq->chain);
447 }
448 
qed_eq_free(struct qed_hwfn * p_hwfn)449 void qed_eq_free(struct qed_hwfn *p_hwfn)
450 {
451 	if (!p_hwfn->p_eq)
452 		return;
453 
454 	qed_chain_free(p_hwfn->cdev, &p_hwfn->p_eq->chain);
455 
456 	kfree(p_hwfn->p_eq);
457 	p_hwfn->p_eq = NULL;
458 }
459 
460 /***************************************************************************
461 * CQE API - manipulate EQ functionality
462 ***************************************************************************/
qed_cqe_completion(struct qed_hwfn * p_hwfn,struct eth_slow_path_rx_cqe * cqe,enum protocol_type protocol)463 static int qed_cqe_completion(struct qed_hwfn *p_hwfn,
464 			      struct eth_slow_path_rx_cqe *cqe,
465 			      enum protocol_type protocol)
466 {
467 	if (IS_VF(p_hwfn->cdev))
468 		return 0;
469 
470 	/* @@@tmp - it's possible we'll eventually want to handle some
471 	 * actual commands that can arrive here, but for now this is only
472 	 * used to complete the ramrod using the echo value on the cqe
473 	 */
474 	return qed_spq_completion(p_hwfn, cqe->echo, 0, NULL);
475 }
476 
qed_eth_cqe_completion(struct qed_hwfn * p_hwfn,struct eth_slow_path_rx_cqe * cqe)477 int qed_eth_cqe_completion(struct qed_hwfn *p_hwfn,
478 			   struct eth_slow_path_rx_cqe *cqe)
479 {
480 	int rc;
481 
482 	rc = qed_cqe_completion(p_hwfn, cqe, PROTOCOLID_ETH);
483 	if (rc)
484 		DP_NOTICE(p_hwfn,
485 			  "Failed to handle RXQ CQE [cmd 0x%02x]\n",
486 			  cqe->ramrod_cmd_id);
487 
488 	return rc;
489 }
490 
491 /***************************************************************************
492 * Slow hwfn Queue (spq)
493 ***************************************************************************/
qed_spq_setup(struct qed_hwfn * p_hwfn)494 void qed_spq_setup(struct qed_hwfn *p_hwfn)
495 {
496 	struct qed_spq *p_spq = p_hwfn->p_spq;
497 	struct qed_spq_entry *p_virt = NULL;
498 	dma_addr_t p_phys = 0;
499 	u32 i, capacity;
500 
501 	INIT_LIST_HEAD(&p_spq->pending);
502 	INIT_LIST_HEAD(&p_spq->completion_pending);
503 	INIT_LIST_HEAD(&p_spq->free_pool);
504 	INIT_LIST_HEAD(&p_spq->unlimited_pending);
505 	spin_lock_init(&p_spq->lock);
506 
507 	/* SPQ empty pool */
508 	p_phys	= p_spq->p_phys + offsetof(struct qed_spq_entry, ramrod);
509 	p_virt	= p_spq->p_virt;
510 
511 	capacity = qed_chain_get_capacity(&p_spq->chain);
512 	for (i = 0; i < capacity; i++) {
513 		DMA_REGPAIR_LE(p_virt->elem.data_ptr, p_phys);
514 
515 		list_add_tail(&p_virt->list, &p_spq->free_pool);
516 
517 		p_virt++;
518 		p_phys += sizeof(struct qed_spq_entry);
519 	}
520 
521 	/* Statistics */
522 	p_spq->normal_count		= 0;
523 	p_spq->comp_count		= 0;
524 	p_spq->comp_sent_count		= 0;
525 	p_spq->unlimited_pending_count	= 0;
526 
527 	bitmap_zero(p_spq->p_comp_bitmap, SPQ_RING_SIZE);
528 	p_spq->comp_bitmap_idx = 0;
529 
530 	/* SPQ cid, cannot fail */
531 	qed_cxt_acquire_cid(p_hwfn, PROTOCOLID_CORE, &p_spq->cid);
532 	qed_spq_hw_initialize(p_hwfn, p_spq);
533 
534 	/* reset the chain itself */
535 	qed_chain_reset(&p_spq->chain);
536 }
537 
qed_spq_alloc(struct qed_hwfn * p_hwfn)538 int qed_spq_alloc(struct qed_hwfn *p_hwfn)
539 {
540 	struct qed_spq_entry *p_virt = NULL;
541 	struct qed_spq *p_spq = NULL;
542 	dma_addr_t p_phys = 0;
543 	u32 capacity;
544 
545 	/* SPQ struct */
546 	p_spq = kzalloc(sizeof(struct qed_spq), GFP_KERNEL);
547 	if (!p_spq)
548 		return -ENOMEM;
549 
550 	/* SPQ ring  */
551 	if (qed_chain_alloc(p_hwfn->cdev,
552 			    QED_CHAIN_USE_TO_PRODUCE,
553 			    QED_CHAIN_MODE_SINGLE,
554 			    QED_CHAIN_CNT_TYPE_U16,
555 			    0,   /* N/A when the mode is SINGLE */
556 			    sizeof(struct slow_path_element),
557 			    &p_spq->chain, NULL))
558 		goto spq_allocate_fail;
559 
560 	/* allocate and fill the SPQ elements (incl. ramrod data list) */
561 	capacity = qed_chain_get_capacity(&p_spq->chain);
562 	p_virt = dma_alloc_coherent(&p_hwfn->cdev->pdev->dev,
563 				    capacity * sizeof(struct qed_spq_entry),
564 				    &p_phys, GFP_KERNEL);
565 	if (!p_virt)
566 		goto spq_allocate_fail;
567 
568 	p_spq->p_virt = p_virt;
569 	p_spq->p_phys = p_phys;
570 	p_hwfn->p_spq = p_spq;
571 
572 	return 0;
573 
574 spq_allocate_fail:
575 	qed_chain_free(p_hwfn->cdev, &p_spq->chain);
576 	kfree(p_spq);
577 	return -ENOMEM;
578 }
579 
qed_spq_free(struct qed_hwfn * p_hwfn)580 void qed_spq_free(struct qed_hwfn *p_hwfn)
581 {
582 	struct qed_spq *p_spq = p_hwfn->p_spq;
583 	u32 capacity;
584 
585 	if (!p_spq)
586 		return;
587 
588 	if (p_spq->p_virt) {
589 		capacity = qed_chain_get_capacity(&p_spq->chain);
590 		dma_free_coherent(&p_hwfn->cdev->pdev->dev,
591 				  capacity *
592 				  sizeof(struct qed_spq_entry),
593 				  p_spq->p_virt, p_spq->p_phys);
594 	}
595 
596 	qed_chain_free(p_hwfn->cdev, &p_spq->chain);
597 	kfree(p_spq);
598 	p_hwfn->p_spq = NULL;
599 }
600 
qed_spq_get_entry(struct qed_hwfn * p_hwfn,struct qed_spq_entry ** pp_ent)601 int qed_spq_get_entry(struct qed_hwfn *p_hwfn, struct qed_spq_entry **pp_ent)
602 {
603 	struct qed_spq *p_spq = p_hwfn->p_spq;
604 	struct qed_spq_entry *p_ent = NULL;
605 	int rc = 0;
606 
607 	spin_lock_bh(&p_spq->lock);
608 
609 	if (list_empty(&p_spq->free_pool)) {
610 		p_ent = kzalloc(sizeof(*p_ent), GFP_ATOMIC);
611 		if (!p_ent) {
612 			DP_NOTICE(p_hwfn,
613 				  "Failed to allocate an SPQ entry for a pending ramrod\n");
614 			rc = -ENOMEM;
615 			goto out_unlock;
616 		}
617 		p_ent->queue = &p_spq->unlimited_pending;
618 	} else {
619 		p_ent = list_first_entry(&p_spq->free_pool,
620 					 struct qed_spq_entry, list);
621 		list_del(&p_ent->list);
622 		p_ent->queue = &p_spq->pending;
623 	}
624 
625 	*pp_ent = p_ent;
626 
627 out_unlock:
628 	spin_unlock_bh(&p_spq->lock);
629 	return rc;
630 }
631 
632 /* Locked variant; Should be called while the SPQ lock is taken */
__qed_spq_return_entry(struct qed_hwfn * p_hwfn,struct qed_spq_entry * p_ent)633 static void __qed_spq_return_entry(struct qed_hwfn *p_hwfn,
634 				   struct qed_spq_entry *p_ent)
635 {
636 	list_add_tail(&p_ent->list, &p_hwfn->p_spq->free_pool);
637 }
638 
qed_spq_return_entry(struct qed_hwfn * p_hwfn,struct qed_spq_entry * p_ent)639 void qed_spq_return_entry(struct qed_hwfn *p_hwfn, struct qed_spq_entry *p_ent)
640 {
641 	spin_lock_bh(&p_hwfn->p_spq->lock);
642 	__qed_spq_return_entry(p_hwfn, p_ent);
643 	spin_unlock_bh(&p_hwfn->p_spq->lock);
644 }
645 
646 /**
647  * @brief qed_spq_add_entry - adds a new entry to the pending
648  *        list. Should be used while lock is being held.
649  *
650  * Addes an entry to the pending list is there is room (en empty
651  * element is available in the free_pool), or else places the
652  * entry in the unlimited_pending pool.
653  *
654  * @param p_hwfn
655  * @param p_ent
656  * @param priority
657  *
658  * @return int
659  */
qed_spq_add_entry(struct qed_hwfn * p_hwfn,struct qed_spq_entry * p_ent,enum spq_priority priority)660 static int qed_spq_add_entry(struct qed_hwfn *p_hwfn,
661 			     struct qed_spq_entry *p_ent,
662 			     enum spq_priority priority)
663 {
664 	struct qed_spq *p_spq = p_hwfn->p_spq;
665 
666 	if (p_ent->queue == &p_spq->unlimited_pending) {
667 
668 		if (list_empty(&p_spq->free_pool)) {
669 			list_add_tail(&p_ent->list, &p_spq->unlimited_pending);
670 			p_spq->unlimited_pending_count++;
671 
672 			return 0;
673 		} else {
674 			struct qed_spq_entry *p_en2;
675 
676 			p_en2 = list_first_entry(&p_spq->free_pool,
677 						 struct qed_spq_entry, list);
678 			list_del(&p_en2->list);
679 
680 			/* Copy the ring element physical pointer to the new
681 			 * entry, since we are about to override the entire ring
682 			 * entry and don't want to lose the pointer.
683 			 */
684 			p_ent->elem.data_ptr = p_en2->elem.data_ptr;
685 
686 			*p_en2 = *p_ent;
687 
688 			/* EBLOCK responsible to free the allocated p_ent */
689 			if (p_ent->comp_mode != QED_SPQ_MODE_EBLOCK)
690 				kfree(p_ent);
691 			else
692 				p_ent->post_ent = p_en2;
693 
694 			p_ent = p_en2;
695 		}
696 	}
697 
698 	/* entry is to be placed in 'pending' queue */
699 	switch (priority) {
700 	case QED_SPQ_PRIORITY_NORMAL:
701 		list_add_tail(&p_ent->list, &p_spq->pending);
702 		p_spq->normal_count++;
703 		break;
704 	case QED_SPQ_PRIORITY_HIGH:
705 		list_add(&p_ent->list, &p_spq->pending);
706 		p_spq->high_count++;
707 		break;
708 	default:
709 		return -EINVAL;
710 	}
711 
712 	return 0;
713 }
714 
715 /***************************************************************************
716 * Accessor
717 ***************************************************************************/
qed_spq_get_cid(struct qed_hwfn * p_hwfn)718 u32 qed_spq_get_cid(struct qed_hwfn *p_hwfn)
719 {
720 	if (!p_hwfn->p_spq)
721 		return 0xffffffff;      /* illegal */
722 	return p_hwfn->p_spq->cid;
723 }
724 
725 /***************************************************************************
726 * Posting new Ramrods
727 ***************************************************************************/
qed_spq_post_list(struct qed_hwfn * p_hwfn,struct list_head * head,u32 keep_reserve)728 static int qed_spq_post_list(struct qed_hwfn *p_hwfn,
729 			     struct list_head *head, u32 keep_reserve)
730 {
731 	struct qed_spq *p_spq = p_hwfn->p_spq;
732 	int rc;
733 
734 	while (qed_chain_get_elem_left(&p_spq->chain) > keep_reserve &&
735 	       !list_empty(head)) {
736 		struct qed_spq_entry *p_ent =
737 			list_first_entry(head, struct qed_spq_entry, list);
738 		list_del(&p_ent->list);
739 		list_add_tail(&p_ent->list, &p_spq->completion_pending);
740 		p_spq->comp_sent_count++;
741 
742 		rc = qed_spq_hw_post(p_hwfn, p_spq, p_ent);
743 		if (rc) {
744 			list_del(&p_ent->list);
745 			__qed_spq_return_entry(p_hwfn, p_ent);
746 			return rc;
747 		}
748 	}
749 
750 	return 0;
751 }
752 
qed_spq_pend_post(struct qed_hwfn * p_hwfn)753 int qed_spq_pend_post(struct qed_hwfn *p_hwfn)
754 {
755 	struct qed_spq *p_spq = p_hwfn->p_spq;
756 	struct qed_spq_entry *p_ent = NULL;
757 
758 	while (!list_empty(&p_spq->free_pool)) {
759 		if (list_empty(&p_spq->unlimited_pending))
760 			break;
761 
762 		p_ent = list_first_entry(&p_spq->unlimited_pending,
763 					 struct qed_spq_entry, list);
764 		if (!p_ent)
765 			return -EINVAL;
766 
767 		list_del(&p_ent->list);
768 
769 		qed_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
770 	}
771 
772 	return qed_spq_post_list(p_hwfn, &p_spq->pending,
773 				 SPQ_HIGH_PRI_RESERVE_DEFAULT);
774 }
775 
776 /* Avoid overriding of SPQ entries when getting out-of-order completions, by
777  * marking the completions in a bitmap and increasing the chain consumer only
778  * for the first successive completed entries.
779  */
qed_spq_comp_bmap_update(struct qed_hwfn * p_hwfn,__le16 echo)780 static void qed_spq_comp_bmap_update(struct qed_hwfn *p_hwfn, __le16 echo)
781 {
782 	u16 pos = le16_to_cpu(echo) % SPQ_RING_SIZE;
783 	struct qed_spq *p_spq = p_hwfn->p_spq;
784 
785 	__set_bit(pos, p_spq->p_comp_bitmap);
786 	while (test_bit(p_spq->comp_bitmap_idx,
787 			p_spq->p_comp_bitmap)) {
788 		__clear_bit(p_spq->comp_bitmap_idx,
789 			    p_spq->p_comp_bitmap);
790 		p_spq->comp_bitmap_idx++;
791 		qed_chain_return_produced(&p_spq->chain);
792 	}
793 }
794 
qed_spq_post(struct qed_hwfn * p_hwfn,struct qed_spq_entry * p_ent,u8 * fw_return_code)795 int qed_spq_post(struct qed_hwfn *p_hwfn,
796 		 struct qed_spq_entry *p_ent, u8 *fw_return_code)
797 {
798 	int rc = 0;
799 	struct qed_spq *p_spq = p_hwfn ? p_hwfn->p_spq : NULL;
800 	bool b_ret_ent = true;
801 	bool eblock;
802 
803 	if (!p_hwfn)
804 		return -EINVAL;
805 
806 	if (!p_ent) {
807 		DP_NOTICE(p_hwfn, "Got a NULL pointer\n");
808 		return -EINVAL;
809 	}
810 
811 	/* Complete the entry */
812 	rc = qed_spq_fill_entry(p_hwfn, p_ent);
813 
814 	spin_lock_bh(&p_spq->lock);
815 
816 	/* Check return value after LOCK is taken for cleaner error flow */
817 	if (rc)
818 		goto spq_post_fail;
819 
820 	/* Check if entry is in block mode before qed_spq_add_entry,
821 	 * which might kfree p_ent.
822 	 */
823 	eblock = (p_ent->comp_mode == QED_SPQ_MODE_EBLOCK);
824 
825 	/* Add the request to the pending queue */
826 	rc = qed_spq_add_entry(p_hwfn, p_ent, p_ent->priority);
827 	if (rc)
828 		goto spq_post_fail;
829 
830 	rc = qed_spq_pend_post(p_hwfn);
831 	if (rc) {
832 		/* Since it's possible that pending failed for a different
833 		 * entry [although unlikely], the failed entry was already
834 		 * dealt with; No need to return it here.
835 		 */
836 		b_ret_ent = false;
837 		goto spq_post_fail;
838 	}
839 
840 	spin_unlock_bh(&p_spq->lock);
841 
842 	if (eblock) {
843 		/* For entries in QED BLOCK mode, the completion code cannot
844 		 * perform the necessary cleanup - if it did, we couldn't
845 		 * access p_ent here to see whether it's successful or not.
846 		 * Thus, after gaining the answer perform the cleanup here.
847 		 */
848 		rc = qed_spq_block(p_hwfn, p_ent, fw_return_code,
849 				   p_ent->queue == &p_spq->unlimited_pending);
850 
851 		if (p_ent->queue == &p_spq->unlimited_pending) {
852 			struct qed_spq_entry *p_post_ent = p_ent->post_ent;
853 
854 			kfree(p_ent);
855 
856 			/* Return the entry which was actually posted */
857 			p_ent = p_post_ent;
858 		}
859 
860 		if (rc)
861 			goto spq_post_fail2;
862 
863 		/* return to pool */
864 		qed_spq_return_entry(p_hwfn, p_ent);
865 	}
866 	return rc;
867 
868 spq_post_fail2:
869 	spin_lock_bh(&p_spq->lock);
870 	list_del(&p_ent->list);
871 	qed_spq_comp_bmap_update(p_hwfn, p_ent->elem.hdr.echo);
872 
873 spq_post_fail:
874 	/* return to the free pool */
875 	if (b_ret_ent)
876 		__qed_spq_return_entry(p_hwfn, p_ent);
877 	spin_unlock_bh(&p_spq->lock);
878 
879 	return rc;
880 }
881 
qed_spq_completion(struct qed_hwfn * p_hwfn,__le16 echo,u8 fw_return_code,union event_ring_data * p_data)882 int qed_spq_completion(struct qed_hwfn *p_hwfn,
883 		       __le16 echo,
884 		       u8 fw_return_code,
885 		       union event_ring_data *p_data)
886 {
887 	struct qed_spq		*p_spq;
888 	struct qed_spq_entry	*p_ent = NULL;
889 	struct qed_spq_entry	*tmp;
890 	struct qed_spq_entry	*found = NULL;
891 
892 	if (!p_hwfn)
893 		return -EINVAL;
894 
895 	p_spq = p_hwfn->p_spq;
896 	if (!p_spq)
897 		return -EINVAL;
898 
899 	spin_lock_bh(&p_spq->lock);
900 	list_for_each_entry_safe(p_ent, tmp, &p_spq->completion_pending, list) {
901 		if (p_ent->elem.hdr.echo == echo) {
902 			list_del(&p_ent->list);
903 			qed_spq_comp_bmap_update(p_hwfn, echo);
904 			p_spq->comp_count++;
905 			found = p_ent;
906 			break;
907 		}
908 
909 		/* This is relatively uncommon - depends on scenarios
910 		 * which have mutliple per-PF sent ramrods.
911 		 */
912 		DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
913 			   "Got completion for echo %04x - doesn't match echo %04x in completion pending list\n",
914 			   le16_to_cpu(echo),
915 			   le16_to_cpu(p_ent->elem.hdr.echo));
916 	}
917 
918 	/* Release lock before callback, as callback may post
919 	 * an additional ramrod.
920 	 */
921 	spin_unlock_bh(&p_spq->lock);
922 
923 	if (!found) {
924 		DP_NOTICE(p_hwfn,
925 			  "Failed to find an entry this EQE [echo %04x] completes\n",
926 			  le16_to_cpu(echo));
927 		return -EEXIST;
928 	}
929 
930 	DP_VERBOSE(p_hwfn, QED_MSG_SPQ,
931 		   "Complete EQE [echo %04x]: func %p cookie %p)\n",
932 		   le16_to_cpu(echo),
933 		   p_ent->comp_cb.function, p_ent->comp_cb.cookie);
934 	if (found->comp_cb.function)
935 		found->comp_cb.function(p_hwfn, found->comp_cb.cookie, p_data,
936 					fw_return_code);
937 	else
938 		DP_VERBOSE(p_hwfn,
939 			   QED_MSG_SPQ,
940 			   "Got a completion without a callback function\n");
941 
942 	if (found->comp_mode != QED_SPQ_MODE_EBLOCK)
943 		/* EBLOCK  is responsible for returning its own entry into the
944 		 * free list.
945 		 */
946 		qed_spq_return_entry(p_hwfn, found);
947 
948 	return 0;
949 }
950 
qed_consq_alloc(struct qed_hwfn * p_hwfn)951 int qed_consq_alloc(struct qed_hwfn *p_hwfn)
952 {
953 	struct qed_consq *p_consq;
954 
955 	/* Allocate ConsQ struct */
956 	p_consq = kzalloc(sizeof(*p_consq), GFP_KERNEL);
957 	if (!p_consq)
958 		return -ENOMEM;
959 
960 	/* Allocate and initialize EQ chain*/
961 	if (qed_chain_alloc(p_hwfn->cdev,
962 			    QED_CHAIN_USE_TO_PRODUCE,
963 			    QED_CHAIN_MODE_PBL,
964 			    QED_CHAIN_CNT_TYPE_U16,
965 			    QED_CHAIN_PAGE_SIZE / 0x80,
966 			    0x80, &p_consq->chain, NULL))
967 		goto consq_allocate_fail;
968 
969 	p_hwfn->p_consq = p_consq;
970 	return 0;
971 
972 consq_allocate_fail:
973 	kfree(p_consq);
974 	return -ENOMEM;
975 }
976 
qed_consq_setup(struct qed_hwfn * p_hwfn)977 void qed_consq_setup(struct qed_hwfn *p_hwfn)
978 {
979 	qed_chain_reset(&p_hwfn->p_consq->chain);
980 }
981 
qed_consq_free(struct qed_hwfn * p_hwfn)982 void qed_consq_free(struct qed_hwfn *p_hwfn)
983 {
984 	if (!p_hwfn->p_consq)
985 		return;
986 
987 	qed_chain_free(p_hwfn->cdev, &p_hwfn->p_consq->chain);
988 
989 	kfree(p_hwfn->p_consq);
990 	p_hwfn->p_consq = NULL;
991 }
992