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