Merge branch 'v4l_for_linus' of git://linuxtv.org/mchehab/for_linus
[pandora-kernel.git] / 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 <linux/slab.h>
35 #include <net/neighbour.h>
36 #include <linux/notifier.h>
37 #include <linux/atomic.h>
38 #include <linux/proc_fs.h>
39 #include <linux/if_vlan.h>
40 #include <net/netevent.h>
41 #include <linux/highmem.h>
42 #include <linux/vmalloc.h>
43
44 #include "common.h"
45 #include "regs.h"
46 #include "cxgb3_ioctl.h"
47 #include "cxgb3_ctl_defs.h"
48 #include "cxgb3_defs.h"
49 #include "l2t.h"
50 #include "firmware_exports.h"
51 #include "cxgb3_offload.h"
52
53 static LIST_HEAD(client_list);
54 static LIST_HEAD(ofld_dev_list);
55 static DEFINE_MUTEX(cxgb3_db_lock);
56
57 static DEFINE_RWLOCK(adapter_list_lock);
58 static LIST_HEAD(adapter_list);
59
60 static const unsigned int MAX_ATIDS = 64 * 1024;
61 static const unsigned int ATID_BASE = 0x10000;
62
63 static void cxgb_neigh_update(struct neighbour *neigh);
64 static void cxgb_redirect(struct dst_entry *old, struct dst_entry *new);
65
66 static inline int offload_activated(struct t3cdev *tdev)
67 {
68         const struct adapter *adapter = tdev2adap(tdev);
69
70         return test_bit(OFFLOAD_DEVMAP_BIT, &adapter->open_device_map);
71 }
72
73 /**
74  *      cxgb3_register_client - register an offload client
75  *      @client: the client
76  *
77  *      Add the client to the client list,
78  *      and call backs the client for each activated offload device
79  */
80 void cxgb3_register_client(struct cxgb3_client *client)
81 {
82         struct t3cdev *tdev;
83
84         mutex_lock(&cxgb3_db_lock);
85         list_add_tail(&client->client_list, &client_list);
86
87         if (client->add) {
88                 list_for_each_entry(tdev, &ofld_dev_list, ofld_dev_list) {
89                         if (offload_activated(tdev))
90                                 client->add(tdev);
91                 }
92         }
93         mutex_unlock(&cxgb3_db_lock);
94 }
95
96 EXPORT_SYMBOL(cxgb3_register_client);
97
98 /**
99  *      cxgb3_unregister_client - unregister an offload client
100  *      @client: the client
101  *
102  *      Remove the client to the client list,
103  *      and call backs the client for each activated offload device.
104  */
105 void cxgb3_unregister_client(struct cxgb3_client *client)
106 {
107         struct t3cdev *tdev;
108
109         mutex_lock(&cxgb3_db_lock);
110         list_del(&client->client_list);
111
112         if (client->remove) {
113                 list_for_each_entry(tdev, &ofld_dev_list, ofld_dev_list) {
114                         if (offload_activated(tdev))
115                                 client->remove(tdev);
116                 }
117         }
118         mutex_unlock(&cxgb3_db_lock);
119 }
120
121 EXPORT_SYMBOL(cxgb3_unregister_client);
122
123 /**
124  *      cxgb3_add_clients - activate registered clients for an offload device
125  *      @tdev: the offload device
126  *
127  *      Call backs all registered clients once a offload device is activated
128  */
129 void cxgb3_add_clients(struct t3cdev *tdev)
130 {
131         struct cxgb3_client *client;
132
133         mutex_lock(&cxgb3_db_lock);
134         list_for_each_entry(client, &client_list, client_list) {
135                 if (client->add)
136                         client->add(tdev);
137         }
138         mutex_unlock(&cxgb3_db_lock);
139 }
140
141 /**
142  *      cxgb3_remove_clients - deactivates registered clients
143  *                             for an offload device
144  *      @tdev: the offload device
145  *
146  *      Call backs all registered clients once a offload device is deactivated
147  */
148 void cxgb3_remove_clients(struct t3cdev *tdev)
149 {
150         struct cxgb3_client *client;
151
152         mutex_lock(&cxgb3_db_lock);
153         list_for_each_entry(client, &client_list, client_list) {
154                 if (client->remove)
155                         client->remove(tdev);
156         }
157         mutex_unlock(&cxgb3_db_lock);
158 }
159
160 void cxgb3_event_notify(struct t3cdev *tdev, u32 event, u32 port)
161 {
162         struct cxgb3_client *client;
163
164         mutex_lock(&cxgb3_db_lock);
165         list_for_each_entry(client, &client_list, client_list) {
166                 if (client->event_handler)
167                         client->event_handler(tdev, event, port);
168         }
169         mutex_unlock(&cxgb3_db_lock);
170 }
171
172 static struct net_device *get_iff_from_mac(struct adapter *adapter,
173                                            const unsigned char *mac,
174                                            unsigned int vlan)
175 {
176         int i;
177
178         for_each_port(adapter, i) {
179                 struct net_device *dev = adapter->port[i];
180
181                 if (!memcmp(dev->dev_addr, mac, ETH_ALEN)) {
182                         if (vlan && vlan != VLAN_VID_MASK) {
183                                 rcu_read_lock();
184                                 dev = __vlan_find_dev_deep(dev, vlan);
185                                 rcu_read_unlock();
186                         } else if (netif_is_bond_slave(dev)) {
187                                 while (dev->master)
188                                         dev = dev->master;
189                         }
190                         return dev;
191                 }
192         }
193         return NULL;
194 }
195
196 static int cxgb_ulp_iscsi_ctl(struct adapter *adapter, unsigned int req,
197                               void *data)
198 {
199         int i;
200         int ret = 0;
201         unsigned int val = 0;
202         struct ulp_iscsi_info *uiip = data;
203
204         switch (req) {
205         case ULP_ISCSI_GET_PARAMS:
206                 uiip->pdev = adapter->pdev;
207                 uiip->llimit = t3_read_reg(adapter, A_ULPRX_ISCSI_LLIMIT);
208                 uiip->ulimit = t3_read_reg(adapter, A_ULPRX_ISCSI_ULIMIT);
209                 uiip->tagmask = t3_read_reg(adapter, A_ULPRX_ISCSI_TAGMASK);
210
211                 val = t3_read_reg(adapter, A_ULPRX_ISCSI_PSZ);
212                 for (i = 0; i < 4; i++, val >>= 8)
213                         uiip->pgsz_factor[i] = val & 0xFF;
214
215                 val = t3_read_reg(adapter, A_TP_PARA_REG7);
216                 uiip->max_txsz =
217                 uiip->max_rxsz = min((val >> S_PMMAXXFERLEN0)&M_PMMAXXFERLEN0,
218                                      (val >> S_PMMAXXFERLEN1)&M_PMMAXXFERLEN1);
219                 /*
220                  * On tx, the iscsi pdu has to be <= tx page size and has to
221                  * fit into the Tx PM FIFO.
222                  */
223                 val = min(adapter->params.tp.tx_pg_size,
224                           t3_read_reg(adapter, A_PM1_TX_CFG) >> 17);
225                 uiip->max_txsz = min(val, uiip->max_txsz);
226
227                 /* set MaxRxData to 16224 */
228                 val = t3_read_reg(adapter, A_TP_PARA_REG2);
229                 if ((val >> S_MAXRXDATA) != 0x3f60) {
230                         val &= (M_RXCOALESCESIZE << S_RXCOALESCESIZE);
231                         val |= V_MAXRXDATA(0x3f60);
232                         printk(KERN_INFO
233                                 "%s, iscsi set MaxRxData to 16224 (0x%x).\n",
234                                 adapter->name, val);
235                         t3_write_reg(adapter, A_TP_PARA_REG2, val);
236                 }
237
238                 /*
239                  * on rx, the iscsi pdu has to be < rx page size and the
240                  * the max rx data length programmed in TP
241                  */
242                 val = min(adapter->params.tp.rx_pg_size,
243                           ((t3_read_reg(adapter, A_TP_PARA_REG2)) >>
244                                 S_MAXRXDATA) & M_MAXRXDATA);
245                 uiip->max_rxsz = min(val, uiip->max_rxsz);
246                 break;
247         case ULP_ISCSI_SET_PARAMS:
248                 t3_write_reg(adapter, A_ULPRX_ISCSI_TAGMASK, uiip->tagmask);
249                 /* program the ddp page sizes */
250                 for (i = 0; i < 4; i++)
251                         val |= (uiip->pgsz_factor[i] & 0xF) << (8 * i);
252                 if (val && (val != t3_read_reg(adapter, A_ULPRX_ISCSI_PSZ))) {
253                         printk(KERN_INFO
254                                 "%s, setting iscsi pgsz 0x%x, %u,%u,%u,%u.\n",
255                                 adapter->name, val, uiip->pgsz_factor[0],
256                                 uiip->pgsz_factor[1], uiip->pgsz_factor[2],
257                                 uiip->pgsz_factor[3]);
258                         t3_write_reg(adapter, A_ULPRX_ISCSI_PSZ, val);
259                 }
260                 break;
261         default:
262                 ret = -EOPNOTSUPP;
263         }
264         return ret;
265 }
266
267 /* Response queue used for RDMA events. */
268 #define ASYNC_NOTIF_RSPQ 0
269
270 static int cxgb_rdma_ctl(struct adapter *adapter, unsigned int req, void *data)
271 {
272         int ret = 0;
273
274         switch (req) {
275         case RDMA_GET_PARAMS: {
276                 struct rdma_info *rdma = data;
277                 struct pci_dev *pdev = adapter->pdev;
278
279                 rdma->udbell_physbase = pci_resource_start(pdev, 2);
280                 rdma->udbell_len = pci_resource_len(pdev, 2);
281                 rdma->tpt_base =
282                         t3_read_reg(adapter, A_ULPTX_TPT_LLIMIT);
283                 rdma->tpt_top = t3_read_reg(adapter, A_ULPTX_TPT_ULIMIT);
284                 rdma->pbl_base =
285                         t3_read_reg(adapter, A_ULPTX_PBL_LLIMIT);
286                 rdma->pbl_top = t3_read_reg(adapter, A_ULPTX_PBL_ULIMIT);
287                 rdma->rqt_base = t3_read_reg(adapter, A_ULPRX_RQ_LLIMIT);
288                 rdma->rqt_top = t3_read_reg(adapter, A_ULPRX_RQ_ULIMIT);
289                 rdma->kdb_addr = adapter->regs + A_SG_KDOORBELL;
290                 rdma->pdev = pdev;
291                 break;
292         }
293         case RDMA_CQ_OP:{
294                 unsigned long flags;
295                 struct rdma_cq_op *rdma = data;
296
297                 /* may be called in any context */
298                 spin_lock_irqsave(&adapter->sge.reg_lock, flags);
299                 ret = t3_sge_cqcntxt_op(adapter, rdma->id, rdma->op,
300                                         rdma->credits);
301                 spin_unlock_irqrestore(&adapter->sge.reg_lock, flags);
302                 break;
303         }
304         case RDMA_GET_MEM:{
305                 struct ch_mem_range *t = data;
306                 struct mc7 *mem;
307
308                 if ((t->addr & 7) || (t->len & 7))
309                         return -EINVAL;
310                 if (t->mem_id == MEM_CM)
311                         mem = &adapter->cm;
312                 else if (t->mem_id == MEM_PMRX)
313                         mem = &adapter->pmrx;
314                 else if (t->mem_id == MEM_PMTX)
315                         mem = &adapter->pmtx;
316                 else
317                         return -EINVAL;
318
319                 ret =
320                         t3_mc7_bd_read(mem, t->addr / 8, t->len / 8,
321                                         (u64 *) t->buf);
322                 if (ret)
323                         return ret;
324                 break;
325         }
326         case RDMA_CQ_SETUP:{
327                 struct rdma_cq_setup *rdma = data;
328
329                 spin_lock_irq(&adapter->sge.reg_lock);
330                 ret =
331                         t3_sge_init_cqcntxt(adapter, rdma->id,
332                                         rdma->base_addr, rdma->size,
333                                         ASYNC_NOTIF_RSPQ,
334                                         rdma->ovfl_mode, rdma->credits,
335                                         rdma->credit_thres);
336                 spin_unlock_irq(&adapter->sge.reg_lock);
337                 break;
338         }
339         case RDMA_CQ_DISABLE:
340                 spin_lock_irq(&adapter->sge.reg_lock);
341                 ret = t3_sge_disable_cqcntxt(adapter, *(unsigned int *)data);
342                 spin_unlock_irq(&adapter->sge.reg_lock);
343                 break;
344         case RDMA_CTRL_QP_SETUP:{
345                 struct rdma_ctrlqp_setup *rdma = data;
346
347                 spin_lock_irq(&adapter->sge.reg_lock);
348                 ret = t3_sge_init_ecntxt(adapter, FW_RI_SGEEC_START, 0,
349                                                 SGE_CNTXT_RDMA,
350                                                 ASYNC_NOTIF_RSPQ,
351                                                 rdma->base_addr, rdma->size,
352                                                 FW_RI_TID_START, 1, 0);
353                 spin_unlock_irq(&adapter->sge.reg_lock);
354                 break;
355         }
356         case RDMA_GET_MIB: {
357                 spin_lock(&adapter->stats_lock);
358                 t3_tp_get_mib_stats(adapter, (struct tp_mib_stats *)data);
359                 spin_unlock(&adapter->stats_lock);
360                 break;
361         }
362         default:
363                 ret = -EOPNOTSUPP;
364         }
365         return ret;
366 }
367
368 static int cxgb_offload_ctl(struct t3cdev *tdev, unsigned int req, void *data)
369 {
370         struct adapter *adapter = tdev2adap(tdev);
371         struct tid_range *tid;
372         struct mtutab *mtup;
373         struct iff_mac *iffmacp;
374         struct ddp_params *ddpp;
375         struct adap_ports *ports;
376         struct ofld_page_info *rx_page_info;
377         struct tp_params *tp = &adapter->params.tp;
378         int i;
379
380         switch (req) {
381         case GET_MAX_OUTSTANDING_WR:
382                 *(unsigned int *)data = FW_WR_NUM;
383                 break;
384         case GET_WR_LEN:
385                 *(unsigned int *)data = WR_FLITS;
386                 break;
387         case GET_TX_MAX_CHUNK:
388                 *(unsigned int *)data = 1 << 20;        /* 1MB */
389                 break;
390         case GET_TID_RANGE:
391                 tid = data;
392                 tid->num = t3_mc5_size(&adapter->mc5) -
393                     adapter->params.mc5.nroutes -
394                     adapter->params.mc5.nfilters - adapter->params.mc5.nservers;
395                 tid->base = 0;
396                 break;
397         case GET_STID_RANGE:
398                 tid = data;
399                 tid->num = adapter->params.mc5.nservers;
400                 tid->base = t3_mc5_size(&adapter->mc5) - tid->num -
401                     adapter->params.mc5.nfilters - adapter->params.mc5.nroutes;
402                 break;
403         case GET_L2T_CAPACITY:
404                 *(unsigned int *)data = 2048;
405                 break;
406         case GET_MTUS:
407                 mtup = data;
408                 mtup->size = NMTUS;
409                 mtup->mtus = adapter->params.mtus;
410                 break;
411         case GET_IFF_FROM_MAC:
412                 iffmacp = data;
413                 iffmacp->dev = get_iff_from_mac(adapter, iffmacp->mac_addr,
414                                                 iffmacp->vlan_tag &
415                                                 VLAN_VID_MASK);
416                 break;
417         case GET_DDP_PARAMS:
418                 ddpp = data;
419                 ddpp->llimit = t3_read_reg(adapter, A_ULPRX_TDDP_LLIMIT);
420                 ddpp->ulimit = t3_read_reg(adapter, A_ULPRX_TDDP_ULIMIT);
421                 ddpp->tag_mask = t3_read_reg(adapter, A_ULPRX_TDDP_TAGMASK);
422                 break;
423         case GET_PORTS:
424                 ports = data;
425                 ports->nports = adapter->params.nports;
426                 for_each_port(adapter, i)
427                         ports->lldevs[i] = adapter->port[i];
428                 break;
429         case ULP_ISCSI_GET_PARAMS:
430         case ULP_ISCSI_SET_PARAMS:
431                 if (!offload_running(adapter))
432                         return -EAGAIN;
433                 return cxgb_ulp_iscsi_ctl(adapter, req, data);
434         case RDMA_GET_PARAMS:
435         case RDMA_CQ_OP:
436         case RDMA_CQ_SETUP:
437         case RDMA_CQ_DISABLE:
438         case RDMA_CTRL_QP_SETUP:
439         case RDMA_GET_MEM:
440         case RDMA_GET_MIB:
441                 if (!offload_running(adapter))
442                         return -EAGAIN;
443                 return cxgb_rdma_ctl(adapter, req, data);
444         case GET_RX_PAGE_INFO:
445                 rx_page_info = data;
446                 rx_page_info->page_size = tp->rx_pg_size;
447                 rx_page_info->num = tp->rx_num_pgs;
448                 break;
449         case GET_ISCSI_IPV4ADDR: {
450                 struct iscsi_ipv4addr *p = data;
451                 struct port_info *pi = netdev_priv(p->dev);
452                 p->ipv4addr = pi->iscsi_ipv4addr;
453                 break;
454         }
455         case GET_EMBEDDED_INFO: {
456                 struct ch_embedded_info *e = data;
457
458                 spin_lock(&adapter->stats_lock);
459                 t3_get_fw_version(adapter, &e->fw_vers);
460                 t3_get_tp_version(adapter, &e->tp_vers);
461                 spin_unlock(&adapter->stats_lock);
462                 break;
463         }
464         default:
465                 return -EOPNOTSUPP;
466         }
467         return 0;
468 }
469
470 /*
471  * Dummy handler for Rx offload packets in case we get an offload packet before
472  * proper processing is setup.  This complains and drops the packet as it isn't
473  * normal to get offload packets at this stage.
474  */
475 static int rx_offload_blackhole(struct t3cdev *dev, struct sk_buff **skbs,
476                                 int n)
477 {
478         while (n--)
479                 dev_kfree_skb_any(skbs[n]);
480         return 0;
481 }
482
483 static void dummy_neigh_update(struct t3cdev *dev, struct neighbour *neigh)
484 {
485 }
486
487 void cxgb3_set_dummy_ops(struct t3cdev *dev)
488 {
489         dev->recv = rx_offload_blackhole;
490         dev->neigh_update = dummy_neigh_update;
491 }
492
493 /*
494  * Free an active-open TID.
495  */
496 void *cxgb3_free_atid(struct t3cdev *tdev, int atid)
497 {
498         struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
499         union active_open_entry *p = atid2entry(t, atid);
500         void *ctx = p->t3c_tid.ctx;
501
502         spin_lock_bh(&t->atid_lock);
503         p->next = t->afree;
504         t->afree = p;
505         t->atids_in_use--;
506         spin_unlock_bh(&t->atid_lock);
507
508         return ctx;
509 }
510
511 EXPORT_SYMBOL(cxgb3_free_atid);
512
513 /*
514  * Free a server TID and return it to the free pool.
515  */
516 void cxgb3_free_stid(struct t3cdev *tdev, int stid)
517 {
518         struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
519         union listen_entry *p = stid2entry(t, stid);
520
521         spin_lock_bh(&t->stid_lock);
522         p->next = t->sfree;
523         t->sfree = p;
524         t->stids_in_use--;
525         spin_unlock_bh(&t->stid_lock);
526 }
527
528 EXPORT_SYMBOL(cxgb3_free_stid);
529
530 void cxgb3_insert_tid(struct t3cdev *tdev, struct cxgb3_client *client,
531                       void *ctx, unsigned int tid)
532 {
533         struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
534
535         t->tid_tab[tid].client = client;
536         t->tid_tab[tid].ctx = ctx;
537         atomic_inc(&t->tids_in_use);
538 }
539
540 EXPORT_SYMBOL(cxgb3_insert_tid);
541
542 /*
543  * Populate a TID_RELEASE WR.  The skb must be already propely sized.
544  */
545 static inline void mk_tid_release(struct sk_buff *skb, unsigned int tid)
546 {
547         struct cpl_tid_release *req;
548
549         skb->priority = CPL_PRIORITY_SETUP;
550         req = (struct cpl_tid_release *)__skb_put(skb, sizeof(*req));
551         req->wr.wr_hi = htonl(V_WR_OP(FW_WROPCODE_FORWARD));
552         OPCODE_TID(req) = htonl(MK_OPCODE_TID(CPL_TID_RELEASE, tid));
553 }
554
555 static void t3_process_tid_release_list(struct work_struct *work)
556 {
557         struct t3c_data *td = container_of(work, struct t3c_data,
558                                            tid_release_task);
559         struct sk_buff *skb;
560         struct t3cdev *tdev = td->dev;
561
562
563         spin_lock_bh(&td->tid_release_lock);
564         while (td->tid_release_list) {
565                 struct t3c_tid_entry *p = td->tid_release_list;
566
567                 td->tid_release_list = p->ctx;
568                 spin_unlock_bh(&td->tid_release_lock);
569
570                 skb = alloc_skb(sizeof(struct cpl_tid_release),
571                                 GFP_KERNEL);
572                 if (!skb)
573                         skb = td->nofail_skb;
574                 if (!skb) {
575                         spin_lock_bh(&td->tid_release_lock);
576                         p->ctx = (void *)td->tid_release_list;
577                         td->tid_release_list = (struct t3c_tid_entry *)p;
578                         break;
579                 }
580                 mk_tid_release(skb, p - td->tid_maps.tid_tab);
581                 cxgb3_ofld_send(tdev, skb);
582                 p->ctx = NULL;
583                 if (skb == td->nofail_skb)
584                         td->nofail_skb =
585                                 alloc_skb(sizeof(struct cpl_tid_release),
586                                         GFP_KERNEL);
587                 spin_lock_bh(&td->tid_release_lock);
588         }
589         td->release_list_incomplete = (td->tid_release_list == NULL) ? 0 : 1;
590         spin_unlock_bh(&td->tid_release_lock);
591
592         if (!td->nofail_skb)
593                 td->nofail_skb =
594                         alloc_skb(sizeof(struct cpl_tid_release),
595                                 GFP_KERNEL);
596 }
597
598 /* use ctx as a next pointer in the tid release list */
599 void cxgb3_queue_tid_release(struct t3cdev *tdev, unsigned int tid)
600 {
601         struct t3c_data *td = T3C_DATA(tdev);
602         struct t3c_tid_entry *p = &td->tid_maps.tid_tab[tid];
603
604         spin_lock_bh(&td->tid_release_lock);
605         p->ctx = (void *)td->tid_release_list;
606         p->client = NULL;
607         td->tid_release_list = p;
608         if (!p->ctx || td->release_list_incomplete)
609                 schedule_work(&td->tid_release_task);
610         spin_unlock_bh(&td->tid_release_lock);
611 }
612
613 EXPORT_SYMBOL(cxgb3_queue_tid_release);
614
615 /*
616  * Remove a tid from the TID table.  A client may defer processing its last
617  * CPL message if it is locked at the time it arrives, and while the message
618  * sits in the client's backlog the TID may be reused for another connection.
619  * To handle this we atomically switch the TID association if it still points
620  * to the original client context.
621  */
622 void cxgb3_remove_tid(struct t3cdev *tdev, void *ctx, unsigned int tid)
623 {
624         struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
625
626         BUG_ON(tid >= t->ntids);
627         if (tdev->type == T3A)
628                 (void)cmpxchg(&t->tid_tab[tid].ctx, ctx, NULL);
629         else {
630                 struct sk_buff *skb;
631
632                 skb = alloc_skb(sizeof(struct cpl_tid_release), GFP_ATOMIC);
633                 if (likely(skb)) {
634                         mk_tid_release(skb, tid);
635                         cxgb3_ofld_send(tdev, skb);
636                         t->tid_tab[tid].ctx = NULL;
637                 } else
638                         cxgb3_queue_tid_release(tdev, tid);
639         }
640         atomic_dec(&t->tids_in_use);
641 }
642
643 EXPORT_SYMBOL(cxgb3_remove_tid);
644
645 int cxgb3_alloc_atid(struct t3cdev *tdev, struct cxgb3_client *client,
646                      void *ctx)
647 {
648         int atid = -1;
649         struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
650
651         spin_lock_bh(&t->atid_lock);
652         if (t->afree &&
653             t->atids_in_use + atomic_read(&t->tids_in_use) + MC5_MIN_TIDS <=
654             t->ntids) {
655                 union active_open_entry *p = t->afree;
656
657                 atid = (p - t->atid_tab) + t->atid_base;
658                 t->afree = p->next;
659                 p->t3c_tid.ctx = ctx;
660                 p->t3c_tid.client = client;
661                 t->atids_in_use++;
662         }
663         spin_unlock_bh(&t->atid_lock);
664         return atid;
665 }
666
667 EXPORT_SYMBOL(cxgb3_alloc_atid);
668
669 int cxgb3_alloc_stid(struct t3cdev *tdev, struct cxgb3_client *client,
670                      void *ctx)
671 {
672         int stid = -1;
673         struct tid_info *t = &(T3C_DATA(tdev))->tid_maps;
674
675         spin_lock_bh(&t->stid_lock);
676         if (t->sfree) {
677                 union listen_entry *p = t->sfree;
678
679                 stid = (p - t->stid_tab) + t->stid_base;
680                 t->sfree = p->next;
681                 p->t3c_tid.ctx = ctx;
682                 p->t3c_tid.client = client;
683                 t->stids_in_use++;
684         }
685         spin_unlock_bh(&t->stid_lock);
686         return stid;
687 }
688
689 EXPORT_SYMBOL(cxgb3_alloc_stid);
690
691 /* Get the t3cdev associated with a net_device */
692 struct t3cdev *dev2t3cdev(struct net_device *dev)
693 {
694         const struct port_info *pi = netdev_priv(dev);
695
696         return (struct t3cdev *)pi->adapter;
697 }
698
699 EXPORT_SYMBOL(dev2t3cdev);
700
701 static int do_smt_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
702 {
703         struct cpl_smt_write_rpl *rpl = cplhdr(skb);
704
705         if (rpl->status != CPL_ERR_NONE)
706                 printk(KERN_ERR
707                        "Unexpected SMT_WRITE_RPL status %u for entry %u\n",
708                        rpl->status, GET_TID(rpl));
709
710         return CPL_RET_BUF_DONE;
711 }
712
713 static int do_l2t_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
714 {
715         struct cpl_l2t_write_rpl *rpl = cplhdr(skb);
716
717         if (rpl->status != CPL_ERR_NONE)
718                 printk(KERN_ERR
719                        "Unexpected L2T_WRITE_RPL status %u for entry %u\n",
720                        rpl->status, GET_TID(rpl));
721
722         return CPL_RET_BUF_DONE;
723 }
724
725 static int do_rte_write_rpl(struct t3cdev *dev, struct sk_buff *skb)
726 {
727         struct cpl_rte_write_rpl *rpl = cplhdr(skb);
728
729         if (rpl->status != CPL_ERR_NONE)
730                 printk(KERN_ERR
731                        "Unexpected RTE_WRITE_RPL status %u for entry %u\n",
732                        rpl->status, GET_TID(rpl));
733
734         return CPL_RET_BUF_DONE;
735 }
736
737 static int do_act_open_rpl(struct t3cdev *dev, struct sk_buff *skb)
738 {
739         struct cpl_act_open_rpl *rpl = cplhdr(skb);
740         unsigned int atid = G_TID(ntohl(rpl->atid));
741         struct t3c_tid_entry *t3c_tid;
742
743         t3c_tid = lookup_atid(&(T3C_DATA(dev))->tid_maps, atid);
744         if (t3c_tid && t3c_tid->ctx && t3c_tid->client &&
745             t3c_tid->client->handlers &&
746             t3c_tid->client->handlers[CPL_ACT_OPEN_RPL]) {
747                 return t3c_tid->client->handlers[CPL_ACT_OPEN_RPL] (dev, skb,
748                                                                     t3c_tid->
749                                                                     ctx);
750         } else {
751                 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
752                        dev->name, CPL_ACT_OPEN_RPL);
753                 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
754         }
755 }
756
757 static int do_stid_rpl(struct t3cdev *dev, struct sk_buff *skb)
758 {
759         union opcode_tid *p = cplhdr(skb);
760         unsigned int stid = G_TID(ntohl(p->opcode_tid));
761         struct t3c_tid_entry *t3c_tid;
762
763         t3c_tid = lookup_stid(&(T3C_DATA(dev))->tid_maps, stid);
764         if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
765             t3c_tid->client->handlers[p->opcode]) {
766                 return t3c_tid->client->handlers[p->opcode] (dev, skb,
767                                                              t3c_tid->ctx);
768         } else {
769                 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
770                        dev->name, p->opcode);
771                 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
772         }
773 }
774
775 static int do_hwtid_rpl(struct t3cdev *dev, struct sk_buff *skb)
776 {
777         union opcode_tid *p = cplhdr(skb);
778         unsigned int hwtid = G_TID(ntohl(p->opcode_tid));
779         struct t3c_tid_entry *t3c_tid;
780
781         t3c_tid = lookup_tid(&(T3C_DATA(dev))->tid_maps, hwtid);
782         if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
783             t3c_tid->client->handlers[p->opcode]) {
784                 return t3c_tid->client->handlers[p->opcode]
785                     (dev, skb, t3c_tid->ctx);
786         } else {
787                 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
788                        dev->name, p->opcode);
789                 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
790         }
791 }
792
793 static int do_cr(struct t3cdev *dev, struct sk_buff *skb)
794 {
795         struct cpl_pass_accept_req *req = cplhdr(skb);
796         unsigned int stid = G_PASS_OPEN_TID(ntohl(req->tos_tid));
797         struct tid_info *t = &(T3C_DATA(dev))->tid_maps;
798         struct t3c_tid_entry *t3c_tid;
799         unsigned int tid = GET_TID(req);
800
801         if (unlikely(tid >= t->ntids)) {
802                 printk("%s: passive open TID %u too large\n",
803                        dev->name, tid);
804                 t3_fatal_err(tdev2adap(dev));
805                 return CPL_RET_BUF_DONE;
806         }
807
808         t3c_tid = lookup_stid(t, stid);
809         if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
810             t3c_tid->client->handlers[CPL_PASS_ACCEPT_REQ]) {
811                 return t3c_tid->client->handlers[CPL_PASS_ACCEPT_REQ]
812                     (dev, skb, t3c_tid->ctx);
813         } else {
814                 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
815                        dev->name, CPL_PASS_ACCEPT_REQ);
816                 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
817         }
818 }
819
820 /*
821  * Returns an sk_buff for a reply CPL message of size len.  If the input
822  * sk_buff has no other users it is trimmed and reused, otherwise a new buffer
823  * is allocated.  The input skb must be of size at least len.  Note that this
824  * operation does not destroy the original skb data even if it decides to reuse
825  * the buffer.
826  */
827 static struct sk_buff *cxgb3_get_cpl_reply_skb(struct sk_buff *skb, size_t len,
828                                                gfp_t gfp)
829 {
830         if (likely(!skb_cloned(skb))) {
831                 BUG_ON(skb->len < len);
832                 __skb_trim(skb, len);
833                 skb_get(skb);
834         } else {
835                 skb = alloc_skb(len, gfp);
836                 if (skb)
837                         __skb_put(skb, len);
838         }
839         return skb;
840 }
841
842 static int do_abort_req_rss(struct t3cdev *dev, struct sk_buff *skb)
843 {
844         union opcode_tid *p = cplhdr(skb);
845         unsigned int hwtid = G_TID(ntohl(p->opcode_tid));
846         struct t3c_tid_entry *t3c_tid;
847
848         t3c_tid = lookup_tid(&(T3C_DATA(dev))->tid_maps, hwtid);
849         if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
850             t3c_tid->client->handlers[p->opcode]) {
851                 return t3c_tid->client->handlers[p->opcode]
852                     (dev, skb, t3c_tid->ctx);
853         } else {
854                 struct cpl_abort_req_rss *req = cplhdr(skb);
855                 struct cpl_abort_rpl *rpl;
856                 struct sk_buff *reply_skb;
857                 unsigned int tid = GET_TID(req);
858                 u8 cmd = req->status;
859
860                 if (req->status == CPL_ERR_RTX_NEG_ADVICE ||
861                     req->status == CPL_ERR_PERSIST_NEG_ADVICE)
862                         goto out;
863
864                 reply_skb = cxgb3_get_cpl_reply_skb(skb,
865                                                     sizeof(struct
866                                                            cpl_abort_rpl),
867                                                     GFP_ATOMIC);
868
869                 if (!reply_skb) {
870                         printk("do_abort_req_rss: couldn't get skb!\n");
871                         goto out;
872                 }
873                 reply_skb->priority = CPL_PRIORITY_DATA;
874                 __skb_put(reply_skb, sizeof(struct cpl_abort_rpl));
875                 rpl = cplhdr(reply_skb);
876                 rpl->wr.wr_hi =
877                     htonl(V_WR_OP(FW_WROPCODE_OFLD_HOST_ABORT_CON_RPL));
878                 rpl->wr.wr_lo = htonl(V_WR_TID(tid));
879                 OPCODE_TID(rpl) = htonl(MK_OPCODE_TID(CPL_ABORT_RPL, tid));
880                 rpl->cmd = cmd;
881                 cxgb3_ofld_send(dev, reply_skb);
882 out:
883                 return CPL_RET_BUF_DONE;
884         }
885 }
886
887 static int do_act_establish(struct t3cdev *dev, struct sk_buff *skb)
888 {
889         struct cpl_act_establish *req = cplhdr(skb);
890         unsigned int atid = G_PASS_OPEN_TID(ntohl(req->tos_tid));
891         struct tid_info *t = &(T3C_DATA(dev))->tid_maps;
892         struct t3c_tid_entry *t3c_tid;
893         unsigned int tid = GET_TID(req);
894
895         if (unlikely(tid >= t->ntids)) {
896                 printk("%s: active establish TID %u too large\n",
897                        dev->name, tid);
898                 t3_fatal_err(tdev2adap(dev));
899                 return CPL_RET_BUF_DONE;
900         }
901
902         t3c_tid = lookup_atid(t, atid);
903         if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
904             t3c_tid->client->handlers[CPL_ACT_ESTABLISH]) {
905                 return t3c_tid->client->handlers[CPL_ACT_ESTABLISH]
906                     (dev, skb, t3c_tid->ctx);
907         } else {
908                 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
909                        dev->name, CPL_ACT_ESTABLISH);
910                 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
911         }
912 }
913
914 static int do_trace(struct t3cdev *dev, struct sk_buff *skb)
915 {
916         struct cpl_trace_pkt *p = cplhdr(skb);
917
918         skb->protocol = htons(0xffff);
919         skb->dev = dev->lldev;
920         skb_pull(skb, sizeof(*p));
921         skb_reset_mac_header(skb);
922         netif_receive_skb(skb);
923         return 0;
924 }
925
926 /*
927  * That skb would better have come from process_responses() where we abuse
928  * ->priority and ->csum to carry our data.  NB: if we get to per-arch
929  * ->csum, the things might get really interesting here.
930  */
931
932 static inline u32 get_hwtid(struct sk_buff *skb)
933 {
934         return ntohl((__force __be32)skb->priority) >> 8 & 0xfffff;
935 }
936
937 static inline u32 get_opcode(struct sk_buff *skb)
938 {
939         return G_OPCODE(ntohl((__force __be32)skb->csum));
940 }
941
942 static int do_term(struct t3cdev *dev, struct sk_buff *skb)
943 {
944         unsigned int hwtid = get_hwtid(skb);
945         unsigned int opcode = get_opcode(skb);
946         struct t3c_tid_entry *t3c_tid;
947
948         t3c_tid = lookup_tid(&(T3C_DATA(dev))->tid_maps, hwtid);
949         if (t3c_tid && t3c_tid->ctx && t3c_tid->client->handlers &&
950             t3c_tid->client->handlers[opcode]) {
951                 return t3c_tid->client->handlers[opcode] (dev, skb,
952                                                           t3c_tid->ctx);
953         } else {
954                 printk(KERN_ERR "%s: received clientless CPL command 0x%x\n",
955                        dev->name, opcode);
956                 return CPL_RET_BUF_DONE | CPL_RET_BAD_MSG;
957         }
958 }
959
960 static int nb_callback(struct notifier_block *self, unsigned long event,
961                        void *ctx)
962 {
963         switch (event) {
964         case (NETEVENT_NEIGH_UPDATE):{
965                 cxgb_neigh_update((struct neighbour *)ctx);
966                 break;
967         }
968         case (NETEVENT_REDIRECT):{
969                 struct netevent_redirect *nr = ctx;
970                 cxgb_redirect(nr->old, nr->new);
971                 cxgb_neigh_update(dst_get_neighbour(nr->new));
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 static 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 static 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 static 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 = dst_get_neighbour(old)->dev;
1117         newdev = dst_get_neighbour(new)->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, dst_get_neighbour(new), 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                                 rcu_read_lock();
1150                                 l2t_hold(L2DATA(tdev), e);
1151                                 rcu_read_unlock();
1152                                 set_l2t_ix(tdev, tid, e);
1153                         }
1154                 }
1155         }
1156         l2t_release(tdev, e);
1157 }
1158
1159 /*
1160  * Allocate a chunk of memory using kmalloc or, if that fails, vmalloc.
1161  * The allocated memory is cleared.
1162  */
1163 void *cxgb_alloc_mem(unsigned long size)
1164 {
1165         void *p = kzalloc(size, GFP_KERNEL);
1166
1167         if (!p)
1168                 p = vzalloc(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 = kzalloc(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         RCU_INIT_POINTER(dev->l2opt, 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         rcu_assign_pointer(dev->l2opt, NULL);
1304 out_free:
1305         kfree(t);
1306         return err;
1307 }
1308
1309 static void clean_l2_data(struct rcu_head *head)
1310 {
1311         struct l2t_data *d = container_of(head, struct l2t_data, rcu_head);
1312         t3_free_l2t(d);
1313 }
1314
1315
1316 void cxgb3_offload_deactivate(struct adapter *adapter)
1317 {
1318         struct t3cdev *tdev = &adapter->tdev;
1319         struct t3c_data *t = T3C_DATA(tdev);
1320         struct l2t_data *d;
1321
1322         remove_adapter(adapter);
1323         if (list_empty(&adapter_list))
1324                 unregister_netevent_notifier(&nb);
1325
1326         free_tid_maps(&t->tid_maps);
1327         T3C_DATA(tdev) = NULL;
1328         rcu_read_lock();
1329         d = L2DATA(tdev);
1330         rcu_read_unlock();
1331         rcu_assign_pointer(tdev->l2opt, NULL);
1332         call_rcu(&d->rcu_head, clean_l2_data);
1333         if (t->nofail_skb)
1334                 kfree_skb(t->nofail_skb);
1335         kfree(t);
1336 }
1337
1338 static inline void register_tdev(struct t3cdev *tdev)
1339 {
1340         static int unit;
1341
1342         mutex_lock(&cxgb3_db_lock);
1343         snprintf(tdev->name, sizeof(tdev->name), "ofld_dev%d", unit++);
1344         list_add_tail(&tdev->ofld_dev_list, &ofld_dev_list);
1345         mutex_unlock(&cxgb3_db_lock);
1346 }
1347
1348 static inline void unregister_tdev(struct t3cdev *tdev)
1349 {
1350         mutex_lock(&cxgb3_db_lock);
1351         list_del(&tdev->ofld_dev_list);
1352         mutex_unlock(&cxgb3_db_lock);
1353 }
1354
1355 static inline int adap2type(struct adapter *adapter)
1356 {
1357         int type = 0;
1358
1359         switch (adapter->params.rev) {
1360         case T3_REV_A:
1361                 type = T3A;
1362                 break;
1363         case T3_REV_B:
1364         case T3_REV_B2:
1365                 type = T3B;
1366                 break;
1367         case T3_REV_C:
1368                 type = T3C;
1369                 break;
1370         }
1371         return type;
1372 }
1373
1374 void __devinit cxgb3_adapter_ofld(struct adapter *adapter)
1375 {
1376         struct t3cdev *tdev = &adapter->tdev;
1377
1378         INIT_LIST_HEAD(&tdev->ofld_dev_list);
1379
1380         cxgb3_set_dummy_ops(tdev);
1381         tdev->send = t3_offload_tx;
1382         tdev->ctl = cxgb_offload_ctl;
1383         tdev->type = adap2type(adapter);
1384
1385         register_tdev(tdev);
1386 }
1387
1388 void __devexit cxgb3_adapter_unofld(struct adapter *adapter)
1389 {
1390         struct t3cdev *tdev = &adapter->tdev;
1391
1392         tdev->recv = NULL;
1393         tdev->neigh_update = NULL;
1394
1395         unregister_tdev(tdev);
1396 }
1397
1398 void __init cxgb3_offload_init(void)
1399 {
1400         int i;
1401
1402         for (i = 0; i < NUM_CPL_CMDS; ++i)
1403                 cpl_handlers[i] = do_bad_cpl;
1404
1405         t3_register_cpl_handler(CPL_SMT_WRITE_RPL, do_smt_write_rpl);
1406         t3_register_cpl_handler(CPL_L2T_WRITE_RPL, do_l2t_write_rpl);
1407         t3_register_cpl_handler(CPL_RTE_WRITE_RPL, do_rte_write_rpl);
1408         t3_register_cpl_handler(CPL_PASS_OPEN_RPL, do_stid_rpl);
1409         t3_register_cpl_handler(CPL_CLOSE_LISTSRV_RPL, do_stid_rpl);
1410         t3_register_cpl_handler(CPL_PASS_ACCEPT_REQ, do_cr);
1411         t3_register_cpl_handler(CPL_PASS_ESTABLISH, do_hwtid_rpl);
1412         t3_register_cpl_handler(CPL_ABORT_RPL_RSS, do_hwtid_rpl);
1413         t3_register_cpl_handler(CPL_ABORT_RPL, do_hwtid_rpl);
1414         t3_register_cpl_handler(CPL_RX_URG_NOTIFY, do_hwtid_rpl);
1415         t3_register_cpl_handler(CPL_RX_DATA, do_hwtid_rpl);
1416         t3_register_cpl_handler(CPL_TX_DATA_ACK, do_hwtid_rpl);
1417         t3_register_cpl_handler(CPL_TX_DMA_ACK, do_hwtid_rpl);
1418         t3_register_cpl_handler(CPL_ACT_OPEN_RPL, do_act_open_rpl);
1419         t3_register_cpl_handler(CPL_PEER_CLOSE, do_hwtid_rpl);
1420         t3_register_cpl_handler(CPL_CLOSE_CON_RPL, do_hwtid_rpl);
1421         t3_register_cpl_handler(CPL_ABORT_REQ_RSS, do_abort_req_rss);
1422         t3_register_cpl_handler(CPL_ACT_ESTABLISH, do_act_establish);
1423         t3_register_cpl_handler(CPL_SET_TCB_RPL, do_hwtid_rpl);
1424         t3_register_cpl_handler(CPL_GET_TCB_RPL, do_hwtid_rpl);
1425         t3_register_cpl_handler(CPL_RDMA_TERMINATE, do_term);
1426         t3_register_cpl_handler(CPL_RDMA_EC_STATUS, do_hwtid_rpl);
1427         t3_register_cpl_handler(CPL_TRACE_PKT, do_trace);
1428         t3_register_cpl_handler(CPL_RX_DATA_DDP, do_hwtid_rpl);
1429         t3_register_cpl_handler(CPL_RX_DDP_COMPLETE, do_hwtid_rpl);
1430         t3_register_cpl_handler(CPL_ISCSI_HDR, do_hwtid_rpl);
1431 }