xref: /OK3568_Linux_fs/kernel/drivers/net/ethernet/chelsio/cxgb4/smt.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun  * This file is part of the Chelsio T4/T5/T6 Ethernet driver for Linux.
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Copyright (c) 2017 Chelsio Communications, Inc. All rights reserved.
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * This software is available to you under a choice of one of two
7*4882a593Smuzhiyun  * licenses.  You may choose to be licensed under the terms of the GNU
8*4882a593Smuzhiyun  * General Public License (GPL) Version 2, available from the file
9*4882a593Smuzhiyun  * COPYING in the main directory of this source tree, or the
10*4882a593Smuzhiyun  * OpenIB.org BSD license below:
11*4882a593Smuzhiyun  *
12*4882a593Smuzhiyun  *     Redistribution and use in source and binary forms, with or
13*4882a593Smuzhiyun  *     without modification, are permitted provided that the following
14*4882a593Smuzhiyun  *     conditions are met:
15*4882a593Smuzhiyun  *
16*4882a593Smuzhiyun  *      - Redistributions of source code must retain the above
17*4882a593Smuzhiyun  *        copyright notice, this list of conditions and the following
18*4882a593Smuzhiyun  *        disclaimer.
19*4882a593Smuzhiyun  *
20*4882a593Smuzhiyun  *      - Redistributions in binary form must reproduce the above
21*4882a593Smuzhiyun  *        copyright notice, this list of conditions and the following
22*4882a593Smuzhiyun  *        disclaimer in the documentation and/or other materials
23*4882a593Smuzhiyun  *        provided with the distribution.
24*4882a593Smuzhiyun  *
25*4882a593Smuzhiyun  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26*4882a593Smuzhiyun  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27*4882a593Smuzhiyun  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28*4882a593Smuzhiyun  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29*4882a593Smuzhiyun  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30*4882a593Smuzhiyun  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31*4882a593Smuzhiyun  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32*4882a593Smuzhiyun  * SOFTWARE.
33*4882a593Smuzhiyun  */
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun #include "cxgb4.h"
36*4882a593Smuzhiyun #include "smt.h"
37*4882a593Smuzhiyun #include "t4_msg.h"
38*4882a593Smuzhiyun #include "t4fw_api.h"
39*4882a593Smuzhiyun #include "t4_regs.h"
40*4882a593Smuzhiyun #include "t4_values.h"
41*4882a593Smuzhiyun 
t4_init_smt(void)42*4882a593Smuzhiyun struct smt_data *t4_init_smt(void)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun 	unsigned int smt_size;
45*4882a593Smuzhiyun 	struct smt_data *s;
46*4882a593Smuzhiyun 	int i;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 	smt_size = SMT_SIZE;
49*4882a593Smuzhiyun 
50*4882a593Smuzhiyun 	s = kvzalloc(struct_size(s, smtab, smt_size), GFP_KERNEL);
51*4882a593Smuzhiyun 	if (!s)
52*4882a593Smuzhiyun 		return NULL;
53*4882a593Smuzhiyun 	s->smt_size = smt_size;
54*4882a593Smuzhiyun 	rwlock_init(&s->lock);
55*4882a593Smuzhiyun 	for (i = 0; i < s->smt_size; ++i) {
56*4882a593Smuzhiyun 		s->smtab[i].idx = i;
57*4882a593Smuzhiyun 		s->smtab[i].state = SMT_STATE_UNUSED;
58*4882a593Smuzhiyun 		eth_zero_addr(s->smtab[i].src_mac);
59*4882a593Smuzhiyun 		spin_lock_init(&s->smtab[i].lock);
60*4882a593Smuzhiyun 		s->smtab[i].refcnt = 0;
61*4882a593Smuzhiyun 	}
62*4882a593Smuzhiyun 	return s;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun 
find_or_alloc_smte(struct smt_data * s,u8 * smac)65*4882a593Smuzhiyun static struct smt_entry *find_or_alloc_smte(struct smt_data *s, u8 *smac)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	struct smt_entry *first_free = NULL;
68*4882a593Smuzhiyun 	struct smt_entry *e, *end;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	for (e = &s->smtab[0], end = &s->smtab[s->smt_size]; e != end; ++e) {
71*4882a593Smuzhiyun 		if (e->refcnt == 0) {
72*4882a593Smuzhiyun 			if (!first_free)
73*4882a593Smuzhiyun 				first_free = e;
74*4882a593Smuzhiyun 		} else {
75*4882a593Smuzhiyun 			if (e->state == SMT_STATE_SWITCHING) {
76*4882a593Smuzhiyun 				/* This entry is actually in use. See if we can
77*4882a593Smuzhiyun 				 * re-use it ?
78*4882a593Smuzhiyun 				 */
79*4882a593Smuzhiyun 				if (memcmp(e->src_mac, smac, ETH_ALEN) == 0)
80*4882a593Smuzhiyun 					goto found_reuse;
81*4882a593Smuzhiyun 			}
82*4882a593Smuzhiyun 		}
83*4882a593Smuzhiyun 	}
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	if (first_free) {
86*4882a593Smuzhiyun 		e = first_free;
87*4882a593Smuzhiyun 		goto found;
88*4882a593Smuzhiyun 	}
89*4882a593Smuzhiyun 	return NULL;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun found:
92*4882a593Smuzhiyun 	e->state = SMT_STATE_UNUSED;
93*4882a593Smuzhiyun 
94*4882a593Smuzhiyun found_reuse:
95*4882a593Smuzhiyun 	return e;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun 
t4_smte_free(struct smt_entry * e)98*4882a593Smuzhiyun static void t4_smte_free(struct smt_entry *e)
99*4882a593Smuzhiyun {
100*4882a593Smuzhiyun 	if (e->refcnt == 0) {  /* hasn't been recycled */
101*4882a593Smuzhiyun 		e->state = SMT_STATE_UNUSED;
102*4882a593Smuzhiyun 	}
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun 
105*4882a593Smuzhiyun /**
106*4882a593Smuzhiyun  * cxgb4_smt_release - Release SMT entry
107*4882a593Smuzhiyun  * @e: smt entry to release
108*4882a593Smuzhiyun  *
109*4882a593Smuzhiyun  * Releases ref count and frees up an smt entry from SMT table
110*4882a593Smuzhiyun  */
cxgb4_smt_release(struct smt_entry * e)111*4882a593Smuzhiyun void cxgb4_smt_release(struct smt_entry *e)
112*4882a593Smuzhiyun {
113*4882a593Smuzhiyun 	spin_lock_bh(&e->lock);
114*4882a593Smuzhiyun 	if ((--e->refcnt) == 0)
115*4882a593Smuzhiyun 		t4_smte_free(e);
116*4882a593Smuzhiyun 	spin_unlock_bh(&e->lock);
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun EXPORT_SYMBOL(cxgb4_smt_release);
119*4882a593Smuzhiyun 
do_smt_write_rpl(struct adapter * adap,const struct cpl_smt_write_rpl * rpl)120*4882a593Smuzhiyun void do_smt_write_rpl(struct adapter *adap, const struct cpl_smt_write_rpl *rpl)
121*4882a593Smuzhiyun {
122*4882a593Smuzhiyun 	unsigned int smtidx = TID_TID_G(GET_TID(rpl));
123*4882a593Smuzhiyun 	struct smt_data *s = adap->smt;
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	if (unlikely(rpl->status != CPL_ERR_NONE)) {
126*4882a593Smuzhiyun 		struct smt_entry *e = &s->smtab[smtidx];
127*4882a593Smuzhiyun 
128*4882a593Smuzhiyun 		dev_err(adap->pdev_dev,
129*4882a593Smuzhiyun 			"Unexpected SMT_WRITE_RPL status %u for entry %u\n",
130*4882a593Smuzhiyun 			rpl->status, smtidx);
131*4882a593Smuzhiyun 		spin_lock(&e->lock);
132*4882a593Smuzhiyun 		e->state = SMT_STATE_ERROR;
133*4882a593Smuzhiyun 		spin_unlock(&e->lock);
134*4882a593Smuzhiyun 		return;
135*4882a593Smuzhiyun 	}
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun 
write_smt_entry(struct adapter * adapter,struct smt_entry * e)138*4882a593Smuzhiyun static int write_smt_entry(struct adapter *adapter, struct smt_entry *e)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun 	struct cpl_t6_smt_write_req *t6req;
141*4882a593Smuzhiyun 	struct smt_data *s = adapter->smt;
142*4882a593Smuzhiyun 	struct cpl_smt_write_req *req;
143*4882a593Smuzhiyun 	struct sk_buff *skb;
144*4882a593Smuzhiyun 	int size;
145*4882a593Smuzhiyun 	u8 row;
146*4882a593Smuzhiyun 
147*4882a593Smuzhiyun 	if (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) {
148*4882a593Smuzhiyun 		size = sizeof(*req);
149*4882a593Smuzhiyun 		skb = alloc_skb(size, GFP_ATOMIC);
150*4882a593Smuzhiyun 		if (!skb)
151*4882a593Smuzhiyun 			return -ENOMEM;
152*4882a593Smuzhiyun 		/* Source MAC Table (SMT) contains 256 SMAC entries
153*4882a593Smuzhiyun 		 * organized in 128 rows of 2 entries each.
154*4882a593Smuzhiyun 		 */
155*4882a593Smuzhiyun 		req = (struct cpl_smt_write_req *)__skb_put(skb, size);
156*4882a593Smuzhiyun 		INIT_TP_WR(req, 0);
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 		/* Each row contains an SMAC pair.
159*4882a593Smuzhiyun 		 * LSB selects the SMAC entry within a row
160*4882a593Smuzhiyun 		 */
161*4882a593Smuzhiyun 		row = (e->idx >> 1);
162*4882a593Smuzhiyun 		if (e->idx & 1) {
163*4882a593Smuzhiyun 			req->pfvf1 = 0x0;
164*4882a593Smuzhiyun 			memcpy(req->src_mac1, e->src_mac, ETH_ALEN);
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 			/* fill pfvf0/src_mac0 with entry
167*4882a593Smuzhiyun 			 * at prev index from smt-tab.
168*4882a593Smuzhiyun 			 */
169*4882a593Smuzhiyun 			req->pfvf0 = 0x0;
170*4882a593Smuzhiyun 			memcpy(req->src_mac0, s->smtab[e->idx - 1].src_mac,
171*4882a593Smuzhiyun 			       ETH_ALEN);
172*4882a593Smuzhiyun 		} else {
173*4882a593Smuzhiyun 			req->pfvf0 = 0x0;
174*4882a593Smuzhiyun 			memcpy(req->src_mac0, e->src_mac, ETH_ALEN);
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 			/* fill pfvf1/src_mac1 with entry
177*4882a593Smuzhiyun 			 * at next index from smt-tab
178*4882a593Smuzhiyun 			 */
179*4882a593Smuzhiyun 			req->pfvf1 = 0x0;
180*4882a593Smuzhiyun 			memcpy(req->src_mac1, s->smtab[e->idx + 1].src_mac,
181*4882a593Smuzhiyun 			       ETH_ALEN);
182*4882a593Smuzhiyun 		}
183*4882a593Smuzhiyun 	} else {
184*4882a593Smuzhiyun 		size = sizeof(*t6req);
185*4882a593Smuzhiyun 		skb = alloc_skb(size, GFP_ATOMIC);
186*4882a593Smuzhiyun 		if (!skb)
187*4882a593Smuzhiyun 			return -ENOMEM;
188*4882a593Smuzhiyun 		/* Source MAC Table (SMT) contains 256 SMAC entries */
189*4882a593Smuzhiyun 		t6req = (struct cpl_t6_smt_write_req *)__skb_put(skb, size);
190*4882a593Smuzhiyun 		INIT_TP_WR(t6req, 0);
191*4882a593Smuzhiyun 		req = (struct cpl_smt_write_req *)t6req;
192*4882a593Smuzhiyun 
193*4882a593Smuzhiyun 		/* fill pfvf0/src_mac0 from smt-tab */
194*4882a593Smuzhiyun 		req->pfvf0 = 0x0;
195*4882a593Smuzhiyun 		memcpy(req->src_mac0, s->smtab[e->idx].src_mac, ETH_ALEN);
196*4882a593Smuzhiyun 		row = e->idx;
197*4882a593Smuzhiyun 	}
198*4882a593Smuzhiyun 
199*4882a593Smuzhiyun 	OPCODE_TID(req) =
200*4882a593Smuzhiyun 		htonl(MK_OPCODE_TID(CPL_SMT_WRITE_REQ, e->idx |
201*4882a593Smuzhiyun 				    TID_QID_V(adapter->sge.fw_evtq.abs_id)));
202*4882a593Smuzhiyun 	req->params = htonl(SMTW_NORPL_V(0) |
203*4882a593Smuzhiyun 			    SMTW_IDX_V(row) |
204*4882a593Smuzhiyun 			    SMTW_OVLAN_IDX_V(0));
205*4882a593Smuzhiyun 	t4_mgmt_tx(adapter, skb);
206*4882a593Smuzhiyun 	return 0;
207*4882a593Smuzhiyun }
208*4882a593Smuzhiyun 
t4_smt_alloc_switching(struct adapter * adap,u16 pfvf,u8 * smac)209*4882a593Smuzhiyun static struct smt_entry *t4_smt_alloc_switching(struct adapter *adap, u16 pfvf,
210*4882a593Smuzhiyun 						u8 *smac)
211*4882a593Smuzhiyun {
212*4882a593Smuzhiyun 	struct smt_data *s = adap->smt;
213*4882a593Smuzhiyun 	struct smt_entry *e;
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	write_lock_bh(&s->lock);
216*4882a593Smuzhiyun 	e = find_or_alloc_smte(s, smac);
217*4882a593Smuzhiyun 	if (e) {
218*4882a593Smuzhiyun 		spin_lock(&e->lock);
219*4882a593Smuzhiyun 		if (!e->refcnt) {
220*4882a593Smuzhiyun 			e->refcnt = 1;
221*4882a593Smuzhiyun 			e->state = SMT_STATE_SWITCHING;
222*4882a593Smuzhiyun 			e->pfvf = pfvf;
223*4882a593Smuzhiyun 			memcpy(e->src_mac, smac, ETH_ALEN);
224*4882a593Smuzhiyun 			write_smt_entry(adap, e);
225*4882a593Smuzhiyun 		} else {
226*4882a593Smuzhiyun 			++e->refcnt;
227*4882a593Smuzhiyun 		}
228*4882a593Smuzhiyun 		spin_unlock(&e->lock);
229*4882a593Smuzhiyun 	}
230*4882a593Smuzhiyun 	write_unlock_bh(&s->lock);
231*4882a593Smuzhiyun 	return e;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun 
234*4882a593Smuzhiyun /**
235*4882a593Smuzhiyun  * cxgb4_smt_alloc_switching - Allocates an SMT entry for switch filters.
236*4882a593Smuzhiyun  * @dev: net_device pointer
237*4882a593Smuzhiyun  * @smac: MAC address to add to SMT
238*4882a593Smuzhiyun  * Returns pointer to the SMT entry created
239*4882a593Smuzhiyun  *
240*4882a593Smuzhiyun  * Allocates an SMT entry to be used by switching rule of a filter.
241*4882a593Smuzhiyun  */
cxgb4_smt_alloc_switching(struct net_device * dev,u8 * smac)242*4882a593Smuzhiyun struct smt_entry *cxgb4_smt_alloc_switching(struct net_device *dev, u8 *smac)
243*4882a593Smuzhiyun {
244*4882a593Smuzhiyun 	struct adapter *adap = netdev2adap(dev);
245*4882a593Smuzhiyun 
246*4882a593Smuzhiyun 	return t4_smt_alloc_switching(adap, 0x0, smac);
247*4882a593Smuzhiyun }
248*4882a593Smuzhiyun EXPORT_SYMBOL(cxgb4_smt_alloc_switching);
249