blob: f897306b0eb77f0d0a20f9e1fe9ea9485cb6cbc1 [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 Chan1f85d582012-06-27 15:08:21 +00001066 uinfo->mem[0].memtype = UIO_MEM_NONE;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001067
Michael Chan5e9b2db2009-08-26 09:49:23 +00001068 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
Michael Chana4dde3a2010-02-24 14:42:08 +00001069 uinfo->mem[1].addr = (unsigned long) cp->status_blk.gen &
Michael Chancd801532010-10-13 14:06:49 +00001070 PAGE_MASK;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001071 if (cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
1072 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE * 9;
1073 else
1074 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE;
1075
1076 uinfo->name = "bnx2_cnic";
Michael Chan71034ba2009-10-10 13:46:59 +00001077 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
1078 uinfo->mem[1].addr = (unsigned long) cp->bnx2x_def_status_blk &
1079 PAGE_MASK;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001080 uinfo->mem[1].size = sizeof(*cp->bnx2x_def_status_blk);
Michael Chan71034ba2009-10-10 13:46:59 +00001081
1082 uinfo->name = "bnx2x_cnic";
Michael Chan5e9b2db2009-08-26 09:49:23 +00001083 }
1084
1085 uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
1086
Michael Chancd801532010-10-13 14:06:49 +00001087 uinfo->mem[2].addr = (unsigned long) udev->l2_ring;
1088 uinfo->mem[2].size = udev->l2_ring_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001089 uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
1090
Michael Chancd801532010-10-13 14:06:49 +00001091 uinfo->mem[3].addr = (unsigned long) udev->l2_buf;
1092 uinfo->mem[3].size = udev->l2_buf_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001093 uinfo->mem[3].memtype = UIO_MEM_LOGICAL;
1094
1095 uinfo->version = CNIC_MODULE_VERSION;
1096 uinfo->irq = UIO_IRQ_CUSTOM;
1097
1098 uinfo->open = cnic_uio_open;
1099 uinfo->release = cnic_uio_close;
1100
Michael Chana3ceeeb2010-10-13 14:06:50 +00001101 if (udev->uio_dev == -1) {
1102 if (!uinfo->priv) {
1103 uinfo->priv = udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001104
Michael Chana3ceeeb2010-10-13 14:06:50 +00001105 ret = uio_register_device(&udev->pdev->dev, uinfo);
1106 }
1107 } else {
1108 cnic_init_rings(dev);
1109 }
Michael Chan5e9b2db2009-08-26 09:49:23 +00001110
Michael Chancd801532010-10-13 14:06:49 +00001111 return ret;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001112}
1113
Michael Chana4636962009-06-08 18:14:43 -07001114static int cnic_alloc_bnx2_resc(struct cnic_dev *dev)
1115{
1116 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07001117 int ret;
1118
1119 ret = cnic_alloc_dma(dev, &cp->kwq_info, KWQ_PAGE_CNT, 1);
1120 if (ret)
1121 goto error;
1122 cp->kwq = (struct kwqe **) cp->kwq_info.pg_arr;
1123
Michael Chan59e51372011-06-14 01:32:38 +00001124 ret = cnic_alloc_kcq(dev, &cp->kcq1, true);
Michael Chana4636962009-06-08 18:14:43 -07001125 if (ret)
1126 goto error;
Michael Chana4636962009-06-08 18:14:43 -07001127
1128 ret = cnic_alloc_context(dev);
1129 if (ret)
1130 goto error;
1131
Michael Chancd801532010-10-13 14:06:49 +00001132 ret = cnic_alloc_uio_rings(dev, 2);
Michael Chanec0248e2009-08-26 09:49:22 +00001133 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001134 goto error;
1135
Michael Chancd801532010-10-13 14:06:49 +00001136 ret = cnic_init_uio(dev);
Michael Chan5e9b2db2009-08-26 09:49:23 +00001137 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001138 goto error;
1139
Michael Chana4636962009-06-08 18:14:43 -07001140 return 0;
1141
1142error:
1143 cnic_free_resc(dev);
1144 return ret;
1145}
1146
Michael Chan71034ba2009-10-10 13:46:59 +00001147static int cnic_alloc_bnx2x_context(struct cnic_dev *dev)
1148{
1149 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001150 int ctx_blk_size = cp->ethdev->ctx_blk_size;
Michael Chan520efdf2010-06-24 14:58:37 +00001151 int total_mem, blks, i;
Michael Chan71034ba2009-10-10 13:46:59 +00001152
Michael Chan520efdf2010-06-24 14:58:37 +00001153 total_mem = BNX2X_CONTEXT_MEM_SIZE * cp->max_cid_space;
Michael Chan71034ba2009-10-10 13:46:59 +00001154 blks = total_mem / ctx_blk_size;
1155 if (total_mem % ctx_blk_size)
1156 blks++;
1157
1158 if (blks > cp->ethdev->ctx_tbl_len)
1159 return -ENOMEM;
1160
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001161 cp->ctx_arr = kcalloc(blks, sizeof(struct cnic_ctx), GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001162 if (cp->ctx_arr == NULL)
1163 return -ENOMEM;
1164
1165 cp->ctx_blks = blks;
1166 cp->ctx_blk_size = ctx_blk_size;
Michael Chanee87a822010-10-13 14:06:51 +00001167 if (!BNX2X_CHIP_IS_57710(cp->chip_id))
Michael Chan71034ba2009-10-10 13:46:59 +00001168 cp->ctx_align = 0;
1169 else
1170 cp->ctx_align = ctx_blk_size;
1171
1172 cp->cids_per_blk = ctx_blk_size / BNX2X_CONTEXT_MEM_SIZE;
1173
1174 for (i = 0; i < blks; i++) {
1175 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +00001176 dma_alloc_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
1177 &cp->ctx_arr[i].mapping,
1178 GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001179 if (cp->ctx_arr[i].ctx == NULL)
1180 return -ENOMEM;
1181
1182 if (cp->ctx_align && cp->ctx_blk_size == ctx_blk_size) {
1183 if (cp->ctx_arr[i].mapping & (cp->ctx_align - 1)) {
1184 cnic_free_context(dev);
1185 cp->ctx_blk_size += cp->ctx_align;
1186 i = -1;
1187 continue;
1188 }
1189 }
1190 }
1191 return 0;
1192}
1193
1194static int cnic_alloc_bnx2x_resc(struct cnic_dev *dev)
1195{
1196 struct cnic_local *cp = dev->cnic_priv;
Michael Chan520efdf2010-06-24 14:58:37 +00001197 struct cnic_eth_dev *ethdev = cp->ethdev;
1198 u32 start_cid = ethdev->starting_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001199 int i, j, n, ret, pages;
1200 struct cnic_dma *kwq_16_dma = &cp->kwq_16_data_info;
1201
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001202 cp->iro_arr = ethdev->iro_arr;
1203
Michael Chanb37a41e2011-07-20 14:55:22 +00001204 cp->max_cid_space = MAX_ISCSI_TBL_SZ;
Michael Chan520efdf2010-06-24 14:58:37 +00001205 cp->iscsi_start_cid = start_cid;
Michael Chane1928c82010-12-23 07:43:04 +00001206 cp->fcoe_start_cid = start_cid + MAX_ISCSI_TBL_SZ;
1207
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001208 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chandc219a22011-08-26 09:45:39 +00001209 cp->max_cid_space += dev->max_fcoe_conn;
Michael Chane1928c82010-12-23 07:43:04 +00001210 cp->fcoe_init_cid = ethdev->fcoe_init_cid;
1211 if (!cp->fcoe_init_cid)
1212 cp->fcoe_init_cid = 0x10;
1213 }
1214
Michael Chan71034ba2009-10-10 13:46:59 +00001215 cp->iscsi_tbl = kzalloc(sizeof(struct cnic_iscsi) * MAX_ISCSI_TBL_SZ,
1216 GFP_KERNEL);
1217 if (!cp->iscsi_tbl)
1218 goto error;
1219
1220 cp->ctx_tbl = kzalloc(sizeof(struct cnic_context) *
Michael Chan520efdf2010-06-24 14:58:37 +00001221 cp->max_cid_space, GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001222 if (!cp->ctx_tbl)
1223 goto error;
1224
1225 for (i = 0; i < MAX_ISCSI_TBL_SZ; i++) {
1226 cp->ctx_tbl[i].proto.iscsi = &cp->iscsi_tbl[i];
1227 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_ISCSI;
1228 }
1229
Michael Chane1928c82010-12-23 07:43:04 +00001230 for (i = MAX_ISCSI_TBL_SZ; i < cp->max_cid_space; i++)
1231 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_FCOE;
1232
Michael Chan520efdf2010-06-24 14:58:37 +00001233 pages = PAGE_ALIGN(cp->max_cid_space * CNIC_KWQ16_DATA_SIZE) /
Michael Chan71034ba2009-10-10 13:46:59 +00001234 PAGE_SIZE;
1235
1236 ret = cnic_alloc_dma(dev, kwq_16_dma, pages, 0);
1237 if (ret)
1238 return -ENOMEM;
1239
1240 n = PAGE_SIZE / CNIC_KWQ16_DATA_SIZE;
Michael Chan520efdf2010-06-24 14:58:37 +00001241 for (i = 0, j = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +00001242 long off = CNIC_KWQ16_DATA_SIZE * (i % n);
1243
1244 cp->ctx_tbl[i].kwqe_data = kwq_16_dma->pg_arr[j] + off;
1245 cp->ctx_tbl[i].kwqe_data_mapping = kwq_16_dma->pg_map_arr[j] +
1246 off;
1247
1248 if ((i % n) == (n - 1))
1249 j++;
1250 }
1251
Michael Chan59e51372011-06-14 01:32:38 +00001252 ret = cnic_alloc_kcq(dev, &cp->kcq1, false);
Michael Chan71034ba2009-10-10 13:46:59 +00001253 if (ret)
1254 goto error;
Michael Chan71034ba2009-10-10 13:46:59 +00001255
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001256 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
1257 ret = cnic_alloc_kcq(dev, &cp->kcq2, true);
Michael Chane21ba412010-12-23 07:43:03 +00001258 if (ret)
1259 goto error;
1260 }
1261
Michael Chan71034ba2009-10-10 13:46:59 +00001262 pages = PAGE_ALIGN(BNX2X_ISCSI_GLB_BUF_SIZE) / PAGE_SIZE;
1263 ret = cnic_alloc_dma(dev, &cp->gbl_buf_info, pages, 0);
1264 if (ret)
1265 goto error;
1266
1267 ret = cnic_alloc_bnx2x_context(dev);
1268 if (ret)
1269 goto error;
1270
Michael Chan71034ba2009-10-10 13:46:59 +00001271 cp->bnx2x_def_status_blk = cp->ethdev->irq_arr[1].status_blk;
1272
1273 cp->l2_rx_ring_size = 15;
1274
Michael Chancd801532010-10-13 14:06:49 +00001275 ret = cnic_alloc_uio_rings(dev, 4);
Michael Chan71034ba2009-10-10 13:46:59 +00001276 if (ret)
1277 goto error;
1278
Michael Chancd801532010-10-13 14:06:49 +00001279 ret = cnic_init_uio(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00001280 if (ret)
1281 goto error;
1282
1283 return 0;
1284
1285error:
1286 cnic_free_resc(dev);
1287 return -ENOMEM;
1288}
1289
Michael Chana4636962009-06-08 18:14:43 -07001290static inline u32 cnic_kwq_avail(struct cnic_local *cp)
1291{
1292 return cp->max_kwq_idx -
1293 ((cp->kwq_prod_idx - cp->kwq_con_idx) & cp->max_kwq_idx);
1294}
1295
1296static int cnic_submit_bnx2_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
1297 u32 num_wqes)
1298{
1299 struct cnic_local *cp = dev->cnic_priv;
1300 struct kwqe *prod_qe;
1301 u16 prod, sw_prod, i;
1302
1303 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
1304 return -EAGAIN; /* bnx2 is down */
1305
1306 spin_lock_bh(&cp->cnic_ulp_lock);
1307 if (num_wqes > cnic_kwq_avail(cp) &&
Michael Chan1f1332a2010-05-18 11:32:52 +00001308 !test_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags)) {
Michael Chana4636962009-06-08 18:14:43 -07001309 spin_unlock_bh(&cp->cnic_ulp_lock);
1310 return -EAGAIN;
1311 }
1312
Michael Chan1f1332a2010-05-18 11:32:52 +00001313 clear_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07001314
1315 prod = cp->kwq_prod_idx;
1316 sw_prod = prod & MAX_KWQ_IDX;
1317 for (i = 0; i < num_wqes; i++) {
1318 prod_qe = &cp->kwq[KWQ_PG(sw_prod)][KWQ_IDX(sw_prod)];
1319 memcpy(prod_qe, wqes[i], sizeof(struct kwqe));
1320 prod++;
1321 sw_prod = prod & MAX_KWQ_IDX;
1322 }
1323 cp->kwq_prod_idx = prod;
1324
1325 CNIC_WR16(dev, cp->kwq_io_addr, cp->kwq_prod_idx);
1326
1327 spin_unlock_bh(&cp->cnic_ulp_lock);
1328 return 0;
1329}
1330
Michael Chan71034ba2009-10-10 13:46:59 +00001331static void *cnic_get_kwqe_16_data(struct cnic_local *cp, u32 l5_cid,
1332 union l5cm_specific_data *l5_data)
1333{
1334 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1335 dma_addr_t map;
1336
1337 map = ctx->kwqe_data_mapping;
1338 l5_data->phy_address.lo = (u64) map & 0xffffffff;
1339 l5_data->phy_address.hi = (u64) map >> 32;
1340 return ctx->kwqe_data;
1341}
1342
1343static int cnic_submit_kwqe_16(struct cnic_dev *dev, u32 cmd, u32 cid,
1344 u32 type, union l5cm_specific_data *l5_data)
1345{
1346 struct cnic_local *cp = dev->cnic_priv;
1347 struct l5cm_spe kwqe;
1348 struct kwqe_16 *kwq[1];
Michael Chan68d7c1a2011-01-05 15:14:13 +00001349 u16 type_16;
Michael Chan71034ba2009-10-10 13:46:59 +00001350 int ret;
1351
1352 kwqe.hdr.conn_and_cmd_data =
1353 cpu_to_le32(((cmd << SPE_HDR_CMD_ID_SHIFT) |
Michael Chanceb7e1c2010-10-06 03:14:54 +00001354 BNX2X_HW_CID(cp, cid)));
Michael Chan68d7c1a2011-01-05 15:14:13 +00001355
1356 type_16 = (type << SPE_HDR_CONN_TYPE_SHIFT) & SPE_HDR_CONN_TYPE;
1357 type_16 |= (cp->pfid << SPE_HDR_FUNCTION_ID_SHIFT) &
1358 SPE_HDR_FUNCTION_ID;
1359
1360 kwqe.hdr.type = cpu_to_le16(type_16);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001361 kwqe.hdr.reserved1 = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001362 kwqe.data.phy_address.lo = cpu_to_le32(l5_data->phy_address.lo);
1363 kwqe.data.phy_address.hi = cpu_to_le32(l5_data->phy_address.hi);
1364
1365 kwq[0] = (struct kwqe_16 *) &kwqe;
1366
1367 spin_lock_bh(&cp->cnic_ulp_lock);
1368 ret = cp->ethdev->drv_submit_kwqes_16(dev->netdev, kwq, 1);
1369 spin_unlock_bh(&cp->cnic_ulp_lock);
1370
1371 if (ret == 1)
1372 return 0;
1373
Michael Chan23021c22012-01-04 12:12:28 +00001374 return ret;
Michael Chan71034ba2009-10-10 13:46:59 +00001375}
1376
1377static void cnic_reply_bnx2x_kcqes(struct cnic_dev *dev, int ulp_type,
1378 struct kcqe *cqes[], u32 num_cqes)
1379{
1380 struct cnic_local *cp = dev->cnic_priv;
1381 struct cnic_ulp_ops *ulp_ops;
1382
1383 rcu_read_lock();
1384 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
1385 if (likely(ulp_ops)) {
1386 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
1387 cqes, num_cqes);
1388 }
1389 rcu_read_unlock();
1390}
1391
1392static int cnic_bnx2x_iscsi_init1(struct cnic_dev *dev, struct kwqe *kwqe)
1393{
1394 struct cnic_local *cp = dev->cnic_priv;
1395 struct iscsi_kwqe_init1 *req1 = (struct iscsi_kwqe_init1 *) kwqe;
Michael Chan14203982010-10-06 03:16:06 +00001396 int hq_bds, pages;
1397 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001398
1399 cp->num_iscsi_tasks = req1->num_tasks_per_conn;
1400 cp->num_ccells = req1->num_ccells_per_conn;
1401 cp->task_array_size = BNX2X_ISCSI_TASK_CONTEXT_SIZE *
1402 cp->num_iscsi_tasks;
1403 cp->r2tq_size = cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS *
1404 BNX2X_ISCSI_R2TQE_SIZE;
1405 cp->hq_size = cp->num_ccells * BNX2X_ISCSI_HQ_BD_SIZE;
1406 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1407 hq_bds = pages * (PAGE_SIZE / BNX2X_ISCSI_HQ_BD_SIZE);
1408 cp->num_cqs = req1->num_cqs;
1409
1410 if (!dev->max_iscsi_conn)
1411 return 0;
1412
1413 /* init Tstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001414 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001415 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001416 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001417 PAGE_SIZE);
1418 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001419 TSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001420 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001421 TSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001422 req1->num_tasks_per_conn);
1423
1424 /* init Ustorm RAM */
1425 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001426 USTORM_ISCSI_RQ_BUFFER_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001427 req1->rq_buffer_size);
Michael Chan14203982010-10-06 03:16:06 +00001428 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001429 PAGE_SIZE);
1430 CNIC_WR8(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001431 USTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001432 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001433 USTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001434 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001435 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001436 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001437 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001438 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001439 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001440 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1441
1442 /* init Xstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001443 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001444 PAGE_SIZE);
1445 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001446 XSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001447 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001448 XSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001449 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001450 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001451 hq_bds);
Michael Chan14203982010-10-06 03:16:06 +00001452 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_SQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001453 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001454 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001455 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1456
1457 /* init Cstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001458 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001459 PAGE_SIZE);
1460 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001461 CSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001462 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001463 CSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001464 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001465 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001466 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001467 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001468 hq_bds);
1469
1470 return 0;
1471}
1472
1473static int cnic_bnx2x_iscsi_init2(struct cnic_dev *dev, struct kwqe *kwqe)
1474{
1475 struct iscsi_kwqe_init2 *req2 = (struct iscsi_kwqe_init2 *) kwqe;
1476 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00001477 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001478 struct iscsi_kcqe kcqe;
1479 struct kcqe *cqes[1];
1480
1481 memset(&kcqe, 0, sizeof(kcqe));
1482 if (!dev->max_iscsi_conn) {
1483 kcqe.completion_status =
1484 ISCSI_KCQE_COMPLETION_STATUS_ISCSI_NOT_SUPPORTED;
1485 goto done;
1486 }
1487
1488 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001489 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001490 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001491 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001492 req2->error_bit_map[1]);
1493
1494 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001495 USTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001496 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001497 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001498 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001499 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001500 req2->error_bit_map[1]);
1501
1502 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001503 CSTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001504
1505 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1506
1507done:
1508 kcqe.op_code = ISCSI_KCQE_OPCODE_INIT;
1509 cqes[0] = (struct kcqe *) &kcqe;
1510 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1511
1512 return 0;
1513}
1514
1515static void cnic_free_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1516{
1517 struct cnic_local *cp = dev->cnic_priv;
1518 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1519
1520 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI) {
1521 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1522
1523 cnic_free_dma(dev, &iscsi->hq_info);
1524 cnic_free_dma(dev, &iscsi->r2tq_info);
1525 cnic_free_dma(dev, &iscsi->task_array_info);
Michael Chane1928c82010-12-23 07:43:04 +00001526 cnic_free_id(&cp->cid_tbl, ctx->cid);
1527 } else {
1528 cnic_free_id(&cp->fcoe_cid_tbl, ctx->cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001529 }
Michael Chane1928c82010-12-23 07:43:04 +00001530
Michael Chan71034ba2009-10-10 13:46:59 +00001531 ctx->cid = 0;
1532}
1533
1534static int cnic_alloc_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1535{
1536 u32 cid;
1537 int ret, pages;
1538 struct cnic_local *cp = dev->cnic_priv;
1539 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1540 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1541
Michael Chane1928c82010-12-23 07:43:04 +00001542 if (ctx->ulp_proto_id == CNIC_ULP_FCOE) {
1543 cid = cnic_alloc_new_id(&cp->fcoe_cid_tbl);
1544 if (cid == -1) {
1545 ret = -ENOMEM;
1546 goto error;
1547 }
1548 ctx->cid = cid;
1549 return 0;
1550 }
1551
Michael Chan71034ba2009-10-10 13:46:59 +00001552 cid = cnic_alloc_new_id(&cp->cid_tbl);
1553 if (cid == -1) {
1554 ret = -ENOMEM;
1555 goto error;
1556 }
1557
1558 ctx->cid = cid;
1559 pages = PAGE_ALIGN(cp->task_array_size) / PAGE_SIZE;
1560
1561 ret = cnic_alloc_dma(dev, &iscsi->task_array_info, pages, 1);
1562 if (ret)
1563 goto error;
1564
1565 pages = PAGE_ALIGN(cp->r2tq_size) / PAGE_SIZE;
1566 ret = cnic_alloc_dma(dev, &iscsi->r2tq_info, pages, 1);
1567 if (ret)
1568 goto error;
1569
1570 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1571 ret = cnic_alloc_dma(dev, &iscsi->hq_info, pages, 1);
1572 if (ret)
1573 goto error;
1574
1575 return 0;
1576
1577error:
1578 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1579 return ret;
1580}
1581
1582static void *cnic_get_bnx2x_ctx(struct cnic_dev *dev, u32 cid, int init,
1583 struct regpair *ctx_addr)
1584{
1585 struct cnic_local *cp = dev->cnic_priv;
1586 struct cnic_eth_dev *ethdev = cp->ethdev;
1587 int blk = (cid - ethdev->starting_cid) / cp->cids_per_blk;
1588 int off = (cid - ethdev->starting_cid) % cp->cids_per_blk;
1589 unsigned long align_off = 0;
1590 dma_addr_t ctx_map;
1591 void *ctx;
1592
1593 if (cp->ctx_align) {
1594 unsigned long mask = cp->ctx_align - 1;
1595
1596 if (cp->ctx_arr[blk].mapping & mask)
1597 align_off = cp->ctx_align -
1598 (cp->ctx_arr[blk].mapping & mask);
1599 }
1600 ctx_map = cp->ctx_arr[blk].mapping + align_off +
1601 (off * BNX2X_CONTEXT_MEM_SIZE);
1602 ctx = cp->ctx_arr[blk].ctx + align_off +
1603 (off * BNX2X_CONTEXT_MEM_SIZE);
1604 if (init)
1605 memset(ctx, 0, BNX2X_CONTEXT_MEM_SIZE);
1606
1607 ctx_addr->lo = ctx_map & 0xffffffff;
1608 ctx_addr->hi = (u64) ctx_map >> 32;
1609 return ctx;
1610}
1611
1612static int cnic_setup_bnx2x_ctx(struct cnic_dev *dev, struct kwqe *wqes[],
1613 u32 num)
1614{
1615 struct cnic_local *cp = dev->cnic_priv;
1616 struct iscsi_kwqe_conn_offload1 *req1 =
1617 (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1618 struct iscsi_kwqe_conn_offload2 *req2 =
1619 (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1620 struct iscsi_kwqe_conn_offload3 *req3;
1621 struct cnic_context *ctx = &cp->ctx_tbl[req1->iscsi_conn_id];
1622 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1623 u32 cid = ctx->cid;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001624 u32 hw_cid = BNX2X_HW_CID(cp, cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001625 struct iscsi_context *ictx;
1626 struct regpair context_addr;
1627 int i, j, n = 2, n_max;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001628 u8 port = CNIC_PORT(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00001629
1630 ctx->ctx_flags = 0;
1631 if (!req2->num_additional_wqes)
1632 return -EINVAL;
1633
1634 n_max = req2->num_additional_wqes + 2;
1635
1636 ictx = cnic_get_bnx2x_ctx(dev, cid, 1, &context_addr);
1637 if (ictx == NULL)
1638 return -ENOMEM;
1639
1640 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1641
1642 ictx->xstorm_ag_context.hq_prod = 1;
1643
1644 ictx->xstorm_st_context.iscsi.first_burst_length =
1645 ISCSI_DEF_FIRST_BURST_LEN;
1646 ictx->xstorm_st_context.iscsi.max_send_pdu_length =
1647 ISCSI_DEF_MAX_RECV_SEG_LEN;
1648 ictx->xstorm_st_context.iscsi.sq_pbl_base.lo =
1649 req1->sq_page_table_addr_lo;
1650 ictx->xstorm_st_context.iscsi.sq_pbl_base.hi =
1651 req1->sq_page_table_addr_hi;
1652 ictx->xstorm_st_context.iscsi.sq_curr_pbe.lo = req2->sq_first_pte.hi;
1653 ictx->xstorm_st_context.iscsi.sq_curr_pbe.hi = req2->sq_first_pte.lo;
1654 ictx->xstorm_st_context.iscsi.hq_pbl_base.lo =
1655 iscsi->hq_info.pgtbl_map & 0xffffffff;
1656 ictx->xstorm_st_context.iscsi.hq_pbl_base.hi =
1657 (u64) iscsi->hq_info.pgtbl_map >> 32;
1658 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.lo =
1659 iscsi->hq_info.pgtbl[0];
1660 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.hi =
1661 iscsi->hq_info.pgtbl[1];
1662 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.lo =
1663 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1664 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.hi =
1665 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1666 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.lo =
1667 iscsi->r2tq_info.pgtbl[0];
1668 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.hi =
1669 iscsi->r2tq_info.pgtbl[1];
1670 ictx->xstorm_st_context.iscsi.task_pbl_base.lo =
1671 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1672 ictx->xstorm_st_context.iscsi.task_pbl_base.hi =
1673 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1674 ictx->xstorm_st_context.iscsi.task_pbl_cache_idx =
1675 BNX2X_ISCSI_PBL_NOT_CACHED;
1676 ictx->xstorm_st_context.iscsi.flags.flags |=
1677 XSTORM_ISCSI_CONTEXT_FLAGS_B_IMMEDIATE_DATA;
1678 ictx->xstorm_st_context.iscsi.flags.flags |=
1679 XSTORM_ISCSI_CONTEXT_FLAGS_B_INITIAL_R2T;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001680 ictx->xstorm_st_context.common.ethernet.reserved_vlan_type =
1681 ETH_P_8021Q;
1682 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id) &&
1683 cp->port_mode == CHIP_2_PORT_MODE) {
1684
1685 port = 0;
1686 }
1687 ictx->xstorm_st_context.common.flags =
1688 1 << XSTORM_COMMON_CONTEXT_SECTION_PHYSQ_INITIALIZED_SHIFT;
1689 ictx->xstorm_st_context.common.flags =
1690 port << XSTORM_COMMON_CONTEXT_SECTION_PBF_PORT_SHIFT;
Michael Chan71034ba2009-10-10 13:46:59 +00001691
1692 ictx->tstorm_st_context.iscsi.hdr_bytes_2_fetch = ISCSI_HEADER_SIZE;
1693 /* TSTORM requires the base address of RQ DB & not PTE */
1694 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.lo =
1695 req2->rq_page_table_addr_lo & PAGE_MASK;
1696 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.hi =
1697 req2->rq_page_table_addr_hi;
1698 ictx->tstorm_st_context.iscsi.iscsi_conn_id = req1->iscsi_conn_id;
1699 ictx->tstorm_st_context.tcp.cwnd = 0x5A8;
1700 ictx->tstorm_st_context.tcp.flags2 |=
1701 TSTORM_TCP_ST_CONTEXT_SECTION_DA_EN;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001702 ictx->tstorm_st_context.tcp.ooo_support_mode =
1703 TCP_TSTORM_OOO_DROP_AND_PROC_ACK;
Michael Chan71034ba2009-10-10 13:46:59 +00001704
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001705 ictx->timers_context.flags |= TIMERS_BLOCK_CONTEXT_CONN_VALID_FLG;
Michael Chan71034ba2009-10-10 13:46:59 +00001706
1707 ictx->ustorm_st_context.ring.rq.pbl_base.lo =
Michael Chan15971c32009-12-02 15:15:38 +00001708 req2->rq_page_table_addr_lo;
Michael Chan71034ba2009-10-10 13:46:59 +00001709 ictx->ustorm_st_context.ring.rq.pbl_base.hi =
Michael Chan15971c32009-12-02 15:15:38 +00001710 req2->rq_page_table_addr_hi;
Michael Chan71034ba2009-10-10 13:46:59 +00001711 ictx->ustorm_st_context.ring.rq.curr_pbe.lo = req3->qp_first_pte[0].hi;
1712 ictx->ustorm_st_context.ring.rq.curr_pbe.hi = req3->qp_first_pte[0].lo;
1713 ictx->ustorm_st_context.ring.r2tq.pbl_base.lo =
1714 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1715 ictx->ustorm_st_context.ring.r2tq.pbl_base.hi =
1716 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1717 ictx->ustorm_st_context.ring.r2tq.curr_pbe.lo =
1718 iscsi->r2tq_info.pgtbl[0];
1719 ictx->ustorm_st_context.ring.r2tq.curr_pbe.hi =
1720 iscsi->r2tq_info.pgtbl[1];
1721 ictx->ustorm_st_context.ring.cq_pbl_base.lo =
1722 req1->cq_page_table_addr_lo;
1723 ictx->ustorm_st_context.ring.cq_pbl_base.hi =
1724 req1->cq_page_table_addr_hi;
1725 ictx->ustorm_st_context.ring.cq[0].cq_sn = ISCSI_INITIAL_SN;
1726 ictx->ustorm_st_context.ring.cq[0].curr_pbe.lo = req2->cq_first_pte.hi;
1727 ictx->ustorm_st_context.ring.cq[0].curr_pbe.hi = req2->cq_first_pte.lo;
1728 ictx->ustorm_st_context.task_pbe_cache_index =
1729 BNX2X_ISCSI_PBL_NOT_CACHED;
1730 ictx->ustorm_st_context.task_pdu_cache_index =
1731 BNX2X_ISCSI_PDU_HEADER_NOT_CACHED;
1732
1733 for (i = 1, j = 1; i < cp->num_cqs; i++, j++) {
1734 if (j == 3) {
1735 if (n >= n_max)
1736 break;
1737 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1738 j = 0;
1739 }
1740 ictx->ustorm_st_context.ring.cq[i].cq_sn = ISCSI_INITIAL_SN;
1741 ictx->ustorm_st_context.ring.cq[i].curr_pbe.lo =
1742 req3->qp_first_pte[j].hi;
1743 ictx->ustorm_st_context.ring.cq[i].curr_pbe.hi =
1744 req3->qp_first_pte[j].lo;
1745 }
1746
1747 ictx->ustorm_st_context.task_pbl_base.lo =
1748 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1749 ictx->ustorm_st_context.task_pbl_base.hi =
1750 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1751 ictx->ustorm_st_context.tce_phy_addr.lo =
1752 iscsi->task_array_info.pgtbl[0];
1753 ictx->ustorm_st_context.tce_phy_addr.hi =
1754 iscsi->task_array_info.pgtbl[1];
1755 ictx->ustorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1756 ictx->ustorm_st_context.num_cqs = cp->num_cqs;
1757 ictx->ustorm_st_context.negotiated_rx |= ISCSI_DEF_MAX_RECV_SEG_LEN;
1758 ictx->ustorm_st_context.negotiated_rx_and_flags |=
1759 ISCSI_DEF_MAX_BURST_LEN;
1760 ictx->ustorm_st_context.negotiated_rx |=
1761 ISCSI_DEFAULT_MAX_OUTSTANDING_R2T <<
1762 USTORM_ISCSI_ST_CONTEXT_MAX_OUTSTANDING_R2TS_SHIFT;
1763
1764 ictx->cstorm_st_context.hq_pbl_base.lo =
1765 iscsi->hq_info.pgtbl_map & 0xffffffff;
1766 ictx->cstorm_st_context.hq_pbl_base.hi =
1767 (u64) iscsi->hq_info.pgtbl_map >> 32;
1768 ictx->cstorm_st_context.hq_curr_pbe.lo = iscsi->hq_info.pgtbl[0];
1769 ictx->cstorm_st_context.hq_curr_pbe.hi = iscsi->hq_info.pgtbl[1];
1770 ictx->cstorm_st_context.task_pbl_base.lo =
1771 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1772 ictx->cstorm_st_context.task_pbl_base.hi =
1773 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1774 /* CSTORM and USTORM initialization is different, CSTORM requires
1775 * CQ DB base & not PTE addr */
1776 ictx->cstorm_st_context.cq_db_base.lo =
1777 req1->cq_page_table_addr_lo & PAGE_MASK;
1778 ictx->cstorm_st_context.cq_db_base.hi = req1->cq_page_table_addr_hi;
1779 ictx->cstorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1780 ictx->cstorm_st_context.cq_proc_en_bit_map = (1 << cp->num_cqs) - 1;
1781 for (i = 0; i < cp->num_cqs; i++) {
1782 ictx->cstorm_st_context.cq_c_prod_sqn_arr.sqn[i] =
1783 ISCSI_INITIAL_SN;
1784 ictx->cstorm_st_context.cq_c_sqn_2_notify_arr.sqn[i] =
1785 ISCSI_INITIAL_SN;
1786 }
1787
1788 ictx->xstorm_ag_context.cdu_reserved =
1789 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
1790 ISCSI_CONNECTION_TYPE);
1791 ictx->ustorm_ag_context.cdu_usage =
1792 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
1793 ISCSI_CONNECTION_TYPE);
1794 return 0;
1795
1796}
1797
1798static int cnic_bnx2x_iscsi_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
1799 u32 num, int *work)
1800{
1801 struct iscsi_kwqe_conn_offload1 *req1;
1802 struct iscsi_kwqe_conn_offload2 *req2;
1803 struct cnic_local *cp = dev->cnic_priv;
Michael Chanfdf24082010-10-13 14:06:47 +00001804 struct cnic_context *ctx;
Michael Chan71034ba2009-10-10 13:46:59 +00001805 struct iscsi_kcqe kcqe;
1806 struct kcqe *cqes[1];
1807 u32 l5_cid;
Michael Chanfdf24082010-10-13 14:06:47 +00001808 int ret = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001809
1810 if (num < 2) {
1811 *work = num;
1812 return -EINVAL;
1813 }
1814
1815 req1 = (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1816 req2 = (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1817 if ((num - 2) < req2->num_additional_wqes) {
1818 *work = num;
1819 return -EINVAL;
1820 }
Joe Perches779bb412010-11-14 17:04:37 +00001821 *work = 2 + req2->num_additional_wqes;
Michael Chan71034ba2009-10-10 13:46:59 +00001822
1823 l5_cid = req1->iscsi_conn_id;
1824 if (l5_cid >= MAX_ISCSI_TBL_SZ)
1825 return -EINVAL;
1826
1827 memset(&kcqe, 0, sizeof(kcqe));
1828 kcqe.op_code = ISCSI_KCQE_OPCODE_OFFLOAD_CONN;
1829 kcqe.iscsi_conn_id = l5_cid;
1830 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
1831
Michael Chanfdf24082010-10-13 14:06:47 +00001832 ctx = &cp->ctx_tbl[l5_cid];
1833 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags)) {
1834 kcqe.completion_status =
1835 ISCSI_KCQE_COMPLETION_STATUS_CID_BUSY;
1836 goto done;
1837 }
1838
Michael Chan71034ba2009-10-10 13:46:59 +00001839 if (atomic_inc_return(&cp->iscsi_conn) > dev->max_iscsi_conn) {
1840 atomic_dec(&cp->iscsi_conn);
Michael Chan71034ba2009-10-10 13:46:59 +00001841 goto done;
1842 }
1843 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
1844 if (ret) {
1845 atomic_dec(&cp->iscsi_conn);
1846 ret = 0;
1847 goto done;
1848 }
1849 ret = cnic_setup_bnx2x_ctx(dev, wqes, num);
1850 if (ret < 0) {
1851 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1852 atomic_dec(&cp->iscsi_conn);
1853 goto done;
1854 }
1855
1856 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001857 kcqe.iscsi_conn_context_id = BNX2X_HW_CID(cp, cp->ctx_tbl[l5_cid].cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001858
1859done:
1860 cqes[0] = (struct kcqe *) &kcqe;
1861 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
Michael Chan23021c22012-01-04 12:12:28 +00001862 return 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001863}
1864
1865
1866static int cnic_bnx2x_iscsi_update(struct cnic_dev *dev, struct kwqe *kwqe)
1867{
1868 struct cnic_local *cp = dev->cnic_priv;
1869 struct iscsi_kwqe_conn_update *req =
1870 (struct iscsi_kwqe_conn_update *) kwqe;
1871 void *data;
1872 union l5cm_specific_data l5_data;
1873 u32 l5_cid, cid = BNX2X_SW_CID(req->context_id);
1874 int ret;
1875
1876 if (cnic_get_l5_cid(cp, cid, &l5_cid) != 0)
1877 return -EINVAL;
1878
1879 data = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
1880 if (!data)
1881 return -ENOMEM;
1882
1883 memcpy(data, kwqe, sizeof(struct kwqe));
1884
1885 ret = cnic_submit_kwqe_16(dev, ISCSI_RAMROD_CMD_ID_UPDATE_CONN,
1886 req->context_id, ISCSI_CONNECTION_TYPE, &l5_data);
1887 return ret;
1888}
1889
Michael Chana2c9e762010-10-13 14:06:46 +00001890static int cnic_bnx2x_destroy_ramrod(struct cnic_dev *dev, u32 l5_cid)
Michael Chan71034ba2009-10-10 13:46:59 +00001891{
1892 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001893 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
Michael Chana2c9e762010-10-13 14:06:46 +00001894 union l5cm_specific_data l5_data;
1895 int ret;
Michael Chan68d7c1a2011-01-05 15:14:13 +00001896 u32 hw_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001897
Michael Chan71034ba2009-10-10 13:46:59 +00001898 init_waitqueue_head(&ctx->waitq);
1899 ctx->wait_cond = 0;
1900 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001901 hw_cid = BNX2X_HW_CID(cp, ctx->cid);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001902
1903 ret = cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
Michael Chan68d7c1a2011-01-05 15:14:13 +00001904 hw_cid, NONE_CONNECTION_TYPE, &l5_data);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001905
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001906 if (ret == 0) {
Michael Chandcc7e3a2011-08-26 09:45:40 +00001907 wait_event_timeout(ctx->waitq, ctx->wait_cond, CNIC_RAMROD_TMO);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001908 if (unlikely(test_bit(CTX_FL_CID_ERROR, &ctx->ctx_flags)))
1909 return -EBUSY;
1910 }
Michael Chan71034ba2009-10-10 13:46:59 +00001911
Michael Chandcc7e3a2011-08-26 09:45:40 +00001912 return 0;
Michael Chana2c9e762010-10-13 14:06:46 +00001913}
1914
1915static int cnic_bnx2x_iscsi_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
1916{
1917 struct cnic_local *cp = dev->cnic_priv;
1918 struct iscsi_kwqe_conn_destroy *req =
1919 (struct iscsi_kwqe_conn_destroy *) kwqe;
1920 u32 l5_cid = req->reserved0;
1921 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1922 int ret = 0;
1923 struct iscsi_kcqe kcqe;
1924 struct kcqe *cqes[1];
1925
1926 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
1927 goto skip_cfc_delete;
1928
Michael Chanfdf24082010-10-13 14:06:47 +00001929 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
1930 unsigned long delta = ctx->timestamp + (2 * HZ) - jiffies;
1931
1932 if (delta > (2 * HZ))
1933 delta = 0;
1934
1935 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
1936 queue_delayed_work(cnic_wq, &cp->delete_task, delta);
1937 goto destroy_reply;
1938 }
Michael Chana2c9e762010-10-13 14:06:46 +00001939
1940 ret = cnic_bnx2x_destroy_ramrod(dev, l5_cid);
1941
Michael Chan71034ba2009-10-10 13:46:59 +00001942skip_cfc_delete:
1943 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1944
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03001945 if (!ret) {
1946 atomic_dec(&cp->iscsi_conn);
1947 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
1948 }
Michael Chan71034ba2009-10-10 13:46:59 +00001949
Michael Chanfdf24082010-10-13 14:06:47 +00001950destroy_reply:
Michael Chan71034ba2009-10-10 13:46:59 +00001951 memset(&kcqe, 0, sizeof(kcqe));
1952 kcqe.op_code = ISCSI_KCQE_OPCODE_DESTROY_CONN;
1953 kcqe.iscsi_conn_id = l5_cid;
1954 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1955 kcqe.iscsi_conn_context_id = req->context_id;
1956
1957 cqes[0] = (struct kcqe *) &kcqe;
1958 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1959
Michael Chan23021c22012-01-04 12:12:28 +00001960 return 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001961}
1962
1963static void cnic_init_storm_conn_bufs(struct cnic_dev *dev,
1964 struct l4_kwq_connect_req1 *kwqe1,
1965 struct l4_kwq_connect_req3 *kwqe3,
1966 struct l5cm_active_conn_buffer *conn_buf)
1967{
1968 struct l5cm_conn_addr_params *conn_addr = &conn_buf->conn_addr_buf;
1969 struct l5cm_xstorm_conn_buffer *xstorm_buf =
1970 &conn_buf->xstorm_conn_buffer;
1971 struct l5cm_tstorm_conn_buffer *tstorm_buf =
1972 &conn_buf->tstorm_conn_buffer;
1973 struct regpair context_addr;
1974 u32 cid = BNX2X_SW_CID(kwqe1->cid);
1975 struct in6_addr src_ip, dst_ip;
1976 int i;
1977 u32 *addrp;
1978
1979 addrp = (u32 *) &conn_addr->local_ip_addr;
1980 for (i = 0; i < 4; i++, addrp++)
1981 src_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1982
1983 addrp = (u32 *) &conn_addr->remote_ip_addr;
1984 for (i = 0; i < 4; i++, addrp++)
1985 dst_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1986
1987 cnic_get_bnx2x_ctx(dev, cid, 0, &context_addr);
1988
1989 xstorm_buf->context_addr.hi = context_addr.hi;
1990 xstorm_buf->context_addr.lo = context_addr.lo;
1991 xstorm_buf->mss = 0xffff;
1992 xstorm_buf->rcv_buf = kwqe3->rcv_buf;
1993 if (kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE)
1994 xstorm_buf->params |= L5CM_XSTORM_CONN_BUFFER_NAGLE_ENABLE;
1995 xstorm_buf->pseudo_header_checksum =
1996 swab16(~csum_ipv6_magic(&src_ip, &dst_ip, 0, IPPROTO_TCP, 0));
1997
1998 if (!(kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK))
1999 tstorm_buf->params |=
2000 L5CM_TSTORM_CONN_BUFFER_DELAYED_ACK_ENABLE;
2001 if (kwqe3->ka_timeout) {
2002 tstorm_buf->ka_enable = 1;
2003 tstorm_buf->ka_timeout = kwqe3->ka_timeout;
2004 tstorm_buf->ka_interval = kwqe3->ka_interval;
2005 tstorm_buf->ka_max_probe_count = kwqe3->ka_max_probe_count;
2006 }
Michael Chan71034ba2009-10-10 13:46:59 +00002007 tstorm_buf->max_rt_time = 0xffffffff;
2008}
2009
2010static void cnic_init_bnx2x_mac(struct cnic_dev *dev)
2011{
2012 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00002013 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00002014 u8 *mac = dev->mac_addr;
2015
2016 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002017 XSTORM_ISCSI_LOCAL_MAC_ADDR0_OFFSET(pfid), mac[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00002018 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002019 XSTORM_ISCSI_LOCAL_MAC_ADDR1_OFFSET(pfid), mac[1]);
Michael Chan71034ba2009-10-10 13:46:59 +00002020 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002021 XSTORM_ISCSI_LOCAL_MAC_ADDR2_OFFSET(pfid), mac[2]);
Michael Chan71034ba2009-10-10 13:46:59 +00002022 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002023 XSTORM_ISCSI_LOCAL_MAC_ADDR3_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00002024 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002025 XSTORM_ISCSI_LOCAL_MAC_ADDR4_OFFSET(pfid), mac[4]);
Michael Chan71034ba2009-10-10 13:46:59 +00002026 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002027 XSTORM_ISCSI_LOCAL_MAC_ADDR5_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00002028
2029 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002030 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00002031 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002032 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00002033 mac[4]);
2034 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002035 TSTORM_ISCSI_TCP_VARS_MID_LOCAL_MAC_ADDR_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00002036 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002037 TSTORM_ISCSI_TCP_VARS_MID_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00002038 mac[2]);
2039 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002040 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[1]);
Michael Chan71034ba2009-10-10 13:46:59 +00002041 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002042 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00002043 mac[0]);
2044}
2045
2046static void cnic_bnx2x_set_tcp_timestamp(struct cnic_dev *dev, int tcp_ts)
2047{
2048 struct cnic_local *cp = dev->cnic_priv;
2049 u8 xstorm_flags = XSTORM_L5CM_TCP_FLAGS_WND_SCL_EN;
2050 u16 tstorm_flags = 0;
2051
2052 if (tcp_ts) {
2053 xstorm_flags |= XSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
2054 tstorm_flags |= TSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
2055 }
2056
2057 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002058 XSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), xstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002059
2060 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002061 TSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), tstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002062}
2063
2064static int cnic_bnx2x_connect(struct cnic_dev *dev, struct kwqe *wqes[],
2065 u32 num, int *work)
2066{
2067 struct cnic_local *cp = dev->cnic_priv;
2068 struct l4_kwq_connect_req1 *kwqe1 =
2069 (struct l4_kwq_connect_req1 *) wqes[0];
2070 struct l4_kwq_connect_req3 *kwqe3;
2071 struct l5cm_active_conn_buffer *conn_buf;
2072 struct l5cm_conn_addr_params *conn_addr;
2073 union l5cm_specific_data l5_data;
2074 u32 l5_cid = kwqe1->pg_cid;
2075 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
2076 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
2077 int ret;
2078
2079 if (num < 2) {
2080 *work = num;
2081 return -EINVAL;
2082 }
2083
2084 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6)
2085 *work = 3;
2086 else
2087 *work = 2;
2088
2089 if (num < *work) {
2090 *work = num;
2091 return -EINVAL;
2092 }
2093
2094 if (sizeof(*conn_buf) > CNIC_KWQ16_DATA_SIZE) {
Joe Perchesddf79b22010-02-17 15:01:54 +00002095 netdev_err(dev->netdev, "conn_buf size too big\n");
Michael Chan71034ba2009-10-10 13:46:59 +00002096 return -ENOMEM;
2097 }
2098 conn_buf = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2099 if (!conn_buf)
2100 return -ENOMEM;
2101
2102 memset(conn_buf, 0, sizeof(*conn_buf));
2103
2104 conn_addr = &conn_buf->conn_addr_buf;
2105 conn_addr->remote_addr_0 = csk->ha[0];
2106 conn_addr->remote_addr_1 = csk->ha[1];
2107 conn_addr->remote_addr_2 = csk->ha[2];
2108 conn_addr->remote_addr_3 = csk->ha[3];
2109 conn_addr->remote_addr_4 = csk->ha[4];
2110 conn_addr->remote_addr_5 = csk->ha[5];
2111
2112 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6) {
2113 struct l4_kwq_connect_req2 *kwqe2 =
2114 (struct l4_kwq_connect_req2 *) wqes[1];
2115
2116 conn_addr->local_ip_addr.ip_addr_hi_hi = kwqe2->src_ip_v6_4;
2117 conn_addr->local_ip_addr.ip_addr_hi_lo = kwqe2->src_ip_v6_3;
2118 conn_addr->local_ip_addr.ip_addr_lo_hi = kwqe2->src_ip_v6_2;
2119
2120 conn_addr->remote_ip_addr.ip_addr_hi_hi = kwqe2->dst_ip_v6_4;
2121 conn_addr->remote_ip_addr.ip_addr_hi_lo = kwqe2->dst_ip_v6_3;
2122 conn_addr->remote_ip_addr.ip_addr_lo_hi = kwqe2->dst_ip_v6_2;
2123 conn_addr->params |= L5CM_CONN_ADDR_PARAMS_IP_VERSION;
2124 }
2125 kwqe3 = (struct l4_kwq_connect_req3 *) wqes[*work - 1];
2126
2127 conn_addr->local_ip_addr.ip_addr_lo_lo = kwqe1->src_ip;
2128 conn_addr->remote_ip_addr.ip_addr_lo_lo = kwqe1->dst_ip;
2129 conn_addr->local_tcp_port = kwqe1->src_port;
2130 conn_addr->remote_tcp_port = kwqe1->dst_port;
2131
2132 conn_addr->pmtu = kwqe3->pmtu;
2133 cnic_init_storm_conn_bufs(dev, kwqe1, kwqe3, conn_buf);
2134
2135 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002136 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(cp->pfid), csk->vlan_id);
Michael Chan71034ba2009-10-10 13:46:59 +00002137
2138 cnic_bnx2x_set_tcp_timestamp(dev,
2139 kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_TIME_STAMP);
2140
2141 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_TCP_CONNECT,
2142 kwqe1->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2143 if (!ret)
Michael Chan6e0dda02010-10-13 14:06:45 +00002144 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002145
2146 return ret;
2147}
2148
2149static int cnic_bnx2x_close(struct cnic_dev *dev, struct kwqe *kwqe)
2150{
2151 struct l4_kwq_close_req *req = (struct l4_kwq_close_req *) kwqe;
2152 union l5cm_specific_data l5_data;
2153 int ret;
2154
2155 memset(&l5_data, 0, sizeof(l5_data));
2156 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_CLOSE,
2157 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2158 return ret;
2159}
2160
2161static int cnic_bnx2x_reset(struct cnic_dev *dev, struct kwqe *kwqe)
2162{
2163 struct l4_kwq_reset_req *req = (struct l4_kwq_reset_req *) kwqe;
2164 union l5cm_specific_data l5_data;
2165 int ret;
2166
2167 memset(&l5_data, 0, sizeof(l5_data));
2168 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_ABORT,
2169 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2170 return ret;
2171}
2172static int cnic_bnx2x_offload_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2173{
2174 struct l4_kwq_offload_pg *req = (struct l4_kwq_offload_pg *) kwqe;
2175 struct l4_kcq kcqe;
2176 struct kcqe *cqes[1];
2177
2178 memset(&kcqe, 0, sizeof(kcqe));
2179 kcqe.pg_host_opaque = req->host_opaque;
2180 kcqe.pg_cid = req->host_opaque;
2181 kcqe.op_code = L4_KCQE_OPCODE_VALUE_OFFLOAD_PG;
2182 cqes[0] = (struct kcqe *) &kcqe;
2183 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2184 return 0;
2185}
2186
2187static int cnic_bnx2x_update_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2188{
2189 struct l4_kwq_update_pg *req = (struct l4_kwq_update_pg *) kwqe;
2190 struct l4_kcq kcqe;
2191 struct kcqe *cqes[1];
2192
2193 memset(&kcqe, 0, sizeof(kcqe));
2194 kcqe.pg_host_opaque = req->pg_host_opaque;
2195 kcqe.pg_cid = req->pg_cid;
2196 kcqe.op_code = L4_KCQE_OPCODE_VALUE_UPDATE_PG;
2197 cqes[0] = (struct kcqe *) &kcqe;
2198 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2199 return 0;
2200}
2201
Michael Chane1928c82010-12-23 07:43:04 +00002202static int cnic_bnx2x_fcoe_stat(struct cnic_dev *dev, struct kwqe *kwqe)
2203{
2204 struct fcoe_kwqe_stat *req;
2205 struct fcoe_stat_ramrod_params *fcoe_stat;
2206 union l5cm_specific_data l5_data;
2207 struct cnic_local *cp = dev->cnic_priv;
2208 int ret;
2209 u32 cid;
2210
2211 req = (struct fcoe_kwqe_stat *) kwqe;
2212 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2213
2214 fcoe_stat = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2215 if (!fcoe_stat)
2216 return -ENOMEM;
2217
2218 memset(fcoe_stat, 0, sizeof(*fcoe_stat));
2219 memcpy(&fcoe_stat->stat_kwqe, req, sizeof(*req));
2220
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002221 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_STAT_FUNC, cid,
Michael Chane1928c82010-12-23 07:43:04 +00002222 FCOE_CONNECTION_TYPE, &l5_data);
2223 return ret;
2224}
2225
2226static int cnic_bnx2x_fcoe_init1(struct cnic_dev *dev, struct kwqe *wqes[],
2227 u32 num, int *work)
2228{
2229 int ret;
2230 struct cnic_local *cp = dev->cnic_priv;
2231 u32 cid;
2232 struct fcoe_init_ramrod_params *fcoe_init;
2233 struct fcoe_kwqe_init1 *req1;
2234 struct fcoe_kwqe_init2 *req2;
2235 struct fcoe_kwqe_init3 *req3;
2236 union l5cm_specific_data l5_data;
2237
2238 if (num < 3) {
2239 *work = num;
2240 return -EINVAL;
2241 }
2242 req1 = (struct fcoe_kwqe_init1 *) wqes[0];
2243 req2 = (struct fcoe_kwqe_init2 *) wqes[1];
2244 req3 = (struct fcoe_kwqe_init3 *) wqes[2];
2245 if (req2->hdr.op_code != FCOE_KWQE_OPCODE_INIT2) {
2246 *work = 1;
2247 return -EINVAL;
2248 }
2249 if (req3->hdr.op_code != FCOE_KWQE_OPCODE_INIT3) {
2250 *work = 2;
2251 return -EINVAL;
2252 }
2253
2254 if (sizeof(*fcoe_init) > CNIC_KWQ16_DATA_SIZE) {
2255 netdev_err(dev->netdev, "fcoe_init size too big\n");
2256 return -ENOMEM;
2257 }
2258 fcoe_init = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2259 if (!fcoe_init)
2260 return -ENOMEM;
2261
2262 memset(fcoe_init, 0, sizeof(*fcoe_init));
2263 memcpy(&fcoe_init->init_kwqe1, req1, sizeof(*req1));
2264 memcpy(&fcoe_init->init_kwqe2, req2, sizeof(*req2));
2265 memcpy(&fcoe_init->init_kwqe3, req3, sizeof(*req3));
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002266 fcoe_init->eq_pbl_base.lo = cp->kcq2.dma.pgtbl_map & 0xffffffff;
2267 fcoe_init->eq_pbl_base.hi = (u64) cp->kcq2.dma.pgtbl_map >> 32;
2268 fcoe_init->eq_pbl_size = cp->kcq2.dma.num_pages;
Michael Chane1928c82010-12-23 07:43:04 +00002269
2270 fcoe_init->sb_num = cp->status_blk_num;
2271 fcoe_init->eq_prod = MAX_KCQ_IDX;
2272 fcoe_init->sb_id = HC_INDEX_FCOE_EQ_CONS;
2273 cp->kcq2.sw_prod_idx = 0;
2274
2275 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002276 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_INIT_FUNC, cid,
Michael Chane1928c82010-12-23 07:43:04 +00002277 FCOE_CONNECTION_TYPE, &l5_data);
2278 *work = 3;
2279 return ret;
2280}
2281
2282static int cnic_bnx2x_fcoe_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
2283 u32 num, int *work)
2284{
2285 int ret = 0;
2286 u32 cid = -1, l5_cid;
2287 struct cnic_local *cp = dev->cnic_priv;
2288 struct fcoe_kwqe_conn_offload1 *req1;
2289 struct fcoe_kwqe_conn_offload2 *req2;
2290 struct fcoe_kwqe_conn_offload3 *req3;
2291 struct fcoe_kwqe_conn_offload4 *req4;
2292 struct fcoe_conn_offload_ramrod_params *fcoe_offload;
2293 struct cnic_context *ctx;
2294 struct fcoe_context *fctx;
2295 struct regpair ctx_addr;
2296 union l5cm_specific_data l5_data;
2297 struct fcoe_kcqe kcqe;
2298 struct kcqe *cqes[1];
2299
2300 if (num < 4) {
2301 *work = num;
2302 return -EINVAL;
2303 }
2304 req1 = (struct fcoe_kwqe_conn_offload1 *) wqes[0];
2305 req2 = (struct fcoe_kwqe_conn_offload2 *) wqes[1];
2306 req3 = (struct fcoe_kwqe_conn_offload3 *) wqes[2];
2307 req4 = (struct fcoe_kwqe_conn_offload4 *) wqes[3];
2308
2309 *work = 4;
2310
2311 l5_cid = req1->fcoe_conn_id;
Michael Chandc219a22011-08-26 09:45:39 +00002312 if (l5_cid >= dev->max_fcoe_conn)
Michael Chane1928c82010-12-23 07:43:04 +00002313 goto err_reply;
2314
2315 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2316
2317 ctx = &cp->ctx_tbl[l5_cid];
2318 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2319 goto err_reply;
2320
2321 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
2322 if (ret) {
2323 ret = 0;
2324 goto err_reply;
2325 }
2326 cid = ctx->cid;
2327
2328 fctx = cnic_get_bnx2x_ctx(dev, cid, 1, &ctx_addr);
2329 if (fctx) {
2330 u32 hw_cid = BNX2X_HW_CID(cp, cid);
2331 u32 val;
2332
2333 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
2334 FCOE_CONNECTION_TYPE);
2335 fctx->xstorm_ag_context.cdu_reserved = val;
2336 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
2337 FCOE_CONNECTION_TYPE);
2338 fctx->ustorm_ag_context.cdu_usage = val;
2339 }
2340 if (sizeof(*fcoe_offload) > CNIC_KWQ16_DATA_SIZE) {
2341 netdev_err(dev->netdev, "fcoe_offload size too big\n");
2342 goto err_reply;
2343 }
2344 fcoe_offload = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2345 if (!fcoe_offload)
2346 goto err_reply;
2347
2348 memset(fcoe_offload, 0, sizeof(*fcoe_offload));
2349 memcpy(&fcoe_offload->offload_kwqe1, req1, sizeof(*req1));
2350 memcpy(&fcoe_offload->offload_kwqe2, req2, sizeof(*req2));
2351 memcpy(&fcoe_offload->offload_kwqe3, req3, sizeof(*req3));
2352 memcpy(&fcoe_offload->offload_kwqe4, req4, sizeof(*req4));
2353
2354 cid = BNX2X_HW_CID(cp, cid);
2355 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_OFFLOAD_CONN, cid,
2356 FCOE_CONNECTION_TYPE, &l5_data);
2357 if (!ret)
2358 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
2359
2360 return ret;
2361
2362err_reply:
2363 if (cid != -1)
2364 cnic_free_bnx2x_conn_resc(dev, l5_cid);
2365
2366 memset(&kcqe, 0, sizeof(kcqe));
2367 kcqe.op_code = FCOE_KCQE_OPCODE_OFFLOAD_CONN;
2368 kcqe.fcoe_conn_id = req1->fcoe_conn_id;
2369 kcqe.completion_status = FCOE_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
2370
2371 cqes[0] = (struct kcqe *) &kcqe;
2372 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2373 return ret;
2374}
2375
2376static int cnic_bnx2x_fcoe_enable(struct cnic_dev *dev, struct kwqe *kwqe)
2377{
2378 struct fcoe_kwqe_conn_enable_disable *req;
2379 struct fcoe_conn_enable_disable_ramrod_params *fcoe_enable;
2380 union l5cm_specific_data l5_data;
2381 int ret;
2382 u32 cid, l5_cid;
2383 struct cnic_local *cp = dev->cnic_priv;
2384
2385 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2386 cid = req->context_id;
2387 l5_cid = req->conn_id + BNX2X_FCOE_L5_CID_BASE;
2388
2389 if (sizeof(*fcoe_enable) > CNIC_KWQ16_DATA_SIZE) {
2390 netdev_err(dev->netdev, "fcoe_enable size too big\n");
2391 return -ENOMEM;
2392 }
2393 fcoe_enable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2394 if (!fcoe_enable)
2395 return -ENOMEM;
2396
2397 memset(fcoe_enable, 0, sizeof(*fcoe_enable));
2398 memcpy(&fcoe_enable->enable_disable_kwqe, req, sizeof(*req));
2399 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_ENABLE_CONN, cid,
2400 FCOE_CONNECTION_TYPE, &l5_data);
2401 return ret;
2402}
2403
2404static int cnic_bnx2x_fcoe_disable(struct cnic_dev *dev, struct kwqe *kwqe)
2405{
2406 struct fcoe_kwqe_conn_enable_disable *req;
2407 struct fcoe_conn_enable_disable_ramrod_params *fcoe_disable;
2408 union l5cm_specific_data l5_data;
2409 int ret;
2410 u32 cid, l5_cid;
2411 struct cnic_local *cp = dev->cnic_priv;
2412
2413 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2414 cid = req->context_id;
2415 l5_cid = req->conn_id;
Michael Chandc219a22011-08-26 09:45:39 +00002416 if (l5_cid >= dev->max_fcoe_conn)
Michael Chane1928c82010-12-23 07:43:04 +00002417 return -EINVAL;
2418
2419 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2420
2421 if (sizeof(*fcoe_disable) > CNIC_KWQ16_DATA_SIZE) {
2422 netdev_err(dev->netdev, "fcoe_disable size too big\n");
2423 return -ENOMEM;
2424 }
2425 fcoe_disable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2426 if (!fcoe_disable)
2427 return -ENOMEM;
2428
2429 memset(fcoe_disable, 0, sizeof(*fcoe_disable));
2430 memcpy(&fcoe_disable->enable_disable_kwqe, req, sizeof(*req));
2431 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DISABLE_CONN, cid,
2432 FCOE_CONNECTION_TYPE, &l5_data);
2433 return ret;
2434}
2435
2436static int cnic_bnx2x_fcoe_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2437{
2438 struct fcoe_kwqe_conn_destroy *req;
2439 union l5cm_specific_data l5_data;
2440 int ret;
2441 u32 cid, l5_cid;
2442 struct cnic_local *cp = dev->cnic_priv;
2443 struct cnic_context *ctx;
2444 struct fcoe_kcqe kcqe;
2445 struct kcqe *cqes[1];
2446
2447 req = (struct fcoe_kwqe_conn_destroy *) kwqe;
2448 cid = req->context_id;
2449 l5_cid = req->conn_id;
Michael Chandc219a22011-08-26 09:45:39 +00002450 if (l5_cid >= dev->max_fcoe_conn)
Michael Chane1928c82010-12-23 07:43:04 +00002451 return -EINVAL;
2452
2453 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2454
2455 ctx = &cp->ctx_tbl[l5_cid];
2456
2457 init_waitqueue_head(&ctx->waitq);
2458 ctx->wait_cond = 0;
2459
Michael Chandcc7e3a2011-08-26 09:45:40 +00002460 memset(&kcqe, 0, sizeof(kcqe));
2461 kcqe.completion_status = FCOE_KCQE_COMPLETION_STATUS_ERROR;
Michael Chane1928c82010-12-23 07:43:04 +00002462 memset(&l5_data, 0, sizeof(l5_data));
2463 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_TERMINATE_CONN, cid,
2464 FCOE_CONNECTION_TYPE, &l5_data);
2465 if (ret == 0) {
Michael Chandcc7e3a2011-08-26 09:45:40 +00002466 wait_event_timeout(ctx->waitq, ctx->wait_cond, CNIC_RAMROD_TMO);
2467 if (ctx->wait_cond)
2468 kcqe.completion_status = 0;
Michael Chane1928c82010-12-23 07:43:04 +00002469 }
2470
Michael Chandcc7e3a2011-08-26 09:45:40 +00002471 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
2472 queue_delayed_work(cnic_wq, &cp->delete_task, msecs_to_jiffies(2000));
2473
Michael Chane1928c82010-12-23 07:43:04 +00002474 kcqe.op_code = FCOE_KCQE_OPCODE_DESTROY_CONN;
2475 kcqe.fcoe_conn_id = req->conn_id;
2476 kcqe.fcoe_conn_context_id = cid;
2477
2478 cqes[0] = (struct kcqe *) &kcqe;
2479 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2480 return ret;
2481}
2482
Michael Chan74e49bb2011-07-20 14:55:23 +00002483static void cnic_bnx2x_delete_wait(struct cnic_dev *dev, u32 start_cid)
2484{
2485 struct cnic_local *cp = dev->cnic_priv;
2486 u32 i;
2487
2488 for (i = start_cid; i < cp->max_cid_space; i++) {
2489 struct cnic_context *ctx = &cp->ctx_tbl[i];
2490 int j;
2491
2492 while (test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
2493 msleep(10);
2494
2495 for (j = 0; j < 5; j++) {
2496 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2497 break;
2498 msleep(20);
2499 }
2500
2501 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2502 netdev_warn(dev->netdev, "CID %x not deleted\n",
2503 ctx->cid);
2504 }
2505}
2506
Michael Chane1928c82010-12-23 07:43:04 +00002507static int cnic_bnx2x_fcoe_fw_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2508{
2509 struct fcoe_kwqe_destroy *req;
2510 union l5cm_specific_data l5_data;
2511 struct cnic_local *cp = dev->cnic_priv;
2512 int ret;
2513 u32 cid;
2514
Michael Chan74e49bb2011-07-20 14:55:23 +00002515 cnic_bnx2x_delete_wait(dev, MAX_ISCSI_TBL_SZ);
2516
Michael Chane1928c82010-12-23 07:43:04 +00002517 req = (struct fcoe_kwqe_destroy *) kwqe;
2518 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2519
2520 memset(&l5_data, 0, sizeof(l5_data));
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002521 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DESTROY_FUNC, cid,
Michael Chane1928c82010-12-23 07:43:04 +00002522 FCOE_CONNECTION_TYPE, &l5_data);
2523 return ret;
2524}
2525
Michael Chan23021c22012-01-04 12:12:28 +00002526static void cnic_bnx2x_kwqe_err(struct cnic_dev *dev, struct kwqe *kwqe)
2527{
2528 struct cnic_local *cp = dev->cnic_priv;
2529 struct kcqe kcqe;
2530 struct kcqe *cqes[1];
2531 u32 cid;
2532 u32 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2533 u32 layer_code = kwqe->kwqe_op_flag & KWQE_LAYER_MASK;
Michael Chan3238a9b2012-02-05 15:24:40 +00002534 u32 kcqe_op;
Michael Chan23021c22012-01-04 12:12:28 +00002535 int ulp_type;
2536
2537 cid = kwqe->kwqe_info0;
2538 memset(&kcqe, 0, sizeof(kcqe));
2539
Michael Chan3238a9b2012-02-05 15:24:40 +00002540 if (layer_code == KWQE_FLAGS_LAYER_MASK_L5_FCOE) {
2541 u32 l5_cid = 0;
2542
2543 ulp_type = CNIC_ULP_FCOE;
2544 if (opcode == FCOE_KWQE_OPCODE_DISABLE_CONN) {
2545 struct fcoe_kwqe_conn_enable_disable *req;
2546
2547 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2548 kcqe_op = FCOE_KCQE_OPCODE_DISABLE_CONN;
2549 cid = req->context_id;
2550 l5_cid = req->conn_id;
2551 } else if (opcode == FCOE_KWQE_OPCODE_DESTROY) {
2552 kcqe_op = FCOE_KCQE_OPCODE_DESTROY_FUNC;
2553 } else {
2554 return;
2555 }
2556 kcqe.kcqe_op_flag = kcqe_op << KCQE_FLAGS_OPCODE_SHIFT;
2557 kcqe.kcqe_op_flag |= KCQE_FLAGS_LAYER_MASK_L5_FCOE;
Michael Chan8ec3e702012-03-21 15:38:34 +00002558 kcqe.kcqe_info1 = FCOE_KCQE_COMPLETION_STATUS_PARITY_ERROR;
Michael Chan3238a9b2012-02-05 15:24:40 +00002559 kcqe.kcqe_info2 = cid;
2560 kcqe.kcqe_info0 = l5_cid;
2561
2562 } else if (layer_code == KWQE_FLAGS_LAYER_MASK_L5_ISCSI) {
Michael Chan23021c22012-01-04 12:12:28 +00002563 ulp_type = CNIC_ULP_ISCSI;
2564 if (opcode == ISCSI_KWQE_OPCODE_UPDATE_CONN)
2565 cid = kwqe->kwqe_info1;
2566
2567 kcqe.kcqe_op_flag = (opcode + 0x10) << KCQE_FLAGS_OPCODE_SHIFT;
2568 kcqe.kcqe_op_flag |= KCQE_FLAGS_LAYER_MASK_L5_ISCSI;
Michael Chan8ec3e702012-03-21 15:38:34 +00002569 kcqe.kcqe_info1 = ISCSI_KCQE_COMPLETION_STATUS_PARITY_ERR;
Michael Chan23021c22012-01-04 12:12:28 +00002570 kcqe.kcqe_info2 = cid;
2571 cnic_get_l5_cid(cp, BNX2X_SW_CID(cid), &kcqe.kcqe_info0);
2572
2573 } else if (layer_code == KWQE_FLAGS_LAYER_MASK_L4) {
2574 struct l4_kcq *l4kcqe = (struct l4_kcq *) &kcqe;
Michael Chan23021c22012-01-04 12:12:28 +00002575
2576 ulp_type = CNIC_ULP_L4;
2577 if (opcode == L4_KWQE_OPCODE_VALUE_CONNECT1)
2578 kcqe_op = L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE;
2579 else if (opcode == L4_KWQE_OPCODE_VALUE_RESET)
2580 kcqe_op = L4_KCQE_OPCODE_VALUE_RESET_COMP;
2581 else if (opcode == L4_KWQE_OPCODE_VALUE_CLOSE)
2582 kcqe_op = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
2583 else
2584 return;
2585
2586 kcqe.kcqe_op_flag = (kcqe_op << KCQE_FLAGS_OPCODE_SHIFT) |
2587 KCQE_FLAGS_LAYER_MASK_L4;
Michael Chan8ec3e702012-03-21 15:38:34 +00002588 l4kcqe->status = L4_KCQE_COMPLETION_STATUS_PARITY_ERROR;
Michael Chan23021c22012-01-04 12:12:28 +00002589 l4kcqe->cid = cid;
2590 cnic_get_l5_cid(cp, BNX2X_SW_CID(cid), &l4kcqe->conn_id);
2591 } else {
2592 return;
2593 }
2594
Joe Perches64699332012-06-04 12:44:16 +00002595 cqes[0] = &kcqe;
Michael Chan23021c22012-01-04 12:12:28 +00002596 cnic_reply_bnx2x_kcqes(dev, ulp_type, cqes, 1);
2597}
2598
Michael Chane1928c82010-12-23 07:43:04 +00002599static int cnic_submit_bnx2x_iscsi_kwqes(struct cnic_dev *dev,
2600 struct kwqe *wqes[], u32 num_wqes)
Michael Chan71034ba2009-10-10 13:46:59 +00002601{
2602 int i, work, ret;
2603 u32 opcode;
2604 struct kwqe *kwqe;
2605
2606 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2607 return -EAGAIN; /* bnx2 is down */
2608
2609 for (i = 0; i < num_wqes; ) {
2610 kwqe = wqes[i];
2611 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2612 work = 1;
2613
2614 switch (opcode) {
2615 case ISCSI_KWQE_OPCODE_INIT1:
2616 ret = cnic_bnx2x_iscsi_init1(dev, kwqe);
2617 break;
2618 case ISCSI_KWQE_OPCODE_INIT2:
2619 ret = cnic_bnx2x_iscsi_init2(dev, kwqe);
2620 break;
2621 case ISCSI_KWQE_OPCODE_OFFLOAD_CONN1:
2622 ret = cnic_bnx2x_iscsi_ofld1(dev, &wqes[i],
2623 num_wqes - i, &work);
2624 break;
2625 case ISCSI_KWQE_OPCODE_UPDATE_CONN:
2626 ret = cnic_bnx2x_iscsi_update(dev, kwqe);
2627 break;
2628 case ISCSI_KWQE_OPCODE_DESTROY_CONN:
2629 ret = cnic_bnx2x_iscsi_destroy(dev, kwqe);
2630 break;
2631 case L4_KWQE_OPCODE_VALUE_CONNECT1:
2632 ret = cnic_bnx2x_connect(dev, &wqes[i], num_wqes - i,
2633 &work);
2634 break;
2635 case L4_KWQE_OPCODE_VALUE_CLOSE:
2636 ret = cnic_bnx2x_close(dev, kwqe);
2637 break;
2638 case L4_KWQE_OPCODE_VALUE_RESET:
2639 ret = cnic_bnx2x_reset(dev, kwqe);
2640 break;
2641 case L4_KWQE_OPCODE_VALUE_OFFLOAD_PG:
2642 ret = cnic_bnx2x_offload_pg(dev, kwqe);
2643 break;
2644 case L4_KWQE_OPCODE_VALUE_UPDATE_PG:
2645 ret = cnic_bnx2x_update_pg(dev, kwqe);
2646 break;
2647 case L4_KWQE_OPCODE_VALUE_UPLOAD_PG:
2648 ret = 0;
2649 break;
2650 default:
2651 ret = 0;
Joe Perchesddf79b22010-02-17 15:01:54 +00002652 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2653 opcode);
Michael Chan71034ba2009-10-10 13:46:59 +00002654 break;
2655 }
Michael Chan23021c22012-01-04 12:12:28 +00002656 if (ret < 0) {
Joe Perchesddf79b22010-02-17 15:01:54 +00002657 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2658 opcode);
Michael Chan23021c22012-01-04 12:12:28 +00002659
2660 /* Possibly bnx2x parity error, send completion
2661 * to ulp drivers with error code to speed up
2662 * cleanup and reset recovery.
2663 */
2664 if (ret == -EIO || ret == -EAGAIN)
2665 cnic_bnx2x_kwqe_err(dev, kwqe);
2666 }
Michael Chan71034ba2009-10-10 13:46:59 +00002667 i += work;
2668 }
2669 return 0;
2670}
2671
Michael Chane1928c82010-12-23 07:43:04 +00002672static int cnic_submit_bnx2x_fcoe_kwqes(struct cnic_dev *dev,
2673 struct kwqe *wqes[], u32 num_wqes)
2674{
2675 struct cnic_local *cp = dev->cnic_priv;
2676 int i, work, ret;
2677 u32 opcode;
2678 struct kwqe *kwqe;
2679
2680 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2681 return -EAGAIN; /* bnx2 is down */
2682
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03002683 if (!BNX2X_CHIP_IS_E2_PLUS(cp->chip_id))
Michael Chane1928c82010-12-23 07:43:04 +00002684 return -EINVAL;
2685
2686 for (i = 0; i < num_wqes; ) {
2687 kwqe = wqes[i];
2688 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2689 work = 1;
2690
2691 switch (opcode) {
2692 case FCOE_KWQE_OPCODE_INIT1:
2693 ret = cnic_bnx2x_fcoe_init1(dev, &wqes[i],
2694 num_wqes - i, &work);
2695 break;
2696 case FCOE_KWQE_OPCODE_OFFLOAD_CONN1:
2697 ret = cnic_bnx2x_fcoe_ofld1(dev, &wqes[i],
2698 num_wqes - i, &work);
2699 break;
2700 case FCOE_KWQE_OPCODE_ENABLE_CONN:
2701 ret = cnic_bnx2x_fcoe_enable(dev, kwqe);
2702 break;
2703 case FCOE_KWQE_OPCODE_DISABLE_CONN:
2704 ret = cnic_bnx2x_fcoe_disable(dev, kwqe);
2705 break;
2706 case FCOE_KWQE_OPCODE_DESTROY_CONN:
2707 ret = cnic_bnx2x_fcoe_destroy(dev, kwqe);
2708 break;
2709 case FCOE_KWQE_OPCODE_DESTROY:
2710 ret = cnic_bnx2x_fcoe_fw_destroy(dev, kwqe);
2711 break;
2712 case FCOE_KWQE_OPCODE_STAT:
2713 ret = cnic_bnx2x_fcoe_stat(dev, kwqe);
2714 break;
2715 default:
2716 ret = 0;
2717 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2718 opcode);
2719 break;
2720 }
Michael Chan3238a9b2012-02-05 15:24:40 +00002721 if (ret < 0) {
Michael Chane1928c82010-12-23 07:43:04 +00002722 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2723 opcode);
Michael Chan3238a9b2012-02-05 15:24:40 +00002724
2725 /* Possibly bnx2x parity error, send completion
2726 * to ulp drivers with error code to speed up
2727 * cleanup and reset recovery.
2728 */
2729 if (ret == -EIO || ret == -EAGAIN)
2730 cnic_bnx2x_kwqe_err(dev, kwqe);
2731 }
Michael Chane1928c82010-12-23 07:43:04 +00002732 i += work;
2733 }
2734 return 0;
2735}
2736
2737static int cnic_submit_bnx2x_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
2738 u32 num_wqes)
2739{
2740 int ret = -EINVAL;
2741 u32 layer_code;
2742
2743 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2744 return -EAGAIN; /* bnx2x is down */
2745
2746 if (!num_wqes)
2747 return 0;
2748
2749 layer_code = wqes[0]->kwqe_op_flag & KWQE_LAYER_MASK;
2750 switch (layer_code) {
2751 case KWQE_FLAGS_LAYER_MASK_L5_ISCSI:
2752 case KWQE_FLAGS_LAYER_MASK_L4:
2753 case KWQE_FLAGS_LAYER_MASK_L2:
2754 ret = cnic_submit_bnx2x_iscsi_kwqes(dev, wqes, num_wqes);
2755 break;
2756
2757 case KWQE_FLAGS_LAYER_MASK_L5_FCOE:
2758 ret = cnic_submit_bnx2x_fcoe_kwqes(dev, wqes, num_wqes);
2759 break;
2760 }
2761 return ret;
2762}
2763
2764static inline u32 cnic_get_kcqe_layer_mask(u32 opflag)
2765{
2766 if (unlikely(KCQE_OPCODE(opflag) == FCOE_RAMROD_CMD_ID_TERMINATE_CONN))
2767 return KCQE_FLAGS_LAYER_MASK_L4;
2768
2769 return opflag & KCQE_FLAGS_LAYER_MASK;
2770}
2771
Michael Chana4636962009-06-08 18:14:43 -07002772static void service_kcqes(struct cnic_dev *dev, int num_cqes)
2773{
2774 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002775 int i, j, comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002776
2777 i = 0;
2778 j = 1;
2779 while (num_cqes) {
2780 struct cnic_ulp_ops *ulp_ops;
2781 int ulp_type;
2782 u32 kcqe_op_flag = cp->completed_kcq[i]->kcqe_op_flag;
Michael Chane1928c82010-12-23 07:43:04 +00002783 u32 kcqe_layer = cnic_get_kcqe_layer_mask(kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002784
2785 if (unlikely(kcqe_op_flag & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002786 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002787
2788 while (j < num_cqes) {
2789 u32 next_op = cp->completed_kcq[i + j]->kcqe_op_flag;
2790
Michael Chane1928c82010-12-23 07:43:04 +00002791 if (cnic_get_kcqe_layer_mask(next_op) != kcqe_layer)
Michael Chana4636962009-06-08 18:14:43 -07002792 break;
2793
2794 if (unlikely(next_op & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002795 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002796 j++;
2797 }
2798
2799 if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_RDMA)
2800 ulp_type = CNIC_ULP_RDMA;
2801 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_ISCSI)
2802 ulp_type = CNIC_ULP_ISCSI;
Michael Chane1928c82010-12-23 07:43:04 +00002803 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_FCOE)
2804 ulp_type = CNIC_ULP_FCOE;
Michael Chana4636962009-06-08 18:14:43 -07002805 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L4)
2806 ulp_type = CNIC_ULP_L4;
2807 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L2)
2808 goto end;
2809 else {
Joe Perchesddf79b22010-02-17 15:01:54 +00002810 netdev_err(dev->netdev, "Unknown type of KCQE(0x%x)\n",
2811 kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002812 goto end;
2813 }
2814
2815 rcu_read_lock();
2816 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
2817 if (likely(ulp_ops)) {
2818 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
2819 cp->completed_kcq + i, j);
2820 }
2821 rcu_read_unlock();
2822end:
2823 num_cqes -= j;
2824 i += j;
2825 j = 1;
2826 }
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002827 if (unlikely(comp))
2828 cnic_spq_completion(dev, DRV_CTL_RET_L5_SPQ_CREDIT_CMD, comp);
Michael Chana4636962009-06-08 18:14:43 -07002829}
2830
Michael Chan644b9d42010-06-24 14:58:40 +00002831static int cnic_get_kcqes(struct cnic_dev *dev, struct kcq_info *info)
Michael Chana4636962009-06-08 18:14:43 -07002832{
2833 struct cnic_local *cp = dev->cnic_priv;
Michael Chan644b9d42010-06-24 14:58:40 +00002834 u16 i, ri, hw_prod, last;
Michael Chana4636962009-06-08 18:14:43 -07002835 struct kcqe *kcqe;
2836 int kcqe_cnt = 0, last_cnt = 0;
2837
Michael Chan644b9d42010-06-24 14:58:40 +00002838 i = ri = last = info->sw_prod_idx;
Michael Chana4636962009-06-08 18:14:43 -07002839 ri &= MAX_KCQ_IDX;
Michael Chan644b9d42010-06-24 14:58:40 +00002840 hw_prod = *info->hw_prod_idx_ptr;
Michael Chan59e51372011-06-14 01:32:38 +00002841 hw_prod = info->hw_idx(hw_prod);
Michael Chana4636962009-06-08 18:14:43 -07002842
2843 while ((i != hw_prod) && (kcqe_cnt < MAX_COMPLETED_KCQE)) {
Michael Chan644b9d42010-06-24 14:58:40 +00002844 kcqe = &info->kcq[KCQ_PG(ri)][KCQ_IDX(ri)];
Michael Chana4636962009-06-08 18:14:43 -07002845 cp->completed_kcq[kcqe_cnt++] = kcqe;
Michael Chan59e51372011-06-14 01:32:38 +00002846 i = info->next_idx(i);
Michael Chana4636962009-06-08 18:14:43 -07002847 ri = i & MAX_KCQ_IDX;
2848 if (likely(!(kcqe->kcqe_op_flag & KCQE_FLAGS_NEXT))) {
2849 last_cnt = kcqe_cnt;
2850 last = i;
2851 }
2852 }
2853
Michael Chan644b9d42010-06-24 14:58:40 +00002854 info->sw_prod_idx = last;
Michael Chana4636962009-06-08 18:14:43 -07002855 return last_cnt;
2856}
2857
Michael Chan48f753d2010-05-18 11:32:53 +00002858static int cnic_l2_completion(struct cnic_local *cp)
2859{
2860 u16 hw_cons, sw_cons;
Michael Chancd801532010-10-13 14:06:49 +00002861 struct cnic_uio_dev *udev = cp->udev;
Michael Chan48f753d2010-05-18 11:32:53 +00002862 union eth_rx_cqe *cqe, *cqe_ring = (union eth_rx_cqe *)
Michael Chancd801532010-10-13 14:06:49 +00002863 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Michael Chan48f753d2010-05-18 11:32:53 +00002864 u32 cmd;
2865 int comp = 0;
2866
2867 if (!test_bit(CNIC_F_BNX2X_CLASS, &cp->dev->flags))
2868 return 0;
2869
2870 hw_cons = *cp->rx_cons_ptr;
2871 if ((hw_cons & BNX2X_MAX_RCQ_DESC_CNT) == BNX2X_MAX_RCQ_DESC_CNT)
2872 hw_cons++;
2873
2874 sw_cons = cp->rx_cons;
2875 while (sw_cons != hw_cons) {
2876 u8 cqe_fp_flags;
2877
2878 cqe = &cqe_ring[sw_cons & BNX2X_MAX_RCQ_DESC_CNT];
2879 cqe_fp_flags = cqe->fast_path_cqe.type_error_flags;
2880 if (cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE) {
2881 cmd = le32_to_cpu(cqe->ramrod_cqe.conn_and_cmd_data);
2882 cmd >>= COMMON_RAMROD_ETH_RX_CQE_CMD_ID_SHIFT;
2883 if (cmd == RAMROD_CMD_ID_ETH_CLIENT_SETUP ||
2884 cmd == RAMROD_CMD_ID_ETH_HALT)
2885 comp++;
2886 }
2887 sw_cons = BNX2X_NEXT_RCQE(sw_cons);
2888 }
2889 return comp;
2890}
2891
Michael Chan86b53602009-10-10 13:46:57 +00002892static void cnic_chk_pkt_rings(struct cnic_local *cp)
Michael Chana4636962009-06-08 18:14:43 -07002893{
Michael Chan541a7812010-10-06 03:17:22 +00002894 u16 rx_cons, tx_cons;
Michael Chan48f753d2010-05-18 11:32:53 +00002895 int comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002896
Michael Chan541a7812010-10-06 03:17:22 +00002897 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
Michael Chan66fee9e2010-06-24 14:58:38 +00002898 return;
2899
Michael Chan541a7812010-10-06 03:17:22 +00002900 rx_cons = *cp->rx_cons_ptr;
2901 tx_cons = *cp->tx_cons_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002902 if (cp->tx_cons != tx_cons || cp->rx_cons != rx_cons) {
Michael Chan48f753d2010-05-18 11:32:53 +00002903 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
2904 comp = cnic_l2_completion(cp);
2905
Michael Chana4636962009-06-08 18:14:43 -07002906 cp->tx_cons = tx_cons;
2907 cp->rx_cons = rx_cons;
Michael Chan71034ba2009-10-10 13:46:59 +00002908
Michael Chancd801532010-10-13 14:06:49 +00002909 if (cp->udev)
2910 uio_event_notify(&cp->udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -07002911 }
Michael Chan48f753d2010-05-18 11:32:53 +00002912 if (comp)
2913 clear_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07002914}
2915
Michael Chanb177a5d52010-06-24 14:58:41 +00002916static u32 cnic_service_bnx2_queues(struct cnic_dev *dev)
Michael Chana4636962009-06-08 18:14:43 -07002917{
Michael Chana4636962009-06-08 18:14:43 -07002918 struct cnic_local *cp = dev->cnic_priv;
Michael Chanb177a5d52010-06-24 14:58:41 +00002919 u32 status_idx = (u16) *cp->kcq1.status_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002920 int kcqe_cnt;
2921
Michael Chan107c3f42011-03-02 13:00:49 +00002922 /* status block index must be read before reading other fields */
2923 rmb();
Michael Chana4636962009-06-08 18:14:43 -07002924 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
2925
Michael Chan644b9d42010-06-24 14:58:40 +00002926 while ((kcqe_cnt = cnic_get_kcqes(dev, &cp->kcq1))) {
Michael Chana4636962009-06-08 18:14:43 -07002927
2928 service_kcqes(dev, kcqe_cnt);
2929
2930 /* Tell compiler that status_blk fields can change. */
2931 barrier();
Michael Chan93736652011-06-08 19:29:32 +00002932 status_idx = (u16) *cp->kcq1.status_idx_ptr;
2933 /* status block index must be read first */
2934 rmb();
2935 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002936 }
2937
Michael Chan644b9d42010-06-24 14:58:40 +00002938 CNIC_WR16(dev, cp->kcq1.io_addr, cp->kcq1.sw_prod_idx);
Michael Chana4636962009-06-08 18:14:43 -07002939
Michael Chan86b53602009-10-10 13:46:57 +00002940 cnic_chk_pkt_rings(cp);
Michael Chanb177a5d52010-06-24 14:58:41 +00002941
Michael Chana4636962009-06-08 18:14:43 -07002942 return status_idx;
2943}
2944
Michael Chanb177a5d52010-06-24 14:58:41 +00002945static int cnic_service_bnx2(void *data, void *status_blk)
2946{
2947 struct cnic_dev *dev = data;
Michael Chanb177a5d52010-06-24 14:58:41 +00002948
Michael Chaneaaa6e92010-12-23 08:38:30 +00002949 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
2950 struct status_block *sblk = status_blk;
2951
2952 return sblk->status_idx;
2953 }
Michael Chanb177a5d52010-06-24 14:58:41 +00002954
2955 return cnic_service_bnx2_queues(dev);
2956}
2957
Michael Chana4636962009-06-08 18:14:43 -07002958static void cnic_service_bnx2_msix(unsigned long data)
2959{
2960 struct cnic_dev *dev = (struct cnic_dev *) data;
2961 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002962
Michael Chanb177a5d52010-06-24 14:58:41 +00002963 cp->last_status_idx = cnic_service_bnx2_queues(dev);
Michael Chana4636962009-06-08 18:14:43 -07002964
Michael Chana4636962009-06-08 18:14:43 -07002965 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
2966 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
2967}
2968
Michael Chan66fee9e2010-06-24 14:58:38 +00002969static void cnic_doirq(struct cnic_dev *dev)
2970{
2971 struct cnic_local *cp = dev->cnic_priv;
Michael Chan66fee9e2010-06-24 14:58:38 +00002972
2973 if (likely(test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
Michael Chaneaaa6e92010-12-23 08:38:30 +00002974 u16 prod = cp->kcq1.sw_prod_idx & MAX_KCQ_IDX;
2975
Michael Chan66fee9e2010-06-24 14:58:38 +00002976 prefetch(cp->status_blk.gen);
Michael Chane6c28892010-06-24 14:58:39 +00002977 prefetch(&cp->kcq1.kcq[KCQ_PG(prod)][KCQ_IDX(prod)]);
Michael Chan66fee9e2010-06-24 14:58:38 +00002978
2979 tasklet_schedule(&cp->cnic_irq_task);
2980 }
2981}
2982
Michael Chana4636962009-06-08 18:14:43 -07002983static irqreturn_t cnic_irq(int irq, void *dev_instance)
2984{
2985 struct cnic_dev *dev = dev_instance;
2986 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002987
2988 if (cp->ack_int)
2989 cp->ack_int(dev);
2990
Michael Chan66fee9e2010-06-24 14:58:38 +00002991 cnic_doirq(dev);
Michael Chana4636962009-06-08 18:14:43 -07002992
2993 return IRQ_HANDLED;
2994}
2995
Michael Chan71034ba2009-10-10 13:46:59 +00002996static inline void cnic_ack_bnx2x_int(struct cnic_dev *dev, u8 id, u8 storm,
2997 u16 index, u8 op, u8 update)
2998{
2999 struct cnic_local *cp = dev->cnic_priv;
3000 u32 hc_addr = (HC_REG_COMMAND_REG + CNIC_PORT(cp) * 32 +
3001 COMMAND_REG_INT_ACK);
3002 struct igu_ack_register igu_ack;
3003
3004 igu_ack.status_block_index = index;
3005 igu_ack.sb_id_and_flags =
3006 ((id << IGU_ACK_REGISTER_STATUS_BLOCK_ID_SHIFT) |
3007 (storm << IGU_ACK_REGISTER_STORM_ID_SHIFT) |
3008 (update << IGU_ACK_REGISTER_UPDATE_INDEX_SHIFT) |
3009 (op << IGU_ACK_REGISTER_INTERRUPT_MODE_SHIFT));
3010
3011 CNIC_WR(dev, hc_addr, (*(u32 *)&igu_ack));
3012}
3013
Michael Chanee87a822010-10-13 14:06:51 +00003014static void cnic_ack_igu_sb(struct cnic_dev *dev, u8 igu_sb_id, u8 segment,
3015 u16 index, u8 op, u8 update)
3016{
3017 struct igu_regular cmd_data;
3018 u32 igu_addr = BAR_IGU_INTMEM + (IGU_CMD_INT_ACK_BASE + igu_sb_id) * 8;
3019
3020 cmd_data.sb_id_and_flags =
3021 (index << IGU_REGULAR_SB_INDEX_SHIFT) |
3022 (segment << IGU_REGULAR_SEGMENT_ACCESS_SHIFT) |
3023 (update << IGU_REGULAR_BUPDATE_SHIFT) |
3024 (op << IGU_REGULAR_ENABLE_INT_SHIFT);
3025
3026
3027 CNIC_WR(dev, igu_addr, cmd_data.sb_id_and_flags);
3028}
3029
Michael Chan71034ba2009-10-10 13:46:59 +00003030static void cnic_ack_bnx2x_msix(struct cnic_dev *dev)
3031{
3032 struct cnic_local *cp = dev->cnic_priv;
3033
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003034 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, CSTORM_ID, 0,
Michael Chan71034ba2009-10-10 13:46:59 +00003035 IGU_INT_DISABLE, 0);
3036}
3037
Michael Chanee87a822010-10-13 14:06:51 +00003038static void cnic_ack_bnx2x_e2_msix(struct cnic_dev *dev)
3039{
3040 struct cnic_local *cp = dev->cnic_priv;
3041
3042 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF, 0,
3043 IGU_INT_DISABLE, 0);
3044}
3045
Michael Chanb177a5d52010-06-24 14:58:41 +00003046static u32 cnic_service_bnx2x_kcq(struct cnic_dev *dev, struct kcq_info *info)
Michael Chan71034ba2009-10-10 13:46:59 +00003047{
Michael Chanb177a5d52010-06-24 14:58:41 +00003048 u32 last_status = *info->status_idx_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00003049 int kcqe_cnt;
3050
Michael Chan107c3f42011-03-02 13:00:49 +00003051 /* status block index must be read before reading the KCQ */
3052 rmb();
Michael Chanb177a5d52010-06-24 14:58:41 +00003053 while ((kcqe_cnt = cnic_get_kcqes(dev, info))) {
Michael Chan71034ba2009-10-10 13:46:59 +00003054
3055 service_kcqes(dev, kcqe_cnt);
3056
3057 /* Tell compiler that sblk fields can change. */
3058 barrier();
Michael Chan71034ba2009-10-10 13:46:59 +00003059
Michael Chanb177a5d52010-06-24 14:58:41 +00003060 last_status = *info->status_idx_ptr;
Michael Chan107c3f42011-03-02 13:00:49 +00003061 /* status block index must be read before reading the KCQ */
3062 rmb();
Michael Chan71034ba2009-10-10 13:46:59 +00003063 }
Michael Chanb177a5d52010-06-24 14:58:41 +00003064 return last_status;
3065}
3066
3067static void cnic_service_bnx2x_bh(unsigned long data)
3068{
3069 struct cnic_dev *dev = (struct cnic_dev *) data;
3070 struct cnic_local *cp = dev->cnic_priv;
Michael Chan0197b082011-03-02 13:00:50 +00003071 u32 status_idx, new_status_idx;
Michael Chanb177a5d52010-06-24 14:58:41 +00003072
3073 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
3074 return;
3075
Michael Chan0197b082011-03-02 13:00:50 +00003076 while (1) {
3077 status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq1);
Michael Chan71034ba2009-10-10 13:46:59 +00003078
Michael Chan0197b082011-03-02 13:00:50 +00003079 CNIC_WR16(dev, cp->kcq1.io_addr,
3080 cp->kcq1.sw_prod_idx + MAX_KCQ_IDX);
Michael Chane21ba412010-12-23 07:43:03 +00003081
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03003082 if (!BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chan0197b082011-03-02 13:00:50 +00003083 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, USTORM_ID,
3084 status_idx, IGU_INT_ENABLE, 1);
3085 break;
3086 }
3087
3088 new_status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq2);
3089
3090 if (new_status_idx != status_idx)
3091 continue;
Michael Chane21ba412010-12-23 07:43:03 +00003092
3093 CNIC_WR16(dev, cp->kcq2.io_addr, cp->kcq2.sw_prod_idx +
3094 MAX_KCQ_IDX);
3095
Michael Chanee87a822010-10-13 14:06:51 +00003096 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF,
3097 status_idx, IGU_INT_ENABLE, 1);
Michael Chan0197b082011-03-02 13:00:50 +00003098
3099 break;
Michael Chane21ba412010-12-23 07:43:03 +00003100 }
Michael Chan71034ba2009-10-10 13:46:59 +00003101}
3102
3103static int cnic_service_bnx2x(void *data, void *status_blk)
3104{
3105 struct cnic_dev *dev = data;
3106 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00003107
Michael Chan66fee9e2010-06-24 14:58:38 +00003108 if (!(cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
3109 cnic_doirq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00003110
Michael Chan66fee9e2010-06-24 14:58:38 +00003111 cnic_chk_pkt_rings(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00003112
3113 return 0;
3114}
3115
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003116static void cnic_ulp_stop_one(struct cnic_local *cp, int if_type)
3117{
3118 struct cnic_ulp_ops *ulp_ops;
3119
3120 if (if_type == CNIC_ULP_ISCSI)
3121 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
3122
3123 mutex_lock(&cnic_lock);
3124 ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
3125 lockdep_is_held(&cnic_lock));
3126 if (!ulp_ops) {
3127 mutex_unlock(&cnic_lock);
3128 return;
3129 }
3130 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
3131 mutex_unlock(&cnic_lock);
3132
3133 if (test_and_clear_bit(ULP_F_START, &cp->ulp_flags[if_type]))
3134 ulp_ops->cnic_stop(cp->ulp_handle[if_type]);
3135
3136 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
3137}
3138
Michael Chana4636962009-06-08 18:14:43 -07003139static void cnic_ulp_stop(struct cnic_dev *dev)
3140{
3141 struct cnic_local *cp = dev->cnic_priv;
3142 int if_type;
3143
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003144 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++)
3145 cnic_ulp_stop_one(cp, if_type);
Michael Chana4636962009-06-08 18:14:43 -07003146}
3147
3148static void cnic_ulp_start(struct cnic_dev *dev)
3149{
3150 struct cnic_local *cp = dev->cnic_priv;
3151 int if_type;
3152
Michael Chana4636962009-06-08 18:14:43 -07003153 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
3154 struct cnic_ulp_ops *ulp_ops;
3155
Michael Chan681dbd72009-08-14 15:49:46 +00003156 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003157 ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
3158 lockdep_is_held(&cnic_lock));
Michael Chan681dbd72009-08-14 15:49:46 +00003159 if (!ulp_ops || !ulp_ops->cnic_start) {
3160 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003161 continue;
Michael Chan681dbd72009-08-14 15:49:46 +00003162 }
3163 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
3164 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003165
3166 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[if_type]))
3167 ulp_ops->cnic_start(cp->ulp_handle[if_type]);
Michael Chan681dbd72009-08-14 15:49:46 +00003168
3169 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
Michael Chana4636962009-06-08 18:14:43 -07003170 }
Michael Chana4636962009-06-08 18:14:43 -07003171}
3172
Barak Witkowski1d187b32011-12-05 22:41:50 +00003173static int cnic_copy_ulp_stats(struct cnic_dev *dev, int ulp_type)
3174{
3175 struct cnic_local *cp = dev->cnic_priv;
3176 struct cnic_ulp_ops *ulp_ops;
3177 int rc;
3178
3179 mutex_lock(&cnic_lock);
3180 ulp_ops = cnic_ulp_tbl_prot(ulp_type);
3181 if (ulp_ops && ulp_ops->cnic_get_stats)
3182 rc = ulp_ops->cnic_get_stats(cp->ulp_handle[ulp_type]);
3183 else
3184 rc = -ENODEV;
3185 mutex_unlock(&cnic_lock);
3186 return rc;
3187}
3188
Michael Chana4636962009-06-08 18:14:43 -07003189static int cnic_ctl(void *data, struct cnic_ctl_info *info)
3190{
3191 struct cnic_dev *dev = data;
Barak Witkowski1d187b32011-12-05 22:41:50 +00003192 int ulp_type = CNIC_ULP_ISCSI;
Michael Chana4636962009-06-08 18:14:43 -07003193
3194 switch (info->cmd) {
3195 case CNIC_CTL_STOP_CMD:
3196 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07003197
3198 cnic_ulp_stop(dev);
3199 cnic_stop_hw(dev);
3200
Michael Chana4636962009-06-08 18:14:43 -07003201 cnic_put(dev);
3202 break;
3203 case CNIC_CTL_START_CMD:
3204 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07003205
3206 if (!cnic_start_hw(dev))
3207 cnic_ulp_start(dev);
3208
Michael Chana4636962009-06-08 18:14:43 -07003209 cnic_put(dev);
3210 break;
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003211 case CNIC_CTL_STOP_ISCSI_CMD: {
3212 struct cnic_local *cp = dev->cnic_priv;
3213 set_bit(CNIC_LCL_FL_STOP_ISCSI, &cp->cnic_local_flags);
3214 queue_delayed_work(cnic_wq, &cp->delete_task, 0);
3215 break;
3216 }
Michael Chan71034ba2009-10-10 13:46:59 +00003217 case CNIC_CTL_COMPLETION_CMD: {
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03003218 struct cnic_ctl_completion *comp = &info->data.comp;
3219 u32 cid = BNX2X_SW_CID(comp->cid);
Michael Chan71034ba2009-10-10 13:46:59 +00003220 u32 l5_cid;
3221 struct cnic_local *cp = dev->cnic_priv;
3222
Michael Chana2028b232012-06-27 15:08:19 +00003223 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
3224 break;
3225
Michael Chan71034ba2009-10-10 13:46:59 +00003226 if (cnic_get_l5_cid(cp, cid, &l5_cid) == 0) {
3227 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3228
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03003229 if (unlikely(comp->error)) {
3230 set_bit(CTX_FL_CID_ERROR, &ctx->ctx_flags);
3231 netdev_err(dev->netdev,
3232 "CID %x CFC delete comp error %x\n",
3233 cid, comp->error);
3234 }
3235
Michael Chan71034ba2009-10-10 13:46:59 +00003236 ctx->wait_cond = 1;
3237 wake_up(&ctx->waitq);
3238 }
3239 break;
3240 }
Barak Witkowski1d187b32011-12-05 22:41:50 +00003241 case CNIC_CTL_FCOE_STATS_GET_CMD:
3242 ulp_type = CNIC_ULP_FCOE;
3243 /* fall through */
3244 case CNIC_CTL_ISCSI_STATS_GET_CMD:
3245 cnic_hold(dev);
3246 cnic_copy_ulp_stats(dev, ulp_type);
3247 cnic_put(dev);
3248 break;
3249
Michael Chana4636962009-06-08 18:14:43 -07003250 default:
3251 return -EINVAL;
3252 }
3253 return 0;
3254}
3255
3256static void cnic_ulp_init(struct cnic_dev *dev)
3257{
3258 int i;
3259 struct cnic_local *cp = dev->cnic_priv;
3260
Michael Chana4636962009-06-08 18:14:43 -07003261 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3262 struct cnic_ulp_ops *ulp_ops;
3263
Michael Chan7fc1ece2009-08-14 15:49:47 +00003264 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003265 ulp_ops = cnic_ulp_tbl_prot(i);
Michael Chan7fc1ece2009-08-14 15:49:47 +00003266 if (!ulp_ops || !ulp_ops->cnic_init) {
3267 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003268 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003269 }
3270 ulp_get(ulp_ops);
3271 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003272
3273 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3274 ulp_ops->cnic_init(dev);
3275
Michael Chan7fc1ece2009-08-14 15:49:47 +00003276 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003277 }
Michael Chana4636962009-06-08 18:14:43 -07003278}
3279
3280static void cnic_ulp_exit(struct cnic_dev *dev)
3281{
3282 int i;
3283 struct cnic_local *cp = dev->cnic_priv;
3284
Michael Chana4636962009-06-08 18:14:43 -07003285 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3286 struct cnic_ulp_ops *ulp_ops;
3287
Michael Chan7fc1ece2009-08-14 15:49:47 +00003288 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003289 ulp_ops = cnic_ulp_tbl_prot(i);
Michael Chan7fc1ece2009-08-14 15:49:47 +00003290 if (!ulp_ops || !ulp_ops->cnic_exit) {
3291 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003292 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003293 }
3294 ulp_get(ulp_ops);
3295 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003296
3297 if (test_and_clear_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3298 ulp_ops->cnic_exit(dev);
3299
Michael Chan7fc1ece2009-08-14 15:49:47 +00003300 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003301 }
Michael Chana4636962009-06-08 18:14:43 -07003302}
3303
3304static int cnic_cm_offload_pg(struct cnic_sock *csk)
3305{
3306 struct cnic_dev *dev = csk->dev;
3307 struct l4_kwq_offload_pg *l4kwqe;
3308 struct kwqe *wqes[1];
3309
3310 l4kwqe = (struct l4_kwq_offload_pg *) &csk->kwqe1;
3311 memset(l4kwqe, 0, sizeof(*l4kwqe));
3312 wqes[0] = (struct kwqe *) l4kwqe;
3313
3314 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_OFFLOAD_PG;
3315 l4kwqe->flags =
3316 L4_LAYER_CODE << L4_KWQ_OFFLOAD_PG_LAYER_CODE_SHIFT;
3317 l4kwqe->l2hdr_nbytes = ETH_HLEN;
3318
3319 l4kwqe->da0 = csk->ha[0];
3320 l4kwqe->da1 = csk->ha[1];
3321 l4kwqe->da2 = csk->ha[2];
3322 l4kwqe->da3 = csk->ha[3];
3323 l4kwqe->da4 = csk->ha[4];
3324 l4kwqe->da5 = csk->ha[5];
3325
3326 l4kwqe->sa0 = dev->mac_addr[0];
3327 l4kwqe->sa1 = dev->mac_addr[1];
3328 l4kwqe->sa2 = dev->mac_addr[2];
3329 l4kwqe->sa3 = dev->mac_addr[3];
3330 l4kwqe->sa4 = dev->mac_addr[4];
3331 l4kwqe->sa5 = dev->mac_addr[5];
3332
3333 l4kwqe->etype = ETH_P_IP;
Eddie Waia9736c02010-02-24 14:42:04 +00003334 l4kwqe->ipid_start = DEF_IPID_START;
Michael Chana4636962009-06-08 18:14:43 -07003335 l4kwqe->host_opaque = csk->l5_cid;
3336
3337 if (csk->vlan_id) {
3338 l4kwqe->pg_flags |= L4_KWQ_OFFLOAD_PG_VLAN_TAGGING;
3339 l4kwqe->vlan_tag = csk->vlan_id;
3340 l4kwqe->l2hdr_nbytes += 4;
3341 }
3342
3343 return dev->submit_kwqes(dev, wqes, 1);
3344}
3345
3346static int cnic_cm_update_pg(struct cnic_sock *csk)
3347{
3348 struct cnic_dev *dev = csk->dev;
3349 struct l4_kwq_update_pg *l4kwqe;
3350 struct kwqe *wqes[1];
3351
3352 l4kwqe = (struct l4_kwq_update_pg *) &csk->kwqe1;
3353 memset(l4kwqe, 0, sizeof(*l4kwqe));
3354 wqes[0] = (struct kwqe *) l4kwqe;
3355
3356 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPDATE_PG;
3357 l4kwqe->flags =
3358 L4_LAYER_CODE << L4_KWQ_UPDATE_PG_LAYER_CODE_SHIFT;
3359 l4kwqe->pg_cid = csk->pg_cid;
3360
3361 l4kwqe->da0 = csk->ha[0];
3362 l4kwqe->da1 = csk->ha[1];
3363 l4kwqe->da2 = csk->ha[2];
3364 l4kwqe->da3 = csk->ha[3];
3365 l4kwqe->da4 = csk->ha[4];
3366 l4kwqe->da5 = csk->ha[5];
3367
3368 l4kwqe->pg_host_opaque = csk->l5_cid;
3369 l4kwqe->pg_valids = L4_KWQ_UPDATE_PG_VALIDS_DA;
3370
3371 return dev->submit_kwqes(dev, wqes, 1);
3372}
3373
3374static int cnic_cm_upload_pg(struct cnic_sock *csk)
3375{
3376 struct cnic_dev *dev = csk->dev;
3377 struct l4_kwq_upload *l4kwqe;
3378 struct kwqe *wqes[1];
3379
3380 l4kwqe = (struct l4_kwq_upload *) &csk->kwqe1;
3381 memset(l4kwqe, 0, sizeof(*l4kwqe));
3382 wqes[0] = (struct kwqe *) l4kwqe;
3383
3384 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPLOAD_PG;
3385 l4kwqe->flags =
3386 L4_LAYER_CODE << L4_KWQ_UPLOAD_LAYER_CODE_SHIFT;
3387 l4kwqe->cid = csk->pg_cid;
3388
3389 return dev->submit_kwqes(dev, wqes, 1);
3390}
3391
3392static int cnic_cm_conn_req(struct cnic_sock *csk)
3393{
3394 struct cnic_dev *dev = csk->dev;
3395 struct l4_kwq_connect_req1 *l4kwqe1;
3396 struct l4_kwq_connect_req2 *l4kwqe2;
3397 struct l4_kwq_connect_req3 *l4kwqe3;
3398 struct kwqe *wqes[3];
3399 u8 tcp_flags = 0;
3400 int num_wqes = 2;
3401
3402 l4kwqe1 = (struct l4_kwq_connect_req1 *) &csk->kwqe1;
3403 l4kwqe2 = (struct l4_kwq_connect_req2 *) &csk->kwqe2;
3404 l4kwqe3 = (struct l4_kwq_connect_req3 *) &csk->kwqe3;
3405 memset(l4kwqe1, 0, sizeof(*l4kwqe1));
3406 memset(l4kwqe2, 0, sizeof(*l4kwqe2));
3407 memset(l4kwqe3, 0, sizeof(*l4kwqe3));
3408
3409 l4kwqe3->op_code = L4_KWQE_OPCODE_VALUE_CONNECT3;
3410 l4kwqe3->flags =
3411 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ3_LAYER_CODE_SHIFT;
3412 l4kwqe3->ka_timeout = csk->ka_timeout;
3413 l4kwqe3->ka_interval = csk->ka_interval;
3414 l4kwqe3->ka_max_probe_count = csk->ka_max_probe_count;
3415 l4kwqe3->tos = csk->tos;
3416 l4kwqe3->ttl = csk->ttl;
3417 l4kwqe3->snd_seq_scale = csk->snd_seq_scale;
3418 l4kwqe3->pmtu = csk->mtu;
3419 l4kwqe3->rcv_buf = csk->rcv_buf;
3420 l4kwqe3->snd_buf = csk->snd_buf;
3421 l4kwqe3->seed = csk->seed;
3422
3423 wqes[0] = (struct kwqe *) l4kwqe1;
3424 if (test_bit(SK_F_IPV6, &csk->flags)) {
3425 wqes[1] = (struct kwqe *) l4kwqe2;
3426 wqes[2] = (struct kwqe *) l4kwqe3;
3427 num_wqes = 3;
3428
3429 l4kwqe1->conn_flags = L4_KWQ_CONNECT_REQ1_IP_V6;
3430 l4kwqe2->op_code = L4_KWQE_OPCODE_VALUE_CONNECT2;
3431 l4kwqe2->flags =
3432 L4_KWQ_CONNECT_REQ2_LINKED_WITH_NEXT |
3433 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ2_LAYER_CODE_SHIFT;
3434 l4kwqe2->src_ip_v6_2 = be32_to_cpu(csk->src_ip[1]);
3435 l4kwqe2->src_ip_v6_3 = be32_to_cpu(csk->src_ip[2]);
3436 l4kwqe2->src_ip_v6_4 = be32_to_cpu(csk->src_ip[3]);
3437 l4kwqe2->dst_ip_v6_2 = be32_to_cpu(csk->dst_ip[1]);
3438 l4kwqe2->dst_ip_v6_3 = be32_to_cpu(csk->dst_ip[2]);
3439 l4kwqe2->dst_ip_v6_4 = be32_to_cpu(csk->dst_ip[3]);
3440 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct ipv6hdr) -
3441 sizeof(struct tcphdr);
3442 } else {
3443 wqes[1] = (struct kwqe *) l4kwqe3;
3444 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct iphdr) -
3445 sizeof(struct tcphdr);
3446 }
3447
3448 l4kwqe1->op_code = L4_KWQE_OPCODE_VALUE_CONNECT1;
3449 l4kwqe1->flags =
3450 (L4_LAYER_CODE << L4_KWQ_CONNECT_REQ1_LAYER_CODE_SHIFT) |
3451 L4_KWQ_CONNECT_REQ3_LINKED_WITH_NEXT;
3452 l4kwqe1->cid = csk->cid;
3453 l4kwqe1->pg_cid = csk->pg_cid;
3454 l4kwqe1->src_ip = be32_to_cpu(csk->src_ip[0]);
3455 l4kwqe1->dst_ip = be32_to_cpu(csk->dst_ip[0]);
3456 l4kwqe1->src_port = be16_to_cpu(csk->src_port);
3457 l4kwqe1->dst_port = be16_to_cpu(csk->dst_port);
3458 if (csk->tcp_flags & SK_TCP_NO_DELAY_ACK)
3459 tcp_flags |= L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK;
3460 if (csk->tcp_flags & SK_TCP_KEEP_ALIVE)
3461 tcp_flags |= L4_KWQ_CONNECT_REQ1_KEEP_ALIVE;
3462 if (csk->tcp_flags & SK_TCP_NAGLE)
3463 tcp_flags |= L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE;
3464 if (csk->tcp_flags & SK_TCP_TIMESTAMP)
3465 tcp_flags |= L4_KWQ_CONNECT_REQ1_TIME_STAMP;
3466 if (csk->tcp_flags & SK_TCP_SACK)
3467 tcp_flags |= L4_KWQ_CONNECT_REQ1_SACK;
3468 if (csk->tcp_flags & SK_TCP_SEG_SCALING)
3469 tcp_flags |= L4_KWQ_CONNECT_REQ1_SEG_SCALING;
3470
3471 l4kwqe1->tcp_flags = tcp_flags;
3472
3473 return dev->submit_kwqes(dev, wqes, num_wqes);
3474}
3475
3476static int cnic_cm_close_req(struct cnic_sock *csk)
3477{
3478 struct cnic_dev *dev = csk->dev;
3479 struct l4_kwq_close_req *l4kwqe;
3480 struct kwqe *wqes[1];
3481
3482 l4kwqe = (struct l4_kwq_close_req *) &csk->kwqe2;
3483 memset(l4kwqe, 0, sizeof(*l4kwqe));
3484 wqes[0] = (struct kwqe *) l4kwqe;
3485
3486 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_CLOSE;
3487 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_CLOSE_REQ_LAYER_CODE_SHIFT;
3488 l4kwqe->cid = csk->cid;
3489
3490 return dev->submit_kwqes(dev, wqes, 1);
3491}
3492
3493static int cnic_cm_abort_req(struct cnic_sock *csk)
3494{
3495 struct cnic_dev *dev = csk->dev;
3496 struct l4_kwq_reset_req *l4kwqe;
3497 struct kwqe *wqes[1];
3498
3499 l4kwqe = (struct l4_kwq_reset_req *) &csk->kwqe2;
3500 memset(l4kwqe, 0, sizeof(*l4kwqe));
3501 wqes[0] = (struct kwqe *) l4kwqe;
3502
3503 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_RESET;
3504 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_RESET_REQ_LAYER_CODE_SHIFT;
3505 l4kwqe->cid = csk->cid;
3506
3507 return dev->submit_kwqes(dev, wqes, 1);
3508}
3509
3510static int cnic_cm_create(struct cnic_dev *dev, int ulp_type, u32 cid,
3511 u32 l5_cid, struct cnic_sock **csk, void *context)
3512{
3513 struct cnic_local *cp = dev->cnic_priv;
3514 struct cnic_sock *csk1;
3515
3516 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3517 return -EINVAL;
3518
Michael Chanfdf24082010-10-13 14:06:47 +00003519 if (cp->ctx_tbl) {
3520 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3521
3522 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
3523 return -EAGAIN;
3524 }
3525
Michael Chana4636962009-06-08 18:14:43 -07003526 csk1 = &cp->csk_tbl[l5_cid];
3527 if (atomic_read(&csk1->ref_count))
3528 return -EAGAIN;
3529
3530 if (test_and_set_bit(SK_F_INUSE, &csk1->flags))
3531 return -EBUSY;
3532
3533 csk1->dev = dev;
3534 csk1->cid = cid;
3535 csk1->l5_cid = l5_cid;
3536 csk1->ulp_type = ulp_type;
3537 csk1->context = context;
3538
3539 csk1->ka_timeout = DEF_KA_TIMEOUT;
3540 csk1->ka_interval = DEF_KA_INTERVAL;
3541 csk1->ka_max_probe_count = DEF_KA_MAX_PROBE_COUNT;
3542 csk1->tos = DEF_TOS;
3543 csk1->ttl = DEF_TTL;
3544 csk1->snd_seq_scale = DEF_SND_SEQ_SCALE;
3545 csk1->rcv_buf = DEF_RCV_BUF;
3546 csk1->snd_buf = DEF_SND_BUF;
3547 csk1->seed = DEF_SEED;
3548
3549 *csk = csk1;
3550 return 0;
3551}
3552
3553static void cnic_cm_cleanup(struct cnic_sock *csk)
3554{
3555 if (csk->src_port) {
3556 struct cnic_dev *dev = csk->dev;
3557 struct cnic_local *cp = dev->cnic_priv;
3558
Michael Chan9b093362010-12-23 07:42:56 +00003559 cnic_free_id(&cp->csk_port_tbl, be16_to_cpu(csk->src_port));
Michael Chana4636962009-06-08 18:14:43 -07003560 csk->src_port = 0;
3561 }
3562}
3563
3564static void cnic_close_conn(struct cnic_sock *csk)
3565{
3566 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags)) {
3567 cnic_cm_upload_pg(csk);
3568 clear_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3569 }
3570 cnic_cm_cleanup(csk);
3571}
3572
3573static int cnic_cm_destroy(struct cnic_sock *csk)
3574{
3575 if (!cnic_in_use(csk))
3576 return -EINVAL;
3577
3578 csk_hold(csk);
3579 clear_bit(SK_F_INUSE, &csk->flags);
3580 smp_mb__after_clear_bit();
3581 while (atomic_read(&csk->ref_count) != 1)
3582 msleep(1);
3583 cnic_cm_cleanup(csk);
3584
3585 csk->flags = 0;
3586 csk_put(csk);
3587 return 0;
3588}
3589
3590static inline u16 cnic_get_vlan(struct net_device *dev,
3591 struct net_device **vlan_dev)
3592{
3593 if (dev->priv_flags & IFF_802_1Q_VLAN) {
3594 *vlan_dev = vlan_dev_real_dev(dev);
3595 return vlan_dev_vlan_id(dev);
3596 }
3597 *vlan_dev = dev;
3598 return 0;
3599}
3600
3601static int cnic_get_v4_route(struct sockaddr_in *dst_addr,
3602 struct dst_entry **dst)
3603{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003604#if defined(CONFIG_INET)
Michael Chana4636962009-06-08 18:14:43 -07003605 struct rtable *rt;
3606
David S. Miller78fbfd82011-03-12 00:00:52 -05003607 rt = ip_route_output(&init_net, dst_addr->sin_addr.s_addr, 0, 0, 0);
3608 if (!IS_ERR(rt)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07003609 *dst = &rt->dst;
David S. Miller78fbfd82011-03-12 00:00:52 -05003610 return 0;
3611 }
3612 return PTR_ERR(rt);
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003613#else
3614 return -ENETUNREACH;
3615#endif
Michael Chana4636962009-06-08 18:14:43 -07003616}
3617
3618static int cnic_get_v6_route(struct sockaddr_in6 *dst_addr,
3619 struct dst_entry **dst)
3620{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003621#if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
David S. Miller4c9483b2011-03-12 16:22:43 -05003622 struct flowi6 fl6;
Michael Chana4636962009-06-08 18:14:43 -07003623
David S. Miller4c9483b2011-03-12 16:22:43 -05003624 memset(&fl6, 0, sizeof(fl6));
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +00003625 fl6.daddr = dst_addr->sin6_addr;
David S. Miller4c9483b2011-03-12 16:22:43 -05003626 if (ipv6_addr_type(&fl6.daddr) & IPV6_ADDR_LINKLOCAL)
3627 fl6.flowi6_oif = dst_addr->sin6_scope_id;
Michael Chana4636962009-06-08 18:14:43 -07003628
David S. Miller4c9483b2011-03-12 16:22:43 -05003629 *dst = ip6_route_output(&init_net, NULL, &fl6);
RongQing.Li05417432012-02-21 22:10:50 +00003630 if ((*dst)->error) {
3631 dst_release(*dst);
3632 *dst = NULL;
3633 return -ENETUNREACH;
3634 } else
Michael Chana4636962009-06-08 18:14:43 -07003635 return 0;
3636#endif
3637
3638 return -ENETUNREACH;
3639}
3640
3641static struct cnic_dev *cnic_cm_select_dev(struct sockaddr_in *dst_addr,
3642 int ulp_type)
3643{
3644 struct cnic_dev *dev = NULL;
3645 struct dst_entry *dst;
3646 struct net_device *netdev = NULL;
3647 int err = -ENETUNREACH;
3648
3649 if (dst_addr->sin_family == AF_INET)
3650 err = cnic_get_v4_route(dst_addr, &dst);
3651 else if (dst_addr->sin_family == AF_INET6) {
3652 struct sockaddr_in6 *dst_addr6 =
3653 (struct sockaddr_in6 *) dst_addr;
3654
3655 err = cnic_get_v6_route(dst_addr6, &dst);
3656 } else
3657 return NULL;
3658
3659 if (err)
3660 return NULL;
3661
3662 if (!dst->dev)
3663 goto done;
3664
3665 cnic_get_vlan(dst->dev, &netdev);
3666
3667 dev = cnic_from_netdev(netdev);
3668
3669done:
3670 dst_release(dst);
3671 if (dev)
3672 cnic_put(dev);
3673 return dev;
3674}
3675
3676static int cnic_resolve_addr(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3677{
3678 struct cnic_dev *dev = csk->dev;
3679 struct cnic_local *cp = dev->cnic_priv;
3680
3681 return cnic_send_nlmsg(cp, ISCSI_KEVENT_PATH_REQ, csk);
3682}
3683
3684static int cnic_get_route(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3685{
3686 struct cnic_dev *dev = csk->dev;
3687 struct cnic_local *cp = dev->cnic_priv;
Michael Chanc76284a2010-02-24 14:42:07 +00003688 int is_v6, rc = 0;
3689 struct dst_entry *dst = NULL;
Michael Chana4636962009-06-08 18:14:43 -07003690 struct net_device *realdev;
Michael Chan9b093362010-12-23 07:42:56 +00003691 __be16 local_port;
3692 u32 port_id;
Michael Chana4636962009-06-08 18:14:43 -07003693
3694 if (saddr->local.v6.sin6_family == AF_INET6 &&
3695 saddr->remote.v6.sin6_family == AF_INET6)
3696 is_v6 = 1;
3697 else if (saddr->local.v4.sin_family == AF_INET &&
3698 saddr->remote.v4.sin_family == AF_INET)
3699 is_v6 = 0;
3700 else
3701 return -EINVAL;
3702
3703 clear_bit(SK_F_IPV6, &csk->flags);
3704
3705 if (is_v6) {
Michael Chana4636962009-06-08 18:14:43 -07003706 set_bit(SK_F_IPV6, &csk->flags);
Michael Chanc76284a2010-02-24 14:42:07 +00003707 cnic_get_v6_route(&saddr->remote.v6, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003708
3709 memcpy(&csk->dst_ip[0], &saddr->remote.v6.sin6_addr,
3710 sizeof(struct in6_addr));
3711 csk->dst_port = saddr->remote.v6.sin6_port;
3712 local_port = saddr->local.v6.sin6_port;
Michael Chana4636962009-06-08 18:14:43 -07003713
3714 } else {
Michael Chanc76284a2010-02-24 14:42:07 +00003715 cnic_get_v4_route(&saddr->remote.v4, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003716
3717 csk->dst_ip[0] = saddr->remote.v4.sin_addr.s_addr;
3718 csk->dst_port = saddr->remote.v4.sin_port;
3719 local_port = saddr->local.v4.sin_port;
3720 }
3721
Michael Chanc76284a2010-02-24 14:42:07 +00003722 csk->vlan_id = 0;
3723 csk->mtu = dev->netdev->mtu;
3724 if (dst && dst->dev) {
3725 u16 vlan = cnic_get_vlan(dst->dev, &realdev);
3726 if (realdev == dev->netdev) {
3727 csk->vlan_id = vlan;
3728 csk->mtu = dst_mtu(dst);
3729 }
3730 }
Michael Chana4636962009-06-08 18:14:43 -07003731
Michael Chan9b093362010-12-23 07:42:56 +00003732 port_id = be16_to_cpu(local_port);
3733 if (port_id >= CNIC_LOCAL_PORT_MIN &&
3734 port_id < CNIC_LOCAL_PORT_MAX) {
3735 if (cnic_alloc_id(&cp->csk_port_tbl, port_id))
3736 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003737 } else
Michael Chan9b093362010-12-23 07:42:56 +00003738 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003739
Michael Chan9b093362010-12-23 07:42:56 +00003740 if (!port_id) {
3741 port_id = cnic_alloc_new_id(&cp->csk_port_tbl);
3742 if (port_id == -1) {
Michael Chana4636962009-06-08 18:14:43 -07003743 rc = -ENOMEM;
3744 goto err_out;
3745 }
Michael Chan9b093362010-12-23 07:42:56 +00003746 local_port = cpu_to_be16(port_id);
Michael Chana4636962009-06-08 18:14:43 -07003747 }
3748 csk->src_port = local_port;
3749
Michael Chana4636962009-06-08 18:14:43 -07003750err_out:
3751 dst_release(dst);
3752 return rc;
3753}
3754
3755static void cnic_init_csk_state(struct cnic_sock *csk)
3756{
3757 csk->state = 0;
3758 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3759 clear_bit(SK_F_CLOSING, &csk->flags);
3760}
3761
3762static int cnic_cm_connect(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3763{
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003764 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07003765 int err = 0;
3766
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003767 if (cp->ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI)
3768 return -EOPNOTSUPP;
3769
Michael Chana4636962009-06-08 18:14:43 -07003770 if (!cnic_in_use(csk))
3771 return -EINVAL;
3772
3773 if (test_and_set_bit(SK_F_CONNECT_START, &csk->flags))
3774 return -EINVAL;
3775
3776 cnic_init_csk_state(csk);
3777
3778 err = cnic_get_route(csk, saddr);
3779 if (err)
3780 goto err_out;
3781
3782 err = cnic_resolve_addr(csk, saddr);
3783 if (!err)
3784 return 0;
3785
3786err_out:
3787 clear_bit(SK_F_CONNECT_START, &csk->flags);
3788 return err;
3789}
3790
3791static int cnic_cm_abort(struct cnic_sock *csk)
3792{
3793 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chan7b34a462010-06-15 08:57:03 +00003794 u32 opcode = L4_KCQE_OPCODE_VALUE_RESET_COMP;
Michael Chana4636962009-06-08 18:14:43 -07003795
3796 if (!cnic_in_use(csk))
3797 return -EINVAL;
3798
3799 if (cnic_abort_prep(csk))
3800 return cnic_cm_abort_req(csk);
3801
3802 /* Getting here means that we haven't started connect, or
3803 * connect was not successful.
3804 */
3805
Michael Chana4636962009-06-08 18:14:43 -07003806 cp->close_conn(csk, opcode);
Michael Chan7b34a462010-06-15 08:57:03 +00003807 if (csk->state != opcode)
3808 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003809
3810 return 0;
3811}
3812
3813static int cnic_cm_close(struct cnic_sock *csk)
3814{
3815 if (!cnic_in_use(csk))
3816 return -EINVAL;
3817
3818 if (cnic_close_prep(csk)) {
3819 csk->state = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
3820 return cnic_cm_close_req(csk);
Michael Chaned99daa52010-06-15 08:57:00 +00003821 } else {
3822 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003823 }
3824 return 0;
3825}
3826
3827static void cnic_cm_upcall(struct cnic_local *cp, struct cnic_sock *csk,
3828 u8 opcode)
3829{
3830 struct cnic_ulp_ops *ulp_ops;
3831 int ulp_type = csk->ulp_type;
3832
3833 rcu_read_lock();
3834 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
3835 if (ulp_ops) {
3836 if (opcode == L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE)
3837 ulp_ops->cm_connect_complete(csk);
3838 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3839 ulp_ops->cm_close_complete(csk);
3840 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED)
3841 ulp_ops->cm_remote_abort(csk);
3842 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_COMP)
3843 ulp_ops->cm_abort_complete(csk);
3844 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED)
3845 ulp_ops->cm_remote_close(csk);
3846 }
3847 rcu_read_unlock();
3848}
3849
3850static int cnic_cm_set_pg(struct cnic_sock *csk)
3851{
3852 if (cnic_offld_prep(csk)) {
3853 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3854 cnic_cm_update_pg(csk);
3855 else
3856 cnic_cm_offload_pg(csk);
3857 }
3858 return 0;
3859}
3860
3861static void cnic_cm_process_offld_pg(struct cnic_dev *dev, struct l4_kcq *kcqe)
3862{
3863 struct cnic_local *cp = dev->cnic_priv;
3864 u32 l5_cid = kcqe->pg_host_opaque;
3865 u8 opcode = kcqe->op_code;
3866 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
3867
3868 csk_hold(csk);
3869 if (!cnic_in_use(csk))
3870 goto done;
3871
3872 if (opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3873 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3874 goto done;
3875 }
Eddie Waia9736c02010-02-24 14:42:04 +00003876 /* Possible PG kcqe status: SUCCESS, OFFLOADED_PG, or CTX_ALLOC_FAIL */
3877 if (kcqe->status == L4_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAIL) {
3878 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3879 cnic_cm_upcall(cp, csk,
3880 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3881 goto done;
3882 }
3883
Michael Chana4636962009-06-08 18:14:43 -07003884 csk->pg_cid = kcqe->pg_cid;
3885 set_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3886 cnic_cm_conn_req(csk);
3887
3888done:
3889 csk_put(csk);
3890}
3891
Michael Chane1928c82010-12-23 07:43:04 +00003892static void cnic_process_fcoe_term_conn(struct cnic_dev *dev, struct kcqe *kcqe)
3893{
3894 struct cnic_local *cp = dev->cnic_priv;
3895 struct fcoe_kcqe *fc_kcqe = (struct fcoe_kcqe *) kcqe;
3896 u32 l5_cid = fc_kcqe->fcoe_conn_id + BNX2X_FCOE_L5_CID_BASE;
3897 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3898
3899 ctx->timestamp = jiffies;
3900 ctx->wait_cond = 1;
3901 wake_up(&ctx->waitq);
3902}
3903
Michael Chana4636962009-06-08 18:14:43 -07003904static void cnic_cm_process_kcqe(struct cnic_dev *dev, struct kcqe *kcqe)
3905{
3906 struct cnic_local *cp = dev->cnic_priv;
3907 struct l4_kcq *l4kcqe = (struct l4_kcq *) kcqe;
3908 u8 opcode = l4kcqe->op_code;
3909 u32 l5_cid;
3910 struct cnic_sock *csk;
3911
Michael Chane1928c82010-12-23 07:43:04 +00003912 if (opcode == FCOE_RAMROD_CMD_ID_TERMINATE_CONN) {
3913 cnic_process_fcoe_term_conn(dev, kcqe);
3914 return;
3915 }
Michael Chana4636962009-06-08 18:14:43 -07003916 if (opcode == L4_KCQE_OPCODE_VALUE_OFFLOAD_PG ||
3917 opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3918 cnic_cm_process_offld_pg(dev, l4kcqe);
3919 return;
3920 }
3921
3922 l5_cid = l4kcqe->conn_id;
3923 if (opcode & 0x80)
3924 l5_cid = l4kcqe->cid;
3925 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3926 return;
3927
3928 csk = &cp->csk_tbl[l5_cid];
3929 csk_hold(csk);
3930
3931 if (!cnic_in_use(csk)) {
3932 csk_put(csk);
3933 return;
3934 }
3935
3936 switch (opcode) {
Eddie Waia9736c02010-02-24 14:42:04 +00003937 case L5CM_RAMROD_CMD_ID_TCP_CONNECT:
3938 if (l4kcqe->status != 0) {
3939 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3940 cnic_cm_upcall(cp, csk,
3941 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3942 }
3943 break;
Michael Chana4636962009-06-08 18:14:43 -07003944 case L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE:
3945 if (l4kcqe->status == 0)
3946 set_bit(SK_F_OFFLD_COMPLETE, &csk->flags);
Michael Chan8ec3e702012-03-21 15:38:34 +00003947 else if (l4kcqe->status ==
3948 L4_KCQE_COMPLETION_STATUS_PARITY_ERROR)
Jeffrey Huang0cb1f4b2012-02-08 17:33:56 +00003949 set_bit(SK_F_HW_ERR, &csk->flags);
Michael Chana4636962009-06-08 18:14:43 -07003950
3951 smp_mb__before_clear_bit();
3952 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3953 cnic_cm_upcall(cp, csk, opcode);
3954 break;
3955
Eddie Wai7bc910f2012-06-27 15:08:22 +00003956 case L5CM_RAMROD_CMD_ID_CLOSE:
3957 if (l4kcqe->status != 0) {
3958 netdev_warn(dev->netdev, "RAMROD CLOSE compl with "
3959 "status 0x%x\n", l4kcqe->status);
3960 opcode = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
3961 /* Fall through */
3962 } else {
3963 break;
3964 }
Michael Chana4636962009-06-08 18:14:43 -07003965 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
Michael Chana4636962009-06-08 18:14:43 -07003966 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3967 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan71034ba2009-10-10 13:46:59 +00003968 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3969 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
Michael Chan8ec3e702012-03-21 15:38:34 +00003970 if (l4kcqe->status == L4_KCQE_COMPLETION_STATUS_PARITY_ERROR)
Michael Chan23021c22012-01-04 12:12:28 +00003971 set_bit(SK_F_HW_ERR, &csk->flags);
3972
Michael Chana4636962009-06-08 18:14:43 -07003973 cp->close_conn(csk, opcode);
3974 break;
3975
3976 case L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED:
Michael Chan101c40c2011-06-08 19:29:33 +00003977 /* after we already sent CLOSE_REQ */
3978 if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) &&
3979 !test_bit(SK_F_OFFLD_COMPLETE, &csk->flags) &&
3980 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3981 cp->close_conn(csk, L4_KCQE_OPCODE_VALUE_RESET_COMP);
3982 else
3983 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07003984 break;
3985 }
3986 csk_put(csk);
3987}
3988
3989static void cnic_cm_indicate_kcqe(void *data, struct kcqe *kcqe[], u32 num)
3990{
3991 struct cnic_dev *dev = data;
3992 int i;
3993
3994 for (i = 0; i < num; i++)
3995 cnic_cm_process_kcqe(dev, kcqe[i]);
3996}
3997
3998static struct cnic_ulp_ops cm_ulp_ops = {
3999 .indicate_kcqes = cnic_cm_indicate_kcqe,
4000};
4001
4002static void cnic_cm_free_mem(struct cnic_dev *dev)
4003{
4004 struct cnic_local *cp = dev->cnic_priv;
4005
4006 kfree(cp->csk_tbl);
4007 cp->csk_tbl = NULL;
4008 cnic_free_id_tbl(&cp->csk_port_tbl);
4009}
4010
4011static int cnic_cm_alloc_mem(struct cnic_dev *dev)
4012{
4013 struct cnic_local *cp = dev->cnic_priv;
Eddie Wai11f23aa2011-06-08 19:29:34 +00004014 u32 port_id;
Michael Chana4636962009-06-08 18:14:43 -07004015
4016 cp->csk_tbl = kzalloc(sizeof(struct cnic_sock) * MAX_CM_SK_TBL_SZ,
4017 GFP_KERNEL);
4018 if (!cp->csk_tbl)
4019 return -ENOMEM;
4020
Michael Chan973e5742011-07-13 17:24:17 +00004021 port_id = random32();
Eddie Wai11f23aa2011-06-08 19:29:34 +00004022 port_id %= CNIC_LOCAL_PORT_RANGE;
Michael Chana4636962009-06-08 18:14:43 -07004023 if (cnic_init_id_tbl(&cp->csk_port_tbl, CNIC_LOCAL_PORT_RANGE,
Eddie Wai11f23aa2011-06-08 19:29:34 +00004024 CNIC_LOCAL_PORT_MIN, port_id)) {
Michael Chana4636962009-06-08 18:14:43 -07004025 cnic_cm_free_mem(dev);
4026 return -ENOMEM;
4027 }
4028 return 0;
4029}
4030
4031static int cnic_ready_to_close(struct cnic_sock *csk, u32 opcode)
4032{
Michael Chan943189f2010-06-15 08:57:02 +00004033 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
4034 /* Unsolicited RESET_COMP or RESET_RECEIVED */
4035 opcode = L4_KCQE_OPCODE_VALUE_RESET_RECEIVED;
4036 csk->state = opcode;
Michael Chana1e621b2010-06-15 08:57:01 +00004037 }
Michael Chan943189f2010-06-15 08:57:02 +00004038
4039 /* 1. If event opcode matches the expected event in csk->state
Michael Chan101c40c2011-06-08 19:29:33 +00004040 * 2. If the expected event is CLOSE_COMP or RESET_COMP, we accept any
4041 * event
Michael Chan7b34a462010-06-15 08:57:03 +00004042 * 3. If the expected event is 0, meaning the connection was never
4043 * never established, we accept the opcode from cm_abort.
Michael Chan943189f2010-06-15 08:57:02 +00004044 */
Michael Chan7b34a462010-06-15 08:57:03 +00004045 if (opcode == csk->state || csk->state == 0 ||
Michael Chan101c40c2011-06-08 19:29:33 +00004046 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP ||
4047 csk->state == L4_KCQE_OPCODE_VALUE_RESET_COMP) {
Michael Chan7b34a462010-06-15 08:57:03 +00004048 if (!test_and_set_bit(SK_F_CLOSING, &csk->flags)) {
4049 if (csk->state == 0)
4050 csk->state = opcode;
Michael Chana4636962009-06-08 18:14:43 -07004051 return 1;
Michael Chan7b34a462010-06-15 08:57:03 +00004052 }
Michael Chana4636962009-06-08 18:14:43 -07004053 }
4054 return 0;
4055}
4056
4057static void cnic_close_bnx2_conn(struct cnic_sock *csk, u32 opcode)
4058{
4059 struct cnic_dev *dev = csk->dev;
4060 struct cnic_local *cp = dev->cnic_priv;
4061
Michael Chana1e621b2010-06-15 08:57:01 +00004062 if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED) {
4063 cnic_cm_upcall(cp, csk, opcode);
4064 return;
4065 }
4066
Michael Chana4636962009-06-08 18:14:43 -07004067 clear_bit(SK_F_CONNECT_START, &csk->flags);
Eddie Wai66883e92010-02-24 14:42:05 +00004068 cnic_close_conn(csk);
Michael Chan7b34a462010-06-15 08:57:03 +00004069 csk->state = opcode;
Eddie Wai66883e92010-02-24 14:42:05 +00004070 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07004071}
4072
4073static void cnic_cm_stop_bnx2_hw(struct cnic_dev *dev)
4074{
4075}
4076
4077static int cnic_cm_init_bnx2_hw(struct cnic_dev *dev)
4078{
4079 u32 seed;
4080
Michael Chan973e5742011-07-13 17:24:17 +00004081 seed = random32();
Michael Chana4636962009-06-08 18:14:43 -07004082 cnic_ctx_wr(dev, 45, 0, seed);
4083 return 0;
4084}
4085
Michael Chan71034ba2009-10-10 13:46:59 +00004086static void cnic_close_bnx2x_conn(struct cnic_sock *csk, u32 opcode)
4087{
4088 struct cnic_dev *dev = csk->dev;
4089 struct cnic_local *cp = dev->cnic_priv;
4090 struct cnic_context *ctx = &cp->ctx_tbl[csk->l5_cid];
4091 union l5cm_specific_data l5_data;
4092 u32 cmd = 0;
4093 int close_complete = 0;
4094
4095 switch (opcode) {
4096 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
4097 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
4098 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan7b34a462010-06-15 08:57:03 +00004099 if (cnic_ready_to_close(csk, opcode)) {
Michael Chan23021c22012-01-04 12:12:28 +00004100 if (test_bit(SK_F_HW_ERR, &csk->flags))
4101 close_complete = 1;
4102 else if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
Michael Chan7b34a462010-06-15 08:57:03 +00004103 cmd = L5CM_RAMROD_CMD_ID_SEARCHER_DELETE;
4104 else
4105 close_complete = 1;
4106 }
Michael Chan71034ba2009-10-10 13:46:59 +00004107 break;
4108 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
4109 cmd = L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD;
4110 break;
4111 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
4112 close_complete = 1;
4113 break;
4114 }
4115 if (cmd) {
4116 memset(&l5_data, 0, sizeof(l5_data));
4117
4118 cnic_submit_kwqe_16(dev, cmd, csk->cid, ISCSI_CONNECTION_TYPE,
4119 &l5_data);
4120 } else if (close_complete) {
4121 ctx->timestamp = jiffies;
4122 cnic_close_conn(csk);
4123 cnic_cm_upcall(cp, csk, csk->state);
4124 }
4125}
4126
4127static void cnic_cm_stop_bnx2x_hw(struct cnic_dev *dev)
4128{
Michael Chanfdf24082010-10-13 14:06:47 +00004129 struct cnic_local *cp = dev->cnic_priv;
Michael Chanfdf24082010-10-13 14:06:47 +00004130
4131 if (!cp->ctx_tbl)
4132 return;
4133
4134 if (!netif_running(dev->netdev))
4135 return;
4136
Michael Chan74e49bb2011-07-20 14:55:23 +00004137 cnic_bnx2x_delete_wait(dev, 0);
Michael Chanfdf24082010-10-13 14:06:47 +00004138
4139 cancel_delayed_work(&cp->delete_task);
4140 flush_workqueue(cnic_wq);
4141
4142 if (atomic_read(&cp->iscsi_conn) != 0)
4143 netdev_warn(dev->netdev, "%d iSCSI connections not destroyed\n",
4144 atomic_read(&cp->iscsi_conn));
Michael Chan71034ba2009-10-10 13:46:59 +00004145}
4146
4147static int cnic_cm_init_bnx2x_hw(struct cnic_dev *dev)
4148{
4149 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00004150 u32 pfid = cp->pfid;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004151 u32 port = CNIC_PORT(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00004152
4153 cnic_init_bnx2x_mac(dev);
4154 cnic_bnx2x_set_tcp_timestamp(dev, 1);
4155
4156 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004157 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(pfid), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004158
4159 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004160 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_ENABLED_OFFSET(port), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00004161 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004162 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_MAX_COUNT_OFFSET(port),
Michael Chan71034ba2009-10-10 13:46:59 +00004163 DEF_MAX_DA_COUNT);
4164
4165 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004166 XSTORM_ISCSI_TCP_VARS_TTL_OFFSET(pfid), DEF_TTL);
Michael Chan71034ba2009-10-10 13:46:59 +00004167 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004168 XSTORM_ISCSI_TCP_VARS_TOS_OFFSET(pfid), DEF_TOS);
Michael Chan71034ba2009-10-10 13:46:59 +00004169 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004170 XSTORM_ISCSI_TCP_VARS_ADV_WND_SCL_OFFSET(pfid), 2);
Michael Chan71034ba2009-10-10 13:46:59 +00004171 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004172 XSTORM_TCP_TX_SWS_TIMER_VAL_OFFSET(pfid), DEF_SWS_TIMER);
Michael Chan71034ba2009-10-10 13:46:59 +00004173
Michael Chan14203982010-10-06 03:16:06 +00004174 CNIC_WR(dev, BAR_TSTRORM_INTMEM + TSTORM_TCP_MAX_CWND_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00004175 DEF_MAX_CWND);
4176 return 0;
4177}
4178
Michael Chanfdf24082010-10-13 14:06:47 +00004179static void cnic_delete_task(struct work_struct *work)
4180{
4181 struct cnic_local *cp;
4182 struct cnic_dev *dev;
4183 u32 i;
4184 int need_resched = 0;
4185
4186 cp = container_of(work, struct cnic_local, delete_task.work);
4187 dev = cp->dev;
4188
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07004189 if (test_and_clear_bit(CNIC_LCL_FL_STOP_ISCSI, &cp->cnic_local_flags)) {
4190 struct drv_ctl_info info;
4191
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07004192 cnic_ulp_stop_one(cp, CNIC_ULP_ISCSI);
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07004193
4194 info.cmd = DRV_CTL_ISCSI_STOPPED_CMD;
4195 cp->ethdev->drv_ctl(dev->netdev, &info);
4196 }
4197
Michael Chanfdf24082010-10-13 14:06:47 +00004198 for (i = 0; i < cp->max_cid_space; i++) {
4199 struct cnic_context *ctx = &cp->ctx_tbl[i];
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004200 int err;
Michael Chanfdf24082010-10-13 14:06:47 +00004201
4202 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags) ||
4203 !test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
4204 continue;
4205
4206 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
4207 need_resched = 1;
4208 continue;
4209 }
4210
4211 if (!test_and_clear_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
4212 continue;
4213
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004214 err = cnic_bnx2x_destroy_ramrod(dev, i);
Michael Chanfdf24082010-10-13 14:06:47 +00004215
4216 cnic_free_bnx2x_conn_resc(dev, i);
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004217 if (!err) {
4218 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI)
4219 atomic_dec(&cp->iscsi_conn);
Michael Chanfdf24082010-10-13 14:06:47 +00004220
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004221 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
4222 }
Michael Chanfdf24082010-10-13 14:06:47 +00004223 }
4224
4225 if (need_resched)
4226 queue_delayed_work(cnic_wq, &cp->delete_task,
4227 msecs_to_jiffies(10));
4228
4229}
4230
Michael Chana4636962009-06-08 18:14:43 -07004231static int cnic_cm_open(struct cnic_dev *dev)
4232{
4233 struct cnic_local *cp = dev->cnic_priv;
4234 int err;
4235
4236 err = cnic_cm_alloc_mem(dev);
4237 if (err)
4238 return err;
4239
4240 err = cp->start_cm(dev);
4241
4242 if (err)
4243 goto err_out;
4244
Michael Chanfdf24082010-10-13 14:06:47 +00004245 INIT_DELAYED_WORK(&cp->delete_task, cnic_delete_task);
4246
Michael Chana4636962009-06-08 18:14:43 -07004247 dev->cm_create = cnic_cm_create;
4248 dev->cm_destroy = cnic_cm_destroy;
4249 dev->cm_connect = cnic_cm_connect;
4250 dev->cm_abort = cnic_cm_abort;
4251 dev->cm_close = cnic_cm_close;
4252 dev->cm_select_dev = cnic_cm_select_dev;
4253
4254 cp->ulp_handle[CNIC_ULP_L4] = dev;
4255 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], &cm_ulp_ops);
4256 return 0;
4257
4258err_out:
4259 cnic_cm_free_mem(dev);
4260 return err;
4261}
4262
4263static int cnic_cm_shutdown(struct cnic_dev *dev)
4264{
4265 struct cnic_local *cp = dev->cnic_priv;
4266 int i;
4267
Michael Chana4636962009-06-08 18:14:43 -07004268 if (!cp->csk_tbl)
4269 return 0;
4270
4271 for (i = 0; i < MAX_CM_SK_TBL_SZ; i++) {
4272 struct cnic_sock *csk = &cp->csk_tbl[i];
4273
4274 clear_bit(SK_F_INUSE, &csk->flags);
4275 cnic_cm_cleanup(csk);
4276 }
4277 cnic_cm_free_mem(dev);
4278
4279 return 0;
4280}
4281
4282static void cnic_init_context(struct cnic_dev *dev, u32 cid)
4283{
Michael Chana4636962009-06-08 18:14:43 -07004284 u32 cid_addr;
4285 int i;
4286
Michael Chana4636962009-06-08 18:14:43 -07004287 cid_addr = GET_CID_ADDR(cid);
4288
4289 for (i = 0; i < CTX_SIZE; i += 4)
4290 cnic_ctx_wr(dev, cid_addr, i, 0);
4291}
4292
4293static int cnic_setup_5709_context(struct cnic_dev *dev, int valid)
4294{
4295 struct cnic_local *cp = dev->cnic_priv;
4296 int ret = 0, i;
4297 u32 valid_bit = valid ? BNX2_CTX_HOST_PAGE_TBL_DATA0_VALID : 0;
4298
4299 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4300 return 0;
4301
4302 for (i = 0; i < cp->ctx_blks; i++) {
4303 int j;
4304 u32 idx = cp->ctx_arr[i].cid / cp->cids_per_blk;
4305 u32 val;
4306
4307 memset(cp->ctx_arr[i].ctx, 0, BCM_PAGE_SIZE);
4308
4309 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA0,
4310 (cp->ctx_arr[i].mapping & 0xffffffff) | valid_bit);
4311 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA1,
4312 (u64) cp->ctx_arr[i].mapping >> 32);
4313 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL, idx |
4314 BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
4315 for (j = 0; j < 10; j++) {
4316
4317 val = CNIC_RD(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL);
4318 if (!(val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ))
4319 break;
4320 udelay(5);
4321 }
4322 if (val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) {
4323 ret = -EBUSY;
4324 break;
4325 }
4326 }
4327 return ret;
4328}
4329
4330static void cnic_free_irq(struct cnic_dev *dev)
4331{
4332 struct cnic_local *cp = dev->cnic_priv;
4333 struct cnic_eth_dev *ethdev = cp->ethdev;
4334
4335 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4336 cp->disable_int_sync(dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004337 tasklet_kill(&cp->cnic_irq_task);
Michael Chana4636962009-06-08 18:14:43 -07004338 free_irq(ethdev->irq_arr[0].vector, dev);
4339 }
4340}
4341
Michael Chan6e0dc642010-10-13 14:06:44 +00004342static int cnic_request_irq(struct cnic_dev *dev)
4343{
4344 struct cnic_local *cp = dev->cnic_priv;
4345 struct cnic_eth_dev *ethdev = cp->ethdev;
4346 int err;
4347
4348 err = request_irq(ethdev->irq_arr[0].vector, cnic_irq, 0, "cnic", dev);
4349 if (err)
4350 tasklet_disable(&cp->cnic_irq_task);
4351
4352 return err;
4353}
4354
Michael Chana4636962009-06-08 18:14:43 -07004355static int cnic_init_bnx2_irq(struct cnic_dev *dev)
4356{
4357 struct cnic_local *cp = dev->cnic_priv;
4358 struct cnic_eth_dev *ethdev = cp->ethdev;
4359
4360 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4361 int err, i = 0;
4362 int sblk_num = cp->status_blk_num;
4363 u32 base = ((sblk_num - 1) * BNX2_HC_SB_CONFIG_SIZE) +
4364 BNX2_HC_SB_CONFIG_1;
4365
4366 CNIC_WR(dev, base, BNX2_HC_SB_CONFIG_1_ONE_SHOT);
4367
4368 CNIC_WR(dev, base + BNX2_HC_COMP_PROD_TRIP_OFF, (2 << 16) | 8);
4369 CNIC_WR(dev, base + BNX2_HC_COM_TICKS_OFF, (64 << 16) | 220);
4370 CNIC_WR(dev, base + BNX2_HC_CMD_TICKS_OFF, (64 << 16) | 220);
4371
Michael Chana4dde3a2010-02-24 14:42:08 +00004372 cp->last_status_idx = cp->status_blk.bnx2->status_idx;
Joe Perches164165d2009-11-19 09:30:10 +00004373 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2_msix,
Michael Chana4636962009-06-08 18:14:43 -07004374 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004375 err = cnic_request_irq(dev);
4376 if (err)
Michael Chana4636962009-06-08 18:14:43 -07004377 return err;
Michael Chan6e0dc642010-10-13 14:06:44 +00004378
Michael Chana4dde3a2010-02-24 14:42:08 +00004379 while (cp->status_blk.bnx2->status_completion_producer_index &&
Michael Chana4636962009-06-08 18:14:43 -07004380 i < 10) {
4381 CNIC_WR(dev, BNX2_HC_COALESCE_NOW,
4382 1 << (11 + sblk_num));
4383 udelay(10);
4384 i++;
4385 barrier();
4386 }
Michael Chana4dde3a2010-02-24 14:42:08 +00004387 if (cp->status_blk.bnx2->status_completion_producer_index) {
Michael Chana4636962009-06-08 18:14:43 -07004388 cnic_free_irq(dev);
4389 goto failed;
4390 }
4391
4392 } else {
Michael Chana4dde3a2010-02-24 14:42:08 +00004393 struct status_block *sblk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004394 u32 hc_cmd = CNIC_RD(dev, BNX2_HC_COMMAND);
4395 int i = 0;
4396
4397 while (sblk->status_completion_producer_index && i < 10) {
4398 CNIC_WR(dev, BNX2_HC_COMMAND,
4399 hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
4400 udelay(10);
4401 i++;
4402 barrier();
4403 }
4404 if (sblk->status_completion_producer_index)
4405 goto failed;
4406
4407 }
4408 return 0;
4409
4410failed:
Joe Perchesddf79b22010-02-17 15:01:54 +00004411 netdev_err(dev->netdev, "KCQ index not resetting to 0\n");
Michael Chana4636962009-06-08 18:14:43 -07004412 return -EBUSY;
4413}
4414
4415static void cnic_enable_bnx2_int(struct cnic_dev *dev)
4416{
4417 struct cnic_local *cp = dev->cnic_priv;
4418 struct cnic_eth_dev *ethdev = cp->ethdev;
4419
4420 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4421 return;
4422
4423 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4424 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
4425}
4426
4427static void cnic_disable_bnx2_int_sync(struct cnic_dev *dev)
4428{
4429 struct cnic_local *cp = dev->cnic_priv;
4430 struct cnic_eth_dev *ethdev = cp->ethdev;
4431
4432 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4433 return;
4434
4435 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4436 BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
4437 CNIC_RD(dev, BNX2_PCICFG_INT_ACK_CMD);
4438 synchronize_irq(ethdev->irq_arr[0].vector);
4439}
4440
4441static void cnic_init_bnx2_tx_ring(struct cnic_dev *dev)
4442{
4443 struct cnic_local *cp = dev->cnic_priv;
4444 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004445 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004446 u32 cid_addr, tx_cid, sb_id;
4447 u32 val, offset0, offset1, offset2, offset3;
4448 int i;
4449 struct tx_bd *txbd;
Michael Chancd801532010-10-13 14:06:49 +00004450 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Michael Chana4dde3a2010-02-24 14:42:08 +00004451 struct status_block *s_blk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004452
4453 sb_id = cp->status_blk_num;
4454 tx_cid = 20;
Michael Chana4636962009-06-08 18:14:43 -07004455 cp->tx_cons_ptr = &s_blk->status_tx_quick_consumer_index2;
4456 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004457 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004458
4459 tx_cid = TX_TSS_CID + sb_id - 1;
Michael Chana4636962009-06-08 18:14:43 -07004460 CNIC_WR(dev, BNX2_TSCH_TSS_CFG, (sb_id << 24) |
4461 (TX_TSS_CID << 7));
4462 cp->tx_cons_ptr = &sblk->status_tx_quick_consumer_index;
4463 }
4464 cp->tx_cons = *cp->tx_cons_ptr;
4465
4466 cid_addr = GET_CID_ADDR(tx_cid);
4467 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
4468 u32 cid_addr2 = GET_CID_ADDR(tx_cid + 4) + 0x40;
4469
4470 for (i = 0; i < PHY_CTX_SIZE; i += 4)
4471 cnic_ctx_wr(dev, cid_addr2, i, 0);
4472
4473 offset0 = BNX2_L2CTX_TYPE_XI;
4474 offset1 = BNX2_L2CTX_CMD_TYPE_XI;
4475 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI_XI;
4476 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO_XI;
4477 } else {
Michael Chanb58ffb42010-05-27 16:31:41 -07004478 cnic_init_context(dev, tx_cid);
4479 cnic_init_context(dev, tx_cid + 1);
4480
Michael Chana4636962009-06-08 18:14:43 -07004481 offset0 = BNX2_L2CTX_TYPE;
4482 offset1 = BNX2_L2CTX_CMD_TYPE;
4483 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI;
4484 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO;
4485 }
4486 val = BNX2_L2CTX_TYPE_TYPE_L2 | BNX2_L2CTX_TYPE_SIZE_L2;
4487 cnic_ctx_wr(dev, cid_addr, offset0, val);
4488
4489 val = BNX2_L2CTX_CMD_TYPE_TYPE_L2 | (8 << 16);
4490 cnic_ctx_wr(dev, cid_addr, offset1, val);
4491
Joe Perches43d620c2011-06-16 19:08:06 +00004492 txbd = udev->l2_ring;
Michael Chana4636962009-06-08 18:14:43 -07004493
Michael Chancd801532010-10-13 14:06:49 +00004494 buf_map = udev->l2_buf_map;
Michael Chana4636962009-06-08 18:14:43 -07004495 for (i = 0; i < MAX_TX_DESC_CNT; i++, txbd++) {
4496 txbd->tx_bd_haddr_hi = (u64) buf_map >> 32;
4497 txbd->tx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4498 }
Michael Chancd801532010-10-13 14:06:49 +00004499 val = (u64) ring_map >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004500 cnic_ctx_wr(dev, cid_addr, offset2, val);
4501 txbd->tx_bd_haddr_hi = val;
4502
Michael Chancd801532010-10-13 14:06:49 +00004503 val = (u64) ring_map & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004504 cnic_ctx_wr(dev, cid_addr, offset3, val);
4505 txbd->tx_bd_haddr_lo = val;
4506}
4507
4508static void cnic_init_bnx2_rx_ring(struct cnic_dev *dev)
4509{
4510 struct cnic_local *cp = dev->cnic_priv;
4511 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004512 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004513 u32 cid_addr, sb_id, val, coal_reg, coal_val;
4514 int i;
4515 struct rx_bd *rxbd;
Michael Chana4dde3a2010-02-24 14:42:08 +00004516 struct status_block *s_blk = cp->status_blk.gen;
Michael Chancd801532010-10-13 14:06:49 +00004517 dma_addr_t ring_map = udev->l2_ring_map;
Michael Chana4636962009-06-08 18:14:43 -07004518
4519 sb_id = cp->status_blk_num;
4520 cnic_init_context(dev, 2);
4521 cp->rx_cons_ptr = &s_blk->status_rx_quick_consumer_index2;
4522 coal_reg = BNX2_HC_COMMAND;
4523 coal_val = CNIC_RD(dev, coal_reg);
4524 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004525 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004526
4527 cp->rx_cons_ptr = &sblk->status_rx_quick_consumer_index;
4528 coal_reg = BNX2_HC_COALESCE_NOW;
4529 coal_val = 1 << (11 + sb_id);
4530 }
4531 i = 0;
4532 while (!(*cp->rx_cons_ptr != 0) && i < 10) {
4533 CNIC_WR(dev, coal_reg, coal_val);
4534 udelay(10);
4535 i++;
4536 barrier();
4537 }
4538 cp->rx_cons = *cp->rx_cons_ptr;
4539
4540 cid_addr = GET_CID_ADDR(2);
4541 val = BNX2_L2CTX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
4542 BNX2_L2CTX_CTX_TYPE_SIZE_L2 | (0x02 << 8);
4543 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_CTX_TYPE, val);
4544
4545 if (sb_id == 0)
Michael Chand0549382009-10-28 03:41:59 -07004546 val = 2 << BNX2_L2CTX_L2_STATUSB_NUM_SHIFT;
Michael Chana4636962009-06-08 18:14:43 -07004547 else
Michael Chand0549382009-10-28 03:41:59 -07004548 val = BNX2_L2CTX_L2_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004549 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_HOST_BDIDX, val);
4550
Joe Perches43d620c2011-06-16 19:08:06 +00004551 rxbd = udev->l2_ring + BCM_PAGE_SIZE;
Michael Chana4636962009-06-08 18:14:43 -07004552 for (i = 0; i < MAX_RX_DESC_CNT; i++, rxbd++) {
4553 dma_addr_t buf_map;
4554 int n = (i % cp->l2_rx_ring_size) + 1;
4555
Michael Chancd801532010-10-13 14:06:49 +00004556 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chana4636962009-06-08 18:14:43 -07004557 rxbd->rx_bd_len = cp->l2_single_buf_size;
4558 rxbd->rx_bd_flags = RX_BD_FLAGS_START | RX_BD_FLAGS_END;
4559 rxbd->rx_bd_haddr_hi = (u64) buf_map >> 32;
4560 rxbd->rx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4561 }
Michael Chancd801532010-10-13 14:06:49 +00004562 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004563 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_HI, val);
4564 rxbd->rx_bd_haddr_hi = val;
4565
Michael Chancd801532010-10-13 14:06:49 +00004566 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004567 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_LO, val);
4568 rxbd->rx_bd_haddr_lo = val;
4569
4570 val = cnic_reg_rd_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD);
4571 cnic_reg_wr_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD, val | (1 << 2));
4572}
4573
4574static void cnic_shutdown_bnx2_rx_ring(struct cnic_dev *dev)
4575{
4576 struct kwqe *wqes[1], l2kwqe;
4577
4578 memset(&l2kwqe, 0, sizeof(l2kwqe));
4579 wqes[0] = &l2kwqe;
Michael Chane1928c82010-12-23 07:43:04 +00004580 l2kwqe.kwqe_op_flag = (L2_LAYER_CODE << KWQE_LAYER_SHIFT) |
Michael Chana4636962009-06-08 18:14:43 -07004581 (L2_KWQE_OPCODE_VALUE_FLUSH <<
4582 KWQE_OPCODE_SHIFT) | 2;
4583 dev->submit_kwqes(dev, wqes, 1);
4584}
4585
4586static void cnic_set_bnx2_mac(struct cnic_dev *dev)
4587{
4588 struct cnic_local *cp = dev->cnic_priv;
4589 u32 val;
4590
4591 val = cp->func << 2;
4592
4593 cp->shmem_base = cnic_reg_rd_ind(dev, BNX2_SHM_HDR_ADDR_0 + val);
4594
4595 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4596 BNX2_PORT_HW_CFG_ISCSI_MAC_UPPER);
4597 dev->mac_addr[0] = (u8) (val >> 8);
4598 dev->mac_addr[1] = (u8) val;
4599
4600 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH4, val);
4601
4602 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4603 BNX2_PORT_HW_CFG_ISCSI_MAC_LOWER);
4604 dev->mac_addr[2] = (u8) (val >> 24);
4605 dev->mac_addr[3] = (u8) (val >> 16);
4606 dev->mac_addr[4] = (u8) (val >> 8);
4607 dev->mac_addr[5] = (u8) val;
4608
4609 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH5, val);
4610
4611 val = 4 | BNX2_RPM_SORT_USER2_BC_EN;
4612 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4613 val |= BNX2_RPM_SORT_USER2_PROM_VLAN;
4614
4615 CNIC_WR(dev, BNX2_RPM_SORT_USER2, 0x0);
4616 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val);
4617 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val | BNX2_RPM_SORT_USER2_ENA);
4618}
4619
4620static int cnic_start_bnx2_hw(struct cnic_dev *dev)
4621{
4622 struct cnic_local *cp = dev->cnic_priv;
4623 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chana4dde3a2010-02-24 14:42:08 +00004624 struct status_block *sblk = cp->status_blk.gen;
Michael Chane6c28892010-06-24 14:58:39 +00004625 u32 val, kcq_cid_addr, kwq_cid_addr;
Michael Chana4636962009-06-08 18:14:43 -07004626 int err;
4627
4628 cnic_set_bnx2_mac(dev);
4629
4630 val = CNIC_RD(dev, BNX2_MQ_CONFIG);
4631 val &= ~BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE;
4632 if (BCM_PAGE_BITS > 12)
4633 val |= (12 - 8) << 4;
4634 else
4635 val |= (BCM_PAGE_BITS - 8) << 4;
4636
4637 CNIC_WR(dev, BNX2_MQ_CONFIG, val);
4638
4639 CNIC_WR(dev, BNX2_HC_COMP_PROD_TRIP, (2 << 16) | 8);
4640 CNIC_WR(dev, BNX2_HC_COM_TICKS, (64 << 16) | 220);
4641 CNIC_WR(dev, BNX2_HC_CMD_TICKS, (64 << 16) | 220);
4642
4643 err = cnic_setup_5709_context(dev, 1);
4644 if (err)
4645 return err;
4646
4647 cnic_init_context(dev, KWQ_CID);
4648 cnic_init_context(dev, KCQ_CID);
4649
Michael Chane6c28892010-06-24 14:58:39 +00004650 kwq_cid_addr = GET_CID_ADDR(KWQ_CID);
Michael Chana4636962009-06-08 18:14:43 -07004651 cp->kwq_io_addr = MB_GET_CID_ADDR(KWQ_CID) + L5_KRNLQ_HOST_QIDX;
4652
4653 cp->max_kwq_idx = MAX_KWQ_IDX;
4654 cp->kwq_prod_idx = 0;
4655 cp->kwq_con_idx = 0;
Michael Chan1f1332a2010-05-18 11:32:52 +00004656 set_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07004657
4658 if (CHIP_NUM(cp) == CHIP_NUM_5706 || CHIP_NUM(cp) == CHIP_NUM_5708)
4659 cp->kwq_con_idx_ptr = &sblk->status_rx_quick_consumer_index15;
4660 else
4661 cp->kwq_con_idx_ptr = &sblk->status_cmd_consumer_index;
4662
4663 /* Initialize the kernel work queue context. */
4664 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4665 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004666 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004667
4668 val = (BCM_PAGE_SIZE / sizeof(struct kwqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004669 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004670
4671 val = ((BCM_PAGE_SIZE / sizeof(struct kwqe)) << 16) | KWQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004672 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004673
4674 val = (u32) ((u64) cp->kwq_info.pgtbl_map >> 32);
Michael Chane6c28892010-06-24 14:58:39 +00004675 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004676
4677 val = (u32) cp->kwq_info.pgtbl_map;
Michael Chane6c28892010-06-24 14:58:39 +00004678 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004679
Michael Chane6c28892010-06-24 14:58:39 +00004680 kcq_cid_addr = GET_CID_ADDR(KCQ_CID);
4681 cp->kcq1.io_addr = MB_GET_CID_ADDR(KCQ_CID) + L5_KRNLQ_HOST_QIDX;
Michael Chana4636962009-06-08 18:14:43 -07004682
Michael Chane6c28892010-06-24 14:58:39 +00004683 cp->kcq1.sw_prod_idx = 0;
4684 cp->kcq1.hw_prod_idx_ptr =
Joe Perches64699332012-06-04 12:44:16 +00004685 &sblk->status_completion_producer_index;
Michael Chane6c28892010-06-24 14:58:39 +00004686
Joe Perches64699332012-06-04 12:44:16 +00004687 cp->kcq1.status_idx_ptr = &sblk->status_idx;
Michael Chana4636962009-06-08 18:14:43 -07004688
4689 /* Initialize the kernel complete queue context. */
4690 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4691 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004692 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004693
4694 val = (BCM_PAGE_SIZE / sizeof(struct kcqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004695 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004696
4697 val = ((BCM_PAGE_SIZE / sizeof(struct kcqe)) << 16) | KCQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004698 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004699
Michael Chane6c28892010-06-24 14:58:39 +00004700 val = (u32) ((u64) cp->kcq1.dma.pgtbl_map >> 32);
4701 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004702
Michael Chane6c28892010-06-24 14:58:39 +00004703 val = (u32) cp->kcq1.dma.pgtbl_map;
4704 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004705
4706 cp->int_num = 0;
4707 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chane6c28892010-06-24 14:58:39 +00004708 struct status_block_msix *msblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004709 u32 sb_id = cp->status_blk_num;
Michael Chand0549382009-10-28 03:41:59 -07004710 u32 sb = BNX2_L2CTX_L5_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004711
Michael Chane6c28892010-06-24 14:58:39 +00004712 cp->kcq1.hw_prod_idx_ptr =
Joe Perches64699332012-06-04 12:44:16 +00004713 &msblk->status_completion_producer_index;
4714 cp->kcq1.status_idx_ptr = &msblk->status_idx;
4715 cp->kwq_con_idx_ptr = &msblk->status_cmd_consumer_index;
Michael Chana4636962009-06-08 18:14:43 -07004716 cp->int_num = sb_id << BNX2_PCICFG_INT_ACK_CMD_INT_NUM_SHIFT;
Michael Chane6c28892010-06-24 14:58:39 +00004717 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
4718 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
Michael Chana4636962009-06-08 18:14:43 -07004719 }
4720
4721 /* Enable Commnad Scheduler notification when we write to the
4722 * host producer index of the kernel contexts. */
4723 CNIC_WR(dev, BNX2_MQ_KNL_CMD_MASK1, 2);
4724
4725 /* Enable Command Scheduler notification when we write to either
4726 * the Send Queue or Receive Queue producer indexes of the kernel
4727 * bypass contexts. */
4728 CNIC_WR(dev, BNX2_MQ_KNL_BYP_CMD_MASK1, 7);
4729 CNIC_WR(dev, BNX2_MQ_KNL_BYP_WRITE_MASK1, 7);
4730
4731 /* Notify COM when the driver post an application buffer. */
4732 CNIC_WR(dev, BNX2_MQ_KNL_RX_V2P_MASK2, 0x2000);
4733
4734 /* Set the CP and COM doorbells. These two processors polls the
4735 * doorbell for a non zero value before running. This must be done
4736 * after setting up the kernel queue contexts. */
4737 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 1);
4738 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 1);
4739
4740 cnic_init_bnx2_tx_ring(dev);
4741 cnic_init_bnx2_rx_ring(dev);
4742
4743 err = cnic_init_bnx2_irq(dev);
4744 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00004745 netdev_err(dev->netdev, "cnic_init_irq failed\n");
Michael Chana4636962009-06-08 18:14:43 -07004746 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
4747 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
4748 return err;
4749 }
4750
4751 return 0;
4752}
4753
Michael Chan71034ba2009-10-10 13:46:59 +00004754static void cnic_setup_bnx2x_context(struct cnic_dev *dev)
4755{
4756 struct cnic_local *cp = dev->cnic_priv;
4757 struct cnic_eth_dev *ethdev = cp->ethdev;
4758 u32 start_offset = ethdev->ctx_tbl_offset;
4759 int i;
4760
4761 for (i = 0; i < cp->ctx_blks; i++) {
4762 struct cnic_ctx *ctx = &cp->ctx_arr[i];
4763 dma_addr_t map = ctx->mapping;
4764
4765 if (cp->ctx_align) {
4766 unsigned long mask = cp->ctx_align - 1;
4767
4768 map = (map + mask) & ~mask;
4769 }
4770
4771 cnic_ctx_tbl_wr(dev, start_offset + i, map);
4772 }
4773}
4774
4775static int cnic_init_bnx2x_irq(struct cnic_dev *dev)
4776{
4777 struct cnic_local *cp = dev->cnic_priv;
4778 struct cnic_eth_dev *ethdev = cp->ethdev;
4779 int err = 0;
4780
Joe Perches164165d2009-11-19 09:30:10 +00004781 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2x_bh,
Michael Chan71034ba2009-10-10 13:46:59 +00004782 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004783 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
4784 err = cnic_request_irq(dev);
4785
Michael Chan71034ba2009-10-10 13:46:59 +00004786 return err;
4787}
4788
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004789static inline void cnic_storm_memset_hc_disable(struct cnic_dev *dev,
4790 u16 sb_id, u8 sb_index,
4791 u8 disable)
4792{
4793
4794 u32 addr = BAR_CSTRORM_INTMEM +
4795 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4796 offsetof(struct hc_status_block_data_e1x, index_data) +
4797 sizeof(struct hc_index_data)*sb_index +
4798 offsetof(struct hc_index_data, flags);
4799 u16 flags = CNIC_RD16(dev, addr);
4800 /* clear and set */
4801 flags &= ~HC_INDEX_DATA_HC_ENABLED;
4802 flags |= (((~disable) << HC_INDEX_DATA_HC_ENABLED_SHIFT) &
4803 HC_INDEX_DATA_HC_ENABLED);
4804 CNIC_WR16(dev, addr, flags);
4805}
4806
Michael Chan71034ba2009-10-10 13:46:59 +00004807static void cnic_enable_bnx2x_int(struct cnic_dev *dev)
4808{
4809 struct cnic_local *cp = dev->cnic_priv;
4810 u8 sb_id = cp->status_blk_num;
Michael Chan71034ba2009-10-10 13:46:59 +00004811
4812 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004813 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4814 offsetof(struct hc_status_block_data_e1x, index_data) +
4815 sizeof(struct hc_index_data)*HC_INDEX_ISCSI_EQ_CONS +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004816 offsetof(struct hc_index_data, timeout), 64 / 4);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004817 cnic_storm_memset_hc_disable(dev, sb_id, HC_INDEX_ISCSI_EQ_CONS, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004818}
4819
4820static void cnic_disable_bnx2x_int_sync(struct cnic_dev *dev)
4821{
4822}
4823
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004824static void cnic_init_bnx2x_tx_ring(struct cnic_dev *dev,
4825 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004826{
4827 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004828 struct cnic_uio_dev *udev = cp->udev;
4829 union eth_tx_bd_types *txbd = (union eth_tx_bd_types *) udev->l2_ring;
4830 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004831 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004832 int i;
Michael Chan5159fdc2010-12-23 07:42:59 +00004833 u32 cli = cp->ethdev->iscsi_l2_client_id;
Michael Chan71034ba2009-10-10 13:46:59 +00004834 u32 val;
4835
4836 memset(txbd, 0, BCM_PAGE_SIZE);
4837
Michael Chancd801532010-10-13 14:06:49 +00004838 buf_map = udev->l2_buf_map;
Michael Chan71034ba2009-10-10 13:46:59 +00004839 for (i = 0; i < MAX_TX_DESC_CNT; i += 3, txbd += 3) {
4840 struct eth_tx_start_bd *start_bd = &txbd->start_bd;
4841 struct eth_tx_bd *reg_bd = &((txbd + 2)->reg_bd);
4842
4843 start_bd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4844 start_bd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4845 reg_bd->addr_hi = start_bd->addr_hi;
4846 reg_bd->addr_lo = start_bd->addr_lo + 0x10;
4847 start_bd->nbytes = cpu_to_le16(0x10);
4848 start_bd->nbd = cpu_to_le16(3);
4849 start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
4850 start_bd->general_data = (UNICAST_ADDRESS <<
4851 ETH_TX_START_BD_ETH_ADDR_TYPE_SHIFT);
4852 start_bd->general_data |= (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
4853
4854 }
Michael Chan71034ba2009-10-10 13:46:59 +00004855
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004856 val = (u64) ring_map >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004857 txbd->next_bd.addr_hi = cpu_to_le32(val);
4858
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004859 data->tx.tx_bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004860
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004861 val = (u64) ring_map & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004862 txbd->next_bd.addr_lo = cpu_to_le32(val);
4863
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004864 data->tx.tx_bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004865
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004866 /* Other ramrod params */
4867 data->tx.tx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_CQ_CONS;
4868 data->tx.tx_status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004869
4870 /* reset xstorm per client statistics */
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004871 if (cli < MAX_STAT_COUNTER_ID) {
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004872 data->general.statistics_zero_flg = 1;
4873 data->general.statistics_en_flg = 1;
4874 data->general.statistics_counter_id = cli;
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004875 }
Michael Chan71034ba2009-10-10 13:46:59 +00004876
4877 cp->tx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004878 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_CQ_CONS];
Michael Chan71034ba2009-10-10 13:46:59 +00004879}
4880
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004881static void cnic_init_bnx2x_rx_ring(struct cnic_dev *dev,
4882 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004883{
4884 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004885 struct cnic_uio_dev *udev = cp->udev;
4886 struct eth_rx_bd *rxbd = (struct eth_rx_bd *) (udev->l2_ring +
Michael Chan71034ba2009-10-10 13:46:59 +00004887 BCM_PAGE_SIZE);
4888 struct eth_rx_cqe_next_page *rxcqe = (struct eth_rx_cqe_next_page *)
Michael Chancd801532010-10-13 14:06:49 +00004889 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004890 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004891 int i;
Michael Chan5159fdc2010-12-23 07:42:59 +00004892 u32 cli = cp->ethdev->iscsi_l2_client_id;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004893 int cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
Michael Chan71034ba2009-10-10 13:46:59 +00004894 u32 val;
Michael Chancd801532010-10-13 14:06:49 +00004895 dma_addr_t ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004896
4897 /* General data */
4898 data->general.client_id = cli;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004899 data->general.activate_flg = 1;
4900 data->general.sp_client_id = cli;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004901 data->general.mtu = cpu_to_le16(cp->l2_single_buf_size - 14);
4902 data->general.func_id = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00004903
4904 for (i = 0; i < BNX2X_MAX_RX_DESC_CNT; i++, rxbd++) {
4905 dma_addr_t buf_map;
4906 int n = (i % cp->l2_rx_ring_size) + 1;
4907
Michael Chancd801532010-10-13 14:06:49 +00004908 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chan71034ba2009-10-10 13:46:59 +00004909 rxbd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4910 rxbd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4911 }
Michael Chan71034ba2009-10-10 13:46:59 +00004912
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004913 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004914 rxbd->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004915 data->rx.bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004916
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004917 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004918 rxbd->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004919 data->rx.bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004920
4921 rxcqe += BNX2X_MAX_RCQ_DESC_CNT;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004922 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004923 rxcqe->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004924 data->rx.cqe_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004925
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004926 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004927 rxcqe->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004928 data->rx.cqe_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004929
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004930 /* Other ramrod params */
4931 data->rx.client_qzone_id = cl_qzone_id;
4932 data->rx.rx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS;
4933 data->rx.status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004934
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004935 data->rx.cache_line_alignment_log_size = L1_CACHE_SHIFT;
Michael Chan71034ba2009-10-10 13:46:59 +00004936
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004937 data->rx.max_bytes_on_bd = cpu_to_le16(cp->l2_single_buf_size);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004938 data->rx.outer_vlan_removal_enable_flg = 1;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004939 data->rx.silent_vlan_removal_flg = 1;
4940 data->rx.silent_vlan_value = 0;
4941 data->rx.silent_vlan_mask = 0xffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004942
4943 cp->rx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004944 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS];
Michael Chan5159fdc2010-12-23 07:42:59 +00004945 cp->rx_cons = *cp->rx_cons_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00004946}
4947
Michael Chane21ba412010-12-23 07:43:03 +00004948static void cnic_init_bnx2x_kcq(struct cnic_dev *dev)
4949{
4950 struct cnic_local *cp = dev->cnic_priv;
4951 u32 pfid = cp->pfid;
4952
4953 cp->kcq1.io_addr = BAR_CSTRORM_INTMEM +
4954 CSTORM_ISCSI_EQ_PROD_OFFSET(pfid, 0);
4955 cp->kcq1.sw_prod_idx = 0;
4956
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004957 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chane21ba412010-12-23 07:43:03 +00004958 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4959
4960 cp->kcq1.hw_prod_idx_ptr =
4961 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4962 cp->kcq1.status_idx_ptr =
4963 &sb->sb.running_index[SM_RX_ID];
4964 } else {
4965 struct host_hc_status_block_e1x *sb = cp->status_blk.gen;
4966
4967 cp->kcq1.hw_prod_idx_ptr =
4968 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4969 cp->kcq1.status_idx_ptr =
4970 &sb->sb.running_index[SM_RX_ID];
4971 }
4972
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004973 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chane21ba412010-12-23 07:43:03 +00004974 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4975
4976 cp->kcq2.io_addr = BAR_USTRORM_INTMEM +
4977 USTORM_FCOE_EQ_PROD_OFFSET(pfid);
4978 cp->kcq2.sw_prod_idx = 0;
4979 cp->kcq2.hw_prod_idx_ptr =
4980 &sb->sb.index_values[HC_INDEX_FCOE_EQ_CONS];
4981 cp->kcq2.status_idx_ptr =
4982 &sb->sb.running_index[SM_RX_ID];
4983 }
4984}
4985
Michael Chan71034ba2009-10-10 13:46:59 +00004986static int cnic_start_bnx2x_hw(struct cnic_dev *dev)
4987{
4988 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004989 struct cnic_eth_dev *ethdev = cp->ethdev;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004990 int func = CNIC_FUNC(cp), ret;
Michael Chan14203982010-10-06 03:16:06 +00004991 u32 pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00004992
Michael Chana9e0a4f2012-01-04 12:12:27 +00004993 dev->stats_addr = ethdev->addr_drv_info_to_mcp;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03004994 cp->port_mode = CHIP_PORT_MODE_NONE;
4995
4996 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Eddie Wai78ea22e2012-06-27 15:08:20 +00004997 u32 val;
Michael Chanee87a822010-10-13 14:06:51 +00004998
Eddie Wai78ea22e2012-06-27 15:08:20 +00004999 pci_read_config_dword(dev->pcidev, PCICFG_ME_REGISTER, &val);
5000 cp->func = (u8) ((val & ME_REG_ABS_PF_NUM) >>
5001 ME_REG_ABS_PF_NUM_SHIFT);
5002 func = CNIC_FUNC(cp);
5003
5004 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN_OVWR);
Michael Chanee87a822010-10-13 14:06:51 +00005005 if (!(val & 1))
5006 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN);
5007 else
5008 val = (val >> 1) & 1;
5009
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005010 if (val) {
5011 cp->port_mode = CHIP_4_PORT_MODE;
Michael Chanee87a822010-10-13 14:06:51 +00005012 cp->pfid = func >> 1;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005013 } else {
Michael Chanb7d40312011-07-13 17:24:18 +00005014 cp->port_mode = CHIP_2_PORT_MODE;
Michael Chanee87a822010-10-13 14:06:51 +00005015 cp->pfid = func & 0x6;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005016 }
Michael Chanee87a822010-10-13 14:06:51 +00005017 } else {
5018 cp->pfid = func;
5019 }
Michael Chan14203982010-10-06 03:16:06 +00005020 pfid = cp->pfid;
5021
Michael Chan71034ba2009-10-10 13:46:59 +00005022 ret = cnic_init_id_tbl(&cp->cid_tbl, MAX_ISCSI_TBL_SZ,
Eddie Wai11f23aa2011-06-08 19:29:34 +00005023 cp->iscsi_start_cid, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00005024
5025 if (ret)
5026 return -ENOMEM;
5027
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005028 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id)) {
Michael Chandc219a22011-08-26 09:45:39 +00005029 ret = cnic_init_id_tbl(&cp->fcoe_cid_tbl, dev->max_fcoe_conn,
Eddie Wai11f23aa2011-06-08 19:29:34 +00005030 cp->fcoe_start_cid, 0);
Michael Chane1928c82010-12-23 07:43:04 +00005031
5032 if (ret)
5033 return -ENOMEM;
5034 }
5035
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005036 cp->bnx2x_igu_sb_id = ethdev->irq_arr[0].status_blk_num2;
5037
Michael Chane21ba412010-12-23 07:43:03 +00005038 cnic_init_bnx2x_kcq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005039
Michael Chan71034ba2009-10-10 13:46:59 +00005040 /* Only 1 EQ */
Michael Chane6c28892010-06-24 14:58:39 +00005041 CNIC_WR16(dev, cp->kcq1.io_addr, MAX_KCQ_IDX);
Michael Chan71034ba2009-10-10 13:46:59 +00005042 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005043 CSTORM_ISCSI_EQ_CONS_OFFSET(pfid, 0), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00005044 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005045 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00005046 cp->kcq1.dma.pg_map_arr[1] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00005047 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005048 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00005049 (u64) cp->kcq1.dma.pg_map_arr[1] >> 32);
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_EQE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00005052 cp->kcq1.dma.pg_map_arr[0] & 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_EQE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00005055 (u64) cp->kcq1.dma.pg_map_arr[0] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00005056 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005057 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_VALID_OFFSET(pfid, 0), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00005058 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005059 CSTORM_ISCSI_EQ_SB_NUM_OFFSET(pfid, 0), cp->status_blk_num);
Michael Chan71034ba2009-10-10 13:46:59 +00005060 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005061 CSTORM_ISCSI_EQ_SB_INDEX_OFFSET(pfid, 0),
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005062 HC_INDEX_ISCSI_EQ_CONS);
Michael Chan71034ba2009-10-10 13:46:59 +00005063
Michael Chan71034ba2009-10-10 13:46:59 +00005064 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005065 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00005066 cp->gbl_buf_info.pg_map_arr[0] & 0xffffffff);
5067 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005068 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00005069 (u64) cp->gbl_buf_info.pg_map_arr[0] >> 32);
5070
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005071 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
5072 TSTORM_ISCSI_TCP_LOCAL_ADV_WND_OFFSET(pfid), DEF_RCV_BUF);
5073
Michael Chan71034ba2009-10-10 13:46:59 +00005074 cnic_setup_bnx2x_context(dev);
5075
Michael Chan71034ba2009-10-10 13:46:59 +00005076 ret = cnic_init_bnx2x_irq(dev);
5077 if (ret)
5078 return ret;
5079
Michael Chan71034ba2009-10-10 13:46:59 +00005080 return 0;
5081}
5082
Michael Chan86b53602009-10-10 13:46:57 +00005083static void cnic_init_rings(struct cnic_dev *dev)
5084{
Michael Chan541a7812010-10-06 03:17:22 +00005085 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00005086 struct cnic_uio_dev *udev = cp->udev;
Michael Chan541a7812010-10-06 03:17:22 +00005087
5088 if (test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
5089 return;
5090
Michael Chan86b53602009-10-10 13:46:57 +00005091 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
5092 cnic_init_bnx2_tx_ring(dev);
5093 cnic_init_bnx2_rx_ring(dev);
Michael Chan541a7812010-10-06 03:17:22 +00005094 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00005095 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
Michael Chan5159fdc2010-12-23 07:42:59 +00005096 u32 cli = cp->ethdev->iscsi_l2_client_id;
5097 u32 cid = cp->ethdev->iscsi_l2_cid;
Michael Chan68d7c1a2011-01-05 15:14:13 +00005098 u32 cl_qzone_id;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005099 struct client_init_ramrod_data *data;
Michael Chan71034ba2009-10-10 13:46:59 +00005100 union l5cm_specific_data l5_data;
5101 struct ustorm_eth_rx_producers rx_prods = {0};
Michael Chane1dd8832011-07-13 17:24:19 +00005102 u32 off, i, *cid_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00005103
5104 rx_prods.bd_prod = 0;
5105 rx_prods.cqe_prod = BNX2X_MAX_RCQ_DESC_CNT;
5106 barrier();
5107
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005108 cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
5109
Michael Chanc7596b72009-12-02 15:15:35 +00005110 off = BAR_USTRORM_INTMEM +
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005111 (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id) ?
Michael Chanee87a822010-10-13 14:06:51 +00005112 USTORM_RX_PRODS_E2_OFFSET(cl_qzone_id) :
5113 USTORM_RX_PRODS_E1X_OFFSET(CNIC_PORT(cp), cli));
Michael Chan71034ba2009-10-10 13:46:59 +00005114
5115 for (i = 0; i < sizeof(struct ustorm_eth_rx_producers) / 4; i++)
Michael Chanc7596b72009-12-02 15:15:35 +00005116 CNIC_WR(dev, off + i * 4, ((u32 *) &rx_prods)[i]);
Michael Chan71034ba2009-10-10 13:46:59 +00005117
Michael Chan48f753d2010-05-18 11:32:53 +00005118 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
5119
Michael Chancd801532010-10-13 14:06:49 +00005120 data = udev->l2_buf;
Michael Chane1dd8832011-07-13 17:24:19 +00005121 cid_ptr = udev->l2_buf + 12;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005122
5123 memset(data, 0, sizeof(*data));
5124
5125 cnic_init_bnx2x_tx_ring(dev, data);
5126 cnic_init_bnx2x_rx_ring(dev, data);
5127
Michael Chancd801532010-10-13 14:06:49 +00005128 l5_data.phy_address.lo = udev->l2_buf_map & 0xffffffff;
5129 l5_data.phy_address.hi = (u64) udev->l2_buf_map >> 32;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005130
Michael Chan541a7812010-10-06 03:17:22 +00005131 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
5132
Michael Chan71034ba2009-10-10 13:46:59 +00005133 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_CLIENT_SETUP,
Michael Chan68d7c1a2011-01-05 15:14:13 +00005134 cid, ETH_CONNECTION_TYPE, &l5_data);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005135
Michael Chan48f753d2010-05-18 11:32:53 +00005136 i = 0;
5137 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
5138 ++i < 10)
5139 msleep(1);
5140
5141 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
5142 netdev_err(dev->netdev,
5143 "iSCSI CLIENT_SETUP did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00005144 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan5159fdc2010-12-23 07:42:59 +00005145 cnic_ring_ctl(dev, cid, cli, 1);
Michael Chane1dd8832011-07-13 17:24:19 +00005146 *cid_ptr = cid;
Michael Chan86b53602009-10-10 13:46:57 +00005147 }
5148}
5149
5150static void cnic_shutdown_rings(struct cnic_dev *dev)
5151{
Michael Chan541a7812010-10-06 03:17:22 +00005152 struct cnic_local *cp = dev->cnic_priv;
Michael Chane1dd8832011-07-13 17:24:19 +00005153 struct cnic_uio_dev *udev = cp->udev;
5154 void *rx_ring;
Michael Chan541a7812010-10-06 03:17:22 +00005155
5156 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
5157 return;
5158
Michael Chan86b53602009-10-10 13:46:57 +00005159 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
5160 cnic_shutdown_bnx2_rx_ring(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005161 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
Michael Chan5159fdc2010-12-23 07:42:59 +00005162 u32 cli = cp->ethdev->iscsi_l2_client_id;
5163 u32 cid = cp->ethdev->iscsi_l2_cid;
Michael Chan8b065b62009-12-02 15:15:36 +00005164 union l5cm_specific_data l5_data;
Michael Chan48f753d2010-05-18 11:32:53 +00005165 int i;
Michael Chan71034ba2009-10-10 13:46:59 +00005166
Michael Chan5159fdc2010-12-23 07:42:59 +00005167 cnic_ring_ctl(dev, cid, cli, 0);
Michael Chan8b065b62009-12-02 15:15:36 +00005168
Michael Chan48f753d2010-05-18 11:32:53 +00005169 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
5170
Michael Chan8b065b62009-12-02 15:15:36 +00005171 l5_data.phy_address.lo = cli;
5172 l5_data.phy_address.hi = 0;
5173 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_HALT,
Michael Chan5159fdc2010-12-23 07:42:59 +00005174 cid, ETH_CONNECTION_TYPE, &l5_data);
Michael Chan48f753d2010-05-18 11:32:53 +00005175 i = 0;
5176 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
5177 ++i < 10)
5178 msleep(1);
5179
5180 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
5181 netdev_err(dev->netdev,
5182 "iSCSI CLIENT_HALT did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00005183 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan1bcdc322009-12-10 15:40:57 +00005184
5185 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005186 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
Michael Chan68d7c1a2011-01-05 15:14:13 +00005187 cid, NONE_CONNECTION_TYPE, &l5_data);
Michael Chan1bcdc322009-12-10 15:40:57 +00005188 msleep(10);
Michael Chan86b53602009-10-10 13:46:57 +00005189 }
Michael Chan541a7812010-10-06 03:17:22 +00005190 clear_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chane1dd8832011-07-13 17:24:19 +00005191 rx_ring = udev->l2_ring + BCM_PAGE_SIZE;
5192 memset(rx_ring, 0, BCM_PAGE_SIZE);
Michael Chan86b53602009-10-10 13:46:57 +00005193}
5194
Michael Chana3059b12009-08-14 15:49:44 +00005195static int cnic_register_netdev(struct cnic_dev *dev)
5196{
5197 struct cnic_local *cp = dev->cnic_priv;
5198 struct cnic_eth_dev *ethdev = cp->ethdev;
5199 int err;
5200
5201 if (!ethdev)
5202 return -ENODEV;
5203
5204 if (ethdev->drv_state & CNIC_DRV_STATE_REGD)
5205 return 0;
5206
5207 err = ethdev->drv_register_cnic(dev->netdev, cp->cnic_ops, dev);
5208 if (err)
Joe Perchesddf79b22010-02-17 15:01:54 +00005209 netdev_err(dev->netdev, "register_cnic failed\n");
Michael Chana3059b12009-08-14 15:49:44 +00005210
5211 return err;
5212}
5213
5214static void cnic_unregister_netdev(struct cnic_dev *dev)
5215{
5216 struct cnic_local *cp = dev->cnic_priv;
5217 struct cnic_eth_dev *ethdev = cp->ethdev;
5218
5219 if (!ethdev)
5220 return;
5221
5222 ethdev->drv_unregister_cnic(dev->netdev);
5223}
5224
Michael Chana4636962009-06-08 18:14:43 -07005225static int cnic_start_hw(struct cnic_dev *dev)
5226{
5227 struct cnic_local *cp = dev->cnic_priv;
5228 struct cnic_eth_dev *ethdev = cp->ethdev;
5229 int err;
5230
5231 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
5232 return -EALREADY;
5233
Michael Chana4636962009-06-08 18:14:43 -07005234 dev->regview = ethdev->io_base;
Michael Chana4636962009-06-08 18:14:43 -07005235 pci_dev_get(dev->pcidev);
5236 cp->func = PCI_FUNC(dev->pcidev->devfn);
Michael Chana4dde3a2010-02-24 14:42:08 +00005237 cp->status_blk.gen = ethdev->irq_arr[0].status_blk;
Michael Chana4636962009-06-08 18:14:43 -07005238 cp->status_blk_num = ethdev->irq_arr[0].status_blk_num;
5239
5240 err = cp->alloc_resc(dev);
5241 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00005242 netdev_err(dev->netdev, "allocate resource failure\n");
Michael Chana4636962009-06-08 18:14:43 -07005243 goto err1;
5244 }
5245
5246 err = cp->start_hw(dev);
5247 if (err)
5248 goto err1;
5249
5250 err = cnic_cm_open(dev);
5251 if (err)
5252 goto err1;
5253
5254 set_bit(CNIC_F_CNIC_UP, &dev->flags);
5255
5256 cp->enable_int(dev);
5257
5258 return 0;
5259
5260err1:
Michael Chana4636962009-06-08 18:14:43 -07005261 cp->free_resc(dev);
5262 pci_dev_put(dev->pcidev);
Michael Chana4636962009-06-08 18:14:43 -07005263 return err;
5264}
5265
5266static void cnic_stop_bnx2_hw(struct cnic_dev *dev)
5267{
Michael Chana4636962009-06-08 18:14:43 -07005268 cnic_disable_bnx2_int_sync(dev);
5269
5270 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
5271 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
5272
5273 cnic_init_context(dev, KWQ_CID);
5274 cnic_init_context(dev, KCQ_CID);
5275
5276 cnic_setup_5709_context(dev, 0);
5277 cnic_free_irq(dev);
5278
Michael Chana4636962009-06-08 18:14:43 -07005279 cnic_free_resc(dev);
5280}
5281
Michael Chan71034ba2009-10-10 13:46:59 +00005282
5283static void cnic_stop_bnx2x_hw(struct cnic_dev *dev)
5284{
5285 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00005286
5287 cnic_free_irq(dev);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005288 *cp->kcq1.hw_prod_idx_ptr = 0;
Michael Chan4e9c4fd2009-12-10 15:40:58 +00005289 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005290 CSTORM_ISCSI_EQ_CONS_OFFSET(cp->pfid, 0), 0);
Michael Chane6c28892010-06-24 14:58:39 +00005291 CNIC_WR16(dev, cp->kcq1.io_addr, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00005292 cnic_free_resc(dev);
5293}
5294
Michael Chana4636962009-06-08 18:14:43 -07005295static void cnic_stop_hw(struct cnic_dev *dev)
5296{
5297 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
5298 struct cnic_local *cp = dev->cnic_priv;
Michael Chan48f753d2010-05-18 11:32:53 +00005299 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -07005300
Michael Chan48f753d2010-05-18 11:32:53 +00005301 /* Need to wait for the ring shutdown event to complete
5302 * before clearing the CNIC_UP flag.
5303 */
Michael Chancd801532010-10-13 14:06:49 +00005304 while (cp->udev->uio_dev != -1 && i < 15) {
Michael Chan48f753d2010-05-18 11:32:53 +00005305 msleep(100);
5306 i++;
5307 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005308 cnic_shutdown_rings(dev);
Michael Chana2028b232012-06-27 15:08:19 +00005309 cp->stop_cm(dev);
Michael Chana4636962009-06-08 18:14:43 -07005310 clear_bit(CNIC_F_CNIC_UP, &dev->flags);
Eric Dumazet2cfa5a02011-11-23 07:09:32 +00005311 RCU_INIT_POINTER(cp->ulp_ops[CNIC_ULP_L4], NULL);
Michael Chana4636962009-06-08 18:14:43 -07005312 synchronize_rcu();
5313 cnic_cm_shutdown(dev);
5314 cp->stop_hw(dev);
5315 pci_dev_put(dev->pcidev);
5316 }
5317}
5318
5319static void cnic_free_dev(struct cnic_dev *dev)
5320{
5321 int i = 0;
5322
5323 while ((atomic_read(&dev->ref_count) != 0) && i < 10) {
5324 msleep(100);
5325 i++;
5326 }
5327 if (atomic_read(&dev->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +00005328 netdev_err(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -07005329
Joe Perchesddf79b22010-02-17 15:01:54 +00005330 netdev_info(dev->netdev, "Removed CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07005331 dev_put(dev->netdev);
5332 kfree(dev);
5333}
5334
5335static struct cnic_dev *cnic_alloc_dev(struct net_device *dev,
5336 struct pci_dev *pdev)
5337{
5338 struct cnic_dev *cdev;
5339 struct cnic_local *cp;
5340 int alloc_size;
5341
5342 alloc_size = sizeof(struct cnic_dev) + sizeof(struct cnic_local);
5343
5344 cdev = kzalloc(alloc_size , GFP_KERNEL);
5345 if (cdev == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +00005346 netdev_err(dev, "allocate dev struct failure\n");
Michael Chana4636962009-06-08 18:14:43 -07005347 return NULL;
5348 }
5349
5350 cdev->netdev = dev;
5351 cdev->cnic_priv = (char *)cdev + sizeof(struct cnic_dev);
5352 cdev->register_device = cnic_register_device;
5353 cdev->unregister_device = cnic_unregister_device;
5354 cdev->iscsi_nl_msg_recv = cnic_iscsi_nl_msg_recv;
5355
5356 cp = cdev->cnic_priv;
5357 cp->dev = cdev;
Michael Chana4636962009-06-08 18:14:43 -07005358 cp->l2_single_buf_size = 0x400;
5359 cp->l2_rx_ring_size = 3;
5360
5361 spin_lock_init(&cp->cnic_ulp_lock);
5362
Joe Perchesddf79b22010-02-17 15:01:54 +00005363 netdev_info(dev, "Added CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07005364
5365 return cdev;
5366}
5367
5368static struct cnic_dev *init_bnx2_cnic(struct net_device *dev)
5369{
5370 struct pci_dev *pdev;
5371 struct cnic_dev *cdev;
5372 struct cnic_local *cp;
5373 struct cnic_eth_dev *ethdev = NULL;
Michael Chane2ee3612009-06-13 17:43:02 -07005374 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
Michael Chana4636962009-06-08 18:14:43 -07005375
Michael Chane2ee3612009-06-13 17:43:02 -07005376 probe = symbol_get(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07005377 if (probe) {
5378 ethdev = (*probe)(dev);
Michael Chan64c64602009-08-14 15:49:43 +00005379 symbol_put(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07005380 }
5381 if (!ethdev)
5382 return NULL;
5383
5384 pdev = ethdev->pdev;
5385 if (!pdev)
5386 return NULL;
5387
5388 dev_hold(dev);
5389 pci_dev_get(pdev);
Sergei Shtylyovff938e42011-02-28 11:57:33 -08005390 if ((pdev->device == PCI_DEVICE_ID_NX2_5709 ||
5391 pdev->device == PCI_DEVICE_ID_NX2_5709S) &&
5392 (pdev->revision < 0x10)) {
5393 pci_dev_put(pdev);
5394 goto cnic_err;
Michael Chana4636962009-06-08 18:14:43 -07005395 }
5396 pci_dev_put(pdev);
5397
5398 cdev = cnic_alloc_dev(dev, pdev);
5399 if (cdev == NULL)
5400 goto cnic_err;
5401
5402 set_bit(CNIC_F_BNX2_CLASS, &cdev->flags);
5403 cdev->submit_kwqes = cnic_submit_bnx2_kwqes;
5404
5405 cp = cdev->cnic_priv;
5406 cp->ethdev = ethdev;
5407 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00005408 cp->chip_id = ethdev->chip_id;
Michael Chana4636962009-06-08 18:14:43 -07005409
Michael Chan7625eb22011-06-08 19:29:36 +00005410 cdev->max_iscsi_conn = ethdev->max_iscsi_conn;
5411
Michael Chana4636962009-06-08 18:14:43 -07005412 cp->cnic_ops = &cnic_bnx2_ops;
5413 cp->start_hw = cnic_start_bnx2_hw;
5414 cp->stop_hw = cnic_stop_bnx2_hw;
5415 cp->setup_pgtbl = cnic_setup_page_tbl;
5416 cp->alloc_resc = cnic_alloc_bnx2_resc;
5417 cp->free_resc = cnic_free_resc;
5418 cp->start_cm = cnic_cm_init_bnx2_hw;
5419 cp->stop_cm = cnic_cm_stop_bnx2_hw;
5420 cp->enable_int = cnic_enable_bnx2_int;
5421 cp->disable_int_sync = cnic_disable_bnx2_int_sync;
5422 cp->close_conn = cnic_close_bnx2_conn;
Michael Chana4636962009-06-08 18:14:43 -07005423 return cdev;
5424
5425cnic_err:
5426 dev_put(dev);
5427 return NULL;
5428}
5429
Michael Chan71034ba2009-10-10 13:46:59 +00005430static struct cnic_dev *init_bnx2x_cnic(struct net_device *dev)
5431{
5432 struct pci_dev *pdev;
5433 struct cnic_dev *cdev;
5434 struct cnic_local *cp;
5435 struct cnic_eth_dev *ethdev = NULL;
5436 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
5437
5438 probe = symbol_get(bnx2x_cnic_probe);
5439 if (probe) {
5440 ethdev = (*probe)(dev);
5441 symbol_put(bnx2x_cnic_probe);
5442 }
5443 if (!ethdev)
5444 return NULL;
5445
5446 pdev = ethdev->pdev;
5447 if (!pdev)
5448 return NULL;
5449
5450 dev_hold(dev);
5451 cdev = cnic_alloc_dev(dev, pdev);
5452 if (cdev == NULL) {
5453 dev_put(dev);
5454 return NULL;
5455 }
5456
5457 set_bit(CNIC_F_BNX2X_CLASS, &cdev->flags);
5458 cdev->submit_kwqes = cnic_submit_bnx2x_kwqes;
5459
5460 cp = cdev->cnic_priv;
5461 cp->ethdev = ethdev;
5462 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00005463 cp->chip_id = ethdev->chip_id;
Michael Chan71034ba2009-10-10 13:46:59 +00005464
Barak Witkowski1d187b32011-12-05 22:41:50 +00005465 cdev->stats_addr = ethdev->addr_drv_info_to_mcp;
5466
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00005467 if (!(ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI))
5468 cdev->max_iscsi_conn = ethdev->max_iscsi_conn;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005469 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id) &&
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00005470 !(ethdev->drv_state & CNIC_DRV_STATE_NO_FCOE))
5471 cdev->max_fcoe_conn = ethdev->max_fcoe_conn;
5472
Michael Chandc219a22011-08-26 09:45:39 +00005473 if (cdev->max_fcoe_conn > BNX2X_FCOE_NUM_CONNECTIONS)
5474 cdev->max_fcoe_conn = BNX2X_FCOE_NUM_CONNECTIONS;
5475
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00005476 memcpy(cdev->mac_addr, ethdev->iscsi_mac, 6);
5477
Michael Chan71034ba2009-10-10 13:46:59 +00005478 cp->cnic_ops = &cnic_bnx2x_ops;
5479 cp->start_hw = cnic_start_bnx2x_hw;
5480 cp->stop_hw = cnic_stop_bnx2x_hw;
5481 cp->setup_pgtbl = cnic_setup_page_tbl_le;
5482 cp->alloc_resc = cnic_alloc_bnx2x_resc;
5483 cp->free_resc = cnic_free_resc;
5484 cp->start_cm = cnic_cm_init_bnx2x_hw;
5485 cp->stop_cm = cnic_cm_stop_bnx2x_hw;
5486 cp->enable_int = cnic_enable_bnx2x_int;
5487 cp->disable_int_sync = cnic_disable_bnx2x_int_sync;
Vlad Zolotarov619c5cb2011-06-14 14:33:44 +03005488 if (BNX2X_CHIP_IS_E2_PLUS(cp->chip_id))
Michael Chanee87a822010-10-13 14:06:51 +00005489 cp->ack_int = cnic_ack_bnx2x_e2_msix;
5490 else
5491 cp->ack_int = cnic_ack_bnx2x_msix;
Michael Chan71034ba2009-10-10 13:46:59 +00005492 cp->close_conn = cnic_close_bnx2x_conn;
Michael Chan71034ba2009-10-10 13:46:59 +00005493 return cdev;
5494}
5495
Michael Chana4636962009-06-08 18:14:43 -07005496static struct cnic_dev *is_cnic_dev(struct net_device *dev)
5497{
5498 struct ethtool_drvinfo drvinfo;
5499 struct cnic_dev *cdev = NULL;
5500
5501 if (dev->ethtool_ops && dev->ethtool_ops->get_drvinfo) {
5502 memset(&drvinfo, 0, sizeof(drvinfo));
5503 dev->ethtool_ops->get_drvinfo(dev, &drvinfo);
5504
5505 if (!strcmp(drvinfo.driver, "bnx2"))
5506 cdev = init_bnx2_cnic(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005507 if (!strcmp(drvinfo.driver, "bnx2x"))
5508 cdev = init_bnx2x_cnic(dev);
Michael Chana4636962009-06-08 18:14:43 -07005509 if (cdev) {
5510 write_lock(&cnic_dev_lock);
5511 list_add(&cdev->list, &cnic_dev_list);
5512 write_unlock(&cnic_dev_lock);
5513 }
5514 }
5515 return cdev;
5516}
5517
Michael Chan415199f2011-07-20 14:55:24 +00005518static void cnic_rcv_netevent(struct cnic_local *cp, unsigned long event,
5519 u16 vlan_id)
5520{
5521 int if_type;
5522
5523 rcu_read_lock();
5524 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
5525 struct cnic_ulp_ops *ulp_ops;
5526 void *ctx;
5527
5528 ulp_ops = rcu_dereference(cp->ulp_ops[if_type]);
5529 if (!ulp_ops || !ulp_ops->indicate_netevent)
5530 continue;
5531
5532 ctx = cp->ulp_handle[if_type];
5533
5534 ulp_ops->indicate_netevent(ctx, event, vlan_id);
5535 }
5536 rcu_read_unlock();
5537}
5538
Michael Chana4636962009-06-08 18:14:43 -07005539/**
5540 * netdev event handler
5541 */
5542static int cnic_netdev_event(struct notifier_block *this, unsigned long event,
5543 void *ptr)
5544{
5545 struct net_device *netdev = ptr;
5546 struct cnic_dev *dev;
Michael Chana4636962009-06-08 18:14:43 -07005547 int new_dev = 0;
5548
5549 dev = cnic_from_netdev(netdev);
5550
Michael Chandb1d3502011-06-08 19:29:35 +00005551 if (!dev && (event == NETDEV_REGISTER || netif_running(netdev))) {
Michael Chana4636962009-06-08 18:14:43 -07005552 /* Check for the hot-plug device */
5553 dev = is_cnic_dev(netdev);
5554 if (dev) {
5555 new_dev = 1;
5556 cnic_hold(dev);
5557 }
5558 }
5559 if (dev) {
5560 struct cnic_local *cp = dev->cnic_priv;
5561
5562 if (new_dev)
5563 cnic_ulp_init(dev);
5564 else if (event == NETDEV_UNREGISTER)
5565 cnic_ulp_exit(dev);
Michael Chan6053bbf2009-10-02 11:03:28 -07005566
Michael Chandb1d3502011-06-08 19:29:35 +00005567 if (event == NETDEV_UP || (new_dev && netif_running(netdev))) {
Michael Chana3059b12009-08-14 15:49:44 +00005568 if (cnic_register_netdev(dev) != 0) {
5569 cnic_put(dev);
5570 goto done;
5571 }
Michael Chana4636962009-06-08 18:14:43 -07005572 if (!cnic_start_hw(dev))
5573 cnic_ulp_start(dev);
Michael Chana4636962009-06-08 18:14:43 -07005574 }
5575
Michael Chan415199f2011-07-20 14:55:24 +00005576 cnic_rcv_netevent(cp, event, 0);
Michael Chana4636962009-06-08 18:14:43 -07005577
5578 if (event == NETDEV_GOING_DOWN) {
Michael Chana4636962009-06-08 18:14:43 -07005579 cnic_ulp_stop(dev);
5580 cnic_stop_hw(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005581 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005582 } else if (event == NETDEV_UNREGISTER) {
5583 write_lock(&cnic_dev_lock);
5584 list_del_init(&dev->list);
5585 write_unlock(&cnic_dev_lock);
5586
5587 cnic_put(dev);
5588 cnic_free_dev(dev);
5589 goto done;
5590 }
5591 cnic_put(dev);
Michael Chan415199f2011-07-20 14:55:24 +00005592 } else {
5593 struct net_device *realdev;
5594 u16 vid;
5595
5596 vid = cnic_get_vlan(netdev, &realdev);
5597 if (realdev) {
5598 dev = cnic_from_netdev(realdev);
5599 if (dev) {
5600 vid |= VLAN_TAG_PRESENT;
5601 cnic_rcv_netevent(dev->cnic_priv, event, vid);
5602 cnic_put(dev);
5603 }
5604 }
Michael Chana4636962009-06-08 18:14:43 -07005605 }
5606done:
5607 return NOTIFY_DONE;
5608}
5609
5610static struct notifier_block cnic_netdev_notifier = {
5611 .notifier_call = cnic_netdev_event
5612};
5613
5614static void cnic_release(void)
5615{
5616 struct cnic_dev *dev;
Michael Chana3ceeeb2010-10-13 14:06:50 +00005617 struct cnic_uio_dev *udev;
Michael Chana4636962009-06-08 18:14:43 -07005618
5619 while (!list_empty(&cnic_dev_list)) {
5620 dev = list_entry(cnic_dev_list.next, struct cnic_dev, list);
5621 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
5622 cnic_ulp_stop(dev);
5623 cnic_stop_hw(dev);
5624 }
5625
5626 cnic_ulp_exit(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005627 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005628 list_del_init(&dev->list);
5629 cnic_free_dev(dev);
5630 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005631 while (!list_empty(&cnic_udev_list)) {
5632 udev = list_entry(cnic_udev_list.next, struct cnic_uio_dev,
5633 list);
5634 cnic_free_uio(udev);
5635 }
Michael Chana4636962009-06-08 18:14:43 -07005636}
5637
5638static int __init cnic_init(void)
5639{
5640 int rc = 0;
5641
Joe Perchesddf79b22010-02-17 15:01:54 +00005642 pr_info("%s", version);
Michael Chana4636962009-06-08 18:14:43 -07005643
5644 rc = register_netdevice_notifier(&cnic_netdev_notifier);
5645 if (rc) {
5646 cnic_release();
5647 return rc;
5648 }
5649
Michael Chanfdf24082010-10-13 14:06:47 +00005650 cnic_wq = create_singlethread_workqueue("cnic_wq");
5651 if (!cnic_wq) {
5652 cnic_release();
5653 unregister_netdevice_notifier(&cnic_netdev_notifier);
5654 return -ENOMEM;
5655 }
5656
Michael Chana4636962009-06-08 18:14:43 -07005657 return 0;
5658}
5659
5660static void __exit cnic_exit(void)
5661{
5662 unregister_netdevice_notifier(&cnic_netdev_notifier);
5663 cnic_release();
Michael Chanfdf24082010-10-13 14:06:47 +00005664 destroy_workqueue(cnic_wq);
Michael Chana4636962009-06-08 18:14:43 -07005665}
5666
5667module_init(cnic_init);
5668module_exit(cnic_exit);