blob: 3c95065e0def819c98b6b6507ae9683cbd72a857 [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;
259
260 if (reg)
261 info.cmd = DRV_CTL_ULP_REGISTER_CMD;
262 else
263 info.cmd = DRV_CTL_ULP_UNREGISTER_CMD;
264
265 info.data.ulp_type = ulp_type;
266 ethdev->drv_ctl(dev->netdev, &info);
267}
268
Michael Chana4636962009-06-08 18:14:43 -0700269static int cnic_in_use(struct cnic_sock *csk)
270{
271 return test_bit(SK_F_INUSE, &csk->flags);
272}
273
Dmitry Kravkovc2bff632010-10-06 03:33:18 +0000274static void cnic_spq_completion(struct cnic_dev *dev, int cmd, u32 count)
Michael Chana4636962009-06-08 18:14:43 -0700275{
276 struct cnic_local *cp = dev->cnic_priv;
277 struct cnic_eth_dev *ethdev = cp->ethdev;
278 struct drv_ctl_info info;
279
Dmitry Kravkovc2bff632010-10-06 03:33:18 +0000280 info.cmd = cmd;
281 info.data.credit.credit_count = count;
Michael Chana4636962009-06-08 18:14:43 -0700282 ethdev->drv_ctl(dev->netdev, &info);
283}
284
Michael Chan71034ba2009-10-10 13:46:59 +0000285static int cnic_get_l5_cid(struct cnic_local *cp, u32 cid, u32 *l5_cid)
286{
287 u32 i;
288
Michael Chan520efdf2010-06-24 14:58:37 +0000289 for (i = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +0000290 if (cp->ctx_tbl[i].cid == cid) {
291 *l5_cid = i;
292 return 0;
293 }
294 }
295 return -EINVAL;
296}
297
Michael Chana4636962009-06-08 18:14:43 -0700298static int cnic_send_nlmsg(struct cnic_local *cp, u32 type,
299 struct cnic_sock *csk)
300{
301 struct iscsi_path path_req;
302 char *buf = NULL;
303 u16 len = 0;
304 u32 msg_type = ISCSI_KEVENT_IF_DOWN;
305 struct cnic_ulp_ops *ulp_ops;
Michael Chancd801532010-10-13 14:06:49 +0000306 struct cnic_uio_dev *udev = cp->udev;
Michael Chan939b82e2010-12-23 07:42:58 +0000307 int rc = 0, retry = 0;
Michael Chana4636962009-06-08 18:14:43 -0700308
Michael Chancd801532010-10-13 14:06:49 +0000309 if (!udev || udev->uio_dev == -1)
Michael Chana4636962009-06-08 18:14:43 -0700310 return -ENODEV;
311
312 if (csk) {
313 len = sizeof(path_req);
314 buf = (char *) &path_req;
315 memset(&path_req, 0, len);
316
317 msg_type = ISCSI_KEVENT_PATH_REQ;
318 path_req.handle = (u64) csk->l5_cid;
319 if (test_bit(SK_F_IPV6, &csk->flags)) {
320 memcpy(&path_req.dst.v6_addr, &csk->dst_ip[0],
321 sizeof(struct in6_addr));
322 path_req.ip_addr_len = 16;
323 } else {
324 memcpy(&path_req.dst.v4_addr, &csk->dst_ip[0],
325 sizeof(struct in_addr));
326 path_req.ip_addr_len = 4;
327 }
328 path_req.vlan_id = csk->vlan_id;
329 path_req.pmtu = csk->mtu;
330 }
331
Michael Chan939b82e2010-12-23 07:42:58 +0000332 while (retry < 3) {
333 rc = 0;
334 rcu_read_lock();
335 ulp_ops = rcu_dereference(cnic_ulp_tbl[CNIC_ULP_ISCSI]);
336 if (ulp_ops)
337 rc = ulp_ops->iscsi_nl_send_msg(
338 cp->ulp_handle[CNIC_ULP_ISCSI],
339 msg_type, buf, len);
340 rcu_read_unlock();
341 if (rc == 0 || msg_type != ISCSI_KEVENT_PATH_REQ)
342 break;
343
344 msleep(100);
345 retry++;
346 }
Michael Chan558e4c72011-07-13 17:24:20 +0000347 return rc;
Michael Chana4636962009-06-08 18:14:43 -0700348}
349
Eddie Wai42ecbb82010-12-23 07:43:02 +0000350static void cnic_cm_upcall(struct cnic_local *, struct cnic_sock *, u8);
351
Michael Chana4636962009-06-08 18:14:43 -0700352static int cnic_iscsi_nl_msg_recv(struct cnic_dev *dev, u32 msg_type,
353 char *buf, u16 len)
354{
355 int rc = -EINVAL;
356
357 switch (msg_type) {
358 case ISCSI_UEVENT_PATH_UPDATE: {
359 struct cnic_local *cp;
360 u32 l5_cid;
361 struct cnic_sock *csk;
362 struct iscsi_path *path_resp;
363
364 if (len < sizeof(*path_resp))
365 break;
366
367 path_resp = (struct iscsi_path *) buf;
368 cp = dev->cnic_priv;
369 l5_cid = (u32) path_resp->handle;
370 if (l5_cid >= MAX_CM_SK_TBL_SZ)
371 break;
372
Michael Chand02a5e62010-02-24 14:42:06 +0000373 rcu_read_lock();
374 if (!rcu_dereference(cp->ulp_ops[CNIC_ULP_L4])) {
375 rc = -ENODEV;
376 rcu_read_unlock();
377 break;
378 }
Michael Chana4636962009-06-08 18:14:43 -0700379 csk = &cp->csk_tbl[l5_cid];
380 csk_hold(csk);
Eddie Wai42ecbb82010-12-23 07:43:02 +0000381 if (cnic_in_use(csk) &&
382 test_bit(SK_F_CONNECT_START, &csk->flags)) {
383
Eddie Wai4cbbb042012-02-08 17:33:57 +0000384 csk->vlan_id = path_resp->vlan_id;
385
Michael Chana4636962009-06-08 18:14:43 -0700386 memcpy(csk->ha, path_resp->mac_addr, 6);
387 if (test_bit(SK_F_IPV6, &csk->flags))
388 memcpy(&csk->src_ip[0], &path_resp->src.v6_addr,
389 sizeof(struct in6_addr));
390 else
391 memcpy(&csk->src_ip[0], &path_resp->src.v4_addr,
392 sizeof(struct in_addr));
Eddie Wai42ecbb82010-12-23 07:43:02 +0000393
394 if (is_valid_ether_addr(csk->ha)) {
Michael Chana4636962009-06-08 18:14:43 -0700395 cnic_cm_set_pg(csk);
Eddie Wai42ecbb82010-12-23 07:43:02 +0000396 } else if (!test_bit(SK_F_OFFLD_SCHED, &csk->flags) &&
397 !test_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
398
399 cnic_cm_upcall(cp, csk,
400 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
401 clear_bit(SK_F_CONNECT_START, &csk->flags);
402 }
Michael Chana4636962009-06-08 18:14:43 -0700403 }
404 csk_put(csk);
Michael Chand02a5e62010-02-24 14:42:06 +0000405 rcu_read_unlock();
Michael Chana4636962009-06-08 18:14:43 -0700406 rc = 0;
407 }
408 }
409
410 return rc;
411}
412
413static int cnic_offld_prep(struct cnic_sock *csk)
414{
415 if (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
416 return 0;
417
418 if (!test_bit(SK_F_CONNECT_START, &csk->flags)) {
419 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
420 return 0;
421 }
422
423 return 1;
424}
425
426static int cnic_close_prep(struct cnic_sock *csk)
427{
428 clear_bit(SK_F_CONNECT_START, &csk->flags);
429 smp_mb__after_clear_bit();
430
431 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
432 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
433 msleep(1);
434
435 return 1;
436 }
437 return 0;
438}
439
440static int cnic_abort_prep(struct cnic_sock *csk)
441{
442 clear_bit(SK_F_CONNECT_START, &csk->flags);
443 smp_mb__after_clear_bit();
444
445 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
446 msleep(1);
447
448 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
449 csk->state = L4_KCQE_OPCODE_VALUE_RESET_COMP;
450 return 1;
451 }
452
453 return 0;
454}
455
456int cnic_register_driver(int ulp_type, struct cnic_ulp_ops *ulp_ops)
457{
458 struct cnic_dev *dev;
459
roel kluin0d37f362009-11-02 06:53:44 +0000460 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000461 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700462 return -EINVAL;
463 }
464 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000465 if (cnic_ulp_tbl_prot(ulp_type)) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000466 pr_err("%s: Type %d has already been registered\n",
467 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700468 mutex_unlock(&cnic_lock);
469 return -EBUSY;
470 }
471
472 read_lock(&cnic_dev_lock);
473 list_for_each_entry(dev, &cnic_dev_list, list) {
474 struct cnic_local *cp = dev->cnic_priv;
475
476 clear_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]);
477 }
478 read_unlock(&cnic_dev_lock);
479
Michael Chan7fc1ece2009-08-14 15:49:47 +0000480 atomic_set(&ulp_ops->ref_count, 0);
Michael Chana4636962009-06-08 18:14:43 -0700481 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], ulp_ops);
482 mutex_unlock(&cnic_lock);
483
484 /* Prevent race conditions with netdev_event */
485 rtnl_lock();
Michael Chana4636962009-06-08 18:14:43 -0700486 list_for_each_entry(dev, &cnic_dev_list, list) {
487 struct cnic_local *cp = dev->cnic_priv;
488
489 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]))
490 ulp_ops->cnic_init(dev);
491 }
Michael Chana4636962009-06-08 18:14:43 -0700492 rtnl_unlock();
493
494 return 0;
495}
496
497int cnic_unregister_driver(int ulp_type)
498{
499 struct cnic_dev *dev;
Michael Chan7fc1ece2009-08-14 15:49:47 +0000500 struct cnic_ulp_ops *ulp_ops;
501 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700502
roel kluin0d37f362009-11-02 06:53:44 +0000503 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000504 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700505 return -EINVAL;
506 }
507 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000508 ulp_ops = cnic_ulp_tbl_prot(ulp_type);
Michael Chan7fc1ece2009-08-14 15:49:47 +0000509 if (!ulp_ops) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000510 pr_err("%s: Type %d has not been registered\n",
511 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700512 goto out_unlock;
513 }
514 read_lock(&cnic_dev_lock);
515 list_for_each_entry(dev, &cnic_dev_list, list) {
516 struct cnic_local *cp = dev->cnic_priv;
517
518 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000519 pr_err("%s: Type %d still has devices registered\n",
520 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700521 read_unlock(&cnic_dev_lock);
522 goto out_unlock;
523 }
524 }
525 read_unlock(&cnic_dev_lock);
526
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000527 RCU_INIT_POINTER(cnic_ulp_tbl[ulp_type], NULL);
Michael Chana4636962009-06-08 18:14:43 -0700528
529 mutex_unlock(&cnic_lock);
530 synchronize_rcu();
Michael Chan7fc1ece2009-08-14 15:49:47 +0000531 while ((atomic_read(&ulp_ops->ref_count) != 0) && (i < 20)) {
532 msleep(100);
533 i++;
534 }
535
536 if (atomic_read(&ulp_ops->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +0000537 netdev_warn(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -0700538 return 0;
539
540out_unlock:
541 mutex_unlock(&cnic_lock);
542 return -EINVAL;
543}
544
545static int cnic_start_hw(struct cnic_dev *);
546static void cnic_stop_hw(struct cnic_dev *);
547
548static int cnic_register_device(struct cnic_dev *dev, int ulp_type,
549 void *ulp_ctx)
550{
551 struct cnic_local *cp = dev->cnic_priv;
552 struct cnic_ulp_ops *ulp_ops;
553
roel kluin0d37f362009-11-02 06:53:44 +0000554 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000555 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700556 return -EINVAL;
557 }
558 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000559 if (cnic_ulp_tbl_prot(ulp_type) == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000560 pr_err("%s: Driver with type %d has not been registered\n",
561 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700562 mutex_unlock(&cnic_lock);
563 return -EAGAIN;
564 }
565 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000566 pr_err("%s: Type %d has already been registered to this device\n",
567 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700568 mutex_unlock(&cnic_lock);
569 return -EBUSY;
570 }
571
572 clear_bit(ULP_F_START, &cp->ulp_flags[ulp_type]);
573 cp->ulp_handle[ulp_type] = ulp_ctx;
Eric Dumazet13707f92011-01-26 19:28:23 +0000574 ulp_ops = cnic_ulp_tbl_prot(ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700575 rcu_assign_pointer(cp->ulp_ops[ulp_type], ulp_ops);
576 cnic_hold(dev);
577
578 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
579 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[ulp_type]))
580 ulp_ops->cnic_start(cp->ulp_handle[ulp_type]);
581
582 mutex_unlock(&cnic_lock);
583
Barak Witkowski1d187b32011-12-05 22:41:50 +0000584 cnic_ulp_ctl(dev, ulp_type, true);
585
Michael Chana4636962009-06-08 18:14:43 -0700586 return 0;
587
588}
589EXPORT_SYMBOL(cnic_register_driver);
590
591static int cnic_unregister_device(struct cnic_dev *dev, int ulp_type)
592{
593 struct cnic_local *cp = dev->cnic_priv;
Michael Chan681dbd72009-08-14 15:49:46 +0000594 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700595
roel kluin0d37f362009-11-02 06:53:44 +0000596 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000597 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700598 return -EINVAL;
599 }
600 mutex_lock(&cnic_lock);
601 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000602 RCU_INIT_POINTER(cp->ulp_ops[ulp_type], NULL);
Michael Chana4636962009-06-08 18:14:43 -0700603 cnic_put(dev);
604 } else {
Joe Perchesddf79b22010-02-17 15:01:54 +0000605 pr_err("%s: device not registered to this ulp type %d\n",
606 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700607 mutex_unlock(&cnic_lock);
608 return -EINVAL;
609 }
610 mutex_unlock(&cnic_lock);
611
Michael Chan42bb8d52011-01-03 15:21:46 +0000612 if (ulp_type == CNIC_ULP_ISCSI)
613 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
614
Michael Chana4636962009-06-08 18:14:43 -0700615 synchronize_rcu();
616
Michael Chan681dbd72009-08-14 15:49:46 +0000617 while (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]) &&
618 i < 20) {
619 msleep(100);
620 i++;
621 }
622 if (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]))
Joe Perchesddf79b22010-02-17 15:01:54 +0000623 netdev_warn(dev->netdev, "Failed waiting for ULP up call to complete\n");
Michael Chan681dbd72009-08-14 15:49:46 +0000624
Barak Witkowski1d187b32011-12-05 22:41:50 +0000625 cnic_ulp_ctl(dev, ulp_type, false);
626
Michael Chana4636962009-06-08 18:14:43 -0700627 return 0;
628}
629EXPORT_SYMBOL(cnic_unregister_driver);
630
Eddie Wai11f23aa2011-06-08 19:29:34 +0000631static int cnic_init_id_tbl(struct cnic_id_tbl *id_tbl, u32 size, u32 start_id,
632 u32 next)
Michael Chana4636962009-06-08 18:14:43 -0700633{
634 id_tbl->start = start_id;
635 id_tbl->max = size;
Eddie Wai11f23aa2011-06-08 19:29:34 +0000636 id_tbl->next = next;
Michael Chana4636962009-06-08 18:14:43 -0700637 spin_lock_init(&id_tbl->lock);
638 id_tbl->table = kzalloc(DIV_ROUND_UP(size, 32) * 4, GFP_KERNEL);
639 if (!id_tbl->table)
640 return -ENOMEM;
641
642 return 0;
643}
644
645static void cnic_free_id_tbl(struct cnic_id_tbl *id_tbl)
646{
647 kfree(id_tbl->table);
648 id_tbl->table = NULL;
649}
650
651static int cnic_alloc_id(struct cnic_id_tbl *id_tbl, u32 id)
652{
653 int ret = -1;
654
655 id -= id_tbl->start;
656 if (id >= id_tbl->max)
657 return ret;
658
659 spin_lock(&id_tbl->lock);
660 if (!test_bit(id, id_tbl->table)) {
661 set_bit(id, id_tbl->table);
662 ret = 0;
663 }
664 spin_unlock(&id_tbl->lock);
665 return ret;
666}
667
668/* Returns -1 if not successful */
669static u32 cnic_alloc_new_id(struct cnic_id_tbl *id_tbl)
670{
671 u32 id;
672
673 spin_lock(&id_tbl->lock);
674 id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
675 if (id >= id_tbl->max) {
676 id = -1;
677 if (id_tbl->next != 0) {
678 id = find_first_zero_bit(id_tbl->table, id_tbl->next);
679 if (id >= id_tbl->next)
680 id = -1;
681 }
682 }
683
684 if (id < id_tbl->max) {
685 set_bit(id, id_tbl->table);
686 id_tbl->next = (id + 1) & (id_tbl->max - 1);
687 id += id_tbl->start;
688 }
689
690 spin_unlock(&id_tbl->lock);
691
692 return id;
693}
694
695static void cnic_free_id(struct cnic_id_tbl *id_tbl, u32 id)
696{
697 if (id == -1)
698 return;
699
700 id -= id_tbl->start;
701 if (id >= id_tbl->max)
702 return;
703
704 clear_bit(id, id_tbl->table);
705}
706
707static void cnic_free_dma(struct cnic_dev *dev, struct cnic_dma *dma)
708{
709 int i;
710
711 if (!dma->pg_arr)
712 return;
713
714 for (i = 0; i < dma->num_pages; i++) {
715 if (dma->pg_arr[i]) {
Michael Chan3248e162009-12-02 15:15:39 +0000716 dma_free_coherent(&dev->pcidev->dev, BCM_PAGE_SIZE,
717 dma->pg_arr[i], dma->pg_map_arr[i]);
Michael Chana4636962009-06-08 18:14:43 -0700718 dma->pg_arr[i] = NULL;
719 }
720 }
721 if (dma->pgtbl) {
Michael Chan3248e162009-12-02 15:15:39 +0000722 dma_free_coherent(&dev->pcidev->dev, dma->pgtbl_size,
723 dma->pgtbl, dma->pgtbl_map);
Michael Chana4636962009-06-08 18:14:43 -0700724 dma->pgtbl = NULL;
725 }
726 kfree(dma->pg_arr);
727 dma->pg_arr = NULL;
728 dma->num_pages = 0;
729}
730
731static void cnic_setup_page_tbl(struct cnic_dev *dev, struct cnic_dma *dma)
732{
733 int i;
Michael Chan51388262011-01-25 22:14:50 +0000734 __le32 *page_table = (__le32 *) dma->pgtbl;
Michael Chana4636962009-06-08 18:14:43 -0700735
736 for (i = 0; i < dma->num_pages; i++) {
737 /* Each entry needs to be in big endian format. */
Michael Chan51388262011-01-25 22:14:50 +0000738 *page_table = cpu_to_le32((u64) dma->pg_map_arr[i] >> 32);
Michael Chana4636962009-06-08 18:14:43 -0700739 page_table++;
Michael Chan51388262011-01-25 22:14:50 +0000740 *page_table = cpu_to_le32(dma->pg_map_arr[i] & 0xffffffff);
Michael Chana4636962009-06-08 18:14:43 -0700741 page_table++;
742 }
743}
744
Michael Chan71034ba2009-10-10 13:46:59 +0000745static void cnic_setup_page_tbl_le(struct cnic_dev *dev, struct cnic_dma *dma)
746{
747 int i;
Michael Chan51388262011-01-25 22:14:50 +0000748 __le32 *page_table = (__le32 *) dma->pgtbl;
Michael Chan71034ba2009-10-10 13:46:59 +0000749
750 for (i = 0; i < dma->num_pages; i++) {
751 /* Each entry needs to be in little endian format. */
Michael Chan51388262011-01-25 22:14:50 +0000752 *page_table = cpu_to_le32(dma->pg_map_arr[i] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +0000753 page_table++;
Michael Chan51388262011-01-25 22:14:50 +0000754 *page_table = cpu_to_le32((u64) dma->pg_map_arr[i] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +0000755 page_table++;
756 }
757}
758
Michael Chana4636962009-06-08 18:14:43 -0700759static int cnic_alloc_dma(struct cnic_dev *dev, struct cnic_dma *dma,
760 int pages, int use_pg_tbl)
761{
762 int i, size;
763 struct cnic_local *cp = dev->cnic_priv;
764
765 size = pages * (sizeof(void *) + sizeof(dma_addr_t));
766 dma->pg_arr = kzalloc(size, GFP_ATOMIC);
767 if (dma->pg_arr == NULL)
768 return -ENOMEM;
769
770 dma->pg_map_arr = (dma_addr_t *) (dma->pg_arr + pages);
771 dma->num_pages = pages;
772
773 for (i = 0; i < pages; i++) {
Michael Chan3248e162009-12-02 15:15:39 +0000774 dma->pg_arr[i] = dma_alloc_coherent(&dev->pcidev->dev,
775 BCM_PAGE_SIZE,
776 &dma->pg_map_arr[i],
777 GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700778 if (dma->pg_arr[i] == NULL)
779 goto error;
780 }
781 if (!use_pg_tbl)
782 return 0;
783
784 dma->pgtbl_size = ((pages * 8) + BCM_PAGE_SIZE - 1) &
785 ~(BCM_PAGE_SIZE - 1);
Michael Chan3248e162009-12-02 15:15:39 +0000786 dma->pgtbl = dma_alloc_coherent(&dev->pcidev->dev, dma->pgtbl_size,
787 &dma->pgtbl_map, GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700788 if (dma->pgtbl == NULL)
789 goto error;
790
791 cp->setup_pgtbl(dev, dma);
792
793 return 0;
794
795error:
796 cnic_free_dma(dev, dma);
797 return -ENOMEM;
798}
799
Michael Chan86b53602009-10-10 13:46:57 +0000800static void cnic_free_context(struct cnic_dev *dev)
801{
802 struct cnic_local *cp = dev->cnic_priv;
803 int i;
804
805 for (i = 0; i < cp->ctx_blks; i++) {
806 if (cp->ctx_arr[i].ctx) {
Michael Chan3248e162009-12-02 15:15:39 +0000807 dma_free_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
808 cp->ctx_arr[i].ctx,
809 cp->ctx_arr[i].mapping);
Michael Chan86b53602009-10-10 13:46:57 +0000810 cp->ctx_arr[i].ctx = NULL;
811 }
812 }
813}
814
Michael Chancd801532010-10-13 14:06:49 +0000815static void __cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chana4636962009-06-08 18:14:43 -0700816{
Michael Chancd801532010-10-13 14:06:49 +0000817 uio_unregister_device(&udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -0700818
Michael Chancd801532010-10-13 14:06:49 +0000819 if (udev->l2_buf) {
820 dma_free_coherent(&udev->pdev->dev, udev->l2_buf_size,
821 udev->l2_buf, udev->l2_buf_map);
822 udev->l2_buf = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700823 }
824
Michael Chancd801532010-10-13 14:06:49 +0000825 if (udev->l2_ring) {
826 dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
827 udev->l2_ring, udev->l2_ring_map);
828 udev->l2_ring = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700829 }
Michael Chana3ceeeb2010-10-13 14:06:50 +0000830
831 pci_dev_put(udev->pdev);
832 kfree(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000833}
834
Michael Chancd801532010-10-13 14:06:49 +0000835static void cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000836{
Michael Chancd801532010-10-13 14:06:49 +0000837 if (!udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000838 return;
839
Michael Chana3ceeeb2010-10-13 14:06:50 +0000840 write_lock(&cnic_dev_lock);
841 list_del_init(&udev->list);
842 write_unlock(&cnic_dev_lock);
Michael Chancd801532010-10-13 14:06:49 +0000843 __cnic_free_uio(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000844}
845
846static void cnic_free_resc(struct cnic_dev *dev)
847{
848 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000849 struct cnic_uio_dev *udev = cp->udev;
Michael Chanc06c0462010-10-13 14:06:48 +0000850
Michael Chancd801532010-10-13 14:06:49 +0000851 if (udev) {
Michael Chana3ceeeb2010-10-13 14:06:50 +0000852 udev->dev = NULL;
Michael Chancd801532010-10-13 14:06:49 +0000853 cp->udev = NULL;
Michael Chanc06c0462010-10-13 14:06:48 +0000854 }
Michael Chana4636962009-06-08 18:14:43 -0700855
Michael Chan86b53602009-10-10 13:46:57 +0000856 cnic_free_context(dev);
Michael Chana4636962009-06-08 18:14:43 -0700857 kfree(cp->ctx_arr);
858 cp->ctx_arr = NULL;
859 cp->ctx_blks = 0;
860
861 cnic_free_dma(dev, &cp->gbl_buf_info);
Michael Chana4636962009-06-08 18:14:43 -0700862 cnic_free_dma(dev, &cp->kwq_info);
Michael Chan71034ba2009-10-10 13:46:59 +0000863 cnic_free_dma(dev, &cp->kwq_16_data_info);
Michael Chane21ba412010-12-23 07:43:03 +0000864 cnic_free_dma(dev, &cp->kcq2.dma);
Michael Chane6c28892010-06-24 14:58:39 +0000865 cnic_free_dma(dev, &cp->kcq1.dma);
Michael Chana4636962009-06-08 18:14:43 -0700866 kfree(cp->iscsi_tbl);
867 cp->iscsi_tbl = NULL;
868 kfree(cp->ctx_tbl);
869 cp->ctx_tbl = NULL;
870
Michael Chane1928c82010-12-23 07:43:04 +0000871 cnic_free_id_tbl(&cp->fcoe_cid_tbl);
Michael Chana4636962009-06-08 18:14:43 -0700872 cnic_free_id_tbl(&cp->cid_tbl);
873}
874
875static int cnic_alloc_context(struct cnic_dev *dev)
876{
877 struct cnic_local *cp = dev->cnic_priv;
878
879 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
880 int i, k, arr_size;
881
882 cp->ctx_blk_size = BCM_PAGE_SIZE;
883 cp->cids_per_blk = BCM_PAGE_SIZE / 128;
884 arr_size = BNX2_MAX_CID / cp->cids_per_blk *
885 sizeof(struct cnic_ctx);
886 cp->ctx_arr = kzalloc(arr_size, GFP_KERNEL);
887 if (cp->ctx_arr == NULL)
888 return -ENOMEM;
889
890 k = 0;
891 for (i = 0; i < 2; i++) {
892 u32 j, reg, off, lo, hi;
893
894 if (i == 0)
895 off = BNX2_PG_CTX_MAP;
896 else
897 off = BNX2_ISCSI_CTX_MAP;
898
899 reg = cnic_reg_rd_ind(dev, off);
900 lo = reg >> 16;
901 hi = reg & 0xffff;
902 for (j = lo; j < hi; j += cp->cids_per_blk, k++)
903 cp->ctx_arr[k].cid = j;
904 }
905
906 cp->ctx_blks = k;
907 if (cp->ctx_blks >= (BNX2_MAX_CID / cp->cids_per_blk)) {
908 cp->ctx_blks = 0;
909 return -ENOMEM;
910 }
911
912 for (i = 0; i < cp->ctx_blks; i++) {
913 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +0000914 dma_alloc_coherent(&dev->pcidev->dev,
915 BCM_PAGE_SIZE,
916 &cp->ctx_arr[i].mapping,
917 GFP_KERNEL);
Michael Chana4636962009-06-08 18:14:43 -0700918 if (cp->ctx_arr[i].ctx == NULL)
919 return -ENOMEM;
920 }
921 }
922 return 0;
923}
924
Michael Chan59e51372011-06-14 01:32:38 +0000925static u16 cnic_bnx2_next_idx(u16 idx)
Michael Chane6c28892010-06-24 14:58:39 +0000926{
Michael Chan59e51372011-06-14 01:32:38 +0000927 return idx + 1;
928}
929
930static u16 cnic_bnx2_hw_idx(u16 idx)
931{
932 return idx;
933}
934
935static u16 cnic_bnx2x_next_idx(u16 idx)
936{
937 idx++;
938 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
939 idx++;
940
941 return idx;
942}
943
944static u16 cnic_bnx2x_hw_idx(u16 idx)
945{
946 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
947 idx++;
948 return idx;
949}
950
951static int cnic_alloc_kcq(struct cnic_dev *dev, struct kcq_info *info,
952 bool use_pg_tbl)
953{
954 int err, i, use_page_tbl = 0;
Michael Chane6c28892010-06-24 14:58:39 +0000955 struct kcqe **kcq;
956
Michael Chan59e51372011-06-14 01:32:38 +0000957 if (use_pg_tbl)
958 use_page_tbl = 1;
Michael Chane6c28892010-06-24 14:58:39 +0000959
Michael Chan59e51372011-06-14 01:32:38 +0000960 err = cnic_alloc_dma(dev, &info->dma, KCQ_PAGE_CNT, use_page_tbl);
Michael Chane6c28892010-06-24 14:58:39 +0000961 if (err)
962 return err;
963
964 kcq = (struct kcqe **) info->dma.pg_arr;
965 info->kcq = kcq;
966
Michael Chan59e51372011-06-14 01:32:38 +0000967 info->next_idx = cnic_bnx2_next_idx;
968 info->hw_idx = cnic_bnx2_hw_idx;
969 if (use_pg_tbl)
Michael Chane6c28892010-06-24 14:58:39 +0000970 return 0;
971
Michael Chan59e51372011-06-14 01:32:38 +0000972 info->next_idx = cnic_bnx2x_next_idx;
973 info->hw_idx = cnic_bnx2x_hw_idx;
974
Michael Chane6c28892010-06-24 14:58:39 +0000975 for (i = 0; i < KCQ_PAGE_CNT; i++) {
976 struct bnx2x_bd_chain_next *next =
977 (struct bnx2x_bd_chain_next *) &kcq[i][MAX_KCQE_CNT];
978 int j = i + 1;
979
980 if (j >= KCQ_PAGE_CNT)
981 j = 0;
982 next->addr_hi = (u64) info->dma.pg_map_arr[j] >> 32;
983 next->addr_lo = info->dma.pg_map_arr[j] & 0xffffffff;
984 }
985 return 0;
986}
987
Michael Chancd801532010-10-13 14:06:49 +0000988static int cnic_alloc_uio_rings(struct cnic_dev *dev, int pages)
Michael Chanec0248e2009-08-26 09:49:22 +0000989{
990 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000991 struct cnic_uio_dev *udev;
Michael Chanec0248e2009-08-26 09:49:22 +0000992
Michael Chana3ceeeb2010-10-13 14:06:50 +0000993 read_lock(&cnic_dev_lock);
994 list_for_each_entry(udev, &cnic_udev_list, list) {
995 if (udev->pdev == dev->pcidev) {
996 udev->dev = dev;
997 cp->udev = udev;
998 read_unlock(&cnic_dev_lock);
999 return 0;
1000 }
1001 }
1002 read_unlock(&cnic_dev_lock);
1003
Michael Chancd801532010-10-13 14:06:49 +00001004 udev = kzalloc(sizeof(struct cnic_uio_dev), GFP_ATOMIC);
1005 if (!udev)
Michael Chanec0248e2009-08-26 09:49:22 +00001006 return -ENOMEM;
1007
Michael Chancd801532010-10-13 14:06:49 +00001008 udev->uio_dev = -1;
1009
1010 udev->dev = dev;
1011 udev->pdev = dev->pcidev;
1012 udev->l2_ring_size = pages * BCM_PAGE_SIZE;
1013 udev->l2_ring = dma_alloc_coherent(&udev->pdev->dev, udev->l2_ring_size,
1014 &udev->l2_ring_map,
1015 GFP_KERNEL | __GFP_COMP);
1016 if (!udev->l2_ring)
Jesper Juhlf7e4c972010-12-31 11:18:48 -08001017 goto err_udev;
Michael Chanec0248e2009-08-26 09:49:22 +00001018
Michael Chancd801532010-10-13 14:06:49 +00001019 udev->l2_buf_size = (cp->l2_rx_ring_size + 1) * cp->l2_single_buf_size;
1020 udev->l2_buf_size = PAGE_ALIGN(udev->l2_buf_size);
1021 udev->l2_buf = dma_alloc_coherent(&udev->pdev->dev, udev->l2_buf_size,
1022 &udev->l2_buf_map,
1023 GFP_KERNEL | __GFP_COMP);
1024 if (!udev->l2_buf)
Jesper Juhlf7e4c972010-12-31 11:18:48 -08001025 goto err_dma;
Michael Chancd801532010-10-13 14:06:49 +00001026
Michael Chana3ceeeb2010-10-13 14:06:50 +00001027 write_lock(&cnic_dev_lock);
1028 list_add(&udev->list, &cnic_udev_list);
1029 write_unlock(&cnic_dev_lock);
1030
1031 pci_dev_get(udev->pdev);
1032
Michael Chancd801532010-10-13 14:06:49 +00001033 cp->udev = udev;
1034
Michael Chanec0248e2009-08-26 09:49:22 +00001035 return 0;
Jesper Juhlf7e4c972010-12-31 11:18:48 -08001036 err_dma:
1037 dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
1038 udev->l2_ring, udev->l2_ring_map);
1039 err_udev:
1040 kfree(udev);
1041 return -ENOMEM;
Michael Chanec0248e2009-08-26 09:49:22 +00001042}
1043
Michael Chancd801532010-10-13 14:06:49 +00001044static int cnic_init_uio(struct cnic_dev *dev)
1045{
Michael Chan5e9b2db2009-08-26 09:49:23 +00001046 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00001047 struct cnic_uio_dev *udev = cp->udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001048 struct uio_info *uinfo;
Michael Chancd801532010-10-13 14:06:49 +00001049 int ret = 0;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001050
Michael Chancd801532010-10-13 14:06:49 +00001051 if (!udev)
Michael Chan5e9b2db2009-08-26 09:49:23 +00001052 return -ENOMEM;
1053
Michael Chancd801532010-10-13 14:06:49 +00001054 uinfo = &udev->cnic_uinfo;
1055
Michael Chan054581e2012-07-05 14:21:55 +00001056 uinfo->mem[0].addr = pci_resource_start(dev->pcidev, 0);
Michael Chan5e9b2db2009-08-26 09:49:23 +00001057 uinfo->mem[0].internal_addr = dev->regview;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001058 uinfo->mem[0].memtype = UIO_MEM_PHYS;
1059
Michael Chan5e9b2db2009-08-26 09:49:23 +00001060 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
Michael Chan054581e2012-07-05 14:21:55 +00001061 uinfo->mem[0].size = MB_GET_CID_ADDR(TX_TSS_CID +
1062 TX_MAX_TSS_RINGS + 1);
Michael Chana4dde3a2010-02-24 14:42:08 +00001063 uinfo->mem[1].addr = (unsigned long) cp->status_blk.gen &
Michael Chancd801532010-10-13 14:06:49 +00001064 PAGE_MASK;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001065 if (cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
1066 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE * 9;
1067 else
1068 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE;
1069
1070 uinfo->name = "bnx2_cnic";
Michael Chan71034ba2009-10-10 13:46:59 +00001071 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
Michael Chan054581e2012-07-05 14:21:55 +00001072 uinfo->mem[0].size = pci_resource_len(dev->pcidev, 0);
1073
Michael Chan71034ba2009-10-10 13:46:59 +00001074 uinfo->mem[1].addr = (unsigned long) cp->bnx2x_def_status_blk &
1075 PAGE_MASK;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001076 uinfo->mem[1].size = sizeof(*cp->bnx2x_def_status_blk);
Michael Chan71034ba2009-10-10 13:46:59 +00001077
1078 uinfo->name = "bnx2x_cnic";
Michael Chan5e9b2db2009-08-26 09:49:23 +00001079 }
1080
1081 uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
1082
Michael Chancd801532010-10-13 14:06:49 +00001083 uinfo->mem[2].addr = (unsigned long) udev->l2_ring;
1084 uinfo->mem[2].size = udev->l2_ring_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001085 uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
1086
Michael Chancd801532010-10-13 14:06:49 +00001087 uinfo->mem[3].addr = (unsigned long) udev->l2_buf;
1088 uinfo->mem[3].size = udev->l2_buf_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001089 uinfo->mem[3].memtype = UIO_MEM_LOGICAL;
1090
1091 uinfo->version = CNIC_MODULE_VERSION;
1092 uinfo->irq = UIO_IRQ_CUSTOM;
1093
1094 uinfo->open = cnic_uio_open;
1095 uinfo->release = cnic_uio_close;
1096
Michael Chana3ceeeb2010-10-13 14:06:50 +00001097 if (udev->uio_dev == -1) {
1098 if (!uinfo->priv) {
1099 uinfo->priv = udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001100
Michael Chana3ceeeb2010-10-13 14:06:50 +00001101 ret = uio_register_device(&udev->pdev->dev, uinfo);
1102 }
1103 } else {
1104 cnic_init_rings(dev);
1105 }
Michael Chan5e9b2db2009-08-26 09:49:23 +00001106
Michael Chancd801532010-10-13 14:06:49 +00001107 return ret;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001108}
1109
Michael Chana4636962009-06-08 18:14:43 -07001110static int cnic_alloc_bnx2_resc(struct cnic_dev *dev)
1111{
1112 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07001113 int ret;
1114
1115 ret = cnic_alloc_dma(dev, &cp->kwq_info, KWQ_PAGE_CNT, 1);
1116 if (ret)
1117 goto error;
1118 cp->kwq = (struct kwqe **) cp->kwq_info.pg_arr;
1119
Michael Chan59e51372011-06-14 01:32:38 +00001120 ret = cnic_alloc_kcq(dev, &cp->kcq1, true);
Michael Chana4636962009-06-08 18:14:43 -07001121 if (ret)
1122 goto error;
Michael Chana4636962009-06-08 18:14:43 -07001123
1124 ret = cnic_alloc_context(dev);
1125 if (ret)
1126 goto error;
1127
Michael Chancd801532010-10-13 14:06:49 +00001128 ret = cnic_alloc_uio_rings(dev, 2);
Michael Chanec0248e2009-08-26 09:49:22 +00001129 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001130 goto error;
1131
Michael Chancd801532010-10-13 14:06:49 +00001132 ret = cnic_init_uio(dev);
Michael Chan5e9b2db2009-08-26 09:49:23 +00001133 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001134 goto error;
1135
Michael Chana4636962009-06-08 18:14:43 -07001136 return 0;
1137
1138error:
1139 cnic_free_resc(dev);
1140 return ret;
1141}
1142
Michael Chan71034ba2009-10-10 13:46:59 +00001143static int cnic_alloc_bnx2x_context(struct cnic_dev *dev)
1144{
1145 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001146 int ctx_blk_size = cp->ethdev->ctx_blk_size;
Michael Chan520efdf2010-06-24 14:58:37 +00001147 int total_mem, blks, i;
Michael Chan71034ba2009-10-10 13:46:59 +00001148
Michael Chan520efdf2010-06-24 14:58:37 +00001149 total_mem = BNX2X_CONTEXT_MEM_SIZE * cp->max_cid_space;
Michael Chan71034ba2009-10-10 13:46:59 +00001150 blks = total_mem / ctx_blk_size;
1151 if (total_mem % ctx_blk_size)
1152 blks++;
1153
1154 if (blks > cp->ethdev->ctx_tbl_len)
1155 return -ENOMEM;
1156
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001157 cp->ctx_arr = kcalloc(blks, sizeof(struct cnic_ctx), GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001158 if (cp->ctx_arr == NULL)
1159 return -ENOMEM;
1160
1161 cp->ctx_blks = blks;
1162 cp->ctx_blk_size = ctx_blk_size;
Michael Chanee87a822010-10-13 14:06:51 +00001163 if (!BNX2X_CHIP_IS_57710(cp->chip_id))
Michael Chan71034ba2009-10-10 13:46:59 +00001164 cp->ctx_align = 0;
1165 else
1166 cp->ctx_align = ctx_blk_size;
1167
1168 cp->cids_per_blk = ctx_blk_size / BNX2X_CONTEXT_MEM_SIZE;
1169
1170 for (i = 0; i < blks; i++) {
1171 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +00001172 dma_alloc_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
1173 &cp->ctx_arr[i].mapping,
1174 GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001175 if (cp->ctx_arr[i].ctx == NULL)
1176 return -ENOMEM;
1177
1178 if (cp->ctx_align && cp->ctx_blk_size == ctx_blk_size) {
1179 if (cp->ctx_arr[i].mapping & (cp->ctx_align - 1)) {
1180 cnic_free_context(dev);
1181 cp->ctx_blk_size += cp->ctx_align;
1182 i = -1;
1183 continue;
1184 }
1185 }
1186 }
1187 return 0;
1188}
1189
1190static int cnic_alloc_bnx2x_resc(struct cnic_dev *dev)
1191{
1192 struct cnic_local *cp = dev->cnic_priv;
Michael Chan520efdf2010-06-24 14:58:37 +00001193 struct cnic_eth_dev *ethdev = cp->ethdev;
1194 u32 start_cid = ethdev->starting_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001195 int i, j, n, ret, pages;
1196 struct cnic_dma *kwq_16_dma = &cp->kwq_16_data_info;
1197
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001198 cp->iro_arr = ethdev->iro_arr;
1199
Michael Chanb37a41e2011-07-20 14:55:22 +00001200 cp->max_cid_space = MAX_ISCSI_TBL_SZ;
Michael Chan520efdf2010-06-24 14:58:37 +00001201 cp->iscsi_start_cid = start_cid;
Michael Chane1928c82010-12-23 07:43:04 +00001202 cp->fcoe_start_cid = start_cid + MAX_ISCSI_TBL_SZ;
1203
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001204 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chandc219a22011-08-26 09:45:39 +00001205 cp->max_cid_space += dev->max_fcoe_conn;
Michael Chane1928c82010-12-23 07:43:04 +00001206 cp->fcoe_init_cid = ethdev->fcoe_init_cid;
1207 if (!cp->fcoe_init_cid)
1208 cp->fcoe_init_cid = 0x10;
1209 }
1210
Michael Chan71034ba2009-10-10 13:46:59 +00001211 cp->iscsi_tbl = kzalloc(sizeof(struct cnic_iscsi) * MAX_ISCSI_TBL_SZ,
1212 GFP_KERNEL);
1213 if (!cp->iscsi_tbl)
1214 goto error;
1215
1216 cp->ctx_tbl = kzalloc(sizeof(struct cnic_context) *
Michael Chan520efdf2010-06-24 14:58:37 +00001217 cp->max_cid_space, GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001218 if (!cp->ctx_tbl)
1219 goto error;
1220
1221 for (i = 0; i < MAX_ISCSI_TBL_SZ; i++) {
1222 cp->ctx_tbl[i].proto.iscsi = &cp->iscsi_tbl[i];
1223 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_ISCSI;
1224 }
1225
Michael Chane1928c82010-12-23 07:43:04 +00001226 for (i = MAX_ISCSI_TBL_SZ; i < cp->max_cid_space; i++)
1227 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_FCOE;
1228
Michael Chan520efdf2010-06-24 14:58:37 +00001229 pages = PAGE_ALIGN(cp->max_cid_space * CNIC_KWQ16_DATA_SIZE) /
Michael Chan71034ba2009-10-10 13:46:59 +00001230 PAGE_SIZE;
1231
1232 ret = cnic_alloc_dma(dev, kwq_16_dma, pages, 0);
1233 if (ret)
1234 return -ENOMEM;
1235
1236 n = PAGE_SIZE / CNIC_KWQ16_DATA_SIZE;
Michael Chan520efdf2010-06-24 14:58:37 +00001237 for (i = 0, j = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +00001238 long off = CNIC_KWQ16_DATA_SIZE * (i % n);
1239
1240 cp->ctx_tbl[i].kwqe_data = kwq_16_dma->pg_arr[j] + off;
1241 cp->ctx_tbl[i].kwqe_data_mapping = kwq_16_dma->pg_map_arr[j] +
1242 off;
1243
1244 if ((i % n) == (n - 1))
1245 j++;
1246 }
1247
Michael Chan59e51372011-06-14 01:32:38 +00001248 ret = cnic_alloc_kcq(dev, &cp->kcq1, false);
Michael Chan71034ba2009-10-10 13:46:59 +00001249 if (ret)
1250 goto error;
Michael Chan71034ba2009-10-10 13:46:59 +00001251
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001252 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
1253 ret = cnic_alloc_kcq(dev, &cp->kcq2, true);
Michael Chane21ba412010-12-23 07:43:03 +00001254 if (ret)
1255 goto error;
1256 }
1257
Michael Chan71034ba2009-10-10 13:46:59 +00001258 pages = PAGE_ALIGN(BNX2X_ISCSI_GLB_BUF_SIZE) / PAGE_SIZE;
1259 ret = cnic_alloc_dma(dev, &cp->gbl_buf_info, pages, 0);
1260 if (ret)
1261 goto error;
1262
1263 ret = cnic_alloc_bnx2x_context(dev);
1264 if (ret)
1265 goto error;
1266
Michael Chan71034ba2009-10-10 13:46:59 +00001267 cp->bnx2x_def_status_blk = cp->ethdev->irq_arr[1].status_blk;
1268
1269 cp->l2_rx_ring_size = 15;
1270
Michael Chancd801532010-10-13 14:06:49 +00001271 ret = cnic_alloc_uio_rings(dev, 4);
Michael Chan71034ba2009-10-10 13:46:59 +00001272 if (ret)
1273 goto error;
1274
Michael Chancd801532010-10-13 14:06:49 +00001275 ret = cnic_init_uio(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00001276 if (ret)
1277 goto error;
1278
1279 return 0;
1280
1281error:
1282 cnic_free_resc(dev);
1283 return -ENOMEM;
1284}
1285
Michael Chana4636962009-06-08 18:14:43 -07001286static inline u32 cnic_kwq_avail(struct cnic_local *cp)
1287{
1288 return cp->max_kwq_idx -
1289 ((cp->kwq_prod_idx - cp->kwq_con_idx) & cp->max_kwq_idx);
1290}
1291
1292static int cnic_submit_bnx2_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
1293 u32 num_wqes)
1294{
1295 struct cnic_local *cp = dev->cnic_priv;
1296 struct kwqe *prod_qe;
1297 u16 prod, sw_prod, i;
1298
1299 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
1300 return -EAGAIN; /* bnx2 is down */
1301
1302 spin_lock_bh(&cp->cnic_ulp_lock);
1303 if (num_wqes > cnic_kwq_avail(cp) &&
Michael Chan1f1332a2010-05-18 11:32:52 +00001304 !test_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags)) {
Michael Chana4636962009-06-08 18:14:43 -07001305 spin_unlock_bh(&cp->cnic_ulp_lock);
1306 return -EAGAIN;
1307 }
1308
Michael Chan1f1332a2010-05-18 11:32:52 +00001309 clear_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07001310
1311 prod = cp->kwq_prod_idx;
1312 sw_prod = prod & MAX_KWQ_IDX;
1313 for (i = 0; i < num_wqes; i++) {
1314 prod_qe = &cp->kwq[KWQ_PG(sw_prod)][KWQ_IDX(sw_prod)];
1315 memcpy(prod_qe, wqes[i], sizeof(struct kwqe));
1316 prod++;
1317 sw_prod = prod & MAX_KWQ_IDX;
1318 }
1319 cp->kwq_prod_idx = prod;
1320
1321 CNIC_WR16(dev, cp->kwq_io_addr, cp->kwq_prod_idx);
1322
1323 spin_unlock_bh(&cp->cnic_ulp_lock);
1324 return 0;
1325}
1326
Michael Chan71034ba2009-10-10 13:46:59 +00001327static void *cnic_get_kwqe_16_data(struct cnic_local *cp, u32 l5_cid,
1328 union l5cm_specific_data *l5_data)
1329{
1330 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1331 dma_addr_t map;
1332
1333 map = ctx->kwqe_data_mapping;
1334 l5_data->phy_address.lo = (u64) map & 0xffffffff;
1335 l5_data->phy_address.hi = (u64) map >> 32;
1336 return ctx->kwqe_data;
1337}
1338
1339static int cnic_submit_kwqe_16(struct cnic_dev *dev, u32 cmd, u32 cid,
1340 u32 type, union l5cm_specific_data *l5_data)
1341{
1342 struct cnic_local *cp = dev->cnic_priv;
1343 struct l5cm_spe kwqe;
1344 struct kwqe_16 *kwq[1];
Michael Chan68d7c1a2011-01-05 15:14:13 +00001345 u16 type_16;
Michael Chan71034ba2009-10-10 13:46:59 +00001346 int ret;
1347
1348 kwqe.hdr.conn_and_cmd_data =
1349 cpu_to_le32(((cmd << SPE_HDR_CMD_ID_SHIFT) |
Michael Chanceb7e1c2010-10-06 03:14:54 +00001350 BNX2X_HW_CID(cp, cid)));
Michael Chan68d7c1a2011-01-05 15:14:13 +00001351
1352 type_16 = (type << SPE_HDR_CONN_TYPE_SHIFT) & SPE_HDR_CONN_TYPE;
1353 type_16 |= (cp->pfid << SPE_HDR_FUNCTION_ID_SHIFT) &
1354 SPE_HDR_FUNCTION_ID;
1355
1356 kwqe.hdr.type = cpu_to_le16(type_16);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001357 kwqe.hdr.reserved1 = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001358 kwqe.data.phy_address.lo = cpu_to_le32(l5_data->phy_address.lo);
1359 kwqe.data.phy_address.hi = cpu_to_le32(l5_data->phy_address.hi);
1360
1361 kwq[0] = (struct kwqe_16 *) &kwqe;
1362
1363 spin_lock_bh(&cp->cnic_ulp_lock);
1364 ret = cp->ethdev->drv_submit_kwqes_16(dev->netdev, kwq, 1);
1365 spin_unlock_bh(&cp->cnic_ulp_lock);
1366
1367 if (ret == 1)
1368 return 0;
1369
Michael Chan23021c22012-01-04 12:12:28 +00001370 return ret;
Michael Chan71034ba2009-10-10 13:46:59 +00001371}
1372
1373static void cnic_reply_bnx2x_kcqes(struct cnic_dev *dev, int ulp_type,
1374 struct kcqe *cqes[], u32 num_cqes)
1375{
1376 struct cnic_local *cp = dev->cnic_priv;
1377 struct cnic_ulp_ops *ulp_ops;
1378
1379 rcu_read_lock();
1380 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
1381 if (likely(ulp_ops)) {
1382 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
1383 cqes, num_cqes);
1384 }
1385 rcu_read_unlock();
1386}
1387
1388static int cnic_bnx2x_iscsi_init1(struct cnic_dev *dev, struct kwqe *kwqe)
1389{
1390 struct cnic_local *cp = dev->cnic_priv;
1391 struct iscsi_kwqe_init1 *req1 = (struct iscsi_kwqe_init1 *) kwqe;
Michael Chan14203982010-10-06 03:16:06 +00001392 int hq_bds, pages;
1393 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001394
1395 cp->num_iscsi_tasks = req1->num_tasks_per_conn;
1396 cp->num_ccells = req1->num_ccells_per_conn;
1397 cp->task_array_size = BNX2X_ISCSI_TASK_CONTEXT_SIZE *
1398 cp->num_iscsi_tasks;
1399 cp->r2tq_size = cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS *
1400 BNX2X_ISCSI_R2TQE_SIZE;
1401 cp->hq_size = cp->num_ccells * BNX2X_ISCSI_HQ_BD_SIZE;
1402 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1403 hq_bds = pages * (PAGE_SIZE / BNX2X_ISCSI_HQ_BD_SIZE);
1404 cp->num_cqs = req1->num_cqs;
1405
1406 if (!dev->max_iscsi_conn)
1407 return 0;
1408
1409 /* init Tstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001410 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001411 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001412 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001413 PAGE_SIZE);
1414 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001415 TSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001416 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001417 TSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001418 req1->num_tasks_per_conn);
1419
1420 /* init Ustorm RAM */
1421 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001422 USTORM_ISCSI_RQ_BUFFER_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001423 req1->rq_buffer_size);
Michael Chan14203982010-10-06 03:16:06 +00001424 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001425 PAGE_SIZE);
1426 CNIC_WR8(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001427 USTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001428 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001429 USTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001430 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001431 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001432 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001433 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001434 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001435 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001436 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1437
1438 /* init Xstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001439 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001440 PAGE_SIZE);
1441 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001442 XSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001443 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001444 XSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001445 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001446 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001447 hq_bds);
Michael Chan14203982010-10-06 03:16:06 +00001448 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_SQ_SIZE_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_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001451 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1452
1453 /* init Cstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001454 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001455 PAGE_SIZE);
1456 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001457 CSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001458 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001459 CSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001460 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001461 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001462 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001463 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001464 hq_bds);
1465
1466 return 0;
1467}
1468
1469static int cnic_bnx2x_iscsi_init2(struct cnic_dev *dev, struct kwqe *kwqe)
1470{
1471 struct iscsi_kwqe_init2 *req2 = (struct iscsi_kwqe_init2 *) kwqe;
1472 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00001473 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001474 struct iscsi_kcqe kcqe;
1475 struct kcqe *cqes[1];
1476
1477 memset(&kcqe, 0, sizeof(kcqe));
1478 if (!dev->max_iscsi_conn) {
1479 kcqe.completion_status =
1480 ISCSI_KCQE_COMPLETION_STATUS_ISCSI_NOT_SUPPORTED;
1481 goto done;
1482 }
1483
1484 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001485 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001486 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001487 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001488 req2->error_bit_map[1]);
1489
1490 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001491 USTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001492 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001493 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001494 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001495 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001496 req2->error_bit_map[1]);
1497
1498 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001499 CSTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001500
1501 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1502
1503done:
1504 kcqe.op_code = ISCSI_KCQE_OPCODE_INIT;
1505 cqes[0] = (struct kcqe *) &kcqe;
1506 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1507
1508 return 0;
1509}
1510
1511static void cnic_free_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1512{
1513 struct cnic_local *cp = dev->cnic_priv;
1514 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1515
1516 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI) {
1517 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1518
1519 cnic_free_dma(dev, &iscsi->hq_info);
1520 cnic_free_dma(dev, &iscsi->r2tq_info);
1521 cnic_free_dma(dev, &iscsi->task_array_info);
Michael Chane1928c82010-12-23 07:43:04 +00001522 cnic_free_id(&cp->cid_tbl, ctx->cid);
1523 } else {
1524 cnic_free_id(&cp->fcoe_cid_tbl, ctx->cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001525 }
Michael Chane1928c82010-12-23 07:43:04 +00001526
Michael Chan71034ba2009-10-10 13:46:59 +00001527 ctx->cid = 0;
1528}
1529
1530static int cnic_alloc_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1531{
1532 u32 cid;
1533 int ret, pages;
1534 struct cnic_local *cp = dev->cnic_priv;
1535 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1536 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1537
Michael Chane1928c82010-12-23 07:43:04 +00001538 if (ctx->ulp_proto_id == CNIC_ULP_FCOE) {
1539 cid = cnic_alloc_new_id(&cp->fcoe_cid_tbl);
1540 if (cid == -1) {
1541 ret = -ENOMEM;
1542 goto error;
1543 }
1544 ctx->cid = cid;
1545 return 0;
1546 }
1547
Michael Chan71034ba2009-10-10 13:46:59 +00001548 cid = cnic_alloc_new_id(&cp->cid_tbl);
1549 if (cid == -1) {
1550 ret = -ENOMEM;
1551 goto error;
1552 }
1553
1554 ctx->cid = cid;
1555 pages = PAGE_ALIGN(cp->task_array_size) / PAGE_SIZE;
1556
1557 ret = cnic_alloc_dma(dev, &iscsi->task_array_info, pages, 1);
1558 if (ret)
1559 goto error;
1560
1561 pages = PAGE_ALIGN(cp->r2tq_size) / PAGE_SIZE;
1562 ret = cnic_alloc_dma(dev, &iscsi->r2tq_info, pages, 1);
1563 if (ret)
1564 goto error;
1565
1566 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1567 ret = cnic_alloc_dma(dev, &iscsi->hq_info, pages, 1);
1568 if (ret)
1569 goto error;
1570
1571 return 0;
1572
1573error:
1574 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1575 return ret;
1576}
1577
1578static void *cnic_get_bnx2x_ctx(struct cnic_dev *dev, u32 cid, int init,
1579 struct regpair *ctx_addr)
1580{
1581 struct cnic_local *cp = dev->cnic_priv;
1582 struct cnic_eth_dev *ethdev = cp->ethdev;
1583 int blk = (cid - ethdev->starting_cid) / cp->cids_per_blk;
1584 int off = (cid - ethdev->starting_cid) % cp->cids_per_blk;
1585 unsigned long align_off = 0;
1586 dma_addr_t ctx_map;
1587 void *ctx;
1588
1589 if (cp->ctx_align) {
1590 unsigned long mask = cp->ctx_align - 1;
1591
1592 if (cp->ctx_arr[blk].mapping & mask)
1593 align_off = cp->ctx_align -
1594 (cp->ctx_arr[blk].mapping & mask);
1595 }
1596 ctx_map = cp->ctx_arr[blk].mapping + align_off +
1597 (off * BNX2X_CONTEXT_MEM_SIZE);
1598 ctx = cp->ctx_arr[blk].ctx + align_off +
1599 (off * BNX2X_CONTEXT_MEM_SIZE);
1600 if (init)
1601 memset(ctx, 0, BNX2X_CONTEXT_MEM_SIZE);
1602
1603 ctx_addr->lo = ctx_map & 0xffffffff;
1604 ctx_addr->hi = (u64) ctx_map >> 32;
1605 return ctx;
1606}
1607
1608static int cnic_setup_bnx2x_ctx(struct cnic_dev *dev, struct kwqe *wqes[],
1609 u32 num)
1610{
1611 struct cnic_local *cp = dev->cnic_priv;
1612 struct iscsi_kwqe_conn_offload1 *req1 =
1613 (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1614 struct iscsi_kwqe_conn_offload2 *req2 =
1615 (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1616 struct iscsi_kwqe_conn_offload3 *req3;
1617 struct cnic_context *ctx = &cp->ctx_tbl[req1->iscsi_conn_id];
1618 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1619 u32 cid = ctx->cid;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001620 u32 hw_cid = BNX2X_HW_CID(cp, cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001621 struct iscsi_context *ictx;
1622 struct regpair context_addr;
1623 int i, j, n = 2, n_max;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001624 u8 port = CNIC_PORT(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00001625
1626 ctx->ctx_flags = 0;
1627 if (!req2->num_additional_wqes)
1628 return -EINVAL;
1629
1630 n_max = req2->num_additional_wqes + 2;
1631
1632 ictx = cnic_get_bnx2x_ctx(dev, cid, 1, &context_addr);
1633 if (ictx == NULL)
1634 return -ENOMEM;
1635
1636 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1637
1638 ictx->xstorm_ag_context.hq_prod = 1;
1639
1640 ictx->xstorm_st_context.iscsi.first_burst_length =
1641 ISCSI_DEF_FIRST_BURST_LEN;
1642 ictx->xstorm_st_context.iscsi.max_send_pdu_length =
1643 ISCSI_DEF_MAX_RECV_SEG_LEN;
1644 ictx->xstorm_st_context.iscsi.sq_pbl_base.lo =
1645 req1->sq_page_table_addr_lo;
1646 ictx->xstorm_st_context.iscsi.sq_pbl_base.hi =
1647 req1->sq_page_table_addr_hi;
1648 ictx->xstorm_st_context.iscsi.sq_curr_pbe.lo = req2->sq_first_pte.hi;
1649 ictx->xstorm_st_context.iscsi.sq_curr_pbe.hi = req2->sq_first_pte.lo;
1650 ictx->xstorm_st_context.iscsi.hq_pbl_base.lo =
1651 iscsi->hq_info.pgtbl_map & 0xffffffff;
1652 ictx->xstorm_st_context.iscsi.hq_pbl_base.hi =
1653 (u64) iscsi->hq_info.pgtbl_map >> 32;
1654 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.lo =
1655 iscsi->hq_info.pgtbl[0];
1656 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.hi =
1657 iscsi->hq_info.pgtbl[1];
1658 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.lo =
1659 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1660 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.hi =
1661 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1662 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.lo =
1663 iscsi->r2tq_info.pgtbl[0];
1664 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.hi =
1665 iscsi->r2tq_info.pgtbl[1];
1666 ictx->xstorm_st_context.iscsi.task_pbl_base.lo =
1667 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1668 ictx->xstorm_st_context.iscsi.task_pbl_base.hi =
1669 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1670 ictx->xstorm_st_context.iscsi.task_pbl_cache_idx =
1671 BNX2X_ISCSI_PBL_NOT_CACHED;
1672 ictx->xstorm_st_context.iscsi.flags.flags |=
1673 XSTORM_ISCSI_CONTEXT_FLAGS_B_IMMEDIATE_DATA;
1674 ictx->xstorm_st_context.iscsi.flags.flags |=
1675 XSTORM_ISCSI_CONTEXT_FLAGS_B_INITIAL_R2T;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001676 ictx->xstorm_st_context.common.ethernet.reserved_vlan_type =
1677 ETH_P_8021Q;
1678 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id) &&
1679 cp->port_mode == CHIP_2_PORT_MODE) {
1680
1681 port = 0;
1682 }
1683 ictx->xstorm_st_context.common.flags =
1684 1 << XSTORM_COMMON_CONTEXT_SECTION_PHYSQ_INITIALIZED_SHIFT;
1685 ictx->xstorm_st_context.common.flags =
1686 port << XSTORM_COMMON_CONTEXT_SECTION_PBF_PORT_SHIFT;
Michael Chan71034ba2009-10-10 13:46:59 +00001687
1688 ictx->tstorm_st_context.iscsi.hdr_bytes_2_fetch = ISCSI_HEADER_SIZE;
1689 /* TSTORM requires the base address of RQ DB & not PTE */
1690 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.lo =
1691 req2->rq_page_table_addr_lo & PAGE_MASK;
1692 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.hi =
1693 req2->rq_page_table_addr_hi;
1694 ictx->tstorm_st_context.iscsi.iscsi_conn_id = req1->iscsi_conn_id;
1695 ictx->tstorm_st_context.tcp.cwnd = 0x5A8;
1696 ictx->tstorm_st_context.tcp.flags2 |=
1697 TSTORM_TCP_ST_CONTEXT_SECTION_DA_EN;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001698 ictx->tstorm_st_context.tcp.ooo_support_mode =
1699 TCP_TSTORM_OOO_DROP_AND_PROC_ACK;
Michael Chan71034ba2009-10-10 13:46:59 +00001700
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001701 ictx->timers_context.flags |= TIMERS_BLOCK_CONTEXT_CONN_VALID_FLG;
Michael Chan71034ba2009-10-10 13:46:59 +00001702
1703 ictx->ustorm_st_context.ring.rq.pbl_base.lo =
Michael Chan15971c32009-12-02 15:15:38 +00001704 req2->rq_page_table_addr_lo;
Michael Chan71034ba2009-10-10 13:46:59 +00001705 ictx->ustorm_st_context.ring.rq.pbl_base.hi =
Michael Chan15971c32009-12-02 15:15:38 +00001706 req2->rq_page_table_addr_hi;
Michael Chan71034ba2009-10-10 13:46:59 +00001707 ictx->ustorm_st_context.ring.rq.curr_pbe.lo = req3->qp_first_pte[0].hi;
1708 ictx->ustorm_st_context.ring.rq.curr_pbe.hi = req3->qp_first_pte[0].lo;
1709 ictx->ustorm_st_context.ring.r2tq.pbl_base.lo =
1710 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1711 ictx->ustorm_st_context.ring.r2tq.pbl_base.hi =
1712 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1713 ictx->ustorm_st_context.ring.r2tq.curr_pbe.lo =
1714 iscsi->r2tq_info.pgtbl[0];
1715 ictx->ustorm_st_context.ring.r2tq.curr_pbe.hi =
1716 iscsi->r2tq_info.pgtbl[1];
1717 ictx->ustorm_st_context.ring.cq_pbl_base.lo =
1718 req1->cq_page_table_addr_lo;
1719 ictx->ustorm_st_context.ring.cq_pbl_base.hi =
1720 req1->cq_page_table_addr_hi;
1721 ictx->ustorm_st_context.ring.cq[0].cq_sn = ISCSI_INITIAL_SN;
1722 ictx->ustorm_st_context.ring.cq[0].curr_pbe.lo = req2->cq_first_pte.hi;
1723 ictx->ustorm_st_context.ring.cq[0].curr_pbe.hi = req2->cq_first_pte.lo;
1724 ictx->ustorm_st_context.task_pbe_cache_index =
1725 BNX2X_ISCSI_PBL_NOT_CACHED;
1726 ictx->ustorm_st_context.task_pdu_cache_index =
1727 BNX2X_ISCSI_PDU_HEADER_NOT_CACHED;
1728
1729 for (i = 1, j = 1; i < cp->num_cqs; i++, j++) {
1730 if (j == 3) {
1731 if (n >= n_max)
1732 break;
1733 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1734 j = 0;
1735 }
1736 ictx->ustorm_st_context.ring.cq[i].cq_sn = ISCSI_INITIAL_SN;
1737 ictx->ustorm_st_context.ring.cq[i].curr_pbe.lo =
1738 req3->qp_first_pte[j].hi;
1739 ictx->ustorm_st_context.ring.cq[i].curr_pbe.hi =
1740 req3->qp_first_pte[j].lo;
1741 }
1742
1743 ictx->ustorm_st_context.task_pbl_base.lo =
1744 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1745 ictx->ustorm_st_context.task_pbl_base.hi =
1746 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1747 ictx->ustorm_st_context.tce_phy_addr.lo =
1748 iscsi->task_array_info.pgtbl[0];
1749 ictx->ustorm_st_context.tce_phy_addr.hi =
1750 iscsi->task_array_info.pgtbl[1];
1751 ictx->ustorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1752 ictx->ustorm_st_context.num_cqs = cp->num_cqs;
1753 ictx->ustorm_st_context.negotiated_rx |= ISCSI_DEF_MAX_RECV_SEG_LEN;
1754 ictx->ustorm_st_context.negotiated_rx_and_flags |=
1755 ISCSI_DEF_MAX_BURST_LEN;
1756 ictx->ustorm_st_context.negotiated_rx |=
1757 ISCSI_DEFAULT_MAX_OUTSTANDING_R2T <<
1758 USTORM_ISCSI_ST_CONTEXT_MAX_OUTSTANDING_R2TS_SHIFT;
1759
1760 ictx->cstorm_st_context.hq_pbl_base.lo =
1761 iscsi->hq_info.pgtbl_map & 0xffffffff;
1762 ictx->cstorm_st_context.hq_pbl_base.hi =
1763 (u64) iscsi->hq_info.pgtbl_map >> 32;
1764 ictx->cstorm_st_context.hq_curr_pbe.lo = iscsi->hq_info.pgtbl[0];
1765 ictx->cstorm_st_context.hq_curr_pbe.hi = iscsi->hq_info.pgtbl[1];
1766 ictx->cstorm_st_context.task_pbl_base.lo =
1767 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1768 ictx->cstorm_st_context.task_pbl_base.hi =
1769 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1770 /* CSTORM and USTORM initialization is different, CSTORM requires
1771 * CQ DB base & not PTE addr */
1772 ictx->cstorm_st_context.cq_db_base.lo =
1773 req1->cq_page_table_addr_lo & PAGE_MASK;
1774 ictx->cstorm_st_context.cq_db_base.hi = req1->cq_page_table_addr_hi;
1775 ictx->cstorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1776 ictx->cstorm_st_context.cq_proc_en_bit_map = (1 << cp->num_cqs) - 1;
1777 for (i = 0; i < cp->num_cqs; i++) {
1778 ictx->cstorm_st_context.cq_c_prod_sqn_arr.sqn[i] =
1779 ISCSI_INITIAL_SN;
1780 ictx->cstorm_st_context.cq_c_sqn_2_notify_arr.sqn[i] =
1781 ISCSI_INITIAL_SN;
1782 }
1783
1784 ictx->xstorm_ag_context.cdu_reserved =
1785 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
1786 ISCSI_CONNECTION_TYPE);
1787 ictx->ustorm_ag_context.cdu_usage =
1788 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
1789 ISCSI_CONNECTION_TYPE);
1790 return 0;
1791
1792}
1793
1794static int cnic_bnx2x_iscsi_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
1795 u32 num, int *work)
1796{
1797 struct iscsi_kwqe_conn_offload1 *req1;
1798 struct iscsi_kwqe_conn_offload2 *req2;
1799 struct cnic_local *cp = dev->cnic_priv;
Michael Chanfdf24082010-10-13 14:06:47 +00001800 struct cnic_context *ctx;
Michael Chan71034ba2009-10-10 13:46:59 +00001801 struct iscsi_kcqe kcqe;
1802 struct kcqe *cqes[1];
1803 u32 l5_cid;
Michael Chanfdf24082010-10-13 14:06:47 +00001804 int ret = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001805
1806 if (num < 2) {
1807 *work = num;
1808 return -EINVAL;
1809 }
1810
1811 req1 = (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1812 req2 = (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1813 if ((num - 2) < req2->num_additional_wqes) {
1814 *work = num;
1815 return -EINVAL;
1816 }
Joe Perches779bb412010-11-14 17:04:37 +00001817 *work = 2 + req2->num_additional_wqes;
Michael Chan71034ba2009-10-10 13:46:59 +00001818
1819 l5_cid = req1->iscsi_conn_id;
1820 if (l5_cid >= MAX_ISCSI_TBL_SZ)
1821 return -EINVAL;
1822
1823 memset(&kcqe, 0, sizeof(kcqe));
1824 kcqe.op_code = ISCSI_KCQE_OPCODE_OFFLOAD_CONN;
1825 kcqe.iscsi_conn_id = l5_cid;
1826 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
1827
Michael Chanfdf24082010-10-13 14:06:47 +00001828 ctx = &cp->ctx_tbl[l5_cid];
1829 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags)) {
1830 kcqe.completion_status =
1831 ISCSI_KCQE_COMPLETION_STATUS_CID_BUSY;
1832 goto done;
1833 }
1834
Michael Chan71034ba2009-10-10 13:46:59 +00001835 if (atomic_inc_return(&cp->iscsi_conn) > dev->max_iscsi_conn) {
1836 atomic_dec(&cp->iscsi_conn);
Michael Chan71034ba2009-10-10 13:46:59 +00001837 goto done;
1838 }
1839 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
1840 if (ret) {
1841 atomic_dec(&cp->iscsi_conn);
1842 ret = 0;
1843 goto done;
1844 }
1845 ret = cnic_setup_bnx2x_ctx(dev, wqes, num);
1846 if (ret < 0) {
1847 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1848 atomic_dec(&cp->iscsi_conn);
1849 goto done;
1850 }
1851
1852 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001853 kcqe.iscsi_conn_context_id = BNX2X_HW_CID(cp, cp->ctx_tbl[l5_cid].cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001854
1855done:
1856 cqes[0] = (struct kcqe *) &kcqe;
1857 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
Michael Chan23021c22012-01-04 12:12:28 +00001858 return 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001859}
1860
1861
1862static int cnic_bnx2x_iscsi_update(struct cnic_dev *dev, struct kwqe *kwqe)
1863{
1864 struct cnic_local *cp = dev->cnic_priv;
1865 struct iscsi_kwqe_conn_update *req =
1866 (struct iscsi_kwqe_conn_update *) kwqe;
1867 void *data;
1868 union l5cm_specific_data l5_data;
1869 u32 l5_cid, cid = BNX2X_SW_CID(req->context_id);
1870 int ret;
1871
1872 if (cnic_get_l5_cid(cp, cid, &l5_cid) != 0)
1873 return -EINVAL;
1874
1875 data = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
1876 if (!data)
1877 return -ENOMEM;
1878
1879 memcpy(data, kwqe, sizeof(struct kwqe));
1880
1881 ret = cnic_submit_kwqe_16(dev, ISCSI_RAMROD_CMD_ID_UPDATE_CONN,
1882 req->context_id, ISCSI_CONNECTION_TYPE, &l5_data);
1883 return ret;
1884}
1885
Michael Chana2c9e762010-10-13 14:06:46 +00001886static int cnic_bnx2x_destroy_ramrod(struct cnic_dev *dev, u32 l5_cid)
Michael Chan71034ba2009-10-10 13:46:59 +00001887{
1888 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001889 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
Michael Chana2c9e762010-10-13 14:06:46 +00001890 union l5cm_specific_data l5_data;
1891 int ret;
Michael Chan68d7c1a2011-01-05 15:14:13 +00001892 u32 hw_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001893
Michael Chan71034ba2009-10-10 13:46:59 +00001894 init_waitqueue_head(&ctx->waitq);
1895 ctx->wait_cond = 0;
1896 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001897 hw_cid = BNX2X_HW_CID(cp, ctx->cid);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001898
1899 ret = cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
Michael Chan68d7c1a2011-01-05 15:14:13 +00001900 hw_cid, NONE_CONNECTION_TYPE, &l5_data);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001901
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001902 if (ret == 0) {
Michael Chandcc7e3a2011-08-26 09:45:40 +00001903 wait_event_timeout(ctx->waitq, ctx->wait_cond, CNIC_RAMROD_TMO);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001904 if (unlikely(test_bit(CTX_FL_CID_ERROR, &ctx->ctx_flags)))
1905 return -EBUSY;
1906 }
Michael Chan71034ba2009-10-10 13:46:59 +00001907
Michael Chandcc7e3a2011-08-26 09:45:40 +00001908 return 0;
Michael Chana2c9e762010-10-13 14:06:46 +00001909}
1910
1911static int cnic_bnx2x_iscsi_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
1912{
1913 struct cnic_local *cp = dev->cnic_priv;
1914 struct iscsi_kwqe_conn_destroy *req =
1915 (struct iscsi_kwqe_conn_destroy *) kwqe;
1916 u32 l5_cid = req->reserved0;
1917 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1918 int ret = 0;
1919 struct iscsi_kcqe kcqe;
1920 struct kcqe *cqes[1];
1921
1922 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
1923 goto skip_cfc_delete;
1924
Michael Chanfdf24082010-10-13 14:06:47 +00001925 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
1926 unsigned long delta = ctx->timestamp + (2 * HZ) - jiffies;
1927
1928 if (delta > (2 * HZ))
1929 delta = 0;
1930
1931 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
1932 queue_delayed_work(cnic_wq, &cp->delete_task, delta);
1933 goto destroy_reply;
1934 }
Michael Chana2c9e762010-10-13 14:06:46 +00001935
1936 ret = cnic_bnx2x_destroy_ramrod(dev, l5_cid);
1937
Michael Chan71034ba2009-10-10 13:46:59 +00001938skip_cfc_delete:
1939 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1940
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001941 if (!ret) {
1942 atomic_dec(&cp->iscsi_conn);
1943 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
1944 }
Michael Chan71034ba2009-10-10 13:46:59 +00001945
Michael Chanfdf24082010-10-13 14:06:47 +00001946destroy_reply:
Michael Chan71034ba2009-10-10 13:46:59 +00001947 memset(&kcqe, 0, sizeof(kcqe));
1948 kcqe.op_code = ISCSI_KCQE_OPCODE_DESTROY_CONN;
1949 kcqe.iscsi_conn_id = l5_cid;
1950 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1951 kcqe.iscsi_conn_context_id = req->context_id;
1952
1953 cqes[0] = (struct kcqe *) &kcqe;
1954 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1955
Michael Chan23021c22012-01-04 12:12:28 +00001956 return 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001957}
1958
1959static void cnic_init_storm_conn_bufs(struct cnic_dev *dev,
1960 struct l4_kwq_connect_req1 *kwqe1,
1961 struct l4_kwq_connect_req3 *kwqe3,
1962 struct l5cm_active_conn_buffer *conn_buf)
1963{
1964 struct l5cm_conn_addr_params *conn_addr = &conn_buf->conn_addr_buf;
1965 struct l5cm_xstorm_conn_buffer *xstorm_buf =
1966 &conn_buf->xstorm_conn_buffer;
1967 struct l5cm_tstorm_conn_buffer *tstorm_buf =
1968 &conn_buf->tstorm_conn_buffer;
1969 struct regpair context_addr;
1970 u32 cid = BNX2X_SW_CID(kwqe1->cid);
1971 struct in6_addr src_ip, dst_ip;
1972 int i;
1973 u32 *addrp;
1974
1975 addrp = (u32 *) &conn_addr->local_ip_addr;
1976 for (i = 0; i < 4; i++, addrp++)
1977 src_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1978
1979 addrp = (u32 *) &conn_addr->remote_ip_addr;
1980 for (i = 0; i < 4; i++, addrp++)
1981 dst_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1982
1983 cnic_get_bnx2x_ctx(dev, cid, 0, &context_addr);
1984
1985 xstorm_buf->context_addr.hi = context_addr.hi;
1986 xstorm_buf->context_addr.lo = context_addr.lo;
1987 xstorm_buf->mss = 0xffff;
1988 xstorm_buf->rcv_buf = kwqe3->rcv_buf;
1989 if (kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE)
1990 xstorm_buf->params |= L5CM_XSTORM_CONN_BUFFER_NAGLE_ENABLE;
1991 xstorm_buf->pseudo_header_checksum =
1992 swab16(~csum_ipv6_magic(&src_ip, &dst_ip, 0, IPPROTO_TCP, 0));
1993
1994 if (!(kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK))
1995 tstorm_buf->params |=
1996 L5CM_TSTORM_CONN_BUFFER_DELAYED_ACK_ENABLE;
1997 if (kwqe3->ka_timeout) {
1998 tstorm_buf->ka_enable = 1;
1999 tstorm_buf->ka_timeout = kwqe3->ka_timeout;
2000 tstorm_buf->ka_interval = kwqe3->ka_interval;
2001 tstorm_buf->ka_max_probe_count = kwqe3->ka_max_probe_count;
2002 }
Michael Chan71034ba2009-10-10 13:46:59 +00002003 tstorm_buf->max_rt_time = 0xffffffff;
2004}
2005
2006static void cnic_init_bnx2x_mac(struct cnic_dev *dev)
2007{
2008 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00002009 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00002010 u8 *mac = dev->mac_addr;
2011
2012 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002013 XSTORM_ISCSI_LOCAL_MAC_ADDR0_OFFSET(pfid), mac[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00002014 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002015 XSTORM_ISCSI_LOCAL_MAC_ADDR1_OFFSET(pfid), mac[1]);
Michael Chan71034ba2009-10-10 13:46:59 +00002016 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002017 XSTORM_ISCSI_LOCAL_MAC_ADDR2_OFFSET(pfid), mac[2]);
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_ADDR3_OFFSET(pfid), mac[3]);
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_ADDR4_OFFSET(pfid), mac[4]);
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_ADDR5_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00002024
2025 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002026 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00002027 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002028 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00002029 mac[4]);
2030 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002031 TSTORM_ISCSI_TCP_VARS_MID_LOCAL_MAC_ADDR_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00002032 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002033 TSTORM_ISCSI_TCP_VARS_MID_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00002034 mac[2]);
2035 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002036 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[1]);
Michael Chan71034ba2009-10-10 13:46:59 +00002037 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002038 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00002039 mac[0]);
2040}
2041
2042static void cnic_bnx2x_set_tcp_timestamp(struct cnic_dev *dev, int tcp_ts)
2043{
2044 struct cnic_local *cp = dev->cnic_priv;
2045 u8 xstorm_flags = XSTORM_L5CM_TCP_FLAGS_WND_SCL_EN;
2046 u16 tstorm_flags = 0;
2047
2048 if (tcp_ts) {
2049 xstorm_flags |= XSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
2050 tstorm_flags |= TSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
2051 }
2052
2053 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002054 XSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), xstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002055
2056 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002057 TSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), tstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002058}
2059
2060static int cnic_bnx2x_connect(struct cnic_dev *dev, struct kwqe *wqes[],
2061 u32 num, int *work)
2062{
2063 struct cnic_local *cp = dev->cnic_priv;
2064 struct l4_kwq_connect_req1 *kwqe1 =
2065 (struct l4_kwq_connect_req1 *) wqes[0];
2066 struct l4_kwq_connect_req3 *kwqe3;
2067 struct l5cm_active_conn_buffer *conn_buf;
2068 struct l5cm_conn_addr_params *conn_addr;
2069 union l5cm_specific_data l5_data;
2070 u32 l5_cid = kwqe1->pg_cid;
2071 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
2072 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
2073 int ret;
2074
2075 if (num < 2) {
2076 *work = num;
2077 return -EINVAL;
2078 }
2079
2080 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6)
2081 *work = 3;
2082 else
2083 *work = 2;
2084
2085 if (num < *work) {
2086 *work = num;
2087 return -EINVAL;
2088 }
2089
2090 if (sizeof(*conn_buf) > CNIC_KWQ16_DATA_SIZE) {
Joe Perchesddf79b22010-02-17 15:01:54 +00002091 netdev_err(dev->netdev, "conn_buf size too big\n");
Michael Chan71034ba2009-10-10 13:46:59 +00002092 return -ENOMEM;
2093 }
2094 conn_buf = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2095 if (!conn_buf)
2096 return -ENOMEM;
2097
2098 memset(conn_buf, 0, sizeof(*conn_buf));
2099
2100 conn_addr = &conn_buf->conn_addr_buf;
2101 conn_addr->remote_addr_0 = csk->ha[0];
2102 conn_addr->remote_addr_1 = csk->ha[1];
2103 conn_addr->remote_addr_2 = csk->ha[2];
2104 conn_addr->remote_addr_3 = csk->ha[3];
2105 conn_addr->remote_addr_4 = csk->ha[4];
2106 conn_addr->remote_addr_5 = csk->ha[5];
2107
2108 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6) {
2109 struct l4_kwq_connect_req2 *kwqe2 =
2110 (struct l4_kwq_connect_req2 *) wqes[1];
2111
2112 conn_addr->local_ip_addr.ip_addr_hi_hi = kwqe2->src_ip_v6_4;
2113 conn_addr->local_ip_addr.ip_addr_hi_lo = kwqe2->src_ip_v6_3;
2114 conn_addr->local_ip_addr.ip_addr_lo_hi = kwqe2->src_ip_v6_2;
2115
2116 conn_addr->remote_ip_addr.ip_addr_hi_hi = kwqe2->dst_ip_v6_4;
2117 conn_addr->remote_ip_addr.ip_addr_hi_lo = kwqe2->dst_ip_v6_3;
2118 conn_addr->remote_ip_addr.ip_addr_lo_hi = kwqe2->dst_ip_v6_2;
2119 conn_addr->params |= L5CM_CONN_ADDR_PARAMS_IP_VERSION;
2120 }
2121 kwqe3 = (struct l4_kwq_connect_req3 *) wqes[*work - 1];
2122
2123 conn_addr->local_ip_addr.ip_addr_lo_lo = kwqe1->src_ip;
2124 conn_addr->remote_ip_addr.ip_addr_lo_lo = kwqe1->dst_ip;
2125 conn_addr->local_tcp_port = kwqe1->src_port;
2126 conn_addr->remote_tcp_port = kwqe1->dst_port;
2127
2128 conn_addr->pmtu = kwqe3->pmtu;
2129 cnic_init_storm_conn_bufs(dev, kwqe1, kwqe3, conn_buf);
2130
2131 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002132 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(cp->pfid), csk->vlan_id);
Michael Chan71034ba2009-10-10 13:46:59 +00002133
2134 cnic_bnx2x_set_tcp_timestamp(dev,
2135 kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_TIME_STAMP);
2136
2137 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_TCP_CONNECT,
2138 kwqe1->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2139 if (!ret)
Michael Chan6e0dda02010-10-13 14:06:45 +00002140 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002141
2142 return ret;
2143}
2144
2145static int cnic_bnx2x_close(struct cnic_dev *dev, struct kwqe *kwqe)
2146{
2147 struct l4_kwq_close_req *req = (struct l4_kwq_close_req *) kwqe;
2148 union l5cm_specific_data l5_data;
2149 int ret;
2150
2151 memset(&l5_data, 0, sizeof(l5_data));
2152 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_CLOSE,
2153 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2154 return ret;
2155}
2156
2157static int cnic_bnx2x_reset(struct cnic_dev *dev, struct kwqe *kwqe)
2158{
2159 struct l4_kwq_reset_req *req = (struct l4_kwq_reset_req *) kwqe;
2160 union l5cm_specific_data l5_data;
2161 int ret;
2162
2163 memset(&l5_data, 0, sizeof(l5_data));
2164 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_ABORT,
2165 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2166 return ret;
2167}
2168static int cnic_bnx2x_offload_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2169{
2170 struct l4_kwq_offload_pg *req = (struct l4_kwq_offload_pg *) kwqe;
2171 struct l4_kcq kcqe;
2172 struct kcqe *cqes[1];
2173
2174 memset(&kcqe, 0, sizeof(kcqe));
2175 kcqe.pg_host_opaque = req->host_opaque;
2176 kcqe.pg_cid = req->host_opaque;
2177 kcqe.op_code = L4_KCQE_OPCODE_VALUE_OFFLOAD_PG;
2178 cqes[0] = (struct kcqe *) &kcqe;
2179 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2180 return 0;
2181}
2182
2183static int cnic_bnx2x_update_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2184{
2185 struct l4_kwq_update_pg *req = (struct l4_kwq_update_pg *) kwqe;
2186 struct l4_kcq kcqe;
2187 struct kcqe *cqes[1];
2188
2189 memset(&kcqe, 0, sizeof(kcqe));
2190 kcqe.pg_host_opaque = req->pg_host_opaque;
2191 kcqe.pg_cid = req->pg_cid;
2192 kcqe.op_code = L4_KCQE_OPCODE_VALUE_UPDATE_PG;
2193 cqes[0] = (struct kcqe *) &kcqe;
2194 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2195 return 0;
2196}
2197
Michael Chane1928c82010-12-23 07:43:04 +00002198static int cnic_bnx2x_fcoe_stat(struct cnic_dev *dev, struct kwqe *kwqe)
2199{
2200 struct fcoe_kwqe_stat *req;
2201 struct fcoe_stat_ramrod_params *fcoe_stat;
2202 union l5cm_specific_data l5_data;
2203 struct cnic_local *cp = dev->cnic_priv;
2204 int ret;
2205 u32 cid;
2206
2207 req = (struct fcoe_kwqe_stat *) kwqe;
2208 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2209
2210 fcoe_stat = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2211 if (!fcoe_stat)
2212 return -ENOMEM;
2213
2214 memset(fcoe_stat, 0, sizeof(*fcoe_stat));
2215 memcpy(&fcoe_stat->stat_kwqe, req, sizeof(*req));
2216
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002217 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_STAT_FUNC, cid,
Michael Chane1928c82010-12-23 07:43:04 +00002218 FCOE_CONNECTION_TYPE, &l5_data);
2219 return ret;
2220}
2221
2222static int cnic_bnx2x_fcoe_init1(struct cnic_dev *dev, struct kwqe *wqes[],
2223 u32 num, int *work)
2224{
2225 int ret;
2226 struct cnic_local *cp = dev->cnic_priv;
2227 u32 cid;
2228 struct fcoe_init_ramrod_params *fcoe_init;
2229 struct fcoe_kwqe_init1 *req1;
2230 struct fcoe_kwqe_init2 *req2;
2231 struct fcoe_kwqe_init3 *req3;
2232 union l5cm_specific_data l5_data;
2233
2234 if (num < 3) {
2235 *work = num;
2236 return -EINVAL;
2237 }
2238 req1 = (struct fcoe_kwqe_init1 *) wqes[0];
2239 req2 = (struct fcoe_kwqe_init2 *) wqes[1];
2240 req3 = (struct fcoe_kwqe_init3 *) wqes[2];
2241 if (req2->hdr.op_code != FCOE_KWQE_OPCODE_INIT2) {
2242 *work = 1;
2243 return -EINVAL;
2244 }
2245 if (req3->hdr.op_code != FCOE_KWQE_OPCODE_INIT3) {
2246 *work = 2;
2247 return -EINVAL;
2248 }
2249
2250 if (sizeof(*fcoe_init) > CNIC_KWQ16_DATA_SIZE) {
2251 netdev_err(dev->netdev, "fcoe_init size too big\n");
2252 return -ENOMEM;
2253 }
2254 fcoe_init = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2255 if (!fcoe_init)
2256 return -ENOMEM;
2257
2258 memset(fcoe_init, 0, sizeof(*fcoe_init));
2259 memcpy(&fcoe_init->init_kwqe1, req1, sizeof(*req1));
2260 memcpy(&fcoe_init->init_kwqe2, req2, sizeof(*req2));
2261 memcpy(&fcoe_init->init_kwqe3, req3, sizeof(*req3));
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002262 fcoe_init->eq_pbl_base.lo = cp->kcq2.dma.pgtbl_map & 0xffffffff;
2263 fcoe_init->eq_pbl_base.hi = (u64) cp->kcq2.dma.pgtbl_map >> 32;
2264 fcoe_init->eq_pbl_size = cp->kcq2.dma.num_pages;
Michael Chane1928c82010-12-23 07:43:04 +00002265
2266 fcoe_init->sb_num = cp->status_blk_num;
2267 fcoe_init->eq_prod = MAX_KCQ_IDX;
2268 fcoe_init->sb_id = HC_INDEX_FCOE_EQ_CONS;
2269 cp->kcq2.sw_prod_idx = 0;
2270
2271 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002272 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_INIT_FUNC, cid,
Michael Chane1928c82010-12-23 07:43:04 +00002273 FCOE_CONNECTION_TYPE, &l5_data);
2274 *work = 3;
2275 return ret;
2276}
2277
2278static int cnic_bnx2x_fcoe_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
2279 u32 num, int *work)
2280{
2281 int ret = 0;
2282 u32 cid = -1, l5_cid;
2283 struct cnic_local *cp = dev->cnic_priv;
2284 struct fcoe_kwqe_conn_offload1 *req1;
2285 struct fcoe_kwqe_conn_offload2 *req2;
2286 struct fcoe_kwqe_conn_offload3 *req3;
2287 struct fcoe_kwqe_conn_offload4 *req4;
2288 struct fcoe_conn_offload_ramrod_params *fcoe_offload;
2289 struct cnic_context *ctx;
2290 struct fcoe_context *fctx;
2291 struct regpair ctx_addr;
2292 union l5cm_specific_data l5_data;
2293 struct fcoe_kcqe kcqe;
2294 struct kcqe *cqes[1];
2295
2296 if (num < 4) {
2297 *work = num;
2298 return -EINVAL;
2299 }
2300 req1 = (struct fcoe_kwqe_conn_offload1 *) wqes[0];
2301 req2 = (struct fcoe_kwqe_conn_offload2 *) wqes[1];
2302 req3 = (struct fcoe_kwqe_conn_offload3 *) wqes[2];
2303 req4 = (struct fcoe_kwqe_conn_offload4 *) wqes[3];
2304
2305 *work = 4;
2306
2307 l5_cid = req1->fcoe_conn_id;
Michael Chandc219a22011-08-26 09:45:39 +00002308 if (l5_cid >= dev->max_fcoe_conn)
Michael Chane1928c82010-12-23 07:43:04 +00002309 goto err_reply;
2310
2311 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2312
2313 ctx = &cp->ctx_tbl[l5_cid];
2314 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2315 goto err_reply;
2316
2317 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
2318 if (ret) {
2319 ret = 0;
2320 goto err_reply;
2321 }
2322 cid = ctx->cid;
2323
2324 fctx = cnic_get_bnx2x_ctx(dev, cid, 1, &ctx_addr);
2325 if (fctx) {
2326 u32 hw_cid = BNX2X_HW_CID(cp, cid);
2327 u32 val;
2328
2329 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
2330 FCOE_CONNECTION_TYPE);
2331 fctx->xstorm_ag_context.cdu_reserved = val;
2332 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
2333 FCOE_CONNECTION_TYPE);
2334 fctx->ustorm_ag_context.cdu_usage = val;
2335 }
2336 if (sizeof(*fcoe_offload) > CNIC_KWQ16_DATA_SIZE) {
2337 netdev_err(dev->netdev, "fcoe_offload size too big\n");
2338 goto err_reply;
2339 }
2340 fcoe_offload = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2341 if (!fcoe_offload)
2342 goto err_reply;
2343
2344 memset(fcoe_offload, 0, sizeof(*fcoe_offload));
2345 memcpy(&fcoe_offload->offload_kwqe1, req1, sizeof(*req1));
2346 memcpy(&fcoe_offload->offload_kwqe2, req2, sizeof(*req2));
2347 memcpy(&fcoe_offload->offload_kwqe3, req3, sizeof(*req3));
2348 memcpy(&fcoe_offload->offload_kwqe4, req4, sizeof(*req4));
2349
2350 cid = BNX2X_HW_CID(cp, cid);
2351 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_OFFLOAD_CONN, cid,
2352 FCOE_CONNECTION_TYPE, &l5_data);
2353 if (!ret)
2354 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
2355
2356 return ret;
2357
2358err_reply:
2359 if (cid != -1)
2360 cnic_free_bnx2x_conn_resc(dev, l5_cid);
2361
2362 memset(&kcqe, 0, sizeof(kcqe));
2363 kcqe.op_code = FCOE_KCQE_OPCODE_OFFLOAD_CONN;
2364 kcqe.fcoe_conn_id = req1->fcoe_conn_id;
2365 kcqe.completion_status = FCOE_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
2366
2367 cqes[0] = (struct kcqe *) &kcqe;
2368 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2369 return ret;
2370}
2371
2372static int cnic_bnx2x_fcoe_enable(struct cnic_dev *dev, struct kwqe *kwqe)
2373{
2374 struct fcoe_kwqe_conn_enable_disable *req;
2375 struct fcoe_conn_enable_disable_ramrod_params *fcoe_enable;
2376 union l5cm_specific_data l5_data;
2377 int ret;
2378 u32 cid, l5_cid;
2379 struct cnic_local *cp = dev->cnic_priv;
2380
2381 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2382 cid = req->context_id;
2383 l5_cid = req->conn_id + BNX2X_FCOE_L5_CID_BASE;
2384
2385 if (sizeof(*fcoe_enable) > CNIC_KWQ16_DATA_SIZE) {
2386 netdev_err(dev->netdev, "fcoe_enable size too big\n");
2387 return -ENOMEM;
2388 }
2389 fcoe_enable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2390 if (!fcoe_enable)
2391 return -ENOMEM;
2392
2393 memset(fcoe_enable, 0, sizeof(*fcoe_enable));
2394 memcpy(&fcoe_enable->enable_disable_kwqe, req, sizeof(*req));
2395 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_ENABLE_CONN, cid,
2396 FCOE_CONNECTION_TYPE, &l5_data);
2397 return ret;
2398}
2399
2400static int cnic_bnx2x_fcoe_disable(struct cnic_dev *dev, struct kwqe *kwqe)
2401{
2402 struct fcoe_kwqe_conn_enable_disable *req;
2403 struct fcoe_conn_enable_disable_ramrod_params *fcoe_disable;
2404 union l5cm_specific_data l5_data;
2405 int ret;
2406 u32 cid, l5_cid;
2407 struct cnic_local *cp = dev->cnic_priv;
2408
2409 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2410 cid = req->context_id;
2411 l5_cid = req->conn_id;
Michael Chandc219a22011-08-26 09:45:39 +00002412 if (l5_cid >= dev->max_fcoe_conn)
Michael Chane1928c82010-12-23 07:43:04 +00002413 return -EINVAL;
2414
2415 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2416
2417 if (sizeof(*fcoe_disable) > CNIC_KWQ16_DATA_SIZE) {
2418 netdev_err(dev->netdev, "fcoe_disable size too big\n");
2419 return -ENOMEM;
2420 }
2421 fcoe_disable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2422 if (!fcoe_disable)
2423 return -ENOMEM;
2424
2425 memset(fcoe_disable, 0, sizeof(*fcoe_disable));
2426 memcpy(&fcoe_disable->enable_disable_kwqe, req, sizeof(*req));
2427 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DISABLE_CONN, cid,
2428 FCOE_CONNECTION_TYPE, &l5_data);
2429 return ret;
2430}
2431
2432static int cnic_bnx2x_fcoe_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2433{
2434 struct fcoe_kwqe_conn_destroy *req;
2435 union l5cm_specific_data l5_data;
2436 int ret;
2437 u32 cid, l5_cid;
2438 struct cnic_local *cp = dev->cnic_priv;
2439 struct cnic_context *ctx;
2440 struct fcoe_kcqe kcqe;
2441 struct kcqe *cqes[1];
2442
2443 req = (struct fcoe_kwqe_conn_destroy *) kwqe;
2444 cid = req->context_id;
2445 l5_cid = req->conn_id;
Michael Chandc219a22011-08-26 09:45:39 +00002446 if (l5_cid >= dev->max_fcoe_conn)
Michael Chane1928c82010-12-23 07:43:04 +00002447 return -EINVAL;
2448
2449 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2450
2451 ctx = &cp->ctx_tbl[l5_cid];
2452
2453 init_waitqueue_head(&ctx->waitq);
2454 ctx->wait_cond = 0;
2455
Michael Chandcc7e3a2011-08-26 09:45:40 +00002456 memset(&kcqe, 0, sizeof(kcqe));
2457 kcqe.completion_status = FCOE_KCQE_COMPLETION_STATUS_ERROR;
Michael Chane1928c82010-12-23 07:43:04 +00002458 memset(&l5_data, 0, sizeof(l5_data));
2459 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_TERMINATE_CONN, cid,
2460 FCOE_CONNECTION_TYPE, &l5_data);
2461 if (ret == 0) {
Michael Chandcc7e3a2011-08-26 09:45:40 +00002462 wait_event_timeout(ctx->waitq, ctx->wait_cond, CNIC_RAMROD_TMO);
2463 if (ctx->wait_cond)
2464 kcqe.completion_status = 0;
Michael Chane1928c82010-12-23 07:43:04 +00002465 }
2466
Michael Chandcc7e3a2011-08-26 09:45:40 +00002467 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
2468 queue_delayed_work(cnic_wq, &cp->delete_task, msecs_to_jiffies(2000));
2469
Michael Chane1928c82010-12-23 07:43:04 +00002470 kcqe.op_code = FCOE_KCQE_OPCODE_DESTROY_CONN;
2471 kcqe.fcoe_conn_id = req->conn_id;
2472 kcqe.fcoe_conn_context_id = cid;
2473
2474 cqes[0] = (struct kcqe *) &kcqe;
2475 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2476 return ret;
2477}
2478
Michael Chan74e49bb2011-07-20 14:55:23 +00002479static void cnic_bnx2x_delete_wait(struct cnic_dev *dev, u32 start_cid)
2480{
2481 struct cnic_local *cp = dev->cnic_priv;
2482 u32 i;
2483
2484 for (i = start_cid; i < cp->max_cid_space; i++) {
2485 struct cnic_context *ctx = &cp->ctx_tbl[i];
2486 int j;
2487
2488 while (test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
2489 msleep(10);
2490
2491 for (j = 0; j < 5; j++) {
2492 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2493 break;
2494 msleep(20);
2495 }
2496
2497 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2498 netdev_warn(dev->netdev, "CID %x not deleted\n",
2499 ctx->cid);
2500 }
2501}
2502
Michael Chane1928c82010-12-23 07:43:04 +00002503static int cnic_bnx2x_fcoe_fw_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2504{
2505 struct fcoe_kwqe_destroy *req;
2506 union l5cm_specific_data l5_data;
2507 struct cnic_local *cp = dev->cnic_priv;
2508 int ret;
2509 u32 cid;
2510
Michael Chan74e49bb2011-07-20 14:55:23 +00002511 cnic_bnx2x_delete_wait(dev, MAX_ISCSI_TBL_SZ);
2512
Michael Chane1928c82010-12-23 07:43:04 +00002513 req = (struct fcoe_kwqe_destroy *) kwqe;
2514 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2515
2516 memset(&l5_data, 0, sizeof(l5_data));
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002517 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DESTROY_FUNC, cid,
Michael Chane1928c82010-12-23 07:43:04 +00002518 FCOE_CONNECTION_TYPE, &l5_data);
2519 return ret;
2520}
2521
Michael Chan23021c22012-01-04 12:12:28 +00002522static void cnic_bnx2x_kwqe_err(struct cnic_dev *dev, struct kwqe *kwqe)
2523{
2524 struct cnic_local *cp = dev->cnic_priv;
2525 struct kcqe kcqe;
2526 struct kcqe *cqes[1];
2527 u32 cid;
2528 u32 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2529 u32 layer_code = kwqe->kwqe_op_flag & KWQE_LAYER_MASK;
Michael Chan3238a9b2012-02-05 15:24:40 +00002530 u32 kcqe_op;
Michael Chan23021c22012-01-04 12:12:28 +00002531 int ulp_type;
2532
2533 cid = kwqe->kwqe_info0;
2534 memset(&kcqe, 0, sizeof(kcqe));
2535
Michael Chan3238a9b2012-02-05 15:24:40 +00002536 if (layer_code == KWQE_FLAGS_LAYER_MASK_L5_FCOE) {
2537 u32 l5_cid = 0;
2538
2539 ulp_type = CNIC_ULP_FCOE;
2540 if (opcode == FCOE_KWQE_OPCODE_DISABLE_CONN) {
2541 struct fcoe_kwqe_conn_enable_disable *req;
2542
2543 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2544 kcqe_op = FCOE_KCQE_OPCODE_DISABLE_CONN;
2545 cid = req->context_id;
2546 l5_cid = req->conn_id;
2547 } else if (opcode == FCOE_KWQE_OPCODE_DESTROY) {
2548 kcqe_op = FCOE_KCQE_OPCODE_DESTROY_FUNC;
2549 } else {
2550 return;
2551 }
2552 kcqe.kcqe_op_flag = kcqe_op << KCQE_FLAGS_OPCODE_SHIFT;
2553 kcqe.kcqe_op_flag |= KCQE_FLAGS_LAYER_MASK_L5_FCOE;
Michael Chan8ec3e702012-03-21 15:38:34 +00002554 kcqe.kcqe_info1 = FCOE_KCQE_COMPLETION_STATUS_PARITY_ERROR;
Michael Chan3238a9b2012-02-05 15:24:40 +00002555 kcqe.kcqe_info2 = cid;
2556 kcqe.kcqe_info0 = l5_cid;
2557
2558 } else if (layer_code == KWQE_FLAGS_LAYER_MASK_L5_ISCSI) {
Michael Chan23021c22012-01-04 12:12:28 +00002559 ulp_type = CNIC_ULP_ISCSI;
2560 if (opcode == ISCSI_KWQE_OPCODE_UPDATE_CONN)
2561 cid = kwqe->kwqe_info1;
2562
2563 kcqe.kcqe_op_flag = (opcode + 0x10) << KCQE_FLAGS_OPCODE_SHIFT;
2564 kcqe.kcqe_op_flag |= KCQE_FLAGS_LAYER_MASK_L5_ISCSI;
Michael Chan8ec3e702012-03-21 15:38:34 +00002565 kcqe.kcqe_info1 = ISCSI_KCQE_COMPLETION_STATUS_PARITY_ERR;
Michael Chan23021c22012-01-04 12:12:28 +00002566 kcqe.kcqe_info2 = cid;
2567 cnic_get_l5_cid(cp, BNX2X_SW_CID(cid), &kcqe.kcqe_info0);
2568
2569 } else if (layer_code == KWQE_FLAGS_LAYER_MASK_L4) {
2570 struct l4_kcq *l4kcqe = (struct l4_kcq *) &kcqe;
Michael Chan23021c22012-01-04 12:12:28 +00002571
2572 ulp_type = CNIC_ULP_L4;
2573 if (opcode == L4_KWQE_OPCODE_VALUE_CONNECT1)
2574 kcqe_op = L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE;
2575 else if (opcode == L4_KWQE_OPCODE_VALUE_RESET)
2576 kcqe_op = L4_KCQE_OPCODE_VALUE_RESET_COMP;
2577 else if (opcode == L4_KWQE_OPCODE_VALUE_CLOSE)
2578 kcqe_op = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
2579 else
2580 return;
2581
2582 kcqe.kcqe_op_flag = (kcqe_op << KCQE_FLAGS_OPCODE_SHIFT) |
2583 KCQE_FLAGS_LAYER_MASK_L4;
Michael Chan8ec3e702012-03-21 15:38:34 +00002584 l4kcqe->status = L4_KCQE_COMPLETION_STATUS_PARITY_ERROR;
Michael Chan23021c22012-01-04 12:12:28 +00002585 l4kcqe->cid = cid;
2586 cnic_get_l5_cid(cp, BNX2X_SW_CID(cid), &l4kcqe->conn_id);
2587 } else {
2588 return;
2589 }
2590
2591 cqes[0] = (struct kcqe *) &kcqe;
2592 cnic_reply_bnx2x_kcqes(dev, ulp_type, cqes, 1);
2593}
2594
Michael Chane1928c82010-12-23 07:43:04 +00002595static int cnic_submit_bnx2x_iscsi_kwqes(struct cnic_dev *dev,
2596 struct kwqe *wqes[], u32 num_wqes)
Michael Chan71034ba2009-10-10 13:46:59 +00002597{
2598 int i, work, ret;
2599 u32 opcode;
2600 struct kwqe *kwqe;
2601
2602 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2603 return -EAGAIN; /* bnx2 is down */
2604
2605 for (i = 0; i < num_wqes; ) {
2606 kwqe = wqes[i];
2607 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2608 work = 1;
2609
2610 switch (opcode) {
2611 case ISCSI_KWQE_OPCODE_INIT1:
2612 ret = cnic_bnx2x_iscsi_init1(dev, kwqe);
2613 break;
2614 case ISCSI_KWQE_OPCODE_INIT2:
2615 ret = cnic_bnx2x_iscsi_init2(dev, kwqe);
2616 break;
2617 case ISCSI_KWQE_OPCODE_OFFLOAD_CONN1:
2618 ret = cnic_bnx2x_iscsi_ofld1(dev, &wqes[i],
2619 num_wqes - i, &work);
2620 break;
2621 case ISCSI_KWQE_OPCODE_UPDATE_CONN:
2622 ret = cnic_bnx2x_iscsi_update(dev, kwqe);
2623 break;
2624 case ISCSI_KWQE_OPCODE_DESTROY_CONN:
2625 ret = cnic_bnx2x_iscsi_destroy(dev, kwqe);
2626 break;
2627 case L4_KWQE_OPCODE_VALUE_CONNECT1:
2628 ret = cnic_bnx2x_connect(dev, &wqes[i], num_wqes - i,
2629 &work);
2630 break;
2631 case L4_KWQE_OPCODE_VALUE_CLOSE:
2632 ret = cnic_bnx2x_close(dev, kwqe);
2633 break;
2634 case L4_KWQE_OPCODE_VALUE_RESET:
2635 ret = cnic_bnx2x_reset(dev, kwqe);
2636 break;
2637 case L4_KWQE_OPCODE_VALUE_OFFLOAD_PG:
2638 ret = cnic_bnx2x_offload_pg(dev, kwqe);
2639 break;
2640 case L4_KWQE_OPCODE_VALUE_UPDATE_PG:
2641 ret = cnic_bnx2x_update_pg(dev, kwqe);
2642 break;
2643 case L4_KWQE_OPCODE_VALUE_UPLOAD_PG:
2644 ret = 0;
2645 break;
2646 default:
2647 ret = 0;
Joe Perchesddf79b22010-02-17 15:01:54 +00002648 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2649 opcode);
Michael Chan71034ba2009-10-10 13:46:59 +00002650 break;
2651 }
Michael Chan23021c22012-01-04 12:12:28 +00002652 if (ret < 0) {
Joe Perchesddf79b22010-02-17 15:01:54 +00002653 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2654 opcode);
Michael Chan23021c22012-01-04 12:12:28 +00002655
2656 /* Possibly bnx2x parity error, send completion
2657 * to ulp drivers with error code to speed up
2658 * cleanup and reset recovery.
2659 */
2660 if (ret == -EIO || ret == -EAGAIN)
2661 cnic_bnx2x_kwqe_err(dev, kwqe);
2662 }
Michael Chan71034ba2009-10-10 13:46:59 +00002663 i += work;
2664 }
2665 return 0;
2666}
2667
Michael Chane1928c82010-12-23 07:43:04 +00002668static int cnic_submit_bnx2x_fcoe_kwqes(struct cnic_dev *dev,
2669 struct kwqe *wqes[], u32 num_wqes)
2670{
2671 struct cnic_local *cp = dev->cnic_priv;
2672 int i, work, ret;
2673 u32 opcode;
2674 struct kwqe *kwqe;
2675
2676 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2677 return -EAGAIN; /* bnx2 is down */
2678
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002679 if (!BNX2X_CHIP_IS_E2_PLUS(cp->chip_id))
Michael Chane1928c82010-12-23 07:43:04 +00002680 return -EINVAL;
2681
2682 for (i = 0; i < num_wqes; ) {
2683 kwqe = wqes[i];
2684 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2685 work = 1;
2686
2687 switch (opcode) {
2688 case FCOE_KWQE_OPCODE_INIT1:
2689 ret = cnic_bnx2x_fcoe_init1(dev, &wqes[i],
2690 num_wqes - i, &work);
2691 break;
2692 case FCOE_KWQE_OPCODE_OFFLOAD_CONN1:
2693 ret = cnic_bnx2x_fcoe_ofld1(dev, &wqes[i],
2694 num_wqes - i, &work);
2695 break;
2696 case FCOE_KWQE_OPCODE_ENABLE_CONN:
2697 ret = cnic_bnx2x_fcoe_enable(dev, kwqe);
2698 break;
2699 case FCOE_KWQE_OPCODE_DISABLE_CONN:
2700 ret = cnic_bnx2x_fcoe_disable(dev, kwqe);
2701 break;
2702 case FCOE_KWQE_OPCODE_DESTROY_CONN:
2703 ret = cnic_bnx2x_fcoe_destroy(dev, kwqe);
2704 break;
2705 case FCOE_KWQE_OPCODE_DESTROY:
2706 ret = cnic_bnx2x_fcoe_fw_destroy(dev, kwqe);
2707 break;
2708 case FCOE_KWQE_OPCODE_STAT:
2709 ret = cnic_bnx2x_fcoe_stat(dev, kwqe);
2710 break;
2711 default:
2712 ret = 0;
2713 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2714 opcode);
2715 break;
2716 }
Michael Chan3238a9b2012-02-05 15:24:40 +00002717 if (ret < 0) {
Michael Chane1928c82010-12-23 07:43:04 +00002718 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2719 opcode);
Michael Chan3238a9b2012-02-05 15:24:40 +00002720
2721 /* Possibly bnx2x parity error, send completion
2722 * to ulp drivers with error code to speed up
2723 * cleanup and reset recovery.
2724 */
2725 if (ret == -EIO || ret == -EAGAIN)
2726 cnic_bnx2x_kwqe_err(dev, kwqe);
2727 }
Michael Chane1928c82010-12-23 07:43:04 +00002728 i += work;
2729 }
2730 return 0;
2731}
2732
2733static int cnic_submit_bnx2x_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
2734 u32 num_wqes)
2735{
2736 int ret = -EINVAL;
2737 u32 layer_code;
2738
2739 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2740 return -EAGAIN; /* bnx2x is down */
2741
2742 if (!num_wqes)
2743 return 0;
2744
2745 layer_code = wqes[0]->kwqe_op_flag & KWQE_LAYER_MASK;
2746 switch (layer_code) {
2747 case KWQE_FLAGS_LAYER_MASK_L5_ISCSI:
2748 case KWQE_FLAGS_LAYER_MASK_L4:
2749 case KWQE_FLAGS_LAYER_MASK_L2:
2750 ret = cnic_submit_bnx2x_iscsi_kwqes(dev, wqes, num_wqes);
2751 break;
2752
2753 case KWQE_FLAGS_LAYER_MASK_L5_FCOE:
2754 ret = cnic_submit_bnx2x_fcoe_kwqes(dev, wqes, num_wqes);
2755 break;
2756 }
2757 return ret;
2758}
2759
2760static inline u32 cnic_get_kcqe_layer_mask(u32 opflag)
2761{
2762 if (unlikely(KCQE_OPCODE(opflag) == FCOE_RAMROD_CMD_ID_TERMINATE_CONN))
2763 return KCQE_FLAGS_LAYER_MASK_L4;
2764
2765 return opflag & KCQE_FLAGS_LAYER_MASK;
2766}
2767
Michael Chana4636962009-06-08 18:14:43 -07002768static void service_kcqes(struct cnic_dev *dev, int num_cqes)
2769{
2770 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002771 int i, j, comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002772
2773 i = 0;
2774 j = 1;
2775 while (num_cqes) {
2776 struct cnic_ulp_ops *ulp_ops;
2777 int ulp_type;
2778 u32 kcqe_op_flag = cp->completed_kcq[i]->kcqe_op_flag;
Michael Chane1928c82010-12-23 07:43:04 +00002779 u32 kcqe_layer = cnic_get_kcqe_layer_mask(kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002780
2781 if (unlikely(kcqe_op_flag & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002782 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002783
2784 while (j < num_cqes) {
2785 u32 next_op = cp->completed_kcq[i + j]->kcqe_op_flag;
2786
Michael Chane1928c82010-12-23 07:43:04 +00002787 if (cnic_get_kcqe_layer_mask(next_op) != kcqe_layer)
Michael Chana4636962009-06-08 18:14:43 -07002788 break;
2789
2790 if (unlikely(next_op & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002791 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002792 j++;
2793 }
2794
2795 if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_RDMA)
2796 ulp_type = CNIC_ULP_RDMA;
2797 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_ISCSI)
2798 ulp_type = CNIC_ULP_ISCSI;
Michael Chane1928c82010-12-23 07:43:04 +00002799 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_FCOE)
2800 ulp_type = CNIC_ULP_FCOE;
Michael Chana4636962009-06-08 18:14:43 -07002801 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L4)
2802 ulp_type = CNIC_ULP_L4;
2803 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L2)
2804 goto end;
2805 else {
Joe Perchesddf79b22010-02-17 15:01:54 +00002806 netdev_err(dev->netdev, "Unknown type of KCQE(0x%x)\n",
2807 kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002808 goto end;
2809 }
2810
2811 rcu_read_lock();
2812 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
2813 if (likely(ulp_ops)) {
2814 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
2815 cp->completed_kcq + i, j);
2816 }
2817 rcu_read_unlock();
2818end:
2819 num_cqes -= j;
2820 i += j;
2821 j = 1;
2822 }
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002823 if (unlikely(comp))
2824 cnic_spq_completion(dev, DRV_CTL_RET_L5_SPQ_CREDIT_CMD, comp);
Michael Chana4636962009-06-08 18:14:43 -07002825}
2826
Michael Chan644b9d42010-06-24 14:58:40 +00002827static int cnic_get_kcqes(struct cnic_dev *dev, struct kcq_info *info)
Michael Chana4636962009-06-08 18:14:43 -07002828{
2829 struct cnic_local *cp = dev->cnic_priv;
Michael Chan644b9d42010-06-24 14:58:40 +00002830 u16 i, ri, hw_prod, last;
Michael Chana4636962009-06-08 18:14:43 -07002831 struct kcqe *kcqe;
2832 int kcqe_cnt = 0, last_cnt = 0;
2833
Michael Chan644b9d42010-06-24 14:58:40 +00002834 i = ri = last = info->sw_prod_idx;
Michael Chana4636962009-06-08 18:14:43 -07002835 ri &= MAX_KCQ_IDX;
Michael Chan644b9d42010-06-24 14:58:40 +00002836 hw_prod = *info->hw_prod_idx_ptr;
Michael Chan59e51372011-06-14 01:32:38 +00002837 hw_prod = info->hw_idx(hw_prod);
Michael Chana4636962009-06-08 18:14:43 -07002838
2839 while ((i != hw_prod) && (kcqe_cnt < MAX_COMPLETED_KCQE)) {
Michael Chan644b9d42010-06-24 14:58:40 +00002840 kcqe = &info->kcq[KCQ_PG(ri)][KCQ_IDX(ri)];
Michael Chana4636962009-06-08 18:14:43 -07002841 cp->completed_kcq[kcqe_cnt++] = kcqe;
Michael Chan59e51372011-06-14 01:32:38 +00002842 i = info->next_idx(i);
Michael Chana4636962009-06-08 18:14:43 -07002843 ri = i & MAX_KCQ_IDX;
2844 if (likely(!(kcqe->kcqe_op_flag & KCQE_FLAGS_NEXT))) {
2845 last_cnt = kcqe_cnt;
2846 last = i;
2847 }
2848 }
2849
Michael Chan644b9d42010-06-24 14:58:40 +00002850 info->sw_prod_idx = last;
Michael Chana4636962009-06-08 18:14:43 -07002851 return last_cnt;
2852}
2853
Michael Chan48f753d2010-05-18 11:32:53 +00002854static int cnic_l2_completion(struct cnic_local *cp)
2855{
2856 u16 hw_cons, sw_cons;
Michael Chancd801532010-10-13 14:06:49 +00002857 struct cnic_uio_dev *udev = cp->udev;
Michael Chan48f753d2010-05-18 11:32:53 +00002858 union eth_rx_cqe *cqe, *cqe_ring = (union eth_rx_cqe *)
Michael Chancd801532010-10-13 14:06:49 +00002859 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Michael Chan48f753d2010-05-18 11:32:53 +00002860 u32 cmd;
2861 int comp = 0;
2862
2863 if (!test_bit(CNIC_F_BNX2X_CLASS, &cp->dev->flags))
2864 return 0;
2865
2866 hw_cons = *cp->rx_cons_ptr;
2867 if ((hw_cons & BNX2X_MAX_RCQ_DESC_CNT) == BNX2X_MAX_RCQ_DESC_CNT)
2868 hw_cons++;
2869
2870 sw_cons = cp->rx_cons;
2871 while (sw_cons != hw_cons) {
2872 u8 cqe_fp_flags;
2873
2874 cqe = &cqe_ring[sw_cons & BNX2X_MAX_RCQ_DESC_CNT];
2875 cqe_fp_flags = cqe->fast_path_cqe.type_error_flags;
2876 if (cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE) {
2877 cmd = le32_to_cpu(cqe->ramrod_cqe.conn_and_cmd_data);
2878 cmd >>= COMMON_RAMROD_ETH_RX_CQE_CMD_ID_SHIFT;
2879 if (cmd == RAMROD_CMD_ID_ETH_CLIENT_SETUP ||
2880 cmd == RAMROD_CMD_ID_ETH_HALT)
2881 comp++;
2882 }
2883 sw_cons = BNX2X_NEXT_RCQE(sw_cons);
2884 }
2885 return comp;
2886}
2887
Michael Chan86b53602009-10-10 13:46:57 +00002888static void cnic_chk_pkt_rings(struct cnic_local *cp)
Michael Chana4636962009-06-08 18:14:43 -07002889{
Michael Chan541a7812010-10-06 03:17:22 +00002890 u16 rx_cons, tx_cons;
Michael Chan48f753d2010-05-18 11:32:53 +00002891 int comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002892
Michael Chan541a7812010-10-06 03:17:22 +00002893 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
Michael Chan66fee9e2010-06-24 14:58:38 +00002894 return;
2895
Michael Chan541a7812010-10-06 03:17:22 +00002896 rx_cons = *cp->rx_cons_ptr;
2897 tx_cons = *cp->tx_cons_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002898 if (cp->tx_cons != tx_cons || cp->rx_cons != rx_cons) {
Michael Chan48f753d2010-05-18 11:32:53 +00002899 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
2900 comp = cnic_l2_completion(cp);
2901
Michael Chana4636962009-06-08 18:14:43 -07002902 cp->tx_cons = tx_cons;
2903 cp->rx_cons = rx_cons;
Michael Chan71034ba2009-10-10 13:46:59 +00002904
Michael Chancd801532010-10-13 14:06:49 +00002905 if (cp->udev)
2906 uio_event_notify(&cp->udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -07002907 }
Michael Chan48f753d2010-05-18 11:32:53 +00002908 if (comp)
2909 clear_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07002910}
2911
Michael Chanb177a5d52010-06-24 14:58:41 +00002912static u32 cnic_service_bnx2_queues(struct cnic_dev *dev)
Michael Chana4636962009-06-08 18:14:43 -07002913{
Michael Chana4636962009-06-08 18:14:43 -07002914 struct cnic_local *cp = dev->cnic_priv;
Michael Chanb177a5d52010-06-24 14:58:41 +00002915 u32 status_idx = (u16) *cp->kcq1.status_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002916 int kcqe_cnt;
2917
Michael Chan107c3f42011-03-02 13:00:49 +00002918 /* status block index must be read before reading other fields */
2919 rmb();
Michael Chana4636962009-06-08 18:14:43 -07002920 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
2921
Michael Chan644b9d42010-06-24 14:58:40 +00002922 while ((kcqe_cnt = cnic_get_kcqes(dev, &cp->kcq1))) {
Michael Chana4636962009-06-08 18:14:43 -07002923
2924 service_kcqes(dev, kcqe_cnt);
2925
2926 /* Tell compiler that status_blk fields can change. */
2927 barrier();
Michael Chan93736652011-06-08 19:29:32 +00002928 status_idx = (u16) *cp->kcq1.status_idx_ptr;
2929 /* status block index must be read first */
2930 rmb();
2931 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002932 }
2933
Michael Chan644b9d42010-06-24 14:58:40 +00002934 CNIC_WR16(dev, cp->kcq1.io_addr, cp->kcq1.sw_prod_idx);
Michael Chana4636962009-06-08 18:14:43 -07002935
Michael Chan86b53602009-10-10 13:46:57 +00002936 cnic_chk_pkt_rings(cp);
Michael Chanb177a5d52010-06-24 14:58:41 +00002937
Michael Chana4636962009-06-08 18:14:43 -07002938 return status_idx;
2939}
2940
Michael Chanb177a5d52010-06-24 14:58:41 +00002941static int cnic_service_bnx2(void *data, void *status_blk)
2942{
2943 struct cnic_dev *dev = data;
Michael Chanb177a5d52010-06-24 14:58:41 +00002944
Michael Chaneaaa6e92010-12-23 08:38:30 +00002945 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
2946 struct status_block *sblk = status_blk;
2947
2948 return sblk->status_idx;
2949 }
Michael Chanb177a5d52010-06-24 14:58:41 +00002950
2951 return cnic_service_bnx2_queues(dev);
2952}
2953
Michael Chana4636962009-06-08 18:14:43 -07002954static void cnic_service_bnx2_msix(unsigned long data)
2955{
2956 struct cnic_dev *dev = (struct cnic_dev *) data;
2957 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002958
Michael Chanb177a5d52010-06-24 14:58:41 +00002959 cp->last_status_idx = cnic_service_bnx2_queues(dev);
Michael Chana4636962009-06-08 18:14:43 -07002960
Michael Chana4636962009-06-08 18:14:43 -07002961 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
2962 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
2963}
2964
Michael Chan66fee9e2010-06-24 14:58:38 +00002965static void cnic_doirq(struct cnic_dev *dev)
2966{
2967 struct cnic_local *cp = dev->cnic_priv;
Michael Chan66fee9e2010-06-24 14:58:38 +00002968
2969 if (likely(test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
Michael Chaneaaa6e92010-12-23 08:38:30 +00002970 u16 prod = cp->kcq1.sw_prod_idx & MAX_KCQ_IDX;
2971
Michael Chan66fee9e2010-06-24 14:58:38 +00002972 prefetch(cp->status_blk.gen);
Michael Chane6c28892010-06-24 14:58:39 +00002973 prefetch(&cp->kcq1.kcq[KCQ_PG(prod)][KCQ_IDX(prod)]);
Michael Chan66fee9e2010-06-24 14:58:38 +00002974
2975 tasklet_schedule(&cp->cnic_irq_task);
2976 }
2977}
2978
Michael Chana4636962009-06-08 18:14:43 -07002979static irqreturn_t cnic_irq(int irq, void *dev_instance)
2980{
2981 struct cnic_dev *dev = dev_instance;
2982 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002983
2984 if (cp->ack_int)
2985 cp->ack_int(dev);
2986
Michael Chan66fee9e2010-06-24 14:58:38 +00002987 cnic_doirq(dev);
Michael Chana4636962009-06-08 18:14:43 -07002988
2989 return IRQ_HANDLED;
2990}
2991
Michael Chan71034ba2009-10-10 13:46:59 +00002992static inline void cnic_ack_bnx2x_int(struct cnic_dev *dev, u8 id, u8 storm,
2993 u16 index, u8 op, u8 update)
2994{
2995 struct cnic_local *cp = dev->cnic_priv;
2996 u32 hc_addr = (HC_REG_COMMAND_REG + CNIC_PORT(cp) * 32 +
2997 COMMAND_REG_INT_ACK);
2998 struct igu_ack_register igu_ack;
2999
3000 igu_ack.status_block_index = index;
3001 igu_ack.sb_id_and_flags =
3002 ((id << IGU_ACK_REGISTER_STATUS_BLOCK_ID_SHIFT) |
3003 (storm << IGU_ACK_REGISTER_STORM_ID_SHIFT) |
3004 (update << IGU_ACK_REGISTER_UPDATE_INDEX_SHIFT) |
3005 (op << IGU_ACK_REGISTER_INTERRUPT_MODE_SHIFT));
3006
3007 CNIC_WR(dev, hc_addr, (*(u32 *)&igu_ack));
3008}
3009
Michael Chanee87a822010-10-13 14:06:51 +00003010static void cnic_ack_igu_sb(struct cnic_dev *dev, u8 igu_sb_id, u8 segment,
3011 u16 index, u8 op, u8 update)
3012{
3013 struct igu_regular cmd_data;
3014 u32 igu_addr = BAR_IGU_INTMEM + (IGU_CMD_INT_ACK_BASE + igu_sb_id) * 8;
3015
3016 cmd_data.sb_id_and_flags =
3017 (index << IGU_REGULAR_SB_INDEX_SHIFT) |
3018 (segment << IGU_REGULAR_SEGMENT_ACCESS_SHIFT) |
3019 (update << IGU_REGULAR_BUPDATE_SHIFT) |
3020 (op << IGU_REGULAR_ENABLE_INT_SHIFT);
3021
3022
3023 CNIC_WR(dev, igu_addr, cmd_data.sb_id_and_flags);
3024}
3025
Michael Chan71034ba2009-10-10 13:46:59 +00003026static void cnic_ack_bnx2x_msix(struct cnic_dev *dev)
3027{
3028 struct cnic_local *cp = dev->cnic_priv;
3029
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003030 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, CSTORM_ID, 0,
Michael Chan71034ba2009-10-10 13:46:59 +00003031 IGU_INT_DISABLE, 0);
3032}
3033
Michael Chanee87a822010-10-13 14:06:51 +00003034static void cnic_ack_bnx2x_e2_msix(struct cnic_dev *dev)
3035{
3036 struct cnic_local *cp = dev->cnic_priv;
3037
3038 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF, 0,
3039 IGU_INT_DISABLE, 0);
3040}
3041
Michael Chanb177a5d52010-06-24 14:58:41 +00003042static u32 cnic_service_bnx2x_kcq(struct cnic_dev *dev, struct kcq_info *info)
Michael Chan71034ba2009-10-10 13:46:59 +00003043{
Michael Chanb177a5d52010-06-24 14:58:41 +00003044 u32 last_status = *info->status_idx_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00003045 int kcqe_cnt;
3046
Michael Chan107c3f42011-03-02 13:00:49 +00003047 /* status block index must be read before reading the KCQ */
3048 rmb();
Michael Chanb177a5d52010-06-24 14:58:41 +00003049 while ((kcqe_cnt = cnic_get_kcqes(dev, info))) {
Michael Chan71034ba2009-10-10 13:46:59 +00003050
3051 service_kcqes(dev, kcqe_cnt);
3052
3053 /* Tell compiler that sblk fields can change. */
3054 barrier();
Michael Chan71034ba2009-10-10 13:46:59 +00003055
Michael Chanb177a5d52010-06-24 14:58:41 +00003056 last_status = *info->status_idx_ptr;
Michael Chan107c3f42011-03-02 13:00:49 +00003057 /* status block index must be read before reading the KCQ */
3058 rmb();
Michael Chan71034ba2009-10-10 13:46:59 +00003059 }
Michael Chanb177a5d52010-06-24 14:58:41 +00003060 return last_status;
3061}
3062
3063static void cnic_service_bnx2x_bh(unsigned long data)
3064{
3065 struct cnic_dev *dev = (struct cnic_dev *) data;
3066 struct cnic_local *cp = dev->cnic_priv;
Michael Chan0197b082011-03-02 13:00:50 +00003067 u32 status_idx, new_status_idx;
Michael Chanb177a5d52010-06-24 14:58:41 +00003068
3069 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
3070 return;
3071
Michael Chan0197b082011-03-02 13:00:50 +00003072 while (1) {
3073 status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq1);
Michael Chan71034ba2009-10-10 13:46:59 +00003074
Michael Chan0197b082011-03-02 13:00:50 +00003075 CNIC_WR16(dev, cp->kcq1.io_addr,
3076 cp->kcq1.sw_prod_idx + MAX_KCQ_IDX);
Michael Chane21ba412010-12-23 07:43:03 +00003077
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03003078 if (!BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chan0197b082011-03-02 13:00:50 +00003079 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, USTORM_ID,
3080 status_idx, IGU_INT_ENABLE, 1);
3081 break;
3082 }
3083
3084 new_status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq2);
3085
3086 if (new_status_idx != status_idx)
3087 continue;
Michael Chane21ba412010-12-23 07:43:03 +00003088
3089 CNIC_WR16(dev, cp->kcq2.io_addr, cp->kcq2.sw_prod_idx +
3090 MAX_KCQ_IDX);
3091
Michael Chanee87a822010-10-13 14:06:51 +00003092 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF,
3093 status_idx, IGU_INT_ENABLE, 1);
Michael Chan0197b082011-03-02 13:00:50 +00003094
3095 break;
Michael Chane21ba412010-12-23 07:43:03 +00003096 }
Michael Chan71034ba2009-10-10 13:46:59 +00003097}
3098
3099static int cnic_service_bnx2x(void *data, void *status_blk)
3100{
3101 struct cnic_dev *dev = data;
3102 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00003103
Michael Chan66fee9e2010-06-24 14:58:38 +00003104 if (!(cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
3105 cnic_doirq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00003106
Michael Chan66fee9e2010-06-24 14:58:38 +00003107 cnic_chk_pkt_rings(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00003108
3109 return 0;
3110}
3111
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003112static void cnic_ulp_stop_one(struct cnic_local *cp, int if_type)
3113{
3114 struct cnic_ulp_ops *ulp_ops;
3115
3116 if (if_type == CNIC_ULP_ISCSI)
3117 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
3118
3119 mutex_lock(&cnic_lock);
3120 ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
3121 lockdep_is_held(&cnic_lock));
3122 if (!ulp_ops) {
3123 mutex_unlock(&cnic_lock);
3124 return;
3125 }
3126 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
3127 mutex_unlock(&cnic_lock);
3128
3129 if (test_and_clear_bit(ULP_F_START, &cp->ulp_flags[if_type]))
3130 ulp_ops->cnic_stop(cp->ulp_handle[if_type]);
3131
3132 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
3133}
3134
Michael Chana4636962009-06-08 18:14:43 -07003135static void cnic_ulp_stop(struct cnic_dev *dev)
3136{
3137 struct cnic_local *cp = dev->cnic_priv;
3138 int if_type;
3139
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003140 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++)
3141 cnic_ulp_stop_one(cp, if_type);
Michael Chana4636962009-06-08 18:14:43 -07003142}
3143
3144static void cnic_ulp_start(struct cnic_dev *dev)
3145{
3146 struct cnic_local *cp = dev->cnic_priv;
3147 int if_type;
3148
Michael Chana4636962009-06-08 18:14:43 -07003149 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
3150 struct cnic_ulp_ops *ulp_ops;
3151
Michael Chan681dbd72009-08-14 15:49:46 +00003152 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003153 ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
3154 lockdep_is_held(&cnic_lock));
Michael Chan681dbd72009-08-14 15:49:46 +00003155 if (!ulp_ops || !ulp_ops->cnic_start) {
3156 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003157 continue;
Michael Chan681dbd72009-08-14 15:49:46 +00003158 }
3159 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
3160 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003161
3162 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[if_type]))
3163 ulp_ops->cnic_start(cp->ulp_handle[if_type]);
Michael Chan681dbd72009-08-14 15:49:46 +00003164
3165 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
Michael Chana4636962009-06-08 18:14:43 -07003166 }
Michael Chana4636962009-06-08 18:14:43 -07003167}
3168
Barak Witkowski1d187b32011-12-05 22:41:50 +00003169static int cnic_copy_ulp_stats(struct cnic_dev *dev, int ulp_type)
3170{
3171 struct cnic_local *cp = dev->cnic_priv;
3172 struct cnic_ulp_ops *ulp_ops;
3173 int rc;
3174
3175 mutex_lock(&cnic_lock);
3176 ulp_ops = cnic_ulp_tbl_prot(ulp_type);
3177 if (ulp_ops && ulp_ops->cnic_get_stats)
3178 rc = ulp_ops->cnic_get_stats(cp->ulp_handle[ulp_type]);
3179 else
3180 rc = -ENODEV;
3181 mutex_unlock(&cnic_lock);
3182 return rc;
3183}
3184
Michael Chana4636962009-06-08 18:14:43 -07003185static int cnic_ctl(void *data, struct cnic_ctl_info *info)
3186{
3187 struct cnic_dev *dev = data;
Barak Witkowski1d187b32011-12-05 22:41:50 +00003188 int ulp_type = CNIC_ULP_ISCSI;
Michael Chana4636962009-06-08 18:14:43 -07003189
3190 switch (info->cmd) {
3191 case CNIC_CTL_STOP_CMD:
3192 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07003193
3194 cnic_ulp_stop(dev);
3195 cnic_stop_hw(dev);
3196
Michael Chana4636962009-06-08 18:14:43 -07003197 cnic_put(dev);
3198 break;
3199 case CNIC_CTL_START_CMD:
3200 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07003201
3202 if (!cnic_start_hw(dev))
3203 cnic_ulp_start(dev);
3204
Michael Chana4636962009-06-08 18:14:43 -07003205 cnic_put(dev);
3206 break;
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003207 case CNIC_CTL_STOP_ISCSI_CMD: {
3208 struct cnic_local *cp = dev->cnic_priv;
3209 set_bit(CNIC_LCL_FL_STOP_ISCSI, &cp->cnic_local_flags);
3210 queue_delayed_work(cnic_wq, &cp->delete_task, 0);
3211 break;
3212 }
Michael Chan71034ba2009-10-10 13:46:59 +00003213 case CNIC_CTL_COMPLETION_CMD: {
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03003214 struct cnic_ctl_completion *comp = &info->data.comp;
3215 u32 cid = BNX2X_SW_CID(comp->cid);
Michael Chan71034ba2009-10-10 13:46:59 +00003216 u32 l5_cid;
3217 struct cnic_local *cp = dev->cnic_priv;
3218
3219 if (cnic_get_l5_cid(cp, cid, &l5_cid) == 0) {
3220 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3221
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03003222 if (unlikely(comp->error)) {
3223 set_bit(CTX_FL_CID_ERROR, &ctx->ctx_flags);
3224 netdev_err(dev->netdev,
3225 "CID %x CFC delete comp error %x\n",
3226 cid, comp->error);
3227 }
3228
Michael Chan71034ba2009-10-10 13:46:59 +00003229 ctx->wait_cond = 1;
3230 wake_up(&ctx->waitq);
3231 }
3232 break;
3233 }
Barak Witkowski1d187b32011-12-05 22:41:50 +00003234 case CNIC_CTL_FCOE_STATS_GET_CMD:
3235 ulp_type = CNIC_ULP_FCOE;
3236 /* fall through */
3237 case CNIC_CTL_ISCSI_STATS_GET_CMD:
3238 cnic_hold(dev);
3239 cnic_copy_ulp_stats(dev, ulp_type);
3240 cnic_put(dev);
3241 break;
3242
Michael Chana4636962009-06-08 18:14:43 -07003243 default:
3244 return -EINVAL;
3245 }
3246 return 0;
3247}
3248
3249static void cnic_ulp_init(struct cnic_dev *dev)
3250{
3251 int i;
3252 struct cnic_local *cp = dev->cnic_priv;
3253
Michael Chana4636962009-06-08 18:14:43 -07003254 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3255 struct cnic_ulp_ops *ulp_ops;
3256
Michael Chan7fc1ece2009-08-14 15:49:47 +00003257 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003258 ulp_ops = cnic_ulp_tbl_prot(i);
Michael Chan7fc1ece2009-08-14 15:49:47 +00003259 if (!ulp_ops || !ulp_ops->cnic_init) {
3260 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003261 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003262 }
3263 ulp_get(ulp_ops);
3264 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003265
3266 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3267 ulp_ops->cnic_init(dev);
3268
Michael Chan7fc1ece2009-08-14 15:49:47 +00003269 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003270 }
Michael Chana4636962009-06-08 18:14:43 -07003271}
3272
3273static void cnic_ulp_exit(struct cnic_dev *dev)
3274{
3275 int i;
3276 struct cnic_local *cp = dev->cnic_priv;
3277
Michael Chana4636962009-06-08 18:14:43 -07003278 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3279 struct cnic_ulp_ops *ulp_ops;
3280
Michael Chan7fc1ece2009-08-14 15:49:47 +00003281 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003282 ulp_ops = cnic_ulp_tbl_prot(i);
Michael Chan7fc1ece2009-08-14 15:49:47 +00003283 if (!ulp_ops || !ulp_ops->cnic_exit) {
3284 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003285 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003286 }
3287 ulp_get(ulp_ops);
3288 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003289
3290 if (test_and_clear_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3291 ulp_ops->cnic_exit(dev);
3292
Michael Chan7fc1ece2009-08-14 15:49:47 +00003293 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003294 }
Michael Chana4636962009-06-08 18:14:43 -07003295}
3296
3297static int cnic_cm_offload_pg(struct cnic_sock *csk)
3298{
3299 struct cnic_dev *dev = csk->dev;
3300 struct l4_kwq_offload_pg *l4kwqe;
3301 struct kwqe *wqes[1];
3302
3303 l4kwqe = (struct l4_kwq_offload_pg *) &csk->kwqe1;
3304 memset(l4kwqe, 0, sizeof(*l4kwqe));
3305 wqes[0] = (struct kwqe *) l4kwqe;
3306
3307 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_OFFLOAD_PG;
3308 l4kwqe->flags =
3309 L4_LAYER_CODE << L4_KWQ_OFFLOAD_PG_LAYER_CODE_SHIFT;
3310 l4kwqe->l2hdr_nbytes = ETH_HLEN;
3311
3312 l4kwqe->da0 = csk->ha[0];
3313 l4kwqe->da1 = csk->ha[1];
3314 l4kwqe->da2 = csk->ha[2];
3315 l4kwqe->da3 = csk->ha[3];
3316 l4kwqe->da4 = csk->ha[4];
3317 l4kwqe->da5 = csk->ha[5];
3318
3319 l4kwqe->sa0 = dev->mac_addr[0];
3320 l4kwqe->sa1 = dev->mac_addr[1];
3321 l4kwqe->sa2 = dev->mac_addr[2];
3322 l4kwqe->sa3 = dev->mac_addr[3];
3323 l4kwqe->sa4 = dev->mac_addr[4];
3324 l4kwqe->sa5 = dev->mac_addr[5];
3325
3326 l4kwqe->etype = ETH_P_IP;
Eddie Waia9736c02010-02-24 14:42:04 +00003327 l4kwqe->ipid_start = DEF_IPID_START;
Michael Chana4636962009-06-08 18:14:43 -07003328 l4kwqe->host_opaque = csk->l5_cid;
3329
3330 if (csk->vlan_id) {
3331 l4kwqe->pg_flags |= L4_KWQ_OFFLOAD_PG_VLAN_TAGGING;
3332 l4kwqe->vlan_tag = csk->vlan_id;
3333 l4kwqe->l2hdr_nbytes += 4;
3334 }
3335
3336 return dev->submit_kwqes(dev, wqes, 1);
3337}
3338
3339static int cnic_cm_update_pg(struct cnic_sock *csk)
3340{
3341 struct cnic_dev *dev = csk->dev;
3342 struct l4_kwq_update_pg *l4kwqe;
3343 struct kwqe *wqes[1];
3344
3345 l4kwqe = (struct l4_kwq_update_pg *) &csk->kwqe1;
3346 memset(l4kwqe, 0, sizeof(*l4kwqe));
3347 wqes[0] = (struct kwqe *) l4kwqe;
3348
3349 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPDATE_PG;
3350 l4kwqe->flags =
3351 L4_LAYER_CODE << L4_KWQ_UPDATE_PG_LAYER_CODE_SHIFT;
3352 l4kwqe->pg_cid = csk->pg_cid;
3353
3354 l4kwqe->da0 = csk->ha[0];
3355 l4kwqe->da1 = csk->ha[1];
3356 l4kwqe->da2 = csk->ha[2];
3357 l4kwqe->da3 = csk->ha[3];
3358 l4kwqe->da4 = csk->ha[4];
3359 l4kwqe->da5 = csk->ha[5];
3360
3361 l4kwqe->pg_host_opaque = csk->l5_cid;
3362 l4kwqe->pg_valids = L4_KWQ_UPDATE_PG_VALIDS_DA;
3363
3364 return dev->submit_kwqes(dev, wqes, 1);
3365}
3366
3367static int cnic_cm_upload_pg(struct cnic_sock *csk)
3368{
3369 struct cnic_dev *dev = csk->dev;
3370 struct l4_kwq_upload *l4kwqe;
3371 struct kwqe *wqes[1];
3372
3373 l4kwqe = (struct l4_kwq_upload *) &csk->kwqe1;
3374 memset(l4kwqe, 0, sizeof(*l4kwqe));
3375 wqes[0] = (struct kwqe *) l4kwqe;
3376
3377 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPLOAD_PG;
3378 l4kwqe->flags =
3379 L4_LAYER_CODE << L4_KWQ_UPLOAD_LAYER_CODE_SHIFT;
3380 l4kwqe->cid = csk->pg_cid;
3381
3382 return dev->submit_kwqes(dev, wqes, 1);
3383}
3384
3385static int cnic_cm_conn_req(struct cnic_sock *csk)
3386{
3387 struct cnic_dev *dev = csk->dev;
3388 struct l4_kwq_connect_req1 *l4kwqe1;
3389 struct l4_kwq_connect_req2 *l4kwqe2;
3390 struct l4_kwq_connect_req3 *l4kwqe3;
3391 struct kwqe *wqes[3];
3392 u8 tcp_flags = 0;
3393 int num_wqes = 2;
3394
3395 l4kwqe1 = (struct l4_kwq_connect_req1 *) &csk->kwqe1;
3396 l4kwqe2 = (struct l4_kwq_connect_req2 *) &csk->kwqe2;
3397 l4kwqe3 = (struct l4_kwq_connect_req3 *) &csk->kwqe3;
3398 memset(l4kwqe1, 0, sizeof(*l4kwqe1));
3399 memset(l4kwqe2, 0, sizeof(*l4kwqe2));
3400 memset(l4kwqe3, 0, sizeof(*l4kwqe3));
3401
3402 l4kwqe3->op_code = L4_KWQE_OPCODE_VALUE_CONNECT3;
3403 l4kwqe3->flags =
3404 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ3_LAYER_CODE_SHIFT;
3405 l4kwqe3->ka_timeout = csk->ka_timeout;
3406 l4kwqe3->ka_interval = csk->ka_interval;
3407 l4kwqe3->ka_max_probe_count = csk->ka_max_probe_count;
3408 l4kwqe3->tos = csk->tos;
3409 l4kwqe3->ttl = csk->ttl;
3410 l4kwqe3->snd_seq_scale = csk->snd_seq_scale;
3411 l4kwqe3->pmtu = csk->mtu;
3412 l4kwqe3->rcv_buf = csk->rcv_buf;
3413 l4kwqe3->snd_buf = csk->snd_buf;
3414 l4kwqe3->seed = csk->seed;
3415
3416 wqes[0] = (struct kwqe *) l4kwqe1;
3417 if (test_bit(SK_F_IPV6, &csk->flags)) {
3418 wqes[1] = (struct kwqe *) l4kwqe2;
3419 wqes[2] = (struct kwqe *) l4kwqe3;
3420 num_wqes = 3;
3421
3422 l4kwqe1->conn_flags = L4_KWQ_CONNECT_REQ1_IP_V6;
3423 l4kwqe2->op_code = L4_KWQE_OPCODE_VALUE_CONNECT2;
3424 l4kwqe2->flags =
3425 L4_KWQ_CONNECT_REQ2_LINKED_WITH_NEXT |
3426 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ2_LAYER_CODE_SHIFT;
3427 l4kwqe2->src_ip_v6_2 = be32_to_cpu(csk->src_ip[1]);
3428 l4kwqe2->src_ip_v6_3 = be32_to_cpu(csk->src_ip[2]);
3429 l4kwqe2->src_ip_v6_4 = be32_to_cpu(csk->src_ip[3]);
3430 l4kwqe2->dst_ip_v6_2 = be32_to_cpu(csk->dst_ip[1]);
3431 l4kwqe2->dst_ip_v6_3 = be32_to_cpu(csk->dst_ip[2]);
3432 l4kwqe2->dst_ip_v6_4 = be32_to_cpu(csk->dst_ip[3]);
3433 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct ipv6hdr) -
3434 sizeof(struct tcphdr);
3435 } else {
3436 wqes[1] = (struct kwqe *) l4kwqe3;
3437 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct iphdr) -
3438 sizeof(struct tcphdr);
3439 }
3440
3441 l4kwqe1->op_code = L4_KWQE_OPCODE_VALUE_CONNECT1;
3442 l4kwqe1->flags =
3443 (L4_LAYER_CODE << L4_KWQ_CONNECT_REQ1_LAYER_CODE_SHIFT) |
3444 L4_KWQ_CONNECT_REQ3_LINKED_WITH_NEXT;
3445 l4kwqe1->cid = csk->cid;
3446 l4kwqe1->pg_cid = csk->pg_cid;
3447 l4kwqe1->src_ip = be32_to_cpu(csk->src_ip[0]);
3448 l4kwqe1->dst_ip = be32_to_cpu(csk->dst_ip[0]);
3449 l4kwqe1->src_port = be16_to_cpu(csk->src_port);
3450 l4kwqe1->dst_port = be16_to_cpu(csk->dst_port);
3451 if (csk->tcp_flags & SK_TCP_NO_DELAY_ACK)
3452 tcp_flags |= L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK;
3453 if (csk->tcp_flags & SK_TCP_KEEP_ALIVE)
3454 tcp_flags |= L4_KWQ_CONNECT_REQ1_KEEP_ALIVE;
3455 if (csk->tcp_flags & SK_TCP_NAGLE)
3456 tcp_flags |= L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE;
3457 if (csk->tcp_flags & SK_TCP_TIMESTAMP)
3458 tcp_flags |= L4_KWQ_CONNECT_REQ1_TIME_STAMP;
3459 if (csk->tcp_flags & SK_TCP_SACK)
3460 tcp_flags |= L4_KWQ_CONNECT_REQ1_SACK;
3461 if (csk->tcp_flags & SK_TCP_SEG_SCALING)
3462 tcp_flags |= L4_KWQ_CONNECT_REQ1_SEG_SCALING;
3463
3464 l4kwqe1->tcp_flags = tcp_flags;
3465
3466 return dev->submit_kwqes(dev, wqes, num_wqes);
3467}
3468
3469static int cnic_cm_close_req(struct cnic_sock *csk)
3470{
3471 struct cnic_dev *dev = csk->dev;
3472 struct l4_kwq_close_req *l4kwqe;
3473 struct kwqe *wqes[1];
3474
3475 l4kwqe = (struct l4_kwq_close_req *) &csk->kwqe2;
3476 memset(l4kwqe, 0, sizeof(*l4kwqe));
3477 wqes[0] = (struct kwqe *) l4kwqe;
3478
3479 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_CLOSE;
3480 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_CLOSE_REQ_LAYER_CODE_SHIFT;
3481 l4kwqe->cid = csk->cid;
3482
3483 return dev->submit_kwqes(dev, wqes, 1);
3484}
3485
3486static int cnic_cm_abort_req(struct cnic_sock *csk)
3487{
3488 struct cnic_dev *dev = csk->dev;
3489 struct l4_kwq_reset_req *l4kwqe;
3490 struct kwqe *wqes[1];
3491
3492 l4kwqe = (struct l4_kwq_reset_req *) &csk->kwqe2;
3493 memset(l4kwqe, 0, sizeof(*l4kwqe));
3494 wqes[0] = (struct kwqe *) l4kwqe;
3495
3496 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_RESET;
3497 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_RESET_REQ_LAYER_CODE_SHIFT;
3498 l4kwqe->cid = csk->cid;
3499
3500 return dev->submit_kwqes(dev, wqes, 1);
3501}
3502
3503static int cnic_cm_create(struct cnic_dev *dev, int ulp_type, u32 cid,
3504 u32 l5_cid, struct cnic_sock **csk, void *context)
3505{
3506 struct cnic_local *cp = dev->cnic_priv;
3507 struct cnic_sock *csk1;
3508
3509 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3510 return -EINVAL;
3511
Michael Chanfdf24082010-10-13 14:06:47 +00003512 if (cp->ctx_tbl) {
3513 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3514
3515 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
3516 return -EAGAIN;
3517 }
3518
Michael Chana4636962009-06-08 18:14:43 -07003519 csk1 = &cp->csk_tbl[l5_cid];
3520 if (atomic_read(&csk1->ref_count))
3521 return -EAGAIN;
3522
3523 if (test_and_set_bit(SK_F_INUSE, &csk1->flags))
3524 return -EBUSY;
3525
3526 csk1->dev = dev;
3527 csk1->cid = cid;
3528 csk1->l5_cid = l5_cid;
3529 csk1->ulp_type = ulp_type;
3530 csk1->context = context;
3531
3532 csk1->ka_timeout = DEF_KA_TIMEOUT;
3533 csk1->ka_interval = DEF_KA_INTERVAL;
3534 csk1->ka_max_probe_count = DEF_KA_MAX_PROBE_COUNT;
3535 csk1->tos = DEF_TOS;
3536 csk1->ttl = DEF_TTL;
3537 csk1->snd_seq_scale = DEF_SND_SEQ_SCALE;
3538 csk1->rcv_buf = DEF_RCV_BUF;
3539 csk1->snd_buf = DEF_SND_BUF;
3540 csk1->seed = DEF_SEED;
3541
3542 *csk = csk1;
3543 return 0;
3544}
3545
3546static void cnic_cm_cleanup(struct cnic_sock *csk)
3547{
3548 if (csk->src_port) {
3549 struct cnic_dev *dev = csk->dev;
3550 struct cnic_local *cp = dev->cnic_priv;
3551
Michael Chan9b093362010-12-23 07:42:56 +00003552 cnic_free_id(&cp->csk_port_tbl, be16_to_cpu(csk->src_port));
Michael Chana4636962009-06-08 18:14:43 -07003553 csk->src_port = 0;
3554 }
3555}
3556
3557static void cnic_close_conn(struct cnic_sock *csk)
3558{
3559 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags)) {
3560 cnic_cm_upload_pg(csk);
3561 clear_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3562 }
3563 cnic_cm_cleanup(csk);
3564}
3565
3566static int cnic_cm_destroy(struct cnic_sock *csk)
3567{
3568 if (!cnic_in_use(csk))
3569 return -EINVAL;
3570
3571 csk_hold(csk);
3572 clear_bit(SK_F_INUSE, &csk->flags);
3573 smp_mb__after_clear_bit();
3574 while (atomic_read(&csk->ref_count) != 1)
3575 msleep(1);
3576 cnic_cm_cleanup(csk);
3577
3578 csk->flags = 0;
3579 csk_put(csk);
3580 return 0;
3581}
3582
3583static inline u16 cnic_get_vlan(struct net_device *dev,
3584 struct net_device **vlan_dev)
3585{
3586 if (dev->priv_flags & IFF_802_1Q_VLAN) {
3587 *vlan_dev = vlan_dev_real_dev(dev);
3588 return vlan_dev_vlan_id(dev);
3589 }
3590 *vlan_dev = dev;
3591 return 0;
3592}
3593
3594static int cnic_get_v4_route(struct sockaddr_in *dst_addr,
3595 struct dst_entry **dst)
3596{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003597#if defined(CONFIG_INET)
Michael Chana4636962009-06-08 18:14:43 -07003598 struct rtable *rt;
3599
David S. Miller78fbfd82011-03-12 00:00:52 -05003600 rt = ip_route_output(&init_net, dst_addr->sin_addr.s_addr, 0, 0, 0);
3601 if (!IS_ERR(rt)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07003602 *dst = &rt->dst;
David S. Miller78fbfd82011-03-12 00:00:52 -05003603 return 0;
3604 }
3605 return PTR_ERR(rt);
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003606#else
3607 return -ENETUNREACH;
3608#endif
Michael Chana4636962009-06-08 18:14:43 -07003609}
3610
3611static int cnic_get_v6_route(struct sockaddr_in6 *dst_addr,
3612 struct dst_entry **dst)
3613{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003614#if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
David S. Miller4c9483b2011-03-12 16:22:43 -05003615 struct flowi6 fl6;
Michael Chana4636962009-06-08 18:14:43 -07003616
David S. Miller4c9483b2011-03-12 16:22:43 -05003617 memset(&fl6, 0, sizeof(fl6));
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003618 fl6.daddr = dst_addr->sin6_addr;
David S. Miller4c9483b2011-03-12 16:22:43 -05003619 if (ipv6_addr_type(&fl6.daddr) & IPV6_ADDR_LINKLOCAL)
3620 fl6.flowi6_oif = dst_addr->sin6_scope_id;
Michael Chana4636962009-06-08 18:14:43 -07003621
David S. Miller4c9483b2011-03-12 16:22:43 -05003622 *dst = ip6_route_output(&init_net, NULL, &fl6);
RongQing.Li05417432012-02-21 22:10:50 +00003623 if ((*dst)->error) {
3624 dst_release(*dst);
3625 *dst = NULL;
3626 return -ENETUNREACH;
3627 } else
Michael Chana4636962009-06-08 18:14:43 -07003628 return 0;
3629#endif
3630
3631 return -ENETUNREACH;
3632}
3633
3634static struct cnic_dev *cnic_cm_select_dev(struct sockaddr_in *dst_addr,
3635 int ulp_type)
3636{
3637 struct cnic_dev *dev = NULL;
3638 struct dst_entry *dst;
3639 struct net_device *netdev = NULL;
3640 int err = -ENETUNREACH;
3641
3642 if (dst_addr->sin_family == AF_INET)
3643 err = cnic_get_v4_route(dst_addr, &dst);
3644 else if (dst_addr->sin_family == AF_INET6) {
3645 struct sockaddr_in6 *dst_addr6 =
3646 (struct sockaddr_in6 *) dst_addr;
3647
3648 err = cnic_get_v6_route(dst_addr6, &dst);
3649 } else
3650 return NULL;
3651
3652 if (err)
3653 return NULL;
3654
3655 if (!dst->dev)
3656 goto done;
3657
3658 cnic_get_vlan(dst->dev, &netdev);
3659
3660 dev = cnic_from_netdev(netdev);
3661
3662done:
3663 dst_release(dst);
3664 if (dev)
3665 cnic_put(dev);
3666 return dev;
3667}
3668
3669static int cnic_resolve_addr(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3670{
3671 struct cnic_dev *dev = csk->dev;
3672 struct cnic_local *cp = dev->cnic_priv;
3673
3674 return cnic_send_nlmsg(cp, ISCSI_KEVENT_PATH_REQ, csk);
3675}
3676
3677static int cnic_get_route(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3678{
3679 struct cnic_dev *dev = csk->dev;
3680 struct cnic_local *cp = dev->cnic_priv;
Michael Chanc76284a2010-02-24 14:42:07 +00003681 int is_v6, rc = 0;
3682 struct dst_entry *dst = NULL;
Michael Chana4636962009-06-08 18:14:43 -07003683 struct net_device *realdev;
Michael Chan9b093362010-12-23 07:42:56 +00003684 __be16 local_port;
3685 u32 port_id;
Michael Chana4636962009-06-08 18:14:43 -07003686
3687 if (saddr->local.v6.sin6_family == AF_INET6 &&
3688 saddr->remote.v6.sin6_family == AF_INET6)
3689 is_v6 = 1;
3690 else if (saddr->local.v4.sin_family == AF_INET &&
3691 saddr->remote.v4.sin_family == AF_INET)
3692 is_v6 = 0;
3693 else
3694 return -EINVAL;
3695
3696 clear_bit(SK_F_IPV6, &csk->flags);
3697
3698 if (is_v6) {
Michael Chana4636962009-06-08 18:14:43 -07003699 set_bit(SK_F_IPV6, &csk->flags);
Michael Chanc76284a2010-02-24 14:42:07 +00003700 cnic_get_v6_route(&saddr->remote.v6, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003701
3702 memcpy(&csk->dst_ip[0], &saddr->remote.v6.sin6_addr,
3703 sizeof(struct in6_addr));
3704 csk->dst_port = saddr->remote.v6.sin6_port;
3705 local_port = saddr->local.v6.sin6_port;
Michael Chana4636962009-06-08 18:14:43 -07003706
3707 } else {
Michael Chanc76284a2010-02-24 14:42:07 +00003708 cnic_get_v4_route(&saddr->remote.v4, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003709
3710 csk->dst_ip[0] = saddr->remote.v4.sin_addr.s_addr;
3711 csk->dst_port = saddr->remote.v4.sin_port;
3712 local_port = saddr->local.v4.sin_port;
3713 }
3714
Michael Chanc76284a2010-02-24 14:42:07 +00003715 csk->vlan_id = 0;
3716 csk->mtu = dev->netdev->mtu;
3717 if (dst && dst->dev) {
3718 u16 vlan = cnic_get_vlan(dst->dev, &realdev);
3719 if (realdev == dev->netdev) {
3720 csk->vlan_id = vlan;
3721 csk->mtu = dst_mtu(dst);
3722 }
3723 }
Michael Chana4636962009-06-08 18:14:43 -07003724
Michael Chan9b093362010-12-23 07:42:56 +00003725 port_id = be16_to_cpu(local_port);
3726 if (port_id >= CNIC_LOCAL_PORT_MIN &&
3727 port_id < CNIC_LOCAL_PORT_MAX) {
3728 if (cnic_alloc_id(&cp->csk_port_tbl, port_id))
3729 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003730 } else
Michael Chan9b093362010-12-23 07:42:56 +00003731 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003732
Michael Chan9b093362010-12-23 07:42:56 +00003733 if (!port_id) {
3734 port_id = cnic_alloc_new_id(&cp->csk_port_tbl);
3735 if (port_id == -1) {
Michael Chana4636962009-06-08 18:14:43 -07003736 rc = -ENOMEM;
3737 goto err_out;
3738 }
Michael Chan9b093362010-12-23 07:42:56 +00003739 local_port = cpu_to_be16(port_id);
Michael Chana4636962009-06-08 18:14:43 -07003740 }
3741 csk->src_port = local_port;
3742
Michael Chana4636962009-06-08 18:14:43 -07003743err_out:
3744 dst_release(dst);
3745 return rc;
3746}
3747
3748static void cnic_init_csk_state(struct cnic_sock *csk)
3749{
3750 csk->state = 0;
3751 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3752 clear_bit(SK_F_CLOSING, &csk->flags);
3753}
3754
3755static int cnic_cm_connect(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3756{
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003757 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07003758 int err = 0;
3759
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003760 if (cp->ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI)
3761 return -EOPNOTSUPP;
3762
Michael Chana4636962009-06-08 18:14:43 -07003763 if (!cnic_in_use(csk))
3764 return -EINVAL;
3765
3766 if (test_and_set_bit(SK_F_CONNECT_START, &csk->flags))
3767 return -EINVAL;
3768
3769 cnic_init_csk_state(csk);
3770
3771 err = cnic_get_route(csk, saddr);
3772 if (err)
3773 goto err_out;
3774
3775 err = cnic_resolve_addr(csk, saddr);
3776 if (!err)
3777 return 0;
3778
3779err_out:
3780 clear_bit(SK_F_CONNECT_START, &csk->flags);
3781 return err;
3782}
3783
3784static int cnic_cm_abort(struct cnic_sock *csk)
3785{
3786 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chan7b34a462010-06-15 08:57:03 +00003787 u32 opcode = L4_KCQE_OPCODE_VALUE_RESET_COMP;
Michael Chana4636962009-06-08 18:14:43 -07003788
3789 if (!cnic_in_use(csk))
3790 return -EINVAL;
3791
3792 if (cnic_abort_prep(csk))
3793 return cnic_cm_abort_req(csk);
3794
3795 /* Getting here means that we haven't started connect, or
3796 * connect was not successful.
3797 */
3798
Michael Chana4636962009-06-08 18:14:43 -07003799 cp->close_conn(csk, opcode);
Michael Chan7b34a462010-06-15 08:57:03 +00003800 if (csk->state != opcode)
3801 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003802
3803 return 0;
3804}
3805
3806static int cnic_cm_close(struct cnic_sock *csk)
3807{
3808 if (!cnic_in_use(csk))
3809 return -EINVAL;
3810
3811 if (cnic_close_prep(csk)) {
3812 csk->state = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
3813 return cnic_cm_close_req(csk);
Michael Chaned99daa52010-06-15 08:57:00 +00003814 } else {
3815 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003816 }
3817 return 0;
3818}
3819
3820static void cnic_cm_upcall(struct cnic_local *cp, struct cnic_sock *csk,
3821 u8 opcode)
3822{
3823 struct cnic_ulp_ops *ulp_ops;
3824 int ulp_type = csk->ulp_type;
3825
3826 rcu_read_lock();
3827 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
3828 if (ulp_ops) {
3829 if (opcode == L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE)
3830 ulp_ops->cm_connect_complete(csk);
3831 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3832 ulp_ops->cm_close_complete(csk);
3833 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED)
3834 ulp_ops->cm_remote_abort(csk);
3835 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_COMP)
3836 ulp_ops->cm_abort_complete(csk);
3837 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED)
3838 ulp_ops->cm_remote_close(csk);
3839 }
3840 rcu_read_unlock();
3841}
3842
3843static int cnic_cm_set_pg(struct cnic_sock *csk)
3844{
3845 if (cnic_offld_prep(csk)) {
3846 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3847 cnic_cm_update_pg(csk);
3848 else
3849 cnic_cm_offload_pg(csk);
3850 }
3851 return 0;
3852}
3853
3854static void cnic_cm_process_offld_pg(struct cnic_dev *dev, struct l4_kcq *kcqe)
3855{
3856 struct cnic_local *cp = dev->cnic_priv;
3857 u32 l5_cid = kcqe->pg_host_opaque;
3858 u8 opcode = kcqe->op_code;
3859 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
3860
3861 csk_hold(csk);
3862 if (!cnic_in_use(csk))
3863 goto done;
3864
3865 if (opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3866 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3867 goto done;
3868 }
Eddie Waia9736c02010-02-24 14:42:04 +00003869 /* Possible PG kcqe status: SUCCESS, OFFLOADED_PG, or CTX_ALLOC_FAIL */
3870 if (kcqe->status == L4_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAIL) {
3871 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3872 cnic_cm_upcall(cp, csk,
3873 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3874 goto done;
3875 }
3876
Michael Chana4636962009-06-08 18:14:43 -07003877 csk->pg_cid = kcqe->pg_cid;
3878 set_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3879 cnic_cm_conn_req(csk);
3880
3881done:
3882 csk_put(csk);
3883}
3884
Michael Chane1928c82010-12-23 07:43:04 +00003885static void cnic_process_fcoe_term_conn(struct cnic_dev *dev, struct kcqe *kcqe)
3886{
3887 struct cnic_local *cp = dev->cnic_priv;
3888 struct fcoe_kcqe *fc_kcqe = (struct fcoe_kcqe *) kcqe;
3889 u32 l5_cid = fc_kcqe->fcoe_conn_id + BNX2X_FCOE_L5_CID_BASE;
3890 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3891
3892 ctx->timestamp = jiffies;
3893 ctx->wait_cond = 1;
3894 wake_up(&ctx->waitq);
3895}
3896
Michael Chana4636962009-06-08 18:14:43 -07003897static void cnic_cm_process_kcqe(struct cnic_dev *dev, struct kcqe *kcqe)
3898{
3899 struct cnic_local *cp = dev->cnic_priv;
3900 struct l4_kcq *l4kcqe = (struct l4_kcq *) kcqe;
3901 u8 opcode = l4kcqe->op_code;
3902 u32 l5_cid;
3903 struct cnic_sock *csk;
3904
Michael Chane1928c82010-12-23 07:43:04 +00003905 if (opcode == FCOE_RAMROD_CMD_ID_TERMINATE_CONN) {
3906 cnic_process_fcoe_term_conn(dev, kcqe);
3907 return;
3908 }
Michael Chana4636962009-06-08 18:14:43 -07003909 if (opcode == L4_KCQE_OPCODE_VALUE_OFFLOAD_PG ||
3910 opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3911 cnic_cm_process_offld_pg(dev, l4kcqe);
3912 return;
3913 }
3914
3915 l5_cid = l4kcqe->conn_id;
3916 if (opcode & 0x80)
3917 l5_cid = l4kcqe->cid;
3918 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3919 return;
3920
3921 csk = &cp->csk_tbl[l5_cid];
3922 csk_hold(csk);
3923
3924 if (!cnic_in_use(csk)) {
3925 csk_put(csk);
3926 return;
3927 }
3928
3929 switch (opcode) {
Eddie Waia9736c02010-02-24 14:42:04 +00003930 case L5CM_RAMROD_CMD_ID_TCP_CONNECT:
3931 if (l4kcqe->status != 0) {
3932 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3933 cnic_cm_upcall(cp, csk,
3934 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3935 }
3936 break;
Michael Chana4636962009-06-08 18:14:43 -07003937 case L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE:
3938 if (l4kcqe->status == 0)
3939 set_bit(SK_F_OFFLD_COMPLETE, &csk->flags);
Michael Chan8ec3e702012-03-21 15:38:34 +00003940 else if (l4kcqe->status ==
3941 L4_KCQE_COMPLETION_STATUS_PARITY_ERROR)
Jeffrey Huang0cb1f4b2012-02-08 17:33:56 +00003942 set_bit(SK_F_HW_ERR, &csk->flags);
Michael Chana4636962009-06-08 18:14:43 -07003943
3944 smp_mb__before_clear_bit();
3945 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3946 cnic_cm_upcall(cp, csk, opcode);
3947 break;
3948
3949 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
Michael Chana4636962009-06-08 18:14:43 -07003950 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3951 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan71034ba2009-10-10 13:46:59 +00003952 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3953 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
Michael Chan8ec3e702012-03-21 15:38:34 +00003954 if (l4kcqe->status == L4_KCQE_COMPLETION_STATUS_PARITY_ERROR)
Michael Chan23021c22012-01-04 12:12:28 +00003955 set_bit(SK_F_HW_ERR, &csk->flags);
3956
Michael Chana4636962009-06-08 18:14:43 -07003957 cp->close_conn(csk, opcode);
3958 break;
3959
3960 case L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED:
Michael Chan101c40c2011-06-08 19:29:33 +00003961 /* after we already sent CLOSE_REQ */
3962 if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) &&
3963 !test_bit(SK_F_OFFLD_COMPLETE, &csk->flags) &&
3964 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3965 cp->close_conn(csk, L4_KCQE_OPCODE_VALUE_RESET_COMP);
3966 else
3967 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07003968 break;
3969 }
3970 csk_put(csk);
3971}
3972
3973static void cnic_cm_indicate_kcqe(void *data, struct kcqe *kcqe[], u32 num)
3974{
3975 struct cnic_dev *dev = data;
3976 int i;
3977
3978 for (i = 0; i < num; i++)
3979 cnic_cm_process_kcqe(dev, kcqe[i]);
3980}
3981
3982static struct cnic_ulp_ops cm_ulp_ops = {
3983 .indicate_kcqes = cnic_cm_indicate_kcqe,
3984};
3985
3986static void cnic_cm_free_mem(struct cnic_dev *dev)
3987{
3988 struct cnic_local *cp = dev->cnic_priv;
3989
3990 kfree(cp->csk_tbl);
3991 cp->csk_tbl = NULL;
3992 cnic_free_id_tbl(&cp->csk_port_tbl);
3993}
3994
3995static int cnic_cm_alloc_mem(struct cnic_dev *dev)
3996{
3997 struct cnic_local *cp = dev->cnic_priv;
Eddie Wai11f23aa2011-06-08 19:29:34 +00003998 u32 port_id;
Michael Chana4636962009-06-08 18:14:43 -07003999
4000 cp->csk_tbl = kzalloc(sizeof(struct cnic_sock) * MAX_CM_SK_TBL_SZ,
4001 GFP_KERNEL);
4002 if (!cp->csk_tbl)
4003 return -ENOMEM;
4004
Michael Chan973e5742011-07-13 17:24:17 +00004005 port_id = random32();
Eddie Wai11f23aa2011-06-08 19:29:34 +00004006 port_id %= CNIC_LOCAL_PORT_RANGE;
Michael Chana4636962009-06-08 18:14:43 -07004007 if (cnic_init_id_tbl(&cp->csk_port_tbl, CNIC_LOCAL_PORT_RANGE,
Eddie Wai11f23aa2011-06-08 19:29:34 +00004008 CNIC_LOCAL_PORT_MIN, port_id)) {
Michael Chana4636962009-06-08 18:14:43 -07004009 cnic_cm_free_mem(dev);
4010 return -ENOMEM;
4011 }
4012 return 0;
4013}
4014
4015static int cnic_ready_to_close(struct cnic_sock *csk, u32 opcode)
4016{
Michael Chan943189f2010-06-15 08:57:02 +00004017 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
4018 /* Unsolicited RESET_COMP or RESET_RECEIVED */
4019 opcode = L4_KCQE_OPCODE_VALUE_RESET_RECEIVED;
4020 csk->state = opcode;
Michael Chana1e621b2010-06-15 08:57:01 +00004021 }
Michael Chan943189f2010-06-15 08:57:02 +00004022
4023 /* 1. If event opcode matches the expected event in csk->state
Michael Chan101c40c2011-06-08 19:29:33 +00004024 * 2. If the expected event is CLOSE_COMP or RESET_COMP, we accept any
4025 * event
Michael Chan7b34a462010-06-15 08:57:03 +00004026 * 3. If the expected event is 0, meaning the connection was never
4027 * never established, we accept the opcode from cm_abort.
Michael Chan943189f2010-06-15 08:57:02 +00004028 */
Michael Chan7b34a462010-06-15 08:57:03 +00004029 if (opcode == csk->state || csk->state == 0 ||
Michael Chan101c40c2011-06-08 19:29:33 +00004030 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP ||
4031 csk->state == L4_KCQE_OPCODE_VALUE_RESET_COMP) {
Michael Chan7b34a462010-06-15 08:57:03 +00004032 if (!test_and_set_bit(SK_F_CLOSING, &csk->flags)) {
4033 if (csk->state == 0)
4034 csk->state = opcode;
Michael Chana4636962009-06-08 18:14:43 -07004035 return 1;
Michael Chan7b34a462010-06-15 08:57:03 +00004036 }
Michael Chana4636962009-06-08 18:14:43 -07004037 }
4038 return 0;
4039}
4040
4041static void cnic_close_bnx2_conn(struct cnic_sock *csk, u32 opcode)
4042{
4043 struct cnic_dev *dev = csk->dev;
4044 struct cnic_local *cp = dev->cnic_priv;
4045
Michael Chana1e621b2010-06-15 08:57:01 +00004046 if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED) {
4047 cnic_cm_upcall(cp, csk, opcode);
4048 return;
4049 }
4050
Michael Chana4636962009-06-08 18:14:43 -07004051 clear_bit(SK_F_CONNECT_START, &csk->flags);
Eddie Wai66883e92010-02-24 14:42:05 +00004052 cnic_close_conn(csk);
Michael Chan7b34a462010-06-15 08:57:03 +00004053 csk->state = opcode;
Eddie Wai66883e92010-02-24 14:42:05 +00004054 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07004055}
4056
4057static void cnic_cm_stop_bnx2_hw(struct cnic_dev *dev)
4058{
4059}
4060
4061static int cnic_cm_init_bnx2_hw(struct cnic_dev *dev)
4062{
4063 u32 seed;
4064
Michael Chan973e5742011-07-13 17:24:17 +00004065 seed = random32();
Michael Chana4636962009-06-08 18:14:43 -07004066 cnic_ctx_wr(dev, 45, 0, seed);
4067 return 0;
4068}
4069
Michael Chan71034ba2009-10-10 13:46:59 +00004070static void cnic_close_bnx2x_conn(struct cnic_sock *csk, u32 opcode)
4071{
4072 struct cnic_dev *dev = csk->dev;
4073 struct cnic_local *cp = dev->cnic_priv;
4074 struct cnic_context *ctx = &cp->ctx_tbl[csk->l5_cid];
4075 union l5cm_specific_data l5_data;
4076 u32 cmd = 0;
4077 int close_complete = 0;
4078
4079 switch (opcode) {
4080 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
4081 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
4082 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan7b34a462010-06-15 08:57:03 +00004083 if (cnic_ready_to_close(csk, opcode)) {
Michael Chan23021c22012-01-04 12:12:28 +00004084 if (test_bit(SK_F_HW_ERR, &csk->flags))
4085 close_complete = 1;
4086 else if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
Michael Chan7b34a462010-06-15 08:57:03 +00004087 cmd = L5CM_RAMROD_CMD_ID_SEARCHER_DELETE;
4088 else
4089 close_complete = 1;
4090 }
Michael Chan71034ba2009-10-10 13:46:59 +00004091 break;
4092 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
4093 cmd = L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD;
4094 break;
4095 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
4096 close_complete = 1;
4097 break;
4098 }
4099 if (cmd) {
4100 memset(&l5_data, 0, sizeof(l5_data));
4101
4102 cnic_submit_kwqe_16(dev, cmd, csk->cid, ISCSI_CONNECTION_TYPE,
4103 &l5_data);
4104 } else if (close_complete) {
4105 ctx->timestamp = jiffies;
4106 cnic_close_conn(csk);
4107 cnic_cm_upcall(cp, csk, csk->state);
4108 }
4109}
4110
4111static void cnic_cm_stop_bnx2x_hw(struct cnic_dev *dev)
4112{
Michael Chanfdf24082010-10-13 14:06:47 +00004113 struct cnic_local *cp = dev->cnic_priv;
Michael Chanfdf24082010-10-13 14:06:47 +00004114
4115 if (!cp->ctx_tbl)
4116 return;
4117
4118 if (!netif_running(dev->netdev))
4119 return;
4120
Michael Chan74e49bb2011-07-20 14:55:23 +00004121 cnic_bnx2x_delete_wait(dev, 0);
Michael Chanfdf24082010-10-13 14:06:47 +00004122
4123 cancel_delayed_work(&cp->delete_task);
4124 flush_workqueue(cnic_wq);
4125
4126 if (atomic_read(&cp->iscsi_conn) != 0)
4127 netdev_warn(dev->netdev, "%d iSCSI connections not destroyed\n",
4128 atomic_read(&cp->iscsi_conn));
Michael Chan71034ba2009-10-10 13:46:59 +00004129}
4130
4131static int cnic_cm_init_bnx2x_hw(struct cnic_dev *dev)
4132{
4133 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00004134 u32 pfid = cp->pfid;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004135 u32 port = CNIC_PORT(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00004136
4137 cnic_init_bnx2x_mac(dev);
4138 cnic_bnx2x_set_tcp_timestamp(dev, 1);
4139
4140 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004141 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(pfid), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004142
4143 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004144 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_ENABLED_OFFSET(port), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00004145 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004146 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_MAX_COUNT_OFFSET(port),
Michael Chan71034ba2009-10-10 13:46:59 +00004147 DEF_MAX_DA_COUNT);
4148
4149 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004150 XSTORM_ISCSI_TCP_VARS_TTL_OFFSET(pfid), DEF_TTL);
Michael Chan71034ba2009-10-10 13:46:59 +00004151 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004152 XSTORM_ISCSI_TCP_VARS_TOS_OFFSET(pfid), DEF_TOS);
Michael Chan71034ba2009-10-10 13:46:59 +00004153 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004154 XSTORM_ISCSI_TCP_VARS_ADV_WND_SCL_OFFSET(pfid), 2);
Michael Chan71034ba2009-10-10 13:46:59 +00004155 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004156 XSTORM_TCP_TX_SWS_TIMER_VAL_OFFSET(pfid), DEF_SWS_TIMER);
Michael Chan71034ba2009-10-10 13:46:59 +00004157
Michael Chan14203982010-10-06 03:16:06 +00004158 CNIC_WR(dev, BAR_TSTRORM_INTMEM + TSTORM_TCP_MAX_CWND_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00004159 DEF_MAX_CWND);
4160 return 0;
4161}
4162
Michael Chanfdf24082010-10-13 14:06:47 +00004163static void cnic_delete_task(struct work_struct *work)
4164{
4165 struct cnic_local *cp;
4166 struct cnic_dev *dev;
4167 u32 i;
4168 int need_resched = 0;
4169
4170 cp = container_of(work, struct cnic_local, delete_task.work);
4171 dev = cp->dev;
4172
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07004173 if (test_and_clear_bit(CNIC_LCL_FL_STOP_ISCSI, &cp->cnic_local_flags)) {
4174 struct drv_ctl_info info;
4175
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07004176 cnic_ulp_stop_one(cp, CNIC_ULP_ISCSI);
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07004177
4178 info.cmd = DRV_CTL_ISCSI_STOPPED_CMD;
4179 cp->ethdev->drv_ctl(dev->netdev, &info);
4180 }
4181
Michael Chanfdf24082010-10-13 14:06:47 +00004182 for (i = 0; i < cp->max_cid_space; i++) {
4183 struct cnic_context *ctx = &cp->ctx_tbl[i];
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004184 int err;
Michael Chanfdf24082010-10-13 14:06:47 +00004185
4186 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags) ||
4187 !test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
4188 continue;
4189
4190 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
4191 need_resched = 1;
4192 continue;
4193 }
4194
4195 if (!test_and_clear_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
4196 continue;
4197
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004198 err = cnic_bnx2x_destroy_ramrod(dev, i);
Michael Chanfdf24082010-10-13 14:06:47 +00004199
4200 cnic_free_bnx2x_conn_resc(dev, i);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004201 if (!err) {
4202 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI)
4203 atomic_dec(&cp->iscsi_conn);
Michael Chanfdf24082010-10-13 14:06:47 +00004204
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004205 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
4206 }
Michael Chanfdf24082010-10-13 14:06:47 +00004207 }
4208
4209 if (need_resched)
4210 queue_delayed_work(cnic_wq, &cp->delete_task,
4211 msecs_to_jiffies(10));
4212
4213}
4214
Michael Chana4636962009-06-08 18:14:43 -07004215static int cnic_cm_open(struct cnic_dev *dev)
4216{
4217 struct cnic_local *cp = dev->cnic_priv;
4218 int err;
4219
4220 err = cnic_cm_alloc_mem(dev);
4221 if (err)
4222 return err;
4223
4224 err = cp->start_cm(dev);
4225
4226 if (err)
4227 goto err_out;
4228
Michael Chanfdf24082010-10-13 14:06:47 +00004229 INIT_DELAYED_WORK(&cp->delete_task, cnic_delete_task);
4230
Michael Chana4636962009-06-08 18:14:43 -07004231 dev->cm_create = cnic_cm_create;
4232 dev->cm_destroy = cnic_cm_destroy;
4233 dev->cm_connect = cnic_cm_connect;
4234 dev->cm_abort = cnic_cm_abort;
4235 dev->cm_close = cnic_cm_close;
4236 dev->cm_select_dev = cnic_cm_select_dev;
4237
4238 cp->ulp_handle[CNIC_ULP_L4] = dev;
4239 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], &cm_ulp_ops);
4240 return 0;
4241
4242err_out:
4243 cnic_cm_free_mem(dev);
4244 return err;
4245}
4246
4247static int cnic_cm_shutdown(struct cnic_dev *dev)
4248{
4249 struct cnic_local *cp = dev->cnic_priv;
4250 int i;
4251
4252 cp->stop_cm(dev);
4253
4254 if (!cp->csk_tbl)
4255 return 0;
4256
4257 for (i = 0; i < MAX_CM_SK_TBL_SZ; i++) {
4258 struct cnic_sock *csk = &cp->csk_tbl[i];
4259
4260 clear_bit(SK_F_INUSE, &csk->flags);
4261 cnic_cm_cleanup(csk);
4262 }
4263 cnic_cm_free_mem(dev);
4264
4265 return 0;
4266}
4267
4268static void cnic_init_context(struct cnic_dev *dev, u32 cid)
4269{
Michael Chana4636962009-06-08 18:14:43 -07004270 u32 cid_addr;
4271 int i;
4272
Michael Chana4636962009-06-08 18:14:43 -07004273 cid_addr = GET_CID_ADDR(cid);
4274
4275 for (i = 0; i < CTX_SIZE; i += 4)
4276 cnic_ctx_wr(dev, cid_addr, i, 0);
4277}
4278
4279static int cnic_setup_5709_context(struct cnic_dev *dev, int valid)
4280{
4281 struct cnic_local *cp = dev->cnic_priv;
4282 int ret = 0, i;
4283 u32 valid_bit = valid ? BNX2_CTX_HOST_PAGE_TBL_DATA0_VALID : 0;
4284
4285 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4286 return 0;
4287
4288 for (i = 0; i < cp->ctx_blks; i++) {
4289 int j;
4290 u32 idx = cp->ctx_arr[i].cid / cp->cids_per_blk;
4291 u32 val;
4292
4293 memset(cp->ctx_arr[i].ctx, 0, BCM_PAGE_SIZE);
4294
4295 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA0,
4296 (cp->ctx_arr[i].mapping & 0xffffffff) | valid_bit);
4297 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA1,
4298 (u64) cp->ctx_arr[i].mapping >> 32);
4299 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL, idx |
4300 BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
4301 for (j = 0; j < 10; j++) {
4302
4303 val = CNIC_RD(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL);
4304 if (!(val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ))
4305 break;
4306 udelay(5);
4307 }
4308 if (val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) {
4309 ret = -EBUSY;
4310 break;
4311 }
4312 }
4313 return ret;
4314}
4315
4316static void cnic_free_irq(struct cnic_dev *dev)
4317{
4318 struct cnic_local *cp = dev->cnic_priv;
4319 struct cnic_eth_dev *ethdev = cp->ethdev;
4320
4321 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4322 cp->disable_int_sync(dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004323 tasklet_kill(&cp->cnic_irq_task);
Michael Chana4636962009-06-08 18:14:43 -07004324 free_irq(ethdev->irq_arr[0].vector, dev);
4325 }
4326}
4327
Michael Chan6e0dc642010-10-13 14:06:44 +00004328static int cnic_request_irq(struct cnic_dev *dev)
4329{
4330 struct cnic_local *cp = dev->cnic_priv;
4331 struct cnic_eth_dev *ethdev = cp->ethdev;
4332 int err;
4333
4334 err = request_irq(ethdev->irq_arr[0].vector, cnic_irq, 0, "cnic", dev);
4335 if (err)
4336 tasklet_disable(&cp->cnic_irq_task);
4337
4338 return err;
4339}
4340
Michael Chana4636962009-06-08 18:14:43 -07004341static int cnic_init_bnx2_irq(struct cnic_dev *dev)
4342{
4343 struct cnic_local *cp = dev->cnic_priv;
4344 struct cnic_eth_dev *ethdev = cp->ethdev;
4345
4346 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4347 int err, i = 0;
4348 int sblk_num = cp->status_blk_num;
4349 u32 base = ((sblk_num - 1) * BNX2_HC_SB_CONFIG_SIZE) +
4350 BNX2_HC_SB_CONFIG_1;
4351
4352 CNIC_WR(dev, base, BNX2_HC_SB_CONFIG_1_ONE_SHOT);
4353
4354 CNIC_WR(dev, base + BNX2_HC_COMP_PROD_TRIP_OFF, (2 << 16) | 8);
4355 CNIC_WR(dev, base + BNX2_HC_COM_TICKS_OFF, (64 << 16) | 220);
4356 CNIC_WR(dev, base + BNX2_HC_CMD_TICKS_OFF, (64 << 16) | 220);
4357
Michael Chana4dde3a2010-02-24 14:42:08 +00004358 cp->last_status_idx = cp->status_blk.bnx2->status_idx;
Joe Perches164165d2009-11-19 09:30:10 +00004359 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2_msix,
Michael Chana4636962009-06-08 18:14:43 -07004360 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004361 err = cnic_request_irq(dev);
4362 if (err)
Michael Chana4636962009-06-08 18:14:43 -07004363 return err;
Michael Chan6e0dc642010-10-13 14:06:44 +00004364
Michael Chana4dde3a2010-02-24 14:42:08 +00004365 while (cp->status_blk.bnx2->status_completion_producer_index &&
Michael Chana4636962009-06-08 18:14:43 -07004366 i < 10) {
4367 CNIC_WR(dev, BNX2_HC_COALESCE_NOW,
4368 1 << (11 + sblk_num));
4369 udelay(10);
4370 i++;
4371 barrier();
4372 }
Michael Chana4dde3a2010-02-24 14:42:08 +00004373 if (cp->status_blk.bnx2->status_completion_producer_index) {
Michael Chana4636962009-06-08 18:14:43 -07004374 cnic_free_irq(dev);
4375 goto failed;
4376 }
4377
4378 } else {
Michael Chana4dde3a2010-02-24 14:42:08 +00004379 struct status_block *sblk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004380 u32 hc_cmd = CNIC_RD(dev, BNX2_HC_COMMAND);
4381 int i = 0;
4382
4383 while (sblk->status_completion_producer_index && i < 10) {
4384 CNIC_WR(dev, BNX2_HC_COMMAND,
4385 hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
4386 udelay(10);
4387 i++;
4388 barrier();
4389 }
4390 if (sblk->status_completion_producer_index)
4391 goto failed;
4392
4393 }
4394 return 0;
4395
4396failed:
Joe Perchesddf79b22010-02-17 15:01:54 +00004397 netdev_err(dev->netdev, "KCQ index not resetting to 0\n");
Michael Chana4636962009-06-08 18:14:43 -07004398 return -EBUSY;
4399}
4400
4401static void cnic_enable_bnx2_int(struct cnic_dev *dev)
4402{
4403 struct cnic_local *cp = dev->cnic_priv;
4404 struct cnic_eth_dev *ethdev = cp->ethdev;
4405
4406 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4407 return;
4408
4409 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4410 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
4411}
4412
4413static void cnic_disable_bnx2_int_sync(struct cnic_dev *dev)
4414{
4415 struct cnic_local *cp = dev->cnic_priv;
4416 struct cnic_eth_dev *ethdev = cp->ethdev;
4417
4418 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4419 return;
4420
4421 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4422 BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
4423 CNIC_RD(dev, BNX2_PCICFG_INT_ACK_CMD);
4424 synchronize_irq(ethdev->irq_arr[0].vector);
4425}
4426
4427static void cnic_init_bnx2_tx_ring(struct cnic_dev *dev)
4428{
4429 struct cnic_local *cp = dev->cnic_priv;
4430 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004431 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004432 u32 cid_addr, tx_cid, sb_id;
4433 u32 val, offset0, offset1, offset2, offset3;
4434 int i;
4435 struct tx_bd *txbd;
Michael Chancd801532010-10-13 14:06:49 +00004436 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Michael Chana4dde3a2010-02-24 14:42:08 +00004437 struct status_block *s_blk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004438
4439 sb_id = cp->status_blk_num;
4440 tx_cid = 20;
Michael Chana4636962009-06-08 18:14:43 -07004441 cp->tx_cons_ptr = &s_blk->status_tx_quick_consumer_index2;
4442 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004443 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004444
4445 tx_cid = TX_TSS_CID + sb_id - 1;
Michael Chana4636962009-06-08 18:14:43 -07004446 CNIC_WR(dev, BNX2_TSCH_TSS_CFG, (sb_id << 24) |
4447 (TX_TSS_CID << 7));
4448 cp->tx_cons_ptr = &sblk->status_tx_quick_consumer_index;
4449 }
4450 cp->tx_cons = *cp->tx_cons_ptr;
4451
4452 cid_addr = GET_CID_ADDR(tx_cid);
4453 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
4454 u32 cid_addr2 = GET_CID_ADDR(tx_cid + 4) + 0x40;
4455
4456 for (i = 0; i < PHY_CTX_SIZE; i += 4)
4457 cnic_ctx_wr(dev, cid_addr2, i, 0);
4458
4459 offset0 = BNX2_L2CTX_TYPE_XI;
4460 offset1 = BNX2_L2CTX_CMD_TYPE_XI;
4461 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI_XI;
4462 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO_XI;
4463 } else {
Michael Chanb58ffb42010-05-27 16:31:41 -07004464 cnic_init_context(dev, tx_cid);
4465 cnic_init_context(dev, tx_cid + 1);
4466
Michael Chana4636962009-06-08 18:14:43 -07004467 offset0 = BNX2_L2CTX_TYPE;
4468 offset1 = BNX2_L2CTX_CMD_TYPE;
4469 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI;
4470 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO;
4471 }
4472 val = BNX2_L2CTX_TYPE_TYPE_L2 | BNX2_L2CTX_TYPE_SIZE_L2;
4473 cnic_ctx_wr(dev, cid_addr, offset0, val);
4474
4475 val = BNX2_L2CTX_CMD_TYPE_TYPE_L2 | (8 << 16);
4476 cnic_ctx_wr(dev, cid_addr, offset1, val);
4477
Joe Perches43d620c2011-06-16 19:08:06 +00004478 txbd = udev->l2_ring;
Michael Chana4636962009-06-08 18:14:43 -07004479
Michael Chancd801532010-10-13 14:06:49 +00004480 buf_map = udev->l2_buf_map;
Michael Chana4636962009-06-08 18:14:43 -07004481 for (i = 0; i < MAX_TX_DESC_CNT; i++, txbd++) {
4482 txbd->tx_bd_haddr_hi = (u64) buf_map >> 32;
4483 txbd->tx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4484 }
Michael Chancd801532010-10-13 14:06:49 +00004485 val = (u64) ring_map >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004486 cnic_ctx_wr(dev, cid_addr, offset2, val);
4487 txbd->tx_bd_haddr_hi = val;
4488
Michael Chancd801532010-10-13 14:06:49 +00004489 val = (u64) ring_map & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004490 cnic_ctx_wr(dev, cid_addr, offset3, val);
4491 txbd->tx_bd_haddr_lo = val;
4492}
4493
4494static void cnic_init_bnx2_rx_ring(struct cnic_dev *dev)
4495{
4496 struct cnic_local *cp = dev->cnic_priv;
4497 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004498 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004499 u32 cid_addr, sb_id, val, coal_reg, coal_val;
4500 int i;
4501 struct rx_bd *rxbd;
Michael Chana4dde3a2010-02-24 14:42:08 +00004502 struct status_block *s_blk = cp->status_blk.gen;
Michael Chancd801532010-10-13 14:06:49 +00004503 dma_addr_t ring_map = udev->l2_ring_map;
Michael Chana4636962009-06-08 18:14:43 -07004504
4505 sb_id = cp->status_blk_num;
4506 cnic_init_context(dev, 2);
4507 cp->rx_cons_ptr = &s_blk->status_rx_quick_consumer_index2;
4508 coal_reg = BNX2_HC_COMMAND;
4509 coal_val = CNIC_RD(dev, coal_reg);
4510 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004511 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004512
4513 cp->rx_cons_ptr = &sblk->status_rx_quick_consumer_index;
4514 coal_reg = BNX2_HC_COALESCE_NOW;
4515 coal_val = 1 << (11 + sb_id);
4516 }
4517 i = 0;
4518 while (!(*cp->rx_cons_ptr != 0) && i < 10) {
4519 CNIC_WR(dev, coal_reg, coal_val);
4520 udelay(10);
4521 i++;
4522 barrier();
4523 }
4524 cp->rx_cons = *cp->rx_cons_ptr;
4525
4526 cid_addr = GET_CID_ADDR(2);
4527 val = BNX2_L2CTX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
4528 BNX2_L2CTX_CTX_TYPE_SIZE_L2 | (0x02 << 8);
4529 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_CTX_TYPE, val);
4530
4531 if (sb_id == 0)
Michael Chand0549382009-10-28 03:41:59 -07004532 val = 2 << BNX2_L2CTX_L2_STATUSB_NUM_SHIFT;
Michael Chana4636962009-06-08 18:14:43 -07004533 else
Michael Chand0549382009-10-28 03:41:59 -07004534 val = BNX2_L2CTX_L2_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004535 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_HOST_BDIDX, val);
4536
Joe Perches43d620c2011-06-16 19:08:06 +00004537 rxbd = udev->l2_ring + BCM_PAGE_SIZE;
Michael Chana4636962009-06-08 18:14:43 -07004538 for (i = 0; i < MAX_RX_DESC_CNT; i++, rxbd++) {
4539 dma_addr_t buf_map;
4540 int n = (i % cp->l2_rx_ring_size) + 1;
4541
Michael Chancd801532010-10-13 14:06:49 +00004542 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chana4636962009-06-08 18:14:43 -07004543 rxbd->rx_bd_len = cp->l2_single_buf_size;
4544 rxbd->rx_bd_flags = RX_BD_FLAGS_START | RX_BD_FLAGS_END;
4545 rxbd->rx_bd_haddr_hi = (u64) buf_map >> 32;
4546 rxbd->rx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4547 }
Michael Chancd801532010-10-13 14:06:49 +00004548 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004549 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_HI, val);
4550 rxbd->rx_bd_haddr_hi = val;
4551
Michael Chancd801532010-10-13 14:06:49 +00004552 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004553 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_LO, val);
4554 rxbd->rx_bd_haddr_lo = val;
4555
4556 val = cnic_reg_rd_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD);
4557 cnic_reg_wr_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD, val | (1 << 2));
4558}
4559
4560static void cnic_shutdown_bnx2_rx_ring(struct cnic_dev *dev)
4561{
4562 struct kwqe *wqes[1], l2kwqe;
4563
4564 memset(&l2kwqe, 0, sizeof(l2kwqe));
4565 wqes[0] = &l2kwqe;
Michael Chane1928c82010-12-23 07:43:04 +00004566 l2kwqe.kwqe_op_flag = (L2_LAYER_CODE << KWQE_LAYER_SHIFT) |
Michael Chana4636962009-06-08 18:14:43 -07004567 (L2_KWQE_OPCODE_VALUE_FLUSH <<
4568 KWQE_OPCODE_SHIFT) | 2;
4569 dev->submit_kwqes(dev, wqes, 1);
4570}
4571
4572static void cnic_set_bnx2_mac(struct cnic_dev *dev)
4573{
4574 struct cnic_local *cp = dev->cnic_priv;
4575 u32 val;
4576
4577 val = cp->func << 2;
4578
4579 cp->shmem_base = cnic_reg_rd_ind(dev, BNX2_SHM_HDR_ADDR_0 + val);
4580
4581 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4582 BNX2_PORT_HW_CFG_ISCSI_MAC_UPPER);
4583 dev->mac_addr[0] = (u8) (val >> 8);
4584 dev->mac_addr[1] = (u8) val;
4585
4586 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH4, val);
4587
4588 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4589 BNX2_PORT_HW_CFG_ISCSI_MAC_LOWER);
4590 dev->mac_addr[2] = (u8) (val >> 24);
4591 dev->mac_addr[3] = (u8) (val >> 16);
4592 dev->mac_addr[4] = (u8) (val >> 8);
4593 dev->mac_addr[5] = (u8) val;
4594
4595 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH5, val);
4596
4597 val = 4 | BNX2_RPM_SORT_USER2_BC_EN;
4598 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4599 val |= BNX2_RPM_SORT_USER2_PROM_VLAN;
4600
4601 CNIC_WR(dev, BNX2_RPM_SORT_USER2, 0x0);
4602 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val);
4603 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val | BNX2_RPM_SORT_USER2_ENA);
4604}
4605
4606static int cnic_start_bnx2_hw(struct cnic_dev *dev)
4607{
4608 struct cnic_local *cp = dev->cnic_priv;
4609 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chana4dde3a2010-02-24 14:42:08 +00004610 struct status_block *sblk = cp->status_blk.gen;
Michael Chane6c28892010-06-24 14:58:39 +00004611 u32 val, kcq_cid_addr, kwq_cid_addr;
Michael Chana4636962009-06-08 18:14:43 -07004612 int err;
4613
4614 cnic_set_bnx2_mac(dev);
4615
4616 val = CNIC_RD(dev, BNX2_MQ_CONFIG);
4617 val &= ~BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE;
4618 if (BCM_PAGE_BITS > 12)
4619 val |= (12 - 8) << 4;
4620 else
4621 val |= (BCM_PAGE_BITS - 8) << 4;
4622
4623 CNIC_WR(dev, BNX2_MQ_CONFIG, val);
4624
4625 CNIC_WR(dev, BNX2_HC_COMP_PROD_TRIP, (2 << 16) | 8);
4626 CNIC_WR(dev, BNX2_HC_COM_TICKS, (64 << 16) | 220);
4627 CNIC_WR(dev, BNX2_HC_CMD_TICKS, (64 << 16) | 220);
4628
4629 err = cnic_setup_5709_context(dev, 1);
4630 if (err)
4631 return err;
4632
4633 cnic_init_context(dev, KWQ_CID);
4634 cnic_init_context(dev, KCQ_CID);
4635
Michael Chane6c28892010-06-24 14:58:39 +00004636 kwq_cid_addr = GET_CID_ADDR(KWQ_CID);
Michael Chana4636962009-06-08 18:14:43 -07004637 cp->kwq_io_addr = MB_GET_CID_ADDR(KWQ_CID) + L5_KRNLQ_HOST_QIDX;
4638
4639 cp->max_kwq_idx = MAX_KWQ_IDX;
4640 cp->kwq_prod_idx = 0;
4641 cp->kwq_con_idx = 0;
Michael Chan1f1332a2010-05-18 11:32:52 +00004642 set_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07004643
4644 if (CHIP_NUM(cp) == CHIP_NUM_5706 || CHIP_NUM(cp) == CHIP_NUM_5708)
4645 cp->kwq_con_idx_ptr = &sblk->status_rx_quick_consumer_index15;
4646 else
4647 cp->kwq_con_idx_ptr = &sblk->status_cmd_consumer_index;
4648
4649 /* Initialize the kernel work queue context. */
4650 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4651 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004652 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004653
4654 val = (BCM_PAGE_SIZE / sizeof(struct kwqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004655 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004656
4657 val = ((BCM_PAGE_SIZE / sizeof(struct kwqe)) << 16) | KWQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004658 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004659
4660 val = (u32) ((u64) cp->kwq_info.pgtbl_map >> 32);
Michael Chane6c28892010-06-24 14:58:39 +00004661 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004662
4663 val = (u32) cp->kwq_info.pgtbl_map;
Michael Chane6c28892010-06-24 14:58:39 +00004664 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004665
Michael Chane6c28892010-06-24 14:58:39 +00004666 kcq_cid_addr = GET_CID_ADDR(KCQ_CID);
4667 cp->kcq1.io_addr = MB_GET_CID_ADDR(KCQ_CID) + L5_KRNLQ_HOST_QIDX;
Michael Chana4636962009-06-08 18:14:43 -07004668
Michael Chane6c28892010-06-24 14:58:39 +00004669 cp->kcq1.sw_prod_idx = 0;
4670 cp->kcq1.hw_prod_idx_ptr =
4671 (u16 *) &sblk->status_completion_producer_index;
4672
4673 cp->kcq1.status_idx_ptr = (u16 *) &sblk->status_idx;
Michael Chana4636962009-06-08 18:14:43 -07004674
4675 /* Initialize the kernel complete queue context. */
4676 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4677 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004678 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004679
4680 val = (BCM_PAGE_SIZE / sizeof(struct kcqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004681 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004682
4683 val = ((BCM_PAGE_SIZE / sizeof(struct kcqe)) << 16) | KCQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004684 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004685
Michael Chane6c28892010-06-24 14:58:39 +00004686 val = (u32) ((u64) cp->kcq1.dma.pgtbl_map >> 32);
4687 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004688
Michael Chane6c28892010-06-24 14:58:39 +00004689 val = (u32) cp->kcq1.dma.pgtbl_map;
4690 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004691
4692 cp->int_num = 0;
4693 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chane6c28892010-06-24 14:58:39 +00004694 struct status_block_msix *msblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004695 u32 sb_id = cp->status_blk_num;
Michael Chand0549382009-10-28 03:41:59 -07004696 u32 sb = BNX2_L2CTX_L5_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004697
Michael Chane6c28892010-06-24 14:58:39 +00004698 cp->kcq1.hw_prod_idx_ptr =
4699 (u16 *) &msblk->status_completion_producer_index;
4700 cp->kcq1.status_idx_ptr = (u16 *) &msblk->status_idx;
Michael Chanb177a5d52010-06-24 14:58:41 +00004701 cp->kwq_con_idx_ptr = (u16 *) &msblk->status_cmd_consumer_index;
Michael Chana4636962009-06-08 18:14:43 -07004702 cp->int_num = sb_id << BNX2_PCICFG_INT_ACK_CMD_INT_NUM_SHIFT;
Michael Chane6c28892010-06-24 14:58:39 +00004703 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
4704 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
Michael Chana4636962009-06-08 18:14:43 -07004705 }
4706
4707 /* Enable Commnad Scheduler notification when we write to the
4708 * host producer index of the kernel contexts. */
4709 CNIC_WR(dev, BNX2_MQ_KNL_CMD_MASK1, 2);
4710
4711 /* Enable Command Scheduler notification when we write to either
4712 * the Send Queue or Receive Queue producer indexes of the kernel
4713 * bypass contexts. */
4714 CNIC_WR(dev, BNX2_MQ_KNL_BYP_CMD_MASK1, 7);
4715 CNIC_WR(dev, BNX2_MQ_KNL_BYP_WRITE_MASK1, 7);
4716
4717 /* Notify COM when the driver post an application buffer. */
4718 CNIC_WR(dev, BNX2_MQ_KNL_RX_V2P_MASK2, 0x2000);
4719
4720 /* Set the CP and COM doorbells. These two processors polls the
4721 * doorbell for a non zero value before running. This must be done
4722 * after setting up the kernel queue contexts. */
4723 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 1);
4724 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 1);
4725
4726 cnic_init_bnx2_tx_ring(dev);
4727 cnic_init_bnx2_rx_ring(dev);
4728
4729 err = cnic_init_bnx2_irq(dev);
4730 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00004731 netdev_err(dev->netdev, "cnic_init_irq failed\n");
Michael Chana4636962009-06-08 18:14:43 -07004732 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
4733 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
4734 return err;
4735 }
4736
4737 return 0;
4738}
4739
Michael Chan71034ba2009-10-10 13:46:59 +00004740static void cnic_setup_bnx2x_context(struct cnic_dev *dev)
4741{
4742 struct cnic_local *cp = dev->cnic_priv;
4743 struct cnic_eth_dev *ethdev = cp->ethdev;
4744 u32 start_offset = ethdev->ctx_tbl_offset;
4745 int i;
4746
4747 for (i = 0; i < cp->ctx_blks; i++) {
4748 struct cnic_ctx *ctx = &cp->ctx_arr[i];
4749 dma_addr_t map = ctx->mapping;
4750
4751 if (cp->ctx_align) {
4752 unsigned long mask = cp->ctx_align - 1;
4753
4754 map = (map + mask) & ~mask;
4755 }
4756
4757 cnic_ctx_tbl_wr(dev, start_offset + i, map);
4758 }
4759}
4760
4761static int cnic_init_bnx2x_irq(struct cnic_dev *dev)
4762{
4763 struct cnic_local *cp = dev->cnic_priv;
4764 struct cnic_eth_dev *ethdev = cp->ethdev;
4765 int err = 0;
4766
Joe Perches164165d2009-11-19 09:30:10 +00004767 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2x_bh,
Michael Chan71034ba2009-10-10 13:46:59 +00004768 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004769 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
4770 err = cnic_request_irq(dev);
4771
Michael Chan71034ba2009-10-10 13:46:59 +00004772 return err;
4773}
4774
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004775static inline void cnic_storm_memset_hc_disable(struct cnic_dev *dev,
4776 u16 sb_id, u8 sb_index,
4777 u8 disable)
4778{
4779
4780 u32 addr = BAR_CSTRORM_INTMEM +
4781 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4782 offsetof(struct hc_status_block_data_e1x, index_data) +
4783 sizeof(struct hc_index_data)*sb_index +
4784 offsetof(struct hc_index_data, flags);
4785 u16 flags = CNIC_RD16(dev, addr);
4786 /* clear and set */
4787 flags &= ~HC_INDEX_DATA_HC_ENABLED;
4788 flags |= (((~disable) << HC_INDEX_DATA_HC_ENABLED_SHIFT) &
4789 HC_INDEX_DATA_HC_ENABLED);
4790 CNIC_WR16(dev, addr, flags);
4791}
4792
Michael Chan71034ba2009-10-10 13:46:59 +00004793static void cnic_enable_bnx2x_int(struct cnic_dev *dev)
4794{
4795 struct cnic_local *cp = dev->cnic_priv;
4796 u8 sb_id = cp->status_blk_num;
Michael Chan71034ba2009-10-10 13:46:59 +00004797
4798 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004799 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4800 offsetof(struct hc_status_block_data_e1x, index_data) +
4801 sizeof(struct hc_index_data)*HC_INDEX_ISCSI_EQ_CONS +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004802 offsetof(struct hc_index_data, timeout), 64 / 4);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004803 cnic_storm_memset_hc_disable(dev, sb_id, HC_INDEX_ISCSI_EQ_CONS, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004804}
4805
4806static void cnic_disable_bnx2x_int_sync(struct cnic_dev *dev)
4807{
4808}
4809
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004810static void cnic_init_bnx2x_tx_ring(struct cnic_dev *dev,
4811 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004812{
4813 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004814 struct cnic_uio_dev *udev = cp->udev;
4815 union eth_tx_bd_types *txbd = (union eth_tx_bd_types *) udev->l2_ring;
4816 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004817 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004818 int i;
Michael Chan5159fdc2010-12-23 07:42:59 +00004819 u32 cli = cp->ethdev->iscsi_l2_client_id;
Michael Chan71034ba2009-10-10 13:46:59 +00004820 u32 val;
4821
4822 memset(txbd, 0, BCM_PAGE_SIZE);
4823
Michael Chancd801532010-10-13 14:06:49 +00004824 buf_map = udev->l2_buf_map;
Michael Chan71034ba2009-10-10 13:46:59 +00004825 for (i = 0; i < MAX_TX_DESC_CNT; i += 3, txbd += 3) {
4826 struct eth_tx_start_bd *start_bd = &txbd->start_bd;
4827 struct eth_tx_bd *reg_bd = &((txbd + 2)->reg_bd);
4828
4829 start_bd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4830 start_bd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4831 reg_bd->addr_hi = start_bd->addr_hi;
4832 reg_bd->addr_lo = start_bd->addr_lo + 0x10;
4833 start_bd->nbytes = cpu_to_le16(0x10);
4834 start_bd->nbd = cpu_to_le16(3);
4835 start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
4836 start_bd->general_data = (UNICAST_ADDRESS <<
4837 ETH_TX_START_BD_ETH_ADDR_TYPE_SHIFT);
4838 start_bd->general_data |= (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
4839
4840 }
Michael Chan71034ba2009-10-10 13:46:59 +00004841
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004842 val = (u64) ring_map >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004843 txbd->next_bd.addr_hi = cpu_to_le32(val);
4844
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004845 data->tx.tx_bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004846
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004847 val = (u64) ring_map & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004848 txbd->next_bd.addr_lo = cpu_to_le32(val);
4849
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004850 data->tx.tx_bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004851
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004852 /* Other ramrod params */
4853 data->tx.tx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_CQ_CONS;
4854 data->tx.tx_status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004855
4856 /* reset xstorm per client statistics */
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004857 if (cli < MAX_STAT_COUNTER_ID) {
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004858 data->general.statistics_zero_flg = 1;
4859 data->general.statistics_en_flg = 1;
4860 data->general.statistics_counter_id = cli;
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004861 }
Michael Chan71034ba2009-10-10 13:46:59 +00004862
4863 cp->tx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004864 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_CQ_CONS];
Michael Chan71034ba2009-10-10 13:46:59 +00004865}
4866
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004867static void cnic_init_bnx2x_rx_ring(struct cnic_dev *dev,
4868 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004869{
4870 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004871 struct cnic_uio_dev *udev = cp->udev;
4872 struct eth_rx_bd *rxbd = (struct eth_rx_bd *) (udev->l2_ring +
Michael Chan71034ba2009-10-10 13:46:59 +00004873 BCM_PAGE_SIZE);
4874 struct eth_rx_cqe_next_page *rxcqe = (struct eth_rx_cqe_next_page *)
Michael Chancd801532010-10-13 14:06:49 +00004875 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004876 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004877 int i;
Michael Chan5159fdc2010-12-23 07:42:59 +00004878 u32 cli = cp->ethdev->iscsi_l2_client_id;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004879 int cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
Michael Chan71034ba2009-10-10 13:46:59 +00004880 u32 val;
Michael Chancd801532010-10-13 14:06:49 +00004881 dma_addr_t ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004882
4883 /* General data */
4884 data->general.client_id = cli;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004885 data->general.activate_flg = 1;
4886 data->general.sp_client_id = cli;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004887 data->general.mtu = cpu_to_le16(cp->l2_single_buf_size - 14);
4888 data->general.func_id = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00004889
4890 for (i = 0; i < BNX2X_MAX_RX_DESC_CNT; i++, rxbd++) {
4891 dma_addr_t buf_map;
4892 int n = (i % cp->l2_rx_ring_size) + 1;
4893
Michael Chancd801532010-10-13 14:06:49 +00004894 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chan71034ba2009-10-10 13:46:59 +00004895 rxbd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4896 rxbd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4897 }
Michael Chan71034ba2009-10-10 13:46:59 +00004898
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004899 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004900 rxbd->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004901 data->rx.bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004902
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004903 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004904 rxbd->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004905 data->rx.bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004906
4907 rxcqe += BNX2X_MAX_RCQ_DESC_CNT;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004908 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004909 rxcqe->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004910 data->rx.cqe_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004911
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004912 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004913 rxcqe->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004914 data->rx.cqe_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004915
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004916 /* Other ramrod params */
4917 data->rx.client_qzone_id = cl_qzone_id;
4918 data->rx.rx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS;
4919 data->rx.status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004920
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004921 data->rx.cache_line_alignment_log_size = L1_CACHE_SHIFT;
Michael Chan71034ba2009-10-10 13:46:59 +00004922
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004923 data->rx.max_bytes_on_bd = cpu_to_le16(cp->l2_single_buf_size);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004924 data->rx.outer_vlan_removal_enable_flg = 1;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004925 data->rx.silent_vlan_removal_flg = 1;
4926 data->rx.silent_vlan_value = 0;
4927 data->rx.silent_vlan_mask = 0xffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004928
4929 cp->rx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004930 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS];
Michael Chan5159fdc2010-12-23 07:42:59 +00004931 cp->rx_cons = *cp->rx_cons_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00004932}
4933
Michael Chane21ba412010-12-23 07:43:03 +00004934static void cnic_init_bnx2x_kcq(struct cnic_dev *dev)
4935{
4936 struct cnic_local *cp = dev->cnic_priv;
4937 u32 pfid = cp->pfid;
4938
4939 cp->kcq1.io_addr = BAR_CSTRORM_INTMEM +
4940 CSTORM_ISCSI_EQ_PROD_OFFSET(pfid, 0);
4941 cp->kcq1.sw_prod_idx = 0;
4942
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004943 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chane21ba412010-12-23 07:43:03 +00004944 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4945
4946 cp->kcq1.hw_prod_idx_ptr =
4947 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4948 cp->kcq1.status_idx_ptr =
4949 &sb->sb.running_index[SM_RX_ID];
4950 } else {
4951 struct host_hc_status_block_e1x *sb = cp->status_blk.gen;
4952
4953 cp->kcq1.hw_prod_idx_ptr =
4954 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4955 cp->kcq1.status_idx_ptr =
4956 &sb->sb.running_index[SM_RX_ID];
4957 }
4958
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004959 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chane21ba412010-12-23 07:43:03 +00004960 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4961
4962 cp->kcq2.io_addr = BAR_USTRORM_INTMEM +
4963 USTORM_FCOE_EQ_PROD_OFFSET(pfid);
4964 cp->kcq2.sw_prod_idx = 0;
4965 cp->kcq2.hw_prod_idx_ptr =
4966 &sb->sb.index_values[HC_INDEX_FCOE_EQ_CONS];
4967 cp->kcq2.status_idx_ptr =
4968 &sb->sb.running_index[SM_RX_ID];
4969 }
4970}
4971
Michael Chan71034ba2009-10-10 13:46:59 +00004972static int cnic_start_bnx2x_hw(struct cnic_dev *dev)
4973{
4974 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004975 struct cnic_eth_dev *ethdev = cp->ethdev;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004976 int func = CNIC_FUNC(cp), ret;
Michael Chan14203982010-10-06 03:16:06 +00004977 u32 pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00004978
Michael Chana9e0a4f2012-01-04 12:12:27 +00004979 dev->stats_addr = ethdev->addr_drv_info_to_mcp;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004980 cp->port_mode = CHIP_PORT_MODE_NONE;
4981
4982 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chanee87a822010-10-13 14:06:51 +00004983 u32 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN_OVWR);
4984
4985 if (!(val & 1))
4986 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN);
4987 else
4988 val = (val >> 1) & 1;
4989
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004990 if (val) {
4991 cp->port_mode = CHIP_4_PORT_MODE;
Michael Chanee87a822010-10-13 14:06:51 +00004992 cp->pfid = func >> 1;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004993 } else {
Michael Chanb7d40312011-07-13 17:24:18 +00004994 cp->port_mode = CHIP_2_PORT_MODE;
Michael Chanee87a822010-10-13 14:06:51 +00004995 cp->pfid = func & 0x6;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004996 }
Michael Chanee87a822010-10-13 14:06:51 +00004997 } else {
4998 cp->pfid = func;
4999 }
Michael Chan14203982010-10-06 03:16:06 +00005000 pfid = cp->pfid;
5001
Michael Chan71034ba2009-10-10 13:46:59 +00005002 ret = cnic_init_id_tbl(&cp->cid_tbl, MAX_ISCSI_TBL_SZ,
Eddie Wai11f23aa2011-06-08 19:29:34 +00005003 cp->iscsi_start_cid, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00005004
5005 if (ret)
5006 return -ENOMEM;
5007
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005008 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chandc219a22011-08-26 09:45:39 +00005009 ret = cnic_init_id_tbl(&cp->fcoe_cid_tbl, dev->max_fcoe_conn,
Eddie Wai11f23aa2011-06-08 19:29:34 +00005010 cp->fcoe_start_cid, 0);
Michael Chane1928c82010-12-23 07:43:04 +00005011
5012 if (ret)
5013 return -ENOMEM;
5014 }
5015
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005016 cp->bnx2x_igu_sb_id = ethdev->irq_arr[0].status_blk_num2;
5017
Michael Chane21ba412010-12-23 07:43:03 +00005018 cnic_init_bnx2x_kcq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005019
Michael Chan71034ba2009-10-10 13:46:59 +00005020 /* Only 1 EQ */
Michael Chane6c28892010-06-24 14:58:39 +00005021 CNIC_WR16(dev, cp->kcq1.io_addr, MAX_KCQ_IDX);
Michael Chan71034ba2009-10-10 13:46:59 +00005022 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005023 CSTORM_ISCSI_EQ_CONS_OFFSET(pfid, 0), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00005024 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005025 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00005026 cp->kcq1.dma.pg_map_arr[1] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00005027 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005028 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00005029 (u64) cp->kcq1.dma.pg_map_arr[1] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00005030 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005031 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00005032 cp->kcq1.dma.pg_map_arr[0] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00005033 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005034 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00005035 (u64) cp->kcq1.dma.pg_map_arr[0] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00005036 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005037 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_VALID_OFFSET(pfid, 0), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00005038 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005039 CSTORM_ISCSI_EQ_SB_NUM_OFFSET(pfid, 0), cp->status_blk_num);
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_SB_INDEX_OFFSET(pfid, 0),
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005042 HC_INDEX_ISCSI_EQ_CONS);
Michael Chan71034ba2009-10-10 13:46:59 +00005043
Michael Chan71034ba2009-10-10 13:46:59 +00005044 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005045 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00005046 cp->gbl_buf_info.pg_map_arr[0] & 0xffffffff);
5047 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005048 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00005049 (u64) cp->gbl_buf_info.pg_map_arr[0] >> 32);
5050
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005051 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
5052 TSTORM_ISCSI_TCP_LOCAL_ADV_WND_OFFSET(pfid), DEF_RCV_BUF);
5053
Michael Chan71034ba2009-10-10 13:46:59 +00005054 cnic_setup_bnx2x_context(dev);
5055
Michael Chan71034ba2009-10-10 13:46:59 +00005056 ret = cnic_init_bnx2x_irq(dev);
5057 if (ret)
5058 return ret;
5059
Michael Chan71034ba2009-10-10 13:46:59 +00005060 return 0;
5061}
5062
Michael Chan86b53602009-10-10 13:46:57 +00005063static void cnic_init_rings(struct cnic_dev *dev)
5064{
Michael Chan541a7812010-10-06 03:17:22 +00005065 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00005066 struct cnic_uio_dev *udev = cp->udev;
Michael Chan541a7812010-10-06 03:17:22 +00005067
5068 if (test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
5069 return;
5070
Michael Chan86b53602009-10-10 13:46:57 +00005071 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
5072 cnic_init_bnx2_tx_ring(dev);
5073 cnic_init_bnx2_rx_ring(dev);
Michael Chan541a7812010-10-06 03:17:22 +00005074 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00005075 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
Michael Chan5159fdc2010-12-23 07:42:59 +00005076 u32 cli = cp->ethdev->iscsi_l2_client_id;
5077 u32 cid = cp->ethdev->iscsi_l2_cid;
Michael Chan68d7c1a2011-01-05 15:14:13 +00005078 u32 cl_qzone_id;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005079 struct client_init_ramrod_data *data;
Michael Chan71034ba2009-10-10 13:46:59 +00005080 union l5cm_specific_data l5_data;
5081 struct ustorm_eth_rx_producers rx_prods = {0};
Michael Chane1dd8832011-07-13 17:24:19 +00005082 u32 off, i, *cid_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00005083
5084 rx_prods.bd_prod = 0;
5085 rx_prods.cqe_prod = BNX2X_MAX_RCQ_DESC_CNT;
5086 barrier();
5087
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005088 cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
5089
Michael Chanc7596b72009-12-02 15:15:35 +00005090 off = BAR_USTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005091 (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id) ?
Michael Chanee87a822010-10-13 14:06:51 +00005092 USTORM_RX_PRODS_E2_OFFSET(cl_qzone_id) :
5093 USTORM_RX_PRODS_E1X_OFFSET(CNIC_PORT(cp), cli));
Michael Chan71034ba2009-10-10 13:46:59 +00005094
5095 for (i = 0; i < sizeof(struct ustorm_eth_rx_producers) / 4; i++)
Michael Chanc7596b72009-12-02 15:15:35 +00005096 CNIC_WR(dev, off + i * 4, ((u32 *) &rx_prods)[i]);
Michael Chan71034ba2009-10-10 13:46:59 +00005097
Michael Chan48f753d2010-05-18 11:32:53 +00005098 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
5099
Michael Chancd801532010-10-13 14:06:49 +00005100 data = udev->l2_buf;
Michael Chane1dd8832011-07-13 17:24:19 +00005101 cid_ptr = udev->l2_buf + 12;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005102
5103 memset(data, 0, sizeof(*data));
5104
5105 cnic_init_bnx2x_tx_ring(dev, data);
5106 cnic_init_bnx2x_rx_ring(dev, data);
5107
Michael Chancd801532010-10-13 14:06:49 +00005108 l5_data.phy_address.lo = udev->l2_buf_map & 0xffffffff;
5109 l5_data.phy_address.hi = (u64) udev->l2_buf_map >> 32;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005110
Michael Chan541a7812010-10-06 03:17:22 +00005111 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
5112
Michael Chan71034ba2009-10-10 13:46:59 +00005113 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_CLIENT_SETUP,
Michael Chan68d7c1a2011-01-05 15:14:13 +00005114 cid, ETH_CONNECTION_TYPE, &l5_data);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005115
Michael Chan48f753d2010-05-18 11:32:53 +00005116 i = 0;
5117 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
5118 ++i < 10)
5119 msleep(1);
5120
5121 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
5122 netdev_err(dev->netdev,
5123 "iSCSI CLIENT_SETUP did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00005124 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan5159fdc2010-12-23 07:42:59 +00005125 cnic_ring_ctl(dev, cid, cli, 1);
Michael Chane1dd8832011-07-13 17:24:19 +00005126 *cid_ptr = cid;
Michael Chan86b53602009-10-10 13:46:57 +00005127 }
5128}
5129
5130static void cnic_shutdown_rings(struct cnic_dev *dev)
5131{
Michael Chan541a7812010-10-06 03:17:22 +00005132 struct cnic_local *cp = dev->cnic_priv;
Michael Chane1dd8832011-07-13 17:24:19 +00005133 struct cnic_uio_dev *udev = cp->udev;
5134 void *rx_ring;
Michael Chan541a7812010-10-06 03:17:22 +00005135
5136 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
5137 return;
5138
Michael Chan86b53602009-10-10 13:46:57 +00005139 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
5140 cnic_shutdown_bnx2_rx_ring(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005141 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
Michael Chan5159fdc2010-12-23 07:42:59 +00005142 u32 cli = cp->ethdev->iscsi_l2_client_id;
5143 u32 cid = cp->ethdev->iscsi_l2_cid;
Michael Chan8b065b62009-12-02 15:15:36 +00005144 union l5cm_specific_data l5_data;
Michael Chan48f753d2010-05-18 11:32:53 +00005145 int i;
Michael Chan71034ba2009-10-10 13:46:59 +00005146
Michael Chan5159fdc2010-12-23 07:42:59 +00005147 cnic_ring_ctl(dev, cid, cli, 0);
Michael Chan8b065b62009-12-02 15:15:36 +00005148
Michael Chan48f753d2010-05-18 11:32:53 +00005149 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
5150
Michael Chan8b065b62009-12-02 15:15:36 +00005151 l5_data.phy_address.lo = cli;
5152 l5_data.phy_address.hi = 0;
5153 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_HALT,
Michael Chan5159fdc2010-12-23 07:42:59 +00005154 cid, ETH_CONNECTION_TYPE, &l5_data);
Michael Chan48f753d2010-05-18 11:32:53 +00005155 i = 0;
5156 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
5157 ++i < 10)
5158 msleep(1);
5159
5160 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
5161 netdev_err(dev->netdev,
5162 "iSCSI CLIENT_HALT did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00005163 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan1bcdc322009-12-10 15:40:57 +00005164
5165 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005166 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
Michael Chan68d7c1a2011-01-05 15:14:13 +00005167 cid, NONE_CONNECTION_TYPE, &l5_data);
Michael Chan1bcdc322009-12-10 15:40:57 +00005168 msleep(10);
Michael Chan86b53602009-10-10 13:46:57 +00005169 }
Michael Chan541a7812010-10-06 03:17:22 +00005170 clear_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chane1dd8832011-07-13 17:24:19 +00005171 rx_ring = udev->l2_ring + BCM_PAGE_SIZE;
5172 memset(rx_ring, 0, BCM_PAGE_SIZE);
Michael Chan86b53602009-10-10 13:46:57 +00005173}
5174
Michael Chana3059b12009-08-14 15:49:44 +00005175static int cnic_register_netdev(struct cnic_dev *dev)
5176{
5177 struct cnic_local *cp = dev->cnic_priv;
5178 struct cnic_eth_dev *ethdev = cp->ethdev;
5179 int err;
5180
5181 if (!ethdev)
5182 return -ENODEV;
5183
5184 if (ethdev->drv_state & CNIC_DRV_STATE_REGD)
5185 return 0;
5186
5187 err = ethdev->drv_register_cnic(dev->netdev, cp->cnic_ops, dev);
5188 if (err)
Joe Perchesddf79b22010-02-17 15:01:54 +00005189 netdev_err(dev->netdev, "register_cnic failed\n");
Michael Chana3059b12009-08-14 15:49:44 +00005190
5191 return err;
5192}
5193
5194static void cnic_unregister_netdev(struct cnic_dev *dev)
5195{
5196 struct cnic_local *cp = dev->cnic_priv;
5197 struct cnic_eth_dev *ethdev = cp->ethdev;
5198
5199 if (!ethdev)
5200 return;
5201
5202 ethdev->drv_unregister_cnic(dev->netdev);
5203}
5204
Michael Chana4636962009-06-08 18:14:43 -07005205static int cnic_start_hw(struct cnic_dev *dev)
5206{
5207 struct cnic_local *cp = dev->cnic_priv;
5208 struct cnic_eth_dev *ethdev = cp->ethdev;
5209 int err;
5210
5211 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
5212 return -EALREADY;
5213
Michael Chana4636962009-06-08 18:14:43 -07005214 dev->regview = ethdev->io_base;
Michael Chana4636962009-06-08 18:14:43 -07005215 pci_dev_get(dev->pcidev);
5216 cp->func = PCI_FUNC(dev->pcidev->devfn);
Michael Chana4dde3a2010-02-24 14:42:08 +00005217 cp->status_blk.gen = ethdev->irq_arr[0].status_blk;
Michael Chana4636962009-06-08 18:14:43 -07005218 cp->status_blk_num = ethdev->irq_arr[0].status_blk_num;
5219
5220 err = cp->alloc_resc(dev);
5221 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00005222 netdev_err(dev->netdev, "allocate resource failure\n");
Michael Chana4636962009-06-08 18:14:43 -07005223 goto err1;
5224 }
5225
5226 err = cp->start_hw(dev);
5227 if (err)
5228 goto err1;
5229
5230 err = cnic_cm_open(dev);
5231 if (err)
5232 goto err1;
5233
5234 set_bit(CNIC_F_CNIC_UP, &dev->flags);
5235
5236 cp->enable_int(dev);
5237
5238 return 0;
5239
5240err1:
Michael Chana4636962009-06-08 18:14:43 -07005241 cp->free_resc(dev);
5242 pci_dev_put(dev->pcidev);
Michael Chana4636962009-06-08 18:14:43 -07005243 return err;
5244}
5245
5246static void cnic_stop_bnx2_hw(struct cnic_dev *dev)
5247{
Michael Chana4636962009-06-08 18:14:43 -07005248 cnic_disable_bnx2_int_sync(dev);
5249
5250 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
5251 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
5252
5253 cnic_init_context(dev, KWQ_CID);
5254 cnic_init_context(dev, KCQ_CID);
5255
5256 cnic_setup_5709_context(dev, 0);
5257 cnic_free_irq(dev);
5258
Michael Chana4636962009-06-08 18:14:43 -07005259 cnic_free_resc(dev);
5260}
5261
Michael Chan71034ba2009-10-10 13:46:59 +00005262
5263static void cnic_stop_bnx2x_hw(struct cnic_dev *dev)
5264{
5265 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00005266
5267 cnic_free_irq(dev);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005268 *cp->kcq1.hw_prod_idx_ptr = 0;
Michael Chan4e9c4fd2009-12-10 15:40:58 +00005269 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005270 CSTORM_ISCSI_EQ_CONS_OFFSET(cp->pfid, 0), 0);
Michael Chane6c28892010-06-24 14:58:39 +00005271 CNIC_WR16(dev, cp->kcq1.io_addr, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00005272 cnic_free_resc(dev);
5273}
5274
Michael Chana4636962009-06-08 18:14:43 -07005275static void cnic_stop_hw(struct cnic_dev *dev)
5276{
5277 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
5278 struct cnic_local *cp = dev->cnic_priv;
Michael Chan48f753d2010-05-18 11:32:53 +00005279 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -07005280
Michael Chan48f753d2010-05-18 11:32:53 +00005281 /* Need to wait for the ring shutdown event to complete
5282 * before clearing the CNIC_UP flag.
5283 */
Michael Chancd801532010-10-13 14:06:49 +00005284 while (cp->udev->uio_dev != -1 && i < 15) {
Michael Chan48f753d2010-05-18 11:32:53 +00005285 msleep(100);
5286 i++;
5287 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005288 cnic_shutdown_rings(dev);
Michael Chana4636962009-06-08 18:14:43 -07005289 clear_bit(CNIC_F_CNIC_UP, &dev->flags);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +00005290 RCU_INIT_POINTER(cp->ulp_ops[CNIC_ULP_L4], NULL);
Michael Chana4636962009-06-08 18:14:43 -07005291 synchronize_rcu();
5292 cnic_cm_shutdown(dev);
5293 cp->stop_hw(dev);
5294 pci_dev_put(dev->pcidev);
5295 }
5296}
5297
5298static void cnic_free_dev(struct cnic_dev *dev)
5299{
5300 int i = 0;
5301
5302 while ((atomic_read(&dev->ref_count) != 0) && i < 10) {
5303 msleep(100);
5304 i++;
5305 }
5306 if (atomic_read(&dev->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +00005307 netdev_err(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -07005308
Joe Perchesddf79b22010-02-17 15:01:54 +00005309 netdev_info(dev->netdev, "Removed CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07005310 dev_put(dev->netdev);
5311 kfree(dev);
5312}
5313
5314static struct cnic_dev *cnic_alloc_dev(struct net_device *dev,
5315 struct pci_dev *pdev)
5316{
5317 struct cnic_dev *cdev;
5318 struct cnic_local *cp;
5319 int alloc_size;
5320
5321 alloc_size = sizeof(struct cnic_dev) + sizeof(struct cnic_local);
5322
5323 cdev = kzalloc(alloc_size , GFP_KERNEL);
5324 if (cdev == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +00005325 netdev_err(dev, "allocate dev struct failure\n");
Michael Chana4636962009-06-08 18:14:43 -07005326 return NULL;
5327 }
5328
5329 cdev->netdev = dev;
5330 cdev->cnic_priv = (char *)cdev + sizeof(struct cnic_dev);
5331 cdev->register_device = cnic_register_device;
5332 cdev->unregister_device = cnic_unregister_device;
5333 cdev->iscsi_nl_msg_recv = cnic_iscsi_nl_msg_recv;
5334
5335 cp = cdev->cnic_priv;
5336 cp->dev = cdev;
Michael Chana4636962009-06-08 18:14:43 -07005337 cp->l2_single_buf_size = 0x400;
5338 cp->l2_rx_ring_size = 3;
5339
5340 spin_lock_init(&cp->cnic_ulp_lock);
5341
Joe Perchesddf79b22010-02-17 15:01:54 +00005342 netdev_info(dev, "Added CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07005343
5344 return cdev;
5345}
5346
5347static struct cnic_dev *init_bnx2_cnic(struct net_device *dev)
5348{
5349 struct pci_dev *pdev;
5350 struct cnic_dev *cdev;
5351 struct cnic_local *cp;
5352 struct cnic_eth_dev *ethdev = NULL;
Michael Chane2ee3612009-06-13 17:43:02 -07005353 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
Michael Chana4636962009-06-08 18:14:43 -07005354
Michael Chane2ee3612009-06-13 17:43:02 -07005355 probe = symbol_get(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07005356 if (probe) {
5357 ethdev = (*probe)(dev);
Michael Chan64c64602009-08-14 15:49:43 +00005358 symbol_put(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07005359 }
5360 if (!ethdev)
5361 return NULL;
5362
5363 pdev = ethdev->pdev;
5364 if (!pdev)
5365 return NULL;
5366
5367 dev_hold(dev);
5368 pci_dev_get(pdev);
Sergei Shtylyovff938e42011-02-28 11:57:33 -08005369 if ((pdev->device == PCI_DEVICE_ID_NX2_5709 ||
5370 pdev->device == PCI_DEVICE_ID_NX2_5709S) &&
5371 (pdev->revision < 0x10)) {
5372 pci_dev_put(pdev);
5373 goto cnic_err;
Michael Chana4636962009-06-08 18:14:43 -07005374 }
5375 pci_dev_put(pdev);
5376
5377 cdev = cnic_alloc_dev(dev, pdev);
5378 if (cdev == NULL)
5379 goto cnic_err;
5380
5381 set_bit(CNIC_F_BNX2_CLASS, &cdev->flags);
5382 cdev->submit_kwqes = cnic_submit_bnx2_kwqes;
5383
5384 cp = cdev->cnic_priv;
5385 cp->ethdev = ethdev;
5386 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00005387 cp->chip_id = ethdev->chip_id;
Michael Chana4636962009-06-08 18:14:43 -07005388
Michael Chan7625eb22011-06-08 19:29:36 +00005389 cdev->max_iscsi_conn = ethdev->max_iscsi_conn;
5390
Michael Chana4636962009-06-08 18:14:43 -07005391 cp->cnic_ops = &cnic_bnx2_ops;
5392 cp->start_hw = cnic_start_bnx2_hw;
5393 cp->stop_hw = cnic_stop_bnx2_hw;
5394 cp->setup_pgtbl = cnic_setup_page_tbl;
5395 cp->alloc_resc = cnic_alloc_bnx2_resc;
5396 cp->free_resc = cnic_free_resc;
5397 cp->start_cm = cnic_cm_init_bnx2_hw;
5398 cp->stop_cm = cnic_cm_stop_bnx2_hw;
5399 cp->enable_int = cnic_enable_bnx2_int;
5400 cp->disable_int_sync = cnic_disable_bnx2_int_sync;
5401 cp->close_conn = cnic_close_bnx2_conn;
Michael Chana4636962009-06-08 18:14:43 -07005402 return cdev;
5403
5404cnic_err:
5405 dev_put(dev);
5406 return NULL;
5407}
5408
Michael Chan71034ba2009-10-10 13:46:59 +00005409static struct cnic_dev *init_bnx2x_cnic(struct net_device *dev)
5410{
5411 struct pci_dev *pdev;
5412 struct cnic_dev *cdev;
5413 struct cnic_local *cp;
5414 struct cnic_eth_dev *ethdev = NULL;
5415 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
5416
5417 probe = symbol_get(bnx2x_cnic_probe);
5418 if (probe) {
5419 ethdev = (*probe)(dev);
5420 symbol_put(bnx2x_cnic_probe);
5421 }
5422 if (!ethdev)
5423 return NULL;
5424
5425 pdev = ethdev->pdev;
5426 if (!pdev)
5427 return NULL;
5428
5429 dev_hold(dev);
5430 cdev = cnic_alloc_dev(dev, pdev);
5431 if (cdev == NULL) {
5432 dev_put(dev);
5433 return NULL;
5434 }
5435
5436 set_bit(CNIC_F_BNX2X_CLASS, &cdev->flags);
5437 cdev->submit_kwqes = cnic_submit_bnx2x_kwqes;
5438
5439 cp = cdev->cnic_priv;
5440 cp->ethdev = ethdev;
5441 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00005442 cp->chip_id = ethdev->chip_id;
Michael Chan71034ba2009-10-10 13:46:59 +00005443
Barak Witkowski1d187b32011-12-05 22:41:50 +00005444 cdev->stats_addr = ethdev->addr_drv_info_to_mcp;
5445
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00005446 if (!(ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI))
5447 cdev->max_iscsi_conn = ethdev->max_iscsi_conn;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005448 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id) &&
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00005449 !(ethdev->drv_state & CNIC_DRV_STATE_NO_FCOE))
5450 cdev->max_fcoe_conn = ethdev->max_fcoe_conn;
5451
Michael Chandc219a22011-08-26 09:45:39 +00005452 if (cdev->max_fcoe_conn > BNX2X_FCOE_NUM_CONNECTIONS)
5453 cdev->max_fcoe_conn = BNX2X_FCOE_NUM_CONNECTIONS;
5454
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00005455 memcpy(cdev->mac_addr, ethdev->iscsi_mac, 6);
5456
Michael Chan71034ba2009-10-10 13:46:59 +00005457 cp->cnic_ops = &cnic_bnx2x_ops;
5458 cp->start_hw = cnic_start_bnx2x_hw;
5459 cp->stop_hw = cnic_stop_bnx2x_hw;
5460 cp->setup_pgtbl = cnic_setup_page_tbl_le;
5461 cp->alloc_resc = cnic_alloc_bnx2x_resc;
5462 cp->free_resc = cnic_free_resc;
5463 cp->start_cm = cnic_cm_init_bnx2x_hw;
5464 cp->stop_cm = cnic_cm_stop_bnx2x_hw;
5465 cp->enable_int = cnic_enable_bnx2x_int;
5466 cp->disable_int_sync = cnic_disable_bnx2x_int_sync;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005467 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id))
Michael Chanee87a822010-10-13 14:06:51 +00005468 cp->ack_int = cnic_ack_bnx2x_e2_msix;
5469 else
5470 cp->ack_int = cnic_ack_bnx2x_msix;
Michael Chan71034ba2009-10-10 13:46:59 +00005471 cp->close_conn = cnic_close_bnx2x_conn;
Michael Chan71034ba2009-10-10 13:46:59 +00005472 return cdev;
5473}
5474
Michael Chana4636962009-06-08 18:14:43 -07005475static struct cnic_dev *is_cnic_dev(struct net_device *dev)
5476{
5477 struct ethtool_drvinfo drvinfo;
5478 struct cnic_dev *cdev = NULL;
5479
5480 if (dev->ethtool_ops && dev->ethtool_ops->get_drvinfo) {
5481 memset(&drvinfo, 0, sizeof(drvinfo));
5482 dev->ethtool_ops->get_drvinfo(dev, &drvinfo);
5483
5484 if (!strcmp(drvinfo.driver, "bnx2"))
5485 cdev = init_bnx2_cnic(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005486 if (!strcmp(drvinfo.driver, "bnx2x"))
5487 cdev = init_bnx2x_cnic(dev);
Michael Chana4636962009-06-08 18:14:43 -07005488 if (cdev) {
5489 write_lock(&cnic_dev_lock);
5490 list_add(&cdev->list, &cnic_dev_list);
5491 write_unlock(&cnic_dev_lock);
5492 }
5493 }
5494 return cdev;
5495}
5496
Michael Chan415199f2011-07-20 14:55:24 +00005497static void cnic_rcv_netevent(struct cnic_local *cp, unsigned long event,
5498 u16 vlan_id)
5499{
5500 int if_type;
5501
5502 rcu_read_lock();
5503 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
5504 struct cnic_ulp_ops *ulp_ops;
5505 void *ctx;
5506
5507 ulp_ops = rcu_dereference(cp->ulp_ops[if_type]);
5508 if (!ulp_ops || !ulp_ops->indicate_netevent)
5509 continue;
5510
5511 ctx = cp->ulp_handle[if_type];
5512
5513 ulp_ops->indicate_netevent(ctx, event, vlan_id);
5514 }
5515 rcu_read_unlock();
5516}
5517
Michael Chana4636962009-06-08 18:14:43 -07005518/**
5519 * netdev event handler
5520 */
5521static int cnic_netdev_event(struct notifier_block *this, unsigned long event,
5522 void *ptr)
5523{
5524 struct net_device *netdev = ptr;
5525 struct cnic_dev *dev;
Michael Chana4636962009-06-08 18:14:43 -07005526 int new_dev = 0;
5527
5528 dev = cnic_from_netdev(netdev);
5529
Michael Chandb1d3502011-06-08 19:29:35 +00005530 if (!dev && (event == NETDEV_REGISTER || netif_running(netdev))) {
Michael Chana4636962009-06-08 18:14:43 -07005531 /* Check for the hot-plug device */
5532 dev = is_cnic_dev(netdev);
5533 if (dev) {
5534 new_dev = 1;
5535 cnic_hold(dev);
5536 }
5537 }
5538 if (dev) {
5539 struct cnic_local *cp = dev->cnic_priv;
5540
5541 if (new_dev)
5542 cnic_ulp_init(dev);
5543 else if (event == NETDEV_UNREGISTER)
5544 cnic_ulp_exit(dev);
Michael Chan6053bbf2009-10-02 11:03:28 -07005545
Michael Chandb1d3502011-06-08 19:29:35 +00005546 if (event == NETDEV_UP || (new_dev && netif_running(netdev))) {
Michael Chana3059b12009-08-14 15:49:44 +00005547 if (cnic_register_netdev(dev) != 0) {
5548 cnic_put(dev);
5549 goto done;
5550 }
Michael Chana4636962009-06-08 18:14:43 -07005551 if (!cnic_start_hw(dev))
5552 cnic_ulp_start(dev);
Michael Chana4636962009-06-08 18:14:43 -07005553 }
5554
Michael Chan415199f2011-07-20 14:55:24 +00005555 cnic_rcv_netevent(cp, event, 0);
Michael Chana4636962009-06-08 18:14:43 -07005556
5557 if (event == NETDEV_GOING_DOWN) {
Michael Chana4636962009-06-08 18:14:43 -07005558 cnic_ulp_stop(dev);
5559 cnic_stop_hw(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005560 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005561 } else if (event == NETDEV_UNREGISTER) {
5562 write_lock(&cnic_dev_lock);
5563 list_del_init(&dev->list);
5564 write_unlock(&cnic_dev_lock);
5565
5566 cnic_put(dev);
5567 cnic_free_dev(dev);
5568 goto done;
5569 }
5570 cnic_put(dev);
Michael Chan415199f2011-07-20 14:55:24 +00005571 } else {
5572 struct net_device *realdev;
5573 u16 vid;
5574
5575 vid = cnic_get_vlan(netdev, &realdev);
5576 if (realdev) {
5577 dev = cnic_from_netdev(realdev);
5578 if (dev) {
5579 vid |= VLAN_TAG_PRESENT;
5580 cnic_rcv_netevent(dev->cnic_priv, event, vid);
5581 cnic_put(dev);
5582 }
5583 }
Michael Chana4636962009-06-08 18:14:43 -07005584 }
5585done:
5586 return NOTIFY_DONE;
5587}
5588
5589static struct notifier_block cnic_netdev_notifier = {
5590 .notifier_call = cnic_netdev_event
5591};
5592
5593static void cnic_release(void)
5594{
5595 struct cnic_dev *dev;
Michael Chana3ceeeb2010-10-13 14:06:50 +00005596 struct cnic_uio_dev *udev;
Michael Chana4636962009-06-08 18:14:43 -07005597
5598 while (!list_empty(&cnic_dev_list)) {
5599 dev = list_entry(cnic_dev_list.next, struct cnic_dev, list);
5600 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
5601 cnic_ulp_stop(dev);
5602 cnic_stop_hw(dev);
5603 }
5604
5605 cnic_ulp_exit(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005606 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005607 list_del_init(&dev->list);
5608 cnic_free_dev(dev);
5609 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005610 while (!list_empty(&cnic_udev_list)) {
5611 udev = list_entry(cnic_udev_list.next, struct cnic_uio_dev,
5612 list);
5613 cnic_free_uio(udev);
5614 }
Michael Chana4636962009-06-08 18:14:43 -07005615}
5616
5617static int __init cnic_init(void)
5618{
5619 int rc = 0;
5620
Joe Perchesddf79b22010-02-17 15:01:54 +00005621 pr_info("%s", version);
Michael Chana4636962009-06-08 18:14:43 -07005622
5623 rc = register_netdevice_notifier(&cnic_netdev_notifier);
5624 if (rc) {
5625 cnic_release();
5626 return rc;
5627 }
5628
Michael Chanfdf24082010-10-13 14:06:47 +00005629 cnic_wq = create_singlethread_workqueue("cnic_wq");
5630 if (!cnic_wq) {
5631 cnic_release();
5632 unregister_netdevice_notifier(&cnic_netdev_notifier);
5633 return -ENOMEM;
5634 }
5635
Michael Chana4636962009-06-08 18:14:43 -07005636 return 0;
5637}
5638
5639static void __exit cnic_exit(void)
5640{
5641 unregister_netdevice_notifier(&cnic_netdev_notifier);
5642 cnic_release();
Michael Chanfdf24082010-10-13 14:06:47 +00005643 destroy_workqueue(cnic_wq);
Michael Chana4636962009-06-08 18:14:43 -07005644}
5645
5646module_init(cnic_init);
5647module_exit(cnic_exit);