cnic: Use proper client and connection IDs on iSCSI ring
[pandora-kernel.git] / drivers / net / cnic.c
1 /* cnic.c: Broadcom CNIC core network driver.
2  *
3  * Copyright (c) 2006-2010 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15 #include <linux/module.h>
16
17 #include <linux/kernel.h>
18 #include <linux/errno.h>
19 #include <linux/list.h>
20 #include <linux/slab.h>
21 #include <linux/pci.h>
22 #include <linux/init.h>
23 #include <linux/netdevice.h>
24 #include <linux/uio_driver.h>
25 #include <linux/in.h>
26 #include <linux/dma-mapping.h>
27 #include <linux/delay.h>
28 #include <linux/ethtool.h>
29 #include <linux/if_vlan.h>
30 #if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
31 #define BCM_VLAN 1
32 #endif
33 #include <net/ip.h>
34 #include <net/tcp.h>
35 #include <net/route.h>
36 #include <net/ipv6.h>
37 #include <net/ip6_route.h>
38 #include <net/ip6_checksum.h>
39 #include <scsi/iscsi_if.h>
40
41 #include "cnic_if.h"
42 #include "bnx2.h"
43 #include "bnx2x/bnx2x_reg.h"
44 #include "bnx2x/bnx2x_fw_defs.h"
45 #include "bnx2x/bnx2x_hsi.h"
46 #include "../scsi/bnx2i/57xx_iscsi_constants.h"
47 #include "../scsi/bnx2i/57xx_iscsi_hsi.h"
48 #include "cnic.h"
49 #include "cnic_defs.h"
50
51 #define DRV_MODULE_NAME         "cnic"
52
53 static char version[] __devinitdata =
54         "Broadcom NetXtreme II CNIC Driver " DRV_MODULE_NAME " v" CNIC_MODULE_VERSION " (" CNIC_MODULE_RELDATE ")\n";
55
56 MODULE_AUTHOR("Michael Chan <mchan@broadcom.com> and John(Zongxi) "
57               "Chen (zongxi@broadcom.com");
58 MODULE_DESCRIPTION("Broadcom NetXtreme II CNIC Driver");
59 MODULE_LICENSE("GPL");
60 MODULE_VERSION(CNIC_MODULE_VERSION);
61
62 /* cnic_dev_list modifications are protected by both rtnl and cnic_dev_lock */
63 static LIST_HEAD(cnic_dev_list);
64 static LIST_HEAD(cnic_udev_list);
65 static DEFINE_RWLOCK(cnic_dev_lock);
66 static DEFINE_MUTEX(cnic_lock);
67
68 static struct cnic_ulp_ops *cnic_ulp_tbl[MAX_CNIC_ULP_TYPE];
69
70 static int cnic_service_bnx2(void *, void *);
71 static int cnic_service_bnx2x(void *, void *);
72 static int cnic_ctl(void *, struct cnic_ctl_info *);
73
74 static struct cnic_ops cnic_bnx2_ops = {
75         .cnic_owner     = THIS_MODULE,
76         .cnic_handler   = cnic_service_bnx2,
77         .cnic_ctl       = cnic_ctl,
78 };
79
80 static struct cnic_ops cnic_bnx2x_ops = {
81         .cnic_owner     = THIS_MODULE,
82         .cnic_handler   = cnic_service_bnx2x,
83         .cnic_ctl       = cnic_ctl,
84 };
85
86 static struct workqueue_struct *cnic_wq;
87
88 static void cnic_shutdown_rings(struct cnic_dev *);
89 static void cnic_init_rings(struct cnic_dev *);
90 static int cnic_cm_set_pg(struct cnic_sock *);
91
92 static int cnic_uio_open(struct uio_info *uinfo, struct inode *inode)
93 {
94         struct cnic_uio_dev *udev = uinfo->priv;
95         struct cnic_dev *dev;
96
97         if (!capable(CAP_NET_ADMIN))
98                 return -EPERM;
99
100         if (udev->uio_dev != -1)
101                 return -EBUSY;
102
103         rtnl_lock();
104         dev = udev->dev;
105
106         if (!dev || !test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
107                 rtnl_unlock();
108                 return -ENODEV;
109         }
110
111         udev->uio_dev = iminor(inode);
112
113         cnic_shutdown_rings(dev);
114         cnic_init_rings(dev);
115         rtnl_unlock();
116
117         return 0;
118 }
119
120 static int cnic_uio_close(struct uio_info *uinfo, struct inode *inode)
121 {
122         struct cnic_uio_dev *udev = uinfo->priv;
123
124         udev->uio_dev = -1;
125         return 0;
126 }
127
128 static inline void cnic_hold(struct cnic_dev *dev)
129 {
130         atomic_inc(&dev->ref_count);
131 }
132
133 static inline void cnic_put(struct cnic_dev *dev)
134 {
135         atomic_dec(&dev->ref_count);
136 }
137
138 static inline void csk_hold(struct cnic_sock *csk)
139 {
140         atomic_inc(&csk->ref_count);
141 }
142
143 static inline void csk_put(struct cnic_sock *csk)
144 {
145         atomic_dec(&csk->ref_count);
146 }
147
148 static struct cnic_dev *cnic_from_netdev(struct net_device *netdev)
149 {
150         struct cnic_dev *cdev;
151
152         read_lock(&cnic_dev_lock);
153         list_for_each_entry(cdev, &cnic_dev_list, list) {
154                 if (netdev == cdev->netdev) {
155                         cnic_hold(cdev);
156                         read_unlock(&cnic_dev_lock);
157                         return cdev;
158                 }
159         }
160         read_unlock(&cnic_dev_lock);
161         return NULL;
162 }
163
164 static inline void ulp_get(struct cnic_ulp_ops *ulp_ops)
165 {
166         atomic_inc(&ulp_ops->ref_count);
167 }
168
169 static inline void ulp_put(struct cnic_ulp_ops *ulp_ops)
170 {
171         atomic_dec(&ulp_ops->ref_count);
172 }
173
174 static void cnic_ctx_wr(struct cnic_dev *dev, u32 cid_addr, u32 off, u32 val)
175 {
176         struct cnic_local *cp = dev->cnic_priv;
177         struct cnic_eth_dev *ethdev = cp->ethdev;
178         struct drv_ctl_info info;
179         struct drv_ctl_io *io = &info.data.io;
180
181         info.cmd = DRV_CTL_CTX_WR_CMD;
182         io->cid_addr = cid_addr;
183         io->offset = off;
184         io->data = val;
185         ethdev->drv_ctl(dev->netdev, &info);
186 }
187
188 static void cnic_ctx_tbl_wr(struct cnic_dev *dev, u32 off, dma_addr_t addr)
189 {
190         struct cnic_local *cp = dev->cnic_priv;
191         struct cnic_eth_dev *ethdev = cp->ethdev;
192         struct drv_ctl_info info;
193         struct drv_ctl_io *io = &info.data.io;
194
195         info.cmd = DRV_CTL_CTXTBL_WR_CMD;
196         io->offset = off;
197         io->dma_addr = addr;
198         ethdev->drv_ctl(dev->netdev, &info);
199 }
200
201 static void cnic_ring_ctl(struct cnic_dev *dev, u32 cid, u32 cl_id, int start)
202 {
203         struct cnic_local *cp = dev->cnic_priv;
204         struct cnic_eth_dev *ethdev = cp->ethdev;
205         struct drv_ctl_info info;
206         struct drv_ctl_l2_ring *ring = &info.data.ring;
207
208         if (start)
209                 info.cmd = DRV_CTL_START_L2_CMD;
210         else
211                 info.cmd = DRV_CTL_STOP_L2_CMD;
212
213         ring->cid = cid;
214         ring->client_id = cl_id;
215         ethdev->drv_ctl(dev->netdev, &info);
216 }
217
218 static void cnic_reg_wr_ind(struct cnic_dev *dev, u32 off, u32 val)
219 {
220         struct cnic_local *cp = dev->cnic_priv;
221         struct cnic_eth_dev *ethdev = cp->ethdev;
222         struct drv_ctl_info info;
223         struct drv_ctl_io *io = &info.data.io;
224
225         info.cmd = DRV_CTL_IO_WR_CMD;
226         io->offset = off;
227         io->data = val;
228         ethdev->drv_ctl(dev->netdev, &info);
229 }
230
231 static u32 cnic_reg_rd_ind(struct cnic_dev *dev, u32 off)
232 {
233         struct cnic_local *cp = dev->cnic_priv;
234         struct cnic_eth_dev *ethdev = cp->ethdev;
235         struct drv_ctl_info info;
236         struct drv_ctl_io *io = &info.data.io;
237
238         info.cmd = DRV_CTL_IO_RD_CMD;
239         io->offset = off;
240         ethdev->drv_ctl(dev->netdev, &info);
241         return io->data;
242 }
243
244 static int cnic_in_use(struct cnic_sock *csk)
245 {
246         return test_bit(SK_F_INUSE, &csk->flags);
247 }
248
249 static void cnic_spq_completion(struct cnic_dev *dev, int cmd, u32 count)
250 {
251         struct cnic_local *cp = dev->cnic_priv;
252         struct cnic_eth_dev *ethdev = cp->ethdev;
253         struct drv_ctl_info info;
254
255         info.cmd = cmd;
256         info.data.credit.credit_count = count;
257         ethdev->drv_ctl(dev->netdev, &info);
258 }
259
260 static int cnic_get_l5_cid(struct cnic_local *cp, u32 cid, u32 *l5_cid)
261 {
262         u32 i;
263
264         for (i = 0; i < cp->max_cid_space; i++) {
265                 if (cp->ctx_tbl[i].cid == cid) {
266                         *l5_cid = i;
267                         return 0;
268                 }
269         }
270         return -EINVAL;
271 }
272
273 static int cnic_send_nlmsg(struct cnic_local *cp, u32 type,
274                            struct cnic_sock *csk)
275 {
276         struct iscsi_path path_req;
277         char *buf = NULL;
278         u16 len = 0;
279         u32 msg_type = ISCSI_KEVENT_IF_DOWN;
280         struct cnic_ulp_ops *ulp_ops;
281         struct cnic_uio_dev *udev = cp->udev;
282         int rc = 0, retry = 0;
283
284         if (!udev || udev->uio_dev == -1)
285                 return -ENODEV;
286
287         if (csk) {
288                 len = sizeof(path_req);
289                 buf = (char *) &path_req;
290                 memset(&path_req, 0, len);
291
292                 msg_type = ISCSI_KEVENT_PATH_REQ;
293                 path_req.handle = (u64) csk->l5_cid;
294                 if (test_bit(SK_F_IPV6, &csk->flags)) {
295                         memcpy(&path_req.dst.v6_addr, &csk->dst_ip[0],
296                                sizeof(struct in6_addr));
297                         path_req.ip_addr_len = 16;
298                 } else {
299                         memcpy(&path_req.dst.v4_addr, &csk->dst_ip[0],
300                                sizeof(struct in_addr));
301                         path_req.ip_addr_len = 4;
302                 }
303                 path_req.vlan_id = csk->vlan_id;
304                 path_req.pmtu = csk->mtu;
305         }
306
307         while (retry < 3) {
308                 rc = 0;
309                 rcu_read_lock();
310                 ulp_ops = rcu_dereference(cnic_ulp_tbl[CNIC_ULP_ISCSI]);
311                 if (ulp_ops)
312                         rc = ulp_ops->iscsi_nl_send_msg(
313                                 cp->ulp_handle[CNIC_ULP_ISCSI],
314                                 msg_type, buf, len);
315                 rcu_read_unlock();
316                 if (rc == 0 || msg_type != ISCSI_KEVENT_PATH_REQ)
317                         break;
318
319                 msleep(100);
320                 retry++;
321         }
322         return 0;
323 }
324
325 static int cnic_iscsi_nl_msg_recv(struct cnic_dev *dev, u32 msg_type,
326                                   char *buf, u16 len)
327 {
328         int rc = -EINVAL;
329
330         switch (msg_type) {
331         case ISCSI_UEVENT_PATH_UPDATE: {
332                 struct cnic_local *cp;
333                 u32 l5_cid;
334                 struct cnic_sock *csk;
335                 struct iscsi_path *path_resp;
336
337                 if (len < sizeof(*path_resp))
338                         break;
339
340                 path_resp = (struct iscsi_path *) buf;
341                 cp = dev->cnic_priv;
342                 l5_cid = (u32) path_resp->handle;
343                 if (l5_cid >= MAX_CM_SK_TBL_SZ)
344                         break;
345
346                 rcu_read_lock();
347                 if (!rcu_dereference(cp->ulp_ops[CNIC_ULP_L4])) {
348                         rc = -ENODEV;
349                         rcu_read_unlock();
350                         break;
351                 }
352                 csk = &cp->csk_tbl[l5_cid];
353                 csk_hold(csk);
354                 if (cnic_in_use(csk)) {
355                         memcpy(csk->ha, path_resp->mac_addr, 6);
356                         if (test_bit(SK_F_IPV6, &csk->flags))
357                                 memcpy(&csk->src_ip[0], &path_resp->src.v6_addr,
358                                        sizeof(struct in6_addr));
359                         else
360                                 memcpy(&csk->src_ip[0], &path_resp->src.v4_addr,
361                                        sizeof(struct in_addr));
362                         if (is_valid_ether_addr(csk->ha))
363                                 cnic_cm_set_pg(csk);
364                 }
365                 csk_put(csk);
366                 rcu_read_unlock();
367                 rc = 0;
368         }
369         }
370
371         return rc;
372 }
373
374 static int cnic_offld_prep(struct cnic_sock *csk)
375 {
376         if (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
377                 return 0;
378
379         if (!test_bit(SK_F_CONNECT_START, &csk->flags)) {
380                 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
381                 return 0;
382         }
383
384         return 1;
385 }
386
387 static int cnic_close_prep(struct cnic_sock *csk)
388 {
389         clear_bit(SK_F_CONNECT_START, &csk->flags);
390         smp_mb__after_clear_bit();
391
392         if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
393                 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
394                         msleep(1);
395
396                 return 1;
397         }
398         return 0;
399 }
400
401 static int cnic_abort_prep(struct cnic_sock *csk)
402 {
403         clear_bit(SK_F_CONNECT_START, &csk->flags);
404         smp_mb__after_clear_bit();
405
406         while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
407                 msleep(1);
408
409         if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
410                 csk->state = L4_KCQE_OPCODE_VALUE_RESET_COMP;
411                 return 1;
412         }
413
414         return 0;
415 }
416
417 static void cnic_uio_stop(void)
418 {
419         struct cnic_dev *dev;
420
421         read_lock(&cnic_dev_lock);
422         list_for_each_entry(dev, &cnic_dev_list, list) {
423                 struct cnic_local *cp = dev->cnic_priv;
424
425                 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
426         }
427         read_unlock(&cnic_dev_lock);
428 }
429
430 int cnic_register_driver(int ulp_type, struct cnic_ulp_ops *ulp_ops)
431 {
432         struct cnic_dev *dev;
433
434         if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
435                 pr_err("%s: Bad type %d\n", __func__, ulp_type);
436                 return -EINVAL;
437         }
438         mutex_lock(&cnic_lock);
439         if (cnic_ulp_tbl[ulp_type]) {
440                 pr_err("%s: Type %d has already been registered\n",
441                        __func__, ulp_type);
442                 mutex_unlock(&cnic_lock);
443                 return -EBUSY;
444         }
445
446         read_lock(&cnic_dev_lock);
447         list_for_each_entry(dev, &cnic_dev_list, list) {
448                 struct cnic_local *cp = dev->cnic_priv;
449
450                 clear_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]);
451         }
452         read_unlock(&cnic_dev_lock);
453
454         atomic_set(&ulp_ops->ref_count, 0);
455         rcu_assign_pointer(cnic_ulp_tbl[ulp_type], ulp_ops);
456         mutex_unlock(&cnic_lock);
457
458         /* Prevent race conditions with netdev_event */
459         rtnl_lock();
460         list_for_each_entry(dev, &cnic_dev_list, list) {
461                 struct cnic_local *cp = dev->cnic_priv;
462
463                 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]))
464                         ulp_ops->cnic_init(dev);
465         }
466         rtnl_unlock();
467
468         return 0;
469 }
470
471 int cnic_unregister_driver(int ulp_type)
472 {
473         struct cnic_dev *dev;
474         struct cnic_ulp_ops *ulp_ops;
475         int i = 0;
476
477         if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
478                 pr_err("%s: Bad type %d\n", __func__, ulp_type);
479                 return -EINVAL;
480         }
481         mutex_lock(&cnic_lock);
482         ulp_ops = cnic_ulp_tbl[ulp_type];
483         if (!ulp_ops) {
484                 pr_err("%s: Type %d has not been registered\n",
485                        __func__, ulp_type);
486                 goto out_unlock;
487         }
488         read_lock(&cnic_dev_lock);
489         list_for_each_entry(dev, &cnic_dev_list, list) {
490                 struct cnic_local *cp = dev->cnic_priv;
491
492                 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
493                         pr_err("%s: Type %d still has devices registered\n",
494                                __func__, ulp_type);
495                         read_unlock(&cnic_dev_lock);
496                         goto out_unlock;
497                 }
498         }
499         read_unlock(&cnic_dev_lock);
500
501         if (ulp_type == CNIC_ULP_ISCSI)
502                 cnic_uio_stop();
503
504         rcu_assign_pointer(cnic_ulp_tbl[ulp_type], NULL);
505
506         mutex_unlock(&cnic_lock);
507         synchronize_rcu();
508         while ((atomic_read(&ulp_ops->ref_count) != 0) && (i < 20)) {
509                 msleep(100);
510                 i++;
511         }
512
513         if (atomic_read(&ulp_ops->ref_count) != 0)
514                 netdev_warn(dev->netdev, "Failed waiting for ref count to go to zero\n");
515         return 0;
516
517 out_unlock:
518         mutex_unlock(&cnic_lock);
519         return -EINVAL;
520 }
521
522 static int cnic_start_hw(struct cnic_dev *);
523 static void cnic_stop_hw(struct cnic_dev *);
524
525 static int cnic_register_device(struct cnic_dev *dev, int ulp_type,
526                                 void *ulp_ctx)
527 {
528         struct cnic_local *cp = dev->cnic_priv;
529         struct cnic_ulp_ops *ulp_ops;
530
531         if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
532                 pr_err("%s: Bad type %d\n", __func__, ulp_type);
533                 return -EINVAL;
534         }
535         mutex_lock(&cnic_lock);
536         if (cnic_ulp_tbl[ulp_type] == NULL) {
537                 pr_err("%s: Driver with type %d has not been registered\n",
538                        __func__, ulp_type);
539                 mutex_unlock(&cnic_lock);
540                 return -EAGAIN;
541         }
542         if (rcu_dereference(cp->ulp_ops[ulp_type])) {
543                 pr_err("%s: Type %d has already been registered to this device\n",
544                        __func__, ulp_type);
545                 mutex_unlock(&cnic_lock);
546                 return -EBUSY;
547         }
548
549         clear_bit(ULP_F_START, &cp->ulp_flags[ulp_type]);
550         cp->ulp_handle[ulp_type] = ulp_ctx;
551         ulp_ops = cnic_ulp_tbl[ulp_type];
552         rcu_assign_pointer(cp->ulp_ops[ulp_type], ulp_ops);
553         cnic_hold(dev);
554
555         if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
556                 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[ulp_type]))
557                         ulp_ops->cnic_start(cp->ulp_handle[ulp_type]);
558
559         mutex_unlock(&cnic_lock);
560
561         return 0;
562
563 }
564 EXPORT_SYMBOL(cnic_register_driver);
565
566 static int cnic_unregister_device(struct cnic_dev *dev, int ulp_type)
567 {
568         struct cnic_local *cp = dev->cnic_priv;
569         int i = 0;
570
571         if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
572                 pr_err("%s: Bad type %d\n", __func__, ulp_type);
573                 return -EINVAL;
574         }
575         mutex_lock(&cnic_lock);
576         if (rcu_dereference(cp->ulp_ops[ulp_type])) {
577                 rcu_assign_pointer(cp->ulp_ops[ulp_type], NULL);
578                 cnic_put(dev);
579         } else {
580                 pr_err("%s: device not registered to this ulp type %d\n",
581                        __func__, ulp_type);
582                 mutex_unlock(&cnic_lock);
583                 return -EINVAL;
584         }
585         mutex_unlock(&cnic_lock);
586
587         synchronize_rcu();
588
589         while (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]) &&
590                i < 20) {
591                 msleep(100);
592                 i++;
593         }
594         if (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]))
595                 netdev_warn(dev->netdev, "Failed waiting for ULP up call to complete\n");
596
597         return 0;
598 }
599 EXPORT_SYMBOL(cnic_unregister_driver);
600
601 static int cnic_init_id_tbl(struct cnic_id_tbl *id_tbl, u32 size, u32 start_id)
602 {
603         id_tbl->start = start_id;
604         id_tbl->max = size;
605         id_tbl->next = 0;
606         spin_lock_init(&id_tbl->lock);
607         id_tbl->table = kzalloc(DIV_ROUND_UP(size, 32) * 4, GFP_KERNEL);
608         if (!id_tbl->table)
609                 return -ENOMEM;
610
611         return 0;
612 }
613
614 static void cnic_free_id_tbl(struct cnic_id_tbl *id_tbl)
615 {
616         kfree(id_tbl->table);
617         id_tbl->table = NULL;
618 }
619
620 static int cnic_alloc_id(struct cnic_id_tbl *id_tbl, u32 id)
621 {
622         int ret = -1;
623
624         id -= id_tbl->start;
625         if (id >= id_tbl->max)
626                 return ret;
627
628         spin_lock(&id_tbl->lock);
629         if (!test_bit(id, id_tbl->table)) {
630                 set_bit(id, id_tbl->table);
631                 ret = 0;
632         }
633         spin_unlock(&id_tbl->lock);
634         return ret;
635 }
636
637 /* Returns -1 if not successful */
638 static u32 cnic_alloc_new_id(struct cnic_id_tbl *id_tbl)
639 {
640         u32 id;
641
642         spin_lock(&id_tbl->lock);
643         id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
644         if (id >= id_tbl->max) {
645                 id = -1;
646                 if (id_tbl->next != 0) {
647                         id = find_first_zero_bit(id_tbl->table, id_tbl->next);
648                         if (id >= id_tbl->next)
649                                 id = -1;
650                 }
651         }
652
653         if (id < id_tbl->max) {
654                 set_bit(id, id_tbl->table);
655                 id_tbl->next = (id + 1) & (id_tbl->max - 1);
656                 id += id_tbl->start;
657         }
658
659         spin_unlock(&id_tbl->lock);
660
661         return id;
662 }
663
664 static void cnic_free_id(struct cnic_id_tbl *id_tbl, u32 id)
665 {
666         if (id == -1)
667                 return;
668
669         id -= id_tbl->start;
670         if (id >= id_tbl->max)
671                 return;
672
673         clear_bit(id, id_tbl->table);
674 }
675
676 static void cnic_free_dma(struct cnic_dev *dev, struct cnic_dma *dma)
677 {
678         int i;
679
680         if (!dma->pg_arr)
681                 return;
682
683         for (i = 0; i < dma->num_pages; i++) {
684                 if (dma->pg_arr[i]) {
685                         dma_free_coherent(&dev->pcidev->dev, BCM_PAGE_SIZE,
686                                           dma->pg_arr[i], dma->pg_map_arr[i]);
687                         dma->pg_arr[i] = NULL;
688                 }
689         }
690         if (dma->pgtbl) {
691                 dma_free_coherent(&dev->pcidev->dev, dma->pgtbl_size,
692                                   dma->pgtbl, dma->pgtbl_map);
693                 dma->pgtbl = NULL;
694         }
695         kfree(dma->pg_arr);
696         dma->pg_arr = NULL;
697         dma->num_pages = 0;
698 }
699
700 static void cnic_setup_page_tbl(struct cnic_dev *dev, struct cnic_dma *dma)
701 {
702         int i;
703         u32 *page_table = dma->pgtbl;
704
705         for (i = 0; i < dma->num_pages; i++) {
706                 /* Each entry needs to be in big endian format. */
707                 *page_table = (u32) ((u64) dma->pg_map_arr[i] >> 32);
708                 page_table++;
709                 *page_table = (u32) dma->pg_map_arr[i];
710                 page_table++;
711         }
712 }
713
714 static void cnic_setup_page_tbl_le(struct cnic_dev *dev, struct cnic_dma *dma)
715 {
716         int i;
717         u32 *page_table = dma->pgtbl;
718
719         for (i = 0; i < dma->num_pages; i++) {
720                 /* Each entry needs to be in little endian format. */
721                 *page_table = dma->pg_map_arr[i] & 0xffffffff;
722                 page_table++;
723                 *page_table = (u32) ((u64) dma->pg_map_arr[i] >> 32);
724                 page_table++;
725         }
726 }
727
728 static int cnic_alloc_dma(struct cnic_dev *dev, struct cnic_dma *dma,
729                           int pages, int use_pg_tbl)
730 {
731         int i, size;
732         struct cnic_local *cp = dev->cnic_priv;
733
734         size = pages * (sizeof(void *) + sizeof(dma_addr_t));
735         dma->pg_arr = kzalloc(size, GFP_ATOMIC);
736         if (dma->pg_arr == NULL)
737                 return -ENOMEM;
738
739         dma->pg_map_arr = (dma_addr_t *) (dma->pg_arr + pages);
740         dma->num_pages = pages;
741
742         for (i = 0; i < pages; i++) {
743                 dma->pg_arr[i] = dma_alloc_coherent(&dev->pcidev->dev,
744                                                     BCM_PAGE_SIZE,
745                                                     &dma->pg_map_arr[i],
746                                                     GFP_ATOMIC);
747                 if (dma->pg_arr[i] == NULL)
748                         goto error;
749         }
750         if (!use_pg_tbl)
751                 return 0;
752
753         dma->pgtbl_size = ((pages * 8) + BCM_PAGE_SIZE - 1) &
754                           ~(BCM_PAGE_SIZE - 1);
755         dma->pgtbl = dma_alloc_coherent(&dev->pcidev->dev, dma->pgtbl_size,
756                                         &dma->pgtbl_map, GFP_ATOMIC);
757         if (dma->pgtbl == NULL)
758                 goto error;
759
760         cp->setup_pgtbl(dev, dma);
761
762         return 0;
763
764 error:
765         cnic_free_dma(dev, dma);
766         return -ENOMEM;
767 }
768
769 static void cnic_free_context(struct cnic_dev *dev)
770 {
771         struct cnic_local *cp = dev->cnic_priv;
772         int i;
773
774         for (i = 0; i < cp->ctx_blks; i++) {
775                 if (cp->ctx_arr[i].ctx) {
776                         dma_free_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
777                                           cp->ctx_arr[i].ctx,
778                                           cp->ctx_arr[i].mapping);
779                         cp->ctx_arr[i].ctx = NULL;
780                 }
781         }
782 }
783
784 static void __cnic_free_uio(struct cnic_uio_dev *udev)
785 {
786         uio_unregister_device(&udev->cnic_uinfo);
787
788         if (udev->l2_buf) {
789                 dma_free_coherent(&udev->pdev->dev, udev->l2_buf_size,
790                                   udev->l2_buf, udev->l2_buf_map);
791                 udev->l2_buf = NULL;
792         }
793
794         if (udev->l2_ring) {
795                 dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
796                                   udev->l2_ring, udev->l2_ring_map);
797                 udev->l2_ring = NULL;
798         }
799
800         pci_dev_put(udev->pdev);
801         kfree(udev);
802 }
803
804 static void cnic_free_uio(struct cnic_uio_dev *udev)
805 {
806         if (!udev)
807                 return;
808
809         write_lock(&cnic_dev_lock);
810         list_del_init(&udev->list);
811         write_unlock(&cnic_dev_lock);
812         __cnic_free_uio(udev);
813 }
814
815 static void cnic_free_resc(struct cnic_dev *dev)
816 {
817         struct cnic_local *cp = dev->cnic_priv;
818         struct cnic_uio_dev *udev = cp->udev;
819
820         if (udev) {
821                 udev->dev = NULL;
822                 cp->udev = NULL;
823         }
824
825         cnic_free_context(dev);
826         kfree(cp->ctx_arr);
827         cp->ctx_arr = NULL;
828         cp->ctx_blks = 0;
829
830         cnic_free_dma(dev, &cp->gbl_buf_info);
831         cnic_free_dma(dev, &cp->conn_buf_info);
832         cnic_free_dma(dev, &cp->kwq_info);
833         cnic_free_dma(dev, &cp->kwq_16_data_info);
834         cnic_free_dma(dev, &cp->kcq1.dma);
835         kfree(cp->iscsi_tbl);
836         cp->iscsi_tbl = NULL;
837         kfree(cp->ctx_tbl);
838         cp->ctx_tbl = NULL;
839
840         cnic_free_id_tbl(&cp->cid_tbl);
841 }
842
843 static int cnic_alloc_context(struct cnic_dev *dev)
844 {
845         struct cnic_local *cp = dev->cnic_priv;
846
847         if (CHIP_NUM(cp) == CHIP_NUM_5709) {
848                 int i, k, arr_size;
849
850                 cp->ctx_blk_size = BCM_PAGE_SIZE;
851                 cp->cids_per_blk = BCM_PAGE_SIZE / 128;
852                 arr_size = BNX2_MAX_CID / cp->cids_per_blk *
853                            sizeof(struct cnic_ctx);
854                 cp->ctx_arr = kzalloc(arr_size, GFP_KERNEL);
855                 if (cp->ctx_arr == NULL)
856                         return -ENOMEM;
857
858                 k = 0;
859                 for (i = 0; i < 2; i++) {
860                         u32 j, reg, off, lo, hi;
861
862                         if (i == 0)
863                                 off = BNX2_PG_CTX_MAP;
864                         else
865                                 off = BNX2_ISCSI_CTX_MAP;
866
867                         reg = cnic_reg_rd_ind(dev, off);
868                         lo = reg >> 16;
869                         hi = reg & 0xffff;
870                         for (j = lo; j < hi; j += cp->cids_per_blk, k++)
871                                 cp->ctx_arr[k].cid = j;
872                 }
873
874                 cp->ctx_blks = k;
875                 if (cp->ctx_blks >= (BNX2_MAX_CID / cp->cids_per_blk)) {
876                         cp->ctx_blks = 0;
877                         return -ENOMEM;
878                 }
879
880                 for (i = 0; i < cp->ctx_blks; i++) {
881                         cp->ctx_arr[i].ctx =
882                                 dma_alloc_coherent(&dev->pcidev->dev,
883                                                    BCM_PAGE_SIZE,
884                                                    &cp->ctx_arr[i].mapping,
885                                                    GFP_KERNEL);
886                         if (cp->ctx_arr[i].ctx == NULL)
887                                 return -ENOMEM;
888                 }
889         }
890         return 0;
891 }
892
893 static int cnic_alloc_kcq(struct cnic_dev *dev, struct kcq_info *info)
894 {
895         int err, i, is_bnx2 = 0;
896         struct kcqe **kcq;
897
898         if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags))
899                 is_bnx2 = 1;
900
901         err = cnic_alloc_dma(dev, &info->dma, KCQ_PAGE_CNT, is_bnx2);
902         if (err)
903                 return err;
904
905         kcq = (struct kcqe **) info->dma.pg_arr;
906         info->kcq = kcq;
907
908         if (is_bnx2)
909                 return 0;
910
911         for (i = 0; i < KCQ_PAGE_CNT; i++) {
912                 struct bnx2x_bd_chain_next *next =
913                         (struct bnx2x_bd_chain_next *) &kcq[i][MAX_KCQE_CNT];
914                 int j = i + 1;
915
916                 if (j >= KCQ_PAGE_CNT)
917                         j = 0;
918                 next->addr_hi = (u64) info->dma.pg_map_arr[j] >> 32;
919                 next->addr_lo = info->dma.pg_map_arr[j] & 0xffffffff;
920         }
921         return 0;
922 }
923
924 static int cnic_alloc_uio_rings(struct cnic_dev *dev, int pages)
925 {
926         struct cnic_local *cp = dev->cnic_priv;
927         struct cnic_uio_dev *udev;
928
929         read_lock(&cnic_dev_lock);
930         list_for_each_entry(udev, &cnic_udev_list, list) {
931                 if (udev->pdev == dev->pcidev) {
932                         udev->dev = dev;
933                         cp->udev = udev;
934                         read_unlock(&cnic_dev_lock);
935                         return 0;
936                 }
937         }
938         read_unlock(&cnic_dev_lock);
939
940         udev = kzalloc(sizeof(struct cnic_uio_dev), GFP_ATOMIC);
941         if (!udev)
942                 return -ENOMEM;
943
944         udev->uio_dev = -1;
945
946         udev->dev = dev;
947         udev->pdev = dev->pcidev;
948         udev->l2_ring_size = pages * BCM_PAGE_SIZE;
949         udev->l2_ring = dma_alloc_coherent(&udev->pdev->dev, udev->l2_ring_size,
950                                            &udev->l2_ring_map,
951                                            GFP_KERNEL | __GFP_COMP);
952         if (!udev->l2_ring)
953                 return -ENOMEM;
954
955         udev->l2_buf_size = (cp->l2_rx_ring_size + 1) * cp->l2_single_buf_size;
956         udev->l2_buf_size = PAGE_ALIGN(udev->l2_buf_size);
957         udev->l2_buf = dma_alloc_coherent(&udev->pdev->dev, udev->l2_buf_size,
958                                           &udev->l2_buf_map,
959                                           GFP_KERNEL | __GFP_COMP);
960         if (!udev->l2_buf)
961                 return -ENOMEM;
962
963         write_lock(&cnic_dev_lock);
964         list_add(&udev->list, &cnic_udev_list);
965         write_unlock(&cnic_dev_lock);
966
967         pci_dev_get(udev->pdev);
968
969         cp->udev = udev;
970
971         return 0;
972 }
973
974 static int cnic_init_uio(struct cnic_dev *dev)
975 {
976         struct cnic_local *cp = dev->cnic_priv;
977         struct cnic_uio_dev *udev = cp->udev;
978         struct uio_info *uinfo;
979         int ret = 0;
980
981         if (!udev)
982                 return -ENOMEM;
983
984         uinfo = &udev->cnic_uinfo;
985
986         uinfo->mem[0].addr = dev->netdev->base_addr;
987         uinfo->mem[0].internal_addr = dev->regview;
988         uinfo->mem[0].size = dev->netdev->mem_end - dev->netdev->mem_start;
989         uinfo->mem[0].memtype = UIO_MEM_PHYS;
990
991         if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
992                 uinfo->mem[1].addr = (unsigned long) cp->status_blk.gen &
993                                         PAGE_MASK;
994                 if (cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
995                         uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE * 9;
996                 else
997                         uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE;
998
999                 uinfo->name = "bnx2_cnic";
1000         } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
1001                 uinfo->mem[1].addr = (unsigned long) cp->bnx2x_def_status_blk &
1002                         PAGE_MASK;
1003                 uinfo->mem[1].size = sizeof(*cp->bnx2x_def_status_blk);
1004
1005                 uinfo->name = "bnx2x_cnic";
1006         }
1007
1008         uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
1009
1010         uinfo->mem[2].addr = (unsigned long) udev->l2_ring;
1011         uinfo->mem[2].size = udev->l2_ring_size;
1012         uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
1013
1014         uinfo->mem[3].addr = (unsigned long) udev->l2_buf;
1015         uinfo->mem[3].size = udev->l2_buf_size;
1016         uinfo->mem[3].memtype = UIO_MEM_LOGICAL;
1017
1018         uinfo->version = CNIC_MODULE_VERSION;
1019         uinfo->irq = UIO_IRQ_CUSTOM;
1020
1021         uinfo->open = cnic_uio_open;
1022         uinfo->release = cnic_uio_close;
1023
1024         if (udev->uio_dev == -1) {
1025                 if (!uinfo->priv) {
1026                         uinfo->priv = udev;
1027
1028                         ret = uio_register_device(&udev->pdev->dev, uinfo);
1029                 }
1030         } else {
1031                 cnic_init_rings(dev);
1032         }
1033
1034         return ret;
1035 }
1036
1037 static int cnic_alloc_bnx2_resc(struct cnic_dev *dev)
1038 {
1039         struct cnic_local *cp = dev->cnic_priv;
1040         int ret;
1041
1042         ret = cnic_alloc_dma(dev, &cp->kwq_info, KWQ_PAGE_CNT, 1);
1043         if (ret)
1044                 goto error;
1045         cp->kwq = (struct kwqe **) cp->kwq_info.pg_arr;
1046
1047         ret = cnic_alloc_kcq(dev, &cp->kcq1);
1048         if (ret)
1049                 goto error;
1050
1051         ret = cnic_alloc_context(dev);
1052         if (ret)
1053                 goto error;
1054
1055         ret = cnic_alloc_uio_rings(dev, 2);
1056         if (ret)
1057                 goto error;
1058
1059         ret = cnic_init_uio(dev);
1060         if (ret)
1061                 goto error;
1062
1063         return 0;
1064
1065 error:
1066         cnic_free_resc(dev);
1067         return ret;
1068 }
1069
1070 static int cnic_alloc_bnx2x_context(struct cnic_dev *dev)
1071 {
1072         struct cnic_local *cp = dev->cnic_priv;
1073         int ctx_blk_size = cp->ethdev->ctx_blk_size;
1074         int total_mem, blks, i;
1075
1076         total_mem = BNX2X_CONTEXT_MEM_SIZE * cp->max_cid_space;
1077         blks = total_mem / ctx_blk_size;
1078         if (total_mem % ctx_blk_size)
1079                 blks++;
1080
1081         if (blks > cp->ethdev->ctx_tbl_len)
1082                 return -ENOMEM;
1083
1084         cp->ctx_arr = kcalloc(blks, sizeof(struct cnic_ctx), GFP_KERNEL);
1085         if (cp->ctx_arr == NULL)
1086                 return -ENOMEM;
1087
1088         cp->ctx_blks = blks;
1089         cp->ctx_blk_size = ctx_blk_size;
1090         if (!BNX2X_CHIP_IS_57710(cp->chip_id))
1091                 cp->ctx_align = 0;
1092         else
1093                 cp->ctx_align = ctx_blk_size;
1094
1095         cp->cids_per_blk = ctx_blk_size / BNX2X_CONTEXT_MEM_SIZE;
1096
1097         for (i = 0; i < blks; i++) {
1098                 cp->ctx_arr[i].ctx =
1099                         dma_alloc_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
1100                                            &cp->ctx_arr[i].mapping,
1101                                            GFP_KERNEL);
1102                 if (cp->ctx_arr[i].ctx == NULL)
1103                         return -ENOMEM;
1104
1105                 if (cp->ctx_align && cp->ctx_blk_size == ctx_blk_size) {
1106                         if (cp->ctx_arr[i].mapping & (cp->ctx_align - 1)) {
1107                                 cnic_free_context(dev);
1108                                 cp->ctx_blk_size += cp->ctx_align;
1109                                 i = -1;
1110                                 continue;
1111                         }
1112                 }
1113         }
1114         return 0;
1115 }
1116
1117 static int cnic_alloc_bnx2x_resc(struct cnic_dev *dev)
1118 {
1119         struct cnic_local *cp = dev->cnic_priv;
1120         struct cnic_eth_dev *ethdev = cp->ethdev;
1121         u32 start_cid = ethdev->starting_cid;
1122         int i, j, n, ret, pages;
1123         struct cnic_dma *kwq_16_dma = &cp->kwq_16_data_info;
1124
1125         cp->iro_arr = ethdev->iro_arr;
1126
1127         cp->max_cid_space = MAX_ISCSI_TBL_SZ;
1128         cp->iscsi_start_cid = start_cid;
1129         if (start_cid < BNX2X_ISCSI_START_CID) {
1130                 u32 delta = BNX2X_ISCSI_START_CID - start_cid;
1131
1132                 cp->iscsi_start_cid = BNX2X_ISCSI_START_CID;
1133                 cp->max_cid_space += delta;
1134         }
1135
1136         cp->iscsi_tbl = kzalloc(sizeof(struct cnic_iscsi) * MAX_ISCSI_TBL_SZ,
1137                                 GFP_KERNEL);
1138         if (!cp->iscsi_tbl)
1139                 goto error;
1140
1141         cp->ctx_tbl = kzalloc(sizeof(struct cnic_context) *
1142                                 cp->max_cid_space, GFP_KERNEL);
1143         if (!cp->ctx_tbl)
1144                 goto error;
1145
1146         for (i = 0; i < MAX_ISCSI_TBL_SZ; i++) {
1147                 cp->ctx_tbl[i].proto.iscsi = &cp->iscsi_tbl[i];
1148                 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_ISCSI;
1149         }
1150
1151         pages = PAGE_ALIGN(cp->max_cid_space * CNIC_KWQ16_DATA_SIZE) /
1152                 PAGE_SIZE;
1153
1154         ret = cnic_alloc_dma(dev, kwq_16_dma, pages, 0);
1155         if (ret)
1156                 return -ENOMEM;
1157
1158         n = PAGE_SIZE / CNIC_KWQ16_DATA_SIZE;
1159         for (i = 0, j = 0; i < cp->max_cid_space; i++) {
1160                 long off = CNIC_KWQ16_DATA_SIZE * (i % n);
1161
1162                 cp->ctx_tbl[i].kwqe_data = kwq_16_dma->pg_arr[j] + off;
1163                 cp->ctx_tbl[i].kwqe_data_mapping = kwq_16_dma->pg_map_arr[j] +
1164                                                    off;
1165
1166                 if ((i % n) == (n - 1))
1167                         j++;
1168         }
1169
1170         ret = cnic_alloc_kcq(dev, &cp->kcq1);
1171         if (ret)
1172                 goto error;
1173
1174         pages = PAGE_ALIGN(BNX2X_ISCSI_NUM_CONNECTIONS *
1175                            BNX2X_ISCSI_CONN_BUF_SIZE) / PAGE_SIZE;
1176         ret = cnic_alloc_dma(dev, &cp->conn_buf_info, pages, 1);
1177         if (ret)
1178                 goto error;
1179
1180         pages = PAGE_ALIGN(BNX2X_ISCSI_GLB_BUF_SIZE) / PAGE_SIZE;
1181         ret = cnic_alloc_dma(dev, &cp->gbl_buf_info, pages, 0);
1182         if (ret)
1183                 goto error;
1184
1185         ret = cnic_alloc_bnx2x_context(dev);
1186         if (ret)
1187                 goto error;
1188
1189         cp->bnx2x_def_status_blk = cp->ethdev->irq_arr[1].status_blk;
1190
1191         cp->l2_rx_ring_size = 15;
1192
1193         ret = cnic_alloc_uio_rings(dev, 4);
1194         if (ret)
1195                 goto error;
1196
1197         ret = cnic_init_uio(dev);
1198         if (ret)
1199                 goto error;
1200
1201         return 0;
1202
1203 error:
1204         cnic_free_resc(dev);
1205         return -ENOMEM;
1206 }
1207
1208 static inline u32 cnic_kwq_avail(struct cnic_local *cp)
1209 {
1210         return cp->max_kwq_idx -
1211                 ((cp->kwq_prod_idx - cp->kwq_con_idx) & cp->max_kwq_idx);
1212 }
1213
1214 static int cnic_submit_bnx2_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
1215                                   u32 num_wqes)
1216 {
1217         struct cnic_local *cp = dev->cnic_priv;
1218         struct kwqe *prod_qe;
1219         u16 prod, sw_prod, i;
1220
1221         if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
1222                 return -EAGAIN;         /* bnx2 is down */
1223
1224         spin_lock_bh(&cp->cnic_ulp_lock);
1225         if (num_wqes > cnic_kwq_avail(cp) &&
1226             !test_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags)) {
1227                 spin_unlock_bh(&cp->cnic_ulp_lock);
1228                 return -EAGAIN;
1229         }
1230
1231         clear_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
1232
1233         prod = cp->kwq_prod_idx;
1234         sw_prod = prod & MAX_KWQ_IDX;
1235         for (i = 0; i < num_wqes; i++) {
1236                 prod_qe = &cp->kwq[KWQ_PG(sw_prod)][KWQ_IDX(sw_prod)];
1237                 memcpy(prod_qe, wqes[i], sizeof(struct kwqe));
1238                 prod++;
1239                 sw_prod = prod & MAX_KWQ_IDX;
1240         }
1241         cp->kwq_prod_idx = prod;
1242
1243         CNIC_WR16(dev, cp->kwq_io_addr, cp->kwq_prod_idx);
1244
1245         spin_unlock_bh(&cp->cnic_ulp_lock);
1246         return 0;
1247 }
1248
1249 static void *cnic_get_kwqe_16_data(struct cnic_local *cp, u32 l5_cid,
1250                                    union l5cm_specific_data *l5_data)
1251 {
1252         struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1253         dma_addr_t map;
1254
1255         map = ctx->kwqe_data_mapping;
1256         l5_data->phy_address.lo = (u64) map & 0xffffffff;
1257         l5_data->phy_address.hi = (u64) map >> 32;
1258         return ctx->kwqe_data;
1259 }
1260
1261 static int cnic_submit_kwqe_16(struct cnic_dev *dev, u32 cmd, u32 cid,
1262                                 u32 type, union l5cm_specific_data *l5_data)
1263 {
1264         struct cnic_local *cp = dev->cnic_priv;
1265         struct l5cm_spe kwqe;
1266         struct kwqe_16 *kwq[1];
1267         int ret;
1268
1269         kwqe.hdr.conn_and_cmd_data =
1270                 cpu_to_le32(((cmd << SPE_HDR_CMD_ID_SHIFT) |
1271                              BNX2X_HW_CID(cp, cid)));
1272         kwqe.hdr.type = cpu_to_le16(type);
1273         kwqe.hdr.reserved1 = 0;
1274         kwqe.data.phy_address.lo = cpu_to_le32(l5_data->phy_address.lo);
1275         kwqe.data.phy_address.hi = cpu_to_le32(l5_data->phy_address.hi);
1276
1277         kwq[0] = (struct kwqe_16 *) &kwqe;
1278
1279         spin_lock_bh(&cp->cnic_ulp_lock);
1280         ret = cp->ethdev->drv_submit_kwqes_16(dev->netdev, kwq, 1);
1281         spin_unlock_bh(&cp->cnic_ulp_lock);
1282
1283         if (ret == 1)
1284                 return 0;
1285
1286         return -EBUSY;
1287 }
1288
1289 static void cnic_reply_bnx2x_kcqes(struct cnic_dev *dev, int ulp_type,
1290                                    struct kcqe *cqes[], u32 num_cqes)
1291 {
1292         struct cnic_local *cp = dev->cnic_priv;
1293         struct cnic_ulp_ops *ulp_ops;
1294
1295         rcu_read_lock();
1296         ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
1297         if (likely(ulp_ops)) {
1298                 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
1299                                           cqes, num_cqes);
1300         }
1301         rcu_read_unlock();
1302 }
1303
1304 static int cnic_bnx2x_iscsi_init1(struct cnic_dev *dev, struct kwqe *kwqe)
1305 {
1306         struct cnic_local *cp = dev->cnic_priv;
1307         struct iscsi_kwqe_init1 *req1 = (struct iscsi_kwqe_init1 *) kwqe;
1308         int hq_bds, pages;
1309         u32 pfid = cp->pfid;
1310
1311         cp->num_iscsi_tasks = req1->num_tasks_per_conn;
1312         cp->num_ccells = req1->num_ccells_per_conn;
1313         cp->task_array_size = BNX2X_ISCSI_TASK_CONTEXT_SIZE *
1314                               cp->num_iscsi_tasks;
1315         cp->r2tq_size = cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS *
1316                         BNX2X_ISCSI_R2TQE_SIZE;
1317         cp->hq_size = cp->num_ccells * BNX2X_ISCSI_HQ_BD_SIZE;
1318         pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1319         hq_bds = pages * (PAGE_SIZE / BNX2X_ISCSI_HQ_BD_SIZE);
1320         cp->num_cqs = req1->num_cqs;
1321
1322         if (!dev->max_iscsi_conn)
1323                 return 0;
1324
1325         /* init Tstorm RAM */
1326         CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
1327                   req1->rq_num_wqes);
1328         CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
1329                   PAGE_SIZE);
1330         CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
1331                  TSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
1332         CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
1333                   TSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
1334                   req1->num_tasks_per_conn);
1335
1336         /* init Ustorm RAM */
1337         CNIC_WR16(dev, BAR_USTRORM_INTMEM +
1338                   USTORM_ISCSI_RQ_BUFFER_SIZE_OFFSET(pfid),
1339                   req1->rq_buffer_size);
1340         CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
1341                   PAGE_SIZE);
1342         CNIC_WR8(dev, BAR_USTRORM_INTMEM +
1343                  USTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
1344         CNIC_WR16(dev, BAR_USTRORM_INTMEM +
1345                   USTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
1346                   req1->num_tasks_per_conn);
1347         CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
1348                   req1->rq_num_wqes);
1349         CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
1350                   req1->cq_num_wqes);
1351         CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
1352                   cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1353
1354         /* init Xstorm RAM */
1355         CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
1356                   PAGE_SIZE);
1357         CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
1358                  XSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
1359         CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
1360                   XSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
1361                   req1->num_tasks_per_conn);
1362         CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
1363                   hq_bds);
1364         CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_SQ_SIZE_OFFSET(pfid),
1365                   req1->num_tasks_per_conn);
1366         CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
1367                   cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1368
1369         /* init Cstorm RAM */
1370         CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
1371                   PAGE_SIZE);
1372         CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
1373                  CSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
1374         CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
1375                   CSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
1376                   req1->num_tasks_per_conn);
1377         CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
1378                   req1->cq_num_wqes);
1379         CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
1380                   hq_bds);
1381
1382         return 0;
1383 }
1384
1385 static int cnic_bnx2x_iscsi_init2(struct cnic_dev *dev, struct kwqe *kwqe)
1386 {
1387         struct iscsi_kwqe_init2 *req2 = (struct iscsi_kwqe_init2 *) kwqe;
1388         struct cnic_local *cp = dev->cnic_priv;
1389         u32 pfid = cp->pfid;
1390         struct iscsi_kcqe kcqe;
1391         struct kcqe *cqes[1];
1392
1393         memset(&kcqe, 0, sizeof(kcqe));
1394         if (!dev->max_iscsi_conn) {
1395                 kcqe.completion_status =
1396                         ISCSI_KCQE_COMPLETION_STATUS_ISCSI_NOT_SUPPORTED;
1397                 goto done;
1398         }
1399
1400         CNIC_WR(dev, BAR_TSTRORM_INTMEM +
1401                 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
1402         CNIC_WR(dev, BAR_TSTRORM_INTMEM +
1403                 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
1404                 req2->error_bit_map[1]);
1405
1406         CNIC_WR16(dev, BAR_USTRORM_INTMEM +
1407                   USTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
1408         CNIC_WR(dev, BAR_USTRORM_INTMEM +
1409                 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
1410         CNIC_WR(dev, BAR_USTRORM_INTMEM +
1411                 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
1412                 req2->error_bit_map[1]);
1413
1414         CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
1415                   CSTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
1416
1417         kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1418
1419 done:
1420         kcqe.op_code = ISCSI_KCQE_OPCODE_INIT;
1421         cqes[0] = (struct kcqe *) &kcqe;
1422         cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1423
1424         return 0;
1425 }
1426
1427 static void cnic_free_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1428 {
1429         struct cnic_local *cp = dev->cnic_priv;
1430         struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1431
1432         if (ctx->ulp_proto_id == CNIC_ULP_ISCSI) {
1433                 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1434
1435                 cnic_free_dma(dev, &iscsi->hq_info);
1436                 cnic_free_dma(dev, &iscsi->r2tq_info);
1437                 cnic_free_dma(dev, &iscsi->task_array_info);
1438         }
1439         cnic_free_id(&cp->cid_tbl, ctx->cid);
1440         ctx->cid = 0;
1441 }
1442
1443 static int cnic_alloc_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1444 {
1445         u32 cid;
1446         int ret, pages;
1447         struct cnic_local *cp = dev->cnic_priv;
1448         struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1449         struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1450
1451         cid = cnic_alloc_new_id(&cp->cid_tbl);
1452         if (cid == -1) {
1453                 ret = -ENOMEM;
1454                 goto error;
1455         }
1456
1457         ctx->cid = cid;
1458         pages = PAGE_ALIGN(cp->task_array_size) / PAGE_SIZE;
1459
1460         ret = cnic_alloc_dma(dev, &iscsi->task_array_info, pages, 1);
1461         if (ret)
1462                 goto error;
1463
1464         pages = PAGE_ALIGN(cp->r2tq_size) / PAGE_SIZE;
1465         ret = cnic_alloc_dma(dev, &iscsi->r2tq_info, pages, 1);
1466         if (ret)
1467                 goto error;
1468
1469         pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1470         ret = cnic_alloc_dma(dev, &iscsi->hq_info, pages, 1);
1471         if (ret)
1472                 goto error;
1473
1474         return 0;
1475
1476 error:
1477         cnic_free_bnx2x_conn_resc(dev, l5_cid);
1478         return ret;
1479 }
1480
1481 static void *cnic_get_bnx2x_ctx(struct cnic_dev *dev, u32 cid, int init,
1482                                 struct regpair *ctx_addr)
1483 {
1484         struct cnic_local *cp = dev->cnic_priv;
1485         struct cnic_eth_dev *ethdev = cp->ethdev;
1486         int blk = (cid - ethdev->starting_cid) / cp->cids_per_blk;
1487         int off = (cid - ethdev->starting_cid) % cp->cids_per_blk;
1488         unsigned long align_off = 0;
1489         dma_addr_t ctx_map;
1490         void *ctx;
1491
1492         if (cp->ctx_align) {
1493                 unsigned long mask = cp->ctx_align - 1;
1494
1495                 if (cp->ctx_arr[blk].mapping & mask)
1496                         align_off = cp->ctx_align -
1497                                     (cp->ctx_arr[blk].mapping & mask);
1498         }
1499         ctx_map = cp->ctx_arr[blk].mapping + align_off +
1500                 (off * BNX2X_CONTEXT_MEM_SIZE);
1501         ctx = cp->ctx_arr[blk].ctx + align_off +
1502               (off * BNX2X_CONTEXT_MEM_SIZE);
1503         if (init)
1504                 memset(ctx, 0, BNX2X_CONTEXT_MEM_SIZE);
1505
1506         ctx_addr->lo = ctx_map & 0xffffffff;
1507         ctx_addr->hi = (u64) ctx_map >> 32;
1508         return ctx;
1509 }
1510
1511 static int cnic_setup_bnx2x_ctx(struct cnic_dev *dev, struct kwqe *wqes[],
1512                                 u32 num)
1513 {
1514         struct cnic_local *cp = dev->cnic_priv;
1515         struct iscsi_kwqe_conn_offload1 *req1 =
1516                         (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1517         struct iscsi_kwqe_conn_offload2 *req2 =
1518                         (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1519         struct iscsi_kwqe_conn_offload3 *req3;
1520         struct cnic_context *ctx = &cp->ctx_tbl[req1->iscsi_conn_id];
1521         struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1522         u32 cid = ctx->cid;
1523         u32 hw_cid = BNX2X_HW_CID(cp, cid);
1524         struct iscsi_context *ictx;
1525         struct regpair context_addr;
1526         int i, j, n = 2, n_max;
1527
1528         ctx->ctx_flags = 0;
1529         if (!req2->num_additional_wqes)
1530                 return -EINVAL;
1531
1532         n_max = req2->num_additional_wqes + 2;
1533
1534         ictx = cnic_get_bnx2x_ctx(dev, cid, 1, &context_addr);
1535         if (ictx == NULL)
1536                 return -ENOMEM;
1537
1538         req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1539
1540         ictx->xstorm_ag_context.hq_prod = 1;
1541
1542         ictx->xstorm_st_context.iscsi.first_burst_length =
1543                 ISCSI_DEF_FIRST_BURST_LEN;
1544         ictx->xstorm_st_context.iscsi.max_send_pdu_length =
1545                 ISCSI_DEF_MAX_RECV_SEG_LEN;
1546         ictx->xstorm_st_context.iscsi.sq_pbl_base.lo =
1547                 req1->sq_page_table_addr_lo;
1548         ictx->xstorm_st_context.iscsi.sq_pbl_base.hi =
1549                 req1->sq_page_table_addr_hi;
1550         ictx->xstorm_st_context.iscsi.sq_curr_pbe.lo = req2->sq_first_pte.hi;
1551         ictx->xstorm_st_context.iscsi.sq_curr_pbe.hi = req2->sq_first_pte.lo;
1552         ictx->xstorm_st_context.iscsi.hq_pbl_base.lo =
1553                 iscsi->hq_info.pgtbl_map & 0xffffffff;
1554         ictx->xstorm_st_context.iscsi.hq_pbl_base.hi =
1555                 (u64) iscsi->hq_info.pgtbl_map >> 32;
1556         ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.lo =
1557                 iscsi->hq_info.pgtbl[0];
1558         ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.hi =
1559                 iscsi->hq_info.pgtbl[1];
1560         ictx->xstorm_st_context.iscsi.r2tq_pbl_base.lo =
1561                 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1562         ictx->xstorm_st_context.iscsi.r2tq_pbl_base.hi =
1563                 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1564         ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.lo =
1565                 iscsi->r2tq_info.pgtbl[0];
1566         ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.hi =
1567                 iscsi->r2tq_info.pgtbl[1];
1568         ictx->xstorm_st_context.iscsi.task_pbl_base.lo =
1569                 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1570         ictx->xstorm_st_context.iscsi.task_pbl_base.hi =
1571                 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1572         ictx->xstorm_st_context.iscsi.task_pbl_cache_idx =
1573                 BNX2X_ISCSI_PBL_NOT_CACHED;
1574         ictx->xstorm_st_context.iscsi.flags.flags |=
1575                 XSTORM_ISCSI_CONTEXT_FLAGS_B_IMMEDIATE_DATA;
1576         ictx->xstorm_st_context.iscsi.flags.flags |=
1577                 XSTORM_ISCSI_CONTEXT_FLAGS_B_INITIAL_R2T;
1578
1579         ictx->tstorm_st_context.iscsi.hdr_bytes_2_fetch = ISCSI_HEADER_SIZE;
1580         /* TSTORM requires the base address of RQ DB & not PTE */
1581         ictx->tstorm_st_context.iscsi.rq_db_phy_addr.lo =
1582                 req2->rq_page_table_addr_lo & PAGE_MASK;
1583         ictx->tstorm_st_context.iscsi.rq_db_phy_addr.hi =
1584                 req2->rq_page_table_addr_hi;
1585         ictx->tstorm_st_context.iscsi.iscsi_conn_id = req1->iscsi_conn_id;
1586         ictx->tstorm_st_context.tcp.cwnd = 0x5A8;
1587         ictx->tstorm_st_context.tcp.flags2 |=
1588                 TSTORM_TCP_ST_CONTEXT_SECTION_DA_EN;
1589         ictx->tstorm_st_context.tcp.ooo_support_mode =
1590                 TCP_TSTORM_OOO_DROP_AND_PROC_ACK;
1591
1592         ictx->timers_context.flags |= TIMERS_BLOCK_CONTEXT_CONN_VALID_FLG;
1593
1594         ictx->ustorm_st_context.ring.rq.pbl_base.lo =
1595                 req2->rq_page_table_addr_lo;
1596         ictx->ustorm_st_context.ring.rq.pbl_base.hi =
1597                 req2->rq_page_table_addr_hi;
1598         ictx->ustorm_st_context.ring.rq.curr_pbe.lo = req3->qp_first_pte[0].hi;
1599         ictx->ustorm_st_context.ring.rq.curr_pbe.hi = req3->qp_first_pte[0].lo;
1600         ictx->ustorm_st_context.ring.r2tq.pbl_base.lo =
1601                 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1602         ictx->ustorm_st_context.ring.r2tq.pbl_base.hi =
1603                 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1604         ictx->ustorm_st_context.ring.r2tq.curr_pbe.lo =
1605                 iscsi->r2tq_info.pgtbl[0];
1606         ictx->ustorm_st_context.ring.r2tq.curr_pbe.hi =
1607                 iscsi->r2tq_info.pgtbl[1];
1608         ictx->ustorm_st_context.ring.cq_pbl_base.lo =
1609                 req1->cq_page_table_addr_lo;
1610         ictx->ustorm_st_context.ring.cq_pbl_base.hi =
1611                 req1->cq_page_table_addr_hi;
1612         ictx->ustorm_st_context.ring.cq[0].cq_sn = ISCSI_INITIAL_SN;
1613         ictx->ustorm_st_context.ring.cq[0].curr_pbe.lo = req2->cq_first_pte.hi;
1614         ictx->ustorm_st_context.ring.cq[0].curr_pbe.hi = req2->cq_first_pte.lo;
1615         ictx->ustorm_st_context.task_pbe_cache_index =
1616                 BNX2X_ISCSI_PBL_NOT_CACHED;
1617         ictx->ustorm_st_context.task_pdu_cache_index =
1618                 BNX2X_ISCSI_PDU_HEADER_NOT_CACHED;
1619
1620         for (i = 1, j = 1; i < cp->num_cqs; i++, j++) {
1621                 if (j == 3) {
1622                         if (n >= n_max)
1623                                 break;
1624                         req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1625                         j = 0;
1626                 }
1627                 ictx->ustorm_st_context.ring.cq[i].cq_sn = ISCSI_INITIAL_SN;
1628                 ictx->ustorm_st_context.ring.cq[i].curr_pbe.lo =
1629                         req3->qp_first_pte[j].hi;
1630                 ictx->ustorm_st_context.ring.cq[i].curr_pbe.hi =
1631                         req3->qp_first_pte[j].lo;
1632         }
1633
1634         ictx->ustorm_st_context.task_pbl_base.lo =
1635                 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1636         ictx->ustorm_st_context.task_pbl_base.hi =
1637                 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1638         ictx->ustorm_st_context.tce_phy_addr.lo =
1639                 iscsi->task_array_info.pgtbl[0];
1640         ictx->ustorm_st_context.tce_phy_addr.hi =
1641                 iscsi->task_array_info.pgtbl[1];
1642         ictx->ustorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1643         ictx->ustorm_st_context.num_cqs = cp->num_cqs;
1644         ictx->ustorm_st_context.negotiated_rx |= ISCSI_DEF_MAX_RECV_SEG_LEN;
1645         ictx->ustorm_st_context.negotiated_rx_and_flags |=
1646                 ISCSI_DEF_MAX_BURST_LEN;
1647         ictx->ustorm_st_context.negotiated_rx |=
1648                 ISCSI_DEFAULT_MAX_OUTSTANDING_R2T <<
1649                 USTORM_ISCSI_ST_CONTEXT_MAX_OUTSTANDING_R2TS_SHIFT;
1650
1651         ictx->cstorm_st_context.hq_pbl_base.lo =
1652                 iscsi->hq_info.pgtbl_map & 0xffffffff;
1653         ictx->cstorm_st_context.hq_pbl_base.hi =
1654                 (u64) iscsi->hq_info.pgtbl_map >> 32;
1655         ictx->cstorm_st_context.hq_curr_pbe.lo = iscsi->hq_info.pgtbl[0];
1656         ictx->cstorm_st_context.hq_curr_pbe.hi = iscsi->hq_info.pgtbl[1];
1657         ictx->cstorm_st_context.task_pbl_base.lo =
1658                 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1659         ictx->cstorm_st_context.task_pbl_base.hi =
1660                 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1661         /* CSTORM and USTORM initialization is different, CSTORM requires
1662          * CQ DB base & not PTE addr */
1663         ictx->cstorm_st_context.cq_db_base.lo =
1664                 req1->cq_page_table_addr_lo & PAGE_MASK;
1665         ictx->cstorm_st_context.cq_db_base.hi = req1->cq_page_table_addr_hi;
1666         ictx->cstorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1667         ictx->cstorm_st_context.cq_proc_en_bit_map = (1 << cp->num_cqs) - 1;
1668         for (i = 0; i < cp->num_cqs; i++) {
1669                 ictx->cstorm_st_context.cq_c_prod_sqn_arr.sqn[i] =
1670                         ISCSI_INITIAL_SN;
1671                 ictx->cstorm_st_context.cq_c_sqn_2_notify_arr.sqn[i] =
1672                         ISCSI_INITIAL_SN;
1673         }
1674
1675         ictx->xstorm_ag_context.cdu_reserved =
1676                 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
1677                                        ISCSI_CONNECTION_TYPE);
1678         ictx->ustorm_ag_context.cdu_usage =
1679                 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
1680                                        ISCSI_CONNECTION_TYPE);
1681         return 0;
1682
1683 }
1684
1685 static int cnic_bnx2x_iscsi_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
1686                                    u32 num, int *work)
1687 {
1688         struct iscsi_kwqe_conn_offload1 *req1;
1689         struct iscsi_kwqe_conn_offload2 *req2;
1690         struct cnic_local *cp = dev->cnic_priv;
1691         struct cnic_context *ctx;
1692         struct iscsi_kcqe kcqe;
1693         struct kcqe *cqes[1];
1694         u32 l5_cid;
1695         int ret = 0;
1696
1697         if (num < 2) {
1698                 *work = num;
1699                 return -EINVAL;
1700         }
1701
1702         req1 = (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1703         req2 = (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1704         if ((num - 2) < req2->num_additional_wqes) {
1705                 *work = num;
1706                 return -EINVAL;
1707         }
1708         *work = 2 + req2->num_additional_wqes;
1709
1710         l5_cid = req1->iscsi_conn_id;
1711         if (l5_cid >= MAX_ISCSI_TBL_SZ)
1712                 return -EINVAL;
1713
1714         memset(&kcqe, 0, sizeof(kcqe));
1715         kcqe.op_code = ISCSI_KCQE_OPCODE_OFFLOAD_CONN;
1716         kcqe.iscsi_conn_id = l5_cid;
1717         kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
1718
1719         ctx = &cp->ctx_tbl[l5_cid];
1720         if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags)) {
1721                 kcqe.completion_status =
1722                         ISCSI_KCQE_COMPLETION_STATUS_CID_BUSY;
1723                 goto done;
1724         }
1725
1726         if (atomic_inc_return(&cp->iscsi_conn) > dev->max_iscsi_conn) {
1727                 atomic_dec(&cp->iscsi_conn);
1728                 goto done;
1729         }
1730         ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
1731         if (ret) {
1732                 atomic_dec(&cp->iscsi_conn);
1733                 ret = 0;
1734                 goto done;
1735         }
1736         ret = cnic_setup_bnx2x_ctx(dev, wqes, num);
1737         if (ret < 0) {
1738                 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1739                 atomic_dec(&cp->iscsi_conn);
1740                 goto done;
1741         }
1742
1743         kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1744         kcqe.iscsi_conn_context_id = BNX2X_HW_CID(cp, cp->ctx_tbl[l5_cid].cid);
1745
1746 done:
1747         cqes[0] = (struct kcqe *) &kcqe;
1748         cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1749         return ret;
1750 }
1751
1752
1753 static int cnic_bnx2x_iscsi_update(struct cnic_dev *dev, struct kwqe *kwqe)
1754 {
1755         struct cnic_local *cp = dev->cnic_priv;
1756         struct iscsi_kwqe_conn_update *req =
1757                 (struct iscsi_kwqe_conn_update *) kwqe;
1758         void *data;
1759         union l5cm_specific_data l5_data;
1760         u32 l5_cid, cid = BNX2X_SW_CID(req->context_id);
1761         int ret;
1762
1763         if (cnic_get_l5_cid(cp, cid, &l5_cid) != 0)
1764                 return -EINVAL;
1765
1766         data = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
1767         if (!data)
1768                 return -ENOMEM;
1769
1770         memcpy(data, kwqe, sizeof(struct kwqe));
1771
1772         ret = cnic_submit_kwqe_16(dev, ISCSI_RAMROD_CMD_ID_UPDATE_CONN,
1773                         req->context_id, ISCSI_CONNECTION_TYPE, &l5_data);
1774         return ret;
1775 }
1776
1777 static int cnic_bnx2x_destroy_ramrod(struct cnic_dev *dev, u32 l5_cid)
1778 {
1779         struct cnic_local *cp = dev->cnic_priv;
1780         struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1781         union l5cm_specific_data l5_data;
1782         int ret;
1783         u32 hw_cid, type;
1784
1785         init_waitqueue_head(&ctx->waitq);
1786         ctx->wait_cond = 0;
1787         memset(&l5_data, 0, sizeof(l5_data));
1788         hw_cid = BNX2X_HW_CID(cp, ctx->cid);
1789         type = (NONE_CONNECTION_TYPE << SPE_HDR_CONN_TYPE_SHIFT)
1790                 & SPE_HDR_CONN_TYPE;
1791         type |= ((cp->pfid << SPE_HDR_FUNCTION_ID_SHIFT) &
1792                  SPE_HDR_FUNCTION_ID);
1793
1794         ret = cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
1795                                   hw_cid, type, &l5_data);
1796
1797         if (ret == 0)
1798                 wait_event(ctx->waitq, ctx->wait_cond);
1799
1800         return ret;
1801 }
1802
1803 static int cnic_bnx2x_iscsi_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
1804 {
1805         struct cnic_local *cp = dev->cnic_priv;
1806         struct iscsi_kwqe_conn_destroy *req =
1807                 (struct iscsi_kwqe_conn_destroy *) kwqe;
1808         u32 l5_cid = req->reserved0;
1809         struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1810         int ret = 0;
1811         struct iscsi_kcqe kcqe;
1812         struct kcqe *cqes[1];
1813
1814         if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
1815                 goto skip_cfc_delete;
1816
1817         if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
1818                 unsigned long delta = ctx->timestamp + (2 * HZ) - jiffies;
1819
1820                 if (delta > (2 * HZ))
1821                         delta = 0;
1822
1823                 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
1824                 queue_delayed_work(cnic_wq, &cp->delete_task, delta);
1825                 goto destroy_reply;
1826         }
1827
1828         ret = cnic_bnx2x_destroy_ramrod(dev, l5_cid);
1829
1830 skip_cfc_delete:
1831         cnic_free_bnx2x_conn_resc(dev, l5_cid);
1832
1833         atomic_dec(&cp->iscsi_conn);
1834         clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
1835
1836 destroy_reply:
1837         memset(&kcqe, 0, sizeof(kcqe));
1838         kcqe.op_code = ISCSI_KCQE_OPCODE_DESTROY_CONN;
1839         kcqe.iscsi_conn_id = l5_cid;
1840         kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1841         kcqe.iscsi_conn_context_id = req->context_id;
1842
1843         cqes[0] = (struct kcqe *) &kcqe;
1844         cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1845
1846         return ret;
1847 }
1848
1849 static void cnic_init_storm_conn_bufs(struct cnic_dev *dev,
1850                                       struct l4_kwq_connect_req1 *kwqe1,
1851                                       struct l4_kwq_connect_req3 *kwqe3,
1852                                       struct l5cm_active_conn_buffer *conn_buf)
1853 {
1854         struct l5cm_conn_addr_params *conn_addr = &conn_buf->conn_addr_buf;
1855         struct l5cm_xstorm_conn_buffer *xstorm_buf =
1856                 &conn_buf->xstorm_conn_buffer;
1857         struct l5cm_tstorm_conn_buffer *tstorm_buf =
1858                 &conn_buf->tstorm_conn_buffer;
1859         struct regpair context_addr;
1860         u32 cid = BNX2X_SW_CID(kwqe1->cid);
1861         struct in6_addr src_ip, dst_ip;
1862         int i;
1863         u32 *addrp;
1864
1865         addrp = (u32 *) &conn_addr->local_ip_addr;
1866         for (i = 0; i < 4; i++, addrp++)
1867                 src_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1868
1869         addrp = (u32 *) &conn_addr->remote_ip_addr;
1870         for (i = 0; i < 4; i++, addrp++)
1871                 dst_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1872
1873         cnic_get_bnx2x_ctx(dev, cid, 0, &context_addr);
1874
1875         xstorm_buf->context_addr.hi = context_addr.hi;
1876         xstorm_buf->context_addr.lo = context_addr.lo;
1877         xstorm_buf->mss = 0xffff;
1878         xstorm_buf->rcv_buf = kwqe3->rcv_buf;
1879         if (kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE)
1880                 xstorm_buf->params |= L5CM_XSTORM_CONN_BUFFER_NAGLE_ENABLE;
1881         xstorm_buf->pseudo_header_checksum =
1882                 swab16(~csum_ipv6_magic(&src_ip, &dst_ip, 0, IPPROTO_TCP, 0));
1883
1884         if (!(kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK))
1885                 tstorm_buf->params |=
1886                         L5CM_TSTORM_CONN_BUFFER_DELAYED_ACK_ENABLE;
1887         if (kwqe3->ka_timeout) {
1888                 tstorm_buf->ka_enable = 1;
1889                 tstorm_buf->ka_timeout = kwqe3->ka_timeout;
1890                 tstorm_buf->ka_interval = kwqe3->ka_interval;
1891                 tstorm_buf->ka_max_probe_count = kwqe3->ka_max_probe_count;
1892         }
1893         tstorm_buf->rcv_buf = kwqe3->rcv_buf;
1894         tstorm_buf->snd_buf = kwqe3->snd_buf;
1895         tstorm_buf->max_rt_time = 0xffffffff;
1896 }
1897
1898 static void cnic_init_bnx2x_mac(struct cnic_dev *dev)
1899 {
1900         struct cnic_local *cp = dev->cnic_priv;
1901         u32 pfid = cp->pfid;
1902         u8 *mac = dev->mac_addr;
1903
1904         CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
1905                  XSTORM_ISCSI_LOCAL_MAC_ADDR0_OFFSET(pfid), mac[0]);
1906         CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
1907                  XSTORM_ISCSI_LOCAL_MAC_ADDR1_OFFSET(pfid), mac[1]);
1908         CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
1909                  XSTORM_ISCSI_LOCAL_MAC_ADDR2_OFFSET(pfid), mac[2]);
1910         CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
1911                  XSTORM_ISCSI_LOCAL_MAC_ADDR3_OFFSET(pfid), mac[3]);
1912         CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
1913                  XSTORM_ISCSI_LOCAL_MAC_ADDR4_OFFSET(pfid), mac[4]);
1914         CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
1915                  XSTORM_ISCSI_LOCAL_MAC_ADDR5_OFFSET(pfid), mac[5]);
1916
1917         CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
1918                  TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[5]);
1919         CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
1920                  TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
1921                  mac[4]);
1922         CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
1923                  TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[3]);
1924         CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
1925                  TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
1926                  mac[2]);
1927         CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
1928                  TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 2,
1929                  mac[1]);
1930         CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
1931                  TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 3,
1932                  mac[0]);
1933 }
1934
1935 static void cnic_bnx2x_set_tcp_timestamp(struct cnic_dev *dev, int tcp_ts)
1936 {
1937         struct cnic_local *cp = dev->cnic_priv;
1938         u8 xstorm_flags = XSTORM_L5CM_TCP_FLAGS_WND_SCL_EN;
1939         u16 tstorm_flags = 0;
1940
1941         if (tcp_ts) {
1942                 xstorm_flags |= XSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
1943                 tstorm_flags |= TSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
1944         }
1945
1946         CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
1947                  XSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), xstorm_flags);
1948
1949         CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
1950                   TSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), tstorm_flags);
1951 }
1952
1953 static int cnic_bnx2x_connect(struct cnic_dev *dev, struct kwqe *wqes[],
1954                               u32 num, int *work)
1955 {
1956         struct cnic_local *cp = dev->cnic_priv;
1957         struct l4_kwq_connect_req1 *kwqe1 =
1958                 (struct l4_kwq_connect_req1 *) wqes[0];
1959         struct l4_kwq_connect_req3 *kwqe3;
1960         struct l5cm_active_conn_buffer *conn_buf;
1961         struct l5cm_conn_addr_params *conn_addr;
1962         union l5cm_specific_data l5_data;
1963         u32 l5_cid = kwqe1->pg_cid;
1964         struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
1965         struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1966         int ret;
1967
1968         if (num < 2) {
1969                 *work = num;
1970                 return -EINVAL;
1971         }
1972
1973         if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6)
1974                 *work = 3;
1975         else
1976                 *work = 2;
1977
1978         if (num < *work) {
1979                 *work = num;
1980                 return -EINVAL;
1981         }
1982
1983         if (sizeof(*conn_buf) > CNIC_KWQ16_DATA_SIZE) {
1984                 netdev_err(dev->netdev, "conn_buf size too big\n");
1985                 return -ENOMEM;
1986         }
1987         conn_buf = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
1988         if (!conn_buf)
1989                 return -ENOMEM;
1990
1991         memset(conn_buf, 0, sizeof(*conn_buf));
1992
1993         conn_addr = &conn_buf->conn_addr_buf;
1994         conn_addr->remote_addr_0 = csk->ha[0];
1995         conn_addr->remote_addr_1 = csk->ha[1];
1996         conn_addr->remote_addr_2 = csk->ha[2];
1997         conn_addr->remote_addr_3 = csk->ha[3];
1998         conn_addr->remote_addr_4 = csk->ha[4];
1999         conn_addr->remote_addr_5 = csk->ha[5];
2000
2001         if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6) {
2002                 struct l4_kwq_connect_req2 *kwqe2 =
2003                         (struct l4_kwq_connect_req2 *) wqes[1];
2004
2005                 conn_addr->local_ip_addr.ip_addr_hi_hi = kwqe2->src_ip_v6_4;
2006                 conn_addr->local_ip_addr.ip_addr_hi_lo = kwqe2->src_ip_v6_3;
2007                 conn_addr->local_ip_addr.ip_addr_lo_hi = kwqe2->src_ip_v6_2;
2008
2009                 conn_addr->remote_ip_addr.ip_addr_hi_hi = kwqe2->dst_ip_v6_4;
2010                 conn_addr->remote_ip_addr.ip_addr_hi_lo = kwqe2->dst_ip_v6_3;
2011                 conn_addr->remote_ip_addr.ip_addr_lo_hi = kwqe2->dst_ip_v6_2;
2012                 conn_addr->params |= L5CM_CONN_ADDR_PARAMS_IP_VERSION;
2013         }
2014         kwqe3 = (struct l4_kwq_connect_req3 *) wqes[*work - 1];
2015
2016         conn_addr->local_ip_addr.ip_addr_lo_lo = kwqe1->src_ip;
2017         conn_addr->remote_ip_addr.ip_addr_lo_lo = kwqe1->dst_ip;
2018         conn_addr->local_tcp_port = kwqe1->src_port;
2019         conn_addr->remote_tcp_port = kwqe1->dst_port;
2020
2021         conn_addr->pmtu = kwqe3->pmtu;
2022         cnic_init_storm_conn_bufs(dev, kwqe1, kwqe3, conn_buf);
2023
2024         CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
2025                   XSTORM_ISCSI_LOCAL_VLAN_OFFSET(cp->pfid), csk->vlan_id);
2026
2027         cnic_bnx2x_set_tcp_timestamp(dev,
2028                 kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_TIME_STAMP);
2029
2030         ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_TCP_CONNECT,
2031                         kwqe1->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2032         if (!ret)
2033                 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
2034
2035         return ret;
2036 }
2037
2038 static int cnic_bnx2x_close(struct cnic_dev *dev, struct kwqe *kwqe)
2039 {
2040         struct l4_kwq_close_req *req = (struct l4_kwq_close_req *) kwqe;
2041         union l5cm_specific_data l5_data;
2042         int ret;
2043
2044         memset(&l5_data, 0, sizeof(l5_data));
2045         ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_CLOSE,
2046                         req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2047         return ret;
2048 }
2049
2050 static int cnic_bnx2x_reset(struct cnic_dev *dev, struct kwqe *kwqe)
2051 {
2052         struct l4_kwq_reset_req *req = (struct l4_kwq_reset_req *) kwqe;
2053         union l5cm_specific_data l5_data;
2054         int ret;
2055
2056         memset(&l5_data, 0, sizeof(l5_data));
2057         ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_ABORT,
2058                         req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2059         return ret;
2060 }
2061 static int cnic_bnx2x_offload_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2062 {
2063         struct l4_kwq_offload_pg *req = (struct l4_kwq_offload_pg *) kwqe;
2064         struct l4_kcq kcqe;
2065         struct kcqe *cqes[1];
2066
2067         memset(&kcqe, 0, sizeof(kcqe));
2068         kcqe.pg_host_opaque = req->host_opaque;
2069         kcqe.pg_cid = req->host_opaque;
2070         kcqe.op_code = L4_KCQE_OPCODE_VALUE_OFFLOAD_PG;
2071         cqes[0] = (struct kcqe *) &kcqe;
2072         cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2073         return 0;
2074 }
2075
2076 static int cnic_bnx2x_update_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2077 {
2078         struct l4_kwq_update_pg *req = (struct l4_kwq_update_pg *) kwqe;
2079         struct l4_kcq kcqe;
2080         struct kcqe *cqes[1];
2081
2082         memset(&kcqe, 0, sizeof(kcqe));
2083         kcqe.pg_host_opaque = req->pg_host_opaque;
2084         kcqe.pg_cid = req->pg_cid;
2085         kcqe.op_code = L4_KCQE_OPCODE_VALUE_UPDATE_PG;
2086         cqes[0] = (struct kcqe *) &kcqe;
2087         cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2088         return 0;
2089 }
2090
2091 static int cnic_submit_bnx2x_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
2092                                    u32 num_wqes)
2093 {
2094         int i, work, ret;
2095         u32 opcode;
2096         struct kwqe *kwqe;
2097
2098         if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2099                 return -EAGAIN;         /* bnx2 is down */
2100
2101         for (i = 0; i < num_wqes; ) {
2102                 kwqe = wqes[i];
2103                 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2104                 work = 1;
2105
2106                 switch (opcode) {
2107                 case ISCSI_KWQE_OPCODE_INIT1:
2108                         ret = cnic_bnx2x_iscsi_init1(dev, kwqe);
2109                         break;
2110                 case ISCSI_KWQE_OPCODE_INIT2:
2111                         ret = cnic_bnx2x_iscsi_init2(dev, kwqe);
2112                         break;
2113                 case ISCSI_KWQE_OPCODE_OFFLOAD_CONN1:
2114                         ret = cnic_bnx2x_iscsi_ofld1(dev, &wqes[i],
2115                                                      num_wqes - i, &work);
2116                         break;
2117                 case ISCSI_KWQE_OPCODE_UPDATE_CONN:
2118                         ret = cnic_bnx2x_iscsi_update(dev, kwqe);
2119                         break;
2120                 case ISCSI_KWQE_OPCODE_DESTROY_CONN:
2121                         ret = cnic_bnx2x_iscsi_destroy(dev, kwqe);
2122                         break;
2123                 case L4_KWQE_OPCODE_VALUE_CONNECT1:
2124                         ret = cnic_bnx2x_connect(dev, &wqes[i], num_wqes - i,
2125                                                  &work);
2126                         break;
2127                 case L4_KWQE_OPCODE_VALUE_CLOSE:
2128                         ret = cnic_bnx2x_close(dev, kwqe);
2129                         break;
2130                 case L4_KWQE_OPCODE_VALUE_RESET:
2131                         ret = cnic_bnx2x_reset(dev, kwqe);
2132                         break;
2133                 case L4_KWQE_OPCODE_VALUE_OFFLOAD_PG:
2134                         ret = cnic_bnx2x_offload_pg(dev, kwqe);
2135                         break;
2136                 case L4_KWQE_OPCODE_VALUE_UPDATE_PG:
2137                         ret = cnic_bnx2x_update_pg(dev, kwqe);
2138                         break;
2139                 case L4_KWQE_OPCODE_VALUE_UPLOAD_PG:
2140                         ret = 0;
2141                         break;
2142                 default:
2143                         ret = 0;
2144                         netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2145                                    opcode);
2146                         break;
2147                 }
2148                 if (ret < 0)
2149                         netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2150                                    opcode);
2151                 i += work;
2152         }
2153         return 0;
2154 }
2155
2156 static void service_kcqes(struct cnic_dev *dev, int num_cqes)
2157 {
2158         struct cnic_local *cp = dev->cnic_priv;
2159         int i, j, comp = 0;
2160
2161         i = 0;
2162         j = 1;
2163         while (num_cqes) {
2164                 struct cnic_ulp_ops *ulp_ops;
2165                 int ulp_type;
2166                 u32 kcqe_op_flag = cp->completed_kcq[i]->kcqe_op_flag;
2167                 u32 kcqe_layer = kcqe_op_flag & KCQE_FLAGS_LAYER_MASK;
2168
2169                 if (unlikely(kcqe_op_flag & KCQE_RAMROD_COMPLETION))
2170                         comp++;
2171
2172                 while (j < num_cqes) {
2173                         u32 next_op = cp->completed_kcq[i + j]->kcqe_op_flag;
2174
2175                         if ((next_op & KCQE_FLAGS_LAYER_MASK) != kcqe_layer)
2176                                 break;
2177
2178                         if (unlikely(next_op & KCQE_RAMROD_COMPLETION))
2179                                 comp++;
2180                         j++;
2181                 }
2182
2183                 if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_RDMA)
2184                         ulp_type = CNIC_ULP_RDMA;
2185                 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_ISCSI)
2186                         ulp_type = CNIC_ULP_ISCSI;
2187                 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L4)
2188                         ulp_type = CNIC_ULP_L4;
2189                 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L2)
2190                         goto end;
2191                 else {
2192                         netdev_err(dev->netdev, "Unknown type of KCQE(0x%x)\n",
2193                                    kcqe_op_flag);
2194                         goto end;
2195                 }
2196
2197                 rcu_read_lock();
2198                 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
2199                 if (likely(ulp_ops)) {
2200                         ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
2201                                                   cp->completed_kcq + i, j);
2202                 }
2203                 rcu_read_unlock();
2204 end:
2205                 num_cqes -= j;
2206                 i += j;
2207                 j = 1;
2208         }
2209         if (unlikely(comp))
2210                 cnic_spq_completion(dev, DRV_CTL_RET_L5_SPQ_CREDIT_CMD, comp);
2211 }
2212
2213 static u16 cnic_bnx2_next_idx(u16 idx)
2214 {
2215         return idx + 1;
2216 }
2217
2218 static u16 cnic_bnx2_hw_idx(u16 idx)
2219 {
2220         return idx;
2221 }
2222
2223 static u16 cnic_bnx2x_next_idx(u16 idx)
2224 {
2225         idx++;
2226         if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
2227                 idx++;
2228
2229         return idx;
2230 }
2231
2232 static u16 cnic_bnx2x_hw_idx(u16 idx)
2233 {
2234         if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
2235                 idx++;
2236         return idx;
2237 }
2238
2239 static int cnic_get_kcqes(struct cnic_dev *dev, struct kcq_info *info)
2240 {
2241         struct cnic_local *cp = dev->cnic_priv;
2242         u16 i, ri, hw_prod, last;
2243         struct kcqe *kcqe;
2244         int kcqe_cnt = 0, last_cnt = 0;
2245
2246         i = ri = last = info->sw_prod_idx;
2247         ri &= MAX_KCQ_IDX;
2248         hw_prod = *info->hw_prod_idx_ptr;
2249         hw_prod = cp->hw_idx(hw_prod);
2250
2251         while ((i != hw_prod) && (kcqe_cnt < MAX_COMPLETED_KCQE)) {
2252                 kcqe = &info->kcq[KCQ_PG(ri)][KCQ_IDX(ri)];
2253                 cp->completed_kcq[kcqe_cnt++] = kcqe;
2254                 i = cp->next_idx(i);
2255                 ri = i & MAX_KCQ_IDX;
2256                 if (likely(!(kcqe->kcqe_op_flag & KCQE_FLAGS_NEXT))) {
2257                         last_cnt = kcqe_cnt;
2258                         last = i;
2259                 }
2260         }
2261
2262         info->sw_prod_idx = last;
2263         return last_cnt;
2264 }
2265
2266 static int cnic_l2_completion(struct cnic_local *cp)
2267 {
2268         u16 hw_cons, sw_cons;
2269         struct cnic_uio_dev *udev = cp->udev;
2270         union eth_rx_cqe *cqe, *cqe_ring = (union eth_rx_cqe *)
2271                                         (udev->l2_ring + (2 * BCM_PAGE_SIZE));
2272         u32 cmd;
2273         int comp = 0;
2274
2275         if (!test_bit(CNIC_F_BNX2X_CLASS, &cp->dev->flags))
2276                 return 0;
2277
2278         hw_cons = *cp->rx_cons_ptr;
2279         if ((hw_cons & BNX2X_MAX_RCQ_DESC_CNT) == BNX2X_MAX_RCQ_DESC_CNT)
2280                 hw_cons++;
2281
2282         sw_cons = cp->rx_cons;
2283         while (sw_cons != hw_cons) {
2284                 u8 cqe_fp_flags;
2285
2286                 cqe = &cqe_ring[sw_cons & BNX2X_MAX_RCQ_DESC_CNT];
2287                 cqe_fp_flags = cqe->fast_path_cqe.type_error_flags;
2288                 if (cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE) {
2289                         cmd = le32_to_cpu(cqe->ramrod_cqe.conn_and_cmd_data);
2290                         cmd >>= COMMON_RAMROD_ETH_RX_CQE_CMD_ID_SHIFT;
2291                         if (cmd == RAMROD_CMD_ID_ETH_CLIENT_SETUP ||
2292                             cmd == RAMROD_CMD_ID_ETH_HALT)
2293                                 comp++;
2294                 }
2295                 sw_cons = BNX2X_NEXT_RCQE(sw_cons);
2296         }
2297         return comp;
2298 }
2299
2300 static void cnic_chk_pkt_rings(struct cnic_local *cp)
2301 {
2302         u16 rx_cons, tx_cons;
2303         int comp = 0;
2304
2305         if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
2306                 return;
2307
2308         rx_cons = *cp->rx_cons_ptr;
2309         tx_cons = *cp->tx_cons_ptr;
2310         if (cp->tx_cons != tx_cons || cp->rx_cons != rx_cons) {
2311                 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
2312                         comp = cnic_l2_completion(cp);
2313
2314                 cp->tx_cons = tx_cons;
2315                 cp->rx_cons = rx_cons;
2316
2317                 if (cp->udev)
2318                         uio_event_notify(&cp->udev->cnic_uinfo);
2319         }
2320         if (comp)
2321                 clear_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
2322 }
2323
2324 static u32 cnic_service_bnx2_queues(struct cnic_dev *dev)
2325 {
2326         struct cnic_local *cp = dev->cnic_priv;
2327         u32 status_idx = (u16) *cp->kcq1.status_idx_ptr;
2328         int kcqe_cnt;
2329
2330         cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
2331
2332         while ((kcqe_cnt = cnic_get_kcqes(dev, &cp->kcq1))) {
2333
2334                 service_kcqes(dev, kcqe_cnt);
2335
2336                 /* Tell compiler that status_blk fields can change. */
2337                 barrier();
2338                 if (status_idx != *cp->kcq1.status_idx_ptr) {
2339                         status_idx = (u16) *cp->kcq1.status_idx_ptr;
2340                         cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
2341                 } else
2342                         break;
2343         }
2344
2345         CNIC_WR16(dev, cp->kcq1.io_addr, cp->kcq1.sw_prod_idx);
2346
2347         cnic_chk_pkt_rings(cp);
2348
2349         return status_idx;
2350 }
2351
2352 static int cnic_service_bnx2(void *data, void *status_blk)
2353 {
2354         struct cnic_dev *dev = data;
2355         struct cnic_local *cp = dev->cnic_priv;
2356         u32 status_idx = *cp->kcq1.status_idx_ptr;
2357
2358         if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
2359                 return status_idx;
2360
2361         return cnic_service_bnx2_queues(dev);
2362 }
2363
2364 static void cnic_service_bnx2_msix(unsigned long data)
2365 {
2366         struct cnic_dev *dev = (struct cnic_dev *) data;
2367         struct cnic_local *cp = dev->cnic_priv;
2368
2369         cp->last_status_idx = cnic_service_bnx2_queues(dev);
2370
2371         CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
2372                 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
2373 }
2374
2375 static void cnic_doirq(struct cnic_dev *dev)
2376 {
2377         struct cnic_local *cp = dev->cnic_priv;
2378         u16 prod = cp->kcq1.sw_prod_idx & MAX_KCQ_IDX;
2379
2380         if (likely(test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
2381                 prefetch(cp->status_blk.gen);
2382                 prefetch(&cp->kcq1.kcq[KCQ_PG(prod)][KCQ_IDX(prod)]);
2383
2384                 tasklet_schedule(&cp->cnic_irq_task);
2385         }
2386 }
2387
2388 static irqreturn_t cnic_irq(int irq, void *dev_instance)
2389 {
2390         struct cnic_dev *dev = dev_instance;
2391         struct cnic_local *cp = dev->cnic_priv;
2392
2393         if (cp->ack_int)
2394                 cp->ack_int(dev);
2395
2396         cnic_doirq(dev);
2397
2398         return IRQ_HANDLED;
2399 }
2400
2401 static inline void cnic_ack_bnx2x_int(struct cnic_dev *dev, u8 id, u8 storm,
2402                                       u16 index, u8 op, u8 update)
2403 {
2404         struct cnic_local *cp = dev->cnic_priv;
2405         u32 hc_addr = (HC_REG_COMMAND_REG + CNIC_PORT(cp) * 32 +
2406                        COMMAND_REG_INT_ACK);
2407         struct igu_ack_register igu_ack;
2408
2409         igu_ack.status_block_index = index;
2410         igu_ack.sb_id_and_flags =
2411                         ((id << IGU_ACK_REGISTER_STATUS_BLOCK_ID_SHIFT) |
2412                          (storm << IGU_ACK_REGISTER_STORM_ID_SHIFT) |
2413                          (update << IGU_ACK_REGISTER_UPDATE_INDEX_SHIFT) |
2414                          (op << IGU_ACK_REGISTER_INTERRUPT_MODE_SHIFT));
2415
2416         CNIC_WR(dev, hc_addr, (*(u32 *)&igu_ack));
2417 }
2418
2419 static void cnic_ack_igu_sb(struct cnic_dev *dev, u8 igu_sb_id, u8 segment,
2420                             u16 index, u8 op, u8 update)
2421 {
2422         struct igu_regular cmd_data;
2423         u32 igu_addr = BAR_IGU_INTMEM + (IGU_CMD_INT_ACK_BASE + igu_sb_id) * 8;
2424
2425         cmd_data.sb_id_and_flags =
2426                 (index << IGU_REGULAR_SB_INDEX_SHIFT) |
2427                 (segment << IGU_REGULAR_SEGMENT_ACCESS_SHIFT) |
2428                 (update << IGU_REGULAR_BUPDATE_SHIFT) |
2429                 (op << IGU_REGULAR_ENABLE_INT_SHIFT);
2430
2431
2432         CNIC_WR(dev, igu_addr, cmd_data.sb_id_and_flags);
2433 }
2434
2435 static void cnic_ack_bnx2x_msix(struct cnic_dev *dev)
2436 {
2437         struct cnic_local *cp = dev->cnic_priv;
2438
2439         cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, CSTORM_ID, 0,
2440                            IGU_INT_DISABLE, 0);
2441 }
2442
2443 static void cnic_ack_bnx2x_e2_msix(struct cnic_dev *dev)
2444 {
2445         struct cnic_local *cp = dev->cnic_priv;
2446
2447         cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF, 0,
2448                         IGU_INT_DISABLE, 0);
2449 }
2450
2451 static u32 cnic_service_bnx2x_kcq(struct cnic_dev *dev, struct kcq_info *info)
2452 {
2453         u32 last_status = *info->status_idx_ptr;
2454         int kcqe_cnt;
2455
2456         while ((kcqe_cnt = cnic_get_kcqes(dev, info))) {
2457
2458                 service_kcqes(dev, kcqe_cnt);
2459
2460                 /* Tell compiler that sblk fields can change. */
2461                 barrier();
2462                 if (last_status == *info->status_idx_ptr)
2463                         break;
2464
2465                 last_status = *info->status_idx_ptr;
2466         }
2467         return last_status;
2468 }
2469
2470 static void cnic_service_bnx2x_bh(unsigned long data)
2471 {
2472         struct cnic_dev *dev = (struct cnic_dev *) data;
2473         struct cnic_local *cp = dev->cnic_priv;
2474         u32 status_idx;
2475
2476         if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
2477                 return;
2478
2479         status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq1);
2480
2481         CNIC_WR16(dev, cp->kcq1.io_addr, cp->kcq1.sw_prod_idx + MAX_KCQ_IDX);
2482         if (BNX2X_CHIP_IS_E2(cp->chip_id))
2483                 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF,
2484                                 status_idx, IGU_INT_ENABLE, 1);
2485         else
2486                 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, USTORM_ID,
2487                                    status_idx, IGU_INT_ENABLE, 1);
2488 }
2489
2490 static int cnic_service_bnx2x(void *data, void *status_blk)
2491 {
2492         struct cnic_dev *dev = data;
2493         struct cnic_local *cp = dev->cnic_priv;
2494
2495         if (!(cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
2496                 cnic_doirq(dev);
2497
2498         cnic_chk_pkt_rings(cp);
2499
2500         return 0;
2501 }
2502
2503 static void cnic_ulp_stop(struct cnic_dev *dev)
2504 {
2505         struct cnic_local *cp = dev->cnic_priv;
2506         int if_type;
2507
2508         cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
2509
2510         for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
2511                 struct cnic_ulp_ops *ulp_ops;
2512
2513                 mutex_lock(&cnic_lock);
2514                 ulp_ops = cp->ulp_ops[if_type];
2515                 if (!ulp_ops) {
2516                         mutex_unlock(&cnic_lock);
2517                         continue;
2518                 }
2519                 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
2520                 mutex_unlock(&cnic_lock);
2521
2522                 if (test_and_clear_bit(ULP_F_START, &cp->ulp_flags[if_type]))
2523                         ulp_ops->cnic_stop(cp->ulp_handle[if_type]);
2524
2525                 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
2526         }
2527 }
2528
2529 static void cnic_ulp_start(struct cnic_dev *dev)
2530 {
2531         struct cnic_local *cp = dev->cnic_priv;
2532         int if_type;
2533
2534         for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
2535                 struct cnic_ulp_ops *ulp_ops;
2536
2537                 mutex_lock(&cnic_lock);
2538                 ulp_ops = cp->ulp_ops[if_type];
2539                 if (!ulp_ops || !ulp_ops->cnic_start) {
2540                         mutex_unlock(&cnic_lock);
2541                         continue;
2542                 }
2543                 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
2544                 mutex_unlock(&cnic_lock);
2545
2546                 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[if_type]))
2547                         ulp_ops->cnic_start(cp->ulp_handle[if_type]);
2548
2549                 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
2550         }
2551 }
2552
2553 static int cnic_ctl(void *data, struct cnic_ctl_info *info)
2554 {
2555         struct cnic_dev *dev = data;
2556
2557         switch (info->cmd) {
2558         case CNIC_CTL_STOP_CMD:
2559                 cnic_hold(dev);
2560
2561                 cnic_ulp_stop(dev);
2562                 cnic_stop_hw(dev);
2563
2564                 cnic_put(dev);
2565                 break;
2566         case CNIC_CTL_START_CMD:
2567                 cnic_hold(dev);
2568
2569                 if (!cnic_start_hw(dev))
2570                         cnic_ulp_start(dev);
2571
2572                 cnic_put(dev);
2573                 break;
2574         case CNIC_CTL_COMPLETION_CMD: {
2575                 u32 cid = BNX2X_SW_CID(info->data.comp.cid);
2576                 u32 l5_cid;
2577                 struct cnic_local *cp = dev->cnic_priv;
2578
2579                 if (cnic_get_l5_cid(cp, cid, &l5_cid) == 0) {
2580                         struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
2581
2582                         ctx->wait_cond = 1;
2583                         wake_up(&ctx->waitq);
2584                 }
2585                 break;
2586         }
2587         default:
2588                 return -EINVAL;
2589         }
2590         return 0;
2591 }
2592
2593 static void cnic_ulp_init(struct cnic_dev *dev)
2594 {
2595         int i;
2596         struct cnic_local *cp = dev->cnic_priv;
2597
2598         for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
2599                 struct cnic_ulp_ops *ulp_ops;
2600
2601                 mutex_lock(&cnic_lock);
2602                 ulp_ops = cnic_ulp_tbl[i];
2603                 if (!ulp_ops || !ulp_ops->cnic_init) {
2604                         mutex_unlock(&cnic_lock);
2605                         continue;
2606                 }
2607                 ulp_get(ulp_ops);
2608                 mutex_unlock(&cnic_lock);
2609
2610                 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[i]))
2611                         ulp_ops->cnic_init(dev);
2612
2613                 ulp_put(ulp_ops);
2614         }
2615 }
2616
2617 static void cnic_ulp_exit(struct cnic_dev *dev)
2618 {
2619         int i;
2620         struct cnic_local *cp = dev->cnic_priv;
2621
2622         for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
2623                 struct cnic_ulp_ops *ulp_ops;
2624
2625                 mutex_lock(&cnic_lock);
2626                 ulp_ops = cnic_ulp_tbl[i];
2627                 if (!ulp_ops || !ulp_ops->cnic_exit) {
2628                         mutex_unlock(&cnic_lock);
2629                         continue;
2630                 }
2631                 ulp_get(ulp_ops);
2632                 mutex_unlock(&cnic_lock);
2633
2634                 if (test_and_clear_bit(ULP_F_INIT, &cp->ulp_flags[i]))
2635                         ulp_ops->cnic_exit(dev);
2636
2637                 ulp_put(ulp_ops);
2638         }
2639 }
2640
2641 static int cnic_cm_offload_pg(struct cnic_sock *csk)
2642 {
2643         struct cnic_dev *dev = csk->dev;
2644         struct l4_kwq_offload_pg *l4kwqe;
2645         struct kwqe *wqes[1];
2646
2647         l4kwqe = (struct l4_kwq_offload_pg *) &csk->kwqe1;
2648         memset(l4kwqe, 0, sizeof(*l4kwqe));
2649         wqes[0] = (struct kwqe *) l4kwqe;
2650
2651         l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_OFFLOAD_PG;
2652         l4kwqe->flags =
2653                 L4_LAYER_CODE << L4_KWQ_OFFLOAD_PG_LAYER_CODE_SHIFT;
2654         l4kwqe->l2hdr_nbytes = ETH_HLEN;
2655
2656         l4kwqe->da0 = csk->ha[0];
2657         l4kwqe->da1 = csk->ha[1];
2658         l4kwqe->da2 = csk->ha[2];
2659         l4kwqe->da3 = csk->ha[3];
2660         l4kwqe->da4 = csk->ha[4];
2661         l4kwqe->da5 = csk->ha[5];
2662
2663         l4kwqe->sa0 = dev->mac_addr[0];
2664         l4kwqe->sa1 = dev->mac_addr[1];
2665         l4kwqe->sa2 = dev->mac_addr[2];
2666         l4kwqe->sa3 = dev->mac_addr[3];
2667         l4kwqe->sa4 = dev->mac_addr[4];
2668         l4kwqe->sa5 = dev->mac_addr[5];
2669
2670         l4kwqe->etype = ETH_P_IP;
2671         l4kwqe->ipid_start = DEF_IPID_START;
2672         l4kwqe->host_opaque = csk->l5_cid;
2673
2674         if (csk->vlan_id) {
2675                 l4kwqe->pg_flags |= L4_KWQ_OFFLOAD_PG_VLAN_TAGGING;
2676                 l4kwqe->vlan_tag = csk->vlan_id;
2677                 l4kwqe->l2hdr_nbytes += 4;
2678         }
2679
2680         return dev->submit_kwqes(dev, wqes, 1);
2681 }
2682
2683 static int cnic_cm_update_pg(struct cnic_sock *csk)
2684 {
2685         struct cnic_dev *dev = csk->dev;
2686         struct l4_kwq_update_pg *l4kwqe;
2687         struct kwqe *wqes[1];
2688
2689         l4kwqe = (struct l4_kwq_update_pg *) &csk->kwqe1;
2690         memset(l4kwqe, 0, sizeof(*l4kwqe));
2691         wqes[0] = (struct kwqe *) l4kwqe;
2692
2693         l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPDATE_PG;
2694         l4kwqe->flags =
2695                 L4_LAYER_CODE << L4_KWQ_UPDATE_PG_LAYER_CODE_SHIFT;
2696         l4kwqe->pg_cid = csk->pg_cid;
2697
2698         l4kwqe->da0 = csk->ha[0];
2699         l4kwqe->da1 = csk->ha[1];
2700         l4kwqe->da2 = csk->ha[2];
2701         l4kwqe->da3 = csk->ha[3];
2702         l4kwqe->da4 = csk->ha[4];
2703         l4kwqe->da5 = csk->ha[5];
2704
2705         l4kwqe->pg_host_opaque = csk->l5_cid;
2706         l4kwqe->pg_valids = L4_KWQ_UPDATE_PG_VALIDS_DA;
2707
2708         return dev->submit_kwqes(dev, wqes, 1);
2709 }
2710
2711 static int cnic_cm_upload_pg(struct cnic_sock *csk)
2712 {
2713         struct cnic_dev *dev = csk->dev;
2714         struct l4_kwq_upload *l4kwqe;
2715         struct kwqe *wqes[1];
2716
2717         l4kwqe = (struct l4_kwq_upload *) &csk->kwqe1;
2718         memset(l4kwqe, 0, sizeof(*l4kwqe));
2719         wqes[0] = (struct kwqe *) l4kwqe;
2720
2721         l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPLOAD_PG;
2722         l4kwqe->flags =
2723                 L4_LAYER_CODE << L4_KWQ_UPLOAD_LAYER_CODE_SHIFT;
2724         l4kwqe->cid = csk->pg_cid;
2725
2726         return dev->submit_kwqes(dev, wqes, 1);
2727 }
2728
2729 static int cnic_cm_conn_req(struct cnic_sock *csk)
2730 {
2731         struct cnic_dev *dev = csk->dev;
2732         struct l4_kwq_connect_req1 *l4kwqe1;
2733         struct l4_kwq_connect_req2 *l4kwqe2;
2734         struct l4_kwq_connect_req3 *l4kwqe3;
2735         struct kwqe *wqes[3];
2736         u8 tcp_flags = 0;
2737         int num_wqes = 2;
2738
2739         l4kwqe1 = (struct l4_kwq_connect_req1 *) &csk->kwqe1;
2740         l4kwqe2 = (struct l4_kwq_connect_req2 *) &csk->kwqe2;
2741         l4kwqe3 = (struct l4_kwq_connect_req3 *) &csk->kwqe3;
2742         memset(l4kwqe1, 0, sizeof(*l4kwqe1));
2743         memset(l4kwqe2, 0, sizeof(*l4kwqe2));
2744         memset(l4kwqe3, 0, sizeof(*l4kwqe3));
2745
2746         l4kwqe3->op_code = L4_KWQE_OPCODE_VALUE_CONNECT3;
2747         l4kwqe3->flags =
2748                 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ3_LAYER_CODE_SHIFT;
2749         l4kwqe3->ka_timeout = csk->ka_timeout;
2750         l4kwqe3->ka_interval = csk->ka_interval;
2751         l4kwqe3->ka_max_probe_count = csk->ka_max_probe_count;
2752         l4kwqe3->tos = csk->tos;
2753         l4kwqe3->ttl = csk->ttl;
2754         l4kwqe3->snd_seq_scale = csk->snd_seq_scale;
2755         l4kwqe3->pmtu = csk->mtu;
2756         l4kwqe3->rcv_buf = csk->rcv_buf;
2757         l4kwqe3->snd_buf = csk->snd_buf;
2758         l4kwqe3->seed = csk->seed;
2759
2760         wqes[0] = (struct kwqe *) l4kwqe1;
2761         if (test_bit(SK_F_IPV6, &csk->flags)) {
2762                 wqes[1] = (struct kwqe *) l4kwqe2;
2763                 wqes[2] = (struct kwqe *) l4kwqe3;
2764                 num_wqes = 3;
2765
2766                 l4kwqe1->conn_flags = L4_KWQ_CONNECT_REQ1_IP_V6;
2767                 l4kwqe2->op_code = L4_KWQE_OPCODE_VALUE_CONNECT2;
2768                 l4kwqe2->flags =
2769                         L4_KWQ_CONNECT_REQ2_LINKED_WITH_NEXT |
2770                         L4_LAYER_CODE << L4_KWQ_CONNECT_REQ2_LAYER_CODE_SHIFT;
2771                 l4kwqe2->src_ip_v6_2 = be32_to_cpu(csk->src_ip[1]);
2772                 l4kwqe2->src_ip_v6_3 = be32_to_cpu(csk->src_ip[2]);
2773                 l4kwqe2->src_ip_v6_4 = be32_to_cpu(csk->src_ip[3]);
2774                 l4kwqe2->dst_ip_v6_2 = be32_to_cpu(csk->dst_ip[1]);
2775                 l4kwqe2->dst_ip_v6_3 = be32_to_cpu(csk->dst_ip[2]);
2776                 l4kwqe2->dst_ip_v6_4 = be32_to_cpu(csk->dst_ip[3]);
2777                 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct ipv6hdr) -
2778                                sizeof(struct tcphdr);
2779         } else {
2780                 wqes[1] = (struct kwqe *) l4kwqe3;
2781                 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct iphdr) -
2782                                sizeof(struct tcphdr);
2783         }
2784
2785         l4kwqe1->op_code = L4_KWQE_OPCODE_VALUE_CONNECT1;
2786         l4kwqe1->flags =
2787                 (L4_LAYER_CODE << L4_KWQ_CONNECT_REQ1_LAYER_CODE_SHIFT) |
2788                  L4_KWQ_CONNECT_REQ3_LINKED_WITH_NEXT;
2789         l4kwqe1->cid = csk->cid;
2790         l4kwqe1->pg_cid = csk->pg_cid;
2791         l4kwqe1->src_ip = be32_to_cpu(csk->src_ip[0]);
2792         l4kwqe1->dst_ip = be32_to_cpu(csk->dst_ip[0]);
2793         l4kwqe1->src_port = be16_to_cpu(csk->src_port);
2794         l4kwqe1->dst_port = be16_to_cpu(csk->dst_port);
2795         if (csk->tcp_flags & SK_TCP_NO_DELAY_ACK)
2796                 tcp_flags |= L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK;
2797         if (csk->tcp_flags & SK_TCP_KEEP_ALIVE)
2798                 tcp_flags |= L4_KWQ_CONNECT_REQ1_KEEP_ALIVE;
2799         if (csk->tcp_flags & SK_TCP_NAGLE)
2800                 tcp_flags |= L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE;
2801         if (csk->tcp_flags & SK_TCP_TIMESTAMP)
2802                 tcp_flags |= L4_KWQ_CONNECT_REQ1_TIME_STAMP;
2803         if (csk->tcp_flags & SK_TCP_SACK)
2804                 tcp_flags |= L4_KWQ_CONNECT_REQ1_SACK;
2805         if (csk->tcp_flags & SK_TCP_SEG_SCALING)
2806                 tcp_flags |= L4_KWQ_CONNECT_REQ1_SEG_SCALING;
2807
2808         l4kwqe1->tcp_flags = tcp_flags;
2809
2810         return dev->submit_kwqes(dev, wqes, num_wqes);
2811 }
2812
2813 static int cnic_cm_close_req(struct cnic_sock *csk)
2814 {
2815         struct cnic_dev *dev = csk->dev;
2816         struct l4_kwq_close_req *l4kwqe;
2817         struct kwqe *wqes[1];
2818
2819         l4kwqe = (struct l4_kwq_close_req *) &csk->kwqe2;
2820         memset(l4kwqe, 0, sizeof(*l4kwqe));
2821         wqes[0] = (struct kwqe *) l4kwqe;
2822
2823         l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_CLOSE;
2824         l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_CLOSE_REQ_LAYER_CODE_SHIFT;
2825         l4kwqe->cid = csk->cid;
2826
2827         return dev->submit_kwqes(dev, wqes, 1);
2828 }
2829
2830 static int cnic_cm_abort_req(struct cnic_sock *csk)
2831 {
2832         struct cnic_dev *dev = csk->dev;
2833         struct l4_kwq_reset_req *l4kwqe;
2834         struct kwqe *wqes[1];
2835
2836         l4kwqe = (struct l4_kwq_reset_req *) &csk->kwqe2;
2837         memset(l4kwqe, 0, sizeof(*l4kwqe));
2838         wqes[0] = (struct kwqe *) l4kwqe;
2839
2840         l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_RESET;
2841         l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_RESET_REQ_LAYER_CODE_SHIFT;
2842         l4kwqe->cid = csk->cid;
2843
2844         return dev->submit_kwqes(dev, wqes, 1);
2845 }
2846
2847 static int cnic_cm_create(struct cnic_dev *dev, int ulp_type, u32 cid,
2848                           u32 l5_cid, struct cnic_sock **csk, void *context)
2849 {
2850         struct cnic_local *cp = dev->cnic_priv;
2851         struct cnic_sock *csk1;
2852
2853         if (l5_cid >= MAX_CM_SK_TBL_SZ)
2854                 return -EINVAL;
2855
2856         if (cp->ctx_tbl) {
2857                 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
2858
2859                 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2860                         return -EAGAIN;
2861         }
2862
2863         csk1 = &cp->csk_tbl[l5_cid];
2864         if (atomic_read(&csk1->ref_count))
2865                 return -EAGAIN;
2866
2867         if (test_and_set_bit(SK_F_INUSE, &csk1->flags))
2868                 return -EBUSY;
2869
2870         csk1->dev = dev;
2871         csk1->cid = cid;
2872         csk1->l5_cid = l5_cid;
2873         csk1->ulp_type = ulp_type;
2874         csk1->context = context;
2875
2876         csk1->ka_timeout = DEF_KA_TIMEOUT;
2877         csk1->ka_interval = DEF_KA_INTERVAL;
2878         csk1->ka_max_probe_count = DEF_KA_MAX_PROBE_COUNT;
2879         csk1->tos = DEF_TOS;
2880         csk1->ttl = DEF_TTL;
2881         csk1->snd_seq_scale = DEF_SND_SEQ_SCALE;
2882         csk1->rcv_buf = DEF_RCV_BUF;
2883         csk1->snd_buf = DEF_SND_BUF;
2884         csk1->seed = DEF_SEED;
2885
2886         *csk = csk1;
2887         return 0;
2888 }
2889
2890 static void cnic_cm_cleanup(struct cnic_sock *csk)
2891 {
2892         if (csk->src_port) {
2893                 struct cnic_dev *dev = csk->dev;
2894                 struct cnic_local *cp = dev->cnic_priv;
2895
2896                 cnic_free_id(&cp->csk_port_tbl, be16_to_cpu(csk->src_port));
2897                 csk->src_port = 0;
2898         }
2899 }
2900
2901 static void cnic_close_conn(struct cnic_sock *csk)
2902 {
2903         if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags)) {
2904                 cnic_cm_upload_pg(csk);
2905                 clear_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
2906         }
2907         cnic_cm_cleanup(csk);
2908 }
2909
2910 static int cnic_cm_destroy(struct cnic_sock *csk)
2911 {
2912         if (!cnic_in_use(csk))
2913                 return -EINVAL;
2914
2915         csk_hold(csk);
2916         clear_bit(SK_F_INUSE, &csk->flags);
2917         smp_mb__after_clear_bit();
2918         while (atomic_read(&csk->ref_count) != 1)
2919                 msleep(1);
2920         cnic_cm_cleanup(csk);
2921
2922         csk->flags = 0;
2923         csk_put(csk);
2924         return 0;
2925 }
2926
2927 static inline u16 cnic_get_vlan(struct net_device *dev,
2928                                 struct net_device **vlan_dev)
2929 {
2930         if (dev->priv_flags & IFF_802_1Q_VLAN) {
2931                 *vlan_dev = vlan_dev_real_dev(dev);
2932                 return vlan_dev_vlan_id(dev);
2933         }
2934         *vlan_dev = dev;
2935         return 0;
2936 }
2937
2938 static int cnic_get_v4_route(struct sockaddr_in *dst_addr,
2939                              struct dst_entry **dst)
2940 {
2941 #if defined(CONFIG_INET)
2942         struct flowi fl;
2943         int err;
2944         struct rtable *rt;
2945
2946         memset(&fl, 0, sizeof(fl));
2947         fl.nl_u.ip4_u.daddr = dst_addr->sin_addr.s_addr;
2948
2949         err = ip_route_output_key(&init_net, &rt, &fl);
2950         if (!err)
2951                 *dst = &rt->dst;
2952         return err;
2953 #else
2954         return -ENETUNREACH;
2955 #endif
2956 }
2957
2958 static int cnic_get_v6_route(struct sockaddr_in6 *dst_addr,
2959                              struct dst_entry **dst)
2960 {
2961 #if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
2962         struct flowi fl;
2963
2964         memset(&fl, 0, sizeof(fl));
2965         ipv6_addr_copy(&fl.fl6_dst, &dst_addr->sin6_addr);
2966         if (ipv6_addr_type(&fl.fl6_dst) & IPV6_ADDR_LINKLOCAL)
2967                 fl.oif = dst_addr->sin6_scope_id;
2968
2969         *dst = ip6_route_output(&init_net, NULL, &fl);
2970         if (*dst)
2971                 return 0;
2972 #endif
2973
2974         return -ENETUNREACH;
2975 }
2976
2977 static struct cnic_dev *cnic_cm_select_dev(struct sockaddr_in *dst_addr,
2978                                            int ulp_type)
2979 {
2980         struct cnic_dev *dev = NULL;
2981         struct dst_entry *dst;
2982         struct net_device *netdev = NULL;
2983         int err = -ENETUNREACH;
2984
2985         if (dst_addr->sin_family == AF_INET)
2986                 err = cnic_get_v4_route(dst_addr, &dst);
2987         else if (dst_addr->sin_family == AF_INET6) {
2988                 struct sockaddr_in6 *dst_addr6 =
2989                         (struct sockaddr_in6 *) dst_addr;
2990
2991                 err = cnic_get_v6_route(dst_addr6, &dst);
2992         } else
2993                 return NULL;
2994
2995         if (err)
2996                 return NULL;
2997
2998         if (!dst->dev)
2999                 goto done;
3000
3001         cnic_get_vlan(dst->dev, &netdev);
3002
3003         dev = cnic_from_netdev(netdev);
3004
3005 done:
3006         dst_release(dst);
3007         if (dev)
3008                 cnic_put(dev);
3009         return dev;
3010 }
3011
3012 static int cnic_resolve_addr(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3013 {
3014         struct cnic_dev *dev = csk->dev;
3015         struct cnic_local *cp = dev->cnic_priv;
3016
3017         return cnic_send_nlmsg(cp, ISCSI_KEVENT_PATH_REQ, csk);
3018 }
3019
3020 static int cnic_get_route(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3021 {
3022         struct cnic_dev *dev = csk->dev;
3023         struct cnic_local *cp = dev->cnic_priv;
3024         int is_v6, rc = 0;
3025         struct dst_entry *dst = NULL;
3026         struct net_device *realdev;
3027         __be16 local_port;
3028         u32 port_id;
3029
3030         if (saddr->local.v6.sin6_family == AF_INET6 &&
3031             saddr->remote.v6.sin6_family == AF_INET6)
3032                 is_v6 = 1;
3033         else if (saddr->local.v4.sin_family == AF_INET &&
3034                  saddr->remote.v4.sin_family == AF_INET)
3035                 is_v6 = 0;
3036         else
3037                 return -EINVAL;
3038
3039         clear_bit(SK_F_IPV6, &csk->flags);
3040
3041         if (is_v6) {
3042                 set_bit(SK_F_IPV6, &csk->flags);
3043                 cnic_get_v6_route(&saddr->remote.v6, &dst);
3044
3045                 memcpy(&csk->dst_ip[0], &saddr->remote.v6.sin6_addr,
3046                        sizeof(struct in6_addr));
3047                 csk->dst_port = saddr->remote.v6.sin6_port;
3048                 local_port = saddr->local.v6.sin6_port;
3049
3050         } else {
3051                 cnic_get_v4_route(&saddr->remote.v4, &dst);
3052
3053                 csk->dst_ip[0] = saddr->remote.v4.sin_addr.s_addr;
3054                 csk->dst_port = saddr->remote.v4.sin_port;
3055                 local_port = saddr->local.v4.sin_port;
3056         }
3057
3058         csk->vlan_id = 0;
3059         csk->mtu = dev->netdev->mtu;
3060         if (dst && dst->dev) {
3061                 u16 vlan = cnic_get_vlan(dst->dev, &realdev);
3062                 if (realdev == dev->netdev) {
3063                         csk->vlan_id = vlan;
3064                         csk->mtu = dst_mtu(dst);
3065                 }
3066         }
3067
3068         port_id = be16_to_cpu(local_port);
3069         if (port_id >= CNIC_LOCAL_PORT_MIN &&
3070             port_id < CNIC_LOCAL_PORT_MAX) {
3071                 if (cnic_alloc_id(&cp->csk_port_tbl, port_id))
3072                         port_id = 0;
3073         } else
3074                 port_id = 0;
3075
3076         if (!port_id) {
3077                 port_id = cnic_alloc_new_id(&cp->csk_port_tbl);
3078                 if (port_id == -1) {
3079                         rc = -ENOMEM;
3080                         goto err_out;
3081                 }
3082                 local_port = cpu_to_be16(port_id);
3083         }
3084         csk->src_port = local_port;
3085
3086 err_out:
3087         dst_release(dst);
3088         return rc;
3089 }
3090
3091 static void cnic_init_csk_state(struct cnic_sock *csk)
3092 {
3093         csk->state = 0;
3094         clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3095         clear_bit(SK_F_CLOSING, &csk->flags);
3096 }
3097
3098 static int cnic_cm_connect(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3099 {
3100         int err = 0;
3101
3102         if (!cnic_in_use(csk))
3103                 return -EINVAL;
3104
3105         if (test_and_set_bit(SK_F_CONNECT_START, &csk->flags))
3106                 return -EINVAL;
3107
3108         cnic_init_csk_state(csk);
3109
3110         err = cnic_get_route(csk, saddr);
3111         if (err)
3112                 goto err_out;
3113
3114         err = cnic_resolve_addr(csk, saddr);
3115         if (!err)
3116                 return 0;
3117
3118 err_out:
3119         clear_bit(SK_F_CONNECT_START, &csk->flags);
3120         return err;
3121 }
3122
3123 static int cnic_cm_abort(struct cnic_sock *csk)
3124 {
3125         struct cnic_local *cp = csk->dev->cnic_priv;
3126         u32 opcode = L4_KCQE_OPCODE_VALUE_RESET_COMP;
3127
3128         if (!cnic_in_use(csk))
3129                 return -EINVAL;
3130
3131         if (cnic_abort_prep(csk))
3132                 return cnic_cm_abort_req(csk);
3133
3134         /* Getting here means that we haven't started connect, or
3135          * connect was not successful.
3136          */
3137
3138         cp->close_conn(csk, opcode);
3139         if (csk->state != opcode)
3140                 return -EALREADY;
3141
3142         return 0;
3143 }
3144
3145 static int cnic_cm_close(struct cnic_sock *csk)
3146 {
3147         if (!cnic_in_use(csk))
3148                 return -EINVAL;
3149
3150         if (cnic_close_prep(csk)) {
3151                 csk->state = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
3152                 return cnic_cm_close_req(csk);
3153         } else {
3154                 return -EALREADY;
3155         }
3156         return 0;
3157 }
3158
3159 static void cnic_cm_upcall(struct cnic_local *cp, struct cnic_sock *csk,
3160                            u8 opcode)
3161 {
3162         struct cnic_ulp_ops *ulp_ops;
3163         int ulp_type = csk->ulp_type;
3164
3165         rcu_read_lock();
3166         ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
3167         if (ulp_ops) {
3168                 if (opcode == L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE)
3169                         ulp_ops->cm_connect_complete(csk);
3170                 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3171                         ulp_ops->cm_close_complete(csk);
3172                 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED)
3173                         ulp_ops->cm_remote_abort(csk);
3174                 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_COMP)
3175                         ulp_ops->cm_abort_complete(csk);
3176                 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED)
3177                         ulp_ops->cm_remote_close(csk);
3178         }
3179         rcu_read_unlock();
3180 }
3181
3182 static int cnic_cm_set_pg(struct cnic_sock *csk)
3183 {
3184         if (cnic_offld_prep(csk)) {
3185                 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3186                         cnic_cm_update_pg(csk);
3187                 else
3188                         cnic_cm_offload_pg(csk);
3189         }
3190         return 0;
3191 }
3192
3193 static void cnic_cm_process_offld_pg(struct cnic_dev *dev, struct l4_kcq *kcqe)
3194 {
3195         struct cnic_local *cp = dev->cnic_priv;
3196         u32 l5_cid = kcqe->pg_host_opaque;
3197         u8 opcode = kcqe->op_code;
3198         struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
3199
3200         csk_hold(csk);
3201         if (!cnic_in_use(csk))
3202                 goto done;
3203
3204         if (opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3205                 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3206                 goto done;
3207         }
3208         /* Possible PG kcqe status:  SUCCESS, OFFLOADED_PG, or CTX_ALLOC_FAIL */
3209         if (kcqe->status == L4_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAIL) {
3210                 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3211                 cnic_cm_upcall(cp, csk,
3212                                L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3213                 goto done;
3214         }
3215
3216         csk->pg_cid = kcqe->pg_cid;
3217         set_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3218         cnic_cm_conn_req(csk);
3219
3220 done:
3221         csk_put(csk);
3222 }
3223
3224 static void cnic_cm_process_kcqe(struct cnic_dev *dev, struct kcqe *kcqe)
3225 {
3226         struct cnic_local *cp = dev->cnic_priv;
3227         struct l4_kcq *l4kcqe = (struct l4_kcq *) kcqe;
3228         u8 opcode = l4kcqe->op_code;
3229         u32 l5_cid;
3230         struct cnic_sock *csk;
3231
3232         if (opcode == L4_KCQE_OPCODE_VALUE_OFFLOAD_PG ||
3233             opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3234                 cnic_cm_process_offld_pg(dev, l4kcqe);
3235                 return;
3236         }
3237
3238         l5_cid = l4kcqe->conn_id;
3239         if (opcode & 0x80)
3240                 l5_cid = l4kcqe->cid;
3241         if (l5_cid >= MAX_CM_SK_TBL_SZ)
3242                 return;
3243
3244         csk = &cp->csk_tbl[l5_cid];
3245         csk_hold(csk);
3246
3247         if (!cnic_in_use(csk)) {
3248                 csk_put(csk);
3249                 return;
3250         }
3251
3252         switch (opcode) {
3253         case L5CM_RAMROD_CMD_ID_TCP_CONNECT:
3254                 if (l4kcqe->status != 0) {
3255                         clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3256                         cnic_cm_upcall(cp, csk,
3257                                        L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3258                 }
3259                 break;
3260         case L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE:
3261                 if (l4kcqe->status == 0)
3262                         set_bit(SK_F_OFFLD_COMPLETE, &csk->flags);
3263
3264                 smp_mb__before_clear_bit();
3265                 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3266                 cnic_cm_upcall(cp, csk, opcode);
3267                 break;
3268
3269         case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
3270         case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3271         case L4_KCQE_OPCODE_VALUE_RESET_COMP:
3272         case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3273         case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
3274                 cp->close_conn(csk, opcode);
3275                 break;
3276
3277         case L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED:
3278                 cnic_cm_upcall(cp, csk, opcode);
3279                 break;
3280         }
3281         csk_put(csk);
3282 }
3283
3284 static void cnic_cm_indicate_kcqe(void *data, struct kcqe *kcqe[], u32 num)
3285 {
3286         struct cnic_dev *dev = data;
3287         int i;
3288
3289         for (i = 0; i < num; i++)
3290                 cnic_cm_process_kcqe(dev, kcqe[i]);
3291 }
3292
3293 static struct cnic_ulp_ops cm_ulp_ops = {
3294         .indicate_kcqes         = cnic_cm_indicate_kcqe,
3295 };
3296
3297 static void cnic_cm_free_mem(struct cnic_dev *dev)
3298 {
3299         struct cnic_local *cp = dev->cnic_priv;
3300
3301         kfree(cp->csk_tbl);
3302         cp->csk_tbl = NULL;
3303         cnic_free_id_tbl(&cp->csk_port_tbl);
3304 }
3305
3306 static int cnic_cm_alloc_mem(struct cnic_dev *dev)
3307 {
3308         struct cnic_local *cp = dev->cnic_priv;
3309
3310         cp->csk_tbl = kzalloc(sizeof(struct cnic_sock) * MAX_CM_SK_TBL_SZ,
3311                               GFP_KERNEL);
3312         if (!cp->csk_tbl)
3313                 return -ENOMEM;
3314
3315         if (cnic_init_id_tbl(&cp->csk_port_tbl, CNIC_LOCAL_PORT_RANGE,
3316                              CNIC_LOCAL_PORT_MIN)) {
3317                 cnic_cm_free_mem(dev);
3318                 return -ENOMEM;
3319         }
3320         return 0;
3321 }
3322
3323 static int cnic_ready_to_close(struct cnic_sock *csk, u32 opcode)
3324 {
3325         if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
3326                 /* Unsolicited RESET_COMP or RESET_RECEIVED */
3327                 opcode = L4_KCQE_OPCODE_VALUE_RESET_RECEIVED;
3328                 csk->state = opcode;
3329         }
3330
3331         /* 1. If event opcode matches the expected event in csk->state
3332          * 2. If the expected event is CLOSE_COMP, we accept any event
3333          * 3. If the expected event is 0, meaning the connection was never
3334          *    never established, we accept the opcode from cm_abort.
3335          */
3336         if (opcode == csk->state || csk->state == 0 ||
3337             csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP) {
3338                 if (!test_and_set_bit(SK_F_CLOSING, &csk->flags)) {
3339                         if (csk->state == 0)
3340                                 csk->state = opcode;
3341                         return 1;
3342                 }
3343         }
3344         return 0;
3345 }
3346
3347 static void cnic_close_bnx2_conn(struct cnic_sock *csk, u32 opcode)
3348 {
3349         struct cnic_dev *dev = csk->dev;
3350         struct cnic_local *cp = dev->cnic_priv;
3351
3352         if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED) {
3353                 cnic_cm_upcall(cp, csk, opcode);
3354                 return;
3355         }
3356
3357         clear_bit(SK_F_CONNECT_START, &csk->flags);
3358         cnic_close_conn(csk);
3359         csk->state = opcode;
3360         cnic_cm_upcall(cp, csk, opcode);
3361 }
3362
3363 static void cnic_cm_stop_bnx2_hw(struct cnic_dev *dev)
3364 {
3365 }
3366
3367 static int cnic_cm_init_bnx2_hw(struct cnic_dev *dev)
3368 {
3369         u32 seed;
3370
3371         get_random_bytes(&seed, 4);
3372         cnic_ctx_wr(dev, 45, 0, seed);
3373         return 0;
3374 }
3375
3376 static void cnic_close_bnx2x_conn(struct cnic_sock *csk, u32 opcode)
3377 {
3378         struct cnic_dev *dev = csk->dev;
3379         struct cnic_local *cp = dev->cnic_priv;
3380         struct cnic_context *ctx = &cp->ctx_tbl[csk->l5_cid];
3381         union l5cm_specific_data l5_data;
3382         u32 cmd = 0;
3383         int close_complete = 0;
3384
3385         switch (opcode) {
3386         case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
3387         case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3388         case L4_KCQE_OPCODE_VALUE_RESET_COMP:
3389                 if (cnic_ready_to_close(csk, opcode)) {
3390                         if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3391                                 cmd = L5CM_RAMROD_CMD_ID_SEARCHER_DELETE;
3392                         else
3393                                 close_complete = 1;
3394                 }
3395                 break;
3396         case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3397                 cmd = L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD;
3398                 break;
3399         case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
3400                 close_complete = 1;
3401                 break;
3402         }
3403         if (cmd) {
3404                 memset(&l5_data, 0, sizeof(l5_data));
3405
3406                 cnic_submit_kwqe_16(dev, cmd, csk->cid, ISCSI_CONNECTION_TYPE,
3407                                     &l5_data);
3408         } else if (close_complete) {
3409                 ctx->timestamp = jiffies;
3410                 cnic_close_conn(csk);
3411                 cnic_cm_upcall(cp, csk, csk->state);
3412         }
3413 }
3414
3415 static void cnic_cm_stop_bnx2x_hw(struct cnic_dev *dev)
3416 {
3417         struct cnic_local *cp = dev->cnic_priv;
3418         int i;
3419
3420         if (!cp->ctx_tbl)
3421                 return;
3422
3423         if (!netif_running(dev->netdev))
3424                 return;
3425
3426         for (i = 0; i < cp->max_cid_space; i++) {
3427                 struct cnic_context *ctx = &cp->ctx_tbl[i];
3428
3429                 while (test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
3430                         msleep(10);
3431
3432                 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
3433                         netdev_warn(dev->netdev, "CID %x not deleted\n",
3434                                    ctx->cid);
3435         }
3436
3437         cancel_delayed_work(&cp->delete_task);
3438         flush_workqueue(cnic_wq);
3439
3440         if (atomic_read(&cp->iscsi_conn) != 0)
3441                 netdev_warn(dev->netdev, "%d iSCSI connections not destroyed\n",
3442                             atomic_read(&cp->iscsi_conn));
3443 }
3444
3445 static int cnic_cm_init_bnx2x_hw(struct cnic_dev *dev)
3446 {
3447         struct cnic_local *cp = dev->cnic_priv;
3448         u32 pfid = cp->pfid;
3449         u32 port = CNIC_PORT(cp);
3450
3451         cnic_init_bnx2x_mac(dev);
3452         cnic_bnx2x_set_tcp_timestamp(dev, 1);
3453
3454         CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
3455                   XSTORM_ISCSI_LOCAL_VLAN_OFFSET(pfid), 0);
3456
3457         CNIC_WR(dev, BAR_XSTRORM_INTMEM +
3458                 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_ENABLED_OFFSET(port), 1);
3459         CNIC_WR(dev, BAR_XSTRORM_INTMEM +
3460                 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_MAX_COUNT_OFFSET(port),
3461                 DEF_MAX_DA_COUNT);
3462
3463         CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
3464                  XSTORM_ISCSI_TCP_VARS_TTL_OFFSET(pfid), DEF_TTL);
3465         CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
3466                  XSTORM_ISCSI_TCP_VARS_TOS_OFFSET(pfid), DEF_TOS);
3467         CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
3468                  XSTORM_ISCSI_TCP_VARS_ADV_WND_SCL_OFFSET(pfid), 2);
3469         CNIC_WR(dev, BAR_XSTRORM_INTMEM +
3470                 XSTORM_TCP_TX_SWS_TIMER_VAL_OFFSET(pfid), DEF_SWS_TIMER);
3471
3472         CNIC_WR(dev, BAR_TSTRORM_INTMEM + TSTORM_TCP_MAX_CWND_OFFSET(pfid),
3473                 DEF_MAX_CWND);
3474         return 0;
3475 }
3476
3477 static void cnic_delete_task(struct work_struct *work)
3478 {
3479         struct cnic_local *cp;
3480         struct cnic_dev *dev;
3481         u32 i;
3482         int need_resched = 0;
3483
3484         cp = container_of(work, struct cnic_local, delete_task.work);
3485         dev = cp->dev;
3486
3487         for (i = 0; i < cp->max_cid_space; i++) {
3488                 struct cnic_context *ctx = &cp->ctx_tbl[i];
3489
3490                 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags) ||
3491                     !test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
3492                         continue;
3493
3494                 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
3495                         need_resched = 1;
3496                         continue;
3497                 }
3498
3499                 if (!test_and_clear_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
3500                         continue;
3501
3502                 cnic_bnx2x_destroy_ramrod(dev, i);
3503
3504                 cnic_free_bnx2x_conn_resc(dev, i);
3505                 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI)
3506                         atomic_dec(&cp->iscsi_conn);
3507
3508                 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
3509         }
3510
3511         if (need_resched)
3512                 queue_delayed_work(cnic_wq, &cp->delete_task,
3513                                    msecs_to_jiffies(10));
3514
3515 }
3516
3517 static int cnic_cm_open(struct cnic_dev *dev)
3518 {
3519         struct cnic_local *cp = dev->cnic_priv;
3520         int err;
3521
3522         err = cnic_cm_alloc_mem(dev);
3523         if (err)
3524                 return err;
3525
3526         err = cp->start_cm(dev);
3527
3528         if (err)
3529                 goto err_out;
3530
3531         INIT_DELAYED_WORK(&cp->delete_task, cnic_delete_task);
3532
3533         dev->cm_create = cnic_cm_create;
3534         dev->cm_destroy = cnic_cm_destroy;
3535         dev->cm_connect = cnic_cm_connect;
3536         dev->cm_abort = cnic_cm_abort;
3537         dev->cm_close = cnic_cm_close;
3538         dev->cm_select_dev = cnic_cm_select_dev;
3539
3540         cp->ulp_handle[CNIC_ULP_L4] = dev;
3541         rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], &cm_ulp_ops);
3542         return 0;
3543
3544 err_out:
3545         cnic_cm_free_mem(dev);
3546         return err;
3547 }
3548
3549 static int cnic_cm_shutdown(struct cnic_dev *dev)
3550 {
3551         struct cnic_local *cp = dev->cnic_priv;
3552         int i;
3553
3554         cp->stop_cm(dev);
3555
3556         if (!cp->csk_tbl)
3557                 return 0;
3558
3559         for (i = 0; i < MAX_CM_SK_TBL_SZ; i++) {
3560                 struct cnic_sock *csk = &cp->csk_tbl[i];
3561
3562                 clear_bit(SK_F_INUSE, &csk->flags);
3563                 cnic_cm_cleanup(csk);
3564         }
3565         cnic_cm_free_mem(dev);
3566
3567         return 0;
3568 }
3569
3570 static void cnic_init_context(struct cnic_dev *dev, u32 cid)
3571 {
3572         u32 cid_addr;
3573         int i;
3574
3575         cid_addr = GET_CID_ADDR(cid);
3576
3577         for (i = 0; i < CTX_SIZE; i += 4)
3578                 cnic_ctx_wr(dev, cid_addr, i, 0);
3579 }
3580
3581 static int cnic_setup_5709_context(struct cnic_dev *dev, int valid)
3582 {
3583         struct cnic_local *cp = dev->cnic_priv;
3584         int ret = 0, i;
3585         u32 valid_bit = valid ? BNX2_CTX_HOST_PAGE_TBL_DATA0_VALID : 0;
3586
3587         if (CHIP_NUM(cp) != CHIP_NUM_5709)
3588                 return 0;
3589
3590         for (i = 0; i < cp->ctx_blks; i++) {
3591                 int j;
3592                 u32 idx = cp->ctx_arr[i].cid / cp->cids_per_blk;
3593                 u32 val;
3594
3595                 memset(cp->ctx_arr[i].ctx, 0, BCM_PAGE_SIZE);
3596
3597                 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA0,
3598                         (cp->ctx_arr[i].mapping & 0xffffffff) | valid_bit);
3599                 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA1,
3600                         (u64) cp->ctx_arr[i].mapping >> 32);
3601                 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL, idx |
3602                         BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
3603                 for (j = 0; j < 10; j++) {
3604
3605                         val = CNIC_RD(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL);
3606                         if (!(val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ))
3607                                 break;
3608                         udelay(5);
3609                 }
3610                 if (val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) {
3611                         ret = -EBUSY;
3612                         break;
3613                 }
3614         }
3615         return ret;
3616 }
3617
3618 static void cnic_free_irq(struct cnic_dev *dev)
3619 {
3620         struct cnic_local *cp = dev->cnic_priv;
3621         struct cnic_eth_dev *ethdev = cp->ethdev;
3622
3623         if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
3624                 cp->disable_int_sync(dev);
3625                 tasklet_kill(&cp->cnic_irq_task);
3626                 free_irq(ethdev->irq_arr[0].vector, dev);
3627         }
3628 }
3629
3630 static int cnic_request_irq(struct cnic_dev *dev)
3631 {
3632         struct cnic_local *cp = dev->cnic_priv;
3633         struct cnic_eth_dev *ethdev = cp->ethdev;
3634         int err;
3635
3636         err = request_irq(ethdev->irq_arr[0].vector, cnic_irq, 0, "cnic", dev);
3637         if (err)
3638                 tasklet_disable(&cp->cnic_irq_task);
3639
3640         return err;
3641 }
3642
3643 static int cnic_init_bnx2_irq(struct cnic_dev *dev)
3644 {
3645         struct cnic_local *cp = dev->cnic_priv;
3646         struct cnic_eth_dev *ethdev = cp->ethdev;
3647
3648         if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
3649                 int err, i = 0;
3650                 int sblk_num = cp->status_blk_num;
3651                 u32 base = ((sblk_num - 1) * BNX2_HC_SB_CONFIG_SIZE) +
3652                            BNX2_HC_SB_CONFIG_1;
3653
3654                 CNIC_WR(dev, base, BNX2_HC_SB_CONFIG_1_ONE_SHOT);
3655
3656                 CNIC_WR(dev, base + BNX2_HC_COMP_PROD_TRIP_OFF, (2 << 16) | 8);
3657                 CNIC_WR(dev, base + BNX2_HC_COM_TICKS_OFF, (64 << 16) | 220);
3658                 CNIC_WR(dev, base + BNX2_HC_CMD_TICKS_OFF, (64 << 16) | 220);
3659
3660                 cp->last_status_idx = cp->status_blk.bnx2->status_idx;
3661                 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2_msix,
3662                              (unsigned long) dev);
3663                 err = cnic_request_irq(dev);
3664                 if (err)
3665                         return err;
3666
3667                 while (cp->status_blk.bnx2->status_completion_producer_index &&
3668                        i < 10) {
3669                         CNIC_WR(dev, BNX2_HC_COALESCE_NOW,
3670                                 1 << (11 + sblk_num));
3671                         udelay(10);
3672                         i++;
3673                         barrier();
3674                 }
3675                 if (cp->status_blk.bnx2->status_completion_producer_index) {
3676                         cnic_free_irq(dev);
3677                         goto failed;
3678                 }
3679
3680         } else {
3681                 struct status_block *sblk = cp->status_blk.gen;
3682                 u32 hc_cmd = CNIC_RD(dev, BNX2_HC_COMMAND);
3683                 int i = 0;
3684
3685                 while (sblk->status_completion_producer_index && i < 10) {
3686                         CNIC_WR(dev, BNX2_HC_COMMAND,
3687                                 hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
3688                         udelay(10);
3689                         i++;
3690                         barrier();
3691                 }
3692                 if (sblk->status_completion_producer_index)
3693                         goto failed;
3694
3695         }
3696         return 0;
3697
3698 failed:
3699         netdev_err(dev->netdev, "KCQ index not resetting to 0\n");
3700         return -EBUSY;
3701 }
3702
3703 static void cnic_enable_bnx2_int(struct cnic_dev *dev)
3704 {
3705         struct cnic_local *cp = dev->cnic_priv;
3706         struct cnic_eth_dev *ethdev = cp->ethdev;
3707
3708         if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
3709                 return;
3710
3711         CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
3712                 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
3713 }
3714
3715 static void cnic_disable_bnx2_int_sync(struct cnic_dev *dev)
3716 {
3717         struct cnic_local *cp = dev->cnic_priv;
3718         struct cnic_eth_dev *ethdev = cp->ethdev;
3719
3720         if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
3721                 return;
3722
3723         CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
3724                 BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
3725         CNIC_RD(dev, BNX2_PCICFG_INT_ACK_CMD);
3726         synchronize_irq(ethdev->irq_arr[0].vector);
3727 }
3728
3729 static void cnic_init_bnx2_tx_ring(struct cnic_dev *dev)
3730 {
3731         struct cnic_local *cp = dev->cnic_priv;
3732         struct cnic_eth_dev *ethdev = cp->ethdev;
3733         struct cnic_uio_dev *udev = cp->udev;
3734         u32 cid_addr, tx_cid, sb_id;
3735         u32 val, offset0, offset1, offset2, offset3;
3736         int i;
3737         struct tx_bd *txbd;
3738         dma_addr_t buf_map, ring_map = udev->l2_ring_map;
3739         struct status_block *s_blk = cp->status_blk.gen;
3740
3741         sb_id = cp->status_blk_num;
3742         tx_cid = 20;
3743         cp->tx_cons_ptr = &s_blk->status_tx_quick_consumer_index2;
3744         if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
3745                 struct status_block_msix *sblk = cp->status_blk.bnx2;
3746
3747                 tx_cid = TX_TSS_CID + sb_id - 1;
3748                 CNIC_WR(dev, BNX2_TSCH_TSS_CFG, (sb_id << 24) |
3749                         (TX_TSS_CID << 7));
3750                 cp->tx_cons_ptr = &sblk->status_tx_quick_consumer_index;
3751         }
3752         cp->tx_cons = *cp->tx_cons_ptr;
3753
3754         cid_addr = GET_CID_ADDR(tx_cid);
3755         if (CHIP_NUM(cp) == CHIP_NUM_5709) {
3756                 u32 cid_addr2 = GET_CID_ADDR(tx_cid + 4) + 0x40;
3757
3758                 for (i = 0; i < PHY_CTX_SIZE; i += 4)
3759                         cnic_ctx_wr(dev, cid_addr2, i, 0);
3760
3761                 offset0 = BNX2_L2CTX_TYPE_XI;
3762                 offset1 = BNX2_L2CTX_CMD_TYPE_XI;
3763                 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI_XI;
3764                 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO_XI;
3765         } else {
3766                 cnic_init_context(dev, tx_cid);
3767                 cnic_init_context(dev, tx_cid + 1);
3768
3769                 offset0 = BNX2_L2CTX_TYPE;
3770                 offset1 = BNX2_L2CTX_CMD_TYPE;
3771                 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI;
3772                 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO;
3773         }
3774         val = BNX2_L2CTX_TYPE_TYPE_L2 | BNX2_L2CTX_TYPE_SIZE_L2;
3775         cnic_ctx_wr(dev, cid_addr, offset0, val);
3776
3777         val = BNX2_L2CTX_CMD_TYPE_TYPE_L2 | (8 << 16);
3778         cnic_ctx_wr(dev, cid_addr, offset1, val);
3779
3780         txbd = (struct tx_bd *) udev->l2_ring;
3781
3782         buf_map = udev->l2_buf_map;
3783         for (i = 0; i < MAX_TX_DESC_CNT; i++, txbd++) {
3784                 txbd->tx_bd_haddr_hi = (u64) buf_map >> 32;
3785                 txbd->tx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
3786         }
3787         val = (u64) ring_map >> 32;
3788         cnic_ctx_wr(dev, cid_addr, offset2, val);
3789         txbd->tx_bd_haddr_hi = val;
3790
3791         val = (u64) ring_map & 0xffffffff;
3792         cnic_ctx_wr(dev, cid_addr, offset3, val);
3793         txbd->tx_bd_haddr_lo = val;
3794 }
3795
3796 static void cnic_init_bnx2_rx_ring(struct cnic_dev *dev)
3797 {
3798         struct cnic_local *cp = dev->cnic_priv;
3799         struct cnic_eth_dev *ethdev = cp->ethdev;
3800         struct cnic_uio_dev *udev = cp->udev;
3801         u32 cid_addr, sb_id, val, coal_reg, coal_val;
3802         int i;
3803         struct rx_bd *rxbd;
3804         struct status_block *s_blk = cp->status_blk.gen;
3805         dma_addr_t ring_map = udev->l2_ring_map;
3806
3807         sb_id = cp->status_blk_num;
3808         cnic_init_context(dev, 2);
3809         cp->rx_cons_ptr = &s_blk->status_rx_quick_consumer_index2;
3810         coal_reg = BNX2_HC_COMMAND;
3811         coal_val = CNIC_RD(dev, coal_reg);
3812         if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
3813                 struct status_block_msix *sblk = cp->status_blk.bnx2;
3814
3815                 cp->rx_cons_ptr = &sblk->status_rx_quick_consumer_index;
3816                 coal_reg = BNX2_HC_COALESCE_NOW;
3817                 coal_val = 1 << (11 + sb_id);
3818         }
3819         i = 0;
3820         while (!(*cp->rx_cons_ptr != 0) && i < 10) {
3821                 CNIC_WR(dev, coal_reg, coal_val);
3822                 udelay(10);
3823                 i++;
3824                 barrier();
3825         }
3826         cp->rx_cons = *cp->rx_cons_ptr;
3827
3828         cid_addr = GET_CID_ADDR(2);
3829         val = BNX2_L2CTX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
3830               BNX2_L2CTX_CTX_TYPE_SIZE_L2 | (0x02 << 8);
3831         cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_CTX_TYPE, val);
3832
3833         if (sb_id == 0)
3834                 val = 2 << BNX2_L2CTX_L2_STATUSB_NUM_SHIFT;
3835         else
3836                 val = BNX2_L2CTX_L2_STATUSB_NUM(sb_id);
3837         cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_HOST_BDIDX, val);
3838
3839         rxbd = (struct rx_bd *) (udev->l2_ring + BCM_PAGE_SIZE);
3840         for (i = 0; i < MAX_RX_DESC_CNT; i++, rxbd++) {
3841                 dma_addr_t buf_map;
3842                 int n = (i % cp->l2_rx_ring_size) + 1;
3843
3844                 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
3845                 rxbd->rx_bd_len = cp->l2_single_buf_size;
3846                 rxbd->rx_bd_flags = RX_BD_FLAGS_START | RX_BD_FLAGS_END;
3847                 rxbd->rx_bd_haddr_hi = (u64) buf_map >> 32;
3848                 rxbd->rx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
3849         }
3850         val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
3851         cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_HI, val);
3852         rxbd->rx_bd_haddr_hi = val;
3853
3854         val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
3855         cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_LO, val);
3856         rxbd->rx_bd_haddr_lo = val;
3857
3858         val = cnic_reg_rd_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD);
3859         cnic_reg_wr_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD, val | (1 << 2));
3860 }
3861
3862 static void cnic_shutdown_bnx2_rx_ring(struct cnic_dev *dev)
3863 {
3864         struct kwqe *wqes[1], l2kwqe;
3865
3866         memset(&l2kwqe, 0, sizeof(l2kwqe));
3867         wqes[0] = &l2kwqe;
3868         l2kwqe.kwqe_op_flag = (L2_LAYER_CODE << KWQE_FLAGS_LAYER_SHIFT) |
3869                               (L2_KWQE_OPCODE_VALUE_FLUSH <<
3870                                KWQE_OPCODE_SHIFT) | 2;
3871         dev->submit_kwqes(dev, wqes, 1);
3872 }
3873
3874 static void cnic_set_bnx2_mac(struct cnic_dev *dev)
3875 {
3876         struct cnic_local *cp = dev->cnic_priv;
3877         u32 val;
3878
3879         val = cp->func << 2;
3880
3881         cp->shmem_base = cnic_reg_rd_ind(dev, BNX2_SHM_HDR_ADDR_0 + val);
3882
3883         val = cnic_reg_rd_ind(dev, cp->shmem_base +
3884                               BNX2_PORT_HW_CFG_ISCSI_MAC_UPPER);
3885         dev->mac_addr[0] = (u8) (val >> 8);
3886         dev->mac_addr[1] = (u8) val;
3887
3888         CNIC_WR(dev, BNX2_EMAC_MAC_MATCH4, val);
3889
3890         val = cnic_reg_rd_ind(dev, cp->shmem_base +
3891                               BNX2_PORT_HW_CFG_ISCSI_MAC_LOWER);
3892         dev->mac_addr[2] = (u8) (val >> 24);
3893         dev->mac_addr[3] = (u8) (val >> 16);
3894         dev->mac_addr[4] = (u8) (val >> 8);
3895         dev->mac_addr[5] = (u8) val;
3896
3897         CNIC_WR(dev, BNX2_EMAC_MAC_MATCH5, val);
3898
3899         val = 4 | BNX2_RPM_SORT_USER2_BC_EN;
3900         if (CHIP_NUM(cp) != CHIP_NUM_5709)
3901                 val |= BNX2_RPM_SORT_USER2_PROM_VLAN;
3902
3903         CNIC_WR(dev, BNX2_RPM_SORT_USER2, 0x0);
3904         CNIC_WR(dev, BNX2_RPM_SORT_USER2, val);
3905         CNIC_WR(dev, BNX2_RPM_SORT_USER2, val | BNX2_RPM_SORT_USER2_ENA);
3906 }
3907
3908 static int cnic_start_bnx2_hw(struct cnic_dev *dev)
3909 {
3910         struct cnic_local *cp = dev->cnic_priv;
3911         struct cnic_eth_dev *ethdev = cp->ethdev;
3912         struct status_block *sblk = cp->status_blk.gen;
3913         u32 val, kcq_cid_addr, kwq_cid_addr;
3914         int err;
3915
3916         cnic_set_bnx2_mac(dev);
3917
3918         val = CNIC_RD(dev, BNX2_MQ_CONFIG);
3919         val &= ~BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE;
3920         if (BCM_PAGE_BITS > 12)
3921                 val |= (12 - 8)  << 4;
3922         else
3923                 val |= (BCM_PAGE_BITS - 8)  << 4;
3924
3925         CNIC_WR(dev, BNX2_MQ_CONFIG, val);
3926
3927         CNIC_WR(dev, BNX2_HC_COMP_PROD_TRIP, (2 << 16) | 8);
3928         CNIC_WR(dev, BNX2_HC_COM_TICKS, (64 << 16) | 220);
3929         CNIC_WR(dev, BNX2_HC_CMD_TICKS, (64 << 16) | 220);
3930
3931         err = cnic_setup_5709_context(dev, 1);
3932         if (err)
3933                 return err;
3934
3935         cnic_init_context(dev, KWQ_CID);
3936         cnic_init_context(dev, KCQ_CID);
3937
3938         kwq_cid_addr = GET_CID_ADDR(KWQ_CID);
3939         cp->kwq_io_addr = MB_GET_CID_ADDR(KWQ_CID) + L5_KRNLQ_HOST_QIDX;
3940
3941         cp->max_kwq_idx = MAX_KWQ_IDX;
3942         cp->kwq_prod_idx = 0;
3943         cp->kwq_con_idx = 0;
3944         set_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
3945
3946         if (CHIP_NUM(cp) == CHIP_NUM_5706 || CHIP_NUM(cp) == CHIP_NUM_5708)
3947                 cp->kwq_con_idx_ptr = &sblk->status_rx_quick_consumer_index15;
3948         else
3949                 cp->kwq_con_idx_ptr = &sblk->status_cmd_consumer_index;
3950
3951         /* Initialize the kernel work queue context. */
3952         val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
3953               (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
3954         cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_TYPE, val);
3955
3956         val = (BCM_PAGE_SIZE / sizeof(struct kwqe) - 1) << 16;
3957         cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
3958
3959         val = ((BCM_PAGE_SIZE / sizeof(struct kwqe)) << 16) | KWQ_PAGE_CNT;
3960         cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
3961
3962         val = (u32) ((u64) cp->kwq_info.pgtbl_map >> 32);
3963         cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
3964
3965         val = (u32) cp->kwq_info.pgtbl_map;
3966         cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
3967
3968         kcq_cid_addr = GET_CID_ADDR(KCQ_CID);
3969         cp->kcq1.io_addr = MB_GET_CID_ADDR(KCQ_CID) + L5_KRNLQ_HOST_QIDX;
3970
3971         cp->kcq1.sw_prod_idx = 0;
3972         cp->kcq1.hw_prod_idx_ptr =
3973                 (u16 *) &sblk->status_completion_producer_index;
3974
3975         cp->kcq1.status_idx_ptr = (u16 *) &sblk->status_idx;
3976
3977         /* Initialize the kernel complete queue context. */
3978         val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
3979               (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
3980         cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_TYPE, val);
3981
3982         val = (BCM_PAGE_SIZE / sizeof(struct kcqe) - 1) << 16;
3983         cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
3984
3985         val = ((BCM_PAGE_SIZE / sizeof(struct kcqe)) << 16) | KCQ_PAGE_CNT;
3986         cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
3987
3988         val = (u32) ((u64) cp->kcq1.dma.pgtbl_map >> 32);
3989         cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
3990
3991         val = (u32) cp->kcq1.dma.pgtbl_map;
3992         cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
3993
3994         cp->int_num = 0;
3995         if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
3996                 struct status_block_msix *msblk = cp->status_blk.bnx2;
3997                 u32 sb_id = cp->status_blk_num;
3998                 u32 sb = BNX2_L2CTX_L5_STATUSB_NUM(sb_id);
3999
4000                 cp->kcq1.hw_prod_idx_ptr =
4001                         (u16 *) &msblk->status_completion_producer_index;
4002                 cp->kcq1.status_idx_ptr = (u16 *) &msblk->status_idx;
4003                 cp->kwq_con_idx_ptr = (u16 *) &msblk->status_cmd_consumer_index;
4004                 cp->int_num = sb_id << BNX2_PCICFG_INT_ACK_CMD_INT_NUM_SHIFT;
4005                 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
4006                 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
4007         }
4008
4009         /* Enable Commnad Scheduler notification when we write to the
4010          * host producer index of the kernel contexts. */
4011         CNIC_WR(dev, BNX2_MQ_KNL_CMD_MASK1, 2);
4012
4013         /* Enable Command Scheduler notification when we write to either
4014          * the Send Queue or Receive Queue producer indexes of the kernel
4015          * bypass contexts. */
4016         CNIC_WR(dev, BNX2_MQ_KNL_BYP_CMD_MASK1, 7);
4017         CNIC_WR(dev, BNX2_MQ_KNL_BYP_WRITE_MASK1, 7);
4018
4019         /* Notify COM when the driver post an application buffer. */
4020         CNIC_WR(dev, BNX2_MQ_KNL_RX_V2P_MASK2, 0x2000);
4021
4022         /* Set the CP and COM doorbells.  These two processors polls the
4023          * doorbell for a non zero value before running.  This must be done
4024          * after setting up the kernel queue contexts. */
4025         cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 1);
4026         cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 1);
4027
4028         cnic_init_bnx2_tx_ring(dev);
4029         cnic_init_bnx2_rx_ring(dev);
4030
4031         err = cnic_init_bnx2_irq(dev);
4032         if (err) {
4033                 netdev_err(dev->netdev, "cnic_init_irq failed\n");
4034                 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
4035                 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
4036                 return err;
4037         }
4038
4039         return 0;
4040 }
4041
4042 static void cnic_setup_bnx2x_context(struct cnic_dev *dev)
4043 {
4044         struct cnic_local *cp = dev->cnic_priv;
4045         struct cnic_eth_dev *ethdev = cp->ethdev;
4046         u32 start_offset = ethdev->ctx_tbl_offset;
4047         int i;
4048
4049         for (i = 0; i < cp->ctx_blks; i++) {
4050                 struct cnic_ctx *ctx = &cp->ctx_arr[i];
4051                 dma_addr_t map = ctx->mapping;
4052
4053                 if (cp->ctx_align) {
4054                         unsigned long mask = cp->ctx_align - 1;
4055
4056                         map = (map + mask) & ~mask;
4057                 }
4058
4059                 cnic_ctx_tbl_wr(dev, start_offset + i, map);
4060         }
4061 }
4062
4063 static int cnic_init_bnx2x_irq(struct cnic_dev *dev)
4064 {
4065         struct cnic_local *cp = dev->cnic_priv;
4066         struct cnic_eth_dev *ethdev = cp->ethdev;
4067         int err = 0;
4068
4069         tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2x_bh,
4070                      (unsigned long) dev);
4071         if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
4072                 err = cnic_request_irq(dev);
4073
4074         return err;
4075 }
4076
4077 static inline void cnic_storm_memset_hc_disable(struct cnic_dev *dev,
4078                                                 u16 sb_id, u8 sb_index,
4079                                                 u8 disable)
4080 {
4081
4082         u32 addr = BAR_CSTRORM_INTMEM +
4083                         CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4084                         offsetof(struct hc_status_block_data_e1x, index_data) +
4085                         sizeof(struct hc_index_data)*sb_index +
4086                         offsetof(struct hc_index_data, flags);
4087         u16 flags = CNIC_RD16(dev, addr);
4088         /* clear and set */
4089         flags &= ~HC_INDEX_DATA_HC_ENABLED;
4090         flags |= (((~disable) << HC_INDEX_DATA_HC_ENABLED_SHIFT) &
4091                   HC_INDEX_DATA_HC_ENABLED);
4092         CNIC_WR16(dev, addr, flags);
4093 }
4094
4095 static void cnic_enable_bnx2x_int(struct cnic_dev *dev)
4096 {
4097         struct cnic_local *cp = dev->cnic_priv;
4098         u8 sb_id = cp->status_blk_num;
4099
4100         CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
4101                         CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4102                         offsetof(struct hc_status_block_data_e1x, index_data) +
4103                         sizeof(struct hc_index_data)*HC_INDEX_ISCSI_EQ_CONS +
4104                         offsetof(struct hc_index_data, timeout), 64 / 12);
4105         cnic_storm_memset_hc_disable(dev, sb_id, HC_INDEX_ISCSI_EQ_CONS, 0);
4106 }
4107
4108 static void cnic_disable_bnx2x_int_sync(struct cnic_dev *dev)
4109 {
4110 }
4111
4112 static void cnic_init_bnx2x_tx_ring(struct cnic_dev *dev,
4113                                     struct client_init_ramrod_data *data)
4114 {
4115         struct cnic_local *cp = dev->cnic_priv;
4116         struct cnic_uio_dev *udev = cp->udev;
4117         union eth_tx_bd_types *txbd = (union eth_tx_bd_types *) udev->l2_ring;
4118         dma_addr_t buf_map, ring_map = udev->l2_ring_map;
4119         struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
4120         int port = CNIC_PORT(cp);
4121         int i;
4122         u32 cli = cp->ethdev->iscsi_l2_client_id;
4123         u32 val;
4124
4125         memset(txbd, 0, BCM_PAGE_SIZE);
4126
4127         buf_map = udev->l2_buf_map;
4128         for (i = 0; i < MAX_TX_DESC_CNT; i += 3, txbd += 3) {
4129                 struct eth_tx_start_bd *start_bd = &txbd->start_bd;
4130                 struct eth_tx_bd *reg_bd = &((txbd + 2)->reg_bd);
4131
4132                 start_bd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4133                 start_bd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4134                 reg_bd->addr_hi = start_bd->addr_hi;
4135                 reg_bd->addr_lo = start_bd->addr_lo + 0x10;
4136                 start_bd->nbytes = cpu_to_le16(0x10);
4137                 start_bd->nbd = cpu_to_le16(3);
4138                 start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
4139                 start_bd->general_data = (UNICAST_ADDRESS <<
4140                         ETH_TX_START_BD_ETH_ADDR_TYPE_SHIFT);
4141                 start_bd->general_data |= (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
4142
4143         }
4144
4145         val = (u64) ring_map >> 32;
4146         txbd->next_bd.addr_hi = cpu_to_le32(val);
4147
4148         data->tx.tx_bd_page_base.hi = cpu_to_le32(val);
4149
4150         val = (u64) ring_map & 0xffffffff;
4151         txbd->next_bd.addr_lo = cpu_to_le32(val);
4152
4153         data->tx.tx_bd_page_base.lo = cpu_to_le32(val);
4154
4155         /* Other ramrod params */
4156         data->tx.tx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_CQ_CONS;
4157         data->tx.tx_status_block_id = BNX2X_DEF_SB_ID;
4158
4159         /* reset xstorm per client statistics */
4160         if (cli < MAX_STAT_COUNTER_ID) {
4161                 val = BAR_XSTRORM_INTMEM +
4162                       XSTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4163                 for (i = 0; i < sizeof(struct xstorm_per_client_stats) / 4; i++)
4164                         CNIC_WR(dev, val + i * 4, 0);
4165         }
4166
4167         cp->tx_cons_ptr =
4168                 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_CQ_CONS];
4169 }
4170
4171 static void cnic_init_bnx2x_rx_ring(struct cnic_dev *dev,
4172                                     struct client_init_ramrod_data *data)
4173 {
4174         struct cnic_local *cp = dev->cnic_priv;
4175         struct cnic_uio_dev *udev = cp->udev;
4176         struct eth_rx_bd *rxbd = (struct eth_rx_bd *) (udev->l2_ring +
4177                                 BCM_PAGE_SIZE);
4178         struct eth_rx_cqe_next_page *rxcqe = (struct eth_rx_cqe_next_page *)
4179                                 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
4180         struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
4181         int i;
4182         int port = CNIC_PORT(cp);
4183         u32 cli = cp->ethdev->iscsi_l2_client_id;
4184         int cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
4185         u32 val;
4186         dma_addr_t ring_map = udev->l2_ring_map;
4187
4188         /* General data */
4189         data->general.client_id = cli;
4190         data->general.statistics_en_flg = 1;
4191         data->general.statistics_counter_id = cli;
4192         data->general.activate_flg = 1;
4193         data->general.sp_client_id = cli;
4194
4195         for (i = 0; i < BNX2X_MAX_RX_DESC_CNT; i++, rxbd++) {
4196                 dma_addr_t buf_map;
4197                 int n = (i % cp->l2_rx_ring_size) + 1;
4198
4199                 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
4200                 rxbd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4201                 rxbd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4202         }
4203
4204         val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
4205         rxbd->addr_hi = cpu_to_le32(val);
4206         data->rx.bd_page_base.hi = cpu_to_le32(val);
4207
4208         val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
4209         rxbd->addr_lo = cpu_to_le32(val);
4210         data->rx.bd_page_base.lo = cpu_to_le32(val);
4211
4212         rxcqe += BNX2X_MAX_RCQ_DESC_CNT;
4213         val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) >> 32;
4214         rxcqe->addr_hi = cpu_to_le32(val);
4215         data->rx.cqe_page_base.hi = cpu_to_le32(val);
4216
4217         val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) & 0xffffffff;
4218         rxcqe->addr_lo = cpu_to_le32(val);
4219         data->rx.cqe_page_base.lo = cpu_to_le32(val);
4220
4221         /* Other ramrod params */
4222         data->rx.client_qzone_id = cl_qzone_id;
4223         data->rx.rx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS;
4224         data->rx.status_block_id = BNX2X_DEF_SB_ID;
4225
4226         data->rx.cache_line_alignment_log_size = L1_CACHE_SHIFT;
4227         data->rx.bd_buff_size = cpu_to_le16(cp->l2_single_buf_size);
4228
4229         data->rx.mtu = cpu_to_le16(cp->l2_single_buf_size - 14);
4230         data->rx.outer_vlan_removal_enable_flg = 1;
4231
4232         /* reset tstorm and ustorm per client statistics */
4233         if (cli < MAX_STAT_COUNTER_ID) {
4234                 val = BAR_TSTRORM_INTMEM +
4235                       TSTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4236                 for (i = 0; i < sizeof(struct tstorm_per_client_stats) / 4; i++)
4237                         CNIC_WR(dev, val + i * 4, 0);
4238
4239                 val = BAR_USTRORM_INTMEM +
4240                       USTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4241                 for (i = 0; i < sizeof(struct ustorm_per_client_stats) / 4; i++)
4242                         CNIC_WR(dev, val + i * 4, 0);
4243         }
4244
4245         cp->rx_cons_ptr =
4246                 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS];
4247         cp->rx_cons = *cp->rx_cons_ptr;
4248 }
4249
4250 static void cnic_get_bnx2x_iscsi_info(struct cnic_dev *dev)
4251 {
4252         struct cnic_local *cp = dev->cnic_priv;
4253         u32 base, base2, addr, val;
4254         int port = CNIC_PORT(cp);
4255
4256         dev->max_iscsi_conn = 0;
4257         base = CNIC_RD(dev, MISC_REG_SHARED_MEM_ADDR);
4258         if (base == 0)
4259                 return;
4260
4261         base2 = CNIC_RD(dev, (CNIC_PATH(cp) ? MISC_REG_GENERIC_CR_1 :
4262                                               MISC_REG_GENERIC_CR_0));
4263         addr = BNX2X_SHMEM_ADDR(base,
4264                 dev_info.port_hw_config[port].iscsi_mac_upper);
4265
4266         val = CNIC_RD(dev, addr);
4267
4268         dev->mac_addr[0] = (u8) (val >> 8);
4269         dev->mac_addr[1] = (u8) val;
4270
4271         addr = BNX2X_SHMEM_ADDR(base,
4272                 dev_info.port_hw_config[port].iscsi_mac_lower);
4273
4274         val = CNIC_RD(dev, addr);
4275
4276         dev->mac_addr[2] = (u8) (val >> 24);
4277         dev->mac_addr[3] = (u8) (val >> 16);
4278         dev->mac_addr[4] = (u8) (val >> 8);
4279         dev->mac_addr[5] = (u8) val;
4280
4281         addr = BNX2X_SHMEM_ADDR(base, validity_map[port]);
4282         val = CNIC_RD(dev, addr);
4283
4284         if (!(val & SHR_MEM_VALIDITY_LIC_NO_KEY_IN_EFFECT)) {
4285                 u16 val16;
4286
4287                 addr = BNX2X_SHMEM_ADDR(base,
4288                                 drv_lic_key[port].max_iscsi_init_conn);
4289                 val16 = CNIC_RD16(dev, addr);
4290
4291                 if (val16)
4292                         val16 ^= 0x1e1e;
4293                 dev->max_iscsi_conn = val16;
4294         }
4295         if (BNX2X_CHIP_IS_E1H(cp->chip_id) || BNX2X_CHIP_IS_E2(cp->chip_id)) {
4296                 int func = CNIC_FUNC(cp);
4297                 u32 mf_cfg_addr;
4298
4299                 if (BNX2X_SHMEM2_HAS(base2, mf_cfg_addr))
4300                         mf_cfg_addr = CNIC_RD(dev, BNX2X_SHMEM2_ADDR(base2,
4301                                               mf_cfg_addr));
4302                 else
4303                         mf_cfg_addr = base + BNX2X_SHMEM_MF_BLK_OFFSET;
4304
4305                 addr = mf_cfg_addr +
4306                         offsetof(struct mf_cfg, func_mf_config[func].e1hov_tag);
4307
4308                 val = CNIC_RD(dev, addr);
4309                 val &= FUNC_MF_CFG_E1HOV_TAG_MASK;
4310                 if (val != FUNC_MF_CFG_E1HOV_TAG_DEFAULT) {
4311                         addr = mf_cfg_addr +
4312                                 offsetof(struct mf_cfg,
4313                                          func_mf_config[func].config);
4314                         val = CNIC_RD(dev, addr);
4315                         val &= FUNC_MF_CFG_PROTOCOL_MASK;
4316                         if (val != FUNC_MF_CFG_PROTOCOL_ISCSI)
4317                                 dev->max_iscsi_conn = 0;
4318                 }
4319         }
4320 }
4321
4322 static int cnic_start_bnx2x_hw(struct cnic_dev *dev)
4323 {
4324         struct cnic_local *cp = dev->cnic_priv;
4325         struct cnic_eth_dev *ethdev = cp->ethdev;
4326         int func = CNIC_FUNC(cp), ret, i;
4327         u32 pfid;
4328
4329         if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4330                 u32 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN_OVWR);
4331
4332                 if (!(val & 1))
4333                         val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN);
4334                 else
4335                         val = (val >> 1) & 1;
4336
4337                 if (val)
4338                         cp->pfid = func >> 1;
4339                 else
4340                         cp->pfid = func & 0x6;
4341         } else {
4342                 cp->pfid = func;
4343         }
4344         pfid = cp->pfid;
4345
4346         ret = cnic_init_id_tbl(&cp->cid_tbl, MAX_ISCSI_TBL_SZ,
4347                                cp->iscsi_start_cid);
4348
4349         if (ret)
4350                 return -ENOMEM;
4351
4352         cp->bnx2x_igu_sb_id = ethdev->irq_arr[0].status_blk_num2;
4353
4354         cp->kcq1.io_addr = BAR_CSTRORM_INTMEM +
4355                           CSTORM_ISCSI_EQ_PROD_OFFSET(pfid, 0);
4356         cp->kcq1.sw_prod_idx = 0;
4357
4358         if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4359                 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4360
4361                 cp->kcq1.hw_prod_idx_ptr =
4362                         &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4363                 cp->kcq1.status_idx_ptr =
4364                         &sb->sb.running_index[SM_RX_ID];
4365         } else {
4366                 struct host_hc_status_block_e1x *sb = cp->status_blk.gen;
4367
4368                 cp->kcq1.hw_prod_idx_ptr =
4369                         &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4370                 cp->kcq1.status_idx_ptr =
4371                         &sb->sb.running_index[SM_RX_ID];
4372         }
4373
4374         cnic_get_bnx2x_iscsi_info(dev);
4375
4376         /* Only 1 EQ */
4377         CNIC_WR16(dev, cp->kcq1.io_addr, MAX_KCQ_IDX);
4378         CNIC_WR(dev, BAR_CSTRORM_INTMEM +
4379                 CSTORM_ISCSI_EQ_CONS_OFFSET(pfid, 0), 0);
4380         CNIC_WR(dev, BAR_CSTRORM_INTMEM +
4381                 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0),
4382                 cp->kcq1.dma.pg_map_arr[1] & 0xffffffff);
4383         CNIC_WR(dev, BAR_CSTRORM_INTMEM +
4384                 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0) + 4,
4385                 (u64) cp->kcq1.dma.pg_map_arr[1] >> 32);
4386         CNIC_WR(dev, BAR_CSTRORM_INTMEM +
4387                 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0),
4388                 cp->kcq1.dma.pg_map_arr[0] & 0xffffffff);
4389         CNIC_WR(dev, BAR_CSTRORM_INTMEM +
4390                 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0) + 4,
4391                 (u64) cp->kcq1.dma.pg_map_arr[0] >> 32);
4392         CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
4393                 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_VALID_OFFSET(pfid, 0), 1);
4394         CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
4395                 CSTORM_ISCSI_EQ_SB_NUM_OFFSET(pfid, 0), cp->status_blk_num);
4396         CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
4397                 CSTORM_ISCSI_EQ_SB_INDEX_OFFSET(pfid, 0),
4398                 HC_INDEX_ISCSI_EQ_CONS);
4399
4400         for (i = 0; i < cp->conn_buf_info.num_pages; i++) {
4401                 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
4402                         TSTORM_ISCSI_CONN_BUF_PBL_OFFSET(pfid, i),
4403                         cp->conn_buf_info.pgtbl[2 * i]);
4404                 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
4405                         TSTORM_ISCSI_CONN_BUF_PBL_OFFSET(pfid, i) + 4,
4406                         cp->conn_buf_info.pgtbl[(2 * i) + 1]);
4407         }
4408
4409         CNIC_WR(dev, BAR_USTRORM_INTMEM +
4410                 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid),
4411                 cp->gbl_buf_info.pg_map_arr[0] & 0xffffffff);
4412         CNIC_WR(dev, BAR_USTRORM_INTMEM +
4413                 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid) + 4,
4414                 (u64) cp->gbl_buf_info.pg_map_arr[0] >> 32);
4415
4416         CNIC_WR(dev, BAR_TSTRORM_INTMEM +
4417                 TSTORM_ISCSI_TCP_LOCAL_ADV_WND_OFFSET(pfid), DEF_RCV_BUF);
4418
4419         cnic_setup_bnx2x_context(dev);
4420
4421         ret = cnic_init_bnx2x_irq(dev);
4422         if (ret)
4423                 return ret;
4424
4425         return 0;
4426 }
4427
4428 static void cnic_init_rings(struct cnic_dev *dev)
4429 {
4430         struct cnic_local *cp = dev->cnic_priv;
4431         struct cnic_uio_dev *udev = cp->udev;
4432
4433         if (test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
4434                 return;
4435
4436         if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
4437                 cnic_init_bnx2_tx_ring(dev);
4438                 cnic_init_bnx2_rx_ring(dev);
4439                 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
4440         } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
4441                 u32 cli = cp->ethdev->iscsi_l2_client_id;
4442                 u32 cid = cp->ethdev->iscsi_l2_cid;
4443                 u32 cl_qzone_id, type;
4444                 struct client_init_ramrod_data *data;
4445                 union l5cm_specific_data l5_data;
4446                 struct ustorm_eth_rx_producers rx_prods = {0};
4447                 u32 off, i;
4448
4449                 rx_prods.bd_prod = 0;
4450                 rx_prods.cqe_prod = BNX2X_MAX_RCQ_DESC_CNT;
4451                 barrier();
4452
4453                 cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
4454
4455                 off = BAR_USTRORM_INTMEM +
4456                         (BNX2X_CHIP_IS_E2(cp->chip_id) ?
4457                          USTORM_RX_PRODS_E2_OFFSET(cl_qzone_id) :
4458                          USTORM_RX_PRODS_E1X_OFFSET(CNIC_PORT(cp), cli));
4459
4460                 for (i = 0; i < sizeof(struct ustorm_eth_rx_producers) / 4; i++)
4461                         CNIC_WR(dev, off + i * 4, ((u32 *) &rx_prods)[i]);
4462
4463                 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
4464
4465                 data = udev->l2_buf;
4466
4467                 memset(data, 0, sizeof(*data));
4468
4469                 cnic_init_bnx2x_tx_ring(dev, data);
4470                 cnic_init_bnx2x_rx_ring(dev, data);
4471
4472                 l5_data.phy_address.lo = udev->l2_buf_map & 0xffffffff;
4473                 l5_data.phy_address.hi = (u64) udev->l2_buf_map >> 32;
4474
4475                 type = (ETH_CONNECTION_TYPE << SPE_HDR_CONN_TYPE_SHIFT)
4476                         & SPE_HDR_CONN_TYPE;
4477                 type |= ((cp->pfid << SPE_HDR_FUNCTION_ID_SHIFT) &
4478                         SPE_HDR_FUNCTION_ID);
4479
4480                 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
4481
4482                 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_CLIENT_SETUP,
4483                         cid, type, &l5_data);
4484
4485                 i = 0;
4486                 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
4487                        ++i < 10)
4488                         msleep(1);
4489
4490                 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
4491                         netdev_err(dev->netdev,
4492                                 "iSCSI CLIENT_SETUP did not complete\n");
4493                 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
4494                 cnic_ring_ctl(dev, cid, cli, 1);
4495         }
4496 }
4497
4498 static void cnic_shutdown_rings(struct cnic_dev *dev)
4499 {
4500         struct cnic_local *cp = dev->cnic_priv;
4501
4502         if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
4503                 return;
4504
4505         if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
4506                 cnic_shutdown_bnx2_rx_ring(dev);
4507         } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
4508                 struct cnic_local *cp = dev->cnic_priv;
4509                 u32 cli = cp->ethdev->iscsi_l2_client_id;
4510                 u32 cid = cp->ethdev->iscsi_l2_cid;
4511                 union l5cm_specific_data l5_data;
4512                 int i;
4513                 u32 type;
4514
4515                 cnic_ring_ctl(dev, cid, cli, 0);
4516
4517                 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
4518
4519                 l5_data.phy_address.lo = cli;
4520                 l5_data.phy_address.hi = 0;
4521                 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_HALT,
4522                         cid, ETH_CONNECTION_TYPE, &l5_data);
4523                 i = 0;
4524                 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
4525                        ++i < 10)
4526                         msleep(1);
4527
4528                 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
4529                         netdev_err(dev->netdev,
4530                                 "iSCSI CLIENT_HALT did not complete\n");
4531                 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
4532
4533                 memset(&l5_data, 0, sizeof(l5_data));
4534                 type = (NONE_CONNECTION_TYPE << SPE_HDR_CONN_TYPE_SHIFT)
4535                         & SPE_HDR_CONN_TYPE;
4536                 type |= ((cp->pfid << SPE_HDR_FUNCTION_ID_SHIFT) &
4537                          SPE_HDR_FUNCTION_ID);
4538                 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
4539                         cid, type, &l5_data);
4540                 msleep(10);
4541         }
4542         clear_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
4543 }
4544
4545 static int cnic_register_netdev(struct cnic_dev *dev)
4546 {
4547         struct cnic_local *cp = dev->cnic_priv;
4548         struct cnic_eth_dev *ethdev = cp->ethdev;
4549         int err;
4550
4551         if (!ethdev)
4552                 return -ENODEV;
4553
4554         if (ethdev->drv_state & CNIC_DRV_STATE_REGD)
4555                 return 0;
4556
4557         err = ethdev->drv_register_cnic(dev->netdev, cp->cnic_ops, dev);
4558         if (err)
4559                 netdev_err(dev->netdev, "register_cnic failed\n");
4560
4561         return err;
4562 }
4563
4564 static void cnic_unregister_netdev(struct cnic_dev *dev)
4565 {
4566         struct cnic_local *cp = dev->cnic_priv;
4567         struct cnic_eth_dev *ethdev = cp->ethdev;
4568
4569         if (!ethdev)
4570                 return;
4571
4572         ethdev->drv_unregister_cnic(dev->netdev);
4573 }
4574
4575 static int cnic_start_hw(struct cnic_dev *dev)
4576 {
4577         struct cnic_local *cp = dev->cnic_priv;
4578         struct cnic_eth_dev *ethdev = cp->ethdev;
4579         int err;
4580
4581         if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
4582                 return -EALREADY;
4583
4584         dev->regview = ethdev->io_base;
4585         pci_dev_get(dev->pcidev);
4586         cp->func = PCI_FUNC(dev->pcidev->devfn);
4587         cp->status_blk.gen = ethdev->irq_arr[0].status_blk;
4588         cp->status_blk_num = ethdev->irq_arr[0].status_blk_num;
4589
4590         err = cp->alloc_resc(dev);
4591         if (err) {
4592                 netdev_err(dev->netdev, "allocate resource failure\n");
4593                 goto err1;
4594         }
4595
4596         err = cp->start_hw(dev);
4597         if (err)
4598                 goto err1;
4599
4600         err = cnic_cm_open(dev);
4601         if (err)
4602                 goto err1;
4603
4604         set_bit(CNIC_F_CNIC_UP, &dev->flags);
4605
4606         cp->enable_int(dev);
4607
4608         return 0;
4609
4610 err1:
4611         cp->free_resc(dev);
4612         pci_dev_put(dev->pcidev);
4613         return err;
4614 }
4615
4616 static void cnic_stop_bnx2_hw(struct cnic_dev *dev)
4617 {
4618         cnic_disable_bnx2_int_sync(dev);
4619
4620         cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
4621         cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
4622
4623         cnic_init_context(dev, KWQ_CID);
4624         cnic_init_context(dev, KCQ_CID);
4625
4626         cnic_setup_5709_context(dev, 0);
4627         cnic_free_irq(dev);
4628
4629         cnic_free_resc(dev);
4630 }
4631
4632
4633 static void cnic_stop_bnx2x_hw(struct cnic_dev *dev)
4634 {
4635         struct cnic_local *cp = dev->cnic_priv;
4636
4637         cnic_free_irq(dev);
4638         *cp->kcq1.hw_prod_idx_ptr = 0;
4639         CNIC_WR(dev, BAR_CSTRORM_INTMEM +
4640                 CSTORM_ISCSI_EQ_CONS_OFFSET(cp->pfid, 0), 0);
4641         CNIC_WR16(dev, cp->kcq1.io_addr, 0);
4642         cnic_free_resc(dev);
4643 }
4644
4645 static void cnic_stop_hw(struct cnic_dev *dev)
4646 {
4647         if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
4648                 struct cnic_local *cp = dev->cnic_priv;
4649                 int i = 0;
4650
4651                 /* Need to wait for the ring shutdown event to complete
4652                  * before clearing the CNIC_UP flag.
4653                  */
4654                 while (cp->udev->uio_dev != -1 && i < 15) {
4655                         msleep(100);
4656                         i++;
4657                 }
4658                 cnic_shutdown_rings(dev);
4659                 clear_bit(CNIC_F_CNIC_UP, &dev->flags);
4660                 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], NULL);
4661                 synchronize_rcu();
4662                 cnic_cm_shutdown(dev);
4663                 cp->stop_hw(dev);
4664                 pci_dev_put(dev->pcidev);
4665         }
4666 }
4667
4668 static void cnic_free_dev(struct cnic_dev *dev)
4669 {
4670         int i = 0;
4671
4672         while ((atomic_read(&dev->ref_count) != 0) && i < 10) {
4673                 msleep(100);
4674                 i++;
4675         }
4676         if (atomic_read(&dev->ref_count) != 0)
4677                 netdev_err(dev->netdev, "Failed waiting for ref count to go to zero\n");
4678
4679         netdev_info(dev->netdev, "Removed CNIC device\n");
4680         dev_put(dev->netdev);
4681         kfree(dev);
4682 }
4683
4684 static struct cnic_dev *cnic_alloc_dev(struct net_device *dev,
4685                                        struct pci_dev *pdev)
4686 {
4687         struct cnic_dev *cdev;
4688         struct cnic_local *cp;
4689         int alloc_size;
4690
4691         alloc_size = sizeof(struct cnic_dev) + sizeof(struct cnic_local);
4692
4693         cdev = kzalloc(alloc_size , GFP_KERNEL);
4694         if (cdev == NULL) {
4695                 netdev_err(dev, "allocate dev struct failure\n");
4696                 return NULL;
4697         }
4698
4699         cdev->netdev = dev;
4700         cdev->cnic_priv = (char *)cdev + sizeof(struct cnic_dev);
4701         cdev->register_device = cnic_register_device;
4702         cdev->unregister_device = cnic_unregister_device;
4703         cdev->iscsi_nl_msg_recv = cnic_iscsi_nl_msg_recv;
4704
4705         cp = cdev->cnic_priv;
4706         cp->dev = cdev;
4707         cp->l2_single_buf_size = 0x400;
4708         cp->l2_rx_ring_size = 3;
4709
4710         spin_lock_init(&cp->cnic_ulp_lock);
4711
4712         netdev_info(dev, "Added CNIC device\n");
4713
4714         return cdev;
4715 }
4716
4717 static struct cnic_dev *init_bnx2_cnic(struct net_device *dev)
4718 {
4719         struct pci_dev *pdev;
4720         struct cnic_dev *cdev;
4721         struct cnic_local *cp;
4722         struct cnic_eth_dev *ethdev = NULL;
4723         struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
4724
4725         probe = symbol_get(bnx2_cnic_probe);
4726         if (probe) {
4727                 ethdev = (*probe)(dev);
4728                 symbol_put(bnx2_cnic_probe);
4729         }
4730         if (!ethdev)
4731                 return NULL;
4732
4733         pdev = ethdev->pdev;
4734         if (!pdev)
4735                 return NULL;
4736
4737         dev_hold(dev);
4738         pci_dev_get(pdev);
4739         if (pdev->device == PCI_DEVICE_ID_NX2_5709 ||
4740             pdev->device == PCI_DEVICE_ID_NX2_5709S) {
4741                 u8 rev;
4742
4743                 pci_read_config_byte(pdev, PCI_REVISION_ID, &rev);
4744                 if (rev < 0x10) {
4745                         pci_dev_put(pdev);
4746                         goto cnic_err;
4747                 }
4748         }
4749         pci_dev_put(pdev);
4750
4751         cdev = cnic_alloc_dev(dev, pdev);
4752         if (cdev == NULL)
4753                 goto cnic_err;
4754
4755         set_bit(CNIC_F_BNX2_CLASS, &cdev->flags);
4756         cdev->submit_kwqes = cnic_submit_bnx2_kwqes;
4757
4758         cp = cdev->cnic_priv;
4759         cp->ethdev = ethdev;
4760         cdev->pcidev = pdev;
4761         cp->chip_id = ethdev->chip_id;
4762
4763         cp->cnic_ops = &cnic_bnx2_ops;
4764         cp->start_hw = cnic_start_bnx2_hw;
4765         cp->stop_hw = cnic_stop_bnx2_hw;
4766         cp->setup_pgtbl = cnic_setup_page_tbl;
4767         cp->alloc_resc = cnic_alloc_bnx2_resc;
4768         cp->free_resc = cnic_free_resc;
4769         cp->start_cm = cnic_cm_init_bnx2_hw;
4770         cp->stop_cm = cnic_cm_stop_bnx2_hw;
4771         cp->enable_int = cnic_enable_bnx2_int;
4772         cp->disable_int_sync = cnic_disable_bnx2_int_sync;
4773         cp->close_conn = cnic_close_bnx2_conn;
4774         cp->next_idx = cnic_bnx2_next_idx;
4775         cp->hw_idx = cnic_bnx2_hw_idx;
4776         return cdev;
4777
4778 cnic_err:
4779         dev_put(dev);
4780         return NULL;
4781 }
4782
4783 static struct cnic_dev *init_bnx2x_cnic(struct net_device *dev)
4784 {
4785         struct pci_dev *pdev;
4786         struct cnic_dev *cdev;
4787         struct cnic_local *cp;
4788         struct cnic_eth_dev *ethdev = NULL;
4789         struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
4790
4791         probe = symbol_get(bnx2x_cnic_probe);
4792         if (probe) {
4793                 ethdev = (*probe)(dev);
4794                 symbol_put(bnx2x_cnic_probe);
4795         }
4796         if (!ethdev)
4797                 return NULL;
4798
4799         pdev = ethdev->pdev;
4800         if (!pdev)
4801                 return NULL;
4802
4803         dev_hold(dev);
4804         cdev = cnic_alloc_dev(dev, pdev);
4805         if (cdev == NULL) {
4806                 dev_put(dev);
4807                 return NULL;
4808         }
4809
4810         set_bit(CNIC_F_BNX2X_CLASS, &cdev->flags);
4811         cdev->submit_kwqes = cnic_submit_bnx2x_kwqes;
4812
4813         cp = cdev->cnic_priv;
4814         cp->ethdev = ethdev;
4815         cdev->pcidev = pdev;
4816         cp->chip_id = ethdev->chip_id;
4817
4818         cp->cnic_ops = &cnic_bnx2x_ops;
4819         cp->start_hw = cnic_start_bnx2x_hw;
4820         cp->stop_hw = cnic_stop_bnx2x_hw;
4821         cp->setup_pgtbl = cnic_setup_page_tbl_le;
4822         cp->alloc_resc = cnic_alloc_bnx2x_resc;
4823         cp->free_resc = cnic_free_resc;
4824         cp->start_cm = cnic_cm_init_bnx2x_hw;
4825         cp->stop_cm = cnic_cm_stop_bnx2x_hw;
4826         cp->enable_int = cnic_enable_bnx2x_int;
4827         cp->disable_int_sync = cnic_disable_bnx2x_int_sync;
4828         if (BNX2X_CHIP_IS_E2(cp->chip_id))
4829                 cp->ack_int = cnic_ack_bnx2x_e2_msix;
4830         else
4831                 cp->ack_int = cnic_ack_bnx2x_msix;
4832         cp->close_conn = cnic_close_bnx2x_conn;
4833         cp->next_idx = cnic_bnx2x_next_idx;
4834         cp->hw_idx = cnic_bnx2x_hw_idx;
4835         return cdev;
4836 }
4837
4838 static struct cnic_dev *is_cnic_dev(struct net_device *dev)
4839 {
4840         struct ethtool_drvinfo drvinfo;
4841         struct cnic_dev *cdev = NULL;
4842
4843         if (dev->ethtool_ops && dev->ethtool_ops->get_drvinfo) {
4844                 memset(&drvinfo, 0, sizeof(drvinfo));
4845                 dev->ethtool_ops->get_drvinfo(dev, &drvinfo);
4846
4847                 if (!strcmp(drvinfo.driver, "bnx2"))
4848                         cdev = init_bnx2_cnic(dev);
4849                 if (!strcmp(drvinfo.driver, "bnx2x"))
4850                         cdev = init_bnx2x_cnic(dev);
4851                 if (cdev) {
4852                         write_lock(&cnic_dev_lock);
4853                         list_add(&cdev->list, &cnic_dev_list);
4854                         write_unlock(&cnic_dev_lock);
4855                 }
4856         }
4857         return cdev;
4858 }
4859
4860 /**
4861  * netdev event handler
4862  */
4863 static int cnic_netdev_event(struct notifier_block *this, unsigned long event,
4864                                                          void *ptr)
4865 {
4866         struct net_device *netdev = ptr;
4867         struct cnic_dev *dev;
4868         int if_type;
4869         int new_dev = 0;
4870
4871         dev = cnic_from_netdev(netdev);
4872
4873         if (!dev && (event == NETDEV_REGISTER || event == NETDEV_UP)) {
4874                 /* Check for the hot-plug device */
4875                 dev = is_cnic_dev(netdev);
4876                 if (dev) {
4877                         new_dev = 1;
4878                         cnic_hold(dev);
4879                 }
4880         }
4881         if (dev) {
4882                 struct cnic_local *cp = dev->cnic_priv;
4883
4884                 if (new_dev)
4885                         cnic_ulp_init(dev);
4886                 else if (event == NETDEV_UNREGISTER)
4887                         cnic_ulp_exit(dev);
4888
4889                 if (event == NETDEV_UP) {
4890                         if (cnic_register_netdev(dev) != 0) {
4891                                 cnic_put(dev);
4892                                 goto done;
4893                         }
4894                         if (!cnic_start_hw(dev))
4895                                 cnic_ulp_start(dev);
4896                 }
4897
4898                 rcu_read_lock();
4899                 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
4900                         struct cnic_ulp_ops *ulp_ops;
4901                         void *ctx;
4902
4903                         ulp_ops = rcu_dereference(cp->ulp_ops[if_type]);
4904                         if (!ulp_ops || !ulp_ops->indicate_netevent)
4905                                 continue;
4906
4907                         ctx = cp->ulp_handle[if_type];
4908
4909                         ulp_ops->indicate_netevent(ctx, event);
4910                 }
4911                 rcu_read_unlock();
4912
4913                 if (event == NETDEV_GOING_DOWN) {
4914                         cnic_ulp_stop(dev);
4915                         cnic_stop_hw(dev);
4916                         cnic_unregister_netdev(dev);
4917                 } else if (event == NETDEV_UNREGISTER) {
4918                         write_lock(&cnic_dev_lock);
4919                         list_del_init(&dev->list);
4920                         write_unlock(&cnic_dev_lock);
4921
4922                         cnic_put(dev);
4923                         cnic_free_dev(dev);
4924                         goto done;
4925                 }
4926                 cnic_put(dev);
4927         }
4928 done:
4929         return NOTIFY_DONE;
4930 }
4931
4932 static struct notifier_block cnic_netdev_notifier = {
4933         .notifier_call = cnic_netdev_event
4934 };
4935
4936 static void cnic_release(void)
4937 {
4938         struct cnic_dev *dev;
4939         struct cnic_uio_dev *udev;
4940
4941         while (!list_empty(&cnic_dev_list)) {
4942                 dev = list_entry(cnic_dev_list.next, struct cnic_dev, list);
4943                 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
4944                         cnic_ulp_stop(dev);
4945                         cnic_stop_hw(dev);
4946                 }
4947
4948                 cnic_ulp_exit(dev);
4949                 cnic_unregister_netdev(dev);
4950                 list_del_init(&dev->list);
4951                 cnic_free_dev(dev);
4952         }
4953         while (!list_empty(&cnic_udev_list)) {
4954                 udev = list_entry(cnic_udev_list.next, struct cnic_uio_dev,
4955                                   list);
4956                 cnic_free_uio(udev);
4957         }
4958 }
4959
4960 static int __init cnic_init(void)
4961 {
4962         int rc = 0;
4963
4964         pr_info("%s", version);
4965
4966         rc = register_netdevice_notifier(&cnic_netdev_notifier);
4967         if (rc) {
4968                 cnic_release();
4969                 return rc;
4970         }
4971
4972         cnic_wq = create_singlethread_workqueue("cnic_wq");
4973         if (!cnic_wq) {
4974                 cnic_release();
4975                 unregister_netdevice_notifier(&cnic_netdev_notifier);
4976                 return -ENOMEM;
4977         }
4978
4979         return 0;
4980 }
4981
4982 static void __exit cnic_exit(void)
4983 {
4984         unregister_netdevice_notifier(&cnic_netdev_notifier);
4985         cnic_release();
4986         destroy_workqueue(cnic_wq);
4987 }
4988
4989 module_init(cnic_init);
4990 module_exit(cnic_exit);