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