]> err.no Git - linux-2.6/blob - drivers/net/cxgb3/cxgb3_offload.c
Merge branch 'upstream' of git://ftp.linux-mips.org/pub/scm/upstream-linus
[linux-2.6] / drivers / net / cxgb3 / cxgb3_offload.c
1 /*
2  * Copyright (c) 2006-2007 Chelsio, Inc. All rights reserved.
3  *
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
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>
42
43 #include "common.h"
44 #include "regs.h"
45 #include "cxgb3_ioctl.h"
46 #include "cxgb3_ctl_defs.h"
47 #include "cxgb3_defs.h"
48 #include "l2t.h"
49 #include "firmware_exports.h"
50 #include "cxgb3_offload.h"
51
52 static LIST_HEAD(client_list);
53 static LIST_HEAD(ofld_dev_list);
54 static DEFINE_MUTEX(cxgb3_db_lock);
55
56 static DEFINE_RWLOCK(adapter_list_lock);
57 static LIST_HEAD(adapter_list);
58
59 static const unsigned int MAX_ATIDS = 64 * 1024;
60 static const unsigned int ATID_BASE = 0x10000;
61
62 static inline int offload_activated(struct t3cdev *tdev)
63 {
64         const struct adapter *adapter = tdev2adap(tdev);
65
66         return (test_bit(OFFLOAD_DEVMAP_BIT, &adapter->open_device_map));
67 }
68
69 /**
70  *      cxgb3_register_client - register an offload client
71  *      @client: the client
72  *
73  *      Add the client to the client list,
74  *      and call backs the client for each activated offload device
75  */
76 void cxgb3_register_client(struct cxgb3_client *client)
77 {
78         struct t3cdev *tdev;
79
80         mutex_lock(&cxgb3_db_lock);
81         list_add_tail(&client->client_list, &client_list);
82
83         if (client->add) {
84                 list_for_each_entry(tdev, &ofld_dev_list, ofld_dev_list) {
85                         if (offload_activated(tdev))
86                                 client->add(tdev);
87                 }
88         }
89         mutex_unlock(&cxgb3_db_lock);
90 }
91
92 EXPORT_SYMBOL(cxgb3_register_client);
93
94 /**
95  *      cxgb3_unregister_client - unregister an offload client
96  *      @client: the client
97  *
98  *      Remove the client to the client list,
99  *      and call backs the client for each activated offload device.
100  */
101 void cxgb3_unregister_client(struct cxgb3_client *client)
102 {
103         struct t3cdev *tdev;
104
105         mutex_lock(&cxgb3_db_lock);
106         list_del(&client->client_list);
107
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);
112                 }
113         }
114         mutex_unlock(&cxgb3_db_lock);
115 }
116
117 EXPORT_SYMBOL(cxgb3_unregister_client);
118
119 /**
120  *      cxgb3_add_clients - activate registered clients for an offload device
121  *      @tdev: the offload device
122  *
123  *      Call backs all registered clients once a offload device is activated
124  */
125 void cxgb3_add_clients(struct t3cdev *tdev)
126 {
127         struct cxgb3_client *client;
128
129         mutex_lock(&cxgb3_db_lock);
130         list_for_each_entry(client, &client_list, client_list) {
131                 if (client->add)
132                         client->add(tdev);
133         }
134         mutex_unlock(&cxgb3_db_lock);
135 }
136
137 /**
138  *      cxgb3_remove_clients - deactivates registered clients
139  *                             for an offload device
140  *      @tdev: the offload device
141  *
142  *      Call backs all registered clients once a offload device is deactivated
143  */
144 void cxgb3_remove_clients(struct t3cdev *tdev)
145 {
146         struct cxgb3_client *client;
147
148         mutex_lock(&cxgb3_db_lock);
149         list_for_each_entry(client, &client_list, client_list) {
150                 if (client->remove)
151                         client->remove(tdev);
152         }
153         mutex_unlock(&cxgb3_db_lock);
154 }
155
156 static struct net_device *get_iff_from_mac(struct adapter *adapter,
157                                            const unsigned char *mac,
158                                            unsigned int vlan)
159 {
160         int i;
161
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);
166
167                 if (!memcmp(dev->dev_addr, mac, ETH_ALEN)) {
168                         if (vlan && vlan != VLAN_VID_MASK) {
169                                 grp = p->vlan_grp;
170                                 dev = NULL;
171                                 if (grp)
172                                         dev = vlan_group_get_device(grp, vlan);
173                         } else
174                                 while (dev->master)
175                                         dev = dev->master;
176                         return dev;
177                 }
178         }
179         return NULL;
180 }
181
182 static int cxgb_ulp_iscsi_ctl(struct adapter *adapter, unsigned int req,
183                               void *data)
184 {
185         int ret = 0;
186         struct ulp_iscsi_info *uiip = data;
187
188         switch (req) {
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);
194                 /*
195                  * On tx, the iscsi pdu has to be <= tx page size and has to
196                  * fit into the Tx PM FIFO.
197                  */
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)));
207                 break;
208         case ULP_ISCSI_SET_PARAMS:
209                 t3_write_reg(adapter, A_ULPRX_ISCSI_TAGMASK, uiip->tagmask);
210                 break;
211         default:
212                 ret = -EOPNOTSUPP;
213         }
214         return ret;
215 }
216
217 /* Response queue used for RDMA events. */
218 #define ASYNC_NOTIF_RSPQ 0
219
220 static int cxgb_rdma_ctl(struct adapter *adapter, unsigned int req, void *data)
221 {
222         int ret = 0;
223
224         switch (req) {
225         case RDMA_GET_PARAMS: {
226                 struct rdma_info *rdma = data;
227                 struct pci_dev *pdev = adapter->pdev;
228
229                 rdma->udbell_physbase = pci_resource_start(pdev, 2);
230                 rdma->udbell_len = pci_resource_len(pdev, 2);
231                 rdma->tpt_base =
232                         t3_read_reg(adapter, A_ULPTX_TPT_LLIMIT);
233                 rdma->tpt_top = t3_read_reg(adapter, A_ULPTX_TPT_ULIMIT);
234                 rdma->pbl_base =
235                         t3_read_reg(adapter, A_ULPTX_PBL_LLIMIT);
236                 rdma->pbl_top = t3_read_reg(adapter, A_ULPTX_PBL_ULIMIT);
237                 rdma->rqt_base = t3_read_reg(adapter, A_ULPRX_RQ_LLIMIT);
238                 rdma->rqt_top = t3_read_reg(adapter, A_ULPRX_RQ_ULIMIT);
239                 rdma->kdb_addr = adapter->regs + A_SG_KDOORBELL;
240                 rdma->pdev = pdev;
241                 break;
242         }
243         case RDMA_CQ_OP:{
244                 unsigned long flags;
245                 struct rdma_cq_op *rdma = data;
246
247                 /* may be called in any context */
248                 spin_lock_irqsave(&adapter->sge.reg_lock, flags);
249                 ret = t3_sge_cqcntxt_op(adapter, rdma->id, rdma->op,
250                                         rdma->credits);
251                 spin_unlock_irqrestore(&adapter->sge.reg_lock, flags);
252                 break;
253         }
254         case RDMA_GET_MEM:{
255                 struct ch_mem_range *t = data;
256                 struct mc7 *mem;
257
258                 if ((t->addr & 7) || (t->len & 7))
259                         return -EINVAL;
260                 if (t->mem_id == MEM_CM)
261                         mem = &adapter->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;
266                 else
267                         return -EINVAL;
268
269                 ret =
270                         t3_mc7_bd_read(mem, t->addr / 8, t->len / 8,
271                                         (u64 *) t->buf);
272                 if (ret)
273                         return ret;
274                 break;
275         }
276         case RDMA_CQ_SETUP:{
277                 struct rdma_cq_setup *rdma = data;
278
279                 spin_lock_irq(&adapter->sge.reg_lock);
280                 ret =
281                         t3_sge_init_cqcntxt(adapter, rdma->id,
282                                         rdma->base_addr, rdma->size,
283                                         ASYNC_NOTIF_RSPQ,
284                                         rdma->ovfl_mode, rdma->credits,
285                                         rdma->credit_thres);
286                 spin_unlock_irq(&adapter->sge.reg_lock);
287                 break;
288         }
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);
293                 break;
294         case RDMA_CTRL_QP_SETUP:{
295                 struct rdma_ctrlqp_setup *rdma = data;
296
297                 spin_lock_irq(&adapter->sge.reg_lock);
298                 ret = t3_sge_init_ecntxt(adapter, FW_RI_SGEEC_START, 0,
299                                                 SGE_CNTXT_RDMA,
300                                                 ASYNC_NOTIF_RSPQ,
301                                                 rdma->base_addr, rdma->size,
302                                                 FW_RI_TID_START, 1, 0);
303                 spin_unlock_irq(&adapter->sge.reg_lock);
304                 break;
305         }
306         case RDMA_GET_MIB: {
307                 spin_lock(&adapter->stats_lock);
308                 t3_tp_get_mib_stats(adapter, (struct tp_mib_stats *)data);
309                 spin_unlock(&adapter->stats_lock);
310                 break;
311         }
312         default:
313                 ret = -EOPNOTSUPP;
314         }
315         return ret;
316 }
317
318 static int cxgb_offload_ctl(struct t3cdev *tdev, unsigned int req, void *data)
319 {
320         struct adapter *adapter = tdev2adap(tdev);
321         struct tid_range *tid;
322         struct mtutab *mtup;
323         struct iff_mac *iffmacp;
324         struct ddp_params *ddpp;
325         struct adap_ports *ports;
326         struct ofld_page_info *rx_page_info;
327         struct tp_params *tp = &adapter->params.tp;
328         int i;
329
330         switch (req) {
331         case GET_MAX_OUTSTANDING_WR:
332                 *(unsigned int *)data = FW_WR_NUM;
333                 break;
334         case GET_WR_LEN:
335                 *(unsigned int *)data = WR_FLITS;
336                 break;
337         case GET_TX_MAX_CHUNK:
338                 *(unsigned int *)data = 1 << 20;        /* 1MB */
339                 break;
340         case GET_TID_RANGE:
341                 tid = data;
342                 tid->num = t3_mc5_size(&adapter->mc5) -
343                     adapter->params.mc5.nroutes -
344                     adapter->params.mc5.nfilters - adapter->params.mc5.nservers;
345                 tid->base = 0;
346                 break;
347         case GET_STID_RANGE:
348                 tid = data;
349                 tid->num = adapter->params.mc5.nservers;
350                 tid->base = t3_mc5_size(&adapter->mc5) - tid->num -
351                     adapter->params.mc5.nfilters - adapter->params.mc5.nroutes;
352                 break;
353         case GET_L2T_CAPACITY:
354                 *(unsigned int *)data = 2048;
355                 break;
356         case GET_MTUS:
357                 mtup = data;
358                 mtup->size = NMTUS;
359                 mtup->mtus = adapter->params.mtus;
360                 break;
361         case GET_IFF_FROM_MAC:
362                 iffmacp = data;
363                 iffmacp->dev = get_iff_from_mac(adapter, iffmacp->mac_addr,
364                                                 iffmacp->vlan_tag &
365                                                 VLAN_VID_MASK);
366                 break;
367         case GET_DDP_PARAMS:
368                 ddpp = data;
369                 ddpp->llimit = t3_read_reg(adapter, A_ULPRX_TDDP_LLIMIT);
370                 ddpp->ulimit = t3_read_reg(adapter, A_ULPRX_TDDP_ULIMIT);
371                 ddpp->tag_mask = t3_read_reg(adapter, A_ULPRX_TDDP_TAGMASK);
372                 break;
373         case GET_PORTS:
374                 ports = data;
375                 ports->nports = adapter->params.nports;
376                 for_each_port(adapter, i)
377                         ports->lldevs[i] = adapter->port[i];
378                 break;
379         case ULP_ISCSI_GET_PARAMS:
380         case ULP_ISCSI_SET_PARAMS:
381                 if (!offload_running(adapter))
382                         return -EAGAIN;
383                 return cxgb_ulp_iscsi_ctl(adapter, req, data);
384         case RDMA_GET_PARAMS:
385         case RDMA_CQ_OP:
386         case RDMA_CQ_SETUP:
387         case RDMA_CQ_DISABLE:
388         case RDMA_CTRL_QP_SETUP:
389         case RDMA_GET_MEM:
390         case RDMA_GET_MIB:
391                 if (!offload_running(adapter))
392                         return -EAGAIN;
393                 return cxgb_rdma_ctl(adapter, req, data);
394         case GET_RX_PAGE_INFO:
395                 rx_page_info = data;
396                 rx_page_info->page_size = tp->rx_pg_size;
397                 rx_page_info->num = tp->rx_num_pgs;
398                 break;
399         default:
400                 return -EOPNOTSUPP;
401         }
402         return 0;
403 }
404
405 /*
406  * Dummy handler for Rx offload packets in case we get an offload packet before
407  * proper processing is setup.  This complains and drops the packet as it isn't
408  * normal to get offload packets at this stage.
409  */
410 static int rx_offload_blackhole(struct t3cdev *dev, struct sk_buff **skbs,
411                                 int n)
412 {
413         while (n--)
414                 dev_kfree_skb_any(skbs[n]);
415         return 0;
416 }
417
418 static void dummy_neigh_update(struct t3cdev *dev, struct neighbour *neigh)
419 {
420 }
421
422 void cxgb3_set_dummy_ops(struct t3cdev *dev)
423 {
424         dev->recv = rx_offload_blackhole;
425         dev->neigh_update = dummy_neigh_update;
426 }
427
428 /*
429  * Free an active-open TID.
430  */
431 void *cxgb3_free_atid(struct t3cdev *tdev, int atid)
432 {
433         struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
434         union active_open_entry *p = atid2entry(t, atid);
435         void *ctx = p->t3c_tid.ctx;
436
437         spin_lock_bh(&t->atid_lock);
438         p->next = t->afree;
439         t->afree = p;
440         t->atids_in_use--;
441         spin_unlock_bh(&t->atid_lock);
442
443         return ctx;
444 }
445
446 EXPORT_SYMBOL(cxgb3_free_atid);
447
448 /*
449  * Free a server TID and return it to the free pool.
450  */
451 void cxgb3_free_stid(struct t3cdev *tdev, int stid)
452 {
453         struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
454         union listen_entry *p = stid2entry(t, stid);
455
456         spin_lock_bh(&t->stid_lock);
457         p->next = t->sfree;
458         t->sfree = p;
459         t->stids_in_use--;
460         spin_unlock_bh(&t->stid_lock);
461 }
462
463 EXPORT_SYMBOL(cxgb3_free_stid);
464
465 void cxgb3_insert_tid(struct t3cdev *tdev, struct cxgb3_client *client,
466                       void *ctx, unsigned int tid)
467 {
468         struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
469
470         t->tid_tab[tid].client = client;
471         t->tid_tab[tid].ctx = ctx;
472         atomic_inc(&t->tids_in_use);
473 }
474
475 EXPORT_SYMBOL(cxgb3_insert_tid);
476
477 /*
478  * Populate a TID_RELEASE WR.  The skb must be already propely sized.
479  */
480 static inline void mk_tid_release(struct sk_buff *skb, unsigned int tid)
481 {
482         struct cpl_tid_release *req;
483
484         skb->priority = CPL_PRIORITY_SETUP;
485         req = (struct cpl_tid_release *)__skb_put(skb, sizeof(*req));
486         req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
487         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
488 }
489
490 static void t3_process_tid_release_list(struct work_struct *work)
491 {
492         struct t3c_data *td = container_of(work, struct t3c_data,
493                                            tid_release_task);
494         struct sk_buff *skb;
495         struct t3cdev *tdev = td->dev;
496
497
498         spin_lock_bh(&td->tid_release_lock);
499         while (td->tid_release_list) {
500                 struct t3c_tid_entry *p = td->tid_release_list;
501
502                 td->tid_release_list = (struct t3c_tid_entry *)p->ctx;
503                 spin_unlock_bh(&td->tid_release_lock);
504
505                 skb = alloc_skb(sizeof(struct cpl_tid_release),
506                                 GFP_KERNEL | __GFP_NOFAIL);
507                 mk_tid_release(skb, p - td->tid_maps.tid_tab);
508                 cxgb3_ofld_send(tdev, skb);
509                 p->ctx = NULL;
510                 spin_lock_bh(&td->tid_release_lock);
511         }
512         spin_unlock_bh(&td->tid_release_lock);
513 }
514
515 /* use ctx as a next pointer in the tid release list */
516 void cxgb3_queue_tid_release(struct t3cdev *tdev, unsigned int tid)
517 {
518         struct t3c_data *td = T3C_DATA(tdev);
519         struct t3c_tid_entry *p = &td->tid_maps.tid_tab[tid];
520
521         spin_lock_bh(&td->tid_release_lock);
522         p->ctx = (void *)td->tid_release_list;
523         p->client = NULL;
524         td->tid_release_list = p;
525         if (!p->ctx)
526                 schedule_work(&td->tid_release_task);
527         spin_unlock_bh(&td->tid_release_lock);
528 }
529
530 EXPORT_SYMBOL(cxgb3_queue_tid_release);
531
532 /*
533  * Remove a tid from the TID table.  A client may defer processing its last
534  * CPL message if it is locked at the time it arrives, and while the message
535  * sits in the client's backlog the TID may be reused for another connection.
536  * To handle this we atomically switch the TID association if it still points
537  * to the original client context.
538  */
539 void cxgb3_remove_tid(struct t3cdev *tdev, void *ctx, unsigned int tid)
540 {
541         struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
542
543         BUG_ON(tid >= t->ntids);
544         if (tdev->type == T3A)
545                 (void)cmpxchg(&t->tid_tab[tid].ctx, ctx, NULL);
546         else {
547                 struct sk_buff *skb;
548
549                 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
550                 if (likely(skb)) {
551                         mk_tid_release(skb, tid);
552                         cxgb3_ofld_send(tdev, skb);
553                         t->tid_tab[tid].ctx = NULL;
554                 } else
555                         cxgb3_queue_tid_release(tdev, tid);
556         }
557         atomic_dec(&t->tids_in_use);
558 }
559
560 EXPORT_SYMBOL(cxgb3_remove_tid);
561
562 int cxgb3_alloc_atid(struct t3cdev *tdev, struct cxgb3_client *client,
563                      void *ctx)
564 {
565         int atid = -1;
566         struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
567
568         spin_lock_bh(&t->atid_lock);
569         if (t->afree &&
570             t->atids_in_use + atomic_read(&t->tids_in_use) + MC5_MIN_TIDS <=
571             t->ntids) {
572                 union active_open_entry *p = t->afree;
573
574                 atid = (p - t->atid_tab) + t->atid_base;
575                 t->afree = p->next;
576                 p->t3c_tid.ctx = ctx;
577                 p->t3c_tid.client = client;
578                 t->atids_in_use++;
579         }
580         spin_unlock_bh(&t->atid_lock);
581         return atid;
582 }
583
584 EXPORT_SYMBOL(cxgb3_alloc_atid);
585
586 int cxgb3_alloc_stid(struct t3cdev *tdev, struct cxgb3_client *client,
587                      void *ctx)
588 {
589         int stid = -1;
590         struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
591
592         spin_lock_bh(&t->stid_lock);
593         if (t->sfree) {
594                 union listen_entry *p = t->sfree;
595
596                 stid = (p - t->stid_tab) + t->stid_base;
597                 t->sfree = p->next;
598                 p->t3c_tid.ctx = ctx;
599                 p->t3c_tid.client = client;
600                 t->stids_in_use++;
601         }
602         spin_unlock_bh(&t->stid_lock);
603         return stid;
604 }
605
606 EXPORT_SYMBOL(cxgb3_alloc_stid);
607
608 /* Get the t3cdev associated with a net_device */
609 struct t3cdev *dev2t3cdev(struct net_device *dev)
610 {
611         const struct port_info *pi = netdev_priv(dev);
612
613         return (struct t3cdev *)pi->adapter;
614 }
615
616 EXPORT_SYMBOL(dev2t3cdev);
617
618 static int do_smt_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
619 {
620         struct cpl_smt_write_rpl *rpl = cplhdr(skb);
621
622         if (rpl->status != CPL_ERR_NONE)
623                 printk(KERN_ERR
624                        "Unexpected SMT_WRITE_RPL status %u for entry %u\n",
625                        rpl->status, GET_TID(rpl));
626
627         return CPL_RET_BUF_DONE;
628 }
629
630 static int do_l2t_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
631 {
632         struct cpl_l2t_write_rpl *rpl = cplhdr(skb);
633
634         if (rpl->status != CPL_ERR_NONE)
635                 printk(KERN_ERR
636                        "Unexpected L2T_WRITE_RPL status %u for entry %u\n",
637                        rpl->status, GET_TID(rpl));
638
639         return CPL_RET_BUF_DONE;
640 }
641
642 static int do_rte_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
643 {
644         struct cpl_rte_write_rpl *rpl = cplhdr(skb);
645
646         if (rpl->status != CPL_ERR_NONE)
647                 printk(KERN_ERR
648                        "Unexpected RTE_WRITE_RPL status %u for entry %u\n",
649                        rpl->status, GET_TID(rpl));
650
651         return CPL_RET_BUF_DONE;
652 }
653
654 static int do_act_open_rpl(struct t3cdev *dev, struct sk_buff *skb)
655 {
656         struct cpl_act_open_rpl *rpl = cplhdr(skb);
657         unsigned int atid = G_TID(ntohl(rpl->atid));
658         struct t3c_tid_entry *t3c_tid;
659
660         t3c_tid = lookup_atid(&(T3C_DATA(dev))->tid_maps, atid);
661         if (t3c_tid && t3c_tid->ctx && t3c_tid->client &&
662             t3c_tid->client->handlers &&
663             t3c_tid->client->handlers[CPL_ACT_OPEN_RPL]) {
664                 return t3c_tid->client->handlers[CPL_ACT_OPEN_RPL] (dev, skb,
665                                                                     t3c_tid->
666                                                                     ctx);
667         } else {
668                 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
669                        dev->name, CPL_ACT_OPEN_RPL);
670                 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
671         }
672 }
673
674 static int do_stid_rpl(struct t3cdev *dev, struct sk_buff *skb)
675 {
676         union opcode_tid *p = cplhdr(skb);
677         unsigned int stid = G_TID(ntohl(p->opcode_tid));
678         struct t3c_tid_entry *t3c_tid;
679
680         t3c_tid = lookup_stid(&(T3C_DATA(dev))->tid_maps, stid);
681         if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
682             t3c_tid->client->handlers[p->opcode]) {
683                 return t3c_tid->client->handlers[p->opcode] (dev, skb,
684                                                              t3c_tid->ctx);
685         } else {
686                 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
687                        dev->name, p->opcode);
688                 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
689         }
690 }
691
692 static int do_hwtid_rpl(struct t3cdev *dev, struct sk_buff *skb)
693 {
694         union opcode_tid *p = cplhdr(skb);
695         unsigned int hwtid = G_TID(ntohl(p->opcode_tid));
696         struct t3c_tid_entry *t3c_tid;
697
698         t3c_tid = lookup_tid(&(T3C_DATA(dev))->tid_maps, hwtid);
699         if (t3c_tid && 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);
703         } else {
704                 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
705                        dev->name, p->opcode);
706                 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
707         }
708 }
709
710 static int do_cr(struct t3cdev *dev, struct sk_buff *skb)
711 {
712         struct cpl_pass_accept_req *req = cplhdr(skb);
713         unsigned int stid = G_PASS_OPEN_TID(ntohl(req->tos_tid));
714         struct tid_info *t = &(T3C_DATA(dev))->tid_maps;
715         struct t3c_tid_entry *t3c_tid;
716         unsigned int tid = GET_TID(req);
717
718         if (unlikely(tid >= t->ntids)) {
719                 printk("%s: passive open TID %u too large\n",
720                        dev->name, tid);
721                 t3_fatal_err(tdev2adap(dev));
722                 return CPL_RET_BUF_DONE;
723         }
724
725         t3c_tid = lookup_stid(t, stid);
726         if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
727             t3c_tid->client->handlers[CPL_PASS_ACCEPT_REQ]) {
728                 return t3c_tid->client->handlers[CPL_PASS_ACCEPT_REQ]
729                     (dev, skb, t3c_tid->ctx);
730         } else {
731                 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
732                        dev->name, CPL_PASS_ACCEPT_REQ);
733                 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
734         }
735 }
736
737 /*
738  * Returns an sk_buff for a reply CPL message of size len.  If the input
739  * sk_buff has no other users it is trimmed and reused, otherwise a new buffer
740  * is allocated.  The input skb must be of size at least len.  Note that this
741  * operation does not destroy the original skb data even if it decides to reuse
742  * the buffer.
743  */
744 static struct sk_buff *cxgb3_get_cpl_reply_skb(struct sk_buff *skb, size_t len,
745                                                gfp_t gfp)
746 {
747         if (likely(!skb_cloned(skb))) {
748                 BUG_ON(skb->len < len);
749                 __skb_trim(skb, len);
750                 skb_get(skb);
751         } else {
752                 skb = alloc_skb(len, gfp);
753                 if (skb)
754                         __skb_put(skb, len);
755         }
756         return skb;
757 }
758
759 static int do_abort_req_rss(struct t3cdev *dev, struct sk_buff *skb)
760 {
761         union opcode_tid *p = cplhdr(skb);
762         unsigned int hwtid = G_TID(ntohl(p->opcode_tid));
763         struct t3c_tid_entry *t3c_tid;
764
765         t3c_tid = lookup_tid(&(T3C_DATA(dev))->tid_maps, hwtid);
766         if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
767             t3c_tid->client->handlers[p->opcode]) {
768                 return t3c_tid->client->handlers[p->opcode]
769                     (dev, skb, t3c_tid->ctx);
770         } else {
771                 struct cpl_abort_req_rss *req = cplhdr(skb);
772                 struct cpl_abort_rpl *rpl;
773                 struct sk_buff *reply_skb;
774                 unsigned int tid = GET_TID(req);
775                 u8 cmd = req->status;
776
777                 if (req->status == CPL_ERR_RTX_NEG_ADVICE ||
778                     req->status == CPL_ERR_PERSIST_NEG_ADVICE)
779                         goto out;
780
781                 reply_skb = cxgb3_get_cpl_reply_skb(skb,
782                                                     sizeof(struct
783                                                            cpl_abort_rpl),
784                                                     GFP_ATOMIC);
785
786                 if (!reply_skb) {
787                         printk("do_abort_req_rss: couldn't get skb!\n");
788                         goto out;
789                 }
790                 reply_skb->priority = CPL_PRIORITY_DATA;
791                 __skb_put(reply_skb, sizeof(struct cpl_abort_rpl));
792                 rpl = cplhdr(reply_skb);
793                 rpl->wr.wr_hi =
794                     htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_RPL));
795                 rpl->wr.wr_lo = htonl(V_WR_TID(tid));
796                 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_ABORT_RPL, tid));
797                 rpl->cmd = cmd;
798                 cxgb3_ofld_send(dev, reply_skb);
799 out:
800                 return CPL_RET_BUF_DONE;
801         }
802 }
803
804 static int do_act_establish(struct t3cdev *dev, struct sk_buff *skb)
805 {
806         struct cpl_act_establish *req = cplhdr(skb);
807         unsigned int atid = G_PASS_OPEN_TID(ntohl(req->tos_tid));
808         struct tid_info *t = &(T3C_DATA(dev))->tid_maps;
809         struct t3c_tid_entry *t3c_tid;
810         unsigned int tid = GET_TID(req);
811
812         if (unlikely(tid >= t->ntids)) {
813                 printk("%s: active establish TID %u too large\n",
814                        dev->name, tid);
815                 t3_fatal_err(tdev2adap(dev));
816                 return CPL_RET_BUF_DONE;
817         }
818
819         t3c_tid = lookup_atid(t, atid);
820         if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
821             t3c_tid->client->handlers[CPL_ACT_ESTABLISH]) {
822                 return t3c_tid->client->handlers[CPL_ACT_ESTABLISH]
823                     (dev, skb, t3c_tid->ctx);
824         } else {
825                 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
826                        dev->name, CPL_ACT_ESTABLISH);
827                 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
828         }
829 }
830
831 static int do_trace(struct t3cdev *dev, struct sk_buff *skb)
832 {
833         struct cpl_trace_pkt *p = cplhdr(skb);
834
835         skb->protocol = htons(0xffff);
836         skb->dev = dev->lldev;
837         skb_pull(skb, sizeof(*p));
838         skb_reset_mac_header(skb);
839         netif_receive_skb(skb);
840         return 0;
841 }
842
843 /*
844  * That skb would better have come from process_responses() where we abuse
845  * ->priority and ->csum to carry our data.  NB: if we get to per-arch
846  * ->csum, the things might get really interesting here.
847  */
848
849 static inline u32 get_hwtid(struct sk_buff *skb)
850 {
851         return ntohl((__force __be32)skb->priority) >> 8 & 0xfffff;
852 }
853
854 static inline u32 get_opcode(struct sk_buff *skb)
855 {
856         return G_OPCODE(ntohl((__force __be32)skb->csum));
857 }
858
859 static int do_term(struct t3cdev *dev, struct sk_buff *skb)
860 {
861         unsigned int hwtid = get_hwtid(skb);
862         unsigned int opcode = get_opcode(skb);
863         struct t3c_tid_entry *t3c_tid;
864
865         t3c_tid = lookup_tid(&(T3C_DATA(dev))->tid_maps, hwtid);
866         if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
867             t3c_tid->client->handlers[opcode]) {
868                 return t3c_tid->client->handlers[opcode] (dev, skb,
869                                                           t3c_tid->ctx);
870         } else {
871                 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
872                        dev->name, opcode);
873                 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
874         }
875 }
876
877 static int nb_callback(struct notifier_block *self, unsigned long event,
878                        void *ctx)
879 {
880         switch (event) {
881         case (NETEVENT_NEIGH_UPDATE):{
882                 cxgb_neigh_update((struct neighbour *)ctx);
883                 break;
884         }
885         case (NETEVENT_PMTU_UPDATE):
886                 break;
887         case (NETEVENT_REDIRECT):{
888                 struct netevent_redirect *nr = ctx;
889                 cxgb_redirect(nr->old, nr->new);
890                 cxgb_neigh_update(nr->new->neighbour);
891                 break;
892         }
893         default:
894                 break;
895         }
896         return 0;
897 }
898
899 static struct notifier_block nb = {
900         .notifier_call = nb_callback
901 };
902
903 /*
904  * Process a received packet with an unknown/unexpected CPL opcode.
905  */
906 static int do_bad_cpl(struct t3cdev *dev, struct sk_buff *skb)
907 {
908         printk(KERN_ERR "%s: received bad CPL command 0x%x\n", dev->name,
909                *skb->data);
910         return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
911 }
912
913 /*
914  * Handlers for each CPL opcode
915  */
916 static cpl_handler_func cpl_handlers[NUM_CPL_CMDS];
917
918 /*
919  * Add a new handler to the CPL dispatch table.  A NULL handler may be supplied
920  * to unregister an existing handler.
921  */
922 void t3_register_cpl_handler(unsigned int opcode, cpl_handler_func h)
923 {
924         if (opcode < NUM_CPL_CMDS)
925                 cpl_handlers[opcode] = h ? h : do_bad_cpl;
926         else
927                 printk(KERN_ERR "T3C: handler registration for "
928                        "opcode %x failed\n", opcode);
929 }
930
931 EXPORT_SYMBOL(t3_register_cpl_handler);
932
933 /*
934  * T3CDEV's receive method.
935  */
936 int process_rx(struct t3cdev *dev, struct sk_buff **skbs, int n)
937 {
938         while (n--) {
939                 struct sk_buff *skb = *skbs++;
940                 unsigned int opcode = get_opcode(skb);
941                 int ret = cpl_handlers[opcode] (dev, skb);
942
943 #if VALIDATE_TID
944                 if (ret & CPL_RET_UNKNOWN_TID) {
945                         union opcode_tid *p = cplhdr(skb);
946
947                         printk(KERN_ERR "%s: CPL message (opcode %u) had "
948                                "unknown TID %u\n", dev->name, opcode,
949                                G_TID(ntohl(p->opcode_tid)));
950                 }
951 #endif
952                 if (ret & CPL_RET_BUF_DONE)
953                         kfree_skb(skb);
954         }
955         return 0;
956 }
957
958 /*
959  * Sends an sk_buff to a T3C driver after dealing with any active network taps.
960  */
961 int cxgb3_ofld_send(struct t3cdev *dev, struct sk_buff *skb)
962 {
963         int r;
964
965         local_bh_disable();
966         r = dev->send(dev, skb);
967         local_bh_enable();
968         return r;
969 }
970
971 EXPORT_SYMBOL(cxgb3_ofld_send);
972
973 static int is_offloading(struct net_device *dev)
974 {
975         struct adapter *adapter;
976         int i;
977
978         read_lock_bh(&adapter_list_lock);
979         list_for_each_entry(adapter, &adapter_list, adapter_list) {
980                 for_each_port(adapter, i) {
981                         if (dev == adapter->port[i]) {
982                                 read_unlock_bh(&adapter_list_lock);
983                                 return 1;
984                         }
985                 }
986         }
987         read_unlock_bh(&adapter_list_lock);
988         return 0;
989 }
990
991 void cxgb_neigh_update(struct neighbour *neigh)
992 {
993         struct net_device *dev = neigh->dev;
994
995         if (dev && (is_offloading(dev))) {
996                 struct t3cdev *tdev = dev2t3cdev(dev);
997
998                 BUG_ON(!tdev);
999                 t3_l2t_update(tdev, neigh);
1000         }
1001 }
1002
1003 static void set_l2t_ix(struct t3cdev *tdev, u32 tid, struct l2t_entry *e)
1004 {
1005         struct sk_buff *skb;
1006         struct cpl_set_tcb_field *req;
1007
1008         skb = alloc_skb(sizeof(*req), GFP_ATOMIC);
1009         if (!skb) {
1010                 printk(KERN_ERR "%s: cannot allocate skb!\n", __FUNCTION__);
1011                 return;
1012         }
1013         skb->priority = CPL_PRIORITY_CONTROL;
1014         req = (struct cpl_set_tcb_field *)skb_put(skb, sizeof(*req));
1015         req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
1016         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_SET_TCB_FIELD, tid));
1017         req->reply = 0;
1018         req->cpu_idx = 0;
1019         req->word = htons(W_TCB_L2T_IX);
1020         req->mask = cpu_to_be64(V_TCB_L2T_IX(M_TCB_L2T_IX));
1021         req->val = cpu_to_be64(V_TCB_L2T_IX(e->idx));
1022         tdev->send(tdev, skb);
1023 }
1024
1025 void cxgb_redirect(struct dst_entry *old, struct dst_entry *new)
1026 {
1027         struct net_device *olddev, *newdev;
1028         struct tid_info *ti;
1029         struct t3cdev *tdev;
1030         u32 tid;
1031         int update_tcb;
1032         struct l2t_entry *e;
1033         struct t3c_tid_entry *te;
1034
1035         olddev = old->neighbour->dev;
1036         newdev = new->neighbour->dev;
1037         if (!is_offloading(olddev))
1038                 return;
1039         if (!is_offloading(newdev)) {
1040                 printk(KERN_WARNING "%s: Redirect to non-offload "
1041                        "device ignored.\n", __FUNCTION__);
1042                 return;
1043         }
1044         tdev = dev2t3cdev(olddev);
1045         BUG_ON(!tdev);
1046         if (tdev != dev2t3cdev(newdev)) {
1047                 printk(KERN_WARNING "%s: Redirect to different "
1048                        "offload device ignored.\n", __FUNCTION__);
1049                 return;
1050         }
1051
1052         /* Add new L2T entry */
1053         e = t3_l2t_get(tdev, new->neighbour, newdev);
1054         if (!e) {
1055                 printk(KERN_ERR "%s: couldn't allocate new l2t entry!\n",
1056                        __FUNCTION__);
1057                 return;
1058         }
1059
1060         /* Walk tid table and notify clients of dst change. */
1061         ti = &(T3C_DATA(tdev))->tid_maps;
1062         for (tid = 0; tid < ti->ntids; tid++) {
1063                 te = lookup_tid(ti, tid);
1064                 BUG_ON(!te);
1065                 if (te && te->ctx && te->client && te->client->redirect) {
1066                         update_tcb = te->client->redirect(te->ctx, old, new, e);
1067                         if (update_tcb) {
1068                                 l2t_hold(L2DATA(tdev), e);
1069                                 set_l2t_ix(tdev, tid, e);
1070                         }
1071                 }
1072         }
1073         l2t_release(L2DATA(tdev), e);
1074 }
1075
1076 /*
1077  * Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
1078  * The allocated memory is cleared.
1079  */
1080 void *cxgb_alloc_mem(unsigned long size)
1081 {
1082         void *p = kmalloc(size, GFP_KERNEL);
1083
1084         if (!p)
1085                 p = vmalloc(size);
1086         if (p)
1087                 memset(p, 0, size);
1088         return p;
1089 }
1090
1091 /*
1092  * Free memory allocated through t3_alloc_mem().
1093  */
1094 void cxgb_free_mem(void *addr)
1095 {
1096         if (is_vmalloc_addr(addr))
1097                 vfree(addr);
1098         else
1099                 kfree(addr);
1100 }
1101
1102 /*
1103  * Allocate and initialize the TID tables.  Returns 0 on success.
1104  */
1105 static int init_tid_tabs(struct tid_info *t, unsigned int ntids,
1106                          unsigned int natids, unsigned int nstids,
1107                          unsigned int atid_base, unsigned int stid_base)
1108 {
1109         unsigned long size = ntids * sizeof(*t->tid_tab) +
1110             natids * sizeof(*t->atid_tab) + nstids * sizeof(*t->stid_tab);
1111
1112         t->tid_tab = cxgb_alloc_mem(size);
1113         if (!t->tid_tab)
1114                 return -ENOMEM;
1115
1116         t->stid_tab = (union listen_entry *)&t->tid_tab[ntids];
1117         t->atid_tab = (union active_open_entry *)&t->stid_tab[nstids];
1118         t->ntids = ntids;
1119         t->nstids = nstids;
1120         t->stid_base = stid_base;
1121         t->sfree = NULL;
1122         t->natids = natids;
1123         t->atid_base = atid_base;
1124         t->afree = NULL;
1125         t->stids_in_use = t->atids_in_use = 0;
1126         atomic_set(&t->tids_in_use, 0);
1127         spin_lock_init(&t->stid_lock);
1128         spin_lock_init(&t->atid_lock);
1129
1130         /*
1131          * Setup the free lists for stid_tab and atid_tab.
1132          */
1133         if (nstids) {
1134                 while (--nstids)
1135                         t->stid_tab[nstids - 1].next = &t->stid_tab[nstids];
1136                 t->sfree = t->stid_tab;
1137         }
1138         if (natids) {
1139                 while (--natids)
1140                         t->atid_tab[natids - 1].next = &t->atid_tab[natids];
1141                 t->afree = t->atid_tab;
1142         }
1143         return 0;
1144 }
1145
1146 static void free_tid_maps(struct tid_info *t)
1147 {
1148         cxgb_free_mem(t->tid_tab);
1149 }
1150
1151 static inline void add_adapter(struct adapter *adap)
1152 {
1153         write_lock_bh(&adapter_list_lock);
1154         list_add_tail(&adap->adapter_list, &adapter_list);
1155         write_unlock_bh(&adapter_list_lock);
1156 }
1157
1158 static inline void remove_adapter(struct adapter *adap)
1159 {
1160         write_lock_bh(&adapter_list_lock);
1161         list_del(&adap->adapter_list);
1162         write_unlock_bh(&adapter_list_lock);
1163 }
1164
1165 int cxgb3_offload_activate(struct adapter *adapter)
1166 {
1167         struct t3cdev *dev = &adapter->tdev;
1168         int natids, err;
1169         struct t3c_data *t;
1170         struct tid_range stid_range, tid_range;
1171         struct mtutab mtutab;
1172         unsigned int l2t_capacity;
1173
1174         t = kcalloc(1, sizeof(*t), GFP_KERNEL);
1175         if (!t)
1176                 return -ENOMEM;
1177
1178         err = -EOPNOTSUPP;
1179         if (dev->ctl(dev, GET_TX_MAX_CHUNK, &t->tx_max_chunk) < 0 ||
1180             dev->ctl(dev, GET_MAX_OUTSTANDING_WR, &t->max_wrs) < 0 ||
1181             dev->ctl(dev, GET_L2T_CAPACITY, &l2t_capacity) < 0 ||
1182             dev->ctl(dev, GET_MTUS, &mtutab) < 0 ||
1183             dev->ctl(dev, GET_TID_RANGE, &tid_range) < 0 ||
1184             dev->ctl(dev, GET_STID_RANGE, &stid_range) < 0)
1185                 goto out_free;
1186
1187         err = -ENOMEM;
1188         L2DATA(dev) = t3_init_l2t(l2t_capacity);
1189         if (!L2DATA(dev))
1190                 goto out_free;
1191
1192         natids = min(tid_range.num / 2, MAX_ATIDS);
1193         err = init_tid_tabs(&t->tid_maps, tid_range.num, natids,
1194                             stid_range.num, ATID_BASE, stid_range.base);
1195         if (err)
1196                 goto out_free_l2t;
1197
1198         t->mtus = mtutab.mtus;
1199         t->nmtus = mtutab.size;
1200
1201         INIT_WORK(&t->tid_release_task, t3_process_tid_release_list);
1202         spin_lock_init(&t->tid_release_lock);
1203         INIT_LIST_HEAD(&t->list_node);
1204         t->dev = dev;
1205
1206         T3C_DATA(dev) = t;
1207         dev->recv = process_rx;
1208         dev->neigh_update = t3_l2t_update;
1209
1210         /* Register netevent handler once */
1211         if (list_empty(&adapter_list))
1212                 register_netevent_notifier(&nb);
1213
1214         add_adapter(adapter);
1215         return 0;
1216
1217 out_free_l2t:
1218         t3_free_l2t(L2DATA(dev));
1219         L2DATA(dev) = NULL;
1220 out_free:
1221         kfree(t);
1222         return err;
1223 }
1224
1225 void cxgb3_offload_deactivate(struct adapter *adapter)
1226 {
1227         struct t3cdev *tdev = &adapter->tdev;
1228         struct t3c_data *t = T3C_DATA(tdev);
1229
1230         remove_adapter(adapter);
1231         if (list_empty(&adapter_list))
1232                 unregister_netevent_notifier(&nb);
1233
1234         free_tid_maps(&t->tid_maps);
1235         T3C_DATA(tdev) = NULL;
1236         t3_free_l2t(L2DATA(tdev));
1237         L2DATA(tdev) = NULL;
1238         kfree(t);
1239 }
1240
1241 static inline void register_tdev(struct t3cdev *tdev)
1242 {
1243         static int unit;
1244
1245         mutex_lock(&cxgb3_db_lock);
1246         snprintf(tdev->name, sizeof(tdev->name), "ofld_dev%d", unit++);
1247         list_add_tail(&tdev->ofld_dev_list, &ofld_dev_list);
1248         mutex_unlock(&cxgb3_db_lock);
1249 }
1250
1251 static inline void unregister_tdev(struct t3cdev *tdev)
1252 {
1253         mutex_lock(&cxgb3_db_lock);
1254         list_del(&tdev->ofld_dev_list);
1255         mutex_unlock(&cxgb3_db_lock);
1256 }
1257
1258 void __devinit cxgb3_adapter_ofld(struct adapter *adapter)
1259 {
1260         struct t3cdev *tdev = &adapter->tdev;
1261
1262         INIT_LIST_HEAD(&tdev->ofld_dev_list);
1263
1264         cxgb3_set_dummy_ops(tdev);
1265         tdev->send = t3_offload_tx;
1266         tdev->ctl = cxgb_offload_ctl;
1267         tdev->type = adapter->params.rev == 0 ? T3A : T3B;
1268
1269         register_tdev(tdev);
1270 }
1271
1272 void __devexit cxgb3_adapter_unofld(struct adapter *adapter)
1273 {
1274         struct t3cdev *tdev = &adapter->tdev;
1275
1276         tdev->recv = NULL;
1277         tdev->neigh_update = NULL;
1278
1279         unregister_tdev(tdev);
1280 }
1281
1282 void __init cxgb3_offload_init(void)
1283 {
1284         int i;
1285
1286         for (i = 0; i < NUM_CPL_CMDS; ++i)
1287                 cpl_handlers[i] = do_bad_cpl;
1288
1289         t3_register_cpl_handler(CPL_SMT_WRITE_RPL, do_smt_write_rpl);
1290         t3_register_cpl_handler(CPL_L2T_WRITE_RPL, do_l2t_write_rpl);
1291         t3_register_cpl_handler(CPL_RTE_WRITE_RPL, do_rte_write_rpl);
1292         t3_register_cpl_handler(CPL_PASS_OPEN_RPL, do_stid_rpl);
1293         t3_register_cpl_handler(CPL_CLOSE_LISTSRV_RPL, do_stid_rpl);
1294         t3_register_cpl_handler(CPL_PASS_ACCEPT_REQ, do_cr);
1295         t3_register_cpl_handler(CPL_PASS_ESTABLISH, do_hwtid_rpl);
1296         t3_register_cpl_handler(CPL_ABORT_RPL_RSS, do_hwtid_rpl);
1297         t3_register_cpl_handler(CPL_ABORT_RPL, do_hwtid_rpl);
1298         t3_register_cpl_handler(CPL_RX_URG_NOTIFY, do_hwtid_rpl);
1299         t3_register_cpl_handler(CPL_RX_DATA, do_hwtid_rpl);
1300         t3_register_cpl_handler(CPL_TX_DATA_ACK, do_hwtid_rpl);
1301         t3_register_cpl_handler(CPL_TX_DMA_ACK, do_hwtid_rpl);
1302         t3_register_cpl_handler(CPL_ACT_OPEN_RPL, do_act_open_rpl);
1303         t3_register_cpl_handler(CPL_PEER_CLOSE, do_hwtid_rpl);
1304         t3_register_cpl_handler(CPL_CLOSE_CON_RPL, do_hwtid_rpl);
1305         t3_register_cpl_handler(CPL_ABORT_REQ_RSS, do_abort_req_rss);
1306         t3_register_cpl_handler(CPL_ACT_ESTABLISH, do_act_establish);
1307         t3_register_cpl_handler(CPL_SET_TCB_RPL, do_hwtid_rpl);
1308         t3_register_cpl_handler(CPL_GET_TCB_RPL, do_hwtid_rpl);
1309         t3_register_cpl_handler(CPL_RDMA_TERMINATE, do_term);
1310         t3_register_cpl_handler(CPL_RDMA_EC_STATUS, do_hwtid_rpl);
1311         t3_register_cpl_handler(CPL_TRACE_PKT, do_trace);
1312         t3_register_cpl_handler(CPL_RX_DATA_DDP, do_hwtid_rpl);
1313         t3_register_cpl_handler(CPL_RX_DDP_COMPLETE, do_hwtid_rpl);
1314         t3_register_cpl_handler(CPL_ISCSI_HDR, do_hwtid_rpl);
1315 }