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