18c2ecf20Sopenharmony_ci/* 28c2ecf20Sopenharmony_ci * This file is part of the Chelsio T4/T5/T6 Ethernet driver for Linux. 38c2ecf20Sopenharmony_ci * 48c2ecf20Sopenharmony_ci * Copyright (c) 2017 Chelsio Communications, Inc. All rights reserved. 58c2ecf20Sopenharmony_ci * 68c2ecf20Sopenharmony_ci * This software is available to you under a choice of one of two 78c2ecf20Sopenharmony_ci * licenses. You may choose to be licensed under the terms of the GNU 88c2ecf20Sopenharmony_ci * General Public License (GPL) Version 2, available from the file 98c2ecf20Sopenharmony_ci * COPYING in the main directory of this source tree, or the 108c2ecf20Sopenharmony_ci * OpenIB.org BSD license below: 118c2ecf20Sopenharmony_ci * 128c2ecf20Sopenharmony_ci * Redistribution and use in source and binary forms, with or 138c2ecf20Sopenharmony_ci * without modification, are permitted provided that the following 148c2ecf20Sopenharmony_ci * conditions are met: 158c2ecf20Sopenharmony_ci * 168c2ecf20Sopenharmony_ci * - Redistributions of source code must retain the above 178c2ecf20Sopenharmony_ci * copyright notice, this list of conditions and the following 188c2ecf20Sopenharmony_ci * disclaimer. 198c2ecf20Sopenharmony_ci * 208c2ecf20Sopenharmony_ci * - Redistributions in binary form must reproduce the above 218c2ecf20Sopenharmony_ci * copyright notice, this list of conditions and the following 228c2ecf20Sopenharmony_ci * disclaimer in the documentation and/or other materials 238c2ecf20Sopenharmony_ci * provided with the distribution. 248c2ecf20Sopenharmony_ci * 258c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 268c2ecf20Sopenharmony_ci * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 278c2ecf20Sopenharmony_ci * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 288c2ecf20Sopenharmony_ci * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 298c2ecf20Sopenharmony_ci * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 308c2ecf20Sopenharmony_ci * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 318c2ecf20Sopenharmony_ci * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 328c2ecf20Sopenharmony_ci * SOFTWARE. 338c2ecf20Sopenharmony_ci */ 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci#include "cxgb4.h" 368c2ecf20Sopenharmony_ci#include "smt.h" 378c2ecf20Sopenharmony_ci#include "t4_msg.h" 388c2ecf20Sopenharmony_ci#include "t4fw_api.h" 398c2ecf20Sopenharmony_ci#include "t4_regs.h" 408c2ecf20Sopenharmony_ci#include "t4_values.h" 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cistruct smt_data *t4_init_smt(void) 438c2ecf20Sopenharmony_ci{ 448c2ecf20Sopenharmony_ci unsigned int smt_size; 458c2ecf20Sopenharmony_ci struct smt_data *s; 468c2ecf20Sopenharmony_ci int i; 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci smt_size = SMT_SIZE; 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci s = kvzalloc(struct_size(s, smtab, smt_size), GFP_KERNEL); 518c2ecf20Sopenharmony_ci if (!s) 528c2ecf20Sopenharmony_ci return NULL; 538c2ecf20Sopenharmony_ci s->smt_size = smt_size; 548c2ecf20Sopenharmony_ci rwlock_init(&s->lock); 558c2ecf20Sopenharmony_ci for (i = 0; i < s->smt_size; ++i) { 568c2ecf20Sopenharmony_ci s->smtab[i].idx = i; 578c2ecf20Sopenharmony_ci s->smtab[i].state = SMT_STATE_UNUSED; 588c2ecf20Sopenharmony_ci eth_zero_addr(s->smtab[i].src_mac); 598c2ecf20Sopenharmony_ci spin_lock_init(&s->smtab[i].lock); 608c2ecf20Sopenharmony_ci s->smtab[i].refcnt = 0; 618c2ecf20Sopenharmony_ci } 628c2ecf20Sopenharmony_ci return s; 638c2ecf20Sopenharmony_ci} 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_cistatic struct smt_entry *find_or_alloc_smte(struct smt_data *s, u8 *smac) 668c2ecf20Sopenharmony_ci{ 678c2ecf20Sopenharmony_ci struct smt_entry *first_free = NULL; 688c2ecf20Sopenharmony_ci struct smt_entry *e, *end; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci for (e = &s->smtab[0], end = &s->smtab[s->smt_size]; e != end; ++e) { 718c2ecf20Sopenharmony_ci if (e->refcnt == 0) { 728c2ecf20Sopenharmony_ci if (!first_free) 738c2ecf20Sopenharmony_ci first_free = e; 748c2ecf20Sopenharmony_ci } else { 758c2ecf20Sopenharmony_ci if (e->state == SMT_STATE_SWITCHING) { 768c2ecf20Sopenharmony_ci /* This entry is actually in use. See if we can 778c2ecf20Sopenharmony_ci * re-use it ? 788c2ecf20Sopenharmony_ci */ 798c2ecf20Sopenharmony_ci if (memcmp(e->src_mac, smac, ETH_ALEN) == 0) 808c2ecf20Sopenharmony_ci goto found_reuse; 818c2ecf20Sopenharmony_ci } 828c2ecf20Sopenharmony_ci } 838c2ecf20Sopenharmony_ci } 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci if (first_free) { 868c2ecf20Sopenharmony_ci e = first_free; 878c2ecf20Sopenharmony_ci goto found; 888c2ecf20Sopenharmony_ci } 898c2ecf20Sopenharmony_ci return NULL; 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_cifound: 928c2ecf20Sopenharmony_ci e->state = SMT_STATE_UNUSED; 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_cifound_reuse: 958c2ecf20Sopenharmony_ci return e; 968c2ecf20Sopenharmony_ci} 978c2ecf20Sopenharmony_ci 988c2ecf20Sopenharmony_cistatic void t4_smte_free(struct smt_entry *e) 998c2ecf20Sopenharmony_ci{ 1008c2ecf20Sopenharmony_ci if (e->refcnt == 0) { /* hasn't been recycled */ 1018c2ecf20Sopenharmony_ci e->state = SMT_STATE_UNUSED; 1028c2ecf20Sopenharmony_ci } 1038c2ecf20Sopenharmony_ci} 1048c2ecf20Sopenharmony_ci 1058c2ecf20Sopenharmony_ci/** 1068c2ecf20Sopenharmony_ci * cxgb4_smt_release - Release SMT entry 1078c2ecf20Sopenharmony_ci * @e: smt entry to release 1088c2ecf20Sopenharmony_ci * 1098c2ecf20Sopenharmony_ci * Releases ref count and frees up an smt entry from SMT table 1108c2ecf20Sopenharmony_ci */ 1118c2ecf20Sopenharmony_civoid cxgb4_smt_release(struct smt_entry *e) 1128c2ecf20Sopenharmony_ci{ 1138c2ecf20Sopenharmony_ci spin_lock_bh(&e->lock); 1148c2ecf20Sopenharmony_ci if ((--e->refcnt) == 0) 1158c2ecf20Sopenharmony_ci t4_smte_free(e); 1168c2ecf20Sopenharmony_ci spin_unlock_bh(&e->lock); 1178c2ecf20Sopenharmony_ci} 1188c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cxgb4_smt_release); 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_civoid do_smt_write_rpl(struct adapter *adap, const struct cpl_smt_write_rpl *rpl) 1218c2ecf20Sopenharmony_ci{ 1228c2ecf20Sopenharmony_ci unsigned int smtidx = TID_TID_G(GET_TID(rpl)); 1238c2ecf20Sopenharmony_ci struct smt_data *s = adap->smt; 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci if (unlikely(rpl->status != CPL_ERR_NONE)) { 1268c2ecf20Sopenharmony_ci struct smt_entry *e = &s->smtab[smtidx]; 1278c2ecf20Sopenharmony_ci 1288c2ecf20Sopenharmony_ci dev_err(adap->pdev_dev, 1298c2ecf20Sopenharmony_ci "Unexpected SMT_WRITE_RPL status %u for entry %u\n", 1308c2ecf20Sopenharmony_ci rpl->status, smtidx); 1318c2ecf20Sopenharmony_ci spin_lock(&e->lock); 1328c2ecf20Sopenharmony_ci e->state = SMT_STATE_ERROR; 1338c2ecf20Sopenharmony_ci spin_unlock(&e->lock); 1348c2ecf20Sopenharmony_ci return; 1358c2ecf20Sopenharmony_ci } 1368c2ecf20Sopenharmony_ci} 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_cistatic int write_smt_entry(struct adapter *adapter, struct smt_entry *e) 1398c2ecf20Sopenharmony_ci{ 1408c2ecf20Sopenharmony_ci struct cpl_t6_smt_write_req *t6req; 1418c2ecf20Sopenharmony_ci struct smt_data *s = adapter->smt; 1428c2ecf20Sopenharmony_ci struct cpl_smt_write_req *req; 1438c2ecf20Sopenharmony_ci struct sk_buff *skb; 1448c2ecf20Sopenharmony_ci int size; 1458c2ecf20Sopenharmony_ci u8 row; 1468c2ecf20Sopenharmony_ci 1478c2ecf20Sopenharmony_ci if (CHELSIO_CHIP_VERSION(adapter->params.chip) <= CHELSIO_T5) { 1488c2ecf20Sopenharmony_ci size = sizeof(*req); 1498c2ecf20Sopenharmony_ci skb = alloc_skb(size, GFP_ATOMIC); 1508c2ecf20Sopenharmony_ci if (!skb) 1518c2ecf20Sopenharmony_ci return -ENOMEM; 1528c2ecf20Sopenharmony_ci /* Source MAC Table (SMT) contains 256 SMAC entries 1538c2ecf20Sopenharmony_ci * organized in 128 rows of 2 entries each. 1548c2ecf20Sopenharmony_ci */ 1558c2ecf20Sopenharmony_ci req = (struct cpl_smt_write_req *)__skb_put(skb, size); 1568c2ecf20Sopenharmony_ci INIT_TP_WR(req, 0); 1578c2ecf20Sopenharmony_ci 1588c2ecf20Sopenharmony_ci /* Each row contains an SMAC pair. 1598c2ecf20Sopenharmony_ci * LSB selects the SMAC entry within a row 1608c2ecf20Sopenharmony_ci */ 1618c2ecf20Sopenharmony_ci row = (e->idx >> 1); 1628c2ecf20Sopenharmony_ci if (e->idx & 1) { 1638c2ecf20Sopenharmony_ci req->pfvf1 = 0x0; 1648c2ecf20Sopenharmony_ci memcpy(req->src_mac1, e->src_mac, ETH_ALEN); 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci /* fill pfvf0/src_mac0 with entry 1678c2ecf20Sopenharmony_ci * at prev index from smt-tab. 1688c2ecf20Sopenharmony_ci */ 1698c2ecf20Sopenharmony_ci req->pfvf0 = 0x0; 1708c2ecf20Sopenharmony_ci memcpy(req->src_mac0, s->smtab[e->idx - 1].src_mac, 1718c2ecf20Sopenharmony_ci ETH_ALEN); 1728c2ecf20Sopenharmony_ci } else { 1738c2ecf20Sopenharmony_ci req->pfvf0 = 0x0; 1748c2ecf20Sopenharmony_ci memcpy(req->src_mac0, e->src_mac, ETH_ALEN); 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci /* fill pfvf1/src_mac1 with entry 1778c2ecf20Sopenharmony_ci * at next index from smt-tab 1788c2ecf20Sopenharmony_ci */ 1798c2ecf20Sopenharmony_ci req->pfvf1 = 0x0; 1808c2ecf20Sopenharmony_ci memcpy(req->src_mac1, s->smtab[e->idx + 1].src_mac, 1818c2ecf20Sopenharmony_ci ETH_ALEN); 1828c2ecf20Sopenharmony_ci } 1838c2ecf20Sopenharmony_ci } else { 1848c2ecf20Sopenharmony_ci size = sizeof(*t6req); 1858c2ecf20Sopenharmony_ci skb = alloc_skb(size, GFP_ATOMIC); 1868c2ecf20Sopenharmony_ci if (!skb) 1878c2ecf20Sopenharmony_ci return -ENOMEM; 1888c2ecf20Sopenharmony_ci /* Source MAC Table (SMT) contains 256 SMAC entries */ 1898c2ecf20Sopenharmony_ci t6req = (struct cpl_t6_smt_write_req *)__skb_put(skb, size); 1908c2ecf20Sopenharmony_ci INIT_TP_WR(t6req, 0); 1918c2ecf20Sopenharmony_ci req = (struct cpl_smt_write_req *)t6req; 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci /* fill pfvf0/src_mac0 from smt-tab */ 1948c2ecf20Sopenharmony_ci req->pfvf0 = 0x0; 1958c2ecf20Sopenharmony_ci memcpy(req->src_mac0, s->smtab[e->idx].src_mac, ETH_ALEN); 1968c2ecf20Sopenharmony_ci row = e->idx; 1978c2ecf20Sopenharmony_ci } 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci OPCODE_TID(req) = 2008c2ecf20Sopenharmony_ci htonl(MK_OPCODE_TID(CPL_SMT_WRITE_REQ, e->idx | 2018c2ecf20Sopenharmony_ci TID_QID_V(adapter->sge.fw_evtq.abs_id))); 2028c2ecf20Sopenharmony_ci req->params = htonl(SMTW_NORPL_V(0) | 2038c2ecf20Sopenharmony_ci SMTW_IDX_V(row) | 2048c2ecf20Sopenharmony_ci SMTW_OVLAN_IDX_V(0)); 2058c2ecf20Sopenharmony_ci t4_mgmt_tx(adapter, skb); 2068c2ecf20Sopenharmony_ci return 0; 2078c2ecf20Sopenharmony_ci} 2088c2ecf20Sopenharmony_ci 2098c2ecf20Sopenharmony_cistatic struct smt_entry *t4_smt_alloc_switching(struct adapter *adap, u16 pfvf, 2108c2ecf20Sopenharmony_ci u8 *smac) 2118c2ecf20Sopenharmony_ci{ 2128c2ecf20Sopenharmony_ci struct smt_data *s = adap->smt; 2138c2ecf20Sopenharmony_ci struct smt_entry *e; 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ci write_lock_bh(&s->lock); 2168c2ecf20Sopenharmony_ci e = find_or_alloc_smte(s, smac); 2178c2ecf20Sopenharmony_ci if (e) { 2188c2ecf20Sopenharmony_ci spin_lock(&e->lock); 2198c2ecf20Sopenharmony_ci if (!e->refcnt) { 2208c2ecf20Sopenharmony_ci e->refcnt = 1; 2218c2ecf20Sopenharmony_ci e->state = SMT_STATE_SWITCHING; 2228c2ecf20Sopenharmony_ci e->pfvf = pfvf; 2238c2ecf20Sopenharmony_ci memcpy(e->src_mac, smac, ETH_ALEN); 2248c2ecf20Sopenharmony_ci write_smt_entry(adap, e); 2258c2ecf20Sopenharmony_ci } else { 2268c2ecf20Sopenharmony_ci ++e->refcnt; 2278c2ecf20Sopenharmony_ci } 2288c2ecf20Sopenharmony_ci spin_unlock(&e->lock); 2298c2ecf20Sopenharmony_ci } 2308c2ecf20Sopenharmony_ci write_unlock_bh(&s->lock); 2318c2ecf20Sopenharmony_ci return e; 2328c2ecf20Sopenharmony_ci} 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci/** 2358c2ecf20Sopenharmony_ci * cxgb4_smt_alloc_switching - Allocates an SMT entry for switch filters. 2368c2ecf20Sopenharmony_ci * @dev: net_device pointer 2378c2ecf20Sopenharmony_ci * @smac: MAC address to add to SMT 2388c2ecf20Sopenharmony_ci * Returns pointer to the SMT entry created 2398c2ecf20Sopenharmony_ci * 2408c2ecf20Sopenharmony_ci * Allocates an SMT entry to be used by switching rule of a filter. 2418c2ecf20Sopenharmony_ci */ 2428c2ecf20Sopenharmony_cistruct smt_entry *cxgb4_smt_alloc_switching(struct net_device *dev, u8 *smac) 2438c2ecf20Sopenharmony_ci{ 2448c2ecf20Sopenharmony_ci struct adapter *adap = netdev2adap(dev); 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci return t4_smt_alloc_switching(adap, 0x0, smac); 2478c2ecf20Sopenharmony_ci} 2488c2ecf20Sopenharmony_ciEXPORT_SYMBOL(cxgb4_smt_alloc_switching); 249