039900c416993742cdc824671d08b1f6a05eea7d
[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_bnx2_resc(struct cnic_dev *dev)
766 {
767         struct cnic_local *cp = dev->cnic_priv;
768         struct uio_info *uinfo;
769         int ret;
770
771         ret = cnic_alloc_dma(dev, &cp->kwq_info, KWQ_PAGE_CNT, 1);
772         if (ret)
773                 goto error;
774         cp->kwq = (struct kwqe **) cp->kwq_info.pg_arr;
775
776         ret = cnic_alloc_dma(dev, &cp->kcq_info, KCQ_PAGE_CNT, 1);
777         if (ret)
778                 goto error;
779         cp->kcq = (struct kcqe **) cp->kcq_info.pg_arr;
780
781         ret = cnic_alloc_context(dev);
782         if (ret)
783                 goto error;
784
785         ret = cnic_alloc_l2_rings(dev, 2);
786         if (ret)
787                 goto error;
788
789         uinfo = kzalloc(sizeof(*uinfo), GFP_ATOMIC);
790         if (!uinfo)
791                 goto error;
792
793         uinfo->mem[0].addr = dev->netdev->base_addr;
794         uinfo->mem[0].internal_addr = dev->regview;
795         uinfo->mem[0].size = dev->netdev->mem_end - dev->netdev->mem_start;
796         uinfo->mem[0].memtype = UIO_MEM_PHYS;
797
798         uinfo->mem[1].addr = (unsigned long) cp->status_blk & PAGE_MASK;
799         if (cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
800                 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE * 9;
801         else
802                 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE;
803         uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
804
805         uinfo->mem[2].addr = (unsigned long) cp->l2_ring;
806         uinfo->mem[2].size = cp->l2_ring_size;
807         uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
808
809         uinfo->mem[3].addr = (unsigned long) cp->l2_buf;
810         uinfo->mem[3].size = cp->l2_buf_size;
811         uinfo->mem[3].memtype = UIO_MEM_LOGICAL;
812
813         uinfo->name = "bnx2_cnic";
814         uinfo->version = CNIC_MODULE_VERSION;
815         uinfo->irq = UIO_IRQ_CUSTOM;
816
817         uinfo->open = cnic_uio_open;
818         uinfo->release = cnic_uio_close;
819
820         uinfo->priv = dev;
821
822         ret = uio_register_device(&dev->pcidev->dev, uinfo);
823         if (ret) {
824                 kfree(uinfo);
825                 goto error;
826         }
827
828         cp->cnic_uinfo = uinfo;
829
830         return 0;
831
832 error:
833         cnic_free_resc(dev);
834         return ret;
835 }
836
837 static inline u32 cnic_kwq_avail(struct cnic_local *cp)
838 {
839         return cp->max_kwq_idx -
840                 ((cp->kwq_prod_idx - cp->kwq_con_idx) & cp->max_kwq_idx);
841 }
842
843 static int cnic_submit_bnx2_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
844                                   u32 num_wqes)
845 {
846         struct cnic_local *cp = dev->cnic_priv;
847         struct kwqe *prod_qe;
848         u16 prod, sw_prod, i;
849
850         if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
851                 return -EAGAIN;         /* bnx2 is down */
852
853         spin_lock_bh(&cp->cnic_ulp_lock);
854         if (num_wqes > cnic_kwq_avail(cp) &&
855             !(cp->cnic_local_flags & CNIC_LCL_FL_KWQ_INIT)) {
856                 spin_unlock_bh(&cp->cnic_ulp_lock);
857                 return -EAGAIN;
858         }
859
860         cp->cnic_local_flags &= ~CNIC_LCL_FL_KWQ_INIT;
861
862         prod = cp->kwq_prod_idx;
863         sw_prod = prod & MAX_KWQ_IDX;
864         for (i = 0; i < num_wqes; i++) {
865                 prod_qe = &cp->kwq[KWQ_PG(sw_prod)][KWQ_IDX(sw_prod)];
866                 memcpy(prod_qe, wqes[i], sizeof(struct kwqe));
867                 prod++;
868                 sw_prod = prod & MAX_KWQ_IDX;
869         }
870         cp->kwq_prod_idx = prod;
871
872         CNIC_WR16(dev, cp->kwq_io_addr, cp->kwq_prod_idx);
873
874         spin_unlock_bh(&cp->cnic_ulp_lock);
875         return 0;
876 }
877
878 static void service_kcqes(struct cnic_dev *dev, int num_cqes)
879 {
880         struct cnic_local *cp = dev->cnic_priv;
881         int i, j;
882
883         i = 0;
884         j = 1;
885         while (num_cqes) {
886                 struct cnic_ulp_ops *ulp_ops;
887                 int ulp_type;
888                 u32 kcqe_op_flag = cp->completed_kcq[i]->kcqe_op_flag;
889                 u32 kcqe_layer = kcqe_op_flag & KCQE_FLAGS_LAYER_MASK;
890
891                 if (unlikely(kcqe_op_flag & KCQE_RAMROD_COMPLETION))
892                         cnic_kwq_completion(dev, 1);
893
894                 while (j < num_cqes) {
895                         u32 next_op = cp->completed_kcq[i + j]->kcqe_op_flag;
896
897                         if ((next_op & KCQE_FLAGS_LAYER_MASK) != kcqe_layer)
898                                 break;
899
900                         if (unlikely(next_op & KCQE_RAMROD_COMPLETION))
901                                 cnic_kwq_completion(dev, 1);
902                         j++;
903                 }
904
905                 if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_RDMA)
906                         ulp_type = CNIC_ULP_RDMA;
907                 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_ISCSI)
908                         ulp_type = CNIC_ULP_ISCSI;
909                 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L4)
910                         ulp_type = CNIC_ULP_L4;
911                 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L2)
912                         goto end;
913                 else {
914                         printk(KERN_ERR PFX "%s: Unknown type of KCQE(0x%x)\n",
915                                dev->netdev->name, kcqe_op_flag);
916                         goto end;
917                 }
918
919                 rcu_read_lock();
920                 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
921                 if (likely(ulp_ops)) {
922                         ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
923                                                   cp->completed_kcq + i, j);
924                 }
925                 rcu_read_unlock();
926 end:
927                 num_cqes -= j;
928                 i += j;
929                 j = 1;
930         }
931         return;
932 }
933
934 static u16 cnic_bnx2_next_idx(u16 idx)
935 {
936         return idx + 1;
937 }
938
939 static u16 cnic_bnx2_hw_idx(u16 idx)
940 {
941         return idx;
942 }
943
944 static int cnic_get_kcqes(struct cnic_dev *dev, u16 hw_prod, u16 *sw_prod)
945 {
946         struct cnic_local *cp = dev->cnic_priv;
947         u16 i, ri, last;
948         struct kcqe *kcqe;
949         int kcqe_cnt = 0, last_cnt = 0;
950
951         i = ri = last = *sw_prod;
952         ri &= MAX_KCQ_IDX;
953
954         while ((i != hw_prod) && (kcqe_cnt < MAX_COMPLETED_KCQE)) {
955                 kcqe = &cp->kcq[KCQ_PG(ri)][KCQ_IDX(ri)];
956                 cp->completed_kcq[kcqe_cnt++] = kcqe;
957                 i = cp->next_idx(i);
958                 ri = i & MAX_KCQ_IDX;
959                 if (likely(!(kcqe->kcqe_op_flag & KCQE_FLAGS_NEXT))) {
960                         last_cnt = kcqe_cnt;
961                         last = i;
962                 }
963         }
964
965         *sw_prod = last;
966         return last_cnt;
967 }
968
969 static void cnic_chk_bnx2_pkt_rings(struct cnic_local *cp)
970 {
971         u16 rx_cons = *cp->rx_cons_ptr;
972         u16 tx_cons = *cp->tx_cons_ptr;
973
974         if (cp->tx_cons != tx_cons || cp->rx_cons != rx_cons) {
975                 cp->tx_cons = tx_cons;
976                 cp->rx_cons = rx_cons;
977                 uio_event_notify(cp->cnic_uinfo);
978         }
979 }
980
981 static int cnic_service_bnx2(void *data, void *status_blk)
982 {
983         struct cnic_dev *dev = data;
984         struct status_block *sblk = status_blk;
985         struct cnic_local *cp = dev->cnic_priv;
986         u32 status_idx = sblk->status_idx;
987         u16 hw_prod, sw_prod;
988         int kcqe_cnt;
989
990         if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
991                 return status_idx;
992
993         cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
994
995         hw_prod = sblk->status_completion_producer_index;
996         sw_prod = cp->kcq_prod_idx;
997         while (sw_prod != hw_prod) {
998                 kcqe_cnt = cnic_get_kcqes(dev, hw_prod, &sw_prod);
999                 if (kcqe_cnt == 0)
1000                         goto done;
1001
1002                 service_kcqes(dev, kcqe_cnt);
1003
1004                 /* Tell compiler that status_blk fields can change. */
1005                 barrier();
1006                 if (status_idx != sblk->status_idx) {
1007                         status_idx = sblk->status_idx;
1008                         cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
1009                         hw_prod = sblk->status_completion_producer_index;
1010                 } else
1011                         break;
1012         }
1013
1014 done:
1015         CNIC_WR16(dev, cp->kcq_io_addr, sw_prod);
1016
1017         cp->kcq_prod_idx = sw_prod;
1018
1019         cnic_chk_bnx2_pkt_rings(cp);
1020         return status_idx;
1021 }
1022
1023 static void cnic_service_bnx2_msix(unsigned long data)
1024 {
1025         struct cnic_dev *dev = (struct cnic_dev *) data;
1026         struct cnic_local *cp = dev->cnic_priv;
1027         struct status_block_msix *status_blk = cp->bnx2_status_blk;
1028         u32 status_idx = status_blk->status_idx;
1029         u16 hw_prod, sw_prod;
1030         int kcqe_cnt;
1031
1032         cp->kwq_con_idx = status_blk->status_cmd_consumer_index;
1033
1034         hw_prod = status_blk->status_completion_producer_index;
1035         sw_prod = cp->kcq_prod_idx;
1036         while (sw_prod != hw_prod) {
1037                 kcqe_cnt = cnic_get_kcqes(dev, hw_prod, &sw_prod);
1038                 if (kcqe_cnt == 0)
1039                         goto done;
1040
1041                 service_kcqes(dev, kcqe_cnt);
1042
1043                 /* Tell compiler that status_blk fields can change. */
1044                 barrier();
1045                 if (status_idx != status_blk->status_idx) {
1046                         status_idx = status_blk->status_idx;
1047                         cp->kwq_con_idx = status_blk->status_cmd_consumer_index;
1048                         hw_prod = status_blk->status_completion_producer_index;
1049                 } else
1050                         break;
1051         }
1052
1053 done:
1054         CNIC_WR16(dev, cp->kcq_io_addr, sw_prod);
1055         cp->kcq_prod_idx = sw_prod;
1056
1057         cnic_chk_bnx2_pkt_rings(cp);
1058
1059         cp->last_status_idx = status_idx;
1060         CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
1061                 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
1062 }
1063
1064 static irqreturn_t cnic_irq(int irq, void *dev_instance)
1065 {
1066         struct cnic_dev *dev = dev_instance;
1067         struct cnic_local *cp = dev->cnic_priv;
1068         u16 prod = cp->kcq_prod_idx & MAX_KCQ_IDX;
1069
1070         if (cp->ack_int)
1071                 cp->ack_int(dev);
1072
1073         prefetch(cp->status_blk);
1074         prefetch(&cp->kcq[KCQ_PG(prod)][KCQ_IDX(prod)]);
1075
1076         if (likely(test_bit(CNIC_F_CNIC_UP, &dev->flags)))
1077                 tasklet_schedule(&cp->cnic_irq_task);
1078
1079         return IRQ_HANDLED;
1080 }
1081
1082 static void cnic_ulp_stop(struct cnic_dev *dev)
1083 {
1084         struct cnic_local *cp = dev->cnic_priv;
1085         int if_type;
1086
1087         if (cp->cnic_uinfo)
1088                 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
1089
1090         rcu_read_lock();
1091         for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
1092                 struct cnic_ulp_ops *ulp_ops;
1093
1094                 ulp_ops = rcu_dereference(cp->ulp_ops[if_type]);
1095                 if (!ulp_ops)
1096                         continue;
1097
1098                 if (test_and_clear_bit(ULP_F_START, &cp->ulp_flags[if_type]))
1099                         ulp_ops->cnic_stop(cp->ulp_handle[if_type]);
1100         }
1101         rcu_read_unlock();
1102 }
1103
1104 static void cnic_ulp_start(struct cnic_dev *dev)
1105 {
1106         struct cnic_local *cp = dev->cnic_priv;
1107         int if_type;
1108
1109         rcu_read_lock();
1110         for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
1111                 struct cnic_ulp_ops *ulp_ops;
1112
1113                 ulp_ops = rcu_dereference(cp->ulp_ops[if_type]);
1114                 if (!ulp_ops || !ulp_ops->cnic_start)
1115                         continue;
1116
1117                 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[if_type]))
1118                         ulp_ops->cnic_start(cp->ulp_handle[if_type]);
1119         }
1120         rcu_read_unlock();
1121 }
1122
1123 static int cnic_ctl(void *data, struct cnic_ctl_info *info)
1124 {
1125         struct cnic_dev *dev = data;
1126
1127         switch (info->cmd) {
1128         case CNIC_CTL_STOP_CMD:
1129                 cnic_hold(dev);
1130                 mutex_lock(&cnic_lock);
1131
1132                 cnic_ulp_stop(dev);
1133                 cnic_stop_hw(dev);
1134
1135                 mutex_unlock(&cnic_lock);
1136                 cnic_put(dev);
1137                 break;
1138         case CNIC_CTL_START_CMD:
1139                 cnic_hold(dev);
1140                 mutex_lock(&cnic_lock);
1141
1142                 if (!cnic_start_hw(dev))
1143                         cnic_ulp_start(dev);
1144
1145                 mutex_unlock(&cnic_lock);
1146                 cnic_put(dev);
1147                 break;
1148         default:
1149                 return -EINVAL;
1150         }
1151         return 0;
1152 }
1153
1154 static void cnic_ulp_init(struct cnic_dev *dev)
1155 {
1156         int i;
1157         struct cnic_local *cp = dev->cnic_priv;
1158
1159         rcu_read_lock();
1160         for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
1161                 struct cnic_ulp_ops *ulp_ops;
1162
1163                 ulp_ops = rcu_dereference(cnic_ulp_tbl[i]);
1164                 if (!ulp_ops || !ulp_ops->cnic_init)
1165                         continue;
1166
1167                 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[i]))
1168                         ulp_ops->cnic_init(dev);
1169
1170         }
1171         rcu_read_unlock();
1172 }
1173
1174 static void cnic_ulp_exit(struct cnic_dev *dev)
1175 {
1176         int i;
1177         struct cnic_local *cp = dev->cnic_priv;
1178
1179         rcu_read_lock();
1180         for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
1181                 struct cnic_ulp_ops *ulp_ops;
1182
1183                 ulp_ops = rcu_dereference(cnic_ulp_tbl[i]);
1184                 if (!ulp_ops || !ulp_ops->cnic_exit)
1185                         continue;
1186
1187                 if (test_and_clear_bit(ULP_F_INIT, &cp->ulp_flags[i]))
1188                         ulp_ops->cnic_exit(dev);
1189
1190         }
1191         rcu_read_unlock();
1192 }
1193
1194 static int cnic_cm_offload_pg(struct cnic_sock *csk)
1195 {
1196         struct cnic_dev *dev = csk->dev;
1197         struct l4_kwq_offload_pg *l4kwqe;
1198         struct kwqe *wqes[1];
1199
1200         l4kwqe = (struct l4_kwq_offload_pg *) &csk->kwqe1;
1201         memset(l4kwqe, 0, sizeof(*l4kwqe));
1202         wqes[0] = (struct kwqe *) l4kwqe;
1203
1204         l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_OFFLOAD_PG;
1205         l4kwqe->flags =
1206                 L4_LAYER_CODE << L4_KWQ_OFFLOAD_PG_LAYER_CODE_SHIFT;
1207         l4kwqe->l2hdr_nbytes = ETH_HLEN;
1208
1209         l4kwqe->da0 = csk->ha[0];
1210         l4kwqe->da1 = csk->ha[1];
1211         l4kwqe->da2 = csk->ha[2];
1212         l4kwqe->da3 = csk->ha[3];
1213         l4kwqe->da4 = csk->ha[4];
1214         l4kwqe->da5 = csk->ha[5];
1215
1216         l4kwqe->sa0 = dev->mac_addr[0];
1217         l4kwqe->sa1 = dev->mac_addr[1];
1218         l4kwqe->sa2 = dev->mac_addr[2];
1219         l4kwqe->sa3 = dev->mac_addr[3];
1220         l4kwqe->sa4 = dev->mac_addr[4];
1221         l4kwqe->sa5 = dev->mac_addr[5];
1222
1223         l4kwqe->etype = ETH_P_IP;
1224         l4kwqe->ipid_count = DEF_IPID_COUNT;
1225         l4kwqe->host_opaque = csk->l5_cid;
1226
1227         if (csk->vlan_id) {
1228                 l4kwqe->pg_flags |= L4_KWQ_OFFLOAD_PG_VLAN_TAGGING;
1229                 l4kwqe->vlan_tag = csk->vlan_id;
1230                 l4kwqe->l2hdr_nbytes += 4;
1231         }
1232
1233         return dev->submit_kwqes(dev, wqes, 1);
1234 }
1235
1236 static int cnic_cm_update_pg(struct cnic_sock *csk)
1237 {
1238         struct cnic_dev *dev = csk->dev;
1239         struct l4_kwq_update_pg *l4kwqe;
1240         struct kwqe *wqes[1];
1241
1242         l4kwqe = (struct l4_kwq_update_pg *) &csk->kwqe1;
1243         memset(l4kwqe, 0, sizeof(*l4kwqe));
1244         wqes[0] = (struct kwqe *) l4kwqe;
1245
1246         l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPDATE_PG;
1247         l4kwqe->flags =
1248                 L4_LAYER_CODE << L4_KWQ_UPDATE_PG_LAYER_CODE_SHIFT;
1249         l4kwqe->pg_cid = csk->pg_cid;
1250
1251         l4kwqe->da0 = csk->ha[0];
1252         l4kwqe->da1 = csk->ha[1];
1253         l4kwqe->da2 = csk->ha[2];
1254         l4kwqe->da3 = csk->ha[3];
1255         l4kwqe->da4 = csk->ha[4];
1256         l4kwqe->da5 = csk->ha[5];
1257
1258         l4kwqe->pg_host_opaque = csk->l5_cid;
1259         l4kwqe->pg_valids = L4_KWQ_UPDATE_PG_VALIDS_DA;
1260
1261         return dev->submit_kwqes(dev, wqes, 1);
1262 }
1263
1264 static int cnic_cm_upload_pg(struct cnic_sock *csk)
1265 {
1266         struct cnic_dev *dev = csk->dev;
1267         struct l4_kwq_upload *l4kwqe;
1268         struct kwqe *wqes[1];
1269
1270         l4kwqe = (struct l4_kwq_upload *) &csk->kwqe1;
1271         memset(l4kwqe, 0, sizeof(*l4kwqe));
1272         wqes[0] = (struct kwqe *) l4kwqe;
1273
1274         l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPLOAD_PG;
1275         l4kwqe->flags =
1276                 L4_LAYER_CODE << L4_KWQ_UPLOAD_LAYER_CODE_SHIFT;
1277         l4kwqe->cid = csk->pg_cid;
1278
1279         return dev->submit_kwqes(dev, wqes, 1);
1280 }
1281
1282 static int cnic_cm_conn_req(struct cnic_sock *csk)
1283 {
1284         struct cnic_dev *dev = csk->dev;
1285         struct l4_kwq_connect_req1 *l4kwqe1;
1286         struct l4_kwq_connect_req2 *l4kwqe2;
1287         struct l4_kwq_connect_req3 *l4kwqe3;
1288         struct kwqe *wqes[3];
1289         u8 tcp_flags = 0;
1290         int num_wqes = 2;
1291
1292         l4kwqe1 = (struct l4_kwq_connect_req1 *) &csk->kwqe1;
1293         l4kwqe2 = (struct l4_kwq_connect_req2 *) &csk->kwqe2;
1294         l4kwqe3 = (struct l4_kwq_connect_req3 *) &csk->kwqe3;
1295         memset(l4kwqe1, 0, sizeof(*l4kwqe1));
1296         memset(l4kwqe2, 0, sizeof(*l4kwqe2));
1297         memset(l4kwqe3, 0, sizeof(*l4kwqe3));
1298
1299         l4kwqe3->op_code = L4_KWQE_OPCODE_VALUE_CONNECT3;
1300         l4kwqe3->flags =
1301                 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ3_LAYER_CODE_SHIFT;
1302         l4kwqe3->ka_timeout = csk->ka_timeout;
1303         l4kwqe3->ka_interval = csk->ka_interval;
1304         l4kwqe3->ka_max_probe_count = csk->ka_max_probe_count;
1305         l4kwqe3->tos = csk->tos;
1306         l4kwqe3->ttl = csk->ttl;
1307         l4kwqe3->snd_seq_scale = csk->snd_seq_scale;
1308         l4kwqe3->pmtu = csk->mtu;
1309         l4kwqe3->rcv_buf = csk->rcv_buf;
1310         l4kwqe3->snd_buf = csk->snd_buf;
1311         l4kwqe3->seed = csk->seed;
1312
1313         wqes[0] = (struct kwqe *) l4kwqe1;
1314         if (test_bit(SK_F_IPV6, &csk->flags)) {
1315                 wqes[1] = (struct kwqe *) l4kwqe2;
1316                 wqes[2] = (struct kwqe *) l4kwqe3;
1317                 num_wqes = 3;
1318
1319                 l4kwqe1->conn_flags = L4_KWQ_CONNECT_REQ1_IP_V6;
1320                 l4kwqe2->op_code = L4_KWQE_OPCODE_VALUE_CONNECT2;
1321                 l4kwqe2->flags =
1322                         L4_KWQ_CONNECT_REQ2_LINKED_WITH_NEXT |
1323                         L4_LAYER_CODE << L4_KWQ_CONNECT_REQ2_LAYER_CODE_SHIFT;
1324                 l4kwqe2->src_ip_v6_2 = be32_to_cpu(csk->src_ip[1]);
1325                 l4kwqe2->src_ip_v6_3 = be32_to_cpu(csk->src_ip[2]);
1326                 l4kwqe2->src_ip_v6_4 = be32_to_cpu(csk->src_ip[3]);
1327                 l4kwqe2->dst_ip_v6_2 = be32_to_cpu(csk->dst_ip[1]);
1328                 l4kwqe2->dst_ip_v6_3 = be32_to_cpu(csk->dst_ip[2]);
1329                 l4kwqe2->dst_ip_v6_4 = be32_to_cpu(csk->dst_ip[3]);
1330                 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct ipv6hdr) -
1331                                sizeof(struct tcphdr);
1332         } else {
1333                 wqes[1] = (struct kwqe *) l4kwqe3;
1334                 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct iphdr) -
1335                                sizeof(struct tcphdr);
1336         }
1337
1338         l4kwqe1->op_code = L4_KWQE_OPCODE_VALUE_CONNECT1;
1339         l4kwqe1->flags =
1340                 (L4_LAYER_CODE << L4_KWQ_CONNECT_REQ1_LAYER_CODE_SHIFT) |
1341                  L4_KWQ_CONNECT_REQ3_LINKED_WITH_NEXT;
1342         l4kwqe1->cid = csk->cid;
1343         l4kwqe1->pg_cid = csk->pg_cid;
1344         l4kwqe1->src_ip = be32_to_cpu(csk->src_ip[0]);
1345         l4kwqe1->dst_ip = be32_to_cpu(csk->dst_ip[0]);
1346         l4kwqe1->src_port = be16_to_cpu(csk->src_port);
1347         l4kwqe1->dst_port = be16_to_cpu(csk->dst_port);
1348         if (csk->tcp_flags & SK_TCP_NO_DELAY_ACK)
1349                 tcp_flags |= L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK;
1350         if (csk->tcp_flags & SK_TCP_KEEP_ALIVE)
1351                 tcp_flags |= L4_KWQ_CONNECT_REQ1_KEEP_ALIVE;
1352         if (csk->tcp_flags & SK_TCP_NAGLE)
1353                 tcp_flags |= L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE;
1354         if (csk->tcp_flags & SK_TCP_TIMESTAMP)
1355                 tcp_flags |= L4_KWQ_CONNECT_REQ1_TIME_STAMP;
1356         if (csk->tcp_flags & SK_TCP_SACK)
1357                 tcp_flags |= L4_KWQ_CONNECT_REQ1_SACK;
1358         if (csk->tcp_flags & SK_TCP_SEG_SCALING)
1359                 tcp_flags |= L4_KWQ_CONNECT_REQ1_SEG_SCALING;
1360
1361         l4kwqe1->tcp_flags = tcp_flags;
1362
1363         return dev->submit_kwqes(dev, wqes, num_wqes);
1364 }
1365
1366 static int cnic_cm_close_req(struct cnic_sock *csk)
1367 {
1368         struct cnic_dev *dev = csk->dev;
1369         struct l4_kwq_close_req *l4kwqe;
1370         struct kwqe *wqes[1];
1371
1372         l4kwqe = (struct l4_kwq_close_req *) &csk->kwqe2;
1373         memset(l4kwqe, 0, sizeof(*l4kwqe));
1374         wqes[0] = (struct kwqe *) l4kwqe;
1375
1376         l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_CLOSE;
1377         l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_CLOSE_REQ_LAYER_CODE_SHIFT;
1378         l4kwqe->cid = csk->cid;
1379
1380         return dev->submit_kwqes(dev, wqes, 1);
1381 }
1382
1383 static int cnic_cm_abort_req(struct cnic_sock *csk)
1384 {
1385         struct cnic_dev *dev = csk->dev;
1386         struct l4_kwq_reset_req *l4kwqe;
1387         struct kwqe *wqes[1];
1388
1389         l4kwqe = (struct l4_kwq_reset_req *) &csk->kwqe2;
1390         memset(l4kwqe, 0, sizeof(*l4kwqe));
1391         wqes[0] = (struct kwqe *) l4kwqe;
1392
1393         l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_RESET;
1394         l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_RESET_REQ_LAYER_CODE_SHIFT;
1395         l4kwqe->cid = csk->cid;
1396
1397         return dev->submit_kwqes(dev, wqes, 1);
1398 }
1399
1400 static int cnic_cm_create(struct cnic_dev *dev, int ulp_type, u32 cid,
1401                           u32 l5_cid, struct cnic_sock **csk, void *context)
1402 {
1403         struct cnic_local *cp = dev->cnic_priv;
1404         struct cnic_sock *csk1;
1405
1406         if (l5_cid >= MAX_CM_SK_TBL_SZ)
1407                 return -EINVAL;
1408
1409         csk1 = &cp->csk_tbl[l5_cid];
1410         if (atomic_read(&csk1->ref_count))
1411                 return -EAGAIN;
1412
1413         if (test_and_set_bit(SK_F_INUSE, &csk1->flags))
1414                 return -EBUSY;
1415
1416         csk1->dev = dev;
1417         csk1->cid = cid;
1418         csk1->l5_cid = l5_cid;
1419         csk1->ulp_type = ulp_type;
1420         csk1->context = context;
1421
1422         csk1->ka_timeout = DEF_KA_TIMEOUT;
1423         csk1->ka_interval = DEF_KA_INTERVAL;
1424         csk1->ka_max_probe_count = DEF_KA_MAX_PROBE_COUNT;
1425         csk1->tos = DEF_TOS;
1426         csk1->ttl = DEF_TTL;
1427         csk1->snd_seq_scale = DEF_SND_SEQ_SCALE;
1428         csk1->rcv_buf = DEF_RCV_BUF;
1429         csk1->snd_buf = DEF_SND_BUF;
1430         csk1->seed = DEF_SEED;
1431
1432         *csk = csk1;
1433         return 0;
1434 }
1435
1436 static void cnic_cm_cleanup(struct cnic_sock *csk)
1437 {
1438         if (csk->src_port) {
1439                 struct cnic_dev *dev = csk->dev;
1440                 struct cnic_local *cp = dev->cnic_priv;
1441
1442                 cnic_free_id(&cp->csk_port_tbl, csk->src_port);
1443                 csk->src_port = 0;
1444         }
1445 }
1446
1447 static void cnic_close_conn(struct cnic_sock *csk)
1448 {
1449         if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags)) {
1450                 cnic_cm_upload_pg(csk);
1451                 clear_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
1452         }
1453         cnic_cm_cleanup(csk);
1454 }
1455
1456 static int cnic_cm_destroy(struct cnic_sock *csk)
1457 {
1458         if (!cnic_in_use(csk))
1459                 return -EINVAL;
1460
1461         csk_hold(csk);
1462         clear_bit(SK_F_INUSE, &csk->flags);
1463         smp_mb__after_clear_bit();
1464         while (atomic_read(&csk->ref_count) != 1)
1465                 msleep(1);
1466         cnic_cm_cleanup(csk);
1467
1468         csk->flags = 0;
1469         csk_put(csk);
1470         return 0;
1471 }
1472
1473 static inline u16 cnic_get_vlan(struct net_device *dev,
1474                                 struct net_device **vlan_dev)
1475 {
1476         if (dev->priv_flags & IFF_802_1Q_VLAN) {
1477                 *vlan_dev = vlan_dev_real_dev(dev);
1478                 return vlan_dev_vlan_id(dev);
1479         }
1480         *vlan_dev = dev;
1481         return 0;
1482 }
1483
1484 static int cnic_get_v4_route(struct sockaddr_in *dst_addr,
1485                              struct dst_entry **dst)
1486 {
1487 #if defined(CONFIG_INET)
1488         struct flowi fl;
1489         int err;
1490         struct rtable *rt;
1491
1492         memset(&fl, 0, sizeof(fl));
1493         fl.nl_u.ip4_u.daddr = dst_addr->sin_addr.s_addr;
1494
1495         err = ip_route_output_key(&init_net, &rt, &fl);
1496         if (!err)
1497                 *dst = &rt->u.dst;
1498         return err;
1499 #else
1500         return -ENETUNREACH;
1501 #endif
1502 }
1503
1504 static int cnic_get_v6_route(struct sockaddr_in6 *dst_addr,
1505                              struct dst_entry **dst)
1506 {
1507 #if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
1508         struct flowi fl;
1509
1510         memset(&fl, 0, sizeof(fl));
1511         ipv6_addr_copy(&fl.fl6_dst, &dst_addr->sin6_addr);
1512         if (ipv6_addr_type(&fl.fl6_dst) & IPV6_ADDR_LINKLOCAL)
1513                 fl.oif = dst_addr->sin6_scope_id;
1514
1515         *dst = ip6_route_output(&init_net, NULL, &fl);
1516         if (*dst)
1517                 return 0;
1518 #endif
1519
1520         return -ENETUNREACH;
1521 }
1522
1523 static struct cnic_dev *cnic_cm_select_dev(struct sockaddr_in *dst_addr,
1524                                            int ulp_type)
1525 {
1526         struct cnic_dev *dev = NULL;
1527         struct dst_entry *dst;
1528         struct net_device *netdev = NULL;
1529         int err = -ENETUNREACH;
1530
1531         if (dst_addr->sin_family == AF_INET)
1532                 err = cnic_get_v4_route(dst_addr, &dst);
1533         else if (dst_addr->sin_family == AF_INET6) {
1534                 struct sockaddr_in6 *dst_addr6 =
1535                         (struct sockaddr_in6 *) dst_addr;
1536
1537                 err = cnic_get_v6_route(dst_addr6, &dst);
1538         } else
1539                 return NULL;
1540
1541         if (err)
1542                 return NULL;
1543
1544         if (!dst->dev)
1545                 goto done;
1546
1547         cnic_get_vlan(dst->dev, &netdev);
1548
1549         dev = cnic_from_netdev(netdev);
1550
1551 done:
1552         dst_release(dst);
1553         if (dev)
1554                 cnic_put(dev);
1555         return dev;
1556 }
1557
1558 static int cnic_resolve_addr(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
1559 {
1560         struct cnic_dev *dev = csk->dev;
1561         struct cnic_local *cp = dev->cnic_priv;
1562
1563         return cnic_send_nlmsg(cp, ISCSI_KEVENT_PATH_REQ, csk);
1564 }
1565
1566 static int cnic_get_route(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
1567 {
1568         struct cnic_dev *dev = csk->dev;
1569         struct cnic_local *cp = dev->cnic_priv;
1570         int is_v6, err, rc = -ENETUNREACH;
1571         struct dst_entry *dst;
1572         struct net_device *realdev;
1573         u32 local_port;
1574
1575         if (saddr->local.v6.sin6_family == AF_INET6 &&
1576             saddr->remote.v6.sin6_family == AF_INET6)
1577                 is_v6 = 1;
1578         else if (saddr->local.v4.sin_family == AF_INET &&
1579                  saddr->remote.v4.sin_family == AF_INET)
1580                 is_v6 = 0;
1581         else
1582                 return -EINVAL;
1583
1584         clear_bit(SK_F_IPV6, &csk->flags);
1585
1586         if (is_v6) {
1587 #if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
1588                 set_bit(SK_F_IPV6, &csk->flags);
1589                 err = cnic_get_v6_route(&saddr->remote.v6, &dst);
1590                 if (err)
1591                         return err;
1592
1593                 if (!dst || dst->error || !dst->dev)
1594                         goto err_out;
1595
1596                 memcpy(&csk->dst_ip[0], &saddr->remote.v6.sin6_addr,
1597                        sizeof(struct in6_addr));
1598                 csk->dst_port = saddr->remote.v6.sin6_port;
1599                 local_port = saddr->local.v6.sin6_port;
1600 #else
1601                 return rc;
1602 #endif
1603
1604         } else {
1605                 err = cnic_get_v4_route(&saddr->remote.v4, &dst);
1606                 if (err)
1607                         return err;
1608
1609                 if (!dst || dst->error || !dst->dev)
1610                         goto err_out;
1611
1612                 csk->dst_ip[0] = saddr->remote.v4.sin_addr.s_addr;
1613                 csk->dst_port = saddr->remote.v4.sin_port;
1614                 local_port = saddr->local.v4.sin_port;
1615         }
1616
1617         csk->vlan_id = cnic_get_vlan(dst->dev, &realdev);
1618         if (realdev != dev->netdev)
1619                 goto err_out;
1620
1621         if (local_port >= CNIC_LOCAL_PORT_MIN &&
1622             local_port < CNIC_LOCAL_PORT_MAX) {
1623                 if (cnic_alloc_id(&cp->csk_port_tbl, local_port))
1624                         local_port = 0;
1625         } else
1626                 local_port = 0;
1627
1628         if (!local_port) {
1629                 local_port = cnic_alloc_new_id(&cp->csk_port_tbl);
1630                 if (local_port == -1) {
1631                         rc = -ENOMEM;
1632                         goto err_out;
1633                 }
1634         }
1635         csk->src_port = local_port;
1636
1637         csk->mtu = dst_mtu(dst);
1638         rc = 0;
1639
1640 err_out:
1641         dst_release(dst);
1642         return rc;
1643 }
1644
1645 static void cnic_init_csk_state(struct cnic_sock *csk)
1646 {
1647         csk->state = 0;
1648         clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
1649         clear_bit(SK_F_CLOSING, &csk->flags);
1650 }
1651
1652 static int cnic_cm_connect(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
1653 {
1654         int err = 0;
1655
1656         if (!cnic_in_use(csk))
1657                 return -EINVAL;
1658
1659         if (test_and_set_bit(SK_F_CONNECT_START, &csk->flags))
1660                 return -EINVAL;
1661
1662         cnic_init_csk_state(csk);
1663
1664         err = cnic_get_route(csk, saddr);
1665         if (err)
1666                 goto err_out;
1667
1668         err = cnic_resolve_addr(csk, saddr);
1669         if (!err)
1670                 return 0;
1671
1672 err_out:
1673         clear_bit(SK_F_CONNECT_START, &csk->flags);
1674         return err;
1675 }
1676
1677 static int cnic_cm_abort(struct cnic_sock *csk)
1678 {
1679         struct cnic_local *cp = csk->dev->cnic_priv;
1680         u32 opcode;
1681
1682         if (!cnic_in_use(csk))
1683                 return -EINVAL;
1684
1685         if (cnic_abort_prep(csk))
1686                 return cnic_cm_abort_req(csk);
1687
1688         /* Getting here means that we haven't started connect, or
1689          * connect was not successful.
1690          */
1691
1692         csk->state = L4_KCQE_OPCODE_VALUE_RESET_COMP;
1693         if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
1694                 opcode = csk->state;
1695         else
1696                 opcode = L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD;
1697         cp->close_conn(csk, opcode);
1698
1699         return 0;
1700 }
1701
1702 static int cnic_cm_close(struct cnic_sock *csk)
1703 {
1704         if (!cnic_in_use(csk))
1705                 return -EINVAL;
1706
1707         if (cnic_close_prep(csk)) {
1708                 csk->state = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
1709                 return cnic_cm_close_req(csk);
1710         }
1711         return 0;
1712 }
1713
1714 static void cnic_cm_upcall(struct cnic_local *cp, struct cnic_sock *csk,
1715                            u8 opcode)
1716 {
1717         struct cnic_ulp_ops *ulp_ops;
1718         int ulp_type = csk->ulp_type;
1719
1720         rcu_read_lock();
1721         ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
1722         if (ulp_ops) {
1723                 if (opcode == L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE)
1724                         ulp_ops->cm_connect_complete(csk);
1725                 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
1726                         ulp_ops->cm_close_complete(csk);
1727                 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED)
1728                         ulp_ops->cm_remote_abort(csk);
1729                 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_COMP)
1730                         ulp_ops->cm_abort_complete(csk);
1731                 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED)
1732                         ulp_ops->cm_remote_close(csk);
1733         }
1734         rcu_read_unlock();
1735 }
1736
1737 static int cnic_cm_set_pg(struct cnic_sock *csk)
1738 {
1739         if (cnic_offld_prep(csk)) {
1740                 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
1741                         cnic_cm_update_pg(csk);
1742                 else
1743                         cnic_cm_offload_pg(csk);
1744         }
1745         return 0;
1746 }
1747
1748 static void cnic_cm_process_offld_pg(struct cnic_dev *dev, struct l4_kcq *kcqe)
1749 {
1750         struct cnic_local *cp = dev->cnic_priv;
1751         u32 l5_cid = kcqe->pg_host_opaque;
1752         u8 opcode = kcqe->op_code;
1753         struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
1754
1755         csk_hold(csk);
1756         if (!cnic_in_use(csk))
1757                 goto done;
1758
1759         if (opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
1760                 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
1761                 goto done;
1762         }
1763         csk->pg_cid = kcqe->pg_cid;
1764         set_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
1765         cnic_cm_conn_req(csk);
1766
1767 done:
1768         csk_put(csk);
1769 }
1770
1771 static void cnic_cm_process_kcqe(struct cnic_dev *dev, struct kcqe *kcqe)
1772 {
1773         struct cnic_local *cp = dev->cnic_priv;
1774         struct l4_kcq *l4kcqe = (struct l4_kcq *) kcqe;
1775         u8 opcode = l4kcqe->op_code;
1776         u32 l5_cid;
1777         struct cnic_sock *csk;
1778
1779         if (opcode == L4_KCQE_OPCODE_VALUE_OFFLOAD_PG ||
1780             opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
1781                 cnic_cm_process_offld_pg(dev, l4kcqe);
1782                 return;
1783         }
1784
1785         l5_cid = l4kcqe->conn_id;
1786         if (opcode & 0x80)
1787                 l5_cid = l4kcqe->cid;
1788         if (l5_cid >= MAX_CM_SK_TBL_SZ)
1789                 return;
1790
1791         csk = &cp->csk_tbl[l5_cid];
1792         csk_hold(csk);
1793
1794         if (!cnic_in_use(csk)) {
1795                 csk_put(csk);
1796                 return;
1797         }
1798
1799         switch (opcode) {
1800         case L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE:
1801                 if (l4kcqe->status == 0)
1802                         set_bit(SK_F_OFFLD_COMPLETE, &csk->flags);
1803
1804                 smp_mb__before_clear_bit();
1805                 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
1806                 cnic_cm_upcall(cp, csk, opcode);
1807                 break;
1808
1809         case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
1810                 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags))
1811                         csk->state = opcode;
1812                 /* fall through */
1813         case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
1814         case L4_KCQE_OPCODE_VALUE_RESET_COMP:
1815                 cp->close_conn(csk, opcode);
1816                 break;
1817
1818         case L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED:
1819                 cnic_cm_upcall(cp, csk, opcode);
1820                 break;
1821         }
1822         csk_put(csk);
1823 }
1824
1825 static void cnic_cm_indicate_kcqe(void *data, struct kcqe *kcqe[], u32 num)
1826 {
1827         struct cnic_dev *dev = data;
1828         int i;
1829
1830         for (i = 0; i < num; i++)
1831                 cnic_cm_process_kcqe(dev, kcqe[i]);
1832 }
1833
1834 static struct cnic_ulp_ops cm_ulp_ops = {
1835         .indicate_kcqes         = cnic_cm_indicate_kcqe,
1836 };
1837
1838 static void cnic_cm_free_mem(struct cnic_dev *dev)
1839 {
1840         struct cnic_local *cp = dev->cnic_priv;
1841
1842         kfree(cp->csk_tbl);
1843         cp->csk_tbl = NULL;
1844         cnic_free_id_tbl(&cp->csk_port_tbl);
1845 }
1846
1847 static int cnic_cm_alloc_mem(struct cnic_dev *dev)
1848 {
1849         struct cnic_local *cp = dev->cnic_priv;
1850
1851         cp->csk_tbl = kzalloc(sizeof(struct cnic_sock) * MAX_CM_SK_TBL_SZ,
1852                               GFP_KERNEL);
1853         if (!cp->csk_tbl)
1854                 return -ENOMEM;
1855
1856         if (cnic_init_id_tbl(&cp->csk_port_tbl, CNIC_LOCAL_PORT_RANGE,
1857                              CNIC_LOCAL_PORT_MIN)) {
1858                 cnic_cm_free_mem(dev);
1859                 return -ENOMEM;
1860         }
1861         return 0;
1862 }
1863
1864 static int cnic_ready_to_close(struct cnic_sock *csk, u32 opcode)
1865 {
1866         if ((opcode == csk->state) ||
1867             (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED &&
1868              csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)) {
1869                 if (!test_and_set_bit(SK_F_CLOSING, &csk->flags))
1870                         return 1;
1871         }
1872         return 0;
1873 }
1874
1875 static void cnic_close_bnx2_conn(struct cnic_sock *csk, u32 opcode)
1876 {
1877         struct cnic_dev *dev = csk->dev;
1878         struct cnic_local *cp = dev->cnic_priv;
1879
1880         clear_bit(SK_F_CONNECT_START, &csk->flags);
1881         if (cnic_ready_to_close(csk, opcode)) {
1882                 cnic_close_conn(csk);
1883                 cnic_cm_upcall(cp, csk, opcode);
1884         }
1885 }
1886
1887 static void cnic_cm_stop_bnx2_hw(struct cnic_dev *dev)
1888 {
1889 }
1890
1891 static int cnic_cm_init_bnx2_hw(struct cnic_dev *dev)
1892 {
1893         u32 seed;
1894
1895         get_random_bytes(&seed, 4);
1896         cnic_ctx_wr(dev, 45, 0, seed);
1897         return 0;
1898 }
1899
1900 static int cnic_cm_open(struct cnic_dev *dev)
1901 {
1902         struct cnic_local *cp = dev->cnic_priv;
1903         int err;
1904
1905         err = cnic_cm_alloc_mem(dev);
1906         if (err)
1907                 return err;
1908
1909         err = cp->start_cm(dev);
1910
1911         if (err)
1912                 goto err_out;
1913
1914         dev->cm_create = cnic_cm_create;
1915         dev->cm_destroy = cnic_cm_destroy;
1916         dev->cm_connect = cnic_cm_connect;
1917         dev->cm_abort = cnic_cm_abort;
1918         dev->cm_close = cnic_cm_close;
1919         dev->cm_select_dev = cnic_cm_select_dev;
1920
1921         cp->ulp_handle[CNIC_ULP_L4] = dev;
1922         rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], &cm_ulp_ops);
1923         return 0;
1924
1925 err_out:
1926         cnic_cm_free_mem(dev);
1927         return err;
1928 }
1929
1930 static int cnic_cm_shutdown(struct cnic_dev *dev)
1931 {
1932         struct cnic_local *cp = dev->cnic_priv;
1933         int i;
1934
1935         cp->stop_cm(dev);
1936
1937         if (!cp->csk_tbl)
1938                 return 0;
1939
1940         for (i = 0; i < MAX_CM_SK_TBL_SZ; i++) {
1941                 struct cnic_sock *csk = &cp->csk_tbl[i];
1942
1943                 clear_bit(SK_F_INUSE, &csk->flags);
1944                 cnic_cm_cleanup(csk);
1945         }
1946         cnic_cm_free_mem(dev);
1947
1948         return 0;
1949 }
1950
1951 static void cnic_init_context(struct cnic_dev *dev, u32 cid)
1952 {
1953         struct cnic_local *cp = dev->cnic_priv;
1954         u32 cid_addr;
1955         int i;
1956
1957         if (CHIP_NUM(cp) == CHIP_NUM_5709)
1958                 return;
1959
1960         cid_addr = GET_CID_ADDR(cid);
1961
1962         for (i = 0; i < CTX_SIZE; i += 4)
1963                 cnic_ctx_wr(dev, cid_addr, i, 0);
1964 }
1965
1966 static int cnic_setup_5709_context(struct cnic_dev *dev, int valid)
1967 {
1968         struct cnic_local *cp = dev->cnic_priv;
1969         int ret = 0, i;
1970         u32 valid_bit = valid ? BNX2_CTX_HOST_PAGE_TBL_DATA0_VALID : 0;
1971
1972         if (CHIP_NUM(cp) != CHIP_NUM_5709)
1973                 return 0;
1974
1975         for (i = 0; i < cp->ctx_blks; i++) {
1976                 int j;
1977                 u32 idx = cp->ctx_arr[i].cid / cp->cids_per_blk;
1978                 u32 val;
1979
1980                 memset(cp->ctx_arr[i].ctx, 0, BCM_PAGE_SIZE);
1981
1982                 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA0,
1983                         (cp->ctx_arr[i].mapping & 0xffffffff) | valid_bit);
1984                 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA1,
1985                         (u64) cp->ctx_arr[i].mapping >> 32);
1986                 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL, idx |
1987                         BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
1988                 for (j = 0; j < 10; j++) {
1989
1990                         val = CNIC_RD(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL);
1991                         if (!(val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ))
1992                                 break;
1993                         udelay(5);
1994                 }
1995                 if (val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) {
1996                         ret = -EBUSY;
1997                         break;
1998                 }
1999         }
2000         return ret;
2001 }
2002
2003 static void cnic_free_irq(struct cnic_dev *dev)
2004 {
2005         struct cnic_local *cp = dev->cnic_priv;
2006         struct cnic_eth_dev *ethdev = cp->ethdev;
2007
2008         if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
2009                 cp->disable_int_sync(dev);
2010                 tasklet_disable(&cp->cnic_irq_task);
2011                 free_irq(ethdev->irq_arr[0].vector, dev);
2012         }
2013 }
2014
2015 static int cnic_init_bnx2_irq(struct cnic_dev *dev)
2016 {
2017         struct cnic_local *cp = dev->cnic_priv;
2018         struct cnic_eth_dev *ethdev = cp->ethdev;
2019
2020         if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
2021                 int err, i = 0;
2022                 int sblk_num = cp->status_blk_num;
2023                 u32 base = ((sblk_num - 1) * BNX2_HC_SB_CONFIG_SIZE) +
2024                            BNX2_HC_SB_CONFIG_1;
2025
2026                 CNIC_WR(dev, base, BNX2_HC_SB_CONFIG_1_ONE_SHOT);
2027
2028                 CNIC_WR(dev, base + BNX2_HC_COMP_PROD_TRIP_OFF, (2 << 16) | 8);
2029                 CNIC_WR(dev, base + BNX2_HC_COM_TICKS_OFF, (64 << 16) | 220);
2030                 CNIC_WR(dev, base + BNX2_HC_CMD_TICKS_OFF, (64 << 16) | 220);
2031
2032                 cp->bnx2_status_blk = cp->status_blk;
2033                 cp->last_status_idx = cp->bnx2_status_blk->status_idx;
2034                 tasklet_init(&cp->cnic_irq_task, &cnic_service_bnx2_msix,
2035                              (unsigned long) dev);
2036                 err = request_irq(ethdev->irq_arr[0].vector, cnic_irq, 0,
2037                                   "cnic", dev);
2038                 if (err) {
2039                         tasklet_disable(&cp->cnic_irq_task);
2040                         return err;
2041                 }
2042                 while (cp->bnx2_status_blk->status_completion_producer_index &&
2043                        i < 10) {
2044                         CNIC_WR(dev, BNX2_HC_COALESCE_NOW,
2045                                 1 << (11 + sblk_num));
2046                         udelay(10);
2047                         i++;
2048                         barrier();
2049                 }
2050                 if (cp->bnx2_status_blk->status_completion_producer_index) {
2051                         cnic_free_irq(dev);
2052                         goto failed;
2053                 }
2054
2055         } else {
2056                 struct status_block *sblk = cp->status_blk;
2057                 u32 hc_cmd = CNIC_RD(dev, BNX2_HC_COMMAND);
2058                 int i = 0;
2059
2060                 while (sblk->status_completion_producer_index && i < 10) {
2061                         CNIC_WR(dev, BNX2_HC_COMMAND,
2062                                 hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
2063                         udelay(10);
2064                         i++;
2065                         barrier();
2066                 }
2067                 if (sblk->status_completion_producer_index)
2068                         goto failed;
2069
2070         }
2071         return 0;
2072
2073 failed:
2074         printk(KERN_ERR PFX "%s: " "KCQ index not resetting to 0.\n",
2075                dev->netdev->name);
2076         return -EBUSY;
2077 }
2078
2079 static void cnic_enable_bnx2_int(struct cnic_dev *dev)
2080 {
2081         struct cnic_local *cp = dev->cnic_priv;
2082         struct cnic_eth_dev *ethdev = cp->ethdev;
2083
2084         if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
2085                 return;
2086
2087         CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
2088                 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
2089 }
2090
2091 static void cnic_disable_bnx2_int_sync(struct cnic_dev *dev)
2092 {
2093         struct cnic_local *cp = dev->cnic_priv;
2094         struct cnic_eth_dev *ethdev = cp->ethdev;
2095
2096         if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
2097                 return;
2098
2099         CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
2100                 BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
2101         CNIC_RD(dev, BNX2_PCICFG_INT_ACK_CMD);
2102         synchronize_irq(ethdev->irq_arr[0].vector);
2103 }
2104
2105 static void cnic_init_bnx2_tx_ring(struct cnic_dev *dev)
2106 {
2107         struct cnic_local *cp = dev->cnic_priv;
2108         struct cnic_eth_dev *ethdev = cp->ethdev;
2109         u32 cid_addr, tx_cid, sb_id;
2110         u32 val, offset0, offset1, offset2, offset3;
2111         int i;
2112         struct tx_bd *txbd;
2113         dma_addr_t buf_map;
2114         struct status_block *s_blk = cp->status_blk;
2115
2116         sb_id = cp->status_blk_num;
2117         tx_cid = 20;
2118         cnic_init_context(dev, tx_cid);
2119         cnic_init_context(dev, tx_cid + 1);
2120         cp->tx_cons_ptr = &s_blk->status_tx_quick_consumer_index2;
2121         if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
2122                 struct status_block_msix *sblk = cp->status_blk;
2123
2124                 tx_cid = TX_TSS_CID + sb_id - 1;
2125                 cnic_init_context(dev, tx_cid);
2126                 CNIC_WR(dev, BNX2_TSCH_TSS_CFG, (sb_id << 24) |
2127                         (TX_TSS_CID << 7));
2128                 cp->tx_cons_ptr = &sblk->status_tx_quick_consumer_index;
2129         }
2130         cp->tx_cons = *cp->tx_cons_ptr;
2131
2132         cid_addr = GET_CID_ADDR(tx_cid);
2133         if (CHIP_NUM(cp) == CHIP_NUM_5709) {
2134                 u32 cid_addr2 = GET_CID_ADDR(tx_cid + 4) + 0x40;
2135
2136                 for (i = 0; i < PHY_CTX_SIZE; i += 4)
2137                         cnic_ctx_wr(dev, cid_addr2, i, 0);
2138
2139                 offset0 = BNX2_L2CTX_TYPE_XI;
2140                 offset1 = BNX2_L2CTX_CMD_TYPE_XI;
2141                 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI_XI;
2142                 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO_XI;
2143         } else {
2144                 offset0 = BNX2_L2CTX_TYPE;
2145                 offset1 = BNX2_L2CTX_CMD_TYPE;
2146                 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI;
2147                 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO;
2148         }
2149         val = BNX2_L2CTX_TYPE_TYPE_L2 | BNX2_L2CTX_TYPE_SIZE_L2;
2150         cnic_ctx_wr(dev, cid_addr, offset0, val);
2151
2152         val = BNX2_L2CTX_CMD_TYPE_TYPE_L2 | (8 << 16);
2153         cnic_ctx_wr(dev, cid_addr, offset1, val);
2154
2155         txbd = (struct tx_bd *) cp->l2_ring;
2156
2157         buf_map = cp->l2_buf_map;
2158         for (i = 0; i < MAX_TX_DESC_CNT; i++, txbd++) {
2159                 txbd->tx_bd_haddr_hi = (u64) buf_map >> 32;
2160                 txbd->tx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
2161         }
2162         val = (u64) cp->l2_ring_map >> 32;
2163         cnic_ctx_wr(dev, cid_addr, offset2, val);
2164         txbd->tx_bd_haddr_hi = val;
2165
2166         val = (u64) cp->l2_ring_map & 0xffffffff;
2167         cnic_ctx_wr(dev, cid_addr, offset3, val);
2168         txbd->tx_bd_haddr_lo = val;
2169 }
2170
2171 static void cnic_init_bnx2_rx_ring(struct cnic_dev *dev)
2172 {
2173         struct cnic_local *cp = dev->cnic_priv;
2174         struct cnic_eth_dev *ethdev = cp->ethdev;
2175         u32 cid_addr, sb_id, val, coal_reg, coal_val;
2176         int i;
2177         struct rx_bd *rxbd;
2178         struct status_block *s_blk = cp->status_blk;
2179
2180         sb_id = cp->status_blk_num;
2181         cnic_init_context(dev, 2);
2182         cp->rx_cons_ptr = &s_blk->status_rx_quick_consumer_index2;
2183         coal_reg = BNX2_HC_COMMAND;
2184         coal_val = CNIC_RD(dev, coal_reg);
2185         if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
2186                 struct status_block_msix *sblk = cp->status_blk;
2187
2188                 cp->rx_cons_ptr = &sblk->status_rx_quick_consumer_index;
2189                 coal_reg = BNX2_HC_COALESCE_NOW;
2190                 coal_val = 1 << (11 + sb_id);
2191         }
2192         i = 0;
2193         while (!(*cp->rx_cons_ptr != 0) && i < 10) {
2194                 CNIC_WR(dev, coal_reg, coal_val);
2195                 udelay(10);
2196                 i++;
2197                 barrier();
2198         }
2199         cp->rx_cons = *cp->rx_cons_ptr;
2200
2201         cid_addr = GET_CID_ADDR(2);
2202         val = BNX2_L2CTX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
2203               BNX2_L2CTX_CTX_TYPE_SIZE_L2 | (0x02 << 8);
2204         cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_CTX_TYPE, val);
2205
2206         if (sb_id == 0)
2207                 val = 2 << BNX2_L2CTX_STATUSB_NUM_SHIFT;
2208         else
2209                 val = BNX2_L2CTX_STATUSB_NUM(sb_id);
2210         cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_HOST_BDIDX, val);
2211
2212         rxbd = (struct rx_bd *) (cp->l2_ring + BCM_PAGE_SIZE);
2213         for (i = 0; i < MAX_RX_DESC_CNT; i++, rxbd++) {
2214                 dma_addr_t buf_map;
2215                 int n = (i % cp->l2_rx_ring_size) + 1;
2216
2217                 buf_map = cp->l2_buf_map + (n * cp->l2_single_buf_size);
2218                 rxbd->rx_bd_len = cp->l2_single_buf_size;
2219                 rxbd->rx_bd_flags = RX_BD_FLAGS_START | RX_BD_FLAGS_END;
2220                 rxbd->rx_bd_haddr_hi = (u64) buf_map >> 32;
2221                 rxbd->rx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
2222         }
2223         val = (u64) (cp->l2_ring_map + BCM_PAGE_SIZE) >> 32;
2224         cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_HI, val);
2225         rxbd->rx_bd_haddr_hi = val;
2226
2227         val = (u64) (cp->l2_ring_map + BCM_PAGE_SIZE) & 0xffffffff;
2228         cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_LO, val);
2229         rxbd->rx_bd_haddr_lo = val;
2230
2231         val = cnic_reg_rd_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD);
2232         cnic_reg_wr_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD, val | (1 << 2));
2233 }
2234
2235 static void cnic_shutdown_bnx2_rx_ring(struct cnic_dev *dev)
2236 {
2237         struct kwqe *wqes[1], l2kwqe;
2238
2239         memset(&l2kwqe, 0, sizeof(l2kwqe));
2240         wqes[0] = &l2kwqe;
2241         l2kwqe.kwqe_op_flag = (L2_LAYER_CODE << KWQE_FLAGS_LAYER_SHIFT) |
2242                               (L2_KWQE_OPCODE_VALUE_FLUSH <<
2243                                KWQE_OPCODE_SHIFT) | 2;
2244         dev->submit_kwqes(dev, wqes, 1);
2245 }
2246
2247 static void cnic_set_bnx2_mac(struct cnic_dev *dev)
2248 {
2249         struct cnic_local *cp = dev->cnic_priv;
2250         u32 val;
2251
2252         val = cp->func << 2;
2253
2254         cp->shmem_base = cnic_reg_rd_ind(dev, BNX2_SHM_HDR_ADDR_0 + val);
2255
2256         val = cnic_reg_rd_ind(dev, cp->shmem_base +
2257                               BNX2_PORT_HW_CFG_ISCSI_MAC_UPPER);
2258         dev->mac_addr[0] = (u8) (val >> 8);
2259         dev->mac_addr[1] = (u8) val;
2260
2261         CNIC_WR(dev, BNX2_EMAC_MAC_MATCH4, val);
2262
2263         val = cnic_reg_rd_ind(dev, cp->shmem_base +
2264                               BNX2_PORT_HW_CFG_ISCSI_MAC_LOWER);
2265         dev->mac_addr[2] = (u8) (val >> 24);
2266         dev->mac_addr[3] = (u8) (val >> 16);
2267         dev->mac_addr[4] = (u8) (val >> 8);
2268         dev->mac_addr[5] = (u8) val;
2269
2270         CNIC_WR(dev, BNX2_EMAC_MAC_MATCH5, val);
2271
2272         val = 4 | BNX2_RPM_SORT_USER2_BC_EN;
2273         if (CHIP_NUM(cp) != CHIP_NUM_5709)
2274                 val |= BNX2_RPM_SORT_USER2_PROM_VLAN;
2275
2276         CNIC_WR(dev, BNX2_RPM_SORT_USER2, 0x0);
2277         CNIC_WR(dev, BNX2_RPM_SORT_USER2, val);
2278         CNIC_WR(dev, BNX2_RPM_SORT_USER2, val | BNX2_RPM_SORT_USER2_ENA);
2279 }
2280
2281 static int cnic_start_bnx2_hw(struct cnic_dev *dev)
2282 {
2283         struct cnic_local *cp = dev->cnic_priv;
2284         struct cnic_eth_dev *ethdev = cp->ethdev;
2285         struct status_block *sblk = cp->status_blk;
2286         u32 val;
2287         int err;
2288
2289         cnic_set_bnx2_mac(dev);
2290
2291         val = CNIC_RD(dev, BNX2_MQ_CONFIG);
2292         val &= ~BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE;
2293         if (BCM_PAGE_BITS > 12)
2294                 val |= (12 - 8)  << 4;
2295         else
2296                 val |= (BCM_PAGE_BITS - 8)  << 4;
2297
2298         CNIC_WR(dev, BNX2_MQ_CONFIG, val);
2299
2300         CNIC_WR(dev, BNX2_HC_COMP_PROD_TRIP, (2 << 16) | 8);
2301         CNIC_WR(dev, BNX2_HC_COM_TICKS, (64 << 16) | 220);
2302         CNIC_WR(dev, BNX2_HC_CMD_TICKS, (64 << 16) | 220);
2303
2304         err = cnic_setup_5709_context(dev, 1);
2305         if (err)
2306                 return err;
2307
2308         cnic_init_context(dev, KWQ_CID);
2309         cnic_init_context(dev, KCQ_CID);
2310
2311         cp->kwq_cid_addr = GET_CID_ADDR(KWQ_CID);
2312         cp->kwq_io_addr = MB_GET_CID_ADDR(KWQ_CID) + L5_KRNLQ_HOST_QIDX;
2313
2314         cp->max_kwq_idx = MAX_KWQ_IDX;
2315         cp->kwq_prod_idx = 0;
2316         cp->kwq_con_idx = 0;
2317         cp->cnic_local_flags |= CNIC_LCL_FL_KWQ_INIT;
2318
2319         if (CHIP_NUM(cp) == CHIP_NUM_5706 || CHIP_NUM(cp) == CHIP_NUM_5708)
2320                 cp->kwq_con_idx_ptr = &sblk->status_rx_quick_consumer_index15;
2321         else
2322                 cp->kwq_con_idx_ptr = &sblk->status_cmd_consumer_index;
2323
2324         /* Initialize the kernel work queue context. */
2325         val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
2326               (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
2327         cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_TYPE, val);
2328
2329         val = (BCM_PAGE_SIZE / sizeof(struct kwqe) - 1) << 16;
2330         cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
2331
2332         val = ((BCM_PAGE_SIZE / sizeof(struct kwqe)) << 16) | KWQ_PAGE_CNT;
2333         cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
2334
2335         val = (u32) ((u64) cp->kwq_info.pgtbl_map >> 32);
2336         cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
2337
2338         val = (u32) cp->kwq_info.pgtbl_map;
2339         cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
2340
2341         cp->kcq_cid_addr = GET_CID_ADDR(KCQ_CID);
2342         cp->kcq_io_addr = MB_GET_CID_ADDR(KCQ_CID) + L5_KRNLQ_HOST_QIDX;
2343
2344         cp->kcq_prod_idx = 0;
2345
2346         /* Initialize the kernel complete queue context. */
2347         val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
2348               (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
2349         cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_TYPE, val);
2350
2351         val = (BCM_PAGE_SIZE / sizeof(struct kcqe) - 1) << 16;
2352         cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
2353
2354         val = ((BCM_PAGE_SIZE / sizeof(struct kcqe)) << 16) | KCQ_PAGE_CNT;
2355         cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
2356
2357         val = (u32) ((u64) cp->kcq_info.pgtbl_map >> 32);
2358         cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
2359
2360         val = (u32) cp->kcq_info.pgtbl_map;
2361         cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
2362
2363         cp->int_num = 0;
2364         if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
2365                 u32 sb_id = cp->status_blk_num;
2366                 u32 sb = BNX2_L2CTX_STATUSB_NUM(sb_id);
2367
2368                 cp->int_num = sb_id << BNX2_PCICFG_INT_ACK_CMD_INT_NUM_SHIFT;
2369                 cnic_ctx_wr(dev, cp->kwq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
2370                 cnic_ctx_wr(dev, cp->kcq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
2371         }
2372
2373         /* Enable Commnad Scheduler notification when we write to the
2374          * host producer index of the kernel contexts. */
2375         CNIC_WR(dev, BNX2_MQ_KNL_CMD_MASK1, 2);
2376
2377         /* Enable Command Scheduler notification when we write to either
2378          * the Send Queue or Receive Queue producer indexes of the kernel
2379          * bypass contexts. */
2380         CNIC_WR(dev, BNX2_MQ_KNL_BYP_CMD_MASK1, 7);
2381         CNIC_WR(dev, BNX2_MQ_KNL_BYP_WRITE_MASK1, 7);
2382
2383         /* Notify COM when the driver post an application buffer. */
2384         CNIC_WR(dev, BNX2_MQ_KNL_RX_V2P_MASK2, 0x2000);
2385
2386         /* Set the CP and COM doorbells.  These two processors polls the
2387          * doorbell for a non zero value before running.  This must be done
2388          * after setting up the kernel queue contexts. */
2389         cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 1);
2390         cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 1);
2391
2392         cnic_init_bnx2_tx_ring(dev);
2393         cnic_init_bnx2_rx_ring(dev);
2394
2395         err = cnic_init_bnx2_irq(dev);
2396         if (err) {
2397                 printk(KERN_ERR PFX "%s: cnic_init_irq failed\n",
2398                        dev->netdev->name);
2399                 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
2400                 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
2401                 return err;
2402         }
2403
2404         return 0;
2405 }
2406
2407 static int cnic_start_hw(struct cnic_dev *dev)
2408 {
2409         struct cnic_local *cp = dev->cnic_priv;
2410         struct cnic_eth_dev *ethdev = cp->ethdev;
2411         int err;
2412
2413         if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
2414                 return -EALREADY;
2415
2416         err = ethdev->drv_register_cnic(dev->netdev, cp->cnic_ops, dev);
2417         if (err) {
2418                 printk(KERN_ERR PFX "%s: register_cnic failed\n",
2419                        dev->netdev->name);
2420                 goto err2;
2421         }
2422
2423         dev->regview = ethdev->io_base;
2424         cp->chip_id = ethdev->chip_id;
2425         pci_dev_get(dev->pcidev);
2426         cp->func = PCI_FUNC(dev->pcidev->devfn);
2427         cp->status_blk = ethdev->irq_arr[0].status_blk;
2428         cp->status_blk_num = ethdev->irq_arr[0].status_blk_num;
2429
2430         err = cp->alloc_resc(dev);
2431         if (err) {
2432                 printk(KERN_ERR PFX "%s: allocate resource failure\n",
2433                        dev->netdev->name);
2434                 goto err1;
2435         }
2436
2437         err = cp->start_hw(dev);
2438         if (err)
2439                 goto err1;
2440
2441         err = cnic_cm_open(dev);
2442         if (err)
2443                 goto err1;
2444
2445         set_bit(CNIC_F_CNIC_UP, &dev->flags);
2446
2447         cp->enable_int(dev);
2448
2449         return 0;
2450
2451 err1:
2452         ethdev->drv_unregister_cnic(dev->netdev);
2453         cp->free_resc(dev);
2454         pci_dev_put(dev->pcidev);
2455 err2:
2456         return err;
2457 }
2458
2459 static void cnic_stop_bnx2_hw(struct cnic_dev *dev)
2460 {
2461         struct cnic_local *cp = dev->cnic_priv;
2462         struct cnic_eth_dev *ethdev = cp->ethdev;
2463
2464         cnic_disable_bnx2_int_sync(dev);
2465
2466         cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
2467         cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
2468
2469         cnic_init_context(dev, KWQ_CID);
2470         cnic_init_context(dev, KCQ_CID);
2471
2472         cnic_setup_5709_context(dev, 0);
2473         cnic_free_irq(dev);
2474
2475         ethdev->drv_unregister_cnic(dev->netdev);
2476
2477         cnic_free_resc(dev);
2478 }
2479
2480 static void cnic_stop_hw(struct cnic_dev *dev)
2481 {
2482         if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
2483                 struct cnic_local *cp = dev->cnic_priv;
2484
2485                 clear_bit(CNIC_F_CNIC_UP, &dev->flags);
2486                 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], NULL);
2487                 synchronize_rcu();
2488                 cnic_cm_shutdown(dev);
2489                 cp->stop_hw(dev);
2490                 pci_dev_put(dev->pcidev);
2491         }
2492 }
2493
2494 static void cnic_free_dev(struct cnic_dev *dev)
2495 {
2496         int i = 0;
2497
2498         while ((atomic_read(&dev->ref_count) != 0) && i < 10) {
2499                 msleep(100);
2500                 i++;
2501         }
2502         if (atomic_read(&dev->ref_count) != 0)
2503                 printk(KERN_ERR PFX "%s: Failed waiting for ref count to go"
2504                                     " to zero.\n", dev->netdev->name);
2505
2506         printk(KERN_INFO PFX "Removed CNIC device: %s\n", dev->netdev->name);
2507         dev_put(dev->netdev);
2508         kfree(dev);
2509 }
2510
2511 static struct cnic_dev *cnic_alloc_dev(struct net_device *dev,
2512                                        struct pci_dev *pdev)
2513 {
2514         struct cnic_dev *cdev;
2515         struct cnic_local *cp;
2516         int alloc_size;
2517
2518         alloc_size = sizeof(struct cnic_dev) + sizeof(struct cnic_local);
2519
2520         cdev = kzalloc(alloc_size , GFP_KERNEL);
2521         if (cdev == NULL) {
2522                 printk(KERN_ERR PFX "%s: allocate dev struct failure\n",
2523                        dev->name);
2524                 return NULL;
2525         }
2526
2527         cdev->netdev = dev;
2528         cdev->cnic_priv = (char *)cdev + sizeof(struct cnic_dev);
2529         cdev->register_device = cnic_register_device;
2530         cdev->unregister_device = cnic_unregister_device;
2531         cdev->iscsi_nl_msg_recv = cnic_iscsi_nl_msg_recv;
2532
2533         cp = cdev->cnic_priv;
2534         cp->dev = cdev;
2535         cp->uio_dev = -1;
2536         cp->l2_single_buf_size = 0x400;
2537         cp->l2_rx_ring_size = 3;
2538
2539         spin_lock_init(&cp->cnic_ulp_lock);
2540
2541         printk(KERN_INFO PFX "Added CNIC device: %s\n", dev->name);
2542
2543         return cdev;
2544 }
2545
2546 static struct cnic_dev *init_bnx2_cnic(struct net_device *dev)
2547 {
2548         struct pci_dev *pdev;
2549         struct cnic_dev *cdev;
2550         struct cnic_local *cp;
2551         struct cnic_eth_dev *ethdev = NULL;
2552         struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
2553
2554         probe = symbol_get(bnx2_cnic_probe);
2555         if (probe) {
2556                 ethdev = (*probe)(dev);
2557                 symbol_put_addr(probe);
2558         }
2559         if (!ethdev)
2560                 return NULL;
2561
2562         pdev = ethdev->pdev;
2563         if (!pdev)
2564                 return NULL;
2565
2566         dev_hold(dev);
2567         pci_dev_get(pdev);
2568         if (pdev->device == PCI_DEVICE_ID_NX2_5709 ||
2569             pdev->device == PCI_DEVICE_ID_NX2_5709S) {
2570                 u8 rev;
2571
2572                 pci_read_config_byte(pdev, PCI_REVISION_ID, &rev);
2573                 if (rev < 0x10) {
2574                         pci_dev_put(pdev);
2575                         goto cnic_err;
2576                 }
2577         }
2578         pci_dev_put(pdev);
2579
2580         cdev = cnic_alloc_dev(dev, pdev);
2581         if (cdev == NULL)
2582                 goto cnic_err;
2583
2584         set_bit(CNIC_F_BNX2_CLASS, &cdev->flags);
2585         cdev->submit_kwqes = cnic_submit_bnx2_kwqes;
2586
2587         cp = cdev->cnic_priv;
2588         cp->ethdev = ethdev;
2589         cdev->pcidev = pdev;
2590
2591         cp->cnic_ops = &cnic_bnx2_ops;
2592         cp->start_hw = cnic_start_bnx2_hw;
2593         cp->stop_hw = cnic_stop_bnx2_hw;
2594         cp->setup_pgtbl = cnic_setup_page_tbl;
2595         cp->alloc_resc = cnic_alloc_bnx2_resc;
2596         cp->free_resc = cnic_free_resc;
2597         cp->start_cm = cnic_cm_init_bnx2_hw;
2598         cp->stop_cm = cnic_cm_stop_bnx2_hw;
2599         cp->enable_int = cnic_enable_bnx2_int;
2600         cp->disable_int_sync = cnic_disable_bnx2_int_sync;
2601         cp->close_conn = cnic_close_bnx2_conn;
2602         cp->next_idx = cnic_bnx2_next_idx;
2603         cp->hw_idx = cnic_bnx2_hw_idx;
2604         return cdev;
2605
2606 cnic_err:
2607         dev_put(dev);
2608         return NULL;
2609 }
2610
2611 static struct cnic_dev *is_cnic_dev(struct net_device *dev)
2612 {
2613         struct ethtool_drvinfo drvinfo;
2614         struct cnic_dev *cdev = NULL;
2615
2616         if (dev->ethtool_ops && dev->ethtool_ops->get_drvinfo) {
2617                 memset(&drvinfo, 0, sizeof(drvinfo));
2618                 dev->ethtool_ops->get_drvinfo(dev, &drvinfo);
2619
2620                 if (!strcmp(drvinfo.driver, "bnx2"))
2621                         cdev = init_bnx2_cnic(dev);
2622                 if (cdev) {
2623                         write_lock(&cnic_dev_lock);
2624                         list_add(&cdev->list, &cnic_dev_list);
2625                         write_unlock(&cnic_dev_lock);
2626                 }
2627         }
2628         return cdev;
2629 }
2630
2631 /**
2632  * netdev event handler
2633  */
2634 static int cnic_netdev_event(struct notifier_block *this, unsigned long event,
2635                                                          void *ptr)
2636 {
2637         struct net_device *netdev = ptr;
2638         struct cnic_dev *dev;
2639         int if_type;
2640         int new_dev = 0;
2641
2642         dev = cnic_from_netdev(netdev);
2643
2644         if (!dev && (event == NETDEV_REGISTER || event == NETDEV_UP)) {
2645                 /* Check for the hot-plug device */
2646                 dev = is_cnic_dev(netdev);
2647                 if (dev) {
2648                         new_dev = 1;
2649                         cnic_hold(dev);
2650                 }
2651         }
2652         if (dev) {
2653                 struct cnic_local *cp = dev->cnic_priv;
2654
2655                 if (new_dev)
2656                         cnic_ulp_init(dev);
2657                 else if (event == NETDEV_UNREGISTER)
2658                         cnic_ulp_exit(dev);
2659                 else if (event == NETDEV_UP) {
2660                         mutex_lock(&cnic_lock);
2661                         if (!cnic_start_hw(dev))
2662                                 cnic_ulp_start(dev);
2663                         mutex_unlock(&cnic_lock);
2664                 }
2665
2666                 rcu_read_lock();
2667                 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
2668                         struct cnic_ulp_ops *ulp_ops;
2669                         void *ctx;
2670
2671                         ulp_ops = rcu_dereference(cp->ulp_ops[if_type]);
2672                         if (!ulp_ops || !ulp_ops->indicate_netevent)
2673                                 continue;
2674
2675                         ctx = cp->ulp_handle[if_type];
2676
2677                         ulp_ops->indicate_netevent(ctx, event);
2678                 }
2679                 rcu_read_unlock();
2680
2681                 if (event == NETDEV_GOING_DOWN) {
2682                         mutex_lock(&cnic_lock);
2683                         cnic_ulp_stop(dev);
2684                         cnic_stop_hw(dev);
2685                         mutex_unlock(&cnic_lock);
2686                 } else if (event == NETDEV_UNREGISTER) {
2687                         write_lock(&cnic_dev_lock);
2688                         list_del_init(&dev->list);
2689                         write_unlock(&cnic_dev_lock);
2690
2691                         cnic_put(dev);
2692                         cnic_free_dev(dev);
2693                         goto done;
2694                 }
2695                 cnic_put(dev);
2696         }
2697 done:
2698         return NOTIFY_DONE;
2699 }
2700
2701 static struct notifier_block cnic_netdev_notifier = {
2702         .notifier_call = cnic_netdev_event
2703 };
2704
2705 static void cnic_release(void)
2706 {
2707         struct cnic_dev *dev;
2708
2709         while (!list_empty(&cnic_dev_list)) {
2710                 dev = list_entry(cnic_dev_list.next, struct cnic_dev, list);
2711                 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
2712                         cnic_ulp_stop(dev);
2713                         cnic_stop_hw(dev);
2714                 }
2715
2716                 cnic_ulp_exit(dev);
2717                 list_del_init(&dev->list);
2718                 cnic_free_dev(dev);
2719         }
2720 }
2721
2722 static int __init cnic_init(void)
2723 {
2724         int rc = 0;
2725
2726         printk(KERN_INFO "%s", version);
2727
2728         rc = register_netdevice_notifier(&cnic_netdev_notifier);
2729         if (rc) {
2730                 cnic_release();
2731                 return rc;
2732         }
2733
2734         return 0;
2735 }
2736
2737 static void __exit cnic_exit(void)
2738 {
2739         unregister_netdevice_notifier(&cnic_netdev_notifier);
2740         cnic_release();
2741         return;
2742 }
2743
2744 module_init(cnic_init);
2745 module_exit(cnic_exit);