2 * Copyright (c) 2006-2007 Chelsio, Inc. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/list.h>
34 #include <net/neighbour.h>
35 #include <linux/notifier.h>
36 #include <asm/atomic.h>
37 #include <linux/proc_fs.h>
38 #include <linux/if_vlan.h>
39 #include <net/netevent.h>
40 #include <linux/highmem.h>
41 #include <linux/vmalloc.h>
45 #include "cxgb3_ioctl.h"
46 #include "cxgb3_ctl_defs.h"
47 #include "cxgb3_defs.h"
49 #include "firmware_exports.h"
50 #include "cxgb3_offload.h"
52 static LIST_HEAD(client_list);
53 static LIST_HEAD(ofld_dev_list);
54 static DEFINE_MUTEX(cxgb3_db_lock);
56 static DEFINE_RWLOCK(adapter_list_lock);
57 static LIST_HEAD(adapter_list);
59 static const unsigned int MAX_ATIDS = 64 * 1024;
60 static const unsigned int ATID_BASE = 0x100000;
62 static inline int offload_activated(struct t3cdev *tdev)
64 const struct adapter *adapter = tdev2adap(tdev);
66 return (test_bit(OFFLOAD_DEVMAP_BIT, &adapter->open_device_map));
70 * cxgb3_register_client - register an offload client
73 * Add the client to the client list,
74 * and call backs the client for each activated offload device
76 void cxgb3_register_client(struct cxgb3_client *client)
80 mutex_lock(&cxgb3_db_lock);
81 list_add_tail(&client->client_list, &client_list);
84 list_for_each_entry(tdev, &ofld_dev_list, ofld_dev_list) {
85 if (offload_activated(tdev))
89 mutex_unlock(&cxgb3_db_lock);
92 EXPORT_SYMBOL(cxgb3_register_client);
95 * cxgb3_unregister_client - unregister an offload client
98 * Remove the client to the client list,
99 * and call backs the client for each activated offload device.
101 void cxgb3_unregister_client(struct cxgb3_client *client)
105 mutex_lock(&cxgb3_db_lock);
106 list_del(&client->client_list);
108 if (client->remove) {
109 list_for_each_entry(tdev, &ofld_dev_list, ofld_dev_list) {
110 if (offload_activated(tdev))
111 client->remove(tdev);
114 mutex_unlock(&cxgb3_db_lock);
117 EXPORT_SYMBOL(cxgb3_unregister_client);
120 * cxgb3_add_clients - activate registered clients for an offload device
121 * @tdev: the offload device
123 * Call backs all registered clients once a offload device is activated
125 void cxgb3_add_clients(struct t3cdev *tdev)
127 struct cxgb3_client *client;
129 mutex_lock(&cxgb3_db_lock);
130 list_for_each_entry(client, &client_list, client_list) {
134 mutex_unlock(&cxgb3_db_lock);
138 * cxgb3_remove_clients - deactivates registered clients
139 * for an offload device
140 * @tdev: the offload device
142 * Call backs all registered clients once a offload device is deactivated
144 void cxgb3_remove_clients(struct t3cdev *tdev)
146 struct cxgb3_client *client;
148 mutex_lock(&cxgb3_db_lock);
149 list_for_each_entry(client, &client_list, client_list) {
151 client->remove(tdev);
153 mutex_unlock(&cxgb3_db_lock);
156 static struct net_device *get_iff_from_mac(struct adapter *adapter,
157 const unsigned char *mac,
162 for_each_port(adapter, i) {
163 struct vlan_group *grp;
164 struct net_device *dev = adapter->port[i];
165 const struct port_info *p = netdev_priv(dev);
167 if (!memcmp(dev->dev_addr, mac, ETH_ALEN)) {
168 if (vlan && vlan != VLAN_VID_MASK) {
172 dev = vlan_group_get_device(grp, vlan);
182 static int cxgb_ulp_iscsi_ctl(struct adapter *adapter, unsigned int req,
186 struct ulp_iscsi_info *uiip = data;
189 case ULP_ISCSI_GET_PARAMS:
190 uiip->pdev = adapter->pdev;
191 uiip->llimit = t3_read_reg(adapter, A_ULPRX_ISCSI_LLIMIT);
192 uiip->ulimit = t3_read_reg(adapter, A_ULPRX_ISCSI_ULIMIT);
193 uiip->tagmask = t3_read_reg(adapter, A_ULPRX_ISCSI_TAGMASK);
195 * On tx, the iscsi pdu has to be <= tx page size and has to
196 * fit into the Tx PM FIFO.
198 uiip->max_txsz = min(adapter->params.tp.tx_pg_size,
199 t3_read_reg(adapter, A_PM1_TX_CFG) >> 17);
200 /* on rx, the iscsi pdu has to be < rx page size and the
201 whole pdu + cpl headers has to fit into one sge buffer */
202 uiip->max_rxsz = min_t(unsigned int,
203 adapter->params.tp.rx_pg_size,
204 (adapter->sge.qs[0].fl[1].buf_size -
205 sizeof(struct cpl_rx_data) * 2 -
206 sizeof(struct cpl_rx_data_ddp)));
208 case ULP_ISCSI_SET_PARAMS:
209 t3_write_reg(adapter, A_ULPRX_ISCSI_TAGMASK, uiip->tagmask);
217 /* Response queue used for RDMA events. */
218 #define ASYNC_NOTIF_RSPQ 0
220 static int cxgb_rdma_ctl(struct adapter *adapter, unsigned int req, void *data)
225 case RDMA_GET_PARAMS:{
226 struct rdma_info *req = data;
227 struct pci_dev *pdev = adapter->pdev;
229 req->udbell_physbase = pci_resource_start(pdev, 2);
230 req->udbell_len = pci_resource_len(pdev, 2);
232 t3_read_reg(adapter, A_ULPTX_TPT_LLIMIT);
233 req->tpt_top = t3_read_reg(adapter, A_ULPTX_TPT_ULIMIT);
235 t3_read_reg(adapter, A_ULPTX_PBL_LLIMIT);
236 req->pbl_top = t3_read_reg(adapter, A_ULPTX_PBL_ULIMIT);
237 req->rqt_base = t3_read_reg(adapter, A_ULPRX_RQ_LLIMIT);
238 req->rqt_top = t3_read_reg(adapter, A_ULPRX_RQ_ULIMIT);
239 req->kdb_addr = adapter->regs + A_SG_KDOORBELL;
245 struct rdma_cq_op *req = data;
247 /* may be called in any context */
248 spin_lock_irqsave(&adapter->sge.reg_lock, flags);
249 ret = t3_sge_cqcntxt_op(adapter, req->id, req->op,
251 spin_unlock_irqrestore(&adapter->sge.reg_lock, flags);
255 struct ch_mem_range *t = data;
258 if ((t->addr & 7) || (t->len & 7))
260 if (t->mem_id == MEM_CM)
262 else if (t->mem_id == MEM_PMRX)
263 mem = &adapter->pmrx;
264 else if (t->mem_id == MEM_PMTX)
265 mem = &adapter->pmtx;
270 t3_mc7_bd_read(mem, t->addr / 8, t->len / 8,
277 struct rdma_cq_setup *req = data;
279 spin_lock_irq(&adapter->sge.reg_lock);
281 t3_sge_init_cqcntxt(adapter, req->id,
282 req->base_addr, req->size,
284 req->ovfl_mode, req->credits,
286 spin_unlock_irq(&adapter->sge.reg_lock);
289 case RDMA_CQ_DISABLE:
290 spin_lock_irq(&adapter->sge.reg_lock);
291 ret = t3_sge_disable_cqcntxt(adapter, *(unsigned int *)data);
292 spin_unlock_irq(&adapter->sge.reg_lock);
294 case RDMA_CTRL_QP_SETUP:{
295 struct rdma_ctrlqp_setup *req = data;
297 spin_lock_irq(&adapter->sge.reg_lock);
298 ret = t3_sge_init_ecntxt(adapter, FW_RI_SGEEC_START, 0,
301 req->base_addr, req->size,
302 FW_RI_TID_START, 1, 0);
303 spin_unlock_irq(&adapter->sge.reg_lock);
312 static int cxgb_offload_ctl(struct t3cdev *tdev, unsigned int req, void *data)
314 struct adapter *adapter = tdev2adap(tdev);
315 struct tid_range *tid;
317 struct iff_mac *iffmacp;
318 struct ddp_params *ddpp;
319 struct adap_ports *ports;
323 case GET_MAX_OUTSTANDING_WR:
324 *(unsigned int *)data = FW_WR_NUM;
327 *(unsigned int *)data = WR_FLITS;
329 case GET_TX_MAX_CHUNK:
330 *(unsigned int *)data = 1 << 20; /* 1MB */
334 tid->num = t3_mc5_size(&adapter->mc5) -
335 adapter->params.mc5.nroutes -
336 adapter->params.mc5.nfilters - adapter->params.mc5.nservers;
341 tid->num = adapter->params.mc5.nservers;
342 tid->base = t3_mc5_size(&adapter->mc5) - tid->num -
343 adapter->params.mc5.nfilters - adapter->params.mc5.nroutes;
345 case GET_L2T_CAPACITY:
346 *(unsigned int *)data = 2048;
351 mtup->mtus = adapter->params.mtus;
353 case GET_IFF_FROM_MAC:
355 iffmacp->dev = get_iff_from_mac(adapter, iffmacp->mac_addr,
361 ddpp->llimit = t3_read_reg(adapter, A_ULPRX_TDDP_LLIMIT);
362 ddpp->ulimit = t3_read_reg(adapter, A_ULPRX_TDDP_ULIMIT);
363 ddpp->tag_mask = t3_read_reg(adapter, A_ULPRX_TDDP_TAGMASK);
367 ports->nports = adapter->params.nports;
368 for_each_port(adapter, i)
369 ports->lldevs[i] = adapter->port[i];
371 case ULP_ISCSI_GET_PARAMS:
372 case ULP_ISCSI_SET_PARAMS:
373 if (!offload_running(adapter))
375 return cxgb_ulp_iscsi_ctl(adapter, req, data);
376 case RDMA_GET_PARAMS:
379 case RDMA_CQ_DISABLE:
380 case RDMA_CTRL_QP_SETUP:
382 if (!offload_running(adapter))
384 return cxgb_rdma_ctl(adapter, req, data);
392 * Dummy handler for Rx offload packets in case we get an offload packet before
393 * proper processing is setup. This complains and drops the packet as it isn't
394 * normal to get offload packets at this stage.
396 static int rx_offload_blackhole(struct t3cdev *dev, struct sk_buff **skbs,
399 CH_ERR(tdev2adap(dev), "%d unexpected offload packets, first data %u\n",
400 n, ntohl(*(__be32 *)skbs[0]->data));
402 dev_kfree_skb_any(skbs[n]);
406 static void dummy_neigh_update(struct t3cdev *dev, struct neighbour *neigh)
410 void cxgb3_set_dummy_ops(struct t3cdev *dev)
412 dev->recv = rx_offload_blackhole;
413 dev->neigh_update = dummy_neigh_update;
417 * Free an active-open TID.
419 void *cxgb3_free_atid(struct t3cdev *tdev, int atid)
421 struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
422 union active_open_entry *p = atid2entry(t, atid);
423 void *ctx = p->t3c_tid.ctx;
425 spin_lock_bh(&t->atid_lock);
429 spin_unlock_bh(&t->atid_lock);
434 EXPORT_SYMBOL(cxgb3_free_atid);
437 * Free a server TID and return it to the free pool.
439 void cxgb3_free_stid(struct t3cdev *tdev, int stid)
441 struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
442 union listen_entry *p = stid2entry(t, stid);
444 spin_lock_bh(&t->stid_lock);
448 spin_unlock_bh(&t->stid_lock);
451 EXPORT_SYMBOL(cxgb3_free_stid);
453 void cxgb3_insert_tid(struct t3cdev *tdev, struct cxgb3_client *client,
454 void *ctx, unsigned int tid)
456 struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
458 t->tid_tab[tid].client = client;
459 t->tid_tab[tid].ctx = ctx;
460 atomic_inc(&t->tids_in_use);
463 EXPORT_SYMBOL(cxgb3_insert_tid);
466 * Populate a TID_RELEASE WR. The skb must be already propely sized.
468 static inline void mk_tid_release(struct sk_buff *skb, unsigned int tid)
470 struct cpl_tid_release *req;
472 skb->priority = CPL_PRIORITY_SETUP;
473 req = (struct cpl_tid_release *)__skb_put(skb, sizeof(*req));
474 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
475 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
478 static void t3_process_tid_release_list(struct work_struct *work)
480 struct t3c_data *td = container_of(work, struct t3c_data,
483 struct t3cdev *tdev = td->dev;
486 spin_lock_bh(&td->tid_release_lock);
487 while (td->tid_release_list) {
488 struct t3c_tid_entry *p = td->tid_release_list;
490 td->tid_release_list = (struct t3c_tid_entry *)p->ctx;
491 spin_unlock_bh(&td->tid_release_lock);
493 skb = alloc_skb(sizeof(struct cpl_tid_release),
494 GFP_KERNEL | __GFP_NOFAIL);
495 mk_tid_release(skb, p - td->tid_maps.tid_tab);
496 cxgb3_ofld_send(tdev, skb);
498 spin_lock_bh(&td->tid_release_lock);
500 spin_unlock_bh(&td->tid_release_lock);
503 /* use ctx as a next pointer in the tid release list */
504 void cxgb3_queue_tid_release(struct t3cdev *tdev, unsigned int tid)
506 struct t3c_data *td = T3C_DATA(tdev);
507 struct t3c_tid_entry *p = &td->tid_maps.tid_tab[tid];
509 spin_lock_bh(&td->tid_release_lock);
510 p->ctx = (void *)td->tid_release_list;
511 td->tid_release_list = p;
513 schedule_work(&td->tid_release_task);
514 spin_unlock_bh(&td->tid_release_lock);
517 EXPORT_SYMBOL(cxgb3_queue_tid_release);
520 * Remove a tid from the TID table. A client may defer processing its last
521 * CPL message if it is locked at the time it arrives, and while the message
522 * sits in the client's backlog the TID may be reused for another connection.
523 * To handle this we atomically switch the TID association if it still points
524 * to the original client context.
526 void cxgb3_remove_tid(struct t3cdev *tdev, void *ctx, unsigned int tid)
528 struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
530 BUG_ON(tid >= t->ntids);
531 if (tdev->type == T3A)
532 (void)cmpxchg(&t->tid_tab[tid].ctx, ctx, NULL);
536 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
538 mk_tid_release(skb, tid);
539 cxgb3_ofld_send(tdev, skb);
540 t->tid_tab[tid].ctx = NULL;
542 cxgb3_queue_tid_release(tdev, tid);
544 atomic_dec(&t->tids_in_use);
547 EXPORT_SYMBOL(cxgb3_remove_tid);
549 int cxgb3_alloc_atid(struct t3cdev *tdev, struct cxgb3_client *client,
553 struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
555 spin_lock_bh(&t->atid_lock);
557 t->atids_in_use + atomic_read(&t->tids_in_use) + MC5_MIN_TIDS <=
559 union active_open_entry *p = t->afree;
561 atid = (p - t->atid_tab) + t->atid_base;
563 p->t3c_tid.ctx = ctx;
564 p->t3c_tid.client = client;
567 spin_unlock_bh(&t->atid_lock);
571 EXPORT_SYMBOL(cxgb3_alloc_atid);
573 int cxgb3_alloc_stid(struct t3cdev *tdev, struct cxgb3_client *client,
577 struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
579 spin_lock_bh(&t->stid_lock);
581 union listen_entry *p = t->sfree;
583 stid = (p - t->stid_tab) + t->stid_base;
585 p->t3c_tid.ctx = ctx;
586 p->t3c_tid.client = client;
589 spin_unlock_bh(&t->stid_lock);
593 EXPORT_SYMBOL(cxgb3_alloc_stid);
595 static int do_smt_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
597 struct cpl_smt_write_rpl *rpl = cplhdr(skb);
599 if (rpl->status != CPL_ERR_NONE)
601 "Unexpected SMT_WRITE_RPL status %u for entry %u\n",
602 rpl->status, GET_TID(rpl));
604 return CPL_RET_BUF_DONE;
607 static int do_l2t_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
609 struct cpl_l2t_write_rpl *rpl = cplhdr(skb);
611 if (rpl->status != CPL_ERR_NONE)
613 "Unexpected L2T_WRITE_RPL status %u for entry %u\n",
614 rpl->status, GET_TID(rpl));
616 return CPL_RET_BUF_DONE;
619 static int do_act_open_rpl(struct t3cdev *dev, struct sk_buff *skb)
621 struct cpl_act_open_rpl *rpl = cplhdr(skb);
622 unsigned int atid = G_TID(ntohl(rpl->atid));
623 struct t3c_tid_entry *t3c_tid;
625 t3c_tid = lookup_atid(&(T3C_DATA(dev))->tid_maps, atid);
626 if (t3c_tid->ctx && t3c_tid->client && t3c_tid->client->handlers &&
627 t3c_tid->client->handlers[CPL_ACT_OPEN_RPL]) {
628 return t3c_tid->client->handlers[CPL_ACT_OPEN_RPL] (dev, skb,
632 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
633 dev->name, CPL_ACT_OPEN_RPL);
634 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
638 static int do_stid_rpl(struct t3cdev *dev, struct sk_buff *skb)
640 union opcode_tid *p = cplhdr(skb);
641 unsigned int stid = G_TID(ntohl(p->opcode_tid));
642 struct t3c_tid_entry *t3c_tid;
644 t3c_tid = lookup_stid(&(T3C_DATA(dev))->tid_maps, stid);
645 if (t3c_tid->ctx && t3c_tid->client->handlers &&
646 t3c_tid->client->handlers[p->opcode]) {
647 return t3c_tid->client->handlers[p->opcode] (dev, skb,
650 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
651 dev->name, p->opcode);
652 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
656 static int do_hwtid_rpl(struct t3cdev *dev, struct sk_buff *skb)
658 union opcode_tid *p = cplhdr(skb);
659 unsigned int hwtid = G_TID(ntohl(p->opcode_tid));
660 struct t3c_tid_entry *t3c_tid;
662 t3c_tid = lookup_tid(&(T3C_DATA(dev))->tid_maps, hwtid);
663 if (t3c_tid->ctx && t3c_tid->client->handlers &&
664 t3c_tid->client->handlers[p->opcode]) {
665 return t3c_tid->client->handlers[p->opcode]
666 (dev, skb, t3c_tid->ctx);
668 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
669 dev->name, p->opcode);
670 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
674 static int do_cr(struct t3cdev *dev, struct sk_buff *skb)
676 struct cpl_pass_accept_req *req = cplhdr(skb);
677 unsigned int stid = G_PASS_OPEN_TID(ntohl(req->tos_tid));
678 struct t3c_tid_entry *t3c_tid;
680 t3c_tid = lookup_stid(&(T3C_DATA(dev))->tid_maps, stid);
681 if (t3c_tid->ctx && t3c_tid->client->handlers &&
682 t3c_tid->client->handlers[CPL_PASS_ACCEPT_REQ]) {
683 return t3c_tid->client->handlers[CPL_PASS_ACCEPT_REQ]
684 (dev, skb, t3c_tid->ctx);
686 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
687 dev->name, CPL_PASS_ACCEPT_REQ);
688 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
692 static int do_abort_req_rss(struct t3cdev *dev, struct sk_buff *skb)
694 union opcode_tid *p = cplhdr(skb);
695 unsigned int hwtid = G_TID(ntohl(p->opcode_tid));
696 struct t3c_tid_entry *t3c_tid;
698 t3c_tid = lookup_tid(&(T3C_DATA(dev))->tid_maps, hwtid);
699 if (t3c_tid->ctx && t3c_tid->client->handlers &&
700 t3c_tid->client->handlers[p->opcode]) {
701 return t3c_tid->client->handlers[p->opcode]
702 (dev, skb, t3c_tid->ctx);
704 struct cpl_abort_req_rss *req = cplhdr(skb);
705 struct cpl_abort_rpl *rpl;
707 struct sk_buff *skb =
708 alloc_skb(sizeof(struct cpl_abort_rpl), GFP_ATOMIC);
710 printk("do_abort_req_rss: couldn't get skb!\n");
713 skb->priority = CPL_PRIORITY_DATA;
714 __skb_put(skb, sizeof(struct cpl_abort_rpl));
717 htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_RPL));
718 rpl->wr.wr_lo = htonl(V_WR_TID(GET_TID(req)));
720 htonl(MK_OPCODE_TID(CPL_ABORT_RPL, GET_TID(req)));
721 rpl->cmd = req->status;
722 cxgb3_ofld_send(dev, skb);
724 return CPL_RET_BUF_DONE;
728 static int do_act_establish(struct t3cdev *dev, struct sk_buff *skb)
730 struct cpl_act_establish *req = cplhdr(skb);
731 unsigned int atid = G_PASS_OPEN_TID(ntohl(req->tos_tid));
732 struct t3c_tid_entry *t3c_tid;
734 t3c_tid = lookup_atid(&(T3C_DATA(dev))->tid_maps, atid);
735 if (t3c_tid->ctx && t3c_tid->client->handlers &&
736 t3c_tid->client->handlers[CPL_ACT_ESTABLISH]) {
737 return t3c_tid->client->handlers[CPL_ACT_ESTABLISH]
738 (dev, skb, t3c_tid->ctx);
740 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
741 dev->name, CPL_PASS_ACCEPT_REQ);
742 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
746 static int do_trace(struct t3cdev *dev, struct sk_buff *skb)
748 struct cpl_trace_pkt *p = cplhdr(skb);
750 skb->protocol = htons(0xffff);
751 skb->dev = dev->lldev;
752 skb_pull(skb, sizeof(*p));
753 skb->mac.raw = skb->data;
754 netif_receive_skb(skb);
758 static int do_term(struct t3cdev *dev, struct sk_buff *skb)
760 unsigned int hwtid = ntohl(skb->priority) >> 8 & 0xfffff;
761 unsigned int opcode = G_OPCODE(ntohl(skb->csum));
762 struct t3c_tid_entry *t3c_tid;
764 t3c_tid = lookup_tid(&(T3C_DATA(dev))->tid_maps, hwtid);
765 if (t3c_tid->ctx && t3c_tid->client->handlers &&
766 t3c_tid->client->handlers[opcode]) {
767 return t3c_tid->client->handlers[opcode] (dev, skb,
770 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
772 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
776 static int nb_callback(struct notifier_block *self, unsigned long event,
780 case (NETEVENT_NEIGH_UPDATE):{
781 cxgb_neigh_update((struct neighbour *)ctx);
784 case (NETEVENT_PMTU_UPDATE):
786 case (NETEVENT_REDIRECT):{
787 struct netevent_redirect *nr = ctx;
788 cxgb_redirect(nr->old, nr->new);
789 cxgb_neigh_update(nr->new->neighbour);
798 static struct notifier_block nb = {
799 .notifier_call = nb_callback
803 * Process a received packet with an unknown/unexpected CPL opcode.
805 static int do_bad_cpl(struct t3cdev *dev, struct sk_buff *skb)
807 printk(KERN_ERR "%s: received bad CPL command 0x%x\n", dev->name,
809 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
813 * Handlers for each CPL opcode
815 static cpl_handler_func cpl_handlers[NUM_CPL_CMDS];
818 * Add a new handler to the CPL dispatch table. A NULL handler may be supplied
819 * to unregister an existing handler.
821 void t3_register_cpl_handler(unsigned int opcode, cpl_handler_func h)
823 if (opcode < NUM_CPL_CMDS)
824 cpl_handlers[opcode] = h ? h : do_bad_cpl;
826 printk(KERN_ERR "T3C: handler registration for "
827 "opcode %x failed\n", opcode);
830 EXPORT_SYMBOL(t3_register_cpl_handler);
833 * T3CDEV's receive method.
835 int process_rx(struct t3cdev *dev, struct sk_buff **skbs, int n)
838 struct sk_buff *skb = *skbs++;
839 unsigned int opcode = G_OPCODE(ntohl(skb->csum));
840 int ret = cpl_handlers[opcode] (dev, skb);
843 if (ret & CPL_RET_UNKNOWN_TID) {
844 union opcode_tid *p = cplhdr(skb);
846 printk(KERN_ERR "%s: CPL message (opcode %u) had "
847 "unknown TID %u\n", dev->name, opcode,
848 G_TID(ntohl(p->opcode_tid)));
851 if (ret & CPL_RET_BUF_DONE)
858 * Sends an sk_buff to a T3C driver after dealing with any active network taps.
860 int cxgb3_ofld_send(struct t3cdev *dev, struct sk_buff *skb)
865 r = dev->send(dev, skb);
870 EXPORT_SYMBOL(cxgb3_ofld_send);
872 static int is_offloading(struct net_device *dev)
874 struct adapter *adapter;
877 read_lock_bh(&adapter_list_lock);
878 list_for_each_entry(adapter, &adapter_list, adapter_list) {
879 for_each_port(adapter, i) {
880 if (dev == adapter->port[i]) {
881 read_unlock_bh(&adapter_list_lock);
886 read_unlock_bh(&adapter_list_lock);
890 void cxgb_neigh_update(struct neighbour *neigh)
892 struct net_device *dev = neigh->dev;
894 if (dev && (is_offloading(dev))) {
895 struct t3cdev *tdev = T3CDEV(dev);
898 t3_l2t_update(tdev, neigh);
902 static void set_l2t_ix(struct t3cdev *tdev, u32 tid, struct l2t_entry *e)
905 struct cpl_set_tcb_field *req;
907 skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
909 printk(KERN_ERR "%s: cannot allocate skb!\n", __FUNCTION__);
912 skb->priority = CPL_PRIORITY_CONTROL;
913 req = (struct cpl_set_tcb_field *)skb_put(skb, sizeof(*req));
914 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
915 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, tid));
918 req->word = htons(W_TCB_L2T_IX);
919 req->mask = cpu_to_be64(V_TCB_L2T_IX(M_TCB_L2T_IX));
920 req->val = cpu_to_be64(V_TCB_L2T_IX(e->idx));
921 tdev->send(tdev, skb);
924 void cxgb_redirect(struct dst_entry *old, struct dst_entry *new)
926 struct net_device *olddev, *newdev;
932 struct t3c_tid_entry *te;
934 olddev = old->neighbour->dev;
935 newdev = new->neighbour->dev;
936 if (!is_offloading(olddev))
938 if (!is_offloading(newdev)) {
939 printk(KERN_WARNING "%s: Redirect to non-offload"
940 "device ignored.\n", __FUNCTION__);
943 tdev = T3CDEV(olddev);
945 if (tdev != T3CDEV(newdev)) {
946 printk(KERN_WARNING "%s: Redirect to different "
947 "offload device ignored.\n", __FUNCTION__);
951 /* Add new L2T entry */
952 e = t3_l2t_get(tdev, new->neighbour, newdev);
954 printk(KERN_ERR "%s: couldn't allocate new l2t entry!\n",
959 /* Walk tid table and notify clients of dst change. */
960 ti = &(T3C_DATA(tdev))->tid_maps;
961 for (tid = 0; tid < ti->ntids; tid++) {
962 te = lookup_tid(ti, tid);
964 if (te->ctx && te->client && te->client->redirect) {
965 update_tcb = te->client->redirect(te->ctx, old, new, e);
967 l2t_hold(L2DATA(tdev), e);
968 set_l2t_ix(tdev, tid, e);
972 l2t_release(L2DATA(tdev), e);
976 * Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
977 * The allocated memory is cleared.
979 void *cxgb_alloc_mem(unsigned long size)
981 void *p = kmalloc(size, GFP_KERNEL);
991 * Free memory allocated through t3_alloc_mem().
993 void cxgb_free_mem(void *addr)
995 unsigned long p = (unsigned long)addr;
997 if (p >= VMALLOC_START && p < VMALLOC_END)
1004 * Allocate and initialize the TID tables. Returns 0 on success.
1006 static int init_tid_tabs(struct tid_info *t, unsigned int ntids,
1007 unsigned int natids, unsigned int nstids,
1008 unsigned int atid_base, unsigned int stid_base)
1010 unsigned long size = ntids * sizeof(*t->tid_tab) +
1011 natids * sizeof(*t->atid_tab) + nstids * sizeof(*t->stid_tab);
1013 t->tid_tab = cxgb_alloc_mem(size);
1017 t->stid_tab = (union listen_entry *)&t->tid_tab[ntids];
1018 t->atid_tab = (union active_open_entry *)&t->stid_tab[nstids];
1021 t->stid_base = stid_base;
1024 t->atid_base = atid_base;
1026 t->stids_in_use = t->atids_in_use = 0;
1027 atomic_set(&t->tids_in_use, 0);
1028 spin_lock_init(&t->stid_lock);
1029 spin_lock_init(&t->atid_lock);
1032 * Setup the free lists for stid_tab and atid_tab.
1036 t->stid_tab[nstids - 1].next = &t->stid_tab[nstids];
1037 t->sfree = t->stid_tab;
1041 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1042 t->afree = t->atid_tab;
1047 static void free_tid_maps(struct tid_info *t)
1049 cxgb_free_mem(t->tid_tab);
1052 static inline void add_adapter(struct adapter *adap)
1054 write_lock_bh(&adapter_list_lock);
1055 list_add_tail(&adap->adapter_list, &adapter_list);
1056 write_unlock_bh(&adapter_list_lock);
1059 static inline void remove_adapter(struct adapter *adap)
1061 write_lock_bh(&adapter_list_lock);
1062 list_del(&adap->adapter_list);
1063 write_unlock_bh(&adapter_list_lock);
1066 int cxgb3_offload_activate(struct adapter *adapter)
1068 struct t3cdev *dev = &adapter->tdev;
1071 struct tid_range stid_range, tid_range;
1072 struct mtutab mtutab;
1073 unsigned int l2t_capacity;
1075 t = kcalloc(1, sizeof(*t), GFP_KERNEL);
1080 if (dev->ctl(dev, GET_TX_MAX_CHUNK, &t->tx_max_chunk) < 0 ||
1081 dev->ctl(dev, GET_MAX_OUTSTANDING_WR, &t->max_wrs) < 0 ||
1082 dev->ctl(dev, GET_L2T_CAPACITY, &l2t_capacity) < 0 ||
1083 dev->ctl(dev, GET_MTUS, &mtutab) < 0 ||
1084 dev->ctl(dev, GET_TID_RANGE, &tid_range) < 0 ||
1085 dev->ctl(dev, GET_STID_RANGE, &stid_range) < 0)
1089 L2DATA(dev) = t3_init_l2t(l2t_capacity);
1093 natids = min(tid_range.num / 2, MAX_ATIDS);
1094 err = init_tid_tabs(&t->tid_maps, tid_range.num, natids,
1095 stid_range.num, ATID_BASE, stid_range.base);
1099 t->mtus = mtutab.mtus;
1100 t->nmtus = mtutab.size;
1102 INIT_WORK(&t->tid_release_task, t3_process_tid_release_list);
1103 spin_lock_init(&t->tid_release_lock);
1104 INIT_LIST_HEAD(&t->list_node);
1108 dev->recv = process_rx;
1109 dev->neigh_update = t3_l2t_update;
1111 /* Register netevent handler once */
1112 if (list_empty(&adapter_list))
1113 register_netevent_notifier(&nb);
1115 add_adapter(adapter);
1119 t3_free_l2t(L2DATA(dev));
1126 void cxgb3_offload_deactivate(struct adapter *adapter)
1128 struct t3cdev *tdev = &adapter->tdev;
1129 struct t3c_data *t = T3C_DATA(tdev);
1131 remove_adapter(adapter);
1132 if (list_empty(&adapter_list))
1133 unregister_netevent_notifier(&nb);
1135 free_tid_maps(&t->tid_maps);
1136 T3C_DATA(tdev) = NULL;
1137 t3_free_l2t(L2DATA(tdev));
1138 L2DATA(tdev) = NULL;
1142 static inline void register_tdev(struct t3cdev *tdev)
1146 mutex_lock(&cxgb3_db_lock);
1147 snprintf(tdev->name, sizeof(tdev->name), "ofld_dev%d", unit++);
1148 list_add_tail(&tdev->ofld_dev_list, &ofld_dev_list);
1149 mutex_unlock(&cxgb3_db_lock);
1152 static inline void unregister_tdev(struct t3cdev *tdev)
1154 mutex_lock(&cxgb3_db_lock);
1155 list_del(&tdev->ofld_dev_list);
1156 mutex_unlock(&cxgb3_db_lock);
1159 void __devinit cxgb3_adapter_ofld(struct adapter *adapter)
1161 struct t3cdev *tdev = &adapter->tdev;
1163 INIT_LIST_HEAD(&tdev->ofld_dev_list);
1165 cxgb3_set_dummy_ops(tdev);
1166 tdev->send = t3_offload_tx;
1167 tdev->ctl = cxgb_offload_ctl;
1168 tdev->type = adapter->params.rev == 0 ? T3A : T3B;
1170 register_tdev(tdev);
1173 void __devexit cxgb3_adapter_unofld(struct adapter *adapter)
1175 struct t3cdev *tdev = &adapter->tdev;
1178 tdev->neigh_update = NULL;
1180 unregister_tdev(tdev);
1183 void __init cxgb3_offload_init(void)
1187 for (i = 0; i < NUM_CPL_CMDS; ++i)
1188 cpl_handlers[i] = do_bad_cpl;
1190 t3_register_cpl_handler(CPL_SMT_WRITE_RPL, do_smt_write_rpl);
1191 t3_register_cpl_handler(CPL_L2T_WRITE_RPL, do_l2t_write_rpl);
1192 t3_register_cpl_handler(CPL_PASS_OPEN_RPL, do_stid_rpl);
1193 t3_register_cpl_handler(CPL_CLOSE_LISTSRV_RPL, do_stid_rpl);
1194 t3_register_cpl_handler(CPL_PASS_ACCEPT_REQ, do_cr);
1195 t3_register_cpl_handler(CPL_PASS_ESTABLISH, do_hwtid_rpl);
1196 t3_register_cpl_handler(CPL_ABORT_RPL_RSS, do_hwtid_rpl);
1197 t3_register_cpl_handler(CPL_ABORT_RPL, do_hwtid_rpl);
1198 t3_register_cpl_handler(CPL_RX_URG_NOTIFY, do_hwtid_rpl);
1199 t3_register_cpl_handler(CPL_RX_DATA, do_hwtid_rpl);
1200 t3_register_cpl_handler(CPL_TX_DATA_ACK, do_hwtid_rpl);
1201 t3_register_cpl_handler(CPL_TX_DMA_ACK, do_hwtid_rpl);
1202 t3_register_cpl_handler(CPL_ACT_OPEN_RPL, do_act_open_rpl);
1203 t3_register_cpl_handler(CPL_PEER_CLOSE, do_hwtid_rpl);
1204 t3_register_cpl_handler(CPL_CLOSE_CON_RPL, do_hwtid_rpl);
1205 t3_register_cpl_handler(CPL_ABORT_REQ_RSS, do_abort_req_rss);
1206 t3_register_cpl_handler(CPL_ACT_ESTABLISH, do_act_establish);
1207 t3_register_cpl_handler(CPL_SET_TCB_RPL, do_hwtid_rpl);
1208 t3_register_cpl_handler(CPL_GET_TCB_RPL, do_hwtid_rpl);
1209 t3_register_cpl_handler(CPL_RDMA_TERMINATE, do_term);
1210 t3_register_cpl_handler(CPL_RDMA_EC_STATUS, do_hwtid_rpl);
1211 t3_register_cpl_handler(CPL_TRACE_PKT, do_trace);
1212 t3_register_cpl_handler(CPL_RX_DATA_DDP, do_hwtid_rpl);
1213 t3_register_cpl_handler(CPL_RX_DDP_COMPLETE, do_hwtid_rpl);
1214 t3_register_cpl_handler(CPL_ISCSI_HDR, do_hwtid_rpl);