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