blob: eac68f96f808a0d653933996ce2c71c8df8d763a [file] [log] [blame]
Michael Chana4636962009-06-08 18:14:43 -07001/* cnic.c: Broadcom CNIC core network driver.
2 *
3 * Copyright (c) 2006-2009 Broadcom Corporation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation.
8 *
9 * Original skeleton written by: John(Zongxi) Chen (zongxi@broadcom.com)
10 * Modified and maintained by: Michael Chan <mchan@broadcom.com>
11 */
12
13#include <linux/module.h>
14
15#include <linux/kernel.h>
16#include <linux/errno.h>
17#include <linux/list.h>
18#include <linux/slab.h>
19#include <linux/pci.h>
20#include <linux/init.h>
21#include <linux/netdevice.h>
22#include <linux/uio_driver.h>
23#include <linux/in.h>
24#include <linux/dma-mapping.h>
25#include <linux/delay.h>
26#include <linux/ethtool.h>
27#include <linux/if_vlan.h>
28#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
29#define BCM_VLAN 1
30#endif
31#include <net/ip.h>
32#include <net/tcp.h>
33#include <net/route.h>
34#include <net/ipv6.h>
35#include <net/ip6_route.h>
36#include <scsi/iscsi_if.h>
37
38#include "cnic_if.h"
39#include "bnx2.h"
40#include "cnic.h"
41#include "cnic_defs.h"
42
43#define DRV_MODULE_NAME "cnic"
44#define PFX DRV_MODULE_NAME ": "
45
46static char version[] __devinitdata =
47 "Broadcom NetXtreme II CNIC Driver " DRV_MODULE_NAME " v" CNIC_MODULE_VERSION " (" CNIC_MODULE_RELDATE ")\n";
48
49MODULE_AUTHOR("Michael Chan <mchan@broadcom.com> and John(Zongxi) "
50 "Chen (zongxi@broadcom.com");
51MODULE_DESCRIPTION("Broadcom NetXtreme II CNIC Driver");
52MODULE_LICENSE("GPL");
53MODULE_VERSION(CNIC_MODULE_VERSION);
54
55static LIST_HEAD(cnic_dev_list);
56static DEFINE_RWLOCK(cnic_dev_lock);
57static DEFINE_MUTEX(cnic_lock);
58
59static struct cnic_ulp_ops *cnic_ulp_tbl[MAX_CNIC_ULP_TYPE];
60
61static int cnic_service_bnx2(void *, void *);
62static int cnic_ctl(void *, struct cnic_ctl_info *);
63
64static struct cnic_ops cnic_bnx2_ops = {
65 .cnic_owner = THIS_MODULE,
66 .cnic_handler = cnic_service_bnx2,
67 .cnic_ctl = cnic_ctl,
68};
69
Michael Chan86b53602009-10-10 13:46:57 +000070static void cnic_shutdown_rings(struct cnic_dev *);
71static void cnic_init_rings(struct cnic_dev *);
Michael Chana4636962009-06-08 18:14:43 -070072static int cnic_cm_set_pg(struct cnic_sock *);
73
74static int cnic_uio_open(struct uio_info *uinfo, struct inode *inode)
75{
76 struct cnic_dev *dev = uinfo->priv;
77 struct cnic_local *cp = dev->cnic_priv;
78
79 if (!capable(CAP_NET_ADMIN))
80 return -EPERM;
81
82 if (cp->uio_dev != -1)
83 return -EBUSY;
84
Michael Chan86b53602009-10-10 13:46:57 +000085 rtnl_lock();
86 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
87 rtnl_unlock();
88 return -ENODEV;
89 }
90
Michael Chana4636962009-06-08 18:14:43 -070091 cp->uio_dev = iminor(inode);
92
Michael Chan86b53602009-10-10 13:46:57 +000093 cnic_init_rings(dev);
94 rtnl_unlock();
Michael Chana4636962009-06-08 18:14:43 -070095
96 return 0;
97}
98
99static int cnic_uio_close(struct uio_info *uinfo, struct inode *inode)
100{
101 struct cnic_dev *dev = uinfo->priv;
102 struct cnic_local *cp = dev->cnic_priv;
103
Michael Chan86b53602009-10-10 13:46:57 +0000104 cnic_shutdown_rings(dev);
Michael Chan6ef57a02009-09-21 15:39:37 +0000105
Michael Chana4636962009-06-08 18:14:43 -0700106 cp->uio_dev = -1;
107 return 0;
108}
109
110static inline void cnic_hold(struct cnic_dev *dev)
111{
112 atomic_inc(&dev->ref_count);
113}
114
115static inline void cnic_put(struct cnic_dev *dev)
116{
117 atomic_dec(&dev->ref_count);
118}
119
120static inline void csk_hold(struct cnic_sock *csk)
121{
122 atomic_inc(&csk->ref_count);
123}
124
125static inline void csk_put(struct cnic_sock *csk)
126{
127 atomic_dec(&csk->ref_count);
128}
129
130static struct cnic_dev *cnic_from_netdev(struct net_device *netdev)
131{
132 struct cnic_dev *cdev;
133
134 read_lock(&cnic_dev_lock);
135 list_for_each_entry(cdev, &cnic_dev_list, list) {
136 if (netdev == cdev->netdev) {
137 cnic_hold(cdev);
138 read_unlock(&cnic_dev_lock);
139 return cdev;
140 }
141 }
142 read_unlock(&cnic_dev_lock);
143 return NULL;
144}
145
Michael Chan7fc1ece2009-08-14 15:49:47 +0000146static inline void ulp_get(struct cnic_ulp_ops *ulp_ops)
147{
148 atomic_inc(&ulp_ops->ref_count);
149}
150
151static inline void ulp_put(struct cnic_ulp_ops *ulp_ops)
152{
153 atomic_dec(&ulp_ops->ref_count);
154}
155
Michael Chana4636962009-06-08 18:14:43 -0700156static void cnic_ctx_wr(struct cnic_dev *dev, u32 cid_addr, u32 off, u32 val)
157{
158 struct cnic_local *cp = dev->cnic_priv;
159 struct cnic_eth_dev *ethdev = cp->ethdev;
160 struct drv_ctl_info info;
161 struct drv_ctl_io *io = &info.data.io;
162
163 info.cmd = DRV_CTL_CTX_WR_CMD;
164 io->cid_addr = cid_addr;
165 io->offset = off;
166 io->data = val;
167 ethdev->drv_ctl(dev->netdev, &info);
168}
169
170static void cnic_reg_wr_ind(struct cnic_dev *dev, u32 off, u32 val)
171{
172 struct cnic_local *cp = dev->cnic_priv;
173 struct cnic_eth_dev *ethdev = cp->ethdev;
174 struct drv_ctl_info info;
175 struct drv_ctl_io *io = &info.data.io;
176
177 info.cmd = DRV_CTL_IO_WR_CMD;
178 io->offset = off;
179 io->data = val;
180 ethdev->drv_ctl(dev->netdev, &info);
181}
182
183static u32 cnic_reg_rd_ind(struct cnic_dev *dev, u32 off)
184{
185 struct cnic_local *cp = dev->cnic_priv;
186 struct cnic_eth_dev *ethdev = cp->ethdev;
187 struct drv_ctl_info info;
188 struct drv_ctl_io *io = &info.data.io;
189
190 info.cmd = DRV_CTL_IO_RD_CMD;
191 io->offset = off;
192 ethdev->drv_ctl(dev->netdev, &info);
193 return io->data;
194}
195
196static int cnic_in_use(struct cnic_sock *csk)
197{
198 return test_bit(SK_F_INUSE, &csk->flags);
199}
200
201static void cnic_kwq_completion(struct cnic_dev *dev, u32 count)
202{
203 struct cnic_local *cp = dev->cnic_priv;
204 struct cnic_eth_dev *ethdev = cp->ethdev;
205 struct drv_ctl_info info;
206
207 info.cmd = DRV_CTL_COMPLETION_CMD;
208 info.data.comp.comp_count = count;
209 ethdev->drv_ctl(dev->netdev, &info);
210}
211
212static int cnic_send_nlmsg(struct cnic_local *cp, u32 type,
213 struct cnic_sock *csk)
214{
215 struct iscsi_path path_req;
216 char *buf = NULL;
217 u16 len = 0;
218 u32 msg_type = ISCSI_KEVENT_IF_DOWN;
219 struct cnic_ulp_ops *ulp_ops;
220
221 if (cp->uio_dev == -1)
222 return -ENODEV;
223
224 if (csk) {
225 len = sizeof(path_req);
226 buf = (char *) &path_req;
227 memset(&path_req, 0, len);
228
229 msg_type = ISCSI_KEVENT_PATH_REQ;
230 path_req.handle = (u64) csk->l5_cid;
231 if (test_bit(SK_F_IPV6, &csk->flags)) {
232 memcpy(&path_req.dst.v6_addr, &csk->dst_ip[0],
233 sizeof(struct in6_addr));
234 path_req.ip_addr_len = 16;
235 } else {
236 memcpy(&path_req.dst.v4_addr, &csk->dst_ip[0],
237 sizeof(struct in_addr));
238 path_req.ip_addr_len = 4;
239 }
240 path_req.vlan_id = csk->vlan_id;
241 path_req.pmtu = csk->mtu;
242 }
243
244 rcu_read_lock();
Michael Chan6d7760a2009-07-27 11:25:58 -0700245 ulp_ops = rcu_dereference(cnic_ulp_tbl[CNIC_ULP_ISCSI]);
Michael Chana4636962009-06-08 18:14:43 -0700246 if (ulp_ops)
247 ulp_ops->iscsi_nl_send_msg(cp->dev, msg_type, buf, len);
248 rcu_read_unlock();
249 return 0;
250}
251
252static int cnic_iscsi_nl_msg_recv(struct cnic_dev *dev, u32 msg_type,
253 char *buf, u16 len)
254{
255 int rc = -EINVAL;
256
257 switch (msg_type) {
258 case ISCSI_UEVENT_PATH_UPDATE: {
259 struct cnic_local *cp;
260 u32 l5_cid;
261 struct cnic_sock *csk;
262 struct iscsi_path *path_resp;
263
264 if (len < sizeof(*path_resp))
265 break;
266
267 path_resp = (struct iscsi_path *) buf;
268 cp = dev->cnic_priv;
269 l5_cid = (u32) path_resp->handle;
270 if (l5_cid >= MAX_CM_SK_TBL_SZ)
271 break;
272
273 csk = &cp->csk_tbl[l5_cid];
274 csk_hold(csk);
275 if (cnic_in_use(csk)) {
276 memcpy(csk->ha, path_resp->mac_addr, 6);
277 if (test_bit(SK_F_IPV6, &csk->flags))
278 memcpy(&csk->src_ip[0], &path_resp->src.v6_addr,
279 sizeof(struct in6_addr));
280 else
281 memcpy(&csk->src_ip[0], &path_resp->src.v4_addr,
282 sizeof(struct in_addr));
283 if (is_valid_ether_addr(csk->ha))
284 cnic_cm_set_pg(csk);
285 }
286 csk_put(csk);
287 rc = 0;
288 }
289 }
290
291 return rc;
292}
293
294static int cnic_offld_prep(struct cnic_sock *csk)
295{
296 if (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
297 return 0;
298
299 if (!test_bit(SK_F_CONNECT_START, &csk->flags)) {
300 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
301 return 0;
302 }
303
304 return 1;
305}
306
307static int cnic_close_prep(struct cnic_sock *csk)
308{
309 clear_bit(SK_F_CONNECT_START, &csk->flags);
310 smp_mb__after_clear_bit();
311
312 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
313 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
314 msleep(1);
315
316 return 1;
317 }
318 return 0;
319}
320
321static int cnic_abort_prep(struct cnic_sock *csk)
322{
323 clear_bit(SK_F_CONNECT_START, &csk->flags);
324 smp_mb__after_clear_bit();
325
326 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
327 msleep(1);
328
329 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
330 csk->state = L4_KCQE_OPCODE_VALUE_RESET_COMP;
331 return 1;
332 }
333
334 return 0;
335}
336
Michael Chan6d7760a2009-07-27 11:25:58 -0700337static void cnic_uio_stop(void)
338{
339 struct cnic_dev *dev;
340
341 read_lock(&cnic_dev_lock);
342 list_for_each_entry(dev, &cnic_dev_list, list) {
343 struct cnic_local *cp = dev->cnic_priv;
344
345 if (cp->cnic_uinfo)
346 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
347 }
348 read_unlock(&cnic_dev_lock);
349}
350
Michael Chana4636962009-06-08 18:14:43 -0700351int cnic_register_driver(int ulp_type, struct cnic_ulp_ops *ulp_ops)
352{
353 struct cnic_dev *dev;
354
355 if (ulp_type >= MAX_CNIC_ULP_TYPE) {
356 printk(KERN_ERR PFX "cnic_register_driver: Bad type %d\n",
357 ulp_type);
358 return -EINVAL;
359 }
360 mutex_lock(&cnic_lock);
361 if (cnic_ulp_tbl[ulp_type]) {
362 printk(KERN_ERR PFX "cnic_register_driver: Type %d has already "
363 "been registered\n", ulp_type);
364 mutex_unlock(&cnic_lock);
365 return -EBUSY;
366 }
367
368 read_lock(&cnic_dev_lock);
369 list_for_each_entry(dev, &cnic_dev_list, list) {
370 struct cnic_local *cp = dev->cnic_priv;
371
372 clear_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]);
373 }
374 read_unlock(&cnic_dev_lock);
375
Michael Chan7fc1ece2009-08-14 15:49:47 +0000376 atomic_set(&ulp_ops->ref_count, 0);
Michael Chana4636962009-06-08 18:14:43 -0700377 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], ulp_ops);
378 mutex_unlock(&cnic_lock);
379
380 /* Prevent race conditions with netdev_event */
381 rtnl_lock();
382 read_lock(&cnic_dev_lock);
383 list_for_each_entry(dev, &cnic_dev_list, list) {
384 struct cnic_local *cp = dev->cnic_priv;
385
386 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]))
387 ulp_ops->cnic_init(dev);
388 }
389 read_unlock(&cnic_dev_lock);
390 rtnl_unlock();
391
392 return 0;
393}
394
395int cnic_unregister_driver(int ulp_type)
396{
397 struct cnic_dev *dev;
Michael Chan7fc1ece2009-08-14 15:49:47 +0000398 struct cnic_ulp_ops *ulp_ops;
399 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700400
401 if (ulp_type >= MAX_CNIC_ULP_TYPE) {
402 printk(KERN_ERR PFX "cnic_unregister_driver: Bad type %d\n",
403 ulp_type);
404 return -EINVAL;
405 }
406 mutex_lock(&cnic_lock);
Michael Chan7fc1ece2009-08-14 15:49:47 +0000407 ulp_ops = cnic_ulp_tbl[ulp_type];
408 if (!ulp_ops) {
Michael Chana4636962009-06-08 18:14:43 -0700409 printk(KERN_ERR PFX "cnic_unregister_driver: Type %d has not "
410 "been registered\n", ulp_type);
411 goto out_unlock;
412 }
413 read_lock(&cnic_dev_lock);
414 list_for_each_entry(dev, &cnic_dev_list, list) {
415 struct cnic_local *cp = dev->cnic_priv;
416
417 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
418 printk(KERN_ERR PFX "cnic_unregister_driver: Type %d "
419 "still has devices registered\n", ulp_type);
420 read_unlock(&cnic_dev_lock);
421 goto out_unlock;
422 }
423 }
424 read_unlock(&cnic_dev_lock);
425
Michael Chan6d7760a2009-07-27 11:25:58 -0700426 if (ulp_type == CNIC_ULP_ISCSI)
427 cnic_uio_stop();
428
Michael Chana4636962009-06-08 18:14:43 -0700429 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], NULL);
430
431 mutex_unlock(&cnic_lock);
432 synchronize_rcu();
Michael Chan7fc1ece2009-08-14 15:49:47 +0000433 while ((atomic_read(&ulp_ops->ref_count) != 0) && (i < 20)) {
434 msleep(100);
435 i++;
436 }
437
438 if (atomic_read(&ulp_ops->ref_count) != 0)
439 printk(KERN_WARNING PFX "%s: Failed waiting for ref count to go"
440 " to zero.\n", dev->netdev->name);
Michael Chana4636962009-06-08 18:14:43 -0700441 return 0;
442
443out_unlock:
444 mutex_unlock(&cnic_lock);
445 return -EINVAL;
446}
447
448static int cnic_start_hw(struct cnic_dev *);
449static void cnic_stop_hw(struct cnic_dev *);
450
451static int cnic_register_device(struct cnic_dev *dev, int ulp_type,
452 void *ulp_ctx)
453{
454 struct cnic_local *cp = dev->cnic_priv;
455 struct cnic_ulp_ops *ulp_ops;
456
457 if (ulp_type >= MAX_CNIC_ULP_TYPE) {
458 printk(KERN_ERR PFX "cnic_register_device: Bad type %d\n",
459 ulp_type);
460 return -EINVAL;
461 }
462 mutex_lock(&cnic_lock);
463 if (cnic_ulp_tbl[ulp_type] == NULL) {
464 printk(KERN_ERR PFX "cnic_register_device: Driver with type %d "
465 "has not been registered\n", ulp_type);
466 mutex_unlock(&cnic_lock);
467 return -EAGAIN;
468 }
469 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
470 printk(KERN_ERR PFX "cnic_register_device: Type %d has already "
471 "been registered to this device\n", ulp_type);
472 mutex_unlock(&cnic_lock);
473 return -EBUSY;
474 }
475
476 clear_bit(ULP_F_START, &cp->ulp_flags[ulp_type]);
477 cp->ulp_handle[ulp_type] = ulp_ctx;
478 ulp_ops = cnic_ulp_tbl[ulp_type];
479 rcu_assign_pointer(cp->ulp_ops[ulp_type], ulp_ops);
480 cnic_hold(dev);
481
482 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
483 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[ulp_type]))
484 ulp_ops->cnic_start(cp->ulp_handle[ulp_type]);
485
486 mutex_unlock(&cnic_lock);
487
488 return 0;
489
490}
491EXPORT_SYMBOL(cnic_register_driver);
492
493static int cnic_unregister_device(struct cnic_dev *dev, int ulp_type)
494{
495 struct cnic_local *cp = dev->cnic_priv;
Michael Chan681dbd72009-08-14 15:49:46 +0000496 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700497
498 if (ulp_type >= MAX_CNIC_ULP_TYPE) {
499 printk(KERN_ERR PFX "cnic_unregister_device: Bad type %d\n",
500 ulp_type);
501 return -EINVAL;
502 }
503 mutex_lock(&cnic_lock);
504 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
505 rcu_assign_pointer(cp->ulp_ops[ulp_type], NULL);
506 cnic_put(dev);
507 } else {
508 printk(KERN_ERR PFX "cnic_unregister_device: device not "
509 "registered to this ulp type %d\n", ulp_type);
510 mutex_unlock(&cnic_lock);
511 return -EINVAL;
512 }
513 mutex_unlock(&cnic_lock);
514
515 synchronize_rcu();
516
Michael Chan681dbd72009-08-14 15:49:46 +0000517 while (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]) &&
518 i < 20) {
519 msleep(100);
520 i++;
521 }
522 if (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]))
523 printk(KERN_WARNING PFX "%s: Failed waiting for ULP up call"
524 " to complete.\n", dev->netdev->name);
525
Michael Chana4636962009-06-08 18:14:43 -0700526 return 0;
527}
528EXPORT_SYMBOL(cnic_unregister_driver);
529
530static int cnic_init_id_tbl(struct cnic_id_tbl *id_tbl, u32 size, u32 start_id)
531{
532 id_tbl->start = start_id;
533 id_tbl->max = size;
534 id_tbl->next = 0;
535 spin_lock_init(&id_tbl->lock);
536 id_tbl->table = kzalloc(DIV_ROUND_UP(size, 32) * 4, GFP_KERNEL);
537 if (!id_tbl->table)
538 return -ENOMEM;
539
540 return 0;
541}
542
543static void cnic_free_id_tbl(struct cnic_id_tbl *id_tbl)
544{
545 kfree(id_tbl->table);
546 id_tbl->table = NULL;
547}
548
549static int cnic_alloc_id(struct cnic_id_tbl *id_tbl, u32 id)
550{
551 int ret = -1;
552
553 id -= id_tbl->start;
554 if (id >= id_tbl->max)
555 return ret;
556
557 spin_lock(&id_tbl->lock);
558 if (!test_bit(id, id_tbl->table)) {
559 set_bit(id, id_tbl->table);
560 ret = 0;
561 }
562 spin_unlock(&id_tbl->lock);
563 return ret;
564}
565
566/* Returns -1 if not successful */
567static u32 cnic_alloc_new_id(struct cnic_id_tbl *id_tbl)
568{
569 u32 id;
570
571 spin_lock(&id_tbl->lock);
572 id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
573 if (id >= id_tbl->max) {
574 id = -1;
575 if (id_tbl->next != 0) {
576 id = find_first_zero_bit(id_tbl->table, id_tbl->next);
577 if (id >= id_tbl->next)
578 id = -1;
579 }
580 }
581
582 if (id < id_tbl->max) {
583 set_bit(id, id_tbl->table);
584 id_tbl->next = (id + 1) & (id_tbl->max - 1);
585 id += id_tbl->start;
586 }
587
588 spin_unlock(&id_tbl->lock);
589
590 return id;
591}
592
593static void cnic_free_id(struct cnic_id_tbl *id_tbl, u32 id)
594{
595 if (id == -1)
596 return;
597
598 id -= id_tbl->start;
599 if (id >= id_tbl->max)
600 return;
601
602 clear_bit(id, id_tbl->table);
603}
604
605static void cnic_free_dma(struct cnic_dev *dev, struct cnic_dma *dma)
606{
607 int i;
608
609 if (!dma->pg_arr)
610 return;
611
612 for (i = 0; i < dma->num_pages; i++) {
613 if (dma->pg_arr[i]) {
614 pci_free_consistent(dev->pcidev, BCM_PAGE_SIZE,
615 dma->pg_arr[i], dma->pg_map_arr[i]);
616 dma->pg_arr[i] = NULL;
617 }
618 }
619 if (dma->pgtbl) {
620 pci_free_consistent(dev->pcidev, dma->pgtbl_size,
621 dma->pgtbl, dma->pgtbl_map);
622 dma->pgtbl = NULL;
623 }
624 kfree(dma->pg_arr);
625 dma->pg_arr = NULL;
626 dma->num_pages = 0;
627}
628
629static void cnic_setup_page_tbl(struct cnic_dev *dev, struct cnic_dma *dma)
630{
631 int i;
632 u32 *page_table = dma->pgtbl;
633
634 for (i = 0; i < dma->num_pages; i++) {
635 /* Each entry needs to be in big endian format. */
636 *page_table = (u32) ((u64) dma->pg_map_arr[i] >> 32);
637 page_table++;
638 *page_table = (u32) dma->pg_map_arr[i];
639 page_table++;
640 }
641}
642
643static int cnic_alloc_dma(struct cnic_dev *dev, struct cnic_dma *dma,
644 int pages, int use_pg_tbl)
645{
646 int i, size;
647 struct cnic_local *cp = dev->cnic_priv;
648
649 size = pages * (sizeof(void *) + sizeof(dma_addr_t));
650 dma->pg_arr = kzalloc(size, GFP_ATOMIC);
651 if (dma->pg_arr == NULL)
652 return -ENOMEM;
653
654 dma->pg_map_arr = (dma_addr_t *) (dma->pg_arr + pages);
655 dma->num_pages = pages;
656
657 for (i = 0; i < pages; i++) {
658 dma->pg_arr[i] = pci_alloc_consistent(dev->pcidev,
659 BCM_PAGE_SIZE,
660 &dma->pg_map_arr[i]);
661 if (dma->pg_arr[i] == NULL)
662 goto error;
663 }
664 if (!use_pg_tbl)
665 return 0;
666
667 dma->pgtbl_size = ((pages * 8) + BCM_PAGE_SIZE - 1) &
668 ~(BCM_PAGE_SIZE - 1);
669 dma->pgtbl = pci_alloc_consistent(dev->pcidev, dma->pgtbl_size,
670 &dma->pgtbl_map);
671 if (dma->pgtbl == NULL)
672 goto error;
673
674 cp->setup_pgtbl(dev, dma);
675
676 return 0;
677
678error:
679 cnic_free_dma(dev, dma);
680 return -ENOMEM;
681}
682
Michael Chan86b53602009-10-10 13:46:57 +0000683static void cnic_free_context(struct cnic_dev *dev)
684{
685 struct cnic_local *cp = dev->cnic_priv;
686 int i;
687
688 for (i = 0; i < cp->ctx_blks; i++) {
689 if (cp->ctx_arr[i].ctx) {
690 pci_free_consistent(dev->pcidev, cp->ctx_blk_size,
691 cp->ctx_arr[i].ctx,
692 cp->ctx_arr[i].mapping);
693 cp->ctx_arr[i].ctx = NULL;
694 }
695 }
696}
697
Michael Chana4636962009-06-08 18:14:43 -0700698static void cnic_free_resc(struct cnic_dev *dev)
699{
700 struct cnic_local *cp = dev->cnic_priv;
701 int i = 0;
702
703 if (cp->cnic_uinfo) {
Michael Chana4636962009-06-08 18:14:43 -0700704 while (cp->uio_dev != -1 && i < 15) {
705 msleep(100);
706 i++;
707 }
708 uio_unregister_device(cp->cnic_uinfo);
709 kfree(cp->cnic_uinfo);
710 cp->cnic_uinfo = NULL;
711 }
712
713 if (cp->l2_buf) {
714 pci_free_consistent(dev->pcidev, cp->l2_buf_size,
715 cp->l2_buf, cp->l2_buf_map);
716 cp->l2_buf = NULL;
717 }
718
719 if (cp->l2_ring) {
720 pci_free_consistent(dev->pcidev, cp->l2_ring_size,
721 cp->l2_ring, cp->l2_ring_map);
722 cp->l2_ring = NULL;
723 }
724
Michael Chan86b53602009-10-10 13:46:57 +0000725 cnic_free_context(dev);
Michael Chana4636962009-06-08 18:14:43 -0700726 kfree(cp->ctx_arr);
727 cp->ctx_arr = NULL;
728 cp->ctx_blks = 0;
729
730 cnic_free_dma(dev, &cp->gbl_buf_info);
731 cnic_free_dma(dev, &cp->conn_buf_info);
732 cnic_free_dma(dev, &cp->kwq_info);
733 cnic_free_dma(dev, &cp->kcq_info);
734 kfree(cp->iscsi_tbl);
735 cp->iscsi_tbl = NULL;
736 kfree(cp->ctx_tbl);
737 cp->ctx_tbl = NULL;
738
739 cnic_free_id_tbl(&cp->cid_tbl);
740}
741
742static int cnic_alloc_context(struct cnic_dev *dev)
743{
744 struct cnic_local *cp = dev->cnic_priv;
745
746 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
747 int i, k, arr_size;
748
749 cp->ctx_blk_size = BCM_PAGE_SIZE;
750 cp->cids_per_blk = BCM_PAGE_SIZE / 128;
751 arr_size = BNX2_MAX_CID / cp->cids_per_blk *
752 sizeof(struct cnic_ctx);
753 cp->ctx_arr = kzalloc(arr_size, GFP_KERNEL);
754 if (cp->ctx_arr == NULL)
755 return -ENOMEM;
756
757 k = 0;
758 for (i = 0; i < 2; i++) {
759 u32 j, reg, off, lo, hi;
760
761 if (i == 0)
762 off = BNX2_PG_CTX_MAP;
763 else
764 off = BNX2_ISCSI_CTX_MAP;
765
766 reg = cnic_reg_rd_ind(dev, off);
767 lo = reg >> 16;
768 hi = reg & 0xffff;
769 for (j = lo; j < hi; j += cp->cids_per_blk, k++)
770 cp->ctx_arr[k].cid = j;
771 }
772
773 cp->ctx_blks = k;
774 if (cp->ctx_blks >= (BNX2_MAX_CID / cp->cids_per_blk)) {
775 cp->ctx_blks = 0;
776 return -ENOMEM;
777 }
778
779 for (i = 0; i < cp->ctx_blks; i++) {
780 cp->ctx_arr[i].ctx =
781 pci_alloc_consistent(dev->pcidev, BCM_PAGE_SIZE,
782 &cp->ctx_arr[i].mapping);
783 if (cp->ctx_arr[i].ctx == NULL)
784 return -ENOMEM;
785 }
786 }
787 return 0;
788}
789
Michael Chanec0248e2009-08-26 09:49:22 +0000790static int cnic_alloc_l2_rings(struct cnic_dev *dev, int pages)
791{
792 struct cnic_local *cp = dev->cnic_priv;
793
794 cp->l2_ring_size = pages * BCM_PAGE_SIZE;
795 cp->l2_ring = pci_alloc_consistent(dev->pcidev, cp->l2_ring_size,
796 &cp->l2_ring_map);
797 if (!cp->l2_ring)
798 return -ENOMEM;
799
800 cp->l2_buf_size = (cp->l2_rx_ring_size + 1) * cp->l2_single_buf_size;
801 cp->l2_buf_size = PAGE_ALIGN(cp->l2_buf_size);
802 cp->l2_buf = pci_alloc_consistent(dev->pcidev, cp->l2_buf_size,
803 &cp->l2_buf_map);
804 if (!cp->l2_buf)
805 return -ENOMEM;
806
807 return 0;
808}
809
Michael Chan5e9b2db2009-08-26 09:49:23 +0000810static int cnic_alloc_uio(struct cnic_dev *dev) {
811 struct cnic_local *cp = dev->cnic_priv;
812 struct uio_info *uinfo;
813 int ret;
814
815 uinfo = kzalloc(sizeof(*uinfo), GFP_ATOMIC);
816 if (!uinfo)
817 return -ENOMEM;
818
819 uinfo->mem[0].addr = dev->netdev->base_addr;
820 uinfo->mem[0].internal_addr = dev->regview;
821 uinfo->mem[0].size = dev->netdev->mem_end - dev->netdev->mem_start;
822 uinfo->mem[0].memtype = UIO_MEM_PHYS;
823
Michael Chan5e9b2db2009-08-26 09:49:23 +0000824 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
Michael Chan86b53602009-10-10 13:46:57 +0000825 uinfo->mem[1].addr = (unsigned long) cp->status_blk & PAGE_MASK;
Michael Chan5e9b2db2009-08-26 09:49:23 +0000826 if (cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
827 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE * 9;
828 else
829 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE;
830
831 uinfo->name = "bnx2_cnic";
832 }
833
834 uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
835
836 uinfo->mem[2].addr = (unsigned long) cp->l2_ring;
837 uinfo->mem[2].size = cp->l2_ring_size;
838 uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
839
840 uinfo->mem[3].addr = (unsigned long) cp->l2_buf;
841 uinfo->mem[3].size = cp->l2_buf_size;
842 uinfo->mem[3].memtype = UIO_MEM_LOGICAL;
843
844 uinfo->version = CNIC_MODULE_VERSION;
845 uinfo->irq = UIO_IRQ_CUSTOM;
846
847 uinfo->open = cnic_uio_open;
848 uinfo->release = cnic_uio_close;
849
850 uinfo->priv = dev;
851
852 ret = uio_register_device(&dev->pcidev->dev, uinfo);
853 if (ret) {
854 kfree(uinfo);
855 return ret;
856 }
857
858 cp->cnic_uinfo = uinfo;
859 return 0;
860}
861
Michael Chana4636962009-06-08 18:14:43 -0700862static int cnic_alloc_bnx2_resc(struct cnic_dev *dev)
863{
864 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -0700865 int ret;
866
867 ret = cnic_alloc_dma(dev, &cp->kwq_info, KWQ_PAGE_CNT, 1);
868 if (ret)
869 goto error;
870 cp->kwq = (struct kwqe **) cp->kwq_info.pg_arr;
871
872 ret = cnic_alloc_dma(dev, &cp->kcq_info, KCQ_PAGE_CNT, 1);
873 if (ret)
874 goto error;
875 cp->kcq = (struct kcqe **) cp->kcq_info.pg_arr;
876
877 ret = cnic_alloc_context(dev);
878 if (ret)
879 goto error;
880
Michael Chanec0248e2009-08-26 09:49:22 +0000881 ret = cnic_alloc_l2_rings(dev, 2);
882 if (ret)
Michael Chana4636962009-06-08 18:14:43 -0700883 goto error;
884
Michael Chan5e9b2db2009-08-26 09:49:23 +0000885 ret = cnic_alloc_uio(dev);
886 if (ret)
Michael Chana4636962009-06-08 18:14:43 -0700887 goto error;
888
Michael Chana4636962009-06-08 18:14:43 -0700889 return 0;
890
891error:
892 cnic_free_resc(dev);
893 return ret;
894}
895
896static inline u32 cnic_kwq_avail(struct cnic_local *cp)
897{
898 return cp->max_kwq_idx -
899 ((cp->kwq_prod_idx - cp->kwq_con_idx) & cp->max_kwq_idx);
900}
901
902static int cnic_submit_bnx2_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
903 u32 num_wqes)
904{
905 struct cnic_local *cp = dev->cnic_priv;
906 struct kwqe *prod_qe;
907 u16 prod, sw_prod, i;
908
909 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
910 return -EAGAIN; /* bnx2 is down */
911
912 spin_lock_bh(&cp->cnic_ulp_lock);
913 if (num_wqes > cnic_kwq_avail(cp) &&
914 !(cp->cnic_local_flags & CNIC_LCL_FL_KWQ_INIT)) {
915 spin_unlock_bh(&cp->cnic_ulp_lock);
916 return -EAGAIN;
917 }
918
919 cp->cnic_local_flags &= ~CNIC_LCL_FL_KWQ_INIT;
920
921 prod = cp->kwq_prod_idx;
922 sw_prod = prod & MAX_KWQ_IDX;
923 for (i = 0; i < num_wqes; i++) {
924 prod_qe = &cp->kwq[KWQ_PG(sw_prod)][KWQ_IDX(sw_prod)];
925 memcpy(prod_qe, wqes[i], sizeof(struct kwqe));
926 prod++;
927 sw_prod = prod & MAX_KWQ_IDX;
928 }
929 cp->kwq_prod_idx = prod;
930
931 CNIC_WR16(dev, cp->kwq_io_addr, cp->kwq_prod_idx);
932
933 spin_unlock_bh(&cp->cnic_ulp_lock);
934 return 0;
935}
936
937static void service_kcqes(struct cnic_dev *dev, int num_cqes)
938{
939 struct cnic_local *cp = dev->cnic_priv;
940 int i, j;
941
942 i = 0;
943 j = 1;
944 while (num_cqes) {
945 struct cnic_ulp_ops *ulp_ops;
946 int ulp_type;
947 u32 kcqe_op_flag = cp->completed_kcq[i]->kcqe_op_flag;
948 u32 kcqe_layer = kcqe_op_flag & KCQE_FLAGS_LAYER_MASK;
949
950 if (unlikely(kcqe_op_flag & KCQE_RAMROD_COMPLETION))
951 cnic_kwq_completion(dev, 1);
952
953 while (j < num_cqes) {
954 u32 next_op = cp->completed_kcq[i + j]->kcqe_op_flag;
955
956 if ((next_op & KCQE_FLAGS_LAYER_MASK) != kcqe_layer)
957 break;
958
959 if (unlikely(next_op & KCQE_RAMROD_COMPLETION))
960 cnic_kwq_completion(dev, 1);
961 j++;
962 }
963
964 if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_RDMA)
965 ulp_type = CNIC_ULP_RDMA;
966 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_ISCSI)
967 ulp_type = CNIC_ULP_ISCSI;
968 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L4)
969 ulp_type = CNIC_ULP_L4;
970 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L2)
971 goto end;
972 else {
973 printk(KERN_ERR PFX "%s: Unknown type of KCQE(0x%x)\n",
974 dev->netdev->name, kcqe_op_flag);
975 goto end;
976 }
977
978 rcu_read_lock();
979 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
980 if (likely(ulp_ops)) {
981 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
982 cp->completed_kcq + i, j);
983 }
984 rcu_read_unlock();
985end:
986 num_cqes -= j;
987 i += j;
988 j = 1;
989 }
990 return;
991}
992
993static u16 cnic_bnx2_next_idx(u16 idx)
994{
995 return idx + 1;
996}
997
998static u16 cnic_bnx2_hw_idx(u16 idx)
999{
1000 return idx;
1001}
1002
1003static int cnic_get_kcqes(struct cnic_dev *dev, u16 hw_prod, u16 *sw_prod)
1004{
1005 struct cnic_local *cp = dev->cnic_priv;
1006 u16 i, ri, last;
1007 struct kcqe *kcqe;
1008 int kcqe_cnt = 0, last_cnt = 0;
1009
1010 i = ri = last = *sw_prod;
1011 ri &= MAX_KCQ_IDX;
1012
1013 while ((i != hw_prod) && (kcqe_cnt < MAX_COMPLETED_KCQE)) {
1014 kcqe = &cp->kcq[KCQ_PG(ri)][KCQ_IDX(ri)];
1015 cp->completed_kcq[kcqe_cnt++] = kcqe;
1016 i = cp->next_idx(i);
1017 ri = i & MAX_KCQ_IDX;
1018 if (likely(!(kcqe->kcqe_op_flag & KCQE_FLAGS_NEXT))) {
1019 last_cnt = kcqe_cnt;
1020 last = i;
1021 }
1022 }
1023
1024 *sw_prod = last;
1025 return last_cnt;
1026}
1027
Michael Chan86b53602009-10-10 13:46:57 +00001028static void cnic_chk_pkt_rings(struct cnic_local *cp)
Michael Chana4636962009-06-08 18:14:43 -07001029{
1030 u16 rx_cons = *cp->rx_cons_ptr;
1031 u16 tx_cons = *cp->tx_cons_ptr;
1032
1033 if (cp->tx_cons != tx_cons || cp->rx_cons != rx_cons) {
1034 cp->tx_cons = tx_cons;
1035 cp->rx_cons = rx_cons;
1036 uio_event_notify(cp->cnic_uinfo);
1037 }
1038}
1039
1040static int cnic_service_bnx2(void *data, void *status_blk)
1041{
1042 struct cnic_dev *dev = data;
1043 struct status_block *sblk = status_blk;
1044 struct cnic_local *cp = dev->cnic_priv;
1045 u32 status_idx = sblk->status_idx;
1046 u16 hw_prod, sw_prod;
1047 int kcqe_cnt;
1048
1049 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
1050 return status_idx;
1051
1052 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
1053
1054 hw_prod = sblk->status_completion_producer_index;
1055 sw_prod = cp->kcq_prod_idx;
1056 while (sw_prod != hw_prod) {
1057 kcqe_cnt = cnic_get_kcqes(dev, hw_prod, &sw_prod);
1058 if (kcqe_cnt == 0)
1059 goto done;
1060
1061 service_kcqes(dev, kcqe_cnt);
1062
1063 /* Tell compiler that status_blk fields can change. */
1064 barrier();
1065 if (status_idx != sblk->status_idx) {
1066 status_idx = sblk->status_idx;
1067 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
1068 hw_prod = sblk->status_completion_producer_index;
1069 } else
1070 break;
1071 }
1072
1073done:
1074 CNIC_WR16(dev, cp->kcq_io_addr, sw_prod);
1075
1076 cp->kcq_prod_idx = sw_prod;
1077
Michael Chan86b53602009-10-10 13:46:57 +00001078 cnic_chk_pkt_rings(cp);
Michael Chana4636962009-06-08 18:14:43 -07001079 return status_idx;
1080}
1081
1082static void cnic_service_bnx2_msix(unsigned long data)
1083{
1084 struct cnic_dev *dev = (struct cnic_dev *) data;
1085 struct cnic_local *cp = dev->cnic_priv;
1086 struct status_block_msix *status_blk = cp->bnx2_status_blk;
1087 u32 status_idx = status_blk->status_idx;
1088 u16 hw_prod, sw_prod;
1089 int kcqe_cnt;
1090
1091 cp->kwq_con_idx = status_blk->status_cmd_consumer_index;
1092
1093 hw_prod = status_blk->status_completion_producer_index;
1094 sw_prod = cp->kcq_prod_idx;
1095 while (sw_prod != hw_prod) {
1096 kcqe_cnt = cnic_get_kcqes(dev, hw_prod, &sw_prod);
1097 if (kcqe_cnt == 0)
1098 goto done;
1099
1100 service_kcqes(dev, kcqe_cnt);
1101
1102 /* Tell compiler that status_blk fields can change. */
1103 barrier();
1104 if (status_idx != status_blk->status_idx) {
1105 status_idx = status_blk->status_idx;
1106 cp->kwq_con_idx = status_blk->status_cmd_consumer_index;
1107 hw_prod = status_blk->status_completion_producer_index;
1108 } else
1109 break;
1110 }
1111
1112done:
1113 CNIC_WR16(dev, cp->kcq_io_addr, sw_prod);
1114 cp->kcq_prod_idx = sw_prod;
1115
Michael Chan86b53602009-10-10 13:46:57 +00001116 cnic_chk_pkt_rings(cp);
Michael Chana4636962009-06-08 18:14:43 -07001117
1118 cp->last_status_idx = status_idx;
1119 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
1120 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
1121}
1122
1123static irqreturn_t cnic_irq(int irq, void *dev_instance)
1124{
1125 struct cnic_dev *dev = dev_instance;
1126 struct cnic_local *cp = dev->cnic_priv;
1127 u16 prod = cp->kcq_prod_idx & MAX_KCQ_IDX;
1128
1129 if (cp->ack_int)
1130 cp->ack_int(dev);
1131
1132 prefetch(cp->status_blk);
1133 prefetch(&cp->kcq[KCQ_PG(prod)][KCQ_IDX(prod)]);
1134
1135 if (likely(test_bit(CNIC_F_CNIC_UP, &dev->flags)))
1136 tasklet_schedule(&cp->cnic_irq_task);
1137
1138 return IRQ_HANDLED;
1139}
1140
1141static void cnic_ulp_stop(struct cnic_dev *dev)
1142{
1143 struct cnic_local *cp = dev->cnic_priv;
1144 int if_type;
1145
Michael Chan6d7760a2009-07-27 11:25:58 -07001146 if (cp->cnic_uinfo)
1147 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
1148
Michael Chana4636962009-06-08 18:14:43 -07001149 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
1150 struct cnic_ulp_ops *ulp_ops;
1151
Michael Chan681dbd72009-08-14 15:49:46 +00001152 mutex_lock(&cnic_lock);
1153 ulp_ops = cp->ulp_ops[if_type];
1154 if (!ulp_ops) {
1155 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07001156 continue;
Michael Chan681dbd72009-08-14 15:49:46 +00001157 }
1158 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
1159 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07001160
1161 if (test_and_clear_bit(ULP_F_START, &cp->ulp_flags[if_type]))
1162 ulp_ops->cnic_stop(cp->ulp_handle[if_type]);
Michael Chan681dbd72009-08-14 15:49:46 +00001163
1164 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
Michael Chana4636962009-06-08 18:14:43 -07001165 }
Michael Chana4636962009-06-08 18:14:43 -07001166}
1167
1168static void cnic_ulp_start(struct cnic_dev *dev)
1169{
1170 struct cnic_local *cp = dev->cnic_priv;
1171 int if_type;
1172
Michael Chana4636962009-06-08 18:14:43 -07001173 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
1174 struct cnic_ulp_ops *ulp_ops;
1175
Michael Chan681dbd72009-08-14 15:49:46 +00001176 mutex_lock(&cnic_lock);
1177 ulp_ops = cp->ulp_ops[if_type];
1178 if (!ulp_ops || !ulp_ops->cnic_start) {
1179 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07001180 continue;
Michael Chan681dbd72009-08-14 15:49:46 +00001181 }
1182 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
1183 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07001184
1185 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[if_type]))
1186 ulp_ops->cnic_start(cp->ulp_handle[if_type]);
Michael Chan681dbd72009-08-14 15:49:46 +00001187
1188 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
Michael Chana4636962009-06-08 18:14:43 -07001189 }
Michael Chana4636962009-06-08 18:14:43 -07001190}
1191
1192static int cnic_ctl(void *data, struct cnic_ctl_info *info)
1193{
1194 struct cnic_dev *dev = data;
1195
1196 switch (info->cmd) {
1197 case CNIC_CTL_STOP_CMD:
1198 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07001199
1200 cnic_ulp_stop(dev);
1201 cnic_stop_hw(dev);
1202
Michael Chana4636962009-06-08 18:14:43 -07001203 cnic_put(dev);
1204 break;
1205 case CNIC_CTL_START_CMD:
1206 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07001207
1208 if (!cnic_start_hw(dev))
1209 cnic_ulp_start(dev);
1210
Michael Chana4636962009-06-08 18:14:43 -07001211 cnic_put(dev);
1212 break;
1213 default:
1214 return -EINVAL;
1215 }
1216 return 0;
1217}
1218
1219static void cnic_ulp_init(struct cnic_dev *dev)
1220{
1221 int i;
1222 struct cnic_local *cp = dev->cnic_priv;
1223
Michael Chana4636962009-06-08 18:14:43 -07001224 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
1225 struct cnic_ulp_ops *ulp_ops;
1226
Michael Chan7fc1ece2009-08-14 15:49:47 +00001227 mutex_lock(&cnic_lock);
1228 ulp_ops = cnic_ulp_tbl[i];
1229 if (!ulp_ops || !ulp_ops->cnic_init) {
1230 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07001231 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00001232 }
1233 ulp_get(ulp_ops);
1234 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07001235
1236 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[i]))
1237 ulp_ops->cnic_init(dev);
1238
Michael Chan7fc1ece2009-08-14 15:49:47 +00001239 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07001240 }
Michael Chana4636962009-06-08 18:14:43 -07001241}
1242
1243static void cnic_ulp_exit(struct cnic_dev *dev)
1244{
1245 int i;
1246 struct cnic_local *cp = dev->cnic_priv;
1247
Michael Chana4636962009-06-08 18:14:43 -07001248 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
1249 struct cnic_ulp_ops *ulp_ops;
1250
Michael Chan7fc1ece2009-08-14 15:49:47 +00001251 mutex_lock(&cnic_lock);
1252 ulp_ops = cnic_ulp_tbl[i];
1253 if (!ulp_ops || !ulp_ops->cnic_exit) {
1254 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07001255 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00001256 }
1257 ulp_get(ulp_ops);
1258 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07001259
1260 if (test_and_clear_bit(ULP_F_INIT, &cp->ulp_flags[i]))
1261 ulp_ops->cnic_exit(dev);
1262
Michael Chan7fc1ece2009-08-14 15:49:47 +00001263 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07001264 }
Michael Chana4636962009-06-08 18:14:43 -07001265}
1266
1267static int cnic_cm_offload_pg(struct cnic_sock *csk)
1268{
1269 struct cnic_dev *dev = csk->dev;
1270 struct l4_kwq_offload_pg *l4kwqe;
1271 struct kwqe *wqes[1];
1272
1273 l4kwqe = (struct l4_kwq_offload_pg *) &csk->kwqe1;
1274 memset(l4kwqe, 0, sizeof(*l4kwqe));
1275 wqes[0] = (struct kwqe *) l4kwqe;
1276
1277 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_OFFLOAD_PG;
1278 l4kwqe->flags =
1279 L4_LAYER_CODE << L4_KWQ_OFFLOAD_PG_LAYER_CODE_SHIFT;
1280 l4kwqe->l2hdr_nbytes = ETH_HLEN;
1281
1282 l4kwqe->da0 = csk->ha[0];
1283 l4kwqe->da1 = csk->ha[1];
1284 l4kwqe->da2 = csk->ha[2];
1285 l4kwqe->da3 = csk->ha[3];
1286 l4kwqe->da4 = csk->ha[4];
1287 l4kwqe->da5 = csk->ha[5];
1288
1289 l4kwqe->sa0 = dev->mac_addr[0];
1290 l4kwqe->sa1 = dev->mac_addr[1];
1291 l4kwqe->sa2 = dev->mac_addr[2];
1292 l4kwqe->sa3 = dev->mac_addr[3];
1293 l4kwqe->sa4 = dev->mac_addr[4];
1294 l4kwqe->sa5 = dev->mac_addr[5];
1295
1296 l4kwqe->etype = ETH_P_IP;
1297 l4kwqe->ipid_count = DEF_IPID_COUNT;
1298 l4kwqe->host_opaque = csk->l5_cid;
1299
1300 if (csk->vlan_id) {
1301 l4kwqe->pg_flags |= L4_KWQ_OFFLOAD_PG_VLAN_TAGGING;
1302 l4kwqe->vlan_tag = csk->vlan_id;
1303 l4kwqe->l2hdr_nbytes += 4;
1304 }
1305
1306 return dev->submit_kwqes(dev, wqes, 1);
1307}
1308
1309static int cnic_cm_update_pg(struct cnic_sock *csk)
1310{
1311 struct cnic_dev *dev = csk->dev;
1312 struct l4_kwq_update_pg *l4kwqe;
1313 struct kwqe *wqes[1];
1314
1315 l4kwqe = (struct l4_kwq_update_pg *) &csk->kwqe1;
1316 memset(l4kwqe, 0, sizeof(*l4kwqe));
1317 wqes[0] = (struct kwqe *) l4kwqe;
1318
1319 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPDATE_PG;
1320 l4kwqe->flags =
1321 L4_LAYER_CODE << L4_KWQ_UPDATE_PG_LAYER_CODE_SHIFT;
1322 l4kwqe->pg_cid = csk->pg_cid;
1323
1324 l4kwqe->da0 = csk->ha[0];
1325 l4kwqe->da1 = csk->ha[1];
1326 l4kwqe->da2 = csk->ha[2];
1327 l4kwqe->da3 = csk->ha[3];
1328 l4kwqe->da4 = csk->ha[4];
1329 l4kwqe->da5 = csk->ha[5];
1330
1331 l4kwqe->pg_host_opaque = csk->l5_cid;
1332 l4kwqe->pg_valids = L4_KWQ_UPDATE_PG_VALIDS_DA;
1333
1334 return dev->submit_kwqes(dev, wqes, 1);
1335}
1336
1337static int cnic_cm_upload_pg(struct cnic_sock *csk)
1338{
1339 struct cnic_dev *dev = csk->dev;
1340 struct l4_kwq_upload *l4kwqe;
1341 struct kwqe *wqes[1];
1342
1343 l4kwqe = (struct l4_kwq_upload *) &csk->kwqe1;
1344 memset(l4kwqe, 0, sizeof(*l4kwqe));
1345 wqes[0] = (struct kwqe *) l4kwqe;
1346
1347 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPLOAD_PG;
1348 l4kwqe->flags =
1349 L4_LAYER_CODE << L4_KWQ_UPLOAD_LAYER_CODE_SHIFT;
1350 l4kwqe->cid = csk->pg_cid;
1351
1352 return dev->submit_kwqes(dev, wqes, 1);
1353}
1354
1355static int cnic_cm_conn_req(struct cnic_sock *csk)
1356{
1357 struct cnic_dev *dev = csk->dev;
1358 struct l4_kwq_connect_req1 *l4kwqe1;
1359 struct l4_kwq_connect_req2 *l4kwqe2;
1360 struct l4_kwq_connect_req3 *l4kwqe3;
1361 struct kwqe *wqes[3];
1362 u8 tcp_flags = 0;
1363 int num_wqes = 2;
1364
1365 l4kwqe1 = (struct l4_kwq_connect_req1 *) &csk->kwqe1;
1366 l4kwqe2 = (struct l4_kwq_connect_req2 *) &csk->kwqe2;
1367 l4kwqe3 = (struct l4_kwq_connect_req3 *) &csk->kwqe3;
1368 memset(l4kwqe1, 0, sizeof(*l4kwqe1));
1369 memset(l4kwqe2, 0, sizeof(*l4kwqe2));
1370 memset(l4kwqe3, 0, sizeof(*l4kwqe3));
1371
1372 l4kwqe3->op_code = L4_KWQE_OPCODE_VALUE_CONNECT3;
1373 l4kwqe3->flags =
1374 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ3_LAYER_CODE_SHIFT;
1375 l4kwqe3->ka_timeout = csk->ka_timeout;
1376 l4kwqe3->ka_interval = csk->ka_interval;
1377 l4kwqe3->ka_max_probe_count = csk->ka_max_probe_count;
1378 l4kwqe3->tos = csk->tos;
1379 l4kwqe3->ttl = csk->ttl;
1380 l4kwqe3->snd_seq_scale = csk->snd_seq_scale;
1381 l4kwqe3->pmtu = csk->mtu;
1382 l4kwqe3->rcv_buf = csk->rcv_buf;
1383 l4kwqe3->snd_buf = csk->snd_buf;
1384 l4kwqe3->seed = csk->seed;
1385
1386 wqes[0] = (struct kwqe *) l4kwqe1;
1387 if (test_bit(SK_F_IPV6, &csk->flags)) {
1388 wqes[1] = (struct kwqe *) l4kwqe2;
1389 wqes[2] = (struct kwqe *) l4kwqe3;
1390 num_wqes = 3;
1391
1392 l4kwqe1->conn_flags = L4_KWQ_CONNECT_REQ1_IP_V6;
1393 l4kwqe2->op_code = L4_KWQE_OPCODE_VALUE_CONNECT2;
1394 l4kwqe2->flags =
1395 L4_KWQ_CONNECT_REQ2_LINKED_WITH_NEXT |
1396 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ2_LAYER_CODE_SHIFT;
1397 l4kwqe2->src_ip_v6_2 = be32_to_cpu(csk->src_ip[1]);
1398 l4kwqe2->src_ip_v6_3 = be32_to_cpu(csk->src_ip[2]);
1399 l4kwqe2->src_ip_v6_4 = be32_to_cpu(csk->src_ip[3]);
1400 l4kwqe2->dst_ip_v6_2 = be32_to_cpu(csk->dst_ip[1]);
1401 l4kwqe2->dst_ip_v6_3 = be32_to_cpu(csk->dst_ip[2]);
1402 l4kwqe2->dst_ip_v6_4 = be32_to_cpu(csk->dst_ip[3]);
1403 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct ipv6hdr) -
1404 sizeof(struct tcphdr);
1405 } else {
1406 wqes[1] = (struct kwqe *) l4kwqe3;
1407 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct iphdr) -
1408 sizeof(struct tcphdr);
1409 }
1410
1411 l4kwqe1->op_code = L4_KWQE_OPCODE_VALUE_CONNECT1;
1412 l4kwqe1->flags =
1413 (L4_LAYER_CODE << L4_KWQ_CONNECT_REQ1_LAYER_CODE_SHIFT) |
1414 L4_KWQ_CONNECT_REQ3_LINKED_WITH_NEXT;
1415 l4kwqe1->cid = csk->cid;
1416 l4kwqe1->pg_cid = csk->pg_cid;
1417 l4kwqe1->src_ip = be32_to_cpu(csk->src_ip[0]);
1418 l4kwqe1->dst_ip = be32_to_cpu(csk->dst_ip[0]);
1419 l4kwqe1->src_port = be16_to_cpu(csk->src_port);
1420 l4kwqe1->dst_port = be16_to_cpu(csk->dst_port);
1421 if (csk->tcp_flags & SK_TCP_NO_DELAY_ACK)
1422 tcp_flags |= L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK;
1423 if (csk->tcp_flags & SK_TCP_KEEP_ALIVE)
1424 tcp_flags |= L4_KWQ_CONNECT_REQ1_KEEP_ALIVE;
1425 if (csk->tcp_flags & SK_TCP_NAGLE)
1426 tcp_flags |= L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE;
1427 if (csk->tcp_flags & SK_TCP_TIMESTAMP)
1428 tcp_flags |= L4_KWQ_CONNECT_REQ1_TIME_STAMP;
1429 if (csk->tcp_flags & SK_TCP_SACK)
1430 tcp_flags |= L4_KWQ_CONNECT_REQ1_SACK;
1431 if (csk->tcp_flags & SK_TCP_SEG_SCALING)
1432 tcp_flags |= L4_KWQ_CONNECT_REQ1_SEG_SCALING;
1433
1434 l4kwqe1->tcp_flags = tcp_flags;
1435
1436 return dev->submit_kwqes(dev, wqes, num_wqes);
1437}
1438
1439static int cnic_cm_close_req(struct cnic_sock *csk)
1440{
1441 struct cnic_dev *dev = csk->dev;
1442 struct l4_kwq_close_req *l4kwqe;
1443 struct kwqe *wqes[1];
1444
1445 l4kwqe = (struct l4_kwq_close_req *) &csk->kwqe2;
1446 memset(l4kwqe, 0, sizeof(*l4kwqe));
1447 wqes[0] = (struct kwqe *) l4kwqe;
1448
1449 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_CLOSE;
1450 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_CLOSE_REQ_LAYER_CODE_SHIFT;
1451 l4kwqe->cid = csk->cid;
1452
1453 return dev->submit_kwqes(dev, wqes, 1);
1454}
1455
1456static int cnic_cm_abort_req(struct cnic_sock *csk)
1457{
1458 struct cnic_dev *dev = csk->dev;
1459 struct l4_kwq_reset_req *l4kwqe;
1460 struct kwqe *wqes[1];
1461
1462 l4kwqe = (struct l4_kwq_reset_req *) &csk->kwqe2;
1463 memset(l4kwqe, 0, sizeof(*l4kwqe));
1464 wqes[0] = (struct kwqe *) l4kwqe;
1465
1466 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_RESET;
1467 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_RESET_REQ_LAYER_CODE_SHIFT;
1468 l4kwqe->cid = csk->cid;
1469
1470 return dev->submit_kwqes(dev, wqes, 1);
1471}
1472
1473static int cnic_cm_create(struct cnic_dev *dev, int ulp_type, u32 cid,
1474 u32 l5_cid, struct cnic_sock **csk, void *context)
1475{
1476 struct cnic_local *cp = dev->cnic_priv;
1477 struct cnic_sock *csk1;
1478
1479 if (l5_cid >= MAX_CM_SK_TBL_SZ)
1480 return -EINVAL;
1481
1482 csk1 = &cp->csk_tbl[l5_cid];
1483 if (atomic_read(&csk1->ref_count))
1484 return -EAGAIN;
1485
1486 if (test_and_set_bit(SK_F_INUSE, &csk1->flags))
1487 return -EBUSY;
1488
1489 csk1->dev = dev;
1490 csk1->cid = cid;
1491 csk1->l5_cid = l5_cid;
1492 csk1->ulp_type = ulp_type;
1493 csk1->context = context;
1494
1495 csk1->ka_timeout = DEF_KA_TIMEOUT;
1496 csk1->ka_interval = DEF_KA_INTERVAL;
1497 csk1->ka_max_probe_count = DEF_KA_MAX_PROBE_COUNT;
1498 csk1->tos = DEF_TOS;
1499 csk1->ttl = DEF_TTL;
1500 csk1->snd_seq_scale = DEF_SND_SEQ_SCALE;
1501 csk1->rcv_buf = DEF_RCV_BUF;
1502 csk1->snd_buf = DEF_SND_BUF;
1503 csk1->seed = DEF_SEED;
1504
1505 *csk = csk1;
1506 return 0;
1507}
1508
1509static void cnic_cm_cleanup(struct cnic_sock *csk)
1510{
1511 if (csk->src_port) {
1512 struct cnic_dev *dev = csk->dev;
1513 struct cnic_local *cp = dev->cnic_priv;
1514
1515 cnic_free_id(&cp->csk_port_tbl, csk->src_port);
1516 csk->src_port = 0;
1517 }
1518}
1519
1520static void cnic_close_conn(struct cnic_sock *csk)
1521{
1522 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags)) {
1523 cnic_cm_upload_pg(csk);
1524 clear_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
1525 }
1526 cnic_cm_cleanup(csk);
1527}
1528
1529static int cnic_cm_destroy(struct cnic_sock *csk)
1530{
1531 if (!cnic_in_use(csk))
1532 return -EINVAL;
1533
1534 csk_hold(csk);
1535 clear_bit(SK_F_INUSE, &csk->flags);
1536 smp_mb__after_clear_bit();
1537 while (atomic_read(&csk->ref_count) != 1)
1538 msleep(1);
1539 cnic_cm_cleanup(csk);
1540
1541 csk->flags = 0;
1542 csk_put(csk);
1543 return 0;
1544}
1545
1546static inline u16 cnic_get_vlan(struct net_device *dev,
1547 struct net_device **vlan_dev)
1548{
1549 if (dev->priv_flags & IFF_802_1Q_VLAN) {
1550 *vlan_dev = vlan_dev_real_dev(dev);
1551 return vlan_dev_vlan_id(dev);
1552 }
1553 *vlan_dev = dev;
1554 return 0;
1555}
1556
1557static int cnic_get_v4_route(struct sockaddr_in *dst_addr,
1558 struct dst_entry **dst)
1559{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07001560#if defined(CONFIG_INET)
Michael Chana4636962009-06-08 18:14:43 -07001561 struct flowi fl;
1562 int err;
1563 struct rtable *rt;
1564
1565 memset(&fl, 0, sizeof(fl));
1566 fl.nl_u.ip4_u.daddr = dst_addr->sin_addr.s_addr;
1567
1568 err = ip_route_output_key(&init_net, &rt, &fl);
1569 if (!err)
1570 *dst = &rt->u.dst;
1571 return err;
Randy Dunlapfaea56c2009-06-12 11:43:48 -07001572#else
1573 return -ENETUNREACH;
1574#endif
Michael Chana4636962009-06-08 18:14:43 -07001575}
1576
1577static int cnic_get_v6_route(struct sockaddr_in6 *dst_addr,
1578 struct dst_entry **dst)
1579{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07001580#if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
Michael Chana4636962009-06-08 18:14:43 -07001581 struct flowi fl;
1582
1583 memset(&fl, 0, sizeof(fl));
1584 ipv6_addr_copy(&fl.fl6_dst, &dst_addr->sin6_addr);
1585 if (ipv6_addr_type(&fl.fl6_dst) & IPV6_ADDR_LINKLOCAL)
1586 fl.oif = dst_addr->sin6_scope_id;
1587
1588 *dst = ip6_route_output(&init_net, NULL, &fl);
1589 if (*dst)
1590 return 0;
1591#endif
1592
1593 return -ENETUNREACH;
1594}
1595
1596static struct cnic_dev *cnic_cm_select_dev(struct sockaddr_in *dst_addr,
1597 int ulp_type)
1598{
1599 struct cnic_dev *dev = NULL;
1600 struct dst_entry *dst;
1601 struct net_device *netdev = NULL;
1602 int err = -ENETUNREACH;
1603
1604 if (dst_addr->sin_family == AF_INET)
1605 err = cnic_get_v4_route(dst_addr, &dst);
1606 else if (dst_addr->sin_family == AF_INET6) {
1607 struct sockaddr_in6 *dst_addr6 =
1608 (struct sockaddr_in6 *) dst_addr;
1609
1610 err = cnic_get_v6_route(dst_addr6, &dst);
1611 } else
1612 return NULL;
1613
1614 if (err)
1615 return NULL;
1616
1617 if (!dst->dev)
1618 goto done;
1619
1620 cnic_get_vlan(dst->dev, &netdev);
1621
1622 dev = cnic_from_netdev(netdev);
1623
1624done:
1625 dst_release(dst);
1626 if (dev)
1627 cnic_put(dev);
1628 return dev;
1629}
1630
1631static int cnic_resolve_addr(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
1632{
1633 struct cnic_dev *dev = csk->dev;
1634 struct cnic_local *cp = dev->cnic_priv;
1635
1636 return cnic_send_nlmsg(cp, ISCSI_KEVENT_PATH_REQ, csk);
1637}
1638
1639static int cnic_get_route(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
1640{
1641 struct cnic_dev *dev = csk->dev;
1642 struct cnic_local *cp = dev->cnic_priv;
1643 int is_v6, err, rc = -ENETUNREACH;
1644 struct dst_entry *dst;
1645 struct net_device *realdev;
1646 u32 local_port;
1647
1648 if (saddr->local.v6.sin6_family == AF_INET6 &&
1649 saddr->remote.v6.sin6_family == AF_INET6)
1650 is_v6 = 1;
1651 else if (saddr->local.v4.sin_family == AF_INET &&
1652 saddr->remote.v4.sin_family == AF_INET)
1653 is_v6 = 0;
1654 else
1655 return -EINVAL;
1656
1657 clear_bit(SK_F_IPV6, &csk->flags);
1658
1659 if (is_v6) {
Randy Dunlapfaea56c2009-06-12 11:43:48 -07001660#if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
Michael Chana4636962009-06-08 18:14:43 -07001661 set_bit(SK_F_IPV6, &csk->flags);
1662 err = cnic_get_v6_route(&saddr->remote.v6, &dst);
1663 if (err)
1664 return err;
1665
1666 if (!dst || dst->error || !dst->dev)
1667 goto err_out;
1668
1669 memcpy(&csk->dst_ip[0], &saddr->remote.v6.sin6_addr,
1670 sizeof(struct in6_addr));
1671 csk->dst_port = saddr->remote.v6.sin6_port;
1672 local_port = saddr->local.v6.sin6_port;
1673#else
1674 return rc;
1675#endif
1676
1677 } else {
1678 err = cnic_get_v4_route(&saddr->remote.v4, &dst);
1679 if (err)
1680 return err;
1681
1682 if (!dst || dst->error || !dst->dev)
1683 goto err_out;
1684
1685 csk->dst_ip[0] = saddr->remote.v4.sin_addr.s_addr;
1686 csk->dst_port = saddr->remote.v4.sin_port;
1687 local_port = saddr->local.v4.sin_port;
1688 }
1689
1690 csk->vlan_id = cnic_get_vlan(dst->dev, &realdev);
1691 if (realdev != dev->netdev)
1692 goto err_out;
1693
1694 if (local_port >= CNIC_LOCAL_PORT_MIN &&
1695 local_port < CNIC_LOCAL_PORT_MAX) {
1696 if (cnic_alloc_id(&cp->csk_port_tbl, local_port))
1697 local_port = 0;
1698 } else
1699 local_port = 0;
1700
1701 if (!local_port) {
1702 local_port = cnic_alloc_new_id(&cp->csk_port_tbl);
1703 if (local_port == -1) {
1704 rc = -ENOMEM;
1705 goto err_out;
1706 }
1707 }
1708 csk->src_port = local_port;
1709
1710 csk->mtu = dst_mtu(dst);
1711 rc = 0;
1712
1713err_out:
1714 dst_release(dst);
1715 return rc;
1716}
1717
1718static void cnic_init_csk_state(struct cnic_sock *csk)
1719{
1720 csk->state = 0;
1721 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
1722 clear_bit(SK_F_CLOSING, &csk->flags);
1723}
1724
1725static int cnic_cm_connect(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
1726{
1727 int err = 0;
1728
1729 if (!cnic_in_use(csk))
1730 return -EINVAL;
1731
1732 if (test_and_set_bit(SK_F_CONNECT_START, &csk->flags))
1733 return -EINVAL;
1734
1735 cnic_init_csk_state(csk);
1736
1737 err = cnic_get_route(csk, saddr);
1738 if (err)
1739 goto err_out;
1740
1741 err = cnic_resolve_addr(csk, saddr);
1742 if (!err)
1743 return 0;
1744
1745err_out:
1746 clear_bit(SK_F_CONNECT_START, &csk->flags);
1747 return err;
1748}
1749
1750static int cnic_cm_abort(struct cnic_sock *csk)
1751{
1752 struct cnic_local *cp = csk->dev->cnic_priv;
1753 u32 opcode;
1754
1755 if (!cnic_in_use(csk))
1756 return -EINVAL;
1757
1758 if (cnic_abort_prep(csk))
1759 return cnic_cm_abort_req(csk);
1760
1761 /* Getting here means that we haven't started connect, or
1762 * connect was not successful.
1763 */
1764
1765 csk->state = L4_KCQE_OPCODE_VALUE_RESET_COMP;
1766 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
1767 opcode = csk->state;
1768 else
1769 opcode = L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD;
1770 cp->close_conn(csk, opcode);
1771
1772 return 0;
1773}
1774
1775static int cnic_cm_close(struct cnic_sock *csk)
1776{
1777 if (!cnic_in_use(csk))
1778 return -EINVAL;
1779
1780 if (cnic_close_prep(csk)) {
1781 csk->state = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
1782 return cnic_cm_close_req(csk);
1783 }
1784 return 0;
1785}
1786
1787static void cnic_cm_upcall(struct cnic_local *cp, struct cnic_sock *csk,
1788 u8 opcode)
1789{
1790 struct cnic_ulp_ops *ulp_ops;
1791 int ulp_type = csk->ulp_type;
1792
1793 rcu_read_lock();
1794 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
1795 if (ulp_ops) {
1796 if (opcode == L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE)
1797 ulp_ops->cm_connect_complete(csk);
1798 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
1799 ulp_ops->cm_close_complete(csk);
1800 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED)
1801 ulp_ops->cm_remote_abort(csk);
1802 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_COMP)
1803 ulp_ops->cm_abort_complete(csk);
1804 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED)
1805 ulp_ops->cm_remote_close(csk);
1806 }
1807 rcu_read_unlock();
1808}
1809
1810static int cnic_cm_set_pg(struct cnic_sock *csk)
1811{
1812 if (cnic_offld_prep(csk)) {
1813 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
1814 cnic_cm_update_pg(csk);
1815 else
1816 cnic_cm_offload_pg(csk);
1817 }
1818 return 0;
1819}
1820
1821static void cnic_cm_process_offld_pg(struct cnic_dev *dev, struct l4_kcq *kcqe)
1822{
1823 struct cnic_local *cp = dev->cnic_priv;
1824 u32 l5_cid = kcqe->pg_host_opaque;
1825 u8 opcode = kcqe->op_code;
1826 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
1827
1828 csk_hold(csk);
1829 if (!cnic_in_use(csk))
1830 goto done;
1831
1832 if (opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
1833 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
1834 goto done;
1835 }
1836 csk->pg_cid = kcqe->pg_cid;
1837 set_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
1838 cnic_cm_conn_req(csk);
1839
1840done:
1841 csk_put(csk);
1842}
1843
1844static void cnic_cm_process_kcqe(struct cnic_dev *dev, struct kcqe *kcqe)
1845{
1846 struct cnic_local *cp = dev->cnic_priv;
1847 struct l4_kcq *l4kcqe = (struct l4_kcq *) kcqe;
1848 u8 opcode = l4kcqe->op_code;
1849 u32 l5_cid;
1850 struct cnic_sock *csk;
1851
1852 if (opcode == L4_KCQE_OPCODE_VALUE_OFFLOAD_PG ||
1853 opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
1854 cnic_cm_process_offld_pg(dev, l4kcqe);
1855 return;
1856 }
1857
1858 l5_cid = l4kcqe->conn_id;
1859 if (opcode & 0x80)
1860 l5_cid = l4kcqe->cid;
1861 if (l5_cid >= MAX_CM_SK_TBL_SZ)
1862 return;
1863
1864 csk = &cp->csk_tbl[l5_cid];
1865 csk_hold(csk);
1866
1867 if (!cnic_in_use(csk)) {
1868 csk_put(csk);
1869 return;
1870 }
1871
1872 switch (opcode) {
1873 case L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE:
1874 if (l4kcqe->status == 0)
1875 set_bit(SK_F_OFFLD_COMPLETE, &csk->flags);
1876
1877 smp_mb__before_clear_bit();
1878 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
1879 cnic_cm_upcall(cp, csk, opcode);
1880 break;
1881
1882 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
1883 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags))
1884 csk->state = opcode;
1885 /* fall through */
1886 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
1887 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
1888 cp->close_conn(csk, opcode);
1889 break;
1890
1891 case L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED:
1892 cnic_cm_upcall(cp, csk, opcode);
1893 break;
1894 }
1895 csk_put(csk);
1896}
1897
1898static void cnic_cm_indicate_kcqe(void *data, struct kcqe *kcqe[], u32 num)
1899{
1900 struct cnic_dev *dev = data;
1901 int i;
1902
1903 for (i = 0; i < num; i++)
1904 cnic_cm_process_kcqe(dev, kcqe[i]);
1905}
1906
1907static struct cnic_ulp_ops cm_ulp_ops = {
1908 .indicate_kcqes = cnic_cm_indicate_kcqe,
1909};
1910
1911static void cnic_cm_free_mem(struct cnic_dev *dev)
1912{
1913 struct cnic_local *cp = dev->cnic_priv;
1914
1915 kfree(cp->csk_tbl);
1916 cp->csk_tbl = NULL;
1917 cnic_free_id_tbl(&cp->csk_port_tbl);
1918}
1919
1920static int cnic_cm_alloc_mem(struct cnic_dev *dev)
1921{
1922 struct cnic_local *cp = dev->cnic_priv;
1923
1924 cp->csk_tbl = kzalloc(sizeof(struct cnic_sock) * MAX_CM_SK_TBL_SZ,
1925 GFP_KERNEL);
1926 if (!cp->csk_tbl)
1927 return -ENOMEM;
1928
1929 if (cnic_init_id_tbl(&cp->csk_port_tbl, CNIC_LOCAL_PORT_RANGE,
1930 CNIC_LOCAL_PORT_MIN)) {
1931 cnic_cm_free_mem(dev);
1932 return -ENOMEM;
1933 }
1934 return 0;
1935}
1936
1937static int cnic_ready_to_close(struct cnic_sock *csk, u32 opcode)
1938{
1939 if ((opcode == csk->state) ||
1940 (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED &&
1941 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)) {
1942 if (!test_and_set_bit(SK_F_CLOSING, &csk->flags))
1943 return 1;
1944 }
1945 return 0;
1946}
1947
1948static void cnic_close_bnx2_conn(struct cnic_sock *csk, u32 opcode)
1949{
1950 struct cnic_dev *dev = csk->dev;
1951 struct cnic_local *cp = dev->cnic_priv;
1952
1953 clear_bit(SK_F_CONNECT_START, &csk->flags);
1954 if (cnic_ready_to_close(csk, opcode)) {
1955 cnic_close_conn(csk);
1956 cnic_cm_upcall(cp, csk, opcode);
1957 }
1958}
1959
1960static void cnic_cm_stop_bnx2_hw(struct cnic_dev *dev)
1961{
1962}
1963
1964static int cnic_cm_init_bnx2_hw(struct cnic_dev *dev)
1965{
1966 u32 seed;
1967
1968 get_random_bytes(&seed, 4);
1969 cnic_ctx_wr(dev, 45, 0, seed);
1970 return 0;
1971}
1972
1973static int cnic_cm_open(struct cnic_dev *dev)
1974{
1975 struct cnic_local *cp = dev->cnic_priv;
1976 int err;
1977
1978 err = cnic_cm_alloc_mem(dev);
1979 if (err)
1980 return err;
1981
1982 err = cp->start_cm(dev);
1983
1984 if (err)
1985 goto err_out;
1986
1987 dev->cm_create = cnic_cm_create;
1988 dev->cm_destroy = cnic_cm_destroy;
1989 dev->cm_connect = cnic_cm_connect;
1990 dev->cm_abort = cnic_cm_abort;
1991 dev->cm_close = cnic_cm_close;
1992 dev->cm_select_dev = cnic_cm_select_dev;
1993
1994 cp->ulp_handle[CNIC_ULP_L4] = dev;
1995 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], &cm_ulp_ops);
1996 return 0;
1997
1998err_out:
1999 cnic_cm_free_mem(dev);
2000 return err;
2001}
2002
2003static int cnic_cm_shutdown(struct cnic_dev *dev)
2004{
2005 struct cnic_local *cp = dev->cnic_priv;
2006 int i;
2007
2008 cp->stop_cm(dev);
2009
2010 if (!cp->csk_tbl)
2011 return 0;
2012
2013 for (i = 0; i < MAX_CM_SK_TBL_SZ; i++) {
2014 struct cnic_sock *csk = &cp->csk_tbl[i];
2015
2016 clear_bit(SK_F_INUSE, &csk->flags);
2017 cnic_cm_cleanup(csk);
2018 }
2019 cnic_cm_free_mem(dev);
2020
2021 return 0;
2022}
2023
2024static void cnic_init_context(struct cnic_dev *dev, u32 cid)
2025{
2026 struct cnic_local *cp = dev->cnic_priv;
2027 u32 cid_addr;
2028 int i;
2029
2030 if (CHIP_NUM(cp) == CHIP_NUM_5709)
2031 return;
2032
2033 cid_addr = GET_CID_ADDR(cid);
2034
2035 for (i = 0; i < CTX_SIZE; i += 4)
2036 cnic_ctx_wr(dev, cid_addr, i, 0);
2037}
2038
2039static int cnic_setup_5709_context(struct cnic_dev *dev, int valid)
2040{
2041 struct cnic_local *cp = dev->cnic_priv;
2042 int ret = 0, i;
2043 u32 valid_bit = valid ? BNX2_CTX_HOST_PAGE_TBL_DATA0_VALID : 0;
2044
2045 if (CHIP_NUM(cp) != CHIP_NUM_5709)
2046 return 0;
2047
2048 for (i = 0; i < cp->ctx_blks; i++) {
2049 int j;
2050 u32 idx = cp->ctx_arr[i].cid / cp->cids_per_blk;
2051 u32 val;
2052
2053 memset(cp->ctx_arr[i].ctx, 0, BCM_PAGE_SIZE);
2054
2055 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA0,
2056 (cp->ctx_arr[i].mapping & 0xffffffff) | valid_bit);
2057 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA1,
2058 (u64) cp->ctx_arr[i].mapping >> 32);
2059 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL, idx |
2060 BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
2061 for (j = 0; j < 10; j++) {
2062
2063 val = CNIC_RD(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL);
2064 if (!(val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ))
2065 break;
2066 udelay(5);
2067 }
2068 if (val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) {
2069 ret = -EBUSY;
2070 break;
2071 }
2072 }
2073 return ret;
2074}
2075
2076static void cnic_free_irq(struct cnic_dev *dev)
2077{
2078 struct cnic_local *cp = dev->cnic_priv;
2079 struct cnic_eth_dev *ethdev = cp->ethdev;
2080
2081 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
2082 cp->disable_int_sync(dev);
2083 tasklet_disable(&cp->cnic_irq_task);
2084 free_irq(ethdev->irq_arr[0].vector, dev);
2085 }
2086}
2087
2088static int cnic_init_bnx2_irq(struct cnic_dev *dev)
2089{
2090 struct cnic_local *cp = dev->cnic_priv;
2091 struct cnic_eth_dev *ethdev = cp->ethdev;
2092
2093 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
2094 int err, i = 0;
2095 int sblk_num = cp->status_blk_num;
2096 u32 base = ((sblk_num - 1) * BNX2_HC_SB_CONFIG_SIZE) +
2097 BNX2_HC_SB_CONFIG_1;
2098
2099 CNIC_WR(dev, base, BNX2_HC_SB_CONFIG_1_ONE_SHOT);
2100
2101 CNIC_WR(dev, base + BNX2_HC_COMP_PROD_TRIP_OFF, (2 << 16) | 8);
2102 CNIC_WR(dev, base + BNX2_HC_COM_TICKS_OFF, (64 << 16) | 220);
2103 CNIC_WR(dev, base + BNX2_HC_CMD_TICKS_OFF, (64 << 16) | 220);
2104
2105 cp->bnx2_status_blk = cp->status_blk;
2106 cp->last_status_idx = cp->bnx2_status_blk->status_idx;
2107 tasklet_init(&cp->cnic_irq_task, &cnic_service_bnx2_msix,
2108 (unsigned long) dev);
2109 err = request_irq(ethdev->irq_arr[0].vector, cnic_irq, 0,
2110 "cnic", dev);
2111 if (err) {
2112 tasklet_disable(&cp->cnic_irq_task);
2113 return err;
2114 }
2115 while (cp->bnx2_status_blk->status_completion_producer_index &&
2116 i < 10) {
2117 CNIC_WR(dev, BNX2_HC_COALESCE_NOW,
2118 1 << (11 + sblk_num));
2119 udelay(10);
2120 i++;
2121 barrier();
2122 }
2123 if (cp->bnx2_status_blk->status_completion_producer_index) {
2124 cnic_free_irq(dev);
2125 goto failed;
2126 }
2127
2128 } else {
2129 struct status_block *sblk = cp->status_blk;
2130 u32 hc_cmd = CNIC_RD(dev, BNX2_HC_COMMAND);
2131 int i = 0;
2132
2133 while (sblk->status_completion_producer_index && i < 10) {
2134 CNIC_WR(dev, BNX2_HC_COMMAND,
2135 hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
2136 udelay(10);
2137 i++;
2138 barrier();
2139 }
2140 if (sblk->status_completion_producer_index)
2141 goto failed;
2142
2143 }
2144 return 0;
2145
2146failed:
2147 printk(KERN_ERR PFX "%s: " "KCQ index not resetting to 0.\n",
2148 dev->netdev->name);
2149 return -EBUSY;
2150}
2151
2152static void cnic_enable_bnx2_int(struct cnic_dev *dev)
2153{
2154 struct cnic_local *cp = dev->cnic_priv;
2155 struct cnic_eth_dev *ethdev = cp->ethdev;
2156
2157 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
2158 return;
2159
2160 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
2161 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
2162}
2163
2164static void cnic_disable_bnx2_int_sync(struct cnic_dev *dev)
2165{
2166 struct cnic_local *cp = dev->cnic_priv;
2167 struct cnic_eth_dev *ethdev = cp->ethdev;
2168
2169 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
2170 return;
2171
2172 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
2173 BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
2174 CNIC_RD(dev, BNX2_PCICFG_INT_ACK_CMD);
2175 synchronize_irq(ethdev->irq_arr[0].vector);
2176}
2177
2178static void cnic_init_bnx2_tx_ring(struct cnic_dev *dev)
2179{
2180 struct cnic_local *cp = dev->cnic_priv;
2181 struct cnic_eth_dev *ethdev = cp->ethdev;
2182 u32 cid_addr, tx_cid, sb_id;
2183 u32 val, offset0, offset1, offset2, offset3;
2184 int i;
2185 struct tx_bd *txbd;
2186 dma_addr_t buf_map;
2187 struct status_block *s_blk = cp->status_blk;
2188
2189 sb_id = cp->status_blk_num;
2190 tx_cid = 20;
2191 cnic_init_context(dev, tx_cid);
2192 cnic_init_context(dev, tx_cid + 1);
2193 cp->tx_cons_ptr = &s_blk->status_tx_quick_consumer_index2;
2194 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
2195 struct status_block_msix *sblk = cp->status_blk;
2196
2197 tx_cid = TX_TSS_CID + sb_id - 1;
2198 cnic_init_context(dev, tx_cid);
2199 CNIC_WR(dev, BNX2_TSCH_TSS_CFG, (sb_id << 24) |
2200 (TX_TSS_CID << 7));
2201 cp->tx_cons_ptr = &sblk->status_tx_quick_consumer_index;
2202 }
2203 cp->tx_cons = *cp->tx_cons_ptr;
2204
2205 cid_addr = GET_CID_ADDR(tx_cid);
2206 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
2207 u32 cid_addr2 = GET_CID_ADDR(tx_cid + 4) + 0x40;
2208
2209 for (i = 0; i < PHY_CTX_SIZE; i += 4)
2210 cnic_ctx_wr(dev, cid_addr2, i, 0);
2211
2212 offset0 = BNX2_L2CTX_TYPE_XI;
2213 offset1 = BNX2_L2CTX_CMD_TYPE_XI;
2214 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI_XI;
2215 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO_XI;
2216 } else {
2217 offset0 = BNX2_L2CTX_TYPE;
2218 offset1 = BNX2_L2CTX_CMD_TYPE;
2219 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI;
2220 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO;
2221 }
2222 val = BNX2_L2CTX_TYPE_TYPE_L2 | BNX2_L2CTX_TYPE_SIZE_L2;
2223 cnic_ctx_wr(dev, cid_addr, offset0, val);
2224
2225 val = BNX2_L2CTX_CMD_TYPE_TYPE_L2 | (8 << 16);
2226 cnic_ctx_wr(dev, cid_addr, offset1, val);
2227
2228 txbd = (struct tx_bd *) cp->l2_ring;
2229
2230 buf_map = cp->l2_buf_map;
2231 for (i = 0; i < MAX_TX_DESC_CNT; i++, txbd++) {
2232 txbd->tx_bd_haddr_hi = (u64) buf_map >> 32;
2233 txbd->tx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
2234 }
2235 val = (u64) cp->l2_ring_map >> 32;
2236 cnic_ctx_wr(dev, cid_addr, offset2, val);
2237 txbd->tx_bd_haddr_hi = val;
2238
2239 val = (u64) cp->l2_ring_map & 0xffffffff;
2240 cnic_ctx_wr(dev, cid_addr, offset3, val);
2241 txbd->tx_bd_haddr_lo = val;
2242}
2243
2244static void cnic_init_bnx2_rx_ring(struct cnic_dev *dev)
2245{
2246 struct cnic_local *cp = dev->cnic_priv;
2247 struct cnic_eth_dev *ethdev = cp->ethdev;
2248 u32 cid_addr, sb_id, val, coal_reg, coal_val;
2249 int i;
2250 struct rx_bd *rxbd;
2251 struct status_block *s_blk = cp->status_blk;
2252
2253 sb_id = cp->status_blk_num;
2254 cnic_init_context(dev, 2);
2255 cp->rx_cons_ptr = &s_blk->status_rx_quick_consumer_index2;
2256 coal_reg = BNX2_HC_COMMAND;
2257 coal_val = CNIC_RD(dev, coal_reg);
2258 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
2259 struct status_block_msix *sblk = cp->status_blk;
2260
2261 cp->rx_cons_ptr = &sblk->status_rx_quick_consumer_index;
2262 coal_reg = BNX2_HC_COALESCE_NOW;
2263 coal_val = 1 << (11 + sb_id);
2264 }
2265 i = 0;
2266 while (!(*cp->rx_cons_ptr != 0) && i < 10) {
2267 CNIC_WR(dev, coal_reg, coal_val);
2268 udelay(10);
2269 i++;
2270 barrier();
2271 }
2272 cp->rx_cons = *cp->rx_cons_ptr;
2273
2274 cid_addr = GET_CID_ADDR(2);
2275 val = BNX2_L2CTX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
2276 BNX2_L2CTX_CTX_TYPE_SIZE_L2 | (0x02 << 8);
2277 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_CTX_TYPE, val);
2278
2279 if (sb_id == 0)
2280 val = 2 << BNX2_L2CTX_STATUSB_NUM_SHIFT;
2281 else
2282 val = BNX2_L2CTX_STATUSB_NUM(sb_id);
2283 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_HOST_BDIDX, val);
2284
2285 rxbd = (struct rx_bd *) (cp->l2_ring + BCM_PAGE_SIZE);
2286 for (i = 0; i < MAX_RX_DESC_CNT; i++, rxbd++) {
2287 dma_addr_t buf_map;
2288 int n = (i % cp->l2_rx_ring_size) + 1;
2289
2290 buf_map = cp->l2_buf_map + (n * cp->l2_single_buf_size);
2291 rxbd->rx_bd_len = cp->l2_single_buf_size;
2292 rxbd->rx_bd_flags = RX_BD_FLAGS_START | RX_BD_FLAGS_END;
2293 rxbd->rx_bd_haddr_hi = (u64) buf_map >> 32;
2294 rxbd->rx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
2295 }
2296 val = (u64) (cp->l2_ring_map + BCM_PAGE_SIZE) >> 32;
2297 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_HI, val);
2298 rxbd->rx_bd_haddr_hi = val;
2299
2300 val = (u64) (cp->l2_ring_map + BCM_PAGE_SIZE) & 0xffffffff;
2301 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_LO, val);
2302 rxbd->rx_bd_haddr_lo = val;
2303
2304 val = cnic_reg_rd_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD);
2305 cnic_reg_wr_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD, val | (1 << 2));
2306}
2307
2308static void cnic_shutdown_bnx2_rx_ring(struct cnic_dev *dev)
2309{
2310 struct kwqe *wqes[1], l2kwqe;
2311
2312 memset(&l2kwqe, 0, sizeof(l2kwqe));
2313 wqes[0] = &l2kwqe;
2314 l2kwqe.kwqe_op_flag = (L2_LAYER_CODE << KWQE_FLAGS_LAYER_SHIFT) |
2315 (L2_KWQE_OPCODE_VALUE_FLUSH <<
2316 KWQE_OPCODE_SHIFT) | 2;
2317 dev->submit_kwqes(dev, wqes, 1);
2318}
2319
2320static void cnic_set_bnx2_mac(struct cnic_dev *dev)
2321{
2322 struct cnic_local *cp = dev->cnic_priv;
2323 u32 val;
2324
2325 val = cp->func << 2;
2326
2327 cp->shmem_base = cnic_reg_rd_ind(dev, BNX2_SHM_HDR_ADDR_0 + val);
2328
2329 val = cnic_reg_rd_ind(dev, cp->shmem_base +
2330 BNX2_PORT_HW_CFG_ISCSI_MAC_UPPER);
2331 dev->mac_addr[0] = (u8) (val >> 8);
2332 dev->mac_addr[1] = (u8) val;
2333
2334 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH4, val);
2335
2336 val = cnic_reg_rd_ind(dev, cp->shmem_base +
2337 BNX2_PORT_HW_CFG_ISCSI_MAC_LOWER);
2338 dev->mac_addr[2] = (u8) (val >> 24);
2339 dev->mac_addr[3] = (u8) (val >> 16);
2340 dev->mac_addr[4] = (u8) (val >> 8);
2341 dev->mac_addr[5] = (u8) val;
2342
2343 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH5, val);
2344
2345 val = 4 | BNX2_RPM_SORT_USER2_BC_EN;
2346 if (CHIP_NUM(cp) != CHIP_NUM_5709)
2347 val |= BNX2_RPM_SORT_USER2_PROM_VLAN;
2348
2349 CNIC_WR(dev, BNX2_RPM_SORT_USER2, 0x0);
2350 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val);
2351 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val | BNX2_RPM_SORT_USER2_ENA);
2352}
2353
2354static int cnic_start_bnx2_hw(struct cnic_dev *dev)
2355{
2356 struct cnic_local *cp = dev->cnic_priv;
2357 struct cnic_eth_dev *ethdev = cp->ethdev;
2358 struct status_block *sblk = cp->status_blk;
2359 u32 val;
2360 int err;
2361
2362 cnic_set_bnx2_mac(dev);
2363
2364 val = CNIC_RD(dev, BNX2_MQ_CONFIG);
2365 val &= ~BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE;
2366 if (BCM_PAGE_BITS > 12)
2367 val |= (12 - 8) << 4;
2368 else
2369 val |= (BCM_PAGE_BITS - 8) << 4;
2370
2371 CNIC_WR(dev, BNX2_MQ_CONFIG, val);
2372
2373 CNIC_WR(dev, BNX2_HC_COMP_PROD_TRIP, (2 << 16) | 8);
2374 CNIC_WR(dev, BNX2_HC_COM_TICKS, (64 << 16) | 220);
2375 CNIC_WR(dev, BNX2_HC_CMD_TICKS, (64 << 16) | 220);
2376
2377 err = cnic_setup_5709_context(dev, 1);
2378 if (err)
2379 return err;
2380
2381 cnic_init_context(dev, KWQ_CID);
2382 cnic_init_context(dev, KCQ_CID);
2383
2384 cp->kwq_cid_addr = GET_CID_ADDR(KWQ_CID);
2385 cp->kwq_io_addr = MB_GET_CID_ADDR(KWQ_CID) + L5_KRNLQ_HOST_QIDX;
2386
2387 cp->max_kwq_idx = MAX_KWQ_IDX;
2388 cp->kwq_prod_idx = 0;
2389 cp->kwq_con_idx = 0;
2390 cp->cnic_local_flags |= CNIC_LCL_FL_KWQ_INIT;
2391
2392 if (CHIP_NUM(cp) == CHIP_NUM_5706 || CHIP_NUM(cp) == CHIP_NUM_5708)
2393 cp->kwq_con_idx_ptr = &sblk->status_rx_quick_consumer_index15;
2394 else
2395 cp->kwq_con_idx_ptr = &sblk->status_cmd_consumer_index;
2396
2397 /* Initialize the kernel work queue context. */
2398 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
2399 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
2400 cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_TYPE, val);
2401
2402 val = (BCM_PAGE_SIZE / sizeof(struct kwqe) - 1) << 16;
2403 cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
2404
2405 val = ((BCM_PAGE_SIZE / sizeof(struct kwqe)) << 16) | KWQ_PAGE_CNT;
2406 cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
2407
2408 val = (u32) ((u64) cp->kwq_info.pgtbl_map >> 32);
2409 cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
2410
2411 val = (u32) cp->kwq_info.pgtbl_map;
2412 cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
2413
2414 cp->kcq_cid_addr = GET_CID_ADDR(KCQ_CID);
2415 cp->kcq_io_addr = MB_GET_CID_ADDR(KCQ_CID) + L5_KRNLQ_HOST_QIDX;
2416
2417 cp->kcq_prod_idx = 0;
2418
2419 /* Initialize the kernel complete queue context. */
2420 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
2421 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
2422 cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_TYPE, val);
2423
2424 val = (BCM_PAGE_SIZE / sizeof(struct kcqe) - 1) << 16;
2425 cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
2426
2427 val = ((BCM_PAGE_SIZE / sizeof(struct kcqe)) << 16) | KCQ_PAGE_CNT;
2428 cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
2429
2430 val = (u32) ((u64) cp->kcq_info.pgtbl_map >> 32);
2431 cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
2432
2433 val = (u32) cp->kcq_info.pgtbl_map;
2434 cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
2435
2436 cp->int_num = 0;
2437 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
2438 u32 sb_id = cp->status_blk_num;
2439 u32 sb = BNX2_L2CTX_STATUSB_NUM(sb_id);
2440
2441 cp->int_num = sb_id << BNX2_PCICFG_INT_ACK_CMD_INT_NUM_SHIFT;
2442 cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
2443 cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
2444 }
2445
2446 /* Enable Commnad Scheduler notification when we write to the
2447 * host producer index of the kernel contexts. */
2448 CNIC_WR(dev, BNX2_MQ_KNL_CMD_MASK1, 2);
2449
2450 /* Enable Command Scheduler notification when we write to either
2451 * the Send Queue or Receive Queue producer indexes of the kernel
2452 * bypass contexts. */
2453 CNIC_WR(dev, BNX2_MQ_KNL_BYP_CMD_MASK1, 7);
2454 CNIC_WR(dev, BNX2_MQ_KNL_BYP_WRITE_MASK1, 7);
2455
2456 /* Notify COM when the driver post an application buffer. */
2457 CNIC_WR(dev, BNX2_MQ_KNL_RX_V2P_MASK2, 0x2000);
2458
2459 /* Set the CP and COM doorbells. These two processors polls the
2460 * doorbell for a non zero value before running. This must be done
2461 * after setting up the kernel queue contexts. */
2462 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 1);
2463 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 1);
2464
2465 cnic_init_bnx2_tx_ring(dev);
2466 cnic_init_bnx2_rx_ring(dev);
2467
2468 err = cnic_init_bnx2_irq(dev);
2469 if (err) {
2470 printk(KERN_ERR PFX "%s: cnic_init_irq failed\n",
2471 dev->netdev->name);
2472 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
2473 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
2474 return err;
2475 }
2476
2477 return 0;
2478}
2479
Michael Chan86b53602009-10-10 13:46:57 +00002480static void cnic_init_rings(struct cnic_dev *dev)
2481{
2482 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
2483 cnic_init_bnx2_tx_ring(dev);
2484 cnic_init_bnx2_rx_ring(dev);
2485 }
2486}
2487
2488static void cnic_shutdown_rings(struct cnic_dev *dev)
2489{
2490 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
2491 cnic_shutdown_bnx2_rx_ring(dev);
2492 }
2493}
2494
Michael Chana3059b12009-08-14 15:49:44 +00002495static int cnic_register_netdev(struct cnic_dev *dev)
2496{
2497 struct cnic_local *cp = dev->cnic_priv;
2498 struct cnic_eth_dev *ethdev = cp->ethdev;
2499 int err;
2500
2501 if (!ethdev)
2502 return -ENODEV;
2503
2504 if (ethdev->drv_state & CNIC_DRV_STATE_REGD)
2505 return 0;
2506
2507 err = ethdev->drv_register_cnic(dev->netdev, cp->cnic_ops, dev);
2508 if (err)
2509 printk(KERN_ERR PFX "%s: register_cnic failed\n",
2510 dev->netdev->name);
2511
2512 return err;
2513}
2514
2515static void cnic_unregister_netdev(struct cnic_dev *dev)
2516{
2517 struct cnic_local *cp = dev->cnic_priv;
2518 struct cnic_eth_dev *ethdev = cp->ethdev;
2519
2520 if (!ethdev)
2521 return;
2522
2523 ethdev->drv_unregister_cnic(dev->netdev);
2524}
2525
Michael Chana4636962009-06-08 18:14:43 -07002526static int cnic_start_hw(struct cnic_dev *dev)
2527{
2528 struct cnic_local *cp = dev->cnic_priv;
2529 struct cnic_eth_dev *ethdev = cp->ethdev;
2530 int err;
2531
2532 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
2533 return -EALREADY;
2534
Michael Chana4636962009-06-08 18:14:43 -07002535 dev->regview = ethdev->io_base;
2536 cp->chip_id = ethdev->chip_id;
2537 pci_dev_get(dev->pcidev);
2538 cp->func = PCI_FUNC(dev->pcidev->devfn);
2539 cp->status_blk = ethdev->irq_arr[0].status_blk;
2540 cp->status_blk_num = ethdev->irq_arr[0].status_blk_num;
2541
2542 err = cp->alloc_resc(dev);
2543 if (err) {
2544 printk(KERN_ERR PFX "%s: allocate resource failure\n",
2545 dev->netdev->name);
2546 goto err1;
2547 }
2548
2549 err = cp->start_hw(dev);
2550 if (err)
2551 goto err1;
2552
2553 err = cnic_cm_open(dev);
2554 if (err)
2555 goto err1;
2556
2557 set_bit(CNIC_F_CNIC_UP, &dev->flags);
2558
2559 cp->enable_int(dev);
2560
2561 return 0;
2562
2563err1:
Michael Chana4636962009-06-08 18:14:43 -07002564 cp->free_resc(dev);
2565 pci_dev_put(dev->pcidev);
Michael Chana4636962009-06-08 18:14:43 -07002566 return err;
2567}
2568
2569static void cnic_stop_bnx2_hw(struct cnic_dev *dev)
2570{
Michael Chana4636962009-06-08 18:14:43 -07002571 cnic_disable_bnx2_int_sync(dev);
2572
2573 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
2574 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
2575
2576 cnic_init_context(dev, KWQ_CID);
2577 cnic_init_context(dev, KCQ_CID);
2578
2579 cnic_setup_5709_context(dev, 0);
2580 cnic_free_irq(dev);
2581
Michael Chana4636962009-06-08 18:14:43 -07002582 cnic_free_resc(dev);
2583}
2584
2585static void cnic_stop_hw(struct cnic_dev *dev)
2586{
2587 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
2588 struct cnic_local *cp = dev->cnic_priv;
2589
2590 clear_bit(CNIC_F_CNIC_UP, &dev->flags);
2591 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], NULL);
2592 synchronize_rcu();
2593 cnic_cm_shutdown(dev);
2594 cp->stop_hw(dev);
2595 pci_dev_put(dev->pcidev);
2596 }
2597}
2598
2599static void cnic_free_dev(struct cnic_dev *dev)
2600{
2601 int i = 0;
2602
2603 while ((atomic_read(&dev->ref_count) != 0) && i < 10) {
2604 msleep(100);
2605 i++;
2606 }
2607 if (atomic_read(&dev->ref_count) != 0)
2608 printk(KERN_ERR PFX "%s: Failed waiting for ref count to go"
2609 " to zero.\n", dev->netdev->name);
2610
2611 printk(KERN_INFO PFX "Removed CNIC device: %s\n", dev->netdev->name);
2612 dev_put(dev->netdev);
2613 kfree(dev);
2614}
2615
2616static struct cnic_dev *cnic_alloc_dev(struct net_device *dev,
2617 struct pci_dev *pdev)
2618{
2619 struct cnic_dev *cdev;
2620 struct cnic_local *cp;
2621 int alloc_size;
2622
2623 alloc_size = sizeof(struct cnic_dev) + sizeof(struct cnic_local);
2624
2625 cdev = kzalloc(alloc_size , GFP_KERNEL);
2626 if (cdev == NULL) {
2627 printk(KERN_ERR PFX "%s: allocate dev struct failure\n",
2628 dev->name);
2629 return NULL;
2630 }
2631
2632 cdev->netdev = dev;
2633 cdev->cnic_priv = (char *)cdev + sizeof(struct cnic_dev);
2634 cdev->register_device = cnic_register_device;
2635 cdev->unregister_device = cnic_unregister_device;
2636 cdev->iscsi_nl_msg_recv = cnic_iscsi_nl_msg_recv;
2637
2638 cp = cdev->cnic_priv;
2639 cp->dev = cdev;
2640 cp->uio_dev = -1;
2641 cp->l2_single_buf_size = 0x400;
2642 cp->l2_rx_ring_size = 3;
2643
2644 spin_lock_init(&cp->cnic_ulp_lock);
2645
2646 printk(KERN_INFO PFX "Added CNIC device: %s\n", dev->name);
2647
2648 return cdev;
2649}
2650
2651static struct cnic_dev *init_bnx2_cnic(struct net_device *dev)
2652{
2653 struct pci_dev *pdev;
2654 struct cnic_dev *cdev;
2655 struct cnic_local *cp;
2656 struct cnic_eth_dev *ethdev = NULL;
Michael Chane2ee3612009-06-13 17:43:02 -07002657 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
Michael Chana4636962009-06-08 18:14:43 -07002658
Michael Chane2ee3612009-06-13 17:43:02 -07002659 probe = symbol_get(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07002660 if (probe) {
2661 ethdev = (*probe)(dev);
Michael Chan64c64602009-08-14 15:49:43 +00002662 symbol_put(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07002663 }
2664 if (!ethdev)
2665 return NULL;
2666
2667 pdev = ethdev->pdev;
2668 if (!pdev)
2669 return NULL;
2670
2671 dev_hold(dev);
2672 pci_dev_get(pdev);
2673 if (pdev->device == PCI_DEVICE_ID_NX2_5709 ||
2674 pdev->device == PCI_DEVICE_ID_NX2_5709S) {
2675 u8 rev;
2676
2677 pci_read_config_byte(pdev, PCI_REVISION_ID, &rev);
2678 if (rev < 0x10) {
2679 pci_dev_put(pdev);
2680 goto cnic_err;
2681 }
2682 }
2683 pci_dev_put(pdev);
2684
2685 cdev = cnic_alloc_dev(dev, pdev);
2686 if (cdev == NULL)
2687 goto cnic_err;
2688
2689 set_bit(CNIC_F_BNX2_CLASS, &cdev->flags);
2690 cdev->submit_kwqes = cnic_submit_bnx2_kwqes;
2691
2692 cp = cdev->cnic_priv;
2693 cp->ethdev = ethdev;
2694 cdev->pcidev = pdev;
2695
2696 cp->cnic_ops = &cnic_bnx2_ops;
2697 cp->start_hw = cnic_start_bnx2_hw;
2698 cp->stop_hw = cnic_stop_bnx2_hw;
2699 cp->setup_pgtbl = cnic_setup_page_tbl;
2700 cp->alloc_resc = cnic_alloc_bnx2_resc;
2701 cp->free_resc = cnic_free_resc;
2702 cp->start_cm = cnic_cm_init_bnx2_hw;
2703 cp->stop_cm = cnic_cm_stop_bnx2_hw;
2704 cp->enable_int = cnic_enable_bnx2_int;
2705 cp->disable_int_sync = cnic_disable_bnx2_int_sync;
2706 cp->close_conn = cnic_close_bnx2_conn;
2707 cp->next_idx = cnic_bnx2_next_idx;
2708 cp->hw_idx = cnic_bnx2_hw_idx;
2709 return cdev;
2710
2711cnic_err:
2712 dev_put(dev);
2713 return NULL;
2714}
2715
2716static struct cnic_dev *is_cnic_dev(struct net_device *dev)
2717{
2718 struct ethtool_drvinfo drvinfo;
2719 struct cnic_dev *cdev = NULL;
2720
2721 if (dev->ethtool_ops && dev->ethtool_ops->get_drvinfo) {
2722 memset(&drvinfo, 0, sizeof(drvinfo));
2723 dev->ethtool_ops->get_drvinfo(dev, &drvinfo);
2724
2725 if (!strcmp(drvinfo.driver, "bnx2"))
2726 cdev = init_bnx2_cnic(dev);
2727 if (cdev) {
2728 write_lock(&cnic_dev_lock);
2729 list_add(&cdev->list, &cnic_dev_list);
2730 write_unlock(&cnic_dev_lock);
2731 }
2732 }
2733 return cdev;
2734}
2735
2736/**
2737 * netdev event handler
2738 */
2739static int cnic_netdev_event(struct notifier_block *this, unsigned long event,
2740 void *ptr)
2741{
2742 struct net_device *netdev = ptr;
2743 struct cnic_dev *dev;
2744 int if_type;
2745 int new_dev = 0;
2746
2747 dev = cnic_from_netdev(netdev);
2748
2749 if (!dev && (event == NETDEV_REGISTER || event == NETDEV_UP)) {
2750 /* Check for the hot-plug device */
2751 dev = is_cnic_dev(netdev);
2752 if (dev) {
2753 new_dev = 1;
2754 cnic_hold(dev);
2755 }
2756 }
2757 if (dev) {
2758 struct cnic_local *cp = dev->cnic_priv;
2759
2760 if (new_dev)
2761 cnic_ulp_init(dev);
2762 else if (event == NETDEV_UNREGISTER)
2763 cnic_ulp_exit(dev);
Michael Chan6053bbf2009-10-02 11:03:28 -07002764
2765 if (event == NETDEV_UP) {
Michael Chana3059b12009-08-14 15:49:44 +00002766 if (cnic_register_netdev(dev) != 0) {
2767 cnic_put(dev);
2768 goto done;
2769 }
Michael Chana4636962009-06-08 18:14:43 -07002770 if (!cnic_start_hw(dev))
2771 cnic_ulp_start(dev);
Michael Chana4636962009-06-08 18:14:43 -07002772 }
2773
2774 rcu_read_lock();
2775 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
2776 struct cnic_ulp_ops *ulp_ops;
2777 void *ctx;
2778
2779 ulp_ops = rcu_dereference(cp->ulp_ops[if_type]);
2780 if (!ulp_ops || !ulp_ops->indicate_netevent)
2781 continue;
2782
2783 ctx = cp->ulp_handle[if_type];
2784
2785 ulp_ops->indicate_netevent(ctx, event);
2786 }
2787 rcu_read_unlock();
2788
2789 if (event == NETDEV_GOING_DOWN) {
Michael Chana4636962009-06-08 18:14:43 -07002790 cnic_ulp_stop(dev);
2791 cnic_stop_hw(dev);
Michael Chana3059b12009-08-14 15:49:44 +00002792 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07002793 } else if (event == NETDEV_UNREGISTER) {
2794 write_lock(&cnic_dev_lock);
2795 list_del_init(&dev->list);
2796 write_unlock(&cnic_dev_lock);
2797
2798 cnic_put(dev);
2799 cnic_free_dev(dev);
2800 goto done;
2801 }
2802 cnic_put(dev);
2803 }
2804done:
2805 return NOTIFY_DONE;
2806}
2807
2808static struct notifier_block cnic_netdev_notifier = {
2809 .notifier_call = cnic_netdev_event
2810};
2811
2812static void cnic_release(void)
2813{
2814 struct cnic_dev *dev;
2815
2816 while (!list_empty(&cnic_dev_list)) {
2817 dev = list_entry(cnic_dev_list.next, struct cnic_dev, list);
2818 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
2819 cnic_ulp_stop(dev);
2820 cnic_stop_hw(dev);
2821 }
2822
2823 cnic_ulp_exit(dev);
Michael Chana3059b12009-08-14 15:49:44 +00002824 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07002825 list_del_init(&dev->list);
2826 cnic_free_dev(dev);
2827 }
2828}
2829
2830static int __init cnic_init(void)
2831{
2832 int rc = 0;
2833
2834 printk(KERN_INFO "%s", version);
2835
2836 rc = register_netdevice_notifier(&cnic_netdev_notifier);
2837 if (rc) {
2838 cnic_release();
2839 return rc;
2840 }
2841
2842 return 0;
2843}
2844
2845static void __exit cnic_exit(void)
2846{
2847 unregister_netdevice_notifier(&cnic_netdev_notifier);
2848 cnic_release();
2849 return;
2850}
2851
2852module_init(cnic_init);
2853module_exit(cnic_exit);