blob: 0e9be2ba924e8c68e194022791814e0957047359 [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 Chan8ec3e702012-03-21 15:38:34 +000050#include "../../../scsi/bnx2fc/bnx2fc_constants.h"
Michael Chana4636962009-06-08 18:14:43 -070051#include "cnic.h"
52#include "cnic_defs.h"
53
54#define DRV_MODULE_NAME "cnic"
Michael Chana4636962009-06-08 18:14:43 -070055
56static char version[] __devinitdata =
57 "Broadcom NetXtreme II CNIC Driver " DRV_MODULE_NAME " v" CNIC_MODULE_VERSION " (" CNIC_MODULE_RELDATE ")\n";
58
59MODULE_AUTHOR("Michael Chan <mchan@broadcom.com> and John(Zongxi) "
60 "Chen (zongxi@broadcom.com");
61MODULE_DESCRIPTION("Broadcom NetXtreme II CNIC Driver");
62MODULE_LICENSE("GPL");
63MODULE_VERSION(CNIC_MODULE_VERSION);
64
Michael Chan8adc92402010-12-23 07:42:57 +000065/* cnic_dev_list modifications are protected by both rtnl and cnic_dev_lock */
Michael Chana4636962009-06-08 18:14:43 -070066static LIST_HEAD(cnic_dev_list);
Michael Chana3ceeeb2010-10-13 14:06:50 +000067static LIST_HEAD(cnic_udev_list);
Michael Chana4636962009-06-08 18:14:43 -070068static DEFINE_RWLOCK(cnic_dev_lock);
69static DEFINE_MUTEX(cnic_lock);
70
Eric Dumazet13707f92011-01-26 19:28:23 +000071static struct cnic_ulp_ops __rcu *cnic_ulp_tbl[MAX_CNIC_ULP_TYPE];
72
73/* helper function, assuming cnic_lock is held */
74static inline struct cnic_ulp_ops *cnic_ulp_tbl_prot(int type)
75{
76 return rcu_dereference_protected(cnic_ulp_tbl[type],
77 lockdep_is_held(&cnic_lock));
78}
Michael Chana4636962009-06-08 18:14:43 -070079
80static int cnic_service_bnx2(void *, void *);
Michael Chan71034ba2009-10-10 13:46:59 +000081static int cnic_service_bnx2x(void *, void *);
Michael Chana4636962009-06-08 18:14:43 -070082static int cnic_ctl(void *, struct cnic_ctl_info *);
83
84static struct cnic_ops cnic_bnx2_ops = {
85 .cnic_owner = THIS_MODULE,
86 .cnic_handler = cnic_service_bnx2,
87 .cnic_ctl = cnic_ctl,
88};
89
Michael Chan71034ba2009-10-10 13:46:59 +000090static struct cnic_ops cnic_bnx2x_ops = {
91 .cnic_owner = THIS_MODULE,
92 .cnic_handler = cnic_service_bnx2x,
93 .cnic_ctl = cnic_ctl,
94};
95
Michael Chanfdf24082010-10-13 14:06:47 +000096static struct workqueue_struct *cnic_wq;
97
Michael Chan86b53602009-10-10 13:46:57 +000098static void cnic_shutdown_rings(struct cnic_dev *);
99static void cnic_init_rings(struct cnic_dev *);
Michael Chana4636962009-06-08 18:14:43 -0700100static int cnic_cm_set_pg(struct cnic_sock *);
101
102static int cnic_uio_open(struct uio_info *uinfo, struct inode *inode)
103{
Michael Chancd801532010-10-13 14:06:49 +0000104 struct cnic_uio_dev *udev = uinfo->priv;
105 struct cnic_dev *dev;
Michael Chana4636962009-06-08 18:14:43 -0700106
107 if (!capable(CAP_NET_ADMIN))
108 return -EPERM;
109
Michael Chancd801532010-10-13 14:06:49 +0000110 if (udev->uio_dev != -1)
Michael Chana4636962009-06-08 18:14:43 -0700111 return -EBUSY;
112
Michael Chan86b53602009-10-10 13:46:57 +0000113 rtnl_lock();
Michael Chancd801532010-10-13 14:06:49 +0000114 dev = udev->dev;
115
Michael Chana3ceeeb2010-10-13 14:06:50 +0000116 if (!dev || !test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
Michael Chan86b53602009-10-10 13:46:57 +0000117 rtnl_unlock();
118 return -ENODEV;
119 }
120
Michael Chancd801532010-10-13 14:06:49 +0000121 udev->uio_dev = iminor(inode);
Michael Chana4636962009-06-08 18:14:43 -0700122
Michael Chana3ceeeb2010-10-13 14:06:50 +0000123 cnic_shutdown_rings(dev);
Michael Chan86b53602009-10-10 13:46:57 +0000124 cnic_init_rings(dev);
125 rtnl_unlock();
Michael Chana4636962009-06-08 18:14:43 -0700126
127 return 0;
128}
129
130static int cnic_uio_close(struct uio_info *uinfo, struct inode *inode)
131{
Michael Chancd801532010-10-13 14:06:49 +0000132 struct cnic_uio_dev *udev = uinfo->priv;
Michael Chan6ef57a02009-09-21 15:39:37 +0000133
Michael Chancd801532010-10-13 14:06:49 +0000134 udev->uio_dev = -1;
Michael Chana4636962009-06-08 18:14:43 -0700135 return 0;
136}
137
138static inline void cnic_hold(struct cnic_dev *dev)
139{
140 atomic_inc(&dev->ref_count);
141}
142
143static inline void cnic_put(struct cnic_dev *dev)
144{
145 atomic_dec(&dev->ref_count);
146}
147
148static inline void csk_hold(struct cnic_sock *csk)
149{
150 atomic_inc(&csk->ref_count);
151}
152
153static inline void csk_put(struct cnic_sock *csk)
154{
155 atomic_dec(&csk->ref_count);
156}
157
158static struct cnic_dev *cnic_from_netdev(struct net_device *netdev)
159{
160 struct cnic_dev *cdev;
161
162 read_lock(&cnic_dev_lock);
163 list_for_each_entry(cdev, &cnic_dev_list, list) {
164 if (netdev == cdev->netdev) {
165 cnic_hold(cdev);
166 read_unlock(&cnic_dev_lock);
167 return cdev;
168 }
169 }
170 read_unlock(&cnic_dev_lock);
171 return NULL;
172}
173
Michael Chan7fc1ece2009-08-14 15:49:47 +0000174static inline void ulp_get(struct cnic_ulp_ops *ulp_ops)
175{
176 atomic_inc(&ulp_ops->ref_count);
177}
178
179static inline void ulp_put(struct cnic_ulp_ops *ulp_ops)
180{
181 atomic_dec(&ulp_ops->ref_count);
182}
183
Michael Chana4636962009-06-08 18:14:43 -0700184static void cnic_ctx_wr(struct cnic_dev *dev, u32 cid_addr, u32 off, u32 val)
185{
186 struct cnic_local *cp = dev->cnic_priv;
187 struct cnic_eth_dev *ethdev = cp->ethdev;
188 struct drv_ctl_info info;
189 struct drv_ctl_io *io = &info.data.io;
190
191 info.cmd = DRV_CTL_CTX_WR_CMD;
192 io->cid_addr = cid_addr;
193 io->offset = off;
194 io->data = val;
195 ethdev->drv_ctl(dev->netdev, &info);
196}
197
Michael Chan71034ba2009-10-10 13:46:59 +0000198static void cnic_ctx_tbl_wr(struct cnic_dev *dev, u32 off, dma_addr_t addr)
199{
200 struct cnic_local *cp = dev->cnic_priv;
201 struct cnic_eth_dev *ethdev = cp->ethdev;
202 struct drv_ctl_info info;
203 struct drv_ctl_io *io = &info.data.io;
204
205 info.cmd = DRV_CTL_CTXTBL_WR_CMD;
206 io->offset = off;
207 io->dma_addr = addr;
208 ethdev->drv_ctl(dev->netdev, &info);
209}
210
211static void cnic_ring_ctl(struct cnic_dev *dev, u32 cid, u32 cl_id, int start)
212{
213 struct cnic_local *cp = dev->cnic_priv;
214 struct cnic_eth_dev *ethdev = cp->ethdev;
215 struct drv_ctl_info info;
216 struct drv_ctl_l2_ring *ring = &info.data.ring;
217
218 if (start)
219 info.cmd = DRV_CTL_START_L2_CMD;
220 else
221 info.cmd = DRV_CTL_STOP_L2_CMD;
222
223 ring->cid = cid;
224 ring->client_id = cl_id;
225 ethdev->drv_ctl(dev->netdev, &info);
226}
227
Michael Chana4636962009-06-08 18:14:43 -0700228static void cnic_reg_wr_ind(struct cnic_dev *dev, u32 off, u32 val)
229{
230 struct cnic_local *cp = dev->cnic_priv;
231 struct cnic_eth_dev *ethdev = cp->ethdev;
232 struct drv_ctl_info info;
233 struct drv_ctl_io *io = &info.data.io;
234
235 info.cmd = DRV_CTL_IO_WR_CMD;
236 io->offset = off;
237 io->data = val;
238 ethdev->drv_ctl(dev->netdev, &info);
239}
240
241static u32 cnic_reg_rd_ind(struct cnic_dev *dev, u32 off)
242{
243 struct cnic_local *cp = dev->cnic_priv;
244 struct cnic_eth_dev *ethdev = cp->ethdev;
245 struct drv_ctl_info info;
246 struct drv_ctl_io *io = &info.data.io;
247
248 info.cmd = DRV_CTL_IO_RD_CMD;
249 io->offset = off;
250 ethdev->drv_ctl(dev->netdev, &info);
251 return io->data;
252}
253
Barak Witkowski1d187b32011-12-05 22:41:50 +0000254static void cnic_ulp_ctl(struct cnic_dev *dev, int ulp_type, bool reg)
255{
256 struct cnic_local *cp = dev->cnic_priv;
257 struct cnic_eth_dev *ethdev = cp->ethdev;
258 struct drv_ctl_info info;
Barak Witkowski2e499d32012-06-26 01:31:19 +0000259 struct fcoe_capabilities *fcoe_cap =
260 &info.data.register_data.fcoe_features;
Barak Witkowski1d187b32011-12-05 22:41:50 +0000261
Barak Witkowski2e499d32012-06-26 01:31:19 +0000262 if (reg) {
Barak Witkowski1d187b32011-12-05 22:41:50 +0000263 info.cmd = DRV_CTL_ULP_REGISTER_CMD;
Barak Witkowski2e499d32012-06-26 01:31:19 +0000264 if (ulp_type == CNIC_ULP_FCOE && dev->fcoe_cap)
265 memcpy(fcoe_cap, dev->fcoe_cap, sizeof(*fcoe_cap));
266 } else {
Barak Witkowski1d187b32011-12-05 22:41:50 +0000267 info.cmd = DRV_CTL_ULP_UNREGISTER_CMD;
Barak Witkowski2e499d32012-06-26 01:31:19 +0000268 }
Barak Witkowski1d187b32011-12-05 22:41:50 +0000269
270 info.data.ulp_type = ulp_type;
271 ethdev->drv_ctl(dev->netdev, &info);
272}
273
Michael Chana4636962009-06-08 18:14:43 -0700274static int cnic_in_use(struct cnic_sock *csk)
275{
276 return test_bit(SK_F_INUSE, &csk->flags);
277}
278
Dmitry Kravkovc2bff632010-10-06 03:33:18 +0000279static void cnic_spq_completion(struct cnic_dev *dev, int cmd, u32 count)
Michael Chana4636962009-06-08 18:14:43 -0700280{
281 struct cnic_local *cp = dev->cnic_priv;
282 struct cnic_eth_dev *ethdev = cp->ethdev;
283 struct drv_ctl_info info;
284
Dmitry Kravkovc2bff632010-10-06 03:33:18 +0000285 info.cmd = cmd;
286 info.data.credit.credit_count = count;
Michael Chana4636962009-06-08 18:14:43 -0700287 ethdev->drv_ctl(dev->netdev, &info);
288}
289
Michael Chan71034ba2009-10-10 13:46:59 +0000290static int cnic_get_l5_cid(struct cnic_local *cp, u32 cid, u32 *l5_cid)
291{
292 u32 i;
293
Michael Chan520efdf2010-06-24 14:58:37 +0000294 for (i = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +0000295 if (cp->ctx_tbl[i].cid == cid) {
296 *l5_cid = i;
297 return 0;
298 }
299 }
300 return -EINVAL;
301}
302
Michael Chana4636962009-06-08 18:14:43 -0700303static int cnic_send_nlmsg(struct cnic_local *cp, u32 type,
304 struct cnic_sock *csk)
305{
306 struct iscsi_path path_req;
307 char *buf = NULL;
308 u16 len = 0;
309 u32 msg_type = ISCSI_KEVENT_IF_DOWN;
310 struct cnic_ulp_ops *ulp_ops;
Michael Chancd801532010-10-13 14:06:49 +0000311 struct cnic_uio_dev *udev = cp->udev;
Michael Chan939b82e2010-12-23 07:42:58 +0000312 int rc = 0, retry = 0;
Michael Chana4636962009-06-08 18:14:43 -0700313
Michael Chancd801532010-10-13 14:06:49 +0000314 if (!udev || udev->uio_dev == -1)
Michael Chana4636962009-06-08 18:14:43 -0700315 return -ENODEV;
316
317 if (csk) {
318 len = sizeof(path_req);
319 buf = (char *) &path_req;
320 memset(&path_req, 0, len);
321
322 msg_type = ISCSI_KEVENT_PATH_REQ;
323 path_req.handle = (u64) csk->l5_cid;
324 if (test_bit(SK_F_IPV6, &csk->flags)) {
325 memcpy(&path_req.dst.v6_addr, &csk->dst_ip[0],
326 sizeof(struct in6_addr));
327 path_req.ip_addr_len = 16;
328 } else {
329 memcpy(&path_req.dst.v4_addr, &csk->dst_ip[0],
330 sizeof(struct in_addr));
331 path_req.ip_addr_len = 4;
332 }
333 path_req.vlan_id = csk->vlan_id;
334 path_req.pmtu = csk->mtu;
335 }
336
Michael Chan939b82e2010-12-23 07:42:58 +0000337 while (retry < 3) {
338 rc = 0;
339 rcu_read_lock();
340 ulp_ops = rcu_dereference(cnic_ulp_tbl[CNIC_ULP_ISCSI]);
341 if (ulp_ops)
342 rc = ulp_ops->iscsi_nl_send_msg(
343 cp->ulp_handle[CNIC_ULP_ISCSI],
344 msg_type, buf, len);
345 rcu_read_unlock();
346 if (rc == 0 || msg_type != ISCSI_KEVENT_PATH_REQ)
347 break;
348
349 msleep(100);
350 retry++;
351 }
Michael Chan558e4c72011-07-13 17:24:20 +0000352 return rc;
Michael Chana4636962009-06-08 18:14:43 -0700353}
354
Eddie Wai42ecbb82010-12-23 07:43:02 +0000355static void cnic_cm_upcall(struct cnic_local *, struct cnic_sock *, u8);
356
Michael Chana4636962009-06-08 18:14:43 -0700357static int cnic_iscsi_nl_msg_recv(struct cnic_dev *dev, u32 msg_type,
358 char *buf, u16 len)
359{
360 int rc = -EINVAL;
361
362 switch (msg_type) {
363 case ISCSI_UEVENT_PATH_UPDATE: {
364 struct cnic_local *cp;
365 u32 l5_cid;
366 struct cnic_sock *csk;
367 struct iscsi_path *path_resp;
368
369 if (len < sizeof(*path_resp))
370 break;
371
372 path_resp = (struct iscsi_path *) buf;
373 cp = dev->cnic_priv;
374 l5_cid = (u32) path_resp->handle;
375 if (l5_cid >= MAX_CM_SK_TBL_SZ)
376 break;
377
Michael Chand02a5e62010-02-24 14:42:06 +0000378 rcu_read_lock();
379 if (!rcu_dereference(cp->ulp_ops[CNIC_ULP_L4])) {
380 rc = -ENODEV;
381 rcu_read_unlock();
382 break;
383 }
Michael Chana4636962009-06-08 18:14:43 -0700384 csk = &cp->csk_tbl[l5_cid];
385 csk_hold(csk);
Eddie Wai42ecbb82010-12-23 07:43:02 +0000386 if (cnic_in_use(csk) &&
387 test_bit(SK_F_CONNECT_START, &csk->flags)) {
388
Eddie Wai4cbbb042012-02-08 17:33:57 +0000389 csk->vlan_id = path_resp->vlan_id;
390
Michael Chana4636962009-06-08 18:14:43 -0700391 memcpy(csk->ha, path_resp->mac_addr, 6);
392 if (test_bit(SK_F_IPV6, &csk->flags))
393 memcpy(&csk->src_ip[0], &path_resp->src.v6_addr,
394 sizeof(struct in6_addr));
395 else
396 memcpy(&csk->src_ip[0], &path_resp->src.v4_addr,
397 sizeof(struct in_addr));
Eddie Wai42ecbb82010-12-23 07:43:02 +0000398
399 if (is_valid_ether_addr(csk->ha)) {
Michael Chana4636962009-06-08 18:14:43 -0700400 cnic_cm_set_pg(csk);
Eddie Wai42ecbb82010-12-23 07:43:02 +0000401 } else if (!test_bit(SK_F_OFFLD_SCHED, &csk->flags) &&
402 !test_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
403
404 cnic_cm_upcall(cp, csk,
405 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
406 clear_bit(SK_F_CONNECT_START, &csk->flags);
407 }
Michael Chana4636962009-06-08 18:14:43 -0700408 }
409 csk_put(csk);
Michael Chand02a5e62010-02-24 14:42:06 +0000410 rcu_read_unlock();
Michael Chana4636962009-06-08 18:14:43 -0700411 rc = 0;
412 }
413 }
414
415 return rc;
416}
417
418static int cnic_offld_prep(struct cnic_sock *csk)
419{
420 if (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
421 return 0;
422
423 if (!test_bit(SK_F_CONNECT_START, &csk->flags)) {
424 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
425 return 0;
426 }
427
428 return 1;
429}
430
431static int cnic_close_prep(struct cnic_sock *csk)
432{
433 clear_bit(SK_F_CONNECT_START, &csk->flags);
434 smp_mb__after_clear_bit();
435
436 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
437 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
438 msleep(1);
439
440 return 1;
441 }
442 return 0;
443}
444
445static int cnic_abort_prep(struct cnic_sock *csk)
446{
447 clear_bit(SK_F_CONNECT_START, &csk->flags);
448 smp_mb__after_clear_bit();
449
450 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
451 msleep(1);
452
453 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
454 csk->state = L4_KCQE_OPCODE_VALUE_RESET_COMP;
455 return 1;
456 }
457
458 return 0;
459}
460
461int cnic_register_driver(int ulp_type, struct cnic_ulp_ops *ulp_ops)
462{
463 struct cnic_dev *dev;
464
roel kluin0d37f362009-11-02 06:53:44 +0000465 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000466 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700467 return -EINVAL;
468 }
469 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000470 if (cnic_ulp_tbl_prot(ulp_type)) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000471 pr_err("%s: Type %d has already been registered\n",
472 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700473 mutex_unlock(&cnic_lock);
474 return -EBUSY;
475 }
476
477 read_lock(&cnic_dev_lock);
478 list_for_each_entry(dev, &cnic_dev_list, list) {
479 struct cnic_local *cp = dev->cnic_priv;
480
481 clear_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]);
482 }
483 read_unlock(&cnic_dev_lock);
484
Michael Chan7fc1ece2009-08-14 15:49:47 +0000485 atomic_set(&ulp_ops->ref_count, 0);
Michael Chana4636962009-06-08 18:14:43 -0700486 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], ulp_ops);
487 mutex_unlock(&cnic_lock);
488
489 /* Prevent race conditions with netdev_event */
490 rtnl_lock();
Michael Chana4636962009-06-08 18:14:43 -0700491 list_for_each_entry(dev, &cnic_dev_list, list) {
492 struct cnic_local *cp = dev->cnic_priv;
493
494 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]))
495 ulp_ops->cnic_init(dev);
496 }
Michael Chana4636962009-06-08 18:14:43 -0700497 rtnl_unlock();
498
499 return 0;
500}
501
502int cnic_unregister_driver(int ulp_type)
503{
504 struct cnic_dev *dev;
Michael Chan7fc1ece2009-08-14 15:49:47 +0000505 struct cnic_ulp_ops *ulp_ops;
506 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700507
roel kluin0d37f362009-11-02 06:53:44 +0000508 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000509 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700510 return -EINVAL;
511 }
512 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000513 ulp_ops = cnic_ulp_tbl_prot(ulp_type);
Michael Chan7fc1ece2009-08-14 15:49:47 +0000514 if (!ulp_ops) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000515 pr_err("%s: Type %d has not been registered\n",
516 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700517 goto out_unlock;
518 }
519 read_lock(&cnic_dev_lock);
520 list_for_each_entry(dev, &cnic_dev_list, list) {
521 struct cnic_local *cp = dev->cnic_priv;
522
523 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000524 pr_err("%s: Type %d still has devices registered\n",
525 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700526 read_unlock(&cnic_dev_lock);
527 goto out_unlock;
528 }
529 }
530 read_unlock(&cnic_dev_lock);
531
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000532 RCU_INIT_POINTER(cnic_ulp_tbl[ulp_type], NULL);
Michael Chana4636962009-06-08 18:14:43 -0700533
534 mutex_unlock(&cnic_lock);
535 synchronize_rcu();
Michael Chan7fc1ece2009-08-14 15:49:47 +0000536 while ((atomic_read(&ulp_ops->ref_count) != 0) && (i < 20)) {
537 msleep(100);
538 i++;
539 }
540
541 if (atomic_read(&ulp_ops->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +0000542 netdev_warn(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -0700543 return 0;
544
545out_unlock:
546 mutex_unlock(&cnic_lock);
547 return -EINVAL;
548}
549
550static int cnic_start_hw(struct cnic_dev *);
551static void cnic_stop_hw(struct cnic_dev *);
552
553static int cnic_register_device(struct cnic_dev *dev, int ulp_type,
554 void *ulp_ctx)
555{
556 struct cnic_local *cp = dev->cnic_priv;
557 struct cnic_ulp_ops *ulp_ops;
558
roel kluin0d37f362009-11-02 06:53:44 +0000559 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000560 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700561 return -EINVAL;
562 }
563 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000564 if (cnic_ulp_tbl_prot(ulp_type) == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000565 pr_err("%s: Driver with type %d has not been registered\n",
566 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700567 mutex_unlock(&cnic_lock);
568 return -EAGAIN;
569 }
570 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000571 pr_err("%s: Type %d has already been registered to this device\n",
572 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700573 mutex_unlock(&cnic_lock);
574 return -EBUSY;
575 }
576
577 clear_bit(ULP_F_START, &cp->ulp_flags[ulp_type]);
578 cp->ulp_handle[ulp_type] = ulp_ctx;
Eric Dumazet13707f92011-01-26 19:28:23 +0000579 ulp_ops = cnic_ulp_tbl_prot(ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700580 rcu_assign_pointer(cp->ulp_ops[ulp_type], ulp_ops);
581 cnic_hold(dev);
582
583 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
584 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[ulp_type]))
585 ulp_ops->cnic_start(cp->ulp_handle[ulp_type]);
586
587 mutex_unlock(&cnic_lock);
588
Barak Witkowski1d187b32011-12-05 22:41:50 +0000589 cnic_ulp_ctl(dev, ulp_type, true);
590
Michael Chana4636962009-06-08 18:14:43 -0700591 return 0;
592
593}
594EXPORT_SYMBOL(cnic_register_driver);
595
596static int cnic_unregister_device(struct cnic_dev *dev, int ulp_type)
597{
598 struct cnic_local *cp = dev->cnic_priv;
Michael Chan681dbd72009-08-14 15:49:46 +0000599 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700600
roel kluin0d37f362009-11-02 06:53:44 +0000601 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000602 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700603 return -EINVAL;
604 }
605 mutex_lock(&cnic_lock);
606 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000607 RCU_INIT_POINTER(cp->ulp_ops[ulp_type], NULL);
Michael Chana4636962009-06-08 18:14:43 -0700608 cnic_put(dev);
609 } else {
Joe Perchesddf79b22010-02-17 15:01:54 +0000610 pr_err("%s: device not registered to this ulp type %d\n",
611 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700612 mutex_unlock(&cnic_lock);
613 return -EINVAL;
614 }
615 mutex_unlock(&cnic_lock);
616
Michael Chan42bb8d52011-01-03 15:21:46 +0000617 if (ulp_type == CNIC_ULP_ISCSI)
618 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
Barak Witkowski2e499d32012-06-26 01:31:19 +0000619 else if (ulp_type == CNIC_ULP_FCOE)
620 dev->fcoe_cap = NULL;
Michael Chan42bb8d52011-01-03 15:21:46 +0000621
Michael Chana4636962009-06-08 18:14:43 -0700622 synchronize_rcu();
623
Michael Chan681dbd72009-08-14 15:49:46 +0000624 while (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]) &&
625 i < 20) {
626 msleep(100);
627 i++;
628 }
629 if (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]))
Joe Perchesddf79b22010-02-17 15:01:54 +0000630 netdev_warn(dev->netdev, "Failed waiting for ULP up call to complete\n");
Michael Chan681dbd72009-08-14 15:49:46 +0000631
Barak Witkowski1d187b32011-12-05 22:41:50 +0000632 cnic_ulp_ctl(dev, ulp_type, false);
633
Michael Chana4636962009-06-08 18:14:43 -0700634 return 0;
635}
636EXPORT_SYMBOL(cnic_unregister_driver);
637
Eddie Wai11f23aa2011-06-08 19:29:34 +0000638static int cnic_init_id_tbl(struct cnic_id_tbl *id_tbl, u32 size, u32 start_id,
639 u32 next)
Michael Chana4636962009-06-08 18:14:43 -0700640{
641 id_tbl->start = start_id;
642 id_tbl->max = size;
Eddie Wai11f23aa2011-06-08 19:29:34 +0000643 id_tbl->next = next;
Michael Chana4636962009-06-08 18:14:43 -0700644 spin_lock_init(&id_tbl->lock);
645 id_tbl->table = kzalloc(DIV_ROUND_UP(size, 32) * 4, GFP_KERNEL);
646 if (!id_tbl->table)
647 return -ENOMEM;
648
649 return 0;
650}
651
652static void cnic_free_id_tbl(struct cnic_id_tbl *id_tbl)
653{
654 kfree(id_tbl->table);
655 id_tbl->table = NULL;
656}
657
658static int cnic_alloc_id(struct cnic_id_tbl *id_tbl, u32 id)
659{
660 int ret = -1;
661
662 id -= id_tbl->start;
663 if (id >= id_tbl->max)
664 return ret;
665
666 spin_lock(&id_tbl->lock);
667 if (!test_bit(id, id_tbl->table)) {
668 set_bit(id, id_tbl->table);
669 ret = 0;
670 }
671 spin_unlock(&id_tbl->lock);
672 return ret;
673}
674
675/* Returns -1 if not successful */
676static u32 cnic_alloc_new_id(struct cnic_id_tbl *id_tbl)
677{
678 u32 id;
679
680 spin_lock(&id_tbl->lock);
681 id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
682 if (id >= id_tbl->max) {
683 id = -1;
684 if (id_tbl->next != 0) {
685 id = find_first_zero_bit(id_tbl->table, id_tbl->next);
686 if (id >= id_tbl->next)
687 id = -1;
688 }
689 }
690
691 if (id < id_tbl->max) {
692 set_bit(id, id_tbl->table);
693 id_tbl->next = (id + 1) & (id_tbl->max - 1);
694 id += id_tbl->start;
695 }
696
697 spin_unlock(&id_tbl->lock);
698
699 return id;
700}
701
702static void cnic_free_id(struct cnic_id_tbl *id_tbl, u32 id)
703{
704 if (id == -1)
705 return;
706
707 id -= id_tbl->start;
708 if (id >= id_tbl->max)
709 return;
710
711 clear_bit(id, id_tbl->table);
712}
713
714static void cnic_free_dma(struct cnic_dev *dev, struct cnic_dma *dma)
715{
716 int i;
717
718 if (!dma->pg_arr)
719 return;
720
721 for (i = 0; i < dma->num_pages; i++) {
722 if (dma->pg_arr[i]) {
Michael Chan3248e162009-12-02 15:15:39 +0000723 dma_free_coherent(&dev->pcidev->dev, BCM_PAGE_SIZE,
724 dma->pg_arr[i], dma->pg_map_arr[i]);
Michael Chana4636962009-06-08 18:14:43 -0700725 dma->pg_arr[i] = NULL;
726 }
727 }
728 if (dma->pgtbl) {
Michael Chan3248e162009-12-02 15:15:39 +0000729 dma_free_coherent(&dev->pcidev->dev, dma->pgtbl_size,
730 dma->pgtbl, dma->pgtbl_map);
Michael Chana4636962009-06-08 18:14:43 -0700731 dma->pgtbl = NULL;
732 }
733 kfree(dma->pg_arr);
734 dma->pg_arr = NULL;
735 dma->num_pages = 0;
736}
737
738static void cnic_setup_page_tbl(struct cnic_dev *dev, struct cnic_dma *dma)
739{
740 int i;
Michael Chan51388262011-01-25 22:14:50 +0000741 __le32 *page_table = (__le32 *) dma->pgtbl;
Michael Chana4636962009-06-08 18:14:43 -0700742
743 for (i = 0; i < dma->num_pages; i++) {
744 /* Each entry needs to be in big endian format. */
Michael Chan51388262011-01-25 22:14:50 +0000745 *page_table = cpu_to_le32((u64) dma->pg_map_arr[i] >> 32);
Michael Chana4636962009-06-08 18:14:43 -0700746 page_table++;
Michael Chan51388262011-01-25 22:14:50 +0000747 *page_table = cpu_to_le32(dma->pg_map_arr[i] & 0xffffffff);
Michael Chana4636962009-06-08 18:14:43 -0700748 page_table++;
749 }
750}
751
Michael Chan71034ba2009-10-10 13:46:59 +0000752static void cnic_setup_page_tbl_le(struct cnic_dev *dev, struct cnic_dma *dma)
753{
754 int i;
Michael Chan51388262011-01-25 22:14:50 +0000755 __le32 *page_table = (__le32 *) dma->pgtbl;
Michael Chan71034ba2009-10-10 13:46:59 +0000756
757 for (i = 0; i < dma->num_pages; i++) {
758 /* Each entry needs to be in little endian format. */
Michael Chan51388262011-01-25 22:14:50 +0000759 *page_table = cpu_to_le32(dma->pg_map_arr[i] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +0000760 page_table++;
Michael Chan51388262011-01-25 22:14:50 +0000761 *page_table = cpu_to_le32((u64) dma->pg_map_arr[i] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +0000762 page_table++;
763 }
764}
765
Michael Chana4636962009-06-08 18:14:43 -0700766static int cnic_alloc_dma(struct cnic_dev *dev, struct cnic_dma *dma,
767 int pages, int use_pg_tbl)
768{
769 int i, size;
770 struct cnic_local *cp = dev->cnic_priv;
771
772 size = pages * (sizeof(void *) + sizeof(dma_addr_t));
773 dma->pg_arr = kzalloc(size, GFP_ATOMIC);
774 if (dma->pg_arr == NULL)
775 return -ENOMEM;
776
777 dma->pg_map_arr = (dma_addr_t *) (dma->pg_arr + pages);
778 dma->num_pages = pages;
779
780 for (i = 0; i < pages; i++) {
Michael Chan3248e162009-12-02 15:15:39 +0000781 dma->pg_arr[i] = dma_alloc_coherent(&dev->pcidev->dev,
782 BCM_PAGE_SIZE,
783 &dma->pg_map_arr[i],
784 GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700785 if (dma->pg_arr[i] == NULL)
786 goto error;
787 }
788 if (!use_pg_tbl)
789 return 0;
790
791 dma->pgtbl_size = ((pages * 8) + BCM_PAGE_SIZE - 1) &
792 ~(BCM_PAGE_SIZE - 1);
Michael Chan3248e162009-12-02 15:15:39 +0000793 dma->pgtbl = dma_alloc_coherent(&dev->pcidev->dev, dma->pgtbl_size,
794 &dma->pgtbl_map, GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700795 if (dma->pgtbl == NULL)
796 goto error;
797
798 cp->setup_pgtbl(dev, dma);
799
800 return 0;
801
802error:
803 cnic_free_dma(dev, dma);
804 return -ENOMEM;
805}
806
Michael Chan86b53602009-10-10 13:46:57 +0000807static void cnic_free_context(struct cnic_dev *dev)
808{
809 struct cnic_local *cp = dev->cnic_priv;
810 int i;
811
812 for (i = 0; i < cp->ctx_blks; i++) {
813 if (cp->ctx_arr[i].ctx) {
Michael Chan3248e162009-12-02 15:15:39 +0000814 dma_free_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
815 cp->ctx_arr[i].ctx,
816 cp->ctx_arr[i].mapping);
Michael Chan86b53602009-10-10 13:46:57 +0000817 cp->ctx_arr[i].ctx = NULL;
818 }
819 }
820}
821
Michael Chancd801532010-10-13 14:06:49 +0000822static void __cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chana4636962009-06-08 18:14:43 -0700823{
Michael Chancd801532010-10-13 14:06:49 +0000824 uio_unregister_device(&udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -0700825
Michael Chancd801532010-10-13 14:06:49 +0000826 if (udev->l2_buf) {
827 dma_free_coherent(&udev->pdev->dev, udev->l2_buf_size,
828 udev->l2_buf, udev->l2_buf_map);
829 udev->l2_buf = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700830 }
831
Michael Chancd801532010-10-13 14:06:49 +0000832 if (udev->l2_ring) {
833 dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
834 udev->l2_ring, udev->l2_ring_map);
835 udev->l2_ring = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700836 }
Michael Chana3ceeeb2010-10-13 14:06:50 +0000837
838 pci_dev_put(udev->pdev);
839 kfree(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000840}
841
Michael Chancd801532010-10-13 14:06:49 +0000842static void cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000843{
Michael Chancd801532010-10-13 14:06:49 +0000844 if (!udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000845 return;
846
Michael Chana3ceeeb2010-10-13 14:06:50 +0000847 write_lock(&cnic_dev_lock);
848 list_del_init(&udev->list);
849 write_unlock(&cnic_dev_lock);
Michael Chancd801532010-10-13 14:06:49 +0000850 __cnic_free_uio(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000851}
852
853static void cnic_free_resc(struct cnic_dev *dev)
854{
855 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000856 struct cnic_uio_dev *udev = cp->udev;
Michael Chanc06c0462010-10-13 14:06:48 +0000857
Michael Chancd801532010-10-13 14:06:49 +0000858 if (udev) {
Michael Chana3ceeeb2010-10-13 14:06:50 +0000859 udev->dev = NULL;
Michael Chancd801532010-10-13 14:06:49 +0000860 cp->udev = NULL;
Michael Chanc06c0462010-10-13 14:06:48 +0000861 }
Michael Chana4636962009-06-08 18:14:43 -0700862
Michael Chan86b53602009-10-10 13:46:57 +0000863 cnic_free_context(dev);
Michael Chana4636962009-06-08 18:14:43 -0700864 kfree(cp->ctx_arr);
865 cp->ctx_arr = NULL;
866 cp->ctx_blks = 0;
867
868 cnic_free_dma(dev, &cp->gbl_buf_info);
Michael Chana4636962009-06-08 18:14:43 -0700869 cnic_free_dma(dev, &cp->kwq_info);
Michael Chan71034ba2009-10-10 13:46:59 +0000870 cnic_free_dma(dev, &cp->kwq_16_data_info);
Michael Chane21ba412010-12-23 07:43:03 +0000871 cnic_free_dma(dev, &cp->kcq2.dma);
Michael Chane6c28892010-06-24 14:58:39 +0000872 cnic_free_dma(dev, &cp->kcq1.dma);
Michael Chana4636962009-06-08 18:14:43 -0700873 kfree(cp->iscsi_tbl);
874 cp->iscsi_tbl = NULL;
875 kfree(cp->ctx_tbl);
876 cp->ctx_tbl = NULL;
877
Michael Chane1928c82010-12-23 07:43:04 +0000878 cnic_free_id_tbl(&cp->fcoe_cid_tbl);
Michael Chana4636962009-06-08 18:14:43 -0700879 cnic_free_id_tbl(&cp->cid_tbl);
880}
881
882static int cnic_alloc_context(struct cnic_dev *dev)
883{
884 struct cnic_local *cp = dev->cnic_priv;
885
886 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
887 int i, k, arr_size;
888
889 cp->ctx_blk_size = BCM_PAGE_SIZE;
890 cp->cids_per_blk = BCM_PAGE_SIZE / 128;
891 arr_size = BNX2_MAX_CID / cp->cids_per_blk *
892 sizeof(struct cnic_ctx);
893 cp->ctx_arr = kzalloc(arr_size, GFP_KERNEL);
894 if (cp->ctx_arr == NULL)
895 return -ENOMEM;
896
897 k = 0;
898 for (i = 0; i < 2; i++) {
899 u32 j, reg, off, lo, hi;
900
901 if (i == 0)
902 off = BNX2_PG_CTX_MAP;
903 else
904 off = BNX2_ISCSI_CTX_MAP;
905
906 reg = cnic_reg_rd_ind(dev, off);
907 lo = reg >> 16;
908 hi = reg & 0xffff;
909 for (j = lo; j < hi; j += cp->cids_per_blk, k++)
910 cp->ctx_arr[k].cid = j;
911 }
912
913 cp->ctx_blks = k;
914 if (cp->ctx_blks >= (BNX2_MAX_CID / cp->cids_per_blk)) {
915 cp->ctx_blks = 0;
916 return -ENOMEM;
917 }
918
919 for (i = 0; i < cp->ctx_blks; i++) {
920 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +0000921 dma_alloc_coherent(&dev->pcidev->dev,
922 BCM_PAGE_SIZE,
923 &cp->ctx_arr[i].mapping,
924 GFP_KERNEL);
Michael Chana4636962009-06-08 18:14:43 -0700925 if (cp->ctx_arr[i].ctx == NULL)
926 return -ENOMEM;
927 }
928 }
929 return 0;
930}
931
Michael Chan59e51372011-06-14 01:32:38 +0000932static u16 cnic_bnx2_next_idx(u16 idx)
Michael Chane6c28892010-06-24 14:58:39 +0000933{
Michael Chan59e51372011-06-14 01:32:38 +0000934 return idx + 1;
935}
936
937static u16 cnic_bnx2_hw_idx(u16 idx)
938{
939 return idx;
940}
941
942static u16 cnic_bnx2x_next_idx(u16 idx)
943{
944 idx++;
945 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
946 idx++;
947
948 return idx;
949}
950
951static u16 cnic_bnx2x_hw_idx(u16 idx)
952{
953 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
954 idx++;
955 return idx;
956}
957
958static int cnic_alloc_kcq(struct cnic_dev *dev, struct kcq_info *info,
959 bool use_pg_tbl)
960{
961 int err, i, use_page_tbl = 0;
Michael Chane6c28892010-06-24 14:58:39 +0000962 struct kcqe **kcq;
963
Michael Chan59e51372011-06-14 01:32:38 +0000964 if (use_pg_tbl)
965 use_page_tbl = 1;
Michael Chane6c28892010-06-24 14:58:39 +0000966
Michael Chan59e51372011-06-14 01:32:38 +0000967 err = cnic_alloc_dma(dev, &info->dma, KCQ_PAGE_CNT, use_page_tbl);
Michael Chane6c28892010-06-24 14:58:39 +0000968 if (err)
969 return err;
970
971 kcq = (struct kcqe **) info->dma.pg_arr;
972 info->kcq = kcq;
973
Michael Chan59e51372011-06-14 01:32:38 +0000974 info->next_idx = cnic_bnx2_next_idx;
975 info->hw_idx = cnic_bnx2_hw_idx;
976 if (use_pg_tbl)
Michael Chane6c28892010-06-24 14:58:39 +0000977 return 0;
978
Michael Chan59e51372011-06-14 01:32:38 +0000979 info->next_idx = cnic_bnx2x_next_idx;
980 info->hw_idx = cnic_bnx2x_hw_idx;
981
Michael Chane6c28892010-06-24 14:58:39 +0000982 for (i = 0; i < KCQ_PAGE_CNT; i++) {
983 struct bnx2x_bd_chain_next *next =
984 (struct bnx2x_bd_chain_next *) &kcq[i][MAX_KCQE_CNT];
985 int j = i + 1;
986
987 if (j >= KCQ_PAGE_CNT)
988 j = 0;
989 next->addr_hi = (u64) info->dma.pg_map_arr[j] >> 32;
990 next->addr_lo = info->dma.pg_map_arr[j] & 0xffffffff;
991 }
992 return 0;
993}
994
Michael Chancd801532010-10-13 14:06:49 +0000995static int cnic_alloc_uio_rings(struct cnic_dev *dev, int pages)
Michael Chanec0248e2009-08-26 09:49:22 +0000996{
997 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000998 struct cnic_uio_dev *udev;
Michael Chanec0248e2009-08-26 09:49:22 +0000999
Michael Chana3ceeeb2010-10-13 14:06:50 +00001000 read_lock(&cnic_dev_lock);
1001 list_for_each_entry(udev, &cnic_udev_list, list) {
1002 if (udev->pdev == dev->pcidev) {
1003 udev->dev = dev;
1004 cp->udev = udev;
1005 read_unlock(&cnic_dev_lock);
1006 return 0;
1007 }
1008 }
1009 read_unlock(&cnic_dev_lock);
1010
Michael Chancd801532010-10-13 14:06:49 +00001011 udev = kzalloc(sizeof(struct cnic_uio_dev), GFP_ATOMIC);
1012 if (!udev)
Michael Chanec0248e2009-08-26 09:49:22 +00001013 return -ENOMEM;
1014
Michael Chancd801532010-10-13 14:06:49 +00001015 udev->uio_dev = -1;
1016
1017 udev->dev = dev;
1018 udev->pdev = dev->pcidev;
1019 udev->l2_ring_size = pages * BCM_PAGE_SIZE;
1020 udev->l2_ring = dma_alloc_coherent(&udev->pdev->dev, udev->l2_ring_size,
1021 &udev->l2_ring_map,
1022 GFP_KERNEL | __GFP_COMP);
1023 if (!udev->l2_ring)
Jesper Juhlf7e4c972010-12-31 11:18:48 -08001024 goto err_udev;
Michael Chanec0248e2009-08-26 09:49:22 +00001025
Michael Chancd801532010-10-13 14:06:49 +00001026 udev->l2_buf_size = (cp->l2_rx_ring_size + 1) * cp->l2_single_buf_size;
1027 udev->l2_buf_size = PAGE_ALIGN(udev->l2_buf_size);
1028 udev->l2_buf = dma_alloc_coherent(&udev->pdev->dev, udev->l2_buf_size,
1029 &udev->l2_buf_map,
1030 GFP_KERNEL | __GFP_COMP);
1031 if (!udev->l2_buf)
Jesper Juhlf7e4c972010-12-31 11:18:48 -08001032 goto err_dma;
Michael Chancd801532010-10-13 14:06:49 +00001033
Michael Chana3ceeeb2010-10-13 14:06:50 +00001034 write_lock(&cnic_dev_lock);
1035 list_add(&udev->list, &cnic_udev_list);
1036 write_unlock(&cnic_dev_lock);
1037
1038 pci_dev_get(udev->pdev);
1039
Michael Chancd801532010-10-13 14:06:49 +00001040 cp->udev = udev;
1041
Michael Chanec0248e2009-08-26 09:49:22 +00001042 return 0;
Jesper Juhlf7e4c972010-12-31 11:18:48 -08001043 err_dma:
1044 dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
1045 udev->l2_ring, udev->l2_ring_map);
1046 err_udev:
1047 kfree(udev);
1048 return -ENOMEM;
Michael Chanec0248e2009-08-26 09:49:22 +00001049}
1050
Michael Chancd801532010-10-13 14:06:49 +00001051static int cnic_init_uio(struct cnic_dev *dev)
1052{
Michael Chan5e9b2db2009-08-26 09:49:23 +00001053 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00001054 struct cnic_uio_dev *udev = cp->udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001055 struct uio_info *uinfo;
Michael Chancd801532010-10-13 14:06:49 +00001056 int ret = 0;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001057
Michael Chancd801532010-10-13 14:06:49 +00001058 if (!udev)
Michael Chan5e9b2db2009-08-26 09:49:23 +00001059 return -ENOMEM;
1060
Michael Chancd801532010-10-13 14:06:49 +00001061 uinfo = &udev->cnic_uinfo;
1062
Michael Chan5e9b2db2009-08-26 09:49:23 +00001063 uinfo->mem[0].addr = dev->netdev->base_addr;
1064 uinfo->mem[0].internal_addr = dev->regview;
1065 uinfo->mem[0].size = dev->netdev->mem_end - dev->netdev->mem_start;
1066 uinfo->mem[0].memtype = UIO_MEM_PHYS;
1067
Michael Chan5e9b2db2009-08-26 09:49:23 +00001068 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
Michael Chana4dde3a2010-02-24 14:42:08 +00001069 uinfo->mem[1].addr = (unsigned long) cp->status_blk.gen &
Michael Chancd801532010-10-13 14:06:49 +00001070 PAGE_MASK;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001071 if (cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
1072 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE * 9;
1073 else
1074 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE;
1075
1076 uinfo->name = "bnx2_cnic";
Michael Chan71034ba2009-10-10 13:46:59 +00001077 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
1078 uinfo->mem[1].addr = (unsigned long) cp->bnx2x_def_status_blk &
1079 PAGE_MASK;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001080 uinfo->mem[1].size = sizeof(*cp->bnx2x_def_status_blk);
Michael Chan71034ba2009-10-10 13:46:59 +00001081
1082 uinfo->name = "bnx2x_cnic";
Michael Chan5e9b2db2009-08-26 09:49:23 +00001083 }
1084
1085 uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
1086
Michael Chancd801532010-10-13 14:06:49 +00001087 uinfo->mem[2].addr = (unsigned long) udev->l2_ring;
1088 uinfo->mem[2].size = udev->l2_ring_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001089 uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
1090
Michael Chancd801532010-10-13 14:06:49 +00001091 uinfo->mem[3].addr = (unsigned long) udev->l2_buf;
1092 uinfo->mem[3].size = udev->l2_buf_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001093 uinfo->mem[3].memtype = UIO_MEM_LOGICAL;
1094
1095 uinfo->version = CNIC_MODULE_VERSION;
1096 uinfo->irq = UIO_IRQ_CUSTOM;
1097
1098 uinfo->open = cnic_uio_open;
1099 uinfo->release = cnic_uio_close;
1100
Michael Chana3ceeeb2010-10-13 14:06:50 +00001101 if (udev->uio_dev == -1) {
1102 if (!uinfo->priv) {
1103 uinfo->priv = udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001104
Michael Chana3ceeeb2010-10-13 14:06:50 +00001105 ret = uio_register_device(&udev->pdev->dev, uinfo);
1106 }
1107 } else {
1108 cnic_init_rings(dev);
1109 }
Michael Chan5e9b2db2009-08-26 09:49:23 +00001110
Michael Chancd801532010-10-13 14:06:49 +00001111 return ret;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001112}
1113
Michael Chana4636962009-06-08 18:14:43 -07001114static int cnic_alloc_bnx2_resc(struct cnic_dev *dev)
1115{
1116 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07001117 int ret;
1118
1119 ret = cnic_alloc_dma(dev, &cp->kwq_info, KWQ_PAGE_CNT, 1);
1120 if (ret)
1121 goto error;
1122 cp->kwq = (struct kwqe **) cp->kwq_info.pg_arr;
1123
Michael Chan59e51372011-06-14 01:32:38 +00001124 ret = cnic_alloc_kcq(dev, &cp->kcq1, true);
Michael Chana4636962009-06-08 18:14:43 -07001125 if (ret)
1126 goto error;
Michael Chana4636962009-06-08 18:14:43 -07001127
1128 ret = cnic_alloc_context(dev);
1129 if (ret)
1130 goto error;
1131
Michael Chancd801532010-10-13 14:06:49 +00001132 ret = cnic_alloc_uio_rings(dev, 2);
Michael Chanec0248e2009-08-26 09:49:22 +00001133 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001134 goto error;
1135
Michael Chancd801532010-10-13 14:06:49 +00001136 ret = cnic_init_uio(dev);
Michael Chan5e9b2db2009-08-26 09:49:23 +00001137 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001138 goto error;
1139
Michael Chana4636962009-06-08 18:14:43 -07001140 return 0;
1141
1142error:
1143 cnic_free_resc(dev);
1144 return ret;
1145}
1146
Michael Chan71034ba2009-10-10 13:46:59 +00001147static int cnic_alloc_bnx2x_context(struct cnic_dev *dev)
1148{
1149 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001150 int ctx_blk_size = cp->ethdev->ctx_blk_size;
Michael Chan520efdf2010-06-24 14:58:37 +00001151 int total_mem, blks, i;
Michael Chan71034ba2009-10-10 13:46:59 +00001152
Michael Chan520efdf2010-06-24 14:58:37 +00001153 total_mem = BNX2X_CONTEXT_MEM_SIZE * cp->max_cid_space;
Michael Chan71034ba2009-10-10 13:46:59 +00001154 blks = total_mem / ctx_blk_size;
1155 if (total_mem % ctx_blk_size)
1156 blks++;
1157
1158 if (blks > cp->ethdev->ctx_tbl_len)
1159 return -ENOMEM;
1160
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001161 cp->ctx_arr = kcalloc(blks, sizeof(struct cnic_ctx), GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001162 if (cp->ctx_arr == NULL)
1163 return -ENOMEM;
1164
1165 cp->ctx_blks = blks;
1166 cp->ctx_blk_size = ctx_blk_size;
Michael Chanee87a822010-10-13 14:06:51 +00001167 if (!BNX2X_CHIP_IS_57710(cp->chip_id))
Michael Chan71034ba2009-10-10 13:46:59 +00001168 cp->ctx_align = 0;
1169 else
1170 cp->ctx_align = ctx_blk_size;
1171
1172 cp->cids_per_blk = ctx_blk_size / BNX2X_CONTEXT_MEM_SIZE;
1173
1174 for (i = 0; i < blks; i++) {
1175 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +00001176 dma_alloc_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
1177 &cp->ctx_arr[i].mapping,
1178 GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001179 if (cp->ctx_arr[i].ctx == NULL)
1180 return -ENOMEM;
1181
1182 if (cp->ctx_align && cp->ctx_blk_size == ctx_blk_size) {
1183 if (cp->ctx_arr[i].mapping & (cp->ctx_align - 1)) {
1184 cnic_free_context(dev);
1185 cp->ctx_blk_size += cp->ctx_align;
1186 i = -1;
1187 continue;
1188 }
1189 }
1190 }
1191 return 0;
1192}
1193
1194static int cnic_alloc_bnx2x_resc(struct cnic_dev *dev)
1195{
1196 struct cnic_local *cp = dev->cnic_priv;
Michael Chan520efdf2010-06-24 14:58:37 +00001197 struct cnic_eth_dev *ethdev = cp->ethdev;
1198 u32 start_cid = ethdev->starting_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001199 int i, j, n, ret, pages;
1200 struct cnic_dma *kwq_16_dma = &cp->kwq_16_data_info;
1201
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001202 cp->iro_arr = ethdev->iro_arr;
1203
Michael Chanb37a41e2011-07-20 14:55:22 +00001204 cp->max_cid_space = MAX_ISCSI_TBL_SZ;
Michael Chan520efdf2010-06-24 14:58:37 +00001205 cp->iscsi_start_cid = start_cid;
Michael Chane1928c82010-12-23 07:43:04 +00001206 cp->fcoe_start_cid = start_cid + MAX_ISCSI_TBL_SZ;
1207
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001208 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chandc219a22011-08-26 09:45:39 +00001209 cp->max_cid_space += dev->max_fcoe_conn;
Michael Chane1928c82010-12-23 07:43:04 +00001210 cp->fcoe_init_cid = ethdev->fcoe_init_cid;
1211 if (!cp->fcoe_init_cid)
1212 cp->fcoe_init_cid = 0x10;
1213 }
1214
Michael Chan71034ba2009-10-10 13:46:59 +00001215 cp->iscsi_tbl = kzalloc(sizeof(struct cnic_iscsi) * MAX_ISCSI_TBL_SZ,
1216 GFP_KERNEL);
1217 if (!cp->iscsi_tbl)
1218 goto error;
1219
1220 cp->ctx_tbl = kzalloc(sizeof(struct cnic_context) *
Michael Chan520efdf2010-06-24 14:58:37 +00001221 cp->max_cid_space, GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001222 if (!cp->ctx_tbl)
1223 goto error;
1224
1225 for (i = 0; i < MAX_ISCSI_TBL_SZ; i++) {
1226 cp->ctx_tbl[i].proto.iscsi = &cp->iscsi_tbl[i];
1227 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_ISCSI;
1228 }
1229
Michael Chane1928c82010-12-23 07:43:04 +00001230 for (i = MAX_ISCSI_TBL_SZ; i < cp->max_cid_space; i++)
1231 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_FCOE;
1232
Michael Chan520efdf2010-06-24 14:58:37 +00001233 pages = PAGE_ALIGN(cp->max_cid_space * CNIC_KWQ16_DATA_SIZE) /
Michael Chan71034ba2009-10-10 13:46:59 +00001234 PAGE_SIZE;
1235
1236 ret = cnic_alloc_dma(dev, kwq_16_dma, pages, 0);
1237 if (ret)
1238 return -ENOMEM;
1239
1240 n = PAGE_SIZE / CNIC_KWQ16_DATA_SIZE;
Michael Chan520efdf2010-06-24 14:58:37 +00001241 for (i = 0, j = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +00001242 long off = CNIC_KWQ16_DATA_SIZE * (i % n);
1243
1244 cp->ctx_tbl[i].kwqe_data = kwq_16_dma->pg_arr[j] + off;
1245 cp->ctx_tbl[i].kwqe_data_mapping = kwq_16_dma->pg_map_arr[j] +
1246 off;
1247
1248 if ((i % n) == (n - 1))
1249 j++;
1250 }
1251
Michael Chan59e51372011-06-14 01:32:38 +00001252 ret = cnic_alloc_kcq(dev, &cp->kcq1, false);
Michael Chan71034ba2009-10-10 13:46:59 +00001253 if (ret)
1254 goto error;
Michael Chan71034ba2009-10-10 13:46:59 +00001255
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001256 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
1257 ret = cnic_alloc_kcq(dev, &cp->kcq2, true);
Michael Chane21ba412010-12-23 07:43:03 +00001258 if (ret)
1259 goto error;
1260 }
1261
Michael Chan71034ba2009-10-10 13:46:59 +00001262 pages = PAGE_ALIGN(BNX2X_ISCSI_GLB_BUF_SIZE) / PAGE_SIZE;
1263 ret = cnic_alloc_dma(dev, &cp->gbl_buf_info, pages, 0);
1264 if (ret)
1265 goto error;
1266
1267 ret = cnic_alloc_bnx2x_context(dev);
1268 if (ret)
1269 goto error;
1270
Michael Chan71034ba2009-10-10 13:46:59 +00001271 cp->bnx2x_def_status_blk = cp->ethdev->irq_arr[1].status_blk;
1272
1273 cp->l2_rx_ring_size = 15;
1274
Michael Chancd801532010-10-13 14:06:49 +00001275 ret = cnic_alloc_uio_rings(dev, 4);
Michael Chan71034ba2009-10-10 13:46:59 +00001276 if (ret)
1277 goto error;
1278
Michael Chancd801532010-10-13 14:06:49 +00001279 ret = cnic_init_uio(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00001280 if (ret)
1281 goto error;
1282
1283 return 0;
1284
1285error:
1286 cnic_free_resc(dev);
1287 return -ENOMEM;
1288}
1289
Michael Chana4636962009-06-08 18:14:43 -07001290static inline u32 cnic_kwq_avail(struct cnic_local *cp)
1291{
1292 return cp->max_kwq_idx -
1293 ((cp->kwq_prod_idx - cp->kwq_con_idx) & cp->max_kwq_idx);
1294}
1295
1296static int cnic_submit_bnx2_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
1297 u32 num_wqes)
1298{
1299 struct cnic_local *cp = dev->cnic_priv;
1300 struct kwqe *prod_qe;
1301 u16 prod, sw_prod, i;
1302
1303 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
1304 return -EAGAIN; /* bnx2 is down */
1305
1306 spin_lock_bh(&cp->cnic_ulp_lock);
1307 if (num_wqes > cnic_kwq_avail(cp) &&
Michael Chan1f1332a2010-05-18 11:32:52 +00001308 !test_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags)) {
Michael Chana4636962009-06-08 18:14:43 -07001309 spin_unlock_bh(&cp->cnic_ulp_lock);
1310 return -EAGAIN;
1311 }
1312
Michael Chan1f1332a2010-05-18 11:32:52 +00001313 clear_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07001314
1315 prod = cp->kwq_prod_idx;
1316 sw_prod = prod & MAX_KWQ_IDX;
1317 for (i = 0; i < num_wqes; i++) {
1318 prod_qe = &cp->kwq[KWQ_PG(sw_prod)][KWQ_IDX(sw_prod)];
1319 memcpy(prod_qe, wqes[i], sizeof(struct kwqe));
1320 prod++;
1321 sw_prod = prod & MAX_KWQ_IDX;
1322 }
1323 cp->kwq_prod_idx = prod;
1324
1325 CNIC_WR16(dev, cp->kwq_io_addr, cp->kwq_prod_idx);
1326
1327 spin_unlock_bh(&cp->cnic_ulp_lock);
1328 return 0;
1329}
1330
Michael Chan71034ba2009-10-10 13:46:59 +00001331static void *cnic_get_kwqe_16_data(struct cnic_local *cp, u32 l5_cid,
1332 union l5cm_specific_data *l5_data)
1333{
1334 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1335 dma_addr_t map;
1336
1337 map = ctx->kwqe_data_mapping;
1338 l5_data->phy_address.lo = (u64) map & 0xffffffff;
1339 l5_data->phy_address.hi = (u64) map >> 32;
1340 return ctx->kwqe_data;
1341}
1342
1343static int cnic_submit_kwqe_16(struct cnic_dev *dev, u32 cmd, u32 cid,
1344 u32 type, union l5cm_specific_data *l5_data)
1345{
1346 struct cnic_local *cp = dev->cnic_priv;
1347 struct l5cm_spe kwqe;
1348 struct kwqe_16 *kwq[1];
Michael Chan68d7c1a2011-01-05 15:14:13 +00001349 u16 type_16;
Michael Chan71034ba2009-10-10 13:46:59 +00001350 int ret;
1351
1352 kwqe.hdr.conn_and_cmd_data =
1353 cpu_to_le32(((cmd << SPE_HDR_CMD_ID_SHIFT) |
Michael Chanceb7e1c2010-10-06 03:14:54 +00001354 BNX2X_HW_CID(cp, cid)));
Michael Chan68d7c1a2011-01-05 15:14:13 +00001355
1356 type_16 = (type << SPE_HDR_CONN_TYPE_SHIFT) & SPE_HDR_CONN_TYPE;
1357 type_16 |= (cp->pfid << SPE_HDR_FUNCTION_ID_SHIFT) &
1358 SPE_HDR_FUNCTION_ID;
1359
1360 kwqe.hdr.type = cpu_to_le16(type_16);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001361 kwqe.hdr.reserved1 = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001362 kwqe.data.phy_address.lo = cpu_to_le32(l5_data->phy_address.lo);
1363 kwqe.data.phy_address.hi = cpu_to_le32(l5_data->phy_address.hi);
1364
1365 kwq[0] = (struct kwqe_16 *) &kwqe;
1366
1367 spin_lock_bh(&cp->cnic_ulp_lock);
1368 ret = cp->ethdev->drv_submit_kwqes_16(dev->netdev, kwq, 1);
1369 spin_unlock_bh(&cp->cnic_ulp_lock);
1370
1371 if (ret == 1)
1372 return 0;
1373
Michael Chan23021c22012-01-04 12:12:28 +00001374 return ret;
Michael Chan71034ba2009-10-10 13:46:59 +00001375}
1376
1377static void cnic_reply_bnx2x_kcqes(struct cnic_dev *dev, int ulp_type,
1378 struct kcqe *cqes[], u32 num_cqes)
1379{
1380 struct cnic_local *cp = dev->cnic_priv;
1381 struct cnic_ulp_ops *ulp_ops;
1382
1383 rcu_read_lock();
1384 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
1385 if (likely(ulp_ops)) {
1386 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
1387 cqes, num_cqes);
1388 }
1389 rcu_read_unlock();
1390}
1391
1392static int cnic_bnx2x_iscsi_init1(struct cnic_dev *dev, struct kwqe *kwqe)
1393{
1394 struct cnic_local *cp = dev->cnic_priv;
1395 struct iscsi_kwqe_init1 *req1 = (struct iscsi_kwqe_init1 *) kwqe;
Michael Chan14203982010-10-06 03:16:06 +00001396 int hq_bds, pages;
1397 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001398
1399 cp->num_iscsi_tasks = req1->num_tasks_per_conn;
1400 cp->num_ccells = req1->num_ccells_per_conn;
1401 cp->task_array_size = BNX2X_ISCSI_TASK_CONTEXT_SIZE *
1402 cp->num_iscsi_tasks;
1403 cp->r2tq_size = cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS *
1404 BNX2X_ISCSI_R2TQE_SIZE;
1405 cp->hq_size = cp->num_ccells * BNX2X_ISCSI_HQ_BD_SIZE;
1406 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1407 hq_bds = pages * (PAGE_SIZE / BNX2X_ISCSI_HQ_BD_SIZE);
1408 cp->num_cqs = req1->num_cqs;
1409
1410 if (!dev->max_iscsi_conn)
1411 return 0;
1412
1413 /* init Tstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001414 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001415 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001416 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001417 PAGE_SIZE);
1418 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001419 TSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001420 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001421 TSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001422 req1->num_tasks_per_conn);
1423
1424 /* init Ustorm RAM */
1425 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001426 USTORM_ISCSI_RQ_BUFFER_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001427 req1->rq_buffer_size);
Michael Chan14203982010-10-06 03:16:06 +00001428 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001429 PAGE_SIZE);
1430 CNIC_WR8(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001431 USTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001432 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001433 USTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001434 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001435 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001436 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001437 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001438 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001439 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001440 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1441
1442 /* init Xstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001443 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001444 PAGE_SIZE);
1445 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001446 XSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001447 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001448 XSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001449 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001450 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001451 hq_bds);
Michael Chan14203982010-10-06 03:16:06 +00001452 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_SQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001453 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001454 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001455 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1456
1457 /* init Cstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001458 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001459 PAGE_SIZE);
1460 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001461 CSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001462 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001463 CSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001464 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001465 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001466 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001467 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001468 hq_bds);
1469
1470 return 0;
1471}
1472
1473static int cnic_bnx2x_iscsi_init2(struct cnic_dev *dev, struct kwqe *kwqe)
1474{
1475 struct iscsi_kwqe_init2 *req2 = (struct iscsi_kwqe_init2 *) kwqe;
1476 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00001477 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001478 struct iscsi_kcqe kcqe;
1479 struct kcqe *cqes[1];
1480
1481 memset(&kcqe, 0, sizeof(kcqe));
1482 if (!dev->max_iscsi_conn) {
1483 kcqe.completion_status =
1484 ISCSI_KCQE_COMPLETION_STATUS_ISCSI_NOT_SUPPORTED;
1485 goto done;
1486 }
1487
1488 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001489 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001490 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001491 TSTORM_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_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001495 USTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001496 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001497 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001498 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001499 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001500 req2->error_bit_map[1]);
1501
1502 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001503 CSTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001504
1505 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1506
1507done:
1508 kcqe.op_code = ISCSI_KCQE_OPCODE_INIT;
1509 cqes[0] = (struct kcqe *) &kcqe;
1510 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1511
1512 return 0;
1513}
1514
1515static void cnic_free_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1516{
1517 struct cnic_local *cp = dev->cnic_priv;
1518 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1519
1520 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI) {
1521 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1522
1523 cnic_free_dma(dev, &iscsi->hq_info);
1524 cnic_free_dma(dev, &iscsi->r2tq_info);
1525 cnic_free_dma(dev, &iscsi->task_array_info);
Michael Chane1928c82010-12-23 07:43:04 +00001526 cnic_free_id(&cp->cid_tbl, ctx->cid);
1527 } else {
1528 cnic_free_id(&cp->fcoe_cid_tbl, ctx->cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001529 }
Michael Chane1928c82010-12-23 07:43:04 +00001530
Michael Chan71034ba2009-10-10 13:46:59 +00001531 ctx->cid = 0;
1532}
1533
1534static int cnic_alloc_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1535{
1536 u32 cid;
1537 int ret, pages;
1538 struct cnic_local *cp = dev->cnic_priv;
1539 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1540 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1541
Michael Chane1928c82010-12-23 07:43:04 +00001542 if (ctx->ulp_proto_id == CNIC_ULP_FCOE) {
1543 cid = cnic_alloc_new_id(&cp->fcoe_cid_tbl);
1544 if (cid == -1) {
1545 ret = -ENOMEM;
1546 goto error;
1547 }
1548 ctx->cid = cid;
1549 return 0;
1550 }
1551
Michael Chan71034ba2009-10-10 13:46:59 +00001552 cid = cnic_alloc_new_id(&cp->cid_tbl);
1553 if (cid == -1) {
1554 ret = -ENOMEM;
1555 goto error;
1556 }
1557
1558 ctx->cid = cid;
1559 pages = PAGE_ALIGN(cp->task_array_size) / PAGE_SIZE;
1560
1561 ret = cnic_alloc_dma(dev, &iscsi->task_array_info, pages, 1);
1562 if (ret)
1563 goto error;
1564
1565 pages = PAGE_ALIGN(cp->r2tq_size) / PAGE_SIZE;
1566 ret = cnic_alloc_dma(dev, &iscsi->r2tq_info, pages, 1);
1567 if (ret)
1568 goto error;
1569
1570 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1571 ret = cnic_alloc_dma(dev, &iscsi->hq_info, pages, 1);
1572 if (ret)
1573 goto error;
1574
1575 return 0;
1576
1577error:
1578 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1579 return ret;
1580}
1581
1582static void *cnic_get_bnx2x_ctx(struct cnic_dev *dev, u32 cid, int init,
1583 struct regpair *ctx_addr)
1584{
1585 struct cnic_local *cp = dev->cnic_priv;
1586 struct cnic_eth_dev *ethdev = cp->ethdev;
1587 int blk = (cid - ethdev->starting_cid) / cp->cids_per_blk;
1588 int off = (cid - ethdev->starting_cid) % cp->cids_per_blk;
1589 unsigned long align_off = 0;
1590 dma_addr_t ctx_map;
1591 void *ctx;
1592
1593 if (cp->ctx_align) {
1594 unsigned long mask = cp->ctx_align - 1;
1595
1596 if (cp->ctx_arr[blk].mapping & mask)
1597 align_off = cp->ctx_align -
1598 (cp->ctx_arr[blk].mapping & mask);
1599 }
1600 ctx_map = cp->ctx_arr[blk].mapping + align_off +
1601 (off * BNX2X_CONTEXT_MEM_SIZE);
1602 ctx = cp->ctx_arr[blk].ctx + align_off +
1603 (off * BNX2X_CONTEXT_MEM_SIZE);
1604 if (init)
1605 memset(ctx, 0, BNX2X_CONTEXT_MEM_SIZE);
1606
1607 ctx_addr->lo = ctx_map & 0xffffffff;
1608 ctx_addr->hi = (u64) ctx_map >> 32;
1609 return ctx;
1610}
1611
1612static int cnic_setup_bnx2x_ctx(struct cnic_dev *dev, struct kwqe *wqes[],
1613 u32 num)
1614{
1615 struct cnic_local *cp = dev->cnic_priv;
1616 struct iscsi_kwqe_conn_offload1 *req1 =
1617 (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1618 struct iscsi_kwqe_conn_offload2 *req2 =
1619 (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1620 struct iscsi_kwqe_conn_offload3 *req3;
1621 struct cnic_context *ctx = &cp->ctx_tbl[req1->iscsi_conn_id];
1622 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1623 u32 cid = ctx->cid;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001624 u32 hw_cid = BNX2X_HW_CID(cp, cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001625 struct iscsi_context *ictx;
1626 struct regpair context_addr;
1627 int i, j, n = 2, n_max;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001628 u8 port = CNIC_PORT(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00001629
1630 ctx->ctx_flags = 0;
1631 if (!req2->num_additional_wqes)
1632 return -EINVAL;
1633
1634 n_max = req2->num_additional_wqes + 2;
1635
1636 ictx = cnic_get_bnx2x_ctx(dev, cid, 1, &context_addr);
1637 if (ictx == NULL)
1638 return -ENOMEM;
1639
1640 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1641
1642 ictx->xstorm_ag_context.hq_prod = 1;
1643
1644 ictx->xstorm_st_context.iscsi.first_burst_length =
1645 ISCSI_DEF_FIRST_BURST_LEN;
1646 ictx->xstorm_st_context.iscsi.max_send_pdu_length =
1647 ISCSI_DEF_MAX_RECV_SEG_LEN;
1648 ictx->xstorm_st_context.iscsi.sq_pbl_base.lo =
1649 req1->sq_page_table_addr_lo;
1650 ictx->xstorm_st_context.iscsi.sq_pbl_base.hi =
1651 req1->sq_page_table_addr_hi;
1652 ictx->xstorm_st_context.iscsi.sq_curr_pbe.lo = req2->sq_first_pte.hi;
1653 ictx->xstorm_st_context.iscsi.sq_curr_pbe.hi = req2->sq_first_pte.lo;
1654 ictx->xstorm_st_context.iscsi.hq_pbl_base.lo =
1655 iscsi->hq_info.pgtbl_map & 0xffffffff;
1656 ictx->xstorm_st_context.iscsi.hq_pbl_base.hi =
1657 (u64) iscsi->hq_info.pgtbl_map >> 32;
1658 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.lo =
1659 iscsi->hq_info.pgtbl[0];
1660 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.hi =
1661 iscsi->hq_info.pgtbl[1];
1662 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.lo =
1663 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1664 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.hi =
1665 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1666 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.lo =
1667 iscsi->r2tq_info.pgtbl[0];
1668 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.hi =
1669 iscsi->r2tq_info.pgtbl[1];
1670 ictx->xstorm_st_context.iscsi.task_pbl_base.lo =
1671 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1672 ictx->xstorm_st_context.iscsi.task_pbl_base.hi =
1673 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1674 ictx->xstorm_st_context.iscsi.task_pbl_cache_idx =
1675 BNX2X_ISCSI_PBL_NOT_CACHED;
1676 ictx->xstorm_st_context.iscsi.flags.flags |=
1677 XSTORM_ISCSI_CONTEXT_FLAGS_B_IMMEDIATE_DATA;
1678 ictx->xstorm_st_context.iscsi.flags.flags |=
1679 XSTORM_ISCSI_CONTEXT_FLAGS_B_INITIAL_R2T;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001680 ictx->xstorm_st_context.common.ethernet.reserved_vlan_type =
1681 ETH_P_8021Q;
1682 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id) &&
1683 cp->port_mode == CHIP_2_PORT_MODE) {
1684
1685 port = 0;
1686 }
1687 ictx->xstorm_st_context.common.flags =
1688 1 << XSTORM_COMMON_CONTEXT_SECTION_PHYSQ_INITIALIZED_SHIFT;
1689 ictx->xstorm_st_context.common.flags =
1690 port << XSTORM_COMMON_CONTEXT_SECTION_PBF_PORT_SHIFT;
Michael Chan71034ba2009-10-10 13:46:59 +00001691
1692 ictx->tstorm_st_context.iscsi.hdr_bytes_2_fetch = ISCSI_HEADER_SIZE;
1693 /* TSTORM requires the base address of RQ DB & not PTE */
1694 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.lo =
1695 req2->rq_page_table_addr_lo & PAGE_MASK;
1696 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.hi =
1697 req2->rq_page_table_addr_hi;
1698 ictx->tstorm_st_context.iscsi.iscsi_conn_id = req1->iscsi_conn_id;
1699 ictx->tstorm_st_context.tcp.cwnd = 0x5A8;
1700 ictx->tstorm_st_context.tcp.flags2 |=
1701 TSTORM_TCP_ST_CONTEXT_SECTION_DA_EN;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001702 ictx->tstorm_st_context.tcp.ooo_support_mode =
1703 TCP_TSTORM_OOO_DROP_AND_PROC_ACK;
Michael Chan71034ba2009-10-10 13:46:59 +00001704
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001705 ictx->timers_context.flags |= TIMERS_BLOCK_CONTEXT_CONN_VALID_FLG;
Michael Chan71034ba2009-10-10 13:46:59 +00001706
1707 ictx->ustorm_st_context.ring.rq.pbl_base.lo =
Michael Chan15971c32009-12-02 15:15:38 +00001708 req2->rq_page_table_addr_lo;
Michael Chan71034ba2009-10-10 13:46:59 +00001709 ictx->ustorm_st_context.ring.rq.pbl_base.hi =
Michael Chan15971c32009-12-02 15:15:38 +00001710 req2->rq_page_table_addr_hi;
Michael Chan71034ba2009-10-10 13:46:59 +00001711 ictx->ustorm_st_context.ring.rq.curr_pbe.lo = req3->qp_first_pte[0].hi;
1712 ictx->ustorm_st_context.ring.rq.curr_pbe.hi = req3->qp_first_pte[0].lo;
1713 ictx->ustorm_st_context.ring.r2tq.pbl_base.lo =
1714 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1715 ictx->ustorm_st_context.ring.r2tq.pbl_base.hi =
1716 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1717 ictx->ustorm_st_context.ring.r2tq.curr_pbe.lo =
1718 iscsi->r2tq_info.pgtbl[0];
1719 ictx->ustorm_st_context.ring.r2tq.curr_pbe.hi =
1720 iscsi->r2tq_info.pgtbl[1];
1721 ictx->ustorm_st_context.ring.cq_pbl_base.lo =
1722 req1->cq_page_table_addr_lo;
1723 ictx->ustorm_st_context.ring.cq_pbl_base.hi =
1724 req1->cq_page_table_addr_hi;
1725 ictx->ustorm_st_context.ring.cq[0].cq_sn = ISCSI_INITIAL_SN;
1726 ictx->ustorm_st_context.ring.cq[0].curr_pbe.lo = req2->cq_first_pte.hi;
1727 ictx->ustorm_st_context.ring.cq[0].curr_pbe.hi = req2->cq_first_pte.lo;
1728 ictx->ustorm_st_context.task_pbe_cache_index =
1729 BNX2X_ISCSI_PBL_NOT_CACHED;
1730 ictx->ustorm_st_context.task_pdu_cache_index =
1731 BNX2X_ISCSI_PDU_HEADER_NOT_CACHED;
1732
1733 for (i = 1, j = 1; i < cp->num_cqs; i++, j++) {
1734 if (j == 3) {
1735 if (n >= n_max)
1736 break;
1737 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1738 j = 0;
1739 }
1740 ictx->ustorm_st_context.ring.cq[i].cq_sn = ISCSI_INITIAL_SN;
1741 ictx->ustorm_st_context.ring.cq[i].curr_pbe.lo =
1742 req3->qp_first_pte[j].hi;
1743 ictx->ustorm_st_context.ring.cq[i].curr_pbe.hi =
1744 req3->qp_first_pte[j].lo;
1745 }
1746
1747 ictx->ustorm_st_context.task_pbl_base.lo =
1748 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1749 ictx->ustorm_st_context.task_pbl_base.hi =
1750 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1751 ictx->ustorm_st_context.tce_phy_addr.lo =
1752 iscsi->task_array_info.pgtbl[0];
1753 ictx->ustorm_st_context.tce_phy_addr.hi =
1754 iscsi->task_array_info.pgtbl[1];
1755 ictx->ustorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1756 ictx->ustorm_st_context.num_cqs = cp->num_cqs;
1757 ictx->ustorm_st_context.negotiated_rx |= ISCSI_DEF_MAX_RECV_SEG_LEN;
1758 ictx->ustorm_st_context.negotiated_rx_and_flags |=
1759 ISCSI_DEF_MAX_BURST_LEN;
1760 ictx->ustorm_st_context.negotiated_rx |=
1761 ISCSI_DEFAULT_MAX_OUTSTANDING_R2T <<
1762 USTORM_ISCSI_ST_CONTEXT_MAX_OUTSTANDING_R2TS_SHIFT;
1763
1764 ictx->cstorm_st_context.hq_pbl_base.lo =
1765 iscsi->hq_info.pgtbl_map & 0xffffffff;
1766 ictx->cstorm_st_context.hq_pbl_base.hi =
1767 (u64) iscsi->hq_info.pgtbl_map >> 32;
1768 ictx->cstorm_st_context.hq_curr_pbe.lo = iscsi->hq_info.pgtbl[0];
1769 ictx->cstorm_st_context.hq_curr_pbe.hi = iscsi->hq_info.pgtbl[1];
1770 ictx->cstorm_st_context.task_pbl_base.lo =
1771 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1772 ictx->cstorm_st_context.task_pbl_base.hi =
1773 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1774 /* CSTORM and USTORM initialization is different, CSTORM requires
1775 * CQ DB base & not PTE addr */
1776 ictx->cstorm_st_context.cq_db_base.lo =
1777 req1->cq_page_table_addr_lo & PAGE_MASK;
1778 ictx->cstorm_st_context.cq_db_base.hi = req1->cq_page_table_addr_hi;
1779 ictx->cstorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1780 ictx->cstorm_st_context.cq_proc_en_bit_map = (1 << cp->num_cqs) - 1;
1781 for (i = 0; i < cp->num_cqs; i++) {
1782 ictx->cstorm_st_context.cq_c_prod_sqn_arr.sqn[i] =
1783 ISCSI_INITIAL_SN;
1784 ictx->cstorm_st_context.cq_c_sqn_2_notify_arr.sqn[i] =
1785 ISCSI_INITIAL_SN;
1786 }
1787
1788 ictx->xstorm_ag_context.cdu_reserved =
1789 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
1790 ISCSI_CONNECTION_TYPE);
1791 ictx->ustorm_ag_context.cdu_usage =
1792 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
1793 ISCSI_CONNECTION_TYPE);
1794 return 0;
1795
1796}
1797
1798static int cnic_bnx2x_iscsi_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
1799 u32 num, int *work)
1800{
1801 struct iscsi_kwqe_conn_offload1 *req1;
1802 struct iscsi_kwqe_conn_offload2 *req2;
1803 struct cnic_local *cp = dev->cnic_priv;
Michael Chanfdf24082010-10-13 14:06:47 +00001804 struct cnic_context *ctx;
Michael Chan71034ba2009-10-10 13:46:59 +00001805 struct iscsi_kcqe kcqe;
1806 struct kcqe *cqes[1];
1807 u32 l5_cid;
Michael Chanfdf24082010-10-13 14:06:47 +00001808 int ret = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001809
1810 if (num < 2) {
1811 *work = num;
1812 return -EINVAL;
1813 }
1814
1815 req1 = (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1816 req2 = (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1817 if ((num - 2) < req2->num_additional_wqes) {
1818 *work = num;
1819 return -EINVAL;
1820 }
Joe Perches779bb412010-11-14 17:04:37 +00001821 *work = 2 + req2->num_additional_wqes;
Michael Chan71034ba2009-10-10 13:46:59 +00001822
1823 l5_cid = req1->iscsi_conn_id;
1824 if (l5_cid >= MAX_ISCSI_TBL_SZ)
1825 return -EINVAL;
1826
1827 memset(&kcqe, 0, sizeof(kcqe));
1828 kcqe.op_code = ISCSI_KCQE_OPCODE_OFFLOAD_CONN;
1829 kcqe.iscsi_conn_id = l5_cid;
1830 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
1831
Michael Chanfdf24082010-10-13 14:06:47 +00001832 ctx = &cp->ctx_tbl[l5_cid];
1833 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags)) {
1834 kcqe.completion_status =
1835 ISCSI_KCQE_COMPLETION_STATUS_CID_BUSY;
1836 goto done;
1837 }
1838
Michael Chan71034ba2009-10-10 13:46:59 +00001839 if (atomic_inc_return(&cp->iscsi_conn) > dev->max_iscsi_conn) {
1840 atomic_dec(&cp->iscsi_conn);
Michael Chan71034ba2009-10-10 13:46:59 +00001841 goto done;
1842 }
1843 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
1844 if (ret) {
1845 atomic_dec(&cp->iscsi_conn);
1846 ret = 0;
1847 goto done;
1848 }
1849 ret = cnic_setup_bnx2x_ctx(dev, wqes, num);
1850 if (ret < 0) {
1851 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1852 atomic_dec(&cp->iscsi_conn);
1853 goto done;
1854 }
1855
1856 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001857 kcqe.iscsi_conn_context_id = BNX2X_HW_CID(cp, cp->ctx_tbl[l5_cid].cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001858
1859done:
1860 cqes[0] = (struct kcqe *) &kcqe;
1861 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
Michael Chan23021c22012-01-04 12:12:28 +00001862 return 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001863}
1864
1865
1866static int cnic_bnx2x_iscsi_update(struct cnic_dev *dev, struct kwqe *kwqe)
1867{
1868 struct cnic_local *cp = dev->cnic_priv;
1869 struct iscsi_kwqe_conn_update *req =
1870 (struct iscsi_kwqe_conn_update *) kwqe;
1871 void *data;
1872 union l5cm_specific_data l5_data;
1873 u32 l5_cid, cid = BNX2X_SW_CID(req->context_id);
1874 int ret;
1875
1876 if (cnic_get_l5_cid(cp, cid, &l5_cid) != 0)
1877 return -EINVAL;
1878
1879 data = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
1880 if (!data)
1881 return -ENOMEM;
1882
1883 memcpy(data, kwqe, sizeof(struct kwqe));
1884
1885 ret = cnic_submit_kwqe_16(dev, ISCSI_RAMROD_CMD_ID_UPDATE_CONN,
1886 req->context_id, ISCSI_CONNECTION_TYPE, &l5_data);
1887 return ret;
1888}
1889
Michael Chana2c9e762010-10-13 14:06:46 +00001890static int cnic_bnx2x_destroy_ramrod(struct cnic_dev *dev, u32 l5_cid)
Michael Chan71034ba2009-10-10 13:46:59 +00001891{
1892 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001893 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
Michael Chana2c9e762010-10-13 14:06:46 +00001894 union l5cm_specific_data l5_data;
1895 int ret;
Michael Chan68d7c1a2011-01-05 15:14:13 +00001896 u32 hw_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001897
Michael Chan71034ba2009-10-10 13:46:59 +00001898 init_waitqueue_head(&ctx->waitq);
1899 ctx->wait_cond = 0;
1900 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001901 hw_cid = BNX2X_HW_CID(cp, ctx->cid);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001902
1903 ret = cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
Michael Chan68d7c1a2011-01-05 15:14:13 +00001904 hw_cid, NONE_CONNECTION_TYPE, &l5_data);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001905
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001906 if (ret == 0) {
Michael Chandcc7e3a2011-08-26 09:45:40 +00001907 wait_event_timeout(ctx->waitq, ctx->wait_cond, CNIC_RAMROD_TMO);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001908 if (unlikely(test_bit(CTX_FL_CID_ERROR, &ctx->ctx_flags)))
1909 return -EBUSY;
1910 }
Michael Chan71034ba2009-10-10 13:46:59 +00001911
Michael Chandcc7e3a2011-08-26 09:45:40 +00001912 return 0;
Michael Chana2c9e762010-10-13 14:06:46 +00001913}
1914
1915static int cnic_bnx2x_iscsi_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
1916{
1917 struct cnic_local *cp = dev->cnic_priv;
1918 struct iscsi_kwqe_conn_destroy *req =
1919 (struct iscsi_kwqe_conn_destroy *) kwqe;
1920 u32 l5_cid = req->reserved0;
1921 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1922 int ret = 0;
1923 struct iscsi_kcqe kcqe;
1924 struct kcqe *cqes[1];
1925
1926 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
1927 goto skip_cfc_delete;
1928
Michael Chanfdf24082010-10-13 14:06:47 +00001929 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
1930 unsigned long delta = ctx->timestamp + (2 * HZ) - jiffies;
1931
1932 if (delta > (2 * HZ))
1933 delta = 0;
1934
1935 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
1936 queue_delayed_work(cnic_wq, &cp->delete_task, delta);
1937 goto destroy_reply;
1938 }
Michael Chana2c9e762010-10-13 14:06:46 +00001939
1940 ret = cnic_bnx2x_destroy_ramrod(dev, l5_cid);
1941
Michael Chan71034ba2009-10-10 13:46:59 +00001942skip_cfc_delete:
1943 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1944
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001945 if (!ret) {
1946 atomic_dec(&cp->iscsi_conn);
1947 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
1948 }
Michael Chan71034ba2009-10-10 13:46:59 +00001949
Michael Chanfdf24082010-10-13 14:06:47 +00001950destroy_reply:
Michael Chan71034ba2009-10-10 13:46:59 +00001951 memset(&kcqe, 0, sizeof(kcqe));
1952 kcqe.op_code = ISCSI_KCQE_OPCODE_DESTROY_CONN;
1953 kcqe.iscsi_conn_id = l5_cid;
1954 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1955 kcqe.iscsi_conn_context_id = req->context_id;
1956
1957 cqes[0] = (struct kcqe *) &kcqe;
1958 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1959
Michael Chan23021c22012-01-04 12:12:28 +00001960 return 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001961}
1962
1963static void cnic_init_storm_conn_bufs(struct cnic_dev *dev,
1964 struct l4_kwq_connect_req1 *kwqe1,
1965 struct l4_kwq_connect_req3 *kwqe3,
1966 struct l5cm_active_conn_buffer *conn_buf)
1967{
1968 struct l5cm_conn_addr_params *conn_addr = &conn_buf->conn_addr_buf;
1969 struct l5cm_xstorm_conn_buffer *xstorm_buf =
1970 &conn_buf->xstorm_conn_buffer;
1971 struct l5cm_tstorm_conn_buffer *tstorm_buf =
1972 &conn_buf->tstorm_conn_buffer;
1973 struct regpair context_addr;
1974 u32 cid = BNX2X_SW_CID(kwqe1->cid);
1975 struct in6_addr src_ip, dst_ip;
1976 int i;
1977 u32 *addrp;
1978
1979 addrp = (u32 *) &conn_addr->local_ip_addr;
1980 for (i = 0; i < 4; i++, addrp++)
1981 src_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1982
1983 addrp = (u32 *) &conn_addr->remote_ip_addr;
1984 for (i = 0; i < 4; i++, addrp++)
1985 dst_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1986
1987 cnic_get_bnx2x_ctx(dev, cid, 0, &context_addr);
1988
1989 xstorm_buf->context_addr.hi = context_addr.hi;
1990 xstorm_buf->context_addr.lo = context_addr.lo;
1991 xstorm_buf->mss = 0xffff;
1992 xstorm_buf->rcv_buf = kwqe3->rcv_buf;
1993 if (kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE)
1994 xstorm_buf->params |= L5CM_XSTORM_CONN_BUFFER_NAGLE_ENABLE;
1995 xstorm_buf->pseudo_header_checksum =
1996 swab16(~csum_ipv6_magic(&src_ip, &dst_ip, 0, IPPROTO_TCP, 0));
1997
1998 if (!(kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK))
1999 tstorm_buf->params |=
2000 L5CM_TSTORM_CONN_BUFFER_DELAYED_ACK_ENABLE;
2001 if (kwqe3->ka_timeout) {
2002 tstorm_buf->ka_enable = 1;
2003 tstorm_buf->ka_timeout = kwqe3->ka_timeout;
2004 tstorm_buf->ka_interval = kwqe3->ka_interval;
2005 tstorm_buf->ka_max_probe_count = kwqe3->ka_max_probe_count;
2006 }
Michael Chan71034ba2009-10-10 13:46:59 +00002007 tstorm_buf->max_rt_time = 0xffffffff;
2008}
2009
2010static void cnic_init_bnx2x_mac(struct cnic_dev *dev)
2011{
2012 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00002013 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00002014 u8 *mac = dev->mac_addr;
2015
2016 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002017 XSTORM_ISCSI_LOCAL_MAC_ADDR0_OFFSET(pfid), mac[0]);
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_ADDR1_OFFSET(pfid), mac[1]);
Michael Chan71034ba2009-10-10 13:46:59 +00002020 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002021 XSTORM_ISCSI_LOCAL_MAC_ADDR2_OFFSET(pfid), mac[2]);
Michael Chan71034ba2009-10-10 13:46:59 +00002022 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002023 XSTORM_ISCSI_LOCAL_MAC_ADDR3_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00002024 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002025 XSTORM_ISCSI_LOCAL_MAC_ADDR4_OFFSET(pfid), mac[4]);
Michael Chan71034ba2009-10-10 13:46:59 +00002026 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002027 XSTORM_ISCSI_LOCAL_MAC_ADDR5_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00002028
2029 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002030 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00002031 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002032 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00002033 mac[4]);
2034 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002035 TSTORM_ISCSI_TCP_VARS_MID_LOCAL_MAC_ADDR_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00002036 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002037 TSTORM_ISCSI_TCP_VARS_MID_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00002038 mac[2]);
2039 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002040 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[1]);
Michael Chan71034ba2009-10-10 13:46:59 +00002041 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002042 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00002043 mac[0]);
2044}
2045
2046static void cnic_bnx2x_set_tcp_timestamp(struct cnic_dev *dev, int tcp_ts)
2047{
2048 struct cnic_local *cp = dev->cnic_priv;
2049 u8 xstorm_flags = XSTORM_L5CM_TCP_FLAGS_WND_SCL_EN;
2050 u16 tstorm_flags = 0;
2051
2052 if (tcp_ts) {
2053 xstorm_flags |= XSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
2054 tstorm_flags |= TSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
2055 }
2056
2057 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002058 XSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), xstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002059
2060 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002061 TSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), tstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002062}
2063
2064static int cnic_bnx2x_connect(struct cnic_dev *dev, struct kwqe *wqes[],
2065 u32 num, int *work)
2066{
2067 struct cnic_local *cp = dev->cnic_priv;
2068 struct l4_kwq_connect_req1 *kwqe1 =
2069 (struct l4_kwq_connect_req1 *) wqes[0];
2070 struct l4_kwq_connect_req3 *kwqe3;
2071 struct l5cm_active_conn_buffer *conn_buf;
2072 struct l5cm_conn_addr_params *conn_addr;
2073 union l5cm_specific_data l5_data;
2074 u32 l5_cid = kwqe1->pg_cid;
2075 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
2076 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
2077 int ret;
2078
2079 if (num < 2) {
2080 *work = num;
2081 return -EINVAL;
2082 }
2083
2084 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6)
2085 *work = 3;
2086 else
2087 *work = 2;
2088
2089 if (num < *work) {
2090 *work = num;
2091 return -EINVAL;
2092 }
2093
2094 if (sizeof(*conn_buf) > CNIC_KWQ16_DATA_SIZE) {
Joe Perchesddf79b22010-02-17 15:01:54 +00002095 netdev_err(dev->netdev, "conn_buf size too big\n");
Michael Chan71034ba2009-10-10 13:46:59 +00002096 return -ENOMEM;
2097 }
2098 conn_buf = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2099 if (!conn_buf)
2100 return -ENOMEM;
2101
2102 memset(conn_buf, 0, sizeof(*conn_buf));
2103
2104 conn_addr = &conn_buf->conn_addr_buf;
2105 conn_addr->remote_addr_0 = csk->ha[0];
2106 conn_addr->remote_addr_1 = csk->ha[1];
2107 conn_addr->remote_addr_2 = csk->ha[2];
2108 conn_addr->remote_addr_3 = csk->ha[3];
2109 conn_addr->remote_addr_4 = csk->ha[4];
2110 conn_addr->remote_addr_5 = csk->ha[5];
2111
2112 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6) {
2113 struct l4_kwq_connect_req2 *kwqe2 =
2114 (struct l4_kwq_connect_req2 *) wqes[1];
2115
2116 conn_addr->local_ip_addr.ip_addr_hi_hi = kwqe2->src_ip_v6_4;
2117 conn_addr->local_ip_addr.ip_addr_hi_lo = kwqe2->src_ip_v6_3;
2118 conn_addr->local_ip_addr.ip_addr_lo_hi = kwqe2->src_ip_v6_2;
2119
2120 conn_addr->remote_ip_addr.ip_addr_hi_hi = kwqe2->dst_ip_v6_4;
2121 conn_addr->remote_ip_addr.ip_addr_hi_lo = kwqe2->dst_ip_v6_3;
2122 conn_addr->remote_ip_addr.ip_addr_lo_hi = kwqe2->dst_ip_v6_2;
2123 conn_addr->params |= L5CM_CONN_ADDR_PARAMS_IP_VERSION;
2124 }
2125 kwqe3 = (struct l4_kwq_connect_req3 *) wqes[*work - 1];
2126
2127 conn_addr->local_ip_addr.ip_addr_lo_lo = kwqe1->src_ip;
2128 conn_addr->remote_ip_addr.ip_addr_lo_lo = kwqe1->dst_ip;
2129 conn_addr->local_tcp_port = kwqe1->src_port;
2130 conn_addr->remote_tcp_port = kwqe1->dst_port;
2131
2132 conn_addr->pmtu = kwqe3->pmtu;
2133 cnic_init_storm_conn_bufs(dev, kwqe1, kwqe3, conn_buf);
2134
2135 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002136 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(cp->pfid), csk->vlan_id);
Michael Chan71034ba2009-10-10 13:46:59 +00002137
2138 cnic_bnx2x_set_tcp_timestamp(dev,
2139 kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_TIME_STAMP);
2140
2141 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_TCP_CONNECT,
2142 kwqe1->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2143 if (!ret)
Michael Chan6e0dda02010-10-13 14:06:45 +00002144 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002145
2146 return ret;
2147}
2148
2149static int cnic_bnx2x_close(struct cnic_dev *dev, struct kwqe *kwqe)
2150{
2151 struct l4_kwq_close_req *req = (struct l4_kwq_close_req *) kwqe;
2152 union l5cm_specific_data l5_data;
2153 int ret;
2154
2155 memset(&l5_data, 0, sizeof(l5_data));
2156 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_CLOSE,
2157 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2158 return ret;
2159}
2160
2161static int cnic_bnx2x_reset(struct cnic_dev *dev, struct kwqe *kwqe)
2162{
2163 struct l4_kwq_reset_req *req = (struct l4_kwq_reset_req *) kwqe;
2164 union l5cm_specific_data l5_data;
2165 int ret;
2166
2167 memset(&l5_data, 0, sizeof(l5_data));
2168 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_ABORT,
2169 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2170 return ret;
2171}
2172static int cnic_bnx2x_offload_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2173{
2174 struct l4_kwq_offload_pg *req = (struct l4_kwq_offload_pg *) kwqe;
2175 struct l4_kcq kcqe;
2176 struct kcqe *cqes[1];
2177
2178 memset(&kcqe, 0, sizeof(kcqe));
2179 kcqe.pg_host_opaque = req->host_opaque;
2180 kcqe.pg_cid = req->host_opaque;
2181 kcqe.op_code = L4_KCQE_OPCODE_VALUE_OFFLOAD_PG;
2182 cqes[0] = (struct kcqe *) &kcqe;
2183 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2184 return 0;
2185}
2186
2187static int cnic_bnx2x_update_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2188{
2189 struct l4_kwq_update_pg *req = (struct l4_kwq_update_pg *) kwqe;
2190 struct l4_kcq kcqe;
2191 struct kcqe *cqes[1];
2192
2193 memset(&kcqe, 0, sizeof(kcqe));
2194 kcqe.pg_host_opaque = req->pg_host_opaque;
2195 kcqe.pg_cid = req->pg_cid;
2196 kcqe.op_code = L4_KCQE_OPCODE_VALUE_UPDATE_PG;
2197 cqes[0] = (struct kcqe *) &kcqe;
2198 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2199 return 0;
2200}
2201
Michael Chane1928c82010-12-23 07:43:04 +00002202static int cnic_bnx2x_fcoe_stat(struct cnic_dev *dev, struct kwqe *kwqe)
2203{
2204 struct fcoe_kwqe_stat *req;
2205 struct fcoe_stat_ramrod_params *fcoe_stat;
2206 union l5cm_specific_data l5_data;
2207 struct cnic_local *cp = dev->cnic_priv;
2208 int ret;
2209 u32 cid;
2210
2211 req = (struct fcoe_kwqe_stat *) kwqe;
2212 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2213
2214 fcoe_stat = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2215 if (!fcoe_stat)
2216 return -ENOMEM;
2217
2218 memset(fcoe_stat, 0, sizeof(*fcoe_stat));
2219 memcpy(&fcoe_stat->stat_kwqe, req, sizeof(*req));
2220
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002221 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_STAT_FUNC, cid,
Michael Chane1928c82010-12-23 07:43:04 +00002222 FCOE_CONNECTION_TYPE, &l5_data);
2223 return ret;
2224}
2225
2226static int cnic_bnx2x_fcoe_init1(struct cnic_dev *dev, struct kwqe *wqes[],
2227 u32 num, int *work)
2228{
2229 int ret;
2230 struct cnic_local *cp = dev->cnic_priv;
2231 u32 cid;
2232 struct fcoe_init_ramrod_params *fcoe_init;
2233 struct fcoe_kwqe_init1 *req1;
2234 struct fcoe_kwqe_init2 *req2;
2235 struct fcoe_kwqe_init3 *req3;
2236 union l5cm_specific_data l5_data;
2237
2238 if (num < 3) {
2239 *work = num;
2240 return -EINVAL;
2241 }
2242 req1 = (struct fcoe_kwqe_init1 *) wqes[0];
2243 req2 = (struct fcoe_kwqe_init2 *) wqes[1];
2244 req3 = (struct fcoe_kwqe_init3 *) wqes[2];
2245 if (req2->hdr.op_code != FCOE_KWQE_OPCODE_INIT2) {
2246 *work = 1;
2247 return -EINVAL;
2248 }
2249 if (req3->hdr.op_code != FCOE_KWQE_OPCODE_INIT3) {
2250 *work = 2;
2251 return -EINVAL;
2252 }
2253
2254 if (sizeof(*fcoe_init) > CNIC_KWQ16_DATA_SIZE) {
2255 netdev_err(dev->netdev, "fcoe_init size too big\n");
2256 return -ENOMEM;
2257 }
2258 fcoe_init = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2259 if (!fcoe_init)
2260 return -ENOMEM;
2261
2262 memset(fcoe_init, 0, sizeof(*fcoe_init));
2263 memcpy(&fcoe_init->init_kwqe1, req1, sizeof(*req1));
2264 memcpy(&fcoe_init->init_kwqe2, req2, sizeof(*req2));
2265 memcpy(&fcoe_init->init_kwqe3, req3, sizeof(*req3));
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002266 fcoe_init->eq_pbl_base.lo = cp->kcq2.dma.pgtbl_map & 0xffffffff;
2267 fcoe_init->eq_pbl_base.hi = (u64) cp->kcq2.dma.pgtbl_map >> 32;
2268 fcoe_init->eq_pbl_size = cp->kcq2.dma.num_pages;
Michael Chane1928c82010-12-23 07:43:04 +00002269
2270 fcoe_init->sb_num = cp->status_blk_num;
2271 fcoe_init->eq_prod = MAX_KCQ_IDX;
2272 fcoe_init->sb_id = HC_INDEX_FCOE_EQ_CONS;
2273 cp->kcq2.sw_prod_idx = 0;
2274
2275 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002276 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_INIT_FUNC, cid,
Michael Chane1928c82010-12-23 07:43:04 +00002277 FCOE_CONNECTION_TYPE, &l5_data);
2278 *work = 3;
2279 return ret;
2280}
2281
2282static int cnic_bnx2x_fcoe_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
2283 u32 num, int *work)
2284{
2285 int ret = 0;
2286 u32 cid = -1, l5_cid;
2287 struct cnic_local *cp = dev->cnic_priv;
2288 struct fcoe_kwqe_conn_offload1 *req1;
2289 struct fcoe_kwqe_conn_offload2 *req2;
2290 struct fcoe_kwqe_conn_offload3 *req3;
2291 struct fcoe_kwqe_conn_offload4 *req4;
2292 struct fcoe_conn_offload_ramrod_params *fcoe_offload;
2293 struct cnic_context *ctx;
2294 struct fcoe_context *fctx;
2295 struct regpair ctx_addr;
2296 union l5cm_specific_data l5_data;
2297 struct fcoe_kcqe kcqe;
2298 struct kcqe *cqes[1];
2299
2300 if (num < 4) {
2301 *work = num;
2302 return -EINVAL;
2303 }
2304 req1 = (struct fcoe_kwqe_conn_offload1 *) wqes[0];
2305 req2 = (struct fcoe_kwqe_conn_offload2 *) wqes[1];
2306 req3 = (struct fcoe_kwqe_conn_offload3 *) wqes[2];
2307 req4 = (struct fcoe_kwqe_conn_offload4 *) wqes[3];
2308
2309 *work = 4;
2310
2311 l5_cid = req1->fcoe_conn_id;
Michael Chandc219a22011-08-26 09:45:39 +00002312 if (l5_cid >= dev->max_fcoe_conn)
Michael Chane1928c82010-12-23 07:43:04 +00002313 goto err_reply;
2314
2315 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2316
2317 ctx = &cp->ctx_tbl[l5_cid];
2318 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2319 goto err_reply;
2320
2321 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
2322 if (ret) {
2323 ret = 0;
2324 goto err_reply;
2325 }
2326 cid = ctx->cid;
2327
2328 fctx = cnic_get_bnx2x_ctx(dev, cid, 1, &ctx_addr);
2329 if (fctx) {
2330 u32 hw_cid = BNX2X_HW_CID(cp, cid);
2331 u32 val;
2332
2333 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
2334 FCOE_CONNECTION_TYPE);
2335 fctx->xstorm_ag_context.cdu_reserved = val;
2336 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
2337 FCOE_CONNECTION_TYPE);
2338 fctx->ustorm_ag_context.cdu_usage = val;
2339 }
2340 if (sizeof(*fcoe_offload) > CNIC_KWQ16_DATA_SIZE) {
2341 netdev_err(dev->netdev, "fcoe_offload size too big\n");
2342 goto err_reply;
2343 }
2344 fcoe_offload = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2345 if (!fcoe_offload)
2346 goto err_reply;
2347
2348 memset(fcoe_offload, 0, sizeof(*fcoe_offload));
2349 memcpy(&fcoe_offload->offload_kwqe1, req1, sizeof(*req1));
2350 memcpy(&fcoe_offload->offload_kwqe2, req2, sizeof(*req2));
2351 memcpy(&fcoe_offload->offload_kwqe3, req3, sizeof(*req3));
2352 memcpy(&fcoe_offload->offload_kwqe4, req4, sizeof(*req4));
2353
2354 cid = BNX2X_HW_CID(cp, cid);
2355 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_OFFLOAD_CONN, cid,
2356 FCOE_CONNECTION_TYPE, &l5_data);
2357 if (!ret)
2358 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
2359
2360 return ret;
2361
2362err_reply:
2363 if (cid != -1)
2364 cnic_free_bnx2x_conn_resc(dev, l5_cid);
2365
2366 memset(&kcqe, 0, sizeof(kcqe));
2367 kcqe.op_code = FCOE_KCQE_OPCODE_OFFLOAD_CONN;
2368 kcqe.fcoe_conn_id = req1->fcoe_conn_id;
2369 kcqe.completion_status = FCOE_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
2370
2371 cqes[0] = (struct kcqe *) &kcqe;
2372 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2373 return ret;
2374}
2375
2376static int cnic_bnx2x_fcoe_enable(struct cnic_dev *dev, struct kwqe *kwqe)
2377{
2378 struct fcoe_kwqe_conn_enable_disable *req;
2379 struct fcoe_conn_enable_disable_ramrod_params *fcoe_enable;
2380 union l5cm_specific_data l5_data;
2381 int ret;
2382 u32 cid, l5_cid;
2383 struct cnic_local *cp = dev->cnic_priv;
2384
2385 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2386 cid = req->context_id;
2387 l5_cid = req->conn_id + BNX2X_FCOE_L5_CID_BASE;
2388
2389 if (sizeof(*fcoe_enable) > CNIC_KWQ16_DATA_SIZE) {
2390 netdev_err(dev->netdev, "fcoe_enable size too big\n");
2391 return -ENOMEM;
2392 }
2393 fcoe_enable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2394 if (!fcoe_enable)
2395 return -ENOMEM;
2396
2397 memset(fcoe_enable, 0, sizeof(*fcoe_enable));
2398 memcpy(&fcoe_enable->enable_disable_kwqe, req, sizeof(*req));
2399 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_ENABLE_CONN, cid,
2400 FCOE_CONNECTION_TYPE, &l5_data);
2401 return ret;
2402}
2403
2404static int cnic_bnx2x_fcoe_disable(struct cnic_dev *dev, struct kwqe *kwqe)
2405{
2406 struct fcoe_kwqe_conn_enable_disable *req;
2407 struct fcoe_conn_enable_disable_ramrod_params *fcoe_disable;
2408 union l5cm_specific_data l5_data;
2409 int ret;
2410 u32 cid, l5_cid;
2411 struct cnic_local *cp = dev->cnic_priv;
2412
2413 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2414 cid = req->context_id;
2415 l5_cid = req->conn_id;
Michael Chandc219a22011-08-26 09:45:39 +00002416 if (l5_cid >= dev->max_fcoe_conn)
Michael Chane1928c82010-12-23 07:43:04 +00002417 return -EINVAL;
2418
2419 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2420
2421 if (sizeof(*fcoe_disable) > CNIC_KWQ16_DATA_SIZE) {
2422 netdev_err(dev->netdev, "fcoe_disable size too big\n");
2423 return -ENOMEM;
2424 }
2425 fcoe_disable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2426 if (!fcoe_disable)
2427 return -ENOMEM;
2428
2429 memset(fcoe_disable, 0, sizeof(*fcoe_disable));
2430 memcpy(&fcoe_disable->enable_disable_kwqe, req, sizeof(*req));
2431 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DISABLE_CONN, cid,
2432 FCOE_CONNECTION_TYPE, &l5_data);
2433 return ret;
2434}
2435
2436static int cnic_bnx2x_fcoe_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2437{
2438 struct fcoe_kwqe_conn_destroy *req;
2439 union l5cm_specific_data l5_data;
2440 int ret;
2441 u32 cid, l5_cid;
2442 struct cnic_local *cp = dev->cnic_priv;
2443 struct cnic_context *ctx;
2444 struct fcoe_kcqe kcqe;
2445 struct kcqe *cqes[1];
2446
2447 req = (struct fcoe_kwqe_conn_destroy *) kwqe;
2448 cid = req->context_id;
2449 l5_cid = req->conn_id;
Michael Chandc219a22011-08-26 09:45:39 +00002450 if (l5_cid >= dev->max_fcoe_conn)
Michael Chane1928c82010-12-23 07:43:04 +00002451 return -EINVAL;
2452
2453 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2454
2455 ctx = &cp->ctx_tbl[l5_cid];
2456
2457 init_waitqueue_head(&ctx->waitq);
2458 ctx->wait_cond = 0;
2459
Michael Chandcc7e3a2011-08-26 09:45:40 +00002460 memset(&kcqe, 0, sizeof(kcqe));
2461 kcqe.completion_status = FCOE_KCQE_COMPLETION_STATUS_ERROR;
Michael Chane1928c82010-12-23 07:43:04 +00002462 memset(&l5_data, 0, sizeof(l5_data));
2463 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_TERMINATE_CONN, cid,
2464 FCOE_CONNECTION_TYPE, &l5_data);
2465 if (ret == 0) {
Michael Chandcc7e3a2011-08-26 09:45:40 +00002466 wait_event_timeout(ctx->waitq, ctx->wait_cond, CNIC_RAMROD_TMO);
2467 if (ctx->wait_cond)
2468 kcqe.completion_status = 0;
Michael Chane1928c82010-12-23 07:43:04 +00002469 }
2470
Michael Chandcc7e3a2011-08-26 09:45:40 +00002471 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
2472 queue_delayed_work(cnic_wq, &cp->delete_task, msecs_to_jiffies(2000));
2473
Michael Chane1928c82010-12-23 07:43:04 +00002474 kcqe.op_code = FCOE_KCQE_OPCODE_DESTROY_CONN;
2475 kcqe.fcoe_conn_id = req->conn_id;
2476 kcqe.fcoe_conn_context_id = cid;
2477
2478 cqes[0] = (struct kcqe *) &kcqe;
2479 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2480 return ret;
2481}
2482
Michael Chan74e49bb2011-07-20 14:55:23 +00002483static void cnic_bnx2x_delete_wait(struct cnic_dev *dev, u32 start_cid)
2484{
2485 struct cnic_local *cp = dev->cnic_priv;
2486 u32 i;
2487
2488 for (i = start_cid; i < cp->max_cid_space; i++) {
2489 struct cnic_context *ctx = &cp->ctx_tbl[i];
2490 int j;
2491
2492 while (test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
2493 msleep(10);
2494
2495 for (j = 0; j < 5; j++) {
2496 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2497 break;
2498 msleep(20);
2499 }
2500
2501 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2502 netdev_warn(dev->netdev, "CID %x not deleted\n",
2503 ctx->cid);
2504 }
2505}
2506
Michael Chane1928c82010-12-23 07:43:04 +00002507static int cnic_bnx2x_fcoe_fw_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2508{
2509 struct fcoe_kwqe_destroy *req;
2510 union l5cm_specific_data l5_data;
2511 struct cnic_local *cp = dev->cnic_priv;
2512 int ret;
2513 u32 cid;
2514
Michael Chan74e49bb2011-07-20 14:55:23 +00002515 cnic_bnx2x_delete_wait(dev, MAX_ISCSI_TBL_SZ);
2516
Michael Chane1928c82010-12-23 07:43:04 +00002517 req = (struct fcoe_kwqe_destroy *) kwqe;
2518 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2519
2520 memset(&l5_data, 0, sizeof(l5_data));
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002521 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DESTROY_FUNC, cid,
Michael Chane1928c82010-12-23 07:43:04 +00002522 FCOE_CONNECTION_TYPE, &l5_data);
2523 return ret;
2524}
2525
Michael Chan23021c22012-01-04 12:12:28 +00002526static void cnic_bnx2x_kwqe_err(struct cnic_dev *dev, struct kwqe *kwqe)
2527{
2528 struct cnic_local *cp = dev->cnic_priv;
2529 struct kcqe kcqe;
2530 struct kcqe *cqes[1];
2531 u32 cid;
2532 u32 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2533 u32 layer_code = kwqe->kwqe_op_flag & KWQE_LAYER_MASK;
Michael Chan3238a9b2012-02-05 15:24:40 +00002534 u32 kcqe_op;
Michael Chan23021c22012-01-04 12:12:28 +00002535 int ulp_type;
2536
2537 cid = kwqe->kwqe_info0;
2538 memset(&kcqe, 0, sizeof(kcqe));
2539
Michael Chan3238a9b2012-02-05 15:24:40 +00002540 if (layer_code == KWQE_FLAGS_LAYER_MASK_L5_FCOE) {
2541 u32 l5_cid = 0;
2542
2543 ulp_type = CNIC_ULP_FCOE;
2544 if (opcode == FCOE_KWQE_OPCODE_DISABLE_CONN) {
2545 struct fcoe_kwqe_conn_enable_disable *req;
2546
2547 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2548 kcqe_op = FCOE_KCQE_OPCODE_DISABLE_CONN;
2549 cid = req->context_id;
2550 l5_cid = req->conn_id;
2551 } else if (opcode == FCOE_KWQE_OPCODE_DESTROY) {
2552 kcqe_op = FCOE_KCQE_OPCODE_DESTROY_FUNC;
2553 } else {
2554 return;
2555 }
2556 kcqe.kcqe_op_flag = kcqe_op << KCQE_FLAGS_OPCODE_SHIFT;
2557 kcqe.kcqe_op_flag |= KCQE_FLAGS_LAYER_MASK_L5_FCOE;
Michael Chan8ec3e702012-03-21 15:38:34 +00002558 kcqe.kcqe_info1 = FCOE_KCQE_COMPLETION_STATUS_PARITY_ERROR;
Michael Chan3238a9b2012-02-05 15:24:40 +00002559 kcqe.kcqe_info2 = cid;
2560 kcqe.kcqe_info0 = l5_cid;
2561
2562 } else if (layer_code == KWQE_FLAGS_LAYER_MASK_L5_ISCSI) {
Michael Chan23021c22012-01-04 12:12:28 +00002563 ulp_type = CNIC_ULP_ISCSI;
2564 if (opcode == ISCSI_KWQE_OPCODE_UPDATE_CONN)
2565 cid = kwqe->kwqe_info1;
2566
2567 kcqe.kcqe_op_flag = (opcode + 0x10) << KCQE_FLAGS_OPCODE_SHIFT;
2568 kcqe.kcqe_op_flag |= KCQE_FLAGS_LAYER_MASK_L5_ISCSI;
Michael Chan8ec3e702012-03-21 15:38:34 +00002569 kcqe.kcqe_info1 = ISCSI_KCQE_COMPLETION_STATUS_PARITY_ERR;
Michael Chan23021c22012-01-04 12:12:28 +00002570 kcqe.kcqe_info2 = cid;
2571 cnic_get_l5_cid(cp, BNX2X_SW_CID(cid), &kcqe.kcqe_info0);
2572
2573 } else if (layer_code == KWQE_FLAGS_LAYER_MASK_L4) {
2574 struct l4_kcq *l4kcqe = (struct l4_kcq *) &kcqe;
Michael Chan23021c22012-01-04 12:12:28 +00002575
2576 ulp_type = CNIC_ULP_L4;
2577 if (opcode == L4_KWQE_OPCODE_VALUE_CONNECT1)
2578 kcqe_op = L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE;
2579 else if (opcode == L4_KWQE_OPCODE_VALUE_RESET)
2580 kcqe_op = L4_KCQE_OPCODE_VALUE_RESET_COMP;
2581 else if (opcode == L4_KWQE_OPCODE_VALUE_CLOSE)
2582 kcqe_op = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
2583 else
2584 return;
2585
2586 kcqe.kcqe_op_flag = (kcqe_op << KCQE_FLAGS_OPCODE_SHIFT) |
2587 KCQE_FLAGS_LAYER_MASK_L4;
Michael Chan8ec3e702012-03-21 15:38:34 +00002588 l4kcqe->status = L4_KCQE_COMPLETION_STATUS_PARITY_ERROR;
Michael Chan23021c22012-01-04 12:12:28 +00002589 l4kcqe->cid = cid;
2590 cnic_get_l5_cid(cp, BNX2X_SW_CID(cid), &l4kcqe->conn_id);
2591 } else {
2592 return;
2593 }
2594
Joe Perches64699332012-06-04 12:44:16 +00002595 cqes[0] = &kcqe;
Michael Chan23021c22012-01-04 12:12:28 +00002596 cnic_reply_bnx2x_kcqes(dev, ulp_type, cqes, 1);
2597}
2598
Michael Chane1928c82010-12-23 07:43:04 +00002599static int cnic_submit_bnx2x_iscsi_kwqes(struct cnic_dev *dev,
2600 struct kwqe *wqes[], u32 num_wqes)
Michael Chan71034ba2009-10-10 13:46:59 +00002601{
2602 int i, work, ret;
2603 u32 opcode;
2604 struct kwqe *kwqe;
2605
2606 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2607 return -EAGAIN; /* bnx2 is down */
2608
2609 for (i = 0; i < num_wqes; ) {
2610 kwqe = wqes[i];
2611 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2612 work = 1;
2613
2614 switch (opcode) {
2615 case ISCSI_KWQE_OPCODE_INIT1:
2616 ret = cnic_bnx2x_iscsi_init1(dev, kwqe);
2617 break;
2618 case ISCSI_KWQE_OPCODE_INIT2:
2619 ret = cnic_bnx2x_iscsi_init2(dev, kwqe);
2620 break;
2621 case ISCSI_KWQE_OPCODE_OFFLOAD_CONN1:
2622 ret = cnic_bnx2x_iscsi_ofld1(dev, &wqes[i],
2623 num_wqes - i, &work);
2624 break;
2625 case ISCSI_KWQE_OPCODE_UPDATE_CONN:
2626 ret = cnic_bnx2x_iscsi_update(dev, kwqe);
2627 break;
2628 case ISCSI_KWQE_OPCODE_DESTROY_CONN:
2629 ret = cnic_bnx2x_iscsi_destroy(dev, kwqe);
2630 break;
2631 case L4_KWQE_OPCODE_VALUE_CONNECT1:
2632 ret = cnic_bnx2x_connect(dev, &wqes[i], num_wqes - i,
2633 &work);
2634 break;
2635 case L4_KWQE_OPCODE_VALUE_CLOSE:
2636 ret = cnic_bnx2x_close(dev, kwqe);
2637 break;
2638 case L4_KWQE_OPCODE_VALUE_RESET:
2639 ret = cnic_bnx2x_reset(dev, kwqe);
2640 break;
2641 case L4_KWQE_OPCODE_VALUE_OFFLOAD_PG:
2642 ret = cnic_bnx2x_offload_pg(dev, kwqe);
2643 break;
2644 case L4_KWQE_OPCODE_VALUE_UPDATE_PG:
2645 ret = cnic_bnx2x_update_pg(dev, kwqe);
2646 break;
2647 case L4_KWQE_OPCODE_VALUE_UPLOAD_PG:
2648 ret = 0;
2649 break;
2650 default:
2651 ret = 0;
Joe Perchesddf79b22010-02-17 15:01:54 +00002652 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2653 opcode);
Michael Chan71034ba2009-10-10 13:46:59 +00002654 break;
2655 }
Michael Chan23021c22012-01-04 12:12:28 +00002656 if (ret < 0) {
Joe Perchesddf79b22010-02-17 15:01:54 +00002657 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2658 opcode);
Michael Chan23021c22012-01-04 12:12:28 +00002659
2660 /* Possibly bnx2x parity error, send completion
2661 * to ulp drivers with error code to speed up
2662 * cleanup and reset recovery.
2663 */
2664 if (ret == -EIO || ret == -EAGAIN)
2665 cnic_bnx2x_kwqe_err(dev, kwqe);
2666 }
Michael Chan71034ba2009-10-10 13:46:59 +00002667 i += work;
2668 }
2669 return 0;
2670}
2671
Michael Chane1928c82010-12-23 07:43:04 +00002672static int cnic_submit_bnx2x_fcoe_kwqes(struct cnic_dev *dev,
2673 struct kwqe *wqes[], u32 num_wqes)
2674{
2675 struct cnic_local *cp = dev->cnic_priv;
2676 int i, work, ret;
2677 u32 opcode;
2678 struct kwqe *kwqe;
2679
2680 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2681 return -EAGAIN; /* bnx2 is down */
2682
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002683 if (!BNX2X_CHIP_IS_E2_PLUS(cp->chip_id))
Michael Chane1928c82010-12-23 07:43:04 +00002684 return -EINVAL;
2685
2686 for (i = 0; i < num_wqes; ) {
2687 kwqe = wqes[i];
2688 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2689 work = 1;
2690
2691 switch (opcode) {
2692 case FCOE_KWQE_OPCODE_INIT1:
2693 ret = cnic_bnx2x_fcoe_init1(dev, &wqes[i],
2694 num_wqes - i, &work);
2695 break;
2696 case FCOE_KWQE_OPCODE_OFFLOAD_CONN1:
2697 ret = cnic_bnx2x_fcoe_ofld1(dev, &wqes[i],
2698 num_wqes - i, &work);
2699 break;
2700 case FCOE_KWQE_OPCODE_ENABLE_CONN:
2701 ret = cnic_bnx2x_fcoe_enable(dev, kwqe);
2702 break;
2703 case FCOE_KWQE_OPCODE_DISABLE_CONN:
2704 ret = cnic_bnx2x_fcoe_disable(dev, kwqe);
2705 break;
2706 case FCOE_KWQE_OPCODE_DESTROY_CONN:
2707 ret = cnic_bnx2x_fcoe_destroy(dev, kwqe);
2708 break;
2709 case FCOE_KWQE_OPCODE_DESTROY:
2710 ret = cnic_bnx2x_fcoe_fw_destroy(dev, kwqe);
2711 break;
2712 case FCOE_KWQE_OPCODE_STAT:
2713 ret = cnic_bnx2x_fcoe_stat(dev, kwqe);
2714 break;
2715 default:
2716 ret = 0;
2717 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2718 opcode);
2719 break;
2720 }
Michael Chan3238a9b2012-02-05 15:24:40 +00002721 if (ret < 0) {
Michael Chane1928c82010-12-23 07:43:04 +00002722 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2723 opcode);
Michael Chan3238a9b2012-02-05 15:24:40 +00002724
2725 /* Possibly bnx2x parity error, send completion
2726 * to ulp drivers with error code to speed up
2727 * cleanup and reset recovery.
2728 */
2729 if (ret == -EIO || ret == -EAGAIN)
2730 cnic_bnx2x_kwqe_err(dev, kwqe);
2731 }
Michael Chane1928c82010-12-23 07:43:04 +00002732 i += work;
2733 }
2734 return 0;
2735}
2736
2737static int cnic_submit_bnx2x_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
2738 u32 num_wqes)
2739{
2740 int ret = -EINVAL;
2741 u32 layer_code;
2742
2743 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2744 return -EAGAIN; /* bnx2x is down */
2745
2746 if (!num_wqes)
2747 return 0;
2748
2749 layer_code = wqes[0]->kwqe_op_flag & KWQE_LAYER_MASK;
2750 switch (layer_code) {
2751 case KWQE_FLAGS_LAYER_MASK_L5_ISCSI:
2752 case KWQE_FLAGS_LAYER_MASK_L4:
2753 case KWQE_FLAGS_LAYER_MASK_L2:
2754 ret = cnic_submit_bnx2x_iscsi_kwqes(dev, wqes, num_wqes);
2755 break;
2756
2757 case KWQE_FLAGS_LAYER_MASK_L5_FCOE:
2758 ret = cnic_submit_bnx2x_fcoe_kwqes(dev, wqes, num_wqes);
2759 break;
2760 }
2761 return ret;
2762}
2763
2764static inline u32 cnic_get_kcqe_layer_mask(u32 opflag)
2765{
2766 if (unlikely(KCQE_OPCODE(opflag) == FCOE_RAMROD_CMD_ID_TERMINATE_CONN))
2767 return KCQE_FLAGS_LAYER_MASK_L4;
2768
2769 return opflag & KCQE_FLAGS_LAYER_MASK;
2770}
2771
Michael Chana4636962009-06-08 18:14:43 -07002772static void service_kcqes(struct cnic_dev *dev, int num_cqes)
2773{
2774 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002775 int i, j, comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002776
2777 i = 0;
2778 j = 1;
2779 while (num_cqes) {
2780 struct cnic_ulp_ops *ulp_ops;
2781 int ulp_type;
2782 u32 kcqe_op_flag = cp->completed_kcq[i]->kcqe_op_flag;
Michael Chane1928c82010-12-23 07:43:04 +00002783 u32 kcqe_layer = cnic_get_kcqe_layer_mask(kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002784
2785 if (unlikely(kcqe_op_flag & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002786 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002787
2788 while (j < num_cqes) {
2789 u32 next_op = cp->completed_kcq[i + j]->kcqe_op_flag;
2790
Michael Chane1928c82010-12-23 07:43:04 +00002791 if (cnic_get_kcqe_layer_mask(next_op) != kcqe_layer)
Michael Chana4636962009-06-08 18:14:43 -07002792 break;
2793
2794 if (unlikely(next_op & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002795 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002796 j++;
2797 }
2798
2799 if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_RDMA)
2800 ulp_type = CNIC_ULP_RDMA;
2801 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_ISCSI)
2802 ulp_type = CNIC_ULP_ISCSI;
Michael Chane1928c82010-12-23 07:43:04 +00002803 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_FCOE)
2804 ulp_type = CNIC_ULP_FCOE;
Michael Chana4636962009-06-08 18:14:43 -07002805 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L4)
2806 ulp_type = CNIC_ULP_L4;
2807 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L2)
2808 goto end;
2809 else {
Joe Perchesddf79b22010-02-17 15:01:54 +00002810 netdev_err(dev->netdev, "Unknown type of KCQE(0x%x)\n",
2811 kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002812 goto end;
2813 }
2814
2815 rcu_read_lock();
2816 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
2817 if (likely(ulp_ops)) {
2818 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
2819 cp->completed_kcq + i, j);
2820 }
2821 rcu_read_unlock();
2822end:
2823 num_cqes -= j;
2824 i += j;
2825 j = 1;
2826 }
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002827 if (unlikely(comp))
2828 cnic_spq_completion(dev, DRV_CTL_RET_L5_SPQ_CREDIT_CMD, comp);
Michael Chana4636962009-06-08 18:14:43 -07002829}
2830
Michael Chan644b9d42010-06-24 14:58:40 +00002831static int cnic_get_kcqes(struct cnic_dev *dev, struct kcq_info *info)
Michael Chana4636962009-06-08 18:14:43 -07002832{
2833 struct cnic_local *cp = dev->cnic_priv;
Michael Chan644b9d42010-06-24 14:58:40 +00002834 u16 i, ri, hw_prod, last;
Michael Chana4636962009-06-08 18:14:43 -07002835 struct kcqe *kcqe;
2836 int kcqe_cnt = 0, last_cnt = 0;
2837
Michael Chan644b9d42010-06-24 14:58:40 +00002838 i = ri = last = info->sw_prod_idx;
Michael Chana4636962009-06-08 18:14:43 -07002839 ri &= MAX_KCQ_IDX;
Michael Chan644b9d42010-06-24 14:58:40 +00002840 hw_prod = *info->hw_prod_idx_ptr;
Michael Chan59e51372011-06-14 01:32:38 +00002841 hw_prod = info->hw_idx(hw_prod);
Michael Chana4636962009-06-08 18:14:43 -07002842
2843 while ((i != hw_prod) && (kcqe_cnt < MAX_COMPLETED_KCQE)) {
Michael Chan644b9d42010-06-24 14:58:40 +00002844 kcqe = &info->kcq[KCQ_PG(ri)][KCQ_IDX(ri)];
Michael Chana4636962009-06-08 18:14:43 -07002845 cp->completed_kcq[kcqe_cnt++] = kcqe;
Michael Chan59e51372011-06-14 01:32:38 +00002846 i = info->next_idx(i);
Michael Chana4636962009-06-08 18:14:43 -07002847 ri = i & MAX_KCQ_IDX;
2848 if (likely(!(kcqe->kcqe_op_flag & KCQE_FLAGS_NEXT))) {
2849 last_cnt = kcqe_cnt;
2850 last = i;
2851 }
2852 }
2853
Michael Chan644b9d42010-06-24 14:58:40 +00002854 info->sw_prod_idx = last;
Michael Chana4636962009-06-08 18:14:43 -07002855 return last_cnt;
2856}
2857
Michael Chan48f753d2010-05-18 11:32:53 +00002858static int cnic_l2_completion(struct cnic_local *cp)
2859{
2860 u16 hw_cons, sw_cons;
Michael Chancd801532010-10-13 14:06:49 +00002861 struct cnic_uio_dev *udev = cp->udev;
Michael Chan48f753d2010-05-18 11:32:53 +00002862 union eth_rx_cqe *cqe, *cqe_ring = (union eth_rx_cqe *)
Michael Chancd801532010-10-13 14:06:49 +00002863 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Michael Chan48f753d2010-05-18 11:32:53 +00002864 u32 cmd;
2865 int comp = 0;
2866
2867 if (!test_bit(CNIC_F_BNX2X_CLASS, &cp->dev->flags))
2868 return 0;
2869
2870 hw_cons = *cp->rx_cons_ptr;
2871 if ((hw_cons & BNX2X_MAX_RCQ_DESC_CNT) == BNX2X_MAX_RCQ_DESC_CNT)
2872 hw_cons++;
2873
2874 sw_cons = cp->rx_cons;
2875 while (sw_cons != hw_cons) {
2876 u8 cqe_fp_flags;
2877
2878 cqe = &cqe_ring[sw_cons & BNX2X_MAX_RCQ_DESC_CNT];
2879 cqe_fp_flags = cqe->fast_path_cqe.type_error_flags;
2880 if (cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE) {
2881 cmd = le32_to_cpu(cqe->ramrod_cqe.conn_and_cmd_data);
2882 cmd >>= COMMON_RAMROD_ETH_RX_CQE_CMD_ID_SHIFT;
2883 if (cmd == RAMROD_CMD_ID_ETH_CLIENT_SETUP ||
2884 cmd == RAMROD_CMD_ID_ETH_HALT)
2885 comp++;
2886 }
2887 sw_cons = BNX2X_NEXT_RCQE(sw_cons);
2888 }
2889 return comp;
2890}
2891
Michael Chan86b53602009-10-10 13:46:57 +00002892static void cnic_chk_pkt_rings(struct cnic_local *cp)
Michael Chana4636962009-06-08 18:14:43 -07002893{
Michael Chan541a78102010-10-06 03:17:22 +00002894 u16 rx_cons, tx_cons;
Michael Chan48f753d2010-05-18 11:32:53 +00002895 int comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002896
Michael Chan541a78102010-10-06 03:17:22 +00002897 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
Michael Chan66fee9e2010-06-24 14:58:38 +00002898 return;
2899
Michael Chan541a78102010-10-06 03:17:22 +00002900 rx_cons = *cp->rx_cons_ptr;
2901 tx_cons = *cp->tx_cons_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002902 if (cp->tx_cons != tx_cons || cp->rx_cons != rx_cons) {
Michael Chan48f753d2010-05-18 11:32:53 +00002903 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
2904 comp = cnic_l2_completion(cp);
2905
Michael Chana4636962009-06-08 18:14:43 -07002906 cp->tx_cons = tx_cons;
2907 cp->rx_cons = rx_cons;
Michael Chan71034ba2009-10-10 13:46:59 +00002908
Michael Chancd801532010-10-13 14:06:49 +00002909 if (cp->udev)
2910 uio_event_notify(&cp->udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -07002911 }
Michael Chan48f753d2010-05-18 11:32:53 +00002912 if (comp)
2913 clear_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07002914}
2915
Michael Chanb177a5d52010-06-24 14:58:41 +00002916static u32 cnic_service_bnx2_queues(struct cnic_dev *dev)
Michael Chana4636962009-06-08 18:14:43 -07002917{
Michael Chana4636962009-06-08 18:14:43 -07002918 struct cnic_local *cp = dev->cnic_priv;
Michael Chanb177a5d52010-06-24 14:58:41 +00002919 u32 status_idx = (u16) *cp->kcq1.status_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002920 int kcqe_cnt;
2921
Michael Chan107c3f42011-03-02 13:00:49 +00002922 /* status block index must be read before reading other fields */
2923 rmb();
Michael Chana4636962009-06-08 18:14:43 -07002924 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
2925
Michael Chan644b9d42010-06-24 14:58:40 +00002926 while ((kcqe_cnt = cnic_get_kcqes(dev, &cp->kcq1))) {
Michael Chana4636962009-06-08 18:14:43 -07002927
2928 service_kcqes(dev, kcqe_cnt);
2929
2930 /* Tell compiler that status_blk fields can change. */
2931 barrier();
Michael Chan93736652011-06-08 19:29:32 +00002932 status_idx = (u16) *cp->kcq1.status_idx_ptr;
2933 /* status block index must be read first */
2934 rmb();
2935 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002936 }
2937
Michael Chan644b9d42010-06-24 14:58:40 +00002938 CNIC_WR16(dev, cp->kcq1.io_addr, cp->kcq1.sw_prod_idx);
Michael Chana4636962009-06-08 18:14:43 -07002939
Michael Chan86b53602009-10-10 13:46:57 +00002940 cnic_chk_pkt_rings(cp);
Michael Chanb177a5d52010-06-24 14:58:41 +00002941
Michael Chana4636962009-06-08 18:14:43 -07002942 return status_idx;
2943}
2944
Michael Chanb177a5d52010-06-24 14:58:41 +00002945static int cnic_service_bnx2(void *data, void *status_blk)
2946{
2947 struct cnic_dev *dev = data;
Michael Chanb177a5d52010-06-24 14:58:41 +00002948
Michael Chaneaaa6e92010-12-23 08:38:30 +00002949 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
2950 struct status_block *sblk = status_blk;
2951
2952 return sblk->status_idx;
2953 }
Michael Chanb177a5d52010-06-24 14:58:41 +00002954
2955 return cnic_service_bnx2_queues(dev);
2956}
2957
Michael Chana4636962009-06-08 18:14:43 -07002958static void cnic_service_bnx2_msix(unsigned long data)
2959{
2960 struct cnic_dev *dev = (struct cnic_dev *) data;
2961 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002962
Michael Chanb177a5d52010-06-24 14:58:41 +00002963 cp->last_status_idx = cnic_service_bnx2_queues(dev);
Michael Chana4636962009-06-08 18:14:43 -07002964
Michael Chana4636962009-06-08 18:14:43 -07002965 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
2966 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
2967}
2968
Michael Chan66fee9e2010-06-24 14:58:38 +00002969static void cnic_doirq(struct cnic_dev *dev)
2970{
2971 struct cnic_local *cp = dev->cnic_priv;
Michael Chan66fee9e2010-06-24 14:58:38 +00002972
2973 if (likely(test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
Michael Chaneaaa6e92010-12-23 08:38:30 +00002974 u16 prod = cp->kcq1.sw_prod_idx & MAX_KCQ_IDX;
2975
Michael Chan66fee9e2010-06-24 14:58:38 +00002976 prefetch(cp->status_blk.gen);
Michael Chane6c28892010-06-24 14:58:39 +00002977 prefetch(&cp->kcq1.kcq[KCQ_PG(prod)][KCQ_IDX(prod)]);
Michael Chan66fee9e2010-06-24 14:58:38 +00002978
2979 tasklet_schedule(&cp->cnic_irq_task);
2980 }
2981}
2982
Michael Chana4636962009-06-08 18:14:43 -07002983static irqreturn_t cnic_irq(int irq, void *dev_instance)
2984{
2985 struct cnic_dev *dev = dev_instance;
2986 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002987
2988 if (cp->ack_int)
2989 cp->ack_int(dev);
2990
Michael Chan66fee9e2010-06-24 14:58:38 +00002991 cnic_doirq(dev);
Michael Chana4636962009-06-08 18:14:43 -07002992
2993 return IRQ_HANDLED;
2994}
2995
Michael Chan71034ba2009-10-10 13:46:59 +00002996static inline void cnic_ack_bnx2x_int(struct cnic_dev *dev, u8 id, u8 storm,
2997 u16 index, u8 op, u8 update)
2998{
2999 struct cnic_local *cp = dev->cnic_priv;
3000 u32 hc_addr = (HC_REG_COMMAND_REG + CNIC_PORT(cp) * 32 +
3001 COMMAND_REG_INT_ACK);
3002 struct igu_ack_register igu_ack;
3003
3004 igu_ack.status_block_index = index;
3005 igu_ack.sb_id_and_flags =
3006 ((id << IGU_ACK_REGISTER_STATUS_BLOCK_ID_SHIFT) |
3007 (storm << IGU_ACK_REGISTER_STORM_ID_SHIFT) |
3008 (update << IGU_ACK_REGISTER_UPDATE_INDEX_SHIFT) |
3009 (op << IGU_ACK_REGISTER_INTERRUPT_MODE_SHIFT));
3010
3011 CNIC_WR(dev, hc_addr, (*(u32 *)&igu_ack));
3012}
3013
Michael Chanee87a822010-10-13 14:06:51 +00003014static void cnic_ack_igu_sb(struct cnic_dev *dev, u8 igu_sb_id, u8 segment,
3015 u16 index, u8 op, u8 update)
3016{
3017 struct igu_regular cmd_data;
3018 u32 igu_addr = BAR_IGU_INTMEM + (IGU_CMD_INT_ACK_BASE + igu_sb_id) * 8;
3019
3020 cmd_data.sb_id_and_flags =
3021 (index << IGU_REGULAR_SB_INDEX_SHIFT) |
3022 (segment << IGU_REGULAR_SEGMENT_ACCESS_SHIFT) |
3023 (update << IGU_REGULAR_BUPDATE_SHIFT) |
3024 (op << IGU_REGULAR_ENABLE_INT_SHIFT);
3025
3026
3027 CNIC_WR(dev, igu_addr, cmd_data.sb_id_and_flags);
3028}
3029
Michael Chan71034ba2009-10-10 13:46:59 +00003030static void cnic_ack_bnx2x_msix(struct cnic_dev *dev)
3031{
3032 struct cnic_local *cp = dev->cnic_priv;
3033
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003034 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, CSTORM_ID, 0,
Michael Chan71034ba2009-10-10 13:46:59 +00003035 IGU_INT_DISABLE, 0);
3036}
3037
Michael Chanee87a822010-10-13 14:06:51 +00003038static void cnic_ack_bnx2x_e2_msix(struct cnic_dev *dev)
3039{
3040 struct cnic_local *cp = dev->cnic_priv;
3041
3042 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF, 0,
3043 IGU_INT_DISABLE, 0);
3044}
3045
Michael Chanb177a5d52010-06-24 14:58:41 +00003046static u32 cnic_service_bnx2x_kcq(struct cnic_dev *dev, struct kcq_info *info)
Michael Chan71034ba2009-10-10 13:46:59 +00003047{
Michael Chanb177a5d52010-06-24 14:58:41 +00003048 u32 last_status = *info->status_idx_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00003049 int kcqe_cnt;
3050
Michael Chan107c3f42011-03-02 13:00:49 +00003051 /* status block index must be read before reading the KCQ */
3052 rmb();
Michael Chanb177a5d52010-06-24 14:58:41 +00003053 while ((kcqe_cnt = cnic_get_kcqes(dev, info))) {
Michael Chan71034ba2009-10-10 13:46:59 +00003054
3055 service_kcqes(dev, kcqe_cnt);
3056
3057 /* Tell compiler that sblk fields can change. */
3058 barrier();
Michael Chan71034ba2009-10-10 13:46:59 +00003059
Michael Chanb177a5d52010-06-24 14:58:41 +00003060 last_status = *info->status_idx_ptr;
Michael Chan107c3f42011-03-02 13:00:49 +00003061 /* status block index must be read before reading the KCQ */
3062 rmb();
Michael Chan71034ba2009-10-10 13:46:59 +00003063 }
Michael Chanb177a5d52010-06-24 14:58:41 +00003064 return last_status;
3065}
3066
3067static void cnic_service_bnx2x_bh(unsigned long data)
3068{
3069 struct cnic_dev *dev = (struct cnic_dev *) data;
3070 struct cnic_local *cp = dev->cnic_priv;
Michael Chan0197b082011-03-02 13:00:50 +00003071 u32 status_idx, new_status_idx;
Michael Chanb177a5d52010-06-24 14:58:41 +00003072
3073 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
3074 return;
3075
Michael Chan0197b082011-03-02 13:00:50 +00003076 while (1) {
3077 status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq1);
Michael Chan71034ba2009-10-10 13:46:59 +00003078
Michael Chan0197b082011-03-02 13:00:50 +00003079 CNIC_WR16(dev, cp->kcq1.io_addr,
3080 cp->kcq1.sw_prod_idx + MAX_KCQ_IDX);
Michael Chane21ba412010-12-23 07:43:03 +00003081
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03003082 if (!BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chan0197b082011-03-02 13:00:50 +00003083 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, USTORM_ID,
3084 status_idx, IGU_INT_ENABLE, 1);
3085 break;
3086 }
3087
3088 new_status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq2);
3089
3090 if (new_status_idx != status_idx)
3091 continue;
Michael Chane21ba412010-12-23 07:43:03 +00003092
3093 CNIC_WR16(dev, cp->kcq2.io_addr, cp->kcq2.sw_prod_idx +
3094 MAX_KCQ_IDX);
3095
Michael Chanee87a822010-10-13 14:06:51 +00003096 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF,
3097 status_idx, IGU_INT_ENABLE, 1);
Michael Chan0197b082011-03-02 13:00:50 +00003098
3099 break;
Michael Chane21ba412010-12-23 07:43:03 +00003100 }
Michael Chan71034ba2009-10-10 13:46:59 +00003101}
3102
3103static int cnic_service_bnx2x(void *data, void *status_blk)
3104{
3105 struct cnic_dev *dev = data;
3106 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00003107
Michael Chan66fee9e2010-06-24 14:58:38 +00003108 if (!(cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
3109 cnic_doirq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00003110
Michael Chan66fee9e2010-06-24 14:58:38 +00003111 cnic_chk_pkt_rings(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00003112
3113 return 0;
3114}
3115
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003116static void cnic_ulp_stop_one(struct cnic_local *cp, int if_type)
3117{
3118 struct cnic_ulp_ops *ulp_ops;
3119
3120 if (if_type == CNIC_ULP_ISCSI)
3121 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
3122
3123 mutex_lock(&cnic_lock);
3124 ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
3125 lockdep_is_held(&cnic_lock));
3126 if (!ulp_ops) {
3127 mutex_unlock(&cnic_lock);
3128 return;
3129 }
3130 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
3131 mutex_unlock(&cnic_lock);
3132
3133 if (test_and_clear_bit(ULP_F_START, &cp->ulp_flags[if_type]))
3134 ulp_ops->cnic_stop(cp->ulp_handle[if_type]);
3135
3136 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
3137}
3138
Michael Chana4636962009-06-08 18:14:43 -07003139static void cnic_ulp_stop(struct cnic_dev *dev)
3140{
3141 struct cnic_local *cp = dev->cnic_priv;
3142 int if_type;
3143
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003144 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++)
3145 cnic_ulp_stop_one(cp, if_type);
Michael Chana4636962009-06-08 18:14:43 -07003146}
3147
3148static void cnic_ulp_start(struct cnic_dev *dev)
3149{
3150 struct cnic_local *cp = dev->cnic_priv;
3151 int if_type;
3152
Michael Chana4636962009-06-08 18:14:43 -07003153 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
3154 struct cnic_ulp_ops *ulp_ops;
3155
Michael Chan681dbd72009-08-14 15:49:46 +00003156 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003157 ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
3158 lockdep_is_held(&cnic_lock));
Michael Chan681dbd72009-08-14 15:49:46 +00003159 if (!ulp_ops || !ulp_ops->cnic_start) {
3160 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003161 continue;
Michael Chan681dbd72009-08-14 15:49:46 +00003162 }
3163 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
3164 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003165
3166 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[if_type]))
3167 ulp_ops->cnic_start(cp->ulp_handle[if_type]);
Michael Chan681dbd72009-08-14 15:49:46 +00003168
3169 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
Michael Chana4636962009-06-08 18:14:43 -07003170 }
Michael Chana4636962009-06-08 18:14:43 -07003171}
3172
Barak Witkowski1d187b32011-12-05 22:41:50 +00003173static int cnic_copy_ulp_stats(struct cnic_dev *dev, int ulp_type)
3174{
3175 struct cnic_local *cp = dev->cnic_priv;
3176 struct cnic_ulp_ops *ulp_ops;
3177 int rc;
3178
3179 mutex_lock(&cnic_lock);
3180 ulp_ops = cnic_ulp_tbl_prot(ulp_type);
3181 if (ulp_ops && ulp_ops->cnic_get_stats)
3182 rc = ulp_ops->cnic_get_stats(cp->ulp_handle[ulp_type]);
3183 else
3184 rc = -ENODEV;
3185 mutex_unlock(&cnic_lock);
3186 return rc;
3187}
3188
Michael Chana4636962009-06-08 18:14:43 -07003189static int cnic_ctl(void *data, struct cnic_ctl_info *info)
3190{
3191 struct cnic_dev *dev = data;
Barak Witkowski1d187b32011-12-05 22:41:50 +00003192 int ulp_type = CNIC_ULP_ISCSI;
Michael Chana4636962009-06-08 18:14:43 -07003193
3194 switch (info->cmd) {
3195 case CNIC_CTL_STOP_CMD:
3196 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07003197
3198 cnic_ulp_stop(dev);
3199 cnic_stop_hw(dev);
3200
Michael Chana4636962009-06-08 18:14:43 -07003201 cnic_put(dev);
3202 break;
3203 case CNIC_CTL_START_CMD:
3204 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07003205
3206 if (!cnic_start_hw(dev))
3207 cnic_ulp_start(dev);
3208
Michael Chana4636962009-06-08 18:14:43 -07003209 cnic_put(dev);
3210 break;
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003211 case CNIC_CTL_STOP_ISCSI_CMD: {
3212 struct cnic_local *cp = dev->cnic_priv;
3213 set_bit(CNIC_LCL_FL_STOP_ISCSI, &cp->cnic_local_flags);
3214 queue_delayed_work(cnic_wq, &cp->delete_task, 0);
3215 break;
3216 }
Michael Chan71034ba2009-10-10 13:46:59 +00003217 case CNIC_CTL_COMPLETION_CMD: {
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03003218 struct cnic_ctl_completion *comp = &info->data.comp;
3219 u32 cid = BNX2X_SW_CID(comp->cid);
Michael Chan71034ba2009-10-10 13:46:59 +00003220 u32 l5_cid;
3221 struct cnic_local *cp = dev->cnic_priv;
3222
3223 if (cnic_get_l5_cid(cp, cid, &l5_cid) == 0) {
3224 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3225
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03003226 if (unlikely(comp->error)) {
3227 set_bit(CTX_FL_CID_ERROR, &ctx->ctx_flags);
3228 netdev_err(dev->netdev,
3229 "CID %x CFC delete comp error %x\n",
3230 cid, comp->error);
3231 }
3232
Michael Chan71034ba2009-10-10 13:46:59 +00003233 ctx->wait_cond = 1;
3234 wake_up(&ctx->waitq);
3235 }
3236 break;
3237 }
Barak Witkowski1d187b32011-12-05 22:41:50 +00003238 case CNIC_CTL_FCOE_STATS_GET_CMD:
3239 ulp_type = CNIC_ULP_FCOE;
3240 /* fall through */
3241 case CNIC_CTL_ISCSI_STATS_GET_CMD:
3242 cnic_hold(dev);
3243 cnic_copy_ulp_stats(dev, ulp_type);
3244 cnic_put(dev);
3245 break;
3246
Michael Chana4636962009-06-08 18:14:43 -07003247 default:
3248 return -EINVAL;
3249 }
3250 return 0;
3251}
3252
3253static void cnic_ulp_init(struct cnic_dev *dev)
3254{
3255 int i;
3256 struct cnic_local *cp = dev->cnic_priv;
3257
Michael Chana4636962009-06-08 18:14:43 -07003258 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3259 struct cnic_ulp_ops *ulp_ops;
3260
Michael Chan7fc1ece2009-08-14 15:49:47 +00003261 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003262 ulp_ops = cnic_ulp_tbl_prot(i);
Michael Chan7fc1ece2009-08-14 15:49:47 +00003263 if (!ulp_ops || !ulp_ops->cnic_init) {
3264 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003265 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003266 }
3267 ulp_get(ulp_ops);
3268 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003269
3270 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3271 ulp_ops->cnic_init(dev);
3272
Michael Chan7fc1ece2009-08-14 15:49:47 +00003273 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003274 }
Michael Chana4636962009-06-08 18:14:43 -07003275}
3276
3277static void cnic_ulp_exit(struct cnic_dev *dev)
3278{
3279 int i;
3280 struct cnic_local *cp = dev->cnic_priv;
3281
Michael Chana4636962009-06-08 18:14:43 -07003282 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3283 struct cnic_ulp_ops *ulp_ops;
3284
Michael Chan7fc1ece2009-08-14 15:49:47 +00003285 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003286 ulp_ops = cnic_ulp_tbl_prot(i);
Michael Chan7fc1ece2009-08-14 15:49:47 +00003287 if (!ulp_ops || !ulp_ops->cnic_exit) {
3288 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003289 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003290 }
3291 ulp_get(ulp_ops);
3292 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003293
3294 if (test_and_clear_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3295 ulp_ops->cnic_exit(dev);
3296
Michael Chan7fc1ece2009-08-14 15:49:47 +00003297 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003298 }
Michael Chana4636962009-06-08 18:14:43 -07003299}
3300
3301static int cnic_cm_offload_pg(struct cnic_sock *csk)
3302{
3303 struct cnic_dev *dev = csk->dev;
3304 struct l4_kwq_offload_pg *l4kwqe;
3305 struct kwqe *wqes[1];
3306
3307 l4kwqe = (struct l4_kwq_offload_pg *) &csk->kwqe1;
3308 memset(l4kwqe, 0, sizeof(*l4kwqe));
3309 wqes[0] = (struct kwqe *) l4kwqe;
3310
3311 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_OFFLOAD_PG;
3312 l4kwqe->flags =
3313 L4_LAYER_CODE << L4_KWQ_OFFLOAD_PG_LAYER_CODE_SHIFT;
3314 l4kwqe->l2hdr_nbytes = ETH_HLEN;
3315
3316 l4kwqe->da0 = csk->ha[0];
3317 l4kwqe->da1 = csk->ha[1];
3318 l4kwqe->da2 = csk->ha[2];
3319 l4kwqe->da3 = csk->ha[3];
3320 l4kwqe->da4 = csk->ha[4];
3321 l4kwqe->da5 = csk->ha[5];
3322
3323 l4kwqe->sa0 = dev->mac_addr[0];
3324 l4kwqe->sa1 = dev->mac_addr[1];
3325 l4kwqe->sa2 = dev->mac_addr[2];
3326 l4kwqe->sa3 = dev->mac_addr[3];
3327 l4kwqe->sa4 = dev->mac_addr[4];
3328 l4kwqe->sa5 = dev->mac_addr[5];
3329
3330 l4kwqe->etype = ETH_P_IP;
Eddie Waia9736c02010-02-24 14:42:04 +00003331 l4kwqe->ipid_start = DEF_IPID_START;
Michael Chana4636962009-06-08 18:14:43 -07003332 l4kwqe->host_opaque = csk->l5_cid;
3333
3334 if (csk->vlan_id) {
3335 l4kwqe->pg_flags |= L4_KWQ_OFFLOAD_PG_VLAN_TAGGING;
3336 l4kwqe->vlan_tag = csk->vlan_id;
3337 l4kwqe->l2hdr_nbytes += 4;
3338 }
3339
3340 return dev->submit_kwqes(dev, wqes, 1);
3341}
3342
3343static int cnic_cm_update_pg(struct cnic_sock *csk)
3344{
3345 struct cnic_dev *dev = csk->dev;
3346 struct l4_kwq_update_pg *l4kwqe;
3347 struct kwqe *wqes[1];
3348
3349 l4kwqe = (struct l4_kwq_update_pg *) &csk->kwqe1;
3350 memset(l4kwqe, 0, sizeof(*l4kwqe));
3351 wqes[0] = (struct kwqe *) l4kwqe;
3352
3353 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPDATE_PG;
3354 l4kwqe->flags =
3355 L4_LAYER_CODE << L4_KWQ_UPDATE_PG_LAYER_CODE_SHIFT;
3356 l4kwqe->pg_cid = csk->pg_cid;
3357
3358 l4kwqe->da0 = csk->ha[0];
3359 l4kwqe->da1 = csk->ha[1];
3360 l4kwqe->da2 = csk->ha[2];
3361 l4kwqe->da3 = csk->ha[3];
3362 l4kwqe->da4 = csk->ha[4];
3363 l4kwqe->da5 = csk->ha[5];
3364
3365 l4kwqe->pg_host_opaque = csk->l5_cid;
3366 l4kwqe->pg_valids = L4_KWQ_UPDATE_PG_VALIDS_DA;
3367
3368 return dev->submit_kwqes(dev, wqes, 1);
3369}
3370
3371static int cnic_cm_upload_pg(struct cnic_sock *csk)
3372{
3373 struct cnic_dev *dev = csk->dev;
3374 struct l4_kwq_upload *l4kwqe;
3375 struct kwqe *wqes[1];
3376
3377 l4kwqe = (struct l4_kwq_upload *) &csk->kwqe1;
3378 memset(l4kwqe, 0, sizeof(*l4kwqe));
3379 wqes[0] = (struct kwqe *) l4kwqe;
3380
3381 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPLOAD_PG;
3382 l4kwqe->flags =
3383 L4_LAYER_CODE << L4_KWQ_UPLOAD_LAYER_CODE_SHIFT;
3384 l4kwqe->cid = csk->pg_cid;
3385
3386 return dev->submit_kwqes(dev, wqes, 1);
3387}
3388
3389static int cnic_cm_conn_req(struct cnic_sock *csk)
3390{
3391 struct cnic_dev *dev = csk->dev;
3392 struct l4_kwq_connect_req1 *l4kwqe1;
3393 struct l4_kwq_connect_req2 *l4kwqe2;
3394 struct l4_kwq_connect_req3 *l4kwqe3;
3395 struct kwqe *wqes[3];
3396 u8 tcp_flags = 0;
3397 int num_wqes = 2;
3398
3399 l4kwqe1 = (struct l4_kwq_connect_req1 *) &csk->kwqe1;
3400 l4kwqe2 = (struct l4_kwq_connect_req2 *) &csk->kwqe2;
3401 l4kwqe3 = (struct l4_kwq_connect_req3 *) &csk->kwqe3;
3402 memset(l4kwqe1, 0, sizeof(*l4kwqe1));
3403 memset(l4kwqe2, 0, sizeof(*l4kwqe2));
3404 memset(l4kwqe3, 0, sizeof(*l4kwqe3));
3405
3406 l4kwqe3->op_code = L4_KWQE_OPCODE_VALUE_CONNECT3;
3407 l4kwqe3->flags =
3408 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ3_LAYER_CODE_SHIFT;
3409 l4kwqe3->ka_timeout = csk->ka_timeout;
3410 l4kwqe3->ka_interval = csk->ka_interval;
3411 l4kwqe3->ka_max_probe_count = csk->ka_max_probe_count;
3412 l4kwqe3->tos = csk->tos;
3413 l4kwqe3->ttl = csk->ttl;
3414 l4kwqe3->snd_seq_scale = csk->snd_seq_scale;
3415 l4kwqe3->pmtu = csk->mtu;
3416 l4kwqe3->rcv_buf = csk->rcv_buf;
3417 l4kwqe3->snd_buf = csk->snd_buf;
3418 l4kwqe3->seed = csk->seed;
3419
3420 wqes[0] = (struct kwqe *) l4kwqe1;
3421 if (test_bit(SK_F_IPV6, &csk->flags)) {
3422 wqes[1] = (struct kwqe *) l4kwqe2;
3423 wqes[2] = (struct kwqe *) l4kwqe3;
3424 num_wqes = 3;
3425
3426 l4kwqe1->conn_flags = L4_KWQ_CONNECT_REQ1_IP_V6;
3427 l4kwqe2->op_code = L4_KWQE_OPCODE_VALUE_CONNECT2;
3428 l4kwqe2->flags =
3429 L4_KWQ_CONNECT_REQ2_LINKED_WITH_NEXT |
3430 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ2_LAYER_CODE_SHIFT;
3431 l4kwqe2->src_ip_v6_2 = be32_to_cpu(csk->src_ip[1]);
3432 l4kwqe2->src_ip_v6_3 = be32_to_cpu(csk->src_ip[2]);
3433 l4kwqe2->src_ip_v6_4 = be32_to_cpu(csk->src_ip[3]);
3434 l4kwqe2->dst_ip_v6_2 = be32_to_cpu(csk->dst_ip[1]);
3435 l4kwqe2->dst_ip_v6_3 = be32_to_cpu(csk->dst_ip[2]);
3436 l4kwqe2->dst_ip_v6_4 = be32_to_cpu(csk->dst_ip[3]);
3437 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct ipv6hdr) -
3438 sizeof(struct tcphdr);
3439 } else {
3440 wqes[1] = (struct kwqe *) l4kwqe3;
3441 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct iphdr) -
3442 sizeof(struct tcphdr);
3443 }
3444
3445 l4kwqe1->op_code = L4_KWQE_OPCODE_VALUE_CONNECT1;
3446 l4kwqe1->flags =
3447 (L4_LAYER_CODE << L4_KWQ_CONNECT_REQ1_LAYER_CODE_SHIFT) |
3448 L4_KWQ_CONNECT_REQ3_LINKED_WITH_NEXT;
3449 l4kwqe1->cid = csk->cid;
3450 l4kwqe1->pg_cid = csk->pg_cid;
3451 l4kwqe1->src_ip = be32_to_cpu(csk->src_ip[0]);
3452 l4kwqe1->dst_ip = be32_to_cpu(csk->dst_ip[0]);
3453 l4kwqe1->src_port = be16_to_cpu(csk->src_port);
3454 l4kwqe1->dst_port = be16_to_cpu(csk->dst_port);
3455 if (csk->tcp_flags & SK_TCP_NO_DELAY_ACK)
3456 tcp_flags |= L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK;
3457 if (csk->tcp_flags & SK_TCP_KEEP_ALIVE)
3458 tcp_flags |= L4_KWQ_CONNECT_REQ1_KEEP_ALIVE;
3459 if (csk->tcp_flags & SK_TCP_NAGLE)
3460 tcp_flags |= L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE;
3461 if (csk->tcp_flags & SK_TCP_TIMESTAMP)
3462 tcp_flags |= L4_KWQ_CONNECT_REQ1_TIME_STAMP;
3463 if (csk->tcp_flags & SK_TCP_SACK)
3464 tcp_flags |= L4_KWQ_CONNECT_REQ1_SACK;
3465 if (csk->tcp_flags & SK_TCP_SEG_SCALING)
3466 tcp_flags |= L4_KWQ_CONNECT_REQ1_SEG_SCALING;
3467
3468 l4kwqe1->tcp_flags = tcp_flags;
3469
3470 return dev->submit_kwqes(dev, wqes, num_wqes);
3471}
3472
3473static int cnic_cm_close_req(struct cnic_sock *csk)
3474{
3475 struct cnic_dev *dev = csk->dev;
3476 struct l4_kwq_close_req *l4kwqe;
3477 struct kwqe *wqes[1];
3478
3479 l4kwqe = (struct l4_kwq_close_req *) &csk->kwqe2;
3480 memset(l4kwqe, 0, sizeof(*l4kwqe));
3481 wqes[0] = (struct kwqe *) l4kwqe;
3482
3483 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_CLOSE;
3484 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_CLOSE_REQ_LAYER_CODE_SHIFT;
3485 l4kwqe->cid = csk->cid;
3486
3487 return dev->submit_kwqes(dev, wqes, 1);
3488}
3489
3490static int cnic_cm_abort_req(struct cnic_sock *csk)
3491{
3492 struct cnic_dev *dev = csk->dev;
3493 struct l4_kwq_reset_req *l4kwqe;
3494 struct kwqe *wqes[1];
3495
3496 l4kwqe = (struct l4_kwq_reset_req *) &csk->kwqe2;
3497 memset(l4kwqe, 0, sizeof(*l4kwqe));
3498 wqes[0] = (struct kwqe *) l4kwqe;
3499
3500 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_RESET;
3501 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_RESET_REQ_LAYER_CODE_SHIFT;
3502 l4kwqe->cid = csk->cid;
3503
3504 return dev->submit_kwqes(dev, wqes, 1);
3505}
3506
3507static int cnic_cm_create(struct cnic_dev *dev, int ulp_type, u32 cid,
3508 u32 l5_cid, struct cnic_sock **csk, void *context)
3509{
3510 struct cnic_local *cp = dev->cnic_priv;
3511 struct cnic_sock *csk1;
3512
3513 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3514 return -EINVAL;
3515
Michael Chanfdf24082010-10-13 14:06:47 +00003516 if (cp->ctx_tbl) {
3517 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3518
3519 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
3520 return -EAGAIN;
3521 }
3522
Michael Chana4636962009-06-08 18:14:43 -07003523 csk1 = &cp->csk_tbl[l5_cid];
3524 if (atomic_read(&csk1->ref_count))
3525 return -EAGAIN;
3526
3527 if (test_and_set_bit(SK_F_INUSE, &csk1->flags))
3528 return -EBUSY;
3529
3530 csk1->dev = dev;
3531 csk1->cid = cid;
3532 csk1->l5_cid = l5_cid;
3533 csk1->ulp_type = ulp_type;
3534 csk1->context = context;
3535
3536 csk1->ka_timeout = DEF_KA_TIMEOUT;
3537 csk1->ka_interval = DEF_KA_INTERVAL;
3538 csk1->ka_max_probe_count = DEF_KA_MAX_PROBE_COUNT;
3539 csk1->tos = DEF_TOS;
3540 csk1->ttl = DEF_TTL;
3541 csk1->snd_seq_scale = DEF_SND_SEQ_SCALE;
3542 csk1->rcv_buf = DEF_RCV_BUF;
3543 csk1->snd_buf = DEF_SND_BUF;
3544 csk1->seed = DEF_SEED;
3545
3546 *csk = csk1;
3547 return 0;
3548}
3549
3550static void cnic_cm_cleanup(struct cnic_sock *csk)
3551{
3552 if (csk->src_port) {
3553 struct cnic_dev *dev = csk->dev;
3554 struct cnic_local *cp = dev->cnic_priv;
3555
Michael Chan9b093362010-12-23 07:42:56 +00003556 cnic_free_id(&cp->csk_port_tbl, be16_to_cpu(csk->src_port));
Michael Chana4636962009-06-08 18:14:43 -07003557 csk->src_port = 0;
3558 }
3559}
3560
3561static void cnic_close_conn(struct cnic_sock *csk)
3562{
3563 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags)) {
3564 cnic_cm_upload_pg(csk);
3565 clear_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3566 }
3567 cnic_cm_cleanup(csk);
3568}
3569
3570static int cnic_cm_destroy(struct cnic_sock *csk)
3571{
3572 if (!cnic_in_use(csk))
3573 return -EINVAL;
3574
3575 csk_hold(csk);
3576 clear_bit(SK_F_INUSE, &csk->flags);
3577 smp_mb__after_clear_bit();
3578 while (atomic_read(&csk->ref_count) != 1)
3579 msleep(1);
3580 cnic_cm_cleanup(csk);
3581
3582 csk->flags = 0;
3583 csk_put(csk);
3584 return 0;
3585}
3586
3587static inline u16 cnic_get_vlan(struct net_device *dev,
3588 struct net_device **vlan_dev)
3589{
3590 if (dev->priv_flags & IFF_802_1Q_VLAN) {
3591 *vlan_dev = vlan_dev_real_dev(dev);
3592 return vlan_dev_vlan_id(dev);
3593 }
3594 *vlan_dev = dev;
3595 return 0;
3596}
3597
3598static int cnic_get_v4_route(struct sockaddr_in *dst_addr,
3599 struct dst_entry **dst)
3600{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003601#if defined(CONFIG_INET)
Michael Chana4636962009-06-08 18:14:43 -07003602 struct rtable *rt;
3603
David S. Miller78fbfd82011-03-12 00:00:52 -05003604 rt = ip_route_output(&init_net, dst_addr->sin_addr.s_addr, 0, 0, 0);
3605 if (!IS_ERR(rt)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07003606 *dst = &rt->dst;
David S. Miller78fbfd82011-03-12 00:00:52 -05003607 return 0;
3608 }
3609 return PTR_ERR(rt);
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003610#else
3611 return -ENETUNREACH;
3612#endif
Michael Chana4636962009-06-08 18:14:43 -07003613}
3614
3615static int cnic_get_v6_route(struct sockaddr_in6 *dst_addr,
3616 struct dst_entry **dst)
3617{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003618#if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
David S. Miller4c9483b2011-03-12 16:22:43 -05003619 struct flowi6 fl6;
Michael Chana4636962009-06-08 18:14:43 -07003620
David S. Miller4c9483b2011-03-12 16:22:43 -05003621 memset(&fl6, 0, sizeof(fl6));
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003622 fl6.daddr = dst_addr->sin6_addr;
David S. Miller4c9483b2011-03-12 16:22:43 -05003623 if (ipv6_addr_type(&fl6.daddr) & IPV6_ADDR_LINKLOCAL)
3624 fl6.flowi6_oif = dst_addr->sin6_scope_id;
Michael Chana4636962009-06-08 18:14:43 -07003625
David S. Miller4c9483b2011-03-12 16:22:43 -05003626 *dst = ip6_route_output(&init_net, NULL, &fl6);
RongQing.Li05417432012-02-21 22:10:50 +00003627 if ((*dst)->error) {
3628 dst_release(*dst);
3629 *dst = NULL;
3630 return -ENETUNREACH;
3631 } else
Michael Chana4636962009-06-08 18:14:43 -07003632 return 0;
3633#endif
3634
3635 return -ENETUNREACH;
3636}
3637
3638static struct cnic_dev *cnic_cm_select_dev(struct sockaddr_in *dst_addr,
3639 int ulp_type)
3640{
3641 struct cnic_dev *dev = NULL;
3642 struct dst_entry *dst;
3643 struct net_device *netdev = NULL;
3644 int err = -ENETUNREACH;
3645
3646 if (dst_addr->sin_family == AF_INET)
3647 err = cnic_get_v4_route(dst_addr, &dst);
3648 else if (dst_addr->sin_family == AF_INET6) {
3649 struct sockaddr_in6 *dst_addr6 =
3650 (struct sockaddr_in6 *) dst_addr;
3651
3652 err = cnic_get_v6_route(dst_addr6, &dst);
3653 } else
3654 return NULL;
3655
3656 if (err)
3657 return NULL;
3658
3659 if (!dst->dev)
3660 goto done;
3661
3662 cnic_get_vlan(dst->dev, &netdev);
3663
3664 dev = cnic_from_netdev(netdev);
3665
3666done:
3667 dst_release(dst);
3668 if (dev)
3669 cnic_put(dev);
3670 return dev;
3671}
3672
3673static int cnic_resolve_addr(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3674{
3675 struct cnic_dev *dev = csk->dev;
3676 struct cnic_local *cp = dev->cnic_priv;
3677
3678 return cnic_send_nlmsg(cp, ISCSI_KEVENT_PATH_REQ, csk);
3679}
3680
3681static int cnic_get_route(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3682{
3683 struct cnic_dev *dev = csk->dev;
3684 struct cnic_local *cp = dev->cnic_priv;
Michael Chanc76284a2010-02-24 14:42:07 +00003685 int is_v6, rc = 0;
3686 struct dst_entry *dst = NULL;
Michael Chana4636962009-06-08 18:14:43 -07003687 struct net_device *realdev;
Michael Chan9b093362010-12-23 07:42:56 +00003688 __be16 local_port;
3689 u32 port_id;
Michael Chana4636962009-06-08 18:14:43 -07003690
3691 if (saddr->local.v6.sin6_family == AF_INET6 &&
3692 saddr->remote.v6.sin6_family == AF_INET6)
3693 is_v6 = 1;
3694 else if (saddr->local.v4.sin_family == AF_INET &&
3695 saddr->remote.v4.sin_family == AF_INET)
3696 is_v6 = 0;
3697 else
3698 return -EINVAL;
3699
3700 clear_bit(SK_F_IPV6, &csk->flags);
3701
3702 if (is_v6) {
Michael Chana4636962009-06-08 18:14:43 -07003703 set_bit(SK_F_IPV6, &csk->flags);
Michael Chanc76284a2010-02-24 14:42:07 +00003704 cnic_get_v6_route(&saddr->remote.v6, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003705
3706 memcpy(&csk->dst_ip[0], &saddr->remote.v6.sin6_addr,
3707 sizeof(struct in6_addr));
3708 csk->dst_port = saddr->remote.v6.sin6_port;
3709 local_port = saddr->local.v6.sin6_port;
Michael Chana4636962009-06-08 18:14:43 -07003710
3711 } else {
Michael Chanc76284a2010-02-24 14:42:07 +00003712 cnic_get_v4_route(&saddr->remote.v4, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003713
3714 csk->dst_ip[0] = saddr->remote.v4.sin_addr.s_addr;
3715 csk->dst_port = saddr->remote.v4.sin_port;
3716 local_port = saddr->local.v4.sin_port;
3717 }
3718
Michael Chanc76284a2010-02-24 14:42:07 +00003719 csk->vlan_id = 0;
3720 csk->mtu = dev->netdev->mtu;
3721 if (dst && dst->dev) {
3722 u16 vlan = cnic_get_vlan(dst->dev, &realdev);
3723 if (realdev == dev->netdev) {
3724 csk->vlan_id = vlan;
3725 csk->mtu = dst_mtu(dst);
3726 }
3727 }
Michael Chana4636962009-06-08 18:14:43 -07003728
Michael Chan9b093362010-12-23 07:42:56 +00003729 port_id = be16_to_cpu(local_port);
3730 if (port_id >= CNIC_LOCAL_PORT_MIN &&
3731 port_id < CNIC_LOCAL_PORT_MAX) {
3732 if (cnic_alloc_id(&cp->csk_port_tbl, port_id))
3733 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003734 } else
Michael Chan9b093362010-12-23 07:42:56 +00003735 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003736
Michael Chan9b093362010-12-23 07:42:56 +00003737 if (!port_id) {
3738 port_id = cnic_alloc_new_id(&cp->csk_port_tbl);
3739 if (port_id == -1) {
Michael Chana4636962009-06-08 18:14:43 -07003740 rc = -ENOMEM;
3741 goto err_out;
3742 }
Michael Chan9b093362010-12-23 07:42:56 +00003743 local_port = cpu_to_be16(port_id);
Michael Chana4636962009-06-08 18:14:43 -07003744 }
3745 csk->src_port = local_port;
3746
Michael Chana4636962009-06-08 18:14:43 -07003747err_out:
3748 dst_release(dst);
3749 return rc;
3750}
3751
3752static void cnic_init_csk_state(struct cnic_sock *csk)
3753{
3754 csk->state = 0;
3755 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3756 clear_bit(SK_F_CLOSING, &csk->flags);
3757}
3758
3759static int cnic_cm_connect(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3760{
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003761 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07003762 int err = 0;
3763
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003764 if (cp->ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI)
3765 return -EOPNOTSUPP;
3766
Michael Chana4636962009-06-08 18:14:43 -07003767 if (!cnic_in_use(csk))
3768 return -EINVAL;
3769
3770 if (test_and_set_bit(SK_F_CONNECT_START, &csk->flags))
3771 return -EINVAL;
3772
3773 cnic_init_csk_state(csk);
3774
3775 err = cnic_get_route(csk, saddr);
3776 if (err)
3777 goto err_out;
3778
3779 err = cnic_resolve_addr(csk, saddr);
3780 if (!err)
3781 return 0;
3782
3783err_out:
3784 clear_bit(SK_F_CONNECT_START, &csk->flags);
3785 return err;
3786}
3787
3788static int cnic_cm_abort(struct cnic_sock *csk)
3789{
3790 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chan7b34a462010-06-15 08:57:03 +00003791 u32 opcode = L4_KCQE_OPCODE_VALUE_RESET_COMP;
Michael Chana4636962009-06-08 18:14:43 -07003792
3793 if (!cnic_in_use(csk))
3794 return -EINVAL;
3795
3796 if (cnic_abort_prep(csk))
3797 return cnic_cm_abort_req(csk);
3798
3799 /* Getting here means that we haven't started connect, or
3800 * connect was not successful.
3801 */
3802
Michael Chana4636962009-06-08 18:14:43 -07003803 cp->close_conn(csk, opcode);
Michael Chan7b34a462010-06-15 08:57:03 +00003804 if (csk->state != opcode)
3805 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003806
3807 return 0;
3808}
3809
3810static int cnic_cm_close(struct cnic_sock *csk)
3811{
3812 if (!cnic_in_use(csk))
3813 return -EINVAL;
3814
3815 if (cnic_close_prep(csk)) {
3816 csk->state = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
3817 return cnic_cm_close_req(csk);
Michael Chaned99daa52010-06-15 08:57:00 +00003818 } else {
3819 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003820 }
3821 return 0;
3822}
3823
3824static void cnic_cm_upcall(struct cnic_local *cp, struct cnic_sock *csk,
3825 u8 opcode)
3826{
3827 struct cnic_ulp_ops *ulp_ops;
3828 int ulp_type = csk->ulp_type;
3829
3830 rcu_read_lock();
3831 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
3832 if (ulp_ops) {
3833 if (opcode == L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE)
3834 ulp_ops->cm_connect_complete(csk);
3835 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3836 ulp_ops->cm_close_complete(csk);
3837 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED)
3838 ulp_ops->cm_remote_abort(csk);
3839 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_COMP)
3840 ulp_ops->cm_abort_complete(csk);
3841 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED)
3842 ulp_ops->cm_remote_close(csk);
3843 }
3844 rcu_read_unlock();
3845}
3846
3847static int cnic_cm_set_pg(struct cnic_sock *csk)
3848{
3849 if (cnic_offld_prep(csk)) {
3850 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3851 cnic_cm_update_pg(csk);
3852 else
3853 cnic_cm_offload_pg(csk);
3854 }
3855 return 0;
3856}
3857
3858static void cnic_cm_process_offld_pg(struct cnic_dev *dev, struct l4_kcq *kcqe)
3859{
3860 struct cnic_local *cp = dev->cnic_priv;
3861 u32 l5_cid = kcqe->pg_host_opaque;
3862 u8 opcode = kcqe->op_code;
3863 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
3864
3865 csk_hold(csk);
3866 if (!cnic_in_use(csk))
3867 goto done;
3868
3869 if (opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3870 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3871 goto done;
3872 }
Eddie Waia9736c02010-02-24 14:42:04 +00003873 /* Possible PG kcqe status: SUCCESS, OFFLOADED_PG, or CTX_ALLOC_FAIL */
3874 if (kcqe->status == L4_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAIL) {
3875 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3876 cnic_cm_upcall(cp, csk,
3877 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3878 goto done;
3879 }
3880
Michael Chana4636962009-06-08 18:14:43 -07003881 csk->pg_cid = kcqe->pg_cid;
3882 set_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3883 cnic_cm_conn_req(csk);
3884
3885done:
3886 csk_put(csk);
3887}
3888
Michael Chane1928c82010-12-23 07:43:04 +00003889static void cnic_process_fcoe_term_conn(struct cnic_dev *dev, struct kcqe *kcqe)
3890{
3891 struct cnic_local *cp = dev->cnic_priv;
3892 struct fcoe_kcqe *fc_kcqe = (struct fcoe_kcqe *) kcqe;
3893 u32 l5_cid = fc_kcqe->fcoe_conn_id + BNX2X_FCOE_L5_CID_BASE;
3894 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3895
3896 ctx->timestamp = jiffies;
3897 ctx->wait_cond = 1;
3898 wake_up(&ctx->waitq);
3899}
3900
Michael Chana4636962009-06-08 18:14:43 -07003901static void cnic_cm_process_kcqe(struct cnic_dev *dev, struct kcqe *kcqe)
3902{
3903 struct cnic_local *cp = dev->cnic_priv;
3904 struct l4_kcq *l4kcqe = (struct l4_kcq *) kcqe;
3905 u8 opcode = l4kcqe->op_code;
3906 u32 l5_cid;
3907 struct cnic_sock *csk;
3908
Michael Chane1928c82010-12-23 07:43:04 +00003909 if (opcode == FCOE_RAMROD_CMD_ID_TERMINATE_CONN) {
3910 cnic_process_fcoe_term_conn(dev, kcqe);
3911 return;
3912 }
Michael Chana4636962009-06-08 18:14:43 -07003913 if (opcode == L4_KCQE_OPCODE_VALUE_OFFLOAD_PG ||
3914 opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3915 cnic_cm_process_offld_pg(dev, l4kcqe);
3916 return;
3917 }
3918
3919 l5_cid = l4kcqe->conn_id;
3920 if (opcode & 0x80)
3921 l5_cid = l4kcqe->cid;
3922 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3923 return;
3924
3925 csk = &cp->csk_tbl[l5_cid];
3926 csk_hold(csk);
3927
3928 if (!cnic_in_use(csk)) {
3929 csk_put(csk);
3930 return;
3931 }
3932
3933 switch (opcode) {
Eddie Waia9736c02010-02-24 14:42:04 +00003934 case L5CM_RAMROD_CMD_ID_TCP_CONNECT:
3935 if (l4kcqe->status != 0) {
3936 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3937 cnic_cm_upcall(cp, csk,
3938 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3939 }
3940 break;
Michael Chana4636962009-06-08 18:14:43 -07003941 case L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE:
3942 if (l4kcqe->status == 0)
3943 set_bit(SK_F_OFFLD_COMPLETE, &csk->flags);
Michael Chan8ec3e702012-03-21 15:38:34 +00003944 else if (l4kcqe->status ==
3945 L4_KCQE_COMPLETION_STATUS_PARITY_ERROR)
Jeffrey Huang0cb1f4b2012-02-08 17:33:56 +00003946 set_bit(SK_F_HW_ERR, &csk->flags);
Michael Chana4636962009-06-08 18:14:43 -07003947
3948 smp_mb__before_clear_bit();
3949 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3950 cnic_cm_upcall(cp, csk, opcode);
3951 break;
3952
3953 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
Michael Chana4636962009-06-08 18:14:43 -07003954 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3955 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan71034ba2009-10-10 13:46:59 +00003956 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3957 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
Michael Chan8ec3e702012-03-21 15:38:34 +00003958 if (l4kcqe->status == L4_KCQE_COMPLETION_STATUS_PARITY_ERROR)
Michael Chan23021c22012-01-04 12:12:28 +00003959 set_bit(SK_F_HW_ERR, &csk->flags);
3960
Michael Chana4636962009-06-08 18:14:43 -07003961 cp->close_conn(csk, opcode);
3962 break;
3963
3964 case L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED:
Michael Chan101c40c2011-06-08 19:29:33 +00003965 /* after we already sent CLOSE_REQ */
3966 if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) &&
3967 !test_bit(SK_F_OFFLD_COMPLETE, &csk->flags) &&
3968 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3969 cp->close_conn(csk, L4_KCQE_OPCODE_VALUE_RESET_COMP);
3970 else
3971 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07003972 break;
3973 }
3974 csk_put(csk);
3975}
3976
3977static void cnic_cm_indicate_kcqe(void *data, struct kcqe *kcqe[], u32 num)
3978{
3979 struct cnic_dev *dev = data;
3980 int i;
3981
3982 for (i = 0; i < num; i++)
3983 cnic_cm_process_kcqe(dev, kcqe[i]);
3984}
3985
3986static struct cnic_ulp_ops cm_ulp_ops = {
3987 .indicate_kcqes = cnic_cm_indicate_kcqe,
3988};
3989
3990static void cnic_cm_free_mem(struct cnic_dev *dev)
3991{
3992 struct cnic_local *cp = dev->cnic_priv;
3993
3994 kfree(cp->csk_tbl);
3995 cp->csk_tbl = NULL;
3996 cnic_free_id_tbl(&cp->csk_port_tbl);
3997}
3998
3999static int cnic_cm_alloc_mem(struct cnic_dev *dev)
4000{
4001 struct cnic_local *cp = dev->cnic_priv;
Eddie Wai11f23aa2011-06-08 19:29:34 +00004002 u32 port_id;
Michael Chana4636962009-06-08 18:14:43 -07004003
4004 cp->csk_tbl = kzalloc(sizeof(struct cnic_sock) * MAX_CM_SK_TBL_SZ,
4005 GFP_KERNEL);
4006 if (!cp->csk_tbl)
4007 return -ENOMEM;
4008
Michael Chan973e5742011-07-13 17:24:17 +00004009 port_id = random32();
Eddie Wai11f23aa2011-06-08 19:29:34 +00004010 port_id %= CNIC_LOCAL_PORT_RANGE;
Michael Chana4636962009-06-08 18:14:43 -07004011 if (cnic_init_id_tbl(&cp->csk_port_tbl, CNIC_LOCAL_PORT_RANGE,
Eddie Wai11f23aa2011-06-08 19:29:34 +00004012 CNIC_LOCAL_PORT_MIN, port_id)) {
Michael Chana4636962009-06-08 18:14:43 -07004013 cnic_cm_free_mem(dev);
4014 return -ENOMEM;
4015 }
4016 return 0;
4017}
4018
4019static int cnic_ready_to_close(struct cnic_sock *csk, u32 opcode)
4020{
Michael Chan943189f2010-06-15 08:57:02 +00004021 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
4022 /* Unsolicited RESET_COMP or RESET_RECEIVED */
4023 opcode = L4_KCQE_OPCODE_VALUE_RESET_RECEIVED;
4024 csk->state = opcode;
Michael Chana1e621b2010-06-15 08:57:01 +00004025 }
Michael Chan943189f2010-06-15 08:57:02 +00004026
4027 /* 1. If event opcode matches the expected event in csk->state
Michael Chan101c40c2011-06-08 19:29:33 +00004028 * 2. If the expected event is CLOSE_COMP or RESET_COMP, we accept any
4029 * event
Michael Chan7b34a462010-06-15 08:57:03 +00004030 * 3. If the expected event is 0, meaning the connection was never
4031 * never established, we accept the opcode from cm_abort.
Michael Chan943189f2010-06-15 08:57:02 +00004032 */
Michael Chan7b34a462010-06-15 08:57:03 +00004033 if (opcode == csk->state || csk->state == 0 ||
Michael Chan101c40c2011-06-08 19:29:33 +00004034 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP ||
4035 csk->state == L4_KCQE_OPCODE_VALUE_RESET_COMP) {
Michael Chan7b34a462010-06-15 08:57:03 +00004036 if (!test_and_set_bit(SK_F_CLOSING, &csk->flags)) {
4037 if (csk->state == 0)
4038 csk->state = opcode;
Michael Chana4636962009-06-08 18:14:43 -07004039 return 1;
Michael Chan7b34a462010-06-15 08:57:03 +00004040 }
Michael Chana4636962009-06-08 18:14:43 -07004041 }
4042 return 0;
4043}
4044
4045static void cnic_close_bnx2_conn(struct cnic_sock *csk, u32 opcode)
4046{
4047 struct cnic_dev *dev = csk->dev;
4048 struct cnic_local *cp = dev->cnic_priv;
4049
Michael Chana1e621b2010-06-15 08:57:01 +00004050 if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED) {
4051 cnic_cm_upcall(cp, csk, opcode);
4052 return;
4053 }
4054
Michael Chana4636962009-06-08 18:14:43 -07004055 clear_bit(SK_F_CONNECT_START, &csk->flags);
Eddie Wai66883e92010-02-24 14:42:05 +00004056 cnic_close_conn(csk);
Michael Chan7b34a462010-06-15 08:57:03 +00004057 csk->state = opcode;
Eddie Wai66883e92010-02-24 14:42:05 +00004058 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07004059}
4060
4061static void cnic_cm_stop_bnx2_hw(struct cnic_dev *dev)
4062{
4063}
4064
4065static int cnic_cm_init_bnx2_hw(struct cnic_dev *dev)
4066{
4067 u32 seed;
4068
Michael Chan973e5742011-07-13 17:24:17 +00004069 seed = random32();
Michael Chana4636962009-06-08 18:14:43 -07004070 cnic_ctx_wr(dev, 45, 0, seed);
4071 return 0;
4072}
4073
Michael Chan71034ba2009-10-10 13:46:59 +00004074static void cnic_close_bnx2x_conn(struct cnic_sock *csk, u32 opcode)
4075{
4076 struct cnic_dev *dev = csk->dev;
4077 struct cnic_local *cp = dev->cnic_priv;
4078 struct cnic_context *ctx = &cp->ctx_tbl[csk->l5_cid];
4079 union l5cm_specific_data l5_data;
4080 u32 cmd = 0;
4081 int close_complete = 0;
4082
4083 switch (opcode) {
4084 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
4085 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
4086 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan7b34a462010-06-15 08:57:03 +00004087 if (cnic_ready_to_close(csk, opcode)) {
Michael Chan23021c22012-01-04 12:12:28 +00004088 if (test_bit(SK_F_HW_ERR, &csk->flags))
4089 close_complete = 1;
4090 else if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
Michael Chan7b34a462010-06-15 08:57:03 +00004091 cmd = L5CM_RAMROD_CMD_ID_SEARCHER_DELETE;
4092 else
4093 close_complete = 1;
4094 }
Michael Chan71034ba2009-10-10 13:46:59 +00004095 break;
4096 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
4097 cmd = L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD;
4098 break;
4099 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
4100 close_complete = 1;
4101 break;
4102 }
4103 if (cmd) {
4104 memset(&l5_data, 0, sizeof(l5_data));
4105
4106 cnic_submit_kwqe_16(dev, cmd, csk->cid, ISCSI_CONNECTION_TYPE,
4107 &l5_data);
4108 } else if (close_complete) {
4109 ctx->timestamp = jiffies;
4110 cnic_close_conn(csk);
4111 cnic_cm_upcall(cp, csk, csk->state);
4112 }
4113}
4114
4115static void cnic_cm_stop_bnx2x_hw(struct cnic_dev *dev)
4116{
Michael Chanfdf24082010-10-13 14:06:47 +00004117 struct cnic_local *cp = dev->cnic_priv;
Michael Chanfdf24082010-10-13 14:06:47 +00004118
4119 if (!cp->ctx_tbl)
4120 return;
4121
4122 if (!netif_running(dev->netdev))
4123 return;
4124
Michael Chan74e49bb2011-07-20 14:55:23 +00004125 cnic_bnx2x_delete_wait(dev, 0);
Michael Chanfdf24082010-10-13 14:06:47 +00004126
4127 cancel_delayed_work(&cp->delete_task);
4128 flush_workqueue(cnic_wq);
4129
4130 if (atomic_read(&cp->iscsi_conn) != 0)
4131 netdev_warn(dev->netdev, "%d iSCSI connections not destroyed\n",
4132 atomic_read(&cp->iscsi_conn));
Michael Chan71034ba2009-10-10 13:46:59 +00004133}
4134
4135static int cnic_cm_init_bnx2x_hw(struct cnic_dev *dev)
4136{
4137 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00004138 u32 pfid = cp->pfid;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004139 u32 port = CNIC_PORT(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00004140
4141 cnic_init_bnx2x_mac(dev);
4142 cnic_bnx2x_set_tcp_timestamp(dev, 1);
4143
4144 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004145 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(pfid), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004146
4147 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004148 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_ENABLED_OFFSET(port), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00004149 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004150 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_MAX_COUNT_OFFSET(port),
Michael Chan71034ba2009-10-10 13:46:59 +00004151 DEF_MAX_DA_COUNT);
4152
4153 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004154 XSTORM_ISCSI_TCP_VARS_TTL_OFFSET(pfid), DEF_TTL);
Michael Chan71034ba2009-10-10 13:46:59 +00004155 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004156 XSTORM_ISCSI_TCP_VARS_TOS_OFFSET(pfid), DEF_TOS);
Michael Chan71034ba2009-10-10 13:46:59 +00004157 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004158 XSTORM_ISCSI_TCP_VARS_ADV_WND_SCL_OFFSET(pfid), 2);
Michael Chan71034ba2009-10-10 13:46:59 +00004159 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004160 XSTORM_TCP_TX_SWS_TIMER_VAL_OFFSET(pfid), DEF_SWS_TIMER);
Michael Chan71034ba2009-10-10 13:46:59 +00004161
Michael Chan14203982010-10-06 03:16:06 +00004162 CNIC_WR(dev, BAR_TSTRORM_INTMEM + TSTORM_TCP_MAX_CWND_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00004163 DEF_MAX_CWND);
4164 return 0;
4165}
4166
Michael Chanfdf24082010-10-13 14:06:47 +00004167static void cnic_delete_task(struct work_struct *work)
4168{
4169 struct cnic_local *cp;
4170 struct cnic_dev *dev;
4171 u32 i;
4172 int need_resched = 0;
4173
4174 cp = container_of(work, struct cnic_local, delete_task.work);
4175 dev = cp->dev;
4176
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07004177 if (test_and_clear_bit(CNIC_LCL_FL_STOP_ISCSI, &cp->cnic_local_flags)) {
4178 struct drv_ctl_info info;
4179
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07004180 cnic_ulp_stop_one(cp, CNIC_ULP_ISCSI);
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07004181
4182 info.cmd = DRV_CTL_ISCSI_STOPPED_CMD;
4183 cp->ethdev->drv_ctl(dev->netdev, &info);
4184 }
4185
Michael Chanfdf24082010-10-13 14:06:47 +00004186 for (i = 0; i < cp->max_cid_space; i++) {
4187 struct cnic_context *ctx = &cp->ctx_tbl[i];
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004188 int err;
Michael Chanfdf24082010-10-13 14:06:47 +00004189
4190 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags) ||
4191 !test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
4192 continue;
4193
4194 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
4195 need_resched = 1;
4196 continue;
4197 }
4198
4199 if (!test_and_clear_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
4200 continue;
4201
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004202 err = cnic_bnx2x_destroy_ramrod(dev, i);
Michael Chanfdf24082010-10-13 14:06:47 +00004203
4204 cnic_free_bnx2x_conn_resc(dev, i);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004205 if (!err) {
4206 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI)
4207 atomic_dec(&cp->iscsi_conn);
Michael Chanfdf24082010-10-13 14:06:47 +00004208
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004209 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
4210 }
Michael Chanfdf24082010-10-13 14:06:47 +00004211 }
4212
4213 if (need_resched)
4214 queue_delayed_work(cnic_wq, &cp->delete_task,
4215 msecs_to_jiffies(10));
4216
4217}
4218
Michael Chana4636962009-06-08 18:14:43 -07004219static int cnic_cm_open(struct cnic_dev *dev)
4220{
4221 struct cnic_local *cp = dev->cnic_priv;
4222 int err;
4223
4224 err = cnic_cm_alloc_mem(dev);
4225 if (err)
4226 return err;
4227
4228 err = cp->start_cm(dev);
4229
4230 if (err)
4231 goto err_out;
4232
Michael Chanfdf24082010-10-13 14:06:47 +00004233 INIT_DELAYED_WORK(&cp->delete_task, cnic_delete_task);
4234
Michael Chana4636962009-06-08 18:14:43 -07004235 dev->cm_create = cnic_cm_create;
4236 dev->cm_destroy = cnic_cm_destroy;
4237 dev->cm_connect = cnic_cm_connect;
4238 dev->cm_abort = cnic_cm_abort;
4239 dev->cm_close = cnic_cm_close;
4240 dev->cm_select_dev = cnic_cm_select_dev;
4241
4242 cp->ulp_handle[CNIC_ULP_L4] = dev;
4243 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], &cm_ulp_ops);
4244 return 0;
4245
4246err_out:
4247 cnic_cm_free_mem(dev);
4248 return err;
4249}
4250
4251static int cnic_cm_shutdown(struct cnic_dev *dev)
4252{
4253 struct cnic_local *cp = dev->cnic_priv;
4254 int i;
4255
4256 cp->stop_cm(dev);
4257
4258 if (!cp->csk_tbl)
4259 return 0;
4260
4261 for (i = 0; i < MAX_CM_SK_TBL_SZ; i++) {
4262 struct cnic_sock *csk = &cp->csk_tbl[i];
4263
4264 clear_bit(SK_F_INUSE, &csk->flags);
4265 cnic_cm_cleanup(csk);
4266 }
4267 cnic_cm_free_mem(dev);
4268
4269 return 0;
4270}
4271
4272static void cnic_init_context(struct cnic_dev *dev, u32 cid)
4273{
Michael Chana4636962009-06-08 18:14:43 -07004274 u32 cid_addr;
4275 int i;
4276
Michael Chana4636962009-06-08 18:14:43 -07004277 cid_addr = GET_CID_ADDR(cid);
4278
4279 for (i = 0; i < CTX_SIZE; i += 4)
4280 cnic_ctx_wr(dev, cid_addr, i, 0);
4281}
4282
4283static int cnic_setup_5709_context(struct cnic_dev *dev, int valid)
4284{
4285 struct cnic_local *cp = dev->cnic_priv;
4286 int ret = 0, i;
4287 u32 valid_bit = valid ? BNX2_CTX_HOST_PAGE_TBL_DATA0_VALID : 0;
4288
4289 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4290 return 0;
4291
4292 for (i = 0; i < cp->ctx_blks; i++) {
4293 int j;
4294 u32 idx = cp->ctx_arr[i].cid / cp->cids_per_blk;
4295 u32 val;
4296
4297 memset(cp->ctx_arr[i].ctx, 0, BCM_PAGE_SIZE);
4298
4299 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA0,
4300 (cp->ctx_arr[i].mapping & 0xffffffff) | valid_bit);
4301 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA1,
4302 (u64) cp->ctx_arr[i].mapping >> 32);
4303 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL, idx |
4304 BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
4305 for (j = 0; j < 10; j++) {
4306
4307 val = CNIC_RD(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL);
4308 if (!(val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ))
4309 break;
4310 udelay(5);
4311 }
4312 if (val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) {
4313 ret = -EBUSY;
4314 break;
4315 }
4316 }
4317 return ret;
4318}
4319
4320static void cnic_free_irq(struct cnic_dev *dev)
4321{
4322 struct cnic_local *cp = dev->cnic_priv;
4323 struct cnic_eth_dev *ethdev = cp->ethdev;
4324
4325 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4326 cp->disable_int_sync(dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004327 tasklet_kill(&cp->cnic_irq_task);
Michael Chana4636962009-06-08 18:14:43 -07004328 free_irq(ethdev->irq_arr[0].vector, dev);
4329 }
4330}
4331
Michael Chan6e0dc642010-10-13 14:06:44 +00004332static int cnic_request_irq(struct cnic_dev *dev)
4333{
4334 struct cnic_local *cp = dev->cnic_priv;
4335 struct cnic_eth_dev *ethdev = cp->ethdev;
4336 int err;
4337
4338 err = request_irq(ethdev->irq_arr[0].vector, cnic_irq, 0, "cnic", dev);
4339 if (err)
4340 tasklet_disable(&cp->cnic_irq_task);
4341
4342 return err;
4343}
4344
Michael Chana4636962009-06-08 18:14:43 -07004345static int cnic_init_bnx2_irq(struct cnic_dev *dev)
4346{
4347 struct cnic_local *cp = dev->cnic_priv;
4348 struct cnic_eth_dev *ethdev = cp->ethdev;
4349
4350 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4351 int err, i = 0;
4352 int sblk_num = cp->status_blk_num;
4353 u32 base = ((sblk_num - 1) * BNX2_HC_SB_CONFIG_SIZE) +
4354 BNX2_HC_SB_CONFIG_1;
4355
4356 CNIC_WR(dev, base, BNX2_HC_SB_CONFIG_1_ONE_SHOT);
4357
4358 CNIC_WR(dev, base + BNX2_HC_COMP_PROD_TRIP_OFF, (2 << 16) | 8);
4359 CNIC_WR(dev, base + BNX2_HC_COM_TICKS_OFF, (64 << 16) | 220);
4360 CNIC_WR(dev, base + BNX2_HC_CMD_TICKS_OFF, (64 << 16) | 220);
4361
Michael Chana4dde3a2010-02-24 14:42:08 +00004362 cp->last_status_idx = cp->status_blk.bnx2->status_idx;
Joe Perches164165d2009-11-19 09:30:10 +00004363 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2_msix,
Michael Chana4636962009-06-08 18:14:43 -07004364 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004365 err = cnic_request_irq(dev);
4366 if (err)
Michael Chana4636962009-06-08 18:14:43 -07004367 return err;
Michael Chan6e0dc642010-10-13 14:06:44 +00004368
Michael Chana4dde3a2010-02-24 14:42:08 +00004369 while (cp->status_blk.bnx2->status_completion_producer_index &&
Michael Chana4636962009-06-08 18:14:43 -07004370 i < 10) {
4371 CNIC_WR(dev, BNX2_HC_COALESCE_NOW,
4372 1 << (11 + sblk_num));
4373 udelay(10);
4374 i++;
4375 barrier();
4376 }
Michael Chana4dde3a2010-02-24 14:42:08 +00004377 if (cp->status_blk.bnx2->status_completion_producer_index) {
Michael Chana4636962009-06-08 18:14:43 -07004378 cnic_free_irq(dev);
4379 goto failed;
4380 }
4381
4382 } else {
Michael Chana4dde3a2010-02-24 14:42:08 +00004383 struct status_block *sblk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004384 u32 hc_cmd = CNIC_RD(dev, BNX2_HC_COMMAND);
4385 int i = 0;
4386
4387 while (sblk->status_completion_producer_index && i < 10) {
4388 CNIC_WR(dev, BNX2_HC_COMMAND,
4389 hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
4390 udelay(10);
4391 i++;
4392 barrier();
4393 }
4394 if (sblk->status_completion_producer_index)
4395 goto failed;
4396
4397 }
4398 return 0;
4399
4400failed:
Joe Perchesddf79b22010-02-17 15:01:54 +00004401 netdev_err(dev->netdev, "KCQ index not resetting to 0\n");
Michael Chana4636962009-06-08 18:14:43 -07004402 return -EBUSY;
4403}
4404
4405static void cnic_enable_bnx2_int(struct cnic_dev *dev)
4406{
4407 struct cnic_local *cp = dev->cnic_priv;
4408 struct cnic_eth_dev *ethdev = cp->ethdev;
4409
4410 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4411 return;
4412
4413 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4414 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
4415}
4416
4417static void cnic_disable_bnx2_int_sync(struct cnic_dev *dev)
4418{
4419 struct cnic_local *cp = dev->cnic_priv;
4420 struct cnic_eth_dev *ethdev = cp->ethdev;
4421
4422 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4423 return;
4424
4425 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4426 BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
4427 CNIC_RD(dev, BNX2_PCICFG_INT_ACK_CMD);
4428 synchronize_irq(ethdev->irq_arr[0].vector);
4429}
4430
4431static void cnic_init_bnx2_tx_ring(struct cnic_dev *dev)
4432{
4433 struct cnic_local *cp = dev->cnic_priv;
4434 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004435 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004436 u32 cid_addr, tx_cid, sb_id;
4437 u32 val, offset0, offset1, offset2, offset3;
4438 int i;
4439 struct tx_bd *txbd;
Michael Chancd801532010-10-13 14:06:49 +00004440 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Michael Chana4dde3a2010-02-24 14:42:08 +00004441 struct status_block *s_blk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004442
4443 sb_id = cp->status_blk_num;
4444 tx_cid = 20;
Michael Chana4636962009-06-08 18:14:43 -07004445 cp->tx_cons_ptr = &s_blk->status_tx_quick_consumer_index2;
4446 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004447 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004448
4449 tx_cid = TX_TSS_CID + sb_id - 1;
Michael Chana4636962009-06-08 18:14:43 -07004450 CNIC_WR(dev, BNX2_TSCH_TSS_CFG, (sb_id << 24) |
4451 (TX_TSS_CID << 7));
4452 cp->tx_cons_ptr = &sblk->status_tx_quick_consumer_index;
4453 }
4454 cp->tx_cons = *cp->tx_cons_ptr;
4455
4456 cid_addr = GET_CID_ADDR(tx_cid);
4457 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
4458 u32 cid_addr2 = GET_CID_ADDR(tx_cid + 4) + 0x40;
4459
4460 for (i = 0; i < PHY_CTX_SIZE; i += 4)
4461 cnic_ctx_wr(dev, cid_addr2, i, 0);
4462
4463 offset0 = BNX2_L2CTX_TYPE_XI;
4464 offset1 = BNX2_L2CTX_CMD_TYPE_XI;
4465 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI_XI;
4466 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO_XI;
4467 } else {
Michael Chanb58ffb42010-05-27 16:31:41 -07004468 cnic_init_context(dev, tx_cid);
4469 cnic_init_context(dev, tx_cid + 1);
4470
Michael Chana4636962009-06-08 18:14:43 -07004471 offset0 = BNX2_L2CTX_TYPE;
4472 offset1 = BNX2_L2CTX_CMD_TYPE;
4473 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI;
4474 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO;
4475 }
4476 val = BNX2_L2CTX_TYPE_TYPE_L2 | BNX2_L2CTX_TYPE_SIZE_L2;
4477 cnic_ctx_wr(dev, cid_addr, offset0, val);
4478
4479 val = BNX2_L2CTX_CMD_TYPE_TYPE_L2 | (8 << 16);
4480 cnic_ctx_wr(dev, cid_addr, offset1, val);
4481
Joe Perches43d620c2011-06-16 19:08:06 +00004482 txbd = udev->l2_ring;
Michael Chana4636962009-06-08 18:14:43 -07004483
Michael Chancd801532010-10-13 14:06:49 +00004484 buf_map = udev->l2_buf_map;
Michael Chana4636962009-06-08 18:14:43 -07004485 for (i = 0; i < MAX_TX_DESC_CNT; i++, txbd++) {
4486 txbd->tx_bd_haddr_hi = (u64) buf_map >> 32;
4487 txbd->tx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4488 }
Michael Chancd801532010-10-13 14:06:49 +00004489 val = (u64) ring_map >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004490 cnic_ctx_wr(dev, cid_addr, offset2, val);
4491 txbd->tx_bd_haddr_hi = val;
4492
Michael Chancd801532010-10-13 14:06:49 +00004493 val = (u64) ring_map & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004494 cnic_ctx_wr(dev, cid_addr, offset3, val);
4495 txbd->tx_bd_haddr_lo = val;
4496}
4497
4498static void cnic_init_bnx2_rx_ring(struct cnic_dev *dev)
4499{
4500 struct cnic_local *cp = dev->cnic_priv;
4501 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004502 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004503 u32 cid_addr, sb_id, val, coal_reg, coal_val;
4504 int i;
4505 struct rx_bd *rxbd;
Michael Chana4dde3a2010-02-24 14:42:08 +00004506 struct status_block *s_blk = cp->status_blk.gen;
Michael Chancd801532010-10-13 14:06:49 +00004507 dma_addr_t ring_map = udev->l2_ring_map;
Michael Chana4636962009-06-08 18:14:43 -07004508
4509 sb_id = cp->status_blk_num;
4510 cnic_init_context(dev, 2);
4511 cp->rx_cons_ptr = &s_blk->status_rx_quick_consumer_index2;
4512 coal_reg = BNX2_HC_COMMAND;
4513 coal_val = CNIC_RD(dev, coal_reg);
4514 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004515 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004516
4517 cp->rx_cons_ptr = &sblk->status_rx_quick_consumer_index;
4518 coal_reg = BNX2_HC_COALESCE_NOW;
4519 coal_val = 1 << (11 + sb_id);
4520 }
4521 i = 0;
4522 while (!(*cp->rx_cons_ptr != 0) && i < 10) {
4523 CNIC_WR(dev, coal_reg, coal_val);
4524 udelay(10);
4525 i++;
4526 barrier();
4527 }
4528 cp->rx_cons = *cp->rx_cons_ptr;
4529
4530 cid_addr = GET_CID_ADDR(2);
4531 val = BNX2_L2CTX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
4532 BNX2_L2CTX_CTX_TYPE_SIZE_L2 | (0x02 << 8);
4533 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_CTX_TYPE, val);
4534
4535 if (sb_id == 0)
Michael Chand0549382009-10-28 03:41:59 -07004536 val = 2 << BNX2_L2CTX_L2_STATUSB_NUM_SHIFT;
Michael Chana4636962009-06-08 18:14:43 -07004537 else
Michael Chand0549382009-10-28 03:41:59 -07004538 val = BNX2_L2CTX_L2_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004539 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_HOST_BDIDX, val);
4540
Joe Perches43d620c2011-06-16 19:08:06 +00004541 rxbd = udev->l2_ring + BCM_PAGE_SIZE;
Michael Chana4636962009-06-08 18:14:43 -07004542 for (i = 0; i < MAX_RX_DESC_CNT; i++, rxbd++) {
4543 dma_addr_t buf_map;
4544 int n = (i % cp->l2_rx_ring_size) + 1;
4545
Michael Chancd801532010-10-13 14:06:49 +00004546 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chana4636962009-06-08 18:14:43 -07004547 rxbd->rx_bd_len = cp->l2_single_buf_size;
4548 rxbd->rx_bd_flags = RX_BD_FLAGS_START | RX_BD_FLAGS_END;
4549 rxbd->rx_bd_haddr_hi = (u64) buf_map >> 32;
4550 rxbd->rx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4551 }
Michael Chancd801532010-10-13 14:06:49 +00004552 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004553 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_HI, val);
4554 rxbd->rx_bd_haddr_hi = val;
4555
Michael Chancd801532010-10-13 14:06:49 +00004556 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004557 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_LO, val);
4558 rxbd->rx_bd_haddr_lo = val;
4559
4560 val = cnic_reg_rd_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD);
4561 cnic_reg_wr_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD, val | (1 << 2));
4562}
4563
4564static void cnic_shutdown_bnx2_rx_ring(struct cnic_dev *dev)
4565{
4566 struct kwqe *wqes[1], l2kwqe;
4567
4568 memset(&l2kwqe, 0, sizeof(l2kwqe));
4569 wqes[0] = &l2kwqe;
Michael Chane1928c82010-12-23 07:43:04 +00004570 l2kwqe.kwqe_op_flag = (L2_LAYER_CODE << KWQE_LAYER_SHIFT) |
Michael Chana4636962009-06-08 18:14:43 -07004571 (L2_KWQE_OPCODE_VALUE_FLUSH <<
4572 KWQE_OPCODE_SHIFT) | 2;
4573 dev->submit_kwqes(dev, wqes, 1);
4574}
4575
4576static void cnic_set_bnx2_mac(struct cnic_dev *dev)
4577{
4578 struct cnic_local *cp = dev->cnic_priv;
4579 u32 val;
4580
4581 val = cp->func << 2;
4582
4583 cp->shmem_base = cnic_reg_rd_ind(dev, BNX2_SHM_HDR_ADDR_0 + val);
4584
4585 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4586 BNX2_PORT_HW_CFG_ISCSI_MAC_UPPER);
4587 dev->mac_addr[0] = (u8) (val >> 8);
4588 dev->mac_addr[1] = (u8) val;
4589
4590 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH4, val);
4591
4592 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4593 BNX2_PORT_HW_CFG_ISCSI_MAC_LOWER);
4594 dev->mac_addr[2] = (u8) (val >> 24);
4595 dev->mac_addr[3] = (u8) (val >> 16);
4596 dev->mac_addr[4] = (u8) (val >> 8);
4597 dev->mac_addr[5] = (u8) val;
4598
4599 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH5, val);
4600
4601 val = 4 | BNX2_RPM_SORT_USER2_BC_EN;
4602 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4603 val |= BNX2_RPM_SORT_USER2_PROM_VLAN;
4604
4605 CNIC_WR(dev, BNX2_RPM_SORT_USER2, 0x0);
4606 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val);
4607 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val | BNX2_RPM_SORT_USER2_ENA);
4608}
4609
4610static int cnic_start_bnx2_hw(struct cnic_dev *dev)
4611{
4612 struct cnic_local *cp = dev->cnic_priv;
4613 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chana4dde3a2010-02-24 14:42:08 +00004614 struct status_block *sblk = cp->status_blk.gen;
Michael Chane6c28892010-06-24 14:58:39 +00004615 u32 val, kcq_cid_addr, kwq_cid_addr;
Michael Chana4636962009-06-08 18:14:43 -07004616 int err;
4617
4618 cnic_set_bnx2_mac(dev);
4619
4620 val = CNIC_RD(dev, BNX2_MQ_CONFIG);
4621 val &= ~BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE;
4622 if (BCM_PAGE_BITS > 12)
4623 val |= (12 - 8) << 4;
4624 else
4625 val |= (BCM_PAGE_BITS - 8) << 4;
4626
4627 CNIC_WR(dev, BNX2_MQ_CONFIG, val);
4628
4629 CNIC_WR(dev, BNX2_HC_COMP_PROD_TRIP, (2 << 16) | 8);
4630 CNIC_WR(dev, BNX2_HC_COM_TICKS, (64 << 16) | 220);
4631 CNIC_WR(dev, BNX2_HC_CMD_TICKS, (64 << 16) | 220);
4632
4633 err = cnic_setup_5709_context(dev, 1);
4634 if (err)
4635 return err;
4636
4637 cnic_init_context(dev, KWQ_CID);
4638 cnic_init_context(dev, KCQ_CID);
4639
Michael Chane6c28892010-06-24 14:58:39 +00004640 kwq_cid_addr = GET_CID_ADDR(KWQ_CID);
Michael Chana4636962009-06-08 18:14:43 -07004641 cp->kwq_io_addr = MB_GET_CID_ADDR(KWQ_CID) + L5_KRNLQ_HOST_QIDX;
4642
4643 cp->max_kwq_idx = MAX_KWQ_IDX;
4644 cp->kwq_prod_idx = 0;
4645 cp->kwq_con_idx = 0;
Michael Chan1f1332a2010-05-18 11:32:52 +00004646 set_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07004647
4648 if (CHIP_NUM(cp) == CHIP_NUM_5706 || CHIP_NUM(cp) == CHIP_NUM_5708)
4649 cp->kwq_con_idx_ptr = &sblk->status_rx_quick_consumer_index15;
4650 else
4651 cp->kwq_con_idx_ptr = &sblk->status_cmd_consumer_index;
4652
4653 /* Initialize the kernel work queue context. */
4654 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4655 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004656 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004657
4658 val = (BCM_PAGE_SIZE / sizeof(struct kwqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004659 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004660
4661 val = ((BCM_PAGE_SIZE / sizeof(struct kwqe)) << 16) | KWQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004662 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004663
4664 val = (u32) ((u64) cp->kwq_info.pgtbl_map >> 32);
Michael Chane6c28892010-06-24 14:58:39 +00004665 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004666
4667 val = (u32) cp->kwq_info.pgtbl_map;
Michael Chane6c28892010-06-24 14:58:39 +00004668 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004669
Michael Chane6c28892010-06-24 14:58:39 +00004670 kcq_cid_addr = GET_CID_ADDR(KCQ_CID);
4671 cp->kcq1.io_addr = MB_GET_CID_ADDR(KCQ_CID) + L5_KRNLQ_HOST_QIDX;
Michael Chana4636962009-06-08 18:14:43 -07004672
Michael Chane6c28892010-06-24 14:58:39 +00004673 cp->kcq1.sw_prod_idx = 0;
4674 cp->kcq1.hw_prod_idx_ptr =
Joe Perches64699332012-06-04 12:44:16 +00004675 &sblk->status_completion_producer_index;
Michael Chane6c28892010-06-24 14:58:39 +00004676
Joe Perches64699332012-06-04 12:44:16 +00004677 cp->kcq1.status_idx_ptr = &sblk->status_idx;
Michael Chana4636962009-06-08 18:14:43 -07004678
4679 /* Initialize the kernel complete queue context. */
4680 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4681 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004682 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004683
4684 val = (BCM_PAGE_SIZE / sizeof(struct kcqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004685 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004686
4687 val = ((BCM_PAGE_SIZE / sizeof(struct kcqe)) << 16) | KCQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004688 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004689
Michael Chane6c28892010-06-24 14:58:39 +00004690 val = (u32) ((u64) cp->kcq1.dma.pgtbl_map >> 32);
4691 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004692
Michael Chane6c28892010-06-24 14:58:39 +00004693 val = (u32) cp->kcq1.dma.pgtbl_map;
4694 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004695
4696 cp->int_num = 0;
4697 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chane6c28892010-06-24 14:58:39 +00004698 struct status_block_msix *msblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004699 u32 sb_id = cp->status_blk_num;
Michael Chand0549382009-10-28 03:41:59 -07004700 u32 sb = BNX2_L2CTX_L5_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004701
Michael Chane6c28892010-06-24 14:58:39 +00004702 cp->kcq1.hw_prod_idx_ptr =
Joe Perches64699332012-06-04 12:44:16 +00004703 &msblk->status_completion_producer_index;
4704 cp->kcq1.status_idx_ptr = &msblk->status_idx;
4705 cp->kwq_con_idx_ptr = &msblk->status_cmd_consumer_index;
Michael Chana4636962009-06-08 18:14:43 -07004706 cp->int_num = sb_id << BNX2_PCICFG_INT_ACK_CMD_INT_NUM_SHIFT;
Michael Chane6c28892010-06-24 14:58:39 +00004707 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
4708 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
Michael Chana4636962009-06-08 18:14:43 -07004709 }
4710
4711 /* Enable Commnad Scheduler notification when we write to the
4712 * host producer index of the kernel contexts. */
4713 CNIC_WR(dev, BNX2_MQ_KNL_CMD_MASK1, 2);
4714
4715 /* Enable Command Scheduler notification when we write to either
4716 * the Send Queue or Receive Queue producer indexes of the kernel
4717 * bypass contexts. */
4718 CNIC_WR(dev, BNX2_MQ_KNL_BYP_CMD_MASK1, 7);
4719 CNIC_WR(dev, BNX2_MQ_KNL_BYP_WRITE_MASK1, 7);
4720
4721 /* Notify COM when the driver post an application buffer. */
4722 CNIC_WR(dev, BNX2_MQ_KNL_RX_V2P_MASK2, 0x2000);
4723
4724 /* Set the CP and COM doorbells. These two processors polls the
4725 * doorbell for a non zero value before running. This must be done
4726 * after setting up the kernel queue contexts. */
4727 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 1);
4728 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 1);
4729
4730 cnic_init_bnx2_tx_ring(dev);
4731 cnic_init_bnx2_rx_ring(dev);
4732
4733 err = cnic_init_bnx2_irq(dev);
4734 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00004735 netdev_err(dev->netdev, "cnic_init_irq failed\n");
Michael Chana4636962009-06-08 18:14:43 -07004736 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
4737 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
4738 return err;
4739 }
4740
4741 return 0;
4742}
4743
Michael Chan71034ba2009-10-10 13:46:59 +00004744static void cnic_setup_bnx2x_context(struct cnic_dev *dev)
4745{
4746 struct cnic_local *cp = dev->cnic_priv;
4747 struct cnic_eth_dev *ethdev = cp->ethdev;
4748 u32 start_offset = ethdev->ctx_tbl_offset;
4749 int i;
4750
4751 for (i = 0; i < cp->ctx_blks; i++) {
4752 struct cnic_ctx *ctx = &cp->ctx_arr[i];
4753 dma_addr_t map = ctx->mapping;
4754
4755 if (cp->ctx_align) {
4756 unsigned long mask = cp->ctx_align - 1;
4757
4758 map = (map + mask) & ~mask;
4759 }
4760
4761 cnic_ctx_tbl_wr(dev, start_offset + i, map);
4762 }
4763}
4764
4765static int cnic_init_bnx2x_irq(struct cnic_dev *dev)
4766{
4767 struct cnic_local *cp = dev->cnic_priv;
4768 struct cnic_eth_dev *ethdev = cp->ethdev;
4769 int err = 0;
4770
Joe Perches164165d2009-11-19 09:30:10 +00004771 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2x_bh,
Michael Chan71034ba2009-10-10 13:46:59 +00004772 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004773 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
4774 err = cnic_request_irq(dev);
4775
Michael Chan71034ba2009-10-10 13:46:59 +00004776 return err;
4777}
4778
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004779static inline void cnic_storm_memset_hc_disable(struct cnic_dev *dev,
4780 u16 sb_id, u8 sb_index,
4781 u8 disable)
4782{
4783
4784 u32 addr = BAR_CSTRORM_INTMEM +
4785 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4786 offsetof(struct hc_status_block_data_e1x, index_data) +
4787 sizeof(struct hc_index_data)*sb_index +
4788 offsetof(struct hc_index_data, flags);
4789 u16 flags = CNIC_RD16(dev, addr);
4790 /* clear and set */
4791 flags &= ~HC_INDEX_DATA_HC_ENABLED;
4792 flags |= (((~disable) << HC_INDEX_DATA_HC_ENABLED_SHIFT) &
4793 HC_INDEX_DATA_HC_ENABLED);
4794 CNIC_WR16(dev, addr, flags);
4795}
4796
Michael Chan71034ba2009-10-10 13:46:59 +00004797static void cnic_enable_bnx2x_int(struct cnic_dev *dev)
4798{
4799 struct cnic_local *cp = dev->cnic_priv;
4800 u8 sb_id = cp->status_blk_num;
Michael Chan71034ba2009-10-10 13:46:59 +00004801
4802 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004803 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4804 offsetof(struct hc_status_block_data_e1x, index_data) +
4805 sizeof(struct hc_index_data)*HC_INDEX_ISCSI_EQ_CONS +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004806 offsetof(struct hc_index_data, timeout), 64 / 4);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004807 cnic_storm_memset_hc_disable(dev, sb_id, HC_INDEX_ISCSI_EQ_CONS, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004808}
4809
4810static void cnic_disable_bnx2x_int_sync(struct cnic_dev *dev)
4811{
4812}
4813
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004814static void cnic_init_bnx2x_tx_ring(struct cnic_dev *dev,
4815 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004816{
4817 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004818 struct cnic_uio_dev *udev = cp->udev;
4819 union eth_tx_bd_types *txbd = (union eth_tx_bd_types *) udev->l2_ring;
4820 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004821 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004822 int i;
Michael Chan5159fdc2010-12-23 07:42:59 +00004823 u32 cli = cp->ethdev->iscsi_l2_client_id;
Michael Chan71034ba2009-10-10 13:46:59 +00004824 u32 val;
4825
4826 memset(txbd, 0, BCM_PAGE_SIZE);
4827
Michael Chancd801532010-10-13 14:06:49 +00004828 buf_map = udev->l2_buf_map;
Michael Chan71034ba2009-10-10 13:46:59 +00004829 for (i = 0; i < MAX_TX_DESC_CNT; i += 3, txbd += 3) {
4830 struct eth_tx_start_bd *start_bd = &txbd->start_bd;
4831 struct eth_tx_bd *reg_bd = &((txbd + 2)->reg_bd);
4832
4833 start_bd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4834 start_bd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4835 reg_bd->addr_hi = start_bd->addr_hi;
4836 reg_bd->addr_lo = start_bd->addr_lo + 0x10;
4837 start_bd->nbytes = cpu_to_le16(0x10);
4838 start_bd->nbd = cpu_to_le16(3);
4839 start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
4840 start_bd->general_data = (UNICAST_ADDRESS <<
4841 ETH_TX_START_BD_ETH_ADDR_TYPE_SHIFT);
4842 start_bd->general_data |= (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
4843
4844 }
Michael Chan71034ba2009-10-10 13:46:59 +00004845
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004846 val = (u64) ring_map >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004847 txbd->next_bd.addr_hi = cpu_to_le32(val);
4848
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004849 data->tx.tx_bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004850
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004851 val = (u64) ring_map & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004852 txbd->next_bd.addr_lo = cpu_to_le32(val);
4853
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004854 data->tx.tx_bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004855
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004856 /* Other ramrod params */
4857 data->tx.tx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_CQ_CONS;
4858 data->tx.tx_status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004859
4860 /* reset xstorm per client statistics */
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004861 if (cli < MAX_STAT_COUNTER_ID) {
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004862 data->general.statistics_zero_flg = 1;
4863 data->general.statistics_en_flg = 1;
4864 data->general.statistics_counter_id = cli;
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004865 }
Michael Chan71034ba2009-10-10 13:46:59 +00004866
4867 cp->tx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004868 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_CQ_CONS];
Michael Chan71034ba2009-10-10 13:46:59 +00004869}
4870
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004871static void cnic_init_bnx2x_rx_ring(struct cnic_dev *dev,
4872 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004873{
4874 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004875 struct cnic_uio_dev *udev = cp->udev;
4876 struct eth_rx_bd *rxbd = (struct eth_rx_bd *) (udev->l2_ring +
Michael Chan71034ba2009-10-10 13:46:59 +00004877 BCM_PAGE_SIZE);
4878 struct eth_rx_cqe_next_page *rxcqe = (struct eth_rx_cqe_next_page *)
Michael Chancd801532010-10-13 14:06:49 +00004879 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004880 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004881 int i;
Michael Chan5159fdc2010-12-23 07:42:59 +00004882 u32 cli = cp->ethdev->iscsi_l2_client_id;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004883 int cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
Michael Chan71034ba2009-10-10 13:46:59 +00004884 u32 val;
Michael Chancd801532010-10-13 14:06:49 +00004885 dma_addr_t ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004886
4887 /* General data */
4888 data->general.client_id = cli;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004889 data->general.activate_flg = 1;
4890 data->general.sp_client_id = cli;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004891 data->general.mtu = cpu_to_le16(cp->l2_single_buf_size - 14);
4892 data->general.func_id = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00004893
4894 for (i = 0; i < BNX2X_MAX_RX_DESC_CNT; i++, rxbd++) {
4895 dma_addr_t buf_map;
4896 int n = (i % cp->l2_rx_ring_size) + 1;
4897
Michael Chancd801532010-10-13 14:06:49 +00004898 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chan71034ba2009-10-10 13:46:59 +00004899 rxbd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4900 rxbd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4901 }
Michael Chan71034ba2009-10-10 13:46:59 +00004902
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004903 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004904 rxbd->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004905 data->rx.bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004906
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004907 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004908 rxbd->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004909 data->rx.bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004910
4911 rxcqe += BNX2X_MAX_RCQ_DESC_CNT;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004912 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004913 rxcqe->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004914 data->rx.cqe_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004915
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004916 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004917 rxcqe->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004918 data->rx.cqe_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004919
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004920 /* Other ramrod params */
4921 data->rx.client_qzone_id = cl_qzone_id;
4922 data->rx.rx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS;
4923 data->rx.status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004924
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004925 data->rx.cache_line_alignment_log_size = L1_CACHE_SHIFT;
Michael Chan71034ba2009-10-10 13:46:59 +00004926
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004927 data->rx.max_bytes_on_bd = cpu_to_le16(cp->l2_single_buf_size);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004928 data->rx.outer_vlan_removal_enable_flg = 1;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004929 data->rx.silent_vlan_removal_flg = 1;
4930 data->rx.silent_vlan_value = 0;
4931 data->rx.silent_vlan_mask = 0xffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004932
4933 cp->rx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004934 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS];
Michael Chan5159fdc2010-12-23 07:42:59 +00004935 cp->rx_cons = *cp->rx_cons_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00004936}
4937
Michael Chane21ba412010-12-23 07:43:03 +00004938static void cnic_init_bnx2x_kcq(struct cnic_dev *dev)
4939{
4940 struct cnic_local *cp = dev->cnic_priv;
4941 u32 pfid = cp->pfid;
4942
4943 cp->kcq1.io_addr = BAR_CSTRORM_INTMEM +
4944 CSTORM_ISCSI_EQ_PROD_OFFSET(pfid, 0);
4945 cp->kcq1.sw_prod_idx = 0;
4946
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004947 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chane21ba412010-12-23 07:43:03 +00004948 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4949
4950 cp->kcq1.hw_prod_idx_ptr =
4951 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4952 cp->kcq1.status_idx_ptr =
4953 &sb->sb.running_index[SM_RX_ID];
4954 } else {
4955 struct host_hc_status_block_e1x *sb = cp->status_blk.gen;
4956
4957 cp->kcq1.hw_prod_idx_ptr =
4958 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4959 cp->kcq1.status_idx_ptr =
4960 &sb->sb.running_index[SM_RX_ID];
4961 }
4962
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004963 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chane21ba412010-12-23 07:43:03 +00004964 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4965
4966 cp->kcq2.io_addr = BAR_USTRORM_INTMEM +
4967 USTORM_FCOE_EQ_PROD_OFFSET(pfid);
4968 cp->kcq2.sw_prod_idx = 0;
4969 cp->kcq2.hw_prod_idx_ptr =
4970 &sb->sb.index_values[HC_INDEX_FCOE_EQ_CONS];
4971 cp->kcq2.status_idx_ptr =
4972 &sb->sb.running_index[SM_RX_ID];
4973 }
4974}
4975
Michael Chan71034ba2009-10-10 13:46:59 +00004976static int cnic_start_bnx2x_hw(struct cnic_dev *dev)
4977{
4978 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004979 struct cnic_eth_dev *ethdev = cp->ethdev;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004980 int func = CNIC_FUNC(cp), ret;
Michael Chan14203982010-10-06 03:16:06 +00004981 u32 pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00004982
Michael Chana9e0a4f2012-01-04 12:12:27 +00004983 dev->stats_addr = ethdev->addr_drv_info_to_mcp;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004984 cp->port_mode = CHIP_PORT_MODE_NONE;
4985
4986 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chanee87a822010-10-13 14:06:51 +00004987 u32 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN_OVWR);
4988
4989 if (!(val & 1))
4990 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN);
4991 else
4992 val = (val >> 1) & 1;
4993
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004994 if (val) {
4995 cp->port_mode = CHIP_4_PORT_MODE;
Michael Chanee87a822010-10-13 14:06:51 +00004996 cp->pfid = func >> 1;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004997 } else {
Michael Chanb7d40312011-07-13 17:24:18 +00004998 cp->port_mode = CHIP_2_PORT_MODE;
Michael Chanee87a822010-10-13 14:06:51 +00004999 cp->pfid = func & 0x6;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005000 }
Michael Chanee87a822010-10-13 14:06:51 +00005001 } else {
5002 cp->pfid = func;
5003 }
Michael Chan14203982010-10-06 03:16:06 +00005004 pfid = cp->pfid;
5005
Michael Chan71034ba2009-10-10 13:46:59 +00005006 ret = cnic_init_id_tbl(&cp->cid_tbl, MAX_ISCSI_TBL_SZ,
Eddie Wai11f23aa2011-06-08 19:29:34 +00005007 cp->iscsi_start_cid, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00005008
5009 if (ret)
5010 return -ENOMEM;
5011
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005012 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chandc219a22011-08-26 09:45:39 +00005013 ret = cnic_init_id_tbl(&cp->fcoe_cid_tbl, dev->max_fcoe_conn,
Eddie Wai11f23aa2011-06-08 19:29:34 +00005014 cp->fcoe_start_cid, 0);
Michael Chane1928c82010-12-23 07:43:04 +00005015
5016 if (ret)
5017 return -ENOMEM;
5018 }
5019
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005020 cp->bnx2x_igu_sb_id = ethdev->irq_arr[0].status_blk_num2;
5021
Michael Chane21ba412010-12-23 07:43:03 +00005022 cnic_init_bnx2x_kcq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005023
Michael Chan71034ba2009-10-10 13:46:59 +00005024 /* Only 1 EQ */
Michael Chane6c28892010-06-24 14:58:39 +00005025 CNIC_WR16(dev, cp->kcq1.io_addr, MAX_KCQ_IDX);
Michael Chan71034ba2009-10-10 13:46:59 +00005026 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005027 CSTORM_ISCSI_EQ_CONS_OFFSET(pfid, 0), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00005028 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005029 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00005030 cp->kcq1.dma.pg_map_arr[1] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00005031 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005032 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00005033 (u64) cp->kcq1.dma.pg_map_arr[1] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00005034 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005035 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00005036 cp->kcq1.dma.pg_map_arr[0] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00005037 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005038 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00005039 (u64) cp->kcq1.dma.pg_map_arr[0] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00005040 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005041 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_VALID_OFFSET(pfid, 0), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00005042 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005043 CSTORM_ISCSI_EQ_SB_NUM_OFFSET(pfid, 0), cp->status_blk_num);
Michael Chan71034ba2009-10-10 13:46:59 +00005044 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005045 CSTORM_ISCSI_EQ_SB_INDEX_OFFSET(pfid, 0),
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005046 HC_INDEX_ISCSI_EQ_CONS);
Michael Chan71034ba2009-10-10 13:46:59 +00005047
Michael Chan71034ba2009-10-10 13:46:59 +00005048 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005049 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00005050 cp->gbl_buf_info.pg_map_arr[0] & 0xffffffff);
5051 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005052 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00005053 (u64) cp->gbl_buf_info.pg_map_arr[0] >> 32);
5054
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005055 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
5056 TSTORM_ISCSI_TCP_LOCAL_ADV_WND_OFFSET(pfid), DEF_RCV_BUF);
5057
Michael Chan71034ba2009-10-10 13:46:59 +00005058 cnic_setup_bnx2x_context(dev);
5059
Michael Chan71034ba2009-10-10 13:46:59 +00005060 ret = cnic_init_bnx2x_irq(dev);
5061 if (ret)
5062 return ret;
5063
Michael Chan71034ba2009-10-10 13:46:59 +00005064 return 0;
5065}
5066
Michael Chan86b53602009-10-10 13:46:57 +00005067static void cnic_init_rings(struct cnic_dev *dev)
5068{
Michael Chan541a78102010-10-06 03:17:22 +00005069 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00005070 struct cnic_uio_dev *udev = cp->udev;
Michael Chan541a78102010-10-06 03:17:22 +00005071
5072 if (test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
5073 return;
5074
Michael Chan86b53602009-10-10 13:46:57 +00005075 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
5076 cnic_init_bnx2_tx_ring(dev);
5077 cnic_init_bnx2_rx_ring(dev);
Michael Chan541a78102010-10-06 03:17:22 +00005078 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00005079 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
Michael Chan5159fdc2010-12-23 07:42:59 +00005080 u32 cli = cp->ethdev->iscsi_l2_client_id;
5081 u32 cid = cp->ethdev->iscsi_l2_cid;
Michael Chan68d7c1a2011-01-05 15:14:13 +00005082 u32 cl_qzone_id;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005083 struct client_init_ramrod_data *data;
Michael Chan71034ba2009-10-10 13:46:59 +00005084 union l5cm_specific_data l5_data;
5085 struct ustorm_eth_rx_producers rx_prods = {0};
Michael Chane1dd8832011-07-13 17:24:19 +00005086 u32 off, i, *cid_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00005087
5088 rx_prods.bd_prod = 0;
5089 rx_prods.cqe_prod = BNX2X_MAX_RCQ_DESC_CNT;
5090 barrier();
5091
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005092 cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
5093
Michael Chanc7596b72009-12-02 15:15:35 +00005094 off = BAR_USTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005095 (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id) ?
Michael Chanee87a822010-10-13 14:06:51 +00005096 USTORM_RX_PRODS_E2_OFFSET(cl_qzone_id) :
5097 USTORM_RX_PRODS_E1X_OFFSET(CNIC_PORT(cp), cli));
Michael Chan71034ba2009-10-10 13:46:59 +00005098
5099 for (i = 0; i < sizeof(struct ustorm_eth_rx_producers) / 4; i++)
Michael Chanc7596b72009-12-02 15:15:35 +00005100 CNIC_WR(dev, off + i * 4, ((u32 *) &rx_prods)[i]);
Michael Chan71034ba2009-10-10 13:46:59 +00005101
Michael Chan48f753d2010-05-18 11:32:53 +00005102 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
5103
Michael Chancd801532010-10-13 14:06:49 +00005104 data = udev->l2_buf;
Michael Chane1dd8832011-07-13 17:24:19 +00005105 cid_ptr = udev->l2_buf + 12;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005106
5107 memset(data, 0, sizeof(*data));
5108
5109 cnic_init_bnx2x_tx_ring(dev, data);
5110 cnic_init_bnx2x_rx_ring(dev, data);
5111
Michael Chancd801532010-10-13 14:06:49 +00005112 l5_data.phy_address.lo = udev->l2_buf_map & 0xffffffff;
5113 l5_data.phy_address.hi = (u64) udev->l2_buf_map >> 32;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005114
Michael Chan541a78102010-10-06 03:17:22 +00005115 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
5116
Michael Chan71034ba2009-10-10 13:46:59 +00005117 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_CLIENT_SETUP,
Michael Chan68d7c1a2011-01-05 15:14:13 +00005118 cid, ETH_CONNECTION_TYPE, &l5_data);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005119
Michael Chan48f753d2010-05-18 11:32:53 +00005120 i = 0;
5121 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
5122 ++i < 10)
5123 msleep(1);
5124
5125 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
5126 netdev_err(dev->netdev,
5127 "iSCSI CLIENT_SETUP did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00005128 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan5159fdc2010-12-23 07:42:59 +00005129 cnic_ring_ctl(dev, cid, cli, 1);
Michael Chane1dd8832011-07-13 17:24:19 +00005130 *cid_ptr = cid;
Michael Chan86b53602009-10-10 13:46:57 +00005131 }
5132}
5133
5134static void cnic_shutdown_rings(struct cnic_dev *dev)
5135{
Michael Chan541a78102010-10-06 03:17:22 +00005136 struct cnic_local *cp = dev->cnic_priv;
Michael Chane1dd8832011-07-13 17:24:19 +00005137 struct cnic_uio_dev *udev = cp->udev;
5138 void *rx_ring;
Michael Chan541a78102010-10-06 03:17:22 +00005139
5140 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
5141 return;
5142
Michael Chan86b53602009-10-10 13:46:57 +00005143 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
5144 cnic_shutdown_bnx2_rx_ring(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005145 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
Michael Chan5159fdc2010-12-23 07:42:59 +00005146 u32 cli = cp->ethdev->iscsi_l2_client_id;
5147 u32 cid = cp->ethdev->iscsi_l2_cid;
Michael Chan8b065b62009-12-02 15:15:36 +00005148 union l5cm_specific_data l5_data;
Michael Chan48f753d2010-05-18 11:32:53 +00005149 int i;
Michael Chan71034ba2009-10-10 13:46:59 +00005150
Michael Chan5159fdc2010-12-23 07:42:59 +00005151 cnic_ring_ctl(dev, cid, cli, 0);
Michael Chan8b065b62009-12-02 15:15:36 +00005152
Michael Chan48f753d2010-05-18 11:32:53 +00005153 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
5154
Michael Chan8b065b62009-12-02 15:15:36 +00005155 l5_data.phy_address.lo = cli;
5156 l5_data.phy_address.hi = 0;
5157 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_HALT,
Michael Chan5159fdc2010-12-23 07:42:59 +00005158 cid, ETH_CONNECTION_TYPE, &l5_data);
Michael Chan48f753d2010-05-18 11:32:53 +00005159 i = 0;
5160 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
5161 ++i < 10)
5162 msleep(1);
5163
5164 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
5165 netdev_err(dev->netdev,
5166 "iSCSI CLIENT_HALT did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00005167 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan1bcdc322009-12-10 15:40:57 +00005168
5169 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005170 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
Michael Chan68d7c1a2011-01-05 15:14:13 +00005171 cid, NONE_CONNECTION_TYPE, &l5_data);
Michael Chan1bcdc322009-12-10 15:40:57 +00005172 msleep(10);
Michael Chan86b53602009-10-10 13:46:57 +00005173 }
Michael Chan541a78102010-10-06 03:17:22 +00005174 clear_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chane1dd8832011-07-13 17:24:19 +00005175 rx_ring = udev->l2_ring + BCM_PAGE_SIZE;
5176 memset(rx_ring, 0, BCM_PAGE_SIZE);
Michael Chan86b53602009-10-10 13:46:57 +00005177}
5178
Michael Chana3059b12009-08-14 15:49:44 +00005179static int cnic_register_netdev(struct cnic_dev *dev)
5180{
5181 struct cnic_local *cp = dev->cnic_priv;
5182 struct cnic_eth_dev *ethdev = cp->ethdev;
5183 int err;
5184
5185 if (!ethdev)
5186 return -ENODEV;
5187
5188 if (ethdev->drv_state & CNIC_DRV_STATE_REGD)
5189 return 0;
5190
5191 err = ethdev->drv_register_cnic(dev->netdev, cp->cnic_ops, dev);
5192 if (err)
Joe Perchesddf79b22010-02-17 15:01:54 +00005193 netdev_err(dev->netdev, "register_cnic failed\n");
Michael Chana3059b12009-08-14 15:49:44 +00005194
5195 return err;
5196}
5197
5198static void cnic_unregister_netdev(struct cnic_dev *dev)
5199{
5200 struct cnic_local *cp = dev->cnic_priv;
5201 struct cnic_eth_dev *ethdev = cp->ethdev;
5202
5203 if (!ethdev)
5204 return;
5205
5206 ethdev->drv_unregister_cnic(dev->netdev);
5207}
5208
Michael Chana4636962009-06-08 18:14:43 -07005209static int cnic_start_hw(struct cnic_dev *dev)
5210{
5211 struct cnic_local *cp = dev->cnic_priv;
5212 struct cnic_eth_dev *ethdev = cp->ethdev;
5213 int err;
5214
5215 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
5216 return -EALREADY;
5217
Michael Chana4636962009-06-08 18:14:43 -07005218 dev->regview = ethdev->io_base;
Michael Chana4636962009-06-08 18:14:43 -07005219 pci_dev_get(dev->pcidev);
5220 cp->func = PCI_FUNC(dev->pcidev->devfn);
Michael Chana4dde3a2010-02-24 14:42:08 +00005221 cp->status_blk.gen = ethdev->irq_arr[0].status_blk;
Michael Chana4636962009-06-08 18:14:43 -07005222 cp->status_blk_num = ethdev->irq_arr[0].status_blk_num;
5223
5224 err = cp->alloc_resc(dev);
5225 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00005226 netdev_err(dev->netdev, "allocate resource failure\n");
Michael Chana4636962009-06-08 18:14:43 -07005227 goto err1;
5228 }
5229
5230 err = cp->start_hw(dev);
5231 if (err)
5232 goto err1;
5233
5234 err = cnic_cm_open(dev);
5235 if (err)
5236 goto err1;
5237
5238 set_bit(CNIC_F_CNIC_UP, &dev->flags);
5239
5240 cp->enable_int(dev);
5241
5242 return 0;
5243
5244err1:
Michael Chana4636962009-06-08 18:14:43 -07005245 cp->free_resc(dev);
5246 pci_dev_put(dev->pcidev);
Michael Chana4636962009-06-08 18:14:43 -07005247 return err;
5248}
5249
5250static void cnic_stop_bnx2_hw(struct cnic_dev *dev)
5251{
Michael Chana4636962009-06-08 18:14:43 -07005252 cnic_disable_bnx2_int_sync(dev);
5253
5254 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
5255 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
5256
5257 cnic_init_context(dev, KWQ_CID);
5258 cnic_init_context(dev, KCQ_CID);
5259
5260 cnic_setup_5709_context(dev, 0);
5261 cnic_free_irq(dev);
5262
Michael Chana4636962009-06-08 18:14:43 -07005263 cnic_free_resc(dev);
5264}
5265
Michael Chan71034ba2009-10-10 13:46:59 +00005266
5267static void cnic_stop_bnx2x_hw(struct cnic_dev *dev)
5268{
5269 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00005270
5271 cnic_free_irq(dev);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005272 *cp->kcq1.hw_prod_idx_ptr = 0;
Michael Chan4e9c4fd2009-12-10 15:40:58 +00005273 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005274 CSTORM_ISCSI_EQ_CONS_OFFSET(cp->pfid, 0), 0);
Michael Chane6c28892010-06-24 14:58:39 +00005275 CNIC_WR16(dev, cp->kcq1.io_addr, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00005276 cnic_free_resc(dev);
5277}
5278
Michael Chana4636962009-06-08 18:14:43 -07005279static void cnic_stop_hw(struct cnic_dev *dev)
5280{
5281 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
5282 struct cnic_local *cp = dev->cnic_priv;
Michael Chan48f753d2010-05-18 11:32:53 +00005283 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -07005284
Michael Chan48f753d2010-05-18 11:32:53 +00005285 /* Need to wait for the ring shutdown event to complete
5286 * before clearing the CNIC_UP flag.
5287 */
Michael Chancd801532010-10-13 14:06:49 +00005288 while (cp->udev->uio_dev != -1 && i < 15) {
Michael Chan48f753d2010-05-18 11:32:53 +00005289 msleep(100);
5290 i++;
5291 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005292 cnic_shutdown_rings(dev);
Michael Chana4636962009-06-08 18:14:43 -07005293 clear_bit(CNIC_F_CNIC_UP, &dev->flags);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +00005294 RCU_INIT_POINTER(cp->ulp_ops[CNIC_ULP_L4], NULL);
Michael Chana4636962009-06-08 18:14:43 -07005295 synchronize_rcu();
5296 cnic_cm_shutdown(dev);
5297 cp->stop_hw(dev);
5298 pci_dev_put(dev->pcidev);
5299 }
5300}
5301
5302static void cnic_free_dev(struct cnic_dev *dev)
5303{
5304 int i = 0;
5305
5306 while ((atomic_read(&dev->ref_count) != 0) && i < 10) {
5307 msleep(100);
5308 i++;
5309 }
5310 if (atomic_read(&dev->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +00005311 netdev_err(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -07005312
Joe Perchesddf79b22010-02-17 15:01:54 +00005313 netdev_info(dev->netdev, "Removed CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07005314 dev_put(dev->netdev);
5315 kfree(dev);
5316}
5317
5318static struct cnic_dev *cnic_alloc_dev(struct net_device *dev,
5319 struct pci_dev *pdev)
5320{
5321 struct cnic_dev *cdev;
5322 struct cnic_local *cp;
5323 int alloc_size;
5324
5325 alloc_size = sizeof(struct cnic_dev) + sizeof(struct cnic_local);
5326
5327 cdev = kzalloc(alloc_size , GFP_KERNEL);
5328 if (cdev == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +00005329 netdev_err(dev, "allocate dev struct failure\n");
Michael Chana4636962009-06-08 18:14:43 -07005330 return NULL;
5331 }
5332
5333 cdev->netdev = dev;
5334 cdev->cnic_priv = (char *)cdev + sizeof(struct cnic_dev);
5335 cdev->register_device = cnic_register_device;
5336 cdev->unregister_device = cnic_unregister_device;
5337 cdev->iscsi_nl_msg_recv = cnic_iscsi_nl_msg_recv;
5338
5339 cp = cdev->cnic_priv;
5340 cp->dev = cdev;
Michael Chana4636962009-06-08 18:14:43 -07005341 cp->l2_single_buf_size = 0x400;
5342 cp->l2_rx_ring_size = 3;
5343
5344 spin_lock_init(&cp->cnic_ulp_lock);
5345
Joe Perchesddf79b22010-02-17 15:01:54 +00005346 netdev_info(dev, "Added CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07005347
5348 return cdev;
5349}
5350
5351static struct cnic_dev *init_bnx2_cnic(struct net_device *dev)
5352{
5353 struct pci_dev *pdev;
5354 struct cnic_dev *cdev;
5355 struct cnic_local *cp;
5356 struct cnic_eth_dev *ethdev = NULL;
Michael Chane2ee3612009-06-13 17:43:02 -07005357 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
Michael Chana4636962009-06-08 18:14:43 -07005358
Michael Chane2ee3612009-06-13 17:43:02 -07005359 probe = symbol_get(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07005360 if (probe) {
5361 ethdev = (*probe)(dev);
Michael Chan64c64602009-08-14 15:49:43 +00005362 symbol_put(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07005363 }
5364 if (!ethdev)
5365 return NULL;
5366
5367 pdev = ethdev->pdev;
5368 if (!pdev)
5369 return NULL;
5370
5371 dev_hold(dev);
5372 pci_dev_get(pdev);
Sergei Shtylyovff938e42011-02-28 11:57:33 -08005373 if ((pdev->device == PCI_DEVICE_ID_NX2_5709 ||
5374 pdev->device == PCI_DEVICE_ID_NX2_5709S) &&
5375 (pdev->revision < 0x10)) {
5376 pci_dev_put(pdev);
5377 goto cnic_err;
Michael Chana4636962009-06-08 18:14:43 -07005378 }
5379 pci_dev_put(pdev);
5380
5381 cdev = cnic_alloc_dev(dev, pdev);
5382 if (cdev == NULL)
5383 goto cnic_err;
5384
5385 set_bit(CNIC_F_BNX2_CLASS, &cdev->flags);
5386 cdev->submit_kwqes = cnic_submit_bnx2_kwqes;
5387
5388 cp = cdev->cnic_priv;
5389 cp->ethdev = ethdev;
5390 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00005391 cp->chip_id = ethdev->chip_id;
Michael Chana4636962009-06-08 18:14:43 -07005392
Michael Chan7625eb22011-06-08 19:29:36 +00005393 cdev->max_iscsi_conn = ethdev->max_iscsi_conn;
5394
Michael Chana4636962009-06-08 18:14:43 -07005395 cp->cnic_ops = &cnic_bnx2_ops;
5396 cp->start_hw = cnic_start_bnx2_hw;
5397 cp->stop_hw = cnic_stop_bnx2_hw;
5398 cp->setup_pgtbl = cnic_setup_page_tbl;
5399 cp->alloc_resc = cnic_alloc_bnx2_resc;
5400 cp->free_resc = cnic_free_resc;
5401 cp->start_cm = cnic_cm_init_bnx2_hw;
5402 cp->stop_cm = cnic_cm_stop_bnx2_hw;
5403 cp->enable_int = cnic_enable_bnx2_int;
5404 cp->disable_int_sync = cnic_disable_bnx2_int_sync;
5405 cp->close_conn = cnic_close_bnx2_conn;
Michael Chana4636962009-06-08 18:14:43 -07005406 return cdev;
5407
5408cnic_err:
5409 dev_put(dev);
5410 return NULL;
5411}
5412
Michael Chan71034ba2009-10-10 13:46:59 +00005413static struct cnic_dev *init_bnx2x_cnic(struct net_device *dev)
5414{
5415 struct pci_dev *pdev;
5416 struct cnic_dev *cdev;
5417 struct cnic_local *cp;
5418 struct cnic_eth_dev *ethdev = NULL;
5419 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
5420
5421 probe = symbol_get(bnx2x_cnic_probe);
5422 if (probe) {
5423 ethdev = (*probe)(dev);
5424 symbol_put(bnx2x_cnic_probe);
5425 }
5426 if (!ethdev)
5427 return NULL;
5428
5429 pdev = ethdev->pdev;
5430 if (!pdev)
5431 return NULL;
5432
5433 dev_hold(dev);
5434 cdev = cnic_alloc_dev(dev, pdev);
5435 if (cdev == NULL) {
5436 dev_put(dev);
5437 return NULL;
5438 }
5439
5440 set_bit(CNIC_F_BNX2X_CLASS, &cdev->flags);
5441 cdev->submit_kwqes = cnic_submit_bnx2x_kwqes;
5442
5443 cp = cdev->cnic_priv;
5444 cp->ethdev = ethdev;
5445 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00005446 cp->chip_id = ethdev->chip_id;
Michael Chan71034ba2009-10-10 13:46:59 +00005447
Barak Witkowski1d187b32011-12-05 22:41:50 +00005448 cdev->stats_addr = ethdev->addr_drv_info_to_mcp;
5449
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00005450 if (!(ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI))
5451 cdev->max_iscsi_conn = ethdev->max_iscsi_conn;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005452 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id) &&
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00005453 !(ethdev->drv_state & CNIC_DRV_STATE_NO_FCOE))
5454 cdev->max_fcoe_conn = ethdev->max_fcoe_conn;
5455
Michael Chandc219a22011-08-26 09:45:39 +00005456 if (cdev->max_fcoe_conn > BNX2X_FCOE_NUM_CONNECTIONS)
5457 cdev->max_fcoe_conn = BNX2X_FCOE_NUM_CONNECTIONS;
5458
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00005459 memcpy(cdev->mac_addr, ethdev->iscsi_mac, 6);
5460
Michael Chan71034ba2009-10-10 13:46:59 +00005461 cp->cnic_ops = &cnic_bnx2x_ops;
5462 cp->start_hw = cnic_start_bnx2x_hw;
5463 cp->stop_hw = cnic_stop_bnx2x_hw;
5464 cp->setup_pgtbl = cnic_setup_page_tbl_le;
5465 cp->alloc_resc = cnic_alloc_bnx2x_resc;
5466 cp->free_resc = cnic_free_resc;
5467 cp->start_cm = cnic_cm_init_bnx2x_hw;
5468 cp->stop_cm = cnic_cm_stop_bnx2x_hw;
5469 cp->enable_int = cnic_enable_bnx2x_int;
5470 cp->disable_int_sync = cnic_disable_bnx2x_int_sync;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005471 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id))
Michael Chanee87a822010-10-13 14:06:51 +00005472 cp->ack_int = cnic_ack_bnx2x_e2_msix;
5473 else
5474 cp->ack_int = cnic_ack_bnx2x_msix;
Michael Chan71034ba2009-10-10 13:46:59 +00005475 cp->close_conn = cnic_close_bnx2x_conn;
Michael Chan71034ba2009-10-10 13:46:59 +00005476 return cdev;
5477}
5478
Michael Chana4636962009-06-08 18:14:43 -07005479static struct cnic_dev *is_cnic_dev(struct net_device *dev)
5480{
5481 struct ethtool_drvinfo drvinfo;
5482 struct cnic_dev *cdev = NULL;
5483
5484 if (dev->ethtool_ops && dev->ethtool_ops->get_drvinfo) {
5485 memset(&drvinfo, 0, sizeof(drvinfo));
5486 dev->ethtool_ops->get_drvinfo(dev, &drvinfo);
5487
5488 if (!strcmp(drvinfo.driver, "bnx2"))
5489 cdev = init_bnx2_cnic(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005490 if (!strcmp(drvinfo.driver, "bnx2x"))
5491 cdev = init_bnx2x_cnic(dev);
Michael Chana4636962009-06-08 18:14:43 -07005492 if (cdev) {
5493 write_lock(&cnic_dev_lock);
5494 list_add(&cdev->list, &cnic_dev_list);
5495 write_unlock(&cnic_dev_lock);
5496 }
5497 }
5498 return cdev;
5499}
5500
Michael Chan415199f2011-07-20 14:55:24 +00005501static void cnic_rcv_netevent(struct cnic_local *cp, unsigned long event,
5502 u16 vlan_id)
5503{
5504 int if_type;
5505
5506 rcu_read_lock();
5507 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
5508 struct cnic_ulp_ops *ulp_ops;
5509 void *ctx;
5510
5511 ulp_ops = rcu_dereference(cp->ulp_ops[if_type]);
5512 if (!ulp_ops || !ulp_ops->indicate_netevent)
5513 continue;
5514
5515 ctx = cp->ulp_handle[if_type];
5516
5517 ulp_ops->indicate_netevent(ctx, event, vlan_id);
5518 }
5519 rcu_read_unlock();
5520}
5521
Michael Chana4636962009-06-08 18:14:43 -07005522/**
5523 * netdev event handler
5524 */
5525static int cnic_netdev_event(struct notifier_block *this, unsigned long event,
5526 void *ptr)
5527{
5528 struct net_device *netdev = ptr;
5529 struct cnic_dev *dev;
Michael Chana4636962009-06-08 18:14:43 -07005530 int new_dev = 0;
5531
5532 dev = cnic_from_netdev(netdev);
5533
Michael Chandb1d3502011-06-08 19:29:35 +00005534 if (!dev && (event == NETDEV_REGISTER || netif_running(netdev))) {
Michael Chana4636962009-06-08 18:14:43 -07005535 /* Check for the hot-plug device */
5536 dev = is_cnic_dev(netdev);
5537 if (dev) {
5538 new_dev = 1;
5539 cnic_hold(dev);
5540 }
5541 }
5542 if (dev) {
5543 struct cnic_local *cp = dev->cnic_priv;
5544
5545 if (new_dev)
5546 cnic_ulp_init(dev);
5547 else if (event == NETDEV_UNREGISTER)
5548 cnic_ulp_exit(dev);
Michael Chan6053bbf2009-10-02 11:03:28 -07005549
Michael Chandb1d3502011-06-08 19:29:35 +00005550 if (event == NETDEV_UP || (new_dev && netif_running(netdev))) {
Michael Chana3059b12009-08-14 15:49:44 +00005551 if (cnic_register_netdev(dev) != 0) {
5552 cnic_put(dev);
5553 goto done;
5554 }
Michael Chana4636962009-06-08 18:14:43 -07005555 if (!cnic_start_hw(dev))
5556 cnic_ulp_start(dev);
Michael Chana4636962009-06-08 18:14:43 -07005557 }
5558
Michael Chan415199f2011-07-20 14:55:24 +00005559 cnic_rcv_netevent(cp, event, 0);
Michael Chana4636962009-06-08 18:14:43 -07005560
5561 if (event == NETDEV_GOING_DOWN) {
Michael Chana4636962009-06-08 18:14:43 -07005562 cnic_ulp_stop(dev);
5563 cnic_stop_hw(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005564 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005565 } else if (event == NETDEV_UNREGISTER) {
5566 write_lock(&cnic_dev_lock);
5567 list_del_init(&dev->list);
5568 write_unlock(&cnic_dev_lock);
5569
5570 cnic_put(dev);
5571 cnic_free_dev(dev);
5572 goto done;
5573 }
5574 cnic_put(dev);
Michael Chan415199f2011-07-20 14:55:24 +00005575 } else {
5576 struct net_device *realdev;
5577 u16 vid;
5578
5579 vid = cnic_get_vlan(netdev, &realdev);
5580 if (realdev) {
5581 dev = cnic_from_netdev(realdev);
5582 if (dev) {
5583 vid |= VLAN_TAG_PRESENT;
5584 cnic_rcv_netevent(dev->cnic_priv, event, vid);
5585 cnic_put(dev);
5586 }
5587 }
Michael Chana4636962009-06-08 18:14:43 -07005588 }
5589done:
5590 return NOTIFY_DONE;
5591}
5592
5593static struct notifier_block cnic_netdev_notifier = {
5594 .notifier_call = cnic_netdev_event
5595};
5596
5597static void cnic_release(void)
5598{
5599 struct cnic_dev *dev;
Michael Chana3ceeeb2010-10-13 14:06:50 +00005600 struct cnic_uio_dev *udev;
Michael Chana4636962009-06-08 18:14:43 -07005601
5602 while (!list_empty(&cnic_dev_list)) {
5603 dev = list_entry(cnic_dev_list.next, struct cnic_dev, list);
5604 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
5605 cnic_ulp_stop(dev);
5606 cnic_stop_hw(dev);
5607 }
5608
5609 cnic_ulp_exit(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005610 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005611 list_del_init(&dev->list);
5612 cnic_free_dev(dev);
5613 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005614 while (!list_empty(&cnic_udev_list)) {
5615 udev = list_entry(cnic_udev_list.next, struct cnic_uio_dev,
5616 list);
5617 cnic_free_uio(udev);
5618 }
Michael Chana4636962009-06-08 18:14:43 -07005619}
5620
5621static int __init cnic_init(void)
5622{
5623 int rc = 0;
5624
Joe Perchesddf79b22010-02-17 15:01:54 +00005625 pr_info("%s", version);
Michael Chana4636962009-06-08 18:14:43 -07005626
5627 rc = register_netdevice_notifier(&cnic_netdev_notifier);
5628 if (rc) {
5629 cnic_release();
5630 return rc;
5631 }
5632
Michael Chanfdf24082010-10-13 14:06:47 +00005633 cnic_wq = create_singlethread_workqueue("cnic_wq");
5634 if (!cnic_wq) {
5635 cnic_release();
5636 unregister_netdevice_notifier(&cnic_netdev_notifier);
5637 return -ENOMEM;
5638 }
5639
Michael Chana4636962009-06-08 18:14:43 -07005640 return 0;
5641}
5642
5643static void __exit cnic_exit(void)
5644{
5645 unregister_netdevice_notifier(&cnic_netdev_notifier);
5646 cnic_release();
Michael Chanfdf24082010-10-13 14:06:47 +00005647 destroy_workqueue(cnic_wq);
Michael Chana4636962009-06-08 18:14:43 -07005648}
5649
5650module_init(cnic_init);
5651module_exit(cnic_exit);