2 * Copyright (c) 2006-2008 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 = 0x10000;
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,
187 unsigned int val = 0;
188 struct ulp_iscsi_info *uiip = data;
191 case ULP_ISCSI_GET_PARAMS:
192 uiip->pdev = adapter->pdev;
193 uiip->llimit = t3_read_reg(adapter, A_ULPRX_ISCSI_LLIMIT);
194 uiip->ulimit = t3_read_reg(adapter, A_ULPRX_ISCSI_ULIMIT);
195 uiip->tagmask = t3_read_reg(adapter, A_ULPRX_ISCSI_TAGMASK);
197 val = t3_read_reg(adapter, A_ULPRX_ISCSI_PSZ);
198 for (i = 0; i < 4; i++, val >>= 8)
199 uiip->pgsz_factor[i] = val & 0xFF;
201 val = t3_read_reg(adapter, A_TP_PARA_REG7);
203 uiip->max_rxsz = min((val >> S_PMMAXXFERLEN0)&M_PMMAXXFERLEN0,
204 (val >> S_PMMAXXFERLEN1)&M_PMMAXXFERLEN1);
206 * On tx, the iscsi pdu has to be <= tx page size and has to
207 * fit into the Tx PM FIFO.
209 val = min(adapter->params.tp.tx_pg_size,
210 t3_read_reg(adapter, A_PM1_TX_CFG) >> 17);
211 uiip->max_txsz = min(val, uiip->max_txsz);
213 /* set MaxRxData to 16224 */
214 val = t3_read_reg(adapter, A_TP_PARA_REG2);
215 if ((val >> S_MAXRXDATA) != 0x3f60) {
216 val &= (M_RXCOALESCESIZE << S_RXCOALESCESIZE);
217 val |= V_MAXRXDATA(0x3f60);
219 "%s, iscsi set MaxRxData to 16224 (0x%x).\n",
221 t3_write_reg(adapter, A_TP_PARA_REG2, val);
225 * on rx, the iscsi pdu has to be < rx page size and the
226 * the max rx data length programmed in TP
228 val = min(adapter->params.tp.rx_pg_size,
229 ((t3_read_reg(adapter, A_TP_PARA_REG2)) >>
230 S_MAXRXDATA) & M_MAXRXDATA);
231 uiip->max_rxsz = min(val, uiip->max_rxsz);
233 case ULP_ISCSI_SET_PARAMS:
234 t3_write_reg(adapter, A_ULPRX_ISCSI_TAGMASK, uiip->tagmask);
235 /* program the ddp page sizes */
236 for (i = 0; i < 4; i++)
237 val |= (uiip->pgsz_factor[i] & 0xF) << (8 * i);
238 if (val && (val != t3_read_reg(adapter, A_ULPRX_ISCSI_PSZ))) {
240 "%s, setting iscsi pgsz 0x%x, %u,%u,%u,%u.\n",
241 adapter->name, val, uiip->pgsz_factor[0],
242 uiip->pgsz_factor[1], uiip->pgsz_factor[2],
243 uiip->pgsz_factor[3]);
244 t3_write_reg(adapter, A_ULPRX_ISCSI_PSZ, val);
253 /* Response queue used for RDMA events. */
254 #define ASYNC_NOTIF_RSPQ 0
256 static int cxgb_rdma_ctl(struct adapter *adapter, unsigned int req, void *data)
261 case RDMA_GET_PARAMS: {
262 struct rdma_info *rdma = data;
263 struct pci_dev *pdev = adapter->pdev;
265 rdma->udbell_physbase = pci_resource_start(pdev, 2);
266 rdma->udbell_len = pci_resource_len(pdev, 2);
268 t3_read_reg(adapter, A_ULPTX_TPT_LLIMIT);
269 rdma->tpt_top = t3_read_reg(adapter, A_ULPTX_TPT_ULIMIT);
271 t3_read_reg(adapter, A_ULPTX_PBL_LLIMIT);
272 rdma->pbl_top = t3_read_reg(adapter, A_ULPTX_PBL_ULIMIT);
273 rdma->rqt_base = t3_read_reg(adapter, A_ULPRX_RQ_LLIMIT);
274 rdma->rqt_top = t3_read_reg(adapter, A_ULPRX_RQ_ULIMIT);
275 rdma->kdb_addr = adapter->regs + A_SG_KDOORBELL;
281 struct rdma_cq_op *rdma = data;
283 /* may be called in any context */
284 spin_lock_irqsave(&adapter->sge.reg_lock, flags);
285 ret = t3_sge_cqcntxt_op(adapter, rdma->id, rdma->op,
287 spin_unlock_irqrestore(&adapter->sge.reg_lock, flags);
291 struct ch_mem_range *t = data;
294 if ((t->addr & 7) || (t->len & 7))
296 if (t->mem_id == MEM_CM)
298 else if (t->mem_id == MEM_PMRX)
299 mem = &adapter->pmrx;
300 else if (t->mem_id == MEM_PMTX)
301 mem = &adapter->pmtx;
306 t3_mc7_bd_read(mem, t->addr / 8, t->len / 8,
313 struct rdma_cq_setup *rdma = data;
315 spin_lock_irq(&adapter->sge.reg_lock);
317 t3_sge_init_cqcntxt(adapter, rdma->id,
318 rdma->base_addr, rdma->size,
320 rdma->ovfl_mode, rdma->credits,
322 spin_unlock_irq(&adapter->sge.reg_lock);
325 case RDMA_CQ_DISABLE:
326 spin_lock_irq(&adapter->sge.reg_lock);
327 ret = t3_sge_disable_cqcntxt(adapter, *(unsigned int *)data);
328 spin_unlock_irq(&adapter->sge.reg_lock);
330 case RDMA_CTRL_QP_SETUP:{
331 struct rdma_ctrlqp_setup *rdma = data;
333 spin_lock_irq(&adapter->sge.reg_lock);
334 ret = t3_sge_init_ecntxt(adapter, FW_RI_SGEEC_START, 0,
337 rdma->base_addr, rdma->size,
338 FW_RI_TID_START, 1, 0);
339 spin_unlock_irq(&adapter->sge.reg_lock);
343 spin_lock(&adapter->stats_lock);
344 t3_tp_get_mib_stats(adapter, (struct tp_mib_stats *)data);
345 spin_unlock(&adapter->stats_lock);
354 static int cxgb_offload_ctl(struct t3cdev *tdev, unsigned int req, void *data)
356 struct adapter *adapter = tdev2adap(tdev);
357 struct tid_range *tid;
359 struct iff_mac *iffmacp;
360 struct ddp_params *ddpp;
361 struct adap_ports *ports;
362 struct ofld_page_info *rx_page_info;
363 struct tp_params *tp = &adapter->params.tp;
367 case GET_MAX_OUTSTANDING_WR:
368 *(unsigned int *)data = FW_WR_NUM;
371 *(unsigned int *)data = WR_FLITS;
373 case GET_TX_MAX_CHUNK:
374 *(unsigned int *)data = 1 << 20; /* 1MB */
378 tid->num = t3_mc5_size(&adapter->mc5) -
379 adapter->params.mc5.nroutes -
380 adapter->params.mc5.nfilters - adapter->params.mc5.nservers;
385 tid->num = adapter->params.mc5.nservers;
386 tid->base = t3_mc5_size(&adapter->mc5) - tid->num -
387 adapter->params.mc5.nfilters - adapter->params.mc5.nroutes;
389 case GET_L2T_CAPACITY:
390 *(unsigned int *)data = 2048;
395 mtup->mtus = adapter->params.mtus;
397 case GET_IFF_FROM_MAC:
399 iffmacp->dev = get_iff_from_mac(adapter, iffmacp->mac_addr,
405 ddpp->llimit = t3_read_reg(adapter, A_ULPRX_TDDP_LLIMIT);
406 ddpp->ulimit = t3_read_reg(adapter, A_ULPRX_TDDP_ULIMIT);
407 ddpp->tag_mask = t3_read_reg(adapter, A_ULPRX_TDDP_TAGMASK);
411 ports->nports = adapter->params.nports;
412 for_each_port(adapter, i)
413 ports->lldevs[i] = adapter->port[i];
415 case ULP_ISCSI_GET_PARAMS:
416 case ULP_ISCSI_SET_PARAMS:
417 if (!offload_running(adapter))
419 return cxgb_ulp_iscsi_ctl(adapter, req, data);
420 case RDMA_GET_PARAMS:
423 case RDMA_CQ_DISABLE:
424 case RDMA_CTRL_QP_SETUP:
427 if (!offload_running(adapter))
429 return cxgb_rdma_ctl(adapter, req, data);
430 case GET_RX_PAGE_INFO:
432 rx_page_info->page_size = tp->rx_pg_size;
433 rx_page_info->num = tp->rx_num_pgs;
435 case GET_ISCSI_IPV4ADDR: {
436 struct iscsi_ipv4addr *p = data;
437 struct port_info *pi = netdev_priv(p->dev);
438 p->ipv4addr = pi->iscsi_ipv4addr;
448 * Dummy handler for Rx offload packets in case we get an offload packet before
449 * proper processing is setup. This complains and drops the packet as it isn't
450 * normal to get offload packets at this stage.
452 static int rx_offload_blackhole(struct t3cdev *dev, struct sk_buff **skbs,
456 dev_kfree_skb_any(skbs[n]);
460 static void dummy_neigh_update(struct t3cdev *dev, struct neighbour *neigh)
464 void cxgb3_set_dummy_ops(struct t3cdev *dev)
466 dev->recv = rx_offload_blackhole;
467 dev->neigh_update = dummy_neigh_update;
471 * Free an active-open TID.
473 void *cxgb3_free_atid(struct t3cdev *tdev, int atid)
475 struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
476 union active_open_entry *p = atid2entry(t, atid);
477 void *ctx = p->t3c_tid.ctx;
479 spin_lock_bh(&t->atid_lock);
483 spin_unlock_bh(&t->atid_lock);
488 EXPORT_SYMBOL(cxgb3_free_atid);
491 * Free a server TID and return it to the free pool.
493 void cxgb3_free_stid(struct t3cdev *tdev, int stid)
495 struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
496 union listen_entry *p = stid2entry(t, stid);
498 spin_lock_bh(&t->stid_lock);
502 spin_unlock_bh(&t->stid_lock);
505 EXPORT_SYMBOL(cxgb3_free_stid);
507 void cxgb3_insert_tid(struct t3cdev *tdev, struct cxgb3_client *client,
508 void *ctx, unsigned int tid)
510 struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
512 t->tid_tab[tid].client = client;
513 t->tid_tab[tid].ctx = ctx;
514 atomic_inc(&t->tids_in_use);
517 EXPORT_SYMBOL(cxgb3_insert_tid);
520 * Populate a TID_RELEASE WR. The skb must be already propely sized.
522 static inline void mk_tid_release(struct sk_buff *skb, unsigned int tid)
524 struct cpl_tid_release *req;
526 skb->priority = CPL_PRIORITY_SETUP;
527 req = (struct cpl_tid_release *)__skb_put(skb, sizeof(*req));
528 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
529 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
532 static void t3_process_tid_release_list(struct work_struct *work)
534 struct t3c_data *td = container_of(work, struct t3c_data,
537 struct t3cdev *tdev = td->dev;
540 spin_lock_bh(&td->tid_release_lock);
541 while (td->tid_release_list) {
542 struct t3c_tid_entry *p = td->tid_release_list;
544 td->tid_release_list = (struct t3c_tid_entry *)p->ctx;
545 spin_unlock_bh(&td->tid_release_lock);
547 skb = alloc_skb(sizeof(struct cpl_tid_release),
548 GFP_KERNEL | __GFP_NOFAIL);
549 mk_tid_release(skb, p - td->tid_maps.tid_tab);
550 cxgb3_ofld_send(tdev, skb);
552 spin_lock_bh(&td->tid_release_lock);
554 spin_unlock_bh(&td->tid_release_lock);
557 /* use ctx as a next pointer in the tid release list */
558 void cxgb3_queue_tid_release(struct t3cdev *tdev, unsigned int tid)
560 struct t3c_data *td = T3C_DATA(tdev);
561 struct t3c_tid_entry *p = &td->tid_maps.tid_tab[tid];
563 spin_lock_bh(&td->tid_release_lock);
564 p->ctx = (void *)td->tid_release_list;
566 td->tid_release_list = p;
568 schedule_work(&td->tid_release_task);
569 spin_unlock_bh(&td->tid_release_lock);
572 EXPORT_SYMBOL(cxgb3_queue_tid_release);
575 * Remove a tid from the TID table. A client may defer processing its last
576 * CPL message if it is locked at the time it arrives, and while the message
577 * sits in the client's backlog the TID may be reused for another connection.
578 * To handle this we atomically switch the TID association if it still points
579 * to the original client context.
581 void cxgb3_remove_tid(struct t3cdev *tdev, void *ctx, unsigned int tid)
583 struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
585 BUG_ON(tid >= t->ntids);
586 if (tdev->type == T3A)
587 (void)cmpxchg(&t->tid_tab[tid].ctx, ctx, NULL);
591 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
593 mk_tid_release(skb, tid);
594 cxgb3_ofld_send(tdev, skb);
595 t->tid_tab[tid].ctx = NULL;
597 cxgb3_queue_tid_release(tdev, tid);
599 atomic_dec(&t->tids_in_use);
602 EXPORT_SYMBOL(cxgb3_remove_tid);
604 int cxgb3_alloc_atid(struct t3cdev *tdev, struct cxgb3_client *client,
608 struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
610 spin_lock_bh(&t->atid_lock);
612 t->atids_in_use + atomic_read(&t->tids_in_use) + MC5_MIN_TIDS <=
614 union active_open_entry *p = t->afree;
616 atid = (p - t->atid_tab) + t->atid_base;
618 p->t3c_tid.ctx = ctx;
619 p->t3c_tid.client = client;
622 spin_unlock_bh(&t->atid_lock);
626 EXPORT_SYMBOL(cxgb3_alloc_atid);
628 int cxgb3_alloc_stid(struct t3cdev *tdev, struct cxgb3_client *client,
632 struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
634 spin_lock_bh(&t->stid_lock);
636 union listen_entry *p = t->sfree;
638 stid = (p - t->stid_tab) + t->stid_base;
640 p->t3c_tid.ctx = ctx;
641 p->t3c_tid.client = client;
644 spin_unlock_bh(&t->stid_lock);
648 EXPORT_SYMBOL(cxgb3_alloc_stid);
650 /* Get the t3cdev associated with a net_device */
651 struct t3cdev *dev2t3cdev(struct net_device *dev)
653 const struct port_info *pi = netdev_priv(dev);
655 return (struct t3cdev *)pi->adapter;
658 EXPORT_SYMBOL(dev2t3cdev);
660 static int do_smt_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
662 struct cpl_smt_write_rpl *rpl = cplhdr(skb);
664 if (rpl->status != CPL_ERR_NONE)
666 "Unexpected SMT_WRITE_RPL status %u for entry %u\n",
667 rpl->status, GET_TID(rpl));
669 return CPL_RET_BUF_DONE;
672 static int do_l2t_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
674 struct cpl_l2t_write_rpl *rpl = cplhdr(skb);
676 if (rpl->status != CPL_ERR_NONE)
678 "Unexpected L2T_WRITE_RPL status %u for entry %u\n",
679 rpl->status, GET_TID(rpl));
681 return CPL_RET_BUF_DONE;
684 static int do_rte_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
686 struct cpl_rte_write_rpl *rpl = cplhdr(skb);
688 if (rpl->status != CPL_ERR_NONE)
690 "Unexpected RTE_WRITE_RPL status %u for entry %u\n",
691 rpl->status, GET_TID(rpl));
693 return CPL_RET_BUF_DONE;
696 static int do_act_open_rpl(struct t3cdev *dev, struct sk_buff *skb)
698 struct cpl_act_open_rpl *rpl = cplhdr(skb);
699 unsigned int atid = G_TID(ntohl(rpl->atid));
700 struct t3c_tid_entry *t3c_tid;
702 t3c_tid = lookup_atid(&(T3C_DATA(dev))->tid_maps, atid);
703 if (t3c_tid && t3c_tid->ctx && t3c_tid->client &&
704 t3c_tid->client->handlers &&
705 t3c_tid->client->handlers[CPL_ACT_OPEN_RPL]) {
706 return t3c_tid->client->handlers[CPL_ACT_OPEN_RPL] (dev, skb,
710 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
711 dev->name, CPL_ACT_OPEN_RPL);
712 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
716 static int do_stid_rpl(struct t3cdev *dev, struct sk_buff *skb)
718 union opcode_tid *p = cplhdr(skb);
719 unsigned int stid = G_TID(ntohl(p->opcode_tid));
720 struct t3c_tid_entry *t3c_tid;
722 t3c_tid = lookup_stid(&(T3C_DATA(dev))->tid_maps, stid);
723 if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
724 t3c_tid->client->handlers[p->opcode]) {
725 return t3c_tid->client->handlers[p->opcode] (dev, skb,
728 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
729 dev->name, p->opcode);
730 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
734 static int do_hwtid_rpl(struct t3cdev *dev, struct sk_buff *skb)
736 union opcode_tid *p = cplhdr(skb);
737 unsigned int hwtid = G_TID(ntohl(p->opcode_tid));
738 struct t3c_tid_entry *t3c_tid;
740 t3c_tid = lookup_tid(&(T3C_DATA(dev))->tid_maps, hwtid);
741 if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
742 t3c_tid->client->handlers[p->opcode]) {
743 return t3c_tid->client->handlers[p->opcode]
744 (dev, skb, t3c_tid->ctx);
746 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
747 dev->name, p->opcode);
748 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
752 static int do_cr(struct t3cdev *dev, struct sk_buff *skb)
754 struct cpl_pass_accept_req *req = cplhdr(skb);
755 unsigned int stid = G_PASS_OPEN_TID(ntohl(req->tos_tid));
756 struct tid_info *t = &(T3C_DATA(dev))->tid_maps;
757 struct t3c_tid_entry *t3c_tid;
758 unsigned int tid = GET_TID(req);
760 if (unlikely(tid >= t->ntids)) {
761 printk("%s: passive open TID %u too large\n",
763 t3_fatal_err(tdev2adap(dev));
764 return CPL_RET_BUF_DONE;
767 t3c_tid = lookup_stid(t, stid);
768 if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
769 t3c_tid->client->handlers[CPL_PASS_ACCEPT_REQ]) {
770 return t3c_tid->client->handlers[CPL_PASS_ACCEPT_REQ]
771 (dev, skb, t3c_tid->ctx);
773 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
774 dev->name, CPL_PASS_ACCEPT_REQ);
775 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
780 * Returns an sk_buff for a reply CPL message of size len. If the input
781 * sk_buff has no other users it is trimmed and reused, otherwise a new buffer
782 * is allocated. The input skb must be of size at least len. Note that this
783 * operation does not destroy the original skb data even if it decides to reuse
786 static struct sk_buff *cxgb3_get_cpl_reply_skb(struct sk_buff *skb, size_t len,
789 if (likely(!skb_cloned(skb))) {
790 BUG_ON(skb->len < len);
791 __skb_trim(skb, len);
794 skb = alloc_skb(len, gfp);
801 static int do_abort_req_rss(struct t3cdev *dev, struct sk_buff *skb)
803 union opcode_tid *p = cplhdr(skb);
804 unsigned int hwtid = G_TID(ntohl(p->opcode_tid));
805 struct t3c_tid_entry *t3c_tid;
807 t3c_tid = lookup_tid(&(T3C_DATA(dev))->tid_maps, hwtid);
808 if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
809 t3c_tid->client->handlers[p->opcode]) {
810 return t3c_tid->client->handlers[p->opcode]
811 (dev, skb, t3c_tid->ctx);
813 struct cpl_abort_req_rss *req = cplhdr(skb);
814 struct cpl_abort_rpl *rpl;
815 struct sk_buff *reply_skb;
816 unsigned int tid = GET_TID(req);
817 u8 cmd = req->status;
819 if (req->status == CPL_ERR_RTX_NEG_ADVICE ||
820 req->status == CPL_ERR_PERSIST_NEG_ADVICE)
823 reply_skb = cxgb3_get_cpl_reply_skb(skb,
829 printk("do_abort_req_rss: couldn't get skb!\n");
832 reply_skb->priority = CPL_PRIORITY_DATA;
833 __skb_put(reply_skb, sizeof(struct cpl_abort_rpl));
834 rpl = cplhdr(reply_skb);
836 htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_RPL));
837 rpl->wr.wr_lo = htonl(V_WR_TID(tid));
838 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_ABORT_RPL, tid));
840 cxgb3_ofld_send(dev, reply_skb);
842 return CPL_RET_BUF_DONE;
846 static int do_act_establish(struct t3cdev *dev, struct sk_buff *skb)
848 struct cpl_act_establish *req = cplhdr(skb);
849 unsigned int atid = G_PASS_OPEN_TID(ntohl(req->tos_tid));
850 struct tid_info *t = &(T3C_DATA(dev))->tid_maps;
851 struct t3c_tid_entry *t3c_tid;
852 unsigned int tid = GET_TID(req);
854 if (unlikely(tid >= t->ntids)) {
855 printk("%s: active establish TID %u too large\n",
857 t3_fatal_err(tdev2adap(dev));
858 return CPL_RET_BUF_DONE;
861 t3c_tid = lookup_atid(t, atid);
862 if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
863 t3c_tid->client->handlers[CPL_ACT_ESTABLISH]) {
864 return t3c_tid->client->handlers[CPL_ACT_ESTABLISH]
865 (dev, skb, t3c_tid->ctx);
867 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
868 dev->name, CPL_ACT_ESTABLISH);
869 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
873 static int do_trace(struct t3cdev *dev, struct sk_buff *skb)
875 struct cpl_trace_pkt *p = cplhdr(skb);
877 skb->protocol = htons(0xffff);
878 skb->dev = dev->lldev;
879 skb_pull(skb, sizeof(*p));
880 skb_reset_mac_header(skb);
881 netif_receive_skb(skb);
886 * That skb would better have come from process_responses() where we abuse
887 * ->priority and ->csum to carry our data. NB: if we get to per-arch
888 * ->csum, the things might get really interesting here.
891 static inline u32 get_hwtid(struct sk_buff *skb)
893 return ntohl((__force __be32)skb->priority) >> 8 & 0xfffff;
896 static inline u32 get_opcode(struct sk_buff *skb)
898 return G_OPCODE(ntohl((__force __be32)skb->csum));
901 static int do_term(struct t3cdev *dev, struct sk_buff *skb)
903 unsigned int hwtid = get_hwtid(skb);
904 unsigned int opcode = get_opcode(skb);
905 struct t3c_tid_entry *t3c_tid;
907 t3c_tid = lookup_tid(&(T3C_DATA(dev))->tid_maps, hwtid);
908 if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
909 t3c_tid->client->handlers[opcode]) {
910 return t3c_tid->client->handlers[opcode] (dev, skb,
913 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
915 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
919 static int nb_callback(struct notifier_block *self, unsigned long event,
923 case (NETEVENT_NEIGH_UPDATE):{
924 cxgb_neigh_update((struct neighbour *)ctx);
927 case (NETEVENT_PMTU_UPDATE):
929 case (NETEVENT_REDIRECT):{
930 struct netevent_redirect *nr = ctx;
931 cxgb_redirect(nr->old, nr->new);
932 cxgb_neigh_update(nr->new->neighbour);
941 static struct notifier_block nb = {
942 .notifier_call = nb_callback
946 * Process a received packet with an unknown/unexpected CPL opcode.
948 static int do_bad_cpl(struct t3cdev *dev, struct sk_buff *skb)
950 printk(KERN_ERR "%s: received bad CPL command 0x%x\n", dev->name,
952 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
956 * Handlers for each CPL opcode
958 static cpl_handler_func cpl_handlers[NUM_CPL_CMDS];
961 * Add a new handler to the CPL dispatch table. A NULL handler may be supplied
962 * to unregister an existing handler.
964 void t3_register_cpl_handler(unsigned int opcode, cpl_handler_func h)
966 if (opcode < NUM_CPL_CMDS)
967 cpl_handlers[opcode] = h ? h : do_bad_cpl;
969 printk(KERN_ERR "T3C: handler registration for "
970 "opcode %x failed\n", opcode);
973 EXPORT_SYMBOL(t3_register_cpl_handler);
976 * T3CDEV's receive method.
978 int process_rx(struct t3cdev *dev, struct sk_buff **skbs, int n)
981 struct sk_buff *skb = *skbs++;
982 unsigned int opcode = get_opcode(skb);
983 int ret = cpl_handlers[opcode] (dev, skb);
986 if (ret & CPL_RET_UNKNOWN_TID) {
987 union opcode_tid *p = cplhdr(skb);
989 printk(KERN_ERR "%s: CPL message (opcode %u) had "
990 "unknown TID %u\n", dev->name, opcode,
991 G_TID(ntohl(p->opcode_tid)));
994 if (ret & CPL_RET_BUF_DONE)
1001 * Sends an sk_buff to a T3C driver after dealing with any active network taps.
1003 int cxgb3_ofld_send(struct t3cdev *dev, struct sk_buff *skb)
1008 r = dev->send(dev, skb);
1013 EXPORT_SYMBOL(cxgb3_ofld_send);
1015 static int is_offloading(struct net_device *dev)
1017 struct adapter *adapter;
1020 read_lock_bh(&adapter_list_lock);
1021 list_for_each_entry(adapter, &adapter_list, adapter_list) {
1022 for_each_port(adapter, i) {
1023 if (dev == adapter->port[i]) {
1024 read_unlock_bh(&adapter_list_lock);
1029 read_unlock_bh(&adapter_list_lock);
1033 void cxgb_neigh_update(struct neighbour *neigh)
1035 struct net_device *dev = neigh->dev;
1037 if (dev && (is_offloading(dev))) {
1038 struct t3cdev *tdev = dev2t3cdev(dev);
1041 t3_l2t_update(tdev, neigh);
1045 static void set_l2t_ix(struct t3cdev *tdev, u32 tid, struct l2t_entry *e)
1047 struct sk_buff *skb;
1048 struct cpl_set_tcb_field *req;
1050 skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
1052 printk(KERN_ERR "%s: cannot allocate skb!\n", __func__);
1055 skb->priority = CPL_PRIORITY_CONTROL;
1056 req = (struct cpl_set_tcb_field *)skb_put(skb, sizeof(*req));
1057 req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1058 OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, tid));
1061 req->word = htons(W_TCB_L2T_IX);
1062 req->mask = cpu_to_be64(V_TCB_L2T_IX(M_TCB_L2T_IX));
1063 req->val = cpu_to_be64(V_TCB_L2T_IX(e->idx));
1064 tdev->send(tdev, skb);
1067 void cxgb_redirect(struct dst_entry *old, struct dst_entry *new)
1069 struct net_device *olddev, *newdev;
1070 struct tid_info *ti;
1071 struct t3cdev *tdev;
1074 struct l2t_entry *e;
1075 struct t3c_tid_entry *te;
1077 olddev = old->neighbour->dev;
1078 newdev = new->neighbour->dev;
1079 if (!is_offloading(olddev))
1081 if (!is_offloading(newdev)) {
1082 printk(KERN_WARNING "%s: Redirect to non-offload "
1083 "device ignored.\n", __func__);
1086 tdev = dev2t3cdev(olddev);
1088 if (tdev != dev2t3cdev(newdev)) {
1089 printk(KERN_WARNING "%s: Redirect to different "
1090 "offload device ignored.\n", __func__);
1094 /* Add new L2T entry */
1095 e = t3_l2t_get(tdev, new->neighbour, newdev);
1097 printk(KERN_ERR "%s: couldn't allocate new l2t entry!\n",
1102 /* Walk tid table and notify clients of dst change. */
1103 ti = &(T3C_DATA(tdev))->tid_maps;
1104 for (tid = 0; tid < ti->ntids; tid++) {
1105 te = lookup_tid(ti, tid);
1107 if (te && te->ctx && te->client && te->client->redirect) {
1108 update_tcb = te->client->redirect(te->ctx, old, new, e);
1110 l2t_hold(L2DATA(tdev), e);
1111 set_l2t_ix(tdev, tid, e);
1115 l2t_release(L2DATA(tdev), e);
1119 * Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
1120 * The allocated memory is cleared.
1122 void *cxgb_alloc_mem(unsigned long size)
1124 void *p = kmalloc(size, GFP_KERNEL);
1134 * Free memory allocated through t3_alloc_mem().
1136 void cxgb_free_mem(void *addr)
1138 if (is_vmalloc_addr(addr))
1145 * Allocate and initialize the TID tables. Returns 0 on success.
1147 static int init_tid_tabs(struct tid_info *t, unsigned int ntids,
1148 unsigned int natids, unsigned int nstids,
1149 unsigned int atid_base, unsigned int stid_base)
1151 unsigned long size = ntids * sizeof(*t->tid_tab) +
1152 natids * sizeof(*t->atid_tab) + nstids * sizeof(*t->stid_tab);
1154 t->tid_tab = cxgb_alloc_mem(size);
1158 t->stid_tab = (union listen_entry *)&t->tid_tab[ntids];
1159 t->atid_tab = (union active_open_entry *)&t->stid_tab[nstids];
1162 t->stid_base = stid_base;
1165 t->atid_base = atid_base;
1167 t->stids_in_use = t->atids_in_use = 0;
1168 atomic_set(&t->tids_in_use, 0);
1169 spin_lock_init(&t->stid_lock);
1170 spin_lock_init(&t->atid_lock);
1173 * Setup the free lists for stid_tab and atid_tab.
1177 t->stid_tab[nstids - 1].next = &t->stid_tab[nstids];
1178 t->sfree = t->stid_tab;
1182 t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1183 t->afree = t->atid_tab;
1188 static void free_tid_maps(struct tid_info *t)
1190 cxgb_free_mem(t->tid_tab);
1193 static inline void add_adapter(struct adapter *adap)
1195 write_lock_bh(&adapter_list_lock);
1196 list_add_tail(&adap->adapter_list, &adapter_list);
1197 write_unlock_bh(&adapter_list_lock);
1200 static inline void remove_adapter(struct adapter *adap)
1202 write_lock_bh(&adapter_list_lock);
1203 list_del(&adap->adapter_list);
1204 write_unlock_bh(&adapter_list_lock);
1207 int cxgb3_offload_activate(struct adapter *adapter)
1209 struct t3cdev *dev = &adapter->tdev;
1212 struct tid_range stid_range, tid_range;
1213 struct mtutab mtutab;
1214 unsigned int l2t_capacity;
1216 t = kcalloc(1, sizeof(*t), GFP_KERNEL);
1221 if (dev->ctl(dev, GET_TX_MAX_CHUNK, &t->tx_max_chunk) < 0 ||
1222 dev->ctl(dev, GET_MAX_OUTSTANDING_WR, &t->max_wrs) < 0 ||
1223 dev->ctl(dev, GET_L2T_CAPACITY, &l2t_capacity) < 0 ||
1224 dev->ctl(dev, GET_MTUS, &mtutab) < 0 ||
1225 dev->ctl(dev, GET_TID_RANGE, &tid_range) < 0 ||
1226 dev->ctl(dev, GET_STID_RANGE, &stid_range) < 0)
1230 L2DATA(dev) = t3_init_l2t(l2t_capacity);
1234 natids = min(tid_range.num / 2, MAX_ATIDS);
1235 err = init_tid_tabs(&t->tid_maps, tid_range.num, natids,
1236 stid_range.num, ATID_BASE, stid_range.base);
1240 t->mtus = mtutab.mtus;
1241 t->nmtus = mtutab.size;
1243 INIT_WORK(&t->tid_release_task, t3_process_tid_release_list);
1244 spin_lock_init(&t->tid_release_lock);
1245 INIT_LIST_HEAD(&t->list_node);
1249 dev->recv = process_rx;
1250 dev->neigh_update = t3_l2t_update;
1252 /* Register netevent handler once */
1253 if (list_empty(&adapter_list))
1254 register_netevent_notifier(&nb);
1256 add_adapter(adapter);
1260 t3_free_l2t(L2DATA(dev));
1267 void cxgb3_offload_deactivate(struct adapter *adapter)
1269 struct t3cdev *tdev = &adapter->tdev;
1270 struct t3c_data *t = T3C_DATA(tdev);
1272 remove_adapter(adapter);
1273 if (list_empty(&adapter_list))
1274 unregister_netevent_notifier(&nb);
1276 free_tid_maps(&t->tid_maps);
1277 T3C_DATA(tdev) = NULL;
1278 t3_free_l2t(L2DATA(tdev));
1279 L2DATA(tdev) = NULL;
1283 static inline void register_tdev(struct t3cdev *tdev)
1287 mutex_lock(&cxgb3_db_lock);
1288 snprintf(tdev->name, sizeof(tdev->name), "ofld_dev%d", unit++);
1289 list_add_tail(&tdev->ofld_dev_list, &ofld_dev_list);
1290 mutex_unlock(&cxgb3_db_lock);
1293 static inline void unregister_tdev(struct t3cdev *tdev)
1295 mutex_lock(&cxgb3_db_lock);
1296 list_del(&tdev->ofld_dev_list);
1297 mutex_unlock(&cxgb3_db_lock);
1300 static inline int adap2type(struct adapter *adapter)
1304 switch (adapter->params.rev) {
1319 void __devinit cxgb3_adapter_ofld(struct adapter *adapter)
1321 struct t3cdev *tdev = &adapter->tdev;
1323 INIT_LIST_HEAD(&tdev->ofld_dev_list);
1325 cxgb3_set_dummy_ops(tdev);
1326 tdev->send = t3_offload_tx;
1327 tdev->ctl = cxgb_offload_ctl;
1328 tdev->type = adap2type(adapter);
1330 register_tdev(tdev);
1333 void __devexit cxgb3_adapter_unofld(struct adapter *adapter)
1335 struct t3cdev *tdev = &adapter->tdev;
1338 tdev->neigh_update = NULL;
1340 unregister_tdev(tdev);
1343 void __init cxgb3_offload_init(void)
1347 for (i = 0; i < NUM_CPL_CMDS; ++i)
1348 cpl_handlers[i] = do_bad_cpl;
1350 t3_register_cpl_handler(CPL_SMT_WRITE_RPL, do_smt_write_rpl);
1351 t3_register_cpl_handler(CPL_L2T_WRITE_RPL, do_l2t_write_rpl);
1352 t3_register_cpl_handler(CPL_RTE_WRITE_RPL, do_rte_write_rpl);
1353 t3_register_cpl_handler(CPL_PASS_OPEN_RPL, do_stid_rpl);
1354 t3_register_cpl_handler(CPL_CLOSE_LISTSRV_RPL, do_stid_rpl);
1355 t3_register_cpl_handler(CPL_PASS_ACCEPT_REQ, do_cr);
1356 t3_register_cpl_handler(CPL_PASS_ESTABLISH, do_hwtid_rpl);
1357 t3_register_cpl_handler(CPL_ABORT_RPL_RSS, do_hwtid_rpl);
1358 t3_register_cpl_handler(CPL_ABORT_RPL, do_hwtid_rpl);
1359 t3_register_cpl_handler(CPL_RX_URG_NOTIFY, do_hwtid_rpl);
1360 t3_register_cpl_handler(CPL_RX_DATA, do_hwtid_rpl);
1361 t3_register_cpl_handler(CPL_TX_DATA_ACK, do_hwtid_rpl);
1362 t3_register_cpl_handler(CPL_TX_DMA_ACK, do_hwtid_rpl);
1363 t3_register_cpl_handler(CPL_ACT_OPEN_RPL, do_act_open_rpl);
1364 t3_register_cpl_handler(CPL_PEER_CLOSE, do_hwtid_rpl);
1365 t3_register_cpl_handler(CPL_CLOSE_CON_RPL, do_hwtid_rpl);
1366 t3_register_cpl_handler(CPL_ABORT_REQ_RSS, do_abort_req_rss);
1367 t3_register_cpl_handler(CPL_ACT_ESTABLISH, do_act_establish);
1368 t3_register_cpl_handler(CPL_SET_TCB_RPL, do_hwtid_rpl);
1369 t3_register_cpl_handler(CPL_GET_TCB_RPL, do_hwtid_rpl);
1370 t3_register_cpl_handler(CPL_RDMA_TERMINATE, do_term);
1371 t3_register_cpl_handler(CPL_RDMA_EC_STATUS, do_hwtid_rpl);
1372 t3_register_cpl_handler(CPL_TRACE_PKT, do_trace);
1373 t3_register_cpl_handler(CPL_RX_DATA_DDP, do_hwtid_rpl);
1374 t3_register_cpl_handler(CPL_RX_DDP_COMPLETE, do_hwtid_rpl);
1375 t3_register_cpl_handler(CPL_ISCSI_HDR, do_hwtid_rpl);