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