DaVinci: EDMA: Add queue 2 and 3 for DM365 and DM6467
[linux-ginger.git] / drivers / net / cnic.c
blob74c342959b7bdf98072311fd371b5e78d302b7e0
1 /* cnic.c: Broadcom CNIC core network driver.
3 * Copyright (c) 2006-2009 Broadcom Corporation
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.
9 * Original skeleton written by: John(Zongxi) Chen (zongxi@broadcom.com)
10 * Modified and maintained by: Michael Chan <mchan@broadcom.com>
13 #include <linux/module.h>
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>
38 #include "cnic_if.h"
39 #include "bnx2.h"
40 #include "cnic.h"
41 #include "cnic_defs.h"
43 #define DRV_MODULE_NAME "cnic"
44 #define PFX DRV_MODULE_NAME ": "
46 static char version[] __devinitdata =
47 "Broadcom NetXtreme II CNIC Driver " DRV_MODULE_NAME " v" CNIC_MODULE_VERSION " (" CNIC_MODULE_RELDATE ")\n";
49 MODULE_AUTHOR("Michael Chan <mchan@broadcom.com> and John(Zongxi) "
50 "Chen (zongxi@broadcom.com");
51 MODULE_DESCRIPTION("Broadcom NetXtreme II CNIC Driver");
52 MODULE_LICENSE("GPL");
53 MODULE_VERSION(CNIC_MODULE_VERSION);
55 static LIST_HEAD(cnic_dev_list);
56 static DEFINE_RWLOCK(cnic_dev_lock);
57 static DEFINE_MUTEX(cnic_lock);
59 static struct cnic_ulp_ops *cnic_ulp_tbl[MAX_CNIC_ULP_TYPE];
61 static int cnic_service_bnx2(void *, void *);
62 static int cnic_ctl(void *, struct cnic_ctl_info *);
64 static struct cnic_ops cnic_bnx2_ops = {
65 .cnic_owner = THIS_MODULE,
66 .cnic_handler = cnic_service_bnx2,
67 .cnic_ctl = cnic_ctl,
70 static void cnic_shutdown_bnx2_rx_ring(struct cnic_dev *);
71 static void cnic_init_bnx2_tx_ring(struct cnic_dev *);
72 static void cnic_init_bnx2_rx_ring(struct cnic_dev *);
73 static int cnic_cm_set_pg(struct cnic_sock *);
75 static int cnic_uio_open(struct uio_info *uinfo, struct inode *inode)
77 struct cnic_dev *dev = uinfo->priv;
78 struct cnic_local *cp = dev->cnic_priv;
80 if (!capable(CAP_NET_ADMIN))
81 return -EPERM;
83 if (cp->uio_dev != -1)
84 return -EBUSY;
86 cp->uio_dev = iminor(inode);
88 cnic_shutdown_bnx2_rx_ring(dev);
90 cnic_init_bnx2_tx_ring(dev);
91 cnic_init_bnx2_rx_ring(dev);
93 return 0;
96 static int cnic_uio_close(struct uio_info *uinfo, struct inode *inode)
98 struct cnic_dev *dev = uinfo->priv;
99 struct cnic_local *cp = dev->cnic_priv;
101 cp->uio_dev = -1;
102 return 0;
105 static inline void cnic_hold(struct cnic_dev *dev)
107 atomic_inc(&dev->ref_count);
110 static inline void cnic_put(struct cnic_dev *dev)
112 atomic_dec(&dev->ref_count);
115 static inline void csk_hold(struct cnic_sock *csk)
117 atomic_inc(&csk->ref_count);
120 static inline void csk_put(struct cnic_sock *csk)
122 atomic_dec(&csk->ref_count);
125 static struct cnic_dev *cnic_from_netdev(struct net_device *netdev)
127 struct cnic_dev *cdev;
129 read_lock(&cnic_dev_lock);
130 list_for_each_entry(cdev, &cnic_dev_list, list) {
131 if (netdev == cdev->netdev) {
132 cnic_hold(cdev);
133 read_unlock(&cnic_dev_lock);
134 return cdev;
137 read_unlock(&cnic_dev_lock);
138 return NULL;
141 static inline void ulp_get(struct cnic_ulp_ops *ulp_ops)
143 atomic_inc(&ulp_ops->ref_count);
146 static inline void ulp_put(struct cnic_ulp_ops *ulp_ops)
148 atomic_dec(&ulp_ops->ref_count);
151 static void cnic_ctx_wr(struct cnic_dev *dev, u32 cid_addr, u32 off, u32 val)
153 struct cnic_local *cp = dev->cnic_priv;
154 struct cnic_eth_dev *ethdev = cp->ethdev;
155 struct drv_ctl_info info;
156 struct drv_ctl_io *io = &info.data.io;
158 info.cmd = DRV_CTL_CTX_WR_CMD;
159 io->cid_addr = cid_addr;
160 io->offset = off;
161 io->data = val;
162 ethdev->drv_ctl(dev->netdev, &info);
165 static void cnic_reg_wr_ind(struct cnic_dev *dev, u32 off, u32 val)
167 struct cnic_local *cp = dev->cnic_priv;
168 struct cnic_eth_dev *ethdev = cp->ethdev;
169 struct drv_ctl_info info;
170 struct drv_ctl_io *io = &info.data.io;
172 info.cmd = DRV_CTL_IO_WR_CMD;
173 io->offset = off;
174 io->data = val;
175 ethdev->drv_ctl(dev->netdev, &info);
178 static u32 cnic_reg_rd_ind(struct cnic_dev *dev, u32 off)
180 struct cnic_local *cp = dev->cnic_priv;
181 struct cnic_eth_dev *ethdev = cp->ethdev;
182 struct drv_ctl_info info;
183 struct drv_ctl_io *io = &info.data.io;
185 info.cmd = DRV_CTL_IO_RD_CMD;
186 io->offset = off;
187 ethdev->drv_ctl(dev->netdev, &info);
188 return io->data;
191 static int cnic_in_use(struct cnic_sock *csk)
193 return test_bit(SK_F_INUSE, &csk->flags);
196 static void cnic_kwq_completion(struct cnic_dev *dev, u32 count)
198 struct cnic_local *cp = dev->cnic_priv;
199 struct cnic_eth_dev *ethdev = cp->ethdev;
200 struct drv_ctl_info info;
202 info.cmd = DRV_CTL_COMPLETION_CMD;
203 info.data.comp.comp_count = count;
204 ethdev->drv_ctl(dev->netdev, &info);
207 static int cnic_send_nlmsg(struct cnic_local *cp, u32 type,
208 struct cnic_sock *csk)
210 struct iscsi_path path_req;
211 char *buf = NULL;
212 u16 len = 0;
213 u32 msg_type = ISCSI_KEVENT_IF_DOWN;
214 struct cnic_ulp_ops *ulp_ops;
216 if (cp->uio_dev == -1)
217 return -ENODEV;
219 if (csk) {
220 len = sizeof(path_req);
221 buf = (char *) &path_req;
222 memset(&path_req, 0, len);
224 msg_type = ISCSI_KEVENT_PATH_REQ;
225 path_req.handle = (u64) csk->l5_cid;
226 if (test_bit(SK_F_IPV6, &csk->flags)) {
227 memcpy(&path_req.dst.v6_addr, &csk->dst_ip[0],
228 sizeof(struct in6_addr));
229 path_req.ip_addr_len = 16;
230 } else {
231 memcpy(&path_req.dst.v4_addr, &csk->dst_ip[0],
232 sizeof(struct in_addr));
233 path_req.ip_addr_len = 4;
235 path_req.vlan_id = csk->vlan_id;
236 path_req.pmtu = csk->mtu;
239 rcu_read_lock();
240 ulp_ops = rcu_dereference(cnic_ulp_tbl[CNIC_ULP_ISCSI]);
241 if (ulp_ops)
242 ulp_ops->iscsi_nl_send_msg(cp->dev, msg_type, buf, len);
243 rcu_read_unlock();
244 return 0;
247 static int cnic_iscsi_nl_msg_recv(struct cnic_dev *dev, u32 msg_type,
248 char *buf, u16 len)
250 int rc = -EINVAL;
252 switch (msg_type) {
253 case ISCSI_UEVENT_PATH_UPDATE: {
254 struct cnic_local *cp;
255 u32 l5_cid;
256 struct cnic_sock *csk;
257 struct iscsi_path *path_resp;
259 if (len < sizeof(*path_resp))
260 break;
262 path_resp = (struct iscsi_path *) buf;
263 cp = dev->cnic_priv;
264 l5_cid = (u32) path_resp->handle;
265 if (l5_cid >= MAX_CM_SK_TBL_SZ)
266 break;
268 csk = &cp->csk_tbl[l5_cid];
269 csk_hold(csk);
270 if (cnic_in_use(csk)) {
271 memcpy(csk->ha, path_resp->mac_addr, 6);
272 if (test_bit(SK_F_IPV6, &csk->flags))
273 memcpy(&csk->src_ip[0], &path_resp->src.v6_addr,
274 sizeof(struct in6_addr));
275 else
276 memcpy(&csk->src_ip[0], &path_resp->src.v4_addr,
277 sizeof(struct in_addr));
278 if (is_valid_ether_addr(csk->ha))
279 cnic_cm_set_pg(csk);
281 csk_put(csk);
282 rc = 0;
286 return rc;
289 static int cnic_offld_prep(struct cnic_sock *csk)
291 if (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
292 return 0;
294 if (!test_bit(SK_F_CONNECT_START, &csk->flags)) {
295 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
296 return 0;
299 return 1;
302 static int cnic_close_prep(struct cnic_sock *csk)
304 clear_bit(SK_F_CONNECT_START, &csk->flags);
305 smp_mb__after_clear_bit();
307 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
308 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
309 msleep(1);
311 return 1;
313 return 0;
316 static int cnic_abort_prep(struct cnic_sock *csk)
318 clear_bit(SK_F_CONNECT_START, &csk->flags);
319 smp_mb__after_clear_bit();
321 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
322 msleep(1);
324 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
325 csk->state = L4_KCQE_OPCODE_VALUE_RESET_COMP;
326 return 1;
329 return 0;
332 static void cnic_uio_stop(void)
334 struct cnic_dev *dev;
336 read_lock(&cnic_dev_lock);
337 list_for_each_entry(dev, &cnic_dev_list, list) {
338 struct cnic_local *cp = dev->cnic_priv;
340 if (cp->cnic_uinfo)
341 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
343 read_unlock(&cnic_dev_lock);
346 int cnic_register_driver(int ulp_type, struct cnic_ulp_ops *ulp_ops)
348 struct cnic_dev *dev;
350 if (ulp_type >= MAX_CNIC_ULP_TYPE) {
351 printk(KERN_ERR PFX "cnic_register_driver: Bad type %d\n",
352 ulp_type);
353 return -EINVAL;
355 mutex_lock(&cnic_lock);
356 if (cnic_ulp_tbl[ulp_type]) {
357 printk(KERN_ERR PFX "cnic_register_driver: Type %d has already "
358 "been registered\n", ulp_type);
359 mutex_unlock(&cnic_lock);
360 return -EBUSY;
363 read_lock(&cnic_dev_lock);
364 list_for_each_entry(dev, &cnic_dev_list, list) {
365 struct cnic_local *cp = dev->cnic_priv;
367 clear_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]);
369 read_unlock(&cnic_dev_lock);
371 atomic_set(&ulp_ops->ref_count, 0);
372 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], ulp_ops);
373 mutex_unlock(&cnic_lock);
375 /* Prevent race conditions with netdev_event */
376 rtnl_lock();
377 read_lock(&cnic_dev_lock);
378 list_for_each_entry(dev, &cnic_dev_list, list) {
379 struct cnic_local *cp = dev->cnic_priv;
381 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]))
382 ulp_ops->cnic_init(dev);
384 read_unlock(&cnic_dev_lock);
385 rtnl_unlock();
387 return 0;
390 int cnic_unregister_driver(int ulp_type)
392 struct cnic_dev *dev;
393 struct cnic_ulp_ops *ulp_ops;
394 int i = 0;
396 if (ulp_type >= MAX_CNIC_ULP_TYPE) {
397 printk(KERN_ERR PFX "cnic_unregister_driver: Bad type %d\n",
398 ulp_type);
399 return -EINVAL;
401 mutex_lock(&cnic_lock);
402 ulp_ops = cnic_ulp_tbl[ulp_type];
403 if (!ulp_ops) {
404 printk(KERN_ERR PFX "cnic_unregister_driver: Type %d has not "
405 "been registered\n", ulp_type);
406 goto out_unlock;
408 read_lock(&cnic_dev_lock);
409 list_for_each_entry(dev, &cnic_dev_list, list) {
410 struct cnic_local *cp = dev->cnic_priv;
412 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
413 printk(KERN_ERR PFX "cnic_unregister_driver: Type %d "
414 "still has devices registered\n", ulp_type);
415 read_unlock(&cnic_dev_lock);
416 goto out_unlock;
419 read_unlock(&cnic_dev_lock);
421 if (ulp_type == CNIC_ULP_ISCSI)
422 cnic_uio_stop();
424 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], NULL);
426 mutex_unlock(&cnic_lock);
427 synchronize_rcu();
428 while ((atomic_read(&ulp_ops->ref_count) != 0) && (i < 20)) {
429 msleep(100);
430 i++;
433 if (atomic_read(&ulp_ops->ref_count) != 0)
434 printk(KERN_WARNING PFX "%s: Failed waiting for ref count to go"
435 " to zero.\n", dev->netdev->name);
436 return 0;
438 out_unlock:
439 mutex_unlock(&cnic_lock);
440 return -EINVAL;
443 static int cnic_start_hw(struct cnic_dev *);
444 static void cnic_stop_hw(struct cnic_dev *);
446 static int cnic_register_device(struct cnic_dev *dev, int ulp_type,
447 void *ulp_ctx)
449 struct cnic_local *cp = dev->cnic_priv;
450 struct cnic_ulp_ops *ulp_ops;
452 if (ulp_type >= MAX_CNIC_ULP_TYPE) {
453 printk(KERN_ERR PFX "cnic_register_device: Bad type %d\n",
454 ulp_type);
455 return -EINVAL;
457 mutex_lock(&cnic_lock);
458 if (cnic_ulp_tbl[ulp_type] == NULL) {
459 printk(KERN_ERR PFX "cnic_register_device: Driver with type %d "
460 "has not been registered\n", ulp_type);
461 mutex_unlock(&cnic_lock);
462 return -EAGAIN;
464 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
465 printk(KERN_ERR PFX "cnic_register_device: Type %d has already "
466 "been registered to this device\n", ulp_type);
467 mutex_unlock(&cnic_lock);
468 return -EBUSY;
471 clear_bit(ULP_F_START, &cp->ulp_flags[ulp_type]);
472 cp->ulp_handle[ulp_type] = ulp_ctx;
473 ulp_ops = cnic_ulp_tbl[ulp_type];
474 rcu_assign_pointer(cp->ulp_ops[ulp_type], ulp_ops);
475 cnic_hold(dev);
477 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
478 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[ulp_type]))
479 ulp_ops->cnic_start(cp->ulp_handle[ulp_type]);
481 mutex_unlock(&cnic_lock);
483 return 0;
486 EXPORT_SYMBOL(cnic_register_driver);
488 static int cnic_unregister_device(struct cnic_dev *dev, int ulp_type)
490 struct cnic_local *cp = dev->cnic_priv;
491 int i = 0;
493 if (ulp_type >= MAX_CNIC_ULP_TYPE) {
494 printk(KERN_ERR PFX "cnic_unregister_device: Bad type %d\n",
495 ulp_type);
496 return -EINVAL;
498 mutex_lock(&cnic_lock);
499 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
500 rcu_assign_pointer(cp->ulp_ops[ulp_type], NULL);
501 cnic_put(dev);
502 } else {
503 printk(KERN_ERR PFX "cnic_unregister_device: device not "
504 "registered to this ulp type %d\n", ulp_type);
505 mutex_unlock(&cnic_lock);
506 return -EINVAL;
508 mutex_unlock(&cnic_lock);
510 synchronize_rcu();
512 while (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]) &&
513 i < 20) {
514 msleep(100);
515 i++;
517 if (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]))
518 printk(KERN_WARNING PFX "%s: Failed waiting for ULP up call"
519 " to complete.\n", dev->netdev->name);
521 return 0;
523 EXPORT_SYMBOL(cnic_unregister_driver);
525 static int cnic_init_id_tbl(struct cnic_id_tbl *id_tbl, u32 size, u32 start_id)
527 id_tbl->start = start_id;
528 id_tbl->max = size;
529 id_tbl->next = 0;
530 spin_lock_init(&id_tbl->lock);
531 id_tbl->table = kzalloc(DIV_ROUND_UP(size, 32) * 4, GFP_KERNEL);
532 if (!id_tbl->table)
533 return -ENOMEM;
535 return 0;
538 static void cnic_free_id_tbl(struct cnic_id_tbl *id_tbl)
540 kfree(id_tbl->table);
541 id_tbl->table = NULL;
544 static int cnic_alloc_id(struct cnic_id_tbl *id_tbl, u32 id)
546 int ret = -1;
548 id -= id_tbl->start;
549 if (id >= id_tbl->max)
550 return ret;
552 spin_lock(&id_tbl->lock);
553 if (!test_bit(id, id_tbl->table)) {
554 set_bit(id, id_tbl->table);
555 ret = 0;
557 spin_unlock(&id_tbl->lock);
558 return ret;
561 /* Returns -1 if not successful */
562 static u32 cnic_alloc_new_id(struct cnic_id_tbl *id_tbl)
564 u32 id;
566 spin_lock(&id_tbl->lock);
567 id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
568 if (id >= id_tbl->max) {
569 id = -1;
570 if (id_tbl->next != 0) {
571 id = find_first_zero_bit(id_tbl->table, id_tbl->next);
572 if (id >= id_tbl->next)
573 id = -1;
577 if (id < id_tbl->max) {
578 set_bit(id, id_tbl->table);
579 id_tbl->next = (id + 1) & (id_tbl->max - 1);
580 id += id_tbl->start;
583 spin_unlock(&id_tbl->lock);
585 return id;
588 static void cnic_free_id(struct cnic_id_tbl *id_tbl, u32 id)
590 if (id == -1)
591 return;
593 id -= id_tbl->start;
594 if (id >= id_tbl->max)
595 return;
597 clear_bit(id, id_tbl->table);
600 static void cnic_free_dma(struct cnic_dev *dev, struct cnic_dma *dma)
602 int i;
604 if (!dma->pg_arr)
605 return;
607 for (i = 0; i < dma->num_pages; i++) {
608 if (dma->pg_arr[i]) {
609 pci_free_consistent(dev->pcidev, BCM_PAGE_SIZE,
610 dma->pg_arr[i], dma->pg_map_arr[i]);
611 dma->pg_arr[i] = NULL;
614 if (dma->pgtbl) {
615 pci_free_consistent(dev->pcidev, dma->pgtbl_size,
616 dma->pgtbl, dma->pgtbl_map);
617 dma->pgtbl = NULL;
619 kfree(dma->pg_arr);
620 dma->pg_arr = NULL;
621 dma->num_pages = 0;
624 static void cnic_setup_page_tbl(struct cnic_dev *dev, struct cnic_dma *dma)
626 int i;
627 u32 *page_table = dma->pgtbl;
629 for (i = 0; i < dma->num_pages; i++) {
630 /* Each entry needs to be in big endian format. */
631 *page_table = (u32) ((u64) dma->pg_map_arr[i] >> 32);
632 page_table++;
633 *page_table = (u32) dma->pg_map_arr[i];
634 page_table++;
638 static int cnic_alloc_dma(struct cnic_dev *dev, struct cnic_dma *dma,
639 int pages, int use_pg_tbl)
641 int i, size;
642 struct cnic_local *cp = dev->cnic_priv;
644 size = pages * (sizeof(void *) + sizeof(dma_addr_t));
645 dma->pg_arr = kzalloc(size, GFP_ATOMIC);
646 if (dma->pg_arr == NULL)
647 return -ENOMEM;
649 dma->pg_map_arr = (dma_addr_t *) (dma->pg_arr + pages);
650 dma->num_pages = pages;
652 for (i = 0; i < pages; i++) {
653 dma->pg_arr[i] = pci_alloc_consistent(dev->pcidev,
654 BCM_PAGE_SIZE,
655 &dma->pg_map_arr[i]);
656 if (dma->pg_arr[i] == NULL)
657 goto error;
659 if (!use_pg_tbl)
660 return 0;
662 dma->pgtbl_size = ((pages * 8) + BCM_PAGE_SIZE - 1) &
663 ~(BCM_PAGE_SIZE - 1);
664 dma->pgtbl = pci_alloc_consistent(dev->pcidev, dma->pgtbl_size,
665 &dma->pgtbl_map);
666 if (dma->pgtbl == NULL)
667 goto error;
669 cp->setup_pgtbl(dev, dma);
671 return 0;
673 error:
674 cnic_free_dma(dev, dma);
675 return -ENOMEM;
678 static void cnic_free_resc(struct cnic_dev *dev)
680 struct cnic_local *cp = dev->cnic_priv;
681 int i = 0;
683 if (cp->cnic_uinfo) {
684 while (cp->uio_dev != -1 && i < 15) {
685 msleep(100);
686 i++;
688 uio_unregister_device(cp->cnic_uinfo);
689 kfree(cp->cnic_uinfo);
690 cp->cnic_uinfo = NULL;
693 if (cp->l2_buf) {
694 pci_free_consistent(dev->pcidev, cp->l2_buf_size,
695 cp->l2_buf, cp->l2_buf_map);
696 cp->l2_buf = NULL;
699 if (cp->l2_ring) {
700 pci_free_consistent(dev->pcidev, cp->l2_ring_size,
701 cp->l2_ring, cp->l2_ring_map);
702 cp->l2_ring = NULL;
705 for (i = 0; i < cp->ctx_blks; i++) {
706 if (cp->ctx_arr[i].ctx) {
707 pci_free_consistent(dev->pcidev, cp->ctx_blk_size,
708 cp->ctx_arr[i].ctx,
709 cp->ctx_arr[i].mapping);
710 cp->ctx_arr[i].ctx = NULL;
713 kfree(cp->ctx_arr);
714 cp->ctx_arr = NULL;
715 cp->ctx_blks = 0;
717 cnic_free_dma(dev, &cp->gbl_buf_info);
718 cnic_free_dma(dev, &cp->conn_buf_info);
719 cnic_free_dma(dev, &cp->kwq_info);
720 cnic_free_dma(dev, &cp->kcq_info);
721 kfree(cp->iscsi_tbl);
722 cp->iscsi_tbl = NULL;
723 kfree(cp->ctx_tbl);
724 cp->ctx_tbl = NULL;
726 cnic_free_id_tbl(&cp->cid_tbl);
729 static int cnic_alloc_context(struct cnic_dev *dev)
731 struct cnic_local *cp = dev->cnic_priv;
733 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
734 int i, k, arr_size;
736 cp->ctx_blk_size = BCM_PAGE_SIZE;
737 cp->cids_per_blk = BCM_PAGE_SIZE / 128;
738 arr_size = BNX2_MAX_CID / cp->cids_per_blk *
739 sizeof(struct cnic_ctx);
740 cp->ctx_arr = kzalloc(arr_size, GFP_KERNEL);
741 if (cp->ctx_arr == NULL)
742 return -ENOMEM;
744 k = 0;
745 for (i = 0; i < 2; i++) {
746 u32 j, reg, off, lo, hi;
748 if (i == 0)
749 off = BNX2_PG_CTX_MAP;
750 else
751 off = BNX2_ISCSI_CTX_MAP;
753 reg = cnic_reg_rd_ind(dev, off);
754 lo = reg >> 16;
755 hi = reg & 0xffff;
756 for (j = lo; j < hi; j += cp->cids_per_blk, k++)
757 cp->ctx_arr[k].cid = j;
760 cp->ctx_blks = k;
761 if (cp->ctx_blks >= (BNX2_MAX_CID / cp->cids_per_blk)) {
762 cp->ctx_blks = 0;
763 return -ENOMEM;
766 for (i = 0; i < cp->ctx_blks; i++) {
767 cp->ctx_arr[i].ctx =
768 pci_alloc_consistent(dev->pcidev, BCM_PAGE_SIZE,
769 &cp->ctx_arr[i].mapping);
770 if (cp->ctx_arr[i].ctx == NULL)
771 return -ENOMEM;
774 return 0;
777 static int cnic_alloc_bnx2_resc(struct cnic_dev *dev)
779 struct cnic_local *cp = dev->cnic_priv;
780 struct uio_info *uinfo;
781 int ret;
783 ret = cnic_alloc_dma(dev, &cp->kwq_info, KWQ_PAGE_CNT, 1);
784 if (ret)
785 goto error;
786 cp->kwq = (struct kwqe **) cp->kwq_info.pg_arr;
788 ret = cnic_alloc_dma(dev, &cp->kcq_info, KCQ_PAGE_CNT, 1);
789 if (ret)
790 goto error;
791 cp->kcq = (struct kcqe **) cp->kcq_info.pg_arr;
793 ret = cnic_alloc_context(dev);
794 if (ret)
795 goto error;
797 cp->l2_ring_size = 2 * BCM_PAGE_SIZE;
798 cp->l2_ring = pci_alloc_consistent(dev->pcidev, cp->l2_ring_size,
799 &cp->l2_ring_map);
800 if (!cp->l2_ring)
801 goto error;
803 cp->l2_buf_size = (cp->l2_rx_ring_size + 1) * cp->l2_single_buf_size;
804 cp->l2_buf_size = PAGE_ALIGN(cp->l2_buf_size);
805 cp->l2_buf = pci_alloc_consistent(dev->pcidev, cp->l2_buf_size,
806 &cp->l2_buf_map);
807 if (!cp->l2_buf)
808 goto error;
810 uinfo = kzalloc(sizeof(*uinfo), GFP_ATOMIC);
811 if (!uinfo)
812 goto error;
814 uinfo->mem[0].addr = dev->netdev->base_addr;
815 uinfo->mem[0].internal_addr = dev->regview;
816 uinfo->mem[0].size = dev->netdev->mem_end - dev->netdev->mem_start;
817 uinfo->mem[0].memtype = UIO_MEM_PHYS;
819 uinfo->mem[1].addr = (unsigned long) cp->status_blk & PAGE_MASK;
820 if (cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
821 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE * 9;
822 else
823 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE;
824 uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
826 uinfo->mem[2].addr = (unsigned long) cp->l2_ring;
827 uinfo->mem[2].size = cp->l2_ring_size;
828 uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
830 uinfo->mem[3].addr = (unsigned long) cp->l2_buf;
831 uinfo->mem[3].size = cp->l2_buf_size;
832 uinfo->mem[3].memtype = UIO_MEM_LOGICAL;
834 uinfo->name = "bnx2_cnic";
835 uinfo->version = CNIC_MODULE_VERSION;
836 uinfo->irq = UIO_IRQ_CUSTOM;
838 uinfo->open = cnic_uio_open;
839 uinfo->release = cnic_uio_close;
841 uinfo->priv = dev;
843 ret = uio_register_device(&dev->pcidev->dev, uinfo);
844 if (ret) {
845 kfree(uinfo);
846 goto error;
849 cp->cnic_uinfo = uinfo;
851 return 0;
853 error:
854 cnic_free_resc(dev);
855 return ret;
858 static inline u32 cnic_kwq_avail(struct cnic_local *cp)
860 return cp->max_kwq_idx -
861 ((cp->kwq_prod_idx - cp->kwq_con_idx) & cp->max_kwq_idx);
864 static int cnic_submit_bnx2_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
865 u32 num_wqes)
867 struct cnic_local *cp = dev->cnic_priv;
868 struct kwqe *prod_qe;
869 u16 prod, sw_prod, i;
871 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
872 return -EAGAIN; /* bnx2 is down */
874 spin_lock_bh(&cp->cnic_ulp_lock);
875 if (num_wqes > cnic_kwq_avail(cp) &&
876 !(cp->cnic_local_flags & CNIC_LCL_FL_KWQ_INIT)) {
877 spin_unlock_bh(&cp->cnic_ulp_lock);
878 return -EAGAIN;
881 cp->cnic_local_flags &= ~CNIC_LCL_FL_KWQ_INIT;
883 prod = cp->kwq_prod_idx;
884 sw_prod = prod & MAX_KWQ_IDX;
885 for (i = 0; i < num_wqes; i++) {
886 prod_qe = &cp->kwq[KWQ_PG(sw_prod)][KWQ_IDX(sw_prod)];
887 memcpy(prod_qe, wqes[i], sizeof(struct kwqe));
888 prod++;
889 sw_prod = prod & MAX_KWQ_IDX;
891 cp->kwq_prod_idx = prod;
893 CNIC_WR16(dev, cp->kwq_io_addr, cp->kwq_prod_idx);
895 spin_unlock_bh(&cp->cnic_ulp_lock);
896 return 0;
899 static void service_kcqes(struct cnic_dev *dev, int num_cqes)
901 struct cnic_local *cp = dev->cnic_priv;
902 int i, j;
904 i = 0;
905 j = 1;
906 while (num_cqes) {
907 struct cnic_ulp_ops *ulp_ops;
908 int ulp_type;
909 u32 kcqe_op_flag = cp->completed_kcq[i]->kcqe_op_flag;
910 u32 kcqe_layer = kcqe_op_flag & KCQE_FLAGS_LAYER_MASK;
912 if (unlikely(kcqe_op_flag & KCQE_RAMROD_COMPLETION))
913 cnic_kwq_completion(dev, 1);
915 while (j < num_cqes) {
916 u32 next_op = cp->completed_kcq[i + j]->kcqe_op_flag;
918 if ((next_op & KCQE_FLAGS_LAYER_MASK) != kcqe_layer)
919 break;
921 if (unlikely(next_op & KCQE_RAMROD_COMPLETION))
922 cnic_kwq_completion(dev, 1);
923 j++;
926 if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_RDMA)
927 ulp_type = CNIC_ULP_RDMA;
928 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_ISCSI)
929 ulp_type = CNIC_ULP_ISCSI;
930 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L4)
931 ulp_type = CNIC_ULP_L4;
932 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L2)
933 goto end;
934 else {
935 printk(KERN_ERR PFX "%s: Unknown type of KCQE(0x%x)\n",
936 dev->netdev->name, kcqe_op_flag);
937 goto end;
940 rcu_read_lock();
941 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
942 if (likely(ulp_ops)) {
943 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
944 cp->completed_kcq + i, j);
946 rcu_read_unlock();
947 end:
948 num_cqes -= j;
949 i += j;
950 j = 1;
952 return;
955 static u16 cnic_bnx2_next_idx(u16 idx)
957 return idx + 1;
960 static u16 cnic_bnx2_hw_idx(u16 idx)
962 return idx;
965 static int cnic_get_kcqes(struct cnic_dev *dev, u16 hw_prod, u16 *sw_prod)
967 struct cnic_local *cp = dev->cnic_priv;
968 u16 i, ri, last;
969 struct kcqe *kcqe;
970 int kcqe_cnt = 0, last_cnt = 0;
972 i = ri = last = *sw_prod;
973 ri &= MAX_KCQ_IDX;
975 while ((i != hw_prod) && (kcqe_cnt < MAX_COMPLETED_KCQE)) {
976 kcqe = &cp->kcq[KCQ_PG(ri)][KCQ_IDX(ri)];
977 cp->completed_kcq[kcqe_cnt++] = kcqe;
978 i = cp->next_idx(i);
979 ri = i & MAX_KCQ_IDX;
980 if (likely(!(kcqe->kcqe_op_flag & KCQE_FLAGS_NEXT))) {
981 last_cnt = kcqe_cnt;
982 last = i;
986 *sw_prod = last;
987 return last_cnt;
990 static void cnic_chk_bnx2_pkt_rings(struct cnic_local *cp)
992 u16 rx_cons = *cp->rx_cons_ptr;
993 u16 tx_cons = *cp->tx_cons_ptr;
995 if (cp->tx_cons != tx_cons || cp->rx_cons != rx_cons) {
996 cp->tx_cons = tx_cons;
997 cp->rx_cons = rx_cons;
998 uio_event_notify(cp->cnic_uinfo);
1002 static int cnic_service_bnx2(void *data, void *status_blk)
1004 struct cnic_dev *dev = data;
1005 struct status_block *sblk = status_blk;
1006 struct cnic_local *cp = dev->cnic_priv;
1007 u32 status_idx = sblk->status_idx;
1008 u16 hw_prod, sw_prod;
1009 int kcqe_cnt;
1011 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
1012 return status_idx;
1014 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
1016 hw_prod = sblk->status_completion_producer_index;
1017 sw_prod = cp->kcq_prod_idx;
1018 while (sw_prod != hw_prod) {
1019 kcqe_cnt = cnic_get_kcqes(dev, hw_prod, &sw_prod);
1020 if (kcqe_cnt == 0)
1021 goto done;
1023 service_kcqes(dev, kcqe_cnt);
1025 /* Tell compiler that status_blk fields can change. */
1026 barrier();
1027 if (status_idx != sblk->status_idx) {
1028 status_idx = sblk->status_idx;
1029 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
1030 hw_prod = sblk->status_completion_producer_index;
1031 } else
1032 break;
1035 done:
1036 CNIC_WR16(dev, cp->kcq_io_addr, sw_prod);
1038 cp->kcq_prod_idx = sw_prod;
1040 cnic_chk_bnx2_pkt_rings(cp);
1041 return status_idx;
1044 static void cnic_service_bnx2_msix(unsigned long data)
1046 struct cnic_dev *dev = (struct cnic_dev *) data;
1047 struct cnic_local *cp = dev->cnic_priv;
1048 struct status_block_msix *status_blk = cp->bnx2_status_blk;
1049 u32 status_idx = status_blk->status_idx;
1050 u16 hw_prod, sw_prod;
1051 int kcqe_cnt;
1053 cp->kwq_con_idx = status_blk->status_cmd_consumer_index;
1055 hw_prod = status_blk->status_completion_producer_index;
1056 sw_prod = cp->kcq_prod_idx;
1057 while (sw_prod != hw_prod) {
1058 kcqe_cnt = cnic_get_kcqes(dev, hw_prod, &sw_prod);
1059 if (kcqe_cnt == 0)
1060 goto done;
1062 service_kcqes(dev, kcqe_cnt);
1064 /* Tell compiler that status_blk fields can change. */
1065 barrier();
1066 if (status_idx != status_blk->status_idx) {
1067 status_idx = status_blk->status_idx;
1068 cp->kwq_con_idx = status_blk->status_cmd_consumer_index;
1069 hw_prod = status_blk->status_completion_producer_index;
1070 } else
1071 break;
1074 done:
1075 CNIC_WR16(dev, cp->kcq_io_addr, sw_prod);
1076 cp->kcq_prod_idx = sw_prod;
1078 cnic_chk_bnx2_pkt_rings(cp);
1080 cp->last_status_idx = status_idx;
1081 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
1082 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
1085 static irqreturn_t cnic_irq(int irq, void *dev_instance)
1087 struct cnic_dev *dev = dev_instance;
1088 struct cnic_local *cp = dev->cnic_priv;
1089 u16 prod = cp->kcq_prod_idx & MAX_KCQ_IDX;
1091 if (cp->ack_int)
1092 cp->ack_int(dev);
1094 prefetch(cp->status_blk);
1095 prefetch(&cp->kcq[KCQ_PG(prod)][KCQ_IDX(prod)]);
1097 if (likely(test_bit(CNIC_F_CNIC_UP, &dev->flags)))
1098 tasklet_schedule(&cp->cnic_irq_task);
1100 return IRQ_HANDLED;
1103 static void cnic_ulp_stop(struct cnic_dev *dev)
1105 struct cnic_local *cp = dev->cnic_priv;
1106 int if_type;
1108 if (cp->cnic_uinfo)
1109 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
1111 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
1112 struct cnic_ulp_ops *ulp_ops;
1114 mutex_lock(&cnic_lock);
1115 ulp_ops = cp->ulp_ops[if_type];
1116 if (!ulp_ops) {
1117 mutex_unlock(&cnic_lock);
1118 continue;
1120 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
1121 mutex_unlock(&cnic_lock);
1123 if (test_and_clear_bit(ULP_F_START, &cp->ulp_flags[if_type]))
1124 ulp_ops->cnic_stop(cp->ulp_handle[if_type]);
1126 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
1130 static void cnic_ulp_start(struct cnic_dev *dev)
1132 struct cnic_local *cp = dev->cnic_priv;
1133 int if_type;
1135 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
1136 struct cnic_ulp_ops *ulp_ops;
1138 mutex_lock(&cnic_lock);
1139 ulp_ops = cp->ulp_ops[if_type];
1140 if (!ulp_ops || !ulp_ops->cnic_start) {
1141 mutex_unlock(&cnic_lock);
1142 continue;
1144 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
1145 mutex_unlock(&cnic_lock);
1147 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[if_type]))
1148 ulp_ops->cnic_start(cp->ulp_handle[if_type]);
1150 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
1154 static int cnic_ctl(void *data, struct cnic_ctl_info *info)
1156 struct cnic_dev *dev = data;
1158 switch (info->cmd) {
1159 case CNIC_CTL_STOP_CMD:
1160 cnic_hold(dev);
1162 cnic_ulp_stop(dev);
1163 cnic_stop_hw(dev);
1165 cnic_put(dev);
1166 break;
1167 case CNIC_CTL_START_CMD:
1168 cnic_hold(dev);
1170 if (!cnic_start_hw(dev))
1171 cnic_ulp_start(dev);
1173 cnic_put(dev);
1174 break;
1175 default:
1176 return -EINVAL;
1178 return 0;
1181 static void cnic_ulp_init(struct cnic_dev *dev)
1183 int i;
1184 struct cnic_local *cp = dev->cnic_priv;
1186 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
1187 struct cnic_ulp_ops *ulp_ops;
1189 mutex_lock(&cnic_lock);
1190 ulp_ops = cnic_ulp_tbl[i];
1191 if (!ulp_ops || !ulp_ops->cnic_init) {
1192 mutex_unlock(&cnic_lock);
1193 continue;
1195 ulp_get(ulp_ops);
1196 mutex_unlock(&cnic_lock);
1198 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[i]))
1199 ulp_ops->cnic_init(dev);
1201 ulp_put(ulp_ops);
1205 static void cnic_ulp_exit(struct cnic_dev *dev)
1207 int i;
1208 struct cnic_local *cp = dev->cnic_priv;
1210 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
1211 struct cnic_ulp_ops *ulp_ops;
1213 mutex_lock(&cnic_lock);
1214 ulp_ops = cnic_ulp_tbl[i];
1215 if (!ulp_ops || !ulp_ops->cnic_exit) {
1216 mutex_unlock(&cnic_lock);
1217 continue;
1219 ulp_get(ulp_ops);
1220 mutex_unlock(&cnic_lock);
1222 if (test_and_clear_bit(ULP_F_INIT, &cp->ulp_flags[i]))
1223 ulp_ops->cnic_exit(dev);
1225 ulp_put(ulp_ops);
1229 static int cnic_cm_offload_pg(struct cnic_sock *csk)
1231 struct cnic_dev *dev = csk->dev;
1232 struct l4_kwq_offload_pg *l4kwqe;
1233 struct kwqe *wqes[1];
1235 l4kwqe = (struct l4_kwq_offload_pg *) &csk->kwqe1;
1236 memset(l4kwqe, 0, sizeof(*l4kwqe));
1237 wqes[0] = (struct kwqe *) l4kwqe;
1239 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_OFFLOAD_PG;
1240 l4kwqe->flags =
1241 L4_LAYER_CODE << L4_KWQ_OFFLOAD_PG_LAYER_CODE_SHIFT;
1242 l4kwqe->l2hdr_nbytes = ETH_HLEN;
1244 l4kwqe->da0 = csk->ha[0];
1245 l4kwqe->da1 = csk->ha[1];
1246 l4kwqe->da2 = csk->ha[2];
1247 l4kwqe->da3 = csk->ha[3];
1248 l4kwqe->da4 = csk->ha[4];
1249 l4kwqe->da5 = csk->ha[5];
1251 l4kwqe->sa0 = dev->mac_addr[0];
1252 l4kwqe->sa1 = dev->mac_addr[1];
1253 l4kwqe->sa2 = dev->mac_addr[2];
1254 l4kwqe->sa3 = dev->mac_addr[3];
1255 l4kwqe->sa4 = dev->mac_addr[4];
1256 l4kwqe->sa5 = dev->mac_addr[5];
1258 l4kwqe->etype = ETH_P_IP;
1259 l4kwqe->ipid_count = DEF_IPID_COUNT;
1260 l4kwqe->host_opaque = csk->l5_cid;
1262 if (csk->vlan_id) {
1263 l4kwqe->pg_flags |= L4_KWQ_OFFLOAD_PG_VLAN_TAGGING;
1264 l4kwqe->vlan_tag = csk->vlan_id;
1265 l4kwqe->l2hdr_nbytes += 4;
1268 return dev->submit_kwqes(dev, wqes, 1);
1271 static int cnic_cm_update_pg(struct cnic_sock *csk)
1273 struct cnic_dev *dev = csk->dev;
1274 struct l4_kwq_update_pg *l4kwqe;
1275 struct kwqe *wqes[1];
1277 l4kwqe = (struct l4_kwq_update_pg *) &csk->kwqe1;
1278 memset(l4kwqe, 0, sizeof(*l4kwqe));
1279 wqes[0] = (struct kwqe *) l4kwqe;
1281 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPDATE_PG;
1282 l4kwqe->flags =
1283 L4_LAYER_CODE << L4_KWQ_UPDATE_PG_LAYER_CODE_SHIFT;
1284 l4kwqe->pg_cid = csk->pg_cid;
1286 l4kwqe->da0 = csk->ha[0];
1287 l4kwqe->da1 = csk->ha[1];
1288 l4kwqe->da2 = csk->ha[2];
1289 l4kwqe->da3 = csk->ha[3];
1290 l4kwqe->da4 = csk->ha[4];
1291 l4kwqe->da5 = csk->ha[5];
1293 l4kwqe->pg_host_opaque = csk->l5_cid;
1294 l4kwqe->pg_valids = L4_KWQ_UPDATE_PG_VALIDS_DA;
1296 return dev->submit_kwqes(dev, wqes, 1);
1299 static int cnic_cm_upload_pg(struct cnic_sock *csk)
1301 struct cnic_dev *dev = csk->dev;
1302 struct l4_kwq_upload *l4kwqe;
1303 struct kwqe *wqes[1];
1305 l4kwqe = (struct l4_kwq_upload *) &csk->kwqe1;
1306 memset(l4kwqe, 0, sizeof(*l4kwqe));
1307 wqes[0] = (struct kwqe *) l4kwqe;
1309 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPLOAD_PG;
1310 l4kwqe->flags =
1311 L4_LAYER_CODE << L4_KWQ_UPLOAD_LAYER_CODE_SHIFT;
1312 l4kwqe->cid = csk->pg_cid;
1314 return dev->submit_kwqes(dev, wqes, 1);
1317 static int cnic_cm_conn_req(struct cnic_sock *csk)
1319 struct cnic_dev *dev = csk->dev;
1320 struct l4_kwq_connect_req1 *l4kwqe1;
1321 struct l4_kwq_connect_req2 *l4kwqe2;
1322 struct l4_kwq_connect_req3 *l4kwqe3;
1323 struct kwqe *wqes[3];
1324 u8 tcp_flags = 0;
1325 int num_wqes = 2;
1327 l4kwqe1 = (struct l4_kwq_connect_req1 *) &csk->kwqe1;
1328 l4kwqe2 = (struct l4_kwq_connect_req2 *) &csk->kwqe2;
1329 l4kwqe3 = (struct l4_kwq_connect_req3 *) &csk->kwqe3;
1330 memset(l4kwqe1, 0, sizeof(*l4kwqe1));
1331 memset(l4kwqe2, 0, sizeof(*l4kwqe2));
1332 memset(l4kwqe3, 0, sizeof(*l4kwqe3));
1334 l4kwqe3->op_code = L4_KWQE_OPCODE_VALUE_CONNECT3;
1335 l4kwqe3->flags =
1336 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ3_LAYER_CODE_SHIFT;
1337 l4kwqe3->ka_timeout = csk->ka_timeout;
1338 l4kwqe3->ka_interval = csk->ka_interval;
1339 l4kwqe3->ka_max_probe_count = csk->ka_max_probe_count;
1340 l4kwqe3->tos = csk->tos;
1341 l4kwqe3->ttl = csk->ttl;
1342 l4kwqe3->snd_seq_scale = csk->snd_seq_scale;
1343 l4kwqe3->pmtu = csk->mtu;
1344 l4kwqe3->rcv_buf = csk->rcv_buf;
1345 l4kwqe3->snd_buf = csk->snd_buf;
1346 l4kwqe3->seed = csk->seed;
1348 wqes[0] = (struct kwqe *) l4kwqe1;
1349 if (test_bit(SK_F_IPV6, &csk->flags)) {
1350 wqes[1] = (struct kwqe *) l4kwqe2;
1351 wqes[2] = (struct kwqe *) l4kwqe3;
1352 num_wqes = 3;
1354 l4kwqe1->conn_flags = L4_KWQ_CONNECT_REQ1_IP_V6;
1355 l4kwqe2->op_code = L4_KWQE_OPCODE_VALUE_CONNECT2;
1356 l4kwqe2->flags =
1357 L4_KWQ_CONNECT_REQ2_LINKED_WITH_NEXT |
1358 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ2_LAYER_CODE_SHIFT;
1359 l4kwqe2->src_ip_v6_2 = be32_to_cpu(csk->src_ip[1]);
1360 l4kwqe2->src_ip_v6_3 = be32_to_cpu(csk->src_ip[2]);
1361 l4kwqe2->src_ip_v6_4 = be32_to_cpu(csk->src_ip[3]);
1362 l4kwqe2->dst_ip_v6_2 = be32_to_cpu(csk->dst_ip[1]);
1363 l4kwqe2->dst_ip_v6_3 = be32_to_cpu(csk->dst_ip[2]);
1364 l4kwqe2->dst_ip_v6_4 = be32_to_cpu(csk->dst_ip[3]);
1365 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct ipv6hdr) -
1366 sizeof(struct tcphdr);
1367 } else {
1368 wqes[1] = (struct kwqe *) l4kwqe3;
1369 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct iphdr) -
1370 sizeof(struct tcphdr);
1373 l4kwqe1->op_code = L4_KWQE_OPCODE_VALUE_CONNECT1;
1374 l4kwqe1->flags =
1375 (L4_LAYER_CODE << L4_KWQ_CONNECT_REQ1_LAYER_CODE_SHIFT) |
1376 L4_KWQ_CONNECT_REQ3_LINKED_WITH_NEXT;
1377 l4kwqe1->cid = csk->cid;
1378 l4kwqe1->pg_cid = csk->pg_cid;
1379 l4kwqe1->src_ip = be32_to_cpu(csk->src_ip[0]);
1380 l4kwqe1->dst_ip = be32_to_cpu(csk->dst_ip[0]);
1381 l4kwqe1->src_port = be16_to_cpu(csk->src_port);
1382 l4kwqe1->dst_port = be16_to_cpu(csk->dst_port);
1383 if (csk->tcp_flags & SK_TCP_NO_DELAY_ACK)
1384 tcp_flags |= L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK;
1385 if (csk->tcp_flags & SK_TCP_KEEP_ALIVE)
1386 tcp_flags |= L4_KWQ_CONNECT_REQ1_KEEP_ALIVE;
1387 if (csk->tcp_flags & SK_TCP_NAGLE)
1388 tcp_flags |= L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE;
1389 if (csk->tcp_flags & SK_TCP_TIMESTAMP)
1390 tcp_flags |= L4_KWQ_CONNECT_REQ1_TIME_STAMP;
1391 if (csk->tcp_flags & SK_TCP_SACK)
1392 tcp_flags |= L4_KWQ_CONNECT_REQ1_SACK;
1393 if (csk->tcp_flags & SK_TCP_SEG_SCALING)
1394 tcp_flags |= L4_KWQ_CONNECT_REQ1_SEG_SCALING;
1396 l4kwqe1->tcp_flags = tcp_flags;
1398 return dev->submit_kwqes(dev, wqes, num_wqes);
1401 static int cnic_cm_close_req(struct cnic_sock *csk)
1403 struct cnic_dev *dev = csk->dev;
1404 struct l4_kwq_close_req *l4kwqe;
1405 struct kwqe *wqes[1];
1407 l4kwqe = (struct l4_kwq_close_req *) &csk->kwqe2;
1408 memset(l4kwqe, 0, sizeof(*l4kwqe));
1409 wqes[0] = (struct kwqe *) l4kwqe;
1411 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_CLOSE;
1412 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_CLOSE_REQ_LAYER_CODE_SHIFT;
1413 l4kwqe->cid = csk->cid;
1415 return dev->submit_kwqes(dev, wqes, 1);
1418 static int cnic_cm_abort_req(struct cnic_sock *csk)
1420 struct cnic_dev *dev = csk->dev;
1421 struct l4_kwq_reset_req *l4kwqe;
1422 struct kwqe *wqes[1];
1424 l4kwqe = (struct l4_kwq_reset_req *) &csk->kwqe2;
1425 memset(l4kwqe, 0, sizeof(*l4kwqe));
1426 wqes[0] = (struct kwqe *) l4kwqe;
1428 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_RESET;
1429 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_RESET_REQ_LAYER_CODE_SHIFT;
1430 l4kwqe->cid = csk->cid;
1432 return dev->submit_kwqes(dev, wqes, 1);
1435 static int cnic_cm_create(struct cnic_dev *dev, int ulp_type, u32 cid,
1436 u32 l5_cid, struct cnic_sock **csk, void *context)
1438 struct cnic_local *cp = dev->cnic_priv;
1439 struct cnic_sock *csk1;
1441 if (l5_cid >= MAX_CM_SK_TBL_SZ)
1442 return -EINVAL;
1444 csk1 = &cp->csk_tbl[l5_cid];
1445 if (atomic_read(&csk1->ref_count))
1446 return -EAGAIN;
1448 if (test_and_set_bit(SK_F_INUSE, &csk1->flags))
1449 return -EBUSY;
1451 csk1->dev = dev;
1452 csk1->cid = cid;
1453 csk1->l5_cid = l5_cid;
1454 csk1->ulp_type = ulp_type;
1455 csk1->context = context;
1457 csk1->ka_timeout = DEF_KA_TIMEOUT;
1458 csk1->ka_interval = DEF_KA_INTERVAL;
1459 csk1->ka_max_probe_count = DEF_KA_MAX_PROBE_COUNT;
1460 csk1->tos = DEF_TOS;
1461 csk1->ttl = DEF_TTL;
1462 csk1->snd_seq_scale = DEF_SND_SEQ_SCALE;
1463 csk1->rcv_buf = DEF_RCV_BUF;
1464 csk1->snd_buf = DEF_SND_BUF;
1465 csk1->seed = DEF_SEED;
1467 *csk = csk1;
1468 return 0;
1471 static void cnic_cm_cleanup(struct cnic_sock *csk)
1473 if (csk->src_port) {
1474 struct cnic_dev *dev = csk->dev;
1475 struct cnic_local *cp = dev->cnic_priv;
1477 cnic_free_id(&cp->csk_port_tbl, csk->src_port);
1478 csk->src_port = 0;
1482 static void cnic_close_conn(struct cnic_sock *csk)
1484 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags)) {
1485 cnic_cm_upload_pg(csk);
1486 clear_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
1488 cnic_cm_cleanup(csk);
1491 static int cnic_cm_destroy(struct cnic_sock *csk)
1493 if (!cnic_in_use(csk))
1494 return -EINVAL;
1496 csk_hold(csk);
1497 clear_bit(SK_F_INUSE, &csk->flags);
1498 smp_mb__after_clear_bit();
1499 while (atomic_read(&csk->ref_count) != 1)
1500 msleep(1);
1501 cnic_cm_cleanup(csk);
1503 csk->flags = 0;
1504 csk_put(csk);
1505 return 0;
1508 static inline u16 cnic_get_vlan(struct net_device *dev,
1509 struct net_device **vlan_dev)
1511 if (dev->priv_flags & IFF_802_1Q_VLAN) {
1512 *vlan_dev = vlan_dev_real_dev(dev);
1513 return vlan_dev_vlan_id(dev);
1515 *vlan_dev = dev;
1516 return 0;
1519 static int cnic_get_v4_route(struct sockaddr_in *dst_addr,
1520 struct dst_entry **dst)
1522 #if defined(CONFIG_INET)
1523 struct flowi fl;
1524 int err;
1525 struct rtable *rt;
1527 memset(&fl, 0, sizeof(fl));
1528 fl.nl_u.ip4_u.daddr = dst_addr->sin_addr.s_addr;
1530 err = ip_route_output_key(&init_net, &rt, &fl);
1531 if (!err)
1532 *dst = &rt->u.dst;
1533 return err;
1534 #else
1535 return -ENETUNREACH;
1536 #endif
1539 static int cnic_get_v6_route(struct sockaddr_in6 *dst_addr,
1540 struct dst_entry **dst)
1542 #if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
1543 struct flowi fl;
1545 memset(&fl, 0, sizeof(fl));
1546 ipv6_addr_copy(&fl.fl6_dst, &dst_addr->sin6_addr);
1547 if (ipv6_addr_type(&fl.fl6_dst) & IPV6_ADDR_LINKLOCAL)
1548 fl.oif = dst_addr->sin6_scope_id;
1550 *dst = ip6_route_output(&init_net, NULL, &fl);
1551 if (*dst)
1552 return 0;
1553 #endif
1555 return -ENETUNREACH;
1558 static struct cnic_dev *cnic_cm_select_dev(struct sockaddr_in *dst_addr,
1559 int ulp_type)
1561 struct cnic_dev *dev = NULL;
1562 struct dst_entry *dst;
1563 struct net_device *netdev = NULL;
1564 int err = -ENETUNREACH;
1566 if (dst_addr->sin_family == AF_INET)
1567 err = cnic_get_v4_route(dst_addr, &dst);
1568 else if (dst_addr->sin_family == AF_INET6) {
1569 struct sockaddr_in6 *dst_addr6 =
1570 (struct sockaddr_in6 *) dst_addr;
1572 err = cnic_get_v6_route(dst_addr6, &dst);
1573 } else
1574 return NULL;
1576 if (err)
1577 return NULL;
1579 if (!dst->dev)
1580 goto done;
1582 cnic_get_vlan(dst->dev, &netdev);
1584 dev = cnic_from_netdev(netdev);
1586 done:
1587 dst_release(dst);
1588 if (dev)
1589 cnic_put(dev);
1590 return dev;
1593 static int cnic_resolve_addr(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
1595 struct cnic_dev *dev = csk->dev;
1596 struct cnic_local *cp = dev->cnic_priv;
1598 return cnic_send_nlmsg(cp, ISCSI_KEVENT_PATH_REQ, csk);
1601 static int cnic_get_route(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
1603 struct cnic_dev *dev = csk->dev;
1604 struct cnic_local *cp = dev->cnic_priv;
1605 int is_v6, err, rc = -ENETUNREACH;
1606 struct dst_entry *dst;
1607 struct net_device *realdev;
1608 u32 local_port;
1610 if (saddr->local.v6.sin6_family == AF_INET6 &&
1611 saddr->remote.v6.sin6_family == AF_INET6)
1612 is_v6 = 1;
1613 else if (saddr->local.v4.sin_family == AF_INET &&
1614 saddr->remote.v4.sin_family == AF_INET)
1615 is_v6 = 0;
1616 else
1617 return -EINVAL;
1619 clear_bit(SK_F_IPV6, &csk->flags);
1621 if (is_v6) {
1622 #if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
1623 set_bit(SK_F_IPV6, &csk->flags);
1624 err = cnic_get_v6_route(&saddr->remote.v6, &dst);
1625 if (err)
1626 return err;
1628 if (!dst || dst->error || !dst->dev)
1629 goto err_out;
1631 memcpy(&csk->dst_ip[0], &saddr->remote.v6.sin6_addr,
1632 sizeof(struct in6_addr));
1633 csk->dst_port = saddr->remote.v6.sin6_port;
1634 local_port = saddr->local.v6.sin6_port;
1635 #else
1636 return rc;
1637 #endif
1639 } else {
1640 err = cnic_get_v4_route(&saddr->remote.v4, &dst);
1641 if (err)
1642 return err;
1644 if (!dst || dst->error || !dst->dev)
1645 goto err_out;
1647 csk->dst_ip[0] = saddr->remote.v4.sin_addr.s_addr;
1648 csk->dst_port = saddr->remote.v4.sin_port;
1649 local_port = saddr->local.v4.sin_port;
1652 csk->vlan_id = cnic_get_vlan(dst->dev, &realdev);
1653 if (realdev != dev->netdev)
1654 goto err_out;
1656 if (local_port >= CNIC_LOCAL_PORT_MIN &&
1657 local_port < CNIC_LOCAL_PORT_MAX) {
1658 if (cnic_alloc_id(&cp->csk_port_tbl, local_port))
1659 local_port = 0;
1660 } else
1661 local_port = 0;
1663 if (!local_port) {
1664 local_port = cnic_alloc_new_id(&cp->csk_port_tbl);
1665 if (local_port == -1) {
1666 rc = -ENOMEM;
1667 goto err_out;
1670 csk->src_port = local_port;
1672 csk->mtu = dst_mtu(dst);
1673 rc = 0;
1675 err_out:
1676 dst_release(dst);
1677 return rc;
1680 static void cnic_init_csk_state(struct cnic_sock *csk)
1682 csk->state = 0;
1683 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
1684 clear_bit(SK_F_CLOSING, &csk->flags);
1687 static int cnic_cm_connect(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
1689 int err = 0;
1691 if (!cnic_in_use(csk))
1692 return -EINVAL;
1694 if (test_and_set_bit(SK_F_CONNECT_START, &csk->flags))
1695 return -EINVAL;
1697 cnic_init_csk_state(csk);
1699 err = cnic_get_route(csk, saddr);
1700 if (err)
1701 goto err_out;
1703 err = cnic_resolve_addr(csk, saddr);
1704 if (!err)
1705 return 0;
1707 err_out:
1708 clear_bit(SK_F_CONNECT_START, &csk->flags);
1709 return err;
1712 static int cnic_cm_abort(struct cnic_sock *csk)
1714 struct cnic_local *cp = csk->dev->cnic_priv;
1715 u32 opcode;
1717 if (!cnic_in_use(csk))
1718 return -EINVAL;
1720 if (cnic_abort_prep(csk))
1721 return cnic_cm_abort_req(csk);
1723 /* Getting here means that we haven't started connect, or
1724 * connect was not successful.
1727 csk->state = L4_KCQE_OPCODE_VALUE_RESET_COMP;
1728 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
1729 opcode = csk->state;
1730 else
1731 opcode = L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD;
1732 cp->close_conn(csk, opcode);
1734 return 0;
1737 static int cnic_cm_close(struct cnic_sock *csk)
1739 if (!cnic_in_use(csk))
1740 return -EINVAL;
1742 if (cnic_close_prep(csk)) {
1743 csk->state = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
1744 return cnic_cm_close_req(csk);
1746 return 0;
1749 static void cnic_cm_upcall(struct cnic_local *cp, struct cnic_sock *csk,
1750 u8 opcode)
1752 struct cnic_ulp_ops *ulp_ops;
1753 int ulp_type = csk->ulp_type;
1755 rcu_read_lock();
1756 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
1757 if (ulp_ops) {
1758 if (opcode == L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE)
1759 ulp_ops->cm_connect_complete(csk);
1760 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
1761 ulp_ops->cm_close_complete(csk);
1762 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED)
1763 ulp_ops->cm_remote_abort(csk);
1764 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_COMP)
1765 ulp_ops->cm_abort_complete(csk);
1766 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED)
1767 ulp_ops->cm_remote_close(csk);
1769 rcu_read_unlock();
1772 static int cnic_cm_set_pg(struct cnic_sock *csk)
1774 if (cnic_offld_prep(csk)) {
1775 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
1776 cnic_cm_update_pg(csk);
1777 else
1778 cnic_cm_offload_pg(csk);
1780 return 0;
1783 static void cnic_cm_process_offld_pg(struct cnic_dev *dev, struct l4_kcq *kcqe)
1785 struct cnic_local *cp = dev->cnic_priv;
1786 u32 l5_cid = kcqe->pg_host_opaque;
1787 u8 opcode = kcqe->op_code;
1788 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
1790 csk_hold(csk);
1791 if (!cnic_in_use(csk))
1792 goto done;
1794 if (opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
1795 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
1796 goto done;
1798 csk->pg_cid = kcqe->pg_cid;
1799 set_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
1800 cnic_cm_conn_req(csk);
1802 done:
1803 csk_put(csk);
1806 static void cnic_cm_process_kcqe(struct cnic_dev *dev, struct kcqe *kcqe)
1808 struct cnic_local *cp = dev->cnic_priv;
1809 struct l4_kcq *l4kcqe = (struct l4_kcq *) kcqe;
1810 u8 opcode = l4kcqe->op_code;
1811 u32 l5_cid;
1812 struct cnic_sock *csk;
1814 if (opcode == L4_KCQE_OPCODE_VALUE_OFFLOAD_PG ||
1815 opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
1816 cnic_cm_process_offld_pg(dev, l4kcqe);
1817 return;
1820 l5_cid = l4kcqe->conn_id;
1821 if (opcode & 0x80)
1822 l5_cid = l4kcqe->cid;
1823 if (l5_cid >= MAX_CM_SK_TBL_SZ)
1824 return;
1826 csk = &cp->csk_tbl[l5_cid];
1827 csk_hold(csk);
1829 if (!cnic_in_use(csk)) {
1830 csk_put(csk);
1831 return;
1834 switch (opcode) {
1835 case L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE:
1836 if (l4kcqe->status == 0)
1837 set_bit(SK_F_OFFLD_COMPLETE, &csk->flags);
1839 smp_mb__before_clear_bit();
1840 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
1841 cnic_cm_upcall(cp, csk, opcode);
1842 break;
1844 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
1845 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags))
1846 csk->state = opcode;
1847 /* fall through */
1848 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
1849 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
1850 cp->close_conn(csk, opcode);
1851 break;
1853 case L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED:
1854 cnic_cm_upcall(cp, csk, opcode);
1855 break;
1857 csk_put(csk);
1860 static void cnic_cm_indicate_kcqe(void *data, struct kcqe *kcqe[], u32 num)
1862 struct cnic_dev *dev = data;
1863 int i;
1865 for (i = 0; i < num; i++)
1866 cnic_cm_process_kcqe(dev, kcqe[i]);
1869 static struct cnic_ulp_ops cm_ulp_ops = {
1870 .indicate_kcqes = cnic_cm_indicate_kcqe,
1873 static void cnic_cm_free_mem(struct cnic_dev *dev)
1875 struct cnic_local *cp = dev->cnic_priv;
1877 kfree(cp->csk_tbl);
1878 cp->csk_tbl = NULL;
1879 cnic_free_id_tbl(&cp->csk_port_tbl);
1882 static int cnic_cm_alloc_mem(struct cnic_dev *dev)
1884 struct cnic_local *cp = dev->cnic_priv;
1886 cp->csk_tbl = kzalloc(sizeof(struct cnic_sock) * MAX_CM_SK_TBL_SZ,
1887 GFP_KERNEL);
1888 if (!cp->csk_tbl)
1889 return -ENOMEM;
1891 if (cnic_init_id_tbl(&cp->csk_port_tbl, CNIC_LOCAL_PORT_RANGE,
1892 CNIC_LOCAL_PORT_MIN)) {
1893 cnic_cm_free_mem(dev);
1894 return -ENOMEM;
1896 return 0;
1899 static int cnic_ready_to_close(struct cnic_sock *csk, u32 opcode)
1901 if ((opcode == csk->state) ||
1902 (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED &&
1903 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)) {
1904 if (!test_and_set_bit(SK_F_CLOSING, &csk->flags))
1905 return 1;
1907 return 0;
1910 static void cnic_close_bnx2_conn(struct cnic_sock *csk, u32 opcode)
1912 struct cnic_dev *dev = csk->dev;
1913 struct cnic_local *cp = dev->cnic_priv;
1915 clear_bit(SK_F_CONNECT_START, &csk->flags);
1916 if (cnic_ready_to_close(csk, opcode)) {
1917 cnic_close_conn(csk);
1918 cnic_cm_upcall(cp, csk, opcode);
1922 static void cnic_cm_stop_bnx2_hw(struct cnic_dev *dev)
1926 static int cnic_cm_init_bnx2_hw(struct cnic_dev *dev)
1928 u32 seed;
1930 get_random_bytes(&seed, 4);
1931 cnic_ctx_wr(dev, 45, 0, seed);
1932 return 0;
1935 static int cnic_cm_open(struct cnic_dev *dev)
1937 struct cnic_local *cp = dev->cnic_priv;
1938 int err;
1940 err = cnic_cm_alloc_mem(dev);
1941 if (err)
1942 return err;
1944 err = cp->start_cm(dev);
1946 if (err)
1947 goto err_out;
1949 dev->cm_create = cnic_cm_create;
1950 dev->cm_destroy = cnic_cm_destroy;
1951 dev->cm_connect = cnic_cm_connect;
1952 dev->cm_abort = cnic_cm_abort;
1953 dev->cm_close = cnic_cm_close;
1954 dev->cm_select_dev = cnic_cm_select_dev;
1956 cp->ulp_handle[CNIC_ULP_L4] = dev;
1957 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], &cm_ulp_ops);
1958 return 0;
1960 err_out:
1961 cnic_cm_free_mem(dev);
1962 return err;
1965 static int cnic_cm_shutdown(struct cnic_dev *dev)
1967 struct cnic_local *cp = dev->cnic_priv;
1968 int i;
1970 cp->stop_cm(dev);
1972 if (!cp->csk_tbl)
1973 return 0;
1975 for (i = 0; i < MAX_CM_SK_TBL_SZ; i++) {
1976 struct cnic_sock *csk = &cp->csk_tbl[i];
1978 clear_bit(SK_F_INUSE, &csk->flags);
1979 cnic_cm_cleanup(csk);
1981 cnic_cm_free_mem(dev);
1983 return 0;
1986 static void cnic_init_context(struct cnic_dev *dev, u32 cid)
1988 struct cnic_local *cp = dev->cnic_priv;
1989 u32 cid_addr;
1990 int i;
1992 if (CHIP_NUM(cp) == CHIP_NUM_5709)
1993 return;
1995 cid_addr = GET_CID_ADDR(cid);
1997 for (i = 0; i < CTX_SIZE; i += 4)
1998 cnic_ctx_wr(dev, cid_addr, i, 0);
2001 static int cnic_setup_5709_context(struct cnic_dev *dev, int valid)
2003 struct cnic_local *cp = dev->cnic_priv;
2004 int ret = 0, i;
2005 u32 valid_bit = valid ? BNX2_CTX_HOST_PAGE_TBL_DATA0_VALID : 0;
2007 if (CHIP_NUM(cp) != CHIP_NUM_5709)
2008 return 0;
2010 for (i = 0; i < cp->ctx_blks; i++) {
2011 int j;
2012 u32 idx = cp->ctx_arr[i].cid / cp->cids_per_blk;
2013 u32 val;
2015 memset(cp->ctx_arr[i].ctx, 0, BCM_PAGE_SIZE);
2017 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA0,
2018 (cp->ctx_arr[i].mapping & 0xffffffff) | valid_bit);
2019 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA1,
2020 (u64) cp->ctx_arr[i].mapping >> 32);
2021 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL, idx |
2022 BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
2023 for (j = 0; j < 10; j++) {
2025 val = CNIC_RD(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL);
2026 if (!(val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ))
2027 break;
2028 udelay(5);
2030 if (val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) {
2031 ret = -EBUSY;
2032 break;
2035 return ret;
2038 static void cnic_free_irq(struct cnic_dev *dev)
2040 struct cnic_local *cp = dev->cnic_priv;
2041 struct cnic_eth_dev *ethdev = cp->ethdev;
2043 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
2044 cp->disable_int_sync(dev);
2045 tasklet_disable(&cp->cnic_irq_task);
2046 free_irq(ethdev->irq_arr[0].vector, dev);
2050 static int cnic_init_bnx2_irq(struct cnic_dev *dev)
2052 struct cnic_local *cp = dev->cnic_priv;
2053 struct cnic_eth_dev *ethdev = cp->ethdev;
2055 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
2056 int err, i = 0;
2057 int sblk_num = cp->status_blk_num;
2058 u32 base = ((sblk_num - 1) * BNX2_HC_SB_CONFIG_SIZE) +
2059 BNX2_HC_SB_CONFIG_1;
2061 CNIC_WR(dev, base, BNX2_HC_SB_CONFIG_1_ONE_SHOT);
2063 CNIC_WR(dev, base + BNX2_HC_COMP_PROD_TRIP_OFF, (2 << 16) | 8);
2064 CNIC_WR(dev, base + BNX2_HC_COM_TICKS_OFF, (64 << 16) | 220);
2065 CNIC_WR(dev, base + BNX2_HC_CMD_TICKS_OFF, (64 << 16) | 220);
2067 cp->bnx2_status_blk = cp->status_blk;
2068 cp->last_status_idx = cp->bnx2_status_blk->status_idx;
2069 tasklet_init(&cp->cnic_irq_task, &cnic_service_bnx2_msix,
2070 (unsigned long) dev);
2071 err = request_irq(ethdev->irq_arr[0].vector, cnic_irq, 0,
2072 "cnic", dev);
2073 if (err) {
2074 tasklet_disable(&cp->cnic_irq_task);
2075 return err;
2077 while (cp->bnx2_status_blk->status_completion_producer_index &&
2078 i < 10) {
2079 CNIC_WR(dev, BNX2_HC_COALESCE_NOW,
2080 1 << (11 + sblk_num));
2081 udelay(10);
2082 i++;
2083 barrier();
2085 if (cp->bnx2_status_blk->status_completion_producer_index) {
2086 cnic_free_irq(dev);
2087 goto failed;
2090 } else {
2091 struct status_block *sblk = cp->status_blk;
2092 u32 hc_cmd = CNIC_RD(dev, BNX2_HC_COMMAND);
2093 int i = 0;
2095 while (sblk->status_completion_producer_index && i < 10) {
2096 CNIC_WR(dev, BNX2_HC_COMMAND,
2097 hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
2098 udelay(10);
2099 i++;
2100 barrier();
2102 if (sblk->status_completion_producer_index)
2103 goto failed;
2106 return 0;
2108 failed:
2109 printk(KERN_ERR PFX "%s: " "KCQ index not resetting to 0.\n",
2110 dev->netdev->name);
2111 return -EBUSY;
2114 static void cnic_enable_bnx2_int(struct cnic_dev *dev)
2116 struct cnic_local *cp = dev->cnic_priv;
2117 struct cnic_eth_dev *ethdev = cp->ethdev;
2119 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
2120 return;
2122 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
2123 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
2126 static void cnic_disable_bnx2_int_sync(struct cnic_dev *dev)
2128 struct cnic_local *cp = dev->cnic_priv;
2129 struct cnic_eth_dev *ethdev = cp->ethdev;
2131 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
2132 return;
2134 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
2135 BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
2136 CNIC_RD(dev, BNX2_PCICFG_INT_ACK_CMD);
2137 synchronize_irq(ethdev->irq_arr[0].vector);
2140 static void cnic_init_bnx2_tx_ring(struct cnic_dev *dev)
2142 struct cnic_local *cp = dev->cnic_priv;
2143 struct cnic_eth_dev *ethdev = cp->ethdev;
2144 u32 cid_addr, tx_cid, sb_id;
2145 u32 val, offset0, offset1, offset2, offset3;
2146 int i;
2147 struct tx_bd *txbd;
2148 dma_addr_t buf_map;
2149 struct status_block *s_blk = cp->status_blk;
2151 sb_id = cp->status_blk_num;
2152 tx_cid = 20;
2153 cnic_init_context(dev, tx_cid);
2154 cnic_init_context(dev, tx_cid + 1);
2155 cp->tx_cons_ptr = &s_blk->status_tx_quick_consumer_index2;
2156 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
2157 struct status_block_msix *sblk = cp->status_blk;
2159 tx_cid = TX_TSS_CID + sb_id - 1;
2160 cnic_init_context(dev, tx_cid);
2161 CNIC_WR(dev, BNX2_TSCH_TSS_CFG, (sb_id << 24) |
2162 (TX_TSS_CID << 7));
2163 cp->tx_cons_ptr = &sblk->status_tx_quick_consumer_index;
2165 cp->tx_cons = *cp->tx_cons_ptr;
2167 cid_addr = GET_CID_ADDR(tx_cid);
2168 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
2169 u32 cid_addr2 = GET_CID_ADDR(tx_cid + 4) + 0x40;
2171 for (i = 0; i < PHY_CTX_SIZE; i += 4)
2172 cnic_ctx_wr(dev, cid_addr2, i, 0);
2174 offset0 = BNX2_L2CTX_TYPE_XI;
2175 offset1 = BNX2_L2CTX_CMD_TYPE_XI;
2176 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI_XI;
2177 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO_XI;
2178 } else {
2179 offset0 = BNX2_L2CTX_TYPE;
2180 offset1 = BNX2_L2CTX_CMD_TYPE;
2181 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI;
2182 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO;
2184 val = BNX2_L2CTX_TYPE_TYPE_L2 | BNX2_L2CTX_TYPE_SIZE_L2;
2185 cnic_ctx_wr(dev, cid_addr, offset0, val);
2187 val = BNX2_L2CTX_CMD_TYPE_TYPE_L2 | (8 << 16);
2188 cnic_ctx_wr(dev, cid_addr, offset1, val);
2190 txbd = (struct tx_bd *) cp->l2_ring;
2192 buf_map = cp->l2_buf_map;
2193 for (i = 0; i < MAX_TX_DESC_CNT; i++, txbd++) {
2194 txbd->tx_bd_haddr_hi = (u64) buf_map >> 32;
2195 txbd->tx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
2197 val = (u64) cp->l2_ring_map >> 32;
2198 cnic_ctx_wr(dev, cid_addr, offset2, val);
2199 txbd->tx_bd_haddr_hi = val;
2201 val = (u64) cp->l2_ring_map & 0xffffffff;
2202 cnic_ctx_wr(dev, cid_addr, offset3, val);
2203 txbd->tx_bd_haddr_lo = val;
2206 static void cnic_init_bnx2_rx_ring(struct cnic_dev *dev)
2208 struct cnic_local *cp = dev->cnic_priv;
2209 struct cnic_eth_dev *ethdev = cp->ethdev;
2210 u32 cid_addr, sb_id, val, coal_reg, coal_val;
2211 int i;
2212 struct rx_bd *rxbd;
2213 struct status_block *s_blk = cp->status_blk;
2215 sb_id = cp->status_blk_num;
2216 cnic_init_context(dev, 2);
2217 cp->rx_cons_ptr = &s_blk->status_rx_quick_consumer_index2;
2218 coal_reg = BNX2_HC_COMMAND;
2219 coal_val = CNIC_RD(dev, coal_reg);
2220 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
2221 struct status_block_msix *sblk = cp->status_blk;
2223 cp->rx_cons_ptr = &sblk->status_rx_quick_consumer_index;
2224 coal_reg = BNX2_HC_COALESCE_NOW;
2225 coal_val = 1 << (11 + sb_id);
2227 i = 0;
2228 while (!(*cp->rx_cons_ptr != 0) && i < 10) {
2229 CNIC_WR(dev, coal_reg, coal_val);
2230 udelay(10);
2231 i++;
2232 barrier();
2234 cp->rx_cons = *cp->rx_cons_ptr;
2236 cid_addr = GET_CID_ADDR(2);
2237 val = BNX2_L2CTX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
2238 BNX2_L2CTX_CTX_TYPE_SIZE_L2 | (0x02 << 8);
2239 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_CTX_TYPE, val);
2241 if (sb_id == 0)
2242 val = 2 << BNX2_L2CTX_STATUSB_NUM_SHIFT;
2243 else
2244 val = BNX2_L2CTX_STATUSB_NUM(sb_id);
2245 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_HOST_BDIDX, val);
2247 rxbd = (struct rx_bd *) (cp->l2_ring + BCM_PAGE_SIZE);
2248 for (i = 0; i < MAX_RX_DESC_CNT; i++, rxbd++) {
2249 dma_addr_t buf_map;
2250 int n = (i % cp->l2_rx_ring_size) + 1;
2252 buf_map = cp->l2_buf_map + (n * cp->l2_single_buf_size);
2253 rxbd->rx_bd_len = cp->l2_single_buf_size;
2254 rxbd->rx_bd_flags = RX_BD_FLAGS_START | RX_BD_FLAGS_END;
2255 rxbd->rx_bd_haddr_hi = (u64) buf_map >> 32;
2256 rxbd->rx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
2258 val = (u64) (cp->l2_ring_map + BCM_PAGE_SIZE) >> 32;
2259 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_HI, val);
2260 rxbd->rx_bd_haddr_hi = val;
2262 val = (u64) (cp->l2_ring_map + BCM_PAGE_SIZE) & 0xffffffff;
2263 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_LO, val);
2264 rxbd->rx_bd_haddr_lo = val;
2266 val = cnic_reg_rd_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD);
2267 cnic_reg_wr_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD, val | (1 << 2));
2270 static void cnic_shutdown_bnx2_rx_ring(struct cnic_dev *dev)
2272 struct kwqe *wqes[1], l2kwqe;
2274 memset(&l2kwqe, 0, sizeof(l2kwqe));
2275 wqes[0] = &l2kwqe;
2276 l2kwqe.kwqe_op_flag = (L2_LAYER_CODE << KWQE_FLAGS_LAYER_SHIFT) |
2277 (L2_KWQE_OPCODE_VALUE_FLUSH <<
2278 KWQE_OPCODE_SHIFT) | 2;
2279 dev->submit_kwqes(dev, wqes, 1);
2282 static void cnic_set_bnx2_mac(struct cnic_dev *dev)
2284 struct cnic_local *cp = dev->cnic_priv;
2285 u32 val;
2287 val = cp->func << 2;
2289 cp->shmem_base = cnic_reg_rd_ind(dev, BNX2_SHM_HDR_ADDR_0 + val);
2291 val = cnic_reg_rd_ind(dev, cp->shmem_base +
2292 BNX2_PORT_HW_CFG_ISCSI_MAC_UPPER);
2293 dev->mac_addr[0] = (u8) (val >> 8);
2294 dev->mac_addr[1] = (u8) val;
2296 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH4, val);
2298 val = cnic_reg_rd_ind(dev, cp->shmem_base +
2299 BNX2_PORT_HW_CFG_ISCSI_MAC_LOWER);
2300 dev->mac_addr[2] = (u8) (val >> 24);
2301 dev->mac_addr[3] = (u8) (val >> 16);
2302 dev->mac_addr[4] = (u8) (val >> 8);
2303 dev->mac_addr[5] = (u8) val;
2305 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH5, val);
2307 val = 4 | BNX2_RPM_SORT_USER2_BC_EN;
2308 if (CHIP_NUM(cp) != CHIP_NUM_5709)
2309 val |= BNX2_RPM_SORT_USER2_PROM_VLAN;
2311 CNIC_WR(dev, BNX2_RPM_SORT_USER2, 0x0);
2312 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val);
2313 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val | BNX2_RPM_SORT_USER2_ENA);
2316 static int cnic_start_bnx2_hw(struct cnic_dev *dev)
2318 struct cnic_local *cp = dev->cnic_priv;
2319 struct cnic_eth_dev *ethdev = cp->ethdev;
2320 struct status_block *sblk = cp->status_blk;
2321 u32 val;
2322 int err;
2324 cnic_set_bnx2_mac(dev);
2326 val = CNIC_RD(dev, BNX2_MQ_CONFIG);
2327 val &= ~BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE;
2328 if (BCM_PAGE_BITS > 12)
2329 val |= (12 - 8) << 4;
2330 else
2331 val |= (BCM_PAGE_BITS - 8) << 4;
2333 CNIC_WR(dev, BNX2_MQ_CONFIG, val);
2335 CNIC_WR(dev, BNX2_HC_COMP_PROD_TRIP, (2 << 16) | 8);
2336 CNIC_WR(dev, BNX2_HC_COM_TICKS, (64 << 16) | 220);
2337 CNIC_WR(dev, BNX2_HC_CMD_TICKS, (64 << 16) | 220);
2339 err = cnic_setup_5709_context(dev, 1);
2340 if (err)
2341 return err;
2343 cnic_init_context(dev, KWQ_CID);
2344 cnic_init_context(dev, KCQ_CID);
2346 cp->kwq_cid_addr = GET_CID_ADDR(KWQ_CID);
2347 cp->kwq_io_addr = MB_GET_CID_ADDR(KWQ_CID) + L5_KRNLQ_HOST_QIDX;
2349 cp->max_kwq_idx = MAX_KWQ_IDX;
2350 cp->kwq_prod_idx = 0;
2351 cp->kwq_con_idx = 0;
2352 cp->cnic_local_flags |= CNIC_LCL_FL_KWQ_INIT;
2354 if (CHIP_NUM(cp) == CHIP_NUM_5706 || CHIP_NUM(cp) == CHIP_NUM_5708)
2355 cp->kwq_con_idx_ptr = &sblk->status_rx_quick_consumer_index15;
2356 else
2357 cp->kwq_con_idx_ptr = &sblk->status_cmd_consumer_index;
2359 /* Initialize the kernel work queue context. */
2360 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
2361 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
2362 cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_TYPE, val);
2364 val = (BCM_PAGE_SIZE / sizeof(struct kwqe) - 1) << 16;
2365 cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
2367 val = ((BCM_PAGE_SIZE / sizeof(struct kwqe)) << 16) | KWQ_PAGE_CNT;
2368 cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
2370 val = (u32) ((u64) cp->kwq_info.pgtbl_map >> 32);
2371 cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
2373 val = (u32) cp->kwq_info.pgtbl_map;
2374 cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
2376 cp->kcq_cid_addr = GET_CID_ADDR(KCQ_CID);
2377 cp->kcq_io_addr = MB_GET_CID_ADDR(KCQ_CID) + L5_KRNLQ_HOST_QIDX;
2379 cp->kcq_prod_idx = 0;
2381 /* Initialize the kernel complete queue context. */
2382 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
2383 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
2384 cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_TYPE, val);
2386 val = (BCM_PAGE_SIZE / sizeof(struct kcqe) - 1) << 16;
2387 cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
2389 val = ((BCM_PAGE_SIZE / sizeof(struct kcqe)) << 16) | KCQ_PAGE_CNT;
2390 cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
2392 val = (u32) ((u64) cp->kcq_info.pgtbl_map >> 32);
2393 cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
2395 val = (u32) cp->kcq_info.pgtbl_map;
2396 cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
2398 cp->int_num = 0;
2399 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
2400 u32 sb_id = cp->status_blk_num;
2401 u32 sb = BNX2_L2CTX_STATUSB_NUM(sb_id);
2403 cp->int_num = sb_id << BNX2_PCICFG_INT_ACK_CMD_INT_NUM_SHIFT;
2404 cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
2405 cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
2408 /* Enable Commnad Scheduler notification when we write to the
2409 * host producer index of the kernel contexts. */
2410 CNIC_WR(dev, BNX2_MQ_KNL_CMD_MASK1, 2);
2412 /* Enable Command Scheduler notification when we write to either
2413 * the Send Queue or Receive Queue producer indexes of the kernel
2414 * bypass contexts. */
2415 CNIC_WR(dev, BNX2_MQ_KNL_BYP_CMD_MASK1, 7);
2416 CNIC_WR(dev, BNX2_MQ_KNL_BYP_WRITE_MASK1, 7);
2418 /* Notify COM when the driver post an application buffer. */
2419 CNIC_WR(dev, BNX2_MQ_KNL_RX_V2P_MASK2, 0x2000);
2421 /* Set the CP and COM doorbells. These two processors polls the
2422 * doorbell for a non zero value before running. This must be done
2423 * after setting up the kernel queue contexts. */
2424 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 1);
2425 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 1);
2427 cnic_init_bnx2_tx_ring(dev);
2428 cnic_init_bnx2_rx_ring(dev);
2430 err = cnic_init_bnx2_irq(dev);
2431 if (err) {
2432 printk(KERN_ERR PFX "%s: cnic_init_irq failed\n",
2433 dev->netdev->name);
2434 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
2435 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
2436 return err;
2439 return 0;
2442 static int cnic_register_netdev(struct cnic_dev *dev)
2444 struct cnic_local *cp = dev->cnic_priv;
2445 struct cnic_eth_dev *ethdev = cp->ethdev;
2446 int err;
2448 if (!ethdev)
2449 return -ENODEV;
2451 if (ethdev->drv_state & CNIC_DRV_STATE_REGD)
2452 return 0;
2454 err = ethdev->drv_register_cnic(dev->netdev, cp->cnic_ops, dev);
2455 if (err)
2456 printk(KERN_ERR PFX "%s: register_cnic failed\n",
2457 dev->netdev->name);
2459 return err;
2462 static void cnic_unregister_netdev(struct cnic_dev *dev)
2464 struct cnic_local *cp = dev->cnic_priv;
2465 struct cnic_eth_dev *ethdev = cp->ethdev;
2467 if (!ethdev)
2468 return;
2470 ethdev->drv_unregister_cnic(dev->netdev);
2473 static int cnic_start_hw(struct cnic_dev *dev)
2475 struct cnic_local *cp = dev->cnic_priv;
2476 struct cnic_eth_dev *ethdev = cp->ethdev;
2477 int err;
2479 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
2480 return -EALREADY;
2482 dev->regview = ethdev->io_base;
2483 cp->chip_id = ethdev->chip_id;
2484 pci_dev_get(dev->pcidev);
2485 cp->func = PCI_FUNC(dev->pcidev->devfn);
2486 cp->status_blk = ethdev->irq_arr[0].status_blk;
2487 cp->status_blk_num = ethdev->irq_arr[0].status_blk_num;
2489 err = cp->alloc_resc(dev);
2490 if (err) {
2491 printk(KERN_ERR PFX "%s: allocate resource failure\n",
2492 dev->netdev->name);
2493 goto err1;
2496 err = cp->start_hw(dev);
2497 if (err)
2498 goto err1;
2500 err = cnic_cm_open(dev);
2501 if (err)
2502 goto err1;
2504 set_bit(CNIC_F_CNIC_UP, &dev->flags);
2506 cp->enable_int(dev);
2508 return 0;
2510 err1:
2511 cp->free_resc(dev);
2512 pci_dev_put(dev->pcidev);
2513 return err;
2516 static void cnic_stop_bnx2_hw(struct cnic_dev *dev)
2518 cnic_disable_bnx2_int_sync(dev);
2520 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
2521 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
2523 cnic_init_context(dev, KWQ_CID);
2524 cnic_init_context(dev, KCQ_CID);
2526 cnic_setup_5709_context(dev, 0);
2527 cnic_free_irq(dev);
2529 cnic_free_resc(dev);
2532 static void cnic_stop_hw(struct cnic_dev *dev)
2534 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
2535 struct cnic_local *cp = dev->cnic_priv;
2537 clear_bit(CNIC_F_CNIC_UP, &dev->flags);
2538 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], NULL);
2539 synchronize_rcu();
2540 cnic_cm_shutdown(dev);
2541 cp->stop_hw(dev);
2542 pci_dev_put(dev->pcidev);
2546 static void cnic_free_dev(struct cnic_dev *dev)
2548 int i = 0;
2550 while ((atomic_read(&dev->ref_count) != 0) && i < 10) {
2551 msleep(100);
2552 i++;
2554 if (atomic_read(&dev->ref_count) != 0)
2555 printk(KERN_ERR PFX "%s: Failed waiting for ref count to go"
2556 " to zero.\n", dev->netdev->name);
2558 printk(KERN_INFO PFX "Removed CNIC device: %s\n", dev->netdev->name);
2559 dev_put(dev->netdev);
2560 kfree(dev);
2563 static struct cnic_dev *cnic_alloc_dev(struct net_device *dev,
2564 struct pci_dev *pdev)
2566 struct cnic_dev *cdev;
2567 struct cnic_local *cp;
2568 int alloc_size;
2570 alloc_size = sizeof(struct cnic_dev) + sizeof(struct cnic_local);
2572 cdev = kzalloc(alloc_size , GFP_KERNEL);
2573 if (cdev == NULL) {
2574 printk(KERN_ERR PFX "%s: allocate dev struct failure\n",
2575 dev->name);
2576 return NULL;
2579 cdev->netdev = dev;
2580 cdev->cnic_priv = (char *)cdev + sizeof(struct cnic_dev);
2581 cdev->register_device = cnic_register_device;
2582 cdev->unregister_device = cnic_unregister_device;
2583 cdev->iscsi_nl_msg_recv = cnic_iscsi_nl_msg_recv;
2585 cp = cdev->cnic_priv;
2586 cp->dev = cdev;
2587 cp->uio_dev = -1;
2588 cp->l2_single_buf_size = 0x400;
2589 cp->l2_rx_ring_size = 3;
2591 spin_lock_init(&cp->cnic_ulp_lock);
2593 printk(KERN_INFO PFX "Added CNIC device: %s\n", dev->name);
2595 return cdev;
2598 static struct cnic_dev *init_bnx2_cnic(struct net_device *dev)
2600 struct pci_dev *pdev;
2601 struct cnic_dev *cdev;
2602 struct cnic_local *cp;
2603 struct cnic_eth_dev *ethdev = NULL;
2604 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
2606 probe = symbol_get(bnx2_cnic_probe);
2607 if (probe) {
2608 ethdev = (*probe)(dev);
2609 symbol_put(bnx2_cnic_probe);
2611 if (!ethdev)
2612 return NULL;
2614 pdev = ethdev->pdev;
2615 if (!pdev)
2616 return NULL;
2618 dev_hold(dev);
2619 pci_dev_get(pdev);
2620 if (pdev->device == PCI_DEVICE_ID_NX2_5709 ||
2621 pdev->device == PCI_DEVICE_ID_NX2_5709S) {
2622 u8 rev;
2624 pci_read_config_byte(pdev, PCI_REVISION_ID, &rev);
2625 if (rev < 0x10) {
2626 pci_dev_put(pdev);
2627 goto cnic_err;
2630 pci_dev_put(pdev);
2632 cdev = cnic_alloc_dev(dev, pdev);
2633 if (cdev == NULL)
2634 goto cnic_err;
2636 set_bit(CNIC_F_BNX2_CLASS, &cdev->flags);
2637 cdev->submit_kwqes = cnic_submit_bnx2_kwqes;
2639 cp = cdev->cnic_priv;
2640 cp->ethdev = ethdev;
2641 cdev->pcidev = pdev;
2643 cp->cnic_ops = &cnic_bnx2_ops;
2644 cp->start_hw = cnic_start_bnx2_hw;
2645 cp->stop_hw = cnic_stop_bnx2_hw;
2646 cp->setup_pgtbl = cnic_setup_page_tbl;
2647 cp->alloc_resc = cnic_alloc_bnx2_resc;
2648 cp->free_resc = cnic_free_resc;
2649 cp->start_cm = cnic_cm_init_bnx2_hw;
2650 cp->stop_cm = cnic_cm_stop_bnx2_hw;
2651 cp->enable_int = cnic_enable_bnx2_int;
2652 cp->disable_int_sync = cnic_disable_bnx2_int_sync;
2653 cp->close_conn = cnic_close_bnx2_conn;
2654 cp->next_idx = cnic_bnx2_next_idx;
2655 cp->hw_idx = cnic_bnx2_hw_idx;
2656 return cdev;
2658 cnic_err:
2659 dev_put(dev);
2660 return NULL;
2663 static struct cnic_dev *is_cnic_dev(struct net_device *dev)
2665 struct ethtool_drvinfo drvinfo;
2666 struct cnic_dev *cdev = NULL;
2668 if (dev->ethtool_ops && dev->ethtool_ops->get_drvinfo) {
2669 memset(&drvinfo, 0, sizeof(drvinfo));
2670 dev->ethtool_ops->get_drvinfo(dev, &drvinfo);
2672 if (!strcmp(drvinfo.driver, "bnx2"))
2673 cdev = init_bnx2_cnic(dev);
2674 if (cdev) {
2675 write_lock(&cnic_dev_lock);
2676 list_add(&cdev->list, &cnic_dev_list);
2677 write_unlock(&cnic_dev_lock);
2680 return cdev;
2684 * netdev event handler
2686 static int cnic_netdev_event(struct notifier_block *this, unsigned long event,
2687 void *ptr)
2689 struct net_device *netdev = ptr;
2690 struct cnic_dev *dev;
2691 int if_type;
2692 int new_dev = 0;
2694 dev = cnic_from_netdev(netdev);
2696 if (!dev && (event == NETDEV_REGISTER || event == NETDEV_UP)) {
2697 /* Check for the hot-plug device */
2698 dev = is_cnic_dev(netdev);
2699 if (dev) {
2700 new_dev = 1;
2701 cnic_hold(dev);
2704 if (dev) {
2705 struct cnic_local *cp = dev->cnic_priv;
2707 if (new_dev)
2708 cnic_ulp_init(dev);
2709 else if (event == NETDEV_UNREGISTER)
2710 cnic_ulp_exit(dev);
2711 else if (event == NETDEV_UP) {
2712 if (cnic_register_netdev(dev) != 0) {
2713 cnic_put(dev);
2714 goto done;
2716 if (!cnic_start_hw(dev))
2717 cnic_ulp_start(dev);
2720 rcu_read_lock();
2721 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
2722 struct cnic_ulp_ops *ulp_ops;
2723 void *ctx;
2725 ulp_ops = rcu_dereference(cp->ulp_ops[if_type]);
2726 if (!ulp_ops || !ulp_ops->indicate_netevent)
2727 continue;
2729 ctx = cp->ulp_handle[if_type];
2731 ulp_ops->indicate_netevent(ctx, event);
2733 rcu_read_unlock();
2735 if (event == NETDEV_GOING_DOWN) {
2736 cnic_ulp_stop(dev);
2737 cnic_stop_hw(dev);
2738 cnic_unregister_netdev(dev);
2739 } else if (event == NETDEV_UNREGISTER) {
2740 write_lock(&cnic_dev_lock);
2741 list_del_init(&dev->list);
2742 write_unlock(&cnic_dev_lock);
2744 cnic_put(dev);
2745 cnic_free_dev(dev);
2746 goto done;
2748 cnic_put(dev);
2750 done:
2751 return NOTIFY_DONE;
2754 static struct notifier_block cnic_netdev_notifier = {
2755 .notifier_call = cnic_netdev_event
2758 static void cnic_release(void)
2760 struct cnic_dev *dev;
2762 while (!list_empty(&cnic_dev_list)) {
2763 dev = list_entry(cnic_dev_list.next, struct cnic_dev, list);
2764 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
2765 cnic_ulp_stop(dev);
2766 cnic_stop_hw(dev);
2769 cnic_ulp_exit(dev);
2770 cnic_unregister_netdev(dev);
2771 list_del_init(&dev->list);
2772 cnic_free_dev(dev);
2776 static int __init cnic_init(void)
2778 int rc = 0;
2780 printk(KERN_INFO "%s", version);
2782 rc = register_netdevice_notifier(&cnic_netdev_notifier);
2783 if (rc) {
2784 cnic_release();
2785 return rc;
2788 return 0;
2791 static void __exit cnic_exit(void)
2793 unregister_netdevice_notifier(&cnic_netdev_notifier);
2794 cnic_release();
2795 return;
2798 module_init(cnic_init);
2799 module_exit(cnic_exit);