blob: 22ad7b6d904883b84a9c1e4af80c86f47b344a95 [file] [log] [blame]
Michael Chana4636962009-06-08 18:14:43 -07001/* cnic.c: Broadcom CNIC core network driver.
2 *
Michael Chan3238a9b2012-02-05 15:24:40 +00003 * Copyright (c) 2006-2012 Broadcom Corporation
Michael Chana4636962009-06-08 18:14:43 -07004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation.
8 *
9 * Original skeleton written by: John(Zongxi) Chen (zongxi@broadcom.com)
10 * Modified and maintained by: Michael Chan <mchan@broadcom.com>
11 */
12
Joe Perchesddf79b22010-02-17 15:01:54 +000013#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
Michael Chana4636962009-06-08 18:14:43 -070015#include <linux/module.h>
16
17#include <linux/kernel.h>
18#include <linux/errno.h>
19#include <linux/list.h>
20#include <linux/slab.h>
21#include <linux/pci.h>
22#include <linux/init.h>
23#include <linux/netdevice.h>
24#include <linux/uio_driver.h>
25#include <linux/in.h>
26#include <linux/dma-mapping.h>
27#include <linux/delay.h>
28#include <linux/ethtool.h>
29#include <linux/if_vlan.h>
Paul Gortmaker70c71602011-05-22 16:47:17 -040030#include <linux/prefetch.h>
Michael Chan973e5742011-07-13 17:24:17 +000031#include <linux/random.h>
Michael Chana4636962009-06-08 18:14:43 -070032#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
33#define BCM_VLAN 1
34#endif
35#include <net/ip.h>
36#include <net/tcp.h>
37#include <net/route.h>
38#include <net/ipv6.h>
39#include <net/ip6_route.h>
David S. Millerc05e85a2009-10-12 23:18:35 -070040#include <net/ip6_checksum.h>
Michael Chana4636962009-06-08 18:14:43 -070041#include <scsi/iscsi_if.h>
42
43#include "cnic_if.h"
44#include "bnx2.h"
Dmitry Kravkov5d1e8592010-07-27 12:31:10 +000045#include "bnx2x/bnx2x_reg.h"
46#include "bnx2x/bnx2x_fw_defs.h"
47#include "bnx2x/bnx2x_hsi.h"
Jeff Kirsheradfc5212011-04-07 06:03:04 -070048#include "../../../scsi/bnx2i/57xx_iscsi_constants.h"
49#include "../../../scsi/bnx2i/57xx_iscsi_hsi.h"
Michael Chan8ec3e702012-03-21 15:38:34 +000050#include "../../../scsi/bnx2fc/bnx2fc_constants.h"
Michael Chana4636962009-06-08 18:14:43 -070051#include "cnic.h"
52#include "cnic_defs.h"
53
54#define DRV_MODULE_NAME "cnic"
Michael Chana4636962009-06-08 18:14:43 -070055
56static char version[] __devinitdata =
57 "Broadcom NetXtreme II CNIC Driver " DRV_MODULE_NAME " v" CNIC_MODULE_VERSION " (" CNIC_MODULE_RELDATE ")\n";
58
59MODULE_AUTHOR("Michael Chan <mchan@broadcom.com> and John(Zongxi) "
60 "Chen (zongxi@broadcom.com");
61MODULE_DESCRIPTION("Broadcom NetXtreme II CNIC Driver");
62MODULE_LICENSE("GPL");
63MODULE_VERSION(CNIC_MODULE_VERSION);
64
Michael Chan8adc92402010-12-23 07:42:57 +000065/* cnic_dev_list modifications are protected by both rtnl and cnic_dev_lock */
Michael Chana4636962009-06-08 18:14:43 -070066static LIST_HEAD(cnic_dev_list);
Michael Chana3ceeeb2010-10-13 14:06:50 +000067static LIST_HEAD(cnic_udev_list);
Michael Chana4636962009-06-08 18:14:43 -070068static DEFINE_RWLOCK(cnic_dev_lock);
69static DEFINE_MUTEX(cnic_lock);
70
Eric Dumazet13707f92011-01-26 19:28:23 +000071static struct cnic_ulp_ops __rcu *cnic_ulp_tbl[MAX_CNIC_ULP_TYPE];
72
73/* helper function, assuming cnic_lock is held */
74static inline struct cnic_ulp_ops *cnic_ulp_tbl_prot(int type)
75{
76 return rcu_dereference_protected(cnic_ulp_tbl[type],
77 lockdep_is_held(&cnic_lock));
78}
Michael Chana4636962009-06-08 18:14:43 -070079
80static int cnic_service_bnx2(void *, void *);
Michael Chan71034ba2009-10-10 13:46:59 +000081static int cnic_service_bnx2x(void *, void *);
Michael Chana4636962009-06-08 18:14:43 -070082static int cnic_ctl(void *, struct cnic_ctl_info *);
83
84static struct cnic_ops cnic_bnx2_ops = {
85 .cnic_owner = THIS_MODULE,
86 .cnic_handler = cnic_service_bnx2,
87 .cnic_ctl = cnic_ctl,
88};
89
Michael Chan71034ba2009-10-10 13:46:59 +000090static struct cnic_ops cnic_bnx2x_ops = {
91 .cnic_owner = THIS_MODULE,
92 .cnic_handler = cnic_service_bnx2x,
93 .cnic_ctl = cnic_ctl,
94};
95
Michael Chanfdf24082010-10-13 14:06:47 +000096static struct workqueue_struct *cnic_wq;
97
Michael Chan86b53602009-10-10 13:46:57 +000098static void cnic_shutdown_rings(struct cnic_dev *);
99static void cnic_init_rings(struct cnic_dev *);
Michael Chana4636962009-06-08 18:14:43 -0700100static int cnic_cm_set_pg(struct cnic_sock *);
101
102static int cnic_uio_open(struct uio_info *uinfo, struct inode *inode)
103{
Michael Chancd801532010-10-13 14:06:49 +0000104 struct cnic_uio_dev *udev = uinfo->priv;
105 struct cnic_dev *dev;
Michael Chana4636962009-06-08 18:14:43 -0700106
107 if (!capable(CAP_NET_ADMIN))
108 return -EPERM;
109
Michael Chancd801532010-10-13 14:06:49 +0000110 if (udev->uio_dev != -1)
Michael Chana4636962009-06-08 18:14:43 -0700111 return -EBUSY;
112
Michael Chan86b53602009-10-10 13:46:57 +0000113 rtnl_lock();
Michael Chancd801532010-10-13 14:06:49 +0000114 dev = udev->dev;
115
Michael Chana3ceeeb2010-10-13 14:06:50 +0000116 if (!dev || !test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
Michael Chan86b53602009-10-10 13:46:57 +0000117 rtnl_unlock();
118 return -ENODEV;
119 }
120
Michael Chancd801532010-10-13 14:06:49 +0000121 udev->uio_dev = iminor(inode);
Michael Chana4636962009-06-08 18:14:43 -0700122
Michael Chana3ceeeb2010-10-13 14:06:50 +0000123 cnic_shutdown_rings(dev);
Michael Chan86b53602009-10-10 13:46:57 +0000124 cnic_init_rings(dev);
125 rtnl_unlock();
Michael Chana4636962009-06-08 18:14:43 -0700126
127 return 0;
128}
129
130static int cnic_uio_close(struct uio_info *uinfo, struct inode *inode)
131{
Michael Chancd801532010-10-13 14:06:49 +0000132 struct cnic_uio_dev *udev = uinfo->priv;
Michael Chan6ef57a02009-09-21 15:39:37 +0000133
Michael Chancd801532010-10-13 14:06:49 +0000134 udev->uio_dev = -1;
Michael Chana4636962009-06-08 18:14:43 -0700135 return 0;
136}
137
138static inline void cnic_hold(struct cnic_dev *dev)
139{
140 atomic_inc(&dev->ref_count);
141}
142
143static inline void cnic_put(struct cnic_dev *dev)
144{
145 atomic_dec(&dev->ref_count);
146}
147
148static inline void csk_hold(struct cnic_sock *csk)
149{
150 atomic_inc(&csk->ref_count);
151}
152
153static inline void csk_put(struct cnic_sock *csk)
154{
155 atomic_dec(&csk->ref_count);
156}
157
158static struct cnic_dev *cnic_from_netdev(struct net_device *netdev)
159{
160 struct cnic_dev *cdev;
161
162 read_lock(&cnic_dev_lock);
163 list_for_each_entry(cdev, &cnic_dev_list, list) {
164 if (netdev == cdev->netdev) {
165 cnic_hold(cdev);
166 read_unlock(&cnic_dev_lock);
167 return cdev;
168 }
169 }
170 read_unlock(&cnic_dev_lock);
171 return NULL;
172}
173
Michael Chan7fc1ece2009-08-14 15:49:47 +0000174static inline void ulp_get(struct cnic_ulp_ops *ulp_ops)
175{
176 atomic_inc(&ulp_ops->ref_count);
177}
178
179static inline void ulp_put(struct cnic_ulp_ops *ulp_ops)
180{
181 atomic_dec(&ulp_ops->ref_count);
182}
183
Michael Chana4636962009-06-08 18:14:43 -0700184static void cnic_ctx_wr(struct cnic_dev *dev, u32 cid_addr, u32 off, u32 val)
185{
186 struct cnic_local *cp = dev->cnic_priv;
187 struct cnic_eth_dev *ethdev = cp->ethdev;
188 struct drv_ctl_info info;
189 struct drv_ctl_io *io = &info.data.io;
190
191 info.cmd = DRV_CTL_CTX_WR_CMD;
192 io->cid_addr = cid_addr;
193 io->offset = off;
194 io->data = val;
195 ethdev->drv_ctl(dev->netdev, &info);
196}
197
Michael Chan71034ba2009-10-10 13:46:59 +0000198static void cnic_ctx_tbl_wr(struct cnic_dev *dev, u32 off, dma_addr_t addr)
199{
200 struct cnic_local *cp = dev->cnic_priv;
201 struct cnic_eth_dev *ethdev = cp->ethdev;
202 struct drv_ctl_info info;
203 struct drv_ctl_io *io = &info.data.io;
204
205 info.cmd = DRV_CTL_CTXTBL_WR_CMD;
206 io->offset = off;
207 io->dma_addr = addr;
208 ethdev->drv_ctl(dev->netdev, &info);
209}
210
211static void cnic_ring_ctl(struct cnic_dev *dev, u32 cid, u32 cl_id, int start)
212{
213 struct cnic_local *cp = dev->cnic_priv;
214 struct cnic_eth_dev *ethdev = cp->ethdev;
215 struct drv_ctl_info info;
216 struct drv_ctl_l2_ring *ring = &info.data.ring;
217
218 if (start)
219 info.cmd = DRV_CTL_START_L2_CMD;
220 else
221 info.cmd = DRV_CTL_STOP_L2_CMD;
222
223 ring->cid = cid;
224 ring->client_id = cl_id;
225 ethdev->drv_ctl(dev->netdev, &info);
226}
227
Michael Chana4636962009-06-08 18:14:43 -0700228static void cnic_reg_wr_ind(struct cnic_dev *dev, u32 off, u32 val)
229{
230 struct cnic_local *cp = dev->cnic_priv;
231 struct cnic_eth_dev *ethdev = cp->ethdev;
232 struct drv_ctl_info info;
233 struct drv_ctl_io *io = &info.data.io;
234
235 info.cmd = DRV_CTL_IO_WR_CMD;
236 io->offset = off;
237 io->data = val;
238 ethdev->drv_ctl(dev->netdev, &info);
239}
240
241static u32 cnic_reg_rd_ind(struct cnic_dev *dev, u32 off)
242{
243 struct cnic_local *cp = dev->cnic_priv;
244 struct cnic_eth_dev *ethdev = cp->ethdev;
245 struct drv_ctl_info info;
246 struct drv_ctl_io *io = &info.data.io;
247
248 info.cmd = DRV_CTL_IO_RD_CMD;
249 io->offset = off;
250 ethdev->drv_ctl(dev->netdev, &info);
251 return io->data;
252}
253
Barak Witkowski1d187b32011-12-05 22:41:50 +0000254static void cnic_ulp_ctl(struct cnic_dev *dev, int ulp_type, bool reg)
255{
256 struct cnic_local *cp = dev->cnic_priv;
257 struct cnic_eth_dev *ethdev = cp->ethdev;
258 struct drv_ctl_info info;
Barak Witkowski2e499d32012-06-26 01:31:19 +0000259 struct fcoe_capabilities *fcoe_cap =
260 &info.data.register_data.fcoe_features;
Barak Witkowski1d187b32011-12-05 22:41:50 +0000261
Barak Witkowski2e499d32012-06-26 01:31:19 +0000262 if (reg) {
Barak Witkowski1d187b32011-12-05 22:41:50 +0000263 info.cmd = DRV_CTL_ULP_REGISTER_CMD;
Barak Witkowski2e499d32012-06-26 01:31:19 +0000264 if (ulp_type == CNIC_ULP_FCOE && dev->fcoe_cap)
265 memcpy(fcoe_cap, dev->fcoe_cap, sizeof(*fcoe_cap));
266 } else {
Barak Witkowski1d187b32011-12-05 22:41:50 +0000267 info.cmd = DRV_CTL_ULP_UNREGISTER_CMD;
Barak Witkowski2e499d32012-06-26 01:31:19 +0000268 }
Barak Witkowski1d187b32011-12-05 22:41:50 +0000269
270 info.data.ulp_type = ulp_type;
271 ethdev->drv_ctl(dev->netdev, &info);
272}
273
Michael Chana4636962009-06-08 18:14:43 -0700274static int cnic_in_use(struct cnic_sock *csk)
275{
276 return test_bit(SK_F_INUSE, &csk->flags);
277}
278
Dmitry Kravkovc2bff632010-10-06 03:33:18 +0000279static void cnic_spq_completion(struct cnic_dev *dev, int cmd, u32 count)
Michael Chana4636962009-06-08 18:14:43 -0700280{
281 struct cnic_local *cp = dev->cnic_priv;
282 struct cnic_eth_dev *ethdev = cp->ethdev;
283 struct drv_ctl_info info;
284
Dmitry Kravkovc2bff632010-10-06 03:33:18 +0000285 info.cmd = cmd;
286 info.data.credit.credit_count = count;
Michael Chana4636962009-06-08 18:14:43 -0700287 ethdev->drv_ctl(dev->netdev, &info);
288}
289
Michael Chan71034ba2009-10-10 13:46:59 +0000290static int cnic_get_l5_cid(struct cnic_local *cp, u32 cid, u32 *l5_cid)
291{
292 u32 i;
293
Michael Chana2028b232012-06-27 15:08:19 +0000294 if (!cp->ctx_tbl)
295 return -EINVAL;
296
Michael Chan520efdf2010-06-24 14:58:37 +0000297 for (i = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +0000298 if (cp->ctx_tbl[i].cid == cid) {
299 *l5_cid = i;
300 return 0;
301 }
302 }
303 return -EINVAL;
304}
305
Michael Chana4636962009-06-08 18:14:43 -0700306static int cnic_send_nlmsg(struct cnic_local *cp, u32 type,
307 struct cnic_sock *csk)
308{
309 struct iscsi_path path_req;
310 char *buf = NULL;
311 u16 len = 0;
312 u32 msg_type = ISCSI_KEVENT_IF_DOWN;
313 struct cnic_ulp_ops *ulp_ops;
Michael Chancd801532010-10-13 14:06:49 +0000314 struct cnic_uio_dev *udev = cp->udev;
Michael Chan939b82e2010-12-23 07:42:58 +0000315 int rc = 0, retry = 0;
Michael Chana4636962009-06-08 18:14:43 -0700316
Michael Chancd801532010-10-13 14:06:49 +0000317 if (!udev || udev->uio_dev == -1)
Michael Chana4636962009-06-08 18:14:43 -0700318 return -ENODEV;
319
320 if (csk) {
321 len = sizeof(path_req);
322 buf = (char *) &path_req;
323 memset(&path_req, 0, len);
324
325 msg_type = ISCSI_KEVENT_PATH_REQ;
326 path_req.handle = (u64) csk->l5_cid;
327 if (test_bit(SK_F_IPV6, &csk->flags)) {
328 memcpy(&path_req.dst.v6_addr, &csk->dst_ip[0],
329 sizeof(struct in6_addr));
330 path_req.ip_addr_len = 16;
331 } else {
332 memcpy(&path_req.dst.v4_addr, &csk->dst_ip[0],
333 sizeof(struct in_addr));
334 path_req.ip_addr_len = 4;
335 }
336 path_req.vlan_id = csk->vlan_id;
337 path_req.pmtu = csk->mtu;
338 }
339
Michael Chan939b82e2010-12-23 07:42:58 +0000340 while (retry < 3) {
341 rc = 0;
342 rcu_read_lock();
343 ulp_ops = rcu_dereference(cnic_ulp_tbl[CNIC_ULP_ISCSI]);
344 if (ulp_ops)
345 rc = ulp_ops->iscsi_nl_send_msg(
346 cp->ulp_handle[CNIC_ULP_ISCSI],
347 msg_type, buf, len);
348 rcu_read_unlock();
349 if (rc == 0 || msg_type != ISCSI_KEVENT_PATH_REQ)
350 break;
351
352 msleep(100);
353 retry++;
354 }
Michael Chan558e4c72011-07-13 17:24:20 +0000355 return rc;
Michael Chana4636962009-06-08 18:14:43 -0700356}
357
Eddie Wai42ecbb82010-12-23 07:43:02 +0000358static void cnic_cm_upcall(struct cnic_local *, struct cnic_sock *, u8);
359
Michael Chana4636962009-06-08 18:14:43 -0700360static int cnic_iscsi_nl_msg_recv(struct cnic_dev *dev, u32 msg_type,
361 char *buf, u16 len)
362{
363 int rc = -EINVAL;
364
365 switch (msg_type) {
366 case ISCSI_UEVENT_PATH_UPDATE: {
367 struct cnic_local *cp;
368 u32 l5_cid;
369 struct cnic_sock *csk;
370 struct iscsi_path *path_resp;
371
372 if (len < sizeof(*path_resp))
373 break;
374
375 path_resp = (struct iscsi_path *) buf;
376 cp = dev->cnic_priv;
377 l5_cid = (u32) path_resp->handle;
378 if (l5_cid >= MAX_CM_SK_TBL_SZ)
379 break;
380
Michael Chand02a5e62010-02-24 14:42:06 +0000381 rcu_read_lock();
382 if (!rcu_dereference(cp->ulp_ops[CNIC_ULP_L4])) {
383 rc = -ENODEV;
384 rcu_read_unlock();
385 break;
386 }
Michael Chana4636962009-06-08 18:14:43 -0700387 csk = &cp->csk_tbl[l5_cid];
388 csk_hold(csk);
Eddie Wai42ecbb82010-12-23 07:43:02 +0000389 if (cnic_in_use(csk) &&
390 test_bit(SK_F_CONNECT_START, &csk->flags)) {
391
Eddie Wai4cbbb042012-02-08 17:33:57 +0000392 csk->vlan_id = path_resp->vlan_id;
393
Michael Chana4636962009-06-08 18:14:43 -0700394 memcpy(csk->ha, path_resp->mac_addr, 6);
395 if (test_bit(SK_F_IPV6, &csk->flags))
396 memcpy(&csk->src_ip[0], &path_resp->src.v6_addr,
397 sizeof(struct in6_addr));
398 else
399 memcpy(&csk->src_ip[0], &path_resp->src.v4_addr,
400 sizeof(struct in_addr));
Eddie Wai42ecbb82010-12-23 07:43:02 +0000401
402 if (is_valid_ether_addr(csk->ha)) {
Michael Chana4636962009-06-08 18:14:43 -0700403 cnic_cm_set_pg(csk);
Eddie Wai42ecbb82010-12-23 07:43:02 +0000404 } else if (!test_bit(SK_F_OFFLD_SCHED, &csk->flags) &&
405 !test_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
406
407 cnic_cm_upcall(cp, csk,
408 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
409 clear_bit(SK_F_CONNECT_START, &csk->flags);
410 }
Michael Chana4636962009-06-08 18:14:43 -0700411 }
412 csk_put(csk);
Michael Chand02a5e62010-02-24 14:42:06 +0000413 rcu_read_unlock();
Michael Chana4636962009-06-08 18:14:43 -0700414 rc = 0;
415 }
416 }
417
418 return rc;
419}
420
421static int cnic_offld_prep(struct cnic_sock *csk)
422{
423 if (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
424 return 0;
425
426 if (!test_bit(SK_F_CONNECT_START, &csk->flags)) {
427 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
428 return 0;
429 }
430
431 return 1;
432}
433
434static int cnic_close_prep(struct cnic_sock *csk)
435{
436 clear_bit(SK_F_CONNECT_START, &csk->flags);
437 smp_mb__after_clear_bit();
438
439 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
440 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
441 msleep(1);
442
443 return 1;
444 }
445 return 0;
446}
447
448static int cnic_abort_prep(struct cnic_sock *csk)
449{
450 clear_bit(SK_F_CONNECT_START, &csk->flags);
451 smp_mb__after_clear_bit();
452
453 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
454 msleep(1);
455
456 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
457 csk->state = L4_KCQE_OPCODE_VALUE_RESET_COMP;
458 return 1;
459 }
460
461 return 0;
462}
463
464int cnic_register_driver(int ulp_type, struct cnic_ulp_ops *ulp_ops)
465{
466 struct cnic_dev *dev;
467
roel kluin0d37f362009-11-02 06:53:44 +0000468 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000469 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700470 return -EINVAL;
471 }
472 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000473 if (cnic_ulp_tbl_prot(ulp_type)) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000474 pr_err("%s: Type %d has already been registered\n",
475 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700476 mutex_unlock(&cnic_lock);
477 return -EBUSY;
478 }
479
480 read_lock(&cnic_dev_lock);
481 list_for_each_entry(dev, &cnic_dev_list, list) {
482 struct cnic_local *cp = dev->cnic_priv;
483
484 clear_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]);
485 }
486 read_unlock(&cnic_dev_lock);
487
Michael Chan7fc1ece2009-08-14 15:49:47 +0000488 atomic_set(&ulp_ops->ref_count, 0);
Michael Chana4636962009-06-08 18:14:43 -0700489 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], ulp_ops);
490 mutex_unlock(&cnic_lock);
491
492 /* Prevent race conditions with netdev_event */
493 rtnl_lock();
Michael Chana4636962009-06-08 18:14:43 -0700494 list_for_each_entry(dev, &cnic_dev_list, list) {
495 struct cnic_local *cp = dev->cnic_priv;
496
497 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]))
498 ulp_ops->cnic_init(dev);
499 }
Michael Chana4636962009-06-08 18:14:43 -0700500 rtnl_unlock();
501
502 return 0;
503}
504
505int cnic_unregister_driver(int ulp_type)
506{
507 struct cnic_dev *dev;
Michael Chan7fc1ece2009-08-14 15:49:47 +0000508 struct cnic_ulp_ops *ulp_ops;
509 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700510
roel kluin0d37f362009-11-02 06:53:44 +0000511 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000512 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700513 return -EINVAL;
514 }
515 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000516 ulp_ops = cnic_ulp_tbl_prot(ulp_type);
Michael Chan7fc1ece2009-08-14 15:49:47 +0000517 if (!ulp_ops) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000518 pr_err("%s: Type %d has not been registered\n",
519 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700520 goto out_unlock;
521 }
522 read_lock(&cnic_dev_lock);
523 list_for_each_entry(dev, &cnic_dev_list, list) {
524 struct cnic_local *cp = dev->cnic_priv;
525
526 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000527 pr_err("%s: Type %d still has devices registered\n",
528 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700529 read_unlock(&cnic_dev_lock);
530 goto out_unlock;
531 }
532 }
533 read_unlock(&cnic_dev_lock);
534
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000535 RCU_INIT_POINTER(cnic_ulp_tbl[ulp_type], NULL);
Michael Chana4636962009-06-08 18:14:43 -0700536
537 mutex_unlock(&cnic_lock);
538 synchronize_rcu();
Michael Chan7fc1ece2009-08-14 15:49:47 +0000539 while ((atomic_read(&ulp_ops->ref_count) != 0) && (i < 20)) {
540 msleep(100);
541 i++;
542 }
543
544 if (atomic_read(&ulp_ops->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +0000545 netdev_warn(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -0700546 return 0;
547
548out_unlock:
549 mutex_unlock(&cnic_lock);
550 return -EINVAL;
551}
552
553static int cnic_start_hw(struct cnic_dev *);
554static void cnic_stop_hw(struct cnic_dev *);
555
556static int cnic_register_device(struct cnic_dev *dev, int ulp_type,
557 void *ulp_ctx)
558{
559 struct cnic_local *cp = dev->cnic_priv;
560 struct cnic_ulp_ops *ulp_ops;
561
roel kluin0d37f362009-11-02 06:53:44 +0000562 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000563 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700564 return -EINVAL;
565 }
566 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000567 if (cnic_ulp_tbl_prot(ulp_type) == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000568 pr_err("%s: Driver with type %d has not been registered\n",
569 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700570 mutex_unlock(&cnic_lock);
571 return -EAGAIN;
572 }
573 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000574 pr_err("%s: Type %d has already been registered to this device\n",
575 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700576 mutex_unlock(&cnic_lock);
577 return -EBUSY;
578 }
579
580 clear_bit(ULP_F_START, &cp->ulp_flags[ulp_type]);
581 cp->ulp_handle[ulp_type] = ulp_ctx;
Eric Dumazet13707f92011-01-26 19:28:23 +0000582 ulp_ops = cnic_ulp_tbl_prot(ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700583 rcu_assign_pointer(cp->ulp_ops[ulp_type], ulp_ops);
584 cnic_hold(dev);
585
586 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
587 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[ulp_type]))
588 ulp_ops->cnic_start(cp->ulp_handle[ulp_type]);
589
590 mutex_unlock(&cnic_lock);
591
Barak Witkowski1d187b32011-12-05 22:41:50 +0000592 cnic_ulp_ctl(dev, ulp_type, true);
593
Michael Chana4636962009-06-08 18:14:43 -0700594 return 0;
595
596}
597EXPORT_SYMBOL(cnic_register_driver);
598
599static int cnic_unregister_device(struct cnic_dev *dev, int ulp_type)
600{
601 struct cnic_local *cp = dev->cnic_priv;
Michael Chan681dbd72009-08-14 15:49:46 +0000602 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700603
roel kluin0d37f362009-11-02 06:53:44 +0000604 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000605 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700606 return -EINVAL;
607 }
608 mutex_lock(&cnic_lock);
609 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Eric Dumazet2cfa5a02011-11-23 07:09:32 +0000610 RCU_INIT_POINTER(cp->ulp_ops[ulp_type], NULL);
Michael Chana4636962009-06-08 18:14:43 -0700611 cnic_put(dev);
612 } else {
Joe Perchesddf79b22010-02-17 15:01:54 +0000613 pr_err("%s: device not registered to this ulp type %d\n",
614 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700615 mutex_unlock(&cnic_lock);
616 return -EINVAL;
617 }
618 mutex_unlock(&cnic_lock);
619
Michael Chan42bb8d52011-01-03 15:21:46 +0000620 if (ulp_type == CNIC_ULP_ISCSI)
621 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
Barak Witkowski2e499d32012-06-26 01:31:19 +0000622 else if (ulp_type == CNIC_ULP_FCOE)
623 dev->fcoe_cap = NULL;
Michael Chan42bb8d52011-01-03 15:21:46 +0000624
Michael Chana4636962009-06-08 18:14:43 -0700625 synchronize_rcu();
626
Michael Chan681dbd72009-08-14 15:49:46 +0000627 while (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]) &&
628 i < 20) {
629 msleep(100);
630 i++;
631 }
632 if (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]))
Joe Perchesddf79b22010-02-17 15:01:54 +0000633 netdev_warn(dev->netdev, "Failed waiting for ULP up call to complete\n");
Michael Chan681dbd72009-08-14 15:49:46 +0000634
Barak Witkowski1d187b32011-12-05 22:41:50 +0000635 cnic_ulp_ctl(dev, ulp_type, false);
636
Michael Chana4636962009-06-08 18:14:43 -0700637 return 0;
638}
639EXPORT_SYMBOL(cnic_unregister_driver);
640
Eddie Wai11f23aa2011-06-08 19:29:34 +0000641static int cnic_init_id_tbl(struct cnic_id_tbl *id_tbl, u32 size, u32 start_id,
642 u32 next)
Michael Chana4636962009-06-08 18:14:43 -0700643{
644 id_tbl->start = start_id;
645 id_tbl->max = size;
Eddie Wai11f23aa2011-06-08 19:29:34 +0000646 id_tbl->next = next;
Michael Chana4636962009-06-08 18:14:43 -0700647 spin_lock_init(&id_tbl->lock);
648 id_tbl->table = kzalloc(DIV_ROUND_UP(size, 32) * 4, GFP_KERNEL);
649 if (!id_tbl->table)
650 return -ENOMEM;
651
652 return 0;
653}
654
655static void cnic_free_id_tbl(struct cnic_id_tbl *id_tbl)
656{
657 kfree(id_tbl->table);
658 id_tbl->table = NULL;
659}
660
661static int cnic_alloc_id(struct cnic_id_tbl *id_tbl, u32 id)
662{
663 int ret = -1;
664
665 id -= id_tbl->start;
666 if (id >= id_tbl->max)
667 return ret;
668
669 spin_lock(&id_tbl->lock);
670 if (!test_bit(id, id_tbl->table)) {
671 set_bit(id, id_tbl->table);
672 ret = 0;
673 }
674 spin_unlock(&id_tbl->lock);
675 return ret;
676}
677
678/* Returns -1 if not successful */
679static u32 cnic_alloc_new_id(struct cnic_id_tbl *id_tbl)
680{
681 u32 id;
682
683 spin_lock(&id_tbl->lock);
684 id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
685 if (id >= id_tbl->max) {
686 id = -1;
687 if (id_tbl->next != 0) {
688 id = find_first_zero_bit(id_tbl->table, id_tbl->next);
689 if (id >= id_tbl->next)
690 id = -1;
691 }
692 }
693
694 if (id < id_tbl->max) {
695 set_bit(id, id_tbl->table);
696 id_tbl->next = (id + 1) & (id_tbl->max - 1);
697 id += id_tbl->start;
698 }
699
700 spin_unlock(&id_tbl->lock);
701
702 return id;
703}
704
705static void cnic_free_id(struct cnic_id_tbl *id_tbl, u32 id)
706{
707 if (id == -1)
708 return;
709
710 id -= id_tbl->start;
711 if (id >= id_tbl->max)
712 return;
713
714 clear_bit(id, id_tbl->table);
715}
716
717static void cnic_free_dma(struct cnic_dev *dev, struct cnic_dma *dma)
718{
719 int i;
720
721 if (!dma->pg_arr)
722 return;
723
724 for (i = 0; i < dma->num_pages; i++) {
725 if (dma->pg_arr[i]) {
Michael Chan3248e162009-12-02 15:15:39 +0000726 dma_free_coherent(&dev->pcidev->dev, BCM_PAGE_SIZE,
727 dma->pg_arr[i], dma->pg_map_arr[i]);
Michael Chana4636962009-06-08 18:14:43 -0700728 dma->pg_arr[i] = NULL;
729 }
730 }
731 if (dma->pgtbl) {
Michael Chan3248e162009-12-02 15:15:39 +0000732 dma_free_coherent(&dev->pcidev->dev, dma->pgtbl_size,
733 dma->pgtbl, dma->pgtbl_map);
Michael Chana4636962009-06-08 18:14:43 -0700734 dma->pgtbl = NULL;
735 }
736 kfree(dma->pg_arr);
737 dma->pg_arr = NULL;
738 dma->num_pages = 0;
739}
740
741static void cnic_setup_page_tbl(struct cnic_dev *dev, struct cnic_dma *dma)
742{
743 int i;
Michael Chan51388262011-01-25 22:14:50 +0000744 __le32 *page_table = (__le32 *) dma->pgtbl;
Michael Chana4636962009-06-08 18:14:43 -0700745
746 for (i = 0; i < dma->num_pages; i++) {
747 /* Each entry needs to be in big endian format. */
Michael Chan51388262011-01-25 22:14:50 +0000748 *page_table = cpu_to_le32((u64) dma->pg_map_arr[i] >> 32);
Michael Chana4636962009-06-08 18:14:43 -0700749 page_table++;
Michael Chan51388262011-01-25 22:14:50 +0000750 *page_table = cpu_to_le32(dma->pg_map_arr[i] & 0xffffffff);
Michael Chana4636962009-06-08 18:14:43 -0700751 page_table++;
752 }
753}
754
Michael Chan71034ba2009-10-10 13:46:59 +0000755static void cnic_setup_page_tbl_le(struct cnic_dev *dev, struct cnic_dma *dma)
756{
757 int i;
Michael Chan51388262011-01-25 22:14:50 +0000758 __le32 *page_table = (__le32 *) dma->pgtbl;
Michael Chan71034ba2009-10-10 13:46:59 +0000759
760 for (i = 0; i < dma->num_pages; i++) {
761 /* Each entry needs to be in little endian format. */
Michael Chan51388262011-01-25 22:14:50 +0000762 *page_table = cpu_to_le32(dma->pg_map_arr[i] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +0000763 page_table++;
Michael Chan51388262011-01-25 22:14:50 +0000764 *page_table = cpu_to_le32((u64) dma->pg_map_arr[i] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +0000765 page_table++;
766 }
767}
768
Michael Chana4636962009-06-08 18:14:43 -0700769static int cnic_alloc_dma(struct cnic_dev *dev, struct cnic_dma *dma,
770 int pages, int use_pg_tbl)
771{
772 int i, size;
773 struct cnic_local *cp = dev->cnic_priv;
774
775 size = pages * (sizeof(void *) + sizeof(dma_addr_t));
776 dma->pg_arr = kzalloc(size, GFP_ATOMIC);
777 if (dma->pg_arr == NULL)
778 return -ENOMEM;
779
780 dma->pg_map_arr = (dma_addr_t *) (dma->pg_arr + pages);
781 dma->num_pages = pages;
782
783 for (i = 0; i < pages; i++) {
Michael Chan3248e162009-12-02 15:15:39 +0000784 dma->pg_arr[i] = dma_alloc_coherent(&dev->pcidev->dev,
785 BCM_PAGE_SIZE,
786 &dma->pg_map_arr[i],
787 GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700788 if (dma->pg_arr[i] == NULL)
789 goto error;
790 }
791 if (!use_pg_tbl)
792 return 0;
793
794 dma->pgtbl_size = ((pages * 8) + BCM_PAGE_SIZE - 1) &
795 ~(BCM_PAGE_SIZE - 1);
Michael Chan3248e162009-12-02 15:15:39 +0000796 dma->pgtbl = dma_alloc_coherent(&dev->pcidev->dev, dma->pgtbl_size,
797 &dma->pgtbl_map, GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700798 if (dma->pgtbl == NULL)
799 goto error;
800
801 cp->setup_pgtbl(dev, dma);
802
803 return 0;
804
805error:
806 cnic_free_dma(dev, dma);
807 return -ENOMEM;
808}
809
Michael Chan86b53602009-10-10 13:46:57 +0000810static void cnic_free_context(struct cnic_dev *dev)
811{
812 struct cnic_local *cp = dev->cnic_priv;
813 int i;
814
815 for (i = 0; i < cp->ctx_blks; i++) {
816 if (cp->ctx_arr[i].ctx) {
Michael Chan3248e162009-12-02 15:15:39 +0000817 dma_free_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
818 cp->ctx_arr[i].ctx,
819 cp->ctx_arr[i].mapping);
Michael Chan86b53602009-10-10 13:46:57 +0000820 cp->ctx_arr[i].ctx = NULL;
821 }
822 }
823}
824
Michael Chancd801532010-10-13 14:06:49 +0000825static void __cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chana4636962009-06-08 18:14:43 -0700826{
Michael Chancd801532010-10-13 14:06:49 +0000827 uio_unregister_device(&udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -0700828
Michael Chancd801532010-10-13 14:06:49 +0000829 if (udev->l2_buf) {
830 dma_free_coherent(&udev->pdev->dev, udev->l2_buf_size,
831 udev->l2_buf, udev->l2_buf_map);
832 udev->l2_buf = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700833 }
834
Michael Chancd801532010-10-13 14:06:49 +0000835 if (udev->l2_ring) {
836 dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
837 udev->l2_ring, udev->l2_ring_map);
838 udev->l2_ring = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700839 }
Michael Chana3ceeeb2010-10-13 14:06:50 +0000840
841 pci_dev_put(udev->pdev);
842 kfree(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000843}
844
Michael Chancd801532010-10-13 14:06:49 +0000845static void cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000846{
Michael Chancd801532010-10-13 14:06:49 +0000847 if (!udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000848 return;
849
Michael Chana3ceeeb2010-10-13 14:06:50 +0000850 write_lock(&cnic_dev_lock);
851 list_del_init(&udev->list);
852 write_unlock(&cnic_dev_lock);
Michael Chancd801532010-10-13 14:06:49 +0000853 __cnic_free_uio(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000854}
855
856static void cnic_free_resc(struct cnic_dev *dev)
857{
858 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000859 struct cnic_uio_dev *udev = cp->udev;
Michael Chanc06c0462010-10-13 14:06:48 +0000860
Michael Chancd801532010-10-13 14:06:49 +0000861 if (udev) {
Michael Chana3ceeeb2010-10-13 14:06:50 +0000862 udev->dev = NULL;
Michael Chancd801532010-10-13 14:06:49 +0000863 cp->udev = NULL;
Michael Chanc06c0462010-10-13 14:06:48 +0000864 }
Michael Chana4636962009-06-08 18:14:43 -0700865
Michael Chan86b53602009-10-10 13:46:57 +0000866 cnic_free_context(dev);
Michael Chana4636962009-06-08 18:14:43 -0700867 kfree(cp->ctx_arr);
868 cp->ctx_arr = NULL;
869 cp->ctx_blks = 0;
870
871 cnic_free_dma(dev, &cp->gbl_buf_info);
Michael Chana4636962009-06-08 18:14:43 -0700872 cnic_free_dma(dev, &cp->kwq_info);
Michael Chan71034ba2009-10-10 13:46:59 +0000873 cnic_free_dma(dev, &cp->kwq_16_data_info);
Michael Chane21ba412010-12-23 07:43:03 +0000874 cnic_free_dma(dev, &cp->kcq2.dma);
Michael Chane6c28892010-06-24 14:58:39 +0000875 cnic_free_dma(dev, &cp->kcq1.dma);
Michael Chana4636962009-06-08 18:14:43 -0700876 kfree(cp->iscsi_tbl);
877 cp->iscsi_tbl = NULL;
878 kfree(cp->ctx_tbl);
879 cp->ctx_tbl = NULL;
880
Michael Chane1928c82010-12-23 07:43:04 +0000881 cnic_free_id_tbl(&cp->fcoe_cid_tbl);
Michael Chana4636962009-06-08 18:14:43 -0700882 cnic_free_id_tbl(&cp->cid_tbl);
883}
884
885static int cnic_alloc_context(struct cnic_dev *dev)
886{
887 struct cnic_local *cp = dev->cnic_priv;
888
889 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
890 int i, k, arr_size;
891
892 cp->ctx_blk_size = BCM_PAGE_SIZE;
893 cp->cids_per_blk = BCM_PAGE_SIZE / 128;
894 arr_size = BNX2_MAX_CID / cp->cids_per_blk *
895 sizeof(struct cnic_ctx);
896 cp->ctx_arr = kzalloc(arr_size, GFP_KERNEL);
897 if (cp->ctx_arr == NULL)
898 return -ENOMEM;
899
900 k = 0;
901 for (i = 0; i < 2; i++) {
902 u32 j, reg, off, lo, hi;
903
904 if (i == 0)
905 off = BNX2_PG_CTX_MAP;
906 else
907 off = BNX2_ISCSI_CTX_MAP;
908
909 reg = cnic_reg_rd_ind(dev, off);
910 lo = reg >> 16;
911 hi = reg & 0xffff;
912 for (j = lo; j < hi; j += cp->cids_per_blk, k++)
913 cp->ctx_arr[k].cid = j;
914 }
915
916 cp->ctx_blks = k;
917 if (cp->ctx_blks >= (BNX2_MAX_CID / cp->cids_per_blk)) {
918 cp->ctx_blks = 0;
919 return -ENOMEM;
920 }
921
922 for (i = 0; i < cp->ctx_blks; i++) {
923 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +0000924 dma_alloc_coherent(&dev->pcidev->dev,
925 BCM_PAGE_SIZE,
926 &cp->ctx_arr[i].mapping,
927 GFP_KERNEL);
Michael Chana4636962009-06-08 18:14:43 -0700928 if (cp->ctx_arr[i].ctx == NULL)
929 return -ENOMEM;
930 }
931 }
932 return 0;
933}
934
Michael Chan59e51372011-06-14 01:32:38 +0000935static u16 cnic_bnx2_next_idx(u16 idx)
Michael Chane6c28892010-06-24 14:58:39 +0000936{
Michael Chan59e51372011-06-14 01:32:38 +0000937 return idx + 1;
938}
939
940static u16 cnic_bnx2_hw_idx(u16 idx)
941{
942 return idx;
943}
944
945static u16 cnic_bnx2x_next_idx(u16 idx)
946{
947 idx++;
948 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
949 idx++;
950
951 return idx;
952}
953
954static u16 cnic_bnx2x_hw_idx(u16 idx)
955{
956 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
957 idx++;
958 return idx;
959}
960
961static int cnic_alloc_kcq(struct cnic_dev *dev, struct kcq_info *info,
962 bool use_pg_tbl)
963{
964 int err, i, use_page_tbl = 0;
Michael Chane6c28892010-06-24 14:58:39 +0000965 struct kcqe **kcq;
966
Michael Chan59e51372011-06-14 01:32:38 +0000967 if (use_pg_tbl)
968 use_page_tbl = 1;
Michael Chane6c28892010-06-24 14:58:39 +0000969
Michael Chan59e51372011-06-14 01:32:38 +0000970 err = cnic_alloc_dma(dev, &info->dma, KCQ_PAGE_CNT, use_page_tbl);
Michael Chane6c28892010-06-24 14:58:39 +0000971 if (err)
972 return err;
973
974 kcq = (struct kcqe **) info->dma.pg_arr;
975 info->kcq = kcq;
976
Michael Chan59e51372011-06-14 01:32:38 +0000977 info->next_idx = cnic_bnx2_next_idx;
978 info->hw_idx = cnic_bnx2_hw_idx;
979 if (use_pg_tbl)
Michael Chane6c28892010-06-24 14:58:39 +0000980 return 0;
981
Michael Chan59e51372011-06-14 01:32:38 +0000982 info->next_idx = cnic_bnx2x_next_idx;
983 info->hw_idx = cnic_bnx2x_hw_idx;
984
Michael Chane6c28892010-06-24 14:58:39 +0000985 for (i = 0; i < KCQ_PAGE_CNT; i++) {
986 struct bnx2x_bd_chain_next *next =
987 (struct bnx2x_bd_chain_next *) &kcq[i][MAX_KCQE_CNT];
988 int j = i + 1;
989
990 if (j >= KCQ_PAGE_CNT)
991 j = 0;
992 next->addr_hi = (u64) info->dma.pg_map_arr[j] >> 32;
993 next->addr_lo = info->dma.pg_map_arr[j] & 0xffffffff;
994 }
995 return 0;
996}
997
Michael Chancd801532010-10-13 14:06:49 +0000998static int cnic_alloc_uio_rings(struct cnic_dev *dev, int pages)
Michael Chanec0248e2009-08-26 09:49:22 +0000999{
1000 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00001001 struct cnic_uio_dev *udev;
Michael Chanec0248e2009-08-26 09:49:22 +00001002
Michael Chana3ceeeb2010-10-13 14:06:50 +00001003 read_lock(&cnic_dev_lock);
1004 list_for_each_entry(udev, &cnic_udev_list, list) {
1005 if (udev->pdev == dev->pcidev) {
1006 udev->dev = dev;
1007 cp->udev = udev;
1008 read_unlock(&cnic_dev_lock);
1009 return 0;
1010 }
1011 }
1012 read_unlock(&cnic_dev_lock);
1013
Michael Chancd801532010-10-13 14:06:49 +00001014 udev = kzalloc(sizeof(struct cnic_uio_dev), GFP_ATOMIC);
1015 if (!udev)
Michael Chanec0248e2009-08-26 09:49:22 +00001016 return -ENOMEM;
1017
Michael Chancd801532010-10-13 14:06:49 +00001018 udev->uio_dev = -1;
1019
1020 udev->dev = dev;
1021 udev->pdev = dev->pcidev;
1022 udev->l2_ring_size = pages * BCM_PAGE_SIZE;
1023 udev->l2_ring = dma_alloc_coherent(&udev->pdev->dev, udev->l2_ring_size,
1024 &udev->l2_ring_map,
1025 GFP_KERNEL | __GFP_COMP);
1026 if (!udev->l2_ring)
Jesper Juhlf7e4c972010-12-31 11:18:48 -08001027 goto err_udev;
Michael Chanec0248e2009-08-26 09:49:22 +00001028
Michael Chancd801532010-10-13 14:06:49 +00001029 udev->l2_buf_size = (cp->l2_rx_ring_size + 1) * cp->l2_single_buf_size;
1030 udev->l2_buf_size = PAGE_ALIGN(udev->l2_buf_size);
1031 udev->l2_buf = dma_alloc_coherent(&udev->pdev->dev, udev->l2_buf_size,
1032 &udev->l2_buf_map,
1033 GFP_KERNEL | __GFP_COMP);
1034 if (!udev->l2_buf)
Jesper Juhlf7e4c972010-12-31 11:18:48 -08001035 goto err_dma;
Michael Chancd801532010-10-13 14:06:49 +00001036
Michael Chana3ceeeb2010-10-13 14:06:50 +00001037 write_lock(&cnic_dev_lock);
1038 list_add(&udev->list, &cnic_udev_list);
1039 write_unlock(&cnic_dev_lock);
1040
1041 pci_dev_get(udev->pdev);
1042
Michael Chancd801532010-10-13 14:06:49 +00001043 cp->udev = udev;
1044
Michael Chanec0248e2009-08-26 09:49:22 +00001045 return 0;
Jesper Juhlf7e4c972010-12-31 11:18:48 -08001046 err_dma:
1047 dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
1048 udev->l2_ring, udev->l2_ring_map);
1049 err_udev:
1050 kfree(udev);
1051 return -ENOMEM;
Michael Chanec0248e2009-08-26 09:49:22 +00001052}
1053
Michael Chancd801532010-10-13 14:06:49 +00001054static int cnic_init_uio(struct cnic_dev *dev)
1055{
Michael Chan5e9b2db2009-08-26 09:49:23 +00001056 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00001057 struct cnic_uio_dev *udev = cp->udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001058 struct uio_info *uinfo;
Michael Chancd801532010-10-13 14:06:49 +00001059 int ret = 0;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001060
Michael Chancd801532010-10-13 14:06:49 +00001061 if (!udev)
Michael Chan5e9b2db2009-08-26 09:49:23 +00001062 return -ENOMEM;
1063
Michael Chancd801532010-10-13 14:06:49 +00001064 uinfo = &udev->cnic_uinfo;
1065
Michael Chanae0eef62012-06-29 09:32:45 +00001066 uinfo->mem[0].addr = pci_resource_start(dev->pcidev, 0);
1067 uinfo->mem[0].internal_addr = dev->regview;
1068 uinfo->mem[0].memtype = UIO_MEM_PHYS;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001069
Michael Chan5e9b2db2009-08-26 09:49:23 +00001070 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
Michael Chanae0eef62012-06-29 09:32:45 +00001071 uinfo->mem[0].size = MB_GET_CID_ADDR(TX_TSS_CID +
1072 TX_MAX_TSS_RINGS + 1);
Michael Chana4dde3a2010-02-24 14:42:08 +00001073 uinfo->mem[1].addr = (unsigned long) cp->status_blk.gen &
Michael Chancd801532010-10-13 14:06:49 +00001074 PAGE_MASK;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001075 if (cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
1076 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE * 9;
1077 else
1078 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE;
1079
1080 uinfo->name = "bnx2_cnic";
Michael Chan71034ba2009-10-10 13:46:59 +00001081 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
Michael Chanae0eef62012-06-29 09:32:45 +00001082 uinfo->mem[0].size = pci_resource_len(dev->pcidev, 0);
1083
Michael Chan71034ba2009-10-10 13:46:59 +00001084 uinfo->mem[1].addr = (unsigned long) cp->bnx2x_def_status_blk &
1085 PAGE_MASK;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001086 uinfo->mem[1].size = sizeof(*cp->bnx2x_def_status_blk);
Michael Chan71034ba2009-10-10 13:46:59 +00001087
1088 uinfo->name = "bnx2x_cnic";
Michael Chan5e9b2db2009-08-26 09:49:23 +00001089 }
1090
1091 uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
1092
Michael Chancd801532010-10-13 14:06:49 +00001093 uinfo->mem[2].addr = (unsigned long) udev->l2_ring;
1094 uinfo->mem[2].size = udev->l2_ring_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001095 uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
1096
Michael Chancd801532010-10-13 14:06:49 +00001097 uinfo->mem[3].addr = (unsigned long) udev->l2_buf;
1098 uinfo->mem[3].size = udev->l2_buf_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001099 uinfo->mem[3].memtype = UIO_MEM_LOGICAL;
1100
1101 uinfo->version = CNIC_MODULE_VERSION;
1102 uinfo->irq = UIO_IRQ_CUSTOM;
1103
1104 uinfo->open = cnic_uio_open;
1105 uinfo->release = cnic_uio_close;
1106
Michael Chana3ceeeb2010-10-13 14:06:50 +00001107 if (udev->uio_dev == -1) {
1108 if (!uinfo->priv) {
1109 uinfo->priv = udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001110
Michael Chana3ceeeb2010-10-13 14:06:50 +00001111 ret = uio_register_device(&udev->pdev->dev, uinfo);
1112 }
1113 } else {
1114 cnic_init_rings(dev);
1115 }
Michael Chan5e9b2db2009-08-26 09:49:23 +00001116
Michael Chancd801532010-10-13 14:06:49 +00001117 return ret;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001118}
1119
Michael Chana4636962009-06-08 18:14:43 -07001120static int cnic_alloc_bnx2_resc(struct cnic_dev *dev)
1121{
1122 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07001123 int ret;
1124
1125 ret = cnic_alloc_dma(dev, &cp->kwq_info, KWQ_PAGE_CNT, 1);
1126 if (ret)
1127 goto error;
1128 cp->kwq = (struct kwqe **) cp->kwq_info.pg_arr;
1129
Michael Chan59e51372011-06-14 01:32:38 +00001130 ret = cnic_alloc_kcq(dev, &cp->kcq1, true);
Michael Chana4636962009-06-08 18:14:43 -07001131 if (ret)
1132 goto error;
Michael Chana4636962009-06-08 18:14:43 -07001133
1134 ret = cnic_alloc_context(dev);
1135 if (ret)
1136 goto error;
1137
Michael Chancd801532010-10-13 14:06:49 +00001138 ret = cnic_alloc_uio_rings(dev, 2);
Michael Chanec0248e2009-08-26 09:49:22 +00001139 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001140 goto error;
1141
Michael Chancd801532010-10-13 14:06:49 +00001142 ret = cnic_init_uio(dev);
Michael Chan5e9b2db2009-08-26 09:49:23 +00001143 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001144 goto error;
1145
Michael Chana4636962009-06-08 18:14:43 -07001146 return 0;
1147
1148error:
1149 cnic_free_resc(dev);
1150 return ret;
1151}
1152
Michael Chan71034ba2009-10-10 13:46:59 +00001153static int cnic_alloc_bnx2x_context(struct cnic_dev *dev)
1154{
1155 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001156 int ctx_blk_size = cp->ethdev->ctx_blk_size;
Michael Chan520efdf2010-06-24 14:58:37 +00001157 int total_mem, blks, i;
Michael Chan71034ba2009-10-10 13:46:59 +00001158
Michael Chan520efdf2010-06-24 14:58:37 +00001159 total_mem = BNX2X_CONTEXT_MEM_SIZE * cp->max_cid_space;
Michael Chan71034ba2009-10-10 13:46:59 +00001160 blks = total_mem / ctx_blk_size;
1161 if (total_mem % ctx_blk_size)
1162 blks++;
1163
1164 if (blks > cp->ethdev->ctx_tbl_len)
1165 return -ENOMEM;
1166
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001167 cp->ctx_arr = kcalloc(blks, sizeof(struct cnic_ctx), GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001168 if (cp->ctx_arr == NULL)
1169 return -ENOMEM;
1170
1171 cp->ctx_blks = blks;
1172 cp->ctx_blk_size = ctx_blk_size;
Michael Chanee87a822010-10-13 14:06:51 +00001173 if (!BNX2X_CHIP_IS_57710(cp->chip_id))
Michael Chan71034ba2009-10-10 13:46:59 +00001174 cp->ctx_align = 0;
1175 else
1176 cp->ctx_align = ctx_blk_size;
1177
1178 cp->cids_per_blk = ctx_blk_size / BNX2X_CONTEXT_MEM_SIZE;
1179
1180 for (i = 0; i < blks; i++) {
1181 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +00001182 dma_alloc_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
1183 &cp->ctx_arr[i].mapping,
1184 GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001185 if (cp->ctx_arr[i].ctx == NULL)
1186 return -ENOMEM;
1187
1188 if (cp->ctx_align && cp->ctx_blk_size == ctx_blk_size) {
1189 if (cp->ctx_arr[i].mapping & (cp->ctx_align - 1)) {
1190 cnic_free_context(dev);
1191 cp->ctx_blk_size += cp->ctx_align;
1192 i = -1;
1193 continue;
1194 }
1195 }
1196 }
1197 return 0;
1198}
1199
1200static int cnic_alloc_bnx2x_resc(struct cnic_dev *dev)
1201{
1202 struct cnic_local *cp = dev->cnic_priv;
Michael Chan520efdf2010-06-24 14:58:37 +00001203 struct cnic_eth_dev *ethdev = cp->ethdev;
1204 u32 start_cid = ethdev->starting_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001205 int i, j, n, ret, pages;
1206 struct cnic_dma *kwq_16_dma = &cp->kwq_16_data_info;
1207
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001208 cp->iro_arr = ethdev->iro_arr;
1209
Michael Chanb37a41e2011-07-20 14:55:22 +00001210 cp->max_cid_space = MAX_ISCSI_TBL_SZ;
Michael Chan520efdf2010-06-24 14:58:37 +00001211 cp->iscsi_start_cid = start_cid;
Michael Chane1928c82010-12-23 07:43:04 +00001212 cp->fcoe_start_cid = start_cid + MAX_ISCSI_TBL_SZ;
1213
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001214 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chandc219a22011-08-26 09:45:39 +00001215 cp->max_cid_space += dev->max_fcoe_conn;
Michael Chane1928c82010-12-23 07:43:04 +00001216 cp->fcoe_init_cid = ethdev->fcoe_init_cid;
1217 if (!cp->fcoe_init_cid)
1218 cp->fcoe_init_cid = 0x10;
1219 }
1220
Michael Chan71034ba2009-10-10 13:46:59 +00001221 cp->iscsi_tbl = kzalloc(sizeof(struct cnic_iscsi) * MAX_ISCSI_TBL_SZ,
1222 GFP_KERNEL);
1223 if (!cp->iscsi_tbl)
1224 goto error;
1225
1226 cp->ctx_tbl = kzalloc(sizeof(struct cnic_context) *
Michael Chan520efdf2010-06-24 14:58:37 +00001227 cp->max_cid_space, GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001228 if (!cp->ctx_tbl)
1229 goto error;
1230
1231 for (i = 0; i < MAX_ISCSI_TBL_SZ; i++) {
1232 cp->ctx_tbl[i].proto.iscsi = &cp->iscsi_tbl[i];
1233 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_ISCSI;
1234 }
1235
Michael Chane1928c82010-12-23 07:43:04 +00001236 for (i = MAX_ISCSI_TBL_SZ; i < cp->max_cid_space; i++)
1237 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_FCOE;
1238
Michael Chan520efdf2010-06-24 14:58:37 +00001239 pages = PAGE_ALIGN(cp->max_cid_space * CNIC_KWQ16_DATA_SIZE) /
Michael Chan71034ba2009-10-10 13:46:59 +00001240 PAGE_SIZE;
1241
1242 ret = cnic_alloc_dma(dev, kwq_16_dma, pages, 0);
1243 if (ret)
1244 return -ENOMEM;
1245
1246 n = PAGE_SIZE / CNIC_KWQ16_DATA_SIZE;
Michael Chan520efdf2010-06-24 14:58:37 +00001247 for (i = 0, j = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +00001248 long off = CNIC_KWQ16_DATA_SIZE * (i % n);
1249
1250 cp->ctx_tbl[i].kwqe_data = kwq_16_dma->pg_arr[j] + off;
1251 cp->ctx_tbl[i].kwqe_data_mapping = kwq_16_dma->pg_map_arr[j] +
1252 off;
1253
1254 if ((i % n) == (n - 1))
1255 j++;
1256 }
1257
Michael Chan59e51372011-06-14 01:32:38 +00001258 ret = cnic_alloc_kcq(dev, &cp->kcq1, false);
Michael Chan71034ba2009-10-10 13:46:59 +00001259 if (ret)
1260 goto error;
Michael Chan71034ba2009-10-10 13:46:59 +00001261
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001262 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
1263 ret = cnic_alloc_kcq(dev, &cp->kcq2, true);
Michael Chane21ba412010-12-23 07:43:03 +00001264 if (ret)
1265 goto error;
1266 }
1267
Michael Chan71034ba2009-10-10 13:46:59 +00001268 pages = PAGE_ALIGN(BNX2X_ISCSI_GLB_BUF_SIZE) / PAGE_SIZE;
1269 ret = cnic_alloc_dma(dev, &cp->gbl_buf_info, pages, 0);
1270 if (ret)
1271 goto error;
1272
1273 ret = cnic_alloc_bnx2x_context(dev);
1274 if (ret)
1275 goto error;
1276
Michael Chan71034ba2009-10-10 13:46:59 +00001277 cp->bnx2x_def_status_blk = cp->ethdev->irq_arr[1].status_blk;
1278
1279 cp->l2_rx_ring_size = 15;
1280
Michael Chancd801532010-10-13 14:06:49 +00001281 ret = cnic_alloc_uio_rings(dev, 4);
Michael Chan71034ba2009-10-10 13:46:59 +00001282 if (ret)
1283 goto error;
1284
Michael Chancd801532010-10-13 14:06:49 +00001285 ret = cnic_init_uio(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00001286 if (ret)
1287 goto error;
1288
1289 return 0;
1290
1291error:
1292 cnic_free_resc(dev);
1293 return -ENOMEM;
1294}
1295
Michael Chana4636962009-06-08 18:14:43 -07001296static inline u32 cnic_kwq_avail(struct cnic_local *cp)
1297{
1298 return cp->max_kwq_idx -
1299 ((cp->kwq_prod_idx - cp->kwq_con_idx) & cp->max_kwq_idx);
1300}
1301
1302static int cnic_submit_bnx2_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
1303 u32 num_wqes)
1304{
1305 struct cnic_local *cp = dev->cnic_priv;
1306 struct kwqe *prod_qe;
1307 u16 prod, sw_prod, i;
1308
1309 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
1310 return -EAGAIN; /* bnx2 is down */
1311
1312 spin_lock_bh(&cp->cnic_ulp_lock);
1313 if (num_wqes > cnic_kwq_avail(cp) &&
Michael Chan1f1332a2010-05-18 11:32:52 +00001314 !test_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags)) {
Michael Chana4636962009-06-08 18:14:43 -07001315 spin_unlock_bh(&cp->cnic_ulp_lock);
1316 return -EAGAIN;
1317 }
1318
Michael Chan1f1332a2010-05-18 11:32:52 +00001319 clear_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07001320
1321 prod = cp->kwq_prod_idx;
1322 sw_prod = prod & MAX_KWQ_IDX;
1323 for (i = 0; i < num_wqes; i++) {
1324 prod_qe = &cp->kwq[KWQ_PG(sw_prod)][KWQ_IDX(sw_prod)];
1325 memcpy(prod_qe, wqes[i], sizeof(struct kwqe));
1326 prod++;
1327 sw_prod = prod & MAX_KWQ_IDX;
1328 }
1329 cp->kwq_prod_idx = prod;
1330
1331 CNIC_WR16(dev, cp->kwq_io_addr, cp->kwq_prod_idx);
1332
1333 spin_unlock_bh(&cp->cnic_ulp_lock);
1334 return 0;
1335}
1336
Michael Chan71034ba2009-10-10 13:46:59 +00001337static void *cnic_get_kwqe_16_data(struct cnic_local *cp, u32 l5_cid,
1338 union l5cm_specific_data *l5_data)
1339{
1340 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1341 dma_addr_t map;
1342
1343 map = ctx->kwqe_data_mapping;
1344 l5_data->phy_address.lo = (u64) map & 0xffffffff;
1345 l5_data->phy_address.hi = (u64) map >> 32;
1346 return ctx->kwqe_data;
1347}
1348
1349static int cnic_submit_kwqe_16(struct cnic_dev *dev, u32 cmd, u32 cid,
1350 u32 type, union l5cm_specific_data *l5_data)
1351{
1352 struct cnic_local *cp = dev->cnic_priv;
1353 struct l5cm_spe kwqe;
1354 struct kwqe_16 *kwq[1];
Michael Chan68d7c1a2011-01-05 15:14:13 +00001355 u16 type_16;
Michael Chan71034ba2009-10-10 13:46:59 +00001356 int ret;
1357
1358 kwqe.hdr.conn_and_cmd_data =
1359 cpu_to_le32(((cmd << SPE_HDR_CMD_ID_SHIFT) |
Michael Chanceb7e1c2010-10-06 03:14:54 +00001360 BNX2X_HW_CID(cp, cid)));
Michael Chan68d7c1a2011-01-05 15:14:13 +00001361
1362 type_16 = (type << SPE_HDR_CONN_TYPE_SHIFT) & SPE_HDR_CONN_TYPE;
1363 type_16 |= (cp->pfid << SPE_HDR_FUNCTION_ID_SHIFT) &
1364 SPE_HDR_FUNCTION_ID;
1365
1366 kwqe.hdr.type = cpu_to_le16(type_16);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001367 kwqe.hdr.reserved1 = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001368 kwqe.data.phy_address.lo = cpu_to_le32(l5_data->phy_address.lo);
1369 kwqe.data.phy_address.hi = cpu_to_le32(l5_data->phy_address.hi);
1370
1371 kwq[0] = (struct kwqe_16 *) &kwqe;
1372
1373 spin_lock_bh(&cp->cnic_ulp_lock);
1374 ret = cp->ethdev->drv_submit_kwqes_16(dev->netdev, kwq, 1);
1375 spin_unlock_bh(&cp->cnic_ulp_lock);
1376
1377 if (ret == 1)
1378 return 0;
1379
Michael Chan23021c22012-01-04 12:12:28 +00001380 return ret;
Michael Chan71034ba2009-10-10 13:46:59 +00001381}
1382
1383static void cnic_reply_bnx2x_kcqes(struct cnic_dev *dev, int ulp_type,
1384 struct kcqe *cqes[], u32 num_cqes)
1385{
1386 struct cnic_local *cp = dev->cnic_priv;
1387 struct cnic_ulp_ops *ulp_ops;
1388
1389 rcu_read_lock();
1390 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
1391 if (likely(ulp_ops)) {
1392 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
1393 cqes, num_cqes);
1394 }
1395 rcu_read_unlock();
1396}
1397
1398static int cnic_bnx2x_iscsi_init1(struct cnic_dev *dev, struct kwqe *kwqe)
1399{
1400 struct cnic_local *cp = dev->cnic_priv;
1401 struct iscsi_kwqe_init1 *req1 = (struct iscsi_kwqe_init1 *) kwqe;
Michael Chan14203982010-10-06 03:16:06 +00001402 int hq_bds, pages;
1403 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001404
1405 cp->num_iscsi_tasks = req1->num_tasks_per_conn;
1406 cp->num_ccells = req1->num_ccells_per_conn;
1407 cp->task_array_size = BNX2X_ISCSI_TASK_CONTEXT_SIZE *
1408 cp->num_iscsi_tasks;
1409 cp->r2tq_size = cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS *
1410 BNX2X_ISCSI_R2TQE_SIZE;
1411 cp->hq_size = cp->num_ccells * BNX2X_ISCSI_HQ_BD_SIZE;
1412 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1413 hq_bds = pages * (PAGE_SIZE / BNX2X_ISCSI_HQ_BD_SIZE);
1414 cp->num_cqs = req1->num_cqs;
1415
1416 if (!dev->max_iscsi_conn)
1417 return 0;
1418
1419 /* init Tstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001420 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001421 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001422 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001423 PAGE_SIZE);
1424 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001425 TSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001426 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001427 TSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001428 req1->num_tasks_per_conn);
1429
1430 /* init Ustorm RAM */
1431 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001432 USTORM_ISCSI_RQ_BUFFER_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001433 req1->rq_buffer_size);
Michael Chan14203982010-10-06 03:16:06 +00001434 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001435 PAGE_SIZE);
1436 CNIC_WR8(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001437 USTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001438 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001439 USTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001440 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001441 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001442 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001443 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001444 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001445 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001446 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1447
1448 /* init Xstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001449 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001450 PAGE_SIZE);
1451 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001452 XSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001453 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001454 XSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001455 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001456 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001457 hq_bds);
Michael Chan14203982010-10-06 03:16:06 +00001458 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_SQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001459 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001460 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001461 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1462
1463 /* init Cstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001464 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001465 PAGE_SIZE);
1466 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001467 CSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001468 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001469 CSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001470 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001471 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001472 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001473 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001474 hq_bds);
1475
1476 return 0;
1477}
1478
1479static int cnic_bnx2x_iscsi_init2(struct cnic_dev *dev, struct kwqe *kwqe)
1480{
1481 struct iscsi_kwqe_init2 *req2 = (struct iscsi_kwqe_init2 *) kwqe;
1482 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00001483 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001484 struct iscsi_kcqe kcqe;
1485 struct kcqe *cqes[1];
1486
1487 memset(&kcqe, 0, sizeof(kcqe));
1488 if (!dev->max_iscsi_conn) {
1489 kcqe.completion_status =
1490 ISCSI_KCQE_COMPLETION_STATUS_ISCSI_NOT_SUPPORTED;
1491 goto done;
1492 }
1493
1494 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001495 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001496 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001497 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001498 req2->error_bit_map[1]);
1499
1500 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001501 USTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001502 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001503 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001504 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001505 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001506 req2->error_bit_map[1]);
1507
1508 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001509 CSTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001510
1511 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1512
1513done:
1514 kcqe.op_code = ISCSI_KCQE_OPCODE_INIT;
1515 cqes[0] = (struct kcqe *) &kcqe;
1516 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1517
1518 return 0;
1519}
1520
1521static void cnic_free_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1522{
1523 struct cnic_local *cp = dev->cnic_priv;
1524 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1525
1526 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI) {
1527 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1528
1529 cnic_free_dma(dev, &iscsi->hq_info);
1530 cnic_free_dma(dev, &iscsi->r2tq_info);
1531 cnic_free_dma(dev, &iscsi->task_array_info);
Michael Chane1928c82010-12-23 07:43:04 +00001532 cnic_free_id(&cp->cid_tbl, ctx->cid);
1533 } else {
1534 cnic_free_id(&cp->fcoe_cid_tbl, ctx->cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001535 }
Michael Chane1928c82010-12-23 07:43:04 +00001536
Michael Chan71034ba2009-10-10 13:46:59 +00001537 ctx->cid = 0;
1538}
1539
1540static int cnic_alloc_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1541{
1542 u32 cid;
1543 int ret, pages;
1544 struct cnic_local *cp = dev->cnic_priv;
1545 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1546 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1547
Michael Chane1928c82010-12-23 07:43:04 +00001548 if (ctx->ulp_proto_id == CNIC_ULP_FCOE) {
1549 cid = cnic_alloc_new_id(&cp->fcoe_cid_tbl);
1550 if (cid == -1) {
1551 ret = -ENOMEM;
1552 goto error;
1553 }
1554 ctx->cid = cid;
1555 return 0;
1556 }
1557
Michael Chan71034ba2009-10-10 13:46:59 +00001558 cid = cnic_alloc_new_id(&cp->cid_tbl);
1559 if (cid == -1) {
1560 ret = -ENOMEM;
1561 goto error;
1562 }
1563
1564 ctx->cid = cid;
1565 pages = PAGE_ALIGN(cp->task_array_size) / PAGE_SIZE;
1566
1567 ret = cnic_alloc_dma(dev, &iscsi->task_array_info, pages, 1);
1568 if (ret)
1569 goto error;
1570
1571 pages = PAGE_ALIGN(cp->r2tq_size) / PAGE_SIZE;
1572 ret = cnic_alloc_dma(dev, &iscsi->r2tq_info, pages, 1);
1573 if (ret)
1574 goto error;
1575
1576 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1577 ret = cnic_alloc_dma(dev, &iscsi->hq_info, pages, 1);
1578 if (ret)
1579 goto error;
1580
1581 return 0;
1582
1583error:
1584 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1585 return ret;
1586}
1587
1588static void *cnic_get_bnx2x_ctx(struct cnic_dev *dev, u32 cid, int init,
1589 struct regpair *ctx_addr)
1590{
1591 struct cnic_local *cp = dev->cnic_priv;
1592 struct cnic_eth_dev *ethdev = cp->ethdev;
1593 int blk = (cid - ethdev->starting_cid) / cp->cids_per_blk;
1594 int off = (cid - ethdev->starting_cid) % cp->cids_per_blk;
1595 unsigned long align_off = 0;
1596 dma_addr_t ctx_map;
1597 void *ctx;
1598
1599 if (cp->ctx_align) {
1600 unsigned long mask = cp->ctx_align - 1;
1601
1602 if (cp->ctx_arr[blk].mapping & mask)
1603 align_off = cp->ctx_align -
1604 (cp->ctx_arr[blk].mapping & mask);
1605 }
1606 ctx_map = cp->ctx_arr[blk].mapping + align_off +
1607 (off * BNX2X_CONTEXT_MEM_SIZE);
1608 ctx = cp->ctx_arr[blk].ctx + align_off +
1609 (off * BNX2X_CONTEXT_MEM_SIZE);
1610 if (init)
1611 memset(ctx, 0, BNX2X_CONTEXT_MEM_SIZE);
1612
1613 ctx_addr->lo = ctx_map & 0xffffffff;
1614 ctx_addr->hi = (u64) ctx_map >> 32;
1615 return ctx;
1616}
1617
1618static int cnic_setup_bnx2x_ctx(struct cnic_dev *dev, struct kwqe *wqes[],
1619 u32 num)
1620{
1621 struct cnic_local *cp = dev->cnic_priv;
1622 struct iscsi_kwqe_conn_offload1 *req1 =
1623 (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1624 struct iscsi_kwqe_conn_offload2 *req2 =
1625 (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1626 struct iscsi_kwqe_conn_offload3 *req3;
1627 struct cnic_context *ctx = &cp->ctx_tbl[req1->iscsi_conn_id];
1628 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1629 u32 cid = ctx->cid;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001630 u32 hw_cid = BNX2X_HW_CID(cp, cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001631 struct iscsi_context *ictx;
1632 struct regpair context_addr;
1633 int i, j, n = 2, n_max;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001634 u8 port = CNIC_PORT(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00001635
1636 ctx->ctx_flags = 0;
1637 if (!req2->num_additional_wqes)
1638 return -EINVAL;
1639
1640 n_max = req2->num_additional_wqes + 2;
1641
1642 ictx = cnic_get_bnx2x_ctx(dev, cid, 1, &context_addr);
1643 if (ictx == NULL)
1644 return -ENOMEM;
1645
1646 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1647
1648 ictx->xstorm_ag_context.hq_prod = 1;
1649
1650 ictx->xstorm_st_context.iscsi.first_burst_length =
1651 ISCSI_DEF_FIRST_BURST_LEN;
1652 ictx->xstorm_st_context.iscsi.max_send_pdu_length =
1653 ISCSI_DEF_MAX_RECV_SEG_LEN;
1654 ictx->xstorm_st_context.iscsi.sq_pbl_base.lo =
1655 req1->sq_page_table_addr_lo;
1656 ictx->xstorm_st_context.iscsi.sq_pbl_base.hi =
1657 req1->sq_page_table_addr_hi;
1658 ictx->xstorm_st_context.iscsi.sq_curr_pbe.lo = req2->sq_first_pte.hi;
1659 ictx->xstorm_st_context.iscsi.sq_curr_pbe.hi = req2->sq_first_pte.lo;
1660 ictx->xstorm_st_context.iscsi.hq_pbl_base.lo =
1661 iscsi->hq_info.pgtbl_map & 0xffffffff;
1662 ictx->xstorm_st_context.iscsi.hq_pbl_base.hi =
1663 (u64) iscsi->hq_info.pgtbl_map >> 32;
1664 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.lo =
1665 iscsi->hq_info.pgtbl[0];
1666 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.hi =
1667 iscsi->hq_info.pgtbl[1];
1668 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.lo =
1669 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1670 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.hi =
1671 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1672 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.lo =
1673 iscsi->r2tq_info.pgtbl[0];
1674 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.hi =
1675 iscsi->r2tq_info.pgtbl[1];
1676 ictx->xstorm_st_context.iscsi.task_pbl_base.lo =
1677 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1678 ictx->xstorm_st_context.iscsi.task_pbl_base.hi =
1679 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1680 ictx->xstorm_st_context.iscsi.task_pbl_cache_idx =
1681 BNX2X_ISCSI_PBL_NOT_CACHED;
1682 ictx->xstorm_st_context.iscsi.flags.flags |=
1683 XSTORM_ISCSI_CONTEXT_FLAGS_B_IMMEDIATE_DATA;
1684 ictx->xstorm_st_context.iscsi.flags.flags |=
1685 XSTORM_ISCSI_CONTEXT_FLAGS_B_INITIAL_R2T;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001686 ictx->xstorm_st_context.common.ethernet.reserved_vlan_type =
1687 ETH_P_8021Q;
1688 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id) &&
1689 cp->port_mode == CHIP_2_PORT_MODE) {
1690
1691 port = 0;
1692 }
1693 ictx->xstorm_st_context.common.flags =
1694 1 << XSTORM_COMMON_CONTEXT_SECTION_PHYSQ_INITIALIZED_SHIFT;
1695 ictx->xstorm_st_context.common.flags =
1696 port << XSTORM_COMMON_CONTEXT_SECTION_PBF_PORT_SHIFT;
Michael Chan71034ba2009-10-10 13:46:59 +00001697
1698 ictx->tstorm_st_context.iscsi.hdr_bytes_2_fetch = ISCSI_HEADER_SIZE;
1699 /* TSTORM requires the base address of RQ DB & not PTE */
1700 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.lo =
1701 req2->rq_page_table_addr_lo & PAGE_MASK;
1702 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.hi =
1703 req2->rq_page_table_addr_hi;
1704 ictx->tstorm_st_context.iscsi.iscsi_conn_id = req1->iscsi_conn_id;
1705 ictx->tstorm_st_context.tcp.cwnd = 0x5A8;
1706 ictx->tstorm_st_context.tcp.flags2 |=
1707 TSTORM_TCP_ST_CONTEXT_SECTION_DA_EN;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001708 ictx->tstorm_st_context.tcp.ooo_support_mode =
1709 TCP_TSTORM_OOO_DROP_AND_PROC_ACK;
Michael Chan71034ba2009-10-10 13:46:59 +00001710
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001711 ictx->timers_context.flags |= TIMERS_BLOCK_CONTEXT_CONN_VALID_FLG;
Michael Chan71034ba2009-10-10 13:46:59 +00001712
1713 ictx->ustorm_st_context.ring.rq.pbl_base.lo =
Michael Chan15971c32009-12-02 15:15:38 +00001714 req2->rq_page_table_addr_lo;
Michael Chan71034ba2009-10-10 13:46:59 +00001715 ictx->ustorm_st_context.ring.rq.pbl_base.hi =
Michael Chan15971c32009-12-02 15:15:38 +00001716 req2->rq_page_table_addr_hi;
Michael Chan71034ba2009-10-10 13:46:59 +00001717 ictx->ustorm_st_context.ring.rq.curr_pbe.lo = req3->qp_first_pte[0].hi;
1718 ictx->ustorm_st_context.ring.rq.curr_pbe.hi = req3->qp_first_pte[0].lo;
1719 ictx->ustorm_st_context.ring.r2tq.pbl_base.lo =
1720 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1721 ictx->ustorm_st_context.ring.r2tq.pbl_base.hi =
1722 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1723 ictx->ustorm_st_context.ring.r2tq.curr_pbe.lo =
1724 iscsi->r2tq_info.pgtbl[0];
1725 ictx->ustorm_st_context.ring.r2tq.curr_pbe.hi =
1726 iscsi->r2tq_info.pgtbl[1];
1727 ictx->ustorm_st_context.ring.cq_pbl_base.lo =
1728 req1->cq_page_table_addr_lo;
1729 ictx->ustorm_st_context.ring.cq_pbl_base.hi =
1730 req1->cq_page_table_addr_hi;
1731 ictx->ustorm_st_context.ring.cq[0].cq_sn = ISCSI_INITIAL_SN;
1732 ictx->ustorm_st_context.ring.cq[0].curr_pbe.lo = req2->cq_first_pte.hi;
1733 ictx->ustorm_st_context.ring.cq[0].curr_pbe.hi = req2->cq_first_pte.lo;
1734 ictx->ustorm_st_context.task_pbe_cache_index =
1735 BNX2X_ISCSI_PBL_NOT_CACHED;
1736 ictx->ustorm_st_context.task_pdu_cache_index =
1737 BNX2X_ISCSI_PDU_HEADER_NOT_CACHED;
1738
1739 for (i = 1, j = 1; i < cp->num_cqs; i++, j++) {
1740 if (j == 3) {
1741 if (n >= n_max)
1742 break;
1743 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1744 j = 0;
1745 }
1746 ictx->ustorm_st_context.ring.cq[i].cq_sn = ISCSI_INITIAL_SN;
1747 ictx->ustorm_st_context.ring.cq[i].curr_pbe.lo =
1748 req3->qp_first_pte[j].hi;
1749 ictx->ustorm_st_context.ring.cq[i].curr_pbe.hi =
1750 req3->qp_first_pte[j].lo;
1751 }
1752
1753 ictx->ustorm_st_context.task_pbl_base.lo =
1754 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1755 ictx->ustorm_st_context.task_pbl_base.hi =
1756 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1757 ictx->ustorm_st_context.tce_phy_addr.lo =
1758 iscsi->task_array_info.pgtbl[0];
1759 ictx->ustorm_st_context.tce_phy_addr.hi =
1760 iscsi->task_array_info.pgtbl[1];
1761 ictx->ustorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1762 ictx->ustorm_st_context.num_cqs = cp->num_cqs;
1763 ictx->ustorm_st_context.negotiated_rx |= ISCSI_DEF_MAX_RECV_SEG_LEN;
1764 ictx->ustorm_st_context.negotiated_rx_and_flags |=
1765 ISCSI_DEF_MAX_BURST_LEN;
1766 ictx->ustorm_st_context.negotiated_rx |=
1767 ISCSI_DEFAULT_MAX_OUTSTANDING_R2T <<
1768 USTORM_ISCSI_ST_CONTEXT_MAX_OUTSTANDING_R2TS_SHIFT;
1769
1770 ictx->cstorm_st_context.hq_pbl_base.lo =
1771 iscsi->hq_info.pgtbl_map & 0xffffffff;
1772 ictx->cstorm_st_context.hq_pbl_base.hi =
1773 (u64) iscsi->hq_info.pgtbl_map >> 32;
1774 ictx->cstorm_st_context.hq_curr_pbe.lo = iscsi->hq_info.pgtbl[0];
1775 ictx->cstorm_st_context.hq_curr_pbe.hi = iscsi->hq_info.pgtbl[1];
1776 ictx->cstorm_st_context.task_pbl_base.lo =
1777 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1778 ictx->cstorm_st_context.task_pbl_base.hi =
1779 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1780 /* CSTORM and USTORM initialization is different, CSTORM requires
1781 * CQ DB base & not PTE addr */
1782 ictx->cstorm_st_context.cq_db_base.lo =
1783 req1->cq_page_table_addr_lo & PAGE_MASK;
1784 ictx->cstorm_st_context.cq_db_base.hi = req1->cq_page_table_addr_hi;
1785 ictx->cstorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1786 ictx->cstorm_st_context.cq_proc_en_bit_map = (1 << cp->num_cqs) - 1;
1787 for (i = 0; i < cp->num_cqs; i++) {
1788 ictx->cstorm_st_context.cq_c_prod_sqn_arr.sqn[i] =
1789 ISCSI_INITIAL_SN;
1790 ictx->cstorm_st_context.cq_c_sqn_2_notify_arr.sqn[i] =
1791 ISCSI_INITIAL_SN;
1792 }
1793
1794 ictx->xstorm_ag_context.cdu_reserved =
1795 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
1796 ISCSI_CONNECTION_TYPE);
1797 ictx->ustorm_ag_context.cdu_usage =
1798 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
1799 ISCSI_CONNECTION_TYPE);
1800 return 0;
1801
1802}
1803
1804static int cnic_bnx2x_iscsi_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
1805 u32 num, int *work)
1806{
1807 struct iscsi_kwqe_conn_offload1 *req1;
1808 struct iscsi_kwqe_conn_offload2 *req2;
1809 struct cnic_local *cp = dev->cnic_priv;
Michael Chanfdf24082010-10-13 14:06:47 +00001810 struct cnic_context *ctx;
Michael Chan71034ba2009-10-10 13:46:59 +00001811 struct iscsi_kcqe kcqe;
1812 struct kcqe *cqes[1];
1813 u32 l5_cid;
Michael Chanfdf24082010-10-13 14:06:47 +00001814 int ret = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001815
1816 if (num < 2) {
1817 *work = num;
1818 return -EINVAL;
1819 }
1820
1821 req1 = (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1822 req2 = (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1823 if ((num - 2) < req2->num_additional_wqes) {
1824 *work = num;
1825 return -EINVAL;
1826 }
Joe Perches779bb412010-11-14 17:04:37 +00001827 *work = 2 + req2->num_additional_wqes;
Michael Chan71034ba2009-10-10 13:46:59 +00001828
1829 l5_cid = req1->iscsi_conn_id;
1830 if (l5_cid >= MAX_ISCSI_TBL_SZ)
1831 return -EINVAL;
1832
1833 memset(&kcqe, 0, sizeof(kcqe));
1834 kcqe.op_code = ISCSI_KCQE_OPCODE_OFFLOAD_CONN;
1835 kcqe.iscsi_conn_id = l5_cid;
1836 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
1837
Michael Chanfdf24082010-10-13 14:06:47 +00001838 ctx = &cp->ctx_tbl[l5_cid];
1839 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags)) {
1840 kcqe.completion_status =
1841 ISCSI_KCQE_COMPLETION_STATUS_CID_BUSY;
1842 goto done;
1843 }
1844
Michael Chan71034ba2009-10-10 13:46:59 +00001845 if (atomic_inc_return(&cp->iscsi_conn) > dev->max_iscsi_conn) {
1846 atomic_dec(&cp->iscsi_conn);
Michael Chan71034ba2009-10-10 13:46:59 +00001847 goto done;
1848 }
1849 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
1850 if (ret) {
1851 atomic_dec(&cp->iscsi_conn);
1852 ret = 0;
1853 goto done;
1854 }
1855 ret = cnic_setup_bnx2x_ctx(dev, wqes, num);
1856 if (ret < 0) {
1857 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1858 atomic_dec(&cp->iscsi_conn);
1859 goto done;
1860 }
1861
1862 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001863 kcqe.iscsi_conn_context_id = BNX2X_HW_CID(cp, cp->ctx_tbl[l5_cid].cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001864
1865done:
1866 cqes[0] = (struct kcqe *) &kcqe;
1867 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
Michael Chan23021c22012-01-04 12:12:28 +00001868 return 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001869}
1870
1871
1872static int cnic_bnx2x_iscsi_update(struct cnic_dev *dev, struct kwqe *kwqe)
1873{
1874 struct cnic_local *cp = dev->cnic_priv;
1875 struct iscsi_kwqe_conn_update *req =
1876 (struct iscsi_kwqe_conn_update *) kwqe;
1877 void *data;
1878 union l5cm_specific_data l5_data;
1879 u32 l5_cid, cid = BNX2X_SW_CID(req->context_id);
1880 int ret;
1881
1882 if (cnic_get_l5_cid(cp, cid, &l5_cid) != 0)
1883 return -EINVAL;
1884
1885 data = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
1886 if (!data)
1887 return -ENOMEM;
1888
1889 memcpy(data, kwqe, sizeof(struct kwqe));
1890
1891 ret = cnic_submit_kwqe_16(dev, ISCSI_RAMROD_CMD_ID_UPDATE_CONN,
1892 req->context_id, ISCSI_CONNECTION_TYPE, &l5_data);
1893 return ret;
1894}
1895
Michael Chana2c9e762010-10-13 14:06:46 +00001896static int cnic_bnx2x_destroy_ramrod(struct cnic_dev *dev, u32 l5_cid)
Michael Chan71034ba2009-10-10 13:46:59 +00001897{
1898 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001899 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
Michael Chana2c9e762010-10-13 14:06:46 +00001900 union l5cm_specific_data l5_data;
1901 int ret;
Michael Chan68d7c1a2011-01-05 15:14:13 +00001902 u32 hw_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001903
Michael Chan71034ba2009-10-10 13:46:59 +00001904 init_waitqueue_head(&ctx->waitq);
1905 ctx->wait_cond = 0;
1906 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001907 hw_cid = BNX2X_HW_CID(cp, ctx->cid);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001908
1909 ret = cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
Michael Chan68d7c1a2011-01-05 15:14:13 +00001910 hw_cid, NONE_CONNECTION_TYPE, &l5_data);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001911
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001912 if (ret == 0) {
Michael Chandcc7e3a2011-08-26 09:45:40 +00001913 wait_event_timeout(ctx->waitq, ctx->wait_cond, CNIC_RAMROD_TMO);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001914 if (unlikely(test_bit(CTX_FL_CID_ERROR, &ctx->ctx_flags)))
1915 return -EBUSY;
1916 }
Michael Chan71034ba2009-10-10 13:46:59 +00001917
Michael Chandcc7e3a2011-08-26 09:45:40 +00001918 return 0;
Michael Chana2c9e762010-10-13 14:06:46 +00001919}
1920
1921static int cnic_bnx2x_iscsi_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
1922{
1923 struct cnic_local *cp = dev->cnic_priv;
1924 struct iscsi_kwqe_conn_destroy *req =
1925 (struct iscsi_kwqe_conn_destroy *) kwqe;
1926 u32 l5_cid = req->reserved0;
1927 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1928 int ret = 0;
1929 struct iscsi_kcqe kcqe;
1930 struct kcqe *cqes[1];
1931
1932 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
1933 goto skip_cfc_delete;
1934
Michael Chanfdf24082010-10-13 14:06:47 +00001935 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
1936 unsigned long delta = ctx->timestamp + (2 * HZ) - jiffies;
1937
1938 if (delta > (2 * HZ))
1939 delta = 0;
1940
1941 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
1942 queue_delayed_work(cnic_wq, &cp->delete_task, delta);
1943 goto destroy_reply;
1944 }
Michael Chana2c9e762010-10-13 14:06:46 +00001945
1946 ret = cnic_bnx2x_destroy_ramrod(dev, l5_cid);
1947
Michael Chan71034ba2009-10-10 13:46:59 +00001948skip_cfc_delete:
1949 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1950
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001951 if (!ret) {
1952 atomic_dec(&cp->iscsi_conn);
1953 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
1954 }
Michael Chan71034ba2009-10-10 13:46:59 +00001955
Michael Chanfdf24082010-10-13 14:06:47 +00001956destroy_reply:
Michael Chan71034ba2009-10-10 13:46:59 +00001957 memset(&kcqe, 0, sizeof(kcqe));
1958 kcqe.op_code = ISCSI_KCQE_OPCODE_DESTROY_CONN;
1959 kcqe.iscsi_conn_id = l5_cid;
1960 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1961 kcqe.iscsi_conn_context_id = req->context_id;
1962
1963 cqes[0] = (struct kcqe *) &kcqe;
1964 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1965
Michael Chan23021c22012-01-04 12:12:28 +00001966 return 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001967}
1968
1969static void cnic_init_storm_conn_bufs(struct cnic_dev *dev,
1970 struct l4_kwq_connect_req1 *kwqe1,
1971 struct l4_kwq_connect_req3 *kwqe3,
1972 struct l5cm_active_conn_buffer *conn_buf)
1973{
1974 struct l5cm_conn_addr_params *conn_addr = &conn_buf->conn_addr_buf;
1975 struct l5cm_xstorm_conn_buffer *xstorm_buf =
1976 &conn_buf->xstorm_conn_buffer;
1977 struct l5cm_tstorm_conn_buffer *tstorm_buf =
1978 &conn_buf->tstorm_conn_buffer;
1979 struct regpair context_addr;
1980 u32 cid = BNX2X_SW_CID(kwqe1->cid);
1981 struct in6_addr src_ip, dst_ip;
1982 int i;
1983 u32 *addrp;
1984
1985 addrp = (u32 *) &conn_addr->local_ip_addr;
1986 for (i = 0; i < 4; i++, addrp++)
1987 src_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1988
1989 addrp = (u32 *) &conn_addr->remote_ip_addr;
1990 for (i = 0; i < 4; i++, addrp++)
1991 dst_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1992
1993 cnic_get_bnx2x_ctx(dev, cid, 0, &context_addr);
1994
1995 xstorm_buf->context_addr.hi = context_addr.hi;
1996 xstorm_buf->context_addr.lo = context_addr.lo;
1997 xstorm_buf->mss = 0xffff;
1998 xstorm_buf->rcv_buf = kwqe3->rcv_buf;
1999 if (kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE)
2000 xstorm_buf->params |= L5CM_XSTORM_CONN_BUFFER_NAGLE_ENABLE;
2001 xstorm_buf->pseudo_header_checksum =
2002 swab16(~csum_ipv6_magic(&src_ip, &dst_ip, 0, IPPROTO_TCP, 0));
2003
2004 if (!(kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK))
2005 tstorm_buf->params |=
2006 L5CM_TSTORM_CONN_BUFFER_DELAYED_ACK_ENABLE;
2007 if (kwqe3->ka_timeout) {
2008 tstorm_buf->ka_enable = 1;
2009 tstorm_buf->ka_timeout = kwqe3->ka_timeout;
2010 tstorm_buf->ka_interval = kwqe3->ka_interval;
2011 tstorm_buf->ka_max_probe_count = kwqe3->ka_max_probe_count;
2012 }
Michael Chan71034ba2009-10-10 13:46:59 +00002013 tstorm_buf->max_rt_time = 0xffffffff;
2014}
2015
2016static void cnic_init_bnx2x_mac(struct cnic_dev *dev)
2017{
2018 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00002019 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00002020 u8 *mac = dev->mac_addr;
2021
2022 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002023 XSTORM_ISCSI_LOCAL_MAC_ADDR0_OFFSET(pfid), mac[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00002024 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002025 XSTORM_ISCSI_LOCAL_MAC_ADDR1_OFFSET(pfid), mac[1]);
Michael Chan71034ba2009-10-10 13:46:59 +00002026 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002027 XSTORM_ISCSI_LOCAL_MAC_ADDR2_OFFSET(pfid), mac[2]);
Michael Chan71034ba2009-10-10 13:46:59 +00002028 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002029 XSTORM_ISCSI_LOCAL_MAC_ADDR3_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00002030 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002031 XSTORM_ISCSI_LOCAL_MAC_ADDR4_OFFSET(pfid), mac[4]);
Michael Chan71034ba2009-10-10 13:46:59 +00002032 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002033 XSTORM_ISCSI_LOCAL_MAC_ADDR5_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00002034
2035 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002036 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00002037 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002038 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00002039 mac[4]);
2040 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002041 TSTORM_ISCSI_TCP_VARS_MID_LOCAL_MAC_ADDR_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00002042 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002043 TSTORM_ISCSI_TCP_VARS_MID_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00002044 mac[2]);
2045 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002046 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[1]);
Michael Chan71034ba2009-10-10 13:46:59 +00002047 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002048 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00002049 mac[0]);
2050}
2051
2052static void cnic_bnx2x_set_tcp_timestamp(struct cnic_dev *dev, int tcp_ts)
2053{
2054 struct cnic_local *cp = dev->cnic_priv;
2055 u8 xstorm_flags = XSTORM_L5CM_TCP_FLAGS_WND_SCL_EN;
2056 u16 tstorm_flags = 0;
2057
2058 if (tcp_ts) {
2059 xstorm_flags |= XSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
2060 tstorm_flags |= TSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
2061 }
2062
2063 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002064 XSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), xstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002065
2066 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002067 TSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), tstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002068}
2069
2070static int cnic_bnx2x_connect(struct cnic_dev *dev, struct kwqe *wqes[],
2071 u32 num, int *work)
2072{
2073 struct cnic_local *cp = dev->cnic_priv;
2074 struct l4_kwq_connect_req1 *kwqe1 =
2075 (struct l4_kwq_connect_req1 *) wqes[0];
2076 struct l4_kwq_connect_req3 *kwqe3;
2077 struct l5cm_active_conn_buffer *conn_buf;
2078 struct l5cm_conn_addr_params *conn_addr;
2079 union l5cm_specific_data l5_data;
2080 u32 l5_cid = kwqe1->pg_cid;
2081 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
2082 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
2083 int ret;
2084
2085 if (num < 2) {
2086 *work = num;
2087 return -EINVAL;
2088 }
2089
2090 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6)
2091 *work = 3;
2092 else
2093 *work = 2;
2094
2095 if (num < *work) {
2096 *work = num;
2097 return -EINVAL;
2098 }
2099
2100 if (sizeof(*conn_buf) > CNIC_KWQ16_DATA_SIZE) {
Joe Perchesddf79b22010-02-17 15:01:54 +00002101 netdev_err(dev->netdev, "conn_buf size too big\n");
Michael Chan71034ba2009-10-10 13:46:59 +00002102 return -ENOMEM;
2103 }
2104 conn_buf = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2105 if (!conn_buf)
2106 return -ENOMEM;
2107
2108 memset(conn_buf, 0, sizeof(*conn_buf));
2109
2110 conn_addr = &conn_buf->conn_addr_buf;
2111 conn_addr->remote_addr_0 = csk->ha[0];
2112 conn_addr->remote_addr_1 = csk->ha[1];
2113 conn_addr->remote_addr_2 = csk->ha[2];
2114 conn_addr->remote_addr_3 = csk->ha[3];
2115 conn_addr->remote_addr_4 = csk->ha[4];
2116 conn_addr->remote_addr_5 = csk->ha[5];
2117
2118 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6) {
2119 struct l4_kwq_connect_req2 *kwqe2 =
2120 (struct l4_kwq_connect_req2 *) wqes[1];
2121
2122 conn_addr->local_ip_addr.ip_addr_hi_hi = kwqe2->src_ip_v6_4;
2123 conn_addr->local_ip_addr.ip_addr_hi_lo = kwqe2->src_ip_v6_3;
2124 conn_addr->local_ip_addr.ip_addr_lo_hi = kwqe2->src_ip_v6_2;
2125
2126 conn_addr->remote_ip_addr.ip_addr_hi_hi = kwqe2->dst_ip_v6_4;
2127 conn_addr->remote_ip_addr.ip_addr_hi_lo = kwqe2->dst_ip_v6_3;
2128 conn_addr->remote_ip_addr.ip_addr_lo_hi = kwqe2->dst_ip_v6_2;
2129 conn_addr->params |= L5CM_CONN_ADDR_PARAMS_IP_VERSION;
2130 }
2131 kwqe3 = (struct l4_kwq_connect_req3 *) wqes[*work - 1];
2132
2133 conn_addr->local_ip_addr.ip_addr_lo_lo = kwqe1->src_ip;
2134 conn_addr->remote_ip_addr.ip_addr_lo_lo = kwqe1->dst_ip;
2135 conn_addr->local_tcp_port = kwqe1->src_port;
2136 conn_addr->remote_tcp_port = kwqe1->dst_port;
2137
2138 conn_addr->pmtu = kwqe3->pmtu;
2139 cnic_init_storm_conn_bufs(dev, kwqe1, kwqe3, conn_buf);
2140
2141 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002142 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(cp->pfid), csk->vlan_id);
Michael Chan71034ba2009-10-10 13:46:59 +00002143
2144 cnic_bnx2x_set_tcp_timestamp(dev,
2145 kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_TIME_STAMP);
2146
2147 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_TCP_CONNECT,
2148 kwqe1->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2149 if (!ret)
Michael Chan6e0dda02010-10-13 14:06:45 +00002150 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002151
2152 return ret;
2153}
2154
2155static int cnic_bnx2x_close(struct cnic_dev *dev, struct kwqe *kwqe)
2156{
2157 struct l4_kwq_close_req *req = (struct l4_kwq_close_req *) kwqe;
2158 union l5cm_specific_data l5_data;
2159 int ret;
2160
2161 memset(&l5_data, 0, sizeof(l5_data));
2162 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_CLOSE,
2163 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2164 return ret;
2165}
2166
2167static int cnic_bnx2x_reset(struct cnic_dev *dev, struct kwqe *kwqe)
2168{
2169 struct l4_kwq_reset_req *req = (struct l4_kwq_reset_req *) kwqe;
2170 union l5cm_specific_data l5_data;
2171 int ret;
2172
2173 memset(&l5_data, 0, sizeof(l5_data));
2174 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_ABORT,
2175 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2176 return ret;
2177}
2178static int cnic_bnx2x_offload_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2179{
2180 struct l4_kwq_offload_pg *req = (struct l4_kwq_offload_pg *) kwqe;
2181 struct l4_kcq kcqe;
2182 struct kcqe *cqes[1];
2183
2184 memset(&kcqe, 0, sizeof(kcqe));
2185 kcqe.pg_host_opaque = req->host_opaque;
2186 kcqe.pg_cid = req->host_opaque;
2187 kcqe.op_code = L4_KCQE_OPCODE_VALUE_OFFLOAD_PG;
2188 cqes[0] = (struct kcqe *) &kcqe;
2189 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2190 return 0;
2191}
2192
2193static int cnic_bnx2x_update_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2194{
2195 struct l4_kwq_update_pg *req = (struct l4_kwq_update_pg *) kwqe;
2196 struct l4_kcq kcqe;
2197 struct kcqe *cqes[1];
2198
2199 memset(&kcqe, 0, sizeof(kcqe));
2200 kcqe.pg_host_opaque = req->pg_host_opaque;
2201 kcqe.pg_cid = req->pg_cid;
2202 kcqe.op_code = L4_KCQE_OPCODE_VALUE_UPDATE_PG;
2203 cqes[0] = (struct kcqe *) &kcqe;
2204 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2205 return 0;
2206}
2207
Michael Chane1928c82010-12-23 07:43:04 +00002208static int cnic_bnx2x_fcoe_stat(struct cnic_dev *dev, struct kwqe *kwqe)
2209{
2210 struct fcoe_kwqe_stat *req;
2211 struct fcoe_stat_ramrod_params *fcoe_stat;
2212 union l5cm_specific_data l5_data;
2213 struct cnic_local *cp = dev->cnic_priv;
2214 int ret;
2215 u32 cid;
2216
2217 req = (struct fcoe_kwqe_stat *) kwqe;
2218 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2219
2220 fcoe_stat = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2221 if (!fcoe_stat)
2222 return -ENOMEM;
2223
2224 memset(fcoe_stat, 0, sizeof(*fcoe_stat));
2225 memcpy(&fcoe_stat->stat_kwqe, req, sizeof(*req));
2226
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002227 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_STAT_FUNC, cid,
Michael Chane1928c82010-12-23 07:43:04 +00002228 FCOE_CONNECTION_TYPE, &l5_data);
2229 return ret;
2230}
2231
2232static int cnic_bnx2x_fcoe_init1(struct cnic_dev *dev, struct kwqe *wqes[],
2233 u32 num, int *work)
2234{
2235 int ret;
2236 struct cnic_local *cp = dev->cnic_priv;
2237 u32 cid;
2238 struct fcoe_init_ramrod_params *fcoe_init;
2239 struct fcoe_kwqe_init1 *req1;
2240 struct fcoe_kwqe_init2 *req2;
2241 struct fcoe_kwqe_init3 *req3;
2242 union l5cm_specific_data l5_data;
2243
2244 if (num < 3) {
2245 *work = num;
2246 return -EINVAL;
2247 }
2248 req1 = (struct fcoe_kwqe_init1 *) wqes[0];
2249 req2 = (struct fcoe_kwqe_init2 *) wqes[1];
2250 req3 = (struct fcoe_kwqe_init3 *) wqes[2];
2251 if (req2->hdr.op_code != FCOE_KWQE_OPCODE_INIT2) {
2252 *work = 1;
2253 return -EINVAL;
2254 }
2255 if (req3->hdr.op_code != FCOE_KWQE_OPCODE_INIT3) {
2256 *work = 2;
2257 return -EINVAL;
2258 }
2259
2260 if (sizeof(*fcoe_init) > CNIC_KWQ16_DATA_SIZE) {
2261 netdev_err(dev->netdev, "fcoe_init size too big\n");
2262 return -ENOMEM;
2263 }
2264 fcoe_init = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2265 if (!fcoe_init)
2266 return -ENOMEM;
2267
2268 memset(fcoe_init, 0, sizeof(*fcoe_init));
2269 memcpy(&fcoe_init->init_kwqe1, req1, sizeof(*req1));
2270 memcpy(&fcoe_init->init_kwqe2, req2, sizeof(*req2));
2271 memcpy(&fcoe_init->init_kwqe3, req3, sizeof(*req3));
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002272 fcoe_init->eq_pbl_base.lo = cp->kcq2.dma.pgtbl_map & 0xffffffff;
2273 fcoe_init->eq_pbl_base.hi = (u64) cp->kcq2.dma.pgtbl_map >> 32;
2274 fcoe_init->eq_pbl_size = cp->kcq2.dma.num_pages;
Michael Chane1928c82010-12-23 07:43:04 +00002275
2276 fcoe_init->sb_num = cp->status_blk_num;
2277 fcoe_init->eq_prod = MAX_KCQ_IDX;
2278 fcoe_init->sb_id = HC_INDEX_FCOE_EQ_CONS;
2279 cp->kcq2.sw_prod_idx = 0;
2280
2281 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002282 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_INIT_FUNC, cid,
Michael Chane1928c82010-12-23 07:43:04 +00002283 FCOE_CONNECTION_TYPE, &l5_data);
2284 *work = 3;
2285 return ret;
2286}
2287
2288static int cnic_bnx2x_fcoe_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
2289 u32 num, int *work)
2290{
2291 int ret = 0;
2292 u32 cid = -1, l5_cid;
2293 struct cnic_local *cp = dev->cnic_priv;
2294 struct fcoe_kwqe_conn_offload1 *req1;
2295 struct fcoe_kwqe_conn_offload2 *req2;
2296 struct fcoe_kwqe_conn_offload3 *req3;
2297 struct fcoe_kwqe_conn_offload4 *req4;
2298 struct fcoe_conn_offload_ramrod_params *fcoe_offload;
2299 struct cnic_context *ctx;
2300 struct fcoe_context *fctx;
2301 struct regpair ctx_addr;
2302 union l5cm_specific_data l5_data;
2303 struct fcoe_kcqe kcqe;
2304 struct kcqe *cqes[1];
2305
2306 if (num < 4) {
2307 *work = num;
2308 return -EINVAL;
2309 }
2310 req1 = (struct fcoe_kwqe_conn_offload1 *) wqes[0];
2311 req2 = (struct fcoe_kwqe_conn_offload2 *) wqes[1];
2312 req3 = (struct fcoe_kwqe_conn_offload3 *) wqes[2];
2313 req4 = (struct fcoe_kwqe_conn_offload4 *) wqes[3];
2314
2315 *work = 4;
2316
2317 l5_cid = req1->fcoe_conn_id;
Michael Chandc219a22011-08-26 09:45:39 +00002318 if (l5_cid >= dev->max_fcoe_conn)
Michael Chane1928c82010-12-23 07:43:04 +00002319 goto err_reply;
2320
2321 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2322
2323 ctx = &cp->ctx_tbl[l5_cid];
2324 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2325 goto err_reply;
2326
2327 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
2328 if (ret) {
2329 ret = 0;
2330 goto err_reply;
2331 }
2332 cid = ctx->cid;
2333
2334 fctx = cnic_get_bnx2x_ctx(dev, cid, 1, &ctx_addr);
2335 if (fctx) {
2336 u32 hw_cid = BNX2X_HW_CID(cp, cid);
2337 u32 val;
2338
2339 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
2340 FCOE_CONNECTION_TYPE);
2341 fctx->xstorm_ag_context.cdu_reserved = val;
2342 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
2343 FCOE_CONNECTION_TYPE);
2344 fctx->ustorm_ag_context.cdu_usage = val;
2345 }
2346 if (sizeof(*fcoe_offload) > CNIC_KWQ16_DATA_SIZE) {
2347 netdev_err(dev->netdev, "fcoe_offload size too big\n");
2348 goto err_reply;
2349 }
2350 fcoe_offload = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2351 if (!fcoe_offload)
2352 goto err_reply;
2353
2354 memset(fcoe_offload, 0, sizeof(*fcoe_offload));
2355 memcpy(&fcoe_offload->offload_kwqe1, req1, sizeof(*req1));
2356 memcpy(&fcoe_offload->offload_kwqe2, req2, sizeof(*req2));
2357 memcpy(&fcoe_offload->offload_kwqe3, req3, sizeof(*req3));
2358 memcpy(&fcoe_offload->offload_kwqe4, req4, sizeof(*req4));
2359
2360 cid = BNX2X_HW_CID(cp, cid);
2361 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_OFFLOAD_CONN, cid,
2362 FCOE_CONNECTION_TYPE, &l5_data);
2363 if (!ret)
2364 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
2365
2366 return ret;
2367
2368err_reply:
2369 if (cid != -1)
2370 cnic_free_bnx2x_conn_resc(dev, l5_cid);
2371
2372 memset(&kcqe, 0, sizeof(kcqe));
2373 kcqe.op_code = FCOE_KCQE_OPCODE_OFFLOAD_CONN;
2374 kcqe.fcoe_conn_id = req1->fcoe_conn_id;
2375 kcqe.completion_status = FCOE_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
2376
2377 cqes[0] = (struct kcqe *) &kcqe;
2378 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2379 return ret;
2380}
2381
2382static int cnic_bnx2x_fcoe_enable(struct cnic_dev *dev, struct kwqe *kwqe)
2383{
2384 struct fcoe_kwqe_conn_enable_disable *req;
2385 struct fcoe_conn_enable_disable_ramrod_params *fcoe_enable;
2386 union l5cm_specific_data l5_data;
2387 int ret;
2388 u32 cid, l5_cid;
2389 struct cnic_local *cp = dev->cnic_priv;
2390
2391 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2392 cid = req->context_id;
2393 l5_cid = req->conn_id + BNX2X_FCOE_L5_CID_BASE;
2394
2395 if (sizeof(*fcoe_enable) > CNIC_KWQ16_DATA_SIZE) {
2396 netdev_err(dev->netdev, "fcoe_enable size too big\n");
2397 return -ENOMEM;
2398 }
2399 fcoe_enable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2400 if (!fcoe_enable)
2401 return -ENOMEM;
2402
2403 memset(fcoe_enable, 0, sizeof(*fcoe_enable));
2404 memcpy(&fcoe_enable->enable_disable_kwqe, req, sizeof(*req));
2405 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_ENABLE_CONN, cid,
2406 FCOE_CONNECTION_TYPE, &l5_data);
2407 return ret;
2408}
2409
2410static int cnic_bnx2x_fcoe_disable(struct cnic_dev *dev, struct kwqe *kwqe)
2411{
2412 struct fcoe_kwqe_conn_enable_disable *req;
2413 struct fcoe_conn_enable_disable_ramrod_params *fcoe_disable;
2414 union l5cm_specific_data l5_data;
2415 int ret;
2416 u32 cid, l5_cid;
2417 struct cnic_local *cp = dev->cnic_priv;
2418
2419 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2420 cid = req->context_id;
2421 l5_cid = req->conn_id;
Michael Chandc219a22011-08-26 09:45:39 +00002422 if (l5_cid >= dev->max_fcoe_conn)
Michael Chane1928c82010-12-23 07:43:04 +00002423 return -EINVAL;
2424
2425 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2426
2427 if (sizeof(*fcoe_disable) > CNIC_KWQ16_DATA_SIZE) {
2428 netdev_err(dev->netdev, "fcoe_disable size too big\n");
2429 return -ENOMEM;
2430 }
2431 fcoe_disable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2432 if (!fcoe_disable)
2433 return -ENOMEM;
2434
2435 memset(fcoe_disable, 0, sizeof(*fcoe_disable));
2436 memcpy(&fcoe_disable->enable_disable_kwqe, req, sizeof(*req));
2437 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DISABLE_CONN, cid,
2438 FCOE_CONNECTION_TYPE, &l5_data);
2439 return ret;
2440}
2441
2442static int cnic_bnx2x_fcoe_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2443{
2444 struct fcoe_kwqe_conn_destroy *req;
2445 union l5cm_specific_data l5_data;
2446 int ret;
2447 u32 cid, l5_cid;
2448 struct cnic_local *cp = dev->cnic_priv;
2449 struct cnic_context *ctx;
2450 struct fcoe_kcqe kcqe;
2451 struct kcqe *cqes[1];
2452
2453 req = (struct fcoe_kwqe_conn_destroy *) kwqe;
2454 cid = req->context_id;
2455 l5_cid = req->conn_id;
Michael Chandc219a22011-08-26 09:45:39 +00002456 if (l5_cid >= dev->max_fcoe_conn)
Michael Chane1928c82010-12-23 07:43:04 +00002457 return -EINVAL;
2458
2459 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2460
2461 ctx = &cp->ctx_tbl[l5_cid];
2462
2463 init_waitqueue_head(&ctx->waitq);
2464 ctx->wait_cond = 0;
2465
Michael Chandcc7e3a2011-08-26 09:45:40 +00002466 memset(&kcqe, 0, sizeof(kcqe));
2467 kcqe.completion_status = FCOE_KCQE_COMPLETION_STATUS_ERROR;
Michael Chane1928c82010-12-23 07:43:04 +00002468 memset(&l5_data, 0, sizeof(l5_data));
2469 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_TERMINATE_CONN, cid,
2470 FCOE_CONNECTION_TYPE, &l5_data);
2471 if (ret == 0) {
Michael Chandcc7e3a2011-08-26 09:45:40 +00002472 wait_event_timeout(ctx->waitq, ctx->wait_cond, CNIC_RAMROD_TMO);
2473 if (ctx->wait_cond)
2474 kcqe.completion_status = 0;
Michael Chane1928c82010-12-23 07:43:04 +00002475 }
2476
Michael Chandcc7e3a2011-08-26 09:45:40 +00002477 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
2478 queue_delayed_work(cnic_wq, &cp->delete_task, msecs_to_jiffies(2000));
2479
Michael Chane1928c82010-12-23 07:43:04 +00002480 kcqe.op_code = FCOE_KCQE_OPCODE_DESTROY_CONN;
2481 kcqe.fcoe_conn_id = req->conn_id;
2482 kcqe.fcoe_conn_context_id = cid;
2483
2484 cqes[0] = (struct kcqe *) &kcqe;
2485 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2486 return ret;
2487}
2488
Michael Chan74e49bb2011-07-20 14:55:23 +00002489static void cnic_bnx2x_delete_wait(struct cnic_dev *dev, u32 start_cid)
2490{
2491 struct cnic_local *cp = dev->cnic_priv;
2492 u32 i;
2493
2494 for (i = start_cid; i < cp->max_cid_space; i++) {
2495 struct cnic_context *ctx = &cp->ctx_tbl[i];
2496 int j;
2497
2498 while (test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
2499 msleep(10);
2500
2501 for (j = 0; j < 5; j++) {
2502 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2503 break;
2504 msleep(20);
2505 }
2506
2507 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2508 netdev_warn(dev->netdev, "CID %x not deleted\n",
2509 ctx->cid);
2510 }
2511}
2512
Michael Chane1928c82010-12-23 07:43:04 +00002513static int cnic_bnx2x_fcoe_fw_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2514{
2515 struct fcoe_kwqe_destroy *req;
2516 union l5cm_specific_data l5_data;
2517 struct cnic_local *cp = dev->cnic_priv;
2518 int ret;
2519 u32 cid;
2520
Michael Chan74e49bb2011-07-20 14:55:23 +00002521 cnic_bnx2x_delete_wait(dev, MAX_ISCSI_TBL_SZ);
2522
Michael Chane1928c82010-12-23 07:43:04 +00002523 req = (struct fcoe_kwqe_destroy *) kwqe;
2524 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2525
2526 memset(&l5_data, 0, sizeof(l5_data));
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002527 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DESTROY_FUNC, cid,
Michael Chane1928c82010-12-23 07:43:04 +00002528 FCOE_CONNECTION_TYPE, &l5_data);
2529 return ret;
2530}
2531
Michael Chan23021c22012-01-04 12:12:28 +00002532static void cnic_bnx2x_kwqe_err(struct cnic_dev *dev, struct kwqe *kwqe)
2533{
2534 struct cnic_local *cp = dev->cnic_priv;
2535 struct kcqe kcqe;
2536 struct kcqe *cqes[1];
2537 u32 cid;
2538 u32 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2539 u32 layer_code = kwqe->kwqe_op_flag & KWQE_LAYER_MASK;
Michael Chan3238a9b2012-02-05 15:24:40 +00002540 u32 kcqe_op;
Michael Chan23021c22012-01-04 12:12:28 +00002541 int ulp_type;
2542
2543 cid = kwqe->kwqe_info0;
2544 memset(&kcqe, 0, sizeof(kcqe));
2545
Michael Chan3238a9b2012-02-05 15:24:40 +00002546 if (layer_code == KWQE_FLAGS_LAYER_MASK_L5_FCOE) {
2547 u32 l5_cid = 0;
2548
2549 ulp_type = CNIC_ULP_FCOE;
2550 if (opcode == FCOE_KWQE_OPCODE_DISABLE_CONN) {
2551 struct fcoe_kwqe_conn_enable_disable *req;
2552
2553 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2554 kcqe_op = FCOE_KCQE_OPCODE_DISABLE_CONN;
2555 cid = req->context_id;
2556 l5_cid = req->conn_id;
2557 } else if (opcode == FCOE_KWQE_OPCODE_DESTROY) {
2558 kcqe_op = FCOE_KCQE_OPCODE_DESTROY_FUNC;
2559 } else {
2560 return;
2561 }
2562 kcqe.kcqe_op_flag = kcqe_op << KCQE_FLAGS_OPCODE_SHIFT;
2563 kcqe.kcqe_op_flag |= KCQE_FLAGS_LAYER_MASK_L5_FCOE;
Michael Chan8ec3e702012-03-21 15:38:34 +00002564 kcqe.kcqe_info1 = FCOE_KCQE_COMPLETION_STATUS_PARITY_ERROR;
Michael Chan3238a9b2012-02-05 15:24:40 +00002565 kcqe.kcqe_info2 = cid;
2566 kcqe.kcqe_info0 = l5_cid;
2567
2568 } else if (layer_code == KWQE_FLAGS_LAYER_MASK_L5_ISCSI) {
Michael Chan23021c22012-01-04 12:12:28 +00002569 ulp_type = CNIC_ULP_ISCSI;
2570 if (opcode == ISCSI_KWQE_OPCODE_UPDATE_CONN)
2571 cid = kwqe->kwqe_info1;
2572
2573 kcqe.kcqe_op_flag = (opcode + 0x10) << KCQE_FLAGS_OPCODE_SHIFT;
2574 kcqe.kcqe_op_flag |= KCQE_FLAGS_LAYER_MASK_L5_ISCSI;
Michael Chan8ec3e702012-03-21 15:38:34 +00002575 kcqe.kcqe_info1 = ISCSI_KCQE_COMPLETION_STATUS_PARITY_ERR;
Michael Chan23021c22012-01-04 12:12:28 +00002576 kcqe.kcqe_info2 = cid;
2577 cnic_get_l5_cid(cp, BNX2X_SW_CID(cid), &kcqe.kcqe_info0);
2578
2579 } else if (layer_code == KWQE_FLAGS_LAYER_MASK_L4) {
2580 struct l4_kcq *l4kcqe = (struct l4_kcq *) &kcqe;
Michael Chan23021c22012-01-04 12:12:28 +00002581
2582 ulp_type = CNIC_ULP_L4;
2583 if (opcode == L4_KWQE_OPCODE_VALUE_CONNECT1)
2584 kcqe_op = L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE;
2585 else if (opcode == L4_KWQE_OPCODE_VALUE_RESET)
2586 kcqe_op = L4_KCQE_OPCODE_VALUE_RESET_COMP;
2587 else if (opcode == L4_KWQE_OPCODE_VALUE_CLOSE)
2588 kcqe_op = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
2589 else
2590 return;
2591
2592 kcqe.kcqe_op_flag = (kcqe_op << KCQE_FLAGS_OPCODE_SHIFT) |
2593 KCQE_FLAGS_LAYER_MASK_L4;
Michael Chan8ec3e702012-03-21 15:38:34 +00002594 l4kcqe->status = L4_KCQE_COMPLETION_STATUS_PARITY_ERROR;
Michael Chan23021c22012-01-04 12:12:28 +00002595 l4kcqe->cid = cid;
2596 cnic_get_l5_cid(cp, BNX2X_SW_CID(cid), &l4kcqe->conn_id);
2597 } else {
2598 return;
2599 }
2600
Joe Perches64699332012-06-04 12:44:16 +00002601 cqes[0] = &kcqe;
Michael Chan23021c22012-01-04 12:12:28 +00002602 cnic_reply_bnx2x_kcqes(dev, ulp_type, cqes, 1);
2603}
2604
Michael Chane1928c82010-12-23 07:43:04 +00002605static int cnic_submit_bnx2x_iscsi_kwqes(struct cnic_dev *dev,
2606 struct kwqe *wqes[], u32 num_wqes)
Michael Chan71034ba2009-10-10 13:46:59 +00002607{
2608 int i, work, ret;
2609 u32 opcode;
2610 struct kwqe *kwqe;
2611
2612 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2613 return -EAGAIN; /* bnx2 is down */
2614
2615 for (i = 0; i < num_wqes; ) {
2616 kwqe = wqes[i];
2617 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2618 work = 1;
2619
2620 switch (opcode) {
2621 case ISCSI_KWQE_OPCODE_INIT1:
2622 ret = cnic_bnx2x_iscsi_init1(dev, kwqe);
2623 break;
2624 case ISCSI_KWQE_OPCODE_INIT2:
2625 ret = cnic_bnx2x_iscsi_init2(dev, kwqe);
2626 break;
2627 case ISCSI_KWQE_OPCODE_OFFLOAD_CONN1:
2628 ret = cnic_bnx2x_iscsi_ofld1(dev, &wqes[i],
2629 num_wqes - i, &work);
2630 break;
2631 case ISCSI_KWQE_OPCODE_UPDATE_CONN:
2632 ret = cnic_bnx2x_iscsi_update(dev, kwqe);
2633 break;
2634 case ISCSI_KWQE_OPCODE_DESTROY_CONN:
2635 ret = cnic_bnx2x_iscsi_destroy(dev, kwqe);
2636 break;
2637 case L4_KWQE_OPCODE_VALUE_CONNECT1:
2638 ret = cnic_bnx2x_connect(dev, &wqes[i], num_wqes - i,
2639 &work);
2640 break;
2641 case L4_KWQE_OPCODE_VALUE_CLOSE:
2642 ret = cnic_bnx2x_close(dev, kwqe);
2643 break;
2644 case L4_KWQE_OPCODE_VALUE_RESET:
2645 ret = cnic_bnx2x_reset(dev, kwqe);
2646 break;
2647 case L4_KWQE_OPCODE_VALUE_OFFLOAD_PG:
2648 ret = cnic_bnx2x_offload_pg(dev, kwqe);
2649 break;
2650 case L4_KWQE_OPCODE_VALUE_UPDATE_PG:
2651 ret = cnic_bnx2x_update_pg(dev, kwqe);
2652 break;
2653 case L4_KWQE_OPCODE_VALUE_UPLOAD_PG:
2654 ret = 0;
2655 break;
2656 default:
2657 ret = 0;
Joe Perchesddf79b22010-02-17 15:01:54 +00002658 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2659 opcode);
Michael Chan71034ba2009-10-10 13:46:59 +00002660 break;
2661 }
Michael Chan23021c22012-01-04 12:12:28 +00002662 if (ret < 0) {
Joe Perchesddf79b22010-02-17 15:01:54 +00002663 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2664 opcode);
Michael Chan23021c22012-01-04 12:12:28 +00002665
2666 /* Possibly bnx2x parity error, send completion
2667 * to ulp drivers with error code to speed up
2668 * cleanup and reset recovery.
2669 */
2670 if (ret == -EIO || ret == -EAGAIN)
2671 cnic_bnx2x_kwqe_err(dev, kwqe);
2672 }
Michael Chan71034ba2009-10-10 13:46:59 +00002673 i += work;
2674 }
2675 return 0;
2676}
2677
Michael Chane1928c82010-12-23 07:43:04 +00002678static int cnic_submit_bnx2x_fcoe_kwqes(struct cnic_dev *dev,
2679 struct kwqe *wqes[], u32 num_wqes)
2680{
2681 struct cnic_local *cp = dev->cnic_priv;
2682 int i, work, ret;
2683 u32 opcode;
2684 struct kwqe *kwqe;
2685
2686 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2687 return -EAGAIN; /* bnx2 is down */
2688
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002689 if (!BNX2X_CHIP_IS_E2_PLUS(cp->chip_id))
Michael Chane1928c82010-12-23 07:43:04 +00002690 return -EINVAL;
2691
2692 for (i = 0; i < num_wqes; ) {
2693 kwqe = wqes[i];
2694 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2695 work = 1;
2696
2697 switch (opcode) {
2698 case FCOE_KWQE_OPCODE_INIT1:
2699 ret = cnic_bnx2x_fcoe_init1(dev, &wqes[i],
2700 num_wqes - i, &work);
2701 break;
2702 case FCOE_KWQE_OPCODE_OFFLOAD_CONN1:
2703 ret = cnic_bnx2x_fcoe_ofld1(dev, &wqes[i],
2704 num_wqes - i, &work);
2705 break;
2706 case FCOE_KWQE_OPCODE_ENABLE_CONN:
2707 ret = cnic_bnx2x_fcoe_enable(dev, kwqe);
2708 break;
2709 case FCOE_KWQE_OPCODE_DISABLE_CONN:
2710 ret = cnic_bnx2x_fcoe_disable(dev, kwqe);
2711 break;
2712 case FCOE_KWQE_OPCODE_DESTROY_CONN:
2713 ret = cnic_bnx2x_fcoe_destroy(dev, kwqe);
2714 break;
2715 case FCOE_KWQE_OPCODE_DESTROY:
2716 ret = cnic_bnx2x_fcoe_fw_destroy(dev, kwqe);
2717 break;
2718 case FCOE_KWQE_OPCODE_STAT:
2719 ret = cnic_bnx2x_fcoe_stat(dev, kwqe);
2720 break;
2721 default:
2722 ret = 0;
2723 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2724 opcode);
2725 break;
2726 }
Michael Chan3238a9b2012-02-05 15:24:40 +00002727 if (ret < 0) {
Michael Chane1928c82010-12-23 07:43:04 +00002728 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2729 opcode);
Michael Chan3238a9b2012-02-05 15:24:40 +00002730
2731 /* Possibly bnx2x parity error, send completion
2732 * to ulp drivers with error code to speed up
2733 * cleanup and reset recovery.
2734 */
2735 if (ret == -EIO || ret == -EAGAIN)
2736 cnic_bnx2x_kwqe_err(dev, kwqe);
2737 }
Michael Chane1928c82010-12-23 07:43:04 +00002738 i += work;
2739 }
2740 return 0;
2741}
2742
2743static int cnic_submit_bnx2x_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
2744 u32 num_wqes)
2745{
2746 int ret = -EINVAL;
2747 u32 layer_code;
2748
2749 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2750 return -EAGAIN; /* bnx2x is down */
2751
2752 if (!num_wqes)
2753 return 0;
2754
2755 layer_code = wqes[0]->kwqe_op_flag & KWQE_LAYER_MASK;
2756 switch (layer_code) {
2757 case KWQE_FLAGS_LAYER_MASK_L5_ISCSI:
2758 case KWQE_FLAGS_LAYER_MASK_L4:
2759 case KWQE_FLAGS_LAYER_MASK_L2:
2760 ret = cnic_submit_bnx2x_iscsi_kwqes(dev, wqes, num_wqes);
2761 break;
2762
2763 case KWQE_FLAGS_LAYER_MASK_L5_FCOE:
2764 ret = cnic_submit_bnx2x_fcoe_kwqes(dev, wqes, num_wqes);
2765 break;
2766 }
2767 return ret;
2768}
2769
2770static inline u32 cnic_get_kcqe_layer_mask(u32 opflag)
2771{
2772 if (unlikely(KCQE_OPCODE(opflag) == FCOE_RAMROD_CMD_ID_TERMINATE_CONN))
2773 return KCQE_FLAGS_LAYER_MASK_L4;
2774
2775 return opflag & KCQE_FLAGS_LAYER_MASK;
2776}
2777
Michael Chana4636962009-06-08 18:14:43 -07002778static void service_kcqes(struct cnic_dev *dev, int num_cqes)
2779{
2780 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002781 int i, j, comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002782
2783 i = 0;
2784 j = 1;
2785 while (num_cqes) {
2786 struct cnic_ulp_ops *ulp_ops;
2787 int ulp_type;
2788 u32 kcqe_op_flag = cp->completed_kcq[i]->kcqe_op_flag;
Michael Chane1928c82010-12-23 07:43:04 +00002789 u32 kcqe_layer = cnic_get_kcqe_layer_mask(kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002790
2791 if (unlikely(kcqe_op_flag & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002792 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002793
2794 while (j < num_cqes) {
2795 u32 next_op = cp->completed_kcq[i + j]->kcqe_op_flag;
2796
Michael Chane1928c82010-12-23 07:43:04 +00002797 if (cnic_get_kcqe_layer_mask(next_op) != kcqe_layer)
Michael Chana4636962009-06-08 18:14:43 -07002798 break;
2799
2800 if (unlikely(next_op & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002801 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002802 j++;
2803 }
2804
2805 if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_RDMA)
2806 ulp_type = CNIC_ULP_RDMA;
2807 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_ISCSI)
2808 ulp_type = CNIC_ULP_ISCSI;
Michael Chane1928c82010-12-23 07:43:04 +00002809 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_FCOE)
2810 ulp_type = CNIC_ULP_FCOE;
Michael Chana4636962009-06-08 18:14:43 -07002811 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L4)
2812 ulp_type = CNIC_ULP_L4;
2813 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L2)
2814 goto end;
2815 else {
Joe Perchesddf79b22010-02-17 15:01:54 +00002816 netdev_err(dev->netdev, "Unknown type of KCQE(0x%x)\n",
2817 kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002818 goto end;
2819 }
2820
2821 rcu_read_lock();
2822 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
2823 if (likely(ulp_ops)) {
2824 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
2825 cp->completed_kcq + i, j);
2826 }
2827 rcu_read_unlock();
2828end:
2829 num_cqes -= j;
2830 i += j;
2831 j = 1;
2832 }
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002833 if (unlikely(comp))
2834 cnic_spq_completion(dev, DRV_CTL_RET_L5_SPQ_CREDIT_CMD, comp);
Michael Chana4636962009-06-08 18:14:43 -07002835}
2836
Michael Chan644b9d42010-06-24 14:58:40 +00002837static int cnic_get_kcqes(struct cnic_dev *dev, struct kcq_info *info)
Michael Chana4636962009-06-08 18:14:43 -07002838{
2839 struct cnic_local *cp = dev->cnic_priv;
Michael Chan644b9d42010-06-24 14:58:40 +00002840 u16 i, ri, hw_prod, last;
Michael Chana4636962009-06-08 18:14:43 -07002841 struct kcqe *kcqe;
2842 int kcqe_cnt = 0, last_cnt = 0;
2843
Michael Chan644b9d42010-06-24 14:58:40 +00002844 i = ri = last = info->sw_prod_idx;
Michael Chana4636962009-06-08 18:14:43 -07002845 ri &= MAX_KCQ_IDX;
Michael Chan644b9d42010-06-24 14:58:40 +00002846 hw_prod = *info->hw_prod_idx_ptr;
Michael Chan59e51372011-06-14 01:32:38 +00002847 hw_prod = info->hw_idx(hw_prod);
Michael Chana4636962009-06-08 18:14:43 -07002848
2849 while ((i != hw_prod) && (kcqe_cnt < MAX_COMPLETED_KCQE)) {
Michael Chan644b9d42010-06-24 14:58:40 +00002850 kcqe = &info->kcq[KCQ_PG(ri)][KCQ_IDX(ri)];
Michael Chana4636962009-06-08 18:14:43 -07002851 cp->completed_kcq[kcqe_cnt++] = kcqe;
Michael Chan59e51372011-06-14 01:32:38 +00002852 i = info->next_idx(i);
Michael Chana4636962009-06-08 18:14:43 -07002853 ri = i & MAX_KCQ_IDX;
2854 if (likely(!(kcqe->kcqe_op_flag & KCQE_FLAGS_NEXT))) {
2855 last_cnt = kcqe_cnt;
2856 last = i;
2857 }
2858 }
2859
Michael Chan644b9d42010-06-24 14:58:40 +00002860 info->sw_prod_idx = last;
Michael Chana4636962009-06-08 18:14:43 -07002861 return last_cnt;
2862}
2863
Michael Chan48f753d2010-05-18 11:32:53 +00002864static int cnic_l2_completion(struct cnic_local *cp)
2865{
2866 u16 hw_cons, sw_cons;
Michael Chancd801532010-10-13 14:06:49 +00002867 struct cnic_uio_dev *udev = cp->udev;
Michael Chan48f753d2010-05-18 11:32:53 +00002868 union eth_rx_cqe *cqe, *cqe_ring = (union eth_rx_cqe *)
Michael Chancd801532010-10-13 14:06:49 +00002869 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Michael Chan48f753d2010-05-18 11:32:53 +00002870 u32 cmd;
2871 int comp = 0;
2872
2873 if (!test_bit(CNIC_F_BNX2X_CLASS, &cp->dev->flags))
2874 return 0;
2875
2876 hw_cons = *cp->rx_cons_ptr;
2877 if ((hw_cons & BNX2X_MAX_RCQ_DESC_CNT) == BNX2X_MAX_RCQ_DESC_CNT)
2878 hw_cons++;
2879
2880 sw_cons = cp->rx_cons;
2881 while (sw_cons != hw_cons) {
2882 u8 cqe_fp_flags;
2883
2884 cqe = &cqe_ring[sw_cons & BNX2X_MAX_RCQ_DESC_CNT];
2885 cqe_fp_flags = cqe->fast_path_cqe.type_error_flags;
2886 if (cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE) {
2887 cmd = le32_to_cpu(cqe->ramrod_cqe.conn_and_cmd_data);
2888 cmd >>= COMMON_RAMROD_ETH_RX_CQE_CMD_ID_SHIFT;
2889 if (cmd == RAMROD_CMD_ID_ETH_CLIENT_SETUP ||
2890 cmd == RAMROD_CMD_ID_ETH_HALT)
2891 comp++;
2892 }
2893 sw_cons = BNX2X_NEXT_RCQE(sw_cons);
2894 }
2895 return comp;
2896}
2897
Michael Chan86b53602009-10-10 13:46:57 +00002898static void cnic_chk_pkt_rings(struct cnic_local *cp)
Michael Chana4636962009-06-08 18:14:43 -07002899{
Michael Chan541a7812010-10-06 03:17:22 +00002900 u16 rx_cons, tx_cons;
Michael Chan48f753d2010-05-18 11:32:53 +00002901 int comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002902
Michael Chan541a7812010-10-06 03:17:22 +00002903 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
Michael Chan66fee9e2010-06-24 14:58:38 +00002904 return;
2905
Michael Chan541a7812010-10-06 03:17:22 +00002906 rx_cons = *cp->rx_cons_ptr;
2907 tx_cons = *cp->tx_cons_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002908 if (cp->tx_cons != tx_cons || cp->rx_cons != rx_cons) {
Michael Chan48f753d2010-05-18 11:32:53 +00002909 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
2910 comp = cnic_l2_completion(cp);
2911
Michael Chana4636962009-06-08 18:14:43 -07002912 cp->tx_cons = tx_cons;
2913 cp->rx_cons = rx_cons;
Michael Chan71034ba2009-10-10 13:46:59 +00002914
Michael Chancd801532010-10-13 14:06:49 +00002915 if (cp->udev)
2916 uio_event_notify(&cp->udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -07002917 }
Michael Chan48f753d2010-05-18 11:32:53 +00002918 if (comp)
2919 clear_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07002920}
2921
Michael Chanb177a5d52010-06-24 14:58:41 +00002922static u32 cnic_service_bnx2_queues(struct cnic_dev *dev)
Michael Chana4636962009-06-08 18:14:43 -07002923{
Michael Chana4636962009-06-08 18:14:43 -07002924 struct cnic_local *cp = dev->cnic_priv;
Michael Chanb177a5d52010-06-24 14:58:41 +00002925 u32 status_idx = (u16) *cp->kcq1.status_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002926 int kcqe_cnt;
2927
Michael Chan107c3f42011-03-02 13:00:49 +00002928 /* status block index must be read before reading other fields */
2929 rmb();
Michael Chana4636962009-06-08 18:14:43 -07002930 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
2931
Michael Chan644b9d42010-06-24 14:58:40 +00002932 while ((kcqe_cnt = cnic_get_kcqes(dev, &cp->kcq1))) {
Michael Chana4636962009-06-08 18:14:43 -07002933
2934 service_kcqes(dev, kcqe_cnt);
2935
2936 /* Tell compiler that status_blk fields can change. */
2937 barrier();
Michael Chan93736652011-06-08 19:29:32 +00002938 status_idx = (u16) *cp->kcq1.status_idx_ptr;
2939 /* status block index must be read first */
2940 rmb();
2941 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002942 }
2943
Michael Chan644b9d42010-06-24 14:58:40 +00002944 CNIC_WR16(dev, cp->kcq1.io_addr, cp->kcq1.sw_prod_idx);
Michael Chana4636962009-06-08 18:14:43 -07002945
Michael Chan86b53602009-10-10 13:46:57 +00002946 cnic_chk_pkt_rings(cp);
Michael Chanb177a5d52010-06-24 14:58:41 +00002947
Michael Chana4636962009-06-08 18:14:43 -07002948 return status_idx;
2949}
2950
Michael Chanb177a5d52010-06-24 14:58:41 +00002951static int cnic_service_bnx2(void *data, void *status_blk)
2952{
2953 struct cnic_dev *dev = data;
Michael Chanb177a5d52010-06-24 14:58:41 +00002954
Michael Chaneaaa6e92010-12-23 08:38:30 +00002955 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
2956 struct status_block *sblk = status_blk;
2957
2958 return sblk->status_idx;
2959 }
Michael Chanb177a5d52010-06-24 14:58:41 +00002960
2961 return cnic_service_bnx2_queues(dev);
2962}
2963
Michael Chana4636962009-06-08 18:14:43 -07002964static void cnic_service_bnx2_msix(unsigned long data)
2965{
2966 struct cnic_dev *dev = (struct cnic_dev *) data;
2967 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002968
Michael Chanb177a5d52010-06-24 14:58:41 +00002969 cp->last_status_idx = cnic_service_bnx2_queues(dev);
Michael Chana4636962009-06-08 18:14:43 -07002970
Michael Chana4636962009-06-08 18:14:43 -07002971 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
2972 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
2973}
2974
Michael Chan66fee9e2010-06-24 14:58:38 +00002975static void cnic_doirq(struct cnic_dev *dev)
2976{
2977 struct cnic_local *cp = dev->cnic_priv;
Michael Chan66fee9e2010-06-24 14:58:38 +00002978
2979 if (likely(test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
Michael Chaneaaa6e92010-12-23 08:38:30 +00002980 u16 prod = cp->kcq1.sw_prod_idx & MAX_KCQ_IDX;
2981
Michael Chan66fee9e2010-06-24 14:58:38 +00002982 prefetch(cp->status_blk.gen);
Michael Chane6c28892010-06-24 14:58:39 +00002983 prefetch(&cp->kcq1.kcq[KCQ_PG(prod)][KCQ_IDX(prod)]);
Michael Chan66fee9e2010-06-24 14:58:38 +00002984
2985 tasklet_schedule(&cp->cnic_irq_task);
2986 }
2987}
2988
Michael Chana4636962009-06-08 18:14:43 -07002989static irqreturn_t cnic_irq(int irq, void *dev_instance)
2990{
2991 struct cnic_dev *dev = dev_instance;
2992 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002993
2994 if (cp->ack_int)
2995 cp->ack_int(dev);
2996
Michael Chan66fee9e2010-06-24 14:58:38 +00002997 cnic_doirq(dev);
Michael Chana4636962009-06-08 18:14:43 -07002998
2999 return IRQ_HANDLED;
3000}
3001
Michael Chan71034ba2009-10-10 13:46:59 +00003002static inline void cnic_ack_bnx2x_int(struct cnic_dev *dev, u8 id, u8 storm,
3003 u16 index, u8 op, u8 update)
3004{
3005 struct cnic_local *cp = dev->cnic_priv;
3006 u32 hc_addr = (HC_REG_COMMAND_REG + CNIC_PORT(cp) * 32 +
3007 COMMAND_REG_INT_ACK);
3008 struct igu_ack_register igu_ack;
3009
3010 igu_ack.status_block_index = index;
3011 igu_ack.sb_id_and_flags =
3012 ((id << IGU_ACK_REGISTER_STATUS_BLOCK_ID_SHIFT) |
3013 (storm << IGU_ACK_REGISTER_STORM_ID_SHIFT) |
3014 (update << IGU_ACK_REGISTER_UPDATE_INDEX_SHIFT) |
3015 (op << IGU_ACK_REGISTER_INTERRUPT_MODE_SHIFT));
3016
3017 CNIC_WR(dev, hc_addr, (*(u32 *)&igu_ack));
3018}
3019
Michael Chanee87a822010-10-13 14:06:51 +00003020static void cnic_ack_igu_sb(struct cnic_dev *dev, u8 igu_sb_id, u8 segment,
3021 u16 index, u8 op, u8 update)
3022{
3023 struct igu_regular cmd_data;
3024 u32 igu_addr = BAR_IGU_INTMEM + (IGU_CMD_INT_ACK_BASE + igu_sb_id) * 8;
3025
3026 cmd_data.sb_id_and_flags =
3027 (index << IGU_REGULAR_SB_INDEX_SHIFT) |
3028 (segment << IGU_REGULAR_SEGMENT_ACCESS_SHIFT) |
3029 (update << IGU_REGULAR_BUPDATE_SHIFT) |
3030 (op << IGU_REGULAR_ENABLE_INT_SHIFT);
3031
3032
3033 CNIC_WR(dev, igu_addr, cmd_data.sb_id_and_flags);
3034}
3035
Michael Chan71034ba2009-10-10 13:46:59 +00003036static void cnic_ack_bnx2x_msix(struct cnic_dev *dev)
3037{
3038 struct cnic_local *cp = dev->cnic_priv;
3039
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003040 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, CSTORM_ID, 0,
Michael Chan71034ba2009-10-10 13:46:59 +00003041 IGU_INT_DISABLE, 0);
3042}
3043
Michael Chanee87a822010-10-13 14:06:51 +00003044static void cnic_ack_bnx2x_e2_msix(struct cnic_dev *dev)
3045{
3046 struct cnic_local *cp = dev->cnic_priv;
3047
3048 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF, 0,
3049 IGU_INT_DISABLE, 0);
3050}
3051
Michael Chanb177a5d52010-06-24 14:58:41 +00003052static u32 cnic_service_bnx2x_kcq(struct cnic_dev *dev, struct kcq_info *info)
Michael Chan71034ba2009-10-10 13:46:59 +00003053{
Michael Chanb177a5d52010-06-24 14:58:41 +00003054 u32 last_status = *info->status_idx_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00003055 int kcqe_cnt;
3056
Michael Chan107c3f42011-03-02 13:00:49 +00003057 /* status block index must be read before reading the KCQ */
3058 rmb();
Michael Chanb177a5d52010-06-24 14:58:41 +00003059 while ((kcqe_cnt = cnic_get_kcqes(dev, info))) {
Michael Chan71034ba2009-10-10 13:46:59 +00003060
3061 service_kcqes(dev, kcqe_cnt);
3062
3063 /* Tell compiler that sblk fields can change. */
3064 barrier();
Michael Chan71034ba2009-10-10 13:46:59 +00003065
Michael Chanb177a5d52010-06-24 14:58:41 +00003066 last_status = *info->status_idx_ptr;
Michael Chan107c3f42011-03-02 13:00:49 +00003067 /* status block index must be read before reading the KCQ */
3068 rmb();
Michael Chan71034ba2009-10-10 13:46:59 +00003069 }
Michael Chanb177a5d52010-06-24 14:58:41 +00003070 return last_status;
3071}
3072
3073static void cnic_service_bnx2x_bh(unsigned long data)
3074{
3075 struct cnic_dev *dev = (struct cnic_dev *) data;
3076 struct cnic_local *cp = dev->cnic_priv;
Michael Chan0197b082011-03-02 13:00:50 +00003077 u32 status_idx, new_status_idx;
Michael Chanb177a5d52010-06-24 14:58:41 +00003078
3079 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
3080 return;
3081
Michael Chan0197b082011-03-02 13:00:50 +00003082 while (1) {
3083 status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq1);
Michael Chan71034ba2009-10-10 13:46:59 +00003084
Michael Chan0197b082011-03-02 13:00:50 +00003085 CNIC_WR16(dev, cp->kcq1.io_addr,
3086 cp->kcq1.sw_prod_idx + MAX_KCQ_IDX);
Michael Chane21ba412010-12-23 07:43:03 +00003087
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03003088 if (!BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chan0197b082011-03-02 13:00:50 +00003089 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, USTORM_ID,
3090 status_idx, IGU_INT_ENABLE, 1);
3091 break;
3092 }
3093
3094 new_status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq2);
3095
3096 if (new_status_idx != status_idx)
3097 continue;
Michael Chane21ba412010-12-23 07:43:03 +00003098
3099 CNIC_WR16(dev, cp->kcq2.io_addr, cp->kcq2.sw_prod_idx +
3100 MAX_KCQ_IDX);
3101
Michael Chanee87a822010-10-13 14:06:51 +00003102 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF,
3103 status_idx, IGU_INT_ENABLE, 1);
Michael Chan0197b082011-03-02 13:00:50 +00003104
3105 break;
Michael Chane21ba412010-12-23 07:43:03 +00003106 }
Michael Chan71034ba2009-10-10 13:46:59 +00003107}
3108
3109static int cnic_service_bnx2x(void *data, void *status_blk)
3110{
3111 struct cnic_dev *dev = data;
3112 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00003113
Michael Chan66fee9e2010-06-24 14:58:38 +00003114 if (!(cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
3115 cnic_doirq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00003116
Michael Chan66fee9e2010-06-24 14:58:38 +00003117 cnic_chk_pkt_rings(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00003118
3119 return 0;
3120}
3121
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003122static void cnic_ulp_stop_one(struct cnic_local *cp, int if_type)
3123{
3124 struct cnic_ulp_ops *ulp_ops;
3125
3126 if (if_type == CNIC_ULP_ISCSI)
3127 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
3128
3129 mutex_lock(&cnic_lock);
3130 ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
3131 lockdep_is_held(&cnic_lock));
3132 if (!ulp_ops) {
3133 mutex_unlock(&cnic_lock);
3134 return;
3135 }
3136 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
3137 mutex_unlock(&cnic_lock);
3138
3139 if (test_and_clear_bit(ULP_F_START, &cp->ulp_flags[if_type]))
3140 ulp_ops->cnic_stop(cp->ulp_handle[if_type]);
3141
3142 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
3143}
3144
Michael Chana4636962009-06-08 18:14:43 -07003145static void cnic_ulp_stop(struct cnic_dev *dev)
3146{
3147 struct cnic_local *cp = dev->cnic_priv;
3148 int if_type;
3149
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003150 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++)
3151 cnic_ulp_stop_one(cp, if_type);
Michael Chana4636962009-06-08 18:14:43 -07003152}
3153
3154static void cnic_ulp_start(struct cnic_dev *dev)
3155{
3156 struct cnic_local *cp = dev->cnic_priv;
3157 int if_type;
3158
Michael Chana4636962009-06-08 18:14:43 -07003159 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
3160 struct cnic_ulp_ops *ulp_ops;
3161
Michael Chan681dbd72009-08-14 15:49:46 +00003162 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003163 ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
3164 lockdep_is_held(&cnic_lock));
Michael Chan681dbd72009-08-14 15:49:46 +00003165 if (!ulp_ops || !ulp_ops->cnic_start) {
3166 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003167 continue;
Michael Chan681dbd72009-08-14 15:49:46 +00003168 }
3169 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
3170 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003171
3172 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[if_type]))
3173 ulp_ops->cnic_start(cp->ulp_handle[if_type]);
Michael Chan681dbd72009-08-14 15:49:46 +00003174
3175 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
Michael Chana4636962009-06-08 18:14:43 -07003176 }
Michael Chana4636962009-06-08 18:14:43 -07003177}
3178
Barak Witkowski1d187b32011-12-05 22:41:50 +00003179static int cnic_copy_ulp_stats(struct cnic_dev *dev, int ulp_type)
3180{
3181 struct cnic_local *cp = dev->cnic_priv;
3182 struct cnic_ulp_ops *ulp_ops;
3183 int rc;
3184
3185 mutex_lock(&cnic_lock);
3186 ulp_ops = cnic_ulp_tbl_prot(ulp_type);
3187 if (ulp_ops && ulp_ops->cnic_get_stats)
3188 rc = ulp_ops->cnic_get_stats(cp->ulp_handle[ulp_type]);
3189 else
3190 rc = -ENODEV;
3191 mutex_unlock(&cnic_lock);
3192 return rc;
3193}
3194
Michael Chana4636962009-06-08 18:14:43 -07003195static int cnic_ctl(void *data, struct cnic_ctl_info *info)
3196{
3197 struct cnic_dev *dev = data;
Barak Witkowski1d187b32011-12-05 22:41:50 +00003198 int ulp_type = CNIC_ULP_ISCSI;
Michael Chana4636962009-06-08 18:14:43 -07003199
3200 switch (info->cmd) {
3201 case CNIC_CTL_STOP_CMD:
3202 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07003203
3204 cnic_ulp_stop(dev);
3205 cnic_stop_hw(dev);
3206
Michael Chana4636962009-06-08 18:14:43 -07003207 cnic_put(dev);
3208 break;
3209 case CNIC_CTL_START_CMD:
3210 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07003211
3212 if (!cnic_start_hw(dev))
3213 cnic_ulp_start(dev);
3214
Michael Chana4636962009-06-08 18:14:43 -07003215 cnic_put(dev);
3216 break;
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003217 case CNIC_CTL_STOP_ISCSI_CMD: {
3218 struct cnic_local *cp = dev->cnic_priv;
3219 set_bit(CNIC_LCL_FL_STOP_ISCSI, &cp->cnic_local_flags);
3220 queue_delayed_work(cnic_wq, &cp->delete_task, 0);
3221 break;
3222 }
Michael Chan71034ba2009-10-10 13:46:59 +00003223 case CNIC_CTL_COMPLETION_CMD: {
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03003224 struct cnic_ctl_completion *comp = &info->data.comp;
3225 u32 cid = BNX2X_SW_CID(comp->cid);
Michael Chan71034ba2009-10-10 13:46:59 +00003226 u32 l5_cid;
3227 struct cnic_local *cp = dev->cnic_priv;
3228
Michael Chana2028b232012-06-27 15:08:19 +00003229 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
3230 break;
3231
Michael Chan71034ba2009-10-10 13:46:59 +00003232 if (cnic_get_l5_cid(cp, cid, &l5_cid) == 0) {
3233 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3234
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03003235 if (unlikely(comp->error)) {
3236 set_bit(CTX_FL_CID_ERROR, &ctx->ctx_flags);
3237 netdev_err(dev->netdev,
3238 "CID %x CFC delete comp error %x\n",
3239 cid, comp->error);
3240 }
3241
Michael Chan71034ba2009-10-10 13:46:59 +00003242 ctx->wait_cond = 1;
3243 wake_up(&ctx->waitq);
3244 }
3245 break;
3246 }
Barak Witkowski1d187b32011-12-05 22:41:50 +00003247 case CNIC_CTL_FCOE_STATS_GET_CMD:
3248 ulp_type = CNIC_ULP_FCOE;
3249 /* fall through */
3250 case CNIC_CTL_ISCSI_STATS_GET_CMD:
3251 cnic_hold(dev);
3252 cnic_copy_ulp_stats(dev, ulp_type);
3253 cnic_put(dev);
3254 break;
3255
Michael Chana4636962009-06-08 18:14:43 -07003256 default:
3257 return -EINVAL;
3258 }
3259 return 0;
3260}
3261
3262static void cnic_ulp_init(struct cnic_dev *dev)
3263{
3264 int i;
3265 struct cnic_local *cp = dev->cnic_priv;
3266
Michael Chana4636962009-06-08 18:14:43 -07003267 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3268 struct cnic_ulp_ops *ulp_ops;
3269
Michael Chan7fc1ece2009-08-14 15:49:47 +00003270 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003271 ulp_ops = cnic_ulp_tbl_prot(i);
Michael Chan7fc1ece2009-08-14 15:49:47 +00003272 if (!ulp_ops || !ulp_ops->cnic_init) {
3273 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003274 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003275 }
3276 ulp_get(ulp_ops);
3277 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003278
3279 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3280 ulp_ops->cnic_init(dev);
3281
Michael Chan7fc1ece2009-08-14 15:49:47 +00003282 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003283 }
Michael Chana4636962009-06-08 18:14:43 -07003284}
3285
3286static void cnic_ulp_exit(struct cnic_dev *dev)
3287{
3288 int i;
3289 struct cnic_local *cp = dev->cnic_priv;
3290
Michael Chana4636962009-06-08 18:14:43 -07003291 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3292 struct cnic_ulp_ops *ulp_ops;
3293
Michael Chan7fc1ece2009-08-14 15:49:47 +00003294 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003295 ulp_ops = cnic_ulp_tbl_prot(i);
Michael Chan7fc1ece2009-08-14 15:49:47 +00003296 if (!ulp_ops || !ulp_ops->cnic_exit) {
3297 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003298 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003299 }
3300 ulp_get(ulp_ops);
3301 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003302
3303 if (test_and_clear_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3304 ulp_ops->cnic_exit(dev);
3305
Michael Chan7fc1ece2009-08-14 15:49:47 +00003306 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003307 }
Michael Chana4636962009-06-08 18:14:43 -07003308}
3309
3310static int cnic_cm_offload_pg(struct cnic_sock *csk)
3311{
3312 struct cnic_dev *dev = csk->dev;
3313 struct l4_kwq_offload_pg *l4kwqe;
3314 struct kwqe *wqes[1];
3315
3316 l4kwqe = (struct l4_kwq_offload_pg *) &csk->kwqe1;
3317 memset(l4kwqe, 0, sizeof(*l4kwqe));
3318 wqes[0] = (struct kwqe *) l4kwqe;
3319
3320 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_OFFLOAD_PG;
3321 l4kwqe->flags =
3322 L4_LAYER_CODE << L4_KWQ_OFFLOAD_PG_LAYER_CODE_SHIFT;
3323 l4kwqe->l2hdr_nbytes = ETH_HLEN;
3324
3325 l4kwqe->da0 = csk->ha[0];
3326 l4kwqe->da1 = csk->ha[1];
3327 l4kwqe->da2 = csk->ha[2];
3328 l4kwqe->da3 = csk->ha[3];
3329 l4kwqe->da4 = csk->ha[4];
3330 l4kwqe->da5 = csk->ha[5];
3331
3332 l4kwqe->sa0 = dev->mac_addr[0];
3333 l4kwqe->sa1 = dev->mac_addr[1];
3334 l4kwqe->sa2 = dev->mac_addr[2];
3335 l4kwqe->sa3 = dev->mac_addr[3];
3336 l4kwqe->sa4 = dev->mac_addr[4];
3337 l4kwqe->sa5 = dev->mac_addr[5];
3338
3339 l4kwqe->etype = ETH_P_IP;
Eddie Waia9736c02010-02-24 14:42:04 +00003340 l4kwqe->ipid_start = DEF_IPID_START;
Michael Chana4636962009-06-08 18:14:43 -07003341 l4kwqe->host_opaque = csk->l5_cid;
3342
3343 if (csk->vlan_id) {
3344 l4kwqe->pg_flags |= L4_KWQ_OFFLOAD_PG_VLAN_TAGGING;
3345 l4kwqe->vlan_tag = csk->vlan_id;
3346 l4kwqe->l2hdr_nbytes += 4;
3347 }
3348
3349 return dev->submit_kwqes(dev, wqes, 1);
3350}
3351
3352static int cnic_cm_update_pg(struct cnic_sock *csk)
3353{
3354 struct cnic_dev *dev = csk->dev;
3355 struct l4_kwq_update_pg *l4kwqe;
3356 struct kwqe *wqes[1];
3357
3358 l4kwqe = (struct l4_kwq_update_pg *) &csk->kwqe1;
3359 memset(l4kwqe, 0, sizeof(*l4kwqe));
3360 wqes[0] = (struct kwqe *) l4kwqe;
3361
3362 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPDATE_PG;
3363 l4kwqe->flags =
3364 L4_LAYER_CODE << L4_KWQ_UPDATE_PG_LAYER_CODE_SHIFT;
3365 l4kwqe->pg_cid = csk->pg_cid;
3366
3367 l4kwqe->da0 = csk->ha[0];
3368 l4kwqe->da1 = csk->ha[1];
3369 l4kwqe->da2 = csk->ha[2];
3370 l4kwqe->da3 = csk->ha[3];
3371 l4kwqe->da4 = csk->ha[4];
3372 l4kwqe->da5 = csk->ha[5];
3373
3374 l4kwqe->pg_host_opaque = csk->l5_cid;
3375 l4kwqe->pg_valids = L4_KWQ_UPDATE_PG_VALIDS_DA;
3376
3377 return dev->submit_kwqes(dev, wqes, 1);
3378}
3379
3380static int cnic_cm_upload_pg(struct cnic_sock *csk)
3381{
3382 struct cnic_dev *dev = csk->dev;
3383 struct l4_kwq_upload *l4kwqe;
3384 struct kwqe *wqes[1];
3385
3386 l4kwqe = (struct l4_kwq_upload *) &csk->kwqe1;
3387 memset(l4kwqe, 0, sizeof(*l4kwqe));
3388 wqes[0] = (struct kwqe *) l4kwqe;
3389
3390 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPLOAD_PG;
3391 l4kwqe->flags =
3392 L4_LAYER_CODE << L4_KWQ_UPLOAD_LAYER_CODE_SHIFT;
3393 l4kwqe->cid = csk->pg_cid;
3394
3395 return dev->submit_kwqes(dev, wqes, 1);
3396}
3397
3398static int cnic_cm_conn_req(struct cnic_sock *csk)
3399{
3400 struct cnic_dev *dev = csk->dev;
3401 struct l4_kwq_connect_req1 *l4kwqe1;
3402 struct l4_kwq_connect_req2 *l4kwqe2;
3403 struct l4_kwq_connect_req3 *l4kwqe3;
3404 struct kwqe *wqes[3];
3405 u8 tcp_flags = 0;
3406 int num_wqes = 2;
3407
3408 l4kwqe1 = (struct l4_kwq_connect_req1 *) &csk->kwqe1;
3409 l4kwqe2 = (struct l4_kwq_connect_req2 *) &csk->kwqe2;
3410 l4kwqe3 = (struct l4_kwq_connect_req3 *) &csk->kwqe3;
3411 memset(l4kwqe1, 0, sizeof(*l4kwqe1));
3412 memset(l4kwqe2, 0, sizeof(*l4kwqe2));
3413 memset(l4kwqe3, 0, sizeof(*l4kwqe3));
3414
3415 l4kwqe3->op_code = L4_KWQE_OPCODE_VALUE_CONNECT3;
3416 l4kwqe3->flags =
3417 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ3_LAYER_CODE_SHIFT;
3418 l4kwqe3->ka_timeout = csk->ka_timeout;
3419 l4kwqe3->ka_interval = csk->ka_interval;
3420 l4kwqe3->ka_max_probe_count = csk->ka_max_probe_count;
3421 l4kwqe3->tos = csk->tos;
3422 l4kwqe3->ttl = csk->ttl;
3423 l4kwqe3->snd_seq_scale = csk->snd_seq_scale;
3424 l4kwqe3->pmtu = csk->mtu;
3425 l4kwqe3->rcv_buf = csk->rcv_buf;
3426 l4kwqe3->snd_buf = csk->snd_buf;
3427 l4kwqe3->seed = csk->seed;
3428
3429 wqes[0] = (struct kwqe *) l4kwqe1;
3430 if (test_bit(SK_F_IPV6, &csk->flags)) {
3431 wqes[1] = (struct kwqe *) l4kwqe2;
3432 wqes[2] = (struct kwqe *) l4kwqe3;
3433 num_wqes = 3;
3434
3435 l4kwqe1->conn_flags = L4_KWQ_CONNECT_REQ1_IP_V6;
3436 l4kwqe2->op_code = L4_KWQE_OPCODE_VALUE_CONNECT2;
3437 l4kwqe2->flags =
3438 L4_KWQ_CONNECT_REQ2_LINKED_WITH_NEXT |
3439 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ2_LAYER_CODE_SHIFT;
3440 l4kwqe2->src_ip_v6_2 = be32_to_cpu(csk->src_ip[1]);
3441 l4kwqe2->src_ip_v6_3 = be32_to_cpu(csk->src_ip[2]);
3442 l4kwqe2->src_ip_v6_4 = be32_to_cpu(csk->src_ip[3]);
3443 l4kwqe2->dst_ip_v6_2 = be32_to_cpu(csk->dst_ip[1]);
3444 l4kwqe2->dst_ip_v6_3 = be32_to_cpu(csk->dst_ip[2]);
3445 l4kwqe2->dst_ip_v6_4 = be32_to_cpu(csk->dst_ip[3]);
3446 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct ipv6hdr) -
3447 sizeof(struct tcphdr);
3448 } else {
3449 wqes[1] = (struct kwqe *) l4kwqe3;
3450 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct iphdr) -
3451 sizeof(struct tcphdr);
3452 }
3453
3454 l4kwqe1->op_code = L4_KWQE_OPCODE_VALUE_CONNECT1;
3455 l4kwqe1->flags =
3456 (L4_LAYER_CODE << L4_KWQ_CONNECT_REQ1_LAYER_CODE_SHIFT) |
3457 L4_KWQ_CONNECT_REQ3_LINKED_WITH_NEXT;
3458 l4kwqe1->cid = csk->cid;
3459 l4kwqe1->pg_cid = csk->pg_cid;
3460 l4kwqe1->src_ip = be32_to_cpu(csk->src_ip[0]);
3461 l4kwqe1->dst_ip = be32_to_cpu(csk->dst_ip[0]);
3462 l4kwqe1->src_port = be16_to_cpu(csk->src_port);
3463 l4kwqe1->dst_port = be16_to_cpu(csk->dst_port);
3464 if (csk->tcp_flags & SK_TCP_NO_DELAY_ACK)
3465 tcp_flags |= L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK;
3466 if (csk->tcp_flags & SK_TCP_KEEP_ALIVE)
3467 tcp_flags |= L4_KWQ_CONNECT_REQ1_KEEP_ALIVE;
3468 if (csk->tcp_flags & SK_TCP_NAGLE)
3469 tcp_flags |= L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE;
3470 if (csk->tcp_flags & SK_TCP_TIMESTAMP)
3471 tcp_flags |= L4_KWQ_CONNECT_REQ1_TIME_STAMP;
3472 if (csk->tcp_flags & SK_TCP_SACK)
3473 tcp_flags |= L4_KWQ_CONNECT_REQ1_SACK;
3474 if (csk->tcp_flags & SK_TCP_SEG_SCALING)
3475 tcp_flags |= L4_KWQ_CONNECT_REQ1_SEG_SCALING;
3476
3477 l4kwqe1->tcp_flags = tcp_flags;
3478
3479 return dev->submit_kwqes(dev, wqes, num_wqes);
3480}
3481
3482static int cnic_cm_close_req(struct cnic_sock *csk)
3483{
3484 struct cnic_dev *dev = csk->dev;
3485 struct l4_kwq_close_req *l4kwqe;
3486 struct kwqe *wqes[1];
3487
3488 l4kwqe = (struct l4_kwq_close_req *) &csk->kwqe2;
3489 memset(l4kwqe, 0, sizeof(*l4kwqe));
3490 wqes[0] = (struct kwqe *) l4kwqe;
3491
3492 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_CLOSE;
3493 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_CLOSE_REQ_LAYER_CODE_SHIFT;
3494 l4kwqe->cid = csk->cid;
3495
3496 return dev->submit_kwqes(dev, wqes, 1);
3497}
3498
3499static int cnic_cm_abort_req(struct cnic_sock *csk)
3500{
3501 struct cnic_dev *dev = csk->dev;
3502 struct l4_kwq_reset_req *l4kwqe;
3503 struct kwqe *wqes[1];
3504
3505 l4kwqe = (struct l4_kwq_reset_req *) &csk->kwqe2;
3506 memset(l4kwqe, 0, sizeof(*l4kwqe));
3507 wqes[0] = (struct kwqe *) l4kwqe;
3508
3509 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_RESET;
3510 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_RESET_REQ_LAYER_CODE_SHIFT;
3511 l4kwqe->cid = csk->cid;
3512
3513 return dev->submit_kwqes(dev, wqes, 1);
3514}
3515
3516static int cnic_cm_create(struct cnic_dev *dev, int ulp_type, u32 cid,
3517 u32 l5_cid, struct cnic_sock **csk, void *context)
3518{
3519 struct cnic_local *cp = dev->cnic_priv;
3520 struct cnic_sock *csk1;
3521
3522 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3523 return -EINVAL;
3524
Michael Chanfdf24082010-10-13 14:06:47 +00003525 if (cp->ctx_tbl) {
3526 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3527
3528 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
3529 return -EAGAIN;
3530 }
3531
Michael Chana4636962009-06-08 18:14:43 -07003532 csk1 = &cp->csk_tbl[l5_cid];
3533 if (atomic_read(&csk1->ref_count))
3534 return -EAGAIN;
3535
3536 if (test_and_set_bit(SK_F_INUSE, &csk1->flags))
3537 return -EBUSY;
3538
3539 csk1->dev = dev;
3540 csk1->cid = cid;
3541 csk1->l5_cid = l5_cid;
3542 csk1->ulp_type = ulp_type;
3543 csk1->context = context;
3544
3545 csk1->ka_timeout = DEF_KA_TIMEOUT;
3546 csk1->ka_interval = DEF_KA_INTERVAL;
3547 csk1->ka_max_probe_count = DEF_KA_MAX_PROBE_COUNT;
3548 csk1->tos = DEF_TOS;
3549 csk1->ttl = DEF_TTL;
3550 csk1->snd_seq_scale = DEF_SND_SEQ_SCALE;
3551 csk1->rcv_buf = DEF_RCV_BUF;
3552 csk1->snd_buf = DEF_SND_BUF;
3553 csk1->seed = DEF_SEED;
3554
3555 *csk = csk1;
3556 return 0;
3557}
3558
3559static void cnic_cm_cleanup(struct cnic_sock *csk)
3560{
3561 if (csk->src_port) {
3562 struct cnic_dev *dev = csk->dev;
3563 struct cnic_local *cp = dev->cnic_priv;
3564
Michael Chan9b093362010-12-23 07:42:56 +00003565 cnic_free_id(&cp->csk_port_tbl, be16_to_cpu(csk->src_port));
Michael Chana4636962009-06-08 18:14:43 -07003566 csk->src_port = 0;
3567 }
3568}
3569
3570static void cnic_close_conn(struct cnic_sock *csk)
3571{
3572 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags)) {
3573 cnic_cm_upload_pg(csk);
3574 clear_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3575 }
3576 cnic_cm_cleanup(csk);
3577}
3578
3579static int cnic_cm_destroy(struct cnic_sock *csk)
3580{
3581 if (!cnic_in_use(csk))
3582 return -EINVAL;
3583
3584 csk_hold(csk);
3585 clear_bit(SK_F_INUSE, &csk->flags);
3586 smp_mb__after_clear_bit();
3587 while (atomic_read(&csk->ref_count) != 1)
3588 msleep(1);
3589 cnic_cm_cleanup(csk);
3590
3591 csk->flags = 0;
3592 csk_put(csk);
3593 return 0;
3594}
3595
3596static inline u16 cnic_get_vlan(struct net_device *dev,
3597 struct net_device **vlan_dev)
3598{
3599 if (dev->priv_flags & IFF_802_1Q_VLAN) {
3600 *vlan_dev = vlan_dev_real_dev(dev);
3601 return vlan_dev_vlan_id(dev);
3602 }
3603 *vlan_dev = dev;
3604 return 0;
3605}
3606
3607static int cnic_get_v4_route(struct sockaddr_in *dst_addr,
3608 struct dst_entry **dst)
3609{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003610#if defined(CONFIG_INET)
Michael Chana4636962009-06-08 18:14:43 -07003611 struct rtable *rt;
3612
David S. Miller78fbfd82011-03-12 00:00:52 -05003613 rt = ip_route_output(&init_net, dst_addr->sin_addr.s_addr, 0, 0, 0);
3614 if (!IS_ERR(rt)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07003615 *dst = &rt->dst;
David S. Miller78fbfd82011-03-12 00:00:52 -05003616 return 0;
3617 }
3618 return PTR_ERR(rt);
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003619#else
3620 return -ENETUNREACH;
3621#endif
Michael Chana4636962009-06-08 18:14:43 -07003622}
3623
3624static int cnic_get_v6_route(struct sockaddr_in6 *dst_addr,
3625 struct dst_entry **dst)
3626{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003627#if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
David S. Miller4c9483b2011-03-12 16:22:43 -05003628 struct flowi6 fl6;
Michael Chana4636962009-06-08 18:14:43 -07003629
David S. Miller4c9483b2011-03-12 16:22:43 -05003630 memset(&fl6, 0, sizeof(fl6));
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003631 fl6.daddr = dst_addr->sin6_addr;
David S. Miller4c9483b2011-03-12 16:22:43 -05003632 if (ipv6_addr_type(&fl6.daddr) & IPV6_ADDR_LINKLOCAL)
3633 fl6.flowi6_oif = dst_addr->sin6_scope_id;
Michael Chana4636962009-06-08 18:14:43 -07003634
David S. Miller4c9483b2011-03-12 16:22:43 -05003635 *dst = ip6_route_output(&init_net, NULL, &fl6);
RongQing.Li05417432012-02-21 22:10:50 +00003636 if ((*dst)->error) {
3637 dst_release(*dst);
3638 *dst = NULL;
3639 return -ENETUNREACH;
3640 } else
Michael Chana4636962009-06-08 18:14:43 -07003641 return 0;
3642#endif
3643
3644 return -ENETUNREACH;
3645}
3646
3647static struct cnic_dev *cnic_cm_select_dev(struct sockaddr_in *dst_addr,
3648 int ulp_type)
3649{
3650 struct cnic_dev *dev = NULL;
3651 struct dst_entry *dst;
3652 struct net_device *netdev = NULL;
3653 int err = -ENETUNREACH;
3654
3655 if (dst_addr->sin_family == AF_INET)
3656 err = cnic_get_v4_route(dst_addr, &dst);
3657 else if (dst_addr->sin_family == AF_INET6) {
3658 struct sockaddr_in6 *dst_addr6 =
3659 (struct sockaddr_in6 *) dst_addr;
3660
3661 err = cnic_get_v6_route(dst_addr6, &dst);
3662 } else
3663 return NULL;
3664
3665 if (err)
3666 return NULL;
3667
3668 if (!dst->dev)
3669 goto done;
3670
3671 cnic_get_vlan(dst->dev, &netdev);
3672
3673 dev = cnic_from_netdev(netdev);
3674
3675done:
3676 dst_release(dst);
3677 if (dev)
3678 cnic_put(dev);
3679 return dev;
3680}
3681
3682static int cnic_resolve_addr(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3683{
3684 struct cnic_dev *dev = csk->dev;
3685 struct cnic_local *cp = dev->cnic_priv;
3686
3687 return cnic_send_nlmsg(cp, ISCSI_KEVENT_PATH_REQ, csk);
3688}
3689
3690static int cnic_get_route(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3691{
3692 struct cnic_dev *dev = csk->dev;
3693 struct cnic_local *cp = dev->cnic_priv;
Michael Chanc76284a2010-02-24 14:42:07 +00003694 int is_v6, rc = 0;
3695 struct dst_entry *dst = NULL;
Michael Chana4636962009-06-08 18:14:43 -07003696 struct net_device *realdev;
Michael Chan9b093362010-12-23 07:42:56 +00003697 __be16 local_port;
3698 u32 port_id;
Michael Chana4636962009-06-08 18:14:43 -07003699
3700 if (saddr->local.v6.sin6_family == AF_INET6 &&
3701 saddr->remote.v6.sin6_family == AF_INET6)
3702 is_v6 = 1;
3703 else if (saddr->local.v4.sin_family == AF_INET &&
3704 saddr->remote.v4.sin_family == AF_INET)
3705 is_v6 = 0;
3706 else
3707 return -EINVAL;
3708
3709 clear_bit(SK_F_IPV6, &csk->flags);
3710
3711 if (is_v6) {
Michael Chana4636962009-06-08 18:14:43 -07003712 set_bit(SK_F_IPV6, &csk->flags);
Michael Chanc76284a2010-02-24 14:42:07 +00003713 cnic_get_v6_route(&saddr->remote.v6, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003714
3715 memcpy(&csk->dst_ip[0], &saddr->remote.v6.sin6_addr,
3716 sizeof(struct in6_addr));
3717 csk->dst_port = saddr->remote.v6.sin6_port;
3718 local_port = saddr->local.v6.sin6_port;
Michael Chana4636962009-06-08 18:14:43 -07003719
3720 } else {
Michael Chanc76284a2010-02-24 14:42:07 +00003721 cnic_get_v4_route(&saddr->remote.v4, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003722
3723 csk->dst_ip[0] = saddr->remote.v4.sin_addr.s_addr;
3724 csk->dst_port = saddr->remote.v4.sin_port;
3725 local_port = saddr->local.v4.sin_port;
3726 }
3727
Michael Chanc76284a2010-02-24 14:42:07 +00003728 csk->vlan_id = 0;
3729 csk->mtu = dev->netdev->mtu;
3730 if (dst && dst->dev) {
3731 u16 vlan = cnic_get_vlan(dst->dev, &realdev);
3732 if (realdev == dev->netdev) {
3733 csk->vlan_id = vlan;
3734 csk->mtu = dst_mtu(dst);
3735 }
3736 }
Michael Chana4636962009-06-08 18:14:43 -07003737
Michael Chan9b093362010-12-23 07:42:56 +00003738 port_id = be16_to_cpu(local_port);
3739 if (port_id >= CNIC_LOCAL_PORT_MIN &&
3740 port_id < CNIC_LOCAL_PORT_MAX) {
3741 if (cnic_alloc_id(&cp->csk_port_tbl, port_id))
3742 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003743 } else
Michael Chan9b093362010-12-23 07:42:56 +00003744 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003745
Michael Chan9b093362010-12-23 07:42:56 +00003746 if (!port_id) {
3747 port_id = cnic_alloc_new_id(&cp->csk_port_tbl);
3748 if (port_id == -1) {
Michael Chana4636962009-06-08 18:14:43 -07003749 rc = -ENOMEM;
3750 goto err_out;
3751 }
Michael Chan9b093362010-12-23 07:42:56 +00003752 local_port = cpu_to_be16(port_id);
Michael Chana4636962009-06-08 18:14:43 -07003753 }
3754 csk->src_port = local_port;
3755
Michael Chana4636962009-06-08 18:14:43 -07003756err_out:
3757 dst_release(dst);
3758 return rc;
3759}
3760
3761static void cnic_init_csk_state(struct cnic_sock *csk)
3762{
3763 csk->state = 0;
3764 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3765 clear_bit(SK_F_CLOSING, &csk->flags);
3766}
3767
3768static int cnic_cm_connect(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3769{
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003770 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07003771 int err = 0;
3772
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003773 if (cp->ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI)
3774 return -EOPNOTSUPP;
3775
Michael Chana4636962009-06-08 18:14:43 -07003776 if (!cnic_in_use(csk))
3777 return -EINVAL;
3778
3779 if (test_and_set_bit(SK_F_CONNECT_START, &csk->flags))
3780 return -EINVAL;
3781
3782 cnic_init_csk_state(csk);
3783
3784 err = cnic_get_route(csk, saddr);
3785 if (err)
3786 goto err_out;
3787
3788 err = cnic_resolve_addr(csk, saddr);
3789 if (!err)
3790 return 0;
3791
3792err_out:
3793 clear_bit(SK_F_CONNECT_START, &csk->flags);
3794 return err;
3795}
3796
3797static int cnic_cm_abort(struct cnic_sock *csk)
3798{
3799 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chan7b34a462010-06-15 08:57:03 +00003800 u32 opcode = L4_KCQE_OPCODE_VALUE_RESET_COMP;
Michael Chana4636962009-06-08 18:14:43 -07003801
3802 if (!cnic_in_use(csk))
3803 return -EINVAL;
3804
3805 if (cnic_abort_prep(csk))
3806 return cnic_cm_abort_req(csk);
3807
3808 /* Getting here means that we haven't started connect, or
3809 * connect was not successful.
3810 */
3811
Michael Chana4636962009-06-08 18:14:43 -07003812 cp->close_conn(csk, opcode);
Michael Chan7b34a462010-06-15 08:57:03 +00003813 if (csk->state != opcode)
3814 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003815
3816 return 0;
3817}
3818
3819static int cnic_cm_close(struct cnic_sock *csk)
3820{
3821 if (!cnic_in_use(csk))
3822 return -EINVAL;
3823
3824 if (cnic_close_prep(csk)) {
3825 csk->state = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
3826 return cnic_cm_close_req(csk);
Michael Chaned99daa52010-06-15 08:57:00 +00003827 } else {
3828 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003829 }
3830 return 0;
3831}
3832
3833static void cnic_cm_upcall(struct cnic_local *cp, struct cnic_sock *csk,
3834 u8 opcode)
3835{
3836 struct cnic_ulp_ops *ulp_ops;
3837 int ulp_type = csk->ulp_type;
3838
3839 rcu_read_lock();
3840 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
3841 if (ulp_ops) {
3842 if (opcode == L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE)
3843 ulp_ops->cm_connect_complete(csk);
3844 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3845 ulp_ops->cm_close_complete(csk);
3846 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED)
3847 ulp_ops->cm_remote_abort(csk);
3848 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_COMP)
3849 ulp_ops->cm_abort_complete(csk);
3850 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED)
3851 ulp_ops->cm_remote_close(csk);
3852 }
3853 rcu_read_unlock();
3854}
3855
3856static int cnic_cm_set_pg(struct cnic_sock *csk)
3857{
3858 if (cnic_offld_prep(csk)) {
3859 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3860 cnic_cm_update_pg(csk);
3861 else
3862 cnic_cm_offload_pg(csk);
3863 }
3864 return 0;
3865}
3866
3867static void cnic_cm_process_offld_pg(struct cnic_dev *dev, struct l4_kcq *kcqe)
3868{
3869 struct cnic_local *cp = dev->cnic_priv;
3870 u32 l5_cid = kcqe->pg_host_opaque;
3871 u8 opcode = kcqe->op_code;
3872 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
3873
3874 csk_hold(csk);
3875 if (!cnic_in_use(csk))
3876 goto done;
3877
3878 if (opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3879 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3880 goto done;
3881 }
Eddie Waia9736c02010-02-24 14:42:04 +00003882 /* Possible PG kcqe status: SUCCESS, OFFLOADED_PG, or CTX_ALLOC_FAIL */
3883 if (kcqe->status == L4_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAIL) {
3884 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3885 cnic_cm_upcall(cp, csk,
3886 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3887 goto done;
3888 }
3889
Michael Chana4636962009-06-08 18:14:43 -07003890 csk->pg_cid = kcqe->pg_cid;
3891 set_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3892 cnic_cm_conn_req(csk);
3893
3894done:
3895 csk_put(csk);
3896}
3897
Michael Chane1928c82010-12-23 07:43:04 +00003898static void cnic_process_fcoe_term_conn(struct cnic_dev *dev, struct kcqe *kcqe)
3899{
3900 struct cnic_local *cp = dev->cnic_priv;
3901 struct fcoe_kcqe *fc_kcqe = (struct fcoe_kcqe *) kcqe;
3902 u32 l5_cid = fc_kcqe->fcoe_conn_id + BNX2X_FCOE_L5_CID_BASE;
3903 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3904
3905 ctx->timestamp = jiffies;
3906 ctx->wait_cond = 1;
3907 wake_up(&ctx->waitq);
3908}
3909
Michael Chana4636962009-06-08 18:14:43 -07003910static void cnic_cm_process_kcqe(struct cnic_dev *dev, struct kcqe *kcqe)
3911{
3912 struct cnic_local *cp = dev->cnic_priv;
3913 struct l4_kcq *l4kcqe = (struct l4_kcq *) kcqe;
3914 u8 opcode = l4kcqe->op_code;
3915 u32 l5_cid;
3916 struct cnic_sock *csk;
3917
Michael Chane1928c82010-12-23 07:43:04 +00003918 if (opcode == FCOE_RAMROD_CMD_ID_TERMINATE_CONN) {
3919 cnic_process_fcoe_term_conn(dev, kcqe);
3920 return;
3921 }
Michael Chana4636962009-06-08 18:14:43 -07003922 if (opcode == L4_KCQE_OPCODE_VALUE_OFFLOAD_PG ||
3923 opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3924 cnic_cm_process_offld_pg(dev, l4kcqe);
3925 return;
3926 }
3927
3928 l5_cid = l4kcqe->conn_id;
3929 if (opcode & 0x80)
3930 l5_cid = l4kcqe->cid;
3931 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3932 return;
3933
3934 csk = &cp->csk_tbl[l5_cid];
3935 csk_hold(csk);
3936
3937 if (!cnic_in_use(csk)) {
3938 csk_put(csk);
3939 return;
3940 }
3941
3942 switch (opcode) {
Eddie Waia9736c02010-02-24 14:42:04 +00003943 case L5CM_RAMROD_CMD_ID_TCP_CONNECT:
3944 if (l4kcqe->status != 0) {
3945 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3946 cnic_cm_upcall(cp, csk,
3947 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3948 }
3949 break;
Michael Chana4636962009-06-08 18:14:43 -07003950 case L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE:
3951 if (l4kcqe->status == 0)
3952 set_bit(SK_F_OFFLD_COMPLETE, &csk->flags);
Michael Chan8ec3e702012-03-21 15:38:34 +00003953 else if (l4kcqe->status ==
3954 L4_KCQE_COMPLETION_STATUS_PARITY_ERROR)
Jeffrey Huang0cb1f4b2012-02-08 17:33:56 +00003955 set_bit(SK_F_HW_ERR, &csk->flags);
Michael Chana4636962009-06-08 18:14:43 -07003956
3957 smp_mb__before_clear_bit();
3958 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3959 cnic_cm_upcall(cp, csk, opcode);
3960 break;
3961
Eddie Wai7bc910f2012-06-27 15:08:22 +00003962 case L5CM_RAMROD_CMD_ID_CLOSE:
3963 if (l4kcqe->status != 0) {
3964 netdev_warn(dev->netdev, "RAMROD CLOSE compl with "
3965 "status 0x%x\n", l4kcqe->status);
3966 opcode = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
3967 /* Fall through */
3968 } else {
3969 break;
3970 }
Michael Chana4636962009-06-08 18:14:43 -07003971 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
Michael Chana4636962009-06-08 18:14:43 -07003972 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3973 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan71034ba2009-10-10 13:46:59 +00003974 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3975 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
Michael Chan8ec3e702012-03-21 15:38:34 +00003976 if (l4kcqe->status == L4_KCQE_COMPLETION_STATUS_PARITY_ERROR)
Michael Chan23021c22012-01-04 12:12:28 +00003977 set_bit(SK_F_HW_ERR, &csk->flags);
3978
Michael Chana4636962009-06-08 18:14:43 -07003979 cp->close_conn(csk, opcode);
3980 break;
3981
3982 case L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED:
Michael Chan101c40c2011-06-08 19:29:33 +00003983 /* after we already sent CLOSE_REQ */
3984 if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) &&
3985 !test_bit(SK_F_OFFLD_COMPLETE, &csk->flags) &&
3986 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3987 cp->close_conn(csk, L4_KCQE_OPCODE_VALUE_RESET_COMP);
3988 else
3989 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07003990 break;
3991 }
3992 csk_put(csk);
3993}
3994
3995static void cnic_cm_indicate_kcqe(void *data, struct kcqe *kcqe[], u32 num)
3996{
3997 struct cnic_dev *dev = data;
3998 int i;
3999
4000 for (i = 0; i < num; i++)
4001 cnic_cm_process_kcqe(dev, kcqe[i]);
4002}
4003
4004static struct cnic_ulp_ops cm_ulp_ops = {
4005 .indicate_kcqes = cnic_cm_indicate_kcqe,
4006};
4007
4008static void cnic_cm_free_mem(struct cnic_dev *dev)
4009{
4010 struct cnic_local *cp = dev->cnic_priv;
4011
4012 kfree(cp->csk_tbl);
4013 cp->csk_tbl = NULL;
4014 cnic_free_id_tbl(&cp->csk_port_tbl);
4015}
4016
4017static int cnic_cm_alloc_mem(struct cnic_dev *dev)
4018{
4019 struct cnic_local *cp = dev->cnic_priv;
Eddie Wai11f23aa2011-06-08 19:29:34 +00004020 u32 port_id;
Michael Chana4636962009-06-08 18:14:43 -07004021
4022 cp->csk_tbl = kzalloc(sizeof(struct cnic_sock) * MAX_CM_SK_TBL_SZ,
4023 GFP_KERNEL);
4024 if (!cp->csk_tbl)
4025 return -ENOMEM;
4026
Michael Chan973e5742011-07-13 17:24:17 +00004027 port_id = random32();
Eddie Wai11f23aa2011-06-08 19:29:34 +00004028 port_id %= CNIC_LOCAL_PORT_RANGE;
Michael Chana4636962009-06-08 18:14:43 -07004029 if (cnic_init_id_tbl(&cp->csk_port_tbl, CNIC_LOCAL_PORT_RANGE,
Eddie Wai11f23aa2011-06-08 19:29:34 +00004030 CNIC_LOCAL_PORT_MIN, port_id)) {
Michael Chana4636962009-06-08 18:14:43 -07004031 cnic_cm_free_mem(dev);
4032 return -ENOMEM;
4033 }
4034 return 0;
4035}
4036
4037static int cnic_ready_to_close(struct cnic_sock *csk, u32 opcode)
4038{
Michael Chan943189f2010-06-15 08:57:02 +00004039 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
4040 /* Unsolicited RESET_COMP or RESET_RECEIVED */
4041 opcode = L4_KCQE_OPCODE_VALUE_RESET_RECEIVED;
4042 csk->state = opcode;
Michael Chana1e621b2010-06-15 08:57:01 +00004043 }
Michael Chan943189f2010-06-15 08:57:02 +00004044
4045 /* 1. If event opcode matches the expected event in csk->state
Michael Chan101c40c2011-06-08 19:29:33 +00004046 * 2. If the expected event is CLOSE_COMP or RESET_COMP, we accept any
4047 * event
Michael Chan7b34a462010-06-15 08:57:03 +00004048 * 3. If the expected event is 0, meaning the connection was never
4049 * never established, we accept the opcode from cm_abort.
Michael Chan943189f2010-06-15 08:57:02 +00004050 */
Michael Chan7b34a462010-06-15 08:57:03 +00004051 if (opcode == csk->state || csk->state == 0 ||
Michael Chan101c40c2011-06-08 19:29:33 +00004052 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP ||
4053 csk->state == L4_KCQE_OPCODE_VALUE_RESET_COMP) {
Michael Chan7b34a462010-06-15 08:57:03 +00004054 if (!test_and_set_bit(SK_F_CLOSING, &csk->flags)) {
4055 if (csk->state == 0)
4056 csk->state = opcode;
Michael Chana4636962009-06-08 18:14:43 -07004057 return 1;
Michael Chan7b34a462010-06-15 08:57:03 +00004058 }
Michael Chana4636962009-06-08 18:14:43 -07004059 }
4060 return 0;
4061}
4062
4063static void cnic_close_bnx2_conn(struct cnic_sock *csk, u32 opcode)
4064{
4065 struct cnic_dev *dev = csk->dev;
4066 struct cnic_local *cp = dev->cnic_priv;
4067
Michael Chana1e621b2010-06-15 08:57:01 +00004068 if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED) {
4069 cnic_cm_upcall(cp, csk, opcode);
4070 return;
4071 }
4072
Michael Chana4636962009-06-08 18:14:43 -07004073 clear_bit(SK_F_CONNECT_START, &csk->flags);
Eddie Wai66883e92010-02-24 14:42:05 +00004074 cnic_close_conn(csk);
Michael Chan7b34a462010-06-15 08:57:03 +00004075 csk->state = opcode;
Eddie Wai66883e92010-02-24 14:42:05 +00004076 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07004077}
4078
4079static void cnic_cm_stop_bnx2_hw(struct cnic_dev *dev)
4080{
4081}
4082
4083static int cnic_cm_init_bnx2_hw(struct cnic_dev *dev)
4084{
4085 u32 seed;
4086
Michael Chan973e5742011-07-13 17:24:17 +00004087 seed = random32();
Michael Chana4636962009-06-08 18:14:43 -07004088 cnic_ctx_wr(dev, 45, 0, seed);
4089 return 0;
4090}
4091
Michael Chan71034ba2009-10-10 13:46:59 +00004092static void cnic_close_bnx2x_conn(struct cnic_sock *csk, u32 opcode)
4093{
4094 struct cnic_dev *dev = csk->dev;
4095 struct cnic_local *cp = dev->cnic_priv;
4096 struct cnic_context *ctx = &cp->ctx_tbl[csk->l5_cid];
4097 union l5cm_specific_data l5_data;
4098 u32 cmd = 0;
4099 int close_complete = 0;
4100
4101 switch (opcode) {
4102 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
4103 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
4104 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan7b34a462010-06-15 08:57:03 +00004105 if (cnic_ready_to_close(csk, opcode)) {
Michael Chan23021c22012-01-04 12:12:28 +00004106 if (test_bit(SK_F_HW_ERR, &csk->flags))
4107 close_complete = 1;
4108 else if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
Michael Chan7b34a462010-06-15 08:57:03 +00004109 cmd = L5CM_RAMROD_CMD_ID_SEARCHER_DELETE;
4110 else
4111 close_complete = 1;
4112 }
Michael Chan71034ba2009-10-10 13:46:59 +00004113 break;
4114 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
4115 cmd = L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD;
4116 break;
4117 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
4118 close_complete = 1;
4119 break;
4120 }
4121 if (cmd) {
4122 memset(&l5_data, 0, sizeof(l5_data));
4123
4124 cnic_submit_kwqe_16(dev, cmd, csk->cid, ISCSI_CONNECTION_TYPE,
4125 &l5_data);
4126 } else if (close_complete) {
4127 ctx->timestamp = jiffies;
4128 cnic_close_conn(csk);
4129 cnic_cm_upcall(cp, csk, csk->state);
4130 }
4131}
4132
4133static void cnic_cm_stop_bnx2x_hw(struct cnic_dev *dev)
4134{
Michael Chanfdf24082010-10-13 14:06:47 +00004135 struct cnic_local *cp = dev->cnic_priv;
Michael Chanfdf24082010-10-13 14:06:47 +00004136
4137 if (!cp->ctx_tbl)
4138 return;
4139
4140 if (!netif_running(dev->netdev))
4141 return;
4142
Michael Chan74e49bb2011-07-20 14:55:23 +00004143 cnic_bnx2x_delete_wait(dev, 0);
Michael Chanfdf24082010-10-13 14:06:47 +00004144
4145 cancel_delayed_work(&cp->delete_task);
4146 flush_workqueue(cnic_wq);
4147
4148 if (atomic_read(&cp->iscsi_conn) != 0)
4149 netdev_warn(dev->netdev, "%d iSCSI connections not destroyed\n",
4150 atomic_read(&cp->iscsi_conn));
Michael Chan71034ba2009-10-10 13:46:59 +00004151}
4152
4153static int cnic_cm_init_bnx2x_hw(struct cnic_dev *dev)
4154{
4155 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00004156 u32 pfid = cp->pfid;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004157 u32 port = CNIC_PORT(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00004158
4159 cnic_init_bnx2x_mac(dev);
4160 cnic_bnx2x_set_tcp_timestamp(dev, 1);
4161
4162 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004163 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(pfid), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004164
4165 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004166 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_ENABLED_OFFSET(port), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00004167 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004168 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_MAX_COUNT_OFFSET(port),
Michael Chan71034ba2009-10-10 13:46:59 +00004169 DEF_MAX_DA_COUNT);
4170
4171 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004172 XSTORM_ISCSI_TCP_VARS_TTL_OFFSET(pfid), DEF_TTL);
Michael Chan71034ba2009-10-10 13:46:59 +00004173 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004174 XSTORM_ISCSI_TCP_VARS_TOS_OFFSET(pfid), DEF_TOS);
Michael Chan71034ba2009-10-10 13:46:59 +00004175 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004176 XSTORM_ISCSI_TCP_VARS_ADV_WND_SCL_OFFSET(pfid), 2);
Michael Chan71034ba2009-10-10 13:46:59 +00004177 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004178 XSTORM_TCP_TX_SWS_TIMER_VAL_OFFSET(pfid), DEF_SWS_TIMER);
Michael Chan71034ba2009-10-10 13:46:59 +00004179
Michael Chan14203982010-10-06 03:16:06 +00004180 CNIC_WR(dev, BAR_TSTRORM_INTMEM + TSTORM_TCP_MAX_CWND_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00004181 DEF_MAX_CWND);
4182 return 0;
4183}
4184
Michael Chanfdf24082010-10-13 14:06:47 +00004185static void cnic_delete_task(struct work_struct *work)
4186{
4187 struct cnic_local *cp;
4188 struct cnic_dev *dev;
4189 u32 i;
4190 int need_resched = 0;
4191
4192 cp = container_of(work, struct cnic_local, delete_task.work);
4193 dev = cp->dev;
4194
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07004195 if (test_and_clear_bit(CNIC_LCL_FL_STOP_ISCSI, &cp->cnic_local_flags)) {
4196 struct drv_ctl_info info;
4197
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07004198 cnic_ulp_stop_one(cp, CNIC_ULP_ISCSI);
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07004199
4200 info.cmd = DRV_CTL_ISCSI_STOPPED_CMD;
4201 cp->ethdev->drv_ctl(dev->netdev, &info);
4202 }
4203
Michael Chanfdf24082010-10-13 14:06:47 +00004204 for (i = 0; i < cp->max_cid_space; i++) {
4205 struct cnic_context *ctx = &cp->ctx_tbl[i];
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004206 int err;
Michael Chanfdf24082010-10-13 14:06:47 +00004207
4208 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags) ||
4209 !test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
4210 continue;
4211
4212 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
4213 need_resched = 1;
4214 continue;
4215 }
4216
4217 if (!test_and_clear_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
4218 continue;
4219
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004220 err = cnic_bnx2x_destroy_ramrod(dev, i);
Michael Chanfdf24082010-10-13 14:06:47 +00004221
4222 cnic_free_bnx2x_conn_resc(dev, i);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004223 if (!err) {
4224 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI)
4225 atomic_dec(&cp->iscsi_conn);
Michael Chanfdf24082010-10-13 14:06:47 +00004226
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004227 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
4228 }
Michael Chanfdf24082010-10-13 14:06:47 +00004229 }
4230
4231 if (need_resched)
4232 queue_delayed_work(cnic_wq, &cp->delete_task,
4233 msecs_to_jiffies(10));
4234
4235}
4236
Michael Chana4636962009-06-08 18:14:43 -07004237static int cnic_cm_open(struct cnic_dev *dev)
4238{
4239 struct cnic_local *cp = dev->cnic_priv;
4240 int err;
4241
4242 err = cnic_cm_alloc_mem(dev);
4243 if (err)
4244 return err;
4245
4246 err = cp->start_cm(dev);
4247
4248 if (err)
4249 goto err_out;
4250
Michael Chanfdf24082010-10-13 14:06:47 +00004251 INIT_DELAYED_WORK(&cp->delete_task, cnic_delete_task);
4252
Michael Chana4636962009-06-08 18:14:43 -07004253 dev->cm_create = cnic_cm_create;
4254 dev->cm_destroy = cnic_cm_destroy;
4255 dev->cm_connect = cnic_cm_connect;
4256 dev->cm_abort = cnic_cm_abort;
4257 dev->cm_close = cnic_cm_close;
4258 dev->cm_select_dev = cnic_cm_select_dev;
4259
4260 cp->ulp_handle[CNIC_ULP_L4] = dev;
4261 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], &cm_ulp_ops);
4262 return 0;
4263
4264err_out:
4265 cnic_cm_free_mem(dev);
4266 return err;
4267}
4268
4269static int cnic_cm_shutdown(struct cnic_dev *dev)
4270{
4271 struct cnic_local *cp = dev->cnic_priv;
4272 int i;
4273
Michael Chana4636962009-06-08 18:14:43 -07004274 if (!cp->csk_tbl)
4275 return 0;
4276
4277 for (i = 0; i < MAX_CM_SK_TBL_SZ; i++) {
4278 struct cnic_sock *csk = &cp->csk_tbl[i];
4279
4280 clear_bit(SK_F_INUSE, &csk->flags);
4281 cnic_cm_cleanup(csk);
4282 }
4283 cnic_cm_free_mem(dev);
4284
4285 return 0;
4286}
4287
4288static void cnic_init_context(struct cnic_dev *dev, u32 cid)
4289{
Michael Chana4636962009-06-08 18:14:43 -07004290 u32 cid_addr;
4291 int i;
4292
Michael Chana4636962009-06-08 18:14:43 -07004293 cid_addr = GET_CID_ADDR(cid);
4294
4295 for (i = 0; i < CTX_SIZE; i += 4)
4296 cnic_ctx_wr(dev, cid_addr, i, 0);
4297}
4298
4299static int cnic_setup_5709_context(struct cnic_dev *dev, int valid)
4300{
4301 struct cnic_local *cp = dev->cnic_priv;
4302 int ret = 0, i;
4303 u32 valid_bit = valid ? BNX2_CTX_HOST_PAGE_TBL_DATA0_VALID : 0;
4304
4305 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4306 return 0;
4307
4308 for (i = 0; i < cp->ctx_blks; i++) {
4309 int j;
4310 u32 idx = cp->ctx_arr[i].cid / cp->cids_per_blk;
4311 u32 val;
4312
4313 memset(cp->ctx_arr[i].ctx, 0, BCM_PAGE_SIZE);
4314
4315 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA0,
4316 (cp->ctx_arr[i].mapping & 0xffffffff) | valid_bit);
4317 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA1,
4318 (u64) cp->ctx_arr[i].mapping >> 32);
4319 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL, idx |
4320 BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
4321 for (j = 0; j < 10; j++) {
4322
4323 val = CNIC_RD(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL);
4324 if (!(val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ))
4325 break;
4326 udelay(5);
4327 }
4328 if (val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) {
4329 ret = -EBUSY;
4330 break;
4331 }
4332 }
4333 return ret;
4334}
4335
4336static void cnic_free_irq(struct cnic_dev *dev)
4337{
4338 struct cnic_local *cp = dev->cnic_priv;
4339 struct cnic_eth_dev *ethdev = cp->ethdev;
4340
4341 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4342 cp->disable_int_sync(dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004343 tasklet_kill(&cp->cnic_irq_task);
Michael Chana4636962009-06-08 18:14:43 -07004344 free_irq(ethdev->irq_arr[0].vector, dev);
4345 }
4346}
4347
Michael Chan6e0dc642010-10-13 14:06:44 +00004348static int cnic_request_irq(struct cnic_dev *dev)
4349{
4350 struct cnic_local *cp = dev->cnic_priv;
4351 struct cnic_eth_dev *ethdev = cp->ethdev;
4352 int err;
4353
4354 err = request_irq(ethdev->irq_arr[0].vector, cnic_irq, 0, "cnic", dev);
4355 if (err)
4356 tasklet_disable(&cp->cnic_irq_task);
4357
4358 return err;
4359}
4360
Michael Chana4636962009-06-08 18:14:43 -07004361static int cnic_init_bnx2_irq(struct cnic_dev *dev)
4362{
4363 struct cnic_local *cp = dev->cnic_priv;
4364 struct cnic_eth_dev *ethdev = cp->ethdev;
4365
4366 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4367 int err, i = 0;
4368 int sblk_num = cp->status_blk_num;
4369 u32 base = ((sblk_num - 1) * BNX2_HC_SB_CONFIG_SIZE) +
4370 BNX2_HC_SB_CONFIG_1;
4371
4372 CNIC_WR(dev, base, BNX2_HC_SB_CONFIG_1_ONE_SHOT);
4373
4374 CNIC_WR(dev, base + BNX2_HC_COMP_PROD_TRIP_OFF, (2 << 16) | 8);
4375 CNIC_WR(dev, base + BNX2_HC_COM_TICKS_OFF, (64 << 16) | 220);
4376 CNIC_WR(dev, base + BNX2_HC_CMD_TICKS_OFF, (64 << 16) | 220);
4377
Michael Chana4dde3a2010-02-24 14:42:08 +00004378 cp->last_status_idx = cp->status_blk.bnx2->status_idx;
Joe Perches164165d2009-11-19 09:30:10 +00004379 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2_msix,
Michael Chana4636962009-06-08 18:14:43 -07004380 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004381 err = cnic_request_irq(dev);
4382 if (err)
Michael Chana4636962009-06-08 18:14:43 -07004383 return err;
Michael Chan6e0dc642010-10-13 14:06:44 +00004384
Michael Chana4dde3a2010-02-24 14:42:08 +00004385 while (cp->status_blk.bnx2->status_completion_producer_index &&
Michael Chana4636962009-06-08 18:14:43 -07004386 i < 10) {
4387 CNIC_WR(dev, BNX2_HC_COALESCE_NOW,
4388 1 << (11 + sblk_num));
4389 udelay(10);
4390 i++;
4391 barrier();
4392 }
Michael Chana4dde3a2010-02-24 14:42:08 +00004393 if (cp->status_blk.bnx2->status_completion_producer_index) {
Michael Chana4636962009-06-08 18:14:43 -07004394 cnic_free_irq(dev);
4395 goto failed;
4396 }
4397
4398 } else {
Michael Chana4dde3a2010-02-24 14:42:08 +00004399 struct status_block *sblk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004400 u32 hc_cmd = CNIC_RD(dev, BNX2_HC_COMMAND);
4401 int i = 0;
4402
4403 while (sblk->status_completion_producer_index && i < 10) {
4404 CNIC_WR(dev, BNX2_HC_COMMAND,
4405 hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
4406 udelay(10);
4407 i++;
4408 barrier();
4409 }
4410 if (sblk->status_completion_producer_index)
4411 goto failed;
4412
4413 }
4414 return 0;
4415
4416failed:
Joe Perchesddf79b22010-02-17 15:01:54 +00004417 netdev_err(dev->netdev, "KCQ index not resetting to 0\n");
Michael Chana4636962009-06-08 18:14:43 -07004418 return -EBUSY;
4419}
4420
4421static void cnic_enable_bnx2_int(struct cnic_dev *dev)
4422{
4423 struct cnic_local *cp = dev->cnic_priv;
4424 struct cnic_eth_dev *ethdev = cp->ethdev;
4425
4426 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4427 return;
4428
4429 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4430 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
4431}
4432
4433static void cnic_disable_bnx2_int_sync(struct cnic_dev *dev)
4434{
4435 struct cnic_local *cp = dev->cnic_priv;
4436 struct cnic_eth_dev *ethdev = cp->ethdev;
4437
4438 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4439 return;
4440
4441 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4442 BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
4443 CNIC_RD(dev, BNX2_PCICFG_INT_ACK_CMD);
4444 synchronize_irq(ethdev->irq_arr[0].vector);
4445}
4446
4447static void cnic_init_bnx2_tx_ring(struct cnic_dev *dev)
4448{
4449 struct cnic_local *cp = dev->cnic_priv;
4450 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004451 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004452 u32 cid_addr, tx_cid, sb_id;
4453 u32 val, offset0, offset1, offset2, offset3;
4454 int i;
4455 struct tx_bd *txbd;
Michael Chancd801532010-10-13 14:06:49 +00004456 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Michael Chana4dde3a2010-02-24 14:42:08 +00004457 struct status_block *s_blk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004458
4459 sb_id = cp->status_blk_num;
4460 tx_cid = 20;
Michael Chana4636962009-06-08 18:14:43 -07004461 cp->tx_cons_ptr = &s_blk->status_tx_quick_consumer_index2;
4462 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004463 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004464
4465 tx_cid = TX_TSS_CID + sb_id - 1;
Michael Chana4636962009-06-08 18:14:43 -07004466 CNIC_WR(dev, BNX2_TSCH_TSS_CFG, (sb_id << 24) |
4467 (TX_TSS_CID << 7));
4468 cp->tx_cons_ptr = &sblk->status_tx_quick_consumer_index;
4469 }
4470 cp->tx_cons = *cp->tx_cons_ptr;
4471
4472 cid_addr = GET_CID_ADDR(tx_cid);
4473 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
4474 u32 cid_addr2 = GET_CID_ADDR(tx_cid + 4) + 0x40;
4475
4476 for (i = 0; i < PHY_CTX_SIZE; i += 4)
4477 cnic_ctx_wr(dev, cid_addr2, i, 0);
4478
4479 offset0 = BNX2_L2CTX_TYPE_XI;
4480 offset1 = BNX2_L2CTX_CMD_TYPE_XI;
4481 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI_XI;
4482 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO_XI;
4483 } else {
Michael Chanb58ffb42010-05-27 16:31:41 -07004484 cnic_init_context(dev, tx_cid);
4485 cnic_init_context(dev, tx_cid + 1);
4486
Michael Chana4636962009-06-08 18:14:43 -07004487 offset0 = BNX2_L2CTX_TYPE;
4488 offset1 = BNX2_L2CTX_CMD_TYPE;
4489 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI;
4490 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO;
4491 }
4492 val = BNX2_L2CTX_TYPE_TYPE_L2 | BNX2_L2CTX_TYPE_SIZE_L2;
4493 cnic_ctx_wr(dev, cid_addr, offset0, val);
4494
4495 val = BNX2_L2CTX_CMD_TYPE_TYPE_L2 | (8 << 16);
4496 cnic_ctx_wr(dev, cid_addr, offset1, val);
4497
Joe Perches43d620c2011-06-16 19:08:06 +00004498 txbd = udev->l2_ring;
Michael Chana4636962009-06-08 18:14:43 -07004499
Michael Chancd801532010-10-13 14:06:49 +00004500 buf_map = udev->l2_buf_map;
Michael Chana4636962009-06-08 18:14:43 -07004501 for (i = 0; i < MAX_TX_DESC_CNT; i++, txbd++) {
4502 txbd->tx_bd_haddr_hi = (u64) buf_map >> 32;
4503 txbd->tx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4504 }
Michael Chancd801532010-10-13 14:06:49 +00004505 val = (u64) ring_map >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004506 cnic_ctx_wr(dev, cid_addr, offset2, val);
4507 txbd->tx_bd_haddr_hi = val;
4508
Michael Chancd801532010-10-13 14:06:49 +00004509 val = (u64) ring_map & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004510 cnic_ctx_wr(dev, cid_addr, offset3, val);
4511 txbd->tx_bd_haddr_lo = val;
4512}
4513
4514static void cnic_init_bnx2_rx_ring(struct cnic_dev *dev)
4515{
4516 struct cnic_local *cp = dev->cnic_priv;
4517 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004518 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004519 u32 cid_addr, sb_id, val, coal_reg, coal_val;
4520 int i;
4521 struct rx_bd *rxbd;
Michael Chana4dde3a2010-02-24 14:42:08 +00004522 struct status_block *s_blk = cp->status_blk.gen;
Michael Chancd801532010-10-13 14:06:49 +00004523 dma_addr_t ring_map = udev->l2_ring_map;
Michael Chana4636962009-06-08 18:14:43 -07004524
4525 sb_id = cp->status_blk_num;
4526 cnic_init_context(dev, 2);
4527 cp->rx_cons_ptr = &s_blk->status_rx_quick_consumer_index2;
4528 coal_reg = BNX2_HC_COMMAND;
4529 coal_val = CNIC_RD(dev, coal_reg);
4530 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004531 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004532
4533 cp->rx_cons_ptr = &sblk->status_rx_quick_consumer_index;
4534 coal_reg = BNX2_HC_COALESCE_NOW;
4535 coal_val = 1 << (11 + sb_id);
4536 }
4537 i = 0;
4538 while (!(*cp->rx_cons_ptr != 0) && i < 10) {
4539 CNIC_WR(dev, coal_reg, coal_val);
4540 udelay(10);
4541 i++;
4542 barrier();
4543 }
4544 cp->rx_cons = *cp->rx_cons_ptr;
4545
4546 cid_addr = GET_CID_ADDR(2);
4547 val = BNX2_L2CTX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
4548 BNX2_L2CTX_CTX_TYPE_SIZE_L2 | (0x02 << 8);
4549 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_CTX_TYPE, val);
4550
4551 if (sb_id == 0)
Michael Chand0549382009-10-28 03:41:59 -07004552 val = 2 << BNX2_L2CTX_L2_STATUSB_NUM_SHIFT;
Michael Chana4636962009-06-08 18:14:43 -07004553 else
Michael Chand0549382009-10-28 03:41:59 -07004554 val = BNX2_L2CTX_L2_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004555 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_HOST_BDIDX, val);
4556
Joe Perches43d620c2011-06-16 19:08:06 +00004557 rxbd = udev->l2_ring + BCM_PAGE_SIZE;
Michael Chana4636962009-06-08 18:14:43 -07004558 for (i = 0; i < MAX_RX_DESC_CNT; i++, rxbd++) {
4559 dma_addr_t buf_map;
4560 int n = (i % cp->l2_rx_ring_size) + 1;
4561
Michael Chancd801532010-10-13 14:06:49 +00004562 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chana4636962009-06-08 18:14:43 -07004563 rxbd->rx_bd_len = cp->l2_single_buf_size;
4564 rxbd->rx_bd_flags = RX_BD_FLAGS_START | RX_BD_FLAGS_END;
4565 rxbd->rx_bd_haddr_hi = (u64) buf_map >> 32;
4566 rxbd->rx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4567 }
Michael Chancd801532010-10-13 14:06:49 +00004568 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004569 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_HI, val);
4570 rxbd->rx_bd_haddr_hi = val;
4571
Michael Chancd801532010-10-13 14:06:49 +00004572 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004573 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_LO, val);
4574 rxbd->rx_bd_haddr_lo = val;
4575
4576 val = cnic_reg_rd_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD);
4577 cnic_reg_wr_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD, val | (1 << 2));
4578}
4579
4580static void cnic_shutdown_bnx2_rx_ring(struct cnic_dev *dev)
4581{
4582 struct kwqe *wqes[1], l2kwqe;
4583
4584 memset(&l2kwqe, 0, sizeof(l2kwqe));
4585 wqes[0] = &l2kwqe;
Michael Chane1928c82010-12-23 07:43:04 +00004586 l2kwqe.kwqe_op_flag = (L2_LAYER_CODE << KWQE_LAYER_SHIFT) |
Michael Chana4636962009-06-08 18:14:43 -07004587 (L2_KWQE_OPCODE_VALUE_FLUSH <<
4588 KWQE_OPCODE_SHIFT) | 2;
4589 dev->submit_kwqes(dev, wqes, 1);
4590}
4591
4592static void cnic_set_bnx2_mac(struct cnic_dev *dev)
4593{
4594 struct cnic_local *cp = dev->cnic_priv;
4595 u32 val;
4596
4597 val = cp->func << 2;
4598
4599 cp->shmem_base = cnic_reg_rd_ind(dev, BNX2_SHM_HDR_ADDR_0 + val);
4600
4601 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4602 BNX2_PORT_HW_CFG_ISCSI_MAC_UPPER);
4603 dev->mac_addr[0] = (u8) (val >> 8);
4604 dev->mac_addr[1] = (u8) val;
4605
4606 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH4, val);
4607
4608 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4609 BNX2_PORT_HW_CFG_ISCSI_MAC_LOWER);
4610 dev->mac_addr[2] = (u8) (val >> 24);
4611 dev->mac_addr[3] = (u8) (val >> 16);
4612 dev->mac_addr[4] = (u8) (val >> 8);
4613 dev->mac_addr[5] = (u8) val;
4614
4615 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH5, val);
4616
4617 val = 4 | BNX2_RPM_SORT_USER2_BC_EN;
4618 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4619 val |= BNX2_RPM_SORT_USER2_PROM_VLAN;
4620
4621 CNIC_WR(dev, BNX2_RPM_SORT_USER2, 0x0);
4622 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val);
4623 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val | BNX2_RPM_SORT_USER2_ENA);
4624}
4625
4626static int cnic_start_bnx2_hw(struct cnic_dev *dev)
4627{
4628 struct cnic_local *cp = dev->cnic_priv;
4629 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chana4dde3a2010-02-24 14:42:08 +00004630 struct status_block *sblk = cp->status_blk.gen;
Michael Chane6c28892010-06-24 14:58:39 +00004631 u32 val, kcq_cid_addr, kwq_cid_addr;
Michael Chana4636962009-06-08 18:14:43 -07004632 int err;
4633
4634 cnic_set_bnx2_mac(dev);
4635
4636 val = CNIC_RD(dev, BNX2_MQ_CONFIG);
4637 val &= ~BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE;
4638 if (BCM_PAGE_BITS > 12)
4639 val |= (12 - 8) << 4;
4640 else
4641 val |= (BCM_PAGE_BITS - 8) << 4;
4642
4643 CNIC_WR(dev, BNX2_MQ_CONFIG, val);
4644
4645 CNIC_WR(dev, BNX2_HC_COMP_PROD_TRIP, (2 << 16) | 8);
4646 CNIC_WR(dev, BNX2_HC_COM_TICKS, (64 << 16) | 220);
4647 CNIC_WR(dev, BNX2_HC_CMD_TICKS, (64 << 16) | 220);
4648
4649 err = cnic_setup_5709_context(dev, 1);
4650 if (err)
4651 return err;
4652
4653 cnic_init_context(dev, KWQ_CID);
4654 cnic_init_context(dev, KCQ_CID);
4655
Michael Chane6c28892010-06-24 14:58:39 +00004656 kwq_cid_addr = GET_CID_ADDR(KWQ_CID);
Michael Chana4636962009-06-08 18:14:43 -07004657 cp->kwq_io_addr = MB_GET_CID_ADDR(KWQ_CID) + L5_KRNLQ_HOST_QIDX;
4658
4659 cp->max_kwq_idx = MAX_KWQ_IDX;
4660 cp->kwq_prod_idx = 0;
4661 cp->kwq_con_idx = 0;
Michael Chan1f1332a2010-05-18 11:32:52 +00004662 set_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07004663
4664 if (CHIP_NUM(cp) == CHIP_NUM_5706 || CHIP_NUM(cp) == CHIP_NUM_5708)
4665 cp->kwq_con_idx_ptr = &sblk->status_rx_quick_consumer_index15;
4666 else
4667 cp->kwq_con_idx_ptr = &sblk->status_cmd_consumer_index;
4668
4669 /* Initialize the kernel work queue context. */
4670 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4671 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004672 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004673
4674 val = (BCM_PAGE_SIZE / sizeof(struct kwqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004675 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004676
4677 val = ((BCM_PAGE_SIZE / sizeof(struct kwqe)) << 16) | KWQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004678 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004679
4680 val = (u32) ((u64) cp->kwq_info.pgtbl_map >> 32);
Michael Chane6c28892010-06-24 14:58:39 +00004681 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004682
4683 val = (u32) cp->kwq_info.pgtbl_map;
Michael Chane6c28892010-06-24 14:58:39 +00004684 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004685
Michael Chane6c28892010-06-24 14:58:39 +00004686 kcq_cid_addr = GET_CID_ADDR(KCQ_CID);
4687 cp->kcq1.io_addr = MB_GET_CID_ADDR(KCQ_CID) + L5_KRNLQ_HOST_QIDX;
Michael Chana4636962009-06-08 18:14:43 -07004688
Michael Chane6c28892010-06-24 14:58:39 +00004689 cp->kcq1.sw_prod_idx = 0;
4690 cp->kcq1.hw_prod_idx_ptr =
Joe Perches64699332012-06-04 12:44:16 +00004691 &sblk->status_completion_producer_index;
Michael Chane6c28892010-06-24 14:58:39 +00004692
Joe Perches64699332012-06-04 12:44:16 +00004693 cp->kcq1.status_idx_ptr = &sblk->status_idx;
Michael Chana4636962009-06-08 18:14:43 -07004694
4695 /* Initialize the kernel complete queue context. */
4696 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4697 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004698 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004699
4700 val = (BCM_PAGE_SIZE / sizeof(struct kcqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004701 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004702
4703 val = ((BCM_PAGE_SIZE / sizeof(struct kcqe)) << 16) | KCQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004704 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004705
Michael Chane6c28892010-06-24 14:58:39 +00004706 val = (u32) ((u64) cp->kcq1.dma.pgtbl_map >> 32);
4707 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004708
Michael Chane6c28892010-06-24 14:58:39 +00004709 val = (u32) cp->kcq1.dma.pgtbl_map;
4710 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004711
4712 cp->int_num = 0;
4713 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chane6c28892010-06-24 14:58:39 +00004714 struct status_block_msix *msblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004715 u32 sb_id = cp->status_blk_num;
Michael Chand0549382009-10-28 03:41:59 -07004716 u32 sb = BNX2_L2CTX_L5_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004717
Michael Chane6c28892010-06-24 14:58:39 +00004718 cp->kcq1.hw_prod_idx_ptr =
Joe Perches64699332012-06-04 12:44:16 +00004719 &msblk->status_completion_producer_index;
4720 cp->kcq1.status_idx_ptr = &msblk->status_idx;
4721 cp->kwq_con_idx_ptr = &msblk->status_cmd_consumer_index;
Michael Chana4636962009-06-08 18:14:43 -07004722 cp->int_num = sb_id << BNX2_PCICFG_INT_ACK_CMD_INT_NUM_SHIFT;
Michael Chane6c28892010-06-24 14:58:39 +00004723 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
4724 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
Michael Chana4636962009-06-08 18:14:43 -07004725 }
4726
4727 /* Enable Commnad Scheduler notification when we write to the
4728 * host producer index of the kernel contexts. */
4729 CNIC_WR(dev, BNX2_MQ_KNL_CMD_MASK1, 2);
4730
4731 /* Enable Command Scheduler notification when we write to either
4732 * the Send Queue or Receive Queue producer indexes of the kernel
4733 * bypass contexts. */
4734 CNIC_WR(dev, BNX2_MQ_KNL_BYP_CMD_MASK1, 7);
4735 CNIC_WR(dev, BNX2_MQ_KNL_BYP_WRITE_MASK1, 7);
4736
4737 /* Notify COM when the driver post an application buffer. */
4738 CNIC_WR(dev, BNX2_MQ_KNL_RX_V2P_MASK2, 0x2000);
4739
4740 /* Set the CP and COM doorbells. These two processors polls the
4741 * doorbell for a non zero value before running. This must be done
4742 * after setting up the kernel queue contexts. */
4743 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 1);
4744 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 1);
4745
4746 cnic_init_bnx2_tx_ring(dev);
4747 cnic_init_bnx2_rx_ring(dev);
4748
4749 err = cnic_init_bnx2_irq(dev);
4750 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00004751 netdev_err(dev->netdev, "cnic_init_irq failed\n");
Michael Chana4636962009-06-08 18:14:43 -07004752 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
4753 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
4754 return err;
4755 }
4756
4757 return 0;
4758}
4759
Michael Chan71034ba2009-10-10 13:46:59 +00004760static void cnic_setup_bnx2x_context(struct cnic_dev *dev)
4761{
4762 struct cnic_local *cp = dev->cnic_priv;
4763 struct cnic_eth_dev *ethdev = cp->ethdev;
4764 u32 start_offset = ethdev->ctx_tbl_offset;
4765 int i;
4766
4767 for (i = 0; i < cp->ctx_blks; i++) {
4768 struct cnic_ctx *ctx = &cp->ctx_arr[i];
4769 dma_addr_t map = ctx->mapping;
4770
4771 if (cp->ctx_align) {
4772 unsigned long mask = cp->ctx_align - 1;
4773
4774 map = (map + mask) & ~mask;
4775 }
4776
4777 cnic_ctx_tbl_wr(dev, start_offset + i, map);
4778 }
4779}
4780
4781static int cnic_init_bnx2x_irq(struct cnic_dev *dev)
4782{
4783 struct cnic_local *cp = dev->cnic_priv;
4784 struct cnic_eth_dev *ethdev = cp->ethdev;
4785 int err = 0;
4786
Joe Perches164165d2009-11-19 09:30:10 +00004787 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2x_bh,
Michael Chan71034ba2009-10-10 13:46:59 +00004788 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004789 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
4790 err = cnic_request_irq(dev);
4791
Michael Chan71034ba2009-10-10 13:46:59 +00004792 return err;
4793}
4794
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004795static inline void cnic_storm_memset_hc_disable(struct cnic_dev *dev,
4796 u16 sb_id, u8 sb_index,
4797 u8 disable)
4798{
4799
4800 u32 addr = BAR_CSTRORM_INTMEM +
4801 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4802 offsetof(struct hc_status_block_data_e1x, index_data) +
4803 sizeof(struct hc_index_data)*sb_index +
4804 offsetof(struct hc_index_data, flags);
4805 u16 flags = CNIC_RD16(dev, addr);
4806 /* clear and set */
4807 flags &= ~HC_INDEX_DATA_HC_ENABLED;
4808 flags |= (((~disable) << HC_INDEX_DATA_HC_ENABLED_SHIFT) &
4809 HC_INDEX_DATA_HC_ENABLED);
4810 CNIC_WR16(dev, addr, flags);
4811}
4812
Michael Chan71034ba2009-10-10 13:46:59 +00004813static void cnic_enable_bnx2x_int(struct cnic_dev *dev)
4814{
4815 struct cnic_local *cp = dev->cnic_priv;
4816 u8 sb_id = cp->status_blk_num;
Michael Chan71034ba2009-10-10 13:46:59 +00004817
4818 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004819 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4820 offsetof(struct hc_status_block_data_e1x, index_data) +
4821 sizeof(struct hc_index_data)*HC_INDEX_ISCSI_EQ_CONS +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004822 offsetof(struct hc_index_data, timeout), 64 / 4);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004823 cnic_storm_memset_hc_disable(dev, sb_id, HC_INDEX_ISCSI_EQ_CONS, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004824}
4825
4826static void cnic_disable_bnx2x_int_sync(struct cnic_dev *dev)
4827{
4828}
4829
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004830static void cnic_init_bnx2x_tx_ring(struct cnic_dev *dev,
4831 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004832{
4833 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004834 struct cnic_uio_dev *udev = cp->udev;
4835 union eth_tx_bd_types *txbd = (union eth_tx_bd_types *) udev->l2_ring;
4836 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004837 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004838 int i;
Michael Chan5159fdc2010-12-23 07:42:59 +00004839 u32 cli = cp->ethdev->iscsi_l2_client_id;
Michael Chan71034ba2009-10-10 13:46:59 +00004840 u32 val;
4841
4842 memset(txbd, 0, BCM_PAGE_SIZE);
4843
Michael Chancd801532010-10-13 14:06:49 +00004844 buf_map = udev->l2_buf_map;
Michael Chan71034ba2009-10-10 13:46:59 +00004845 for (i = 0; i < MAX_TX_DESC_CNT; i += 3, txbd += 3) {
4846 struct eth_tx_start_bd *start_bd = &txbd->start_bd;
4847 struct eth_tx_bd *reg_bd = &((txbd + 2)->reg_bd);
4848
4849 start_bd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4850 start_bd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4851 reg_bd->addr_hi = start_bd->addr_hi;
4852 reg_bd->addr_lo = start_bd->addr_lo + 0x10;
4853 start_bd->nbytes = cpu_to_le16(0x10);
4854 start_bd->nbd = cpu_to_le16(3);
4855 start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
4856 start_bd->general_data = (UNICAST_ADDRESS <<
4857 ETH_TX_START_BD_ETH_ADDR_TYPE_SHIFT);
4858 start_bd->general_data |= (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
4859
4860 }
Michael Chan71034ba2009-10-10 13:46:59 +00004861
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004862 val = (u64) ring_map >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004863 txbd->next_bd.addr_hi = cpu_to_le32(val);
4864
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004865 data->tx.tx_bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004866
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004867 val = (u64) ring_map & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004868 txbd->next_bd.addr_lo = cpu_to_le32(val);
4869
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004870 data->tx.tx_bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004871
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004872 /* Other ramrod params */
4873 data->tx.tx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_CQ_CONS;
4874 data->tx.tx_status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004875
4876 /* reset xstorm per client statistics */
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004877 if (cli < MAX_STAT_COUNTER_ID) {
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004878 data->general.statistics_zero_flg = 1;
4879 data->general.statistics_en_flg = 1;
4880 data->general.statistics_counter_id = cli;
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004881 }
Michael Chan71034ba2009-10-10 13:46:59 +00004882
4883 cp->tx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004884 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_CQ_CONS];
Michael Chan71034ba2009-10-10 13:46:59 +00004885}
4886
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004887static void cnic_init_bnx2x_rx_ring(struct cnic_dev *dev,
4888 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004889{
4890 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004891 struct cnic_uio_dev *udev = cp->udev;
4892 struct eth_rx_bd *rxbd = (struct eth_rx_bd *) (udev->l2_ring +
Michael Chan71034ba2009-10-10 13:46:59 +00004893 BCM_PAGE_SIZE);
4894 struct eth_rx_cqe_next_page *rxcqe = (struct eth_rx_cqe_next_page *)
Michael Chancd801532010-10-13 14:06:49 +00004895 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004896 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004897 int i;
Michael Chan5159fdc2010-12-23 07:42:59 +00004898 u32 cli = cp->ethdev->iscsi_l2_client_id;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004899 int cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
Michael Chan71034ba2009-10-10 13:46:59 +00004900 u32 val;
Michael Chancd801532010-10-13 14:06:49 +00004901 dma_addr_t ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004902
4903 /* General data */
4904 data->general.client_id = cli;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004905 data->general.activate_flg = 1;
4906 data->general.sp_client_id = cli;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004907 data->general.mtu = cpu_to_le16(cp->l2_single_buf_size - 14);
4908 data->general.func_id = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00004909
4910 for (i = 0; i < BNX2X_MAX_RX_DESC_CNT; i++, rxbd++) {
4911 dma_addr_t buf_map;
4912 int n = (i % cp->l2_rx_ring_size) + 1;
4913
Michael Chancd801532010-10-13 14:06:49 +00004914 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chan71034ba2009-10-10 13:46:59 +00004915 rxbd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4916 rxbd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4917 }
Michael Chan71034ba2009-10-10 13:46:59 +00004918
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004919 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004920 rxbd->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004921 data->rx.bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004922
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004923 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004924 rxbd->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004925 data->rx.bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004926
4927 rxcqe += BNX2X_MAX_RCQ_DESC_CNT;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004928 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004929 rxcqe->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004930 data->rx.cqe_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004931
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004932 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004933 rxcqe->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004934 data->rx.cqe_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004935
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004936 /* Other ramrod params */
4937 data->rx.client_qzone_id = cl_qzone_id;
4938 data->rx.rx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS;
4939 data->rx.status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004940
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004941 data->rx.cache_line_alignment_log_size = L1_CACHE_SHIFT;
Michael Chan71034ba2009-10-10 13:46:59 +00004942
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004943 data->rx.max_bytes_on_bd = cpu_to_le16(cp->l2_single_buf_size);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004944 data->rx.outer_vlan_removal_enable_flg = 1;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004945 data->rx.silent_vlan_removal_flg = 1;
4946 data->rx.silent_vlan_value = 0;
4947 data->rx.silent_vlan_mask = 0xffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004948
4949 cp->rx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004950 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS];
Michael Chan5159fdc2010-12-23 07:42:59 +00004951 cp->rx_cons = *cp->rx_cons_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00004952}
4953
Michael Chane21ba412010-12-23 07:43:03 +00004954static void cnic_init_bnx2x_kcq(struct cnic_dev *dev)
4955{
4956 struct cnic_local *cp = dev->cnic_priv;
4957 u32 pfid = cp->pfid;
4958
4959 cp->kcq1.io_addr = BAR_CSTRORM_INTMEM +
4960 CSTORM_ISCSI_EQ_PROD_OFFSET(pfid, 0);
4961 cp->kcq1.sw_prod_idx = 0;
4962
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004963 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chane21ba412010-12-23 07:43:03 +00004964 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4965
4966 cp->kcq1.hw_prod_idx_ptr =
4967 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4968 cp->kcq1.status_idx_ptr =
4969 &sb->sb.running_index[SM_RX_ID];
4970 } else {
4971 struct host_hc_status_block_e1x *sb = cp->status_blk.gen;
4972
4973 cp->kcq1.hw_prod_idx_ptr =
4974 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4975 cp->kcq1.status_idx_ptr =
4976 &sb->sb.running_index[SM_RX_ID];
4977 }
4978
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004979 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chane21ba412010-12-23 07:43:03 +00004980 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4981
4982 cp->kcq2.io_addr = BAR_USTRORM_INTMEM +
4983 USTORM_FCOE_EQ_PROD_OFFSET(pfid);
4984 cp->kcq2.sw_prod_idx = 0;
4985 cp->kcq2.hw_prod_idx_ptr =
4986 &sb->sb.index_values[HC_INDEX_FCOE_EQ_CONS];
4987 cp->kcq2.status_idx_ptr =
4988 &sb->sb.running_index[SM_RX_ID];
4989 }
4990}
4991
Michael Chan71034ba2009-10-10 13:46:59 +00004992static int cnic_start_bnx2x_hw(struct cnic_dev *dev)
4993{
4994 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004995 struct cnic_eth_dev *ethdev = cp->ethdev;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004996 int func = CNIC_FUNC(cp), ret;
Michael Chan14203982010-10-06 03:16:06 +00004997 u32 pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00004998
Michael Chana9e0a4f2012-01-04 12:12:27 +00004999 dev->stats_addr = ethdev->addr_drv_info_to_mcp;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005000 cp->port_mode = CHIP_PORT_MODE_NONE;
5001
5002 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Eddie Wai78ea22e2012-06-27 15:08:20 +00005003 u32 val;
Michael Chanee87a822010-10-13 14:06:51 +00005004
Eddie Wai78ea22e2012-06-27 15:08:20 +00005005 pci_read_config_dword(dev->pcidev, PCICFG_ME_REGISTER, &val);
5006 cp->func = (u8) ((val & ME_REG_ABS_PF_NUM) >>
5007 ME_REG_ABS_PF_NUM_SHIFT);
5008 func = CNIC_FUNC(cp);
5009
5010 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN_OVWR);
Michael Chanee87a822010-10-13 14:06:51 +00005011 if (!(val & 1))
5012 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN);
5013 else
5014 val = (val >> 1) & 1;
5015
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005016 if (val) {
5017 cp->port_mode = CHIP_4_PORT_MODE;
Michael Chanee87a822010-10-13 14:06:51 +00005018 cp->pfid = func >> 1;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005019 } else {
Michael Chanb7d40312011-07-13 17:24:18 +00005020 cp->port_mode = CHIP_2_PORT_MODE;
Michael Chanee87a822010-10-13 14:06:51 +00005021 cp->pfid = func & 0x6;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005022 }
Michael Chanee87a822010-10-13 14:06:51 +00005023 } else {
5024 cp->pfid = func;
5025 }
Michael Chan14203982010-10-06 03:16:06 +00005026 pfid = cp->pfid;
5027
Michael Chan71034ba2009-10-10 13:46:59 +00005028 ret = cnic_init_id_tbl(&cp->cid_tbl, MAX_ISCSI_TBL_SZ,
Eddie Wai11f23aa2011-06-08 19:29:34 +00005029 cp->iscsi_start_cid, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00005030
5031 if (ret)
5032 return -ENOMEM;
5033
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005034 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chandc219a22011-08-26 09:45:39 +00005035 ret = cnic_init_id_tbl(&cp->fcoe_cid_tbl, dev->max_fcoe_conn,
Eddie Wai11f23aa2011-06-08 19:29:34 +00005036 cp->fcoe_start_cid, 0);
Michael Chane1928c82010-12-23 07:43:04 +00005037
5038 if (ret)
5039 return -ENOMEM;
5040 }
5041
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005042 cp->bnx2x_igu_sb_id = ethdev->irq_arr[0].status_blk_num2;
5043
Michael Chane21ba412010-12-23 07:43:03 +00005044 cnic_init_bnx2x_kcq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005045
Michael Chan71034ba2009-10-10 13:46:59 +00005046 /* Only 1 EQ */
Michael Chane6c28892010-06-24 14:58:39 +00005047 CNIC_WR16(dev, cp->kcq1.io_addr, MAX_KCQ_IDX);
Michael Chan71034ba2009-10-10 13:46:59 +00005048 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005049 CSTORM_ISCSI_EQ_CONS_OFFSET(pfid, 0), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00005050 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005051 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00005052 cp->kcq1.dma.pg_map_arr[1] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00005053 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005054 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00005055 (u64) cp->kcq1.dma.pg_map_arr[1] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00005056 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005057 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00005058 cp->kcq1.dma.pg_map_arr[0] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00005059 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005060 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00005061 (u64) cp->kcq1.dma.pg_map_arr[0] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00005062 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005063 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_VALID_OFFSET(pfid, 0), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00005064 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005065 CSTORM_ISCSI_EQ_SB_NUM_OFFSET(pfid, 0), cp->status_blk_num);
Michael Chan71034ba2009-10-10 13:46:59 +00005066 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005067 CSTORM_ISCSI_EQ_SB_INDEX_OFFSET(pfid, 0),
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005068 HC_INDEX_ISCSI_EQ_CONS);
Michael Chan71034ba2009-10-10 13:46:59 +00005069
Michael Chan71034ba2009-10-10 13:46:59 +00005070 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005071 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00005072 cp->gbl_buf_info.pg_map_arr[0] & 0xffffffff);
5073 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005074 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00005075 (u64) cp->gbl_buf_info.pg_map_arr[0] >> 32);
5076
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005077 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
5078 TSTORM_ISCSI_TCP_LOCAL_ADV_WND_OFFSET(pfid), DEF_RCV_BUF);
5079
Michael Chan71034ba2009-10-10 13:46:59 +00005080 cnic_setup_bnx2x_context(dev);
5081
Michael Chan71034ba2009-10-10 13:46:59 +00005082 ret = cnic_init_bnx2x_irq(dev);
5083 if (ret)
5084 return ret;
5085
Michael Chan71034ba2009-10-10 13:46:59 +00005086 return 0;
5087}
5088
Michael Chan86b53602009-10-10 13:46:57 +00005089static void cnic_init_rings(struct cnic_dev *dev)
5090{
Michael Chan541a7812010-10-06 03:17:22 +00005091 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00005092 struct cnic_uio_dev *udev = cp->udev;
Michael Chan541a7812010-10-06 03:17:22 +00005093
5094 if (test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
5095 return;
5096
Michael Chan86b53602009-10-10 13:46:57 +00005097 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
5098 cnic_init_bnx2_tx_ring(dev);
5099 cnic_init_bnx2_rx_ring(dev);
Michael Chan541a7812010-10-06 03:17:22 +00005100 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00005101 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
Michael Chan5159fdc2010-12-23 07:42:59 +00005102 u32 cli = cp->ethdev->iscsi_l2_client_id;
5103 u32 cid = cp->ethdev->iscsi_l2_cid;
Michael Chan68d7c1a2011-01-05 15:14:13 +00005104 u32 cl_qzone_id;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005105 struct client_init_ramrod_data *data;
Michael Chan71034ba2009-10-10 13:46:59 +00005106 union l5cm_specific_data l5_data;
5107 struct ustorm_eth_rx_producers rx_prods = {0};
Michael Chane1dd8832011-07-13 17:24:19 +00005108 u32 off, i, *cid_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00005109
5110 rx_prods.bd_prod = 0;
5111 rx_prods.cqe_prod = BNX2X_MAX_RCQ_DESC_CNT;
5112 barrier();
5113
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005114 cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
5115
Michael Chanc7596b72009-12-02 15:15:35 +00005116 off = BAR_USTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005117 (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id) ?
Michael Chanee87a822010-10-13 14:06:51 +00005118 USTORM_RX_PRODS_E2_OFFSET(cl_qzone_id) :
5119 USTORM_RX_PRODS_E1X_OFFSET(CNIC_PORT(cp), cli));
Michael Chan71034ba2009-10-10 13:46:59 +00005120
5121 for (i = 0; i < sizeof(struct ustorm_eth_rx_producers) / 4; i++)
Michael Chanc7596b72009-12-02 15:15:35 +00005122 CNIC_WR(dev, off + i * 4, ((u32 *) &rx_prods)[i]);
Michael Chan71034ba2009-10-10 13:46:59 +00005123
Michael Chan48f753d2010-05-18 11:32:53 +00005124 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
5125
Michael Chancd801532010-10-13 14:06:49 +00005126 data = udev->l2_buf;
Michael Chane1dd8832011-07-13 17:24:19 +00005127 cid_ptr = udev->l2_buf + 12;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005128
5129 memset(data, 0, sizeof(*data));
5130
5131 cnic_init_bnx2x_tx_ring(dev, data);
5132 cnic_init_bnx2x_rx_ring(dev, data);
5133
Michael Chancd801532010-10-13 14:06:49 +00005134 l5_data.phy_address.lo = udev->l2_buf_map & 0xffffffff;
5135 l5_data.phy_address.hi = (u64) udev->l2_buf_map >> 32;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005136
Michael Chan541a7812010-10-06 03:17:22 +00005137 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
5138
Michael Chan71034ba2009-10-10 13:46:59 +00005139 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_CLIENT_SETUP,
Michael Chan68d7c1a2011-01-05 15:14:13 +00005140 cid, ETH_CONNECTION_TYPE, &l5_data);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005141
Michael Chan48f753d2010-05-18 11:32:53 +00005142 i = 0;
5143 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
5144 ++i < 10)
5145 msleep(1);
5146
5147 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
5148 netdev_err(dev->netdev,
5149 "iSCSI CLIENT_SETUP did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00005150 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan5159fdc2010-12-23 07:42:59 +00005151 cnic_ring_ctl(dev, cid, cli, 1);
Michael Chane1dd8832011-07-13 17:24:19 +00005152 *cid_ptr = cid;
Michael Chan86b53602009-10-10 13:46:57 +00005153 }
5154}
5155
5156static void cnic_shutdown_rings(struct cnic_dev *dev)
5157{
Michael Chan541a7812010-10-06 03:17:22 +00005158 struct cnic_local *cp = dev->cnic_priv;
Michael Chane1dd8832011-07-13 17:24:19 +00005159 struct cnic_uio_dev *udev = cp->udev;
5160 void *rx_ring;
Michael Chan541a7812010-10-06 03:17:22 +00005161
5162 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
5163 return;
5164
Michael Chan86b53602009-10-10 13:46:57 +00005165 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
5166 cnic_shutdown_bnx2_rx_ring(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005167 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
Michael Chan5159fdc2010-12-23 07:42:59 +00005168 u32 cli = cp->ethdev->iscsi_l2_client_id;
5169 u32 cid = cp->ethdev->iscsi_l2_cid;
Michael Chan8b065b62009-12-02 15:15:36 +00005170 union l5cm_specific_data l5_data;
Michael Chan48f753d2010-05-18 11:32:53 +00005171 int i;
Michael Chan71034ba2009-10-10 13:46:59 +00005172
Michael Chan5159fdc2010-12-23 07:42:59 +00005173 cnic_ring_ctl(dev, cid, cli, 0);
Michael Chan8b065b62009-12-02 15:15:36 +00005174
Michael Chan48f753d2010-05-18 11:32:53 +00005175 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
5176
Michael Chan8b065b62009-12-02 15:15:36 +00005177 l5_data.phy_address.lo = cli;
5178 l5_data.phy_address.hi = 0;
5179 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_HALT,
Michael Chan5159fdc2010-12-23 07:42:59 +00005180 cid, ETH_CONNECTION_TYPE, &l5_data);
Michael Chan48f753d2010-05-18 11:32:53 +00005181 i = 0;
5182 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
5183 ++i < 10)
5184 msleep(1);
5185
5186 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
5187 netdev_err(dev->netdev,
5188 "iSCSI CLIENT_HALT did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00005189 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan1bcdc322009-12-10 15:40:57 +00005190
5191 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005192 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
Michael Chan68d7c1a2011-01-05 15:14:13 +00005193 cid, NONE_CONNECTION_TYPE, &l5_data);
Michael Chan1bcdc322009-12-10 15:40:57 +00005194 msleep(10);
Michael Chan86b53602009-10-10 13:46:57 +00005195 }
Michael Chan541a7812010-10-06 03:17:22 +00005196 clear_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chane1dd8832011-07-13 17:24:19 +00005197 rx_ring = udev->l2_ring + BCM_PAGE_SIZE;
5198 memset(rx_ring, 0, BCM_PAGE_SIZE);
Michael Chan86b53602009-10-10 13:46:57 +00005199}
5200
Michael Chana3059b12009-08-14 15:49:44 +00005201static int cnic_register_netdev(struct cnic_dev *dev)
5202{
5203 struct cnic_local *cp = dev->cnic_priv;
5204 struct cnic_eth_dev *ethdev = cp->ethdev;
5205 int err;
5206
5207 if (!ethdev)
5208 return -ENODEV;
5209
5210 if (ethdev->drv_state & CNIC_DRV_STATE_REGD)
5211 return 0;
5212
5213 err = ethdev->drv_register_cnic(dev->netdev, cp->cnic_ops, dev);
5214 if (err)
Joe Perchesddf79b22010-02-17 15:01:54 +00005215 netdev_err(dev->netdev, "register_cnic failed\n");
Michael Chana3059b12009-08-14 15:49:44 +00005216
5217 return err;
5218}
5219
5220static void cnic_unregister_netdev(struct cnic_dev *dev)
5221{
5222 struct cnic_local *cp = dev->cnic_priv;
5223 struct cnic_eth_dev *ethdev = cp->ethdev;
5224
5225 if (!ethdev)
5226 return;
5227
5228 ethdev->drv_unregister_cnic(dev->netdev);
5229}
5230
Michael Chana4636962009-06-08 18:14:43 -07005231static int cnic_start_hw(struct cnic_dev *dev)
5232{
5233 struct cnic_local *cp = dev->cnic_priv;
5234 struct cnic_eth_dev *ethdev = cp->ethdev;
5235 int err;
5236
5237 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
5238 return -EALREADY;
5239
Michael Chana4636962009-06-08 18:14:43 -07005240 dev->regview = ethdev->io_base;
Michael Chana4636962009-06-08 18:14:43 -07005241 pci_dev_get(dev->pcidev);
5242 cp->func = PCI_FUNC(dev->pcidev->devfn);
Michael Chana4dde3a2010-02-24 14:42:08 +00005243 cp->status_blk.gen = ethdev->irq_arr[0].status_blk;
Michael Chana4636962009-06-08 18:14:43 -07005244 cp->status_blk_num = ethdev->irq_arr[0].status_blk_num;
5245
5246 err = cp->alloc_resc(dev);
5247 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00005248 netdev_err(dev->netdev, "allocate resource failure\n");
Michael Chana4636962009-06-08 18:14:43 -07005249 goto err1;
5250 }
5251
5252 err = cp->start_hw(dev);
5253 if (err)
5254 goto err1;
5255
5256 err = cnic_cm_open(dev);
5257 if (err)
5258 goto err1;
5259
5260 set_bit(CNIC_F_CNIC_UP, &dev->flags);
5261
5262 cp->enable_int(dev);
5263
5264 return 0;
5265
5266err1:
Michael Chana4636962009-06-08 18:14:43 -07005267 cp->free_resc(dev);
5268 pci_dev_put(dev->pcidev);
Michael Chana4636962009-06-08 18:14:43 -07005269 return err;
5270}
5271
5272static void cnic_stop_bnx2_hw(struct cnic_dev *dev)
5273{
Michael Chana4636962009-06-08 18:14:43 -07005274 cnic_disable_bnx2_int_sync(dev);
5275
5276 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
5277 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
5278
5279 cnic_init_context(dev, KWQ_CID);
5280 cnic_init_context(dev, KCQ_CID);
5281
5282 cnic_setup_5709_context(dev, 0);
5283 cnic_free_irq(dev);
5284
Michael Chana4636962009-06-08 18:14:43 -07005285 cnic_free_resc(dev);
5286}
5287
Michael Chan71034ba2009-10-10 13:46:59 +00005288
5289static void cnic_stop_bnx2x_hw(struct cnic_dev *dev)
5290{
5291 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00005292
5293 cnic_free_irq(dev);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005294 *cp->kcq1.hw_prod_idx_ptr = 0;
Michael Chan4e9c4fd2009-12-10 15:40:58 +00005295 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005296 CSTORM_ISCSI_EQ_CONS_OFFSET(cp->pfid, 0), 0);
Michael Chane6c28892010-06-24 14:58:39 +00005297 CNIC_WR16(dev, cp->kcq1.io_addr, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00005298 cnic_free_resc(dev);
5299}
5300
Michael Chana4636962009-06-08 18:14:43 -07005301static void cnic_stop_hw(struct cnic_dev *dev)
5302{
5303 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
5304 struct cnic_local *cp = dev->cnic_priv;
Michael Chan48f753d2010-05-18 11:32:53 +00005305 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -07005306
Michael Chan48f753d2010-05-18 11:32:53 +00005307 /* Need to wait for the ring shutdown event to complete
5308 * before clearing the CNIC_UP flag.
5309 */
Michael Chancd801532010-10-13 14:06:49 +00005310 while (cp->udev->uio_dev != -1 && i < 15) {
Michael Chan48f753d2010-05-18 11:32:53 +00005311 msleep(100);
5312 i++;
5313 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005314 cnic_shutdown_rings(dev);
Michael Chana2028b232012-06-27 15:08:19 +00005315 cp->stop_cm(dev);
Michael Chana4636962009-06-08 18:14:43 -07005316 clear_bit(CNIC_F_CNIC_UP, &dev->flags);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +00005317 RCU_INIT_POINTER(cp->ulp_ops[CNIC_ULP_L4], NULL);
Michael Chana4636962009-06-08 18:14:43 -07005318 synchronize_rcu();
5319 cnic_cm_shutdown(dev);
5320 cp->stop_hw(dev);
5321 pci_dev_put(dev->pcidev);
5322 }
5323}
5324
5325static void cnic_free_dev(struct cnic_dev *dev)
5326{
5327 int i = 0;
5328
5329 while ((atomic_read(&dev->ref_count) != 0) && i < 10) {
5330 msleep(100);
5331 i++;
5332 }
5333 if (atomic_read(&dev->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +00005334 netdev_err(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -07005335
Joe Perchesddf79b22010-02-17 15:01:54 +00005336 netdev_info(dev->netdev, "Removed CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07005337 dev_put(dev->netdev);
5338 kfree(dev);
5339}
5340
5341static struct cnic_dev *cnic_alloc_dev(struct net_device *dev,
5342 struct pci_dev *pdev)
5343{
5344 struct cnic_dev *cdev;
5345 struct cnic_local *cp;
5346 int alloc_size;
5347
5348 alloc_size = sizeof(struct cnic_dev) + sizeof(struct cnic_local);
5349
5350 cdev = kzalloc(alloc_size , GFP_KERNEL);
5351 if (cdev == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +00005352 netdev_err(dev, "allocate dev struct failure\n");
Michael Chana4636962009-06-08 18:14:43 -07005353 return NULL;
5354 }
5355
5356 cdev->netdev = dev;
5357 cdev->cnic_priv = (char *)cdev + sizeof(struct cnic_dev);
5358 cdev->register_device = cnic_register_device;
5359 cdev->unregister_device = cnic_unregister_device;
5360 cdev->iscsi_nl_msg_recv = cnic_iscsi_nl_msg_recv;
5361
5362 cp = cdev->cnic_priv;
5363 cp->dev = cdev;
Michael Chana4636962009-06-08 18:14:43 -07005364 cp->l2_single_buf_size = 0x400;
5365 cp->l2_rx_ring_size = 3;
5366
5367 spin_lock_init(&cp->cnic_ulp_lock);
5368
Joe Perchesddf79b22010-02-17 15:01:54 +00005369 netdev_info(dev, "Added CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07005370
5371 return cdev;
5372}
5373
5374static struct cnic_dev *init_bnx2_cnic(struct net_device *dev)
5375{
5376 struct pci_dev *pdev;
5377 struct cnic_dev *cdev;
5378 struct cnic_local *cp;
5379 struct cnic_eth_dev *ethdev = NULL;
Michael Chane2ee3612009-06-13 17:43:02 -07005380 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
Michael Chana4636962009-06-08 18:14:43 -07005381
Michael Chane2ee3612009-06-13 17:43:02 -07005382 probe = symbol_get(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07005383 if (probe) {
5384 ethdev = (*probe)(dev);
Michael Chan64c64602009-08-14 15:49:43 +00005385 symbol_put(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07005386 }
5387 if (!ethdev)
5388 return NULL;
5389
5390 pdev = ethdev->pdev;
5391 if (!pdev)
5392 return NULL;
5393
5394 dev_hold(dev);
5395 pci_dev_get(pdev);
Sergei Shtylyovff938e42011-02-28 11:57:33 -08005396 if ((pdev->device == PCI_DEVICE_ID_NX2_5709 ||
5397 pdev->device == PCI_DEVICE_ID_NX2_5709S) &&
5398 (pdev->revision < 0x10)) {
5399 pci_dev_put(pdev);
5400 goto cnic_err;
Michael Chana4636962009-06-08 18:14:43 -07005401 }
5402 pci_dev_put(pdev);
5403
5404 cdev = cnic_alloc_dev(dev, pdev);
5405 if (cdev == NULL)
5406 goto cnic_err;
5407
5408 set_bit(CNIC_F_BNX2_CLASS, &cdev->flags);
5409 cdev->submit_kwqes = cnic_submit_bnx2_kwqes;
5410
5411 cp = cdev->cnic_priv;
5412 cp->ethdev = ethdev;
5413 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00005414 cp->chip_id = ethdev->chip_id;
Michael Chana4636962009-06-08 18:14:43 -07005415
Michael Chan7625eb22011-06-08 19:29:36 +00005416 cdev->max_iscsi_conn = ethdev->max_iscsi_conn;
5417
Michael Chana4636962009-06-08 18:14:43 -07005418 cp->cnic_ops = &cnic_bnx2_ops;
5419 cp->start_hw = cnic_start_bnx2_hw;
5420 cp->stop_hw = cnic_stop_bnx2_hw;
5421 cp->setup_pgtbl = cnic_setup_page_tbl;
5422 cp->alloc_resc = cnic_alloc_bnx2_resc;
5423 cp->free_resc = cnic_free_resc;
5424 cp->start_cm = cnic_cm_init_bnx2_hw;
5425 cp->stop_cm = cnic_cm_stop_bnx2_hw;
5426 cp->enable_int = cnic_enable_bnx2_int;
5427 cp->disable_int_sync = cnic_disable_bnx2_int_sync;
5428 cp->close_conn = cnic_close_bnx2_conn;
Michael Chana4636962009-06-08 18:14:43 -07005429 return cdev;
5430
5431cnic_err:
5432 dev_put(dev);
5433 return NULL;
5434}
5435
Michael Chan71034ba2009-10-10 13:46:59 +00005436static struct cnic_dev *init_bnx2x_cnic(struct net_device *dev)
5437{
5438 struct pci_dev *pdev;
5439 struct cnic_dev *cdev;
5440 struct cnic_local *cp;
5441 struct cnic_eth_dev *ethdev = NULL;
5442 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
5443
5444 probe = symbol_get(bnx2x_cnic_probe);
5445 if (probe) {
5446 ethdev = (*probe)(dev);
5447 symbol_put(bnx2x_cnic_probe);
5448 }
5449 if (!ethdev)
5450 return NULL;
5451
5452 pdev = ethdev->pdev;
5453 if (!pdev)
5454 return NULL;
5455
5456 dev_hold(dev);
5457 cdev = cnic_alloc_dev(dev, pdev);
5458 if (cdev == NULL) {
5459 dev_put(dev);
5460 return NULL;
5461 }
5462
5463 set_bit(CNIC_F_BNX2X_CLASS, &cdev->flags);
5464 cdev->submit_kwqes = cnic_submit_bnx2x_kwqes;
5465
5466 cp = cdev->cnic_priv;
5467 cp->ethdev = ethdev;
5468 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00005469 cp->chip_id = ethdev->chip_id;
Michael Chan71034ba2009-10-10 13:46:59 +00005470
Barak Witkowski1d187b32011-12-05 22:41:50 +00005471 cdev->stats_addr = ethdev->addr_drv_info_to_mcp;
5472
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00005473 if (!(ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI))
5474 cdev->max_iscsi_conn = ethdev->max_iscsi_conn;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005475 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id) &&
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00005476 !(ethdev->drv_state & CNIC_DRV_STATE_NO_FCOE))
5477 cdev->max_fcoe_conn = ethdev->max_fcoe_conn;
5478
Michael Chandc219a22011-08-26 09:45:39 +00005479 if (cdev->max_fcoe_conn > BNX2X_FCOE_NUM_CONNECTIONS)
5480 cdev->max_fcoe_conn = BNX2X_FCOE_NUM_CONNECTIONS;
5481
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00005482 memcpy(cdev->mac_addr, ethdev->iscsi_mac, 6);
5483
Michael Chan71034ba2009-10-10 13:46:59 +00005484 cp->cnic_ops = &cnic_bnx2x_ops;
5485 cp->start_hw = cnic_start_bnx2x_hw;
5486 cp->stop_hw = cnic_stop_bnx2x_hw;
5487 cp->setup_pgtbl = cnic_setup_page_tbl_le;
5488 cp->alloc_resc = cnic_alloc_bnx2x_resc;
5489 cp->free_resc = cnic_free_resc;
5490 cp->start_cm = cnic_cm_init_bnx2x_hw;
5491 cp->stop_cm = cnic_cm_stop_bnx2x_hw;
5492 cp->enable_int = cnic_enable_bnx2x_int;
5493 cp->disable_int_sync = cnic_disable_bnx2x_int_sync;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005494 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id))
Michael Chanee87a822010-10-13 14:06:51 +00005495 cp->ack_int = cnic_ack_bnx2x_e2_msix;
5496 else
5497 cp->ack_int = cnic_ack_bnx2x_msix;
Michael Chan71034ba2009-10-10 13:46:59 +00005498 cp->close_conn = cnic_close_bnx2x_conn;
Michael Chan71034ba2009-10-10 13:46:59 +00005499 return cdev;
5500}
5501
Michael Chana4636962009-06-08 18:14:43 -07005502static struct cnic_dev *is_cnic_dev(struct net_device *dev)
5503{
5504 struct ethtool_drvinfo drvinfo;
5505 struct cnic_dev *cdev = NULL;
5506
5507 if (dev->ethtool_ops && dev->ethtool_ops->get_drvinfo) {
5508 memset(&drvinfo, 0, sizeof(drvinfo));
5509 dev->ethtool_ops->get_drvinfo(dev, &drvinfo);
5510
5511 if (!strcmp(drvinfo.driver, "bnx2"))
5512 cdev = init_bnx2_cnic(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005513 if (!strcmp(drvinfo.driver, "bnx2x"))
5514 cdev = init_bnx2x_cnic(dev);
Michael Chana4636962009-06-08 18:14:43 -07005515 if (cdev) {
5516 write_lock(&cnic_dev_lock);
5517 list_add(&cdev->list, &cnic_dev_list);
5518 write_unlock(&cnic_dev_lock);
5519 }
5520 }
5521 return cdev;
5522}
5523
Michael Chan415199f2011-07-20 14:55:24 +00005524static void cnic_rcv_netevent(struct cnic_local *cp, unsigned long event,
5525 u16 vlan_id)
5526{
5527 int if_type;
5528
5529 rcu_read_lock();
5530 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
5531 struct cnic_ulp_ops *ulp_ops;
5532 void *ctx;
5533
5534 ulp_ops = rcu_dereference(cp->ulp_ops[if_type]);
5535 if (!ulp_ops || !ulp_ops->indicate_netevent)
5536 continue;
5537
5538 ctx = cp->ulp_handle[if_type];
5539
5540 ulp_ops->indicate_netevent(ctx, event, vlan_id);
5541 }
5542 rcu_read_unlock();
5543}
5544
Michael Chana4636962009-06-08 18:14:43 -07005545/**
5546 * netdev event handler
5547 */
5548static int cnic_netdev_event(struct notifier_block *this, unsigned long event,
5549 void *ptr)
5550{
5551 struct net_device *netdev = ptr;
5552 struct cnic_dev *dev;
Michael Chana4636962009-06-08 18:14:43 -07005553 int new_dev = 0;
5554
5555 dev = cnic_from_netdev(netdev);
5556
Michael Chandb1d3502011-06-08 19:29:35 +00005557 if (!dev && (event == NETDEV_REGISTER || netif_running(netdev))) {
Michael Chana4636962009-06-08 18:14:43 -07005558 /* Check for the hot-plug device */
5559 dev = is_cnic_dev(netdev);
5560 if (dev) {
5561 new_dev = 1;
5562 cnic_hold(dev);
5563 }
5564 }
5565 if (dev) {
5566 struct cnic_local *cp = dev->cnic_priv;
5567
5568 if (new_dev)
5569 cnic_ulp_init(dev);
5570 else if (event == NETDEV_UNREGISTER)
5571 cnic_ulp_exit(dev);
Michael Chan6053bbf2009-10-02 11:03:28 -07005572
Michael Chandb1d3502011-06-08 19:29:35 +00005573 if (event == NETDEV_UP || (new_dev && netif_running(netdev))) {
Michael Chana3059b12009-08-14 15:49:44 +00005574 if (cnic_register_netdev(dev) != 0) {
5575 cnic_put(dev);
5576 goto done;
5577 }
Michael Chana4636962009-06-08 18:14:43 -07005578 if (!cnic_start_hw(dev))
5579 cnic_ulp_start(dev);
Michael Chana4636962009-06-08 18:14:43 -07005580 }
5581
Michael Chan415199f2011-07-20 14:55:24 +00005582 cnic_rcv_netevent(cp, event, 0);
Michael Chana4636962009-06-08 18:14:43 -07005583
5584 if (event == NETDEV_GOING_DOWN) {
Michael Chana4636962009-06-08 18:14:43 -07005585 cnic_ulp_stop(dev);
5586 cnic_stop_hw(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005587 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005588 } else if (event == NETDEV_UNREGISTER) {
5589 write_lock(&cnic_dev_lock);
5590 list_del_init(&dev->list);
5591 write_unlock(&cnic_dev_lock);
5592
5593 cnic_put(dev);
5594 cnic_free_dev(dev);
5595 goto done;
5596 }
5597 cnic_put(dev);
Michael Chan415199f2011-07-20 14:55:24 +00005598 } else {
5599 struct net_device *realdev;
5600 u16 vid;
5601
5602 vid = cnic_get_vlan(netdev, &realdev);
5603 if (realdev) {
5604 dev = cnic_from_netdev(realdev);
5605 if (dev) {
5606 vid |= VLAN_TAG_PRESENT;
5607 cnic_rcv_netevent(dev->cnic_priv, event, vid);
5608 cnic_put(dev);
5609 }
5610 }
Michael Chana4636962009-06-08 18:14:43 -07005611 }
5612done:
5613 return NOTIFY_DONE;
5614}
5615
5616static struct notifier_block cnic_netdev_notifier = {
5617 .notifier_call = cnic_netdev_event
5618};
5619
5620static void cnic_release(void)
5621{
5622 struct cnic_dev *dev;
Michael Chana3ceeeb2010-10-13 14:06:50 +00005623 struct cnic_uio_dev *udev;
Michael Chana4636962009-06-08 18:14:43 -07005624
5625 while (!list_empty(&cnic_dev_list)) {
5626 dev = list_entry(cnic_dev_list.next, struct cnic_dev, list);
5627 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
5628 cnic_ulp_stop(dev);
5629 cnic_stop_hw(dev);
5630 }
5631
5632 cnic_ulp_exit(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005633 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005634 list_del_init(&dev->list);
5635 cnic_free_dev(dev);
5636 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005637 while (!list_empty(&cnic_udev_list)) {
5638 udev = list_entry(cnic_udev_list.next, struct cnic_uio_dev,
5639 list);
5640 cnic_free_uio(udev);
5641 }
Michael Chana4636962009-06-08 18:14:43 -07005642}
5643
5644static int __init cnic_init(void)
5645{
5646 int rc = 0;
5647
Joe Perchesddf79b22010-02-17 15:01:54 +00005648 pr_info("%s", version);
Michael Chana4636962009-06-08 18:14:43 -07005649
5650 rc = register_netdevice_notifier(&cnic_netdev_notifier);
5651 if (rc) {
5652 cnic_release();
5653 return rc;
5654 }
5655
Michael Chanfdf24082010-10-13 14:06:47 +00005656 cnic_wq = create_singlethread_workqueue("cnic_wq");
5657 if (!cnic_wq) {
5658 cnic_release();
5659 unregister_netdevice_notifier(&cnic_netdev_notifier);
5660 return -ENOMEM;
5661 }
5662
Michael Chana4636962009-06-08 18:14:43 -07005663 return 0;
5664}
5665
5666static void __exit cnic_exit(void)
5667{
5668 unregister_netdevice_notifier(&cnic_netdev_notifier);
5669 cnic_release();
Michael Chanfdf24082010-10-13 14:06:47 +00005670 destroy_workqueue(cnic_wq);
Michael Chana4636962009-06-08 18:14:43 -07005671}
5672
5673module_init(cnic_init);
5674module_exit(cnic_exit);