blob: 07f1b13c8dce27529806e346f558938336347132 [file] [log] [blame]
Michael Chana4636962009-06-08 18:14:43 -07001/* cnic.c: Broadcom CNIC core network driver.
2 *
Michael Chan1d9cfc42010-02-24 14:42:09 +00003 * Copyright (c) 2006-2010 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 Chana4636962009-06-08 18:14:43 -070031#if defined(CONFIG_VLAN_8021Q) || defined(CONFIG_VLAN_8021Q_MODULE)
32#define BCM_VLAN 1
33#endif
34#include <net/ip.h>
35#include <net/tcp.h>
36#include <net/route.h>
37#include <net/ipv6.h>
38#include <net/ip6_route.h>
David S. Millerc05e85a2009-10-12 23:18:35 -070039#include <net/ip6_checksum.h>
Michael Chana4636962009-06-08 18:14:43 -070040#include <scsi/iscsi_if.h>
41
42#include "cnic_if.h"
43#include "bnx2.h"
Dmitry Kravkov5d1e8592010-07-27 12:31:10 +000044#include "bnx2x/bnx2x_reg.h"
45#include "bnx2x/bnx2x_fw_defs.h"
46#include "bnx2x/bnx2x_hsi.h"
Michael Chane2513062009-10-10 13:46:58 +000047#include "../scsi/bnx2i/57xx_iscsi_constants.h"
48#include "../scsi/bnx2i/57xx_iscsi_hsi.h"
Michael Chana4636962009-06-08 18:14:43 -070049#include "cnic.h"
50#include "cnic_defs.h"
51
52#define DRV_MODULE_NAME "cnic"
Michael Chana4636962009-06-08 18:14:43 -070053
54static char version[] __devinitdata =
55 "Broadcom NetXtreme II CNIC Driver " DRV_MODULE_NAME " v" CNIC_MODULE_VERSION " (" CNIC_MODULE_RELDATE ")\n";
56
57MODULE_AUTHOR("Michael Chan <mchan@broadcom.com> and John(Zongxi) "
58 "Chen (zongxi@broadcom.com");
59MODULE_DESCRIPTION("Broadcom NetXtreme II CNIC Driver");
60MODULE_LICENSE("GPL");
61MODULE_VERSION(CNIC_MODULE_VERSION);
62
Michael Chan8adc92402010-12-23 07:42:57 +000063/* cnic_dev_list modifications are protected by both rtnl and cnic_dev_lock */
Michael Chana4636962009-06-08 18:14:43 -070064static LIST_HEAD(cnic_dev_list);
Michael Chana3ceeeb2010-10-13 14:06:50 +000065static LIST_HEAD(cnic_udev_list);
Michael Chana4636962009-06-08 18:14:43 -070066static DEFINE_RWLOCK(cnic_dev_lock);
67static DEFINE_MUTEX(cnic_lock);
68
Eric Dumazet13707f92011-01-26 19:28:23 +000069static struct cnic_ulp_ops __rcu *cnic_ulp_tbl[MAX_CNIC_ULP_TYPE];
70
71/* helper function, assuming cnic_lock is held */
72static inline struct cnic_ulp_ops *cnic_ulp_tbl_prot(int type)
73{
74 return rcu_dereference_protected(cnic_ulp_tbl[type],
75 lockdep_is_held(&cnic_lock));
76}
Michael Chana4636962009-06-08 18:14:43 -070077
78static int cnic_service_bnx2(void *, void *);
Michael Chan71034ba2009-10-10 13:46:59 +000079static int cnic_service_bnx2x(void *, void *);
Michael Chana4636962009-06-08 18:14:43 -070080static int cnic_ctl(void *, struct cnic_ctl_info *);
81
82static struct cnic_ops cnic_bnx2_ops = {
83 .cnic_owner = THIS_MODULE,
84 .cnic_handler = cnic_service_bnx2,
85 .cnic_ctl = cnic_ctl,
86};
87
Michael Chan71034ba2009-10-10 13:46:59 +000088static struct cnic_ops cnic_bnx2x_ops = {
89 .cnic_owner = THIS_MODULE,
90 .cnic_handler = cnic_service_bnx2x,
91 .cnic_ctl = cnic_ctl,
92};
93
Michael Chanfdf24082010-10-13 14:06:47 +000094static struct workqueue_struct *cnic_wq;
95
Michael Chan86b53602009-10-10 13:46:57 +000096static void cnic_shutdown_rings(struct cnic_dev *);
97static void cnic_init_rings(struct cnic_dev *);
Michael Chana4636962009-06-08 18:14:43 -070098static int cnic_cm_set_pg(struct cnic_sock *);
99
100static int cnic_uio_open(struct uio_info *uinfo, struct inode *inode)
101{
Michael Chancd801532010-10-13 14:06:49 +0000102 struct cnic_uio_dev *udev = uinfo->priv;
103 struct cnic_dev *dev;
Michael Chana4636962009-06-08 18:14:43 -0700104
105 if (!capable(CAP_NET_ADMIN))
106 return -EPERM;
107
Michael Chancd801532010-10-13 14:06:49 +0000108 if (udev->uio_dev != -1)
Michael Chana4636962009-06-08 18:14:43 -0700109 return -EBUSY;
110
Michael Chan86b53602009-10-10 13:46:57 +0000111 rtnl_lock();
Michael Chancd801532010-10-13 14:06:49 +0000112 dev = udev->dev;
113
Michael Chana3ceeeb2010-10-13 14:06:50 +0000114 if (!dev || !test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
Michael Chan86b53602009-10-10 13:46:57 +0000115 rtnl_unlock();
116 return -ENODEV;
117 }
118
Michael Chancd801532010-10-13 14:06:49 +0000119 udev->uio_dev = iminor(inode);
Michael Chana4636962009-06-08 18:14:43 -0700120
Michael Chana3ceeeb2010-10-13 14:06:50 +0000121 cnic_shutdown_rings(dev);
Michael Chan86b53602009-10-10 13:46:57 +0000122 cnic_init_rings(dev);
123 rtnl_unlock();
Michael Chana4636962009-06-08 18:14:43 -0700124
125 return 0;
126}
127
128static int cnic_uio_close(struct uio_info *uinfo, struct inode *inode)
129{
Michael Chancd801532010-10-13 14:06:49 +0000130 struct cnic_uio_dev *udev = uinfo->priv;
Michael Chan6ef57a02009-09-21 15:39:37 +0000131
Michael Chancd801532010-10-13 14:06:49 +0000132 udev->uio_dev = -1;
Michael Chana4636962009-06-08 18:14:43 -0700133 return 0;
134}
135
136static inline void cnic_hold(struct cnic_dev *dev)
137{
138 atomic_inc(&dev->ref_count);
139}
140
141static inline void cnic_put(struct cnic_dev *dev)
142{
143 atomic_dec(&dev->ref_count);
144}
145
146static inline void csk_hold(struct cnic_sock *csk)
147{
148 atomic_inc(&csk->ref_count);
149}
150
151static inline void csk_put(struct cnic_sock *csk)
152{
153 atomic_dec(&csk->ref_count);
154}
155
156static struct cnic_dev *cnic_from_netdev(struct net_device *netdev)
157{
158 struct cnic_dev *cdev;
159
160 read_lock(&cnic_dev_lock);
161 list_for_each_entry(cdev, &cnic_dev_list, list) {
162 if (netdev == cdev->netdev) {
163 cnic_hold(cdev);
164 read_unlock(&cnic_dev_lock);
165 return cdev;
166 }
167 }
168 read_unlock(&cnic_dev_lock);
169 return NULL;
170}
171
Michael Chan7fc1ece2009-08-14 15:49:47 +0000172static inline void ulp_get(struct cnic_ulp_ops *ulp_ops)
173{
174 atomic_inc(&ulp_ops->ref_count);
175}
176
177static inline void ulp_put(struct cnic_ulp_ops *ulp_ops)
178{
179 atomic_dec(&ulp_ops->ref_count);
180}
181
Michael Chana4636962009-06-08 18:14:43 -0700182static void cnic_ctx_wr(struct cnic_dev *dev, u32 cid_addr, u32 off, u32 val)
183{
184 struct cnic_local *cp = dev->cnic_priv;
185 struct cnic_eth_dev *ethdev = cp->ethdev;
186 struct drv_ctl_info info;
187 struct drv_ctl_io *io = &info.data.io;
188
189 info.cmd = DRV_CTL_CTX_WR_CMD;
190 io->cid_addr = cid_addr;
191 io->offset = off;
192 io->data = val;
193 ethdev->drv_ctl(dev->netdev, &info);
194}
195
Michael Chan71034ba2009-10-10 13:46:59 +0000196static void cnic_ctx_tbl_wr(struct cnic_dev *dev, u32 off, dma_addr_t addr)
197{
198 struct cnic_local *cp = dev->cnic_priv;
199 struct cnic_eth_dev *ethdev = cp->ethdev;
200 struct drv_ctl_info info;
201 struct drv_ctl_io *io = &info.data.io;
202
203 info.cmd = DRV_CTL_CTXTBL_WR_CMD;
204 io->offset = off;
205 io->dma_addr = addr;
206 ethdev->drv_ctl(dev->netdev, &info);
207}
208
209static void cnic_ring_ctl(struct cnic_dev *dev, u32 cid, u32 cl_id, int start)
210{
211 struct cnic_local *cp = dev->cnic_priv;
212 struct cnic_eth_dev *ethdev = cp->ethdev;
213 struct drv_ctl_info info;
214 struct drv_ctl_l2_ring *ring = &info.data.ring;
215
216 if (start)
217 info.cmd = DRV_CTL_START_L2_CMD;
218 else
219 info.cmd = DRV_CTL_STOP_L2_CMD;
220
221 ring->cid = cid;
222 ring->client_id = cl_id;
223 ethdev->drv_ctl(dev->netdev, &info);
224}
225
Michael Chana4636962009-06-08 18:14:43 -0700226static void cnic_reg_wr_ind(struct cnic_dev *dev, u32 off, u32 val)
227{
228 struct cnic_local *cp = dev->cnic_priv;
229 struct cnic_eth_dev *ethdev = cp->ethdev;
230 struct drv_ctl_info info;
231 struct drv_ctl_io *io = &info.data.io;
232
233 info.cmd = DRV_CTL_IO_WR_CMD;
234 io->offset = off;
235 io->data = val;
236 ethdev->drv_ctl(dev->netdev, &info);
237}
238
239static u32 cnic_reg_rd_ind(struct cnic_dev *dev, u32 off)
240{
241 struct cnic_local *cp = dev->cnic_priv;
242 struct cnic_eth_dev *ethdev = cp->ethdev;
243 struct drv_ctl_info info;
244 struct drv_ctl_io *io = &info.data.io;
245
246 info.cmd = DRV_CTL_IO_RD_CMD;
247 io->offset = off;
248 ethdev->drv_ctl(dev->netdev, &info);
249 return io->data;
250}
251
252static int cnic_in_use(struct cnic_sock *csk)
253{
254 return test_bit(SK_F_INUSE, &csk->flags);
255}
256
Dmitry Kravkovc2bff632010-10-06 03:33:18 +0000257static void cnic_spq_completion(struct cnic_dev *dev, int cmd, u32 count)
Michael Chana4636962009-06-08 18:14:43 -0700258{
259 struct cnic_local *cp = dev->cnic_priv;
260 struct cnic_eth_dev *ethdev = cp->ethdev;
261 struct drv_ctl_info info;
262
Dmitry Kravkovc2bff632010-10-06 03:33:18 +0000263 info.cmd = cmd;
264 info.data.credit.credit_count = count;
Michael Chana4636962009-06-08 18:14:43 -0700265 ethdev->drv_ctl(dev->netdev, &info);
266}
267
Michael Chan71034ba2009-10-10 13:46:59 +0000268static int cnic_get_l5_cid(struct cnic_local *cp, u32 cid, u32 *l5_cid)
269{
270 u32 i;
271
Michael Chan520efdf2010-06-24 14:58:37 +0000272 for (i = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +0000273 if (cp->ctx_tbl[i].cid == cid) {
274 *l5_cid = i;
275 return 0;
276 }
277 }
278 return -EINVAL;
279}
280
Michael Chana4636962009-06-08 18:14:43 -0700281static int cnic_send_nlmsg(struct cnic_local *cp, u32 type,
282 struct cnic_sock *csk)
283{
284 struct iscsi_path path_req;
285 char *buf = NULL;
286 u16 len = 0;
287 u32 msg_type = ISCSI_KEVENT_IF_DOWN;
288 struct cnic_ulp_ops *ulp_ops;
Michael Chancd801532010-10-13 14:06:49 +0000289 struct cnic_uio_dev *udev = cp->udev;
Michael Chan939b82e2010-12-23 07:42:58 +0000290 int rc = 0, retry = 0;
Michael Chana4636962009-06-08 18:14:43 -0700291
Michael Chancd801532010-10-13 14:06:49 +0000292 if (!udev || udev->uio_dev == -1)
Michael Chana4636962009-06-08 18:14:43 -0700293 return -ENODEV;
294
295 if (csk) {
296 len = sizeof(path_req);
297 buf = (char *) &path_req;
298 memset(&path_req, 0, len);
299
300 msg_type = ISCSI_KEVENT_PATH_REQ;
301 path_req.handle = (u64) csk->l5_cid;
302 if (test_bit(SK_F_IPV6, &csk->flags)) {
303 memcpy(&path_req.dst.v6_addr, &csk->dst_ip[0],
304 sizeof(struct in6_addr));
305 path_req.ip_addr_len = 16;
306 } else {
307 memcpy(&path_req.dst.v4_addr, &csk->dst_ip[0],
308 sizeof(struct in_addr));
309 path_req.ip_addr_len = 4;
310 }
311 path_req.vlan_id = csk->vlan_id;
312 path_req.pmtu = csk->mtu;
313 }
314
Michael Chan939b82e2010-12-23 07:42:58 +0000315 while (retry < 3) {
316 rc = 0;
317 rcu_read_lock();
318 ulp_ops = rcu_dereference(cnic_ulp_tbl[CNIC_ULP_ISCSI]);
319 if (ulp_ops)
320 rc = ulp_ops->iscsi_nl_send_msg(
321 cp->ulp_handle[CNIC_ULP_ISCSI],
322 msg_type, buf, len);
323 rcu_read_unlock();
324 if (rc == 0 || msg_type != ISCSI_KEVENT_PATH_REQ)
325 break;
326
327 msleep(100);
328 retry++;
329 }
Michael Chana4636962009-06-08 18:14:43 -0700330 return 0;
331}
332
Eddie Wai42ecbb82010-12-23 07:43:02 +0000333static void cnic_cm_upcall(struct cnic_local *, struct cnic_sock *, u8);
334
Michael Chana4636962009-06-08 18:14:43 -0700335static int cnic_iscsi_nl_msg_recv(struct cnic_dev *dev, u32 msg_type,
336 char *buf, u16 len)
337{
338 int rc = -EINVAL;
339
340 switch (msg_type) {
341 case ISCSI_UEVENT_PATH_UPDATE: {
342 struct cnic_local *cp;
343 u32 l5_cid;
344 struct cnic_sock *csk;
345 struct iscsi_path *path_resp;
346
347 if (len < sizeof(*path_resp))
348 break;
349
350 path_resp = (struct iscsi_path *) buf;
351 cp = dev->cnic_priv;
352 l5_cid = (u32) path_resp->handle;
353 if (l5_cid >= MAX_CM_SK_TBL_SZ)
354 break;
355
Michael Chand02a5e62010-02-24 14:42:06 +0000356 rcu_read_lock();
357 if (!rcu_dereference(cp->ulp_ops[CNIC_ULP_L4])) {
358 rc = -ENODEV;
359 rcu_read_unlock();
360 break;
361 }
Michael Chana4636962009-06-08 18:14:43 -0700362 csk = &cp->csk_tbl[l5_cid];
363 csk_hold(csk);
Eddie Wai42ecbb82010-12-23 07:43:02 +0000364 if (cnic_in_use(csk) &&
365 test_bit(SK_F_CONNECT_START, &csk->flags)) {
366
Michael Chana4636962009-06-08 18:14:43 -0700367 memcpy(csk->ha, path_resp->mac_addr, 6);
368 if (test_bit(SK_F_IPV6, &csk->flags))
369 memcpy(&csk->src_ip[0], &path_resp->src.v6_addr,
370 sizeof(struct in6_addr));
371 else
372 memcpy(&csk->src_ip[0], &path_resp->src.v4_addr,
373 sizeof(struct in_addr));
Eddie Wai42ecbb82010-12-23 07:43:02 +0000374
375 if (is_valid_ether_addr(csk->ha)) {
Michael Chana4636962009-06-08 18:14:43 -0700376 cnic_cm_set_pg(csk);
Eddie Wai42ecbb82010-12-23 07:43:02 +0000377 } else if (!test_bit(SK_F_OFFLD_SCHED, &csk->flags) &&
378 !test_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
379
380 cnic_cm_upcall(cp, csk,
381 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
382 clear_bit(SK_F_CONNECT_START, &csk->flags);
383 }
Michael Chana4636962009-06-08 18:14:43 -0700384 }
385 csk_put(csk);
Michael Chand02a5e62010-02-24 14:42:06 +0000386 rcu_read_unlock();
Michael Chana4636962009-06-08 18:14:43 -0700387 rc = 0;
388 }
389 }
390
391 return rc;
392}
393
394static int cnic_offld_prep(struct cnic_sock *csk)
395{
396 if (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
397 return 0;
398
399 if (!test_bit(SK_F_CONNECT_START, &csk->flags)) {
400 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
401 return 0;
402 }
403
404 return 1;
405}
406
407static int cnic_close_prep(struct cnic_sock *csk)
408{
409 clear_bit(SK_F_CONNECT_START, &csk->flags);
410 smp_mb__after_clear_bit();
411
412 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
413 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
414 msleep(1);
415
416 return 1;
417 }
418 return 0;
419}
420
421static int cnic_abort_prep(struct cnic_sock *csk)
422{
423 clear_bit(SK_F_CONNECT_START, &csk->flags);
424 smp_mb__after_clear_bit();
425
426 while (test_and_set_bit(SK_F_OFFLD_SCHED, &csk->flags))
427 msleep(1);
428
429 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
430 csk->state = L4_KCQE_OPCODE_VALUE_RESET_COMP;
431 return 1;
432 }
433
434 return 0;
435}
436
437int cnic_register_driver(int ulp_type, struct cnic_ulp_ops *ulp_ops)
438{
439 struct cnic_dev *dev;
440
roel kluin0d37f362009-11-02 06:53:44 +0000441 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000442 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700443 return -EINVAL;
444 }
445 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000446 if (cnic_ulp_tbl_prot(ulp_type)) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000447 pr_err("%s: Type %d has already been registered\n",
448 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700449 mutex_unlock(&cnic_lock);
450 return -EBUSY;
451 }
452
453 read_lock(&cnic_dev_lock);
454 list_for_each_entry(dev, &cnic_dev_list, list) {
455 struct cnic_local *cp = dev->cnic_priv;
456
457 clear_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]);
458 }
459 read_unlock(&cnic_dev_lock);
460
Michael Chan7fc1ece2009-08-14 15:49:47 +0000461 atomic_set(&ulp_ops->ref_count, 0);
Michael Chana4636962009-06-08 18:14:43 -0700462 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], ulp_ops);
463 mutex_unlock(&cnic_lock);
464
465 /* Prevent race conditions with netdev_event */
466 rtnl_lock();
Michael Chana4636962009-06-08 18:14:43 -0700467 list_for_each_entry(dev, &cnic_dev_list, list) {
468 struct cnic_local *cp = dev->cnic_priv;
469
470 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[ulp_type]))
471 ulp_ops->cnic_init(dev);
472 }
Michael Chana4636962009-06-08 18:14:43 -0700473 rtnl_unlock();
474
475 return 0;
476}
477
478int cnic_unregister_driver(int ulp_type)
479{
480 struct cnic_dev *dev;
Michael Chan7fc1ece2009-08-14 15:49:47 +0000481 struct cnic_ulp_ops *ulp_ops;
482 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700483
roel kluin0d37f362009-11-02 06:53:44 +0000484 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000485 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700486 return -EINVAL;
487 }
488 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000489 ulp_ops = cnic_ulp_tbl_prot(ulp_type);
Michael Chan7fc1ece2009-08-14 15:49:47 +0000490 if (!ulp_ops) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000491 pr_err("%s: Type %d has not been registered\n",
492 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700493 goto out_unlock;
494 }
495 read_lock(&cnic_dev_lock);
496 list_for_each_entry(dev, &cnic_dev_list, list) {
497 struct cnic_local *cp = dev->cnic_priv;
498
499 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000500 pr_err("%s: Type %d still has devices registered\n",
501 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700502 read_unlock(&cnic_dev_lock);
503 goto out_unlock;
504 }
505 }
506 read_unlock(&cnic_dev_lock);
507
508 rcu_assign_pointer(cnic_ulp_tbl[ulp_type], NULL);
509
510 mutex_unlock(&cnic_lock);
511 synchronize_rcu();
Michael Chan7fc1ece2009-08-14 15:49:47 +0000512 while ((atomic_read(&ulp_ops->ref_count) != 0) && (i < 20)) {
513 msleep(100);
514 i++;
515 }
516
517 if (atomic_read(&ulp_ops->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +0000518 netdev_warn(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -0700519 return 0;
520
521out_unlock:
522 mutex_unlock(&cnic_lock);
523 return -EINVAL;
524}
525
526static int cnic_start_hw(struct cnic_dev *);
527static void cnic_stop_hw(struct cnic_dev *);
528
529static int cnic_register_device(struct cnic_dev *dev, int ulp_type,
530 void *ulp_ctx)
531{
532 struct cnic_local *cp = dev->cnic_priv;
533 struct cnic_ulp_ops *ulp_ops;
534
roel kluin0d37f362009-11-02 06:53:44 +0000535 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000536 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700537 return -EINVAL;
538 }
539 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +0000540 if (cnic_ulp_tbl_prot(ulp_type) == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000541 pr_err("%s: Driver with type %d has not been registered\n",
542 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700543 mutex_unlock(&cnic_lock);
544 return -EAGAIN;
545 }
546 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000547 pr_err("%s: Type %d has already been registered to this device\n",
548 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700549 mutex_unlock(&cnic_lock);
550 return -EBUSY;
551 }
552
553 clear_bit(ULP_F_START, &cp->ulp_flags[ulp_type]);
554 cp->ulp_handle[ulp_type] = ulp_ctx;
Eric Dumazet13707f92011-01-26 19:28:23 +0000555 ulp_ops = cnic_ulp_tbl_prot(ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700556 rcu_assign_pointer(cp->ulp_ops[ulp_type], ulp_ops);
557 cnic_hold(dev);
558
559 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
560 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[ulp_type]))
561 ulp_ops->cnic_start(cp->ulp_handle[ulp_type]);
562
563 mutex_unlock(&cnic_lock);
564
565 return 0;
566
567}
568EXPORT_SYMBOL(cnic_register_driver);
569
570static int cnic_unregister_device(struct cnic_dev *dev, int ulp_type)
571{
572 struct cnic_local *cp = dev->cnic_priv;
Michael Chan681dbd72009-08-14 15:49:46 +0000573 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -0700574
roel kluin0d37f362009-11-02 06:53:44 +0000575 if (ulp_type < 0 || ulp_type >= MAX_CNIC_ULP_TYPE) {
Joe Perchesddf79b22010-02-17 15:01:54 +0000576 pr_err("%s: Bad type %d\n", __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700577 return -EINVAL;
578 }
579 mutex_lock(&cnic_lock);
580 if (rcu_dereference(cp->ulp_ops[ulp_type])) {
581 rcu_assign_pointer(cp->ulp_ops[ulp_type], NULL);
582 cnic_put(dev);
583 } else {
Joe Perchesddf79b22010-02-17 15:01:54 +0000584 pr_err("%s: device not registered to this ulp type %d\n",
585 __func__, ulp_type);
Michael Chana4636962009-06-08 18:14:43 -0700586 mutex_unlock(&cnic_lock);
587 return -EINVAL;
588 }
589 mutex_unlock(&cnic_lock);
590
Michael Chan42bb8d52011-01-03 15:21:46 +0000591 if (ulp_type == CNIC_ULP_ISCSI)
592 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
593
Michael Chana4636962009-06-08 18:14:43 -0700594 synchronize_rcu();
595
Michael Chan681dbd72009-08-14 15:49:46 +0000596 while (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]) &&
597 i < 20) {
598 msleep(100);
599 i++;
600 }
601 if (test_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[ulp_type]))
Joe Perchesddf79b22010-02-17 15:01:54 +0000602 netdev_warn(dev->netdev, "Failed waiting for ULP up call to complete\n");
Michael Chan681dbd72009-08-14 15:49:46 +0000603
Michael Chana4636962009-06-08 18:14:43 -0700604 return 0;
605}
606EXPORT_SYMBOL(cnic_unregister_driver);
607
Eddie Wai11f23aa2011-06-08 19:29:34 +0000608static int cnic_init_id_tbl(struct cnic_id_tbl *id_tbl, u32 size, u32 start_id,
609 u32 next)
Michael Chana4636962009-06-08 18:14:43 -0700610{
611 id_tbl->start = start_id;
612 id_tbl->max = size;
Eddie Wai11f23aa2011-06-08 19:29:34 +0000613 id_tbl->next = next;
Michael Chana4636962009-06-08 18:14:43 -0700614 spin_lock_init(&id_tbl->lock);
615 id_tbl->table = kzalloc(DIV_ROUND_UP(size, 32) * 4, GFP_KERNEL);
616 if (!id_tbl->table)
617 return -ENOMEM;
618
619 return 0;
620}
621
622static void cnic_free_id_tbl(struct cnic_id_tbl *id_tbl)
623{
624 kfree(id_tbl->table);
625 id_tbl->table = NULL;
626}
627
628static int cnic_alloc_id(struct cnic_id_tbl *id_tbl, u32 id)
629{
630 int ret = -1;
631
632 id -= id_tbl->start;
633 if (id >= id_tbl->max)
634 return ret;
635
636 spin_lock(&id_tbl->lock);
637 if (!test_bit(id, id_tbl->table)) {
638 set_bit(id, id_tbl->table);
639 ret = 0;
640 }
641 spin_unlock(&id_tbl->lock);
642 return ret;
643}
644
645/* Returns -1 if not successful */
646static u32 cnic_alloc_new_id(struct cnic_id_tbl *id_tbl)
647{
648 u32 id;
649
650 spin_lock(&id_tbl->lock);
651 id = find_next_zero_bit(id_tbl->table, id_tbl->max, id_tbl->next);
652 if (id >= id_tbl->max) {
653 id = -1;
654 if (id_tbl->next != 0) {
655 id = find_first_zero_bit(id_tbl->table, id_tbl->next);
656 if (id >= id_tbl->next)
657 id = -1;
658 }
659 }
660
661 if (id < id_tbl->max) {
662 set_bit(id, id_tbl->table);
663 id_tbl->next = (id + 1) & (id_tbl->max - 1);
664 id += id_tbl->start;
665 }
666
667 spin_unlock(&id_tbl->lock);
668
669 return id;
670}
671
672static void cnic_free_id(struct cnic_id_tbl *id_tbl, u32 id)
673{
674 if (id == -1)
675 return;
676
677 id -= id_tbl->start;
678 if (id >= id_tbl->max)
679 return;
680
681 clear_bit(id, id_tbl->table);
682}
683
684static void cnic_free_dma(struct cnic_dev *dev, struct cnic_dma *dma)
685{
686 int i;
687
688 if (!dma->pg_arr)
689 return;
690
691 for (i = 0; i < dma->num_pages; i++) {
692 if (dma->pg_arr[i]) {
Michael Chan3248e162009-12-02 15:15:39 +0000693 dma_free_coherent(&dev->pcidev->dev, BCM_PAGE_SIZE,
694 dma->pg_arr[i], dma->pg_map_arr[i]);
Michael Chana4636962009-06-08 18:14:43 -0700695 dma->pg_arr[i] = NULL;
696 }
697 }
698 if (dma->pgtbl) {
Michael Chan3248e162009-12-02 15:15:39 +0000699 dma_free_coherent(&dev->pcidev->dev, dma->pgtbl_size,
700 dma->pgtbl, dma->pgtbl_map);
Michael Chana4636962009-06-08 18:14:43 -0700701 dma->pgtbl = NULL;
702 }
703 kfree(dma->pg_arr);
704 dma->pg_arr = NULL;
705 dma->num_pages = 0;
706}
707
708static void cnic_setup_page_tbl(struct cnic_dev *dev, struct cnic_dma *dma)
709{
710 int i;
Michael Chan51388262011-01-25 22:14:50 +0000711 __le32 *page_table = (__le32 *) dma->pgtbl;
Michael Chana4636962009-06-08 18:14:43 -0700712
713 for (i = 0; i < dma->num_pages; i++) {
714 /* Each entry needs to be in big endian format. */
Michael Chan51388262011-01-25 22:14:50 +0000715 *page_table = cpu_to_le32((u64) dma->pg_map_arr[i] >> 32);
Michael Chana4636962009-06-08 18:14:43 -0700716 page_table++;
Michael Chan51388262011-01-25 22:14:50 +0000717 *page_table = cpu_to_le32(dma->pg_map_arr[i] & 0xffffffff);
Michael Chana4636962009-06-08 18:14:43 -0700718 page_table++;
719 }
720}
721
Michael Chan71034ba2009-10-10 13:46:59 +0000722static void cnic_setup_page_tbl_le(struct cnic_dev *dev, struct cnic_dma *dma)
723{
724 int i;
Michael Chan51388262011-01-25 22:14:50 +0000725 __le32 *page_table = (__le32 *) dma->pgtbl;
Michael Chan71034ba2009-10-10 13:46:59 +0000726
727 for (i = 0; i < dma->num_pages; i++) {
728 /* Each entry needs to be in little endian format. */
Michael Chan51388262011-01-25 22:14:50 +0000729 *page_table = cpu_to_le32(dma->pg_map_arr[i] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +0000730 page_table++;
Michael Chan51388262011-01-25 22:14:50 +0000731 *page_table = cpu_to_le32((u64) dma->pg_map_arr[i] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +0000732 page_table++;
733 }
734}
735
Michael Chana4636962009-06-08 18:14:43 -0700736static int cnic_alloc_dma(struct cnic_dev *dev, struct cnic_dma *dma,
737 int pages, int use_pg_tbl)
738{
739 int i, size;
740 struct cnic_local *cp = dev->cnic_priv;
741
742 size = pages * (sizeof(void *) + sizeof(dma_addr_t));
743 dma->pg_arr = kzalloc(size, GFP_ATOMIC);
744 if (dma->pg_arr == NULL)
745 return -ENOMEM;
746
747 dma->pg_map_arr = (dma_addr_t *) (dma->pg_arr + pages);
748 dma->num_pages = pages;
749
750 for (i = 0; i < pages; i++) {
Michael Chan3248e162009-12-02 15:15:39 +0000751 dma->pg_arr[i] = dma_alloc_coherent(&dev->pcidev->dev,
752 BCM_PAGE_SIZE,
753 &dma->pg_map_arr[i],
754 GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700755 if (dma->pg_arr[i] == NULL)
756 goto error;
757 }
758 if (!use_pg_tbl)
759 return 0;
760
761 dma->pgtbl_size = ((pages * 8) + BCM_PAGE_SIZE - 1) &
762 ~(BCM_PAGE_SIZE - 1);
Michael Chan3248e162009-12-02 15:15:39 +0000763 dma->pgtbl = dma_alloc_coherent(&dev->pcidev->dev, dma->pgtbl_size,
764 &dma->pgtbl_map, GFP_ATOMIC);
Michael Chana4636962009-06-08 18:14:43 -0700765 if (dma->pgtbl == NULL)
766 goto error;
767
768 cp->setup_pgtbl(dev, dma);
769
770 return 0;
771
772error:
773 cnic_free_dma(dev, dma);
774 return -ENOMEM;
775}
776
Michael Chan86b53602009-10-10 13:46:57 +0000777static void cnic_free_context(struct cnic_dev *dev)
778{
779 struct cnic_local *cp = dev->cnic_priv;
780 int i;
781
782 for (i = 0; i < cp->ctx_blks; i++) {
783 if (cp->ctx_arr[i].ctx) {
Michael Chan3248e162009-12-02 15:15:39 +0000784 dma_free_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
785 cp->ctx_arr[i].ctx,
786 cp->ctx_arr[i].mapping);
Michael Chan86b53602009-10-10 13:46:57 +0000787 cp->ctx_arr[i].ctx = NULL;
788 }
789 }
790}
791
Michael Chancd801532010-10-13 14:06:49 +0000792static void __cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chana4636962009-06-08 18:14:43 -0700793{
Michael Chancd801532010-10-13 14:06:49 +0000794 uio_unregister_device(&udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -0700795
Michael Chancd801532010-10-13 14:06:49 +0000796 if (udev->l2_buf) {
797 dma_free_coherent(&udev->pdev->dev, udev->l2_buf_size,
798 udev->l2_buf, udev->l2_buf_map);
799 udev->l2_buf = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700800 }
801
Michael Chancd801532010-10-13 14:06:49 +0000802 if (udev->l2_ring) {
803 dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
804 udev->l2_ring, udev->l2_ring_map);
805 udev->l2_ring = NULL;
Michael Chana4636962009-06-08 18:14:43 -0700806 }
Michael Chana3ceeeb2010-10-13 14:06:50 +0000807
808 pci_dev_put(udev->pdev);
809 kfree(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000810}
811
Michael Chancd801532010-10-13 14:06:49 +0000812static void cnic_free_uio(struct cnic_uio_dev *udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000813{
Michael Chancd801532010-10-13 14:06:49 +0000814 if (!udev)
Michael Chanc06c0462010-10-13 14:06:48 +0000815 return;
816
Michael Chana3ceeeb2010-10-13 14:06:50 +0000817 write_lock(&cnic_dev_lock);
818 list_del_init(&udev->list);
819 write_unlock(&cnic_dev_lock);
Michael Chancd801532010-10-13 14:06:49 +0000820 __cnic_free_uio(udev);
Michael Chanc06c0462010-10-13 14:06:48 +0000821}
822
823static void cnic_free_resc(struct cnic_dev *dev)
824{
825 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000826 struct cnic_uio_dev *udev = cp->udev;
Michael Chanc06c0462010-10-13 14:06:48 +0000827
Michael Chancd801532010-10-13 14:06:49 +0000828 if (udev) {
Michael Chana3ceeeb2010-10-13 14:06:50 +0000829 udev->dev = NULL;
Michael Chancd801532010-10-13 14:06:49 +0000830 cp->udev = NULL;
Michael Chanc06c0462010-10-13 14:06:48 +0000831 }
Michael Chana4636962009-06-08 18:14:43 -0700832
Michael Chan86b53602009-10-10 13:46:57 +0000833 cnic_free_context(dev);
Michael Chana4636962009-06-08 18:14:43 -0700834 kfree(cp->ctx_arr);
835 cp->ctx_arr = NULL;
836 cp->ctx_blks = 0;
837
838 cnic_free_dma(dev, &cp->gbl_buf_info);
839 cnic_free_dma(dev, &cp->conn_buf_info);
840 cnic_free_dma(dev, &cp->kwq_info);
Michael Chan71034ba2009-10-10 13:46:59 +0000841 cnic_free_dma(dev, &cp->kwq_16_data_info);
Michael Chane21ba412010-12-23 07:43:03 +0000842 cnic_free_dma(dev, &cp->kcq2.dma);
Michael Chane6c28892010-06-24 14:58:39 +0000843 cnic_free_dma(dev, &cp->kcq1.dma);
Michael Chana4636962009-06-08 18:14:43 -0700844 kfree(cp->iscsi_tbl);
845 cp->iscsi_tbl = NULL;
846 kfree(cp->ctx_tbl);
847 cp->ctx_tbl = NULL;
848
Michael Chane1928c82010-12-23 07:43:04 +0000849 cnic_free_id_tbl(&cp->fcoe_cid_tbl);
Michael Chana4636962009-06-08 18:14:43 -0700850 cnic_free_id_tbl(&cp->cid_tbl);
851}
852
853static int cnic_alloc_context(struct cnic_dev *dev)
854{
855 struct cnic_local *cp = dev->cnic_priv;
856
857 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
858 int i, k, arr_size;
859
860 cp->ctx_blk_size = BCM_PAGE_SIZE;
861 cp->cids_per_blk = BCM_PAGE_SIZE / 128;
862 arr_size = BNX2_MAX_CID / cp->cids_per_blk *
863 sizeof(struct cnic_ctx);
864 cp->ctx_arr = kzalloc(arr_size, GFP_KERNEL);
865 if (cp->ctx_arr == NULL)
866 return -ENOMEM;
867
868 k = 0;
869 for (i = 0; i < 2; i++) {
870 u32 j, reg, off, lo, hi;
871
872 if (i == 0)
873 off = BNX2_PG_CTX_MAP;
874 else
875 off = BNX2_ISCSI_CTX_MAP;
876
877 reg = cnic_reg_rd_ind(dev, off);
878 lo = reg >> 16;
879 hi = reg & 0xffff;
880 for (j = lo; j < hi; j += cp->cids_per_blk, k++)
881 cp->ctx_arr[k].cid = j;
882 }
883
884 cp->ctx_blks = k;
885 if (cp->ctx_blks >= (BNX2_MAX_CID / cp->cids_per_blk)) {
886 cp->ctx_blks = 0;
887 return -ENOMEM;
888 }
889
890 for (i = 0; i < cp->ctx_blks; i++) {
891 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +0000892 dma_alloc_coherent(&dev->pcidev->dev,
893 BCM_PAGE_SIZE,
894 &cp->ctx_arr[i].mapping,
895 GFP_KERNEL);
Michael Chana4636962009-06-08 18:14:43 -0700896 if (cp->ctx_arr[i].ctx == NULL)
897 return -ENOMEM;
898 }
899 }
900 return 0;
901}
902
Michael Chan59e51372011-06-14 01:32:38 +0000903static u16 cnic_bnx2_next_idx(u16 idx)
Michael Chane6c28892010-06-24 14:58:39 +0000904{
Michael Chan59e51372011-06-14 01:32:38 +0000905 return idx + 1;
906}
907
908static u16 cnic_bnx2_hw_idx(u16 idx)
909{
910 return idx;
911}
912
913static u16 cnic_bnx2x_next_idx(u16 idx)
914{
915 idx++;
916 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
917 idx++;
918
919 return idx;
920}
921
922static u16 cnic_bnx2x_hw_idx(u16 idx)
923{
924 if ((idx & MAX_KCQE_CNT) == MAX_KCQE_CNT)
925 idx++;
926 return idx;
927}
928
929static int cnic_alloc_kcq(struct cnic_dev *dev, struct kcq_info *info,
930 bool use_pg_tbl)
931{
932 int err, i, use_page_tbl = 0;
Michael Chane6c28892010-06-24 14:58:39 +0000933 struct kcqe **kcq;
934
Michael Chan59e51372011-06-14 01:32:38 +0000935 if (use_pg_tbl)
936 use_page_tbl = 1;
Michael Chane6c28892010-06-24 14:58:39 +0000937
Michael Chan59e51372011-06-14 01:32:38 +0000938 err = cnic_alloc_dma(dev, &info->dma, KCQ_PAGE_CNT, use_page_tbl);
Michael Chane6c28892010-06-24 14:58:39 +0000939 if (err)
940 return err;
941
942 kcq = (struct kcqe **) info->dma.pg_arr;
943 info->kcq = kcq;
944
Michael Chan59e51372011-06-14 01:32:38 +0000945 info->next_idx = cnic_bnx2_next_idx;
946 info->hw_idx = cnic_bnx2_hw_idx;
947 if (use_pg_tbl)
Michael Chane6c28892010-06-24 14:58:39 +0000948 return 0;
949
Michael Chan59e51372011-06-14 01:32:38 +0000950 info->next_idx = cnic_bnx2x_next_idx;
951 info->hw_idx = cnic_bnx2x_hw_idx;
952
Michael Chane6c28892010-06-24 14:58:39 +0000953 for (i = 0; i < KCQ_PAGE_CNT; i++) {
954 struct bnx2x_bd_chain_next *next =
955 (struct bnx2x_bd_chain_next *) &kcq[i][MAX_KCQE_CNT];
956 int j = i + 1;
957
958 if (j >= KCQ_PAGE_CNT)
959 j = 0;
960 next->addr_hi = (u64) info->dma.pg_map_arr[j] >> 32;
961 next->addr_lo = info->dma.pg_map_arr[j] & 0xffffffff;
962 }
963 return 0;
964}
965
Michael Chancd801532010-10-13 14:06:49 +0000966static int cnic_alloc_uio_rings(struct cnic_dev *dev, int pages)
Michael Chanec0248e2009-08-26 09:49:22 +0000967{
968 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +0000969 struct cnic_uio_dev *udev;
Michael Chanec0248e2009-08-26 09:49:22 +0000970
Michael Chana3ceeeb2010-10-13 14:06:50 +0000971 read_lock(&cnic_dev_lock);
972 list_for_each_entry(udev, &cnic_udev_list, list) {
973 if (udev->pdev == dev->pcidev) {
974 udev->dev = dev;
975 cp->udev = udev;
976 read_unlock(&cnic_dev_lock);
977 return 0;
978 }
979 }
980 read_unlock(&cnic_dev_lock);
981
Michael Chancd801532010-10-13 14:06:49 +0000982 udev = kzalloc(sizeof(struct cnic_uio_dev), GFP_ATOMIC);
983 if (!udev)
Michael Chanec0248e2009-08-26 09:49:22 +0000984 return -ENOMEM;
985
Michael Chancd801532010-10-13 14:06:49 +0000986 udev->uio_dev = -1;
987
988 udev->dev = dev;
989 udev->pdev = dev->pcidev;
990 udev->l2_ring_size = pages * BCM_PAGE_SIZE;
991 udev->l2_ring = dma_alloc_coherent(&udev->pdev->dev, udev->l2_ring_size,
992 &udev->l2_ring_map,
993 GFP_KERNEL | __GFP_COMP);
994 if (!udev->l2_ring)
Jesper Juhlf7e4c972010-12-31 11:18:48 -0800995 goto err_udev;
Michael Chanec0248e2009-08-26 09:49:22 +0000996
Michael Chancd801532010-10-13 14:06:49 +0000997 udev->l2_buf_size = (cp->l2_rx_ring_size + 1) * cp->l2_single_buf_size;
998 udev->l2_buf_size = PAGE_ALIGN(udev->l2_buf_size);
999 udev->l2_buf = dma_alloc_coherent(&udev->pdev->dev, udev->l2_buf_size,
1000 &udev->l2_buf_map,
1001 GFP_KERNEL | __GFP_COMP);
1002 if (!udev->l2_buf)
Jesper Juhlf7e4c972010-12-31 11:18:48 -08001003 goto err_dma;
Michael Chancd801532010-10-13 14:06:49 +00001004
Michael Chana3ceeeb2010-10-13 14:06:50 +00001005 write_lock(&cnic_dev_lock);
1006 list_add(&udev->list, &cnic_udev_list);
1007 write_unlock(&cnic_dev_lock);
1008
1009 pci_dev_get(udev->pdev);
1010
Michael Chancd801532010-10-13 14:06:49 +00001011 cp->udev = udev;
1012
Michael Chanec0248e2009-08-26 09:49:22 +00001013 return 0;
Jesper Juhlf7e4c972010-12-31 11:18:48 -08001014 err_dma:
1015 dma_free_coherent(&udev->pdev->dev, udev->l2_ring_size,
1016 udev->l2_ring, udev->l2_ring_map);
1017 err_udev:
1018 kfree(udev);
1019 return -ENOMEM;
Michael Chanec0248e2009-08-26 09:49:22 +00001020}
1021
Michael Chancd801532010-10-13 14:06:49 +00001022static int cnic_init_uio(struct cnic_dev *dev)
1023{
Michael Chan5e9b2db2009-08-26 09:49:23 +00001024 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00001025 struct cnic_uio_dev *udev = cp->udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001026 struct uio_info *uinfo;
Michael Chancd801532010-10-13 14:06:49 +00001027 int ret = 0;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001028
Michael Chancd801532010-10-13 14:06:49 +00001029 if (!udev)
Michael Chan5e9b2db2009-08-26 09:49:23 +00001030 return -ENOMEM;
1031
Michael Chancd801532010-10-13 14:06:49 +00001032 uinfo = &udev->cnic_uinfo;
1033
Michael Chan5e9b2db2009-08-26 09:49:23 +00001034 uinfo->mem[0].addr = dev->netdev->base_addr;
1035 uinfo->mem[0].internal_addr = dev->regview;
1036 uinfo->mem[0].size = dev->netdev->mem_end - dev->netdev->mem_start;
1037 uinfo->mem[0].memtype = UIO_MEM_PHYS;
1038
Michael Chan5e9b2db2009-08-26 09:49:23 +00001039 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
Michael Chana4dde3a2010-02-24 14:42:08 +00001040 uinfo->mem[1].addr = (unsigned long) cp->status_blk.gen &
Michael Chancd801532010-10-13 14:06:49 +00001041 PAGE_MASK;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001042 if (cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
1043 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE * 9;
1044 else
1045 uinfo->mem[1].size = BNX2_SBLK_MSIX_ALIGN_SIZE;
1046
1047 uinfo->name = "bnx2_cnic";
Michael Chan71034ba2009-10-10 13:46:59 +00001048 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
1049 uinfo->mem[1].addr = (unsigned long) cp->bnx2x_def_status_blk &
1050 PAGE_MASK;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001051 uinfo->mem[1].size = sizeof(*cp->bnx2x_def_status_blk);
Michael Chan71034ba2009-10-10 13:46:59 +00001052
1053 uinfo->name = "bnx2x_cnic";
Michael Chan5e9b2db2009-08-26 09:49:23 +00001054 }
1055
1056 uinfo->mem[1].memtype = UIO_MEM_LOGICAL;
1057
Michael Chancd801532010-10-13 14:06:49 +00001058 uinfo->mem[2].addr = (unsigned long) udev->l2_ring;
1059 uinfo->mem[2].size = udev->l2_ring_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001060 uinfo->mem[2].memtype = UIO_MEM_LOGICAL;
1061
Michael Chancd801532010-10-13 14:06:49 +00001062 uinfo->mem[3].addr = (unsigned long) udev->l2_buf;
1063 uinfo->mem[3].size = udev->l2_buf_size;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001064 uinfo->mem[3].memtype = UIO_MEM_LOGICAL;
1065
1066 uinfo->version = CNIC_MODULE_VERSION;
1067 uinfo->irq = UIO_IRQ_CUSTOM;
1068
1069 uinfo->open = cnic_uio_open;
1070 uinfo->release = cnic_uio_close;
1071
Michael Chana3ceeeb2010-10-13 14:06:50 +00001072 if (udev->uio_dev == -1) {
1073 if (!uinfo->priv) {
1074 uinfo->priv = udev;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001075
Michael Chana3ceeeb2010-10-13 14:06:50 +00001076 ret = uio_register_device(&udev->pdev->dev, uinfo);
1077 }
1078 } else {
1079 cnic_init_rings(dev);
1080 }
Michael Chan5e9b2db2009-08-26 09:49:23 +00001081
Michael Chancd801532010-10-13 14:06:49 +00001082 return ret;
Michael Chan5e9b2db2009-08-26 09:49:23 +00001083}
1084
Michael Chana4636962009-06-08 18:14:43 -07001085static int cnic_alloc_bnx2_resc(struct cnic_dev *dev)
1086{
1087 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07001088 int ret;
1089
1090 ret = cnic_alloc_dma(dev, &cp->kwq_info, KWQ_PAGE_CNT, 1);
1091 if (ret)
1092 goto error;
1093 cp->kwq = (struct kwqe **) cp->kwq_info.pg_arr;
1094
Michael Chan59e51372011-06-14 01:32:38 +00001095 ret = cnic_alloc_kcq(dev, &cp->kcq1, true);
Michael Chana4636962009-06-08 18:14:43 -07001096 if (ret)
1097 goto error;
Michael Chana4636962009-06-08 18:14:43 -07001098
1099 ret = cnic_alloc_context(dev);
1100 if (ret)
1101 goto error;
1102
Michael Chancd801532010-10-13 14:06:49 +00001103 ret = cnic_alloc_uio_rings(dev, 2);
Michael Chanec0248e2009-08-26 09:49:22 +00001104 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001105 goto error;
1106
Michael Chancd801532010-10-13 14:06:49 +00001107 ret = cnic_init_uio(dev);
Michael Chan5e9b2db2009-08-26 09:49:23 +00001108 if (ret)
Michael Chana4636962009-06-08 18:14:43 -07001109 goto error;
1110
Michael Chana4636962009-06-08 18:14:43 -07001111 return 0;
1112
1113error:
1114 cnic_free_resc(dev);
1115 return ret;
1116}
1117
Michael Chan71034ba2009-10-10 13:46:59 +00001118static int cnic_alloc_bnx2x_context(struct cnic_dev *dev)
1119{
1120 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001121 int ctx_blk_size = cp->ethdev->ctx_blk_size;
Michael Chan520efdf2010-06-24 14:58:37 +00001122 int total_mem, blks, i;
Michael Chan71034ba2009-10-10 13:46:59 +00001123
Michael Chan520efdf2010-06-24 14:58:37 +00001124 total_mem = BNX2X_CONTEXT_MEM_SIZE * cp->max_cid_space;
Michael Chan71034ba2009-10-10 13:46:59 +00001125 blks = total_mem / ctx_blk_size;
1126 if (total_mem % ctx_blk_size)
1127 blks++;
1128
1129 if (blks > cp->ethdev->ctx_tbl_len)
1130 return -ENOMEM;
1131
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001132 cp->ctx_arr = kcalloc(blks, sizeof(struct cnic_ctx), GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001133 if (cp->ctx_arr == NULL)
1134 return -ENOMEM;
1135
1136 cp->ctx_blks = blks;
1137 cp->ctx_blk_size = ctx_blk_size;
Michael Chanee87a822010-10-13 14:06:51 +00001138 if (!BNX2X_CHIP_IS_57710(cp->chip_id))
Michael Chan71034ba2009-10-10 13:46:59 +00001139 cp->ctx_align = 0;
1140 else
1141 cp->ctx_align = ctx_blk_size;
1142
1143 cp->cids_per_blk = ctx_blk_size / BNX2X_CONTEXT_MEM_SIZE;
1144
1145 for (i = 0; i < blks; i++) {
1146 cp->ctx_arr[i].ctx =
Michael Chan3248e162009-12-02 15:15:39 +00001147 dma_alloc_coherent(&dev->pcidev->dev, cp->ctx_blk_size,
1148 &cp->ctx_arr[i].mapping,
1149 GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001150 if (cp->ctx_arr[i].ctx == NULL)
1151 return -ENOMEM;
1152
1153 if (cp->ctx_align && cp->ctx_blk_size == ctx_blk_size) {
1154 if (cp->ctx_arr[i].mapping & (cp->ctx_align - 1)) {
1155 cnic_free_context(dev);
1156 cp->ctx_blk_size += cp->ctx_align;
1157 i = -1;
1158 continue;
1159 }
1160 }
1161 }
1162 return 0;
1163}
1164
1165static int cnic_alloc_bnx2x_resc(struct cnic_dev *dev)
1166{
1167 struct cnic_local *cp = dev->cnic_priv;
Michael Chan520efdf2010-06-24 14:58:37 +00001168 struct cnic_eth_dev *ethdev = cp->ethdev;
1169 u32 start_cid = ethdev->starting_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001170 int i, j, n, ret, pages;
1171 struct cnic_dma *kwq_16_dma = &cp->kwq_16_data_info;
1172
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001173 cp->iro_arr = ethdev->iro_arr;
1174
Michael Chane1928c82010-12-23 07:43:04 +00001175 cp->max_cid_space = MAX_ISCSI_TBL_SZ + BNX2X_FCOE_NUM_CONNECTIONS;
Michael Chan520efdf2010-06-24 14:58:37 +00001176 cp->iscsi_start_cid = start_cid;
Michael Chane1928c82010-12-23 07:43:04 +00001177 cp->fcoe_start_cid = start_cid + MAX_ISCSI_TBL_SZ;
1178
1179 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
1180 cp->max_cid_space += BNX2X_FCOE_NUM_CONNECTIONS;
1181 cp->fcoe_init_cid = ethdev->fcoe_init_cid;
1182 if (!cp->fcoe_init_cid)
1183 cp->fcoe_init_cid = 0x10;
1184 }
1185
Michael Chan520efdf2010-06-24 14:58:37 +00001186 if (start_cid < BNX2X_ISCSI_START_CID) {
1187 u32 delta = BNX2X_ISCSI_START_CID - start_cid;
1188
1189 cp->iscsi_start_cid = BNX2X_ISCSI_START_CID;
Michael Chane1928c82010-12-23 07:43:04 +00001190 cp->fcoe_start_cid += delta;
Michael Chan520efdf2010-06-24 14:58:37 +00001191 cp->max_cid_space += delta;
1192 }
1193
Michael Chan71034ba2009-10-10 13:46:59 +00001194 cp->iscsi_tbl = kzalloc(sizeof(struct cnic_iscsi) * MAX_ISCSI_TBL_SZ,
1195 GFP_KERNEL);
1196 if (!cp->iscsi_tbl)
1197 goto error;
1198
1199 cp->ctx_tbl = kzalloc(sizeof(struct cnic_context) *
Michael Chan520efdf2010-06-24 14:58:37 +00001200 cp->max_cid_space, GFP_KERNEL);
Michael Chan71034ba2009-10-10 13:46:59 +00001201 if (!cp->ctx_tbl)
1202 goto error;
1203
1204 for (i = 0; i < MAX_ISCSI_TBL_SZ; i++) {
1205 cp->ctx_tbl[i].proto.iscsi = &cp->iscsi_tbl[i];
1206 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_ISCSI;
1207 }
1208
Michael Chane1928c82010-12-23 07:43:04 +00001209 for (i = MAX_ISCSI_TBL_SZ; i < cp->max_cid_space; i++)
1210 cp->ctx_tbl[i].ulp_proto_id = CNIC_ULP_FCOE;
1211
Michael Chan520efdf2010-06-24 14:58:37 +00001212 pages = PAGE_ALIGN(cp->max_cid_space * CNIC_KWQ16_DATA_SIZE) /
Michael Chan71034ba2009-10-10 13:46:59 +00001213 PAGE_SIZE;
1214
1215 ret = cnic_alloc_dma(dev, kwq_16_dma, pages, 0);
1216 if (ret)
1217 return -ENOMEM;
1218
1219 n = PAGE_SIZE / CNIC_KWQ16_DATA_SIZE;
Michael Chan520efdf2010-06-24 14:58:37 +00001220 for (i = 0, j = 0; i < cp->max_cid_space; i++) {
Michael Chan71034ba2009-10-10 13:46:59 +00001221 long off = CNIC_KWQ16_DATA_SIZE * (i % n);
1222
1223 cp->ctx_tbl[i].kwqe_data = kwq_16_dma->pg_arr[j] + off;
1224 cp->ctx_tbl[i].kwqe_data_mapping = kwq_16_dma->pg_map_arr[j] +
1225 off;
1226
1227 if ((i % n) == (n - 1))
1228 j++;
1229 }
1230
Michael Chan59e51372011-06-14 01:32:38 +00001231 ret = cnic_alloc_kcq(dev, &cp->kcq1, false);
Michael Chan71034ba2009-10-10 13:46:59 +00001232 if (ret)
1233 goto error;
Michael Chan71034ba2009-10-10 13:46:59 +00001234
Michael Chane21ba412010-12-23 07:43:03 +00001235 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
Michael Chan59e51372011-06-14 01:32:38 +00001236 ret = cnic_alloc_kcq(dev, &cp->kcq2, false);
Michael Chane21ba412010-12-23 07:43:03 +00001237 if (ret)
1238 goto error;
1239 }
1240
Michael Chan71034ba2009-10-10 13:46:59 +00001241 pages = PAGE_ALIGN(BNX2X_ISCSI_NUM_CONNECTIONS *
1242 BNX2X_ISCSI_CONN_BUF_SIZE) / PAGE_SIZE;
1243 ret = cnic_alloc_dma(dev, &cp->conn_buf_info, pages, 1);
1244 if (ret)
1245 goto error;
1246
1247 pages = PAGE_ALIGN(BNX2X_ISCSI_GLB_BUF_SIZE) / PAGE_SIZE;
1248 ret = cnic_alloc_dma(dev, &cp->gbl_buf_info, pages, 0);
1249 if (ret)
1250 goto error;
1251
1252 ret = cnic_alloc_bnx2x_context(dev);
1253 if (ret)
1254 goto error;
1255
Michael Chan71034ba2009-10-10 13:46:59 +00001256 cp->bnx2x_def_status_blk = cp->ethdev->irq_arr[1].status_blk;
1257
1258 cp->l2_rx_ring_size = 15;
1259
Michael Chancd801532010-10-13 14:06:49 +00001260 ret = cnic_alloc_uio_rings(dev, 4);
Michael Chan71034ba2009-10-10 13:46:59 +00001261 if (ret)
1262 goto error;
1263
Michael Chancd801532010-10-13 14:06:49 +00001264 ret = cnic_init_uio(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00001265 if (ret)
1266 goto error;
1267
1268 return 0;
1269
1270error:
1271 cnic_free_resc(dev);
1272 return -ENOMEM;
1273}
1274
Michael Chana4636962009-06-08 18:14:43 -07001275static inline u32 cnic_kwq_avail(struct cnic_local *cp)
1276{
1277 return cp->max_kwq_idx -
1278 ((cp->kwq_prod_idx - cp->kwq_con_idx) & cp->max_kwq_idx);
1279}
1280
1281static int cnic_submit_bnx2_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
1282 u32 num_wqes)
1283{
1284 struct cnic_local *cp = dev->cnic_priv;
1285 struct kwqe *prod_qe;
1286 u16 prod, sw_prod, i;
1287
1288 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
1289 return -EAGAIN; /* bnx2 is down */
1290
1291 spin_lock_bh(&cp->cnic_ulp_lock);
1292 if (num_wqes > cnic_kwq_avail(cp) &&
Michael Chan1f1332a2010-05-18 11:32:52 +00001293 !test_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags)) {
Michael Chana4636962009-06-08 18:14:43 -07001294 spin_unlock_bh(&cp->cnic_ulp_lock);
1295 return -EAGAIN;
1296 }
1297
Michael Chan1f1332a2010-05-18 11:32:52 +00001298 clear_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07001299
1300 prod = cp->kwq_prod_idx;
1301 sw_prod = prod & MAX_KWQ_IDX;
1302 for (i = 0; i < num_wqes; i++) {
1303 prod_qe = &cp->kwq[KWQ_PG(sw_prod)][KWQ_IDX(sw_prod)];
1304 memcpy(prod_qe, wqes[i], sizeof(struct kwqe));
1305 prod++;
1306 sw_prod = prod & MAX_KWQ_IDX;
1307 }
1308 cp->kwq_prod_idx = prod;
1309
1310 CNIC_WR16(dev, cp->kwq_io_addr, cp->kwq_prod_idx);
1311
1312 spin_unlock_bh(&cp->cnic_ulp_lock);
1313 return 0;
1314}
1315
Michael Chan71034ba2009-10-10 13:46:59 +00001316static void *cnic_get_kwqe_16_data(struct cnic_local *cp, u32 l5_cid,
1317 union l5cm_specific_data *l5_data)
1318{
1319 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1320 dma_addr_t map;
1321
1322 map = ctx->kwqe_data_mapping;
1323 l5_data->phy_address.lo = (u64) map & 0xffffffff;
1324 l5_data->phy_address.hi = (u64) map >> 32;
1325 return ctx->kwqe_data;
1326}
1327
1328static int cnic_submit_kwqe_16(struct cnic_dev *dev, u32 cmd, u32 cid,
1329 u32 type, union l5cm_specific_data *l5_data)
1330{
1331 struct cnic_local *cp = dev->cnic_priv;
1332 struct l5cm_spe kwqe;
1333 struct kwqe_16 *kwq[1];
Michael Chan68d7c1a2011-01-05 15:14:13 +00001334 u16 type_16;
Michael Chan71034ba2009-10-10 13:46:59 +00001335 int ret;
1336
1337 kwqe.hdr.conn_and_cmd_data =
1338 cpu_to_le32(((cmd << SPE_HDR_CMD_ID_SHIFT) |
Michael Chanceb7e1c2010-10-06 03:14:54 +00001339 BNX2X_HW_CID(cp, cid)));
Michael Chan68d7c1a2011-01-05 15:14:13 +00001340
1341 type_16 = (type << SPE_HDR_CONN_TYPE_SHIFT) & SPE_HDR_CONN_TYPE;
1342 type_16 |= (cp->pfid << SPE_HDR_FUNCTION_ID_SHIFT) &
1343 SPE_HDR_FUNCTION_ID;
1344
1345 kwqe.hdr.type = cpu_to_le16(type_16);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001346 kwqe.hdr.reserved1 = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001347 kwqe.data.phy_address.lo = cpu_to_le32(l5_data->phy_address.lo);
1348 kwqe.data.phy_address.hi = cpu_to_le32(l5_data->phy_address.hi);
1349
1350 kwq[0] = (struct kwqe_16 *) &kwqe;
1351
1352 spin_lock_bh(&cp->cnic_ulp_lock);
1353 ret = cp->ethdev->drv_submit_kwqes_16(dev->netdev, kwq, 1);
1354 spin_unlock_bh(&cp->cnic_ulp_lock);
1355
1356 if (ret == 1)
1357 return 0;
1358
1359 return -EBUSY;
1360}
1361
1362static void cnic_reply_bnx2x_kcqes(struct cnic_dev *dev, int ulp_type,
1363 struct kcqe *cqes[], u32 num_cqes)
1364{
1365 struct cnic_local *cp = dev->cnic_priv;
1366 struct cnic_ulp_ops *ulp_ops;
1367
1368 rcu_read_lock();
1369 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
1370 if (likely(ulp_ops)) {
1371 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
1372 cqes, num_cqes);
1373 }
1374 rcu_read_unlock();
1375}
1376
1377static int cnic_bnx2x_iscsi_init1(struct cnic_dev *dev, struct kwqe *kwqe)
1378{
1379 struct cnic_local *cp = dev->cnic_priv;
1380 struct iscsi_kwqe_init1 *req1 = (struct iscsi_kwqe_init1 *) kwqe;
Michael Chan14203982010-10-06 03:16:06 +00001381 int hq_bds, pages;
1382 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001383
1384 cp->num_iscsi_tasks = req1->num_tasks_per_conn;
1385 cp->num_ccells = req1->num_ccells_per_conn;
1386 cp->task_array_size = BNX2X_ISCSI_TASK_CONTEXT_SIZE *
1387 cp->num_iscsi_tasks;
1388 cp->r2tq_size = cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS *
1389 BNX2X_ISCSI_R2TQE_SIZE;
1390 cp->hq_size = cp->num_ccells * BNX2X_ISCSI_HQ_BD_SIZE;
1391 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1392 hq_bds = pages * (PAGE_SIZE / BNX2X_ISCSI_HQ_BD_SIZE);
1393 cp->num_cqs = req1->num_cqs;
1394
1395 if (!dev->max_iscsi_conn)
1396 return 0;
1397
1398 /* init Tstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001399 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001400 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001401 CNIC_WR16(dev, BAR_TSTRORM_INTMEM + TSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001402 PAGE_SIZE);
1403 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001404 TSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001405 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001406 TSTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001407 req1->num_tasks_per_conn);
1408
1409 /* init Ustorm RAM */
1410 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001411 USTORM_ISCSI_RQ_BUFFER_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001412 req1->rq_buffer_size);
Michael Chan14203982010-10-06 03:16:06 +00001413 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001414 PAGE_SIZE);
1415 CNIC_WR8(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001416 USTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001417 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001418 USTORM_ISCSI_NUM_OF_TASKS_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001419 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001420 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_RQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001421 req1->rq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001422 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001423 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001424 CNIC_WR16(dev, BAR_USTRORM_INTMEM + USTORM_ISCSI_R2TQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001425 cp->num_iscsi_tasks * BNX2X_ISCSI_MAX_PENDING_R2TS);
1426
1427 /* init Xstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001428 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001429 PAGE_SIZE);
1430 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001431 XSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001432 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001433 XSTORM_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_XSTRORM_INTMEM + XSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001436 hq_bds);
Michael Chan14203982010-10-06 03:16:06 +00001437 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_ISCSI_SQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001438 req1->num_tasks_per_conn);
Michael Chan14203982010-10-06 03:16:06 +00001439 CNIC_WR16(dev, BAR_XSTRORM_INTMEM + XSTORM_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 Cstorm RAM */
Michael Chan14203982010-10-06 03:16:06 +00001443 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_PAGE_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001444 PAGE_SIZE);
1445 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001446 CSTORM_ISCSI_PAGE_SIZE_LOG_OFFSET(pfid), PAGE_SHIFT);
Michael Chan71034ba2009-10-10 13:46:59 +00001447 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001448 CSTORM_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_CSTRORM_INTMEM + CSTORM_ISCSI_CQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001451 req1->cq_num_wqes);
Michael Chan14203982010-10-06 03:16:06 +00001452 CNIC_WR16(dev, BAR_CSTRORM_INTMEM + CSTORM_ISCSI_HQ_SIZE_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00001453 hq_bds);
1454
1455 return 0;
1456}
1457
1458static int cnic_bnx2x_iscsi_init2(struct cnic_dev *dev, struct kwqe *kwqe)
1459{
1460 struct iscsi_kwqe_init2 *req2 = (struct iscsi_kwqe_init2 *) kwqe;
1461 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00001462 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001463 struct iscsi_kcqe kcqe;
1464 struct kcqe *cqes[1];
1465
1466 memset(&kcqe, 0, sizeof(kcqe));
1467 if (!dev->max_iscsi_conn) {
1468 kcqe.completion_status =
1469 ISCSI_KCQE_COMPLETION_STATUS_ISCSI_NOT_SUPPORTED;
1470 goto done;
1471 }
1472
1473 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001474 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001475 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001476 TSTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001477 req2->error_bit_map[1]);
1478
1479 CNIC_WR16(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001480 USTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001481 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001482 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid), req2->error_bit_map[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001483 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001484 USTORM_ISCSI_ERROR_BITMAP_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00001485 req2->error_bit_map[1]);
1486
1487 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001488 CSTORM_ISCSI_CQ_SQN_SIZE_OFFSET(pfid), req2->max_cq_sqn);
Michael Chan71034ba2009-10-10 13:46:59 +00001489
1490 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1491
1492done:
1493 kcqe.op_code = ISCSI_KCQE_OPCODE_INIT;
1494 cqes[0] = (struct kcqe *) &kcqe;
1495 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1496
1497 return 0;
1498}
1499
1500static void cnic_free_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1501{
1502 struct cnic_local *cp = dev->cnic_priv;
1503 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1504
1505 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI) {
1506 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1507
1508 cnic_free_dma(dev, &iscsi->hq_info);
1509 cnic_free_dma(dev, &iscsi->r2tq_info);
1510 cnic_free_dma(dev, &iscsi->task_array_info);
Michael Chane1928c82010-12-23 07:43:04 +00001511 cnic_free_id(&cp->cid_tbl, ctx->cid);
1512 } else {
1513 cnic_free_id(&cp->fcoe_cid_tbl, ctx->cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001514 }
Michael Chane1928c82010-12-23 07:43:04 +00001515
Michael Chan71034ba2009-10-10 13:46:59 +00001516 ctx->cid = 0;
1517}
1518
1519static int cnic_alloc_bnx2x_conn_resc(struct cnic_dev *dev, u32 l5_cid)
1520{
1521 u32 cid;
1522 int ret, pages;
1523 struct cnic_local *cp = dev->cnic_priv;
1524 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1525 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1526
Michael Chane1928c82010-12-23 07:43:04 +00001527 if (ctx->ulp_proto_id == CNIC_ULP_FCOE) {
1528 cid = cnic_alloc_new_id(&cp->fcoe_cid_tbl);
1529 if (cid == -1) {
1530 ret = -ENOMEM;
1531 goto error;
1532 }
1533 ctx->cid = cid;
1534 return 0;
1535 }
1536
Michael Chan71034ba2009-10-10 13:46:59 +00001537 cid = cnic_alloc_new_id(&cp->cid_tbl);
1538 if (cid == -1) {
1539 ret = -ENOMEM;
1540 goto error;
1541 }
1542
1543 ctx->cid = cid;
1544 pages = PAGE_ALIGN(cp->task_array_size) / PAGE_SIZE;
1545
1546 ret = cnic_alloc_dma(dev, &iscsi->task_array_info, pages, 1);
1547 if (ret)
1548 goto error;
1549
1550 pages = PAGE_ALIGN(cp->r2tq_size) / PAGE_SIZE;
1551 ret = cnic_alloc_dma(dev, &iscsi->r2tq_info, pages, 1);
1552 if (ret)
1553 goto error;
1554
1555 pages = PAGE_ALIGN(cp->hq_size) / PAGE_SIZE;
1556 ret = cnic_alloc_dma(dev, &iscsi->hq_info, pages, 1);
1557 if (ret)
1558 goto error;
1559
1560 return 0;
1561
1562error:
1563 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1564 return ret;
1565}
1566
1567static void *cnic_get_bnx2x_ctx(struct cnic_dev *dev, u32 cid, int init,
1568 struct regpair *ctx_addr)
1569{
1570 struct cnic_local *cp = dev->cnic_priv;
1571 struct cnic_eth_dev *ethdev = cp->ethdev;
1572 int blk = (cid - ethdev->starting_cid) / cp->cids_per_blk;
1573 int off = (cid - ethdev->starting_cid) % cp->cids_per_blk;
1574 unsigned long align_off = 0;
1575 dma_addr_t ctx_map;
1576 void *ctx;
1577
1578 if (cp->ctx_align) {
1579 unsigned long mask = cp->ctx_align - 1;
1580
1581 if (cp->ctx_arr[blk].mapping & mask)
1582 align_off = cp->ctx_align -
1583 (cp->ctx_arr[blk].mapping & mask);
1584 }
1585 ctx_map = cp->ctx_arr[blk].mapping + align_off +
1586 (off * BNX2X_CONTEXT_MEM_SIZE);
1587 ctx = cp->ctx_arr[blk].ctx + align_off +
1588 (off * BNX2X_CONTEXT_MEM_SIZE);
1589 if (init)
1590 memset(ctx, 0, BNX2X_CONTEXT_MEM_SIZE);
1591
1592 ctx_addr->lo = ctx_map & 0xffffffff;
1593 ctx_addr->hi = (u64) ctx_map >> 32;
1594 return ctx;
1595}
1596
1597static int cnic_setup_bnx2x_ctx(struct cnic_dev *dev, struct kwqe *wqes[],
1598 u32 num)
1599{
1600 struct cnic_local *cp = dev->cnic_priv;
1601 struct iscsi_kwqe_conn_offload1 *req1 =
1602 (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1603 struct iscsi_kwqe_conn_offload2 *req2 =
1604 (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1605 struct iscsi_kwqe_conn_offload3 *req3;
1606 struct cnic_context *ctx = &cp->ctx_tbl[req1->iscsi_conn_id];
1607 struct cnic_iscsi *iscsi = ctx->proto.iscsi;
1608 u32 cid = ctx->cid;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001609 u32 hw_cid = BNX2X_HW_CID(cp, cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001610 struct iscsi_context *ictx;
1611 struct regpair context_addr;
1612 int i, j, n = 2, n_max;
1613
1614 ctx->ctx_flags = 0;
1615 if (!req2->num_additional_wqes)
1616 return -EINVAL;
1617
1618 n_max = req2->num_additional_wqes + 2;
1619
1620 ictx = cnic_get_bnx2x_ctx(dev, cid, 1, &context_addr);
1621 if (ictx == NULL)
1622 return -ENOMEM;
1623
1624 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1625
1626 ictx->xstorm_ag_context.hq_prod = 1;
1627
1628 ictx->xstorm_st_context.iscsi.first_burst_length =
1629 ISCSI_DEF_FIRST_BURST_LEN;
1630 ictx->xstorm_st_context.iscsi.max_send_pdu_length =
1631 ISCSI_DEF_MAX_RECV_SEG_LEN;
1632 ictx->xstorm_st_context.iscsi.sq_pbl_base.lo =
1633 req1->sq_page_table_addr_lo;
1634 ictx->xstorm_st_context.iscsi.sq_pbl_base.hi =
1635 req1->sq_page_table_addr_hi;
1636 ictx->xstorm_st_context.iscsi.sq_curr_pbe.lo = req2->sq_first_pte.hi;
1637 ictx->xstorm_st_context.iscsi.sq_curr_pbe.hi = req2->sq_first_pte.lo;
1638 ictx->xstorm_st_context.iscsi.hq_pbl_base.lo =
1639 iscsi->hq_info.pgtbl_map & 0xffffffff;
1640 ictx->xstorm_st_context.iscsi.hq_pbl_base.hi =
1641 (u64) iscsi->hq_info.pgtbl_map >> 32;
1642 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.lo =
1643 iscsi->hq_info.pgtbl[0];
1644 ictx->xstorm_st_context.iscsi.hq_curr_pbe_base.hi =
1645 iscsi->hq_info.pgtbl[1];
1646 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.lo =
1647 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1648 ictx->xstorm_st_context.iscsi.r2tq_pbl_base.hi =
1649 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1650 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.lo =
1651 iscsi->r2tq_info.pgtbl[0];
1652 ictx->xstorm_st_context.iscsi.r2tq_curr_pbe_base.hi =
1653 iscsi->r2tq_info.pgtbl[1];
1654 ictx->xstorm_st_context.iscsi.task_pbl_base.lo =
1655 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1656 ictx->xstorm_st_context.iscsi.task_pbl_base.hi =
1657 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1658 ictx->xstorm_st_context.iscsi.task_pbl_cache_idx =
1659 BNX2X_ISCSI_PBL_NOT_CACHED;
1660 ictx->xstorm_st_context.iscsi.flags.flags |=
1661 XSTORM_ISCSI_CONTEXT_FLAGS_B_IMMEDIATE_DATA;
1662 ictx->xstorm_st_context.iscsi.flags.flags |=
1663 XSTORM_ISCSI_CONTEXT_FLAGS_B_INITIAL_R2T;
1664
1665 ictx->tstorm_st_context.iscsi.hdr_bytes_2_fetch = ISCSI_HEADER_SIZE;
1666 /* TSTORM requires the base address of RQ DB & not PTE */
1667 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.lo =
1668 req2->rq_page_table_addr_lo & PAGE_MASK;
1669 ictx->tstorm_st_context.iscsi.rq_db_phy_addr.hi =
1670 req2->rq_page_table_addr_hi;
1671 ictx->tstorm_st_context.iscsi.iscsi_conn_id = req1->iscsi_conn_id;
1672 ictx->tstorm_st_context.tcp.cwnd = 0x5A8;
1673 ictx->tstorm_st_context.tcp.flags2 |=
1674 TSTORM_TCP_ST_CONTEXT_SECTION_DA_EN;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001675 ictx->tstorm_st_context.tcp.ooo_support_mode =
1676 TCP_TSTORM_OOO_DROP_AND_PROC_ACK;
Michael Chan71034ba2009-10-10 13:46:59 +00001677
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001678 ictx->timers_context.flags |= TIMERS_BLOCK_CONTEXT_CONN_VALID_FLG;
Michael Chan71034ba2009-10-10 13:46:59 +00001679
1680 ictx->ustorm_st_context.ring.rq.pbl_base.lo =
Michael Chan15971c32009-12-02 15:15:38 +00001681 req2->rq_page_table_addr_lo;
Michael Chan71034ba2009-10-10 13:46:59 +00001682 ictx->ustorm_st_context.ring.rq.pbl_base.hi =
Michael Chan15971c32009-12-02 15:15:38 +00001683 req2->rq_page_table_addr_hi;
Michael Chan71034ba2009-10-10 13:46:59 +00001684 ictx->ustorm_st_context.ring.rq.curr_pbe.lo = req3->qp_first_pte[0].hi;
1685 ictx->ustorm_st_context.ring.rq.curr_pbe.hi = req3->qp_first_pte[0].lo;
1686 ictx->ustorm_st_context.ring.r2tq.pbl_base.lo =
1687 iscsi->r2tq_info.pgtbl_map & 0xffffffff;
1688 ictx->ustorm_st_context.ring.r2tq.pbl_base.hi =
1689 (u64) iscsi->r2tq_info.pgtbl_map >> 32;
1690 ictx->ustorm_st_context.ring.r2tq.curr_pbe.lo =
1691 iscsi->r2tq_info.pgtbl[0];
1692 ictx->ustorm_st_context.ring.r2tq.curr_pbe.hi =
1693 iscsi->r2tq_info.pgtbl[1];
1694 ictx->ustorm_st_context.ring.cq_pbl_base.lo =
1695 req1->cq_page_table_addr_lo;
1696 ictx->ustorm_st_context.ring.cq_pbl_base.hi =
1697 req1->cq_page_table_addr_hi;
1698 ictx->ustorm_st_context.ring.cq[0].cq_sn = ISCSI_INITIAL_SN;
1699 ictx->ustorm_st_context.ring.cq[0].curr_pbe.lo = req2->cq_first_pte.hi;
1700 ictx->ustorm_st_context.ring.cq[0].curr_pbe.hi = req2->cq_first_pte.lo;
1701 ictx->ustorm_st_context.task_pbe_cache_index =
1702 BNX2X_ISCSI_PBL_NOT_CACHED;
1703 ictx->ustorm_st_context.task_pdu_cache_index =
1704 BNX2X_ISCSI_PDU_HEADER_NOT_CACHED;
1705
1706 for (i = 1, j = 1; i < cp->num_cqs; i++, j++) {
1707 if (j == 3) {
1708 if (n >= n_max)
1709 break;
1710 req3 = (struct iscsi_kwqe_conn_offload3 *) wqes[n++];
1711 j = 0;
1712 }
1713 ictx->ustorm_st_context.ring.cq[i].cq_sn = ISCSI_INITIAL_SN;
1714 ictx->ustorm_st_context.ring.cq[i].curr_pbe.lo =
1715 req3->qp_first_pte[j].hi;
1716 ictx->ustorm_st_context.ring.cq[i].curr_pbe.hi =
1717 req3->qp_first_pte[j].lo;
1718 }
1719
1720 ictx->ustorm_st_context.task_pbl_base.lo =
1721 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1722 ictx->ustorm_st_context.task_pbl_base.hi =
1723 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1724 ictx->ustorm_st_context.tce_phy_addr.lo =
1725 iscsi->task_array_info.pgtbl[0];
1726 ictx->ustorm_st_context.tce_phy_addr.hi =
1727 iscsi->task_array_info.pgtbl[1];
1728 ictx->ustorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1729 ictx->ustorm_st_context.num_cqs = cp->num_cqs;
1730 ictx->ustorm_st_context.negotiated_rx |= ISCSI_DEF_MAX_RECV_SEG_LEN;
1731 ictx->ustorm_st_context.negotiated_rx_and_flags |=
1732 ISCSI_DEF_MAX_BURST_LEN;
1733 ictx->ustorm_st_context.negotiated_rx |=
1734 ISCSI_DEFAULT_MAX_OUTSTANDING_R2T <<
1735 USTORM_ISCSI_ST_CONTEXT_MAX_OUTSTANDING_R2TS_SHIFT;
1736
1737 ictx->cstorm_st_context.hq_pbl_base.lo =
1738 iscsi->hq_info.pgtbl_map & 0xffffffff;
1739 ictx->cstorm_st_context.hq_pbl_base.hi =
1740 (u64) iscsi->hq_info.pgtbl_map >> 32;
1741 ictx->cstorm_st_context.hq_curr_pbe.lo = iscsi->hq_info.pgtbl[0];
1742 ictx->cstorm_st_context.hq_curr_pbe.hi = iscsi->hq_info.pgtbl[1];
1743 ictx->cstorm_st_context.task_pbl_base.lo =
1744 iscsi->task_array_info.pgtbl_map & 0xffffffff;
1745 ictx->cstorm_st_context.task_pbl_base.hi =
1746 (u64) iscsi->task_array_info.pgtbl_map >> 32;
1747 /* CSTORM and USTORM initialization is different, CSTORM requires
1748 * CQ DB base & not PTE addr */
1749 ictx->cstorm_st_context.cq_db_base.lo =
1750 req1->cq_page_table_addr_lo & PAGE_MASK;
1751 ictx->cstorm_st_context.cq_db_base.hi = req1->cq_page_table_addr_hi;
1752 ictx->cstorm_st_context.iscsi_conn_id = req1->iscsi_conn_id;
1753 ictx->cstorm_st_context.cq_proc_en_bit_map = (1 << cp->num_cqs) - 1;
1754 for (i = 0; i < cp->num_cqs; i++) {
1755 ictx->cstorm_st_context.cq_c_prod_sqn_arr.sqn[i] =
1756 ISCSI_INITIAL_SN;
1757 ictx->cstorm_st_context.cq_c_sqn_2_notify_arr.sqn[i] =
1758 ISCSI_INITIAL_SN;
1759 }
1760
1761 ictx->xstorm_ag_context.cdu_reserved =
1762 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
1763 ISCSI_CONNECTION_TYPE);
1764 ictx->ustorm_ag_context.cdu_usage =
1765 CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
1766 ISCSI_CONNECTION_TYPE);
1767 return 0;
1768
1769}
1770
1771static int cnic_bnx2x_iscsi_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
1772 u32 num, int *work)
1773{
1774 struct iscsi_kwqe_conn_offload1 *req1;
1775 struct iscsi_kwqe_conn_offload2 *req2;
1776 struct cnic_local *cp = dev->cnic_priv;
Michael Chanfdf24082010-10-13 14:06:47 +00001777 struct cnic_context *ctx;
Michael Chan71034ba2009-10-10 13:46:59 +00001778 struct iscsi_kcqe kcqe;
1779 struct kcqe *cqes[1];
1780 u32 l5_cid;
Michael Chanfdf24082010-10-13 14:06:47 +00001781 int ret = 0;
Michael Chan71034ba2009-10-10 13:46:59 +00001782
1783 if (num < 2) {
1784 *work = num;
1785 return -EINVAL;
1786 }
1787
1788 req1 = (struct iscsi_kwqe_conn_offload1 *) wqes[0];
1789 req2 = (struct iscsi_kwqe_conn_offload2 *) wqes[1];
1790 if ((num - 2) < req2->num_additional_wqes) {
1791 *work = num;
1792 return -EINVAL;
1793 }
Joe Perches779bb412010-11-14 17:04:37 +00001794 *work = 2 + req2->num_additional_wqes;
Michael Chan71034ba2009-10-10 13:46:59 +00001795
1796 l5_cid = req1->iscsi_conn_id;
1797 if (l5_cid >= MAX_ISCSI_TBL_SZ)
1798 return -EINVAL;
1799
1800 memset(&kcqe, 0, sizeof(kcqe));
1801 kcqe.op_code = ISCSI_KCQE_OPCODE_OFFLOAD_CONN;
1802 kcqe.iscsi_conn_id = l5_cid;
1803 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
1804
Michael Chanfdf24082010-10-13 14:06:47 +00001805 ctx = &cp->ctx_tbl[l5_cid];
1806 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags)) {
1807 kcqe.completion_status =
1808 ISCSI_KCQE_COMPLETION_STATUS_CID_BUSY;
1809 goto done;
1810 }
1811
Michael Chan71034ba2009-10-10 13:46:59 +00001812 if (atomic_inc_return(&cp->iscsi_conn) > dev->max_iscsi_conn) {
1813 atomic_dec(&cp->iscsi_conn);
Michael Chan71034ba2009-10-10 13:46:59 +00001814 goto done;
1815 }
1816 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
1817 if (ret) {
1818 atomic_dec(&cp->iscsi_conn);
1819 ret = 0;
1820 goto done;
1821 }
1822 ret = cnic_setup_bnx2x_ctx(dev, wqes, num);
1823 if (ret < 0) {
1824 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1825 atomic_dec(&cp->iscsi_conn);
1826 goto done;
1827 }
1828
1829 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
Michael Chanceb7e1c2010-10-06 03:14:54 +00001830 kcqe.iscsi_conn_context_id = BNX2X_HW_CID(cp, cp->ctx_tbl[l5_cid].cid);
Michael Chan71034ba2009-10-10 13:46:59 +00001831
1832done:
1833 cqes[0] = (struct kcqe *) &kcqe;
1834 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1835 return ret;
1836}
1837
1838
1839static int cnic_bnx2x_iscsi_update(struct cnic_dev *dev, struct kwqe *kwqe)
1840{
1841 struct cnic_local *cp = dev->cnic_priv;
1842 struct iscsi_kwqe_conn_update *req =
1843 (struct iscsi_kwqe_conn_update *) kwqe;
1844 void *data;
1845 union l5cm_specific_data l5_data;
1846 u32 l5_cid, cid = BNX2X_SW_CID(req->context_id);
1847 int ret;
1848
1849 if (cnic_get_l5_cid(cp, cid, &l5_cid) != 0)
1850 return -EINVAL;
1851
1852 data = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
1853 if (!data)
1854 return -ENOMEM;
1855
1856 memcpy(data, kwqe, sizeof(struct kwqe));
1857
1858 ret = cnic_submit_kwqe_16(dev, ISCSI_RAMROD_CMD_ID_UPDATE_CONN,
1859 req->context_id, ISCSI_CONNECTION_TYPE, &l5_data);
1860 return ret;
1861}
1862
Michael Chana2c9e762010-10-13 14:06:46 +00001863static int cnic_bnx2x_destroy_ramrod(struct cnic_dev *dev, u32 l5_cid)
Michael Chan71034ba2009-10-10 13:46:59 +00001864{
1865 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00001866 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
Michael Chana2c9e762010-10-13 14:06:46 +00001867 union l5cm_specific_data l5_data;
1868 int ret;
Michael Chan68d7c1a2011-01-05 15:14:13 +00001869 u32 hw_cid;
Michael Chan71034ba2009-10-10 13:46:59 +00001870
Michael Chan71034ba2009-10-10 13:46:59 +00001871 init_waitqueue_head(&ctx->waitq);
1872 ctx->wait_cond = 0;
1873 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001874 hw_cid = BNX2X_HW_CID(cp, ctx->cid);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001875
1876 ret = cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
Michael Chan68d7c1a2011-01-05 15:14:13 +00001877 hw_cid, NONE_CONNECTION_TYPE, &l5_data);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00001878
Michael Chan71034ba2009-10-10 13:46:59 +00001879 if (ret == 0)
1880 wait_event(ctx->waitq, ctx->wait_cond);
1881
Michael Chana2c9e762010-10-13 14:06:46 +00001882 return ret;
1883}
1884
1885static int cnic_bnx2x_iscsi_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
1886{
1887 struct cnic_local *cp = dev->cnic_priv;
1888 struct iscsi_kwqe_conn_destroy *req =
1889 (struct iscsi_kwqe_conn_destroy *) kwqe;
1890 u32 l5_cid = req->reserved0;
1891 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
1892 int ret = 0;
1893 struct iscsi_kcqe kcqe;
1894 struct kcqe *cqes[1];
1895
1896 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
1897 goto skip_cfc_delete;
1898
Michael Chanfdf24082010-10-13 14:06:47 +00001899 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
1900 unsigned long delta = ctx->timestamp + (2 * HZ) - jiffies;
1901
1902 if (delta > (2 * HZ))
1903 delta = 0;
1904
1905 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
1906 queue_delayed_work(cnic_wq, &cp->delete_task, delta);
1907 goto destroy_reply;
1908 }
Michael Chana2c9e762010-10-13 14:06:46 +00001909
1910 ret = cnic_bnx2x_destroy_ramrod(dev, l5_cid);
1911
Michael Chan71034ba2009-10-10 13:46:59 +00001912skip_cfc_delete:
1913 cnic_free_bnx2x_conn_resc(dev, l5_cid);
1914
1915 atomic_dec(&cp->iscsi_conn);
Michael Chanfdf24082010-10-13 14:06:47 +00001916 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00001917
Michael Chanfdf24082010-10-13 14:06:47 +00001918destroy_reply:
Michael Chan71034ba2009-10-10 13:46:59 +00001919 memset(&kcqe, 0, sizeof(kcqe));
1920 kcqe.op_code = ISCSI_KCQE_OPCODE_DESTROY_CONN;
1921 kcqe.iscsi_conn_id = l5_cid;
1922 kcqe.completion_status = ISCSI_KCQE_COMPLETION_STATUS_SUCCESS;
1923 kcqe.iscsi_conn_context_id = req->context_id;
1924
1925 cqes[0] = (struct kcqe *) &kcqe;
1926 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_ISCSI, cqes, 1);
1927
1928 return ret;
1929}
1930
1931static void cnic_init_storm_conn_bufs(struct cnic_dev *dev,
1932 struct l4_kwq_connect_req1 *kwqe1,
1933 struct l4_kwq_connect_req3 *kwqe3,
1934 struct l5cm_active_conn_buffer *conn_buf)
1935{
1936 struct l5cm_conn_addr_params *conn_addr = &conn_buf->conn_addr_buf;
1937 struct l5cm_xstorm_conn_buffer *xstorm_buf =
1938 &conn_buf->xstorm_conn_buffer;
1939 struct l5cm_tstorm_conn_buffer *tstorm_buf =
1940 &conn_buf->tstorm_conn_buffer;
1941 struct regpair context_addr;
1942 u32 cid = BNX2X_SW_CID(kwqe1->cid);
1943 struct in6_addr src_ip, dst_ip;
1944 int i;
1945 u32 *addrp;
1946
1947 addrp = (u32 *) &conn_addr->local_ip_addr;
1948 for (i = 0; i < 4; i++, addrp++)
1949 src_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1950
1951 addrp = (u32 *) &conn_addr->remote_ip_addr;
1952 for (i = 0; i < 4; i++, addrp++)
1953 dst_ip.in6_u.u6_addr32[i] = cpu_to_be32(*addrp);
1954
1955 cnic_get_bnx2x_ctx(dev, cid, 0, &context_addr);
1956
1957 xstorm_buf->context_addr.hi = context_addr.hi;
1958 xstorm_buf->context_addr.lo = context_addr.lo;
1959 xstorm_buf->mss = 0xffff;
1960 xstorm_buf->rcv_buf = kwqe3->rcv_buf;
1961 if (kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE)
1962 xstorm_buf->params |= L5CM_XSTORM_CONN_BUFFER_NAGLE_ENABLE;
1963 xstorm_buf->pseudo_header_checksum =
1964 swab16(~csum_ipv6_magic(&src_ip, &dst_ip, 0, IPPROTO_TCP, 0));
1965
1966 if (!(kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK))
1967 tstorm_buf->params |=
1968 L5CM_TSTORM_CONN_BUFFER_DELAYED_ACK_ENABLE;
1969 if (kwqe3->ka_timeout) {
1970 tstorm_buf->ka_enable = 1;
1971 tstorm_buf->ka_timeout = kwqe3->ka_timeout;
1972 tstorm_buf->ka_interval = kwqe3->ka_interval;
1973 tstorm_buf->ka_max_probe_count = kwqe3->ka_max_probe_count;
1974 }
1975 tstorm_buf->rcv_buf = kwqe3->rcv_buf;
1976 tstorm_buf->snd_buf = kwqe3->snd_buf;
1977 tstorm_buf->max_rt_time = 0xffffffff;
1978}
1979
1980static void cnic_init_bnx2x_mac(struct cnic_dev *dev)
1981{
1982 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00001983 u32 pfid = cp->pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00001984 u8 *mac = dev->mac_addr;
1985
1986 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001987 XSTORM_ISCSI_LOCAL_MAC_ADDR0_OFFSET(pfid), mac[0]);
Michael Chan71034ba2009-10-10 13:46:59 +00001988 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001989 XSTORM_ISCSI_LOCAL_MAC_ADDR1_OFFSET(pfid), mac[1]);
Michael Chan71034ba2009-10-10 13:46:59 +00001990 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001991 XSTORM_ISCSI_LOCAL_MAC_ADDR2_OFFSET(pfid), mac[2]);
Michael Chan71034ba2009-10-10 13:46:59 +00001992 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001993 XSTORM_ISCSI_LOCAL_MAC_ADDR3_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00001994 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001995 XSTORM_ISCSI_LOCAL_MAC_ADDR4_OFFSET(pfid), mac[4]);
Michael Chan71034ba2009-10-10 13:46:59 +00001996 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00001997 XSTORM_ISCSI_LOCAL_MAC_ADDR5_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00001998
1999 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002000 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[5]);
Michael Chan71034ba2009-10-10 13:46:59 +00002001 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002002 TSTORM_ISCSI_TCP_VARS_LSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00002003 mac[4]);
2004 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002005 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid), mac[3]);
Michael Chan71034ba2009-10-10 13:46:59 +00002006 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002007 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 1,
Michael Chan71034ba2009-10-10 13:46:59 +00002008 mac[2]);
2009 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002010 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 2,
Michael Chan71034ba2009-10-10 13:46:59 +00002011 mac[1]);
2012 CNIC_WR8(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002013 TSTORM_ISCSI_TCP_VARS_MSB_LOCAL_MAC_ADDR_OFFSET(pfid) + 3,
Michael Chan71034ba2009-10-10 13:46:59 +00002014 mac[0]);
2015}
2016
2017static void cnic_bnx2x_set_tcp_timestamp(struct cnic_dev *dev, int tcp_ts)
2018{
2019 struct cnic_local *cp = dev->cnic_priv;
2020 u8 xstorm_flags = XSTORM_L5CM_TCP_FLAGS_WND_SCL_EN;
2021 u16 tstorm_flags = 0;
2022
2023 if (tcp_ts) {
2024 xstorm_flags |= XSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
2025 tstorm_flags |= TSTORM_L5CM_TCP_FLAGS_TS_ENABLED;
2026 }
2027
2028 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002029 XSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), xstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002030
2031 CNIC_WR16(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002032 TSTORM_ISCSI_TCP_VARS_FLAGS_OFFSET(cp->pfid), tstorm_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002033}
2034
2035static int cnic_bnx2x_connect(struct cnic_dev *dev, struct kwqe *wqes[],
2036 u32 num, int *work)
2037{
2038 struct cnic_local *cp = dev->cnic_priv;
2039 struct l4_kwq_connect_req1 *kwqe1 =
2040 (struct l4_kwq_connect_req1 *) wqes[0];
2041 struct l4_kwq_connect_req3 *kwqe3;
2042 struct l5cm_active_conn_buffer *conn_buf;
2043 struct l5cm_conn_addr_params *conn_addr;
2044 union l5cm_specific_data l5_data;
2045 u32 l5_cid = kwqe1->pg_cid;
2046 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
2047 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
2048 int ret;
2049
2050 if (num < 2) {
2051 *work = num;
2052 return -EINVAL;
2053 }
2054
2055 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6)
2056 *work = 3;
2057 else
2058 *work = 2;
2059
2060 if (num < *work) {
2061 *work = num;
2062 return -EINVAL;
2063 }
2064
2065 if (sizeof(*conn_buf) > CNIC_KWQ16_DATA_SIZE) {
Joe Perchesddf79b22010-02-17 15:01:54 +00002066 netdev_err(dev->netdev, "conn_buf size too big\n");
Michael Chan71034ba2009-10-10 13:46:59 +00002067 return -ENOMEM;
2068 }
2069 conn_buf = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2070 if (!conn_buf)
2071 return -ENOMEM;
2072
2073 memset(conn_buf, 0, sizeof(*conn_buf));
2074
2075 conn_addr = &conn_buf->conn_addr_buf;
2076 conn_addr->remote_addr_0 = csk->ha[0];
2077 conn_addr->remote_addr_1 = csk->ha[1];
2078 conn_addr->remote_addr_2 = csk->ha[2];
2079 conn_addr->remote_addr_3 = csk->ha[3];
2080 conn_addr->remote_addr_4 = csk->ha[4];
2081 conn_addr->remote_addr_5 = csk->ha[5];
2082
2083 if (kwqe1->conn_flags & L4_KWQ_CONNECT_REQ1_IP_V6) {
2084 struct l4_kwq_connect_req2 *kwqe2 =
2085 (struct l4_kwq_connect_req2 *) wqes[1];
2086
2087 conn_addr->local_ip_addr.ip_addr_hi_hi = kwqe2->src_ip_v6_4;
2088 conn_addr->local_ip_addr.ip_addr_hi_lo = kwqe2->src_ip_v6_3;
2089 conn_addr->local_ip_addr.ip_addr_lo_hi = kwqe2->src_ip_v6_2;
2090
2091 conn_addr->remote_ip_addr.ip_addr_hi_hi = kwqe2->dst_ip_v6_4;
2092 conn_addr->remote_ip_addr.ip_addr_hi_lo = kwqe2->dst_ip_v6_3;
2093 conn_addr->remote_ip_addr.ip_addr_lo_hi = kwqe2->dst_ip_v6_2;
2094 conn_addr->params |= L5CM_CONN_ADDR_PARAMS_IP_VERSION;
2095 }
2096 kwqe3 = (struct l4_kwq_connect_req3 *) wqes[*work - 1];
2097
2098 conn_addr->local_ip_addr.ip_addr_lo_lo = kwqe1->src_ip;
2099 conn_addr->remote_ip_addr.ip_addr_lo_lo = kwqe1->dst_ip;
2100 conn_addr->local_tcp_port = kwqe1->src_port;
2101 conn_addr->remote_tcp_port = kwqe1->dst_port;
2102
2103 conn_addr->pmtu = kwqe3->pmtu;
2104 cnic_init_storm_conn_bufs(dev, kwqe1, kwqe3, conn_buf);
2105
2106 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00002107 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(cp->pfid), csk->vlan_id);
Michael Chan71034ba2009-10-10 13:46:59 +00002108
2109 cnic_bnx2x_set_tcp_timestamp(dev,
2110 kwqe1->tcp_flags & L4_KWQ_CONNECT_REQ1_TIME_STAMP);
2111
2112 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_TCP_CONNECT,
2113 kwqe1->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2114 if (!ret)
Michael Chan6e0dda02010-10-13 14:06:45 +00002115 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00002116
2117 return ret;
2118}
2119
2120static int cnic_bnx2x_close(struct cnic_dev *dev, struct kwqe *kwqe)
2121{
2122 struct l4_kwq_close_req *req = (struct l4_kwq_close_req *) kwqe;
2123 union l5cm_specific_data l5_data;
2124 int ret;
2125
2126 memset(&l5_data, 0, sizeof(l5_data));
2127 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_CLOSE,
2128 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2129 return ret;
2130}
2131
2132static int cnic_bnx2x_reset(struct cnic_dev *dev, struct kwqe *kwqe)
2133{
2134 struct l4_kwq_reset_req *req = (struct l4_kwq_reset_req *) kwqe;
2135 union l5cm_specific_data l5_data;
2136 int ret;
2137
2138 memset(&l5_data, 0, sizeof(l5_data));
2139 ret = cnic_submit_kwqe_16(dev, L5CM_RAMROD_CMD_ID_ABORT,
2140 req->cid, ISCSI_CONNECTION_TYPE, &l5_data);
2141 return ret;
2142}
2143static int cnic_bnx2x_offload_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2144{
2145 struct l4_kwq_offload_pg *req = (struct l4_kwq_offload_pg *) kwqe;
2146 struct l4_kcq kcqe;
2147 struct kcqe *cqes[1];
2148
2149 memset(&kcqe, 0, sizeof(kcqe));
2150 kcqe.pg_host_opaque = req->host_opaque;
2151 kcqe.pg_cid = req->host_opaque;
2152 kcqe.op_code = L4_KCQE_OPCODE_VALUE_OFFLOAD_PG;
2153 cqes[0] = (struct kcqe *) &kcqe;
2154 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2155 return 0;
2156}
2157
2158static int cnic_bnx2x_update_pg(struct cnic_dev *dev, struct kwqe *kwqe)
2159{
2160 struct l4_kwq_update_pg *req = (struct l4_kwq_update_pg *) kwqe;
2161 struct l4_kcq kcqe;
2162 struct kcqe *cqes[1];
2163
2164 memset(&kcqe, 0, sizeof(kcqe));
2165 kcqe.pg_host_opaque = req->pg_host_opaque;
2166 kcqe.pg_cid = req->pg_cid;
2167 kcqe.op_code = L4_KCQE_OPCODE_VALUE_UPDATE_PG;
2168 cqes[0] = (struct kcqe *) &kcqe;
2169 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_L4, cqes, 1);
2170 return 0;
2171}
2172
Michael Chane1928c82010-12-23 07:43:04 +00002173static int cnic_bnx2x_fcoe_stat(struct cnic_dev *dev, struct kwqe *kwqe)
2174{
2175 struct fcoe_kwqe_stat *req;
2176 struct fcoe_stat_ramrod_params *fcoe_stat;
2177 union l5cm_specific_data l5_data;
2178 struct cnic_local *cp = dev->cnic_priv;
2179 int ret;
2180 u32 cid;
2181
2182 req = (struct fcoe_kwqe_stat *) kwqe;
2183 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2184
2185 fcoe_stat = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2186 if (!fcoe_stat)
2187 return -ENOMEM;
2188
2189 memset(fcoe_stat, 0, sizeof(*fcoe_stat));
2190 memcpy(&fcoe_stat->stat_kwqe, req, sizeof(*req));
2191
2192 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_STAT, cid,
2193 FCOE_CONNECTION_TYPE, &l5_data);
2194 return ret;
2195}
2196
2197static int cnic_bnx2x_fcoe_init1(struct cnic_dev *dev, struct kwqe *wqes[],
2198 u32 num, int *work)
2199{
2200 int ret;
2201 struct cnic_local *cp = dev->cnic_priv;
2202 u32 cid;
2203 struct fcoe_init_ramrod_params *fcoe_init;
2204 struct fcoe_kwqe_init1 *req1;
2205 struct fcoe_kwqe_init2 *req2;
2206 struct fcoe_kwqe_init3 *req3;
2207 union l5cm_specific_data l5_data;
2208
2209 if (num < 3) {
2210 *work = num;
2211 return -EINVAL;
2212 }
2213 req1 = (struct fcoe_kwqe_init1 *) wqes[0];
2214 req2 = (struct fcoe_kwqe_init2 *) wqes[1];
2215 req3 = (struct fcoe_kwqe_init3 *) wqes[2];
2216 if (req2->hdr.op_code != FCOE_KWQE_OPCODE_INIT2) {
2217 *work = 1;
2218 return -EINVAL;
2219 }
2220 if (req3->hdr.op_code != FCOE_KWQE_OPCODE_INIT3) {
2221 *work = 2;
2222 return -EINVAL;
2223 }
2224
2225 if (sizeof(*fcoe_init) > CNIC_KWQ16_DATA_SIZE) {
2226 netdev_err(dev->netdev, "fcoe_init size too big\n");
2227 return -ENOMEM;
2228 }
2229 fcoe_init = cnic_get_kwqe_16_data(cp, BNX2X_FCOE_L5_CID_BASE, &l5_data);
2230 if (!fcoe_init)
2231 return -ENOMEM;
2232
2233 memset(fcoe_init, 0, sizeof(*fcoe_init));
2234 memcpy(&fcoe_init->init_kwqe1, req1, sizeof(*req1));
2235 memcpy(&fcoe_init->init_kwqe2, req2, sizeof(*req2));
2236 memcpy(&fcoe_init->init_kwqe3, req3, sizeof(*req3));
2237 fcoe_init->eq_addr.lo = cp->kcq2.dma.pg_map_arr[0] & 0xffffffff;
2238 fcoe_init->eq_addr.hi = (u64) cp->kcq2.dma.pg_map_arr[0] >> 32;
2239 fcoe_init->eq_next_page_addr.lo =
2240 cp->kcq2.dma.pg_map_arr[1] & 0xffffffff;
2241 fcoe_init->eq_next_page_addr.hi =
2242 (u64) cp->kcq2.dma.pg_map_arr[1] >> 32;
2243
2244 fcoe_init->sb_num = cp->status_blk_num;
2245 fcoe_init->eq_prod = MAX_KCQ_IDX;
2246 fcoe_init->sb_id = HC_INDEX_FCOE_EQ_CONS;
2247 cp->kcq2.sw_prod_idx = 0;
2248
2249 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
Michael Chane1928c82010-12-23 07:43:04 +00002250 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_INIT, cid,
2251 FCOE_CONNECTION_TYPE, &l5_data);
2252 *work = 3;
2253 return ret;
2254}
2255
2256static int cnic_bnx2x_fcoe_ofld1(struct cnic_dev *dev, struct kwqe *wqes[],
2257 u32 num, int *work)
2258{
2259 int ret = 0;
2260 u32 cid = -1, l5_cid;
2261 struct cnic_local *cp = dev->cnic_priv;
2262 struct fcoe_kwqe_conn_offload1 *req1;
2263 struct fcoe_kwqe_conn_offload2 *req2;
2264 struct fcoe_kwqe_conn_offload3 *req3;
2265 struct fcoe_kwqe_conn_offload4 *req4;
2266 struct fcoe_conn_offload_ramrod_params *fcoe_offload;
2267 struct cnic_context *ctx;
2268 struct fcoe_context *fctx;
2269 struct regpair ctx_addr;
2270 union l5cm_specific_data l5_data;
2271 struct fcoe_kcqe kcqe;
2272 struct kcqe *cqes[1];
2273
2274 if (num < 4) {
2275 *work = num;
2276 return -EINVAL;
2277 }
2278 req1 = (struct fcoe_kwqe_conn_offload1 *) wqes[0];
2279 req2 = (struct fcoe_kwqe_conn_offload2 *) wqes[1];
2280 req3 = (struct fcoe_kwqe_conn_offload3 *) wqes[2];
2281 req4 = (struct fcoe_kwqe_conn_offload4 *) wqes[3];
2282
2283 *work = 4;
2284
2285 l5_cid = req1->fcoe_conn_id;
2286 if (l5_cid >= BNX2X_FCOE_NUM_CONNECTIONS)
2287 goto err_reply;
2288
2289 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2290
2291 ctx = &cp->ctx_tbl[l5_cid];
2292 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
2293 goto err_reply;
2294
2295 ret = cnic_alloc_bnx2x_conn_resc(dev, l5_cid);
2296 if (ret) {
2297 ret = 0;
2298 goto err_reply;
2299 }
2300 cid = ctx->cid;
2301
2302 fctx = cnic_get_bnx2x_ctx(dev, cid, 1, &ctx_addr);
2303 if (fctx) {
2304 u32 hw_cid = BNX2X_HW_CID(cp, cid);
2305 u32 val;
2306
2307 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_XCM_AG,
2308 FCOE_CONNECTION_TYPE);
2309 fctx->xstorm_ag_context.cdu_reserved = val;
2310 val = CDU_RSRVD_VALUE_TYPE_A(hw_cid, CDU_REGION_NUMBER_UCM_AG,
2311 FCOE_CONNECTION_TYPE);
2312 fctx->ustorm_ag_context.cdu_usage = val;
2313 }
2314 if (sizeof(*fcoe_offload) > CNIC_KWQ16_DATA_SIZE) {
2315 netdev_err(dev->netdev, "fcoe_offload size too big\n");
2316 goto err_reply;
2317 }
2318 fcoe_offload = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2319 if (!fcoe_offload)
2320 goto err_reply;
2321
2322 memset(fcoe_offload, 0, sizeof(*fcoe_offload));
2323 memcpy(&fcoe_offload->offload_kwqe1, req1, sizeof(*req1));
2324 memcpy(&fcoe_offload->offload_kwqe2, req2, sizeof(*req2));
2325 memcpy(&fcoe_offload->offload_kwqe3, req3, sizeof(*req3));
2326 memcpy(&fcoe_offload->offload_kwqe4, req4, sizeof(*req4));
2327
2328 cid = BNX2X_HW_CID(cp, cid);
2329 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_OFFLOAD_CONN, cid,
2330 FCOE_CONNECTION_TYPE, &l5_data);
2331 if (!ret)
2332 set_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
2333
2334 return ret;
2335
2336err_reply:
2337 if (cid != -1)
2338 cnic_free_bnx2x_conn_resc(dev, l5_cid);
2339
2340 memset(&kcqe, 0, sizeof(kcqe));
2341 kcqe.op_code = FCOE_KCQE_OPCODE_OFFLOAD_CONN;
2342 kcqe.fcoe_conn_id = req1->fcoe_conn_id;
2343 kcqe.completion_status = FCOE_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAILURE;
2344
2345 cqes[0] = (struct kcqe *) &kcqe;
2346 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2347 return ret;
2348}
2349
2350static int cnic_bnx2x_fcoe_enable(struct cnic_dev *dev, struct kwqe *kwqe)
2351{
2352 struct fcoe_kwqe_conn_enable_disable *req;
2353 struct fcoe_conn_enable_disable_ramrod_params *fcoe_enable;
2354 union l5cm_specific_data l5_data;
2355 int ret;
2356 u32 cid, l5_cid;
2357 struct cnic_local *cp = dev->cnic_priv;
2358
2359 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2360 cid = req->context_id;
2361 l5_cid = req->conn_id + BNX2X_FCOE_L5_CID_BASE;
2362
2363 if (sizeof(*fcoe_enable) > CNIC_KWQ16_DATA_SIZE) {
2364 netdev_err(dev->netdev, "fcoe_enable size too big\n");
2365 return -ENOMEM;
2366 }
2367 fcoe_enable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2368 if (!fcoe_enable)
2369 return -ENOMEM;
2370
2371 memset(fcoe_enable, 0, sizeof(*fcoe_enable));
2372 memcpy(&fcoe_enable->enable_disable_kwqe, req, sizeof(*req));
2373 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_ENABLE_CONN, cid,
2374 FCOE_CONNECTION_TYPE, &l5_data);
2375 return ret;
2376}
2377
2378static int cnic_bnx2x_fcoe_disable(struct cnic_dev *dev, struct kwqe *kwqe)
2379{
2380 struct fcoe_kwqe_conn_enable_disable *req;
2381 struct fcoe_conn_enable_disable_ramrod_params *fcoe_disable;
2382 union l5cm_specific_data l5_data;
2383 int ret;
2384 u32 cid, l5_cid;
2385 struct cnic_local *cp = dev->cnic_priv;
2386
2387 req = (struct fcoe_kwqe_conn_enable_disable *) kwqe;
2388 cid = req->context_id;
2389 l5_cid = req->conn_id;
2390 if (l5_cid >= BNX2X_FCOE_NUM_CONNECTIONS)
2391 return -EINVAL;
2392
2393 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2394
2395 if (sizeof(*fcoe_disable) > CNIC_KWQ16_DATA_SIZE) {
2396 netdev_err(dev->netdev, "fcoe_disable size too big\n");
2397 return -ENOMEM;
2398 }
2399 fcoe_disable = cnic_get_kwqe_16_data(cp, l5_cid, &l5_data);
2400 if (!fcoe_disable)
2401 return -ENOMEM;
2402
2403 memset(fcoe_disable, 0, sizeof(*fcoe_disable));
2404 memcpy(&fcoe_disable->enable_disable_kwqe, req, sizeof(*req));
2405 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DISABLE_CONN, cid,
2406 FCOE_CONNECTION_TYPE, &l5_data);
2407 return ret;
2408}
2409
2410static int cnic_bnx2x_fcoe_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2411{
2412 struct fcoe_kwqe_conn_destroy *req;
2413 union l5cm_specific_data l5_data;
2414 int ret;
2415 u32 cid, l5_cid;
2416 struct cnic_local *cp = dev->cnic_priv;
2417 struct cnic_context *ctx;
2418 struct fcoe_kcqe kcqe;
2419 struct kcqe *cqes[1];
2420
2421 req = (struct fcoe_kwqe_conn_destroy *) kwqe;
2422 cid = req->context_id;
2423 l5_cid = req->conn_id;
2424 if (l5_cid >= BNX2X_FCOE_NUM_CONNECTIONS)
2425 return -EINVAL;
2426
2427 l5_cid += BNX2X_FCOE_L5_CID_BASE;
2428
2429 ctx = &cp->ctx_tbl[l5_cid];
2430
2431 init_waitqueue_head(&ctx->waitq);
2432 ctx->wait_cond = 0;
2433
2434 memset(&l5_data, 0, sizeof(l5_data));
2435 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_TERMINATE_CONN, cid,
2436 FCOE_CONNECTION_TYPE, &l5_data);
2437 if (ret == 0) {
2438 wait_event(ctx->waitq, ctx->wait_cond);
2439 set_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags);
2440 queue_delayed_work(cnic_wq, &cp->delete_task,
2441 msecs_to_jiffies(2000));
2442 }
2443
2444 memset(&kcqe, 0, sizeof(kcqe));
2445 kcqe.op_code = FCOE_KCQE_OPCODE_DESTROY_CONN;
2446 kcqe.fcoe_conn_id = req->conn_id;
2447 kcqe.fcoe_conn_context_id = cid;
2448
2449 cqes[0] = (struct kcqe *) &kcqe;
2450 cnic_reply_bnx2x_kcqes(dev, CNIC_ULP_FCOE, cqes, 1);
2451 return ret;
2452}
2453
2454static int cnic_bnx2x_fcoe_fw_destroy(struct cnic_dev *dev, struct kwqe *kwqe)
2455{
2456 struct fcoe_kwqe_destroy *req;
2457 union l5cm_specific_data l5_data;
2458 struct cnic_local *cp = dev->cnic_priv;
2459 int ret;
2460 u32 cid;
2461
2462 req = (struct fcoe_kwqe_destroy *) kwqe;
2463 cid = BNX2X_HW_CID(cp, cp->fcoe_init_cid);
2464
2465 memset(&l5_data, 0, sizeof(l5_data));
2466 ret = cnic_submit_kwqe_16(dev, FCOE_RAMROD_CMD_ID_DESTROY, cid,
2467 FCOE_CONNECTION_TYPE, &l5_data);
2468 return ret;
2469}
2470
2471static int cnic_submit_bnx2x_iscsi_kwqes(struct cnic_dev *dev,
2472 struct kwqe *wqes[], u32 num_wqes)
Michael Chan71034ba2009-10-10 13:46:59 +00002473{
2474 int i, work, ret;
2475 u32 opcode;
2476 struct kwqe *kwqe;
2477
2478 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2479 return -EAGAIN; /* bnx2 is down */
2480
2481 for (i = 0; i < num_wqes; ) {
2482 kwqe = wqes[i];
2483 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2484 work = 1;
2485
2486 switch (opcode) {
2487 case ISCSI_KWQE_OPCODE_INIT1:
2488 ret = cnic_bnx2x_iscsi_init1(dev, kwqe);
2489 break;
2490 case ISCSI_KWQE_OPCODE_INIT2:
2491 ret = cnic_bnx2x_iscsi_init2(dev, kwqe);
2492 break;
2493 case ISCSI_KWQE_OPCODE_OFFLOAD_CONN1:
2494 ret = cnic_bnx2x_iscsi_ofld1(dev, &wqes[i],
2495 num_wqes - i, &work);
2496 break;
2497 case ISCSI_KWQE_OPCODE_UPDATE_CONN:
2498 ret = cnic_bnx2x_iscsi_update(dev, kwqe);
2499 break;
2500 case ISCSI_KWQE_OPCODE_DESTROY_CONN:
2501 ret = cnic_bnx2x_iscsi_destroy(dev, kwqe);
2502 break;
2503 case L4_KWQE_OPCODE_VALUE_CONNECT1:
2504 ret = cnic_bnx2x_connect(dev, &wqes[i], num_wqes - i,
2505 &work);
2506 break;
2507 case L4_KWQE_OPCODE_VALUE_CLOSE:
2508 ret = cnic_bnx2x_close(dev, kwqe);
2509 break;
2510 case L4_KWQE_OPCODE_VALUE_RESET:
2511 ret = cnic_bnx2x_reset(dev, kwqe);
2512 break;
2513 case L4_KWQE_OPCODE_VALUE_OFFLOAD_PG:
2514 ret = cnic_bnx2x_offload_pg(dev, kwqe);
2515 break;
2516 case L4_KWQE_OPCODE_VALUE_UPDATE_PG:
2517 ret = cnic_bnx2x_update_pg(dev, kwqe);
2518 break;
2519 case L4_KWQE_OPCODE_VALUE_UPLOAD_PG:
2520 ret = 0;
2521 break;
2522 default:
2523 ret = 0;
Joe Perchesddf79b22010-02-17 15:01:54 +00002524 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2525 opcode);
Michael Chan71034ba2009-10-10 13:46:59 +00002526 break;
2527 }
2528 if (ret < 0)
Joe Perchesddf79b22010-02-17 15:01:54 +00002529 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2530 opcode);
Michael Chan71034ba2009-10-10 13:46:59 +00002531 i += work;
2532 }
2533 return 0;
2534}
2535
Michael Chane1928c82010-12-23 07:43:04 +00002536static int cnic_submit_bnx2x_fcoe_kwqes(struct cnic_dev *dev,
2537 struct kwqe *wqes[], u32 num_wqes)
2538{
2539 struct cnic_local *cp = dev->cnic_priv;
2540 int i, work, ret;
2541 u32 opcode;
2542 struct kwqe *kwqe;
2543
2544 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2545 return -EAGAIN; /* bnx2 is down */
2546
2547 if (BNX2X_CHIP_NUM(cp->chip_id) == BNX2X_CHIP_NUM_57710)
2548 return -EINVAL;
2549
2550 for (i = 0; i < num_wqes; ) {
2551 kwqe = wqes[i];
2552 opcode = KWQE_OPCODE(kwqe->kwqe_op_flag);
2553 work = 1;
2554
2555 switch (opcode) {
2556 case FCOE_KWQE_OPCODE_INIT1:
2557 ret = cnic_bnx2x_fcoe_init1(dev, &wqes[i],
2558 num_wqes - i, &work);
2559 break;
2560 case FCOE_KWQE_OPCODE_OFFLOAD_CONN1:
2561 ret = cnic_bnx2x_fcoe_ofld1(dev, &wqes[i],
2562 num_wqes - i, &work);
2563 break;
2564 case FCOE_KWQE_OPCODE_ENABLE_CONN:
2565 ret = cnic_bnx2x_fcoe_enable(dev, kwqe);
2566 break;
2567 case FCOE_KWQE_OPCODE_DISABLE_CONN:
2568 ret = cnic_bnx2x_fcoe_disable(dev, kwqe);
2569 break;
2570 case FCOE_KWQE_OPCODE_DESTROY_CONN:
2571 ret = cnic_bnx2x_fcoe_destroy(dev, kwqe);
2572 break;
2573 case FCOE_KWQE_OPCODE_DESTROY:
2574 ret = cnic_bnx2x_fcoe_fw_destroy(dev, kwqe);
2575 break;
2576 case FCOE_KWQE_OPCODE_STAT:
2577 ret = cnic_bnx2x_fcoe_stat(dev, kwqe);
2578 break;
2579 default:
2580 ret = 0;
2581 netdev_err(dev->netdev, "Unknown type of KWQE(0x%x)\n",
2582 opcode);
2583 break;
2584 }
2585 if (ret < 0)
2586 netdev_err(dev->netdev, "KWQE(0x%x) failed\n",
2587 opcode);
2588 i += work;
2589 }
2590 return 0;
2591}
2592
2593static int cnic_submit_bnx2x_kwqes(struct cnic_dev *dev, struct kwqe *wqes[],
2594 u32 num_wqes)
2595{
2596 int ret = -EINVAL;
2597 u32 layer_code;
2598
2599 if (!test_bit(CNIC_F_CNIC_UP, &dev->flags))
2600 return -EAGAIN; /* bnx2x is down */
2601
2602 if (!num_wqes)
2603 return 0;
2604
2605 layer_code = wqes[0]->kwqe_op_flag & KWQE_LAYER_MASK;
2606 switch (layer_code) {
2607 case KWQE_FLAGS_LAYER_MASK_L5_ISCSI:
2608 case KWQE_FLAGS_LAYER_MASK_L4:
2609 case KWQE_FLAGS_LAYER_MASK_L2:
2610 ret = cnic_submit_bnx2x_iscsi_kwqes(dev, wqes, num_wqes);
2611 break;
2612
2613 case KWQE_FLAGS_LAYER_MASK_L5_FCOE:
2614 ret = cnic_submit_bnx2x_fcoe_kwqes(dev, wqes, num_wqes);
2615 break;
2616 }
2617 return ret;
2618}
2619
2620static inline u32 cnic_get_kcqe_layer_mask(u32 opflag)
2621{
2622 if (unlikely(KCQE_OPCODE(opflag) == FCOE_RAMROD_CMD_ID_TERMINATE_CONN))
2623 return KCQE_FLAGS_LAYER_MASK_L4;
2624
2625 return opflag & KCQE_FLAGS_LAYER_MASK;
2626}
2627
Michael Chana4636962009-06-08 18:14:43 -07002628static void service_kcqes(struct cnic_dev *dev, int num_cqes)
2629{
2630 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002631 int i, j, comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002632
2633 i = 0;
2634 j = 1;
2635 while (num_cqes) {
2636 struct cnic_ulp_ops *ulp_ops;
2637 int ulp_type;
2638 u32 kcqe_op_flag = cp->completed_kcq[i]->kcqe_op_flag;
Michael Chane1928c82010-12-23 07:43:04 +00002639 u32 kcqe_layer = cnic_get_kcqe_layer_mask(kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002640
2641 if (unlikely(kcqe_op_flag & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002642 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002643
2644 while (j < num_cqes) {
2645 u32 next_op = cp->completed_kcq[i + j]->kcqe_op_flag;
2646
Michael Chane1928c82010-12-23 07:43:04 +00002647 if (cnic_get_kcqe_layer_mask(next_op) != kcqe_layer)
Michael Chana4636962009-06-08 18:14:43 -07002648 break;
2649
2650 if (unlikely(next_op & KCQE_RAMROD_COMPLETION))
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002651 comp++;
Michael Chana4636962009-06-08 18:14:43 -07002652 j++;
2653 }
2654
2655 if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_RDMA)
2656 ulp_type = CNIC_ULP_RDMA;
2657 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_ISCSI)
2658 ulp_type = CNIC_ULP_ISCSI;
Michael Chane1928c82010-12-23 07:43:04 +00002659 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L5_FCOE)
2660 ulp_type = CNIC_ULP_FCOE;
Michael Chana4636962009-06-08 18:14:43 -07002661 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L4)
2662 ulp_type = CNIC_ULP_L4;
2663 else if (kcqe_layer == KCQE_FLAGS_LAYER_MASK_L2)
2664 goto end;
2665 else {
Joe Perchesddf79b22010-02-17 15:01:54 +00002666 netdev_err(dev->netdev, "Unknown type of KCQE(0x%x)\n",
2667 kcqe_op_flag);
Michael Chana4636962009-06-08 18:14:43 -07002668 goto end;
2669 }
2670
2671 rcu_read_lock();
2672 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
2673 if (likely(ulp_ops)) {
2674 ulp_ops->indicate_kcqes(cp->ulp_handle[ulp_type],
2675 cp->completed_kcq + i, j);
2676 }
2677 rcu_read_unlock();
2678end:
2679 num_cqes -= j;
2680 i += j;
2681 j = 1;
2682 }
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00002683 if (unlikely(comp))
2684 cnic_spq_completion(dev, DRV_CTL_RET_L5_SPQ_CREDIT_CMD, comp);
Michael Chana4636962009-06-08 18:14:43 -07002685}
2686
Michael Chan644b9d42010-06-24 14:58:40 +00002687static int cnic_get_kcqes(struct cnic_dev *dev, struct kcq_info *info)
Michael Chana4636962009-06-08 18:14:43 -07002688{
2689 struct cnic_local *cp = dev->cnic_priv;
Michael Chan644b9d42010-06-24 14:58:40 +00002690 u16 i, ri, hw_prod, last;
Michael Chana4636962009-06-08 18:14:43 -07002691 struct kcqe *kcqe;
2692 int kcqe_cnt = 0, last_cnt = 0;
2693
Michael Chan644b9d42010-06-24 14:58:40 +00002694 i = ri = last = info->sw_prod_idx;
Michael Chana4636962009-06-08 18:14:43 -07002695 ri &= MAX_KCQ_IDX;
Michael Chan644b9d42010-06-24 14:58:40 +00002696 hw_prod = *info->hw_prod_idx_ptr;
Michael Chan59e51372011-06-14 01:32:38 +00002697 hw_prod = info->hw_idx(hw_prod);
Michael Chana4636962009-06-08 18:14:43 -07002698
2699 while ((i != hw_prod) && (kcqe_cnt < MAX_COMPLETED_KCQE)) {
Michael Chan644b9d42010-06-24 14:58:40 +00002700 kcqe = &info->kcq[KCQ_PG(ri)][KCQ_IDX(ri)];
Michael Chana4636962009-06-08 18:14:43 -07002701 cp->completed_kcq[kcqe_cnt++] = kcqe;
Michael Chan59e51372011-06-14 01:32:38 +00002702 i = info->next_idx(i);
Michael Chana4636962009-06-08 18:14:43 -07002703 ri = i & MAX_KCQ_IDX;
2704 if (likely(!(kcqe->kcqe_op_flag & KCQE_FLAGS_NEXT))) {
2705 last_cnt = kcqe_cnt;
2706 last = i;
2707 }
2708 }
2709
Michael Chan644b9d42010-06-24 14:58:40 +00002710 info->sw_prod_idx = last;
Michael Chana4636962009-06-08 18:14:43 -07002711 return last_cnt;
2712}
2713
Michael Chan48f753d2010-05-18 11:32:53 +00002714static int cnic_l2_completion(struct cnic_local *cp)
2715{
2716 u16 hw_cons, sw_cons;
Michael Chancd801532010-10-13 14:06:49 +00002717 struct cnic_uio_dev *udev = cp->udev;
Michael Chan48f753d2010-05-18 11:32:53 +00002718 union eth_rx_cqe *cqe, *cqe_ring = (union eth_rx_cqe *)
Michael Chancd801532010-10-13 14:06:49 +00002719 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Michael Chan48f753d2010-05-18 11:32:53 +00002720 u32 cmd;
2721 int comp = 0;
2722
2723 if (!test_bit(CNIC_F_BNX2X_CLASS, &cp->dev->flags))
2724 return 0;
2725
2726 hw_cons = *cp->rx_cons_ptr;
2727 if ((hw_cons & BNX2X_MAX_RCQ_DESC_CNT) == BNX2X_MAX_RCQ_DESC_CNT)
2728 hw_cons++;
2729
2730 sw_cons = cp->rx_cons;
2731 while (sw_cons != hw_cons) {
2732 u8 cqe_fp_flags;
2733
2734 cqe = &cqe_ring[sw_cons & BNX2X_MAX_RCQ_DESC_CNT];
2735 cqe_fp_flags = cqe->fast_path_cqe.type_error_flags;
2736 if (cqe_fp_flags & ETH_FAST_PATH_RX_CQE_TYPE) {
2737 cmd = le32_to_cpu(cqe->ramrod_cqe.conn_and_cmd_data);
2738 cmd >>= COMMON_RAMROD_ETH_RX_CQE_CMD_ID_SHIFT;
2739 if (cmd == RAMROD_CMD_ID_ETH_CLIENT_SETUP ||
2740 cmd == RAMROD_CMD_ID_ETH_HALT)
2741 comp++;
2742 }
2743 sw_cons = BNX2X_NEXT_RCQE(sw_cons);
2744 }
2745 return comp;
2746}
2747
Michael Chan86b53602009-10-10 13:46:57 +00002748static void cnic_chk_pkt_rings(struct cnic_local *cp)
Michael Chana4636962009-06-08 18:14:43 -07002749{
Michael Chan541a7812010-10-06 03:17:22 +00002750 u16 rx_cons, tx_cons;
Michael Chan48f753d2010-05-18 11:32:53 +00002751 int comp = 0;
Michael Chana4636962009-06-08 18:14:43 -07002752
Michael Chan541a7812010-10-06 03:17:22 +00002753 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
Michael Chan66fee9e2010-06-24 14:58:38 +00002754 return;
2755
Michael Chan541a7812010-10-06 03:17:22 +00002756 rx_cons = *cp->rx_cons_ptr;
2757 tx_cons = *cp->tx_cons_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002758 if (cp->tx_cons != tx_cons || cp->rx_cons != rx_cons) {
Michael Chan48f753d2010-05-18 11:32:53 +00002759 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
2760 comp = cnic_l2_completion(cp);
2761
Michael Chana4636962009-06-08 18:14:43 -07002762 cp->tx_cons = tx_cons;
2763 cp->rx_cons = rx_cons;
Michael Chan71034ba2009-10-10 13:46:59 +00002764
Michael Chancd801532010-10-13 14:06:49 +00002765 if (cp->udev)
2766 uio_event_notify(&cp->udev->cnic_uinfo);
Michael Chana4636962009-06-08 18:14:43 -07002767 }
Michael Chan48f753d2010-05-18 11:32:53 +00002768 if (comp)
2769 clear_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07002770}
2771
Michael Chanb177a5d52010-06-24 14:58:41 +00002772static u32 cnic_service_bnx2_queues(struct cnic_dev *dev)
Michael Chana4636962009-06-08 18:14:43 -07002773{
Michael Chana4636962009-06-08 18:14:43 -07002774 struct cnic_local *cp = dev->cnic_priv;
Michael Chanb177a5d52010-06-24 14:58:41 +00002775 u32 status_idx = (u16) *cp->kcq1.status_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002776 int kcqe_cnt;
2777
Michael Chan107c3f42011-03-02 13:00:49 +00002778 /* status block index must be read before reading other fields */
2779 rmb();
Michael Chana4636962009-06-08 18:14:43 -07002780 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
2781
Michael Chan644b9d42010-06-24 14:58:40 +00002782 while ((kcqe_cnt = cnic_get_kcqes(dev, &cp->kcq1))) {
Michael Chana4636962009-06-08 18:14:43 -07002783
2784 service_kcqes(dev, kcqe_cnt);
2785
2786 /* Tell compiler that status_blk fields can change. */
2787 barrier();
Michael Chan93736652011-06-08 19:29:32 +00002788 status_idx = (u16) *cp->kcq1.status_idx_ptr;
2789 /* status block index must be read first */
2790 rmb();
2791 cp->kwq_con_idx = *cp->kwq_con_idx_ptr;
Michael Chana4636962009-06-08 18:14:43 -07002792 }
2793
Michael Chan644b9d42010-06-24 14:58:40 +00002794 CNIC_WR16(dev, cp->kcq1.io_addr, cp->kcq1.sw_prod_idx);
Michael Chana4636962009-06-08 18:14:43 -07002795
Michael Chan86b53602009-10-10 13:46:57 +00002796 cnic_chk_pkt_rings(cp);
Michael Chanb177a5d52010-06-24 14:58:41 +00002797
Michael Chana4636962009-06-08 18:14:43 -07002798 return status_idx;
2799}
2800
Michael Chanb177a5d52010-06-24 14:58:41 +00002801static int cnic_service_bnx2(void *data, void *status_blk)
2802{
2803 struct cnic_dev *dev = data;
Michael Chanb177a5d52010-06-24 14:58:41 +00002804
Michael Chaneaaa6e92010-12-23 08:38:30 +00002805 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
2806 struct status_block *sblk = status_blk;
2807
2808 return sblk->status_idx;
2809 }
Michael Chanb177a5d52010-06-24 14:58:41 +00002810
2811 return cnic_service_bnx2_queues(dev);
2812}
2813
Michael Chana4636962009-06-08 18:14:43 -07002814static void cnic_service_bnx2_msix(unsigned long data)
2815{
2816 struct cnic_dev *dev = (struct cnic_dev *) data;
2817 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002818
Michael Chanb177a5d52010-06-24 14:58:41 +00002819 cp->last_status_idx = cnic_service_bnx2_queues(dev);
Michael Chana4636962009-06-08 18:14:43 -07002820
Michael Chana4636962009-06-08 18:14:43 -07002821 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
2822 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
2823}
2824
Michael Chan66fee9e2010-06-24 14:58:38 +00002825static void cnic_doirq(struct cnic_dev *dev)
2826{
2827 struct cnic_local *cp = dev->cnic_priv;
Michael Chan66fee9e2010-06-24 14:58:38 +00002828
2829 if (likely(test_bit(CNIC_F_CNIC_UP, &dev->flags))) {
Michael Chaneaaa6e92010-12-23 08:38:30 +00002830 u16 prod = cp->kcq1.sw_prod_idx & MAX_KCQ_IDX;
2831
Michael Chan66fee9e2010-06-24 14:58:38 +00002832 prefetch(cp->status_blk.gen);
Michael Chane6c28892010-06-24 14:58:39 +00002833 prefetch(&cp->kcq1.kcq[KCQ_PG(prod)][KCQ_IDX(prod)]);
Michael Chan66fee9e2010-06-24 14:58:38 +00002834
2835 tasklet_schedule(&cp->cnic_irq_task);
2836 }
2837}
2838
Michael Chana4636962009-06-08 18:14:43 -07002839static irqreturn_t cnic_irq(int irq, void *dev_instance)
2840{
2841 struct cnic_dev *dev = dev_instance;
2842 struct cnic_local *cp = dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07002843
2844 if (cp->ack_int)
2845 cp->ack_int(dev);
2846
Michael Chan66fee9e2010-06-24 14:58:38 +00002847 cnic_doirq(dev);
Michael Chana4636962009-06-08 18:14:43 -07002848
2849 return IRQ_HANDLED;
2850}
2851
Michael Chan71034ba2009-10-10 13:46:59 +00002852static inline void cnic_ack_bnx2x_int(struct cnic_dev *dev, u8 id, u8 storm,
2853 u16 index, u8 op, u8 update)
2854{
2855 struct cnic_local *cp = dev->cnic_priv;
2856 u32 hc_addr = (HC_REG_COMMAND_REG + CNIC_PORT(cp) * 32 +
2857 COMMAND_REG_INT_ACK);
2858 struct igu_ack_register igu_ack;
2859
2860 igu_ack.status_block_index = index;
2861 igu_ack.sb_id_and_flags =
2862 ((id << IGU_ACK_REGISTER_STATUS_BLOCK_ID_SHIFT) |
2863 (storm << IGU_ACK_REGISTER_STORM_ID_SHIFT) |
2864 (update << IGU_ACK_REGISTER_UPDATE_INDEX_SHIFT) |
2865 (op << IGU_ACK_REGISTER_INTERRUPT_MODE_SHIFT));
2866
2867 CNIC_WR(dev, hc_addr, (*(u32 *)&igu_ack));
2868}
2869
Michael Chanee87a822010-10-13 14:06:51 +00002870static void cnic_ack_igu_sb(struct cnic_dev *dev, u8 igu_sb_id, u8 segment,
2871 u16 index, u8 op, u8 update)
2872{
2873 struct igu_regular cmd_data;
2874 u32 igu_addr = BAR_IGU_INTMEM + (IGU_CMD_INT_ACK_BASE + igu_sb_id) * 8;
2875
2876 cmd_data.sb_id_and_flags =
2877 (index << IGU_REGULAR_SB_INDEX_SHIFT) |
2878 (segment << IGU_REGULAR_SEGMENT_ACCESS_SHIFT) |
2879 (update << IGU_REGULAR_BUPDATE_SHIFT) |
2880 (op << IGU_REGULAR_ENABLE_INT_SHIFT);
2881
2882
2883 CNIC_WR(dev, igu_addr, cmd_data.sb_id_and_flags);
2884}
2885
Michael Chan71034ba2009-10-10 13:46:59 +00002886static void cnic_ack_bnx2x_msix(struct cnic_dev *dev)
2887{
2888 struct cnic_local *cp = dev->cnic_priv;
2889
Dmitry Kravkov523224a2010-10-06 03:23:26 +00002890 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, CSTORM_ID, 0,
Michael Chan71034ba2009-10-10 13:46:59 +00002891 IGU_INT_DISABLE, 0);
2892}
2893
Michael Chanee87a822010-10-13 14:06:51 +00002894static void cnic_ack_bnx2x_e2_msix(struct cnic_dev *dev)
2895{
2896 struct cnic_local *cp = dev->cnic_priv;
2897
2898 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF, 0,
2899 IGU_INT_DISABLE, 0);
2900}
2901
Michael Chanb177a5d52010-06-24 14:58:41 +00002902static u32 cnic_service_bnx2x_kcq(struct cnic_dev *dev, struct kcq_info *info)
Michael Chan71034ba2009-10-10 13:46:59 +00002903{
Michael Chanb177a5d52010-06-24 14:58:41 +00002904 u32 last_status = *info->status_idx_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00002905 int kcqe_cnt;
2906
Michael Chan107c3f42011-03-02 13:00:49 +00002907 /* status block index must be read before reading the KCQ */
2908 rmb();
Michael Chanb177a5d52010-06-24 14:58:41 +00002909 while ((kcqe_cnt = cnic_get_kcqes(dev, info))) {
Michael Chan71034ba2009-10-10 13:46:59 +00002910
2911 service_kcqes(dev, kcqe_cnt);
2912
2913 /* Tell compiler that sblk fields can change. */
2914 barrier();
Michael Chan71034ba2009-10-10 13:46:59 +00002915
Michael Chanb177a5d52010-06-24 14:58:41 +00002916 last_status = *info->status_idx_ptr;
Michael Chan107c3f42011-03-02 13:00:49 +00002917 /* status block index must be read before reading the KCQ */
2918 rmb();
Michael Chan71034ba2009-10-10 13:46:59 +00002919 }
Michael Chanb177a5d52010-06-24 14:58:41 +00002920 return last_status;
2921}
2922
2923static void cnic_service_bnx2x_bh(unsigned long data)
2924{
2925 struct cnic_dev *dev = (struct cnic_dev *) data;
2926 struct cnic_local *cp = dev->cnic_priv;
Michael Chan0197b082011-03-02 13:00:50 +00002927 u32 status_idx, new_status_idx;
Michael Chanb177a5d52010-06-24 14:58:41 +00002928
2929 if (unlikely(!test_bit(CNIC_F_CNIC_UP, &dev->flags)))
2930 return;
2931
Michael Chan0197b082011-03-02 13:00:50 +00002932 while (1) {
2933 status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq1);
Michael Chan71034ba2009-10-10 13:46:59 +00002934
Michael Chan0197b082011-03-02 13:00:50 +00002935 CNIC_WR16(dev, cp->kcq1.io_addr,
2936 cp->kcq1.sw_prod_idx + MAX_KCQ_IDX);
Michael Chane21ba412010-12-23 07:43:03 +00002937
Michael Chan0197b082011-03-02 13:00:50 +00002938 if (!BNX2X_CHIP_IS_E2(cp->chip_id)) {
2939 cnic_ack_bnx2x_int(dev, cp->bnx2x_igu_sb_id, USTORM_ID,
2940 status_idx, IGU_INT_ENABLE, 1);
2941 break;
2942 }
2943
2944 new_status_idx = cnic_service_bnx2x_kcq(dev, &cp->kcq2);
2945
2946 if (new_status_idx != status_idx)
2947 continue;
Michael Chane21ba412010-12-23 07:43:03 +00002948
2949 CNIC_WR16(dev, cp->kcq2.io_addr, cp->kcq2.sw_prod_idx +
2950 MAX_KCQ_IDX);
2951
Michael Chanee87a822010-10-13 14:06:51 +00002952 cnic_ack_igu_sb(dev, cp->bnx2x_igu_sb_id, IGU_SEG_ACCESS_DEF,
2953 status_idx, IGU_INT_ENABLE, 1);
Michael Chan0197b082011-03-02 13:00:50 +00002954
2955 break;
Michael Chane21ba412010-12-23 07:43:03 +00002956 }
Michael Chan71034ba2009-10-10 13:46:59 +00002957}
2958
2959static int cnic_service_bnx2x(void *data, void *status_blk)
2960{
2961 struct cnic_dev *dev = data;
2962 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00002963
Michael Chan66fee9e2010-06-24 14:58:38 +00002964 if (!(cp->ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
2965 cnic_doirq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00002966
Michael Chan66fee9e2010-06-24 14:58:38 +00002967 cnic_chk_pkt_rings(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00002968
2969 return 0;
2970}
2971
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07002972static void cnic_ulp_stop_one(struct cnic_local *cp, int if_type)
2973{
2974 struct cnic_ulp_ops *ulp_ops;
2975
2976 if (if_type == CNIC_ULP_ISCSI)
2977 cnic_send_nlmsg(cp, ISCSI_KEVENT_IF_DOWN, NULL);
2978
2979 mutex_lock(&cnic_lock);
2980 ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
2981 lockdep_is_held(&cnic_lock));
2982 if (!ulp_ops) {
2983 mutex_unlock(&cnic_lock);
2984 return;
2985 }
2986 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
2987 mutex_unlock(&cnic_lock);
2988
2989 if (test_and_clear_bit(ULP_F_START, &cp->ulp_flags[if_type]))
2990 ulp_ops->cnic_stop(cp->ulp_handle[if_type]);
2991
2992 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
2993}
2994
Michael Chana4636962009-06-08 18:14:43 -07002995static void cnic_ulp_stop(struct cnic_dev *dev)
2996{
2997 struct cnic_local *cp = dev->cnic_priv;
2998 int if_type;
2999
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003000 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++)
3001 cnic_ulp_stop_one(cp, if_type);
Michael Chana4636962009-06-08 18:14:43 -07003002}
3003
3004static void cnic_ulp_start(struct cnic_dev *dev)
3005{
3006 struct cnic_local *cp = dev->cnic_priv;
3007 int if_type;
3008
Michael Chana4636962009-06-08 18:14:43 -07003009 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
3010 struct cnic_ulp_ops *ulp_ops;
3011
Michael Chan681dbd72009-08-14 15:49:46 +00003012 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003013 ulp_ops = rcu_dereference_protected(cp->ulp_ops[if_type],
3014 lockdep_is_held(&cnic_lock));
Michael Chan681dbd72009-08-14 15:49:46 +00003015 if (!ulp_ops || !ulp_ops->cnic_start) {
3016 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003017 continue;
Michael Chan681dbd72009-08-14 15:49:46 +00003018 }
3019 set_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
3020 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003021
3022 if (!test_and_set_bit(ULP_F_START, &cp->ulp_flags[if_type]))
3023 ulp_ops->cnic_start(cp->ulp_handle[if_type]);
Michael Chan681dbd72009-08-14 15:49:46 +00003024
3025 clear_bit(ULP_F_CALL_PENDING, &cp->ulp_flags[if_type]);
Michael Chana4636962009-06-08 18:14:43 -07003026 }
Michael Chana4636962009-06-08 18:14:43 -07003027}
3028
3029static int cnic_ctl(void *data, struct cnic_ctl_info *info)
3030{
3031 struct cnic_dev *dev = data;
3032
3033 switch (info->cmd) {
3034 case CNIC_CTL_STOP_CMD:
3035 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07003036
3037 cnic_ulp_stop(dev);
3038 cnic_stop_hw(dev);
3039
Michael Chana4636962009-06-08 18:14:43 -07003040 cnic_put(dev);
3041 break;
3042 case CNIC_CTL_START_CMD:
3043 cnic_hold(dev);
Michael Chana4636962009-06-08 18:14:43 -07003044
3045 if (!cnic_start_hw(dev))
3046 cnic_ulp_start(dev);
3047
Michael Chana4636962009-06-08 18:14:43 -07003048 cnic_put(dev);
3049 break;
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003050 case CNIC_CTL_STOP_ISCSI_CMD: {
3051 struct cnic_local *cp = dev->cnic_priv;
3052 set_bit(CNIC_LCL_FL_STOP_ISCSI, &cp->cnic_local_flags);
3053 queue_delayed_work(cnic_wq, &cp->delete_task, 0);
3054 break;
3055 }
Michael Chan71034ba2009-10-10 13:46:59 +00003056 case CNIC_CTL_COMPLETION_CMD: {
3057 u32 cid = BNX2X_SW_CID(info->data.comp.cid);
3058 u32 l5_cid;
3059 struct cnic_local *cp = dev->cnic_priv;
3060
3061 if (cnic_get_l5_cid(cp, cid, &l5_cid) == 0) {
3062 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3063
3064 ctx->wait_cond = 1;
3065 wake_up(&ctx->waitq);
3066 }
3067 break;
3068 }
Michael Chana4636962009-06-08 18:14:43 -07003069 default:
3070 return -EINVAL;
3071 }
3072 return 0;
3073}
3074
3075static void cnic_ulp_init(struct cnic_dev *dev)
3076{
3077 int i;
3078 struct cnic_local *cp = dev->cnic_priv;
3079
Michael Chana4636962009-06-08 18:14:43 -07003080 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3081 struct cnic_ulp_ops *ulp_ops;
3082
Michael Chan7fc1ece2009-08-14 15:49:47 +00003083 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003084 ulp_ops = cnic_ulp_tbl_prot(i);
Michael Chan7fc1ece2009-08-14 15:49:47 +00003085 if (!ulp_ops || !ulp_ops->cnic_init) {
3086 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003087 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003088 }
3089 ulp_get(ulp_ops);
3090 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003091
3092 if (!test_and_set_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3093 ulp_ops->cnic_init(dev);
3094
Michael Chan7fc1ece2009-08-14 15:49:47 +00003095 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003096 }
Michael Chana4636962009-06-08 18:14:43 -07003097}
3098
3099static void cnic_ulp_exit(struct cnic_dev *dev)
3100{
3101 int i;
3102 struct cnic_local *cp = dev->cnic_priv;
3103
Michael Chana4636962009-06-08 18:14:43 -07003104 for (i = 0; i < MAX_CNIC_ULP_TYPE_EXT; i++) {
3105 struct cnic_ulp_ops *ulp_ops;
3106
Michael Chan7fc1ece2009-08-14 15:49:47 +00003107 mutex_lock(&cnic_lock);
Eric Dumazet13707f92011-01-26 19:28:23 +00003108 ulp_ops = cnic_ulp_tbl_prot(i);
Michael Chan7fc1ece2009-08-14 15:49:47 +00003109 if (!ulp_ops || !ulp_ops->cnic_exit) {
3110 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003111 continue;
Michael Chan7fc1ece2009-08-14 15:49:47 +00003112 }
3113 ulp_get(ulp_ops);
3114 mutex_unlock(&cnic_lock);
Michael Chana4636962009-06-08 18:14:43 -07003115
3116 if (test_and_clear_bit(ULP_F_INIT, &cp->ulp_flags[i]))
3117 ulp_ops->cnic_exit(dev);
3118
Michael Chan7fc1ece2009-08-14 15:49:47 +00003119 ulp_put(ulp_ops);
Michael Chana4636962009-06-08 18:14:43 -07003120 }
Michael Chana4636962009-06-08 18:14:43 -07003121}
3122
3123static int cnic_cm_offload_pg(struct cnic_sock *csk)
3124{
3125 struct cnic_dev *dev = csk->dev;
3126 struct l4_kwq_offload_pg *l4kwqe;
3127 struct kwqe *wqes[1];
3128
3129 l4kwqe = (struct l4_kwq_offload_pg *) &csk->kwqe1;
3130 memset(l4kwqe, 0, sizeof(*l4kwqe));
3131 wqes[0] = (struct kwqe *) l4kwqe;
3132
3133 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_OFFLOAD_PG;
3134 l4kwqe->flags =
3135 L4_LAYER_CODE << L4_KWQ_OFFLOAD_PG_LAYER_CODE_SHIFT;
3136 l4kwqe->l2hdr_nbytes = ETH_HLEN;
3137
3138 l4kwqe->da0 = csk->ha[0];
3139 l4kwqe->da1 = csk->ha[1];
3140 l4kwqe->da2 = csk->ha[2];
3141 l4kwqe->da3 = csk->ha[3];
3142 l4kwqe->da4 = csk->ha[4];
3143 l4kwqe->da5 = csk->ha[5];
3144
3145 l4kwqe->sa0 = dev->mac_addr[0];
3146 l4kwqe->sa1 = dev->mac_addr[1];
3147 l4kwqe->sa2 = dev->mac_addr[2];
3148 l4kwqe->sa3 = dev->mac_addr[3];
3149 l4kwqe->sa4 = dev->mac_addr[4];
3150 l4kwqe->sa5 = dev->mac_addr[5];
3151
3152 l4kwqe->etype = ETH_P_IP;
Eddie Waia9736c02010-02-24 14:42:04 +00003153 l4kwqe->ipid_start = DEF_IPID_START;
Michael Chana4636962009-06-08 18:14:43 -07003154 l4kwqe->host_opaque = csk->l5_cid;
3155
3156 if (csk->vlan_id) {
3157 l4kwqe->pg_flags |= L4_KWQ_OFFLOAD_PG_VLAN_TAGGING;
3158 l4kwqe->vlan_tag = csk->vlan_id;
3159 l4kwqe->l2hdr_nbytes += 4;
3160 }
3161
3162 return dev->submit_kwqes(dev, wqes, 1);
3163}
3164
3165static int cnic_cm_update_pg(struct cnic_sock *csk)
3166{
3167 struct cnic_dev *dev = csk->dev;
3168 struct l4_kwq_update_pg *l4kwqe;
3169 struct kwqe *wqes[1];
3170
3171 l4kwqe = (struct l4_kwq_update_pg *) &csk->kwqe1;
3172 memset(l4kwqe, 0, sizeof(*l4kwqe));
3173 wqes[0] = (struct kwqe *) l4kwqe;
3174
3175 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPDATE_PG;
3176 l4kwqe->flags =
3177 L4_LAYER_CODE << L4_KWQ_UPDATE_PG_LAYER_CODE_SHIFT;
3178 l4kwqe->pg_cid = csk->pg_cid;
3179
3180 l4kwqe->da0 = csk->ha[0];
3181 l4kwqe->da1 = csk->ha[1];
3182 l4kwqe->da2 = csk->ha[2];
3183 l4kwqe->da3 = csk->ha[3];
3184 l4kwqe->da4 = csk->ha[4];
3185 l4kwqe->da5 = csk->ha[5];
3186
3187 l4kwqe->pg_host_opaque = csk->l5_cid;
3188 l4kwqe->pg_valids = L4_KWQ_UPDATE_PG_VALIDS_DA;
3189
3190 return dev->submit_kwqes(dev, wqes, 1);
3191}
3192
3193static int cnic_cm_upload_pg(struct cnic_sock *csk)
3194{
3195 struct cnic_dev *dev = csk->dev;
3196 struct l4_kwq_upload *l4kwqe;
3197 struct kwqe *wqes[1];
3198
3199 l4kwqe = (struct l4_kwq_upload *) &csk->kwqe1;
3200 memset(l4kwqe, 0, sizeof(*l4kwqe));
3201 wqes[0] = (struct kwqe *) l4kwqe;
3202
3203 l4kwqe->opcode = L4_KWQE_OPCODE_VALUE_UPLOAD_PG;
3204 l4kwqe->flags =
3205 L4_LAYER_CODE << L4_KWQ_UPLOAD_LAYER_CODE_SHIFT;
3206 l4kwqe->cid = csk->pg_cid;
3207
3208 return dev->submit_kwqes(dev, wqes, 1);
3209}
3210
3211static int cnic_cm_conn_req(struct cnic_sock *csk)
3212{
3213 struct cnic_dev *dev = csk->dev;
3214 struct l4_kwq_connect_req1 *l4kwqe1;
3215 struct l4_kwq_connect_req2 *l4kwqe2;
3216 struct l4_kwq_connect_req3 *l4kwqe3;
3217 struct kwqe *wqes[3];
3218 u8 tcp_flags = 0;
3219 int num_wqes = 2;
3220
3221 l4kwqe1 = (struct l4_kwq_connect_req1 *) &csk->kwqe1;
3222 l4kwqe2 = (struct l4_kwq_connect_req2 *) &csk->kwqe2;
3223 l4kwqe3 = (struct l4_kwq_connect_req3 *) &csk->kwqe3;
3224 memset(l4kwqe1, 0, sizeof(*l4kwqe1));
3225 memset(l4kwqe2, 0, sizeof(*l4kwqe2));
3226 memset(l4kwqe3, 0, sizeof(*l4kwqe3));
3227
3228 l4kwqe3->op_code = L4_KWQE_OPCODE_VALUE_CONNECT3;
3229 l4kwqe3->flags =
3230 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ3_LAYER_CODE_SHIFT;
3231 l4kwqe3->ka_timeout = csk->ka_timeout;
3232 l4kwqe3->ka_interval = csk->ka_interval;
3233 l4kwqe3->ka_max_probe_count = csk->ka_max_probe_count;
3234 l4kwqe3->tos = csk->tos;
3235 l4kwqe3->ttl = csk->ttl;
3236 l4kwqe3->snd_seq_scale = csk->snd_seq_scale;
3237 l4kwqe3->pmtu = csk->mtu;
3238 l4kwqe3->rcv_buf = csk->rcv_buf;
3239 l4kwqe3->snd_buf = csk->snd_buf;
3240 l4kwqe3->seed = csk->seed;
3241
3242 wqes[0] = (struct kwqe *) l4kwqe1;
3243 if (test_bit(SK_F_IPV6, &csk->flags)) {
3244 wqes[1] = (struct kwqe *) l4kwqe2;
3245 wqes[2] = (struct kwqe *) l4kwqe3;
3246 num_wqes = 3;
3247
3248 l4kwqe1->conn_flags = L4_KWQ_CONNECT_REQ1_IP_V6;
3249 l4kwqe2->op_code = L4_KWQE_OPCODE_VALUE_CONNECT2;
3250 l4kwqe2->flags =
3251 L4_KWQ_CONNECT_REQ2_LINKED_WITH_NEXT |
3252 L4_LAYER_CODE << L4_KWQ_CONNECT_REQ2_LAYER_CODE_SHIFT;
3253 l4kwqe2->src_ip_v6_2 = be32_to_cpu(csk->src_ip[1]);
3254 l4kwqe2->src_ip_v6_3 = be32_to_cpu(csk->src_ip[2]);
3255 l4kwqe2->src_ip_v6_4 = be32_to_cpu(csk->src_ip[3]);
3256 l4kwqe2->dst_ip_v6_2 = be32_to_cpu(csk->dst_ip[1]);
3257 l4kwqe2->dst_ip_v6_3 = be32_to_cpu(csk->dst_ip[2]);
3258 l4kwqe2->dst_ip_v6_4 = be32_to_cpu(csk->dst_ip[3]);
3259 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct ipv6hdr) -
3260 sizeof(struct tcphdr);
3261 } else {
3262 wqes[1] = (struct kwqe *) l4kwqe3;
3263 l4kwqe3->mss = l4kwqe3->pmtu - sizeof(struct iphdr) -
3264 sizeof(struct tcphdr);
3265 }
3266
3267 l4kwqe1->op_code = L4_KWQE_OPCODE_VALUE_CONNECT1;
3268 l4kwqe1->flags =
3269 (L4_LAYER_CODE << L4_KWQ_CONNECT_REQ1_LAYER_CODE_SHIFT) |
3270 L4_KWQ_CONNECT_REQ3_LINKED_WITH_NEXT;
3271 l4kwqe1->cid = csk->cid;
3272 l4kwqe1->pg_cid = csk->pg_cid;
3273 l4kwqe1->src_ip = be32_to_cpu(csk->src_ip[0]);
3274 l4kwqe1->dst_ip = be32_to_cpu(csk->dst_ip[0]);
3275 l4kwqe1->src_port = be16_to_cpu(csk->src_port);
3276 l4kwqe1->dst_port = be16_to_cpu(csk->dst_port);
3277 if (csk->tcp_flags & SK_TCP_NO_DELAY_ACK)
3278 tcp_flags |= L4_KWQ_CONNECT_REQ1_NO_DELAY_ACK;
3279 if (csk->tcp_flags & SK_TCP_KEEP_ALIVE)
3280 tcp_flags |= L4_KWQ_CONNECT_REQ1_KEEP_ALIVE;
3281 if (csk->tcp_flags & SK_TCP_NAGLE)
3282 tcp_flags |= L4_KWQ_CONNECT_REQ1_NAGLE_ENABLE;
3283 if (csk->tcp_flags & SK_TCP_TIMESTAMP)
3284 tcp_flags |= L4_KWQ_CONNECT_REQ1_TIME_STAMP;
3285 if (csk->tcp_flags & SK_TCP_SACK)
3286 tcp_flags |= L4_KWQ_CONNECT_REQ1_SACK;
3287 if (csk->tcp_flags & SK_TCP_SEG_SCALING)
3288 tcp_flags |= L4_KWQ_CONNECT_REQ1_SEG_SCALING;
3289
3290 l4kwqe1->tcp_flags = tcp_flags;
3291
3292 return dev->submit_kwqes(dev, wqes, num_wqes);
3293}
3294
3295static int cnic_cm_close_req(struct cnic_sock *csk)
3296{
3297 struct cnic_dev *dev = csk->dev;
3298 struct l4_kwq_close_req *l4kwqe;
3299 struct kwqe *wqes[1];
3300
3301 l4kwqe = (struct l4_kwq_close_req *) &csk->kwqe2;
3302 memset(l4kwqe, 0, sizeof(*l4kwqe));
3303 wqes[0] = (struct kwqe *) l4kwqe;
3304
3305 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_CLOSE;
3306 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_CLOSE_REQ_LAYER_CODE_SHIFT;
3307 l4kwqe->cid = csk->cid;
3308
3309 return dev->submit_kwqes(dev, wqes, 1);
3310}
3311
3312static int cnic_cm_abort_req(struct cnic_sock *csk)
3313{
3314 struct cnic_dev *dev = csk->dev;
3315 struct l4_kwq_reset_req *l4kwqe;
3316 struct kwqe *wqes[1];
3317
3318 l4kwqe = (struct l4_kwq_reset_req *) &csk->kwqe2;
3319 memset(l4kwqe, 0, sizeof(*l4kwqe));
3320 wqes[0] = (struct kwqe *) l4kwqe;
3321
3322 l4kwqe->op_code = L4_KWQE_OPCODE_VALUE_RESET;
3323 l4kwqe->flags = L4_LAYER_CODE << L4_KWQ_RESET_REQ_LAYER_CODE_SHIFT;
3324 l4kwqe->cid = csk->cid;
3325
3326 return dev->submit_kwqes(dev, wqes, 1);
3327}
3328
3329static int cnic_cm_create(struct cnic_dev *dev, int ulp_type, u32 cid,
3330 u32 l5_cid, struct cnic_sock **csk, void *context)
3331{
3332 struct cnic_local *cp = dev->cnic_priv;
3333 struct cnic_sock *csk1;
3334
3335 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3336 return -EINVAL;
3337
Michael Chanfdf24082010-10-13 14:06:47 +00003338 if (cp->ctx_tbl) {
3339 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3340
3341 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
3342 return -EAGAIN;
3343 }
3344
Michael Chana4636962009-06-08 18:14:43 -07003345 csk1 = &cp->csk_tbl[l5_cid];
3346 if (atomic_read(&csk1->ref_count))
3347 return -EAGAIN;
3348
3349 if (test_and_set_bit(SK_F_INUSE, &csk1->flags))
3350 return -EBUSY;
3351
3352 csk1->dev = dev;
3353 csk1->cid = cid;
3354 csk1->l5_cid = l5_cid;
3355 csk1->ulp_type = ulp_type;
3356 csk1->context = context;
3357
3358 csk1->ka_timeout = DEF_KA_TIMEOUT;
3359 csk1->ka_interval = DEF_KA_INTERVAL;
3360 csk1->ka_max_probe_count = DEF_KA_MAX_PROBE_COUNT;
3361 csk1->tos = DEF_TOS;
3362 csk1->ttl = DEF_TTL;
3363 csk1->snd_seq_scale = DEF_SND_SEQ_SCALE;
3364 csk1->rcv_buf = DEF_RCV_BUF;
3365 csk1->snd_buf = DEF_SND_BUF;
3366 csk1->seed = DEF_SEED;
3367
3368 *csk = csk1;
3369 return 0;
3370}
3371
3372static void cnic_cm_cleanup(struct cnic_sock *csk)
3373{
3374 if (csk->src_port) {
3375 struct cnic_dev *dev = csk->dev;
3376 struct cnic_local *cp = dev->cnic_priv;
3377
Michael Chan9b093362010-12-23 07:42:56 +00003378 cnic_free_id(&cp->csk_port_tbl, be16_to_cpu(csk->src_port));
Michael Chana4636962009-06-08 18:14:43 -07003379 csk->src_port = 0;
3380 }
3381}
3382
3383static void cnic_close_conn(struct cnic_sock *csk)
3384{
3385 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags)) {
3386 cnic_cm_upload_pg(csk);
3387 clear_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3388 }
3389 cnic_cm_cleanup(csk);
3390}
3391
3392static int cnic_cm_destroy(struct cnic_sock *csk)
3393{
3394 if (!cnic_in_use(csk))
3395 return -EINVAL;
3396
3397 csk_hold(csk);
3398 clear_bit(SK_F_INUSE, &csk->flags);
3399 smp_mb__after_clear_bit();
3400 while (atomic_read(&csk->ref_count) != 1)
3401 msleep(1);
3402 cnic_cm_cleanup(csk);
3403
3404 csk->flags = 0;
3405 csk_put(csk);
3406 return 0;
3407}
3408
3409static inline u16 cnic_get_vlan(struct net_device *dev,
3410 struct net_device **vlan_dev)
3411{
3412 if (dev->priv_flags & IFF_802_1Q_VLAN) {
3413 *vlan_dev = vlan_dev_real_dev(dev);
3414 return vlan_dev_vlan_id(dev);
3415 }
3416 *vlan_dev = dev;
3417 return 0;
3418}
3419
3420static int cnic_get_v4_route(struct sockaddr_in *dst_addr,
3421 struct dst_entry **dst)
3422{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003423#if defined(CONFIG_INET)
Michael Chana4636962009-06-08 18:14:43 -07003424 struct rtable *rt;
3425
David S. Miller78fbfd82011-03-12 00:00:52 -05003426 rt = ip_route_output(&init_net, dst_addr->sin_addr.s_addr, 0, 0, 0);
3427 if (!IS_ERR(rt)) {
Changli Gaod8d1f302010-06-10 23:31:35 -07003428 *dst = &rt->dst;
David S. Miller78fbfd82011-03-12 00:00:52 -05003429 return 0;
3430 }
3431 return PTR_ERR(rt);
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003432#else
3433 return -ENETUNREACH;
3434#endif
Michael Chana4636962009-06-08 18:14:43 -07003435}
3436
3437static int cnic_get_v6_route(struct sockaddr_in6 *dst_addr,
3438 struct dst_entry **dst)
3439{
Randy Dunlapfaea56c2009-06-12 11:43:48 -07003440#if defined(CONFIG_IPV6) || (defined(CONFIG_IPV6_MODULE) && defined(MODULE))
David S. Miller4c9483b2011-03-12 16:22:43 -05003441 struct flowi6 fl6;
Michael Chana4636962009-06-08 18:14:43 -07003442
David S. Miller4c9483b2011-03-12 16:22:43 -05003443 memset(&fl6, 0, sizeof(fl6));
3444 ipv6_addr_copy(&fl6.daddr, &dst_addr->sin6_addr);
3445 if (ipv6_addr_type(&fl6.daddr) & IPV6_ADDR_LINKLOCAL)
3446 fl6.flowi6_oif = dst_addr->sin6_scope_id;
Michael Chana4636962009-06-08 18:14:43 -07003447
David S. Miller4c9483b2011-03-12 16:22:43 -05003448 *dst = ip6_route_output(&init_net, NULL, &fl6);
Michael Chana4636962009-06-08 18:14:43 -07003449 if (*dst)
3450 return 0;
3451#endif
3452
3453 return -ENETUNREACH;
3454}
3455
3456static struct cnic_dev *cnic_cm_select_dev(struct sockaddr_in *dst_addr,
3457 int ulp_type)
3458{
3459 struct cnic_dev *dev = NULL;
3460 struct dst_entry *dst;
3461 struct net_device *netdev = NULL;
3462 int err = -ENETUNREACH;
3463
3464 if (dst_addr->sin_family == AF_INET)
3465 err = cnic_get_v4_route(dst_addr, &dst);
3466 else if (dst_addr->sin_family == AF_INET6) {
3467 struct sockaddr_in6 *dst_addr6 =
3468 (struct sockaddr_in6 *) dst_addr;
3469
3470 err = cnic_get_v6_route(dst_addr6, &dst);
3471 } else
3472 return NULL;
3473
3474 if (err)
3475 return NULL;
3476
3477 if (!dst->dev)
3478 goto done;
3479
3480 cnic_get_vlan(dst->dev, &netdev);
3481
3482 dev = cnic_from_netdev(netdev);
3483
3484done:
3485 dst_release(dst);
3486 if (dev)
3487 cnic_put(dev);
3488 return dev;
3489}
3490
3491static int cnic_resolve_addr(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3492{
3493 struct cnic_dev *dev = csk->dev;
3494 struct cnic_local *cp = dev->cnic_priv;
3495
3496 return cnic_send_nlmsg(cp, ISCSI_KEVENT_PATH_REQ, csk);
3497}
3498
3499static int cnic_get_route(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3500{
3501 struct cnic_dev *dev = csk->dev;
3502 struct cnic_local *cp = dev->cnic_priv;
Michael Chanc76284a2010-02-24 14:42:07 +00003503 int is_v6, rc = 0;
3504 struct dst_entry *dst = NULL;
Michael Chana4636962009-06-08 18:14:43 -07003505 struct net_device *realdev;
Michael Chan9b093362010-12-23 07:42:56 +00003506 __be16 local_port;
3507 u32 port_id;
Michael Chana4636962009-06-08 18:14:43 -07003508
3509 if (saddr->local.v6.sin6_family == AF_INET6 &&
3510 saddr->remote.v6.sin6_family == AF_INET6)
3511 is_v6 = 1;
3512 else if (saddr->local.v4.sin_family == AF_INET &&
3513 saddr->remote.v4.sin_family == AF_INET)
3514 is_v6 = 0;
3515 else
3516 return -EINVAL;
3517
3518 clear_bit(SK_F_IPV6, &csk->flags);
3519
3520 if (is_v6) {
Michael Chana4636962009-06-08 18:14:43 -07003521 set_bit(SK_F_IPV6, &csk->flags);
Michael Chanc76284a2010-02-24 14:42:07 +00003522 cnic_get_v6_route(&saddr->remote.v6, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003523
3524 memcpy(&csk->dst_ip[0], &saddr->remote.v6.sin6_addr,
3525 sizeof(struct in6_addr));
3526 csk->dst_port = saddr->remote.v6.sin6_port;
3527 local_port = saddr->local.v6.sin6_port;
Michael Chana4636962009-06-08 18:14:43 -07003528
3529 } else {
Michael Chanc76284a2010-02-24 14:42:07 +00003530 cnic_get_v4_route(&saddr->remote.v4, &dst);
Michael Chana4636962009-06-08 18:14:43 -07003531
3532 csk->dst_ip[0] = saddr->remote.v4.sin_addr.s_addr;
3533 csk->dst_port = saddr->remote.v4.sin_port;
3534 local_port = saddr->local.v4.sin_port;
3535 }
3536
Michael Chanc76284a2010-02-24 14:42:07 +00003537 csk->vlan_id = 0;
3538 csk->mtu = dev->netdev->mtu;
3539 if (dst && dst->dev) {
3540 u16 vlan = cnic_get_vlan(dst->dev, &realdev);
3541 if (realdev == dev->netdev) {
3542 csk->vlan_id = vlan;
3543 csk->mtu = dst_mtu(dst);
3544 }
3545 }
Michael Chana4636962009-06-08 18:14:43 -07003546
Michael Chan9b093362010-12-23 07:42:56 +00003547 port_id = be16_to_cpu(local_port);
3548 if (port_id >= CNIC_LOCAL_PORT_MIN &&
3549 port_id < CNIC_LOCAL_PORT_MAX) {
3550 if (cnic_alloc_id(&cp->csk_port_tbl, port_id))
3551 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003552 } else
Michael Chan9b093362010-12-23 07:42:56 +00003553 port_id = 0;
Michael Chana4636962009-06-08 18:14:43 -07003554
Michael Chan9b093362010-12-23 07:42:56 +00003555 if (!port_id) {
3556 port_id = cnic_alloc_new_id(&cp->csk_port_tbl);
3557 if (port_id == -1) {
Michael Chana4636962009-06-08 18:14:43 -07003558 rc = -ENOMEM;
3559 goto err_out;
3560 }
Michael Chan9b093362010-12-23 07:42:56 +00003561 local_port = cpu_to_be16(port_id);
Michael Chana4636962009-06-08 18:14:43 -07003562 }
3563 csk->src_port = local_port;
3564
Michael Chana4636962009-06-08 18:14:43 -07003565err_out:
3566 dst_release(dst);
3567 return rc;
3568}
3569
3570static void cnic_init_csk_state(struct cnic_sock *csk)
3571{
3572 csk->state = 0;
3573 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3574 clear_bit(SK_F_CLOSING, &csk->flags);
3575}
3576
3577static int cnic_cm_connect(struct cnic_sock *csk, struct cnic_sockaddr *saddr)
3578{
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003579 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chana4636962009-06-08 18:14:43 -07003580 int err = 0;
3581
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003582 if (cp->ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI)
3583 return -EOPNOTSUPP;
3584
Michael Chana4636962009-06-08 18:14:43 -07003585 if (!cnic_in_use(csk))
3586 return -EINVAL;
3587
3588 if (test_and_set_bit(SK_F_CONNECT_START, &csk->flags))
3589 return -EINVAL;
3590
3591 cnic_init_csk_state(csk);
3592
3593 err = cnic_get_route(csk, saddr);
3594 if (err)
3595 goto err_out;
3596
3597 err = cnic_resolve_addr(csk, saddr);
3598 if (!err)
3599 return 0;
3600
3601err_out:
3602 clear_bit(SK_F_CONNECT_START, &csk->flags);
3603 return err;
3604}
3605
3606static int cnic_cm_abort(struct cnic_sock *csk)
3607{
3608 struct cnic_local *cp = csk->dev->cnic_priv;
Michael Chan7b34a462010-06-15 08:57:03 +00003609 u32 opcode = L4_KCQE_OPCODE_VALUE_RESET_COMP;
Michael Chana4636962009-06-08 18:14:43 -07003610
3611 if (!cnic_in_use(csk))
3612 return -EINVAL;
3613
3614 if (cnic_abort_prep(csk))
3615 return cnic_cm_abort_req(csk);
3616
3617 /* Getting here means that we haven't started connect, or
3618 * connect was not successful.
3619 */
3620
Michael Chana4636962009-06-08 18:14:43 -07003621 cp->close_conn(csk, opcode);
Michael Chan7b34a462010-06-15 08:57:03 +00003622 if (csk->state != opcode)
3623 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003624
3625 return 0;
3626}
3627
3628static int cnic_cm_close(struct cnic_sock *csk)
3629{
3630 if (!cnic_in_use(csk))
3631 return -EINVAL;
3632
3633 if (cnic_close_prep(csk)) {
3634 csk->state = L4_KCQE_OPCODE_VALUE_CLOSE_COMP;
3635 return cnic_cm_close_req(csk);
Michael Chaned99daa52010-06-15 08:57:00 +00003636 } else {
3637 return -EALREADY;
Michael Chana4636962009-06-08 18:14:43 -07003638 }
3639 return 0;
3640}
3641
3642static void cnic_cm_upcall(struct cnic_local *cp, struct cnic_sock *csk,
3643 u8 opcode)
3644{
3645 struct cnic_ulp_ops *ulp_ops;
3646 int ulp_type = csk->ulp_type;
3647
3648 rcu_read_lock();
3649 ulp_ops = rcu_dereference(cp->ulp_ops[ulp_type]);
3650 if (ulp_ops) {
3651 if (opcode == L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE)
3652 ulp_ops->cm_connect_complete(csk);
3653 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3654 ulp_ops->cm_close_complete(csk);
3655 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED)
3656 ulp_ops->cm_remote_abort(csk);
3657 else if (opcode == L4_KCQE_OPCODE_VALUE_RESET_COMP)
3658 ulp_ops->cm_abort_complete(csk);
3659 else if (opcode == L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED)
3660 ulp_ops->cm_remote_close(csk);
3661 }
3662 rcu_read_unlock();
3663}
3664
3665static int cnic_cm_set_pg(struct cnic_sock *csk)
3666{
3667 if (cnic_offld_prep(csk)) {
3668 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3669 cnic_cm_update_pg(csk);
3670 else
3671 cnic_cm_offload_pg(csk);
3672 }
3673 return 0;
3674}
3675
3676static void cnic_cm_process_offld_pg(struct cnic_dev *dev, struct l4_kcq *kcqe)
3677{
3678 struct cnic_local *cp = dev->cnic_priv;
3679 u32 l5_cid = kcqe->pg_host_opaque;
3680 u8 opcode = kcqe->op_code;
3681 struct cnic_sock *csk = &cp->csk_tbl[l5_cid];
3682
3683 csk_hold(csk);
3684 if (!cnic_in_use(csk))
3685 goto done;
3686
3687 if (opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3688 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3689 goto done;
3690 }
Eddie Waia9736c02010-02-24 14:42:04 +00003691 /* Possible PG kcqe status: SUCCESS, OFFLOADED_PG, or CTX_ALLOC_FAIL */
3692 if (kcqe->status == L4_KCQE_COMPLETION_STATUS_CTX_ALLOC_FAIL) {
3693 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3694 cnic_cm_upcall(cp, csk,
3695 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3696 goto done;
3697 }
3698
Michael Chana4636962009-06-08 18:14:43 -07003699 csk->pg_cid = kcqe->pg_cid;
3700 set_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags);
3701 cnic_cm_conn_req(csk);
3702
3703done:
3704 csk_put(csk);
3705}
3706
Michael Chane1928c82010-12-23 07:43:04 +00003707static void cnic_process_fcoe_term_conn(struct cnic_dev *dev, struct kcqe *kcqe)
3708{
3709 struct cnic_local *cp = dev->cnic_priv;
3710 struct fcoe_kcqe *fc_kcqe = (struct fcoe_kcqe *) kcqe;
3711 u32 l5_cid = fc_kcqe->fcoe_conn_id + BNX2X_FCOE_L5_CID_BASE;
3712 struct cnic_context *ctx = &cp->ctx_tbl[l5_cid];
3713
3714 ctx->timestamp = jiffies;
3715 ctx->wait_cond = 1;
3716 wake_up(&ctx->waitq);
3717}
3718
Michael Chana4636962009-06-08 18:14:43 -07003719static void cnic_cm_process_kcqe(struct cnic_dev *dev, struct kcqe *kcqe)
3720{
3721 struct cnic_local *cp = dev->cnic_priv;
3722 struct l4_kcq *l4kcqe = (struct l4_kcq *) kcqe;
3723 u8 opcode = l4kcqe->op_code;
3724 u32 l5_cid;
3725 struct cnic_sock *csk;
3726
Michael Chane1928c82010-12-23 07:43:04 +00003727 if (opcode == FCOE_RAMROD_CMD_ID_TERMINATE_CONN) {
3728 cnic_process_fcoe_term_conn(dev, kcqe);
3729 return;
3730 }
Michael Chana4636962009-06-08 18:14:43 -07003731 if (opcode == L4_KCQE_OPCODE_VALUE_OFFLOAD_PG ||
3732 opcode == L4_KCQE_OPCODE_VALUE_UPDATE_PG) {
3733 cnic_cm_process_offld_pg(dev, l4kcqe);
3734 return;
3735 }
3736
3737 l5_cid = l4kcqe->conn_id;
3738 if (opcode & 0x80)
3739 l5_cid = l4kcqe->cid;
3740 if (l5_cid >= MAX_CM_SK_TBL_SZ)
3741 return;
3742
3743 csk = &cp->csk_tbl[l5_cid];
3744 csk_hold(csk);
3745
3746 if (!cnic_in_use(csk)) {
3747 csk_put(csk);
3748 return;
3749 }
3750
3751 switch (opcode) {
Eddie Waia9736c02010-02-24 14:42:04 +00003752 case L5CM_RAMROD_CMD_ID_TCP_CONNECT:
3753 if (l4kcqe->status != 0) {
3754 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3755 cnic_cm_upcall(cp, csk,
3756 L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE);
3757 }
3758 break;
Michael Chana4636962009-06-08 18:14:43 -07003759 case L4_KCQE_OPCODE_VALUE_CONNECT_COMPLETE:
3760 if (l4kcqe->status == 0)
3761 set_bit(SK_F_OFFLD_COMPLETE, &csk->flags);
3762
3763 smp_mb__before_clear_bit();
3764 clear_bit(SK_F_OFFLD_SCHED, &csk->flags);
3765 cnic_cm_upcall(cp, csk, opcode);
3766 break;
3767
3768 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
Michael Chana4636962009-06-08 18:14:43 -07003769 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3770 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan71034ba2009-10-10 13:46:59 +00003771 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3772 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
Michael Chana4636962009-06-08 18:14:43 -07003773 cp->close_conn(csk, opcode);
3774 break;
3775
3776 case L4_KCQE_OPCODE_VALUE_CLOSE_RECEIVED:
Michael Chan101c40c2011-06-08 19:29:33 +00003777 /* after we already sent CLOSE_REQ */
3778 if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags) &&
3779 !test_bit(SK_F_OFFLD_COMPLETE, &csk->flags) &&
3780 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP)
3781 cp->close_conn(csk, L4_KCQE_OPCODE_VALUE_RESET_COMP);
3782 else
3783 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07003784 break;
3785 }
3786 csk_put(csk);
3787}
3788
3789static void cnic_cm_indicate_kcqe(void *data, struct kcqe *kcqe[], u32 num)
3790{
3791 struct cnic_dev *dev = data;
3792 int i;
3793
3794 for (i = 0; i < num; i++)
3795 cnic_cm_process_kcqe(dev, kcqe[i]);
3796}
3797
3798static struct cnic_ulp_ops cm_ulp_ops = {
3799 .indicate_kcqes = cnic_cm_indicate_kcqe,
3800};
3801
3802static void cnic_cm_free_mem(struct cnic_dev *dev)
3803{
3804 struct cnic_local *cp = dev->cnic_priv;
3805
3806 kfree(cp->csk_tbl);
3807 cp->csk_tbl = NULL;
3808 cnic_free_id_tbl(&cp->csk_port_tbl);
3809}
3810
3811static int cnic_cm_alloc_mem(struct cnic_dev *dev)
3812{
3813 struct cnic_local *cp = dev->cnic_priv;
Eddie Wai11f23aa2011-06-08 19:29:34 +00003814 u32 port_id;
Michael Chana4636962009-06-08 18:14:43 -07003815
3816 cp->csk_tbl = kzalloc(sizeof(struct cnic_sock) * MAX_CM_SK_TBL_SZ,
3817 GFP_KERNEL);
3818 if (!cp->csk_tbl)
3819 return -ENOMEM;
3820
Eddie Wai11f23aa2011-06-08 19:29:34 +00003821 get_random_bytes(&port_id, sizeof(port_id));
3822 port_id %= CNIC_LOCAL_PORT_RANGE;
Michael Chana4636962009-06-08 18:14:43 -07003823 if (cnic_init_id_tbl(&cp->csk_port_tbl, CNIC_LOCAL_PORT_RANGE,
Eddie Wai11f23aa2011-06-08 19:29:34 +00003824 CNIC_LOCAL_PORT_MIN, port_id)) {
Michael Chana4636962009-06-08 18:14:43 -07003825 cnic_cm_free_mem(dev);
3826 return -ENOMEM;
3827 }
3828 return 0;
3829}
3830
3831static int cnic_ready_to_close(struct cnic_sock *csk, u32 opcode)
3832{
Michael Chan943189f2010-06-15 08:57:02 +00003833 if (test_and_clear_bit(SK_F_OFFLD_COMPLETE, &csk->flags)) {
3834 /* Unsolicited RESET_COMP or RESET_RECEIVED */
3835 opcode = L4_KCQE_OPCODE_VALUE_RESET_RECEIVED;
3836 csk->state = opcode;
Michael Chana1e621b2010-06-15 08:57:01 +00003837 }
Michael Chan943189f2010-06-15 08:57:02 +00003838
3839 /* 1. If event opcode matches the expected event in csk->state
Michael Chan101c40c2011-06-08 19:29:33 +00003840 * 2. If the expected event is CLOSE_COMP or RESET_COMP, we accept any
3841 * event
Michael Chan7b34a462010-06-15 08:57:03 +00003842 * 3. If the expected event is 0, meaning the connection was never
3843 * never established, we accept the opcode from cm_abort.
Michael Chan943189f2010-06-15 08:57:02 +00003844 */
Michael Chan7b34a462010-06-15 08:57:03 +00003845 if (opcode == csk->state || csk->state == 0 ||
Michael Chan101c40c2011-06-08 19:29:33 +00003846 csk->state == L4_KCQE_OPCODE_VALUE_CLOSE_COMP ||
3847 csk->state == L4_KCQE_OPCODE_VALUE_RESET_COMP) {
Michael Chan7b34a462010-06-15 08:57:03 +00003848 if (!test_and_set_bit(SK_F_CLOSING, &csk->flags)) {
3849 if (csk->state == 0)
3850 csk->state = opcode;
Michael Chana4636962009-06-08 18:14:43 -07003851 return 1;
Michael Chan7b34a462010-06-15 08:57:03 +00003852 }
Michael Chana4636962009-06-08 18:14:43 -07003853 }
3854 return 0;
3855}
3856
3857static void cnic_close_bnx2_conn(struct cnic_sock *csk, u32 opcode)
3858{
3859 struct cnic_dev *dev = csk->dev;
3860 struct cnic_local *cp = dev->cnic_priv;
3861
Michael Chana1e621b2010-06-15 08:57:01 +00003862 if (opcode == L4_KCQE_OPCODE_VALUE_RESET_RECEIVED) {
3863 cnic_cm_upcall(cp, csk, opcode);
3864 return;
3865 }
3866
Michael Chana4636962009-06-08 18:14:43 -07003867 clear_bit(SK_F_CONNECT_START, &csk->flags);
Eddie Wai66883e92010-02-24 14:42:05 +00003868 cnic_close_conn(csk);
Michael Chan7b34a462010-06-15 08:57:03 +00003869 csk->state = opcode;
Eddie Wai66883e92010-02-24 14:42:05 +00003870 cnic_cm_upcall(cp, csk, opcode);
Michael Chana4636962009-06-08 18:14:43 -07003871}
3872
3873static void cnic_cm_stop_bnx2_hw(struct cnic_dev *dev)
3874{
3875}
3876
3877static int cnic_cm_init_bnx2_hw(struct cnic_dev *dev)
3878{
3879 u32 seed;
3880
3881 get_random_bytes(&seed, 4);
3882 cnic_ctx_wr(dev, 45, 0, seed);
3883 return 0;
3884}
3885
Michael Chan71034ba2009-10-10 13:46:59 +00003886static void cnic_close_bnx2x_conn(struct cnic_sock *csk, u32 opcode)
3887{
3888 struct cnic_dev *dev = csk->dev;
3889 struct cnic_local *cp = dev->cnic_priv;
3890 struct cnic_context *ctx = &cp->ctx_tbl[csk->l5_cid];
3891 union l5cm_specific_data l5_data;
3892 u32 cmd = 0;
3893 int close_complete = 0;
3894
3895 switch (opcode) {
3896 case L4_KCQE_OPCODE_VALUE_RESET_RECEIVED:
3897 case L4_KCQE_OPCODE_VALUE_CLOSE_COMP:
3898 case L4_KCQE_OPCODE_VALUE_RESET_COMP:
Michael Chan7b34a462010-06-15 08:57:03 +00003899 if (cnic_ready_to_close(csk, opcode)) {
3900 if (test_bit(SK_F_PG_OFFLD_COMPLETE, &csk->flags))
3901 cmd = L5CM_RAMROD_CMD_ID_SEARCHER_DELETE;
3902 else
3903 close_complete = 1;
3904 }
Michael Chan71034ba2009-10-10 13:46:59 +00003905 break;
3906 case L5CM_RAMROD_CMD_ID_SEARCHER_DELETE:
3907 cmd = L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD;
3908 break;
3909 case L5CM_RAMROD_CMD_ID_TERMINATE_OFFLOAD:
3910 close_complete = 1;
3911 break;
3912 }
3913 if (cmd) {
3914 memset(&l5_data, 0, sizeof(l5_data));
3915
3916 cnic_submit_kwqe_16(dev, cmd, csk->cid, ISCSI_CONNECTION_TYPE,
3917 &l5_data);
3918 } else if (close_complete) {
3919 ctx->timestamp = jiffies;
3920 cnic_close_conn(csk);
3921 cnic_cm_upcall(cp, csk, csk->state);
3922 }
3923}
3924
3925static void cnic_cm_stop_bnx2x_hw(struct cnic_dev *dev)
3926{
Michael Chanfdf24082010-10-13 14:06:47 +00003927 struct cnic_local *cp = dev->cnic_priv;
3928 int i;
3929
3930 if (!cp->ctx_tbl)
3931 return;
3932
3933 if (!netif_running(dev->netdev))
3934 return;
3935
3936 for (i = 0; i < cp->max_cid_space; i++) {
3937 struct cnic_context *ctx = &cp->ctx_tbl[i];
3938
3939 while (test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
3940 msleep(10);
3941
3942 if (test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags))
3943 netdev_warn(dev->netdev, "CID %x not deleted\n",
3944 ctx->cid);
3945 }
3946
3947 cancel_delayed_work(&cp->delete_task);
3948 flush_workqueue(cnic_wq);
3949
3950 if (atomic_read(&cp->iscsi_conn) != 0)
3951 netdev_warn(dev->netdev, "%d iSCSI connections not destroyed\n",
3952 atomic_read(&cp->iscsi_conn));
Michael Chan71034ba2009-10-10 13:46:59 +00003953}
3954
3955static int cnic_cm_init_bnx2x_hw(struct cnic_dev *dev)
3956{
3957 struct cnic_local *cp = dev->cnic_priv;
Michael Chan14203982010-10-06 03:16:06 +00003958 u32 pfid = cp->pfid;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003959 u32 port = CNIC_PORT(cp);
Michael Chan71034ba2009-10-10 13:46:59 +00003960
3961 cnic_init_bnx2x_mac(dev);
3962 cnic_bnx2x_set_tcp_timestamp(dev, 1);
3963
3964 CNIC_WR16(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003965 XSTORM_ISCSI_LOCAL_VLAN_OFFSET(pfid), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00003966
3967 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003968 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_ENABLED_OFFSET(port), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00003969 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00003970 XSTORM_TCP_GLOBAL_DEL_ACK_COUNTER_MAX_COUNT_OFFSET(port),
Michael Chan71034ba2009-10-10 13:46:59 +00003971 DEF_MAX_DA_COUNT);
3972
3973 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003974 XSTORM_ISCSI_TCP_VARS_TTL_OFFSET(pfid), DEF_TTL);
Michael Chan71034ba2009-10-10 13:46:59 +00003975 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003976 XSTORM_ISCSI_TCP_VARS_TOS_OFFSET(pfid), DEF_TOS);
Michael Chan71034ba2009-10-10 13:46:59 +00003977 CNIC_WR8(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003978 XSTORM_ISCSI_TCP_VARS_ADV_WND_SCL_OFFSET(pfid), 2);
Michael Chan71034ba2009-10-10 13:46:59 +00003979 CNIC_WR(dev, BAR_XSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00003980 XSTORM_TCP_TX_SWS_TIMER_VAL_OFFSET(pfid), DEF_SWS_TIMER);
Michael Chan71034ba2009-10-10 13:46:59 +00003981
Michael Chan14203982010-10-06 03:16:06 +00003982 CNIC_WR(dev, BAR_TSTRORM_INTMEM + TSTORM_TCP_MAX_CWND_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00003983 DEF_MAX_CWND);
3984 return 0;
3985}
3986
Michael Chanfdf24082010-10-13 14:06:47 +00003987static void cnic_delete_task(struct work_struct *work)
3988{
3989 struct cnic_local *cp;
3990 struct cnic_dev *dev;
3991 u32 i;
3992 int need_resched = 0;
3993
3994 cp = container_of(work, struct cnic_local, delete_task.work);
3995 dev = cp->dev;
3996
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07003997 if (test_and_clear_bit(CNIC_LCL_FL_STOP_ISCSI, &cp->cnic_local_flags)) {
3998 struct drv_ctl_info info;
3999
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07004000 cnic_ulp_stop_one(cp, CNIC_ULP_ISCSI);
Dmitry Kravkovfab0dc82011-03-31 17:04:22 -07004001
4002 info.cmd = DRV_CTL_ISCSI_STOPPED_CMD;
4003 cp->ethdev->drv_ctl(dev->netdev, &info);
4004 }
4005
Michael Chanfdf24082010-10-13 14:06:47 +00004006 for (i = 0; i < cp->max_cid_space; i++) {
4007 struct cnic_context *ctx = &cp->ctx_tbl[i];
4008
4009 if (!test_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags) ||
4010 !test_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
4011 continue;
4012
4013 if (!time_after(jiffies, ctx->timestamp + (2 * HZ))) {
4014 need_resched = 1;
4015 continue;
4016 }
4017
4018 if (!test_and_clear_bit(CTX_FL_DELETE_WAIT, &ctx->ctx_flags))
4019 continue;
4020
4021 cnic_bnx2x_destroy_ramrod(dev, i);
4022
4023 cnic_free_bnx2x_conn_resc(dev, i);
4024 if (ctx->ulp_proto_id == CNIC_ULP_ISCSI)
4025 atomic_dec(&cp->iscsi_conn);
4026
4027 clear_bit(CTX_FL_OFFLD_START, &ctx->ctx_flags);
4028 }
4029
4030 if (need_resched)
4031 queue_delayed_work(cnic_wq, &cp->delete_task,
4032 msecs_to_jiffies(10));
4033
4034}
4035
Michael Chana4636962009-06-08 18:14:43 -07004036static int cnic_cm_open(struct cnic_dev *dev)
4037{
4038 struct cnic_local *cp = dev->cnic_priv;
4039 int err;
4040
4041 err = cnic_cm_alloc_mem(dev);
4042 if (err)
4043 return err;
4044
4045 err = cp->start_cm(dev);
4046
4047 if (err)
4048 goto err_out;
4049
Michael Chanfdf24082010-10-13 14:06:47 +00004050 INIT_DELAYED_WORK(&cp->delete_task, cnic_delete_task);
4051
Michael Chana4636962009-06-08 18:14:43 -07004052 dev->cm_create = cnic_cm_create;
4053 dev->cm_destroy = cnic_cm_destroy;
4054 dev->cm_connect = cnic_cm_connect;
4055 dev->cm_abort = cnic_cm_abort;
4056 dev->cm_close = cnic_cm_close;
4057 dev->cm_select_dev = cnic_cm_select_dev;
4058
4059 cp->ulp_handle[CNIC_ULP_L4] = dev;
4060 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], &cm_ulp_ops);
4061 return 0;
4062
4063err_out:
4064 cnic_cm_free_mem(dev);
4065 return err;
4066}
4067
4068static int cnic_cm_shutdown(struct cnic_dev *dev)
4069{
4070 struct cnic_local *cp = dev->cnic_priv;
4071 int i;
4072
4073 cp->stop_cm(dev);
4074
4075 if (!cp->csk_tbl)
4076 return 0;
4077
4078 for (i = 0; i < MAX_CM_SK_TBL_SZ; i++) {
4079 struct cnic_sock *csk = &cp->csk_tbl[i];
4080
4081 clear_bit(SK_F_INUSE, &csk->flags);
4082 cnic_cm_cleanup(csk);
4083 }
4084 cnic_cm_free_mem(dev);
4085
4086 return 0;
4087}
4088
4089static void cnic_init_context(struct cnic_dev *dev, u32 cid)
4090{
Michael Chana4636962009-06-08 18:14:43 -07004091 u32 cid_addr;
4092 int i;
4093
Michael Chana4636962009-06-08 18:14:43 -07004094 cid_addr = GET_CID_ADDR(cid);
4095
4096 for (i = 0; i < CTX_SIZE; i += 4)
4097 cnic_ctx_wr(dev, cid_addr, i, 0);
4098}
4099
4100static int cnic_setup_5709_context(struct cnic_dev *dev, int valid)
4101{
4102 struct cnic_local *cp = dev->cnic_priv;
4103 int ret = 0, i;
4104 u32 valid_bit = valid ? BNX2_CTX_HOST_PAGE_TBL_DATA0_VALID : 0;
4105
4106 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4107 return 0;
4108
4109 for (i = 0; i < cp->ctx_blks; i++) {
4110 int j;
4111 u32 idx = cp->ctx_arr[i].cid / cp->cids_per_blk;
4112 u32 val;
4113
4114 memset(cp->ctx_arr[i].ctx, 0, BCM_PAGE_SIZE);
4115
4116 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA0,
4117 (cp->ctx_arr[i].mapping & 0xffffffff) | valid_bit);
4118 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_DATA1,
4119 (u64) cp->ctx_arr[i].mapping >> 32);
4120 CNIC_WR(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL, idx |
4121 BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ);
4122 for (j = 0; j < 10; j++) {
4123
4124 val = CNIC_RD(dev, BNX2_CTX_HOST_PAGE_TBL_CTRL);
4125 if (!(val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ))
4126 break;
4127 udelay(5);
4128 }
4129 if (val & BNX2_CTX_HOST_PAGE_TBL_CTRL_WRITE_REQ) {
4130 ret = -EBUSY;
4131 break;
4132 }
4133 }
4134 return ret;
4135}
4136
4137static void cnic_free_irq(struct cnic_dev *dev)
4138{
4139 struct cnic_local *cp = dev->cnic_priv;
4140 struct cnic_eth_dev *ethdev = cp->ethdev;
4141
4142 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4143 cp->disable_int_sync(dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004144 tasklet_kill(&cp->cnic_irq_task);
Michael Chana4636962009-06-08 18:14:43 -07004145 free_irq(ethdev->irq_arr[0].vector, dev);
4146 }
4147}
4148
Michael Chan6e0dc642010-10-13 14:06:44 +00004149static int cnic_request_irq(struct cnic_dev *dev)
4150{
4151 struct cnic_local *cp = dev->cnic_priv;
4152 struct cnic_eth_dev *ethdev = cp->ethdev;
4153 int err;
4154
4155 err = request_irq(ethdev->irq_arr[0].vector, cnic_irq, 0, "cnic", dev);
4156 if (err)
4157 tasklet_disable(&cp->cnic_irq_task);
4158
4159 return err;
4160}
4161
Michael Chana4636962009-06-08 18:14:43 -07004162static int cnic_init_bnx2_irq(struct cnic_dev *dev)
4163{
4164 struct cnic_local *cp = dev->cnic_priv;
4165 struct cnic_eth_dev *ethdev = cp->ethdev;
4166
4167 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
4168 int err, i = 0;
4169 int sblk_num = cp->status_blk_num;
4170 u32 base = ((sblk_num - 1) * BNX2_HC_SB_CONFIG_SIZE) +
4171 BNX2_HC_SB_CONFIG_1;
4172
4173 CNIC_WR(dev, base, BNX2_HC_SB_CONFIG_1_ONE_SHOT);
4174
4175 CNIC_WR(dev, base + BNX2_HC_COMP_PROD_TRIP_OFF, (2 << 16) | 8);
4176 CNIC_WR(dev, base + BNX2_HC_COM_TICKS_OFF, (64 << 16) | 220);
4177 CNIC_WR(dev, base + BNX2_HC_CMD_TICKS_OFF, (64 << 16) | 220);
4178
Michael Chana4dde3a2010-02-24 14:42:08 +00004179 cp->last_status_idx = cp->status_blk.bnx2->status_idx;
Joe Perches164165d2009-11-19 09:30:10 +00004180 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2_msix,
Michael Chana4636962009-06-08 18:14:43 -07004181 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004182 err = cnic_request_irq(dev);
4183 if (err)
Michael Chana4636962009-06-08 18:14:43 -07004184 return err;
Michael Chan6e0dc642010-10-13 14:06:44 +00004185
Michael Chana4dde3a2010-02-24 14:42:08 +00004186 while (cp->status_blk.bnx2->status_completion_producer_index &&
Michael Chana4636962009-06-08 18:14:43 -07004187 i < 10) {
4188 CNIC_WR(dev, BNX2_HC_COALESCE_NOW,
4189 1 << (11 + sblk_num));
4190 udelay(10);
4191 i++;
4192 barrier();
4193 }
Michael Chana4dde3a2010-02-24 14:42:08 +00004194 if (cp->status_blk.bnx2->status_completion_producer_index) {
Michael Chana4636962009-06-08 18:14:43 -07004195 cnic_free_irq(dev);
4196 goto failed;
4197 }
4198
4199 } else {
Michael Chana4dde3a2010-02-24 14:42:08 +00004200 struct status_block *sblk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004201 u32 hc_cmd = CNIC_RD(dev, BNX2_HC_COMMAND);
4202 int i = 0;
4203
4204 while (sblk->status_completion_producer_index && i < 10) {
4205 CNIC_WR(dev, BNX2_HC_COMMAND,
4206 hc_cmd | BNX2_HC_COMMAND_COAL_NOW_WO_INT);
4207 udelay(10);
4208 i++;
4209 barrier();
4210 }
4211 if (sblk->status_completion_producer_index)
4212 goto failed;
4213
4214 }
4215 return 0;
4216
4217failed:
Joe Perchesddf79b22010-02-17 15:01:54 +00004218 netdev_err(dev->netdev, "KCQ index not resetting to 0\n");
Michael Chana4636962009-06-08 18:14:43 -07004219 return -EBUSY;
4220}
4221
4222static void cnic_enable_bnx2_int(struct cnic_dev *dev)
4223{
4224 struct cnic_local *cp = dev->cnic_priv;
4225 struct cnic_eth_dev *ethdev = cp->ethdev;
4226
4227 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4228 return;
4229
4230 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4231 BNX2_PCICFG_INT_ACK_CMD_INDEX_VALID | cp->last_status_idx);
4232}
4233
4234static void cnic_disable_bnx2_int_sync(struct cnic_dev *dev)
4235{
4236 struct cnic_local *cp = dev->cnic_priv;
4237 struct cnic_eth_dev *ethdev = cp->ethdev;
4238
4239 if (!(ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX))
4240 return;
4241
4242 CNIC_WR(dev, BNX2_PCICFG_INT_ACK_CMD, cp->int_num |
4243 BNX2_PCICFG_INT_ACK_CMD_MASK_INT);
4244 CNIC_RD(dev, BNX2_PCICFG_INT_ACK_CMD);
4245 synchronize_irq(ethdev->irq_arr[0].vector);
4246}
4247
4248static void cnic_init_bnx2_tx_ring(struct cnic_dev *dev)
4249{
4250 struct cnic_local *cp = dev->cnic_priv;
4251 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004252 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004253 u32 cid_addr, tx_cid, sb_id;
4254 u32 val, offset0, offset1, offset2, offset3;
4255 int i;
4256 struct tx_bd *txbd;
Michael Chancd801532010-10-13 14:06:49 +00004257 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Michael Chana4dde3a2010-02-24 14:42:08 +00004258 struct status_block *s_blk = cp->status_blk.gen;
Michael Chana4636962009-06-08 18:14:43 -07004259
4260 sb_id = cp->status_blk_num;
4261 tx_cid = 20;
Michael Chana4636962009-06-08 18:14:43 -07004262 cp->tx_cons_ptr = &s_blk->status_tx_quick_consumer_index2;
4263 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004264 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004265
4266 tx_cid = TX_TSS_CID + sb_id - 1;
Michael Chana4636962009-06-08 18:14:43 -07004267 CNIC_WR(dev, BNX2_TSCH_TSS_CFG, (sb_id << 24) |
4268 (TX_TSS_CID << 7));
4269 cp->tx_cons_ptr = &sblk->status_tx_quick_consumer_index;
4270 }
4271 cp->tx_cons = *cp->tx_cons_ptr;
4272
4273 cid_addr = GET_CID_ADDR(tx_cid);
4274 if (CHIP_NUM(cp) == CHIP_NUM_5709) {
4275 u32 cid_addr2 = GET_CID_ADDR(tx_cid + 4) + 0x40;
4276
4277 for (i = 0; i < PHY_CTX_SIZE; i += 4)
4278 cnic_ctx_wr(dev, cid_addr2, i, 0);
4279
4280 offset0 = BNX2_L2CTX_TYPE_XI;
4281 offset1 = BNX2_L2CTX_CMD_TYPE_XI;
4282 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI_XI;
4283 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO_XI;
4284 } else {
Michael Chanb58ffb42010-05-27 16:31:41 -07004285 cnic_init_context(dev, tx_cid);
4286 cnic_init_context(dev, tx_cid + 1);
4287
Michael Chana4636962009-06-08 18:14:43 -07004288 offset0 = BNX2_L2CTX_TYPE;
4289 offset1 = BNX2_L2CTX_CMD_TYPE;
4290 offset2 = BNX2_L2CTX_TBDR_BHADDR_HI;
4291 offset3 = BNX2_L2CTX_TBDR_BHADDR_LO;
4292 }
4293 val = BNX2_L2CTX_TYPE_TYPE_L2 | BNX2_L2CTX_TYPE_SIZE_L2;
4294 cnic_ctx_wr(dev, cid_addr, offset0, val);
4295
4296 val = BNX2_L2CTX_CMD_TYPE_TYPE_L2 | (8 << 16);
4297 cnic_ctx_wr(dev, cid_addr, offset1, val);
4298
Michael Chancd801532010-10-13 14:06:49 +00004299 txbd = (struct tx_bd *) udev->l2_ring;
Michael Chana4636962009-06-08 18:14:43 -07004300
Michael Chancd801532010-10-13 14:06:49 +00004301 buf_map = udev->l2_buf_map;
Michael Chana4636962009-06-08 18:14:43 -07004302 for (i = 0; i < MAX_TX_DESC_CNT; i++, txbd++) {
4303 txbd->tx_bd_haddr_hi = (u64) buf_map >> 32;
4304 txbd->tx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4305 }
Michael Chancd801532010-10-13 14:06:49 +00004306 val = (u64) ring_map >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004307 cnic_ctx_wr(dev, cid_addr, offset2, val);
4308 txbd->tx_bd_haddr_hi = val;
4309
Michael Chancd801532010-10-13 14:06:49 +00004310 val = (u64) ring_map & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004311 cnic_ctx_wr(dev, cid_addr, offset3, val);
4312 txbd->tx_bd_haddr_lo = val;
4313}
4314
4315static void cnic_init_bnx2_rx_ring(struct cnic_dev *dev)
4316{
4317 struct cnic_local *cp = dev->cnic_priv;
4318 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chancd801532010-10-13 14:06:49 +00004319 struct cnic_uio_dev *udev = cp->udev;
Michael Chana4636962009-06-08 18:14:43 -07004320 u32 cid_addr, sb_id, val, coal_reg, coal_val;
4321 int i;
4322 struct rx_bd *rxbd;
Michael Chana4dde3a2010-02-24 14:42:08 +00004323 struct status_block *s_blk = cp->status_blk.gen;
Michael Chancd801532010-10-13 14:06:49 +00004324 dma_addr_t ring_map = udev->l2_ring_map;
Michael Chana4636962009-06-08 18:14:43 -07004325
4326 sb_id = cp->status_blk_num;
4327 cnic_init_context(dev, 2);
4328 cp->rx_cons_ptr = &s_blk->status_rx_quick_consumer_index2;
4329 coal_reg = BNX2_HC_COMMAND;
4330 coal_val = CNIC_RD(dev, coal_reg);
4331 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chana4dde3a2010-02-24 14:42:08 +00004332 struct status_block_msix *sblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004333
4334 cp->rx_cons_ptr = &sblk->status_rx_quick_consumer_index;
4335 coal_reg = BNX2_HC_COALESCE_NOW;
4336 coal_val = 1 << (11 + sb_id);
4337 }
4338 i = 0;
4339 while (!(*cp->rx_cons_ptr != 0) && i < 10) {
4340 CNIC_WR(dev, coal_reg, coal_val);
4341 udelay(10);
4342 i++;
4343 barrier();
4344 }
4345 cp->rx_cons = *cp->rx_cons_ptr;
4346
4347 cid_addr = GET_CID_ADDR(2);
4348 val = BNX2_L2CTX_CTX_TYPE_CTX_BD_CHN_TYPE_VALUE |
4349 BNX2_L2CTX_CTX_TYPE_SIZE_L2 | (0x02 << 8);
4350 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_CTX_TYPE, val);
4351
4352 if (sb_id == 0)
Michael Chand0549382009-10-28 03:41:59 -07004353 val = 2 << BNX2_L2CTX_L2_STATUSB_NUM_SHIFT;
Michael Chana4636962009-06-08 18:14:43 -07004354 else
Michael Chand0549382009-10-28 03:41:59 -07004355 val = BNX2_L2CTX_L2_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004356 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_HOST_BDIDX, val);
4357
Michael Chancd801532010-10-13 14:06:49 +00004358 rxbd = (struct rx_bd *) (udev->l2_ring + BCM_PAGE_SIZE);
Michael Chana4636962009-06-08 18:14:43 -07004359 for (i = 0; i < MAX_RX_DESC_CNT; i++, rxbd++) {
4360 dma_addr_t buf_map;
4361 int n = (i % cp->l2_rx_ring_size) + 1;
4362
Michael Chancd801532010-10-13 14:06:49 +00004363 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chana4636962009-06-08 18:14:43 -07004364 rxbd->rx_bd_len = cp->l2_single_buf_size;
4365 rxbd->rx_bd_flags = RX_BD_FLAGS_START | RX_BD_FLAGS_END;
4366 rxbd->rx_bd_haddr_hi = (u64) buf_map >> 32;
4367 rxbd->rx_bd_haddr_lo = (u64) buf_map & 0xffffffff;
4368 }
Michael Chancd801532010-10-13 14:06:49 +00004369 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chana4636962009-06-08 18:14:43 -07004370 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_HI, val);
4371 rxbd->rx_bd_haddr_hi = val;
4372
Michael Chancd801532010-10-13 14:06:49 +00004373 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chana4636962009-06-08 18:14:43 -07004374 cnic_ctx_wr(dev, cid_addr, BNX2_L2CTX_NX_BDHADDR_LO, val);
4375 rxbd->rx_bd_haddr_lo = val;
4376
4377 val = cnic_reg_rd_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD);
4378 cnic_reg_wr_ind(dev, BNX2_RXP_SCRATCH_RXP_FLOOD, val | (1 << 2));
4379}
4380
4381static void cnic_shutdown_bnx2_rx_ring(struct cnic_dev *dev)
4382{
4383 struct kwqe *wqes[1], l2kwqe;
4384
4385 memset(&l2kwqe, 0, sizeof(l2kwqe));
4386 wqes[0] = &l2kwqe;
Michael Chane1928c82010-12-23 07:43:04 +00004387 l2kwqe.kwqe_op_flag = (L2_LAYER_CODE << KWQE_LAYER_SHIFT) |
Michael Chana4636962009-06-08 18:14:43 -07004388 (L2_KWQE_OPCODE_VALUE_FLUSH <<
4389 KWQE_OPCODE_SHIFT) | 2;
4390 dev->submit_kwqes(dev, wqes, 1);
4391}
4392
4393static void cnic_set_bnx2_mac(struct cnic_dev *dev)
4394{
4395 struct cnic_local *cp = dev->cnic_priv;
4396 u32 val;
4397
4398 val = cp->func << 2;
4399
4400 cp->shmem_base = cnic_reg_rd_ind(dev, BNX2_SHM_HDR_ADDR_0 + val);
4401
4402 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4403 BNX2_PORT_HW_CFG_ISCSI_MAC_UPPER);
4404 dev->mac_addr[0] = (u8) (val >> 8);
4405 dev->mac_addr[1] = (u8) val;
4406
4407 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH4, val);
4408
4409 val = cnic_reg_rd_ind(dev, cp->shmem_base +
4410 BNX2_PORT_HW_CFG_ISCSI_MAC_LOWER);
4411 dev->mac_addr[2] = (u8) (val >> 24);
4412 dev->mac_addr[3] = (u8) (val >> 16);
4413 dev->mac_addr[4] = (u8) (val >> 8);
4414 dev->mac_addr[5] = (u8) val;
4415
4416 CNIC_WR(dev, BNX2_EMAC_MAC_MATCH5, val);
4417
4418 val = 4 | BNX2_RPM_SORT_USER2_BC_EN;
4419 if (CHIP_NUM(cp) != CHIP_NUM_5709)
4420 val |= BNX2_RPM_SORT_USER2_PROM_VLAN;
4421
4422 CNIC_WR(dev, BNX2_RPM_SORT_USER2, 0x0);
4423 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val);
4424 CNIC_WR(dev, BNX2_RPM_SORT_USER2, val | BNX2_RPM_SORT_USER2_ENA);
4425}
4426
4427static int cnic_start_bnx2_hw(struct cnic_dev *dev)
4428{
4429 struct cnic_local *cp = dev->cnic_priv;
4430 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chana4dde3a2010-02-24 14:42:08 +00004431 struct status_block *sblk = cp->status_blk.gen;
Michael Chane6c28892010-06-24 14:58:39 +00004432 u32 val, kcq_cid_addr, kwq_cid_addr;
Michael Chana4636962009-06-08 18:14:43 -07004433 int err;
4434
4435 cnic_set_bnx2_mac(dev);
4436
4437 val = CNIC_RD(dev, BNX2_MQ_CONFIG);
4438 val &= ~BNX2_MQ_CONFIG_KNL_BYP_BLK_SIZE;
4439 if (BCM_PAGE_BITS > 12)
4440 val |= (12 - 8) << 4;
4441 else
4442 val |= (BCM_PAGE_BITS - 8) << 4;
4443
4444 CNIC_WR(dev, BNX2_MQ_CONFIG, val);
4445
4446 CNIC_WR(dev, BNX2_HC_COMP_PROD_TRIP, (2 << 16) | 8);
4447 CNIC_WR(dev, BNX2_HC_COM_TICKS, (64 << 16) | 220);
4448 CNIC_WR(dev, BNX2_HC_CMD_TICKS, (64 << 16) | 220);
4449
4450 err = cnic_setup_5709_context(dev, 1);
4451 if (err)
4452 return err;
4453
4454 cnic_init_context(dev, KWQ_CID);
4455 cnic_init_context(dev, KCQ_CID);
4456
Michael Chane6c28892010-06-24 14:58:39 +00004457 kwq_cid_addr = GET_CID_ADDR(KWQ_CID);
Michael Chana4636962009-06-08 18:14:43 -07004458 cp->kwq_io_addr = MB_GET_CID_ADDR(KWQ_CID) + L5_KRNLQ_HOST_QIDX;
4459
4460 cp->max_kwq_idx = MAX_KWQ_IDX;
4461 cp->kwq_prod_idx = 0;
4462 cp->kwq_con_idx = 0;
Michael Chan1f1332a2010-05-18 11:32:52 +00004463 set_bit(CNIC_LCL_FL_KWQ_INIT, &cp->cnic_local_flags);
Michael Chana4636962009-06-08 18:14:43 -07004464
4465 if (CHIP_NUM(cp) == CHIP_NUM_5706 || CHIP_NUM(cp) == CHIP_NUM_5708)
4466 cp->kwq_con_idx_ptr = &sblk->status_rx_quick_consumer_index15;
4467 else
4468 cp->kwq_con_idx_ptr = &sblk->status_cmd_consumer_index;
4469
4470 /* Initialize the kernel work queue context. */
4471 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4472 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004473 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004474
4475 val = (BCM_PAGE_SIZE / sizeof(struct kwqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004476 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004477
4478 val = ((BCM_PAGE_SIZE / sizeof(struct kwqe)) << 16) | KWQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004479 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004480
4481 val = (u32) ((u64) cp->kwq_info.pgtbl_map >> 32);
Michael Chane6c28892010-06-24 14:58:39 +00004482 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004483
4484 val = (u32) cp->kwq_info.pgtbl_map;
Michael Chane6c28892010-06-24 14:58:39 +00004485 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004486
Michael Chane6c28892010-06-24 14:58:39 +00004487 kcq_cid_addr = GET_CID_ADDR(KCQ_CID);
4488 cp->kcq1.io_addr = MB_GET_CID_ADDR(KCQ_CID) + L5_KRNLQ_HOST_QIDX;
Michael Chana4636962009-06-08 18:14:43 -07004489
Michael Chane6c28892010-06-24 14:58:39 +00004490 cp->kcq1.sw_prod_idx = 0;
4491 cp->kcq1.hw_prod_idx_ptr =
4492 (u16 *) &sblk->status_completion_producer_index;
4493
4494 cp->kcq1.status_idx_ptr = (u16 *) &sblk->status_idx;
Michael Chana4636962009-06-08 18:14:43 -07004495
4496 /* Initialize the kernel complete queue context. */
4497 val = KRNLQ_TYPE_TYPE_KRNLQ | KRNLQ_SIZE_TYPE_SIZE |
4498 (BCM_PAGE_BITS - 8) | KRNLQ_FLAGS_QE_SELF_SEQ;
Michael Chane6c28892010-06-24 14:58:39 +00004499 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_TYPE, val);
Michael Chana4636962009-06-08 18:14:43 -07004500
4501 val = (BCM_PAGE_SIZE / sizeof(struct kcqe) - 1) << 16;
Michael Chane6c28892010-06-24 14:58:39 +00004502 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_QE_SELF_SEQ_MAX, val);
Michael Chana4636962009-06-08 18:14:43 -07004503
4504 val = ((BCM_PAGE_SIZE / sizeof(struct kcqe)) << 16) | KCQ_PAGE_CNT;
Michael Chane6c28892010-06-24 14:58:39 +00004505 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_NPAGES, val);
Michael Chana4636962009-06-08 18:14:43 -07004506
Michael Chane6c28892010-06-24 14:58:39 +00004507 val = (u32) ((u64) cp->kcq1.dma.pgtbl_map >> 32);
4508 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_HI, val);
Michael Chana4636962009-06-08 18:14:43 -07004509
Michael Chane6c28892010-06-24 14:58:39 +00004510 val = (u32) cp->kcq1.dma.pgtbl_map;
4511 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_PGTBL_HADDR_LO, val);
Michael Chana4636962009-06-08 18:14:43 -07004512
4513 cp->int_num = 0;
4514 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX) {
Michael Chane6c28892010-06-24 14:58:39 +00004515 struct status_block_msix *msblk = cp->status_blk.bnx2;
Michael Chana4636962009-06-08 18:14:43 -07004516 u32 sb_id = cp->status_blk_num;
Michael Chand0549382009-10-28 03:41:59 -07004517 u32 sb = BNX2_L2CTX_L5_STATUSB_NUM(sb_id);
Michael Chana4636962009-06-08 18:14:43 -07004518
Michael Chane6c28892010-06-24 14:58:39 +00004519 cp->kcq1.hw_prod_idx_ptr =
4520 (u16 *) &msblk->status_completion_producer_index;
4521 cp->kcq1.status_idx_ptr = (u16 *) &msblk->status_idx;
Michael Chanb177a5d52010-06-24 14:58:41 +00004522 cp->kwq_con_idx_ptr = (u16 *) &msblk->status_cmd_consumer_index;
Michael Chana4636962009-06-08 18:14:43 -07004523 cp->int_num = sb_id << BNX2_PCICFG_INT_ACK_CMD_INT_NUM_SHIFT;
Michael Chane6c28892010-06-24 14:58:39 +00004524 cnic_ctx_wr(dev, kwq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
4525 cnic_ctx_wr(dev, kcq_cid_addr, L5_KRNLQ_HOST_QIDX, sb);
Michael Chana4636962009-06-08 18:14:43 -07004526 }
4527
4528 /* Enable Commnad Scheduler notification when we write to the
4529 * host producer index of the kernel contexts. */
4530 CNIC_WR(dev, BNX2_MQ_KNL_CMD_MASK1, 2);
4531
4532 /* Enable Command Scheduler notification when we write to either
4533 * the Send Queue or Receive Queue producer indexes of the kernel
4534 * bypass contexts. */
4535 CNIC_WR(dev, BNX2_MQ_KNL_BYP_CMD_MASK1, 7);
4536 CNIC_WR(dev, BNX2_MQ_KNL_BYP_WRITE_MASK1, 7);
4537
4538 /* Notify COM when the driver post an application buffer. */
4539 CNIC_WR(dev, BNX2_MQ_KNL_RX_V2P_MASK2, 0x2000);
4540
4541 /* Set the CP and COM doorbells. These two processors polls the
4542 * doorbell for a non zero value before running. This must be done
4543 * after setting up the kernel queue contexts. */
4544 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 1);
4545 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 1);
4546
4547 cnic_init_bnx2_tx_ring(dev);
4548 cnic_init_bnx2_rx_ring(dev);
4549
4550 err = cnic_init_bnx2_irq(dev);
4551 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00004552 netdev_err(dev->netdev, "cnic_init_irq failed\n");
Michael Chana4636962009-06-08 18:14:43 -07004553 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
4554 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
4555 return err;
4556 }
4557
4558 return 0;
4559}
4560
Michael Chan71034ba2009-10-10 13:46:59 +00004561static void cnic_setup_bnx2x_context(struct cnic_dev *dev)
4562{
4563 struct cnic_local *cp = dev->cnic_priv;
4564 struct cnic_eth_dev *ethdev = cp->ethdev;
4565 u32 start_offset = ethdev->ctx_tbl_offset;
4566 int i;
4567
4568 for (i = 0; i < cp->ctx_blks; i++) {
4569 struct cnic_ctx *ctx = &cp->ctx_arr[i];
4570 dma_addr_t map = ctx->mapping;
4571
4572 if (cp->ctx_align) {
4573 unsigned long mask = cp->ctx_align - 1;
4574
4575 map = (map + mask) & ~mask;
4576 }
4577
4578 cnic_ctx_tbl_wr(dev, start_offset + i, map);
4579 }
4580}
4581
4582static int cnic_init_bnx2x_irq(struct cnic_dev *dev)
4583{
4584 struct cnic_local *cp = dev->cnic_priv;
4585 struct cnic_eth_dev *ethdev = cp->ethdev;
4586 int err = 0;
4587
Joe Perches164165d2009-11-19 09:30:10 +00004588 tasklet_init(&cp->cnic_irq_task, cnic_service_bnx2x_bh,
Michael Chan71034ba2009-10-10 13:46:59 +00004589 (unsigned long) dev);
Michael Chan6e0dc642010-10-13 14:06:44 +00004590 if (ethdev->drv_state & CNIC_DRV_STATE_USING_MSIX)
4591 err = cnic_request_irq(dev);
4592
Michael Chan71034ba2009-10-10 13:46:59 +00004593 return err;
4594}
4595
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004596static inline void cnic_storm_memset_hc_disable(struct cnic_dev *dev,
4597 u16 sb_id, u8 sb_index,
4598 u8 disable)
4599{
4600
4601 u32 addr = BAR_CSTRORM_INTMEM +
4602 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4603 offsetof(struct hc_status_block_data_e1x, index_data) +
4604 sizeof(struct hc_index_data)*sb_index +
4605 offsetof(struct hc_index_data, flags);
4606 u16 flags = CNIC_RD16(dev, addr);
4607 /* clear and set */
4608 flags &= ~HC_INDEX_DATA_HC_ENABLED;
4609 flags |= (((~disable) << HC_INDEX_DATA_HC_ENABLED_SHIFT) &
4610 HC_INDEX_DATA_HC_ENABLED);
4611 CNIC_WR16(dev, addr, flags);
4612}
4613
Michael Chan71034ba2009-10-10 13:46:59 +00004614static void cnic_enable_bnx2x_int(struct cnic_dev *dev)
4615{
4616 struct cnic_local *cp = dev->cnic_priv;
4617 u8 sb_id = cp->status_blk_num;
Michael Chan71034ba2009-10-10 13:46:59 +00004618
4619 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004620 CSTORM_STATUS_BLOCK_DATA_OFFSET(sb_id) +
4621 offsetof(struct hc_status_block_data_e1x, index_data) +
4622 sizeof(struct hc_index_data)*HC_INDEX_ISCSI_EQ_CONS +
4623 offsetof(struct hc_index_data, timeout), 64 / 12);
4624 cnic_storm_memset_hc_disable(dev, sb_id, HC_INDEX_ISCSI_EQ_CONS, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004625}
4626
4627static void cnic_disable_bnx2x_int_sync(struct cnic_dev *dev)
4628{
4629}
4630
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004631static void cnic_init_bnx2x_tx_ring(struct cnic_dev *dev,
4632 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004633{
4634 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004635 struct cnic_uio_dev *udev = cp->udev;
4636 union eth_tx_bd_types *txbd = (union eth_tx_bd_types *) udev->l2_ring;
4637 dma_addr_t buf_map, ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004638 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004639 int port = CNIC_PORT(cp);
4640 int i;
Michael Chan5159fdc2010-12-23 07:42:59 +00004641 u32 cli = cp->ethdev->iscsi_l2_client_id;
Michael Chan71034ba2009-10-10 13:46:59 +00004642 u32 val;
4643
4644 memset(txbd, 0, BCM_PAGE_SIZE);
4645
Michael Chancd801532010-10-13 14:06:49 +00004646 buf_map = udev->l2_buf_map;
Michael Chan71034ba2009-10-10 13:46:59 +00004647 for (i = 0; i < MAX_TX_DESC_CNT; i += 3, txbd += 3) {
4648 struct eth_tx_start_bd *start_bd = &txbd->start_bd;
4649 struct eth_tx_bd *reg_bd = &((txbd + 2)->reg_bd);
4650
4651 start_bd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4652 start_bd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4653 reg_bd->addr_hi = start_bd->addr_hi;
4654 reg_bd->addr_lo = start_bd->addr_lo + 0x10;
4655 start_bd->nbytes = cpu_to_le16(0x10);
4656 start_bd->nbd = cpu_to_le16(3);
4657 start_bd->bd_flags.as_bitfield = ETH_TX_BD_FLAGS_START_BD;
4658 start_bd->general_data = (UNICAST_ADDRESS <<
4659 ETH_TX_START_BD_ETH_ADDR_TYPE_SHIFT);
4660 start_bd->general_data |= (1 << ETH_TX_START_BD_HDR_NBDS_SHIFT);
4661
4662 }
Michael Chan71034ba2009-10-10 13:46:59 +00004663
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004664 val = (u64) ring_map >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004665 txbd->next_bd.addr_hi = cpu_to_le32(val);
4666
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004667 data->tx.tx_bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004668
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004669 val = (u64) ring_map & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004670 txbd->next_bd.addr_lo = cpu_to_le32(val);
4671
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004672 data->tx.tx_bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004673
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004674 /* Other ramrod params */
4675 data->tx.tx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_CQ_CONS;
4676 data->tx.tx_status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004677
4678 /* reset xstorm per client statistics */
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004679 if (cli < MAX_STAT_COUNTER_ID) {
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004680 val = BAR_XSTRORM_INTMEM +
4681 XSTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4682 for (i = 0; i < sizeof(struct xstorm_per_client_stats) / 4; i++)
4683 CNIC_WR(dev, val + i * 4, 0);
4684 }
Michael Chan71034ba2009-10-10 13:46:59 +00004685
4686 cp->tx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004687 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_CQ_CONS];
Michael Chan71034ba2009-10-10 13:46:59 +00004688}
4689
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004690static void cnic_init_bnx2x_rx_ring(struct cnic_dev *dev,
4691 struct client_init_ramrod_data *data)
Michael Chan71034ba2009-10-10 13:46:59 +00004692{
4693 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004694 struct cnic_uio_dev *udev = cp->udev;
4695 struct eth_rx_bd *rxbd = (struct eth_rx_bd *) (udev->l2_ring +
Michael Chan71034ba2009-10-10 13:46:59 +00004696 BCM_PAGE_SIZE);
4697 struct eth_rx_cqe_next_page *rxcqe = (struct eth_rx_cqe_next_page *)
Michael Chancd801532010-10-13 14:06:49 +00004698 (udev->l2_ring + (2 * BCM_PAGE_SIZE));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004699 struct host_sp_status_block *sb = cp->bnx2x_def_status_blk;
Michael Chan71034ba2009-10-10 13:46:59 +00004700 int i;
4701 int port = CNIC_PORT(cp);
Michael Chan5159fdc2010-12-23 07:42:59 +00004702 u32 cli = cp->ethdev->iscsi_l2_client_id;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004703 int cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
Michael Chan71034ba2009-10-10 13:46:59 +00004704 u32 val;
Michael Chancd801532010-10-13 14:06:49 +00004705 dma_addr_t ring_map = udev->l2_ring_map;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004706
4707 /* General data */
4708 data->general.client_id = cli;
4709 data->general.statistics_en_flg = 1;
4710 data->general.statistics_counter_id = cli;
4711 data->general.activate_flg = 1;
4712 data->general.sp_client_id = cli;
Michael Chan71034ba2009-10-10 13:46:59 +00004713
4714 for (i = 0; i < BNX2X_MAX_RX_DESC_CNT; i++, rxbd++) {
4715 dma_addr_t buf_map;
4716 int n = (i % cp->l2_rx_ring_size) + 1;
4717
Michael Chancd801532010-10-13 14:06:49 +00004718 buf_map = udev->l2_buf_map + (n * cp->l2_single_buf_size);
Michael Chan71034ba2009-10-10 13:46:59 +00004719 rxbd->addr_hi = cpu_to_le32((u64) buf_map >> 32);
4720 rxbd->addr_lo = cpu_to_le32(buf_map & 0xffffffff);
4721 }
Michael Chan71034ba2009-10-10 13:46:59 +00004722
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004723 val = (u64) (ring_map + BCM_PAGE_SIZE) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004724 rxbd->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004725 data->rx.bd_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004726
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004727 val = (u64) (ring_map + BCM_PAGE_SIZE) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004728 rxbd->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004729 data->rx.bd_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004730
4731 rxcqe += BNX2X_MAX_RCQ_DESC_CNT;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004732 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) >> 32;
Michael Chan71034ba2009-10-10 13:46:59 +00004733 rxcqe->addr_hi = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004734 data->rx.cqe_page_base.hi = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004735
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004736 val = (u64) (ring_map + (2 * BCM_PAGE_SIZE)) & 0xffffffff;
Michael Chan71034ba2009-10-10 13:46:59 +00004737 rxcqe->addr_lo = cpu_to_le32(val);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004738 data->rx.cqe_page_base.lo = cpu_to_le32(val);
Michael Chan71034ba2009-10-10 13:46:59 +00004739
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004740 /* Other ramrod params */
4741 data->rx.client_qzone_id = cl_qzone_id;
4742 data->rx.rx_sb_index_number = HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS;
4743 data->rx.status_block_id = BNX2X_DEF_SB_ID;
Michael Chan71034ba2009-10-10 13:46:59 +00004744
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004745 data->rx.cache_line_alignment_log_size = L1_CACHE_SHIFT;
4746 data->rx.bd_buff_size = cpu_to_le16(cp->l2_single_buf_size);
Michael Chan71034ba2009-10-10 13:46:59 +00004747
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004748 data->rx.mtu = cpu_to_le16(cp->l2_single_buf_size - 14);
4749 data->rx.outer_vlan_removal_enable_flg = 1;
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004750
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004751 /* reset tstorm and ustorm per client statistics */
4752 if (cli < MAX_STAT_COUNTER_ID) {
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004753 val = BAR_TSTRORM_INTMEM +
4754 TSTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4755 for (i = 0; i < sizeof(struct tstorm_per_client_stats) / 4; i++)
4756 CNIC_WR(dev, val + i * 4, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004757
Dmitry Kravkov6b2a5412010-06-23 11:57:09 -07004758 val = BAR_USTRORM_INTMEM +
4759 USTORM_PER_COUNTER_ID_STATS_OFFSET(port, cli);
4760 for (i = 0; i < sizeof(struct ustorm_per_client_stats) / 4; i++)
4761 CNIC_WR(dev, val + i * 4, 0);
4762 }
Michael Chan71034ba2009-10-10 13:46:59 +00004763
4764 cp->rx_cons_ptr =
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004765 &sb->sp_sb.index_values[HC_SP_INDEX_ETH_ISCSI_RX_CQ_CONS];
Michael Chan5159fdc2010-12-23 07:42:59 +00004766 cp->rx_cons = *cp->rx_cons_ptr;
Michael Chan71034ba2009-10-10 13:46:59 +00004767}
4768
Michael Chane21ba412010-12-23 07:43:03 +00004769static void cnic_init_bnx2x_kcq(struct cnic_dev *dev)
4770{
4771 struct cnic_local *cp = dev->cnic_priv;
4772 u32 pfid = cp->pfid;
4773
4774 cp->kcq1.io_addr = BAR_CSTRORM_INTMEM +
4775 CSTORM_ISCSI_EQ_PROD_OFFSET(pfid, 0);
4776 cp->kcq1.sw_prod_idx = 0;
4777
4778 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4779 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4780
4781 cp->kcq1.hw_prod_idx_ptr =
4782 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4783 cp->kcq1.status_idx_ptr =
4784 &sb->sb.running_index[SM_RX_ID];
4785 } else {
4786 struct host_hc_status_block_e1x *sb = cp->status_blk.gen;
4787
4788 cp->kcq1.hw_prod_idx_ptr =
4789 &sb->sb.index_values[HC_INDEX_ISCSI_EQ_CONS];
4790 cp->kcq1.status_idx_ptr =
4791 &sb->sb.running_index[SM_RX_ID];
4792 }
4793
4794 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4795 struct host_hc_status_block_e2 *sb = cp->status_blk.gen;
4796
4797 cp->kcq2.io_addr = BAR_USTRORM_INTMEM +
4798 USTORM_FCOE_EQ_PROD_OFFSET(pfid);
4799 cp->kcq2.sw_prod_idx = 0;
4800 cp->kcq2.hw_prod_idx_ptr =
4801 &sb->sb.index_values[HC_INDEX_FCOE_EQ_CONS];
4802 cp->kcq2.status_idx_ptr =
4803 &sb->sb.running_index[SM_RX_ID];
4804 }
4805}
4806
Michael Chan71034ba2009-10-10 13:46:59 +00004807static int cnic_start_bnx2x_hw(struct cnic_dev *dev)
4808{
4809 struct cnic_local *cp = dev->cnic_priv;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004810 struct cnic_eth_dev *ethdev = cp->ethdev;
Michael Chan71034ba2009-10-10 13:46:59 +00004811 int func = CNIC_FUNC(cp), ret, i;
Michael Chan14203982010-10-06 03:16:06 +00004812 u32 pfid;
Michael Chan71034ba2009-10-10 13:46:59 +00004813
Michael Chanee87a822010-10-13 14:06:51 +00004814 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4815 u32 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN_OVWR);
4816
4817 if (!(val & 1))
4818 val = CNIC_RD(dev, MISC_REG_PORT4MODE_EN);
4819 else
4820 val = (val >> 1) & 1;
4821
4822 if (val)
4823 cp->pfid = func >> 1;
4824 else
4825 cp->pfid = func & 0x6;
4826 } else {
4827 cp->pfid = func;
4828 }
Michael Chan14203982010-10-06 03:16:06 +00004829 pfid = cp->pfid;
4830
Michael Chan71034ba2009-10-10 13:46:59 +00004831 ret = cnic_init_id_tbl(&cp->cid_tbl, MAX_ISCSI_TBL_SZ,
Eddie Wai11f23aa2011-06-08 19:29:34 +00004832 cp->iscsi_start_cid, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004833
4834 if (ret)
4835 return -ENOMEM;
4836
Michael Chane1928c82010-12-23 07:43:04 +00004837 if (BNX2X_CHIP_IS_E2(cp->chip_id)) {
4838 ret = cnic_init_id_tbl(&cp->fcoe_cid_tbl,
4839 BNX2X_FCOE_NUM_CONNECTIONS,
Eddie Wai11f23aa2011-06-08 19:29:34 +00004840 cp->fcoe_start_cid, 0);
Michael Chane1928c82010-12-23 07:43:04 +00004841
4842 if (ret)
4843 return -ENOMEM;
4844 }
4845
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004846 cp->bnx2x_igu_sb_id = ethdev->irq_arr[0].status_blk_num2;
4847
Michael Chane21ba412010-12-23 07:43:03 +00004848 cnic_init_bnx2x_kcq(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00004849
Michael Chan71034ba2009-10-10 13:46:59 +00004850 /* Only 1 EQ */
Michael Chane6c28892010-06-24 14:58:39 +00004851 CNIC_WR16(dev, cp->kcq1.io_addr, MAX_KCQ_IDX);
Michael Chan71034ba2009-10-10 13:46:59 +00004852 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004853 CSTORM_ISCSI_EQ_CONS_OFFSET(pfid, 0), 0);
Michael Chan71034ba2009-10-10 13:46:59 +00004854 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004855 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00004856 cp->kcq1.dma.pg_map_arr[1] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00004857 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004858 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00004859 (u64) cp->kcq1.dma.pg_map_arr[1] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00004860 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004861 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0),
Michael Chane6c28892010-06-24 14:58:39 +00004862 cp->kcq1.dma.pg_map_arr[0] & 0xffffffff);
Michael Chan71034ba2009-10-10 13:46:59 +00004863 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004864 CSTORM_ISCSI_EQ_NEXT_EQE_ADDR_OFFSET(pfid, 0) + 4,
Michael Chane6c28892010-06-24 14:58:39 +00004865 (u64) cp->kcq1.dma.pg_map_arr[0] >> 32);
Michael Chan71034ba2009-10-10 13:46:59 +00004866 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004867 CSTORM_ISCSI_EQ_NEXT_PAGE_ADDR_VALID_OFFSET(pfid, 0), 1);
Michael Chan71034ba2009-10-10 13:46:59 +00004868 CNIC_WR16(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004869 CSTORM_ISCSI_EQ_SB_NUM_OFFSET(pfid, 0), cp->status_blk_num);
Michael Chan71034ba2009-10-10 13:46:59 +00004870 CNIC_WR8(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004871 CSTORM_ISCSI_EQ_SB_INDEX_OFFSET(pfid, 0),
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004872 HC_INDEX_ISCSI_EQ_CONS);
Michael Chan71034ba2009-10-10 13:46:59 +00004873
4874 for (i = 0; i < cp->conn_buf_info.num_pages; i++) {
4875 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004876 TSTORM_ISCSI_CONN_BUF_PBL_OFFSET(pfid, i),
Michael Chan71034ba2009-10-10 13:46:59 +00004877 cp->conn_buf_info.pgtbl[2 * i]);
4878 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004879 TSTORM_ISCSI_CONN_BUF_PBL_OFFSET(pfid, i) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00004880 cp->conn_buf_info.pgtbl[(2 * i) + 1]);
4881 }
4882
4883 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004884 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid),
Michael Chan71034ba2009-10-10 13:46:59 +00004885 cp->gbl_buf_info.pg_map_arr[0] & 0xffffffff);
4886 CNIC_WR(dev, BAR_USTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00004887 USTORM_ISCSI_GLOBAL_BUF_PHYS_ADDR_OFFSET(pfid) + 4,
Michael Chan71034ba2009-10-10 13:46:59 +00004888 (u64) cp->gbl_buf_info.pg_map_arr[0] >> 32);
4889
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004890 CNIC_WR(dev, BAR_TSTRORM_INTMEM +
4891 TSTORM_ISCSI_TCP_LOCAL_ADV_WND_OFFSET(pfid), DEF_RCV_BUF);
4892
Michael Chan71034ba2009-10-10 13:46:59 +00004893 cnic_setup_bnx2x_context(dev);
4894
Michael Chan71034ba2009-10-10 13:46:59 +00004895 ret = cnic_init_bnx2x_irq(dev);
4896 if (ret)
4897 return ret;
4898
Michael Chan71034ba2009-10-10 13:46:59 +00004899 return 0;
4900}
4901
Michael Chan86b53602009-10-10 13:46:57 +00004902static void cnic_init_rings(struct cnic_dev *dev)
4903{
Michael Chan541a7812010-10-06 03:17:22 +00004904 struct cnic_local *cp = dev->cnic_priv;
Michael Chancd801532010-10-13 14:06:49 +00004905 struct cnic_uio_dev *udev = cp->udev;
Michael Chan541a7812010-10-06 03:17:22 +00004906
4907 if (test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
4908 return;
4909
Michael Chan86b53602009-10-10 13:46:57 +00004910 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
4911 cnic_init_bnx2_tx_ring(dev);
4912 cnic_init_bnx2_rx_ring(dev);
Michael Chan541a7812010-10-06 03:17:22 +00004913 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chan71034ba2009-10-10 13:46:59 +00004914 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
Michael Chan5159fdc2010-12-23 07:42:59 +00004915 u32 cli = cp->ethdev->iscsi_l2_client_id;
4916 u32 cid = cp->ethdev->iscsi_l2_cid;
Michael Chan68d7c1a2011-01-05 15:14:13 +00004917 u32 cl_qzone_id;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004918 struct client_init_ramrod_data *data;
Michael Chan71034ba2009-10-10 13:46:59 +00004919 union l5cm_specific_data l5_data;
4920 struct ustorm_eth_rx_producers rx_prods = {0};
Michael Chanc7596b72009-12-02 15:15:35 +00004921 u32 off, i;
Michael Chan71034ba2009-10-10 13:46:59 +00004922
4923 rx_prods.bd_prod = 0;
4924 rx_prods.cqe_prod = BNX2X_MAX_RCQ_DESC_CNT;
4925 barrier();
4926
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004927 cl_qzone_id = BNX2X_CL_QZONE_ID(cp, cli);
4928
Michael Chanc7596b72009-12-02 15:15:35 +00004929 off = BAR_USTRORM_INTMEM +
Michael Chanee87a822010-10-13 14:06:51 +00004930 (BNX2X_CHIP_IS_E2(cp->chip_id) ?
4931 USTORM_RX_PRODS_E2_OFFSET(cl_qzone_id) :
4932 USTORM_RX_PRODS_E1X_OFFSET(CNIC_PORT(cp), cli));
Michael Chan71034ba2009-10-10 13:46:59 +00004933
4934 for (i = 0; i < sizeof(struct ustorm_eth_rx_producers) / 4; i++)
Michael Chanc7596b72009-12-02 15:15:35 +00004935 CNIC_WR(dev, off + i * 4, ((u32 *) &rx_prods)[i]);
Michael Chan71034ba2009-10-10 13:46:59 +00004936
Michael Chan48f753d2010-05-18 11:32:53 +00004937 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
4938
Michael Chancd801532010-10-13 14:06:49 +00004939 data = udev->l2_buf;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004940
4941 memset(data, 0, sizeof(*data));
4942
4943 cnic_init_bnx2x_tx_ring(dev, data);
4944 cnic_init_bnx2x_rx_ring(dev, data);
4945
Michael Chancd801532010-10-13 14:06:49 +00004946 l5_data.phy_address.lo = udev->l2_buf_map & 0xffffffff;
4947 l5_data.phy_address.hi = (u64) udev->l2_buf_map >> 32;
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004948
Michael Chan541a7812010-10-06 03:17:22 +00004949 set_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
4950
Michael Chan71034ba2009-10-10 13:46:59 +00004951 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_CLIENT_SETUP,
Michael Chan68d7c1a2011-01-05 15:14:13 +00004952 cid, ETH_CONNECTION_TYPE, &l5_data);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00004953
Michael Chan48f753d2010-05-18 11:32:53 +00004954 i = 0;
4955 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
4956 ++i < 10)
4957 msleep(1);
4958
4959 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
4960 netdev_err(dev->netdev,
4961 "iSCSI CLIENT_SETUP did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00004962 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan5159fdc2010-12-23 07:42:59 +00004963 cnic_ring_ctl(dev, cid, cli, 1);
Michael Chan86b53602009-10-10 13:46:57 +00004964 }
4965}
4966
4967static void cnic_shutdown_rings(struct cnic_dev *dev)
4968{
Michael Chan541a7812010-10-06 03:17:22 +00004969 struct cnic_local *cp = dev->cnic_priv;
4970
4971 if (!test_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags))
4972 return;
4973
Michael Chan86b53602009-10-10 13:46:57 +00004974 if (test_bit(CNIC_F_BNX2_CLASS, &dev->flags)) {
4975 cnic_shutdown_bnx2_rx_ring(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00004976 } else if (test_bit(CNIC_F_BNX2X_CLASS, &dev->flags)) {
4977 struct cnic_local *cp = dev->cnic_priv;
Michael Chan5159fdc2010-12-23 07:42:59 +00004978 u32 cli = cp->ethdev->iscsi_l2_client_id;
4979 u32 cid = cp->ethdev->iscsi_l2_cid;
Michael Chan8b065b62009-12-02 15:15:36 +00004980 union l5cm_specific_data l5_data;
Michael Chan48f753d2010-05-18 11:32:53 +00004981 int i;
Michael Chan71034ba2009-10-10 13:46:59 +00004982
Michael Chan5159fdc2010-12-23 07:42:59 +00004983 cnic_ring_ctl(dev, cid, cli, 0);
Michael Chan8b065b62009-12-02 15:15:36 +00004984
Michael Chan48f753d2010-05-18 11:32:53 +00004985 set_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags);
4986
Michael Chan8b065b62009-12-02 15:15:36 +00004987 l5_data.phy_address.lo = cli;
4988 l5_data.phy_address.hi = 0;
4989 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_ETH_HALT,
Michael Chan5159fdc2010-12-23 07:42:59 +00004990 cid, ETH_CONNECTION_TYPE, &l5_data);
Michael Chan48f753d2010-05-18 11:32:53 +00004991 i = 0;
4992 while (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags) &&
4993 ++i < 10)
4994 msleep(1);
4995
4996 if (test_bit(CNIC_LCL_FL_L2_WAIT, &cp->cnic_local_flags))
4997 netdev_err(dev->netdev,
4998 "iSCSI CLIENT_HALT did not complete\n");
Dmitry Kravkovc2bff632010-10-06 03:33:18 +00004999 cnic_spq_completion(dev, DRV_CTL_RET_L2_SPQ_CREDIT_CMD, 1);
Michael Chan1bcdc322009-12-10 15:40:57 +00005000
5001 memset(&l5_data, 0, sizeof(l5_data));
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005002 cnic_submit_kwqe_16(dev, RAMROD_CMD_ID_COMMON_CFC_DEL,
Michael Chan68d7c1a2011-01-05 15:14:13 +00005003 cid, NONE_CONNECTION_TYPE, &l5_data);
Michael Chan1bcdc322009-12-10 15:40:57 +00005004 msleep(10);
Michael Chan86b53602009-10-10 13:46:57 +00005005 }
Michael Chan541a7812010-10-06 03:17:22 +00005006 clear_bit(CNIC_LCL_FL_RINGS_INITED, &cp->cnic_local_flags);
Michael Chan86b53602009-10-10 13:46:57 +00005007}
5008
Michael Chana3059b12009-08-14 15:49:44 +00005009static int cnic_register_netdev(struct cnic_dev *dev)
5010{
5011 struct cnic_local *cp = dev->cnic_priv;
5012 struct cnic_eth_dev *ethdev = cp->ethdev;
5013 int err;
5014
5015 if (!ethdev)
5016 return -ENODEV;
5017
5018 if (ethdev->drv_state & CNIC_DRV_STATE_REGD)
5019 return 0;
5020
5021 err = ethdev->drv_register_cnic(dev->netdev, cp->cnic_ops, dev);
5022 if (err)
Joe Perchesddf79b22010-02-17 15:01:54 +00005023 netdev_err(dev->netdev, "register_cnic failed\n");
Michael Chana3059b12009-08-14 15:49:44 +00005024
5025 return err;
5026}
5027
5028static void cnic_unregister_netdev(struct cnic_dev *dev)
5029{
5030 struct cnic_local *cp = dev->cnic_priv;
5031 struct cnic_eth_dev *ethdev = cp->ethdev;
5032
5033 if (!ethdev)
5034 return;
5035
5036 ethdev->drv_unregister_cnic(dev->netdev);
5037}
5038
Michael Chana4636962009-06-08 18:14:43 -07005039static int cnic_start_hw(struct cnic_dev *dev)
5040{
5041 struct cnic_local *cp = dev->cnic_priv;
5042 struct cnic_eth_dev *ethdev = cp->ethdev;
5043 int err;
5044
5045 if (test_bit(CNIC_F_CNIC_UP, &dev->flags))
5046 return -EALREADY;
5047
Michael Chana4636962009-06-08 18:14:43 -07005048 dev->regview = ethdev->io_base;
Michael Chana4636962009-06-08 18:14:43 -07005049 pci_dev_get(dev->pcidev);
5050 cp->func = PCI_FUNC(dev->pcidev->devfn);
Michael Chana4dde3a2010-02-24 14:42:08 +00005051 cp->status_blk.gen = ethdev->irq_arr[0].status_blk;
Michael Chana4636962009-06-08 18:14:43 -07005052 cp->status_blk_num = ethdev->irq_arr[0].status_blk_num;
5053
5054 err = cp->alloc_resc(dev);
5055 if (err) {
Joe Perchesddf79b22010-02-17 15:01:54 +00005056 netdev_err(dev->netdev, "allocate resource failure\n");
Michael Chana4636962009-06-08 18:14:43 -07005057 goto err1;
5058 }
5059
5060 err = cp->start_hw(dev);
5061 if (err)
5062 goto err1;
5063
5064 err = cnic_cm_open(dev);
5065 if (err)
5066 goto err1;
5067
5068 set_bit(CNIC_F_CNIC_UP, &dev->flags);
5069
5070 cp->enable_int(dev);
5071
5072 return 0;
5073
5074err1:
Michael Chana4636962009-06-08 18:14:43 -07005075 cp->free_resc(dev);
5076 pci_dev_put(dev->pcidev);
Michael Chana4636962009-06-08 18:14:43 -07005077 return err;
5078}
5079
5080static void cnic_stop_bnx2_hw(struct cnic_dev *dev)
5081{
Michael Chana4636962009-06-08 18:14:43 -07005082 cnic_disable_bnx2_int_sync(dev);
5083
5084 cnic_reg_wr_ind(dev, BNX2_CP_SCRATCH + 0x20, 0);
5085 cnic_reg_wr_ind(dev, BNX2_COM_SCRATCH + 0x20, 0);
5086
5087 cnic_init_context(dev, KWQ_CID);
5088 cnic_init_context(dev, KCQ_CID);
5089
5090 cnic_setup_5709_context(dev, 0);
5091 cnic_free_irq(dev);
5092
Michael Chana4636962009-06-08 18:14:43 -07005093 cnic_free_resc(dev);
5094}
5095
Michael Chan71034ba2009-10-10 13:46:59 +00005096
5097static void cnic_stop_bnx2x_hw(struct cnic_dev *dev)
5098{
5099 struct cnic_local *cp = dev->cnic_priv;
Michael Chan71034ba2009-10-10 13:46:59 +00005100
5101 cnic_free_irq(dev);
Dmitry Kravkov523224a2010-10-06 03:23:26 +00005102 *cp->kcq1.hw_prod_idx_ptr = 0;
Michael Chan4e9c4fd2009-12-10 15:40:58 +00005103 CNIC_WR(dev, BAR_CSTRORM_INTMEM +
Michael Chan14203982010-10-06 03:16:06 +00005104 CSTORM_ISCSI_EQ_CONS_OFFSET(cp->pfid, 0), 0);
Michael Chane6c28892010-06-24 14:58:39 +00005105 CNIC_WR16(dev, cp->kcq1.io_addr, 0);
Michael Chan71034ba2009-10-10 13:46:59 +00005106 cnic_free_resc(dev);
5107}
5108
Michael Chana4636962009-06-08 18:14:43 -07005109static void cnic_stop_hw(struct cnic_dev *dev)
5110{
5111 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
5112 struct cnic_local *cp = dev->cnic_priv;
Michael Chan48f753d2010-05-18 11:32:53 +00005113 int i = 0;
Michael Chana4636962009-06-08 18:14:43 -07005114
Michael Chan48f753d2010-05-18 11:32:53 +00005115 /* Need to wait for the ring shutdown event to complete
5116 * before clearing the CNIC_UP flag.
5117 */
Michael Chancd801532010-10-13 14:06:49 +00005118 while (cp->udev->uio_dev != -1 && i < 15) {
Michael Chan48f753d2010-05-18 11:32:53 +00005119 msleep(100);
5120 i++;
5121 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005122 cnic_shutdown_rings(dev);
Michael Chana4636962009-06-08 18:14:43 -07005123 clear_bit(CNIC_F_CNIC_UP, &dev->flags);
5124 rcu_assign_pointer(cp->ulp_ops[CNIC_ULP_L4], NULL);
5125 synchronize_rcu();
5126 cnic_cm_shutdown(dev);
5127 cp->stop_hw(dev);
5128 pci_dev_put(dev->pcidev);
5129 }
5130}
5131
5132static void cnic_free_dev(struct cnic_dev *dev)
5133{
5134 int i = 0;
5135
5136 while ((atomic_read(&dev->ref_count) != 0) && i < 10) {
5137 msleep(100);
5138 i++;
5139 }
5140 if (atomic_read(&dev->ref_count) != 0)
Joe Perchesddf79b22010-02-17 15:01:54 +00005141 netdev_err(dev->netdev, "Failed waiting for ref count to go to zero\n");
Michael Chana4636962009-06-08 18:14:43 -07005142
Joe Perchesddf79b22010-02-17 15:01:54 +00005143 netdev_info(dev->netdev, "Removed CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07005144 dev_put(dev->netdev);
5145 kfree(dev);
5146}
5147
5148static struct cnic_dev *cnic_alloc_dev(struct net_device *dev,
5149 struct pci_dev *pdev)
5150{
5151 struct cnic_dev *cdev;
5152 struct cnic_local *cp;
5153 int alloc_size;
5154
5155 alloc_size = sizeof(struct cnic_dev) + sizeof(struct cnic_local);
5156
5157 cdev = kzalloc(alloc_size , GFP_KERNEL);
5158 if (cdev == NULL) {
Joe Perchesddf79b22010-02-17 15:01:54 +00005159 netdev_err(dev, "allocate dev struct failure\n");
Michael Chana4636962009-06-08 18:14:43 -07005160 return NULL;
5161 }
5162
5163 cdev->netdev = dev;
5164 cdev->cnic_priv = (char *)cdev + sizeof(struct cnic_dev);
5165 cdev->register_device = cnic_register_device;
5166 cdev->unregister_device = cnic_unregister_device;
5167 cdev->iscsi_nl_msg_recv = cnic_iscsi_nl_msg_recv;
5168
5169 cp = cdev->cnic_priv;
5170 cp->dev = cdev;
Michael Chana4636962009-06-08 18:14:43 -07005171 cp->l2_single_buf_size = 0x400;
5172 cp->l2_rx_ring_size = 3;
5173
5174 spin_lock_init(&cp->cnic_ulp_lock);
5175
Joe Perchesddf79b22010-02-17 15:01:54 +00005176 netdev_info(dev, "Added CNIC device\n");
Michael Chana4636962009-06-08 18:14:43 -07005177
5178 return cdev;
5179}
5180
5181static struct cnic_dev *init_bnx2_cnic(struct net_device *dev)
5182{
5183 struct pci_dev *pdev;
5184 struct cnic_dev *cdev;
5185 struct cnic_local *cp;
5186 struct cnic_eth_dev *ethdev = NULL;
Michael Chane2ee3612009-06-13 17:43:02 -07005187 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
Michael Chana4636962009-06-08 18:14:43 -07005188
Michael Chane2ee3612009-06-13 17:43:02 -07005189 probe = symbol_get(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07005190 if (probe) {
5191 ethdev = (*probe)(dev);
Michael Chan64c64602009-08-14 15:49:43 +00005192 symbol_put(bnx2_cnic_probe);
Michael Chana4636962009-06-08 18:14:43 -07005193 }
5194 if (!ethdev)
5195 return NULL;
5196
5197 pdev = ethdev->pdev;
5198 if (!pdev)
5199 return NULL;
5200
5201 dev_hold(dev);
5202 pci_dev_get(pdev);
Sergei Shtylyovff938e42011-02-28 11:57:33 -08005203 if ((pdev->device == PCI_DEVICE_ID_NX2_5709 ||
5204 pdev->device == PCI_DEVICE_ID_NX2_5709S) &&
5205 (pdev->revision < 0x10)) {
5206 pci_dev_put(pdev);
5207 goto cnic_err;
Michael Chana4636962009-06-08 18:14:43 -07005208 }
5209 pci_dev_put(pdev);
5210
5211 cdev = cnic_alloc_dev(dev, pdev);
5212 if (cdev == NULL)
5213 goto cnic_err;
5214
5215 set_bit(CNIC_F_BNX2_CLASS, &cdev->flags);
5216 cdev->submit_kwqes = cnic_submit_bnx2_kwqes;
5217
5218 cp = cdev->cnic_priv;
5219 cp->ethdev = ethdev;
5220 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00005221 cp->chip_id = ethdev->chip_id;
Michael Chana4636962009-06-08 18:14:43 -07005222
Michael Chan7625eb22011-06-08 19:29:36 +00005223 cdev->max_iscsi_conn = ethdev->max_iscsi_conn;
5224
Michael Chana4636962009-06-08 18:14:43 -07005225 cp->cnic_ops = &cnic_bnx2_ops;
5226 cp->start_hw = cnic_start_bnx2_hw;
5227 cp->stop_hw = cnic_stop_bnx2_hw;
5228 cp->setup_pgtbl = cnic_setup_page_tbl;
5229 cp->alloc_resc = cnic_alloc_bnx2_resc;
5230 cp->free_resc = cnic_free_resc;
5231 cp->start_cm = cnic_cm_init_bnx2_hw;
5232 cp->stop_cm = cnic_cm_stop_bnx2_hw;
5233 cp->enable_int = cnic_enable_bnx2_int;
5234 cp->disable_int_sync = cnic_disable_bnx2_int_sync;
5235 cp->close_conn = cnic_close_bnx2_conn;
Michael Chana4636962009-06-08 18:14:43 -07005236 return cdev;
5237
5238cnic_err:
5239 dev_put(dev);
5240 return NULL;
5241}
5242
Michael Chan71034ba2009-10-10 13:46:59 +00005243static struct cnic_dev *init_bnx2x_cnic(struct net_device *dev)
5244{
5245 struct pci_dev *pdev;
5246 struct cnic_dev *cdev;
5247 struct cnic_local *cp;
5248 struct cnic_eth_dev *ethdev = NULL;
5249 struct cnic_eth_dev *(*probe)(struct net_device *) = NULL;
5250
5251 probe = symbol_get(bnx2x_cnic_probe);
5252 if (probe) {
5253 ethdev = (*probe)(dev);
5254 symbol_put(bnx2x_cnic_probe);
5255 }
5256 if (!ethdev)
5257 return NULL;
5258
5259 pdev = ethdev->pdev;
5260 if (!pdev)
5261 return NULL;
5262
5263 dev_hold(dev);
5264 cdev = cnic_alloc_dev(dev, pdev);
5265 if (cdev == NULL) {
5266 dev_put(dev);
5267 return NULL;
5268 }
5269
5270 set_bit(CNIC_F_BNX2X_CLASS, &cdev->flags);
5271 cdev->submit_kwqes = cnic_submit_bnx2x_kwqes;
5272
5273 cp = cdev->cnic_priv;
5274 cp->ethdev = ethdev;
5275 cdev->pcidev = pdev;
Michael Chanee87a822010-10-13 14:06:51 +00005276 cp->chip_id = ethdev->chip_id;
Michael Chan71034ba2009-10-10 13:46:59 +00005277
Vladislav Zolotarov2ba45142011-01-31 14:39:17 +00005278 if (!(ethdev->drv_state & CNIC_DRV_STATE_NO_ISCSI))
5279 cdev->max_iscsi_conn = ethdev->max_iscsi_conn;
5280 if (BNX2X_CHIP_IS_E2(cp->chip_id) &&
5281 !(ethdev->drv_state & CNIC_DRV_STATE_NO_FCOE))
5282 cdev->max_fcoe_conn = ethdev->max_fcoe_conn;
5283
5284 memcpy(cdev->mac_addr, ethdev->iscsi_mac, 6);
5285
Michael Chan71034ba2009-10-10 13:46:59 +00005286 cp->cnic_ops = &cnic_bnx2x_ops;
5287 cp->start_hw = cnic_start_bnx2x_hw;
5288 cp->stop_hw = cnic_stop_bnx2x_hw;
5289 cp->setup_pgtbl = cnic_setup_page_tbl_le;
5290 cp->alloc_resc = cnic_alloc_bnx2x_resc;
5291 cp->free_resc = cnic_free_resc;
5292 cp->start_cm = cnic_cm_init_bnx2x_hw;
5293 cp->stop_cm = cnic_cm_stop_bnx2x_hw;
5294 cp->enable_int = cnic_enable_bnx2x_int;
5295 cp->disable_int_sync = cnic_disable_bnx2x_int_sync;
Michael Chanee87a822010-10-13 14:06:51 +00005296 if (BNX2X_CHIP_IS_E2(cp->chip_id))
5297 cp->ack_int = cnic_ack_bnx2x_e2_msix;
5298 else
5299 cp->ack_int = cnic_ack_bnx2x_msix;
Michael Chan71034ba2009-10-10 13:46:59 +00005300 cp->close_conn = cnic_close_bnx2x_conn;
Michael Chan71034ba2009-10-10 13:46:59 +00005301 return cdev;
5302}
5303
Michael Chana4636962009-06-08 18:14:43 -07005304static struct cnic_dev *is_cnic_dev(struct net_device *dev)
5305{
5306 struct ethtool_drvinfo drvinfo;
5307 struct cnic_dev *cdev = NULL;
5308
5309 if (dev->ethtool_ops && dev->ethtool_ops->get_drvinfo) {
5310 memset(&drvinfo, 0, sizeof(drvinfo));
5311 dev->ethtool_ops->get_drvinfo(dev, &drvinfo);
5312
5313 if (!strcmp(drvinfo.driver, "bnx2"))
5314 cdev = init_bnx2_cnic(dev);
Michael Chan71034ba2009-10-10 13:46:59 +00005315 if (!strcmp(drvinfo.driver, "bnx2x"))
5316 cdev = init_bnx2x_cnic(dev);
Michael Chana4636962009-06-08 18:14:43 -07005317 if (cdev) {
5318 write_lock(&cnic_dev_lock);
5319 list_add(&cdev->list, &cnic_dev_list);
5320 write_unlock(&cnic_dev_lock);
5321 }
5322 }
5323 return cdev;
5324}
5325
5326/**
5327 * netdev event handler
5328 */
5329static int cnic_netdev_event(struct notifier_block *this, unsigned long event,
5330 void *ptr)
5331{
5332 struct net_device *netdev = ptr;
5333 struct cnic_dev *dev;
5334 int if_type;
5335 int new_dev = 0;
5336
5337 dev = cnic_from_netdev(netdev);
5338
Michael Chandb1d3502011-06-08 19:29:35 +00005339 if (!dev && (event == NETDEV_REGISTER || netif_running(netdev))) {
Michael Chana4636962009-06-08 18:14:43 -07005340 /* Check for the hot-plug device */
5341 dev = is_cnic_dev(netdev);
5342 if (dev) {
5343 new_dev = 1;
5344 cnic_hold(dev);
5345 }
5346 }
5347 if (dev) {
5348 struct cnic_local *cp = dev->cnic_priv;
5349
5350 if (new_dev)
5351 cnic_ulp_init(dev);
5352 else if (event == NETDEV_UNREGISTER)
5353 cnic_ulp_exit(dev);
Michael Chan6053bbf2009-10-02 11:03:28 -07005354
Michael Chandb1d3502011-06-08 19:29:35 +00005355 if (event == NETDEV_UP || (new_dev && netif_running(netdev))) {
Michael Chana3059b12009-08-14 15:49:44 +00005356 if (cnic_register_netdev(dev) != 0) {
5357 cnic_put(dev);
5358 goto done;
5359 }
Michael Chana4636962009-06-08 18:14:43 -07005360 if (!cnic_start_hw(dev))
5361 cnic_ulp_start(dev);
Michael Chana4636962009-06-08 18:14:43 -07005362 }
5363
5364 rcu_read_lock();
5365 for (if_type = 0; if_type < MAX_CNIC_ULP_TYPE; if_type++) {
5366 struct cnic_ulp_ops *ulp_ops;
5367 void *ctx;
5368
5369 ulp_ops = rcu_dereference(cp->ulp_ops[if_type]);
5370 if (!ulp_ops || !ulp_ops->indicate_netevent)
5371 continue;
5372
5373 ctx = cp->ulp_handle[if_type];
5374
5375 ulp_ops->indicate_netevent(ctx, event);
5376 }
5377 rcu_read_unlock();
5378
5379 if (event == NETDEV_GOING_DOWN) {
Michael Chana4636962009-06-08 18:14:43 -07005380 cnic_ulp_stop(dev);
5381 cnic_stop_hw(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005382 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005383 } else if (event == NETDEV_UNREGISTER) {
5384 write_lock(&cnic_dev_lock);
5385 list_del_init(&dev->list);
5386 write_unlock(&cnic_dev_lock);
5387
5388 cnic_put(dev);
5389 cnic_free_dev(dev);
5390 goto done;
5391 }
5392 cnic_put(dev);
5393 }
5394done:
5395 return NOTIFY_DONE;
5396}
5397
5398static struct notifier_block cnic_netdev_notifier = {
5399 .notifier_call = cnic_netdev_event
5400};
5401
5402static void cnic_release(void)
5403{
5404 struct cnic_dev *dev;
Michael Chana3ceeeb2010-10-13 14:06:50 +00005405 struct cnic_uio_dev *udev;
Michael Chana4636962009-06-08 18:14:43 -07005406
5407 while (!list_empty(&cnic_dev_list)) {
5408 dev = list_entry(cnic_dev_list.next, struct cnic_dev, list);
5409 if (test_bit(CNIC_F_CNIC_UP, &dev->flags)) {
5410 cnic_ulp_stop(dev);
5411 cnic_stop_hw(dev);
5412 }
5413
5414 cnic_ulp_exit(dev);
Michael Chana3059b12009-08-14 15:49:44 +00005415 cnic_unregister_netdev(dev);
Michael Chana4636962009-06-08 18:14:43 -07005416 list_del_init(&dev->list);
5417 cnic_free_dev(dev);
5418 }
Michael Chana3ceeeb2010-10-13 14:06:50 +00005419 while (!list_empty(&cnic_udev_list)) {
5420 udev = list_entry(cnic_udev_list.next, struct cnic_uio_dev,
5421 list);
5422 cnic_free_uio(udev);
5423 }
Michael Chana4636962009-06-08 18:14:43 -07005424}
5425
5426static int __init cnic_init(void)
5427{
5428 int rc = 0;
5429
Joe Perchesddf79b22010-02-17 15:01:54 +00005430 pr_info("%s", version);
Michael Chana4636962009-06-08 18:14:43 -07005431
5432 rc = register_netdevice_notifier(&cnic_netdev_notifier);
5433 if (rc) {
5434 cnic_release();
5435 return rc;
5436 }
5437
Michael Chanfdf24082010-10-13 14:06:47 +00005438 cnic_wq = create_singlethread_workqueue("cnic_wq");
5439 if (!cnic_wq) {
5440 cnic_release();
5441 unregister_netdevice_notifier(&cnic_netdev_notifier);
5442 return -ENOMEM;
5443 }
5444
Michael Chana4636962009-06-08 18:14:43 -07005445 return 0;
5446}
5447
5448static void __exit cnic_exit(void)
5449{
5450 unregister_netdevice_notifier(&cnic_netdev_notifier);
5451 cnic_release();
Michael Chanfdf24082010-10-13 14:06:47 +00005452 destroy_workqueue(cnic_wq);
Michael Chana4636962009-06-08 18:14:43 -07005453}
5454
5455module_init(cnic_init);
5456module_exit(cnic_exit);