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