blob: df429959abdd9f8c2cbb64937ba23fcc45b84c32 [file] [log] [blame]
Michael Chana4636962009-06-08 18:14:43 -07001/* cnic.c: Broadcom CNIC core network driver.
2 *
Michael Chan3238a9b2012-02-05 15:24:40 +00003 * Copyright (c) 2006-2012 Broadcom Corporation
Michael Chana4636962009-06-08 18:14:43 -07004 *
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
Joe Perchesddf79b22010-02-17 15:01:54 +000013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Michael Chana4636962009-06-08 18:14:43 -070015#include <linux/module.h>
16
17#include <linux/kernel.h>
18#include <linux/errno.h>
19#include <linux/list.h>
20#include <linux/slab.h>
21#include <linux/pci.h>
22#include <linux/init.h>
23#include <linux/netdevice.h>
24#include <linux/uio_driver.h>
25#include <linux/in.h>
26#include <linux/dma-mapping.h>
27#include <linux/delay.h>
28#include <linux/ethtool.h>
29#include <linux/if_vlan.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
Michael Chan973e5742011-07-13 17:24:17 +000031#include <linux/random.h>
Michael Chana4636962009-06-08 18:14:43 -070032#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
33#define BCM_VLAN 1
34#endif
35#include <net/ip.h>
36#include <net/tcp.h>
37#include <net/route.h>
38#include <net/ipv6.h>
39#include <net/ip6_route.h>
David S. Millerc05e85a2009-10-12 23:18:35 -070040#include <net/ip6_checksum.h>
Michael Chana4636962009-06-08 18:14:43 -070041#include <scsi/iscsi_if.h>
42
43#include "cnic_if.h"
44#include "bnx2.h"
Dmitry Kravkov5d1e8592010-07-27 12:31:10 +000045#include "bnx2x/bnx2x_reg.h"
46#include "bnx2x/bnx2x_fw_defs.h"
47#include "bnx2x/bnx2x_hsi.h"
Jeff Kirsheradfc5212011-04-07 06:03:04 -070048#include "../../../scsi/bnx2i/57xx_iscsi_constants.h"
49#include "../../../scsi/bnx2i/57xx_iscsi_hsi.h"
Michael Chana4636962009-06-08 18:14:43 -070050#include "cnic.h"
51#include "cnic_defs.h"
52
53#define DRV_MODULE_NAME "cnic"
Michael Chana4636962009-06-08 18:14:43 -070054
55static char version[] __devinitdata =
56 "Broadcom NetXtreme II CNIC Driver " DRV_MODULE_NAME " v" CNIC_MODULE_VERSION " (" CNIC_MODULE_RELDATE ")\n";
57
58MODULE_AUTHOR("Michael Chan <mchan@broadcom.com> and John(Zongxi) "
59 "Chen (zongxi@broadcom.com");
60MODULE_DESCRIPTION("Broadcom NetXtreme II CNIC Driver");
61MODULE_LICENSE("GPL");
62MODULE_VERSION(CNIC_MODULE_VERSION);
63
Michael Chan8adc92402010-12-23 07:42:57 +000064/* cnic_dev_list modifications are protected by both rtnl and cnic_dev_lock */
Michael Chana4636962009-06-08 18:14:43 -070065static LIST_HEAD(cnic_dev_list);
Michael Chana3ceeeb2010-10-13 14:06:50 +000066static LIST_HEAD(cnic_udev_list);
Michael Chana4636962009-06-08 18:14:43 -070067static DEFINE_RWLOCK(cnic_dev_lock);
68static DEFINE_MUTEX(cnic_lock);
69
Eric Dumazet13707f92011-01-26 19:28:23 +000070static struct cnic_ulp_ops __rcu *cnic_ulp_tbl[MAX_CNIC_ULP_TYPE];
71
72/* helper function, assuming cnic_lock is held */
73static inline struct cnic_ulp_ops *cnic_ulp_tbl_prot(int type)
74{
75 return rcu_dereference_protected(cnic_ulp_tbl[type],
76 lockdep_is_held(&cnic_lock));
77}
Michael Chana4636962009-06-08 18:14:43 -070078
79static int cnic_service_bnx2(void *, void *);
Michael Chan71034ba2009-10-10 13:46:59 +000080static int cnic_service_bnx2x(void *, void *);
Michael Chana4636962009-06-08 18:14:43 -070081static int cnic_ctl(void *, struct cnic_ctl_info *);
82
83static struct cnic_ops cnic_bnx2_ops = {
84 .cnic_owner = THIS_MODULE,
85 .cnic_handler = cnic_service_bnx2,
86 .cnic_ctl = cnic_ctl,
87};
88
Michael Chan71034ba2009-10-10 13:46:59 +000089static struct cnic_ops cnic_bnx2x_ops = {
90 .cnic_owner = THIS_MODULE,
91 .cnic_handler = cnic_service_bnx2x,
92 .cnic_ctl = cnic_ctl,
93};
94
Michael Chanfdf24082010-10-13 14:06:47 +000095static struct workqueue_struct *cnic_wq;
96
Michael Chan86b53602009-10-10 13:46:57 +000097static void cnic_shutdown_rings(struct cnic_dev *);
98static void cnic_init_rings(struct cnic_dev *);
Michael Chana4636962009-06-08 18:14:43 -070099static int cnic_cm_set_pg(struct cnic_sock *);
100
101static int cnic_uio_open(struct uio_info *uinfo, struct inode *inode)
102{
Michael Chancd801532010-10-13 14:06:49 +0000103 struct cnic_uio_dev *udev = uinfo->priv;
104 struct cnic_dev *dev;
Michael Chana4636962009-06-08 18:14:43 -0700105
106 if (!capable(CAP_NET_ADMIN))
107 return -EPERM;
108
Michael Chancd801532010-10-13 14:06:49 +0000109 if (udev->uio_dev != -1)
Michael Chana4636962009-06-08 18:14:43 -0700110 return -EBUSY;
111
Michael Chan86b53602009-10-10 13:46:57 +0000112 rtnl_lock();
Michael Chancd801532010-10-13 14:06:49 +0000113 dev = udev->dev;
114
Michael Chana3ceeeb2010-10-13 14:06:50 +0000115 if (!dev || !test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
Michael Chan86b53602009-10-10 13:46:57 +0000116 rtnl_unlock();
117 return -ENODEV;
118 }
119
Michael Chancd801532010-10-13 14:06:49 +0000120 udev->uio_dev = iminor(inode);
Michael Chana4636962009-06-08 18:14:43 -0700121
Michael Chana3ceeeb2010-10-13 14:06:50 +0000122 cnic_shutdown_rings(dev);
Michael Chan86b53602009-10-10 13:46:57 +0000123 cnic_init_rings(dev);
124 rtnl_unlock();
Michael Chana4636962009-06-08 18:14:43 -0700125
126 return 0;
127}
128
129static int cnic_uio_close(struct uio_info *uinfo, struct inode *inode)
130{
Michael Chancd801532010-10-13 14:06:49 +0000131 struct cnic_uio_dev *udev = uinfo->priv;
Michael Chan6ef57a02009-09-21 15:39:37 +0000132
Michael Chancd801532010-10-13 14:06:49 +0000133 udev->uio_dev = -1;
Michael Chana4636962009-06-08 18:14:43 -0700134 return 0;
135}
136
137static inline void cnic_hold(struct cnic_dev *dev)
138{
139 atomic_inc(&dev->ref_count);
140}
141
142static inline void cnic_put(struct cnic_dev *dev)
143{
144 atomic_dec(&dev->ref_count);
145}
146
147static inline void csk_hold(struct cnic_sock *csk)
148{
149 atomic_inc(&csk->ref_count);
150}
151
152static inline void csk_put(struct cnic_sock *csk)
153{
154 atomic_dec(&csk->ref_count);
155}
156
157static struct cnic_dev *cnic_from_netdev(struct net_device *netdev)
158{
159 struct cnic_dev *cdev;
160
161 read_lock(&cnic_dev_lock);
162 list_for_each_entry(cdev, &cnic_dev_list, list) {
163 if (netdev == cdev->netdev) {
164 cnic_hold(cdev);
165 read_unlock(&cnic_dev_lock);
166 return cdev;
167 }
168 }
169 read_unlock(&cnic_dev_lock);
170 return NULL;
171}
172
Michael Chan7fc1ece2009-08-14 15:49:47 +0000173static inline void ulp_get(struct cnic_ulp_ops *ulp_ops)
174{
175 atomic_inc(&ulp_ops->ref_count);
176}
177
178static inline void ulp_put(struct cnic_ulp_ops *ulp_ops)
179{
180 atomic_dec(&ulp_ops->ref_count);
181}
182
Michael Chana4636962009-06-08 18:14:43 -0700183static void cnic_ctx_wr(struct cnic_dev *dev, u32 cid_addr, u32 off, u32 val)
184{
185 struct cnic_local *cp = dev->cnic_priv;
186 struct cnic_eth_dev *ethdev = cp->ethdev;
187 struct drv_ctl_info info;
188 struct drv_ctl_io *io = &info.data.io;
189
190 info.cmd = DRV_CTL_CTX_WR_CMD;
191 io->cid_addr = cid_addr;
192 io->offset = off;
193 io->data = val;
194 ethdev->drv_ctl(dev->netdev, &info);
195}
196
Michael Chan71034ba2009-10-10 13:46:59 +0000197static void cnic_ctx_tbl_wr(struct cnic_dev *dev, u32 off, dma_addr_t addr)
198{
199 struct cnic_local *cp = dev->cnic_priv;
200 struct cnic_eth_dev *ethdev = cp->ethdev;
201 struct drv_ctl_info info;
202 struct drv_ctl_io *io = &info.data.io;
203
204 info.cmd = DRV_CTL_CTXTBL_WR_CMD;
205 io->offset = off;
206 io->dma_addr = addr;
207 ethdev->drv_ctl(dev->netdev, &info);
208}
209
210static void cnic_ring_ctl(struct cnic_dev *dev, u32 cid, u32 cl_id, int start)
211{
212 struct cnic_local *cp = dev->cnic_priv;
213 struct cnic_eth_dev *ethdev = cp->ethdev;
214 struct drv_ctl_info info;
215 struct drv_ctl_l2_ring *ring = &info.data.ring;
216
217 if (start)
218 info.cmd = DRV_CTL_START_L2_CMD;
219 else
220 info.cmd = DRV_CTL_STOP_L2_CMD;
221
222 ring->cid = cid;
223 ring->client_id = cl_id;
224 ethdev->drv_ctl(dev->netdev, &info);
225}
226
Michael Chana4636962009-06-08 18:14:43 -0700227static void cnic_reg_wr_ind(struct cnic_dev *dev, u32 off, u32 val)
228{
229 struct cnic_local *cp = dev->cnic_priv;
230 struct cnic_eth_dev *ethdev = cp->ethdev;
231 struct drv_ctl_info info;
232 struct drv_ctl_io *io = &info.data.io;
233
234 info.cmd = DRV_CTL_IO_WR_CMD;
235 io->offset = off;
236 io->data = val;
237 ethdev->drv_ctl(dev->netdev, &info);
238}
239
240static u32 cnic_reg_rd_ind(struct cnic_dev *dev, u32 off)
241{
242 struct cnic_local *cp = dev->cnic_priv;
243 struct cnic_eth_dev *ethdev = cp->ethdev;
244 struct drv_ctl_info info;
245 struct drv_ctl_io *io = &info.data.io;
246
247 info.cmd = DRV_CTL_IO_RD_CMD;
248 io->offset = off;
249 ethdev->drv_ctl(dev->netdev, &info);
250 return io->data;
251}
252
Barak Witkowski1d187b32011-12-05 22:41:50 +0000253static void cnic_ulp_ctl(struct cnic_dev *dev, int ulp_type, bool reg)
254{
255 struct cnic_local *cp = dev->cnic_priv;
256 struct cnic_eth_dev *ethdev = cp->ethdev;
257 struct drv_ctl_info info;
258
259 if (reg)
260 info.cmd = DRV_CTL_ULP_REGISTER_CMD;
261 else
262 info.cmd = DRV_CTL_ULP_UNREGISTER_CMD;
263
264 info.data.ulp_type = ulp_type;
265 ethdev->drv_ctl(dev->netdev, &info);
266}
267
Michael Chana4636962009-06-08 18:14:43 -0700268static int cnic_in_use(struct cnic_sock *csk)
269{
270 return test_bit(SK_F_INUSE, &csk->flags);
271}
272
Dmitry Kravkovc2bff632010-10-06 03:33:18 +0000273static void cnic_spq_completion(struct cnic_dev *dev, int cmd, u32 count)
Michael Chana4636962009-06-08 18:14:43 -0700274{
275 struct cnic_local *cp = dev->cnic_priv;
276 struct cnic_eth_dev *ethdev = cp->ethdev;
277 struct drv_ctl_info info;
278
Dmitry Kravkovc2bff632010-10-06 03:33:18 +0000279 info.cmd = cmd;
280 info.data.credit.credit_count = count;
Michael Chana4636962009-06-08 18:14:43 -0700281 ethdev->drv_ctl(dev->netdev, &info);
282}
283
Michael Chan71034ba2009-10-10 13:46:59 +0000284static int cnic_get_l5_cid(struct cnic_local *cp, u32 cid, u32 *l5_cid)
285{
286 u32 i;
287
Michael Chan520efdf2010-06-24 14:58:37 +0000288 for (i = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +0000289 if (cp->ctx_tbl[i].cid == cid) {
290 *l5_cid = i;
291 return 0;
292 }
293 }
294 return -EINVAL;
295}
296
Michael Chana4636962009-06-08 18:14:43 -0700297static int cnic_send_nlmsg(struct cnic_local *cp, u32 type,
298 struct cnic_sock *csk)
299{
300 struct iscsi_path path_req;
301 char *buf = NULL;
302 u16 len = 0;
303 u32 msg_type = ISCSI_KEVENT_IF_DOWN;
304 struct cnic_ulp_ops *ulp_ops;
Michael Chancd801532010-10-13 14:06:49 +0000305 struct cnic_uio_dev *udev = cp->udev;
Michael Chan939b82e2010-12-23 07:42:58 +0000306 int rc = 0, retry = 0;
Michael Chana4636962009-06-08 18:14:43 -0700307
Michael Chancd801532010-10-13 14:06:49 +0000308 if (!udev || udev->uio_dev == -1)
Michael Chana4636962009-06-08 18:14:43 -0700309 return -ENODEV;
310
311 if (csk) {
312 len = sizeof(path_req);
313 buf = (char *) &path_req;
314 memset(&path_req, 0, len);
315
316 msg_type = ISCSI_KEVENT_PATH_REQ;
317 path_req.handle = (u64) csk->l5_cid;
318 if (test_bit(SK_F_IPV6, &csk->flags)) {
319 memcpy(&path_req.dst.v6_addr, &csk->dst_ip[0],
320 sizeof(struct in6_addr));
321 path_req.ip_addr_len = 16;
322 } else {
323 memcpy(&path_req.dst.v4_addr, &csk->dst_ip[0],
324 sizeof(struct in_addr));
325 path_req.ip_addr_len = 4;
326 }
327 path_req.vlan_id = csk->vlan_id;
328 path_req.pmtu = csk->mtu;
329 }
330
Michael Chan939b82e2010-12-23 07:42:58 +0000331 while (retry < 3) {
332 rc = 0;
333 rcu_read_lock();
334 ulp_ops = rcu_dereference(cnic_ulp_tbl[CNIC_ULP_ISCSI]);
335 if (ulp_ops)
336 rc = ulp_ops->iscsi_nl_send_msg(
337 cp->ulp_handle[CNIC_ULP_ISCSI],
338 msg_type, buf, len);
339 rcu_read_unlock();
340 if (rc == 0 || msg_type != ISCSI_KEVENT_PATH_REQ)
341 break;
342
343 msleep(100);
344 retry++;
345 }
Michael Chan558e4c72011-07-13 17:24:20 +0000346 return rc;
Michael Chana4636962009-06-08 18:14:43 -0700347}
348
Eddie Wai42ecbb82010-12-23 07:43:02 +0000349static void cnic_cm_upcall(struct cnic_local *, struct cnic_sock *, u8);
350
Michael Chana4636962009-06-08 18:14:43 -0700351static int cnic_iscsi_nl_msg_recv(struct cnic_dev *dev, u32 msg_type,
352 char *buf, u16 len)
353{
354 int rc = -EINVAL;
355
356 switch (msg_type) {
357 case ISCSI_UEVENT_PATH_UPDATE: {
358 struct cnic_local *cp;
359 u32 l5_cid;
360 struct cnic_sock *csk;
361 struct iscsi_path *path_resp;
362
363 if (len < sizeof(*path_resp))
364 break;
365
366 path_resp = (struct iscsi_path *) buf;
367 cp = dev->cnic_priv;
368 l5_cid = (u32) path_resp->handle;
369 if (l5_cid >= MAX_CM_SK_TBL_SZ)
370 break;
371
Michael Chand02a5e62010-02-24 14:42:06 +0000372 rcu_read_lock();
373 if (!rcu_dereference(cp->ulp_ops[CNIC_ULP_L4])) {
374 rc = -ENODEV;
375 rcu_read_unlock();
376 break;
377 }
Michael Chana4636962009-06-08 18:14:43 -0700378 csk = &cp->csk_tbl[l5_cid];
379 csk_hold(csk);
Eddie Wai42ecbb82010-12-23 07:43:02 +0000380 if (cnic_in_use(csk) &&
381 test_bit(SK_F_CONNECT_START, &csk->flags)) {
382
Eddie Wai4cbbb042012-02-08 17:33:57 +0000383 csk->vlan_id = path_resp->vlan_id;
384
Michael Chana4636962009-06-08 18:14:43 -0700385 memcpy(csk->ha, path_resp->mac_addr, 6);
386 if (test_bit(SK_F_IPV6, &csk->flags))
387 memcpy(&csk->src_ip[0], &path_resp->src.v6_addr,
388 sizeof(struct in6_addr));
389 else
390 memcpy(&csk->src_ip[0], &path_resp->src.v4_addr,
391 sizeof(struct in_addr));
Eddie Wai42ecbb82010-12-23 07:43:02 +0000392
393 if (is_valid_ether_addr(csk->ha)) {
Michael Chana4636962009-06-08 18:14:43 -0700394 cnic_cm_set_pg(csk);
Eddie Wai42ecbb82010-12-23 07:43:02 +0000395 } else if (!test_bit(SK_F_OFFLD_SCHED, &csk->flags) &&
396 !test_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
397
398 cnic_cm_upcall(cp, csk,
399 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
400 clear_bit(SK_F_CONNECT_START, &csk->flags);
401 }
Michael Chana4636962009-06-08 18:14:43 -0700402 }
403 csk_put(csk);
Michael Chand02a5e62010-02-24 14:42:06 +0000404 rcu_read_unlock();
Michael Chana4636962009-06-08 18:14:43 -0700405 rc = 0;
406 }
407 }
408
409 return rc;
410}
411
412static int cnic_offld_prep(struct cnic_sock *csk)
413{
414 if (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
415 return 0;
416
417 if (!test_bit(SK_F_CONNECT_START, &csk->flags)) {
418 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
419 return 0;
420 }
421
422 return 1;
423}
424
425static int cnic_close_prep(struct cnic_sock *csk)
426{
427 clear_bit(SK_F_CONNECT_START, &csk->flags);
428 smp_mb__after_clear_bit();
429
430 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
431 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
432 msleep(1);
433
434 return 1;
435 }
436 return 0;
437}
438
439static int cnic_abort_prep(struct cnic_sock *csk)
440{
441 clear_bit(SK_F_CONNECT_START, &csk->flags);
442 smp_mb__after_clear_bit();
443
444 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
445 msleep(1);
446
447 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
448 csk->state = L4_KCQE_OPCODE_VALUE_RESET_COMP;
449 return 1;
450 }
451
452 return 0;
453}
454
455int cnic_register_driver(int ulp_type, struct cnic_ulp_ops *ulp_ops)
456{
457 struct cnic_dev *dev;
458
roel kluin0d37f362009-11-02 06:53:44 +0000459 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000460 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700461 return -EINVAL;
462 }
463 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000464 if (cnic_ulp_tbl_prot(ulp_type)) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000465 pr_err("%s: Type %d has already been registered\n",
466 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700467 mutex_unlock(&cnic_lock);
468 return -EBUSY;
469 }
470
471 read_lock(&cnic_dev_lock);
472 list_for_each_entry(dev, &cnic_dev_list, list) {
473 struct cnic_local *cp = dev->cnic_priv;
474
475 clear_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]);
476 }
477 read_unlock(&cnic_dev_lock);
478
Michael Chan7fc1ece2009-08-14 15:49:47 +0000479 atomic_set(&ulp_ops->ref_count, 0);
Michael Chana4636962009-06-08 18:14:43 -0700480 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], ulp_ops);
481 mutex_unlock(&cnic_lock);
482
483 /* Prevent race conditions with netdev_event */
484 rtnl_lock();
Michael Chana4636962009-06-08 18:14:43 -0700485 list_for_each_entry(dev, &cnic_dev_list, list) {
486 struct cnic_local *cp = dev->cnic_priv;
487
488 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]))
489 ulp_ops->cnic_init(dev);
490 }
Michael Chana4636962009-06-08 18:14:43 -0700491 rtnl_unlock();
492
493 return 0;
494}
495
496int cnic_unregister_driver(int ulp_type)
497{
498 struct cnic_dev *dev;
Michael Chan7fc1ece2009-08-14 15:49:47 +0000499 struct cnic_ulp_ops *ulp_ops;
500 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700501
roel kluin0d37f362009-11-02 06:53:44 +0000502 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000503 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700504 return -EINVAL;
505 }
506 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000507 ulp_ops = cnic_ulp_tbl_prot(ulp_type);
Michael Chan7fc1ece2009-08-14 15:49:47 +0000508 if (!ulp_ops) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000509 pr_err("%s: Type %d has not been registered\n",
510 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700511 goto out_unlock;
512 }
513 read_lock(&cnic_dev_lock);
514 list_for_each_entry(dev, &cnic_dev_list, list) {
515 struct cnic_local *cp = dev->cnic_priv;
516
517 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000518 pr_err("%s: Type %d still has devices registered\n",
519 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700520 read_unlock(&cnic_dev_lock);
521 goto out_unlock;
522 }
523 }
524 read_unlock(&cnic_dev_lock);
525
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000526 RCU_INIT_POINTER(cnic_ulp_tbl[ulp_type], NULL);
Michael Chana4636962009-06-08 18:14:43 -0700527
528 mutex_unlock(&cnic_lock);
529 synchronize_rcu();
Michael Chan7fc1ece2009-08-14 15:49:47 +0000530 while ((atomic_read(&ulp_ops->ref_count) != 0) && (i < 20)) {
531 msleep(100);
532 i++;
533 }
534
535 if (atomic_read(&ulp_ops->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +0000536 netdev_warn(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -0700537 return 0;
538
539out_unlock:
540 mutex_unlock(&cnic_lock);
541 return -EINVAL;
542}
543
544static int cnic_start_hw(struct cnic_dev *);
545static void cnic_stop_hw(struct cnic_dev *);
546
547static int cnic_register_device(struct cnic_dev *dev, int ulp_type,
548 void *ulp_ctx)
549{
550 struct cnic_local *cp = dev->cnic_priv;
551 struct cnic_ulp_ops *ulp_ops;
552
roel kluin0d37f362009-11-02 06:53:44 +0000553 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000554 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700555 return -EINVAL;
556 }
557 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000558 if (cnic_ulp_tbl_prot(ulp_type) == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000559 pr_err("%s: Driver with type %d has not been registered\n",
560 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700561 mutex_unlock(&cnic_lock);
562 return -EAGAIN;
563 }
564 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000565 pr_err("%s: Type %d has already been registered to this device\n",
566 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700567 mutex_unlock(&cnic_lock);
568 return -EBUSY;
569 }
570
571 clear_bit(ULP_F_START, &cp->ulp_flags[ulp_type]);
572 cp->ulp_handle[ulp_type] = ulp_ctx;
Eric Dumazet13707f92011-01-26 19:28:23 +0000573 ulp_ops = cnic_ulp_tbl_prot(ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700574 rcu_assign_pointer(cp->ulp_ops[ulp_type], ulp_ops);
575 cnic_hold(dev);
576
577 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
578 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[ulp_type]))
579 ulp_ops->cnic_start(cp->ulp_handle[ulp_type]);
580
581 mutex_unlock(&cnic_lock);
582
Barak Witkowski1d187b32011-12-05 22:41:50 +0000583 cnic_ulp_ctl(dev, ulp_type, true);
584
Michael Chana4636962009-06-08 18:14:43 -0700585 return 0;
586
587}
588EXPORT_SYMBOL(cnic_register_driver);
589
590static int cnic_unregister_device(struct cnic_dev *dev, int ulp_type)
591{
592 struct cnic_local *cp = dev->cnic_priv;
Michael Chan681dbd72009-08-14 15:49:46 +0000593 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700594
roel kluin0d37f362009-11-02 06:53:44 +0000595 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000596 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700597 return -EINVAL;
598 }
599 mutex_lock(&cnic_lock);
600 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000601 RCU_INIT_POINTER(cp->ulp_ops[ulp_type], NULL);
Michael Chana4636962009-06-08 18:14:43 -0700602 cnic_put(dev);
603 } else {
Joe Perchesddf79b22010-02-17 15:01:54 +0000604 pr_err("%s: device not registered to this ulp type %d\n",
605 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700606 mutex_unlock(&cnic_lock);
607 return -EINVAL;
608 }
609 mutex_unlock(&cnic_lock);
610
Michael Chan42bb8d52011-01-03 15:21:46 +0000611 if (ulp_type == CNIC_ULP_ISCSI)
612 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
613
Michael Chana4636962009-06-08 18:14:43 -0700614 synchronize_rcu();
615
Michael Chan681dbd72009-08-14 15:49:46 +0000616 while (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]) &&
617 i < 20) {
618 msleep(100);
619 i++;
620 }
621 if (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]))
Joe Perchesddf79b22010-02-17 15:01:54 +0000622 netdev_warn(dev->netdev, "Failed waiting for ULP up call to complete\n");
Michael Chan681dbd72009-08-14 15:49:46 +0000623
Barak Witkowski1d187b32011-12-05 22:41:50 +0000624 cnic_ulp_ctl(dev, ulp_type, false);
625
Michael Chana4636962009-06-08 18:14:43 -0700626 return 0;
627}
628EXPORT_SYMBOL(cnic_unregister_driver);
629
Eddie Wai11f23aa2011-06-08 19:29:34 +0000630static int cnic_init_id_tbl(struct cnic_id_tbl *id_tbl, u32 size, u32 start_id,
631 u32 next)
Michael Chana4636962009-06-08 18:14:43 -0700632{
633 id_tbl->start = start_id;
634 id_tbl->max = size;
Eddie Wai11f23aa2011-06-08 19:29:34 +0000635 id_tbl->next = next;
Michael Chana4636962009-06-08 18:14:43 -0700636 spin_lock_init(&id_tbl->lock);
637 id_tbl->table = kzalloc(DIV_ROUND_UP(size, 32) * 4, GFP_KERNEL);
638 if (!id_tbl->table)
639 return -ENOMEM;
640
641 return 0;
642}
643
644static void cnic_free_id_tbl(struct cnic_id_tbl *id_tbl)
645{
646 kfree(id_tbl->table);
647 id_tbl->table = NULL;
648}
649
650static int cnic_alloc_id(struct cnic_id_tbl *id_tbl, u32 id)
651{
652 int ret = -1;
653
654 id -= id_tbl->start;
655 if (id >= id_tbl->max)
656 return ret;
657
658 spin_lock(&id_tbl->lock);
659 if (!test_bit(id, id_tbl->table)) {
660 set_bit(id, id_tbl->table);
661 ret = 0;
662 }
663 spin_unlock(&id_tbl->lock);
664 return ret;
665}
666
667/* Returns -1 if not successful */
668static u32 cnic_alloc_new_id(struct cnic_id_tbl *id_tbl)
669{
670 u32 id;
671
672 spin_lock(&id_tbl->lock);
673 id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
674 if (id >= id_tbl->max) {
675 id = -1;
676 if (id_tbl->next != 0) {
677 id = find_first_zero_bit(id_tbl->table, id_tbl->next);
678 if (id >= id_tbl->next)
679 id = -1;
680 }
681 }
682
683 if (id < id_tbl->max) {
684 set_bit(id, id_tbl->table);
685 id_tbl->next = (id + 1) & (id_tbl->max - 1);
686 id += id_tbl->start;
687 }
688
689 spin_unlock(&id_tbl->lock);
690
691 return id;
692}
693
694static void cnic_free_id(struct cnic_id_tbl *id_tbl, u32 id)
695{
696 if (id == -1)
697 return;
698
699 id -= id_tbl->start;
700 if (id >= id_tbl->max)
701 return;
702
703 clear_bit(id, id_tbl->table);
704}
705
706static void cnic_free_dma(struct cnic_dev *dev, struct cnic_dma *dma)
707{
708 int i;
709
710 if (!dma->pg_arr)
711 return;
712
713 for (i = 0; i < dma->num_pages; i++) {
714 if (dma->pg_arr[i]) {
Michael Chan3248e162009-12-02 15:15:39 +0000715 dma_free_coherent(&dev->pcidev->dev, BCM_PAGE_SIZE,
716 dma->pg_arr[i], dma->pg_map_arr[i]);
Michael Chana4636962009-06-08 18:14:43 -0700717 dma->pg_arr[i] = NULL;
718 }
719 }
720 if (dma->pgtbl) {
Michael Chan3248e162009-12-02 15:15:39 +0000721 dma_free_coherent(&dev->pcidev->dev, dma->pgtbl_size,
722 dma->pgtbl, dma->pgtbl_map);
Michael Chana4636962009-06-08 18:14:43 -0700723 dma->pgtbl = NULL;
724 }
725 kfree(dma->pg_arr);
726 dma->pg_arr = NULL;
727 dma->num_pages = 0;
728}
729
730static void cnic_setup_page_tbl(struct cnic_dev *dev, struct cnic_dma *dma)
731{
732 int i;
Michael Chan51388262011-01-25 22:14:50 +0000733 __le32 *page_table = (__le32 *) dma->pgtbl;
Michael Chana4636962009-06-08 18:14:43 -0700734
735 for (i = 0; i < dma->num_pages; i++) {
736 /* Each entry needs to be in big endian format. */
Michael Chan51388262011-01-25 22:14:50 +0000737 *page_table = cpu_to_le32((u64) dma->pg_map_arr[i] >> 32);
Michael Chana4636962009-06-08 18:14:43 -0700738 page_table++;
Michael Chan51388262011-01-25 22:14:50 +0000739 *page_table = cpu_to_le32(dma->pg_map_arr[i] & 0xffffffff);
Michael Chana4636962009-06-08 18:14:43 -0700740 page_table++;
741 }
742}
743
Michael Chan71034ba2009-10-10 13:46:59 +0000744static void cnic_setup_page_tbl_le(struct cnic_dev *dev, struct cnic_dma *dma)
745{
746 int i;
Michael Chan51388262011-01-25 22:14:50 +0000747 __le32 *page_table = (__le32 *) dma->pgtbl;
Michael Chan71034ba2009-10-10 13:46:59 +0000748
749 for (i = 0; i < dma->num_pages; i++) {
750 /* Each entry needs to be in little endian format. */
Michael Chan51388262011-01-25 22:14:50 +0000751 *page_table = cpu_to_le32(dma->pg_map_arr[i] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +0000752 page_table++;
Michael Chan51388262011-01-25 22:14:50 +0000753 *page_table = cpu_to_le32((u64) dma->pg_map_arr[i] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +0000754 page_table++;
755 }
756}
757
Michael Chana4636962009-06-08 18:14:43 -0700758static int cnic_alloc_dma(struct cnic_dev *dev, struct cnic_dma *dma,
759 int pages, int use_pg_tbl)
760{
761 int i, size;
762 struct cnic_local *cp = dev->cnic_priv;
763
764 size = pages * (sizeof(void *) + sizeof(dma_addr_t));
765 dma->pg_arr = kzalloc(size, GFP_ATOMIC);
766 if (dma->pg_arr == NULL)
767 return -ENOMEM;
768
769 dma->pg_map_arr = (dma_addr_t *) (dma->pg_arr + pages);
770 dma->num_pages = pages;
771
772 for (i = 0; i < pages; i++) {
Michael Chan3248e162009-12-02 15:15:39 +0000773 dma->pg_arr[i] = dma_alloc_coherent(&dev->pcidev->dev,
774 BCM_PAGE_SIZE,
775 &dma->pg_map_arr[i],
776 GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700777 if (dma->pg_arr[i] == NULL)
778 goto error;
779 }
780 if (!use_pg_tbl)
781 return 0;
782
783 dma->pgtbl_size = ((pages * 8) + BCM_PAGE_SIZE - 1) &
784 ~(BCM_PAGE_SIZE - 1);
Michael Chan3248e162009-12-02 15:15:39 +0000785 dma->pgtbl = dma_alloc_coherent(&dev->pcidev->dev, dma->pgtbl_size,
786 &dma->pgtbl_map, GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700787 if (dma->pgtbl == NULL)
788 goto error;
789
790 cp->setup_pgtbl(dev, dma);
791
792 return 0;
793
794error:
795 cnic_free_dma(dev, dma);
796 return -ENOMEM;
797}
798
Michael Chan86b53602009-10-10 13:46:57 +0000799static void cnic_free_context(struct cnic_dev *dev)
800{
801 struct cnic_local *cp = dev->cnic_priv;
802 int i;
803
804 for (i = 0; i < cp->ctx_blks; i++) {
805 if (cp->ctx_arr[i].ctx) {
Michael Chan3248e162009-12-02 15:15:39 +0000806 dma_free_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
807 cp->ctx_arr[i].ctx,
808 cp->ctx_arr[i].mapping);
Michael Chan86b53602009-10-10 13:46:57 +0000809 cp->ctx_arr[i].ctx = NULL;
810 }
811 }
812}
813
Michael Chancd801532010-10-13 14:06:49 +0000814static void __cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chana4636962009-06-08 18:14:43 -0700815{
Michael Chancd801532010-10-13 14:06:49 +0000816 uio_unregister_device(&udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -0700817
Michael Chancd801532010-10-13 14:06:49 +0000818 if (udev->l2_buf) {
819 dma_free_coherent(&udev->pdev->dev, udev->l2_buf_size,
820 udev->l2_buf, udev->l2_buf_map);
821 udev->l2_buf = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700822 }
823
Michael Chancd801532010-10-13 14:06:49 +0000824 if (udev->l2_ring) {
825 dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
826 udev->l2_ring, udev->l2_ring_map);
827 udev->l2_ring = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700828 }
Michael Chana3ceeeb2010-10-13 14:06:50 +0000829
830 pci_dev_put(udev->pdev);
831 kfree(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000832}
833
Michael Chancd801532010-10-13 14:06:49 +0000834static void cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000835{
Michael Chancd801532010-10-13 14:06:49 +0000836 if (!udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000837 return;
838
Michael Chana3ceeeb2010-10-13 14:06:50 +0000839 write_lock(&cnic_dev_lock);
840 list_del_init(&udev->list);
841 write_unlock(&cnic_dev_lock);
Michael Chancd801532010-10-13 14:06:49 +0000842 __cnic_free_uio(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000843}
844
845static void cnic_free_resc(struct cnic_dev *dev)
846{
847 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000848 struct cnic_uio_dev *udev = cp->udev;
Michael Chanc06c0462010-10-13 14:06:48 +0000849
Michael Chancd801532010-10-13 14:06:49 +0000850 if (udev) {
Michael Chana3ceeeb2010-10-13 14:06:50 +0000851 udev->dev = NULL;
Michael Chancd801532010-10-13 14:06:49 +0000852 cp->udev = NULL;
Michael Chanc06c0462010-10-13 14:06:48 +0000853 }
Michael Chana4636962009-06-08 18:14:43 -0700854
Michael Chan86b53602009-10-10 13:46:57 +0000855 cnic_free_context(dev);
Michael Chana4636962009-06-08 18:14:43 -0700856 kfree(cp->ctx_arr);
857 cp->ctx_arr = NULL;
858 cp->ctx_blks = 0;
859
860 cnic_free_dma(dev, &cp->gbl_buf_info);
Michael Chana4636962009-06-08 18:14:43 -0700861 cnic_free_dma(dev, &cp->kwq_info);
Michael Chan71034ba2009-10-10 13:46:59 +0000862 cnic_free_dma(dev, &cp->kwq_16_data_info);
Michael Chane21ba412010-12-23 07:43:03 +0000863 cnic_free_dma(dev, &cp->kcq2.dma);
Michael Chane6c28892010-06-24 14:58:39 +0000864 cnic_free_dma(dev, &cp->kcq1.dma);
Michael Chana4636962009-06-08 18:14:43 -0700865 kfree(cp->iscsi_tbl);
866 cp->iscsi_tbl = NULL;
867 kfree(cp->ctx_tbl);
868 cp->ctx_tbl = NULL;
869
Michael Chane1928c82010-12-23 07:43:04 +0000870 cnic_free_id_tbl(&cp->fcoe_cid_tbl);
Michael Chana4636962009-06-08 18:14:43 -0700871 cnic_free_id_tbl(&cp->cid_tbl);
872}
873
874static int cnic_alloc_context(struct cnic_dev *dev)
875{
876 struct cnic_local *cp = dev->cnic_priv;
877
878 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
879 int i, k, arr_size;
880
881 cp->ctx_blk_size = BCM_PAGE_SIZE;
882 cp->cids_per_blk = BCM_PAGE_SIZE / 128;
883 arr_size = BNX2_MAX_CID / cp->cids_per_blk *
884 sizeof(struct cnic_ctx);
885 cp->ctx_arr = kzalloc(arr_size, GFP_KERNEL);
886 if (cp->ctx_arr == NULL)
887 return -ENOMEM;
888
889 k = 0;
890 for (i = 0; i < 2; i++) {
891 u32 j, reg, off, lo, hi;
892
893 if (i == 0)
894 off = BNX2_PG_CTX_MAP;
895 else
896 off = BNX2_ISCSI_CTX_MAP;
897
898 reg = cnic_reg_rd_ind(dev, off);
899 lo = reg >> 16;
900 hi = reg & 0xffff;
901 for (j = lo; j < hi; j += cp->cids_per_blk, k++)
902 cp->ctx_arr[k].cid = j;
903 }
904
905 cp->ctx_blks = k;
906 if (cp->ctx_blks >= (BNX2_MAX_CID / cp->cids_per_blk)) {
907 cp->ctx_blks = 0;
908 return -ENOMEM;
909 }
910
911 for (i = 0; i < cp->ctx_blks; i++) {
912 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +0000913 dma_alloc_coherent(&dev->pcidev->dev,
914 BCM_PAGE_SIZE,
915 &cp->ctx_arr[i].mapping,
916 GFP_KERNEL);
Michael Chana4636962009-06-08 18:14:43 -0700917 if (cp->ctx_arr[i].ctx == NULL)
918 return -ENOMEM;
919 }
920 }
921 return 0;
922}
923
Michael Chan59e51372011-06-14 01:32:38 +0000924static u16 cnic_bnx2_next_idx(u16 idx)
Michael Chane6c28892010-06-24 14:58:39 +0000925{
Michael Chan59e51372011-06-14 01:32:38 +0000926 return idx + 1;
927}
928
929static u16 cnic_bnx2_hw_idx(u16 idx)
930{
931 return idx;
932}
933
934static u16 cnic_bnx2x_next_idx(u16 idx)
935{
936 idx++;
937 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
938 idx++;
939
940 return idx;
941}
942
943static u16 cnic_bnx2x_hw_idx(u16 idx)
944{
945 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
946 idx++;
947 return idx;
948}
949
950static int cnic_alloc_kcq(struct cnic_dev *dev, struct kcq_info *info,
951 bool use_pg_tbl)
952{
953 int err, i, use_page_tbl = 0;
Michael Chane6c28892010-06-24 14:58:39 +0000954 struct kcqe **kcq;
955
Michael Chan59e51372011-06-14 01:32:38 +0000956 if (use_pg_tbl)
957 use_page_tbl = 1;
Michael Chane6c28892010-06-24 14:58:39 +0000958
Michael Chan59e51372011-06-14 01:32:38 +0000959 err = cnic_alloc_dma(dev, &info->dma, KCQ_PAGE_CNT, use_page_tbl);
Michael Chane6c28892010-06-24 14:58:39 +0000960 if (err)
961 return err;
962
963 kcq = (struct kcqe **) info->dma.pg_arr;
964 info->kcq = kcq;
965
Michael Chan59e51372011-06-14 01:32:38 +0000966 info->next_idx = cnic_bnx2_next_idx;
967 info->hw_idx = cnic_bnx2_hw_idx;
968 if (use_pg_tbl)
Michael Chane6c28892010-06-24 14:58:39 +0000969 return 0;
970
Michael Chan59e51372011-06-14 01:32:38 +0000971 info->next_idx = cnic_bnx2x_next_idx;
972 info->hw_idx = cnic_bnx2x_hw_idx;
973
Michael Chane6c28892010-06-24 14:58:39 +0000974 for (i = 0; i < KCQ_PAGE_CNT; i++) {
975 struct bnx2x_bd_chain_next *next =
976 (struct bnx2x_bd_chain_next *) &kcq[i][MAX_KCQE_CNT];
977 int j = i + 1;
978
979 if (j >= KCQ_PAGE_CNT)
980 j = 0;
981 next->addr_hi = (u64) info->dma.pg_map_arr[j] >> 32;
982 next->addr_lo = info->dma.pg_map_arr[j] & 0xffffffff;
983 }
984 return 0;
985}
986
Michael Chancd801532010-10-13 14:06:49 +0000987static int cnic_alloc_uio_rings(struct cnic_dev *dev, int pages)
Michael Chanec0248e2009-08-26 09:49:22 +0000988{
989 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000990 struct cnic_uio_dev *udev;
Michael Chanec0248e2009-08-26 09:49:22 +0000991
Michael Chana3ceeeb2010-10-13 14:06:50 +0000992 read_lock(&cnic_dev_lock);
993 list_for_each_entry(udev, &cnic_udev_list, list) {
994 if (udev->pdev == dev->pcidev) {
995 udev->dev = dev;
996 cp->udev = udev;
997 read_unlock(&cnic_dev_lock);
998 return 0;
999 }
1000 }
1001 read_unlock(&cnic_dev_lock);
1002
Michael Chancd801532010-10-13 14:06:49 +00001003 udev = kzalloc(sizeof(struct cnic_uio_dev), GFP_ATOMIC);
1004 if (!udev)
Michael Chanec0248e2009-08-26 09:49:22 +00001005 return -ENOMEM;
1006
Michael Chancd801532010-10-13 14:06:49 +00001007 udev->uio_dev = -1;
1008
1009 udev->dev = dev;
1010 udev->pdev = dev->pcidev;
1011 udev->l2_ring_size = pages * BCM_PAGE_SIZE;
1012 udev->l2_ring = dma_alloc_coherent(&udev->pdev->dev, udev->l2_ring_size,
1013 &udev->l2_ring_map,
1014 GFP_KERNEL | __GFP_COMP);
1015 if (!udev->l2_ring)
Jesper Juhlf7e4c972010-12-31 11:18:48 -08001016 goto err_udev;
Michael Chanec0248e2009-08-26 09:49:22 +00001017
Michael Chancd801532010-10-13 14:06:49 +00001018 udev->l2_buf_size = (cp->l2_rx_ring_size + 1) * cp->l2_single_buf_size;
1019 udev->l2_buf_size = PAGE_ALIGN(udev->l2_buf_size);
1020 udev->l2_buf = dma_alloc_coherent(&udev->pdev->dev, udev->l2_buf_size,
1021 &udev->l2_buf_map,
1022 GFP_KERNEL | __GFP_COMP);
1023 if (!udev->l2_buf)
Jesper Juhlf7e4c972010-12-31 11:18:48 -08001024 goto err_dma;
Michael Chancd801532010-10-13 14:06:49 +00001025
Michael Chana3ceeeb2010-10-13 14:06:50 +00001026 write_lock(&cnic_dev_lock);
1027 list_add(&udev->list, &cnic_udev_list);
1028 write_unlock(&cnic_dev_lock);
1029
1030 pci_dev_get(udev->pdev);
1031
Michael Chancd801532010-10-13 14:06:49 +00001032 cp->udev = udev;
1033
Michael Chanec0248e2009-08-26 09:49:22 +00001034 return 0;
Jesper Juhlf7e4c972010-12-31 11:18:48 -08001035 err_dma:
1036 dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
1037 udev->l2_ring, udev->l2_ring_map);
1038 err_udev:
1039 kfree(udev);
1040 return -ENOMEM;
Michael Chanec0248e2009-08-26 09:49:22 +00001041}
1042
Michael Chancd801532010-10-13 14:06:49 +00001043static int cnic_init_uio(struct cnic_dev *dev)
1044{
Michael Chan5e9b2db2009-08-26 09:49:23 +00001045 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00001046 struct cnic_uio_dev *udev = cp->udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001047 struct uio_info *uinfo;
Michael Chancd801532010-10-13 14:06:49 +00001048 int ret = 0;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001049
Michael Chancd801532010-10-13 14:06:49 +00001050 if (!udev)
Michael Chan5e9b2db2009-08-26 09:49:23 +00001051 return -ENOMEM;
1052
Michael Chancd801532010-10-13 14:06:49 +00001053 uinfo = &udev->cnic_uinfo;
1054
Michael Chan5e9b2db2009-08-26 09:49:23 +00001055 uinfo->mem[0].addr = dev->netdev->base_addr;
1056 uinfo->mem[0].internal_addr = dev->regview;
1057 uinfo->mem[0].size = dev->netdev->mem_end - dev->netdev->mem_start;
1058 uinfo->mem[0].memtype = UIO_MEM_PHYS;
1059
Michael Chan5e9b2db2009-08-26 09:49:23 +00001060 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
Michael Chana4dde3a2010-02-24 14:42:08 +00001061 uinfo->mem[1].addr = (unsigned long) cp->status_blk.gen &
Michael Chancd801532010-10-13 14:06:49 +00001062 PAGE_MASK;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001063 if (cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
1064 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE * 9;
1065 else
1066 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE;
1067
1068 uinfo->name = "bnx2_cnic";
Michael Chan71034ba2009-10-10 13:46:59 +00001069 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
1070 uinfo->mem[1].addr = (unsigned long) cp->bnx2x_def_status_blk &
1071 PAGE_MASK;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001072 uinfo->mem[1].size = sizeof(*cp->bnx2x_def_status_blk);
Michael Chan71034ba2009-10-10 13:46:59 +00001073
1074 uinfo->name = "bnx2x_cnic";
Michael Chan5e9b2db2009-08-26 09:49:23 +00001075 }
1076
1077 uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
1078
Michael Chancd801532010-10-13 14:06:49 +00001079 uinfo->mem[2].addr = (unsigned long) udev->l2_ring;
1080 uinfo->mem[2].size = udev->l2_ring_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001081 uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
1082
Michael Chancd801532010-10-13 14:06:49 +00001083 uinfo->mem[3].addr = (unsigned long) udev->l2_buf;
1084 uinfo->mem[3].size = udev->l2_buf_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001085 uinfo->mem[3].memtype = UIO_MEM_LOGICAL;
1086
1087 uinfo->version = CNIC_MODULE_VERSION;
1088 uinfo->irq = UIO_IRQ_CUSTOM;
1089
1090 uinfo->open = cnic_uio_open;
1091 uinfo->release = cnic_uio_close;
1092
Michael Chana3ceeeb2010-10-13 14:06:50 +00001093 if (udev->uio_dev == -1) {
1094 if (!uinfo->priv) {
1095 uinfo->priv = udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001096
Michael Chana3ceeeb2010-10-13 14:06:50 +00001097 ret = uio_register_device(&udev->pdev->dev, uinfo);
1098 }
1099 } else {
1100 cnic_init_rings(dev);
1101 }
Michael Chan5e9b2db2009-08-26 09:49:23 +00001102
Michael Chancd801532010-10-13 14:06:49 +00001103 return ret;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001104}
1105
Michael Chana4636962009-06-08 18:14:43 -07001106static int cnic_alloc_bnx2_resc(struct cnic_dev *dev)
1107{
1108 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07001109 int ret;
1110
1111 ret = cnic_alloc_dma(dev, &cp->kwq_info, KWQ_PAGE_CNT, 1);
1112 if (ret)
1113 goto error;
1114 cp->kwq = (struct kwqe **) cp->kwq_info.pg_arr;
1115
Michael Chan59e51372011-06-14 01:32:38 +00001116 ret = cnic_alloc_kcq(dev, &cp->kcq1, true);
Michael Chana4636962009-06-08 18:14:43 -07001117 if (ret)
1118 goto error;
Michael Chana4636962009-06-08 18:14:43 -07001119
1120 ret = cnic_alloc_context(dev);
1121 if (ret)
1122 goto error;
1123
Michael Chancd801532010-10-13 14:06:49 +00001124 ret = cnic_alloc_uio_rings(dev, 2);
Michael Chanec0248e2009-08-26 09:49:22 +00001125 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001126 goto error;
1127
Michael Chancd801532010-10-13 14:06:49 +00001128 ret = cnic_init_uio(dev);
Michael Chan5e9b2db2009-08-26 09:49:23 +00001129 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001130 goto error;
1131
Michael Chana4636962009-06-08 18:14:43 -07001132 return 0;
1133
1134error:
1135 cnic_free_resc(dev);
1136 return ret;
1137}
1138
Michael Chan71034ba2009-10-10 13:46:59 +00001139static int cnic_alloc_bnx2x_context(struct cnic_dev *dev)
1140{
1141 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001142 int ctx_blk_size = cp->ethdev->ctx_blk_size;
Michael Chan520efdf2010-06-24 14:58:37 +00001143 int total_mem, blks, i;
Michael Chan71034ba2009-10-10 13:46:59 +00001144
Michael Chan520efdf2010-06-24 14:58:37 +00001145 total_mem = BNX2X_CONTEXT_MEM_SIZE * cp->max_cid_space;
Michael Chan71034ba2009-10-10 13:46:59 +00001146 blks = total_mem / ctx_blk_size;
1147 if (total_mem % ctx_blk_size)
1148 blks++;
1149
1150 if (blks > cp->ethdev->ctx_tbl_len)
1151 return -ENOMEM;
1152
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001153 cp->ctx_arr = kcalloc(blks, sizeof(struct cnic_ctx), GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001154 if (cp->ctx_arr == NULL)
1155 return -ENOMEM;
1156
1157 cp->ctx_blks = blks;
1158 cp->ctx_blk_size = ctx_blk_size;
Michael Chanee87a822010-10-13 14:06:51 +00001159 if (!BNX2X_CHIP_IS_57710(cp->chip_id))
Michael Chan71034ba2009-10-10 13:46:59 +00001160 cp->ctx_align = 0;
1161 else
1162 cp->ctx_align = ctx_blk_size;
1163
1164 cp->cids_per_blk = ctx_blk_size / BNX2X_CONTEXT_MEM_SIZE;
1165
1166 for (i = 0; i < blks; i++) {
1167 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +00001168 dma_alloc_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
1169 &cp->ctx_arr[i].mapping,
1170 GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001171 if (cp->ctx_arr[i].ctx == NULL)
1172 return -ENOMEM;
1173
1174 if (cp->ctx_align && cp->ctx_blk_size == ctx_blk_size) {
1175 if (cp->ctx_arr[i].mapping & (cp->ctx_align - 1)) {
1176 cnic_free_context(dev);
1177 cp->ctx_blk_size += cp->ctx_align;
1178 i = -1;
1179 continue;
1180 }
1181 }
1182 }
1183 return 0;
1184}
1185
1186static int cnic_alloc_bnx2x_resc(struct cnic_dev *dev)
1187{
1188 struct cnic_local *cp = dev->cnic_priv;
Michael Chan520efdf2010-06-24 14:58:37 +00001189 struct cnic_eth_dev *ethdev = cp->ethdev;
1190 u32 start_cid = ethdev->starting_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001191 int i, j, n, ret, pages;
1192 struct cnic_dma *kwq_16_dma = &cp->kwq_16_data_info;
1193
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001194 cp->iro_arr = ethdev->iro_arr;
1195
Michael Chanb37a41e2011-07-20 14:55:22 +00001196 cp->max_cid_space = MAX_ISCSI_TBL_SZ;
Michael Chan520efdf2010-06-24 14:58:37 +00001197 cp->iscsi_start_cid = start_cid;
Michael Chane1928c82010-12-23 07:43:04 +00001198 cp->fcoe_start_cid = start_cid + MAX_ISCSI_TBL_SZ;
1199
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001200 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chandc219a22011-08-26 09:45:39 +00001201 cp->max_cid_space += dev->max_fcoe_conn;
Michael Chane1928c82010-12-23 07:43:04 +00001202 cp->fcoe_init_cid = ethdev->fcoe_init_cid;
1203 if (!cp->fcoe_init_cid)
1204 cp->fcoe_init_cid = 0x10;
1205 }
1206
Michael Chan71034ba2009-10-10 13:46:59 +00001207 cp->iscsi_tbl = kzalloc(sizeof(struct cnic_iscsi) * MAX_ISCSI_TBL_SZ,
1208 GFP_KERNEL);
1209 if (!cp->iscsi_tbl)
1210 goto error;
1211
1212 cp->ctx_tbl = kzalloc(sizeof(struct cnic_context) *
Michael Chan520efdf2010-06-24 14:58:37 +00001213 cp->max_cid_space, GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001214 if (!cp->ctx_tbl)
1215 goto error;
1216
1217 for (i = 0; i < MAX_ISCSI_TBL_SZ; i++) {
1218 cp->ctx_tbl[i].proto.iscsi = &cp->iscsi_tbl[i];
1219 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_ISCSI;
1220 }
1221
Michael Chane1928c82010-12-23 07:43:04 +00001222 for (i = MAX_ISCSI_TBL_SZ; i < cp->max_cid_space; i++)
1223 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_FCOE;
1224
Michael Chan520efdf2010-06-24 14:58:37 +00001225 pages = PAGE_ALIGN(cp->max_cid_space * CNIC_KWQ16_DATA_SIZE) /
Michael Chan71034ba2009-10-10 13:46:59 +00001226 PAGE_SIZE;
1227
1228 ret = cnic_alloc_dma(dev, kwq_16_dma, pages, 0);
1229 if (ret)
1230 return -ENOMEM;
1231
1232 n = PAGE_SIZE / CNIC_KWQ16_DATA_SIZE;
Michael Chan520efdf2010-06-24 14:58:37 +00001233 for (i = 0, j = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +00001234 long off = CNIC_KWQ16_DATA_SIZE * (i % n);
1235
1236 cp->ctx_tbl[i].kwqe_data = kwq_16_dma->pg_arr[j] + off;
1237 cp->ctx_tbl[i].kwqe_data_mapping = kwq_16_dma->pg_map_arr[j] +
1238 off;
1239
1240 if ((i % n) == (n - 1))
1241 j++;
1242 }
1243
Michael Chan59e51372011-06-14 01:32:38 +00001244 ret = cnic_alloc_kcq(dev, &cp->kcq1, false);
Michael Chan71034ba2009-10-10 13:46:59 +00001245 if (ret)
1246 goto error;
Michael Chan71034ba2009-10-10 13:46:59 +00001247
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001248 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
1249 ret = cnic_alloc_kcq(dev, &cp->kcq2, true);
Michael Chane21ba412010-12-23 07:43:03 +00001250 if (ret)
1251 goto error;
1252 }
1253
Michael Chan71034ba2009-10-10 13:46:59 +00001254 pages = PAGE_ALIGN(BNX2X_ISCSI_GLB_BUF_SIZE) / PAGE_SIZE;
1255 ret = cnic_alloc_dma(dev, &cp->gbl_buf_info, pages, 0);
1256 if (ret)
1257 goto error;
1258
1259 ret = cnic_alloc_bnx2x_context(dev);
1260 if (ret)
1261 goto error;
1262
Michael Chan71034ba2009-10-10 13:46:59 +00001263 cp->bnx2x_def_status_blk = cp->ethdev->irq_arr[1].status_blk;
1264
1265 cp->l2_rx_ring_size = 15;
1266
Michael Chancd801532010-10-13 14:06:49 +00001267 ret = cnic_alloc_uio_rings(dev, 4);
Michael Chan71034ba2009-10-10 13:46:59 +00001268 if (ret)
1269 goto error;
1270
Michael Chancd801532010-10-13 14:06:49 +00001271 ret = cnic_init_uio(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00001272 if (ret)
1273 goto error;
1274
1275 return 0;
1276
1277error:
1278 cnic_free_resc(dev);
1279 return -ENOMEM;
1280}
1281
Michael Chana4636962009-06-08 18:14:43 -07001282static inline u32 cnic_kwq_avail(struct cnic_local *cp)
1283{
1284 return cp->max_kwq_idx -
1285 ((cp->kwq_prod_idx - cp->kwq_con_idx) & cp->max_kwq_idx);
1286}
1287
1288static int cnic_submit_bnx2_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
1289 u32 num_wqes)
1290{
1291 struct cnic_local *cp = dev->cnic_priv;
1292 struct kwqe *prod_qe;
1293 u16 prod, sw_prod, i;
1294
1295 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
1296 return -EAGAIN; /* bnx2 is down */
1297
1298 spin_lock_bh(&cp->cnic_ulp_lock);
1299 if (num_wqes > cnic_kwq_avail(cp) &&
Michael Chan1f1332a2010-05-18 11:32:52 +00001300 !test_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags)) {
Michael Chana4636962009-06-08 18:14:43 -07001301 spin_unlock_bh(&cp->cnic_ulp_lock);
1302 return -EAGAIN;
1303 }
1304
Michael Chan1f1332a2010-05-18 11:32:52 +00001305 clear_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07001306
1307 prod = cp->kwq_prod_idx;
1308 sw_prod = prod & MAX_KWQ_IDX;
1309 for (i = 0; i < num_wqes; i++) {
1310 prod_qe = &cp->kwq[KWQ_PG(sw_prod)][KWQ_IDX(sw_prod)];
1311 memcpy(prod_qe, wqes[i], sizeof(struct kwqe));
1312 prod++;
1313 sw_prod = prod & MAX_KWQ_IDX;
1314 }
1315 cp->kwq_prod_idx = prod;
1316
1317 CNIC_WR16(dev, cp->kwq_io_addr, cp->kwq_prod_idx);
1318
1319 spin_unlock_bh(&cp->cnic_ulp_lock);
1320 return 0;
1321}
1322
Michael Chan71034ba2009-10-10 13:46:59 +00001323static void *cnic_get_kwqe_16_data(struct cnic_local *cp, u32 l5_cid,
1324 union l5cm_specific_data *l5_data)
1325{
1326 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1327 dma_addr_t map;
1328
1329 map = ctx->kwqe_data_mapping;
1330 l5_data->phy_address.lo = (u64) map & 0xffffffff;
1331 l5_data->phy_address.hi = (u64) map >> 32;
1332 return ctx->kwqe_data;
1333}
1334
1335static int cnic_submit_kwqe_16(struct cnic_dev *dev, u32 cmd, u32 cid,
1336 u32 type, union l5cm_specific_data *l5_data)
1337{
1338 struct cnic_local *cp = dev->cnic_priv;
1339 struct l5cm_spe kwqe;
1340 struct kwqe_16 *kwq[1];
Michael Chan68d7c1a2011-01-05 15:14:13 +00001341 u16 type_16;
Michael Chan71034ba2009-10-10 13:46:59 +00001342 int ret;
1343
1344 kwqe.hdr.conn_and_cmd_data =
1345 cpu_to_le32(((cmd << SPE_HDR_CMD_ID_SHIFT) |
Michael Chanceb7e1c2010-10-06 03:14:54 +00001346 BNX2X_HW_CID(cp, cid)));
Michael Chan68d7c1a2011-01-05 15:14:13 +00001347
1348 type_16 = (type << SPE_HDR_CONN_TYPE_SHIFT) & SPE_HDR_CONN_TYPE;
1349 type_16 |= (cp->pfid << SPE_HDR_FUNCTION_ID_SHIFT) &
1350 SPE_HDR_FUNCTION_ID;
1351
1352 kwqe.hdr.type = cpu_to_le16(type_16);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001353 kwqe.hdr.reserved1 = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001354 kwqe.data.phy_address.lo = cpu_to_le32(l5_data->phy_address.lo);
1355 kwqe.data.phy_address.hi = cpu_to_le32(l5_data->phy_address.hi);
1356
1357 kwq[0] = (struct kwqe_16 *) &kwqe;
1358
1359 spin_lock_bh(&cp->cnic_ulp_lock);
1360 ret = cp->ethdev->drv_submit_kwqes_16(dev->netdev, kwq, 1);
1361 spin_unlock_bh(&cp->cnic_ulp_lock);
1362
1363 if (ret == 1)
1364 return 0;
1365
Michael Chan23021c22012-01-04 12:12:28 +00001366 return ret;
Michael Chan71034ba2009-10-10 13:46:59 +00001367}
1368
1369static void cnic_reply_bnx2x_kcqes(struct cnic_dev *dev, int ulp_type,
1370 struct kcqe *cqes[], u32 num_cqes)
1371{
1372 struct cnic_local *cp = dev->cnic_priv;
1373 struct cnic_ulp_ops *ulp_ops;
1374
1375 rcu_read_lock();
1376 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
1377 if (likely(ulp_ops)) {
1378 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
1379 cqes, num_cqes);
1380 }
1381 rcu_read_unlock();
1382}
1383
1384static int cnic_bnx2x_iscsi_init1(struct cnic_dev *dev, struct kwqe *kwqe)
1385{
1386 struct cnic_local *cp = dev->cnic_priv;
1387 struct iscsi_kwqe_init1 *req1 = (struct iscsi_kwqe_init1 *) kwqe;
Michael Chan14203982010-10-06 03:16:06 +00001388 int hq_bds, pages;
1389 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001390
1391 cp->num_iscsi_tasks = req1->num_tasks_per_conn;
1392 cp->num_ccells = req1->num_ccells_per_conn;
1393 cp->task_array_size = BNX2X_ISCSI_TASK_CONTEXT_SIZE *
1394 cp->num_iscsi_tasks;
1395 cp->r2tq_size = cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS *
1396 BNX2X_ISCSI_R2TQE_SIZE;
1397 cp->hq_size = cp->num_ccells * BNX2X_ISCSI_HQ_BD_SIZE;
1398 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1399 hq_bds = pages * (PAGE_SIZE / BNX2X_ISCSI_HQ_BD_SIZE);
1400 cp->num_cqs = req1->num_cqs;
1401
1402 if (!dev->max_iscsi_conn)
1403 return 0;
1404
1405 /* init Tstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001406 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001407 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001408 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001409 PAGE_SIZE);
1410 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001411 TSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001412 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001413 TSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001414 req1->num_tasks_per_conn);
1415
1416 /* init Ustorm RAM */
1417 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001418 USTORM_ISCSI_RQ_BUFFER_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001419 req1->rq_buffer_size);
Michael Chan14203982010-10-06 03:16:06 +00001420 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001421 PAGE_SIZE);
1422 CNIC_WR8(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001423 USTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001424 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001425 USTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001426 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001427 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001428 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001429 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001430 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001431 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001432 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1433
1434 /* init Xstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001435 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001436 PAGE_SIZE);
1437 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001438 XSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001439 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001440 XSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001441 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001442 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001443 hq_bds);
Michael Chan14203982010-10-06 03:16:06 +00001444 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_SQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001445 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001446 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001447 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1448
1449 /* init Cstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001450 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001451 PAGE_SIZE);
1452 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001453 CSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001454 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001455 CSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001456 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001457 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001458 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001459 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001460 hq_bds);
1461
1462 return 0;
1463}
1464
1465static int cnic_bnx2x_iscsi_init2(struct cnic_dev *dev, struct kwqe *kwqe)
1466{
1467 struct iscsi_kwqe_init2 *req2 = (struct iscsi_kwqe_init2 *) kwqe;
1468 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00001469 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001470 struct iscsi_kcqe kcqe;
1471 struct kcqe *cqes[1];
1472
1473 memset(&kcqe, 0, sizeof(kcqe));
1474 if (!dev->max_iscsi_conn) {
1475 kcqe.completion_status =
1476 ISCSI_KCQE_COMPLETION_STATUS_ISCSI_NOT_SUPPORTED;
1477 goto done;
1478 }
1479
1480 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001481 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001482 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001483 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001484 req2->error_bit_map[1]);
1485
1486 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001487 USTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001488 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001489 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001490 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001491 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001492 req2->error_bit_map[1]);
1493
1494 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001495 CSTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001496
1497 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1498
1499done:
1500 kcqe.op_code = ISCSI_KCQE_OPCODE_INIT;
1501 cqes[0] = (struct kcqe *) &kcqe;
1502 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1503
1504 return 0;
1505}
1506
1507static void cnic_free_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1508{
1509 struct cnic_local *cp = dev->cnic_priv;
1510 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1511
1512 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI) {
1513 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1514
1515 cnic_free_dma(dev, &iscsi->hq_info);
1516 cnic_free_dma(dev, &iscsi->r2tq_info);
1517 cnic_free_dma(dev, &iscsi->task_array_info);
Michael Chane1928c82010-12-23 07:43:04 +00001518 cnic_free_id(&cp->cid_tbl, ctx->cid);
1519 } else {
1520 cnic_free_id(&cp->fcoe_cid_tbl, ctx->cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001521 }
Michael Chane1928c82010-12-23 07:43:04 +00001522
Michael Chan71034ba2009-10-10 13:46:59 +00001523 ctx->cid = 0;
1524}
1525
1526static int cnic_alloc_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1527{
1528 u32 cid;
1529 int ret, pages;
1530 struct cnic_local *cp = dev->cnic_priv;
1531 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1532 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1533
Michael Chane1928c82010-12-23 07:43:04 +00001534 if (ctx->ulp_proto_id == CNIC_ULP_FCOE) {
1535 cid = cnic_alloc_new_id(&cp->fcoe_cid_tbl);
1536 if (cid == -1) {
1537 ret = -ENOMEM;
1538 goto error;
1539 }
1540 ctx->cid = cid;
1541 return 0;
1542 }
1543
Michael Chan71034ba2009-10-10 13:46:59 +00001544 cid = cnic_alloc_new_id(&cp->cid_tbl);
1545 if (cid == -1) {
1546 ret = -ENOMEM;
1547 goto error;
1548 }
1549
1550 ctx->cid = cid;
1551 pages = PAGE_ALIGN(cp->task_array_size) / PAGE_SIZE;
1552
1553 ret = cnic_alloc_dma(dev, &iscsi->task_array_info, pages, 1);
1554 if (ret)
1555 goto error;
1556
1557 pages = PAGE_ALIGN(cp->r2tq_size) / PAGE_SIZE;
1558 ret = cnic_alloc_dma(dev, &iscsi->r2tq_info, pages, 1);
1559 if (ret)
1560 goto error;
1561
1562 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1563 ret = cnic_alloc_dma(dev, &iscsi->hq_info, pages, 1);
1564 if (ret)
1565 goto error;
1566
1567 return 0;
1568
1569error:
1570 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1571 return ret;
1572}
1573
1574static void *cnic_get_bnx2x_ctx(struct cnic_dev *dev, u32 cid, int init,
1575 struct regpair *ctx_addr)
1576{
1577 struct cnic_local *cp = dev->cnic_priv;
1578 struct cnic_eth_dev *ethdev = cp->ethdev;
1579 int blk = (cid - ethdev->starting_cid) / cp->cids_per_blk;
1580 int off = (cid - ethdev->starting_cid) % cp->cids_per_blk;
1581 unsigned long align_off = 0;
1582 dma_addr_t ctx_map;
1583 void *ctx;
1584
1585 if (cp->ctx_align) {
1586 unsigned long mask = cp->ctx_align - 1;
1587
1588 if (cp->ctx_arr[blk].mapping & mask)
1589 align_off = cp->ctx_align -
1590 (cp->ctx_arr[blk].mapping & mask);
1591 }
1592 ctx_map = cp->ctx_arr[blk].mapping + align_off +
1593 (off * BNX2X_CONTEXT_MEM_SIZE);
1594 ctx = cp->ctx_arr[blk].ctx + align_off +
1595 (off * BNX2X_CONTEXT_MEM_SIZE);
1596 if (init)
1597 memset(ctx, 0, BNX2X_CONTEXT_MEM_SIZE);
1598
1599 ctx_addr->lo = ctx_map & 0xffffffff;
1600 ctx_addr->hi = (u64) ctx_map >> 32;
1601 return ctx;
1602}
1603
1604static int cnic_setup_bnx2x_ctx(struct cnic_dev *dev, struct kwqe *wqes[],
1605 u32 num)
1606{
1607 struct cnic_local *cp = dev->cnic_priv;
1608 struct iscsi_kwqe_conn_offload1 *req1 =
1609 (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1610 struct iscsi_kwqe_conn_offload2 *req2 =
1611 (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1612 struct iscsi_kwqe_conn_offload3 *req3;
1613 struct cnic_context *ctx = &cp->ctx_tbl[req1->iscsi_conn_id];
1614 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1615 u32 cid = ctx->cid;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001616 u32 hw_cid = BNX2X_HW_CID(cp, cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001617 struct iscsi_context *ictx;
1618 struct regpair context_addr;
1619 int i, j, n = 2, n_max;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001620 u8 port = CNIC_PORT(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00001621
1622 ctx->ctx_flags = 0;
1623 if (!req2->num_additional_wqes)
1624 return -EINVAL;
1625
1626 n_max = req2->num_additional_wqes + 2;
1627
1628 ictx = cnic_get_bnx2x_ctx(dev, cid, 1, &context_addr);
1629 if (ictx == NULL)
1630 return -ENOMEM;
1631
1632 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1633
1634 ictx->xstorm_ag_context.hq_prod = 1;
1635
1636 ictx->xstorm_st_context.iscsi.first_burst_length =
1637 ISCSI_DEF_FIRST_BURST_LEN;
1638 ictx->xstorm_st_context.iscsi.max_send_pdu_length =
1639 ISCSI_DEF_MAX_RECV_SEG_LEN;
1640 ictx->xstorm_st_context.iscsi.sq_pbl_base.lo =
1641 req1->sq_page_table_addr_lo;
1642 ictx->xstorm_st_context.iscsi.sq_pbl_base.hi =
1643 req1->sq_page_table_addr_hi;
1644 ictx->xstorm_st_context.iscsi.sq_curr_pbe.lo = req2->sq_first_pte.hi;
1645 ictx->xstorm_st_context.iscsi.sq_curr_pbe.hi = req2->sq_first_pte.lo;
1646 ictx->xstorm_st_context.iscsi.hq_pbl_base.lo =
1647 iscsi->hq_info.pgtbl_map & 0xffffffff;
1648 ictx->xstorm_st_context.iscsi.hq_pbl_base.hi =
1649 (u64) iscsi->hq_info.pgtbl_map >> 32;
1650 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.lo =
1651 iscsi->hq_info.pgtbl[0];
1652 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.hi =
1653 iscsi->hq_info.pgtbl[1];
1654 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.lo =
1655 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1656 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.hi =
1657 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1658 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.lo =
1659 iscsi->r2tq_info.pgtbl[0];
1660 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.hi =
1661 iscsi->r2tq_info.pgtbl[1];
1662 ictx->xstorm_st_context.iscsi.task_pbl_base.lo =
1663 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1664 ictx->xstorm_st_context.iscsi.task_pbl_base.hi =
1665 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1666 ictx->xstorm_st_context.iscsi.task_pbl_cache_idx =
1667 BNX2X_ISCSI_PBL_NOT_CACHED;
1668 ictx->xstorm_st_context.iscsi.flags.flags |=
1669 XSTORM_ISCSI_CONTEXT_FLAGS_B_IMMEDIATE_DATA;
1670 ictx->xstorm_st_context.iscsi.flags.flags |=
1671 XSTORM_ISCSI_CONTEXT_FLAGS_B_INITIAL_R2T;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001672 ictx->xstorm_st_context.common.ethernet.reserved_vlan_type =
1673 ETH_P_8021Q;
1674 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id) &&
1675 cp->port_mode == CHIP_2_PORT_MODE) {
1676
1677 port = 0;
1678 }
1679 ictx->xstorm_st_context.common.flags =
1680 1 << XSTORM_COMMON_CONTEXT_SECTION_PHYSQ_INITIALIZED_SHIFT;
1681 ictx->xstorm_st_context.common.flags =
1682 port << XSTORM_COMMON_CONTEXT_SECTION_PBF_PORT_SHIFT;
Michael Chan71034ba2009-10-10 13:46:59 +00001683
1684 ictx->tstorm_st_context.iscsi.hdr_bytes_2_fetch = ISCSI_HEADER_SIZE;
1685 /* TSTORM requires the base address of RQ DB & not PTE */
1686 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.lo =
1687 req2->rq_page_table_addr_lo & PAGE_MASK;
1688 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.hi =
1689 req2->rq_page_table_addr_hi;
1690 ictx->tstorm_st_context.iscsi.iscsi_conn_id = req1->iscsi_conn_id;
1691 ictx->tstorm_st_context.tcp.cwnd = 0x5A8;
1692 ictx->tstorm_st_context.tcp.flags2 |=
1693 TSTORM_TCP_ST_CONTEXT_SECTION_DA_EN;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001694 ictx->tstorm_st_context.tcp.ooo_support_mode =
1695 TCP_TSTORM_OOO_DROP_AND_PROC_ACK;
Michael Chan71034ba2009-10-10 13:46:59 +00001696
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001697 ictx->timers_context.flags |= TIMERS_BLOCK_CONTEXT_CONN_VALID_FLG;
Michael Chan71034ba2009-10-10 13:46:59 +00001698
1699 ictx->ustorm_st_context.ring.rq.pbl_base.lo =
Michael Chan15971c32009-12-02 15:15:38 +00001700 req2->rq_page_table_addr_lo;
Michael Chan71034ba2009-10-10 13:46:59 +00001701 ictx->ustorm_st_context.ring.rq.pbl_base.hi =
Michael Chan15971c32009-12-02 15:15:38 +00001702 req2->rq_page_table_addr_hi;
Michael Chan71034ba2009-10-10 13:46:59 +00001703 ictx->ustorm_st_context.ring.rq.curr_pbe.lo = req3->qp_first_pte[0].hi;
1704 ictx->ustorm_st_context.ring.rq.curr_pbe.hi = req3->qp_first_pte[0].lo;
1705 ictx->ustorm_st_context.ring.r2tq.pbl_base.lo =
1706 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1707 ictx->ustorm_st_context.ring.r2tq.pbl_base.hi =
1708 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1709 ictx->ustorm_st_context.ring.r2tq.curr_pbe.lo =
1710 iscsi->r2tq_info.pgtbl[0];
1711 ictx->ustorm_st_context.ring.r2tq.curr_pbe.hi =
1712 iscsi->r2tq_info.pgtbl[1];
1713 ictx->ustorm_st_context.ring.cq_pbl_base.lo =
1714 req1->cq_page_table_addr_lo;
1715 ictx->ustorm_st_context.ring.cq_pbl_base.hi =
1716 req1->cq_page_table_addr_hi;
1717 ictx->ustorm_st_context.ring.cq[0].cq_sn = ISCSI_INITIAL_SN;
1718 ictx->ustorm_st_context.ring.cq[0].curr_pbe.lo = req2->cq_first_pte.hi;
1719 ictx->ustorm_st_context.ring.cq[0].curr_pbe.hi = req2->cq_first_pte.lo;
1720 ictx->ustorm_st_context.task_pbe_cache_index =
1721 BNX2X_ISCSI_PBL_NOT_CACHED;
1722 ictx->ustorm_st_context.task_pdu_cache_index =
1723 BNX2X_ISCSI_PDU_HEADER_NOT_CACHED;
1724
1725 for (i = 1, j = 1; i < cp->num_cqs; i++, j++) {
1726 if (j == 3) {
1727 if (n >= n_max)
1728 break;
1729 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1730 j = 0;
1731 }
1732 ictx->ustorm_st_context.ring.cq[i].cq_sn = ISCSI_INITIAL_SN;
1733 ictx->ustorm_st_context.ring.cq[i].curr_pbe.lo =
1734 req3->qp_first_pte[j].hi;
1735 ictx->ustorm_st_context.ring.cq[i].curr_pbe.hi =
1736 req3->qp_first_pte[j].lo;
1737 }
1738
1739 ictx->ustorm_st_context.task_pbl_base.lo =
1740 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1741 ictx->ustorm_st_context.task_pbl_base.hi =
1742 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1743 ictx->ustorm_st_context.tce_phy_addr.lo =
1744 iscsi->task_array_info.pgtbl[0];
1745 ictx->ustorm_st_context.tce_phy_addr.hi =
1746 iscsi->task_array_info.pgtbl[1];
1747 ictx->ustorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1748 ictx->ustorm_st_context.num_cqs = cp->num_cqs;
1749 ictx->ustorm_st_context.negotiated_rx |= ISCSI_DEF_MAX_RECV_SEG_LEN;
1750 ictx->ustorm_st_context.negotiated_rx_and_flags |=
1751 ISCSI_DEF_MAX_BURST_LEN;
1752 ictx->ustorm_st_context.negotiated_rx |=
1753 ISCSI_DEFAULT_MAX_OUTSTANDING_R2T <<
1754 USTORM_ISCSI_ST_CONTEXT_MAX_OUTSTANDING_R2TS_SHIFT;
1755
1756 ictx->cstorm_st_context.hq_pbl_base.lo =
1757 iscsi->hq_info.pgtbl_map & 0xffffffff;
1758 ictx->cstorm_st_context.hq_pbl_base.hi =
1759 (u64) iscsi->hq_info.pgtbl_map >> 32;
1760 ictx->cstorm_st_context.hq_curr_pbe.lo = iscsi->hq_info.pgtbl[0];
1761 ictx->cstorm_st_context.hq_curr_pbe.hi = iscsi->hq_info.pgtbl[1];
1762 ictx->cstorm_st_context.task_pbl_base.lo =
1763 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1764 ictx->cstorm_st_context.task_pbl_base.hi =
1765 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1766 /* CSTORM and USTORM initialization is different, CSTORM requires
1767 * CQ DB base & not PTE addr */
1768 ictx->cstorm_st_context.cq_db_base.lo =
1769 req1->cq_page_table_addr_lo & PAGE_MASK;
1770 ictx->cstorm_st_context.cq_db_base.hi = req1->cq_page_table_addr_hi;
1771 ictx->cstorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1772 ictx->cstorm_st_context.cq_proc_en_bit_map = (1 << cp->num_cqs) - 1;
1773 for (i = 0; i < cp->num_cqs; i++) {
1774 ictx->cstorm_st_context.cq_c_prod_sqn_arr.sqn[i] =
1775 ISCSI_INITIAL_SN;
1776 ictx->cstorm_st_context.cq_c_sqn_2_notify_arr.sqn[i] =
1777 ISCSI_INITIAL_SN;
1778 }
1779
1780 ictx->xstorm_ag_context.cdu_reserved =
1781 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
1782 ISCSI_CONNECTION_TYPE);
1783 ictx->ustorm_ag_context.cdu_usage =
1784 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
1785 ISCSI_CONNECTION_TYPE);
1786 return 0;
1787
1788}
1789
1790static int cnic_bnx2x_iscsi_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
1791 u32 num, int *work)
1792{
1793 struct iscsi_kwqe_conn_offload1 *req1;
1794 struct iscsi_kwqe_conn_offload2 *req2;
1795 struct cnic_local *cp = dev->cnic_priv;
Michael Chanfdf24082010-10-13 14:06:47 +00001796 struct cnic_context *ctx;
Michael Chan71034ba2009-10-10 13:46:59 +00001797 struct iscsi_kcqe kcqe;
1798 struct kcqe *cqes[1];
1799 u32 l5_cid;
Michael Chanfdf24082010-10-13 14:06:47 +00001800 int ret = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001801
1802 if (num < 2) {
1803 *work = num;
1804 return -EINVAL;
1805 }
1806
1807 req1 = (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1808 req2 = (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1809 if ((num - 2) < req2->num_additional_wqes) {
1810 *work = num;
1811 return -EINVAL;
1812 }
Joe Perches779bb412010-11-14 17:04:37 +00001813 *work = 2 + req2->num_additional_wqes;
Michael Chan71034ba2009-10-10 13:46:59 +00001814
1815 l5_cid = req1->iscsi_conn_id;
1816 if (l5_cid >= MAX_ISCSI_TBL_SZ)
1817 return -EINVAL;
1818
1819 memset(&kcqe, 0, sizeof(kcqe));
1820 kcqe.op_code = ISCSI_KCQE_OPCODE_OFFLOAD_CONN;
1821 kcqe.iscsi_conn_id = l5_cid;
1822 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
1823
Michael Chanfdf24082010-10-13 14:06:47 +00001824 ctx = &cp->ctx_tbl[l5_cid];
1825 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags)) {
1826 kcqe.completion_status =
1827 ISCSI_KCQE_COMPLETION_STATUS_CID_BUSY;
1828 goto done;
1829 }
1830
Michael Chan71034ba2009-10-10 13:46:59 +00001831 if (atomic_inc_return(&cp->iscsi_conn) > dev->max_iscsi_conn) {
1832 atomic_dec(&cp->iscsi_conn);
Michael Chan71034ba2009-10-10 13:46:59 +00001833 goto done;
1834 }
1835 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
1836 if (ret) {
1837 atomic_dec(&cp->iscsi_conn);
1838 ret = 0;
1839 goto done;
1840 }
1841 ret = cnic_setup_bnx2x_ctx(dev, wqes, num);
1842 if (ret < 0) {
1843 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1844 atomic_dec(&cp->iscsi_conn);
1845 goto done;
1846 }
1847
1848 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001849 kcqe.iscsi_conn_context_id = BNX2X_HW_CID(cp, cp->ctx_tbl[l5_cid].cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001850
1851done:
1852 cqes[0] = (struct kcqe *) &kcqe;
1853 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
Michael Chan23021c22012-01-04 12:12:28 +00001854 return 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001855}
1856
1857
1858static int cnic_bnx2x_iscsi_update(struct cnic_dev *dev, struct kwqe *kwqe)
1859{
1860 struct cnic_local *cp = dev->cnic_priv;
1861 struct iscsi_kwqe_conn_update *req =
1862 (struct iscsi_kwqe_conn_update *) kwqe;
1863 void *data;
1864 union l5cm_specific_data l5_data;
1865 u32 l5_cid, cid = BNX2X_SW_CID(req->context_id);
1866 int ret;
1867
1868 if (cnic_get_l5_cid(cp, cid, &l5_cid) != 0)
1869 return -EINVAL;
1870
1871 data = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
1872 if (!data)
1873 return -ENOMEM;
1874
1875 memcpy(data, kwqe, sizeof(struct kwqe));
1876
1877 ret = cnic_submit_kwqe_16(dev, ISCSI_RAMROD_CMD_ID_UPDATE_CONN,
1878 req->context_id, ISCSI_CONNECTION_TYPE, &l5_data);
1879 return ret;
1880}
1881
Michael Chana2c9e762010-10-13 14:06:46 +00001882static int cnic_bnx2x_destroy_ramrod(struct cnic_dev *dev, u32 l5_cid)
Michael Chan71034ba2009-10-10 13:46:59 +00001883{
1884 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001885 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
Michael Chana2c9e762010-10-13 14:06:46 +00001886 union l5cm_specific_data l5_data;
1887 int ret;
Michael Chan68d7c1a2011-01-05 15:14:13 +00001888 u32 hw_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001889
Michael Chan71034ba2009-10-10 13:46:59 +00001890 init_waitqueue_head(&ctx->waitq);
1891 ctx->wait_cond = 0;
1892 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001893 hw_cid = BNX2X_HW_CID(cp, ctx->cid);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001894
1895 ret = cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
Michael Chan68d7c1a2011-01-05 15:14:13 +00001896 hw_cid, NONE_CONNECTION_TYPE, &l5_data);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001897
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001898 if (ret == 0) {
Michael Chandcc7e3a2011-08-26 09:45:40 +00001899 wait_event_timeout(ctx->waitq, ctx->wait_cond, CNIC_RAMROD_TMO);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001900 if (unlikely(test_bit(CTX_FL_CID_ERROR, &ctx->ctx_flags)))
1901 return -EBUSY;
1902 }
Michael Chan71034ba2009-10-10 13:46:59 +00001903
Michael Chandcc7e3a2011-08-26 09:45:40 +00001904 return 0;
Michael Chana2c9e762010-10-13 14:06:46 +00001905}
1906
1907static int cnic_bnx2x_iscsi_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
1908{
1909 struct cnic_local *cp = dev->cnic_priv;
1910 struct iscsi_kwqe_conn_destroy *req =
1911 (struct iscsi_kwqe_conn_destroy *) kwqe;
1912 u32 l5_cid = req->reserved0;
1913 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1914 int ret = 0;
1915 struct iscsi_kcqe kcqe;
1916 struct kcqe *cqes[1];
1917
1918 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
1919 goto skip_cfc_delete;
1920
Michael Chanfdf24082010-10-13 14:06:47 +00001921 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
1922 unsigned long delta = ctx->timestamp + (2 * HZ) - jiffies;
1923
1924 if (delta > (2 * HZ))
1925 delta = 0;
1926
1927 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
1928 queue_delayed_work(cnic_wq, &cp->delete_task, delta);
1929 goto destroy_reply;
1930 }
Michael Chana2c9e762010-10-13 14:06:46 +00001931
1932 ret = cnic_bnx2x_destroy_ramrod(dev, l5_cid);
1933
Michael Chan71034ba2009-10-10 13:46:59 +00001934skip_cfc_delete:
1935 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1936
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001937 if (!ret) {
1938 atomic_dec(&cp->iscsi_conn);
1939 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
1940 }
Michael Chan71034ba2009-10-10 13:46:59 +00001941
Michael Chanfdf24082010-10-13 14:06:47 +00001942destroy_reply:
Michael Chan71034ba2009-10-10 13:46:59 +00001943 memset(&kcqe, 0, sizeof(kcqe));
1944 kcqe.op_code = ISCSI_KCQE_OPCODE_DESTROY_CONN;
1945 kcqe.iscsi_conn_id = l5_cid;
1946 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1947 kcqe.iscsi_conn_context_id = req->context_id;
1948
1949 cqes[0] = (struct kcqe *) &kcqe;
1950 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1951
Michael Chan23021c22012-01-04 12:12:28 +00001952 return 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001953}
1954
1955static void cnic_init_storm_conn_bufs(struct cnic_dev *dev,
1956 struct l4_kwq_connect_req1 *kwqe1,
1957 struct l4_kwq_connect_req3 *kwqe3,
1958 struct l5cm_active_conn_buffer *conn_buf)
1959{
1960 struct l5cm_conn_addr_params *conn_addr = &conn_buf->conn_addr_buf;
1961 struct l5cm_xstorm_conn_buffer *xstorm_buf =
1962 &conn_buf->xstorm_conn_buffer;
1963 struct l5cm_tstorm_conn_buffer *tstorm_buf =
1964 &conn_buf->tstorm_conn_buffer;
1965 struct regpair context_addr;
1966 u32 cid = BNX2X_SW_CID(kwqe1->cid);
1967 struct in6_addr src_ip, dst_ip;
1968 int i;
1969 u32 *addrp;
1970
1971 addrp = (u32 *) &conn_addr->local_ip_addr;
1972 for (i = 0; i < 4; i++, addrp++)
1973 src_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1974
1975 addrp = (u32 *) &conn_addr->remote_ip_addr;
1976 for (i = 0; i < 4; i++, addrp++)
1977 dst_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1978
1979 cnic_get_bnx2x_ctx(dev, cid, 0, &context_addr);
1980
1981 xstorm_buf->context_addr.hi = context_addr.hi;
1982 xstorm_buf->context_addr.lo = context_addr.lo;
1983 xstorm_buf->mss = 0xffff;
1984 xstorm_buf->rcv_buf = kwqe3->rcv_buf;
1985 if (kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE)
1986 xstorm_buf->params |= L5CM_XSTORM_CONN_BUFFER_NAGLE_ENABLE;
1987 xstorm_buf->pseudo_header_checksum =
1988 swab16(~csum_ipv6_magic(&src_ip, &dst_ip, 0, IPPROTO_TCP, 0));
1989
1990 if (!(kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK))
1991 tstorm_buf->params |=
1992 L5CM_TSTORM_CONN_BUFFER_DELAYED_ACK_ENABLE;
1993 if (kwqe3->ka_timeout) {
1994 tstorm_buf->ka_enable = 1;
1995 tstorm_buf->ka_timeout = kwqe3->ka_timeout;
1996 tstorm_buf->ka_interval = kwqe3->ka_interval;
1997 tstorm_buf->ka_max_probe_count = kwqe3->ka_max_probe_count;
1998 }
Michael Chan71034ba2009-10-10 13:46:59 +00001999 tstorm_buf->max_rt_time = 0xffffffff;
2000}
2001
2002static void cnic_init_bnx2x_mac(struct cnic_dev *dev)
2003{
2004 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00002005 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00002006 u8 *mac = dev->mac_addr;
2007
2008 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002009 XSTORM_ISCSI_LOCAL_MAC_ADDR0_OFFSET(pfid), mac[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00002010 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002011 XSTORM_ISCSI_LOCAL_MAC_ADDR1_OFFSET(pfid), mac[1]);
Michael Chan71034ba2009-10-10 13:46:59 +00002012 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002013 XSTORM_ISCSI_LOCAL_MAC_ADDR2_OFFSET(pfid), mac[2]);
Michael Chan71034ba2009-10-10 13:46:59 +00002014 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002015 XSTORM_ISCSI_LOCAL_MAC_ADDR3_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00002016 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002017 XSTORM_ISCSI_LOCAL_MAC_ADDR4_OFFSET(pfid), mac[4]);
Michael Chan71034ba2009-10-10 13:46:59 +00002018 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002019 XSTORM_ISCSI_LOCAL_MAC_ADDR5_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00002020
2021 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002022 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00002023 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002024 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00002025 mac[4]);
2026 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002027 TSTORM_ISCSI_TCP_VARS_MID_LOCAL_MAC_ADDR_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00002028 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002029 TSTORM_ISCSI_TCP_VARS_MID_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00002030 mac[2]);
2031 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002032 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[1]);
Michael Chan71034ba2009-10-10 13:46:59 +00002033 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002034 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00002035 mac[0]);
2036}
2037
2038static void cnic_bnx2x_set_tcp_timestamp(struct cnic_dev *dev, int tcp_ts)
2039{
2040 struct cnic_local *cp = dev->cnic_priv;
2041 u8 xstorm_flags = XSTORM_L5CM_TCP_FLAGS_WND_SCL_EN;
2042 u16 tstorm_flags = 0;
2043
2044 if (tcp_ts) {
2045 xstorm_flags |= XSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
2046 tstorm_flags |= TSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
2047 }
2048
2049 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002050 XSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), xstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002051
2052 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002053 TSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), tstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002054}
2055
2056static int cnic_bnx2x_connect(struct cnic_dev *dev, struct kwqe *wqes[],
2057 u32 num, int *work)
2058{
2059 struct cnic_local *cp = dev->cnic_priv;
2060 struct l4_kwq_connect_req1 *kwqe1 =
2061 (struct l4_kwq_connect_req1 *) wqes[0];
2062 struct l4_kwq_connect_req3 *kwqe3;
2063 struct l5cm_active_conn_buffer *conn_buf;
2064 struct l5cm_conn_addr_params *conn_addr;
2065 union l5cm_specific_data l5_data;
2066 u32 l5_cid = kwqe1->pg_cid;
2067 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
2068 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
2069 int ret;
2070
2071 if (num < 2) {
2072 *work = num;
2073 return -EINVAL;
2074 }
2075
2076 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6)
2077 *work = 3;
2078 else
2079 *work = 2;
2080
2081 if (num < *work) {
2082 *work = num;
2083 return -EINVAL;
2084 }
2085
2086 if (sizeof(*conn_buf) > CNIC_KWQ16_DATA_SIZE) {
Joe Perchesddf79b22010-02-17 15:01:54 +00002087 netdev_err(dev->netdev, "conn_buf size too big\n");
Michael Chan71034ba2009-10-10 13:46:59 +00002088 return -ENOMEM;
2089 }
2090 conn_buf = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2091 if (!conn_buf)
2092 return -ENOMEM;
2093
2094 memset(conn_buf, 0, sizeof(*conn_buf));
2095
2096 conn_addr = &conn_buf->conn_addr_buf;
2097 conn_addr->remote_addr_0 = csk->ha[0];
2098 conn_addr->remote_addr_1 = csk->ha[1];
2099 conn_addr->remote_addr_2 = csk->ha[2];
2100 conn_addr->remote_addr_3 = csk->ha[3];
2101 conn_addr->remote_addr_4 = csk->ha[4];
2102 conn_addr->remote_addr_5 = csk->ha[5];
2103
2104 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6) {
2105 struct l4_kwq_connect_req2 *kwqe2 =
2106 (struct l4_kwq_connect_req2 *) wqes[1];
2107
2108 conn_addr->local_ip_addr.ip_addr_hi_hi = kwqe2->src_ip_v6_4;
2109 conn_addr->local_ip_addr.ip_addr_hi_lo = kwqe2->src_ip_v6_3;
2110 conn_addr->local_ip_addr.ip_addr_lo_hi = kwqe2->src_ip_v6_2;
2111
2112 conn_addr->remote_ip_addr.ip_addr_hi_hi = kwqe2->dst_ip_v6_4;
2113 conn_addr->remote_ip_addr.ip_addr_hi_lo = kwqe2->dst_ip_v6_3;
2114 conn_addr->remote_ip_addr.ip_addr_lo_hi = kwqe2->dst_ip_v6_2;
2115 conn_addr->params |= L5CM_CONN_ADDR_PARAMS_IP_VERSION;
2116 }
2117 kwqe3 = (struct l4_kwq_connect_req3 *) wqes[*work - 1];
2118
2119 conn_addr->local_ip_addr.ip_addr_lo_lo = kwqe1->src_ip;
2120 conn_addr->remote_ip_addr.ip_addr_lo_lo = kwqe1->dst_ip;
2121 conn_addr->local_tcp_port = kwqe1->src_port;
2122 conn_addr->remote_tcp_port = kwqe1->dst_port;
2123
2124 conn_addr->pmtu = kwqe3->pmtu;
2125 cnic_init_storm_conn_bufs(dev, kwqe1, kwqe3, conn_buf);
2126
2127 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002128 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(cp->pfid), csk->vlan_id);
Michael Chan71034ba2009-10-10 13:46:59 +00002129
2130 cnic_bnx2x_set_tcp_timestamp(dev,
2131 kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_TIME_STAMP);
2132
2133 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_TCP_CONNECT,
2134 kwqe1->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2135 if (!ret)
Michael Chan6e0dda02010-10-13 14:06:45 +00002136 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002137
2138 return ret;
2139}
2140
2141static int cnic_bnx2x_close(struct cnic_dev *dev, struct kwqe *kwqe)
2142{
2143 struct l4_kwq_close_req *req = (struct l4_kwq_close_req *) kwqe;
2144 union l5cm_specific_data l5_data;
2145 int ret;
2146
2147 memset(&l5_data, 0, sizeof(l5_data));
2148 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_CLOSE,
2149 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2150 return ret;
2151}
2152
2153static int cnic_bnx2x_reset(struct cnic_dev *dev, struct kwqe *kwqe)
2154{
2155 struct l4_kwq_reset_req *req = (struct l4_kwq_reset_req *) kwqe;
2156 union l5cm_specific_data l5_data;
2157 int ret;
2158
2159 memset(&l5_data, 0, sizeof(l5_data));
2160 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_ABORT,
2161 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2162 return ret;
2163}
2164static int cnic_bnx2x_offload_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2165{
2166 struct l4_kwq_offload_pg *req = (struct l4_kwq_offload_pg *) kwqe;
2167 struct l4_kcq kcqe;
2168 struct kcqe *cqes[1];
2169
2170 memset(&kcqe, 0, sizeof(kcqe));
2171 kcqe.pg_host_opaque = req->host_opaque;
2172 kcqe.pg_cid = req->host_opaque;
2173 kcqe.op_code = L4_KCQE_OPCODE_VALUE_OFFLOAD_PG;
2174 cqes[0] = (struct kcqe *) &kcqe;
2175 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2176 return 0;
2177}
2178
2179static int cnic_bnx2x_update_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2180{
2181 struct l4_kwq_update_pg *req = (struct l4_kwq_update_pg *) kwqe;
2182 struct l4_kcq kcqe;
2183 struct kcqe *cqes[1];
2184
2185 memset(&kcqe, 0, sizeof(kcqe));
2186 kcqe.pg_host_opaque = req->pg_host_opaque;
2187 kcqe.pg_cid = req->pg_cid;
2188 kcqe.op_code = L4_KCQE_OPCODE_VALUE_UPDATE_PG;
2189 cqes[0] = (struct kcqe *) &kcqe;
2190 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2191 return 0;
2192}
2193
Michael Chane1928c82010-12-23 07:43:04 +00002194static int cnic_bnx2x_fcoe_stat(struct cnic_dev *dev, struct kwqe *kwqe)
2195{
2196 struct fcoe_kwqe_stat *req;
2197 struct fcoe_stat_ramrod_params *fcoe_stat;
2198 union l5cm_specific_data l5_data;
2199 struct cnic_local *cp = dev->cnic_priv;
2200 int ret;
2201 u32 cid;
2202
2203 req = (struct fcoe_kwqe_stat *) kwqe;
2204 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2205
2206 fcoe_stat = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2207 if (!fcoe_stat)
2208 return -ENOMEM;
2209
2210 memset(fcoe_stat, 0, sizeof(*fcoe_stat));
2211 memcpy(&fcoe_stat->stat_kwqe, req, sizeof(*req));
2212
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002213 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_STAT_FUNC, cid,
Michael Chane1928c82010-12-23 07:43:04 +00002214 FCOE_CONNECTION_TYPE, &l5_data);
2215 return ret;
2216}
2217
2218static int cnic_bnx2x_fcoe_init1(struct cnic_dev *dev, struct kwqe *wqes[],
2219 u32 num, int *work)
2220{
2221 int ret;
2222 struct cnic_local *cp = dev->cnic_priv;
2223 u32 cid;
2224 struct fcoe_init_ramrod_params *fcoe_init;
2225 struct fcoe_kwqe_init1 *req1;
2226 struct fcoe_kwqe_init2 *req2;
2227 struct fcoe_kwqe_init3 *req3;
2228 union l5cm_specific_data l5_data;
2229
2230 if (num < 3) {
2231 *work = num;
2232 return -EINVAL;
2233 }
2234 req1 = (struct fcoe_kwqe_init1 *) wqes[0];
2235 req2 = (struct fcoe_kwqe_init2 *) wqes[1];
2236 req3 = (struct fcoe_kwqe_init3 *) wqes[2];
2237 if (req2->hdr.op_code != FCOE_KWQE_OPCODE_INIT2) {
2238 *work = 1;
2239 return -EINVAL;
2240 }
2241 if (req3->hdr.op_code != FCOE_KWQE_OPCODE_INIT3) {
2242 *work = 2;
2243 return -EINVAL;
2244 }
2245
2246 if (sizeof(*fcoe_init) > CNIC_KWQ16_DATA_SIZE) {
2247 netdev_err(dev->netdev, "fcoe_init size too big\n");
2248 return -ENOMEM;
2249 }
2250 fcoe_init = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2251 if (!fcoe_init)
2252 return -ENOMEM;
2253
2254 memset(fcoe_init, 0, sizeof(*fcoe_init));
2255 memcpy(&fcoe_init->init_kwqe1, req1, sizeof(*req1));
2256 memcpy(&fcoe_init->init_kwqe2, req2, sizeof(*req2));
2257 memcpy(&fcoe_init->init_kwqe3, req3, sizeof(*req3));
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002258 fcoe_init->eq_pbl_base.lo = cp->kcq2.dma.pgtbl_map & 0xffffffff;
2259 fcoe_init->eq_pbl_base.hi = (u64) cp->kcq2.dma.pgtbl_map >> 32;
2260 fcoe_init->eq_pbl_size = cp->kcq2.dma.num_pages;
Michael Chane1928c82010-12-23 07:43:04 +00002261
2262 fcoe_init->sb_num = cp->status_blk_num;
2263 fcoe_init->eq_prod = MAX_KCQ_IDX;
2264 fcoe_init->sb_id = HC_INDEX_FCOE_EQ_CONS;
2265 cp->kcq2.sw_prod_idx = 0;
2266
2267 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002268 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_INIT_FUNC, cid,
Michael Chane1928c82010-12-23 07:43:04 +00002269 FCOE_CONNECTION_TYPE, &l5_data);
2270 *work = 3;
2271 return ret;
2272}
2273
2274static int cnic_bnx2x_fcoe_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
2275 u32 num, int *work)
2276{
2277 int ret = 0;
2278 u32 cid = -1, l5_cid;
2279 struct cnic_local *cp = dev->cnic_priv;
2280 struct fcoe_kwqe_conn_offload1 *req1;
2281 struct fcoe_kwqe_conn_offload2 *req2;
2282 struct fcoe_kwqe_conn_offload3 *req3;
2283 struct fcoe_kwqe_conn_offload4 *req4;
2284 struct fcoe_conn_offload_ramrod_params *fcoe_offload;
2285 struct cnic_context *ctx;
2286 struct fcoe_context *fctx;
2287 struct regpair ctx_addr;
2288 union l5cm_specific_data l5_data;
2289 struct fcoe_kcqe kcqe;
2290 struct kcqe *cqes[1];
2291
2292 if (num < 4) {
2293 *work = num;
2294 return -EINVAL;
2295 }
2296 req1 = (struct fcoe_kwqe_conn_offload1 *) wqes[0];
2297 req2 = (struct fcoe_kwqe_conn_offload2 *) wqes[1];
2298 req3 = (struct fcoe_kwqe_conn_offload3 *) wqes[2];
2299 req4 = (struct fcoe_kwqe_conn_offload4 *) wqes[3];
2300
2301 *work = 4;
2302
2303 l5_cid = req1->fcoe_conn_id;
Michael Chandc219a22011-08-26 09:45:39 +00002304 if (l5_cid >= dev->max_fcoe_conn)
Michael Chane1928c82010-12-23 07:43:04 +00002305 goto err_reply;
2306
2307 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2308
2309 ctx = &cp->ctx_tbl[l5_cid];
2310 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2311 goto err_reply;
2312
2313 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
2314 if (ret) {
2315 ret = 0;
2316 goto err_reply;
2317 }
2318 cid = ctx->cid;
2319
2320 fctx = cnic_get_bnx2x_ctx(dev, cid, 1, &ctx_addr);
2321 if (fctx) {
2322 u32 hw_cid = BNX2X_HW_CID(cp, cid);
2323 u32 val;
2324
2325 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
2326 FCOE_CONNECTION_TYPE);
2327 fctx->xstorm_ag_context.cdu_reserved = val;
2328 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
2329 FCOE_CONNECTION_TYPE);
2330 fctx->ustorm_ag_context.cdu_usage = val;
2331 }
2332 if (sizeof(*fcoe_offload) > CNIC_KWQ16_DATA_SIZE) {
2333 netdev_err(dev->netdev, "fcoe_offload size too big\n");
2334 goto err_reply;
2335 }
2336 fcoe_offload = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2337 if (!fcoe_offload)
2338 goto err_reply;
2339
2340 memset(fcoe_offload, 0, sizeof(*fcoe_offload));
2341 memcpy(&fcoe_offload->offload_kwqe1, req1, sizeof(*req1));
2342 memcpy(&fcoe_offload->offload_kwqe2, req2, sizeof(*req2));
2343 memcpy(&fcoe_offload->offload_kwqe3, req3, sizeof(*req3));
2344 memcpy(&fcoe_offload->offload_kwqe4, req4, sizeof(*req4));
2345
2346 cid = BNX2X_HW_CID(cp, cid);
2347 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_OFFLOAD_CONN, cid,
2348 FCOE_CONNECTION_TYPE, &l5_data);
2349 if (!ret)
2350 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
2351
2352 return ret;
2353
2354err_reply:
2355 if (cid != -1)
2356 cnic_free_bnx2x_conn_resc(dev, l5_cid);
2357
2358 memset(&kcqe, 0, sizeof(kcqe));
2359 kcqe.op_code = FCOE_KCQE_OPCODE_OFFLOAD_CONN;
2360 kcqe.fcoe_conn_id = req1->fcoe_conn_id;
2361 kcqe.completion_status = FCOE_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
2362
2363 cqes[0] = (struct kcqe *) &kcqe;
2364 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2365 return ret;
2366}
2367
2368static int cnic_bnx2x_fcoe_enable(struct cnic_dev *dev, struct kwqe *kwqe)
2369{
2370 struct fcoe_kwqe_conn_enable_disable *req;
2371 struct fcoe_conn_enable_disable_ramrod_params *fcoe_enable;
2372 union l5cm_specific_data l5_data;
2373 int ret;
2374 u32 cid, l5_cid;
2375 struct cnic_local *cp = dev->cnic_priv;
2376
2377 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2378 cid = req->context_id;
2379 l5_cid = req->conn_id + BNX2X_FCOE_L5_CID_BASE;
2380
2381 if (sizeof(*fcoe_enable) > CNIC_KWQ16_DATA_SIZE) {
2382 netdev_err(dev->netdev, "fcoe_enable size too big\n");
2383 return -ENOMEM;
2384 }
2385 fcoe_enable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2386 if (!fcoe_enable)
2387 return -ENOMEM;
2388
2389 memset(fcoe_enable, 0, sizeof(*fcoe_enable));
2390 memcpy(&fcoe_enable->enable_disable_kwqe, req, sizeof(*req));
2391 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_ENABLE_CONN, cid,
2392 FCOE_CONNECTION_TYPE, &l5_data);
2393 return ret;
2394}
2395
2396static int cnic_bnx2x_fcoe_disable(struct cnic_dev *dev, struct kwqe *kwqe)
2397{
2398 struct fcoe_kwqe_conn_enable_disable *req;
2399 struct fcoe_conn_enable_disable_ramrod_params *fcoe_disable;
2400 union l5cm_specific_data l5_data;
2401 int ret;
2402 u32 cid, l5_cid;
2403 struct cnic_local *cp = dev->cnic_priv;
2404
2405 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2406 cid = req->context_id;
2407 l5_cid = req->conn_id;
Michael Chandc219a22011-08-26 09:45:39 +00002408 if (l5_cid >= dev->max_fcoe_conn)
Michael Chane1928c82010-12-23 07:43:04 +00002409 return -EINVAL;
2410
2411 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2412
2413 if (sizeof(*fcoe_disable) > CNIC_KWQ16_DATA_SIZE) {
2414 netdev_err(dev->netdev, "fcoe_disable size too big\n");
2415 return -ENOMEM;
2416 }
2417 fcoe_disable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2418 if (!fcoe_disable)
2419 return -ENOMEM;
2420
2421 memset(fcoe_disable, 0, sizeof(*fcoe_disable));
2422 memcpy(&fcoe_disable->enable_disable_kwqe, req, sizeof(*req));
2423 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DISABLE_CONN, cid,
2424 FCOE_CONNECTION_TYPE, &l5_data);
2425 return ret;
2426}
2427
2428static int cnic_bnx2x_fcoe_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2429{
2430 struct fcoe_kwqe_conn_destroy *req;
2431 union l5cm_specific_data l5_data;
2432 int ret;
2433 u32 cid, l5_cid;
2434 struct cnic_local *cp = dev->cnic_priv;
2435 struct cnic_context *ctx;
2436 struct fcoe_kcqe kcqe;
2437 struct kcqe *cqes[1];
2438
2439 req = (struct fcoe_kwqe_conn_destroy *) kwqe;
2440 cid = req->context_id;
2441 l5_cid = req->conn_id;
Michael Chandc219a22011-08-26 09:45:39 +00002442 if (l5_cid >= dev->max_fcoe_conn)
Michael Chane1928c82010-12-23 07:43:04 +00002443 return -EINVAL;
2444
2445 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2446
2447 ctx = &cp->ctx_tbl[l5_cid];
2448
2449 init_waitqueue_head(&ctx->waitq);
2450 ctx->wait_cond = 0;
2451
Michael Chandcc7e3a2011-08-26 09:45:40 +00002452 memset(&kcqe, 0, sizeof(kcqe));
2453 kcqe.completion_status = FCOE_KCQE_COMPLETION_STATUS_ERROR;
Michael Chane1928c82010-12-23 07:43:04 +00002454 memset(&l5_data, 0, sizeof(l5_data));
2455 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_TERMINATE_CONN, cid,
2456 FCOE_CONNECTION_TYPE, &l5_data);
2457 if (ret == 0) {
Michael Chandcc7e3a2011-08-26 09:45:40 +00002458 wait_event_timeout(ctx->waitq, ctx->wait_cond, CNIC_RAMROD_TMO);
2459 if (ctx->wait_cond)
2460 kcqe.completion_status = 0;
Michael Chane1928c82010-12-23 07:43:04 +00002461 }
2462
Michael Chandcc7e3a2011-08-26 09:45:40 +00002463 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
2464 queue_delayed_work(cnic_wq, &cp->delete_task, msecs_to_jiffies(2000));
2465
Michael Chane1928c82010-12-23 07:43:04 +00002466 kcqe.op_code = FCOE_KCQE_OPCODE_DESTROY_CONN;
2467 kcqe.fcoe_conn_id = req->conn_id;
2468 kcqe.fcoe_conn_context_id = cid;
2469
2470 cqes[0] = (struct kcqe *) &kcqe;
2471 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2472 return ret;
2473}
2474
Michael Chan74e49bb2011-07-20 14:55:23 +00002475static void cnic_bnx2x_delete_wait(struct cnic_dev *dev, u32 start_cid)
2476{
2477 struct cnic_local *cp = dev->cnic_priv;
2478 u32 i;
2479
2480 for (i = start_cid; i < cp->max_cid_space; i++) {
2481 struct cnic_context *ctx = &cp->ctx_tbl[i];
2482 int j;
2483
2484 while (test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
2485 msleep(10);
2486
2487 for (j = 0; j < 5; j++) {
2488 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2489 break;
2490 msleep(20);
2491 }
2492
2493 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2494 netdev_warn(dev->netdev, "CID %x not deleted\n",
2495 ctx->cid);
2496 }
2497}
2498
Michael Chane1928c82010-12-23 07:43:04 +00002499static int cnic_bnx2x_fcoe_fw_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2500{
2501 struct fcoe_kwqe_destroy *req;
2502 union l5cm_specific_data l5_data;
2503 struct cnic_local *cp = dev->cnic_priv;
2504 int ret;
2505 u32 cid;
2506
Michael Chan74e49bb2011-07-20 14:55:23 +00002507 cnic_bnx2x_delete_wait(dev, MAX_ISCSI_TBL_SZ);
2508
Michael Chane1928c82010-12-23 07:43:04 +00002509 req = (struct fcoe_kwqe_destroy *) kwqe;
2510 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2511
2512 memset(&l5_data, 0, sizeof(l5_data));
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002513 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DESTROY_FUNC, cid,
Michael Chane1928c82010-12-23 07:43:04 +00002514 FCOE_CONNECTION_TYPE, &l5_data);
2515 return ret;
2516}
2517
Michael Chan23021c22012-01-04 12:12:28 +00002518static void cnic_bnx2x_kwqe_err(struct cnic_dev *dev, struct kwqe *kwqe)
2519{
2520 struct cnic_local *cp = dev->cnic_priv;
2521 struct kcqe kcqe;
2522 struct kcqe *cqes[1];
2523 u32 cid;
2524 u32 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2525 u32 layer_code = kwqe->kwqe_op_flag & KWQE_LAYER_MASK;
Michael Chan3238a9b2012-02-05 15:24:40 +00002526 u32 kcqe_op;
Michael Chan23021c22012-01-04 12:12:28 +00002527 int ulp_type;
2528
2529 cid = kwqe->kwqe_info0;
2530 memset(&kcqe, 0, sizeof(kcqe));
2531
Michael Chan3238a9b2012-02-05 15:24:40 +00002532 if (layer_code == KWQE_FLAGS_LAYER_MASK_L5_FCOE) {
2533 u32 l5_cid = 0;
2534
2535 ulp_type = CNIC_ULP_FCOE;
2536 if (opcode == FCOE_KWQE_OPCODE_DISABLE_CONN) {
2537 struct fcoe_kwqe_conn_enable_disable *req;
2538
2539 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2540 kcqe_op = FCOE_KCQE_OPCODE_DISABLE_CONN;
2541 cid = req->context_id;
2542 l5_cid = req->conn_id;
2543 } else if (opcode == FCOE_KWQE_OPCODE_DESTROY) {
2544 kcqe_op = FCOE_KCQE_OPCODE_DESTROY_FUNC;
2545 } else {
2546 return;
2547 }
2548 kcqe.kcqe_op_flag = kcqe_op << KCQE_FLAGS_OPCODE_SHIFT;
2549 kcqe.kcqe_op_flag |= KCQE_FLAGS_LAYER_MASK_L5_FCOE;
2550 kcqe.kcqe_info1 = FCOE_KCQE_COMPLETION_STATUS_NIC_ERROR;
2551 kcqe.kcqe_info2 = cid;
2552 kcqe.kcqe_info0 = l5_cid;
2553
2554 } else if (layer_code == KWQE_FLAGS_LAYER_MASK_L5_ISCSI) {
Michael Chan23021c22012-01-04 12:12:28 +00002555 ulp_type = CNIC_ULP_ISCSI;
2556 if (opcode == ISCSI_KWQE_OPCODE_UPDATE_CONN)
2557 cid = kwqe->kwqe_info1;
2558
2559 kcqe.kcqe_op_flag = (opcode + 0x10) << KCQE_FLAGS_OPCODE_SHIFT;
2560 kcqe.kcqe_op_flag |= KCQE_FLAGS_LAYER_MASK_L5_ISCSI;
2561 kcqe.kcqe_info1 = ISCSI_KCQE_COMPLETION_STATUS_NIC_ERROR;
2562 kcqe.kcqe_info2 = cid;
2563 cnic_get_l5_cid(cp, BNX2X_SW_CID(cid), &kcqe.kcqe_info0);
2564
2565 } else if (layer_code == KWQE_FLAGS_LAYER_MASK_L4) {
2566 struct l4_kcq *l4kcqe = (struct l4_kcq *) &kcqe;
Michael Chan23021c22012-01-04 12:12:28 +00002567
2568 ulp_type = CNIC_ULP_L4;
2569 if (opcode == L4_KWQE_OPCODE_VALUE_CONNECT1)
2570 kcqe_op = L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE;
2571 else if (opcode == L4_KWQE_OPCODE_VALUE_RESET)
2572 kcqe_op = L4_KCQE_OPCODE_VALUE_RESET_COMP;
2573 else if (opcode == L4_KWQE_OPCODE_VALUE_CLOSE)
2574 kcqe_op = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
2575 else
2576 return;
2577
2578 kcqe.kcqe_op_flag = (kcqe_op << KCQE_FLAGS_OPCODE_SHIFT) |
2579 KCQE_FLAGS_LAYER_MASK_L4;
2580 l4kcqe->status = L4_KCQE_COMPLETION_STATUS_NIC_ERROR;
2581 l4kcqe->cid = cid;
2582 cnic_get_l5_cid(cp, BNX2X_SW_CID(cid), &l4kcqe->conn_id);
2583 } else {
2584 return;
2585 }
2586
2587 cqes[0] = (struct kcqe *) &kcqe;
2588 cnic_reply_bnx2x_kcqes(dev, ulp_type, cqes, 1);
2589}
2590
Michael Chane1928c82010-12-23 07:43:04 +00002591static int cnic_submit_bnx2x_iscsi_kwqes(struct cnic_dev *dev,
2592 struct kwqe *wqes[], u32 num_wqes)
Michael Chan71034ba2009-10-10 13:46:59 +00002593{
2594 int i, work, ret;
2595 u32 opcode;
2596 struct kwqe *kwqe;
2597
2598 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2599 return -EAGAIN; /* bnx2 is down */
2600
2601 for (i = 0; i < num_wqes; ) {
2602 kwqe = wqes[i];
2603 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2604 work = 1;
2605
2606 switch (opcode) {
2607 case ISCSI_KWQE_OPCODE_INIT1:
2608 ret = cnic_bnx2x_iscsi_init1(dev, kwqe);
2609 break;
2610 case ISCSI_KWQE_OPCODE_INIT2:
2611 ret = cnic_bnx2x_iscsi_init2(dev, kwqe);
2612 break;
2613 case ISCSI_KWQE_OPCODE_OFFLOAD_CONN1:
2614 ret = cnic_bnx2x_iscsi_ofld1(dev, &wqes[i],
2615 num_wqes - i, &work);
2616 break;
2617 case ISCSI_KWQE_OPCODE_UPDATE_CONN:
2618 ret = cnic_bnx2x_iscsi_update(dev, kwqe);
2619 break;
2620 case ISCSI_KWQE_OPCODE_DESTROY_CONN:
2621 ret = cnic_bnx2x_iscsi_destroy(dev, kwqe);
2622 break;
2623 case L4_KWQE_OPCODE_VALUE_CONNECT1:
2624 ret = cnic_bnx2x_connect(dev, &wqes[i], num_wqes - i,
2625 &work);
2626 break;
2627 case L4_KWQE_OPCODE_VALUE_CLOSE:
2628 ret = cnic_bnx2x_close(dev, kwqe);
2629 break;
2630 case L4_KWQE_OPCODE_VALUE_RESET:
2631 ret = cnic_bnx2x_reset(dev, kwqe);
2632 break;
2633 case L4_KWQE_OPCODE_VALUE_OFFLOAD_PG:
2634 ret = cnic_bnx2x_offload_pg(dev, kwqe);
2635 break;
2636 case L4_KWQE_OPCODE_VALUE_UPDATE_PG:
2637 ret = cnic_bnx2x_update_pg(dev, kwqe);
2638 break;
2639 case L4_KWQE_OPCODE_VALUE_UPLOAD_PG:
2640 ret = 0;
2641 break;
2642 default:
2643 ret = 0;
Joe Perchesddf79b22010-02-17 15:01:54 +00002644 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2645 opcode);
Michael Chan71034ba2009-10-10 13:46:59 +00002646 break;
2647 }
Michael Chan23021c22012-01-04 12:12:28 +00002648 if (ret < 0) {
Joe Perchesddf79b22010-02-17 15:01:54 +00002649 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2650 opcode);
Michael Chan23021c22012-01-04 12:12:28 +00002651
2652 /* Possibly bnx2x parity error, send completion
2653 * to ulp drivers with error code to speed up
2654 * cleanup and reset recovery.
2655 */
2656 if (ret == -EIO || ret == -EAGAIN)
2657 cnic_bnx2x_kwqe_err(dev, kwqe);
2658 }
Michael Chan71034ba2009-10-10 13:46:59 +00002659 i += work;
2660 }
2661 return 0;
2662}
2663
Michael Chane1928c82010-12-23 07:43:04 +00002664static int cnic_submit_bnx2x_fcoe_kwqes(struct cnic_dev *dev,
2665 struct kwqe *wqes[], u32 num_wqes)
2666{
2667 struct cnic_local *cp = dev->cnic_priv;
2668 int i, work, ret;
2669 u32 opcode;
2670 struct kwqe *kwqe;
2671
2672 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2673 return -EAGAIN; /* bnx2 is down */
2674
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002675 if (!BNX2X_CHIP_IS_E2_PLUS(cp->chip_id))
Michael Chane1928c82010-12-23 07:43:04 +00002676 return -EINVAL;
2677
2678 for (i = 0; i < num_wqes; ) {
2679 kwqe = wqes[i];
2680 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2681 work = 1;
2682
2683 switch (opcode) {
2684 case FCOE_KWQE_OPCODE_INIT1:
2685 ret = cnic_bnx2x_fcoe_init1(dev, &wqes[i],
2686 num_wqes - i, &work);
2687 break;
2688 case FCOE_KWQE_OPCODE_OFFLOAD_CONN1:
2689 ret = cnic_bnx2x_fcoe_ofld1(dev, &wqes[i],
2690 num_wqes - i, &work);
2691 break;
2692 case FCOE_KWQE_OPCODE_ENABLE_CONN:
2693 ret = cnic_bnx2x_fcoe_enable(dev, kwqe);
2694 break;
2695 case FCOE_KWQE_OPCODE_DISABLE_CONN:
2696 ret = cnic_bnx2x_fcoe_disable(dev, kwqe);
2697 break;
2698 case FCOE_KWQE_OPCODE_DESTROY_CONN:
2699 ret = cnic_bnx2x_fcoe_destroy(dev, kwqe);
2700 break;
2701 case FCOE_KWQE_OPCODE_DESTROY:
2702 ret = cnic_bnx2x_fcoe_fw_destroy(dev, kwqe);
2703 break;
2704 case FCOE_KWQE_OPCODE_STAT:
2705 ret = cnic_bnx2x_fcoe_stat(dev, kwqe);
2706 break;
2707 default:
2708 ret = 0;
2709 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2710 opcode);
2711 break;
2712 }
Michael Chan3238a9b2012-02-05 15:24:40 +00002713 if (ret < 0) {
Michael Chane1928c82010-12-23 07:43:04 +00002714 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2715 opcode);
Michael Chan3238a9b2012-02-05 15:24:40 +00002716
2717 /* Possibly bnx2x parity error, send completion
2718 * to ulp drivers with error code to speed up
2719 * cleanup and reset recovery.
2720 */
2721 if (ret == -EIO || ret == -EAGAIN)
2722 cnic_bnx2x_kwqe_err(dev, kwqe);
2723 }
Michael Chane1928c82010-12-23 07:43:04 +00002724 i += work;
2725 }
2726 return 0;
2727}
2728
2729static int cnic_submit_bnx2x_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
2730 u32 num_wqes)
2731{
2732 int ret = -EINVAL;
2733 u32 layer_code;
2734
2735 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2736 return -EAGAIN; /* bnx2x is down */
2737
2738 if (!num_wqes)
2739 return 0;
2740
2741 layer_code = wqes[0]->kwqe_op_flag & KWQE_LAYER_MASK;
2742 switch (layer_code) {
2743 case KWQE_FLAGS_LAYER_MASK_L5_ISCSI:
2744 case KWQE_FLAGS_LAYER_MASK_L4:
2745 case KWQE_FLAGS_LAYER_MASK_L2:
2746 ret = cnic_submit_bnx2x_iscsi_kwqes(dev, wqes, num_wqes);
2747 break;
2748
2749 case KWQE_FLAGS_LAYER_MASK_L5_FCOE:
2750 ret = cnic_submit_bnx2x_fcoe_kwqes(dev, wqes, num_wqes);
2751 break;
2752 }
2753 return ret;
2754}
2755
2756static inline u32 cnic_get_kcqe_layer_mask(u32 opflag)
2757{
2758 if (unlikely(KCQE_OPCODE(opflag) == FCOE_RAMROD_CMD_ID_TERMINATE_CONN))
2759 return KCQE_FLAGS_LAYER_MASK_L4;
2760
2761 return opflag & KCQE_FLAGS_LAYER_MASK;
2762}
2763
Michael Chana4636962009-06-08 18:14:43 -07002764static void service_kcqes(struct cnic_dev *dev, int num_cqes)
2765{
2766 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002767 int i, j, comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002768
2769 i = 0;
2770 j = 1;
2771 while (num_cqes) {
2772 struct cnic_ulp_ops *ulp_ops;
2773 int ulp_type;
2774 u32 kcqe_op_flag = cp->completed_kcq[i]->kcqe_op_flag;
Michael Chane1928c82010-12-23 07:43:04 +00002775 u32 kcqe_layer = cnic_get_kcqe_layer_mask(kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002776
2777 if (unlikely(kcqe_op_flag & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002778 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002779
2780 while (j < num_cqes) {
2781 u32 next_op = cp->completed_kcq[i + j]->kcqe_op_flag;
2782
Michael Chane1928c82010-12-23 07:43:04 +00002783 if (cnic_get_kcqe_layer_mask(next_op) != kcqe_layer)
Michael Chana4636962009-06-08 18:14:43 -07002784 break;
2785
2786 if (unlikely(next_op & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002787 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002788 j++;
2789 }
2790
2791 if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_RDMA)
2792 ulp_type = CNIC_ULP_RDMA;
2793 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_ISCSI)
2794 ulp_type = CNIC_ULP_ISCSI;
Michael Chane1928c82010-12-23 07:43:04 +00002795 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_FCOE)
2796 ulp_type = CNIC_ULP_FCOE;
Michael Chana4636962009-06-08 18:14:43 -07002797 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L4)
2798 ulp_type = CNIC_ULP_L4;
2799 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L2)
2800 goto end;
2801 else {
Joe Perchesddf79b22010-02-17 15:01:54 +00002802 netdev_err(dev->netdev, "Unknown type of KCQE(0x%x)\n",
2803 kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002804 goto end;
2805 }
2806
2807 rcu_read_lock();
2808 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
2809 if (likely(ulp_ops)) {
2810 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
2811 cp->completed_kcq + i, j);
2812 }
2813 rcu_read_unlock();
2814end:
2815 num_cqes -= j;
2816 i += j;
2817 j = 1;
2818 }
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002819 if (unlikely(comp))
2820 cnic_spq_completion(dev, DRV_CTL_RET_L5_SPQ_CREDIT_CMD, comp);
Michael Chana4636962009-06-08 18:14:43 -07002821}
2822
Michael Chan644b9d42010-06-24 14:58:40 +00002823static int cnic_get_kcqes(struct cnic_dev *dev, struct kcq_info *info)
Michael Chana4636962009-06-08 18:14:43 -07002824{
2825 struct cnic_local *cp = dev->cnic_priv;
Michael Chan644b9d42010-06-24 14:58:40 +00002826 u16 i, ri, hw_prod, last;
Michael Chana4636962009-06-08 18:14:43 -07002827 struct kcqe *kcqe;
2828 int kcqe_cnt = 0, last_cnt = 0;
2829
Michael Chan644b9d42010-06-24 14:58:40 +00002830 i = ri = last = info->sw_prod_idx;
Michael Chana4636962009-06-08 18:14:43 -07002831 ri &= MAX_KCQ_IDX;
Michael Chan644b9d42010-06-24 14:58:40 +00002832 hw_prod = *info->hw_prod_idx_ptr;
Michael Chan59e51372011-06-14 01:32:38 +00002833 hw_prod = info->hw_idx(hw_prod);
Michael Chana4636962009-06-08 18:14:43 -07002834
2835 while ((i != hw_prod) && (kcqe_cnt < MAX_COMPLETED_KCQE)) {
Michael Chan644b9d42010-06-24 14:58:40 +00002836 kcqe = &info->kcq[KCQ_PG(ri)][KCQ_IDX(ri)];
Michael Chana4636962009-06-08 18:14:43 -07002837 cp->completed_kcq[kcqe_cnt++] = kcqe;
Michael Chan59e51372011-06-14 01:32:38 +00002838 i = info->next_idx(i);
Michael Chana4636962009-06-08 18:14:43 -07002839 ri = i & MAX_KCQ_IDX;
2840 if (likely(!(kcqe->kcqe_op_flag & KCQE_FLAGS_NEXT))) {
2841 last_cnt = kcqe_cnt;
2842 last = i;
2843 }
2844 }
2845
Michael Chan644b9d42010-06-24 14:58:40 +00002846 info->sw_prod_idx = last;
Michael Chana4636962009-06-08 18:14:43 -07002847 return last_cnt;
2848}
2849
Michael Chan48f753d2010-05-18 11:32:53 +00002850static int cnic_l2_completion(struct cnic_local *cp)
2851{
2852 u16 hw_cons, sw_cons;
Michael Chancd801532010-10-13 14:06:49 +00002853 struct cnic_uio_dev *udev = cp->udev;
Michael Chan48f753d2010-05-18 11:32:53 +00002854 union eth_rx_cqe *cqe, *cqe_ring = (union eth_rx_cqe *)
Michael Chancd801532010-10-13 14:06:49 +00002855 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Michael Chan48f753d2010-05-18 11:32:53 +00002856 u32 cmd;
2857 int comp = 0;
2858
2859 if (!test_bit(CNIC_F_BNX2X_CLASS, &cp->dev->flags))
2860 return 0;
2861
2862 hw_cons = *cp->rx_cons_ptr;
2863 if ((hw_cons & BNX2X_MAX_RCQ_DESC_CNT) == BNX2X_MAX_RCQ_DESC_CNT)
2864 hw_cons++;
2865
2866 sw_cons = cp->rx_cons;
2867 while (sw_cons != hw_cons) {
2868 u8 cqe_fp_flags;
2869
2870 cqe = &cqe_ring[sw_cons & BNX2X_MAX_RCQ_DESC_CNT];
2871 cqe_fp_flags = cqe->fast_path_cqe.type_error_flags;
2872 if (cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE) {
2873 cmd = le32_to_cpu(cqe->ramrod_cqe.conn_and_cmd_data);
2874 cmd >>= COMMON_RAMROD_ETH_RX_CQE_CMD_ID_SHIFT;
2875 if (cmd == RAMROD_CMD_ID_ETH_CLIENT_SETUP ||
2876 cmd == RAMROD_CMD_ID_ETH_HALT)
2877 comp++;
2878 }
2879 sw_cons = BNX2X_NEXT_RCQE(sw_cons);
2880 }
2881 return comp;
2882}
2883
Michael Chan86b53602009-10-10 13:46:57 +00002884static void cnic_chk_pkt_rings(struct cnic_local *cp)
Michael Chana4636962009-06-08 18:14:43 -07002885{
Michael Chan541a7812010-10-06 03:17:22 +00002886 u16 rx_cons, tx_cons;
Michael Chan48f753d2010-05-18 11:32:53 +00002887 int comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002888
Michael Chan541a7812010-10-06 03:17:22 +00002889 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
Michael Chan66fee9e2010-06-24 14:58:38 +00002890 return;
2891
Michael Chan541a7812010-10-06 03:17:22 +00002892 rx_cons = *cp->rx_cons_ptr;
2893 tx_cons = *cp->tx_cons_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002894 if (cp->tx_cons != tx_cons || cp->rx_cons != rx_cons) {
Michael Chan48f753d2010-05-18 11:32:53 +00002895 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
2896 comp = cnic_l2_completion(cp);
2897
Michael Chana4636962009-06-08 18:14:43 -07002898 cp->tx_cons = tx_cons;
2899 cp->rx_cons = rx_cons;
Michael Chan71034ba2009-10-10 13:46:59 +00002900
Michael Chancd801532010-10-13 14:06:49 +00002901 if (cp->udev)
2902 uio_event_notify(&cp->udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -07002903 }
Michael Chan48f753d2010-05-18 11:32:53 +00002904 if (comp)
2905 clear_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07002906}
2907
Michael Chanb177a5d52010-06-24 14:58:41 +00002908static u32 cnic_service_bnx2_queues(struct cnic_dev *dev)
Michael Chana4636962009-06-08 18:14:43 -07002909{
Michael Chana4636962009-06-08 18:14:43 -07002910 struct cnic_local *cp = dev->cnic_priv;
Michael Chanb177a5d52010-06-24 14:58:41 +00002911 u32 status_idx = (u16) *cp->kcq1.status_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002912 int kcqe_cnt;
2913
Michael Chan107c3f42011-03-02 13:00:49 +00002914 /* status block index must be read before reading other fields */
2915 rmb();
Michael Chana4636962009-06-08 18:14:43 -07002916 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
2917
Michael Chan644b9d42010-06-24 14:58:40 +00002918 while ((kcqe_cnt = cnic_get_kcqes(dev, &cp->kcq1))) {
Michael Chana4636962009-06-08 18:14:43 -07002919
2920 service_kcqes(dev, kcqe_cnt);
2921
2922 /* Tell compiler that status_blk fields can change. */
2923 barrier();
Michael Chan93736652011-06-08 19:29:32 +00002924 status_idx = (u16) *cp->kcq1.status_idx_ptr;
2925 /* status block index must be read first */
2926 rmb();
2927 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002928 }
2929
Michael Chan644b9d42010-06-24 14:58:40 +00002930 CNIC_WR16(dev, cp->kcq1.io_addr, cp->kcq1.sw_prod_idx);
Michael Chana4636962009-06-08 18:14:43 -07002931
Michael Chan86b53602009-10-10 13:46:57 +00002932 cnic_chk_pkt_rings(cp);
Michael Chanb177a5d52010-06-24 14:58:41 +00002933
Michael Chana4636962009-06-08 18:14:43 -07002934 return status_idx;
2935}
2936
Michael Chanb177a5d52010-06-24 14:58:41 +00002937static int cnic_service_bnx2(void *data, void *status_blk)
2938{
2939 struct cnic_dev *dev = data;
Michael Chanb177a5d52010-06-24 14:58:41 +00002940
Michael Chaneaaa6e92010-12-23 08:38:30 +00002941 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
2942 struct status_block *sblk = status_blk;
2943
2944 return sblk->status_idx;
2945 }
Michael Chanb177a5d52010-06-24 14:58:41 +00002946
2947 return cnic_service_bnx2_queues(dev);
2948}
2949
Michael Chana4636962009-06-08 18:14:43 -07002950static void cnic_service_bnx2_msix(unsigned long data)
2951{
2952 struct cnic_dev *dev = (struct cnic_dev *) data;
2953 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002954
Michael Chanb177a5d52010-06-24 14:58:41 +00002955 cp->last_status_idx = cnic_service_bnx2_queues(dev);
Michael Chana4636962009-06-08 18:14:43 -07002956
Michael Chana4636962009-06-08 18:14:43 -07002957 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
2958 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
2959}
2960
Michael Chan66fee9e2010-06-24 14:58:38 +00002961static void cnic_doirq(struct cnic_dev *dev)
2962{
2963 struct cnic_local *cp = dev->cnic_priv;
Michael Chan66fee9e2010-06-24 14:58:38 +00002964
2965 if (likely(test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
Michael Chaneaaa6e92010-12-23 08:38:30 +00002966 u16 prod = cp->kcq1.sw_prod_idx & MAX_KCQ_IDX;
2967
Michael Chan66fee9e2010-06-24 14:58:38 +00002968 prefetch(cp->status_blk.gen);
Michael Chane6c28892010-06-24 14:58:39 +00002969 prefetch(&cp->kcq1.kcq[KCQ_PG(prod)][KCQ_IDX(prod)]);
Michael Chan66fee9e2010-06-24 14:58:38 +00002970
2971 tasklet_schedule(&cp->cnic_irq_task);
2972 }
2973}
2974
Michael Chana4636962009-06-08 18:14:43 -07002975static irqreturn_t cnic_irq(int irq, void *dev_instance)
2976{
2977 struct cnic_dev *dev = dev_instance;
2978 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002979
2980 if (cp->ack_int)
2981 cp->ack_int(dev);
2982
Michael Chan66fee9e2010-06-24 14:58:38 +00002983 cnic_doirq(dev);
Michael Chana4636962009-06-08 18:14:43 -07002984
2985 return IRQ_HANDLED;
2986}
2987
Michael Chan71034ba2009-10-10 13:46:59 +00002988static inline void cnic_ack_bnx2x_int(struct cnic_dev *dev, u8 id, u8 storm,
2989 u16 index, u8 op, u8 update)
2990{
2991 struct cnic_local *cp = dev->cnic_priv;
2992 u32 hc_addr = (HC_REG_COMMAND_REG + CNIC_PORT(cp) * 32 +
2993 COMMAND_REG_INT_ACK);
2994 struct igu_ack_register igu_ack;
2995
2996 igu_ack.status_block_index = index;
2997 igu_ack.sb_id_and_flags =
2998 ((id << IGU_ACK_REGISTER_STATUS_BLOCK_ID_SHIFT) |
2999 (storm << IGU_ACK_REGISTER_STORM_ID_SHIFT) |
3000 (update << IGU_ACK_REGISTER_UPDATE_INDEX_SHIFT) |
3001 (op << IGU_ACK_REGISTER_INTERRUPT_MODE_SHIFT));
3002
3003 CNIC_WR(dev, hc_addr, (*(u32 *)&igu_ack));
3004}
3005
Michael Chanee87a822010-10-13 14:06:51 +00003006static void cnic_ack_igu_sb(struct cnic_dev *dev, u8 igu_sb_id, u8 segment,
3007 u16 index, u8 op, u8 update)
3008{
3009 struct igu_regular cmd_data;
3010 u32 igu_addr = BAR_IGU_INTMEM + (IGU_CMD_INT_ACK_BASE + igu_sb_id) * 8;
3011
3012 cmd_data.sb_id_and_flags =
3013 (index << IGU_REGULAR_SB_INDEX_SHIFT) |
3014 (segment << IGU_REGULAR_SEGMENT_ACCESS_SHIFT) |
3015 (update << IGU_REGULAR_BUPDATE_SHIFT) |
3016 (op << IGU_REGULAR_ENABLE_INT_SHIFT);
3017
3018
3019 CNIC_WR(dev, igu_addr, cmd_data.sb_id_and_flags);
3020}
3021
Michael Chan71034ba2009-10-10 13:46:59 +00003022static void cnic_ack_bnx2x_msix(struct cnic_dev *dev)
3023{
3024 struct cnic_local *cp = dev->cnic_priv;
3025
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003026 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, CSTORM_ID, 0,
Michael Chan71034ba2009-10-10 13:46:59 +00003027 IGU_INT_DISABLE, 0);
3028}
3029
Michael Chanee87a822010-10-13 14:06:51 +00003030static void cnic_ack_bnx2x_e2_msix(struct cnic_dev *dev)
3031{
3032 struct cnic_local *cp = dev->cnic_priv;
3033
3034 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF, 0,
3035 IGU_INT_DISABLE, 0);
3036}
3037
Michael Chanb177a5d52010-06-24 14:58:41 +00003038static u32 cnic_service_bnx2x_kcq(struct cnic_dev *dev, struct kcq_info *info)
Michael Chan71034ba2009-10-10 13:46:59 +00003039{
Michael Chanb177a5d52010-06-24 14:58:41 +00003040 u32 last_status = *info->status_idx_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00003041 int kcqe_cnt;
3042
Michael Chan107c3f42011-03-02 13:00:49 +00003043 /* status block index must be read before reading the KCQ */
3044 rmb();
Michael Chanb177a5d52010-06-24 14:58:41 +00003045 while ((kcqe_cnt = cnic_get_kcqes(dev, info))) {
Michael Chan71034ba2009-10-10 13:46:59 +00003046
3047 service_kcqes(dev, kcqe_cnt);
3048
3049 /* Tell compiler that sblk fields can change. */
3050 barrier();
Michael Chan71034ba2009-10-10 13:46:59 +00003051
Michael Chanb177a5d52010-06-24 14:58:41 +00003052 last_status = *info->status_idx_ptr;
Michael Chan107c3f42011-03-02 13:00:49 +00003053 /* status block index must be read before reading the KCQ */
3054 rmb();
Michael Chan71034ba2009-10-10 13:46:59 +00003055 }
Michael Chanb177a5d52010-06-24 14:58:41 +00003056 return last_status;
3057}
3058
3059static void cnic_service_bnx2x_bh(unsigned long data)
3060{
3061 struct cnic_dev *dev = (struct cnic_dev *) data;
3062 struct cnic_local *cp = dev->cnic_priv;
Michael Chan0197b082011-03-02 13:00:50 +00003063 u32 status_idx, new_status_idx;
Michael Chanb177a5d52010-06-24 14:58:41 +00003064
3065 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
3066 return;
3067
Michael Chan0197b082011-03-02 13:00:50 +00003068 while (1) {
3069 status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq1);
Michael Chan71034ba2009-10-10 13:46:59 +00003070
Michael Chan0197b082011-03-02 13:00:50 +00003071 CNIC_WR16(dev, cp->kcq1.io_addr,
3072 cp->kcq1.sw_prod_idx + MAX_KCQ_IDX);
Michael Chane21ba412010-12-23 07:43:03 +00003073
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03003074 if (!BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chan0197b082011-03-02 13:00:50 +00003075 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, USTORM_ID,
3076 status_idx, IGU_INT_ENABLE, 1);
3077 break;
3078 }
3079
3080 new_status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq2);
3081
3082 if (new_status_idx != status_idx)
3083 continue;
Michael Chane21ba412010-12-23 07:43:03 +00003084
3085 CNIC_WR16(dev, cp->kcq2.io_addr, cp->kcq2.sw_prod_idx +
3086 MAX_KCQ_IDX);
3087
Michael Chanee87a822010-10-13 14:06:51 +00003088 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF,
3089 status_idx, IGU_INT_ENABLE, 1);
Michael Chan0197b082011-03-02 13:00:50 +00003090
3091 break;
Michael Chane21ba412010-12-23 07:43:03 +00003092 }
Michael Chan71034ba2009-10-10 13:46:59 +00003093}
3094
3095static int cnic_service_bnx2x(void *data, void *status_blk)
3096{
3097 struct cnic_dev *dev = data;
3098 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00003099
Michael Chan66fee9e2010-06-24 14:58:38 +00003100 if (!(cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
3101 cnic_doirq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00003102
Michael Chan66fee9e2010-06-24 14:58:38 +00003103 cnic_chk_pkt_rings(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00003104
3105 return 0;
3106}
3107
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003108static void cnic_ulp_stop_one(struct cnic_local *cp, int if_type)
3109{
3110 struct cnic_ulp_ops *ulp_ops;
3111
3112 if (if_type == CNIC_ULP_ISCSI)
3113 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
3114
3115 mutex_lock(&cnic_lock);
3116 ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
3117 lockdep_is_held(&cnic_lock));
3118 if (!ulp_ops) {
3119 mutex_unlock(&cnic_lock);
3120 return;
3121 }
3122 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
3123 mutex_unlock(&cnic_lock);
3124
3125 if (test_and_clear_bit(ULP_F_START, &cp->ulp_flags[if_type]))
3126 ulp_ops->cnic_stop(cp->ulp_handle[if_type]);
3127
3128 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
3129}
3130
Michael Chana4636962009-06-08 18:14:43 -07003131static void cnic_ulp_stop(struct cnic_dev *dev)
3132{
3133 struct cnic_local *cp = dev->cnic_priv;
3134 int if_type;
3135
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003136 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++)
3137 cnic_ulp_stop_one(cp, if_type);
Michael Chana4636962009-06-08 18:14:43 -07003138}
3139
3140static void cnic_ulp_start(struct cnic_dev *dev)
3141{
3142 struct cnic_local *cp = dev->cnic_priv;
3143 int if_type;
3144
Michael Chana4636962009-06-08 18:14:43 -07003145 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
3146 struct cnic_ulp_ops *ulp_ops;
3147
Michael Chan681dbd72009-08-14 15:49:46 +00003148 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003149 ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
3150 lockdep_is_held(&cnic_lock));
Michael Chan681dbd72009-08-14 15:49:46 +00003151 if (!ulp_ops || !ulp_ops->cnic_start) {
3152 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003153 continue;
Michael Chan681dbd72009-08-14 15:49:46 +00003154 }
3155 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
3156 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003157
3158 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[if_type]))
3159 ulp_ops->cnic_start(cp->ulp_handle[if_type]);
Michael Chan681dbd72009-08-14 15:49:46 +00003160
3161 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
Michael Chana4636962009-06-08 18:14:43 -07003162 }
Michael Chana4636962009-06-08 18:14:43 -07003163}
3164
Barak Witkowski1d187b32011-12-05 22:41:50 +00003165static int cnic_copy_ulp_stats(struct cnic_dev *dev, int ulp_type)
3166{
3167 struct cnic_local *cp = dev->cnic_priv;
3168 struct cnic_ulp_ops *ulp_ops;
3169 int rc;
3170
3171 mutex_lock(&cnic_lock);
3172 ulp_ops = cnic_ulp_tbl_prot(ulp_type);
3173 if (ulp_ops && ulp_ops->cnic_get_stats)
3174 rc = ulp_ops->cnic_get_stats(cp->ulp_handle[ulp_type]);
3175 else
3176 rc = -ENODEV;
3177 mutex_unlock(&cnic_lock);
3178 return rc;
3179}
3180
Michael Chana4636962009-06-08 18:14:43 -07003181static int cnic_ctl(void *data, struct cnic_ctl_info *info)
3182{
3183 struct cnic_dev *dev = data;
Barak Witkowski1d187b32011-12-05 22:41:50 +00003184 int ulp_type = CNIC_ULP_ISCSI;
Michael Chana4636962009-06-08 18:14:43 -07003185
3186 switch (info->cmd) {
3187 case CNIC_CTL_STOP_CMD:
3188 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07003189
3190 cnic_ulp_stop(dev);
3191 cnic_stop_hw(dev);
3192
Michael Chana4636962009-06-08 18:14:43 -07003193 cnic_put(dev);
3194 break;
3195 case CNIC_CTL_START_CMD:
3196 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07003197
3198 if (!cnic_start_hw(dev))
3199 cnic_ulp_start(dev);
3200
Michael Chana4636962009-06-08 18:14:43 -07003201 cnic_put(dev);
3202 break;
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003203 case CNIC_CTL_STOP_ISCSI_CMD: {
3204 struct cnic_local *cp = dev->cnic_priv;
3205 set_bit(CNIC_LCL_FL_STOP_ISCSI, &cp->cnic_local_flags);
3206 queue_delayed_work(cnic_wq, &cp->delete_task, 0);
3207 break;
3208 }
Michael Chan71034ba2009-10-10 13:46:59 +00003209 case CNIC_CTL_COMPLETION_CMD: {
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03003210 struct cnic_ctl_completion *comp = &info->data.comp;
3211 u32 cid = BNX2X_SW_CID(comp->cid);
Michael Chan71034ba2009-10-10 13:46:59 +00003212 u32 l5_cid;
3213 struct cnic_local *cp = dev->cnic_priv;
3214
3215 if (cnic_get_l5_cid(cp, cid, &l5_cid) == 0) {
3216 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3217
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03003218 if (unlikely(comp->error)) {
3219 set_bit(CTX_FL_CID_ERROR, &ctx->ctx_flags);
3220 netdev_err(dev->netdev,
3221 "CID %x CFC delete comp error %x\n",
3222 cid, comp->error);
3223 }
3224
Michael Chan71034ba2009-10-10 13:46:59 +00003225 ctx->wait_cond = 1;
3226 wake_up(&ctx->waitq);
3227 }
3228 break;
3229 }
Barak Witkowski1d187b32011-12-05 22:41:50 +00003230 case CNIC_CTL_FCOE_STATS_GET_CMD:
3231 ulp_type = CNIC_ULP_FCOE;
3232 /* fall through */
3233 case CNIC_CTL_ISCSI_STATS_GET_CMD:
3234 cnic_hold(dev);
3235 cnic_copy_ulp_stats(dev, ulp_type);
3236 cnic_put(dev);
3237 break;
3238
Michael Chana4636962009-06-08 18:14:43 -07003239 default:
3240 return -EINVAL;
3241 }
3242 return 0;
3243}
3244
3245static void cnic_ulp_init(struct cnic_dev *dev)
3246{
3247 int i;
3248 struct cnic_local *cp = dev->cnic_priv;
3249
Michael Chana4636962009-06-08 18:14:43 -07003250 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3251 struct cnic_ulp_ops *ulp_ops;
3252
Michael Chan7fc1ece2009-08-14 15:49:47 +00003253 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003254 ulp_ops = cnic_ulp_tbl_prot(i);
Michael Chan7fc1ece2009-08-14 15:49:47 +00003255 if (!ulp_ops || !ulp_ops->cnic_init) {
3256 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003257 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003258 }
3259 ulp_get(ulp_ops);
3260 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003261
3262 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3263 ulp_ops->cnic_init(dev);
3264
Michael Chan7fc1ece2009-08-14 15:49:47 +00003265 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003266 }
Michael Chana4636962009-06-08 18:14:43 -07003267}
3268
3269static void cnic_ulp_exit(struct cnic_dev *dev)
3270{
3271 int i;
3272 struct cnic_local *cp = dev->cnic_priv;
3273
Michael Chana4636962009-06-08 18:14:43 -07003274 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3275 struct cnic_ulp_ops *ulp_ops;
3276
Michael Chan7fc1ece2009-08-14 15:49:47 +00003277 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003278 ulp_ops = cnic_ulp_tbl_prot(i);
Michael Chan7fc1ece2009-08-14 15:49:47 +00003279 if (!ulp_ops || !ulp_ops->cnic_exit) {
3280 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003281 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003282 }
3283 ulp_get(ulp_ops);
3284 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003285
3286 if (test_and_clear_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3287 ulp_ops->cnic_exit(dev);
3288
Michael Chan7fc1ece2009-08-14 15:49:47 +00003289 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003290 }
Michael Chana4636962009-06-08 18:14:43 -07003291}
3292
3293static int cnic_cm_offload_pg(struct cnic_sock *csk)
3294{
3295 struct cnic_dev *dev = csk->dev;
3296 struct l4_kwq_offload_pg *l4kwqe;
3297 struct kwqe *wqes[1];
3298
3299 l4kwqe = (struct l4_kwq_offload_pg *) &csk->kwqe1;
3300 memset(l4kwqe, 0, sizeof(*l4kwqe));
3301 wqes[0] = (struct kwqe *) l4kwqe;
3302
3303 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_OFFLOAD_PG;
3304 l4kwqe->flags =
3305 L4_LAYER_CODE << L4_KWQ_OFFLOAD_PG_LAYER_CODE_SHIFT;
3306 l4kwqe->l2hdr_nbytes = ETH_HLEN;
3307
3308 l4kwqe->da0 = csk->ha[0];
3309 l4kwqe->da1 = csk->ha[1];
3310 l4kwqe->da2 = csk->ha[2];
3311 l4kwqe->da3 = csk->ha[3];
3312 l4kwqe->da4 = csk->ha[4];
3313 l4kwqe->da5 = csk->ha[5];
3314
3315 l4kwqe->sa0 = dev->mac_addr[0];
3316 l4kwqe->sa1 = dev->mac_addr[1];
3317 l4kwqe->sa2 = dev->mac_addr[2];
3318 l4kwqe->sa3 = dev->mac_addr[3];
3319 l4kwqe->sa4 = dev->mac_addr[4];
3320 l4kwqe->sa5 = dev->mac_addr[5];
3321
3322 l4kwqe->etype = ETH_P_IP;
Eddie Waia9736c02010-02-24 14:42:04 +00003323 l4kwqe->ipid_start = DEF_IPID_START;
Michael Chana4636962009-06-08 18:14:43 -07003324 l4kwqe->host_opaque = csk->l5_cid;
3325
3326 if (csk->vlan_id) {
3327 l4kwqe->pg_flags |= L4_KWQ_OFFLOAD_PG_VLAN_TAGGING;
3328 l4kwqe->vlan_tag = csk->vlan_id;
3329 l4kwqe->l2hdr_nbytes += 4;
3330 }
3331
3332 return dev->submit_kwqes(dev, wqes, 1);
3333}
3334
3335static int cnic_cm_update_pg(struct cnic_sock *csk)
3336{
3337 struct cnic_dev *dev = csk->dev;
3338 struct l4_kwq_update_pg *l4kwqe;
3339 struct kwqe *wqes[1];
3340
3341 l4kwqe = (struct l4_kwq_update_pg *) &csk->kwqe1;
3342 memset(l4kwqe, 0, sizeof(*l4kwqe));
3343 wqes[0] = (struct kwqe *) l4kwqe;
3344
3345 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPDATE_PG;
3346 l4kwqe->flags =
3347 L4_LAYER_CODE << L4_KWQ_UPDATE_PG_LAYER_CODE_SHIFT;
3348 l4kwqe->pg_cid = csk->pg_cid;
3349
3350 l4kwqe->da0 = csk->ha[0];
3351 l4kwqe->da1 = csk->ha[1];
3352 l4kwqe->da2 = csk->ha[2];
3353 l4kwqe->da3 = csk->ha[3];
3354 l4kwqe->da4 = csk->ha[4];
3355 l4kwqe->da5 = csk->ha[5];
3356
3357 l4kwqe->pg_host_opaque = csk->l5_cid;
3358 l4kwqe->pg_valids = L4_KWQ_UPDATE_PG_VALIDS_DA;
3359
3360 return dev->submit_kwqes(dev, wqes, 1);
3361}
3362
3363static int cnic_cm_upload_pg(struct cnic_sock *csk)
3364{
3365 struct cnic_dev *dev = csk->dev;
3366 struct l4_kwq_upload *l4kwqe;
3367 struct kwqe *wqes[1];
3368
3369 l4kwqe = (struct l4_kwq_upload *) &csk->kwqe1;
3370 memset(l4kwqe, 0, sizeof(*l4kwqe));
3371 wqes[0] = (struct kwqe *) l4kwqe;
3372
3373 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPLOAD_PG;
3374 l4kwqe->flags =
3375 L4_LAYER_CODE << L4_KWQ_UPLOAD_LAYER_CODE_SHIFT;
3376 l4kwqe->cid = csk->pg_cid;
3377
3378 return dev->submit_kwqes(dev, wqes, 1);
3379}
3380
3381static int cnic_cm_conn_req(struct cnic_sock *csk)
3382{
3383 struct cnic_dev *dev = csk->dev;
3384 struct l4_kwq_connect_req1 *l4kwqe1;
3385 struct l4_kwq_connect_req2 *l4kwqe2;
3386 struct l4_kwq_connect_req3 *l4kwqe3;
3387 struct kwqe *wqes[3];
3388 u8 tcp_flags = 0;
3389 int num_wqes = 2;
3390
3391 l4kwqe1 = (struct l4_kwq_connect_req1 *) &csk->kwqe1;
3392 l4kwqe2 = (struct l4_kwq_connect_req2 *) &csk->kwqe2;
3393 l4kwqe3 = (struct l4_kwq_connect_req3 *) &csk->kwqe3;
3394 memset(l4kwqe1, 0, sizeof(*l4kwqe1));
3395 memset(l4kwqe2, 0, sizeof(*l4kwqe2));
3396 memset(l4kwqe3, 0, sizeof(*l4kwqe3));
3397
3398 l4kwqe3->op_code = L4_KWQE_OPCODE_VALUE_CONNECT3;
3399 l4kwqe3->flags =
3400 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ3_LAYER_CODE_SHIFT;
3401 l4kwqe3->ka_timeout = csk->ka_timeout;
3402 l4kwqe3->ka_interval = csk->ka_interval;
3403 l4kwqe3->ka_max_probe_count = csk->ka_max_probe_count;
3404 l4kwqe3->tos = csk->tos;
3405 l4kwqe3->ttl = csk->ttl;
3406 l4kwqe3->snd_seq_scale = csk->snd_seq_scale;
3407 l4kwqe3->pmtu = csk->mtu;
3408 l4kwqe3->rcv_buf = csk->rcv_buf;
3409 l4kwqe3->snd_buf = csk->snd_buf;
3410 l4kwqe3->seed = csk->seed;
3411
3412 wqes[0] = (struct kwqe *) l4kwqe1;
3413 if (test_bit(SK_F_IPV6, &csk->flags)) {
3414 wqes[1] = (struct kwqe *) l4kwqe2;
3415 wqes[2] = (struct kwqe *) l4kwqe3;
3416 num_wqes = 3;
3417
3418 l4kwqe1->conn_flags = L4_KWQ_CONNECT_REQ1_IP_V6;
3419 l4kwqe2->op_code = L4_KWQE_OPCODE_VALUE_CONNECT2;
3420 l4kwqe2->flags =
3421 L4_KWQ_CONNECT_REQ2_LINKED_WITH_NEXT |
3422 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ2_LAYER_CODE_SHIFT;
3423 l4kwqe2->src_ip_v6_2 = be32_to_cpu(csk->src_ip[1]);
3424 l4kwqe2->src_ip_v6_3 = be32_to_cpu(csk->src_ip[2]);
3425 l4kwqe2->src_ip_v6_4 = be32_to_cpu(csk->src_ip[3]);
3426 l4kwqe2->dst_ip_v6_2 = be32_to_cpu(csk->dst_ip[1]);
3427 l4kwqe2->dst_ip_v6_3 = be32_to_cpu(csk->dst_ip[2]);
3428 l4kwqe2->dst_ip_v6_4 = be32_to_cpu(csk->dst_ip[3]);
3429 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct ipv6hdr) -
3430 sizeof(struct tcphdr);
3431 } else {
3432 wqes[1] = (struct kwqe *) l4kwqe3;
3433 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct iphdr) -
3434 sizeof(struct tcphdr);
3435 }
3436
3437 l4kwqe1->op_code = L4_KWQE_OPCODE_VALUE_CONNECT1;
3438 l4kwqe1->flags =
3439 (L4_LAYER_CODE << L4_KWQ_CONNECT_REQ1_LAYER_CODE_SHIFT) |
3440 L4_KWQ_CONNECT_REQ3_LINKED_WITH_NEXT;
3441 l4kwqe1->cid = csk->cid;
3442 l4kwqe1->pg_cid = csk->pg_cid;
3443 l4kwqe1->src_ip = be32_to_cpu(csk->src_ip[0]);
3444 l4kwqe1->dst_ip = be32_to_cpu(csk->dst_ip[0]);
3445 l4kwqe1->src_port = be16_to_cpu(csk->src_port);
3446 l4kwqe1->dst_port = be16_to_cpu(csk->dst_port);
3447 if (csk->tcp_flags & SK_TCP_NO_DELAY_ACK)
3448 tcp_flags |= L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK;
3449 if (csk->tcp_flags & SK_TCP_KEEP_ALIVE)
3450 tcp_flags |= L4_KWQ_CONNECT_REQ1_KEEP_ALIVE;
3451 if (csk->tcp_flags & SK_TCP_NAGLE)
3452 tcp_flags |= L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE;
3453 if (csk->tcp_flags & SK_TCP_TIMESTAMP)
3454 tcp_flags |= L4_KWQ_CONNECT_REQ1_TIME_STAMP;
3455 if (csk->tcp_flags & SK_TCP_SACK)
3456 tcp_flags |= L4_KWQ_CONNECT_REQ1_SACK;
3457 if (csk->tcp_flags & SK_TCP_SEG_SCALING)
3458 tcp_flags |= L4_KWQ_CONNECT_REQ1_SEG_SCALING;
3459
3460 l4kwqe1->tcp_flags = tcp_flags;
3461
3462 return dev->submit_kwqes(dev, wqes, num_wqes);
3463}
3464
3465static int cnic_cm_close_req(struct cnic_sock *csk)
3466{
3467 struct cnic_dev *dev = csk->dev;
3468 struct l4_kwq_close_req *l4kwqe;
3469 struct kwqe *wqes[1];
3470
3471 l4kwqe = (struct l4_kwq_close_req *) &csk->kwqe2;
3472 memset(l4kwqe, 0, sizeof(*l4kwqe));
3473 wqes[0] = (struct kwqe *) l4kwqe;
3474
3475 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_CLOSE;
3476 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_CLOSE_REQ_LAYER_CODE_SHIFT;
3477 l4kwqe->cid = csk->cid;
3478
3479 return dev->submit_kwqes(dev, wqes, 1);
3480}
3481
3482static int cnic_cm_abort_req(struct cnic_sock *csk)
3483{
3484 struct cnic_dev *dev = csk->dev;
3485 struct l4_kwq_reset_req *l4kwqe;
3486 struct kwqe *wqes[1];
3487
3488 l4kwqe = (struct l4_kwq_reset_req *) &csk->kwqe2;
3489 memset(l4kwqe, 0, sizeof(*l4kwqe));
3490 wqes[0] = (struct kwqe *) l4kwqe;
3491
3492 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_RESET;
3493 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_RESET_REQ_LAYER_CODE_SHIFT;
3494 l4kwqe->cid = csk->cid;
3495
3496 return dev->submit_kwqes(dev, wqes, 1);
3497}
3498
3499static int cnic_cm_create(struct cnic_dev *dev, int ulp_type, u32 cid,
3500 u32 l5_cid, struct cnic_sock **csk, void *context)
3501{
3502 struct cnic_local *cp = dev->cnic_priv;
3503 struct cnic_sock *csk1;
3504
3505 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3506 return -EINVAL;
3507
Michael Chanfdf24082010-10-13 14:06:47 +00003508 if (cp->ctx_tbl) {
3509 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3510
3511 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
3512 return -EAGAIN;
3513 }
3514
Michael Chana4636962009-06-08 18:14:43 -07003515 csk1 = &cp->csk_tbl[l5_cid];
3516 if (atomic_read(&csk1->ref_count))
3517 return -EAGAIN;
3518
3519 if (test_and_set_bit(SK_F_INUSE, &csk1->flags))
3520 return -EBUSY;
3521
3522 csk1->dev = dev;
3523 csk1->cid = cid;
3524 csk1->l5_cid = l5_cid;
3525 csk1->ulp_type = ulp_type;
3526 csk1->context = context;
3527
3528 csk1->ka_timeout = DEF_KA_TIMEOUT;
3529 csk1->ka_interval = DEF_KA_INTERVAL;
3530 csk1->ka_max_probe_count = DEF_KA_MAX_PROBE_COUNT;
3531 csk1->tos = DEF_TOS;
3532 csk1->ttl = DEF_TTL;
3533 csk1->snd_seq_scale = DEF_SND_SEQ_SCALE;
3534 csk1->rcv_buf = DEF_RCV_BUF;
3535 csk1->snd_buf = DEF_SND_BUF;
3536 csk1->seed = DEF_SEED;
3537
3538 *csk = csk1;
3539 return 0;
3540}
3541
3542static void cnic_cm_cleanup(struct cnic_sock *csk)
3543{
3544 if (csk->src_port) {
3545 struct cnic_dev *dev = csk->dev;
3546 struct cnic_local *cp = dev->cnic_priv;
3547
Michael Chan9b093362010-12-23 07:42:56 +00003548 cnic_free_id(&cp->csk_port_tbl, be16_to_cpu(csk->src_port));
Michael Chana4636962009-06-08 18:14:43 -07003549 csk->src_port = 0;
3550 }
3551}
3552
3553static void cnic_close_conn(struct cnic_sock *csk)
3554{
3555 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags)) {
3556 cnic_cm_upload_pg(csk);
3557 clear_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3558 }
3559 cnic_cm_cleanup(csk);
3560}
3561
3562static int cnic_cm_destroy(struct cnic_sock *csk)
3563{
3564 if (!cnic_in_use(csk))
3565 return -EINVAL;
3566
3567 csk_hold(csk);
3568 clear_bit(SK_F_INUSE, &csk->flags);
3569 smp_mb__after_clear_bit();
3570 while (atomic_read(&csk->ref_count) != 1)
3571 msleep(1);
3572 cnic_cm_cleanup(csk);
3573
3574 csk->flags = 0;
3575 csk_put(csk);
3576 return 0;
3577}
3578
3579static inline u16 cnic_get_vlan(struct net_device *dev,
3580 struct net_device **vlan_dev)
3581{
3582 if (dev->priv_flags & IFF_802_1Q_VLAN) {
3583 *vlan_dev = vlan_dev_real_dev(dev);
3584 return vlan_dev_vlan_id(dev);
3585 }
3586 *vlan_dev = dev;
3587 return 0;
3588}
3589
3590static int cnic_get_v4_route(struct sockaddr_in *dst_addr,
3591 struct dst_entry **dst)
3592{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003593#if defined(CONFIG_INET)
Michael Chana4636962009-06-08 18:14:43 -07003594 struct rtable *rt;
3595
David S. Miller78fbfd82011-03-12 00:00:52 -05003596 rt = ip_route_output(&init_net, dst_addr->sin_addr.s_addr, 0, 0, 0);
3597 if (!IS_ERR(rt)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07003598 *dst = &rt->dst;
David S. Miller78fbfd82011-03-12 00:00:52 -05003599 return 0;
3600 }
3601 return PTR_ERR(rt);
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003602#else
3603 return -ENETUNREACH;
3604#endif
Michael Chana4636962009-06-08 18:14:43 -07003605}
3606
3607static int cnic_get_v6_route(struct sockaddr_in6 *dst_addr,
3608 struct dst_entry **dst)
3609{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003610#if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
David S. Miller4c9483b2011-03-12 16:22:43 -05003611 struct flowi6 fl6;
Michael Chana4636962009-06-08 18:14:43 -07003612
David S. Miller4c9483b2011-03-12 16:22:43 -05003613 memset(&fl6, 0, sizeof(fl6));
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003614 fl6.daddr = dst_addr->sin6_addr;
David S. Miller4c9483b2011-03-12 16:22:43 -05003615 if (ipv6_addr_type(&fl6.daddr) & IPV6_ADDR_LINKLOCAL)
3616 fl6.flowi6_oif = dst_addr->sin6_scope_id;
Michael Chana4636962009-06-08 18:14:43 -07003617
David S. Miller4c9483b2011-03-12 16:22:43 -05003618 *dst = ip6_route_output(&init_net, NULL, &fl6);
Michael Chana4636962009-06-08 18:14:43 -07003619 if (*dst)
3620 return 0;
3621#endif
3622
3623 return -ENETUNREACH;
3624}
3625
3626static struct cnic_dev *cnic_cm_select_dev(struct sockaddr_in *dst_addr,
3627 int ulp_type)
3628{
3629 struct cnic_dev *dev = NULL;
3630 struct dst_entry *dst;
3631 struct net_device *netdev = NULL;
3632 int err = -ENETUNREACH;
3633
3634 if (dst_addr->sin_family == AF_INET)
3635 err = cnic_get_v4_route(dst_addr, &dst);
3636 else if (dst_addr->sin_family == AF_INET6) {
3637 struct sockaddr_in6 *dst_addr6 =
3638 (struct sockaddr_in6 *) dst_addr;
3639
3640 err = cnic_get_v6_route(dst_addr6, &dst);
3641 } else
3642 return NULL;
3643
3644 if (err)
3645 return NULL;
3646
3647 if (!dst->dev)
3648 goto done;
3649
3650 cnic_get_vlan(dst->dev, &netdev);
3651
3652 dev = cnic_from_netdev(netdev);
3653
3654done:
3655 dst_release(dst);
3656 if (dev)
3657 cnic_put(dev);
3658 return dev;
3659}
3660
3661static int cnic_resolve_addr(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3662{
3663 struct cnic_dev *dev = csk->dev;
3664 struct cnic_local *cp = dev->cnic_priv;
3665
3666 return cnic_send_nlmsg(cp, ISCSI_KEVENT_PATH_REQ, csk);
3667}
3668
3669static int cnic_get_route(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3670{
3671 struct cnic_dev *dev = csk->dev;
3672 struct cnic_local *cp = dev->cnic_priv;
Michael Chanc76284a2010-02-24 14:42:07 +00003673 int is_v6, rc = 0;
3674 struct dst_entry *dst = NULL;
Michael Chana4636962009-06-08 18:14:43 -07003675 struct net_device *realdev;
Michael Chan9b093362010-12-23 07:42:56 +00003676 __be16 local_port;
3677 u32 port_id;
Michael Chana4636962009-06-08 18:14:43 -07003678
3679 if (saddr->local.v6.sin6_family == AF_INET6 &&
3680 saddr->remote.v6.sin6_family == AF_INET6)
3681 is_v6 = 1;
3682 else if (saddr->local.v4.sin_family == AF_INET &&
3683 saddr->remote.v4.sin_family == AF_INET)
3684 is_v6 = 0;
3685 else
3686 return -EINVAL;
3687
3688 clear_bit(SK_F_IPV6, &csk->flags);
3689
3690 if (is_v6) {
Michael Chana4636962009-06-08 18:14:43 -07003691 set_bit(SK_F_IPV6, &csk->flags);
Michael Chanc76284a2010-02-24 14:42:07 +00003692 cnic_get_v6_route(&saddr->remote.v6, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003693
3694 memcpy(&csk->dst_ip[0], &saddr->remote.v6.sin6_addr,
3695 sizeof(struct in6_addr));
3696 csk->dst_port = saddr->remote.v6.sin6_port;
3697 local_port = saddr->local.v6.sin6_port;
Michael Chana4636962009-06-08 18:14:43 -07003698
3699 } else {
Michael Chanc76284a2010-02-24 14:42:07 +00003700 cnic_get_v4_route(&saddr->remote.v4, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003701
3702 csk->dst_ip[0] = saddr->remote.v4.sin_addr.s_addr;
3703 csk->dst_port = saddr->remote.v4.sin_port;
3704 local_port = saddr->local.v4.sin_port;
3705 }
3706
Michael Chanc76284a2010-02-24 14:42:07 +00003707 csk->vlan_id = 0;
3708 csk->mtu = dev->netdev->mtu;
3709 if (dst && dst->dev) {
3710 u16 vlan = cnic_get_vlan(dst->dev, &realdev);
3711 if (realdev == dev->netdev) {
3712 csk->vlan_id = vlan;
3713 csk->mtu = dst_mtu(dst);
3714 }
3715 }
Michael Chana4636962009-06-08 18:14:43 -07003716
Michael Chan9b093362010-12-23 07:42:56 +00003717 port_id = be16_to_cpu(local_port);
3718 if (port_id >= CNIC_LOCAL_PORT_MIN &&
3719 port_id < CNIC_LOCAL_PORT_MAX) {
3720 if (cnic_alloc_id(&cp->csk_port_tbl, port_id))
3721 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003722 } else
Michael Chan9b093362010-12-23 07:42:56 +00003723 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003724
Michael Chan9b093362010-12-23 07:42:56 +00003725 if (!port_id) {
3726 port_id = cnic_alloc_new_id(&cp->csk_port_tbl);
3727 if (port_id == -1) {
Michael Chana4636962009-06-08 18:14:43 -07003728 rc = -ENOMEM;
3729 goto err_out;
3730 }
Michael Chan9b093362010-12-23 07:42:56 +00003731 local_port = cpu_to_be16(port_id);
Michael Chana4636962009-06-08 18:14:43 -07003732 }
3733 csk->src_port = local_port;
3734
Michael Chana4636962009-06-08 18:14:43 -07003735err_out:
3736 dst_release(dst);
3737 return rc;
3738}
3739
3740static void cnic_init_csk_state(struct cnic_sock *csk)
3741{
3742 csk->state = 0;
3743 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3744 clear_bit(SK_F_CLOSING, &csk->flags);
3745}
3746
3747static int cnic_cm_connect(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3748{
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003749 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07003750 int err = 0;
3751
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003752 if (cp->ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI)
3753 return -EOPNOTSUPP;
3754
Michael Chana4636962009-06-08 18:14:43 -07003755 if (!cnic_in_use(csk))
3756 return -EINVAL;
3757
3758 if (test_and_set_bit(SK_F_CONNECT_START, &csk->flags))
3759 return -EINVAL;
3760
3761 cnic_init_csk_state(csk);
3762
3763 err = cnic_get_route(csk, saddr);
3764 if (err)
3765 goto err_out;
3766
3767 err = cnic_resolve_addr(csk, saddr);
3768 if (!err)
3769 return 0;
3770
3771err_out:
3772 clear_bit(SK_F_CONNECT_START, &csk->flags);
3773 return err;
3774}
3775
3776static int cnic_cm_abort(struct cnic_sock *csk)
3777{
3778 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chan7b34a462010-06-15 08:57:03 +00003779 u32 opcode = L4_KCQE_OPCODE_VALUE_RESET_COMP;
Michael Chana4636962009-06-08 18:14:43 -07003780
3781 if (!cnic_in_use(csk))
3782 return -EINVAL;
3783
3784 if (cnic_abort_prep(csk))
3785 return cnic_cm_abort_req(csk);
3786
3787 /* Getting here means that we haven't started connect, or
3788 * connect was not successful.
3789 */
3790
Michael Chana4636962009-06-08 18:14:43 -07003791 cp->close_conn(csk, opcode);
Michael Chan7b34a462010-06-15 08:57:03 +00003792 if (csk->state != opcode)
3793 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003794
3795 return 0;
3796}
3797
3798static int cnic_cm_close(struct cnic_sock *csk)
3799{
3800 if (!cnic_in_use(csk))
3801 return -EINVAL;
3802
3803 if (cnic_close_prep(csk)) {
3804 csk->state = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
3805 return cnic_cm_close_req(csk);
Michael Chaned99daa52010-06-15 08:57:00 +00003806 } else {
3807 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003808 }
3809 return 0;
3810}
3811
3812static void cnic_cm_upcall(struct cnic_local *cp, struct cnic_sock *csk,
3813 u8 opcode)
3814{
3815 struct cnic_ulp_ops *ulp_ops;
3816 int ulp_type = csk->ulp_type;
3817
3818 rcu_read_lock();
3819 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
3820 if (ulp_ops) {
3821 if (opcode == L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE)
3822 ulp_ops->cm_connect_complete(csk);
3823 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3824 ulp_ops->cm_close_complete(csk);
3825 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED)
3826 ulp_ops->cm_remote_abort(csk);
3827 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_COMP)
3828 ulp_ops->cm_abort_complete(csk);
3829 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED)
3830 ulp_ops->cm_remote_close(csk);
3831 }
3832 rcu_read_unlock();
3833}
3834
3835static int cnic_cm_set_pg(struct cnic_sock *csk)
3836{
3837 if (cnic_offld_prep(csk)) {
3838 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3839 cnic_cm_update_pg(csk);
3840 else
3841 cnic_cm_offload_pg(csk);
3842 }
3843 return 0;
3844}
3845
3846static void cnic_cm_process_offld_pg(struct cnic_dev *dev, struct l4_kcq *kcqe)
3847{
3848 struct cnic_local *cp = dev->cnic_priv;
3849 u32 l5_cid = kcqe->pg_host_opaque;
3850 u8 opcode = kcqe->op_code;
3851 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
3852
3853 csk_hold(csk);
3854 if (!cnic_in_use(csk))
3855 goto done;
3856
3857 if (opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3858 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3859 goto done;
3860 }
Eddie Waia9736c02010-02-24 14:42:04 +00003861 /* Possible PG kcqe status: SUCCESS, OFFLOADED_PG, or CTX_ALLOC_FAIL */
3862 if (kcqe->status == L4_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAIL) {
3863 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3864 cnic_cm_upcall(cp, csk,
3865 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3866 goto done;
3867 }
3868
Michael Chana4636962009-06-08 18:14:43 -07003869 csk->pg_cid = kcqe->pg_cid;
3870 set_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3871 cnic_cm_conn_req(csk);
3872
3873done:
3874 csk_put(csk);
3875}
3876
Michael Chane1928c82010-12-23 07:43:04 +00003877static void cnic_process_fcoe_term_conn(struct cnic_dev *dev, struct kcqe *kcqe)
3878{
3879 struct cnic_local *cp = dev->cnic_priv;
3880 struct fcoe_kcqe *fc_kcqe = (struct fcoe_kcqe *) kcqe;
3881 u32 l5_cid = fc_kcqe->fcoe_conn_id + BNX2X_FCOE_L5_CID_BASE;
3882 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3883
3884 ctx->timestamp = jiffies;
3885 ctx->wait_cond = 1;
3886 wake_up(&ctx->waitq);
3887}
3888
Michael Chana4636962009-06-08 18:14:43 -07003889static void cnic_cm_process_kcqe(struct cnic_dev *dev, struct kcqe *kcqe)
3890{
3891 struct cnic_local *cp = dev->cnic_priv;
3892 struct l4_kcq *l4kcqe = (struct l4_kcq *) kcqe;
3893 u8 opcode = l4kcqe->op_code;
3894 u32 l5_cid;
3895 struct cnic_sock *csk;
3896
Michael Chane1928c82010-12-23 07:43:04 +00003897 if (opcode == FCOE_RAMROD_CMD_ID_TERMINATE_CONN) {
3898 cnic_process_fcoe_term_conn(dev, kcqe);
3899 return;
3900 }
Michael Chana4636962009-06-08 18:14:43 -07003901 if (opcode == L4_KCQE_OPCODE_VALUE_OFFLOAD_PG ||
3902 opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3903 cnic_cm_process_offld_pg(dev, l4kcqe);
3904 return;
3905 }
3906
3907 l5_cid = l4kcqe->conn_id;
3908 if (opcode & 0x80)
3909 l5_cid = l4kcqe->cid;
3910 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3911 return;
3912
3913 csk = &cp->csk_tbl[l5_cid];
3914 csk_hold(csk);
3915
3916 if (!cnic_in_use(csk)) {
3917 csk_put(csk);
3918 return;
3919 }
3920
3921 switch (opcode) {
Eddie Waia9736c02010-02-24 14:42:04 +00003922 case L5CM_RAMROD_CMD_ID_TCP_CONNECT:
3923 if (l4kcqe->status != 0) {
3924 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3925 cnic_cm_upcall(cp, csk,
3926 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3927 }
3928 break;
Michael Chana4636962009-06-08 18:14:43 -07003929 case L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE:
3930 if (l4kcqe->status == 0)
3931 set_bit(SK_F_OFFLD_COMPLETE, &csk->flags);
Jeffrey Huang0cb1f4b2012-02-08 17:33:56 +00003932 else if (l4kcqe->status == L4_KCQE_COMPLETION_STATUS_NIC_ERROR)
3933 set_bit(SK_F_HW_ERR, &csk->flags);
Michael Chana4636962009-06-08 18:14:43 -07003934
3935 smp_mb__before_clear_bit();
3936 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3937 cnic_cm_upcall(cp, csk, opcode);
3938 break;
3939
3940 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
Michael Chana4636962009-06-08 18:14:43 -07003941 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3942 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan71034ba2009-10-10 13:46:59 +00003943 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3944 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
Michael Chan23021c22012-01-04 12:12:28 +00003945 if (l4kcqe->status == L4_KCQE_COMPLETION_STATUS_NIC_ERROR)
3946 set_bit(SK_F_HW_ERR, &csk->flags);
3947
Michael Chana4636962009-06-08 18:14:43 -07003948 cp->close_conn(csk, opcode);
3949 break;
3950
3951 case L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED:
Michael Chan101c40c2011-06-08 19:29:33 +00003952 /* after we already sent CLOSE_REQ */
3953 if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) &&
3954 !test_bit(SK_F_OFFLD_COMPLETE, &csk->flags) &&
3955 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3956 cp->close_conn(csk, L4_KCQE_OPCODE_VALUE_RESET_COMP);
3957 else
3958 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07003959 break;
3960 }
3961 csk_put(csk);
3962}
3963
3964static void cnic_cm_indicate_kcqe(void *data, struct kcqe *kcqe[], u32 num)
3965{
3966 struct cnic_dev *dev = data;
3967 int i;
3968
3969 for (i = 0; i < num; i++)
3970 cnic_cm_process_kcqe(dev, kcqe[i]);
3971}
3972
3973static struct cnic_ulp_ops cm_ulp_ops = {
3974 .indicate_kcqes = cnic_cm_indicate_kcqe,
3975};
3976
3977static void cnic_cm_free_mem(struct cnic_dev *dev)
3978{
3979 struct cnic_local *cp = dev->cnic_priv;
3980
3981 kfree(cp->csk_tbl);
3982 cp->csk_tbl = NULL;
3983 cnic_free_id_tbl(&cp->csk_port_tbl);
3984}
3985
3986static int cnic_cm_alloc_mem(struct cnic_dev *dev)
3987{
3988 struct cnic_local *cp = dev->cnic_priv;
Eddie Wai11f23aa2011-06-08 19:29:34 +00003989 u32 port_id;
Michael Chana4636962009-06-08 18:14:43 -07003990
3991 cp->csk_tbl = kzalloc(sizeof(struct cnic_sock) * MAX_CM_SK_TBL_SZ,
3992 GFP_KERNEL);
3993 if (!cp->csk_tbl)
3994 return -ENOMEM;
3995
Michael Chan973e5742011-07-13 17:24:17 +00003996 port_id = random32();
Eddie Wai11f23aa2011-06-08 19:29:34 +00003997 port_id %= CNIC_LOCAL_PORT_RANGE;
Michael Chana4636962009-06-08 18:14:43 -07003998 if (cnic_init_id_tbl(&cp->csk_port_tbl, CNIC_LOCAL_PORT_RANGE,
Eddie Wai11f23aa2011-06-08 19:29:34 +00003999 CNIC_LOCAL_PORT_MIN, port_id)) {
Michael Chana4636962009-06-08 18:14:43 -07004000 cnic_cm_free_mem(dev);
4001 return -ENOMEM;
4002 }
4003 return 0;
4004}
4005
4006static int cnic_ready_to_close(struct cnic_sock *csk, u32 opcode)
4007{
Michael Chan943189f2010-06-15 08:57:02 +00004008 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
4009 /* Unsolicited RESET_COMP or RESET_RECEIVED */
4010 opcode = L4_KCQE_OPCODE_VALUE_RESET_RECEIVED;
4011 csk->state = opcode;
Michael Chana1e621b2010-06-15 08:57:01 +00004012 }
Michael Chan943189f2010-06-15 08:57:02 +00004013
4014 /* 1. If event opcode matches the expected event in csk->state
Michael Chan101c40c2011-06-08 19:29:33 +00004015 * 2. If the expected event is CLOSE_COMP or RESET_COMP, we accept any
4016 * event
Michael Chan7b34a462010-06-15 08:57:03 +00004017 * 3. If the expected event is 0, meaning the connection was never
4018 * never established, we accept the opcode from cm_abort.
Michael Chan943189f2010-06-15 08:57:02 +00004019 */
Michael Chan7b34a462010-06-15 08:57:03 +00004020 if (opcode == csk->state || csk->state == 0 ||
Michael Chan101c40c2011-06-08 19:29:33 +00004021 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP ||
4022 csk->state == L4_KCQE_OPCODE_VALUE_RESET_COMP) {
Michael Chan7b34a462010-06-15 08:57:03 +00004023 if (!test_and_set_bit(SK_F_CLOSING, &csk->flags)) {
4024 if (csk->state == 0)
4025 csk->state = opcode;
Michael Chana4636962009-06-08 18:14:43 -07004026 return 1;
Michael Chan7b34a462010-06-15 08:57:03 +00004027 }
Michael Chana4636962009-06-08 18:14:43 -07004028 }
4029 return 0;
4030}
4031
4032static void cnic_close_bnx2_conn(struct cnic_sock *csk, u32 opcode)
4033{
4034 struct cnic_dev *dev = csk->dev;
4035 struct cnic_local *cp = dev->cnic_priv;
4036
Michael Chana1e621b2010-06-15 08:57:01 +00004037 if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED) {
4038 cnic_cm_upcall(cp, csk, opcode);
4039 return;
4040 }
4041
Michael Chana4636962009-06-08 18:14:43 -07004042 clear_bit(SK_F_CONNECT_START, &csk->flags);
Eddie Wai66883e92010-02-24 14:42:05 +00004043 cnic_close_conn(csk);
Michael Chan7b34a462010-06-15 08:57:03 +00004044 csk->state = opcode;
Eddie Wai66883e92010-02-24 14:42:05 +00004045 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07004046}
4047
4048static void cnic_cm_stop_bnx2_hw(struct cnic_dev *dev)
4049{
4050}
4051
4052static int cnic_cm_init_bnx2_hw(struct cnic_dev *dev)
4053{
4054 u32 seed;
4055
Michael Chan973e5742011-07-13 17:24:17 +00004056 seed = random32();
Michael Chana4636962009-06-08 18:14:43 -07004057 cnic_ctx_wr(dev, 45, 0, seed);
4058 return 0;
4059}
4060
Michael Chan71034ba2009-10-10 13:46:59 +00004061static void cnic_close_bnx2x_conn(struct cnic_sock *csk, u32 opcode)
4062{
4063 struct cnic_dev *dev = csk->dev;
4064 struct cnic_local *cp = dev->cnic_priv;
4065 struct cnic_context *ctx = &cp->ctx_tbl[csk->l5_cid];
4066 union l5cm_specific_data l5_data;
4067 u32 cmd = 0;
4068 int close_complete = 0;
4069
4070 switch (opcode) {
4071 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
4072 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
4073 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan7b34a462010-06-15 08:57:03 +00004074 if (cnic_ready_to_close(csk, opcode)) {
Michael Chan23021c22012-01-04 12:12:28 +00004075 if (test_bit(SK_F_HW_ERR, &csk->flags))
4076 close_complete = 1;
4077 else if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
Michael Chan7b34a462010-06-15 08:57:03 +00004078 cmd = L5CM_RAMROD_CMD_ID_SEARCHER_DELETE;
4079 else
4080 close_complete = 1;
4081 }
Michael Chan71034ba2009-10-10 13:46:59 +00004082 break;
4083 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
4084 cmd = L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD;
4085 break;
4086 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
4087 close_complete = 1;
4088 break;
4089 }
4090 if (cmd) {
4091 memset(&l5_data, 0, sizeof(l5_data));
4092
4093 cnic_submit_kwqe_16(dev, cmd, csk->cid, ISCSI_CONNECTION_TYPE,
4094 &l5_data);
4095 } else if (close_complete) {
4096 ctx->timestamp = jiffies;
4097 cnic_close_conn(csk);
4098 cnic_cm_upcall(cp, csk, csk->state);
4099 }
4100}
4101
4102static void cnic_cm_stop_bnx2x_hw(struct cnic_dev *dev)
4103{
Michael Chanfdf24082010-10-13 14:06:47 +00004104 struct cnic_local *cp = dev->cnic_priv;
Michael Chanfdf24082010-10-13 14:06:47 +00004105
4106 if (!cp->ctx_tbl)
4107 return;
4108
4109 if (!netif_running(dev->netdev))
4110 return;
4111
Michael Chan74e49bb2011-07-20 14:55:23 +00004112 cnic_bnx2x_delete_wait(dev, 0);
Michael Chanfdf24082010-10-13 14:06:47 +00004113
4114 cancel_delayed_work(&cp->delete_task);
4115 flush_workqueue(cnic_wq);
4116
4117 if (atomic_read(&cp->iscsi_conn) != 0)
4118 netdev_warn(dev->netdev, "%d iSCSI connections not destroyed\n",
4119 atomic_read(&cp->iscsi_conn));
Michael Chan71034ba2009-10-10 13:46:59 +00004120}
4121
4122static int cnic_cm_init_bnx2x_hw(struct cnic_dev *dev)
4123{
4124 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00004125 u32 pfid = cp->pfid;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004126 u32 port = CNIC_PORT(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00004127
4128 cnic_init_bnx2x_mac(dev);
4129 cnic_bnx2x_set_tcp_timestamp(dev, 1);
4130
4131 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004132 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(pfid), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004133
4134 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004135 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_ENABLED_OFFSET(port), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00004136 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004137 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_MAX_COUNT_OFFSET(port),
Michael Chan71034ba2009-10-10 13:46:59 +00004138 DEF_MAX_DA_COUNT);
4139
4140 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004141 XSTORM_ISCSI_TCP_VARS_TTL_OFFSET(pfid), DEF_TTL);
Michael Chan71034ba2009-10-10 13:46:59 +00004142 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004143 XSTORM_ISCSI_TCP_VARS_TOS_OFFSET(pfid), DEF_TOS);
Michael Chan71034ba2009-10-10 13:46:59 +00004144 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004145 XSTORM_ISCSI_TCP_VARS_ADV_WND_SCL_OFFSET(pfid), 2);
Michael Chan71034ba2009-10-10 13:46:59 +00004146 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004147 XSTORM_TCP_TX_SWS_TIMER_VAL_OFFSET(pfid), DEF_SWS_TIMER);
Michael Chan71034ba2009-10-10 13:46:59 +00004148
Michael Chan14203982010-10-06 03:16:06 +00004149 CNIC_WR(dev, BAR_TSTRORM_INTMEM + TSTORM_TCP_MAX_CWND_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00004150 DEF_MAX_CWND);
4151 return 0;
4152}
4153
Michael Chanfdf24082010-10-13 14:06:47 +00004154static void cnic_delete_task(struct work_struct *work)
4155{
4156 struct cnic_local *cp;
4157 struct cnic_dev *dev;
4158 u32 i;
4159 int need_resched = 0;
4160
4161 cp = container_of(work, struct cnic_local, delete_task.work);
4162 dev = cp->dev;
4163
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07004164 if (test_and_clear_bit(CNIC_LCL_FL_STOP_ISCSI, &cp->cnic_local_flags)) {
4165 struct drv_ctl_info info;
4166
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07004167 cnic_ulp_stop_one(cp, CNIC_ULP_ISCSI);
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07004168
4169 info.cmd = DRV_CTL_ISCSI_STOPPED_CMD;
4170 cp->ethdev->drv_ctl(dev->netdev, &info);
4171 }
4172
Michael Chanfdf24082010-10-13 14:06:47 +00004173 for (i = 0; i < cp->max_cid_space; i++) {
4174 struct cnic_context *ctx = &cp->ctx_tbl[i];
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004175 int err;
Michael Chanfdf24082010-10-13 14:06:47 +00004176
4177 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags) ||
4178 !test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
4179 continue;
4180
4181 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
4182 need_resched = 1;
4183 continue;
4184 }
4185
4186 if (!test_and_clear_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
4187 continue;
4188
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004189 err = cnic_bnx2x_destroy_ramrod(dev, i);
Michael Chanfdf24082010-10-13 14:06:47 +00004190
4191 cnic_free_bnx2x_conn_resc(dev, i);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004192 if (!err) {
4193 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI)
4194 atomic_dec(&cp->iscsi_conn);
Michael Chanfdf24082010-10-13 14:06:47 +00004195
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004196 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
4197 }
Michael Chanfdf24082010-10-13 14:06:47 +00004198 }
4199
4200 if (need_resched)
4201 queue_delayed_work(cnic_wq, &cp->delete_task,
4202 msecs_to_jiffies(10));
4203
4204}
4205
Michael Chana4636962009-06-08 18:14:43 -07004206static int cnic_cm_open(struct cnic_dev *dev)
4207{
4208 struct cnic_local *cp = dev->cnic_priv;
4209 int err;
4210
4211 err = cnic_cm_alloc_mem(dev);
4212 if (err)
4213 return err;
4214
4215 err = cp->start_cm(dev);
4216
4217 if (err)
4218 goto err_out;
4219
Michael Chanfdf24082010-10-13 14:06:47 +00004220 INIT_DELAYED_WORK(&cp->delete_task, cnic_delete_task);
4221
Michael Chana4636962009-06-08 18:14:43 -07004222 dev->cm_create = cnic_cm_create;
4223 dev->cm_destroy = cnic_cm_destroy;
4224 dev->cm_connect = cnic_cm_connect;
4225 dev->cm_abort = cnic_cm_abort;
4226 dev->cm_close = cnic_cm_close;
4227 dev->cm_select_dev = cnic_cm_select_dev;
4228
4229 cp->ulp_handle[CNIC_ULP_L4] = dev;
4230 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], &cm_ulp_ops);
4231 return 0;
4232
4233err_out:
4234 cnic_cm_free_mem(dev);
4235 return err;
4236}
4237
4238static int cnic_cm_shutdown(struct cnic_dev *dev)
4239{
4240 struct cnic_local *cp = dev->cnic_priv;
4241 int i;
4242
4243 cp->stop_cm(dev);
4244
4245 if (!cp->csk_tbl)
4246 return 0;
4247
4248 for (i = 0; i < MAX_CM_SK_TBL_SZ; i++) {
4249 struct cnic_sock *csk = &cp->csk_tbl[i];
4250
4251 clear_bit(SK_F_INUSE, &csk->flags);
4252 cnic_cm_cleanup(csk);
4253 }
4254 cnic_cm_free_mem(dev);
4255
4256 return 0;
4257}
4258
4259static void cnic_init_context(struct cnic_dev *dev, u32 cid)
4260{
Michael Chana4636962009-06-08 18:14:43 -07004261 u32 cid_addr;
4262 int i;
4263
Michael Chana4636962009-06-08 18:14:43 -07004264 cid_addr = GET_CID_ADDR(cid);
4265
4266 for (i = 0; i < CTX_SIZE; i += 4)
4267 cnic_ctx_wr(dev, cid_addr, i, 0);
4268}
4269
4270static int cnic_setup_5709_context(struct cnic_dev *dev, int valid)
4271{
4272 struct cnic_local *cp = dev->cnic_priv;
4273 int ret = 0, i;
4274 u32 valid_bit = valid ? BNX2_CTX_HOST_PAGE_TBL_DATA0_VALID : 0;
4275
4276 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4277 return 0;
4278
4279 for (i = 0; i < cp->ctx_blks; i++) {
4280 int j;
4281 u32 idx = cp->ctx_arr[i].cid / cp->cids_per_blk;
4282 u32 val;
4283
4284 memset(cp->ctx_arr[i].ctx, 0, BCM_PAGE_SIZE);
4285
4286 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA0,
4287 (cp->ctx_arr[i].mapping & 0xffffffff) | valid_bit);
4288 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA1,
4289 (u64) cp->ctx_arr[i].mapping >> 32);
4290 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL, idx |
4291 BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
4292 for (j = 0; j < 10; j++) {
4293
4294 val = CNIC_RD(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL);
4295 if (!(val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ))
4296 break;
4297 udelay(5);
4298 }
4299 if (val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) {
4300 ret = -EBUSY;
4301 break;
4302 }
4303 }
4304 return ret;
4305}
4306
4307static void cnic_free_irq(struct cnic_dev *dev)
4308{
4309 struct cnic_local *cp = dev->cnic_priv;
4310 struct cnic_eth_dev *ethdev = cp->ethdev;
4311
4312 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4313 cp->disable_int_sync(dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004314 tasklet_kill(&cp->cnic_irq_task);
Michael Chana4636962009-06-08 18:14:43 -07004315 free_irq(ethdev->irq_arr[0].vector, dev);
4316 }
4317}
4318
Michael Chan6e0dc642010-10-13 14:06:44 +00004319static int cnic_request_irq(struct cnic_dev *dev)
4320{
4321 struct cnic_local *cp = dev->cnic_priv;
4322 struct cnic_eth_dev *ethdev = cp->ethdev;
4323 int err;
4324
4325 err = request_irq(ethdev->irq_arr[0].vector, cnic_irq, 0, "cnic", dev);
4326 if (err)
4327 tasklet_disable(&cp->cnic_irq_task);
4328
4329 return err;
4330}
4331
Michael Chana4636962009-06-08 18:14:43 -07004332static int cnic_init_bnx2_irq(struct cnic_dev *dev)
4333{
4334 struct cnic_local *cp = dev->cnic_priv;
4335 struct cnic_eth_dev *ethdev = cp->ethdev;
4336
4337 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4338 int err, i = 0;
4339 int sblk_num = cp->status_blk_num;
4340 u32 base = ((sblk_num - 1) * BNX2_HC_SB_CONFIG_SIZE) +
4341 BNX2_HC_SB_CONFIG_1;
4342
4343 CNIC_WR(dev, base, BNX2_HC_SB_CONFIG_1_ONE_SHOT);
4344
4345 CNIC_WR(dev, base + BNX2_HC_COMP_PROD_TRIP_OFF, (2 << 16) | 8);
4346 CNIC_WR(dev, base + BNX2_HC_COM_TICKS_OFF, (64 << 16) | 220);
4347 CNIC_WR(dev, base + BNX2_HC_CMD_TICKS_OFF, (64 << 16) | 220);
4348
Michael Chana4dde3a2010-02-24 14:42:08 +00004349 cp->last_status_idx = cp->status_blk.bnx2->status_idx;
Joe Perches164165d2009-11-19 09:30:10 +00004350 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2_msix,
Michael Chana4636962009-06-08 18:14:43 -07004351 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004352 err = cnic_request_irq(dev);
4353 if (err)
Michael Chana4636962009-06-08 18:14:43 -07004354 return err;
Michael Chan6e0dc642010-10-13 14:06:44 +00004355
Michael Chana4dde3a2010-02-24 14:42:08 +00004356 while (cp->status_blk.bnx2->status_completion_producer_index &&
Michael Chana4636962009-06-08 18:14:43 -07004357 i < 10) {
4358 CNIC_WR(dev, BNX2_HC_COALESCE_NOW,
4359 1 << (11 + sblk_num));
4360 udelay(10);
4361 i++;
4362 barrier();
4363 }
Michael Chana4dde3a2010-02-24 14:42:08 +00004364 if (cp->status_blk.bnx2->status_completion_producer_index) {
Michael Chana4636962009-06-08 18:14:43 -07004365 cnic_free_irq(dev);
4366 goto failed;
4367 }
4368
4369 } else {
Michael Chana4dde3a2010-02-24 14:42:08 +00004370 struct status_block *sblk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004371 u32 hc_cmd = CNIC_RD(dev, BNX2_HC_COMMAND);
4372 int i = 0;
4373
4374 while (sblk->status_completion_producer_index && i < 10) {
4375 CNIC_WR(dev, BNX2_HC_COMMAND,
4376 hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
4377 udelay(10);
4378 i++;
4379 barrier();
4380 }
4381 if (sblk->status_completion_producer_index)
4382 goto failed;
4383
4384 }
4385 return 0;
4386
4387failed:
Joe Perchesddf79b22010-02-17 15:01:54 +00004388 netdev_err(dev->netdev, "KCQ index not resetting to 0\n");
Michael Chana4636962009-06-08 18:14:43 -07004389 return -EBUSY;
4390}
4391
4392static void cnic_enable_bnx2_int(struct cnic_dev *dev)
4393{
4394 struct cnic_local *cp = dev->cnic_priv;
4395 struct cnic_eth_dev *ethdev = cp->ethdev;
4396
4397 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4398 return;
4399
4400 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4401 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
4402}
4403
4404static void cnic_disable_bnx2_int_sync(struct cnic_dev *dev)
4405{
4406 struct cnic_local *cp = dev->cnic_priv;
4407 struct cnic_eth_dev *ethdev = cp->ethdev;
4408
4409 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4410 return;
4411
4412 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4413 BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
4414 CNIC_RD(dev, BNX2_PCICFG_INT_ACK_CMD);
4415 synchronize_irq(ethdev->irq_arr[0].vector);
4416}
4417
4418static void cnic_init_bnx2_tx_ring(struct cnic_dev *dev)
4419{
4420 struct cnic_local *cp = dev->cnic_priv;
4421 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004422 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004423 u32 cid_addr, tx_cid, sb_id;
4424 u32 val, offset0, offset1, offset2, offset3;
4425 int i;
4426 struct tx_bd *txbd;
Michael Chancd801532010-10-13 14:06:49 +00004427 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Michael Chana4dde3a2010-02-24 14:42:08 +00004428 struct status_block *s_blk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004429
4430 sb_id = cp->status_blk_num;
4431 tx_cid = 20;
Michael Chana4636962009-06-08 18:14:43 -07004432 cp->tx_cons_ptr = &s_blk->status_tx_quick_consumer_index2;
4433 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004434 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004435
4436 tx_cid = TX_TSS_CID + sb_id - 1;
Michael Chana4636962009-06-08 18:14:43 -07004437 CNIC_WR(dev, BNX2_TSCH_TSS_CFG, (sb_id << 24) |
4438 (TX_TSS_CID << 7));
4439 cp->tx_cons_ptr = &sblk->status_tx_quick_consumer_index;
4440 }
4441 cp->tx_cons = *cp->tx_cons_ptr;
4442
4443 cid_addr = GET_CID_ADDR(tx_cid);
4444 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
4445 u32 cid_addr2 = GET_CID_ADDR(tx_cid + 4) + 0x40;
4446
4447 for (i = 0; i < PHY_CTX_SIZE; i += 4)
4448 cnic_ctx_wr(dev, cid_addr2, i, 0);
4449
4450 offset0 = BNX2_L2CTX_TYPE_XI;
4451 offset1 = BNX2_L2CTX_CMD_TYPE_XI;
4452 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI_XI;
4453 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO_XI;
4454 } else {
Michael Chanb58ffb42010-05-27 16:31:41 -07004455 cnic_init_context(dev, tx_cid);
4456 cnic_init_context(dev, tx_cid + 1);
4457
Michael Chana4636962009-06-08 18:14:43 -07004458 offset0 = BNX2_L2CTX_TYPE;
4459 offset1 = BNX2_L2CTX_CMD_TYPE;
4460 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI;
4461 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO;
4462 }
4463 val = BNX2_L2CTX_TYPE_TYPE_L2 | BNX2_L2CTX_TYPE_SIZE_L2;
4464 cnic_ctx_wr(dev, cid_addr, offset0, val);
4465
4466 val = BNX2_L2CTX_CMD_TYPE_TYPE_L2 | (8 << 16);
4467 cnic_ctx_wr(dev, cid_addr, offset1, val);
4468
Joe Perches43d620c2011-06-16 19:08:06 +00004469 txbd = udev->l2_ring;
Michael Chana4636962009-06-08 18:14:43 -07004470
Michael Chancd801532010-10-13 14:06:49 +00004471 buf_map = udev->l2_buf_map;
Michael Chana4636962009-06-08 18:14:43 -07004472 for (i = 0; i < MAX_TX_DESC_CNT; i++, txbd++) {
4473 txbd->tx_bd_haddr_hi = (u64) buf_map >> 32;
4474 txbd->tx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4475 }
Michael Chancd801532010-10-13 14:06:49 +00004476 val = (u64) ring_map >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004477 cnic_ctx_wr(dev, cid_addr, offset2, val);
4478 txbd->tx_bd_haddr_hi = val;
4479
Michael Chancd801532010-10-13 14:06:49 +00004480 val = (u64) ring_map & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004481 cnic_ctx_wr(dev, cid_addr, offset3, val);
4482 txbd->tx_bd_haddr_lo = val;
4483}
4484
4485static void cnic_init_bnx2_rx_ring(struct cnic_dev *dev)
4486{
4487 struct cnic_local *cp = dev->cnic_priv;
4488 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004489 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004490 u32 cid_addr, sb_id, val, coal_reg, coal_val;
4491 int i;
4492 struct rx_bd *rxbd;
Michael Chana4dde3a2010-02-24 14:42:08 +00004493 struct status_block *s_blk = cp->status_blk.gen;
Michael Chancd801532010-10-13 14:06:49 +00004494 dma_addr_t ring_map = udev->l2_ring_map;
Michael Chana4636962009-06-08 18:14:43 -07004495
4496 sb_id = cp->status_blk_num;
4497 cnic_init_context(dev, 2);
4498 cp->rx_cons_ptr = &s_blk->status_rx_quick_consumer_index2;
4499 coal_reg = BNX2_HC_COMMAND;
4500 coal_val = CNIC_RD(dev, coal_reg);
4501 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004502 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004503
4504 cp->rx_cons_ptr = &sblk->status_rx_quick_consumer_index;
4505 coal_reg = BNX2_HC_COALESCE_NOW;
4506 coal_val = 1 << (11 + sb_id);
4507 }
4508 i = 0;
4509 while (!(*cp->rx_cons_ptr != 0) && i < 10) {
4510 CNIC_WR(dev, coal_reg, coal_val);
4511 udelay(10);
4512 i++;
4513 barrier();
4514 }
4515 cp->rx_cons = *cp->rx_cons_ptr;
4516
4517 cid_addr = GET_CID_ADDR(2);
4518 val = BNX2_L2CTX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
4519 BNX2_L2CTX_CTX_TYPE_SIZE_L2 | (0x02 << 8);
4520 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_CTX_TYPE, val);
4521
4522 if (sb_id == 0)
Michael Chand0549382009-10-28 03:41:59 -07004523 val = 2 << BNX2_L2CTX_L2_STATUSB_NUM_SHIFT;
Michael Chana4636962009-06-08 18:14:43 -07004524 else
Michael Chand0549382009-10-28 03:41:59 -07004525 val = BNX2_L2CTX_L2_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004526 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_HOST_BDIDX, val);
4527
Joe Perches43d620c2011-06-16 19:08:06 +00004528 rxbd = udev->l2_ring + BCM_PAGE_SIZE;
Michael Chana4636962009-06-08 18:14:43 -07004529 for (i = 0; i < MAX_RX_DESC_CNT; i++, rxbd++) {
4530 dma_addr_t buf_map;
4531 int n = (i % cp->l2_rx_ring_size) + 1;
4532
Michael Chancd801532010-10-13 14:06:49 +00004533 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chana4636962009-06-08 18:14:43 -07004534 rxbd->rx_bd_len = cp->l2_single_buf_size;
4535 rxbd->rx_bd_flags = RX_BD_FLAGS_START | RX_BD_FLAGS_END;
4536 rxbd->rx_bd_haddr_hi = (u64) buf_map >> 32;
4537 rxbd->rx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4538 }
Michael Chancd801532010-10-13 14:06:49 +00004539 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004540 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_HI, val);
4541 rxbd->rx_bd_haddr_hi = val;
4542
Michael Chancd801532010-10-13 14:06:49 +00004543 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004544 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_LO, val);
4545 rxbd->rx_bd_haddr_lo = val;
4546
4547 val = cnic_reg_rd_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD);
4548 cnic_reg_wr_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD, val | (1 << 2));
4549}
4550
4551static void cnic_shutdown_bnx2_rx_ring(struct cnic_dev *dev)
4552{
4553 struct kwqe *wqes[1], l2kwqe;
4554
4555 memset(&l2kwqe, 0, sizeof(l2kwqe));
4556 wqes[0] = &l2kwqe;
Michael Chane1928c82010-12-23 07:43:04 +00004557 l2kwqe.kwqe_op_flag = (L2_LAYER_CODE << KWQE_LAYER_SHIFT) |
Michael Chana4636962009-06-08 18:14:43 -07004558 (L2_KWQE_OPCODE_VALUE_FLUSH <<
4559 KWQE_OPCODE_SHIFT) | 2;
4560 dev->submit_kwqes(dev, wqes, 1);
4561}
4562
4563static void cnic_set_bnx2_mac(struct cnic_dev *dev)
4564{
4565 struct cnic_local *cp = dev->cnic_priv;
4566 u32 val;
4567
4568 val = cp->func << 2;
4569
4570 cp->shmem_base = cnic_reg_rd_ind(dev, BNX2_SHM_HDR_ADDR_0 + val);
4571
4572 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4573 BNX2_PORT_HW_CFG_ISCSI_MAC_UPPER);
4574 dev->mac_addr[0] = (u8) (val >> 8);
4575 dev->mac_addr[1] = (u8) val;
4576
4577 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH4, val);
4578
4579 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4580 BNX2_PORT_HW_CFG_ISCSI_MAC_LOWER);
4581 dev->mac_addr[2] = (u8) (val >> 24);
4582 dev->mac_addr[3] = (u8) (val >> 16);
4583 dev->mac_addr[4] = (u8) (val >> 8);
4584 dev->mac_addr[5] = (u8) val;
4585
4586 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH5, val);
4587
4588 val = 4 | BNX2_RPM_SORT_USER2_BC_EN;
4589 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4590 val |= BNX2_RPM_SORT_USER2_PROM_VLAN;
4591
4592 CNIC_WR(dev, BNX2_RPM_SORT_USER2, 0x0);
4593 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val);
4594 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val | BNX2_RPM_SORT_USER2_ENA);
4595}
4596
4597static int cnic_start_bnx2_hw(struct cnic_dev *dev)
4598{
4599 struct cnic_local *cp = dev->cnic_priv;
4600 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chana4dde3a2010-02-24 14:42:08 +00004601 struct status_block *sblk = cp->status_blk.gen;
Michael Chane6c28892010-06-24 14:58:39 +00004602 u32 val, kcq_cid_addr, kwq_cid_addr;
Michael Chana4636962009-06-08 18:14:43 -07004603 int err;
4604
4605 cnic_set_bnx2_mac(dev);
4606
4607 val = CNIC_RD(dev, BNX2_MQ_CONFIG);
4608 val &= ~BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE;
4609 if (BCM_PAGE_BITS > 12)
4610 val |= (12 - 8) << 4;
4611 else
4612 val |= (BCM_PAGE_BITS - 8) << 4;
4613
4614 CNIC_WR(dev, BNX2_MQ_CONFIG, val);
4615
4616 CNIC_WR(dev, BNX2_HC_COMP_PROD_TRIP, (2 << 16) | 8);
4617 CNIC_WR(dev, BNX2_HC_COM_TICKS, (64 << 16) | 220);
4618 CNIC_WR(dev, BNX2_HC_CMD_TICKS, (64 << 16) | 220);
4619
4620 err = cnic_setup_5709_context(dev, 1);
4621 if (err)
4622 return err;
4623
4624 cnic_init_context(dev, KWQ_CID);
4625 cnic_init_context(dev, KCQ_CID);
4626
Michael Chane6c28892010-06-24 14:58:39 +00004627 kwq_cid_addr = GET_CID_ADDR(KWQ_CID);
Michael Chana4636962009-06-08 18:14:43 -07004628 cp->kwq_io_addr = MB_GET_CID_ADDR(KWQ_CID) + L5_KRNLQ_HOST_QIDX;
4629
4630 cp->max_kwq_idx = MAX_KWQ_IDX;
4631 cp->kwq_prod_idx = 0;
4632 cp->kwq_con_idx = 0;
Michael Chan1f1332a2010-05-18 11:32:52 +00004633 set_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07004634
4635 if (CHIP_NUM(cp) == CHIP_NUM_5706 || CHIP_NUM(cp) == CHIP_NUM_5708)
4636 cp->kwq_con_idx_ptr = &sblk->status_rx_quick_consumer_index15;
4637 else
4638 cp->kwq_con_idx_ptr = &sblk->status_cmd_consumer_index;
4639
4640 /* Initialize the kernel work queue context. */
4641 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4642 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004643 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004644
4645 val = (BCM_PAGE_SIZE / sizeof(struct kwqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004646 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004647
4648 val = ((BCM_PAGE_SIZE / sizeof(struct kwqe)) << 16) | KWQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004649 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004650
4651 val = (u32) ((u64) cp->kwq_info.pgtbl_map >> 32);
Michael Chane6c28892010-06-24 14:58:39 +00004652 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004653
4654 val = (u32) cp->kwq_info.pgtbl_map;
Michael Chane6c28892010-06-24 14:58:39 +00004655 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004656
Michael Chane6c28892010-06-24 14:58:39 +00004657 kcq_cid_addr = GET_CID_ADDR(KCQ_CID);
4658 cp->kcq1.io_addr = MB_GET_CID_ADDR(KCQ_CID) + L5_KRNLQ_HOST_QIDX;
Michael Chana4636962009-06-08 18:14:43 -07004659
Michael Chane6c28892010-06-24 14:58:39 +00004660 cp->kcq1.sw_prod_idx = 0;
4661 cp->kcq1.hw_prod_idx_ptr =
4662 (u16 *) &sblk->status_completion_producer_index;
4663
4664 cp->kcq1.status_idx_ptr = (u16 *) &sblk->status_idx;
Michael Chana4636962009-06-08 18:14:43 -07004665
4666 /* Initialize the kernel complete queue context. */
4667 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4668 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004669 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004670
4671 val = (BCM_PAGE_SIZE / sizeof(struct kcqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004672 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004673
4674 val = ((BCM_PAGE_SIZE / sizeof(struct kcqe)) << 16) | KCQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004675 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004676
Michael Chane6c28892010-06-24 14:58:39 +00004677 val = (u32) ((u64) cp->kcq1.dma.pgtbl_map >> 32);
4678 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004679
Michael Chane6c28892010-06-24 14:58:39 +00004680 val = (u32) cp->kcq1.dma.pgtbl_map;
4681 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004682
4683 cp->int_num = 0;
4684 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chane6c28892010-06-24 14:58:39 +00004685 struct status_block_msix *msblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004686 u32 sb_id = cp->status_blk_num;
Michael Chand0549382009-10-28 03:41:59 -07004687 u32 sb = BNX2_L2CTX_L5_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004688
Michael Chane6c28892010-06-24 14:58:39 +00004689 cp->kcq1.hw_prod_idx_ptr =
4690 (u16 *) &msblk->status_completion_producer_index;
4691 cp->kcq1.status_idx_ptr = (u16 *) &msblk->status_idx;
Michael Chanb177a5d52010-06-24 14:58:41 +00004692 cp->kwq_con_idx_ptr = (u16 *) &msblk->status_cmd_consumer_index;
Michael Chana4636962009-06-08 18:14:43 -07004693 cp->int_num = sb_id << BNX2_PCICFG_INT_ACK_CMD_INT_NUM_SHIFT;
Michael Chane6c28892010-06-24 14:58:39 +00004694 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
4695 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
Michael Chana4636962009-06-08 18:14:43 -07004696 }
4697
4698 /* Enable Commnad Scheduler notification when we write to the
4699 * host producer index of the kernel contexts. */
4700 CNIC_WR(dev, BNX2_MQ_KNL_CMD_MASK1, 2);
4701
4702 /* Enable Command Scheduler notification when we write to either
4703 * the Send Queue or Receive Queue producer indexes of the kernel
4704 * bypass contexts. */
4705 CNIC_WR(dev, BNX2_MQ_KNL_BYP_CMD_MASK1, 7);
4706 CNIC_WR(dev, BNX2_MQ_KNL_BYP_WRITE_MASK1, 7);
4707
4708 /* Notify COM when the driver post an application buffer. */
4709 CNIC_WR(dev, BNX2_MQ_KNL_RX_V2P_MASK2, 0x2000);
4710
4711 /* Set the CP and COM doorbells. These two processors polls the
4712 * doorbell for a non zero value before running. This must be done
4713 * after setting up the kernel queue contexts. */
4714 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 1);
4715 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 1);
4716
4717 cnic_init_bnx2_tx_ring(dev);
4718 cnic_init_bnx2_rx_ring(dev);
4719
4720 err = cnic_init_bnx2_irq(dev);
4721 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00004722 netdev_err(dev->netdev, "cnic_init_irq failed\n");
Michael Chana4636962009-06-08 18:14:43 -07004723 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
4724 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
4725 return err;
4726 }
4727
4728 return 0;
4729}
4730
Michael Chan71034ba2009-10-10 13:46:59 +00004731static void cnic_setup_bnx2x_context(struct cnic_dev *dev)
4732{
4733 struct cnic_local *cp = dev->cnic_priv;
4734 struct cnic_eth_dev *ethdev = cp->ethdev;
4735 u32 start_offset = ethdev->ctx_tbl_offset;
4736 int i;
4737
4738 for (i = 0; i < cp->ctx_blks; i++) {
4739 struct cnic_ctx *ctx = &cp->ctx_arr[i];
4740 dma_addr_t map = ctx->mapping;
4741
4742 if (cp->ctx_align) {
4743 unsigned long mask = cp->ctx_align - 1;
4744
4745 map = (map + mask) & ~mask;
4746 }
4747
4748 cnic_ctx_tbl_wr(dev, start_offset + i, map);
4749 }
4750}
4751
4752static int cnic_init_bnx2x_irq(struct cnic_dev *dev)
4753{
4754 struct cnic_local *cp = dev->cnic_priv;
4755 struct cnic_eth_dev *ethdev = cp->ethdev;
4756 int err = 0;
4757
Joe Perches164165d2009-11-19 09:30:10 +00004758 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2x_bh,
Michael Chan71034ba2009-10-10 13:46:59 +00004759 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004760 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
4761 err = cnic_request_irq(dev);
4762
Michael Chan71034ba2009-10-10 13:46:59 +00004763 return err;
4764}
4765
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004766static inline void cnic_storm_memset_hc_disable(struct cnic_dev *dev,
4767 u16 sb_id, u8 sb_index,
4768 u8 disable)
4769{
4770
4771 u32 addr = BAR_CSTRORM_INTMEM +
4772 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4773 offsetof(struct hc_status_block_data_e1x, index_data) +
4774 sizeof(struct hc_index_data)*sb_index +
4775 offsetof(struct hc_index_data, flags);
4776 u16 flags = CNIC_RD16(dev, addr);
4777 /* clear and set */
4778 flags &= ~HC_INDEX_DATA_HC_ENABLED;
4779 flags |= (((~disable) << HC_INDEX_DATA_HC_ENABLED_SHIFT) &
4780 HC_INDEX_DATA_HC_ENABLED);
4781 CNIC_WR16(dev, addr, flags);
4782}
4783
Michael Chan71034ba2009-10-10 13:46:59 +00004784static void cnic_enable_bnx2x_int(struct cnic_dev *dev)
4785{
4786 struct cnic_local *cp = dev->cnic_priv;
4787 u8 sb_id = cp->status_blk_num;
Michael Chan71034ba2009-10-10 13:46:59 +00004788
4789 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004790 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4791 offsetof(struct hc_status_block_data_e1x, index_data) +
4792 sizeof(struct hc_index_data)*HC_INDEX_ISCSI_EQ_CONS +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004793 offsetof(struct hc_index_data, timeout), 64 / 4);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004794 cnic_storm_memset_hc_disable(dev, sb_id, HC_INDEX_ISCSI_EQ_CONS, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004795}
4796
4797static void cnic_disable_bnx2x_int_sync(struct cnic_dev *dev)
4798{
4799}
4800
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004801static void cnic_init_bnx2x_tx_ring(struct cnic_dev *dev,
4802 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004803{
4804 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004805 struct cnic_uio_dev *udev = cp->udev;
4806 union eth_tx_bd_types *txbd = (union eth_tx_bd_types *) udev->l2_ring;
4807 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004808 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004809 int i;
Michael Chan5159fdc2010-12-23 07:42:59 +00004810 u32 cli = cp->ethdev->iscsi_l2_client_id;
Michael Chan71034ba2009-10-10 13:46:59 +00004811 u32 val;
4812
4813 memset(txbd, 0, BCM_PAGE_SIZE);
4814
Michael Chancd801532010-10-13 14:06:49 +00004815 buf_map = udev->l2_buf_map;
Michael Chan71034ba2009-10-10 13:46:59 +00004816 for (i = 0; i < MAX_TX_DESC_CNT; i += 3, txbd += 3) {
4817 struct eth_tx_start_bd *start_bd = &txbd->start_bd;
4818 struct eth_tx_bd *reg_bd = &((txbd + 2)->reg_bd);
4819
4820 start_bd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4821 start_bd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4822 reg_bd->addr_hi = start_bd->addr_hi;
4823 reg_bd->addr_lo = start_bd->addr_lo + 0x10;
4824 start_bd->nbytes = cpu_to_le16(0x10);
4825 start_bd->nbd = cpu_to_le16(3);
4826 start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
4827 start_bd->general_data = (UNICAST_ADDRESS <<
4828 ETH_TX_START_BD_ETH_ADDR_TYPE_SHIFT);
4829 start_bd->general_data |= (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
4830
4831 }
Michael Chan71034ba2009-10-10 13:46:59 +00004832
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004833 val = (u64) ring_map >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004834 txbd->next_bd.addr_hi = cpu_to_le32(val);
4835
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004836 data->tx.tx_bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004837
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004838 val = (u64) ring_map & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004839 txbd->next_bd.addr_lo = cpu_to_le32(val);
4840
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004841 data->tx.tx_bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004842
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004843 /* Other ramrod params */
4844 data->tx.tx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_CQ_CONS;
4845 data->tx.tx_status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004846
4847 /* reset xstorm per client statistics */
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004848 if (cli < MAX_STAT_COUNTER_ID) {
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004849 data->general.statistics_zero_flg = 1;
4850 data->general.statistics_en_flg = 1;
4851 data->general.statistics_counter_id = cli;
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004852 }
Michael Chan71034ba2009-10-10 13:46:59 +00004853
4854 cp->tx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004855 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_CQ_CONS];
Michael Chan71034ba2009-10-10 13:46:59 +00004856}
4857
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004858static void cnic_init_bnx2x_rx_ring(struct cnic_dev *dev,
4859 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004860{
4861 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004862 struct cnic_uio_dev *udev = cp->udev;
4863 struct eth_rx_bd *rxbd = (struct eth_rx_bd *) (udev->l2_ring +
Michael Chan71034ba2009-10-10 13:46:59 +00004864 BCM_PAGE_SIZE);
4865 struct eth_rx_cqe_next_page *rxcqe = (struct eth_rx_cqe_next_page *)
Michael Chancd801532010-10-13 14:06:49 +00004866 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004867 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004868 int i;
Michael Chan5159fdc2010-12-23 07:42:59 +00004869 u32 cli = cp->ethdev->iscsi_l2_client_id;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004870 int cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
Michael Chan71034ba2009-10-10 13:46:59 +00004871 u32 val;
Michael Chancd801532010-10-13 14:06:49 +00004872 dma_addr_t ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004873
4874 /* General data */
4875 data->general.client_id = cli;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004876 data->general.activate_flg = 1;
4877 data->general.sp_client_id = cli;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004878 data->general.mtu = cpu_to_le16(cp->l2_single_buf_size - 14);
4879 data->general.func_id = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00004880
4881 for (i = 0; i < BNX2X_MAX_RX_DESC_CNT; i++, rxbd++) {
4882 dma_addr_t buf_map;
4883 int n = (i % cp->l2_rx_ring_size) + 1;
4884
Michael Chancd801532010-10-13 14:06:49 +00004885 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chan71034ba2009-10-10 13:46:59 +00004886 rxbd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4887 rxbd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4888 }
Michael Chan71034ba2009-10-10 13:46:59 +00004889
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004890 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004891 rxbd->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004892 data->rx.bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004893
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004894 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004895 rxbd->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004896 data->rx.bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004897
4898 rxcqe += BNX2X_MAX_RCQ_DESC_CNT;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004899 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004900 rxcqe->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004901 data->rx.cqe_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004902
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004903 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004904 rxcqe->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004905 data->rx.cqe_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004906
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004907 /* Other ramrod params */
4908 data->rx.client_qzone_id = cl_qzone_id;
4909 data->rx.rx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS;
4910 data->rx.status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004911
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004912 data->rx.cache_line_alignment_log_size = L1_CACHE_SHIFT;
Michael Chan71034ba2009-10-10 13:46:59 +00004913
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004914 data->rx.max_bytes_on_bd = cpu_to_le16(cp->l2_single_buf_size);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004915 data->rx.outer_vlan_removal_enable_flg = 1;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004916 data->rx.silent_vlan_removal_flg = 1;
4917 data->rx.silent_vlan_value = 0;
4918 data->rx.silent_vlan_mask = 0xffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004919
4920 cp->rx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004921 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS];
Michael Chan5159fdc2010-12-23 07:42:59 +00004922 cp->rx_cons = *cp->rx_cons_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00004923}
4924
Michael Chane21ba412010-12-23 07:43:03 +00004925static void cnic_init_bnx2x_kcq(struct cnic_dev *dev)
4926{
4927 struct cnic_local *cp = dev->cnic_priv;
4928 u32 pfid = cp->pfid;
4929
4930 cp->kcq1.io_addr = BAR_CSTRORM_INTMEM +
4931 CSTORM_ISCSI_EQ_PROD_OFFSET(pfid, 0);
4932 cp->kcq1.sw_prod_idx = 0;
4933
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004934 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chane21ba412010-12-23 07:43:03 +00004935 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4936
4937 cp->kcq1.hw_prod_idx_ptr =
4938 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4939 cp->kcq1.status_idx_ptr =
4940 &sb->sb.running_index[SM_RX_ID];
4941 } else {
4942 struct host_hc_status_block_e1x *sb = cp->status_blk.gen;
4943
4944 cp->kcq1.hw_prod_idx_ptr =
4945 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4946 cp->kcq1.status_idx_ptr =
4947 &sb->sb.running_index[SM_RX_ID];
4948 }
4949
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004950 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chane21ba412010-12-23 07:43:03 +00004951 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4952
4953 cp->kcq2.io_addr = BAR_USTRORM_INTMEM +
4954 USTORM_FCOE_EQ_PROD_OFFSET(pfid);
4955 cp->kcq2.sw_prod_idx = 0;
4956 cp->kcq2.hw_prod_idx_ptr =
4957 &sb->sb.index_values[HC_INDEX_FCOE_EQ_CONS];
4958 cp->kcq2.status_idx_ptr =
4959 &sb->sb.running_index[SM_RX_ID];
4960 }
4961}
4962
Michael Chan71034ba2009-10-10 13:46:59 +00004963static int cnic_start_bnx2x_hw(struct cnic_dev *dev)
4964{
4965 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004966 struct cnic_eth_dev *ethdev = cp->ethdev;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004967 int func = CNIC_FUNC(cp), ret;
Michael Chan14203982010-10-06 03:16:06 +00004968 u32 pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00004969
Michael Chana9e0a4f2012-01-04 12:12:27 +00004970 dev->stats_addr = ethdev->addr_drv_info_to_mcp;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004971 cp->port_mode = CHIP_PORT_MODE_NONE;
4972
4973 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chanee87a822010-10-13 14:06:51 +00004974 u32 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN_OVWR);
4975
4976 if (!(val & 1))
4977 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN);
4978 else
4979 val = (val >> 1) & 1;
4980
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004981 if (val) {
4982 cp->port_mode = CHIP_4_PORT_MODE;
Michael Chanee87a822010-10-13 14:06:51 +00004983 cp->pfid = func >> 1;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004984 } else {
Michael Chanb7d40312011-07-13 17:24:18 +00004985 cp->port_mode = CHIP_2_PORT_MODE;
Michael Chanee87a822010-10-13 14:06:51 +00004986 cp->pfid = func & 0x6;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004987 }
Michael Chanee87a822010-10-13 14:06:51 +00004988 } else {
4989 cp->pfid = func;
4990 }
Michael Chan14203982010-10-06 03:16:06 +00004991 pfid = cp->pfid;
4992
Michael Chan71034ba2009-10-10 13:46:59 +00004993 ret = cnic_init_id_tbl(&cp->cid_tbl, MAX_ISCSI_TBL_SZ,
Eddie Wai11f23aa2011-06-08 19:29:34 +00004994 cp->iscsi_start_cid, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004995
4996 if (ret)
4997 return -ENOMEM;
4998
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004999 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chandc219a22011-08-26 09:45:39 +00005000 ret = cnic_init_id_tbl(&cp->fcoe_cid_tbl, dev->max_fcoe_conn,
Eddie Wai11f23aa2011-06-08 19:29:34 +00005001 cp->fcoe_start_cid, 0);
Michael Chane1928c82010-12-23 07:43:04 +00005002
5003 if (ret)
5004 return -ENOMEM;
5005 }
5006
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005007 cp->bnx2x_igu_sb_id = ethdev->irq_arr[0].status_blk_num2;
5008
Michael Chane21ba412010-12-23 07:43:03 +00005009 cnic_init_bnx2x_kcq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005010
Michael Chan71034ba2009-10-10 13:46:59 +00005011 /* Only 1 EQ */
Michael Chane6c28892010-06-24 14:58:39 +00005012 CNIC_WR16(dev, cp->kcq1.io_addr, MAX_KCQ_IDX);
Michael Chan71034ba2009-10-10 13:46:59 +00005013 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005014 CSTORM_ISCSI_EQ_CONS_OFFSET(pfid, 0), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00005015 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005016 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00005017 cp->kcq1.dma.pg_map_arr[1] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00005018 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005019 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00005020 (u64) cp->kcq1.dma.pg_map_arr[1] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00005021 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005022 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00005023 cp->kcq1.dma.pg_map_arr[0] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00005024 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005025 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00005026 (u64) cp->kcq1.dma.pg_map_arr[0] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00005027 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005028 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_VALID_OFFSET(pfid, 0), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00005029 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005030 CSTORM_ISCSI_EQ_SB_NUM_OFFSET(pfid, 0), cp->status_blk_num);
Michael Chan71034ba2009-10-10 13:46:59 +00005031 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005032 CSTORM_ISCSI_EQ_SB_INDEX_OFFSET(pfid, 0),
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005033 HC_INDEX_ISCSI_EQ_CONS);
Michael Chan71034ba2009-10-10 13:46:59 +00005034
Michael Chan71034ba2009-10-10 13:46:59 +00005035 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005036 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00005037 cp->gbl_buf_info.pg_map_arr[0] & 0xffffffff);
5038 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005039 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00005040 (u64) cp->gbl_buf_info.pg_map_arr[0] >> 32);
5041
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005042 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
5043 TSTORM_ISCSI_TCP_LOCAL_ADV_WND_OFFSET(pfid), DEF_RCV_BUF);
5044
Michael Chan71034ba2009-10-10 13:46:59 +00005045 cnic_setup_bnx2x_context(dev);
5046
Michael Chan71034ba2009-10-10 13:46:59 +00005047 ret = cnic_init_bnx2x_irq(dev);
5048 if (ret)
5049 return ret;
5050
Michael Chan71034ba2009-10-10 13:46:59 +00005051 return 0;
5052}
5053
Michael Chan86b53602009-10-10 13:46:57 +00005054static void cnic_init_rings(struct cnic_dev *dev)
5055{
Michael Chan541a7812010-10-06 03:17:22 +00005056 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00005057 struct cnic_uio_dev *udev = cp->udev;
Michael Chan541a7812010-10-06 03:17:22 +00005058
5059 if (test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
5060 return;
5061
Michael Chan86b53602009-10-10 13:46:57 +00005062 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
5063 cnic_init_bnx2_tx_ring(dev);
5064 cnic_init_bnx2_rx_ring(dev);
Michael Chan541a7812010-10-06 03:17:22 +00005065 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00005066 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
Michael Chan5159fdc2010-12-23 07:42:59 +00005067 u32 cli = cp->ethdev->iscsi_l2_client_id;
5068 u32 cid = cp->ethdev->iscsi_l2_cid;
Michael Chan68d7c1a2011-01-05 15:14:13 +00005069 u32 cl_qzone_id;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005070 struct client_init_ramrod_data *data;
Michael Chan71034ba2009-10-10 13:46:59 +00005071 union l5cm_specific_data l5_data;
5072 struct ustorm_eth_rx_producers rx_prods = {0};
Michael Chane1dd8832011-07-13 17:24:19 +00005073 u32 off, i, *cid_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00005074
5075 rx_prods.bd_prod = 0;
5076 rx_prods.cqe_prod = BNX2X_MAX_RCQ_DESC_CNT;
5077 barrier();
5078
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005079 cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
5080
Michael Chanc7596b72009-12-02 15:15:35 +00005081 off = BAR_USTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005082 (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id) ?
Michael Chanee87a822010-10-13 14:06:51 +00005083 USTORM_RX_PRODS_E2_OFFSET(cl_qzone_id) :
5084 USTORM_RX_PRODS_E1X_OFFSET(CNIC_PORT(cp), cli));
Michael Chan71034ba2009-10-10 13:46:59 +00005085
5086 for (i = 0; i < sizeof(struct ustorm_eth_rx_producers) / 4; i++)
Michael Chanc7596b72009-12-02 15:15:35 +00005087 CNIC_WR(dev, off + i * 4, ((u32 *) &rx_prods)[i]);
Michael Chan71034ba2009-10-10 13:46:59 +00005088
Michael Chan48f753d2010-05-18 11:32:53 +00005089 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
5090
Michael Chancd801532010-10-13 14:06:49 +00005091 data = udev->l2_buf;
Michael Chane1dd8832011-07-13 17:24:19 +00005092 cid_ptr = udev->l2_buf + 12;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005093
5094 memset(data, 0, sizeof(*data));
5095
5096 cnic_init_bnx2x_tx_ring(dev, data);
5097 cnic_init_bnx2x_rx_ring(dev, data);
5098
Michael Chancd801532010-10-13 14:06:49 +00005099 l5_data.phy_address.lo = udev->l2_buf_map & 0xffffffff;
5100 l5_data.phy_address.hi = (u64) udev->l2_buf_map >> 32;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005101
Michael Chan541a7812010-10-06 03:17:22 +00005102 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
5103
Michael Chan71034ba2009-10-10 13:46:59 +00005104 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_CLIENT_SETUP,
Michael Chan68d7c1a2011-01-05 15:14:13 +00005105 cid, ETH_CONNECTION_TYPE, &l5_data);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005106
Michael Chan48f753d2010-05-18 11:32:53 +00005107 i = 0;
5108 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
5109 ++i < 10)
5110 msleep(1);
5111
5112 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
5113 netdev_err(dev->netdev,
5114 "iSCSI CLIENT_SETUP did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00005115 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan5159fdc2010-12-23 07:42:59 +00005116 cnic_ring_ctl(dev, cid, cli, 1);
Michael Chane1dd8832011-07-13 17:24:19 +00005117 *cid_ptr = cid;
Michael Chan86b53602009-10-10 13:46:57 +00005118 }
5119}
5120
5121static void cnic_shutdown_rings(struct cnic_dev *dev)
5122{
Michael Chan541a7812010-10-06 03:17:22 +00005123 struct cnic_local *cp = dev->cnic_priv;
Michael Chane1dd8832011-07-13 17:24:19 +00005124 struct cnic_uio_dev *udev = cp->udev;
5125 void *rx_ring;
Michael Chan541a7812010-10-06 03:17:22 +00005126
5127 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
5128 return;
5129
Michael Chan86b53602009-10-10 13:46:57 +00005130 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
5131 cnic_shutdown_bnx2_rx_ring(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005132 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
Michael Chan5159fdc2010-12-23 07:42:59 +00005133 u32 cli = cp->ethdev->iscsi_l2_client_id;
5134 u32 cid = cp->ethdev->iscsi_l2_cid;
Michael Chan8b065b62009-12-02 15:15:36 +00005135 union l5cm_specific_data l5_data;
Michael Chan48f753d2010-05-18 11:32:53 +00005136 int i;
Michael Chan71034ba2009-10-10 13:46:59 +00005137
Michael Chan5159fdc2010-12-23 07:42:59 +00005138 cnic_ring_ctl(dev, cid, cli, 0);
Michael Chan8b065b62009-12-02 15:15:36 +00005139
Michael Chan48f753d2010-05-18 11:32:53 +00005140 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
5141
Michael Chan8b065b62009-12-02 15:15:36 +00005142 l5_data.phy_address.lo = cli;
5143 l5_data.phy_address.hi = 0;
5144 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_HALT,
Michael Chan5159fdc2010-12-23 07:42:59 +00005145 cid, ETH_CONNECTION_TYPE, &l5_data);
Michael Chan48f753d2010-05-18 11:32:53 +00005146 i = 0;
5147 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
5148 ++i < 10)
5149 msleep(1);
5150
5151 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
5152 netdev_err(dev->netdev,
5153 "iSCSI CLIENT_HALT did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00005154 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan1bcdc322009-12-10 15:40:57 +00005155
5156 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005157 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
Michael Chan68d7c1a2011-01-05 15:14:13 +00005158 cid, NONE_CONNECTION_TYPE, &l5_data);
Michael Chan1bcdc322009-12-10 15:40:57 +00005159 msleep(10);
Michael Chan86b53602009-10-10 13:46:57 +00005160 }
Michael Chan541a7812010-10-06 03:17:22 +00005161 clear_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chane1dd8832011-07-13 17:24:19 +00005162 rx_ring = udev->l2_ring + BCM_PAGE_SIZE;
5163 memset(rx_ring, 0, BCM_PAGE_SIZE);
Michael Chan86b53602009-10-10 13:46:57 +00005164}
5165
Michael Chana3059b12009-08-14 15:49:44 +00005166static int cnic_register_netdev(struct cnic_dev *dev)
5167{
5168 struct cnic_local *cp = dev->cnic_priv;
5169 struct cnic_eth_dev *ethdev = cp->ethdev;
5170 int err;
5171
5172 if (!ethdev)
5173 return -ENODEV;
5174
5175 if (ethdev->drv_state & CNIC_DRV_STATE_REGD)
5176 return 0;
5177
5178 err = ethdev->drv_register_cnic(dev->netdev, cp->cnic_ops, dev);
5179 if (err)
Joe Perchesddf79b22010-02-17 15:01:54 +00005180 netdev_err(dev->netdev, "register_cnic failed\n");
Michael Chana3059b12009-08-14 15:49:44 +00005181
5182 return err;
5183}
5184
5185static void cnic_unregister_netdev(struct cnic_dev *dev)
5186{
5187 struct cnic_local *cp = dev->cnic_priv;
5188 struct cnic_eth_dev *ethdev = cp->ethdev;
5189
5190 if (!ethdev)
5191 return;
5192
5193 ethdev->drv_unregister_cnic(dev->netdev);
5194}
5195
Michael Chana4636962009-06-08 18:14:43 -07005196static int cnic_start_hw(struct cnic_dev *dev)
5197{
5198 struct cnic_local *cp = dev->cnic_priv;
5199 struct cnic_eth_dev *ethdev = cp->ethdev;
5200 int err;
5201
5202 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
5203 return -EALREADY;
5204
Michael Chana4636962009-06-08 18:14:43 -07005205 dev->regview = ethdev->io_base;
Michael Chana4636962009-06-08 18:14:43 -07005206 pci_dev_get(dev->pcidev);
5207 cp->func = PCI_FUNC(dev->pcidev->devfn);
Michael Chana4dde3a2010-02-24 14:42:08 +00005208 cp->status_blk.gen = ethdev->irq_arr[0].status_blk;
Michael Chana4636962009-06-08 18:14:43 -07005209 cp->status_blk_num = ethdev->irq_arr[0].status_blk_num;
5210
5211 err = cp->alloc_resc(dev);
5212 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00005213 netdev_err(dev->netdev, "allocate resource failure\n");
Michael Chana4636962009-06-08 18:14:43 -07005214 goto err1;
5215 }
5216
5217 err = cp->start_hw(dev);
5218 if (err)
5219 goto err1;
5220
5221 err = cnic_cm_open(dev);
5222 if (err)
5223 goto err1;
5224
5225 set_bit(CNIC_F_CNIC_UP, &dev->flags);
5226
5227 cp->enable_int(dev);
5228
5229 return 0;
5230
5231err1:
Michael Chana4636962009-06-08 18:14:43 -07005232 cp->free_resc(dev);
5233 pci_dev_put(dev->pcidev);
Michael Chana4636962009-06-08 18:14:43 -07005234 return err;
5235}
5236
5237static void cnic_stop_bnx2_hw(struct cnic_dev *dev)
5238{
Michael Chana4636962009-06-08 18:14:43 -07005239 cnic_disable_bnx2_int_sync(dev);
5240
5241 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
5242 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
5243
5244 cnic_init_context(dev, KWQ_CID);
5245 cnic_init_context(dev, KCQ_CID);
5246
5247 cnic_setup_5709_context(dev, 0);
5248 cnic_free_irq(dev);
5249
Michael Chana4636962009-06-08 18:14:43 -07005250 cnic_free_resc(dev);
5251}
5252
Michael Chan71034ba2009-10-10 13:46:59 +00005253
5254static void cnic_stop_bnx2x_hw(struct cnic_dev *dev)
5255{
5256 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00005257
5258 cnic_free_irq(dev);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005259 *cp->kcq1.hw_prod_idx_ptr = 0;
Michael Chan4e9c4fd2009-12-10 15:40:58 +00005260 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005261 CSTORM_ISCSI_EQ_CONS_OFFSET(cp->pfid, 0), 0);
Michael Chane6c28892010-06-24 14:58:39 +00005262 CNIC_WR16(dev, cp->kcq1.io_addr, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00005263 cnic_free_resc(dev);
5264}
5265
Michael Chana4636962009-06-08 18:14:43 -07005266static void cnic_stop_hw(struct cnic_dev *dev)
5267{
5268 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
5269 struct cnic_local *cp = dev->cnic_priv;
Michael Chan48f753d2010-05-18 11:32:53 +00005270 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -07005271
Michael Chan48f753d2010-05-18 11:32:53 +00005272 /* Need to wait for the ring shutdown event to complete
5273 * before clearing the CNIC_UP flag.
5274 */
Michael Chancd801532010-10-13 14:06:49 +00005275 while (cp->udev->uio_dev != -1 && i < 15) {
Michael Chan48f753d2010-05-18 11:32:53 +00005276 msleep(100);
5277 i++;
5278 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005279 cnic_shutdown_rings(dev);
Michael Chana4636962009-06-08 18:14:43 -07005280 clear_bit(CNIC_F_CNIC_UP, &dev->flags);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +00005281 RCU_INIT_POINTER(cp->ulp_ops[CNIC_ULP_L4], NULL);
Michael Chana4636962009-06-08 18:14:43 -07005282 synchronize_rcu();
5283 cnic_cm_shutdown(dev);
5284 cp->stop_hw(dev);
5285 pci_dev_put(dev->pcidev);
5286 }
5287}
5288
5289static void cnic_free_dev(struct cnic_dev *dev)
5290{
5291 int i = 0;
5292
5293 while ((atomic_read(&dev->ref_count) != 0) && i < 10) {
5294 msleep(100);
5295 i++;
5296 }
5297 if (atomic_read(&dev->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +00005298 netdev_err(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -07005299
Joe Perchesddf79b22010-02-17 15:01:54 +00005300 netdev_info(dev->netdev, "Removed CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07005301 dev_put(dev->netdev);
5302 kfree(dev);
5303}
5304
5305static struct cnic_dev *cnic_alloc_dev(struct net_device *dev,
5306 struct pci_dev *pdev)
5307{
5308 struct cnic_dev *cdev;
5309 struct cnic_local *cp;
5310 int alloc_size;
5311
5312 alloc_size = sizeof(struct cnic_dev) + sizeof(struct cnic_local);
5313
5314 cdev = kzalloc(alloc_size , GFP_KERNEL);
5315 if (cdev == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +00005316 netdev_err(dev, "allocate dev struct failure\n");
Michael Chana4636962009-06-08 18:14:43 -07005317 return NULL;
5318 }
5319
5320 cdev->netdev = dev;
5321 cdev->cnic_priv = (char *)cdev + sizeof(struct cnic_dev);
5322 cdev->register_device = cnic_register_device;
5323 cdev->unregister_device = cnic_unregister_device;
5324 cdev->iscsi_nl_msg_recv = cnic_iscsi_nl_msg_recv;
5325
5326 cp = cdev->cnic_priv;
5327 cp->dev = cdev;
Michael Chana4636962009-06-08 18:14:43 -07005328 cp->l2_single_buf_size = 0x400;
5329 cp->l2_rx_ring_size = 3;
5330
5331 spin_lock_init(&cp->cnic_ulp_lock);
5332
Joe Perchesddf79b22010-02-17 15:01:54 +00005333 netdev_info(dev, "Added CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07005334
5335 return cdev;
5336}
5337
5338static struct cnic_dev *init_bnx2_cnic(struct net_device *dev)
5339{
5340 struct pci_dev *pdev;
5341 struct cnic_dev *cdev;
5342 struct cnic_local *cp;
5343 struct cnic_eth_dev *ethdev = NULL;
Michael Chane2ee3612009-06-13 17:43:02 -07005344 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
Michael Chana4636962009-06-08 18:14:43 -07005345
Michael Chane2ee3612009-06-13 17:43:02 -07005346 probe = symbol_get(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07005347 if (probe) {
5348 ethdev = (*probe)(dev);
Michael Chan64c64602009-08-14 15:49:43 +00005349 symbol_put(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07005350 }
5351 if (!ethdev)
5352 return NULL;
5353
5354 pdev = ethdev->pdev;
5355 if (!pdev)
5356 return NULL;
5357
5358 dev_hold(dev);
5359 pci_dev_get(pdev);
Sergei Shtylyovff938e42011-02-28 11:57:33 -08005360 if ((pdev->device == PCI_DEVICE_ID_NX2_5709 ||
5361 pdev->device == PCI_DEVICE_ID_NX2_5709S) &&
5362 (pdev->revision < 0x10)) {
5363 pci_dev_put(pdev);
5364 goto cnic_err;
Michael Chana4636962009-06-08 18:14:43 -07005365 }
5366 pci_dev_put(pdev);
5367
5368 cdev = cnic_alloc_dev(dev, pdev);
5369 if (cdev == NULL)
5370 goto cnic_err;
5371
5372 set_bit(CNIC_F_BNX2_CLASS, &cdev->flags);
5373 cdev->submit_kwqes = cnic_submit_bnx2_kwqes;
5374
5375 cp = cdev->cnic_priv;
5376 cp->ethdev = ethdev;
5377 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00005378 cp->chip_id = ethdev->chip_id;
Michael Chana4636962009-06-08 18:14:43 -07005379
Michael Chan7625eb22011-06-08 19:29:36 +00005380 cdev->max_iscsi_conn = ethdev->max_iscsi_conn;
5381
Michael Chana4636962009-06-08 18:14:43 -07005382 cp->cnic_ops = &cnic_bnx2_ops;
5383 cp->start_hw = cnic_start_bnx2_hw;
5384 cp->stop_hw = cnic_stop_bnx2_hw;
5385 cp->setup_pgtbl = cnic_setup_page_tbl;
5386 cp->alloc_resc = cnic_alloc_bnx2_resc;
5387 cp->free_resc = cnic_free_resc;
5388 cp->start_cm = cnic_cm_init_bnx2_hw;
5389 cp->stop_cm = cnic_cm_stop_bnx2_hw;
5390 cp->enable_int = cnic_enable_bnx2_int;
5391 cp->disable_int_sync = cnic_disable_bnx2_int_sync;
5392 cp->close_conn = cnic_close_bnx2_conn;
Michael Chana4636962009-06-08 18:14:43 -07005393 return cdev;
5394
5395cnic_err:
5396 dev_put(dev);
5397 return NULL;
5398}
5399
Michael Chan71034ba2009-10-10 13:46:59 +00005400static struct cnic_dev *init_bnx2x_cnic(struct net_device *dev)
5401{
5402 struct pci_dev *pdev;
5403 struct cnic_dev *cdev;
5404 struct cnic_local *cp;
5405 struct cnic_eth_dev *ethdev = NULL;
5406 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
5407
5408 probe = symbol_get(bnx2x_cnic_probe);
5409 if (probe) {
5410 ethdev = (*probe)(dev);
5411 symbol_put(bnx2x_cnic_probe);
5412 }
5413 if (!ethdev)
5414 return NULL;
5415
5416 pdev = ethdev->pdev;
5417 if (!pdev)
5418 return NULL;
5419
5420 dev_hold(dev);
5421 cdev = cnic_alloc_dev(dev, pdev);
5422 if (cdev == NULL) {
5423 dev_put(dev);
5424 return NULL;
5425 }
5426
5427 set_bit(CNIC_F_BNX2X_CLASS, &cdev->flags);
5428 cdev->submit_kwqes = cnic_submit_bnx2x_kwqes;
5429
5430 cp = cdev->cnic_priv;
5431 cp->ethdev = ethdev;
5432 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00005433 cp->chip_id = ethdev->chip_id;
Michael Chan71034ba2009-10-10 13:46:59 +00005434
Barak Witkowski1d187b32011-12-05 22:41:50 +00005435 cdev->stats_addr = ethdev->addr_drv_info_to_mcp;
5436
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00005437 if (!(ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI))
5438 cdev->max_iscsi_conn = ethdev->max_iscsi_conn;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005439 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id) &&
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00005440 !(ethdev->drv_state & CNIC_DRV_STATE_NO_FCOE))
5441 cdev->max_fcoe_conn = ethdev->max_fcoe_conn;
5442
Michael Chandc219a22011-08-26 09:45:39 +00005443 if (cdev->max_fcoe_conn > BNX2X_FCOE_NUM_CONNECTIONS)
5444 cdev->max_fcoe_conn = BNX2X_FCOE_NUM_CONNECTIONS;
5445
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00005446 memcpy(cdev->mac_addr, ethdev->iscsi_mac, 6);
5447
Michael Chan71034ba2009-10-10 13:46:59 +00005448 cp->cnic_ops = &cnic_bnx2x_ops;
5449 cp->start_hw = cnic_start_bnx2x_hw;
5450 cp->stop_hw = cnic_stop_bnx2x_hw;
5451 cp->setup_pgtbl = cnic_setup_page_tbl_le;
5452 cp->alloc_resc = cnic_alloc_bnx2x_resc;
5453 cp->free_resc = cnic_free_resc;
5454 cp->start_cm = cnic_cm_init_bnx2x_hw;
5455 cp->stop_cm = cnic_cm_stop_bnx2x_hw;
5456 cp->enable_int = cnic_enable_bnx2x_int;
5457 cp->disable_int_sync = cnic_disable_bnx2x_int_sync;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005458 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id))
Michael Chanee87a822010-10-13 14:06:51 +00005459 cp->ack_int = cnic_ack_bnx2x_e2_msix;
5460 else
5461 cp->ack_int = cnic_ack_bnx2x_msix;
Michael Chan71034ba2009-10-10 13:46:59 +00005462 cp->close_conn = cnic_close_bnx2x_conn;
Michael Chan71034ba2009-10-10 13:46:59 +00005463 return cdev;
5464}
5465
Michael Chana4636962009-06-08 18:14:43 -07005466static struct cnic_dev *is_cnic_dev(struct net_device *dev)
5467{
5468 struct ethtool_drvinfo drvinfo;
5469 struct cnic_dev *cdev = NULL;
5470
5471 if (dev->ethtool_ops && dev->ethtool_ops->get_drvinfo) {
5472 memset(&drvinfo, 0, sizeof(drvinfo));
5473 dev->ethtool_ops->get_drvinfo(dev, &drvinfo);
5474
5475 if (!strcmp(drvinfo.driver, "bnx2"))
5476 cdev = init_bnx2_cnic(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005477 if (!strcmp(drvinfo.driver, "bnx2x"))
5478 cdev = init_bnx2x_cnic(dev);
Michael Chana4636962009-06-08 18:14:43 -07005479 if (cdev) {
5480 write_lock(&cnic_dev_lock);
5481 list_add(&cdev->list, &cnic_dev_list);
5482 write_unlock(&cnic_dev_lock);
5483 }
5484 }
5485 return cdev;
5486}
5487
Michael Chan415199f2011-07-20 14:55:24 +00005488static void cnic_rcv_netevent(struct cnic_local *cp, unsigned long event,
5489 u16 vlan_id)
5490{
5491 int if_type;
5492
5493 rcu_read_lock();
5494 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
5495 struct cnic_ulp_ops *ulp_ops;
5496 void *ctx;
5497
5498 ulp_ops = rcu_dereference(cp->ulp_ops[if_type]);
5499 if (!ulp_ops || !ulp_ops->indicate_netevent)
5500 continue;
5501
5502 ctx = cp->ulp_handle[if_type];
5503
5504 ulp_ops->indicate_netevent(ctx, event, vlan_id);
5505 }
5506 rcu_read_unlock();
5507}
5508
Michael Chana4636962009-06-08 18:14:43 -07005509/**
5510 * netdev event handler
5511 */
5512static int cnic_netdev_event(struct notifier_block *this, unsigned long event,
5513 void *ptr)
5514{
5515 struct net_device *netdev = ptr;
5516 struct cnic_dev *dev;
Michael Chana4636962009-06-08 18:14:43 -07005517 int new_dev = 0;
5518
5519 dev = cnic_from_netdev(netdev);
5520
Michael Chandb1d3502011-06-08 19:29:35 +00005521 if (!dev && (event == NETDEV_REGISTER || netif_running(netdev))) {
Michael Chana4636962009-06-08 18:14:43 -07005522 /* Check for the hot-plug device */
5523 dev = is_cnic_dev(netdev);
5524 if (dev) {
5525 new_dev = 1;
5526 cnic_hold(dev);
5527 }
5528 }
5529 if (dev) {
5530 struct cnic_local *cp = dev->cnic_priv;
5531
5532 if (new_dev)
5533 cnic_ulp_init(dev);
5534 else if (event == NETDEV_UNREGISTER)
5535 cnic_ulp_exit(dev);
Michael Chan6053bbf2009-10-02 11:03:28 -07005536
Michael Chandb1d3502011-06-08 19:29:35 +00005537 if (event == NETDEV_UP || (new_dev && netif_running(netdev))) {
Michael Chana3059b12009-08-14 15:49:44 +00005538 if (cnic_register_netdev(dev) != 0) {
5539 cnic_put(dev);
5540 goto done;
5541 }
Michael Chana4636962009-06-08 18:14:43 -07005542 if (!cnic_start_hw(dev))
5543 cnic_ulp_start(dev);
Michael Chana4636962009-06-08 18:14:43 -07005544 }
5545
Michael Chan415199f2011-07-20 14:55:24 +00005546 cnic_rcv_netevent(cp, event, 0);
Michael Chana4636962009-06-08 18:14:43 -07005547
5548 if (event == NETDEV_GOING_DOWN) {
Michael Chana4636962009-06-08 18:14:43 -07005549 cnic_ulp_stop(dev);
5550 cnic_stop_hw(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005551 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005552 } else if (event == NETDEV_UNREGISTER) {
5553 write_lock(&cnic_dev_lock);
5554 list_del_init(&dev->list);
5555 write_unlock(&cnic_dev_lock);
5556
5557 cnic_put(dev);
5558 cnic_free_dev(dev);
5559 goto done;
5560 }
5561 cnic_put(dev);
Michael Chan415199f2011-07-20 14:55:24 +00005562 } else {
5563 struct net_device *realdev;
5564 u16 vid;
5565
5566 vid = cnic_get_vlan(netdev, &realdev);
5567 if (realdev) {
5568 dev = cnic_from_netdev(realdev);
5569 if (dev) {
5570 vid |= VLAN_TAG_PRESENT;
5571 cnic_rcv_netevent(dev->cnic_priv, event, vid);
5572 cnic_put(dev);
5573 }
5574 }
Michael Chana4636962009-06-08 18:14:43 -07005575 }
5576done:
5577 return NOTIFY_DONE;
5578}
5579
5580static struct notifier_block cnic_netdev_notifier = {
5581 .notifier_call = cnic_netdev_event
5582};
5583
5584static void cnic_release(void)
5585{
5586 struct cnic_dev *dev;
Michael Chana3ceeeb2010-10-13 14:06:50 +00005587 struct cnic_uio_dev *udev;
Michael Chana4636962009-06-08 18:14:43 -07005588
5589 while (!list_empty(&cnic_dev_list)) {
5590 dev = list_entry(cnic_dev_list.next, struct cnic_dev, list);
5591 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
5592 cnic_ulp_stop(dev);
5593 cnic_stop_hw(dev);
5594 }
5595
5596 cnic_ulp_exit(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005597 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005598 list_del_init(&dev->list);
5599 cnic_free_dev(dev);
5600 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005601 while (!list_empty(&cnic_udev_list)) {
5602 udev = list_entry(cnic_udev_list.next, struct cnic_uio_dev,
5603 list);
5604 cnic_free_uio(udev);
5605 }
Michael Chana4636962009-06-08 18:14:43 -07005606}
5607
5608static int __init cnic_init(void)
5609{
5610 int rc = 0;
5611
Joe Perchesddf79b22010-02-17 15:01:54 +00005612 pr_info("%s", version);
Michael Chana4636962009-06-08 18:14:43 -07005613
5614 rc = register_netdevice_notifier(&cnic_netdev_notifier);
5615 if (rc) {
5616 cnic_release();
5617 return rc;
5618 }
5619
Michael Chanfdf24082010-10-13 14:06:47 +00005620 cnic_wq = create_singlethread_workqueue("cnic_wq");
5621 if (!cnic_wq) {
5622 cnic_release();
5623 unregister_netdevice_notifier(&cnic_netdev_notifier);
5624 return -ENOMEM;
5625 }
5626
Michael Chana4636962009-06-08 18:14:43 -07005627 return 0;
5628}
5629
5630static void __exit cnic_exit(void)
5631{
5632 unregister_netdevice_notifier(&cnic_netdev_notifier);
5633 cnic_release();
Michael Chanfdf24082010-10-13 14:06:47 +00005634 destroy_workqueue(cnic_wq);
Michael Chana4636962009-06-08 18:14:43 -07005635}
5636
5637module_init(cnic_init);
5638module_exit(cnic_exit);