xref: /linux/drivers/net/ethernet/chelsio/cxgb4/cxgb4_uld.c (revision 32a92f8c89326985e05dce8b22d3f0aa07a3e1bd)
1 /*
2  * cxgb4_uld.c:Chelsio Upper Layer Driver Interface for T4/T5/T6 SGE management
3  *
4  * Copyright (c) 2016 Chelsio Communications, Inc. All rights reserved.
5  *
6  * This software is available to you under a choice of one of two
7  * licenses.  You may choose to be licensed under the terms of the GNU
8  * General Public License (GPL) Version 2, available from the file
9  * COPYING in the main directory of this source tree, or the
10  * OpenIB.org BSD license below:
11  *
12  *     Redistribution and use in source and binary forms, with or
13  *     without modification, are permitted provided that the following
14  *     conditions are met:
15  *
16  *      - Redistributions of source code must retain the above
17  *        copyright notice, this list of conditions and the following
18  *        disclaimer.
19  *
20  *      - Redistributions in binary form must reproduce the above
21  *        copyright notice, this list of conditions and the following
22  *        disclaimer in the documentation and/or other materials
23  *        provided with the distribution.
24  *
25  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32  * SOFTWARE.
33  *
34  *  Written by: Atul Gupta (atul.gupta@chelsio.com)
35  *  Written by: Hariprasad Shenai (hariprasad@chelsio.com)
36  */
37 
38 #include <linux/kernel.h>
39 #include <linux/module.h>
40 #include <linux/errno.h>
41 #include <linux/types.h>
42 #include <linux/debugfs.h>
43 #include <linux/export.h>
44 #include <linux/list.h>
45 #include <linux/skbuff.h>
46 #include <linux/pci.h>
47 
48 #include "cxgb4.h"
49 #include "cxgb4_uld.h"
50 #include "t4_regs.h"
51 #include "t4fw_api.h"
52 #include "t4_msg.h"
53 
54 #define for_each_uldrxq(m, i) for (i = 0; i < ((m)->nrxq + (m)->nciq); i++)
55 
56 /* Flush the aggregated lro sessions */
uldrx_flush_handler(struct sge_rspq * q)57 static void uldrx_flush_handler(struct sge_rspq *q)
58 {
59 	struct adapter *adap = q->adap;
60 
61 	if (adap->uld[q->uld].lro_flush)
62 		adap->uld[q->uld].lro_flush(&q->lro_mgr);
63 }
64 
65 /**
66  *	uldrx_handler - response queue handler for ULD queues
67  *	@q: the response queue that received the packet
68  *	@rsp: the response queue descriptor holding the offload message
69  *	@gl: the gather list of packet fragments
70  *
71  *	Deliver an ingress offload packet to a ULD.  All processing is done by
72  *	the ULD, we just maintain statistics.
73  */
uldrx_handler(struct sge_rspq * q,const __be64 * rsp,const struct pkt_gl * gl)74 static int uldrx_handler(struct sge_rspq *q, const __be64 *rsp,
75 			 const struct pkt_gl *gl)
76 {
77 	struct adapter *adap = q->adap;
78 	struct sge_ofld_rxq *rxq = container_of(q, struct sge_ofld_rxq, rspq);
79 	int ret;
80 
81 	/* FW can send CPLs encapsulated in a CPL_FW4_MSG */
82 	if (((const struct rss_header *)rsp)->opcode == CPL_FW4_MSG &&
83 	    ((const struct cpl_fw4_msg *)(rsp + 1))->type == FW_TYPE_RSSCPL)
84 		rsp += 2;
85 
86 	if (q->flush_handler)
87 		ret = adap->uld[q->uld].lro_rx_handler(adap->uld[q->uld].handle,
88 				rsp, gl, &q->lro_mgr,
89 				&q->napi);
90 	else
91 		ret = adap->uld[q->uld].rx_handler(adap->uld[q->uld].handle,
92 				rsp, gl);
93 
94 	if (ret) {
95 		rxq->stats.nomem++;
96 		return -1;
97 	}
98 
99 	if (!gl)
100 		rxq->stats.imm++;
101 	else if (gl == CXGB4_MSG_AN)
102 		rxq->stats.an++;
103 	else
104 		rxq->stats.pkts++;
105 	return 0;
106 }
107 
alloc_uld_rxqs(struct adapter * adap,struct sge_uld_rxq_info * rxq_info,bool lro)108 static int alloc_uld_rxqs(struct adapter *adap,
109 			  struct sge_uld_rxq_info *rxq_info, bool lro)
110 {
111 	unsigned int nq = rxq_info->nrxq + rxq_info->nciq;
112 	struct sge_ofld_rxq *q = rxq_info->uldrxq;
113 	unsigned short *ids = rxq_info->rspq_id;
114 	int i, err, msi_idx, que_idx = 0;
115 	struct sge *s = &adap->sge;
116 	unsigned int per_chan;
117 
118 	per_chan = rxq_info->nrxq / adap->params.nports;
119 
120 	if (adap->flags & CXGB4_USING_MSIX)
121 		msi_idx = 1;
122 	else
123 		msi_idx = -((int)s->intrq.abs_id + 1);
124 
125 	for (i = 0; i < nq; i++, q++) {
126 		if (i == rxq_info->nrxq) {
127 			/* start allocation of concentrator queues */
128 			per_chan = rxq_info->nciq / adap->params.nports;
129 			que_idx = 0;
130 		}
131 
132 		if (msi_idx >= 0) {
133 			msi_idx = cxgb4_get_msix_idx_from_bmap(adap);
134 			if (msi_idx < 0) {
135 				err = -ENOSPC;
136 				goto freeout;
137 			}
138 
139 			snprintf(adap->msix_info[msi_idx].desc,
140 				 sizeof(adap->msix_info[msi_idx].desc),
141 				 "%s-%s%d",
142 				 adap->port[0]->name, rxq_info->name, i);
143 
144 			q->msix = &adap->msix_info[msi_idx];
145 		}
146 		err = t4_sge_alloc_rxq(adap, &q->rspq, false,
147 				       adap->port[que_idx++ / per_chan],
148 				       msi_idx,
149 				       q->fl.size ? &q->fl : NULL,
150 				       uldrx_handler,
151 				       lro ? uldrx_flush_handler : NULL,
152 				       0);
153 		if (err)
154 			goto freeout;
155 
156 		memset(&q->stats, 0, sizeof(q->stats));
157 		if (ids)
158 			ids[i] = q->rspq.abs_id;
159 	}
160 	return 0;
161 freeout:
162 	q = rxq_info->uldrxq;
163 	for ( ; i; i--, q++) {
164 		if (q->rspq.desc)
165 			free_rspq_fl(adap, &q->rspq,
166 				     q->fl.size ? &q->fl : NULL);
167 		if (q->msix)
168 			cxgb4_free_msix_idx_in_bmap(adap, q->msix->idx);
169 	}
170 	return err;
171 }
172 
173 static int
setup_sge_queues_uld(struct adapter * adap,unsigned int uld_type,bool lro)174 setup_sge_queues_uld(struct adapter *adap, unsigned int uld_type, bool lro)
175 {
176 	struct sge_uld_rxq_info *rxq_info = adap->sge.uld_rxq_info[uld_type];
177 	int i, ret;
178 
179 	ret = alloc_uld_rxqs(adap, rxq_info, lro);
180 	if (ret)
181 		return ret;
182 
183 	/* Tell uP to route control queue completions to rdma rspq */
184 	if (adap->flags & CXGB4_FULL_INIT_DONE && uld_type == CXGB4_ULD_RDMA) {
185 		struct sge *s = &adap->sge;
186 		unsigned int cmplqid;
187 		u32 param, cmdop;
188 
189 		cmdop = FW_PARAMS_PARAM_DMAQ_EQ_CMPLIQID_CTRL;
190 		for_each_port(adap, i) {
191 			cmplqid = rxq_info->uldrxq[i].rspq.cntxt_id;
192 			param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
193 				 FW_PARAMS_PARAM_X_V(cmdop) |
194 				 FW_PARAMS_PARAM_YZ_V(s->ctrlq[i].q.cntxt_id));
195 			ret = t4_set_params(adap, adap->mbox, adap->pf,
196 					    0, 1, &param, &cmplqid);
197 		}
198 	}
199 	return ret;
200 }
201 
t4_free_uld_rxqs(struct adapter * adap,int n,struct sge_ofld_rxq * q)202 static void t4_free_uld_rxqs(struct adapter *adap, int n,
203 			     struct sge_ofld_rxq *q)
204 {
205 	for ( ; n; n--, q++) {
206 		if (q->rspq.desc)
207 			free_rspq_fl(adap, &q->rspq,
208 				     q->fl.size ? &q->fl : NULL);
209 	}
210 }
211 
free_sge_queues_uld(struct adapter * adap,unsigned int uld_type)212 static void free_sge_queues_uld(struct adapter *adap, unsigned int uld_type)
213 {
214 	struct sge_uld_rxq_info *rxq_info = adap->sge.uld_rxq_info[uld_type];
215 
216 	if (adap->flags & CXGB4_FULL_INIT_DONE && uld_type == CXGB4_ULD_RDMA) {
217 		struct sge *s = &adap->sge;
218 		u32 param, cmdop, cmplqid = 0;
219 		int i;
220 
221 		cmdop = FW_PARAMS_PARAM_DMAQ_EQ_CMPLIQID_CTRL;
222 		for_each_port(adap, i) {
223 			param = (FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DMAQ) |
224 				 FW_PARAMS_PARAM_X_V(cmdop) |
225 				 FW_PARAMS_PARAM_YZ_V(s->ctrlq[i].q.cntxt_id));
226 			t4_set_params(adap, adap->mbox, adap->pf,
227 				      0, 1, &param, &cmplqid);
228 		}
229 	}
230 
231 	if (rxq_info->nciq)
232 		t4_free_uld_rxqs(adap, rxq_info->nciq,
233 				 rxq_info->uldrxq + rxq_info->nrxq);
234 	t4_free_uld_rxqs(adap, rxq_info->nrxq, rxq_info->uldrxq);
235 }
236 
cfg_queues_uld(struct adapter * adap,unsigned int uld_type,const struct cxgb4_uld_info * uld_info)237 static int cfg_queues_uld(struct adapter *adap, unsigned int uld_type,
238 			  const struct cxgb4_uld_info *uld_info)
239 {
240 	struct sge *s = &adap->sge;
241 	struct sge_uld_rxq_info *rxq_info;
242 	int i, nrxq, ciq_size;
243 
244 	rxq_info = kzalloc_obj(*rxq_info);
245 	if (!rxq_info)
246 		return -ENOMEM;
247 
248 	if (adap->flags & CXGB4_USING_MSIX && uld_info->nrxq > s->nqs_per_uld) {
249 		i = s->nqs_per_uld;
250 		rxq_info->nrxq = roundup(i, adap->params.nports);
251 	} else {
252 		i = min_t(int, uld_info->nrxq,
253 			  num_online_cpus());
254 		rxq_info->nrxq = roundup(i, adap->params.nports);
255 	}
256 	if (!uld_info->ciq) {
257 		rxq_info->nciq = 0;
258 	} else  {
259 		if (adap->flags & CXGB4_USING_MSIX)
260 			rxq_info->nciq = min_t(int, s->nqs_per_uld,
261 					       num_online_cpus());
262 		else
263 			rxq_info->nciq = min_t(int, MAX_OFLD_QSETS,
264 					       num_online_cpus());
265 		rxq_info->nciq = ((rxq_info->nciq / adap->params.nports) *
266 				  adap->params.nports);
267 		rxq_info->nciq = max_t(int, rxq_info->nciq,
268 				       adap->params.nports);
269 	}
270 
271 	nrxq = rxq_info->nrxq + rxq_info->nciq; /* total rxq's */
272 	rxq_info->uldrxq = kzalloc_objs(struct sge_ofld_rxq, nrxq);
273 	if (!rxq_info->uldrxq) {
274 		kfree(rxq_info);
275 		return -ENOMEM;
276 	}
277 
278 	rxq_info->rspq_id = kcalloc(nrxq, sizeof(unsigned short), GFP_KERNEL);
279 	if (!rxq_info->rspq_id) {
280 		kfree(rxq_info->uldrxq);
281 		kfree(rxq_info);
282 		return -ENOMEM;
283 	}
284 
285 	for (i = 0; i < rxq_info->nrxq; i++) {
286 		struct sge_ofld_rxq *r = &rxq_info->uldrxq[i];
287 
288 		init_rspq(adap, &r->rspq, 5, 1, uld_info->rxq_size, 64);
289 		r->rspq.uld = uld_type;
290 		r->fl.size = 72;
291 	}
292 
293 	ciq_size = 64 + adap->vres.cq.size + adap->tids.nftids;
294 	if (ciq_size > SGE_MAX_IQ_SIZE) {
295 		dev_warn(adap->pdev_dev, "CIQ size too small for available IQs\n");
296 		ciq_size = SGE_MAX_IQ_SIZE;
297 	}
298 
299 	for (i = rxq_info->nrxq; i < nrxq; i++) {
300 		struct sge_ofld_rxq *r = &rxq_info->uldrxq[i];
301 
302 		init_rspq(adap, &r->rspq, 5, 1, ciq_size, 64);
303 		r->rspq.uld = uld_type;
304 	}
305 
306 	memcpy(rxq_info->name, uld_info->name, IFNAMSIZ);
307 	adap->sge.uld_rxq_info[uld_type] = rxq_info;
308 
309 	return 0;
310 }
311 
free_queues_uld(struct adapter * adap,unsigned int uld_type)312 static void free_queues_uld(struct adapter *adap, unsigned int uld_type)
313 {
314 	struct sge_uld_rxq_info *rxq_info = adap->sge.uld_rxq_info[uld_type];
315 
316 	adap->sge.uld_rxq_info[uld_type] = NULL;
317 	kfree(rxq_info->rspq_id);
318 	kfree(rxq_info->uldrxq);
319 	kfree(rxq_info);
320 }
321 
322 static int
request_msix_queue_irqs_uld(struct adapter * adap,unsigned int uld_type)323 request_msix_queue_irqs_uld(struct adapter *adap, unsigned int uld_type)
324 {
325 	struct sge_uld_rxq_info *rxq_info = adap->sge.uld_rxq_info[uld_type];
326 	struct msix_info *minfo;
327 	unsigned int idx;
328 	int err = 0;
329 
330 	for_each_uldrxq(rxq_info, idx) {
331 		minfo = rxq_info->uldrxq[idx].msix;
332 		err = request_irq(minfo->vec,
333 				  t4_sge_intr_msix, 0,
334 				  minfo->desc,
335 				  &rxq_info->uldrxq[idx].rspq);
336 		if (err)
337 			goto unwind;
338 
339 		cxgb4_set_msix_aff(adap, minfo->vec,
340 				   &minfo->aff_mask, idx);
341 	}
342 	return 0;
343 
344 unwind:
345 	while (idx-- > 0) {
346 		minfo = rxq_info->uldrxq[idx].msix;
347 		cxgb4_clear_msix_aff(minfo->vec, minfo->aff_mask);
348 		cxgb4_free_msix_idx_in_bmap(adap, minfo->idx);
349 		free_irq(minfo->vec, &rxq_info->uldrxq[idx].rspq);
350 	}
351 	return err;
352 }
353 
354 static void
free_msix_queue_irqs_uld(struct adapter * adap,unsigned int uld_type)355 free_msix_queue_irqs_uld(struct adapter *adap, unsigned int uld_type)
356 {
357 	struct sge_uld_rxq_info *rxq_info = adap->sge.uld_rxq_info[uld_type];
358 	struct msix_info *minfo;
359 	unsigned int idx;
360 
361 	for_each_uldrxq(rxq_info, idx) {
362 		minfo = rxq_info->uldrxq[idx].msix;
363 		cxgb4_clear_msix_aff(minfo->vec, minfo->aff_mask);
364 		cxgb4_free_msix_idx_in_bmap(adap, minfo->idx);
365 		free_irq(minfo->vec, &rxq_info->uldrxq[idx].rspq);
366 	}
367 }
368 
enable_rx_uld(struct adapter * adap,unsigned int uld_type)369 static void enable_rx_uld(struct adapter *adap, unsigned int uld_type)
370 {
371 	struct sge_uld_rxq_info *rxq_info = adap->sge.uld_rxq_info[uld_type];
372 	int idx;
373 
374 	for_each_uldrxq(rxq_info, idx) {
375 		struct sge_rspq *q = &rxq_info->uldrxq[idx].rspq;
376 
377 		if (!q)
378 			continue;
379 
380 		cxgb4_enable_rx(adap, q);
381 	}
382 }
383 
quiesce_rx_uld(struct adapter * adap,unsigned int uld_type)384 static void quiesce_rx_uld(struct adapter *adap, unsigned int uld_type)
385 {
386 	struct sge_uld_rxq_info *rxq_info = adap->sge.uld_rxq_info[uld_type];
387 	int idx;
388 
389 	for_each_uldrxq(rxq_info, idx) {
390 		struct sge_rspq *q = &rxq_info->uldrxq[idx].rspq;
391 
392 		if (!q)
393 			continue;
394 
395 		cxgb4_quiesce_rx(q);
396 	}
397 }
398 
399 static void
free_sge_txq_uld(struct adapter * adap,struct sge_uld_txq_info * txq_info)400 free_sge_txq_uld(struct adapter *adap, struct sge_uld_txq_info *txq_info)
401 {
402 	int nq = txq_info->ntxq;
403 	int i;
404 
405 	for (i = 0; i < nq; i++) {
406 		struct sge_uld_txq *txq = &txq_info->uldtxq[i];
407 
408 		if (txq->q.desc) {
409 			tasklet_kill(&txq->qresume_tsk);
410 			t4_ofld_eq_free(adap, adap->mbox, adap->pf, 0,
411 					txq->q.cntxt_id);
412 			free_tx_desc(adap, &txq->q, txq->q.in_use, false);
413 			kfree(txq->q.sdesc);
414 			__skb_queue_purge(&txq->sendq);
415 			free_txq(adap, &txq->q);
416 		}
417 	}
418 }
419 
420 static int
alloc_sge_txq_uld(struct adapter * adap,struct sge_uld_txq_info * txq_info,unsigned int uld_type)421 alloc_sge_txq_uld(struct adapter *adap, struct sge_uld_txq_info *txq_info,
422 		  unsigned int uld_type)
423 {
424 	struct sge *s = &adap->sge;
425 	int nq = txq_info->ntxq;
426 	int i, j, err;
427 
428 	j = nq / adap->params.nports;
429 	for (i = 0; i < nq; i++) {
430 		struct sge_uld_txq *txq = &txq_info->uldtxq[i];
431 
432 		txq->q.size = 1024;
433 		err = t4_sge_alloc_uld_txq(adap, txq, adap->port[i / j],
434 					   s->fw_evtq.cntxt_id, uld_type);
435 		if (err)
436 			goto freeout;
437 	}
438 	return 0;
439 freeout:
440 	free_sge_txq_uld(adap, txq_info);
441 	return err;
442 }
443 
444 static void
release_sge_txq_uld(struct adapter * adap,unsigned int uld_type)445 release_sge_txq_uld(struct adapter *adap, unsigned int uld_type)
446 {
447 	struct sge_uld_txq_info *txq_info = NULL;
448 	int tx_uld_type = TX_ULD(uld_type);
449 
450 	txq_info = adap->sge.uld_txq_info[tx_uld_type];
451 
452 	if (txq_info && atomic_dec_and_test(&txq_info->users)) {
453 		free_sge_txq_uld(adap, txq_info);
454 		kfree(txq_info->uldtxq);
455 		kfree(txq_info);
456 		adap->sge.uld_txq_info[tx_uld_type] = NULL;
457 	}
458 }
459 
460 static int
setup_sge_txq_uld(struct adapter * adap,unsigned int uld_type,const struct cxgb4_uld_info * uld_info)461 setup_sge_txq_uld(struct adapter *adap, unsigned int uld_type,
462 		  const struct cxgb4_uld_info *uld_info)
463 {
464 	struct sge_uld_txq_info *txq_info = NULL;
465 	int tx_uld_type, i;
466 
467 	tx_uld_type = TX_ULD(uld_type);
468 	txq_info = adap->sge.uld_txq_info[tx_uld_type];
469 
470 	if ((tx_uld_type == CXGB4_TX_OFLD) && txq_info &&
471 	    (atomic_inc_return(&txq_info->users) > 1))
472 		return 0;
473 
474 	txq_info = kzalloc_obj(*txq_info);
475 	if (!txq_info)
476 		return -ENOMEM;
477 	if (uld_type == CXGB4_ULD_CRYPTO) {
478 		i = min_t(int, adap->vres.ncrypto_fc,
479 			  num_online_cpus());
480 		txq_info->ntxq = rounddown(i, adap->params.nports);
481 		if (txq_info->ntxq <= 0) {
482 			dev_warn(adap->pdev_dev, "Crypto Tx Queues can't be zero\n");
483 			kfree(txq_info);
484 			return -EINVAL;
485 		}
486 
487 	} else {
488 		i = min_t(int, uld_info->ntxq, num_online_cpus());
489 		txq_info->ntxq = roundup(i, adap->params.nports);
490 	}
491 	txq_info->uldtxq = kzalloc_objs(struct sge_uld_txq, txq_info->ntxq);
492 	if (!txq_info->uldtxq) {
493 		kfree(txq_info);
494 		return -ENOMEM;
495 	}
496 
497 	if (alloc_sge_txq_uld(adap, txq_info, tx_uld_type)) {
498 		kfree(txq_info->uldtxq);
499 		kfree(txq_info);
500 		return -ENOMEM;
501 	}
502 
503 	atomic_inc(&txq_info->users);
504 	adap->sge.uld_txq_info[tx_uld_type] = txq_info;
505 	return 0;
506 }
507 
uld_queue_init(struct adapter * adap,unsigned int uld_type,struct cxgb4_lld_info * lli)508 static void uld_queue_init(struct adapter *adap, unsigned int uld_type,
509 			   struct cxgb4_lld_info *lli)
510 {
511 	struct sge_uld_rxq_info *rxq_info = adap->sge.uld_rxq_info[uld_type];
512 	int tx_uld_type = TX_ULD(uld_type);
513 	struct sge_uld_txq_info *txq_info = adap->sge.uld_txq_info[tx_uld_type];
514 
515 	lli->rxq_ids = rxq_info->rspq_id;
516 	lli->nrxq = rxq_info->nrxq;
517 	lli->ciq_ids = rxq_info->rspq_id + rxq_info->nrxq;
518 	lli->nciq = rxq_info->nciq;
519 	lli->ntxq = txq_info->ntxq;
520 }
521 
t4_uld_mem_alloc(struct adapter * adap)522 int t4_uld_mem_alloc(struct adapter *adap)
523 {
524 	struct sge *s = &adap->sge;
525 
526 	adap->uld = kzalloc_objs(*adap->uld, CXGB4_ULD_MAX);
527 	if (!adap->uld)
528 		return -ENOMEM;
529 
530 	s->uld_rxq_info = kzalloc_objs(struct sge_uld_rxq_info *, CXGB4_ULD_MAX);
531 	if (!s->uld_rxq_info)
532 		goto err_uld;
533 
534 	s->uld_txq_info = kzalloc_objs(struct sge_uld_txq_info *, CXGB4_TX_MAX);
535 	if (!s->uld_txq_info)
536 		goto err_uld_rx;
537 	return 0;
538 
539 err_uld_rx:
540 	kfree(s->uld_rxq_info);
541 err_uld:
542 	kfree(adap->uld);
543 	return -ENOMEM;
544 }
545 
t4_uld_mem_free(struct adapter * adap)546 void t4_uld_mem_free(struct adapter *adap)
547 {
548 	struct sge *s = &adap->sge;
549 
550 	kfree(s->uld_txq_info);
551 	kfree(s->uld_rxq_info);
552 	kfree(adap->uld);
553 }
554 
555 /* This function should be called with uld_mutex taken. */
cxgb4_shutdown_uld_adapter(struct adapter * adap,enum cxgb4_uld type)556 static void cxgb4_shutdown_uld_adapter(struct adapter *adap, enum cxgb4_uld type)
557 {
558 	if (adap->uld[type].handle) {
559 		adap->uld[type].handle = NULL;
560 		adap->uld[type].add = NULL;
561 		release_sge_txq_uld(adap, type);
562 
563 		if (adap->flags & CXGB4_FULL_INIT_DONE)
564 			quiesce_rx_uld(adap, type);
565 
566 		if (adap->flags & CXGB4_USING_MSIX)
567 			free_msix_queue_irqs_uld(adap, type);
568 
569 		free_sge_queues_uld(adap, type);
570 		free_queues_uld(adap, type);
571 	}
572 }
573 
t4_uld_clean_up(struct adapter * adap)574 void t4_uld_clean_up(struct adapter *adap)
575 {
576 	unsigned int i;
577 
578 	if (!is_uld(adap))
579 		return;
580 
581 	mutex_lock(&uld_mutex);
582 	for (i = 0; i < CXGB4_ULD_MAX; i++) {
583 		if (!adap->uld[i].handle)
584 			continue;
585 
586 		cxgb4_shutdown_uld_adapter(adap, i);
587 	}
588 	mutex_unlock(&uld_mutex);
589 }
590 
uld_init(struct adapter * adap,struct cxgb4_lld_info * lld)591 static void uld_init(struct adapter *adap, struct cxgb4_lld_info *lld)
592 {
593 	int i;
594 
595 	lld->pdev = adap->pdev;
596 	lld->pf = adap->pf;
597 	lld->l2t = adap->l2t;
598 	lld->tids = &adap->tids;
599 	lld->ports = adap->port;
600 	lld->vr = &adap->vres;
601 	lld->mtus = adap->params.mtus;
602 	lld->nchan = adap->params.nports;
603 	lld->nports = adap->params.nports;
604 	lld->wr_cred = adap->params.ofldq_wr_cred;
605 	lld->crypto = adap->params.crypto;
606 	lld->iscsi_iolen = MAXRXDATA_G(t4_read_reg(adap, TP_PARA_REG2_A));
607 	lld->iscsi_tagmask = t4_read_reg(adap, ULP_RX_ISCSI_TAGMASK_A);
608 	lld->iscsi_pgsz_order = t4_read_reg(adap, ULP_RX_ISCSI_PSZ_A);
609 	lld->iscsi_llimit = t4_read_reg(adap, ULP_RX_ISCSI_LLIMIT_A);
610 	lld->iscsi_ppm = &adap->iscsi_ppm;
611 	lld->adapter_type = adap->params.chip;
612 	lld->cclk_ps = 1000000000 / adap->params.vpd.cclk;
613 	lld->udb_density = 1 << adap->params.sge.eq_qpp;
614 	lld->ucq_density = 1 << adap->params.sge.iq_qpp;
615 	lld->sge_host_page_size = 1 << (adap->params.sge.hps + 10);
616 	lld->filt_mode = adap->params.tp.vlan_pri_map;
617 	/* MODQ_REQ_MAP sets queues 0-3 to chan 0-3 */
618 	for (i = 0; i < NCHAN; i++)
619 		lld->tx_modq[i] = i;
620 	lld->gts_reg = adap->regs + MYPF_REG(SGE_PF_GTS_A);
621 	lld->db_reg = adap->regs + MYPF_REG(SGE_PF_KDOORBELL_A);
622 	lld->fw_vers = adap->params.fw_vers;
623 	lld->dbfifo_int_thresh = dbfifo_int_thresh;
624 	lld->sge_ingpadboundary = adap->sge.fl_align;
625 	lld->sge_egrstatuspagesize = adap->sge.stat_len;
626 	lld->sge_pktshift = adap->sge.pktshift;
627 	lld->ulp_crypto = adap->params.crypto;
628 	lld->enable_fw_ofld_conn = adap->flags & CXGB4_FW_OFLD_CONN;
629 	lld->max_ordird_qp = adap->params.max_ordird_qp;
630 	lld->max_ird_adapter = adap->params.max_ird_adapter;
631 	lld->ulptx_memwrite_dsgl = adap->params.ulptx_memwrite_dsgl;
632 	lld->nodeid = dev_to_node(adap->pdev_dev);
633 	lld->fr_nsmr_tpte_wr_support = adap->params.fr_nsmr_tpte_wr_support;
634 	lld->write_w_imm_support = adap->params.write_w_imm_support;
635 	lld->write_cmpl_support = adap->params.write_cmpl_support;
636 }
637 
uld_attach(struct adapter * adap,unsigned int uld)638 static int uld_attach(struct adapter *adap, unsigned int uld)
639 {
640 	struct cxgb4_lld_info lli;
641 	void *handle;
642 
643 	uld_init(adap, &lli);
644 	uld_queue_init(adap, uld, &lli);
645 
646 	handle = adap->uld[uld].add(&lli);
647 	if (IS_ERR(handle)) {
648 		dev_warn(adap->pdev_dev,
649 			 "could not attach to the %s driver, error %ld\n",
650 			 adap->uld[uld].name, PTR_ERR(handle));
651 		return PTR_ERR(handle);
652 	}
653 
654 	adap->uld[uld].handle = handle;
655 	t4_register_netevent_notifier();
656 
657 	if (adap->flags & CXGB4_FULL_INIT_DONE)
658 		adap->uld[uld].state_change(handle, CXGB4_STATE_UP);
659 
660 	return 0;
661 }
662 
663 #if IS_ENABLED(CONFIG_CHELSIO_TLS_DEVICE)
cxgb4_uld_in_use(struct adapter * adap)664 static bool cxgb4_uld_in_use(struct adapter *adap)
665 {
666 	const struct tid_info *t = &adap->tids;
667 
668 	return (atomic_read(&t->conns_in_use) || t->stids_in_use);
669 }
670 
671 /* cxgb4_set_ktls_feature: request FW to enable/disable ktls settings.
672  * @adap: adapter info
673  * @enable: 1 to enable / 0 to disable ktls settings.
674  */
cxgb4_set_ktls_feature(struct adapter * adap,bool enable)675 int cxgb4_set_ktls_feature(struct adapter *adap, bool enable)
676 {
677 	int ret = 0;
678 	u32 params =
679 		FW_PARAMS_MNEM_V(FW_PARAMS_MNEM_DEV) |
680 		FW_PARAMS_PARAM_X_V(FW_PARAMS_PARAM_DEV_KTLS_HW) |
681 		FW_PARAMS_PARAM_Y_V(enable) |
682 		FW_PARAMS_PARAM_Z_V(FW_PARAMS_PARAM_DEV_KTLS_HW_USER_ENABLE);
683 
684 	if (enable) {
685 		if (!refcount_read(&adap->chcr_ktls.ktls_refcount)) {
686 			/* At this moment if ULD connection are up means, other
687 			 * ULD is/are already active, return failure.
688 			 */
689 			if (cxgb4_uld_in_use(adap)) {
690 				dev_dbg(adap->pdev_dev,
691 					"ULD connections (tid/stid) active. Can't enable kTLS\n");
692 				return -EINVAL;
693 			}
694 			ret = t4_set_params(adap, adap->mbox, adap->pf,
695 					    0, 1, &params, &params);
696 			if (ret)
697 				return ret;
698 			refcount_set(&adap->chcr_ktls.ktls_refcount, 1);
699 			pr_debug("kTLS has been enabled. Restrictions placed on ULD support\n");
700 		} else {
701 			/* ktls settings already up, just increment refcount. */
702 			refcount_inc(&adap->chcr_ktls.ktls_refcount);
703 		}
704 	} else {
705 		/* return failure if refcount is already 0. */
706 		if (!refcount_read(&adap->chcr_ktls.ktls_refcount))
707 			return -EINVAL;
708 		/* decrement refcount and test, if 0, disable ktls feature,
709 		 * else return command success.
710 		 */
711 		if (refcount_dec_and_test(&adap->chcr_ktls.ktls_refcount)) {
712 			ret = t4_set_params(adap, adap->mbox, adap->pf,
713 					    0, 1, &params, &params);
714 			if (ret)
715 				return ret;
716 			pr_debug("kTLS is disabled. Restrictions on ULD support removed\n");
717 		}
718 	}
719 
720 	return ret;
721 }
722 #endif
723 
cxgb4_uld_alloc_resources(struct adapter * adap,enum cxgb4_uld type,const struct cxgb4_uld_info * p)724 static void cxgb4_uld_alloc_resources(struct adapter *adap,
725 				      enum cxgb4_uld type,
726 				      const struct cxgb4_uld_info *p)
727 {
728 	int ret = 0;
729 
730 	if ((type == CXGB4_ULD_CRYPTO && !is_pci_uld(adap)) ||
731 	    (type != CXGB4_ULD_CRYPTO && !is_offload(adap)))
732 		return;
733 	if (type == CXGB4_ULD_ISCSIT && is_t4(adap->params.chip))
734 		return;
735 	ret = cfg_queues_uld(adap, type, p);
736 	if (ret)
737 		goto out;
738 	ret = setup_sge_queues_uld(adap, type, p->lro);
739 	if (ret)
740 		goto free_queues;
741 	if (adap->flags & CXGB4_USING_MSIX) {
742 		ret = request_msix_queue_irqs_uld(adap, type);
743 		if (ret)
744 			goto free_rxq;
745 	}
746 	if (adap->flags & CXGB4_FULL_INIT_DONE)
747 		enable_rx_uld(adap, type);
748 	if (adap->uld[type].add)
749 		goto free_irq;
750 	ret = setup_sge_txq_uld(adap, type, p);
751 	if (ret)
752 		goto free_irq;
753 	adap->uld[type] = *p;
754 	ret = uld_attach(adap, type);
755 	if (ret)
756 		goto free_txq;
757 	return;
758 free_txq:
759 	release_sge_txq_uld(adap, type);
760 free_irq:
761 	if (adap->flags & CXGB4_FULL_INIT_DONE)
762 		quiesce_rx_uld(adap, type);
763 	if (adap->flags & CXGB4_USING_MSIX)
764 		free_msix_queue_irqs_uld(adap, type);
765 free_rxq:
766 	free_sge_queues_uld(adap, type);
767 free_queues:
768 	free_queues_uld(adap, type);
769 out:
770 	dev_warn(adap->pdev_dev,
771 		 "ULD registration failed for uld type %d\n", type);
772 }
773 
cxgb4_uld_enable(struct adapter * adap)774 void cxgb4_uld_enable(struct adapter *adap)
775 {
776 	struct cxgb4_uld_list *uld_entry;
777 
778 	mutex_lock(&uld_mutex);
779 	list_add_tail(&adap->list_node, &adapter_list);
780 	list_for_each_entry(uld_entry, &uld_list, list_node)
781 		cxgb4_uld_alloc_resources(adap, uld_entry->uld_type,
782 					  &uld_entry->uld_info);
783 	mutex_unlock(&uld_mutex);
784 }
785 
786 /* cxgb4_register_uld - register an upper-layer driver
787  * @type: the ULD type
788  * @p: the ULD methods
789  *
790  * Registers an upper-layer driver with this driver and notifies the ULD
791  * about any presently available devices that support its type.
792  */
cxgb4_register_uld(enum cxgb4_uld type,const struct cxgb4_uld_info * p)793 void cxgb4_register_uld(enum cxgb4_uld type,
794 			const struct cxgb4_uld_info *p)
795 {
796 	struct cxgb4_uld_list *uld_entry;
797 	struct adapter *adap;
798 
799 	if (type >= CXGB4_ULD_MAX)
800 		return;
801 
802 	uld_entry = kzalloc_obj(*uld_entry);
803 	if (!uld_entry)
804 		return;
805 
806 	memcpy(&uld_entry->uld_info, p, sizeof(struct cxgb4_uld_info));
807 	mutex_lock(&uld_mutex);
808 	list_for_each_entry(adap, &adapter_list, list_node)
809 		cxgb4_uld_alloc_resources(adap, type, p);
810 
811 	uld_entry->uld_type = type;
812 	list_add_tail(&uld_entry->list_node, &uld_list);
813 	mutex_unlock(&uld_mutex);
814 	return;
815 }
816 EXPORT_SYMBOL(cxgb4_register_uld);
817 
818 /**
819  *	cxgb4_unregister_uld - unregister an upper-layer driver
820  *	@type: the ULD type
821  *
822  *	Unregisters an existing upper-layer driver.
823  */
cxgb4_unregister_uld(enum cxgb4_uld type)824 int cxgb4_unregister_uld(enum cxgb4_uld type)
825 {
826 	struct cxgb4_uld_list *uld_entry, *tmp;
827 	struct adapter *adap;
828 
829 	if (type >= CXGB4_ULD_MAX)
830 		return -EINVAL;
831 
832 	mutex_lock(&uld_mutex);
833 	list_for_each_entry(adap, &adapter_list, list_node) {
834 		if ((type == CXGB4_ULD_CRYPTO && !is_pci_uld(adap)) ||
835 		    (type != CXGB4_ULD_CRYPTO && !is_offload(adap)))
836 			continue;
837 		if (type == CXGB4_ULD_ISCSIT && is_t4(adap->params.chip))
838 			continue;
839 
840 		cxgb4_shutdown_uld_adapter(adap, type);
841 	}
842 
843 	list_for_each_entry_safe(uld_entry, tmp, &uld_list, list_node) {
844 		if (uld_entry->uld_type == type) {
845 			list_del(&uld_entry->list_node);
846 			kfree(uld_entry);
847 		}
848 	}
849 	mutex_unlock(&uld_mutex);
850 
851 	return 0;
852 }
853 EXPORT_SYMBOL(cxgb4_unregister_uld);
854