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