blob: 91d02eb51b8bb2c98c34ce92f9be33c5ecc8e973 [file] [log] [blame]
Divy Le Ray4d22de32007-01-18 22:04:14 -05001/*
Divy Le Raya02d44a2008-10-13 18:47:30 -07002 * Copyright (c) 2006-2008 Chelsio, Inc. All rights reserved.
Divy Le Ray4d22de32007-01-18 22:04:14 -05003 *
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
Joe Perches428ac432013-01-06 13:34:49 +000033#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
34
Divy Le Ray4d22de32007-01-18 22:04:14 -050035#include <linux/list.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Divy Le Ray4d22de32007-01-18 22:04:14 -050037#include <net/neighbour.h>
38#include <linux/notifier.h>
Arun Sharma600634972011-07-26 16:09:06 -070039#include <linux/atomic.h>
Divy Le Ray4d22de32007-01-18 22:04:14 -050040#include <linux/proc_fs.h>
41#include <linux/if_vlan.h>
42#include <net/netevent.h>
43#include <linux/highmem.h>
44#include <linux/vmalloc.h>
Paul Gortmakeree40fa02011-05-27 16:14:23 -040045#include <linux/export.h>
Divy Le Ray4d22de32007-01-18 22:04:14 -050046
47#include "common.h"
48#include "regs.h"
49#include "cxgb3_ioctl.h"
50#include "cxgb3_ctl_defs.h"
51#include "cxgb3_defs.h"
52#include "l2t.h"
53#include "firmware_exports.h"
54#include "cxgb3_offload.h"
55
56static LIST_HEAD(client_list);
57static LIST_HEAD(ofld_dev_list);
58static DEFINE_MUTEX(cxgb3_db_lock);
59
60static DEFINE_RWLOCK(adapter_list_lock);
61static LIST_HEAD(adapter_list);
62
63static const unsigned int MAX_ATIDS = 64 * 1024;
Divy Le Rayc9a6ce52007-08-21 20:49:26 -070064static const unsigned int ATID_BASE = 0x10000;
Divy Le Ray4d22de32007-01-18 22:04:14 -050065
stephen hemmingera5190b42010-10-15 12:43:10 +000066static void cxgb_neigh_update(struct neighbour *neigh);
David S. Miller1d248b12012-07-03 01:01:51 -070067static void cxgb_redirect(struct dst_entry *old, struct neighbour *old_neigh,
David S. Miller534cb282012-07-02 22:35:31 -070068 struct dst_entry *new, struct neighbour *new_neigh,
69 const void *daddr);
stephen hemmingera5190b42010-10-15 12:43:10 +000070
Divy Le Ray4d22de32007-01-18 22:04:14 -050071static inline int offload_activated(struct t3cdev *tdev)
72{
73 const struct adapter *adapter = tdev2adap(tdev);
74
Eric Dumazet807540b2010-09-23 05:40:09 +000075 return test_bit(OFFLOAD_DEVMAP_BIT, &adapter->open_device_map);
Divy Le Ray4d22de32007-01-18 22:04:14 -050076}
77
78/**
79 * cxgb3_register_client - register an offload client
80 * @client: the client
81 *
82 * Add the client to the client list,
83 * and call backs the client for each activated offload device
84 */
85void cxgb3_register_client(struct cxgb3_client *client)
86{
87 struct t3cdev *tdev;
88
89 mutex_lock(&cxgb3_db_lock);
90 list_add_tail(&client->client_list, &client_list);
91
92 if (client->add) {
93 list_for_each_entry(tdev, &ofld_dev_list, ofld_dev_list) {
94 if (offload_activated(tdev))
95 client->add(tdev);
96 }
97 }
98 mutex_unlock(&cxgb3_db_lock);
99}
100
101EXPORT_SYMBOL(cxgb3_register_client);
102
103/**
104 * cxgb3_unregister_client - unregister an offload client
105 * @client: the client
106 *
107 * Remove the client to the client list,
108 * and call backs the client for each activated offload device.
109 */
110void cxgb3_unregister_client(struct cxgb3_client *client)
111{
112 struct t3cdev *tdev;
113
114 mutex_lock(&cxgb3_db_lock);
115 list_del(&client->client_list);
116
117 if (client->remove) {
118 list_for_each_entry(tdev, &ofld_dev_list, ofld_dev_list) {
119 if (offload_activated(tdev))
120 client->remove(tdev);
121 }
122 }
123 mutex_unlock(&cxgb3_db_lock);
124}
125
126EXPORT_SYMBOL(cxgb3_unregister_client);
127
128/**
129 * cxgb3_add_clients - activate registered clients for an offload device
130 * @tdev: the offload device
131 *
132 * Call backs all registered clients once a offload device is activated
133 */
134void cxgb3_add_clients(struct t3cdev *tdev)
135{
136 struct cxgb3_client *client;
137
138 mutex_lock(&cxgb3_db_lock);
139 list_for_each_entry(client, &client_list, client_list) {
140 if (client->add)
141 client->add(tdev);
142 }
143 mutex_unlock(&cxgb3_db_lock);
144}
145
146/**
147 * cxgb3_remove_clients - deactivates registered clients
148 * for an offload device
149 * @tdev: the offload device
150 *
151 * Call backs all registered clients once a offload device is deactivated
152 */
153void cxgb3_remove_clients(struct t3cdev *tdev)
154{
155 struct cxgb3_client *client;
156
157 mutex_lock(&cxgb3_db_lock);
158 list_for_each_entry(client, &client_list, client_list) {
159 if (client->remove)
160 client->remove(tdev);
161 }
162 mutex_unlock(&cxgb3_db_lock);
163}
164
Steve Wisefa0d4c12009-09-05 20:22:38 -0700165void cxgb3_event_notify(struct t3cdev *tdev, u32 event, u32 port)
Divy Le Raycb0bc202009-01-26 22:21:59 -0800166{
167 struct cxgb3_client *client;
168
169 mutex_lock(&cxgb3_db_lock);
170 list_for_each_entry(client, &client_list, client_list) {
Steve Wisefa0d4c12009-09-05 20:22:38 -0700171 if (client->event_handler)
172 client->event_handler(tdev, event, port);
Divy Le Raycb0bc202009-01-26 22:21:59 -0800173 }
174 mutex_unlock(&cxgb3_db_lock);
175}
176
Divy Le Ray4d22de32007-01-18 22:04:14 -0500177static struct net_device *get_iff_from_mac(struct adapter *adapter,
178 const unsigned char *mac,
179 unsigned int vlan)
180{
181 int i;
182
183 for_each_port(adapter, i) {
Divy Le Ray4d22de32007-01-18 22:04:14 -0500184 struct net_device *dev = adapter->port[i];
Divy Le Ray4d22de32007-01-18 22:04:14 -0500185
186 if (!memcmp(dev->dev_addr, mac, ETH_ALEN)) {
Jiri Pirko2eea05b2013-01-03 22:48:56 +0000187 rcu_read_lock();
Divy Le Ray4d22de32007-01-18 22:04:14 -0500188 if (vlan && vlan != VLAN_VID_MASK) {
Jiri Pirko892ef5d2011-07-20 04:54:35 +0000189 dev = __vlan_find_dev_deep(dev, vlan);
Jiri Pirko1765a572011-02-12 06:48:36 +0000190 } else if (netif_is_bond_slave(dev)) {
Jiri Pirko2eea05b2013-01-03 22:48:56 +0000191 struct net_device *upper_dev;
192
193 while ((upper_dev =
194 netdev_master_upper_dev_get_rcu(dev)))
195 dev = upper_dev;
Jiri Pirko1765a572011-02-12 06:48:36 +0000196 }
Jiri Pirko2eea05b2013-01-03 22:48:56 +0000197 rcu_read_unlock();
Divy Le Ray4d22de32007-01-18 22:04:14 -0500198 return dev;
199 }
200 }
201 return NULL;
202}
203
204static int cxgb_ulp_iscsi_ctl(struct adapter *adapter, unsigned int req,
205 void *data)
206{
Karen Xiea109a5b2008-12-18 22:56:20 -0800207 int i;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500208 int ret = 0;
Karen Xiea109a5b2008-12-18 22:56:20 -0800209 unsigned int val = 0;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500210 struct ulp_iscsi_info *uiip = data;
211
212 switch (req) {
213 case ULP_ISCSI_GET_PARAMS:
214 uiip->pdev = adapter->pdev;
215 uiip->llimit = t3_read_reg(adapter, A_ULPRX_ISCSI_LLIMIT);
216 uiip->ulimit = t3_read_reg(adapter, A_ULPRX_ISCSI_ULIMIT);
217 uiip->tagmask = t3_read_reg(adapter, A_ULPRX_ISCSI_TAGMASK);
Karen Xiea109a5b2008-12-18 22:56:20 -0800218
219 val = t3_read_reg(adapter, A_ULPRX_ISCSI_PSZ);
220 for (i = 0; i < 4; i++, val >>= 8)
221 uiip->pgsz_factor[i] = val & 0xFF;
222
223 val = t3_read_reg(adapter, A_TP_PARA_REG7);
224 uiip->max_txsz =
225 uiip->max_rxsz = min((val >> S_PMMAXXFERLEN0)&M_PMMAXXFERLEN0,
226 (val >> S_PMMAXXFERLEN1)&M_PMMAXXFERLEN1);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500227 /*
228 * On tx, the iscsi pdu has to be <= tx page size and has to
229 * fit into the Tx PM FIFO.
230 */
Karen Xiea109a5b2008-12-18 22:56:20 -0800231 val = min(adapter->params.tp.tx_pg_size,
232 t3_read_reg(adapter, A_PM1_TX_CFG) >> 17);
233 uiip->max_txsz = min(val, uiip->max_txsz);
234
235 /* set MaxRxData to 16224 */
236 val = t3_read_reg(adapter, A_TP_PARA_REG2);
237 if ((val >> S_MAXRXDATA) != 0x3f60) {
238 val &= (M_RXCOALESCESIZE << S_RXCOALESCESIZE);
239 val |= V_MAXRXDATA(0x3f60);
Joe Perches428ac432013-01-06 13:34:49 +0000240 pr_info("%s, iscsi set MaxRxData to 16224 (0x%x)\n",
Karen Xiea109a5b2008-12-18 22:56:20 -0800241 adapter->name, val);
242 t3_write_reg(adapter, A_TP_PARA_REG2, val);
243 }
244
245 /*
246 * on rx, the iscsi pdu has to be < rx page size and the
247 * the max rx data length programmed in TP
248 */
249 val = min(adapter->params.tp.rx_pg_size,
250 ((t3_read_reg(adapter, A_TP_PARA_REG2)) >>
251 S_MAXRXDATA) & M_MAXRXDATA);
252 uiip->max_rxsz = min(val, uiip->max_rxsz);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500253 break;
254 case ULP_ISCSI_SET_PARAMS:
255 t3_write_reg(adapter, A_ULPRX_ISCSI_TAGMASK, uiip->tagmask);
Karen Xie9439f742008-07-08 09:32:34 -0700256 /* program the ddp page sizes */
Karen Xiea109a5b2008-12-18 22:56:20 -0800257 for (i = 0; i < 4; i++)
258 val |= (uiip->pgsz_factor[i] & 0xF) << (8 * i);
259 if (val && (val != t3_read_reg(adapter, A_ULPRX_ISCSI_PSZ))) {
Joe Perches428ac432013-01-06 13:34:49 +0000260 pr_info("%s, setting iscsi pgsz 0x%x, %u,%u,%u,%u\n",
Karen Xiea109a5b2008-12-18 22:56:20 -0800261 adapter->name, val, uiip->pgsz_factor[0],
262 uiip->pgsz_factor[1], uiip->pgsz_factor[2],
263 uiip->pgsz_factor[3]);
264 t3_write_reg(adapter, A_ULPRX_ISCSI_PSZ, val);
Karen Xie9439f742008-07-08 09:32:34 -0700265 }
Divy Le Ray4d22de32007-01-18 22:04:14 -0500266 break;
267 default:
268 ret = -EOPNOTSUPP;
269 }
270 return ret;
271}
272
273/* Response queue used for RDMA events. */
274#define ASYNC_NOTIF_RSPQ 0
275
276static int cxgb_rdma_ctl(struct adapter *adapter, unsigned int req, void *data)
277{
278 int ret = 0;
279
280 switch (req) {
Stephen Hemminger9265fab2007-10-08 16:22:29 -0700281 case RDMA_GET_PARAMS: {
282 struct rdma_info *rdma = data;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500283 struct pci_dev *pdev = adapter->pdev;
284
Stephen Hemminger9265fab2007-10-08 16:22:29 -0700285 rdma->udbell_physbase = pci_resource_start(pdev, 2);
286 rdma->udbell_len = pci_resource_len(pdev, 2);
287 rdma->tpt_base =
Divy Le Ray4d22de32007-01-18 22:04:14 -0500288 t3_read_reg(adapter, A_ULPTX_TPT_LLIMIT);
Stephen Hemminger9265fab2007-10-08 16:22:29 -0700289 rdma->tpt_top = t3_read_reg(adapter, A_ULPTX_TPT_ULIMIT);
290 rdma->pbl_base =
Divy Le Ray4d22de32007-01-18 22:04:14 -0500291 t3_read_reg(adapter, A_ULPTX_PBL_LLIMIT);
Stephen Hemminger9265fab2007-10-08 16:22:29 -0700292 rdma->pbl_top = t3_read_reg(adapter, A_ULPTX_PBL_ULIMIT);
293 rdma->rqt_base = t3_read_reg(adapter, A_ULPRX_RQ_LLIMIT);
294 rdma->rqt_top = t3_read_reg(adapter, A_ULPRX_RQ_ULIMIT);
295 rdma->kdb_addr = adapter->regs + A_SG_KDOORBELL;
296 rdma->pdev = pdev;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500297 break;
298 }
299 case RDMA_CQ_OP:{
300 unsigned long flags;
Stephen Hemminger9265fab2007-10-08 16:22:29 -0700301 struct rdma_cq_op *rdma = data;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500302
303 /* may be called in any context */
304 spin_lock_irqsave(&adapter->sge.reg_lock, flags);
Stephen Hemminger9265fab2007-10-08 16:22:29 -0700305 ret = t3_sge_cqcntxt_op(adapter, rdma->id, rdma->op,
306 rdma->credits);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500307 spin_unlock_irqrestore(&adapter->sge.reg_lock, flags);
308 break;
309 }
310 case RDMA_GET_MEM:{
311 struct ch_mem_range *t = data;
312 struct mc7 *mem;
313
314 if ((t->addr & 7) || (t->len & 7))
315 return -EINVAL;
316 if (t->mem_id == MEM_CM)
317 mem = &adapter->cm;
318 else if (t->mem_id == MEM_PMRX)
319 mem = &adapter->pmrx;
320 else if (t->mem_id == MEM_PMTX)
321 mem = &adapter->pmtx;
322 else
323 return -EINVAL;
324
325 ret =
326 t3_mc7_bd_read(mem, t->addr / 8, t->len / 8,
327 (u64 *) t->buf);
328 if (ret)
329 return ret;
330 break;
331 }
332 case RDMA_CQ_SETUP:{
Stephen Hemminger9265fab2007-10-08 16:22:29 -0700333 struct rdma_cq_setup *rdma = data;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500334
335 spin_lock_irq(&adapter->sge.reg_lock);
336 ret =
Stephen Hemminger9265fab2007-10-08 16:22:29 -0700337 t3_sge_init_cqcntxt(adapter, rdma->id,
338 rdma->base_addr, rdma->size,
Divy Le Ray4d22de32007-01-18 22:04:14 -0500339 ASYNC_NOTIF_RSPQ,
Stephen Hemminger9265fab2007-10-08 16:22:29 -0700340 rdma->ovfl_mode, rdma->credits,
341 rdma->credit_thres);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500342 spin_unlock_irq(&adapter->sge.reg_lock);
343 break;
344 }
345 case RDMA_CQ_DISABLE:
346 spin_lock_irq(&adapter->sge.reg_lock);
347 ret = t3_sge_disable_cqcntxt(adapter, *(unsigned int *)data);
348 spin_unlock_irq(&adapter->sge.reg_lock);
349 break;
350 case RDMA_CTRL_QP_SETUP:{
Stephen Hemminger9265fab2007-10-08 16:22:29 -0700351 struct rdma_ctrlqp_setup *rdma = data;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500352
353 spin_lock_irq(&adapter->sge.reg_lock);
354 ret = t3_sge_init_ecntxt(adapter, FW_RI_SGEEC_START, 0,
355 SGE_CNTXT_RDMA,
356 ASYNC_NOTIF_RSPQ,
Stephen Hemminger9265fab2007-10-08 16:22:29 -0700357 rdma->base_addr, rdma->size,
Divy Le Ray4d22de32007-01-18 22:04:14 -0500358 FW_RI_TID_START, 1, 0);
359 spin_unlock_irq(&adapter->sge.reg_lock);
360 break;
361 }
Steve Wise14cc1802008-07-14 23:48:48 -0700362 case RDMA_GET_MIB: {
363 spin_lock(&adapter->stats_lock);
364 t3_tp_get_mib_stats(adapter, (struct tp_mib_stats *)data);
365 spin_unlock(&adapter->stats_lock);
366 break;
367 }
Divy Le Ray4d22de32007-01-18 22:04:14 -0500368 default:
369 ret = -EOPNOTSUPP;
370 }
371 return ret;
372}
373
374static int cxgb_offload_ctl(struct t3cdev *tdev, unsigned int req, void *data)
375{
376 struct adapter *adapter = tdev2adap(tdev);
377 struct tid_range *tid;
378 struct mtutab *mtup;
379 struct iff_mac *iffmacp;
380 struct ddp_params *ddpp;
381 struct adap_ports *ports;
Divy Le Raye22bb452007-08-21 20:49:21 -0700382 struct ofld_page_info *rx_page_info;
383 struct tp_params *tp = &adapter->params.tp;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500384 int i;
385
386 switch (req) {
387 case GET_MAX_OUTSTANDING_WR:
388 *(unsigned int *)data = FW_WR_NUM;
389 break;
390 case GET_WR_LEN:
391 *(unsigned int *)data = WR_FLITS;
392 break;
393 case GET_TX_MAX_CHUNK:
394 *(unsigned int *)data = 1 << 20; /* 1MB */
395 break;
396 case GET_TID_RANGE:
397 tid = data;
398 tid->num = t3_mc5_size(&adapter->mc5) -
399 adapter->params.mc5.nroutes -
400 adapter->params.mc5.nfilters - adapter->params.mc5.nservers;
401 tid->base = 0;
402 break;
403 case GET_STID_RANGE:
404 tid = data;
405 tid->num = adapter->params.mc5.nservers;
406 tid->base = t3_mc5_size(&adapter->mc5) - tid->num -
407 adapter->params.mc5.nfilters - adapter->params.mc5.nroutes;
408 break;
409 case GET_L2T_CAPACITY:
410 *(unsigned int *)data = 2048;
411 break;
412 case GET_MTUS:
413 mtup = data;
414 mtup->size = NMTUS;
415 mtup->mtus = adapter->params.mtus;
416 break;
417 case GET_IFF_FROM_MAC:
418 iffmacp = data;
419 iffmacp->dev = get_iff_from_mac(adapter, iffmacp->mac_addr,
420 iffmacp->vlan_tag &
421 VLAN_VID_MASK);
422 break;
423 case GET_DDP_PARAMS:
424 ddpp = data;
425 ddpp->llimit = t3_read_reg(adapter, A_ULPRX_TDDP_LLIMIT);
426 ddpp->ulimit = t3_read_reg(adapter, A_ULPRX_TDDP_ULIMIT);
427 ddpp->tag_mask = t3_read_reg(adapter, A_ULPRX_TDDP_TAGMASK);
428 break;
429 case GET_PORTS:
430 ports = data;
431 ports->nports = adapter->params.nports;
432 for_each_port(adapter, i)
433 ports->lldevs[i] = adapter->port[i];
434 break;
435 case ULP_ISCSI_GET_PARAMS:
436 case ULP_ISCSI_SET_PARAMS:
437 if (!offload_running(adapter))
438 return -EAGAIN;
439 return cxgb_ulp_iscsi_ctl(adapter, req, data);
440 case RDMA_GET_PARAMS:
441 case RDMA_CQ_OP:
442 case RDMA_CQ_SETUP:
443 case RDMA_CQ_DISABLE:
444 case RDMA_CTRL_QP_SETUP:
445 case RDMA_GET_MEM:
Steve Wise14cc1802008-07-14 23:48:48 -0700446 case RDMA_GET_MIB:
Divy Le Ray4d22de32007-01-18 22:04:14 -0500447 if (!offload_running(adapter))
448 return -EAGAIN;
449 return cxgb_rdma_ctl(adapter, req, data);
Divy Le Raye22bb452007-08-21 20:49:21 -0700450 case GET_RX_PAGE_INFO:
451 rx_page_info = data;
452 rx_page_info->page_size = tp->rx_pg_size;
453 rx_page_info->num = tp->rx_num_pgs;
454 break;
Karen Xiea109a5b2008-12-18 22:56:20 -0800455 case GET_ISCSI_IPV4ADDR: {
456 struct iscsi_ipv4addr *p = data;
457 struct port_info *pi = netdev_priv(p->dev);
458 p->ipv4addr = pi->iscsi_ipv4addr;
459 break;
460 }
Divy Le Ray4d8cd002008-12-26 01:16:39 -0800461 case GET_EMBEDDED_INFO: {
462 struct ch_embedded_info *e = data;
463
464 spin_lock(&adapter->stats_lock);
465 t3_get_fw_version(adapter, &e->fw_vers);
466 t3_get_tp_version(adapter, &e->tp_vers);
467 spin_unlock(&adapter->stats_lock);
468 break;
469 }
Divy Le Ray4d22de32007-01-18 22:04:14 -0500470 default:
471 return -EOPNOTSUPP;
472 }
473 return 0;
474}
475
476/*
477 * Dummy handler for Rx offload packets in case we get an offload packet before
478 * proper processing is setup. This complains and drops the packet as it isn't
479 * normal to get offload packets at this stage.
480 */
481static int rx_offload_blackhole(struct t3cdev *dev, struct sk_buff **skbs,
482 int n)
483{
Divy Le Ray4d22de32007-01-18 22:04:14 -0500484 while (n--)
485 dev_kfree_skb_any(skbs[n]);
486 return 0;
487}
488
489static void dummy_neigh_update(struct t3cdev *dev, struct neighbour *neigh)
490{
491}
492
493void cxgb3_set_dummy_ops(struct t3cdev *dev)
494{
495 dev->recv = rx_offload_blackhole;
496 dev->neigh_update = dummy_neigh_update;
497}
498
499/*
500 * Free an active-open TID.
501 */
502void *cxgb3_free_atid(struct t3cdev *tdev, int atid)
503{
504 struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
505 union active_open_entry *p = atid2entry(t, atid);
506 void *ctx = p->t3c_tid.ctx;
507
508 spin_lock_bh(&t->atid_lock);
509 p->next = t->afree;
510 t->afree = p;
511 t->atids_in_use--;
512 spin_unlock_bh(&t->atid_lock);
513
514 return ctx;
515}
516
517EXPORT_SYMBOL(cxgb3_free_atid);
518
519/*
520 * Free a server TID and return it to the free pool.
521 */
522void cxgb3_free_stid(struct t3cdev *tdev, int stid)
523{
524 struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
525 union listen_entry *p = stid2entry(t, stid);
526
527 spin_lock_bh(&t->stid_lock);
528 p->next = t->sfree;
529 t->sfree = p;
530 t->stids_in_use--;
531 spin_unlock_bh(&t->stid_lock);
532}
533
534EXPORT_SYMBOL(cxgb3_free_stid);
535
536void cxgb3_insert_tid(struct t3cdev *tdev, struct cxgb3_client *client,
537 void *ctx, unsigned int tid)
538{
539 struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
540
541 t->tid_tab[tid].client = client;
542 t->tid_tab[tid].ctx = ctx;
543 atomic_inc(&t->tids_in_use);
544}
545
546EXPORT_SYMBOL(cxgb3_insert_tid);
547
548/*
549 * Populate a TID_RELEASE WR. The skb must be already propely sized.
550 */
551static inline void mk_tid_release(struct sk_buff *skb, unsigned int tid)
552{
553 struct cpl_tid_release *req;
554
555 skb->priority = CPL_PRIORITY_SETUP;
556 req = (struct cpl_tid_release *)__skb_put(skb, sizeof(*req));
557 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
558 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
559}
560
561static void t3_process_tid_release_list(struct work_struct *work)
562{
563 struct t3c_data *td = container_of(work, struct t3c_data,
564 tid_release_task);
565 struct sk_buff *skb;
566 struct t3cdev *tdev = td->dev;
Jeff Garzik2eab17a2007-11-23 21:59:45 -0500567
Divy Le Ray4d22de32007-01-18 22:04:14 -0500568
569 spin_lock_bh(&td->tid_release_lock);
570 while (td->tid_release_list) {
571 struct t3c_tid_entry *p = td->tid_release_list;
572
Joe Perches43d620c2011-06-16 19:08:06 +0000573 td->tid_release_list = p->ctx;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500574 spin_unlock_bh(&td->tid_release_lock);
575
576 skb = alloc_skb(sizeof(struct cpl_tid_release),
Divy Le Ray74b793e2009-06-09 23:25:21 +0000577 GFP_KERNEL);
578 if (!skb)
579 skb = td->nofail_skb;
580 if (!skb) {
581 spin_lock_bh(&td->tid_release_lock);
582 p->ctx = (void *)td->tid_release_list;
Joe Perches64699332012-06-04 12:44:16 +0000583 td->tid_release_list = p;
Divy Le Ray74b793e2009-06-09 23:25:21 +0000584 break;
585 }
Divy Le Ray4d22de32007-01-18 22:04:14 -0500586 mk_tid_release(skb, p - td->tid_maps.tid_tab);
587 cxgb3_ofld_send(tdev, skb);
588 p->ctx = NULL;
Divy Le Ray74b793e2009-06-09 23:25:21 +0000589 if (skb == td->nofail_skb)
590 td->nofail_skb =
591 alloc_skb(sizeof(struct cpl_tid_release),
592 GFP_KERNEL);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500593 spin_lock_bh(&td->tid_release_lock);
594 }
Divy Le Ray74b793e2009-06-09 23:25:21 +0000595 td->release_list_incomplete = (td->tid_release_list == NULL) ? 0 : 1;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500596 spin_unlock_bh(&td->tid_release_lock);
Divy Le Ray74b793e2009-06-09 23:25:21 +0000597
598 if (!td->nofail_skb)
599 td->nofail_skb =
600 alloc_skb(sizeof(struct cpl_tid_release),
601 GFP_KERNEL);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500602}
603
604/* use ctx as a next pointer in the tid release list */
605void cxgb3_queue_tid_release(struct t3cdev *tdev, unsigned int tid)
606{
607 struct t3c_data *td = T3C_DATA(tdev);
608 struct t3c_tid_entry *p = &td->tid_maps.tid_tab[tid];
609
610 spin_lock_bh(&td->tid_release_lock);
611 p->ctx = (void *)td->tid_release_list;
Divy Le Ray606fcd02007-04-17 11:06:30 -0700612 p->client = NULL;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500613 td->tid_release_list = p;
Divy Le Ray74b793e2009-06-09 23:25:21 +0000614 if (!p->ctx || td->release_list_incomplete)
Divy Le Ray4d22de32007-01-18 22:04:14 -0500615 schedule_work(&td->tid_release_task);
616 spin_unlock_bh(&td->tid_release_lock);
617}
618
619EXPORT_SYMBOL(cxgb3_queue_tid_release);
620
621/*
622 * Remove a tid from the TID table. A client may defer processing its last
623 * CPL message if it is locked at the time it arrives, and while the message
624 * sits in the client's backlog the TID may be reused for another connection.
625 * To handle this we atomically switch the TID association if it still points
626 * to the original client context.
627 */
628void cxgb3_remove_tid(struct t3cdev *tdev, void *ctx, unsigned int tid)
629{
630 struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
631
632 BUG_ON(tid >= t->ntids);
633 if (tdev->type == T3A)
634 (void)cmpxchg(&t->tid_tab[tid].ctx, ctx, NULL);
635 else {
636 struct sk_buff *skb;
637
638 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
639 if (likely(skb)) {
640 mk_tid_release(skb, tid);
641 cxgb3_ofld_send(tdev, skb);
642 t->tid_tab[tid].ctx = NULL;
643 } else
644 cxgb3_queue_tid_release(tdev, tid);
645 }
646 atomic_dec(&t->tids_in_use);
647}
648
649EXPORT_SYMBOL(cxgb3_remove_tid);
650
651int cxgb3_alloc_atid(struct t3cdev *tdev, struct cxgb3_client *client,
652 void *ctx)
653{
654 int atid = -1;
655 struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
656
657 spin_lock_bh(&t->atid_lock);
Divy Le Ray9f238482007-03-31 00:23:13 -0700658 if (t->afree &&
659 t->atids_in_use + atomic_read(&t->tids_in_use) + MC5_MIN_TIDS <=
660 t->ntids) {
Divy Le Ray4d22de32007-01-18 22:04:14 -0500661 union active_open_entry *p = t->afree;
662
663 atid = (p - t->atid_tab) + t->atid_base;
664 t->afree = p->next;
665 p->t3c_tid.ctx = ctx;
666 p->t3c_tid.client = client;
667 t->atids_in_use++;
668 }
669 spin_unlock_bh(&t->atid_lock);
670 return atid;
671}
672
673EXPORT_SYMBOL(cxgb3_alloc_atid);
674
675int cxgb3_alloc_stid(struct t3cdev *tdev, struct cxgb3_client *client,
676 void *ctx)
677{
678 int stid = -1;
679 struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
680
681 spin_lock_bh(&t->stid_lock);
682 if (t->sfree) {
683 union listen_entry *p = t->sfree;
684
685 stid = (p - t->stid_tab) + t->stid_base;
686 t->sfree = p->next;
687 p->t3c_tid.ctx = ctx;
688 p->t3c_tid.client = client;
689 t->stids_in_use++;
690 }
691 spin_unlock_bh(&t->stid_lock);
692 return stid;
693}
694
695EXPORT_SYMBOL(cxgb3_alloc_stid);
696
Divy Le Ray5fbf8162007-08-29 19:15:47 -0700697/* Get the t3cdev associated with a net_device */
698struct t3cdev *dev2t3cdev(struct net_device *dev)
699{
700 const struct port_info *pi = netdev_priv(dev);
701
702 return (struct t3cdev *)pi->adapter;
703}
704
705EXPORT_SYMBOL(dev2t3cdev);
706
Divy Le Ray4d22de32007-01-18 22:04:14 -0500707static int do_smt_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
708{
709 struct cpl_smt_write_rpl *rpl = cplhdr(skb);
710
711 if (rpl->status != CPL_ERR_NONE)
Joe Perches428ac432013-01-06 13:34:49 +0000712 pr_err("Unexpected SMT_WRITE_RPL status %u for entry %u\n",
Divy Le Ray4d22de32007-01-18 22:04:14 -0500713 rpl->status, GET_TID(rpl));
714
715 return CPL_RET_BUF_DONE;
716}
717
718static int do_l2t_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
719{
720 struct cpl_l2t_write_rpl *rpl = cplhdr(skb);
721
722 if (rpl->status != CPL_ERR_NONE)
Joe Perches428ac432013-01-06 13:34:49 +0000723 pr_err("Unexpected L2T_WRITE_RPL status %u for entry %u\n",
Divy Le Ray4d22de32007-01-18 22:04:14 -0500724 rpl->status, GET_TID(rpl));
725
726 return CPL_RET_BUF_DONE;
727}
728
Divy Le Rayb8819552007-12-17 18:47:31 -0800729static int do_rte_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
730{
731 struct cpl_rte_write_rpl *rpl = cplhdr(skb);
732
733 if (rpl->status != CPL_ERR_NONE)
Joe Perches428ac432013-01-06 13:34:49 +0000734 pr_err("Unexpected RTE_WRITE_RPL status %u for entry %u\n",
Divy Le Rayb8819552007-12-17 18:47:31 -0800735 rpl->status, GET_TID(rpl));
736
737 return CPL_RET_BUF_DONE;
738}
739
Divy Le Ray4d22de32007-01-18 22:04:14 -0500740static int do_act_open_rpl(struct t3cdev *dev, struct sk_buff *skb)
741{
742 struct cpl_act_open_rpl *rpl = cplhdr(skb);
743 unsigned int atid = G_TID(ntohl(rpl->atid));
744 struct t3c_tid_entry *t3c_tid;
745
746 t3c_tid = lookup_atid(&(T3C_DATA(dev))->tid_maps, atid);
Divy Le Ray606fcd02007-04-17 11:06:30 -0700747 if (t3c_tid && t3c_tid->ctx && t3c_tid->client &&
748 t3c_tid->client->handlers &&
Divy Le Ray4d22de32007-01-18 22:04:14 -0500749 t3c_tid->client->handlers[CPL_ACT_OPEN_RPL]) {
750 return t3c_tid->client->handlers[CPL_ACT_OPEN_RPL] (dev, skb,
751 t3c_tid->
752 ctx);
753 } else {
Joe Perches428ac432013-01-06 13:34:49 +0000754 pr_err("%s: received clientless CPL command 0x%x\n",
Divy Le Ray4d22de32007-01-18 22:04:14 -0500755 dev->name, CPL_ACT_OPEN_RPL);
756 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
757 }
758}
759
760static int do_stid_rpl(struct t3cdev *dev, struct sk_buff *skb)
761{
762 union opcode_tid *p = cplhdr(skb);
763 unsigned int stid = G_TID(ntohl(p->opcode_tid));
764 struct t3c_tid_entry *t3c_tid;
765
766 t3c_tid = lookup_stid(&(T3C_DATA(dev))->tid_maps, stid);
Divy Le Ray606fcd02007-04-17 11:06:30 -0700767 if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
Divy Le Ray4d22de32007-01-18 22:04:14 -0500768 t3c_tid->client->handlers[p->opcode]) {
769 return t3c_tid->client->handlers[p->opcode] (dev, skb,
770 t3c_tid->ctx);
771 } else {
Joe Perches428ac432013-01-06 13:34:49 +0000772 pr_err("%s: received clientless CPL command 0x%x\n",
Divy Le Ray4d22de32007-01-18 22:04:14 -0500773 dev->name, p->opcode);
774 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
775 }
776}
777
778static int do_hwtid_rpl(struct t3cdev *dev, struct sk_buff *skb)
779{
780 union opcode_tid *p = cplhdr(skb);
781 unsigned int hwtid = G_TID(ntohl(p->opcode_tid));
782 struct t3c_tid_entry *t3c_tid;
783
784 t3c_tid = lookup_tid(&(T3C_DATA(dev))->tid_maps, hwtid);
Divy Le Ray606fcd02007-04-17 11:06:30 -0700785 if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
Divy Le Ray4d22de32007-01-18 22:04:14 -0500786 t3c_tid->client->handlers[p->opcode]) {
787 return t3c_tid->client->handlers[p->opcode]
788 (dev, skb, t3c_tid->ctx);
789 } else {
Joe Perches428ac432013-01-06 13:34:49 +0000790 pr_err("%s: received clientless CPL command 0x%x\n",
Divy Le Ray4d22de32007-01-18 22:04:14 -0500791 dev->name, p->opcode);
792 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
793 }
794}
795
796static int do_cr(struct t3cdev *dev, struct sk_buff *skb)
797{
798 struct cpl_pass_accept_req *req = cplhdr(skb);
799 unsigned int stid = G_PASS_OPEN_TID(ntohl(req->tos_tid));
Divy Le Rayc9a6ce52007-08-21 20:49:26 -0700800 struct tid_info *t = &(T3C_DATA(dev))->tid_maps;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500801 struct t3c_tid_entry *t3c_tid;
Divy Le Rayc9a6ce52007-08-21 20:49:26 -0700802 unsigned int tid = GET_TID(req);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500803
Divy Le Rayc9a6ce52007-08-21 20:49:26 -0700804 if (unlikely(tid >= t->ntids)) {
805 printk("%s: passive open TID %u too large\n",
806 dev->name, tid);
807 t3_fatal_err(tdev2adap(dev));
808 return CPL_RET_BUF_DONE;
809 }
810
811 t3c_tid = lookup_stid(t, stid);
812 if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
Divy Le Ray4d22de32007-01-18 22:04:14 -0500813 t3c_tid->client->handlers[CPL_PASS_ACCEPT_REQ]) {
814 return t3c_tid->client->handlers[CPL_PASS_ACCEPT_REQ]
815 (dev, skb, t3c_tid->ctx);
816 } else {
Joe Perches428ac432013-01-06 13:34:49 +0000817 pr_err("%s: received clientless CPL command 0x%x\n",
Divy Le Ray4d22de32007-01-18 22:04:14 -0500818 dev->name, CPL_PASS_ACCEPT_REQ);
819 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
820 }
821}
822
Divy Le Ray606fcd02007-04-17 11:06:30 -0700823/*
824 * Returns an sk_buff for a reply CPL message of size len. If the input
825 * sk_buff has no other users it is trimmed and reused, otherwise a new buffer
826 * is allocated. The input skb must be of size at least len. Note that this
827 * operation does not destroy the original skb data even if it decides to reuse
828 * the buffer.
829 */
830static struct sk_buff *cxgb3_get_cpl_reply_skb(struct sk_buff *skb, size_t len,
Al Viro1f41bb32007-07-26 17:35:19 +0100831 gfp_t gfp)
Divy Le Ray606fcd02007-04-17 11:06:30 -0700832{
833 if (likely(!skb_cloned(skb))) {
834 BUG_ON(skb->len < len);
835 __skb_trim(skb, len);
836 skb_get(skb);
837 } else {
838 skb = alloc_skb(len, gfp);
839 if (skb)
840 __skb_put(skb, len);
841 }
842 return skb;
843}
844
Divy Le Ray4d22de32007-01-18 22:04:14 -0500845static int do_abort_req_rss(struct t3cdev *dev, struct sk_buff *skb)
846{
847 union opcode_tid *p = cplhdr(skb);
848 unsigned int hwtid = G_TID(ntohl(p->opcode_tid));
849 struct t3c_tid_entry *t3c_tid;
850
851 t3c_tid = lookup_tid(&(T3C_DATA(dev))->tid_maps, hwtid);
Divy Le Ray606fcd02007-04-17 11:06:30 -0700852 if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
Divy Le Ray4d22de32007-01-18 22:04:14 -0500853 t3c_tid->client->handlers[p->opcode]) {
854 return t3c_tid->client->handlers[p->opcode]
855 (dev, skb, t3c_tid->ctx);
856 } else {
857 struct cpl_abort_req_rss *req = cplhdr(skb);
858 struct cpl_abort_rpl *rpl;
Divy Le Ray606fcd02007-04-17 11:06:30 -0700859 struct sk_buff *reply_skb;
860 unsigned int tid = GET_TID(req);
861 u8 cmd = req->status;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500862
Divy Le Ray606fcd02007-04-17 11:06:30 -0700863 if (req->status == CPL_ERR_RTX_NEG_ADVICE ||
864 req->status == CPL_ERR_PERSIST_NEG_ADVICE)
865 goto out;
866
867 reply_skb = cxgb3_get_cpl_reply_skb(skb,
868 sizeof(struct
869 cpl_abort_rpl),
870 GFP_ATOMIC);
871
872 if (!reply_skb) {
Divy Le Ray4d22de32007-01-18 22:04:14 -0500873 printk("do_abort_req_rss: couldn't get skb!\n");
874 goto out;
875 }
Divy Le Ray606fcd02007-04-17 11:06:30 -0700876 reply_skb->priority = CPL_PRIORITY_DATA;
877 __skb_put(reply_skb, sizeof(struct cpl_abort_rpl));
878 rpl = cplhdr(reply_skb);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500879 rpl->wr.wr_hi =
880 htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_RPL));
Divy Le Ray606fcd02007-04-17 11:06:30 -0700881 rpl->wr.wr_lo = htonl(V_WR_TID(tid));
882 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_ABORT_RPL, tid));
883 rpl->cmd = cmd;
884 cxgb3_ofld_send(dev, reply_skb);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500885out:
886 return CPL_RET_BUF_DONE;
887 }
888}
889
890static int do_act_establish(struct t3cdev *dev, struct sk_buff *skb)
891{
892 struct cpl_act_establish *req = cplhdr(skb);
893 unsigned int atid = G_PASS_OPEN_TID(ntohl(req->tos_tid));
Divy Le Rayc9a6ce52007-08-21 20:49:26 -0700894 struct tid_info *t = &(T3C_DATA(dev))->tid_maps;
Divy Le Ray4d22de32007-01-18 22:04:14 -0500895 struct t3c_tid_entry *t3c_tid;
Divy Le Rayc9a6ce52007-08-21 20:49:26 -0700896 unsigned int tid = GET_TID(req);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500897
Divy Le Rayc9a6ce52007-08-21 20:49:26 -0700898 if (unlikely(tid >= t->ntids)) {
899 printk("%s: active establish TID %u too large\n",
900 dev->name, tid);
901 t3_fatal_err(tdev2adap(dev));
902 return CPL_RET_BUF_DONE;
903 }
904
905 t3c_tid = lookup_atid(t, atid);
Divy Le Ray606fcd02007-04-17 11:06:30 -0700906 if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
Divy Le Ray4d22de32007-01-18 22:04:14 -0500907 t3c_tid->client->handlers[CPL_ACT_ESTABLISH]) {
908 return t3c_tid->client->handlers[CPL_ACT_ESTABLISH]
909 (dev, skb, t3c_tid->ctx);
910 } else {
Joe Perches428ac432013-01-06 13:34:49 +0000911 pr_err("%s: received clientless CPL command 0x%x\n",
Divy Le Rayc9a6ce52007-08-21 20:49:26 -0700912 dev->name, CPL_ACT_ESTABLISH);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500913 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
914 }
915}
916
Divy Le Ray4d22de32007-01-18 22:04:14 -0500917static int do_trace(struct t3cdev *dev, struct sk_buff *skb)
918{
919 struct cpl_trace_pkt *p = cplhdr(skb);
920
Al Virob5344972007-02-09 16:40:10 +0000921 skb->protocol = htons(0xffff);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500922 skb->dev = dev->lldev;
923 skb_pull(skb, sizeof(*p));
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700924 skb_reset_mac_header(skb);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500925 netif_receive_skb(skb);
926 return 0;
927}
928
Al Virofa3a6cb2008-03-16 22:22:54 +0000929/*
930 * That skb would better have come from process_responses() where we abuse
931 * ->priority and ->csum to carry our data. NB: if we get to per-arch
932 * ->csum, the things might get really interesting here.
933 */
934
935static inline u32 get_hwtid(struct sk_buff *skb)
936{
937 return ntohl((__force __be32)skb->priority) >> 8 & 0xfffff;
938}
939
940static inline u32 get_opcode(struct sk_buff *skb)
941{
942 return G_OPCODE(ntohl((__force __be32)skb->csum));
943}
944
Divy Le Ray4d22de32007-01-18 22:04:14 -0500945static int do_term(struct t3cdev *dev, struct sk_buff *skb)
946{
Al Virofa3a6cb2008-03-16 22:22:54 +0000947 unsigned int hwtid = get_hwtid(skb);
948 unsigned int opcode = get_opcode(skb);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500949 struct t3c_tid_entry *t3c_tid;
950
951 t3c_tid = lookup_tid(&(T3C_DATA(dev))->tid_maps, hwtid);
Divy Le Ray606fcd02007-04-17 11:06:30 -0700952 if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
Divy Le Ray4d22de32007-01-18 22:04:14 -0500953 t3c_tid->client->handlers[opcode]) {
954 return t3c_tid->client->handlers[opcode] (dev, skb,
955 t3c_tid->ctx);
956 } else {
Joe Perches428ac432013-01-06 13:34:49 +0000957 pr_err("%s: received clientless CPL command 0x%x\n",
Divy Le Ray4d22de32007-01-18 22:04:14 -0500958 dev->name, opcode);
959 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
960 }
961}
962
963static int nb_callback(struct notifier_block *self, unsigned long event,
964 void *ctx)
965{
966 switch (event) {
967 case (NETEVENT_NEIGH_UPDATE):{
968 cxgb_neigh_update((struct neighbour *)ctx);
969 break;
970 }
Divy Le Ray4d22de32007-01-18 22:04:14 -0500971 case (NETEVENT_REDIRECT):{
972 struct netevent_redirect *nr = ctx;
David S. Miller1d248b12012-07-03 01:01:51 -0700973 cxgb_redirect(nr->old, nr->old_neigh,
David S. Miller534cb282012-07-02 22:35:31 -0700974 nr->new, nr->new_neigh,
975 nr->daddr);
David S. Miller1d248b12012-07-03 01:01:51 -0700976 cxgb_neigh_update(nr->new_neigh);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500977 break;
978 }
979 default:
980 break;
981 }
982 return 0;
983}
984
985static struct notifier_block nb = {
986 .notifier_call = nb_callback
987};
988
989/*
990 * Process a received packet with an unknown/unexpected CPL opcode.
991 */
992static int do_bad_cpl(struct t3cdev *dev, struct sk_buff *skb)
993{
Joe Perches428ac432013-01-06 13:34:49 +0000994 pr_err("%s: received bad CPL command 0x%x\n", dev->name, *skb->data);
Divy Le Ray4d22de32007-01-18 22:04:14 -0500995 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
996}
997
998/*
999 * Handlers for each CPL opcode
1000 */
1001static cpl_handler_func cpl_handlers[NUM_CPL_CMDS];
1002
1003/*
1004 * Add a new handler to the CPL dispatch table. A NULL handler may be supplied
1005 * to unregister an existing handler.
1006 */
1007void t3_register_cpl_handler(unsigned int opcode, cpl_handler_func h)
1008{
1009 if (opcode < NUM_CPL_CMDS)
1010 cpl_handlers[opcode] = h ? h : do_bad_cpl;
1011 else
Joe Perches428ac432013-01-06 13:34:49 +00001012 pr_err("T3C: handler registration for opcode %x failed\n",
1013 opcode);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001014}
1015
1016EXPORT_SYMBOL(t3_register_cpl_handler);
1017
1018/*
1019 * T3CDEV's receive method.
1020 */
stephen hemmingera5190b42010-10-15 12:43:10 +00001021static int process_rx(struct t3cdev *dev, struct sk_buff **skbs, int n)
Divy Le Ray4d22de32007-01-18 22:04:14 -05001022{
1023 while (n--) {
1024 struct sk_buff *skb = *skbs++;
Al Virofa3a6cb2008-03-16 22:22:54 +00001025 unsigned int opcode = get_opcode(skb);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001026 int ret = cpl_handlers[opcode] (dev, skb);
1027
1028#if VALIDATE_TID
1029 if (ret & CPL_RET_UNKNOWN_TID) {
1030 union opcode_tid *p = cplhdr(skb);
1031
Joe Perches428ac432013-01-06 13:34:49 +00001032 pr_err("%s: CPL message (opcode %u) had unknown TID %u\n",
1033 dev->name, opcode, G_TID(ntohl(p->opcode_tid)));
Divy Le Ray4d22de32007-01-18 22:04:14 -05001034 }
1035#endif
1036 if (ret & CPL_RET_BUF_DONE)
1037 kfree_skb(skb);
1038 }
1039 return 0;
1040}
1041
1042/*
1043 * Sends an sk_buff to a T3C driver after dealing with any active network taps.
1044 */
1045int cxgb3_ofld_send(struct t3cdev *dev, struct sk_buff *skb)
1046{
1047 int r;
1048
1049 local_bh_disable();
1050 r = dev->send(dev, skb);
1051 local_bh_enable();
1052 return r;
1053}
1054
1055EXPORT_SYMBOL(cxgb3_ofld_send);
1056
1057static int is_offloading(struct net_device *dev)
1058{
1059 struct adapter *adapter;
1060 int i;
1061
1062 read_lock_bh(&adapter_list_lock);
1063 list_for_each_entry(adapter, &adapter_list, adapter_list) {
1064 for_each_port(adapter, i) {
1065 if (dev == adapter->port[i]) {
1066 read_unlock_bh(&adapter_list_lock);
1067 return 1;
1068 }
1069 }
1070 }
1071 read_unlock_bh(&adapter_list_lock);
1072 return 0;
1073}
1074
stephen hemmingera5190b42010-10-15 12:43:10 +00001075static void cxgb_neigh_update(struct neighbour *neigh)
Divy Le Ray4d22de32007-01-18 22:04:14 -05001076{
David Millerc4be62a2011-12-02 16:52:22 +00001077 struct net_device *dev;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001078
David Millerc4be62a2011-12-02 16:52:22 +00001079 if (!neigh)
1080 return;
1081 dev = neigh->dev;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001082 if (dev && (is_offloading(dev))) {
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001083 struct t3cdev *tdev = dev2t3cdev(dev);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001084
1085 BUG_ON(!tdev);
1086 t3_l2t_update(tdev, neigh);
1087 }
1088}
1089
1090static void set_l2t_ix(struct t3cdev *tdev, u32 tid, struct l2t_entry *e)
1091{
1092 struct sk_buff *skb;
1093 struct cpl_set_tcb_field *req;
1094
1095 skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
1096 if (!skb) {
Joe Perches428ac432013-01-06 13:34:49 +00001097 pr_err("%s: cannot allocate skb!\n", __func__);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001098 return;
1099 }
1100 skb->priority = CPL_PRIORITY_CONTROL;
1101 req = (struct cpl_set_tcb_field *)skb_put(skb, sizeof(*req));
1102 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1103 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, tid));
1104 req->reply = 0;
1105 req->cpu_idx = 0;
1106 req->word = htons(W_TCB_L2T_IX);
1107 req->mask = cpu_to_be64(V_TCB_L2T_IX(M_TCB_L2T_IX));
1108 req->val = cpu_to_be64(V_TCB_L2T_IX(e->idx));
1109 tdev->send(tdev, skb);
1110}
1111
David S. Miller1d248b12012-07-03 01:01:51 -07001112static void cxgb_redirect(struct dst_entry *old, struct neighbour *old_neigh,
David S. Miller534cb282012-07-02 22:35:31 -07001113 struct dst_entry *new, struct neighbour *new_neigh,
1114 const void *daddr)
Divy Le Ray4d22de32007-01-18 22:04:14 -05001115{
1116 struct net_device *olddev, *newdev;
1117 struct tid_info *ti;
1118 struct t3cdev *tdev;
1119 u32 tid;
1120 int update_tcb;
1121 struct l2t_entry *e;
1122 struct t3c_tid_entry *te;
1123
David S. Miller1d248b12012-07-03 01:01:51 -07001124 olddev = old_neigh->dev;
1125 newdev = new_neigh->dev;
David Millerc4be62a2011-12-02 16:52:22 +00001126
Divy Le Ray4d22de32007-01-18 22:04:14 -05001127 if (!is_offloading(olddev))
1128 return;
1129 if (!is_offloading(newdev)) {
Joe Perches428ac432013-01-06 13:34:49 +00001130 pr_warn("%s: Redirect to non-offload device ignored\n",
1131 __func__);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001132 return;
1133 }
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001134 tdev = dev2t3cdev(olddev);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001135 BUG_ON(!tdev);
Divy Le Ray5fbf8162007-08-29 19:15:47 -07001136 if (tdev != dev2t3cdev(newdev)) {
Joe Perches428ac432013-01-06 13:34:49 +00001137 pr_warn("%s: Redirect to different offload device ignored\n",
1138 __func__);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001139 return;
1140 }
1141
1142 /* Add new L2T entry */
David S. Miller534cb282012-07-02 22:35:31 -07001143 e = t3_l2t_get(tdev, new, newdev, daddr);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001144 if (!e) {
Joe Perches428ac432013-01-06 13:34:49 +00001145 pr_err("%s: couldn't allocate new l2t entry!\n", __func__);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001146 return;
1147 }
1148
1149 /* Walk tid table and notify clients of dst change. */
1150 ti = &(T3C_DATA(tdev))->tid_maps;
1151 for (tid = 0; tid < ti->ntids; tid++) {
1152 te = lookup_tid(ti, tid);
1153 BUG_ON(!te);
Divy Le Ray606fcd02007-04-17 11:06:30 -07001154 if (te && te->ctx && te->client && te->client->redirect) {
Divy Le Ray4d22de32007-01-18 22:04:14 -05001155 update_tcb = te->client->redirect(te->ctx, old, new, e);
1156 if (update_tcb) {
Neil Hormane48f1292011-09-06 13:59:13 -04001157 rcu_read_lock();
Divy Le Ray4d22de32007-01-18 22:04:14 -05001158 l2t_hold(L2DATA(tdev), e);
Neil Hormane48f1292011-09-06 13:59:13 -04001159 rcu_read_unlock();
Divy Le Ray4d22de32007-01-18 22:04:14 -05001160 set_l2t_ix(tdev, tid, e);
1161 }
1162 }
1163 }
Neil Hormane48f1292011-09-06 13:59:13 -04001164 l2t_release(tdev, e);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001165}
1166
1167/*
1168 * Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
1169 * The allocated memory is cleared.
1170 */
1171void *cxgb_alloc_mem(unsigned long size)
1172{
Eric Dumazet89bf67f2010-11-22 00:15:06 +00001173 void *p = kzalloc(size, GFP_KERNEL);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001174
1175 if (!p)
Eric Dumazet89bf67f2010-11-22 00:15:06 +00001176 p = vzalloc(size);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001177 return p;
1178}
1179
1180/*
1181 * Free memory allocated through t3_alloc_mem().
1182 */
1183void cxgb_free_mem(void *addr)
1184{
Christoph Lameter9e2779f2008-02-04 22:28:34 -08001185 if (is_vmalloc_addr(addr))
Divy Le Ray4d22de32007-01-18 22:04:14 -05001186 vfree(addr);
1187 else
1188 kfree(addr);
1189}
1190
1191/*
1192 * Allocate and initialize the TID tables. Returns 0 on success.
1193 */
1194static int init_tid_tabs(struct tid_info *t, unsigned int ntids,
1195 unsigned int natids, unsigned int nstids,
1196 unsigned int atid_base, unsigned int stid_base)
1197{
1198 unsigned long size = ntids * sizeof(*t->tid_tab) +
1199 natids * sizeof(*t->atid_tab) + nstids * sizeof(*t->stid_tab);
1200
1201 t->tid_tab = cxgb_alloc_mem(size);
1202 if (!t->tid_tab)
1203 return -ENOMEM;
1204
1205 t->stid_tab = (union listen_entry *)&t->tid_tab[ntids];
1206 t->atid_tab = (union active_open_entry *)&t->stid_tab[nstids];
1207 t->ntids = ntids;
1208 t->nstids = nstids;
1209 t->stid_base = stid_base;
1210 t->sfree = NULL;
1211 t->natids = natids;
1212 t->atid_base = atid_base;
1213 t->afree = NULL;
1214 t->stids_in_use = t->atids_in_use = 0;
1215 atomic_set(&t->tids_in_use, 0);
1216 spin_lock_init(&t->stid_lock);
1217 spin_lock_init(&t->atid_lock);
1218
1219 /*
1220 * Setup the free lists for stid_tab and atid_tab.
1221 */
1222 if (nstids) {
1223 while (--nstids)
1224 t->stid_tab[nstids - 1].next = &t->stid_tab[nstids];
1225 t->sfree = t->stid_tab;
1226 }
1227 if (natids) {
1228 while (--natids)
1229 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1230 t->afree = t->atid_tab;
1231 }
1232 return 0;
1233}
1234
1235static void free_tid_maps(struct tid_info *t)
1236{
1237 cxgb_free_mem(t->tid_tab);
1238}
1239
1240static inline void add_adapter(struct adapter *adap)
1241{
1242 write_lock_bh(&adapter_list_lock);
1243 list_add_tail(&adap->adapter_list, &adapter_list);
1244 write_unlock_bh(&adapter_list_lock);
1245}
1246
1247static inline void remove_adapter(struct adapter *adap)
1248{
1249 write_lock_bh(&adapter_list_lock);
1250 list_del(&adap->adapter_list);
1251 write_unlock_bh(&adapter_list_lock);
1252}
1253
1254int cxgb3_offload_activate(struct adapter *adapter)
1255{
1256 struct t3cdev *dev = &adapter->tdev;
1257 int natids, err;
1258 struct t3c_data *t;
1259 struct tid_range stid_range, tid_range;
1260 struct mtutab mtutab;
1261 unsigned int l2t_capacity;
1262
Julia Lawall75ed0a82009-12-18 21:16:52 +00001263 t = kzalloc(sizeof(*t), GFP_KERNEL);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001264 if (!t)
1265 return -ENOMEM;
1266
1267 err = -EOPNOTSUPP;
1268 if (dev->ctl(dev, GET_TX_MAX_CHUNK, &t->tx_max_chunk) < 0 ||
1269 dev->ctl(dev, GET_MAX_OUTSTANDING_WR, &t->max_wrs) < 0 ||
1270 dev->ctl(dev, GET_L2T_CAPACITY, &l2t_capacity) < 0 ||
1271 dev->ctl(dev, GET_MTUS, &mtutab) < 0 ||
1272 dev->ctl(dev, GET_TID_RANGE, &tid_range) < 0 ||
1273 dev->ctl(dev, GET_STID_RANGE, &stid_range) < 0)
1274 goto out_free;
1275
1276 err = -ENOMEM;
Neil Hormane48f1292011-09-06 13:59:13 -04001277 RCU_INIT_POINTER(dev->l2opt, t3_init_l2t(l2t_capacity));
Divy Le Ray4d22de32007-01-18 22:04:14 -05001278 if (!L2DATA(dev))
1279 goto out_free;
1280
1281 natids = min(tid_range.num / 2, MAX_ATIDS);
1282 err = init_tid_tabs(&t->tid_maps, tid_range.num, natids,
1283 stid_range.num, ATID_BASE, stid_range.base);
1284 if (err)
1285 goto out_free_l2t;
1286
1287 t->mtus = mtutab.mtus;
1288 t->nmtus = mtutab.size;
1289
1290 INIT_WORK(&t->tid_release_task, t3_process_tid_release_list);
1291 spin_lock_init(&t->tid_release_lock);
1292 INIT_LIST_HEAD(&t->list_node);
1293 t->dev = dev;
1294
1295 T3C_DATA(dev) = t;
1296 dev->recv = process_rx;
1297 dev->neigh_update = t3_l2t_update;
1298
1299 /* Register netevent handler once */
1300 if (list_empty(&adapter_list))
1301 register_netevent_notifier(&nb);
1302
Divy Le Ray74b793e2009-06-09 23:25:21 +00001303 t->nofail_skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_KERNEL);
1304 t->release_list_incomplete = 0;
1305
Divy Le Ray4d22de32007-01-18 22:04:14 -05001306 add_adapter(adapter);
1307 return 0;
1308
1309out_free_l2t:
1310 t3_free_l2t(L2DATA(dev));
Eric Dumazet2cfa5a02011-11-23 07:09:32 +00001311 RCU_INIT_POINTER(dev->l2opt, NULL);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001312out_free:
1313 kfree(t);
1314 return err;
1315}
1316
Neil Hormane48f1292011-09-06 13:59:13 -04001317static void clean_l2_data(struct rcu_head *head)
1318{
1319 struct l2t_data *d = container_of(head, struct l2t_data, rcu_head);
1320 t3_free_l2t(d);
1321}
1322
1323
Divy Le Ray4d22de32007-01-18 22:04:14 -05001324void cxgb3_offload_deactivate(struct adapter *adapter)
1325{
1326 struct t3cdev *tdev = &adapter->tdev;
1327 struct t3c_data *t = T3C_DATA(tdev);
Neil Hormane48f1292011-09-06 13:59:13 -04001328 struct l2t_data *d;
Divy Le Ray4d22de32007-01-18 22:04:14 -05001329
1330 remove_adapter(adapter);
1331 if (list_empty(&adapter_list))
1332 unregister_netevent_notifier(&nb);
1333
1334 free_tid_maps(&t->tid_maps);
1335 T3C_DATA(tdev) = NULL;
Neil Hormane48f1292011-09-06 13:59:13 -04001336 rcu_read_lock();
1337 d = L2DATA(tdev);
1338 rcu_read_unlock();
Eric Dumazet2cfa5a02011-11-23 07:09:32 +00001339 RCU_INIT_POINTER(tdev->l2opt, NULL);
Neil Hormane48f1292011-09-06 13:59:13 -04001340 call_rcu(&d->rcu_head, clean_l2_data);
Divy Le Ray74b793e2009-06-09 23:25:21 +00001341 if (t->nofail_skb)
1342 kfree_skb(t->nofail_skb);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001343 kfree(t);
1344}
1345
1346static inline void register_tdev(struct t3cdev *tdev)
1347{
1348 static int unit;
1349
1350 mutex_lock(&cxgb3_db_lock);
1351 snprintf(tdev->name, sizeof(tdev->name), "ofld_dev%d", unit++);
1352 list_add_tail(&tdev->ofld_dev_list, &ofld_dev_list);
1353 mutex_unlock(&cxgb3_db_lock);
1354}
1355
1356static inline void unregister_tdev(struct t3cdev *tdev)
1357{
1358 mutex_lock(&cxgb3_db_lock);
1359 list_del(&tdev->ofld_dev_list);
1360 mutex_unlock(&cxgb3_db_lock);
1361}
1362
Divy Le Ray8f85cd72008-06-23 11:02:59 -07001363static inline int adap2type(struct adapter *adapter)
1364{
1365 int type = 0;
1366
1367 switch (adapter->params.rev) {
1368 case T3_REV_A:
1369 type = T3A;
1370 break;
1371 case T3_REV_B:
1372 case T3_REV_B2:
1373 type = T3B;
1374 break;
1375 case T3_REV_C:
1376 type = T3C;
1377 break;
1378 }
1379 return type;
1380}
1381
Bill Pemberton2109eaa2012-12-03 09:23:01 -05001382void cxgb3_adapter_ofld(struct adapter *adapter)
Divy Le Ray4d22de32007-01-18 22:04:14 -05001383{
1384 struct t3cdev *tdev = &adapter->tdev;
1385
1386 INIT_LIST_HEAD(&tdev->ofld_dev_list);
1387
1388 cxgb3_set_dummy_ops(tdev);
1389 tdev->send = t3_offload_tx;
1390 tdev->ctl = cxgb_offload_ctl;
Divy Le Ray8f85cd72008-06-23 11:02:59 -07001391 tdev->type = adap2type(adapter);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001392
1393 register_tdev(tdev);
1394}
1395
Bill Pemberton2109eaa2012-12-03 09:23:01 -05001396void cxgb3_adapter_unofld(struct adapter *adapter)
Divy Le Ray4d22de32007-01-18 22:04:14 -05001397{
1398 struct t3cdev *tdev = &adapter->tdev;
1399
1400 tdev->recv = NULL;
1401 tdev->neigh_update = NULL;
1402
1403 unregister_tdev(tdev);
1404}
1405
1406void __init cxgb3_offload_init(void)
1407{
1408 int i;
1409
1410 for (i = 0; i < NUM_CPL_CMDS; ++i)
1411 cpl_handlers[i] = do_bad_cpl;
1412
1413 t3_register_cpl_handler(CPL_SMT_WRITE_RPL, do_smt_write_rpl);
1414 t3_register_cpl_handler(CPL_L2T_WRITE_RPL, do_l2t_write_rpl);
Divy Le Rayb8819552007-12-17 18:47:31 -08001415 t3_register_cpl_handler(CPL_RTE_WRITE_RPL, do_rte_write_rpl);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001416 t3_register_cpl_handler(CPL_PASS_OPEN_RPL, do_stid_rpl);
1417 t3_register_cpl_handler(CPL_CLOSE_LISTSRV_RPL, do_stid_rpl);
1418 t3_register_cpl_handler(CPL_PASS_ACCEPT_REQ, do_cr);
1419 t3_register_cpl_handler(CPL_PASS_ESTABLISH, do_hwtid_rpl);
1420 t3_register_cpl_handler(CPL_ABORT_RPL_RSS, do_hwtid_rpl);
1421 t3_register_cpl_handler(CPL_ABORT_RPL, do_hwtid_rpl);
1422 t3_register_cpl_handler(CPL_RX_URG_NOTIFY, do_hwtid_rpl);
1423 t3_register_cpl_handler(CPL_RX_DATA, do_hwtid_rpl);
1424 t3_register_cpl_handler(CPL_TX_DATA_ACK, do_hwtid_rpl);
1425 t3_register_cpl_handler(CPL_TX_DMA_ACK, do_hwtid_rpl);
1426 t3_register_cpl_handler(CPL_ACT_OPEN_RPL, do_act_open_rpl);
1427 t3_register_cpl_handler(CPL_PEER_CLOSE, do_hwtid_rpl);
1428 t3_register_cpl_handler(CPL_CLOSE_CON_RPL, do_hwtid_rpl);
1429 t3_register_cpl_handler(CPL_ABORT_REQ_RSS, do_abort_req_rss);
1430 t3_register_cpl_handler(CPL_ACT_ESTABLISH, do_act_establish);
Divy Le Ray6cdbd772007-04-09 20:10:33 -07001431 t3_register_cpl_handler(CPL_SET_TCB_RPL, do_hwtid_rpl);
1432 t3_register_cpl_handler(CPL_GET_TCB_RPL, do_hwtid_rpl);
Divy Le Ray4d22de32007-01-18 22:04:14 -05001433 t3_register_cpl_handler(CPL_RDMA_TERMINATE, do_term);
1434 t3_register_cpl_handler(CPL_RDMA_EC_STATUS, do_hwtid_rpl);
1435 t3_register_cpl_handler(CPL_TRACE_PKT, do_trace);
1436 t3_register_cpl_handler(CPL_RX_DATA_DDP, do_hwtid_rpl);
1437 t3_register_cpl_handler(CPL_RX_DDP_COMPLETE, do_hwtid_rpl);
1438 t3_register_cpl_handler(CPL_ISCSI_HDR, do_hwtid_rpl);
1439}